nau8821.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // nau8821.c -- Nuvoton NAU88L21 audio codec driver
  4. //
  5. // Copyright 2021 Nuvoton Technology Corp.
  6. // Author: John Hsu <[email protected]>
  7. // Co-author: Seven Lee <[email protected]>
  8. //
  9. #include <linux/acpi.h>
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/dmi.h>
  13. #include <linux/init.h>
  14. #include <linux/i2c.h>
  15. #include <linux/module.h>
  16. #include <linux/math64.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. #include <sound/core.h>
  20. #include <sound/initval.h>
  21. #include <sound/jack.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/tlv.h>
  26. #include "nau8821.h"
  27. #define NAU8821_JD_ACTIVE_HIGH BIT(0)
  28. static int nau8821_quirk;
  29. static int quirk_override = -1;
  30. module_param_named(quirk, quirk_override, uint, 0444);
  31. MODULE_PARM_DESC(quirk, "Board-specific quirk override");
  32. #define NAU_FREF_MAX 13500000
  33. #define NAU_FVCO_MAX 100000000
  34. #define NAU_FVCO_MIN 90000000
  35. #define NAU8821_BUTTON SND_JACK_BTN_0
  36. /* the maximum frequency of CLK_ADC and CLK_DAC */
  37. #define CLK_DA_AD_MAX 6144000
  38. static int nau8821_configure_sysclk(struct nau8821 *nau8821,
  39. int clk_id, unsigned int freq);
  40. static bool nau8821_is_jack_inserted(struct regmap *regmap);
  41. struct nau8821_fll {
  42. int mclk_src;
  43. int ratio;
  44. int fll_frac;
  45. int fll_int;
  46. int clk_ref_div;
  47. };
  48. struct nau8821_fll_attr {
  49. unsigned int param;
  50. unsigned int val;
  51. };
  52. /* scaling for mclk from sysclk_src output */
  53. static const struct nau8821_fll_attr mclk_src_scaling[] = {
  54. { 1, 0x0 },
  55. { 2, 0x2 },
  56. { 4, 0x3 },
  57. { 8, 0x4 },
  58. { 16, 0x5 },
  59. { 32, 0x6 },
  60. { 3, 0x7 },
  61. { 6, 0xa },
  62. { 12, 0xb },
  63. { 24, 0xc },
  64. { 48, 0xd },
  65. { 96, 0xe },
  66. { 5, 0xf },
  67. };
  68. /* ratio for input clk freq */
  69. static const struct nau8821_fll_attr fll_ratio[] = {
  70. { 512000, 0x01 },
  71. { 256000, 0x02 },
  72. { 128000, 0x04 },
  73. { 64000, 0x08 },
  74. { 32000, 0x10 },
  75. { 8000, 0x20 },
  76. { 4000, 0x40 },
  77. };
  78. static const struct nau8821_fll_attr fll_pre_scalar[] = {
  79. { 0, 0x0 },
  80. { 1, 0x1 },
  81. { 2, 0x2 },
  82. { 3, 0x3 },
  83. };
  84. /* over sampling rate */
  85. struct nau8821_osr_attr {
  86. unsigned int osr;
  87. unsigned int clk_src;
  88. };
  89. static const struct nau8821_osr_attr osr_dac_sel[] = {
  90. { 64, 2 }, /* OSR 64, SRC 1/4 */
  91. { 256, 0 }, /* OSR 256, SRC 1 */
  92. { 128, 1 }, /* OSR 128, SRC 1/2 */
  93. { 0, 0 },
  94. { 32, 3 }, /* OSR 32, SRC 1/8 */
  95. };
  96. static const struct nau8821_osr_attr osr_adc_sel[] = {
  97. { 32, 3 }, /* OSR 32, SRC 1/8 */
  98. { 64, 2 }, /* OSR 64, SRC 1/4 */
  99. { 128, 1 }, /* OSR 128, SRC 1/2 */
  100. { 256, 0 }, /* OSR 256, SRC 1 */
  101. };
  102. struct nau8821_dmic_speed {
  103. unsigned int param;
  104. unsigned int val;
  105. };
  106. static const struct nau8821_dmic_speed dmic_speed_sel[] = {
  107. { 0, 0x0 }, /*SPEED 1, SRC 1 */
  108. { 1, 0x1 }, /*SPEED 2, SRC 1/2 */
  109. { 2, 0x2 }, /*SPEED 4, SRC 1/4 */
  110. { 3, 0x3 }, /*SPEED 8, SRC 1/8 */
  111. };
  112. static const struct reg_default nau8821_reg_defaults[] = {
  113. { NAU8821_R01_ENA_CTRL, 0x00ff },
  114. { NAU8821_R03_CLK_DIVIDER, 0x0050 },
  115. { NAU8821_R04_FLL1, 0x0 },
  116. { NAU8821_R05_FLL2, 0x00bc },
  117. { NAU8821_R06_FLL3, 0x0008 },
  118. { NAU8821_R07_FLL4, 0x0010 },
  119. { NAU8821_R08_FLL5, 0x4000 },
  120. { NAU8821_R09_FLL6, 0x6900 },
  121. { NAU8821_R0A_FLL7, 0x0031 },
  122. { NAU8821_R0B_FLL8, 0x26e9 },
  123. { NAU8821_R0D_JACK_DET_CTRL, 0x0 },
  124. { NAU8821_R0F_INTERRUPT_MASK, 0x0 },
  125. { NAU8821_R12_INTERRUPT_DIS_CTRL, 0xffff },
  126. { NAU8821_R13_DMIC_CTRL, 0x0 },
  127. { NAU8821_R1A_GPIO12_CTRL, 0x0 },
  128. { NAU8821_R1B_TDM_CTRL, 0x0 },
  129. { NAU8821_R1C_I2S_PCM_CTRL1, 0x000a },
  130. { NAU8821_R1D_I2S_PCM_CTRL2, 0x8010 },
  131. { NAU8821_R1E_LEFT_TIME_SLOT, 0x0 },
  132. { NAU8821_R1F_RIGHT_TIME_SLOT, 0x0 },
  133. { NAU8821_R21_BIQ0_COF1, 0x0 },
  134. { NAU8821_R22_BIQ0_COF2, 0x0 },
  135. { NAU8821_R23_BIQ0_COF3, 0x0 },
  136. { NAU8821_R24_BIQ0_COF4, 0x0 },
  137. { NAU8821_R25_BIQ0_COF5, 0x0 },
  138. { NAU8821_R26_BIQ0_COF6, 0x0 },
  139. { NAU8821_R27_BIQ0_COF7, 0x0 },
  140. { NAU8821_R28_BIQ0_COF8, 0x0 },
  141. { NAU8821_R29_BIQ0_COF9, 0x0 },
  142. { NAU8821_R2A_BIQ0_COF10, 0x0 },
  143. { NAU8821_R2B_ADC_RATE, 0x0002 },
  144. { NAU8821_R2C_DAC_CTRL1, 0x0082 },
  145. { NAU8821_R2D_DAC_CTRL2, 0x0 },
  146. { NAU8821_R2F_DAC_DGAIN_CTRL, 0x0 },
  147. { NAU8821_R30_ADC_DGAIN_CTRL, 0x0 },
  148. { NAU8821_R31_MUTE_CTRL, 0x0 },
  149. { NAU8821_R32_HSVOL_CTRL, 0x0 },
  150. { NAU8821_R34_DACR_CTRL, 0xcfcf },
  151. { NAU8821_R35_ADC_DGAIN_CTRL1, 0xcfcf },
  152. { NAU8821_R36_ADC_DRC_KNEE_IP12, 0x1486 },
  153. { NAU8821_R37_ADC_DRC_KNEE_IP34, 0x0f12 },
  154. { NAU8821_R38_ADC_DRC_SLOPES, 0x25ff },
  155. { NAU8821_R39_ADC_DRC_ATKDCY, 0x3457 },
  156. { NAU8821_R3A_DAC_DRC_KNEE_IP12, 0x1486 },
  157. { NAU8821_R3B_DAC_DRC_KNEE_IP34, 0x0f12 },
  158. { NAU8821_R3C_DAC_DRC_SLOPES, 0x25f9 },
  159. { NAU8821_R3D_DAC_DRC_ATKDCY, 0x3457 },
  160. { NAU8821_R41_BIQ1_COF1, 0x0 },
  161. { NAU8821_R42_BIQ1_COF2, 0x0 },
  162. { NAU8821_R43_BIQ1_COF3, 0x0 },
  163. { NAU8821_R44_BIQ1_COF4, 0x0 },
  164. { NAU8821_R45_BIQ1_COF5, 0x0 },
  165. { NAU8821_R46_BIQ1_COF6, 0x0 },
  166. { NAU8821_R47_BIQ1_COF7, 0x0 },
  167. { NAU8821_R48_BIQ1_COF8, 0x0 },
  168. { NAU8821_R49_BIQ1_COF9, 0x0 },
  169. { NAU8821_R4A_BIQ1_COF10, 0x0 },
  170. { NAU8821_R4B_CLASSG_CTRL, 0x0 },
  171. { NAU8821_R4C_IMM_MODE_CTRL, 0x0 },
  172. { NAU8821_R4D_IMM_RMS_L, 0x0 },
  173. { NAU8821_R53_OTPDOUT_1, 0xaad8 },
  174. { NAU8821_R54_OTPDOUT_2, 0x0002 },
  175. { NAU8821_R55_MISC_CTRL, 0x0 },
  176. { NAU8821_R66_BIAS_ADJ, 0x0 },
  177. { NAU8821_R68_TRIM_SETTINGS, 0x0 },
  178. { NAU8821_R69_ANALOG_CONTROL_1, 0x0 },
  179. { NAU8821_R6A_ANALOG_CONTROL_2, 0x0 },
  180. { NAU8821_R6B_PGA_MUTE, 0x0 },
  181. { NAU8821_R71_ANALOG_ADC_1, 0x0011 },
  182. { NAU8821_R72_ANALOG_ADC_2, 0x0020 },
  183. { NAU8821_R73_RDAC, 0x0008 },
  184. { NAU8821_R74_MIC_BIAS, 0x0006 },
  185. { NAU8821_R76_BOOST, 0x0 },
  186. { NAU8821_R77_FEPGA, 0x0 },
  187. { NAU8821_R7E_PGA_GAIN, 0x0 },
  188. { NAU8821_R7F_POWER_UP_CONTROL, 0x0 },
  189. { NAU8821_R80_CHARGE_PUMP, 0x0 },
  190. };
  191. static bool nau8821_readable_reg(struct device *dev, unsigned int reg)
  192. {
  193. switch (reg) {
  194. case NAU8821_R00_RESET ... NAU8821_R01_ENA_CTRL:
  195. case NAU8821_R03_CLK_DIVIDER ... NAU8821_R0B_FLL8:
  196. case NAU8821_R0D_JACK_DET_CTRL:
  197. case NAU8821_R0F_INTERRUPT_MASK ... NAU8821_R13_DMIC_CTRL:
  198. case NAU8821_R1A_GPIO12_CTRL ... NAU8821_R1F_RIGHT_TIME_SLOT:
  199. case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2D_DAC_CTRL2:
  200. case NAU8821_R2F_DAC_DGAIN_CTRL ... NAU8821_R32_HSVOL_CTRL:
  201. case NAU8821_R34_DACR_CTRL ... NAU8821_R3D_DAC_DRC_ATKDCY:
  202. case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4F_FUSE_CTRL3:
  203. case NAU8821_R51_FUSE_CTRL1:
  204. case NAU8821_R53_OTPDOUT_1 ... NAU8821_R55_MISC_CTRL:
  205. case NAU8821_R58_I2C_DEVICE_ID ... NAU8821_R5A_SOFTWARE_RST:
  206. case NAU8821_R66_BIAS_ADJ:
  207. case NAU8821_R68_TRIM_SETTINGS ... NAU8821_R6B_PGA_MUTE:
  208. case NAU8821_R71_ANALOG_ADC_1 ... NAU8821_R74_MIC_BIAS:
  209. case NAU8821_R76_BOOST ... NAU8821_R77_FEPGA:
  210. case NAU8821_R7E_PGA_GAIN ... NAU8821_R82_GENERAL_STATUS:
  211. return true;
  212. default:
  213. return false;
  214. }
  215. }
  216. static bool nau8821_writeable_reg(struct device *dev, unsigned int reg)
  217. {
  218. switch (reg) {
  219. case NAU8821_R00_RESET ... NAU8821_R01_ENA_CTRL:
  220. case NAU8821_R03_CLK_DIVIDER ... NAU8821_R0B_FLL8:
  221. case NAU8821_R0D_JACK_DET_CTRL:
  222. case NAU8821_R0F_INTERRUPT_MASK:
  223. case NAU8821_R11_INT_CLR_KEY_STATUS ... NAU8821_R13_DMIC_CTRL:
  224. case NAU8821_R1A_GPIO12_CTRL ... NAU8821_R1F_RIGHT_TIME_SLOT:
  225. case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2D_DAC_CTRL2:
  226. case NAU8821_R2F_DAC_DGAIN_CTRL ... NAU8821_R32_HSVOL_CTRL:
  227. case NAU8821_R34_DACR_CTRL ... NAU8821_R3D_DAC_DRC_ATKDCY:
  228. case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4C_IMM_MODE_CTRL:
  229. case NAU8821_R4E_FUSE_CTRL2 ... NAU8821_R4F_FUSE_CTRL3:
  230. case NAU8821_R51_FUSE_CTRL1:
  231. case NAU8821_R55_MISC_CTRL:
  232. case NAU8821_R5A_SOFTWARE_RST:
  233. case NAU8821_R66_BIAS_ADJ:
  234. case NAU8821_R68_TRIM_SETTINGS ... NAU8821_R6B_PGA_MUTE:
  235. case NAU8821_R71_ANALOG_ADC_1 ... NAU8821_R74_MIC_BIAS:
  236. case NAU8821_R76_BOOST ... NAU8821_R77_FEPGA:
  237. case NAU8821_R7E_PGA_GAIN ... NAU8821_R80_CHARGE_PUMP:
  238. return true;
  239. default:
  240. return false;
  241. }
  242. }
  243. static bool nau8821_volatile_reg(struct device *dev, unsigned int reg)
  244. {
  245. switch (reg) {
  246. case NAU8821_R00_RESET:
  247. case NAU8821_R10_IRQ_STATUS ... NAU8821_R11_INT_CLR_KEY_STATUS:
  248. case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2A_BIQ0_COF10:
  249. case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4A_BIQ1_COF10:
  250. case NAU8821_R4D_IMM_RMS_L:
  251. case NAU8821_R53_OTPDOUT_1 ... NAU8821_R54_OTPDOUT_2:
  252. case NAU8821_R58_I2C_DEVICE_ID ... NAU8821_R5A_SOFTWARE_RST:
  253. case NAU8821_R81_CHARGE_PUMP_INPUT_READ ... NAU8821_R82_GENERAL_STATUS:
  254. return true;
  255. default:
  256. return false;
  257. }
  258. }
  259. static int nau8821_biq_coeff_get(struct snd_kcontrol *kcontrol,
  260. struct snd_ctl_elem_value *ucontrol)
  261. {
  262. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  263. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  264. if (!component->regmap)
  265. return -EINVAL;
  266. regmap_raw_read(component->regmap, NAU8821_R21_BIQ0_COF1,
  267. ucontrol->value.bytes.data, params->max);
  268. return 0;
  269. }
  270. static int nau8821_biq_coeff_put(struct snd_kcontrol *kcontrol,
  271. struct snd_ctl_elem_value *ucontrol)
  272. {
  273. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  274. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  275. void *data;
  276. if (!component->regmap)
  277. return -EINVAL;
  278. data = kmemdup(ucontrol->value.bytes.data,
  279. params->max, GFP_KERNEL | GFP_DMA);
  280. if (!data)
  281. return -ENOMEM;
  282. regmap_raw_write(component->regmap, NAU8821_R21_BIQ0_COF1,
  283. data, params->max);
  284. kfree(data);
  285. return 0;
  286. }
  287. static const char * const nau8821_adc_decimation[] = {
  288. "32", "64", "128", "256" };
  289. static const struct soc_enum nau8821_adc_decimation_enum =
  290. SOC_ENUM_SINGLE(NAU8821_R2B_ADC_RATE, NAU8821_ADC_SYNC_DOWN_SFT,
  291. ARRAY_SIZE(nau8821_adc_decimation), nau8821_adc_decimation);
  292. static const char * const nau8821_dac_oversampl[] = {
  293. "64", "256", "128", "", "32" };
  294. static const struct soc_enum nau8821_dac_oversampl_enum =
  295. SOC_ENUM_SINGLE(NAU8821_R2C_DAC_CTRL1, NAU8821_DAC_OVERSAMPLE_SFT,
  296. ARRAY_SIZE(nau8821_dac_oversampl), nau8821_dac_oversampl);
  297. static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -6600, 2400);
  298. static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0);
  299. static const DECLARE_TLV_DB_MINMAX(hp_vol_tlv, -900, 0);
  300. static const DECLARE_TLV_DB_SCALE(playback_vol_tlv, -6600, 50, 1);
  301. static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
  302. static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -7000, 2400);
  303. static const struct snd_kcontrol_new nau8821_controls[] = {
  304. SOC_DOUBLE_TLV("Mic Volume", NAU8821_R35_ADC_DGAIN_CTRL1,
  305. NAU8821_ADCL_CH_VOL_SFT, NAU8821_ADCR_CH_VOL_SFT,
  306. 0xff, 0, adc_vol_tlv),
  307. SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8821_R30_ADC_DGAIN_CTRL,
  308. 12, 8, 0x0f, 0, sidetone_vol_tlv),
  309. SOC_DOUBLE_TLV("Headphone Volume", NAU8821_R32_HSVOL_CTRL,
  310. NAU8821_HPL_VOL_SFT, NAU8821_HPR_VOL_SFT, 0x3, 1, hp_vol_tlv),
  311. SOC_DOUBLE_TLV("Digital Playback Volume", NAU8821_R34_DACR_CTRL,
  312. NAU8821_DACL_CH_VOL_SFT, NAU8821_DACR_CH_VOL_SFT,
  313. 0xcf, 0, playback_vol_tlv),
  314. SOC_DOUBLE_TLV("Frontend PGA Volume", NAU8821_R7E_PGA_GAIN,
  315. NAU8821_PGA_GAIN_L_SFT, NAU8821_PGA_GAIN_R_SFT,
  316. 37, 0, fepga_gain_tlv),
  317. SOC_DOUBLE_TLV("Headphone Crosstalk Volume",
  318. NAU8821_R2F_DAC_DGAIN_CTRL,
  319. 0, 8, 0xff, 0, crosstalk_vol_tlv),
  320. SOC_ENUM("ADC Decimation Rate", nau8821_adc_decimation_enum),
  321. SOC_ENUM("DAC Oversampling Rate", nau8821_dac_oversampl_enum),
  322. SND_SOC_BYTES_EXT("BIQ Coefficients", 20,
  323. nau8821_biq_coeff_get, nau8821_biq_coeff_put),
  324. SOC_SINGLE("ADC Phase Switch", NAU8821_R1B_TDM_CTRL,
  325. NAU8821_ADCPHS_SFT, 1, 0),
  326. };
  327. static const struct snd_kcontrol_new nau8821_dmic_mode_switch =
  328. SOC_DAPM_SINGLE("Switch", NAU8821_R13_DMIC_CTRL,
  329. NAU8821_DMIC_EN_SFT, 1, 0);
  330. static int dmic_clock_control(struct snd_soc_dapm_widget *w,
  331. struct snd_kcontrol *k, int event)
  332. {
  333. struct snd_soc_component *component =
  334. snd_soc_dapm_to_component(w->dapm);
  335. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  336. int i, speed_selection = -1, clk_adc_src, clk_adc;
  337. unsigned int clk_divider_r03;
  338. /* The DMIC clock is gotten from adc clock divided by
  339. * CLK_DMIC_SRC (1, 2, 4, 8). The clock has to be equal or
  340. * less than nau8821->dmic_clk_threshold.
  341. */
  342. regmap_read(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
  343. &clk_divider_r03);
  344. clk_adc_src = (clk_divider_r03 & NAU8821_CLK_ADC_SRC_MASK)
  345. >> NAU8821_CLK_ADC_SRC_SFT;
  346. clk_adc = (nau8821->fs * 256) >> clk_adc_src;
  347. for (i = 0 ; i < 4 ; i++)
  348. if ((clk_adc >> dmic_speed_sel[i].param) <=
  349. nau8821->dmic_clk_threshold) {
  350. speed_selection = dmic_speed_sel[i].val;
  351. break;
  352. }
  353. if (i == 4)
  354. return -EINVAL;
  355. dev_dbg(nau8821->dev,
  356. "clk_adc=%d, dmic_clk_threshold = %d, param=%d, val = %d\n",
  357. clk_adc, nau8821->dmic_clk_threshold,
  358. dmic_speed_sel[i].param, dmic_speed_sel[i].val);
  359. regmap_update_bits(nau8821->regmap, NAU8821_R13_DMIC_CTRL,
  360. NAU8821_DMIC_SRC_MASK,
  361. (speed_selection << NAU8821_DMIC_SRC_SFT));
  362. return 0;
  363. }
  364. static int nau8821_left_adc_event(struct snd_soc_dapm_widget *w,
  365. struct snd_kcontrol *kcontrol, int event)
  366. {
  367. struct snd_soc_component *component =
  368. snd_soc_dapm_to_component(w->dapm);
  369. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  370. switch (event) {
  371. case SND_SOC_DAPM_POST_PMU:
  372. msleep(125);
  373. regmap_update_bits(nau8821->regmap, NAU8821_R01_ENA_CTRL,
  374. NAU8821_EN_ADCL, NAU8821_EN_ADCL);
  375. break;
  376. case SND_SOC_DAPM_POST_PMD:
  377. regmap_update_bits(nau8821->regmap,
  378. NAU8821_R01_ENA_CTRL, NAU8821_EN_ADCL, 0);
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }
  385. static int nau8821_right_adc_event(struct snd_soc_dapm_widget *w,
  386. struct snd_kcontrol *kcontrol, int event)
  387. {
  388. struct snd_soc_component *component =
  389. snd_soc_dapm_to_component(w->dapm);
  390. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  391. switch (event) {
  392. case SND_SOC_DAPM_POST_PMU:
  393. msleep(125);
  394. regmap_update_bits(nau8821->regmap, NAU8821_R01_ENA_CTRL,
  395. NAU8821_EN_ADCR, NAU8821_EN_ADCR);
  396. break;
  397. case SND_SOC_DAPM_POST_PMD:
  398. regmap_update_bits(nau8821->regmap,
  399. NAU8821_R01_ENA_CTRL, NAU8821_EN_ADCR, 0);
  400. break;
  401. default:
  402. return -EINVAL;
  403. }
  404. return 0;
  405. }
  406. static int nau8821_pump_event(struct snd_soc_dapm_widget *w,
  407. struct snd_kcontrol *kcontrol, int event)
  408. {
  409. struct snd_soc_component *component =
  410. snd_soc_dapm_to_component(w->dapm);
  411. struct nau8821 *nau8821 =
  412. snd_soc_component_get_drvdata(component);
  413. switch (event) {
  414. case SND_SOC_DAPM_POST_PMU:
  415. /* Prevent startup click by letting charge pump to ramp up */
  416. msleep(20);
  417. regmap_update_bits(nau8821->regmap, NAU8821_R80_CHARGE_PUMP,
  418. NAU8821_JAMNODCLOW, NAU8821_JAMNODCLOW);
  419. break;
  420. case SND_SOC_DAPM_PRE_PMD:
  421. regmap_update_bits(nau8821->regmap, NAU8821_R80_CHARGE_PUMP,
  422. NAU8821_JAMNODCLOW, 0);
  423. break;
  424. default:
  425. return -EINVAL;
  426. }
  427. return 0;
  428. }
  429. static int nau8821_output_dac_event(struct snd_soc_dapm_widget *w,
  430. struct snd_kcontrol *kcontrol, int event)
  431. {
  432. struct snd_soc_component *component =
  433. snd_soc_dapm_to_component(w->dapm);
  434. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  435. switch (event) {
  436. case SND_SOC_DAPM_PRE_PMU:
  437. /* Disables the TESTDAC to let DAC signal pass through. */
  438. regmap_update_bits(nau8821->regmap, NAU8821_R66_BIAS_ADJ,
  439. NAU8821_BIAS_TESTDAC_EN, 0);
  440. break;
  441. case SND_SOC_DAPM_POST_PMD:
  442. regmap_update_bits(nau8821->regmap, NAU8821_R66_BIAS_ADJ,
  443. NAU8821_BIAS_TESTDAC_EN, NAU8821_BIAS_TESTDAC_EN);
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. return 0;
  449. }
  450. static int system_clock_control(struct snd_soc_dapm_widget *w,
  451. struct snd_kcontrol *k, int event)
  452. {
  453. struct snd_soc_component *component =
  454. snd_soc_dapm_to_component(w->dapm);
  455. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  456. if (SND_SOC_DAPM_EVENT_OFF(event)) {
  457. dev_dbg(nau8821->dev, "system clock control : POWER OFF\n");
  458. /* Set clock source to disable or internal clock before the
  459. * playback or capture end. Codec needs clock for Jack
  460. * detection and button press if jack inserted; otherwise,
  461. * the clock should be closed.
  462. */
  463. if (nau8821_is_jack_inserted(nau8821->regmap)) {
  464. nau8821_configure_sysclk(nau8821,
  465. NAU8821_CLK_INTERNAL, 0);
  466. } else {
  467. nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0);
  468. }
  469. }
  470. return 0;
  471. }
  472. static const struct snd_soc_dapm_widget nau8821_dapm_widgets[] = {
  473. SND_SOC_DAPM_SUPPLY("System Clock", SND_SOC_NOPM, 0, 0,
  474. system_clock_control, SND_SOC_DAPM_POST_PMD),
  475. SND_SOC_DAPM_SUPPLY("MICBIAS", NAU8821_R74_MIC_BIAS,
  476. NAU8821_MICBIAS_POWERUP_SFT, 0, NULL, 0),
  477. SND_SOC_DAPM_SUPPLY("DMIC Clock", SND_SOC_NOPM, 0, 0,
  478. dmic_clock_control, SND_SOC_DAPM_POST_PMU),
  479. SND_SOC_DAPM_ADC("ADCL Power", NULL, NAU8821_R72_ANALOG_ADC_2,
  480. NAU8821_POWERUP_ADCL_SFT, 0),
  481. SND_SOC_DAPM_ADC("ADCR Power", NULL, NAU8821_R72_ANALOG_ADC_2,
  482. NAU8821_POWERUP_ADCR_SFT, 0),
  483. SND_SOC_DAPM_PGA_S("Frontend PGA L", 1, NAU8821_R7F_POWER_UP_CONTROL,
  484. NAU8821_PUP_PGA_L_SFT, 0, NULL, 0),
  485. SND_SOC_DAPM_PGA_S("Frontend PGA R", 1, NAU8821_R7F_POWER_UP_CONTROL,
  486. NAU8821_PUP_PGA_R_SFT, 0, NULL, 0),
  487. SND_SOC_DAPM_PGA_S("ADCL Digital path", 0, NAU8821_R01_ENA_CTRL,
  488. NAU8821_EN_ADCL_SFT, 0, nau8821_left_adc_event,
  489. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  490. SND_SOC_DAPM_PGA_S("ADCR Digital path", 0, NAU8821_R01_ENA_CTRL,
  491. NAU8821_EN_ADCR_SFT, 0, nau8821_right_adc_event,
  492. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  493. SND_SOC_DAPM_SWITCH("DMIC Enable", SND_SOC_NOPM,
  494. 0, 0, &nau8821_dmic_mode_switch),
  495. SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8821_R1D_I2S_PCM_CTRL2,
  496. NAU8821_I2S_TRISTATE_SFT, 1),
  497. SND_SOC_DAPM_AIF_IN("AIFRX", "Playback", 0, SND_SOC_NOPM, 0, 0),
  498. SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8821_R73_RDAC,
  499. NAU8821_DACL_EN_SFT, 0, NULL, 0),
  500. SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8821_R73_RDAC,
  501. NAU8821_DACR_EN_SFT, 0, NULL, 0),
  502. SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8821_R73_RDAC,
  503. NAU8821_DACL_CLK_EN_SFT, 0, NULL, 0),
  504. SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8821_R73_RDAC,
  505. NAU8821_DACR_CLK_EN_SFT, 0, NULL, 0),
  506. SND_SOC_DAPM_DAC("DDACR", NULL, NAU8821_R01_ENA_CTRL,
  507. NAU8821_EN_DACR_SFT, 0),
  508. SND_SOC_DAPM_DAC("DDACL", NULL, NAU8821_R01_ENA_CTRL,
  509. NAU8821_EN_DACL_SFT, 0),
  510. SND_SOC_DAPM_PGA_S("HP amp L", 0, NAU8821_R4B_CLASSG_CTRL,
  511. NAU8821_CLASSG_LDAC_EN_SFT, 0, NULL, 0),
  512. SND_SOC_DAPM_PGA_S("HP amp R", 0, NAU8821_R4B_CLASSG_CTRL,
  513. NAU8821_CLASSG_RDAC_EN_SFT, 0, NULL, 0),
  514. SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8821_R80_CHARGE_PUMP,
  515. NAU8821_CHANRGE_PUMP_EN_SFT, 0, nau8821_pump_event,
  516. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  517. SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4,
  518. NAU8821_R7F_POWER_UP_CONTROL,
  519. NAU8821_PUP_INTEG_R_SFT, 0, NULL, 0),
  520. SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4,
  521. NAU8821_R7F_POWER_UP_CONTROL,
  522. NAU8821_PUP_INTEG_L_SFT, 0, NULL, 0),
  523. SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5,
  524. NAU8821_R7F_POWER_UP_CONTROL,
  525. NAU8821_PUP_DRV_INSTG_R_SFT, 0, NULL, 0),
  526. SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5,
  527. NAU8821_R7F_POWER_UP_CONTROL,
  528. NAU8821_PUP_DRV_INSTG_L_SFT, 0, NULL, 0),
  529. SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6,
  530. NAU8821_R7F_POWER_UP_CONTROL,
  531. NAU8821_PUP_MAIN_DRV_R_SFT, 0, NULL, 0),
  532. SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6,
  533. NAU8821_R7F_POWER_UP_CONTROL,
  534. NAU8821_PUP_MAIN_DRV_L_SFT, 0, NULL, 0),
  535. SND_SOC_DAPM_PGA_S("Output DACL", 7,
  536. NAU8821_R80_CHARGE_PUMP, NAU8821_POWER_DOWN_DACL_SFT,
  537. 0, nau8821_output_dac_event,
  538. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  539. SND_SOC_DAPM_PGA_S("Output DACR", 7,
  540. NAU8821_R80_CHARGE_PUMP, NAU8821_POWER_DOWN_DACR_SFT,
  541. 0, nau8821_output_dac_event,
  542. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  543. /* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */
  544. SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8,
  545. NAU8821_R0D_JACK_DET_CTRL,
  546. NAU8821_SPKR_DWN1L_SFT, 0, NULL, 0),
  547. SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8,
  548. NAU8821_R0D_JACK_DET_CTRL,
  549. NAU8821_SPKR_DWN1R_SFT, 0, NULL, 0),
  550. /* High current HPOL/R boost driver */
  551. SND_SOC_DAPM_PGA_S("HP Boost Driver", 9,
  552. NAU8821_R76_BOOST, NAU8821_HP_BOOST_DIS_SFT, 1, NULL, 0),
  553. SND_SOC_DAPM_PGA("Class G", NAU8821_R4B_CLASSG_CTRL,
  554. NAU8821_CLASSG_EN_SFT, 0, NULL, 0),
  555. SND_SOC_DAPM_INPUT("MICL"),
  556. SND_SOC_DAPM_INPUT("MICR"),
  557. SND_SOC_DAPM_INPUT("DMIC"),
  558. SND_SOC_DAPM_OUTPUT("HPOL"),
  559. SND_SOC_DAPM_OUTPUT("HPOR"),
  560. };
  561. static const struct snd_soc_dapm_route nau8821_dapm_routes[] = {
  562. {"DMIC Enable", "Switch", "DMIC"},
  563. {"DMIC Enable", NULL, "DMIC Clock"},
  564. {"Frontend PGA L", NULL, "MICL"},
  565. {"Frontend PGA R", NULL, "MICR"},
  566. {"Frontend PGA L", NULL, "MICBIAS"},
  567. {"Frontend PGA R", NULL, "MICBIAS"},
  568. {"ADCL Power", NULL, "Frontend PGA L"},
  569. {"ADCR Power", NULL, "Frontend PGA R"},
  570. {"ADCL Digital path", NULL, "ADCL Power"},
  571. {"ADCR Digital path", NULL, "ADCR Power"},
  572. {"ADCL Digital path", NULL, "DMIC Enable"},
  573. {"ADCR Digital path", NULL, "DMIC Enable"},
  574. {"AIFTX", NULL, "ADCL Digital path"},
  575. {"AIFTX", NULL, "ADCR Digital path"},
  576. {"AIFTX", NULL, "System Clock"},
  577. {"AIFRX", NULL, "System Clock"},
  578. {"DDACL", NULL, "AIFRX"},
  579. {"DDACR", NULL, "AIFRX"},
  580. {"HP amp L", NULL, "DDACL"},
  581. {"HP amp R", NULL, "DDACR"},
  582. {"Charge Pump", NULL, "HP amp L"},
  583. {"Charge Pump", NULL, "HP amp R"},
  584. {"ADACL", NULL, "Charge Pump"},
  585. {"ADACR", NULL, "Charge Pump"},
  586. {"ADACL Clock", NULL, "ADACL"},
  587. {"ADACR Clock", NULL, "ADACR"},
  588. {"Output Driver L Stage 1", NULL, "ADACL Clock"},
  589. {"Output Driver R Stage 1", NULL, "ADACR Clock"},
  590. {"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"},
  591. {"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"},
  592. {"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"},
  593. {"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"},
  594. {"Output DACL", NULL, "Output Driver L Stage 3"},
  595. {"Output DACR", NULL, "Output Driver R Stage 3"},
  596. {"HPOL Pulldown", NULL, "Output DACL"},
  597. {"HPOR Pulldown", NULL, "Output DACR"},
  598. {"HP Boost Driver", NULL, "HPOL Pulldown"},
  599. {"HP Boost Driver", NULL, "HPOR Pulldown"},
  600. {"Class G", NULL, "HP Boost Driver"},
  601. {"HPOL", NULL, "Class G"},
  602. {"HPOR", NULL, "Class G"},
  603. };
  604. static const struct nau8821_osr_attr *
  605. nau8821_get_osr(struct nau8821 *nau8821, int stream)
  606. {
  607. unsigned int osr;
  608. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  609. regmap_read(nau8821->regmap, NAU8821_R2C_DAC_CTRL1, &osr);
  610. osr &= NAU8821_DAC_OVERSAMPLE_MASK;
  611. if (osr >= ARRAY_SIZE(osr_dac_sel))
  612. return NULL;
  613. return &osr_dac_sel[osr];
  614. } else {
  615. regmap_read(nau8821->regmap, NAU8821_R2B_ADC_RATE, &osr);
  616. osr &= NAU8821_ADC_SYNC_DOWN_MASK;
  617. if (osr >= ARRAY_SIZE(osr_adc_sel))
  618. return NULL;
  619. return &osr_adc_sel[osr];
  620. }
  621. }
  622. static int nau8821_dai_startup(struct snd_pcm_substream *substream,
  623. struct snd_soc_dai *dai)
  624. {
  625. struct snd_soc_component *component = dai->component;
  626. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  627. const struct nau8821_osr_attr *osr;
  628. osr = nau8821_get_osr(nau8821, substream->stream);
  629. if (!osr || !osr->osr)
  630. return -EINVAL;
  631. return snd_pcm_hw_constraint_minmax(substream->runtime,
  632. SNDRV_PCM_HW_PARAM_RATE,
  633. 0, CLK_DA_AD_MAX / osr->osr);
  634. }
  635. static int nau8821_hw_params(struct snd_pcm_substream *substream,
  636. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  637. {
  638. struct snd_soc_component *component = dai->component;
  639. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  640. unsigned int val_len = 0, ctrl_val, bclk_fs, clk_div;
  641. const struct nau8821_osr_attr *osr;
  642. nau8821->fs = params_rate(params);
  643. /* CLK_DAC or CLK_ADC = OSR * FS
  644. * DAC or ADC clock frequency is defined as Over Sampling Rate (OSR)
  645. * multiplied by the audio sample rate (Fs). Note that the OSR and Fs
  646. * values must be selected such that the maximum frequency is less
  647. * than 6.144 MHz.
  648. */
  649. osr = nau8821_get_osr(nau8821, substream->stream);
  650. if (!osr || !osr->osr)
  651. return -EINVAL;
  652. if (nau8821->fs * osr->osr > CLK_DA_AD_MAX)
  653. return -EINVAL;
  654. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  655. regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
  656. NAU8821_CLK_DAC_SRC_MASK,
  657. osr->clk_src << NAU8821_CLK_DAC_SRC_SFT);
  658. else
  659. regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
  660. NAU8821_CLK_ADC_SRC_MASK,
  661. osr->clk_src << NAU8821_CLK_ADC_SRC_SFT);
  662. /* make BCLK and LRC divde configuration if the codec as master. */
  663. regmap_read(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2, &ctrl_val);
  664. if (ctrl_val & NAU8821_I2S_MS_MASTER) {
  665. /* get the bclk and fs ratio */
  666. bclk_fs = snd_soc_params_to_bclk(params) / nau8821->fs;
  667. if (bclk_fs <= 32)
  668. clk_div = 3;
  669. else if (bclk_fs <= 64)
  670. clk_div = 2;
  671. else if (bclk_fs <= 128)
  672. clk_div = 1;
  673. else {
  674. return -EINVAL;
  675. }
  676. regmap_update_bits(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2,
  677. NAU8821_I2S_LRC_DIV_MASK | NAU8821_I2S_BLK_DIV_MASK,
  678. (clk_div << NAU8821_I2S_LRC_DIV_SFT) | clk_div);
  679. }
  680. switch (params_width(params)) {
  681. case 16:
  682. val_len |= NAU8821_I2S_DL_16;
  683. break;
  684. case 20:
  685. val_len |= NAU8821_I2S_DL_20;
  686. break;
  687. case 24:
  688. val_len |= NAU8821_I2S_DL_24;
  689. break;
  690. case 32:
  691. val_len |= NAU8821_I2S_DL_32;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. regmap_update_bits(nau8821->regmap, NAU8821_R1C_I2S_PCM_CTRL1,
  697. NAU8821_I2S_DL_MASK, val_len);
  698. return 0;
  699. }
  700. static int nau8821_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  701. {
  702. struct snd_soc_component *component = codec_dai->component;
  703. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  704. unsigned int ctrl1_val = 0, ctrl2_val = 0;
  705. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  706. case SND_SOC_DAIFMT_CBP_CFP:
  707. ctrl2_val |= NAU8821_I2S_MS_MASTER;
  708. break;
  709. case SND_SOC_DAIFMT_CBC_CFC:
  710. break;
  711. default:
  712. return -EINVAL;
  713. }
  714. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  715. case SND_SOC_DAIFMT_NB_NF:
  716. break;
  717. case SND_SOC_DAIFMT_IB_NF:
  718. ctrl1_val |= NAU8821_I2S_BP_INV;
  719. break;
  720. default:
  721. return -EINVAL;
  722. }
  723. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  724. case SND_SOC_DAIFMT_I2S:
  725. ctrl1_val |= NAU8821_I2S_DF_I2S;
  726. break;
  727. case SND_SOC_DAIFMT_LEFT_J:
  728. ctrl1_val |= NAU8821_I2S_DF_LEFT;
  729. break;
  730. case SND_SOC_DAIFMT_RIGHT_J:
  731. ctrl1_val |= NAU8821_I2S_DF_RIGTH;
  732. break;
  733. case SND_SOC_DAIFMT_DSP_A:
  734. ctrl1_val |= NAU8821_I2S_DF_PCM_AB;
  735. break;
  736. case SND_SOC_DAIFMT_DSP_B:
  737. ctrl1_val |= NAU8821_I2S_DF_PCM_AB;
  738. ctrl1_val |= NAU8821_I2S_PCMB_EN;
  739. break;
  740. default:
  741. return -EINVAL;
  742. }
  743. regmap_update_bits(nau8821->regmap, NAU8821_R1C_I2S_PCM_CTRL1,
  744. NAU8821_I2S_DL_MASK | NAU8821_I2S_DF_MASK |
  745. NAU8821_I2S_BP_MASK | NAU8821_I2S_PCMB_MASK, ctrl1_val);
  746. regmap_update_bits(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2,
  747. NAU8821_I2S_MS_MASK, ctrl2_val);
  748. return 0;
  749. }
  750. static int nau8821_digital_mute(struct snd_soc_dai *dai, int mute,
  751. int direction)
  752. {
  753. struct snd_soc_component *component = dai->component;
  754. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  755. unsigned int val = 0;
  756. if (mute)
  757. val = NAU8821_DAC_SOFT_MUTE;
  758. return regmap_update_bits(nau8821->regmap,
  759. NAU8821_R31_MUTE_CTRL, NAU8821_DAC_SOFT_MUTE, val);
  760. }
  761. static const struct snd_soc_dai_ops nau8821_dai_ops = {
  762. .startup = nau8821_dai_startup,
  763. .hw_params = nau8821_hw_params,
  764. .set_fmt = nau8821_set_dai_fmt,
  765. .mute_stream = nau8821_digital_mute,
  766. .no_capture_mute = 1,
  767. };
  768. #define NAU8821_RATES SNDRV_PCM_RATE_8000_192000
  769. #define NAU8821_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  770. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  771. static struct snd_soc_dai_driver nau8821_dai = {
  772. .name = NUVOTON_CODEC_DAI,
  773. .playback = {
  774. .stream_name = "Playback",
  775. .channels_min = 1,
  776. .channels_max = 2,
  777. .rates = NAU8821_RATES,
  778. .formats = NAU8821_FORMATS,
  779. },
  780. .capture = {
  781. .stream_name = "Capture",
  782. .channels_min = 1,
  783. .channels_max = 2,
  784. .rates = NAU8821_RATES,
  785. .formats = NAU8821_FORMATS,
  786. },
  787. .ops = &nau8821_dai_ops,
  788. };
  789. static bool nau8821_is_jack_inserted(struct regmap *regmap)
  790. {
  791. bool active_high, is_high;
  792. int status, jkdet;
  793. regmap_read(regmap, NAU8821_R0D_JACK_DET_CTRL, &jkdet);
  794. active_high = jkdet & NAU8821_JACK_POLARITY;
  795. regmap_read(regmap, NAU8821_R82_GENERAL_STATUS, &status);
  796. is_high = status & NAU8821_GPIO2_IN;
  797. /* return jack connection status according to jack insertion logic
  798. * active high or active low.
  799. */
  800. return active_high == is_high;
  801. }
  802. static void nau8821_int_status_clear_all(struct regmap *regmap)
  803. {
  804. int active_irq, clear_irq, i;
  805. /* Reset the intrruption status from rightmost bit if the corres-
  806. * ponding irq event occurs.
  807. */
  808. regmap_read(regmap, NAU8821_R10_IRQ_STATUS, &active_irq);
  809. for (i = 0; i < NAU8821_REG_DATA_LEN; i++) {
  810. clear_irq = (0x1 << i);
  811. if (active_irq & clear_irq)
  812. regmap_write(regmap,
  813. NAU8821_R11_INT_CLR_KEY_STATUS, clear_irq);
  814. }
  815. }
  816. static void nau8821_eject_jack(struct nau8821 *nau8821)
  817. {
  818. struct snd_soc_dapm_context *dapm = nau8821->dapm;
  819. struct regmap *regmap = nau8821->regmap;
  820. struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
  821. /* Detach 2kOhm Resistors from MICBIAS to MICGND */
  822. regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
  823. NAU8821_MICBIAS_JKR2, 0);
  824. /* HPL/HPR short to ground */
  825. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  826. NAU8821_SPKR_DWN1R | NAU8821_SPKR_DWN1L, 0);
  827. snd_soc_component_disable_pin(component, "MICBIAS");
  828. snd_soc_dapm_sync(dapm);
  829. /* Clear all interruption status */
  830. nau8821_int_status_clear_all(regmap);
  831. /* Enable the insertion interruption, disable the ejection inter-
  832. * ruption, and then bypass de-bounce circuit.
  833. */
  834. regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL,
  835. NAU8821_IRQ_EJECT_DIS | NAU8821_IRQ_INSERT_DIS,
  836. NAU8821_IRQ_EJECT_DIS);
  837. /* Mask unneeded IRQs: 1 - disable, 0 - enable */
  838. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
  839. NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN,
  840. NAU8821_IRQ_EJECT_EN);
  841. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  842. NAU8821_JACK_DET_DB_BYPASS, NAU8821_JACK_DET_DB_BYPASS);
  843. /* Close clock for jack type detection at manual mode */
  844. if (dapm->bias_level < SND_SOC_BIAS_PREPARE)
  845. nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0);
  846. /* Recover to normal channel input */
  847. regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE,
  848. NAU8821_ADC_R_SRC_EN, 0);
  849. if (nau8821->key_enable) {
  850. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
  851. NAU8821_IRQ_KEY_RELEASE_EN |
  852. NAU8821_IRQ_KEY_PRESS_EN,
  853. NAU8821_IRQ_KEY_RELEASE_EN |
  854. NAU8821_IRQ_KEY_PRESS_EN);
  855. regmap_update_bits(regmap,
  856. NAU8821_R12_INTERRUPT_DIS_CTRL,
  857. NAU8821_IRQ_KEY_RELEASE_DIS |
  858. NAU8821_IRQ_KEY_PRESS_DIS,
  859. NAU8821_IRQ_KEY_RELEASE_DIS |
  860. NAU8821_IRQ_KEY_PRESS_DIS);
  861. }
  862. }
  863. static void nau8821_jdet_work(struct work_struct *work)
  864. {
  865. struct nau8821 *nau8821 =
  866. container_of(work, struct nau8821, jdet_work);
  867. struct snd_soc_dapm_context *dapm = nau8821->dapm;
  868. struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
  869. struct regmap *regmap = nau8821->regmap;
  870. int jack_status_reg, mic_detected, event = 0, event_mask = 0;
  871. snd_soc_component_force_enable_pin(component, "MICBIAS");
  872. snd_soc_dapm_sync(dapm);
  873. msleep(20);
  874. regmap_read(regmap, NAU8821_R58_I2C_DEVICE_ID, &jack_status_reg);
  875. mic_detected = !(jack_status_reg & NAU8821_KEYDET);
  876. if (mic_detected) {
  877. dev_dbg(nau8821->dev, "Headset connected\n");
  878. event |= SND_JACK_HEADSET;
  879. /* 2kOhm Resistor from MICBIAS to MICGND1 */
  880. regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
  881. NAU8821_MICBIAS_JKR2, NAU8821_MICBIAS_JKR2);
  882. /* Latch Right Channel Analog data
  883. * input into the Right Channel Filter
  884. */
  885. regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE,
  886. NAU8821_ADC_R_SRC_EN, NAU8821_ADC_R_SRC_EN);
  887. if (nau8821->key_enable) {
  888. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
  889. NAU8821_IRQ_KEY_RELEASE_EN |
  890. NAU8821_IRQ_KEY_PRESS_EN, 0);
  891. regmap_update_bits(regmap,
  892. NAU8821_R12_INTERRUPT_DIS_CTRL,
  893. NAU8821_IRQ_KEY_RELEASE_DIS |
  894. NAU8821_IRQ_KEY_PRESS_DIS, 0);
  895. }
  896. } else {
  897. dev_dbg(nau8821->dev, "Headphone connected\n");
  898. event |= SND_JACK_HEADPHONE;
  899. snd_soc_component_disable_pin(component, "MICBIAS");
  900. snd_soc_dapm_sync(dapm);
  901. }
  902. event_mask |= SND_JACK_HEADSET;
  903. snd_soc_jack_report(nau8821->jack, event, event_mask);
  904. }
  905. /* Enable interruptions with internal clock. */
  906. static void nau8821_setup_inserted_irq(struct nau8821 *nau8821)
  907. {
  908. struct regmap *regmap = nau8821->regmap;
  909. /* Enable internal VCO needed for interruptions */
  910. if (nau8821->dapm->bias_level < SND_SOC_BIAS_PREPARE)
  911. nau8821_configure_sysclk(nau8821, NAU8821_CLK_INTERNAL, 0);
  912. /* Chip needs one FSCLK cycle in order to generate interruptions,
  913. * as we cannot guarantee one will be provided by the system. Turning
  914. * master mode on then off enables us to generate that FSCLK cycle
  915. * with a minimum of contention on the clock bus.
  916. */
  917. regmap_update_bits(regmap, NAU8821_R1D_I2S_PCM_CTRL2,
  918. NAU8821_I2S_MS_MASK, NAU8821_I2S_MS_MASTER);
  919. regmap_update_bits(regmap, NAU8821_R1D_I2S_PCM_CTRL2,
  920. NAU8821_I2S_MS_MASK, NAU8821_I2S_MS_SLAVE);
  921. /* Not bypass de-bounce circuit */
  922. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  923. NAU8821_JACK_DET_DB_BYPASS, 0);
  924. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
  925. NAU8821_IRQ_EJECT_EN, 0);
  926. regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL,
  927. NAU8821_IRQ_EJECT_DIS, 0);
  928. }
  929. static irqreturn_t nau8821_interrupt(int irq, void *data)
  930. {
  931. struct nau8821 *nau8821 = (struct nau8821 *)data;
  932. struct regmap *regmap = nau8821->regmap;
  933. int active_irq, clear_irq = 0, event = 0, event_mask = 0;
  934. if (regmap_read(regmap, NAU8821_R10_IRQ_STATUS, &active_irq)) {
  935. dev_err(nau8821->dev, "failed to read irq status\n");
  936. return IRQ_NONE;
  937. }
  938. dev_dbg(nau8821->dev, "IRQ %d\n", active_irq);
  939. if ((active_irq & NAU8821_JACK_EJECT_IRQ_MASK) ==
  940. NAU8821_JACK_EJECT_DETECTED) {
  941. regmap_update_bits(regmap, NAU8821_R71_ANALOG_ADC_1,
  942. NAU8821_MICDET_MASK, NAU8821_MICDET_DIS);
  943. nau8821_eject_jack(nau8821);
  944. event_mask |= SND_JACK_HEADSET;
  945. clear_irq = NAU8821_JACK_EJECT_IRQ_MASK;
  946. } else if (active_irq & NAU8821_KEY_SHORT_PRESS_IRQ) {
  947. event |= NAU8821_BUTTON;
  948. event_mask |= NAU8821_BUTTON;
  949. clear_irq = NAU8821_KEY_SHORT_PRESS_IRQ;
  950. } else if (active_irq & NAU8821_KEY_RELEASE_IRQ) {
  951. event_mask = NAU8821_BUTTON;
  952. clear_irq = NAU8821_KEY_RELEASE_IRQ;
  953. } else if ((active_irq & NAU8821_JACK_INSERT_IRQ_MASK) ==
  954. NAU8821_JACK_INSERT_DETECTED) {
  955. regmap_update_bits(regmap, NAU8821_R71_ANALOG_ADC_1,
  956. NAU8821_MICDET_MASK, NAU8821_MICDET_EN);
  957. if (nau8821_is_jack_inserted(regmap)) {
  958. /* detect microphone and jack type */
  959. cancel_work_sync(&nau8821->jdet_work);
  960. schedule_work(&nau8821->jdet_work);
  961. /* Turn off insertion interruption at manual mode */
  962. regmap_update_bits(regmap,
  963. NAU8821_R12_INTERRUPT_DIS_CTRL,
  964. NAU8821_IRQ_INSERT_DIS,
  965. NAU8821_IRQ_INSERT_DIS);
  966. regmap_update_bits(regmap,
  967. NAU8821_R0F_INTERRUPT_MASK,
  968. NAU8821_IRQ_INSERT_EN,
  969. NAU8821_IRQ_INSERT_EN);
  970. nau8821_setup_inserted_irq(nau8821);
  971. } else {
  972. dev_warn(nau8821->dev,
  973. "Inserted IRQ fired but not connected\n");
  974. nau8821_eject_jack(nau8821);
  975. }
  976. }
  977. if (!clear_irq)
  978. clear_irq = active_irq;
  979. /* clears the rightmost interruption */
  980. regmap_write(regmap, NAU8821_R11_INT_CLR_KEY_STATUS, clear_irq);
  981. if (event_mask)
  982. snd_soc_jack_report(nau8821->jack, event, event_mask);
  983. return IRQ_HANDLED;
  984. }
  985. static const struct regmap_config nau8821_regmap_config = {
  986. .val_bits = NAU8821_REG_DATA_LEN,
  987. .reg_bits = NAU8821_REG_ADDR_LEN,
  988. .max_register = NAU8821_REG_MAX,
  989. .readable_reg = nau8821_readable_reg,
  990. .writeable_reg = nau8821_writeable_reg,
  991. .volatile_reg = nau8821_volatile_reg,
  992. .cache_type = REGCACHE_RBTREE,
  993. .reg_defaults = nau8821_reg_defaults,
  994. .num_reg_defaults = ARRAY_SIZE(nau8821_reg_defaults),
  995. };
  996. static int nau8821_component_probe(struct snd_soc_component *component)
  997. {
  998. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  999. struct snd_soc_dapm_context *dapm =
  1000. snd_soc_component_get_dapm(component);
  1001. nau8821->dapm = dapm;
  1002. return 0;
  1003. }
  1004. /**
  1005. * nau8821_calc_fll_param - Calculate FLL parameters.
  1006. * @fll_in: external clock provided to codec.
  1007. * @fs: sampling rate.
  1008. * @fll_param: Pointer to structure of FLL parameters.
  1009. *
  1010. * Calculate FLL parameters to configure codec.
  1011. *
  1012. * Returns 0 for success or negative error code.
  1013. */
  1014. static int nau8821_calc_fll_param(unsigned int fll_in,
  1015. unsigned int fs, struct nau8821_fll *fll_param)
  1016. {
  1017. u64 fvco, fvco_max;
  1018. unsigned int fref, i, fvco_sel;
  1019. /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by
  1020. * dividing freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
  1021. * FREF = freq_in / NAU8821_FLL_REF_DIV_MASK
  1022. */
  1023. for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
  1024. fref = fll_in >> fll_pre_scalar[i].param;
  1025. if (fref <= NAU_FREF_MAX)
  1026. break;
  1027. }
  1028. if (i == ARRAY_SIZE(fll_pre_scalar))
  1029. return -EINVAL;
  1030. fll_param->clk_ref_div = fll_pre_scalar[i].val;
  1031. /* Choose the FLL ratio based on FREF */
  1032. for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
  1033. if (fref >= fll_ratio[i].param)
  1034. break;
  1035. }
  1036. if (i == ARRAY_SIZE(fll_ratio))
  1037. return -EINVAL;
  1038. fll_param->ratio = fll_ratio[i].val;
  1039. /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
  1040. * FDCO must be within the 90MHz - 100MHz or the FFL cannot be
  1041. * guaranteed across the full range of operation.
  1042. * FDCO = freq_out * 2 * mclk_src_scaling
  1043. */
  1044. fvco_max = 0;
  1045. fvco_sel = ARRAY_SIZE(mclk_src_scaling);
  1046. for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
  1047. fvco = 256ULL * fs * 2 * mclk_src_scaling[i].param;
  1048. if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
  1049. fvco_max < fvco) {
  1050. fvco_max = fvco;
  1051. fvco_sel = i;
  1052. }
  1053. }
  1054. if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
  1055. return -EINVAL;
  1056. fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
  1057. /* Calculate the FLL 10-bit integer input and the FLL 24-bit fractional
  1058. * input based on FDCO, FREF and FLL ratio.
  1059. */
  1060. fvco = div_u64(fvco_max << 24, fref * fll_param->ratio);
  1061. fll_param->fll_int = (fvco >> 24) & 0x3ff;
  1062. fll_param->fll_frac = fvco & 0xffffff;
  1063. return 0;
  1064. }
  1065. static void nau8821_fll_apply(struct nau8821 *nau8821,
  1066. struct nau8821_fll *fll_param)
  1067. {
  1068. struct regmap *regmap = nau8821->regmap;
  1069. regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
  1070. NAU8821_CLK_SRC_MASK | NAU8821_CLK_MCLK_SRC_MASK,
  1071. NAU8821_CLK_SRC_MCLK | fll_param->mclk_src);
  1072. /* Make DSP operate at high speed for better performance. */
  1073. regmap_update_bits(regmap, NAU8821_R04_FLL1,
  1074. NAU8821_FLL_RATIO_MASK | NAU8821_ICTRL_LATCH_MASK,
  1075. fll_param->ratio | (0x6 << NAU8821_ICTRL_LATCH_SFT));
  1076. /* FLL 24-bit fractional input */
  1077. regmap_write(regmap, NAU8821_R0A_FLL7,
  1078. (fll_param->fll_frac >> 16) & 0xff);
  1079. regmap_write(regmap, NAU8821_R0B_FLL8, fll_param->fll_frac & 0xffff);
  1080. /* FLL 10-bit integer input */
  1081. regmap_update_bits(regmap, NAU8821_R06_FLL3,
  1082. NAU8821_FLL_INTEGER_MASK, fll_param->fll_int);
  1083. /* FLL pre-scaler */
  1084. regmap_update_bits(regmap, NAU8821_R07_FLL4,
  1085. NAU8821_HIGHBW_EN | NAU8821_FLL_REF_DIV_MASK,
  1086. NAU8821_HIGHBW_EN |
  1087. (fll_param->clk_ref_div << NAU8821_FLL_REF_DIV_SFT));
  1088. /* select divided VCO input */
  1089. regmap_update_bits(regmap, NAU8821_R08_FLL5,
  1090. NAU8821_FLL_CLK_SW_MASK, NAU8821_FLL_CLK_SW_REF);
  1091. /* Disable free-running mode */
  1092. regmap_update_bits(regmap,
  1093. NAU8821_R09_FLL6, NAU8821_DCO_EN, 0);
  1094. if (fll_param->fll_frac) {
  1095. /* set FLL loop filter enable and cutoff frequency at 500Khz */
  1096. regmap_update_bits(regmap, NAU8821_R08_FLL5,
  1097. NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN |
  1098. NAU8821_FLL_FTR_SW_MASK,
  1099. NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN |
  1100. NAU8821_FLL_FTR_SW_FILTER);
  1101. regmap_update_bits(regmap, NAU8821_R09_FLL6,
  1102. NAU8821_SDM_EN | NAU8821_CUTOFF500,
  1103. NAU8821_SDM_EN | NAU8821_CUTOFF500);
  1104. } else {
  1105. /* disable FLL loop filter and cutoff frequency */
  1106. regmap_update_bits(regmap, NAU8821_R08_FLL5,
  1107. NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN |
  1108. NAU8821_FLL_FTR_SW_MASK, NAU8821_FLL_FTR_SW_ACCU);
  1109. regmap_update_bits(regmap, NAU8821_R09_FLL6,
  1110. NAU8821_SDM_EN | NAU8821_CUTOFF500, 0);
  1111. }
  1112. }
  1113. /**
  1114. * nau8821_set_fll - FLL configuration of nau8821
  1115. * @component: codec component
  1116. * @pll_id: PLL requested
  1117. * @source: clock source
  1118. * @freq_in: frequency of input clock source
  1119. * @freq_out: must be 256*Fs in order to achieve the best performance
  1120. *
  1121. * The FLL function can select BCLK or MCLK as the input clock source.
  1122. *
  1123. * Returns 0 if the parameters have been applied successfully
  1124. * or negative error code.
  1125. */
  1126. static int nau8821_set_fll(struct snd_soc_component *component,
  1127. int pll_id, int source, unsigned int freq_in, unsigned int freq_out)
  1128. {
  1129. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  1130. struct nau8821_fll fll_set_param, *fll_param = &fll_set_param;
  1131. int ret, fs;
  1132. fs = freq_out >> 8;
  1133. ret = nau8821_calc_fll_param(freq_in, fs, fll_param);
  1134. if (ret) {
  1135. dev_err(nau8821->dev,
  1136. "Unsupported input clock %d to output clock %d\n",
  1137. freq_in, freq_out);
  1138. return ret;
  1139. }
  1140. dev_dbg(nau8821->dev,
  1141. "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
  1142. fll_param->mclk_src, fll_param->ratio, fll_param->fll_frac,
  1143. fll_param->fll_int, fll_param->clk_ref_div);
  1144. nau8821_fll_apply(nau8821, fll_param);
  1145. mdelay(2);
  1146. regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
  1147. NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_VCO);
  1148. return 0;
  1149. }
  1150. static void nau8821_configure_mclk_as_sysclk(struct regmap *regmap)
  1151. {
  1152. regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
  1153. NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_MCLK);
  1154. regmap_update_bits(regmap, NAU8821_R09_FLL6,
  1155. NAU8821_DCO_EN, 0);
  1156. /* Make DSP operate as default setting for power saving. */
  1157. regmap_update_bits(regmap, NAU8821_R04_FLL1,
  1158. NAU8821_ICTRL_LATCH_MASK, 0);
  1159. }
  1160. static int nau8821_configure_sysclk(struct nau8821 *nau8821,
  1161. int clk_id, unsigned int freq)
  1162. {
  1163. struct regmap *regmap = nau8821->regmap;
  1164. switch (clk_id) {
  1165. case NAU8821_CLK_DIS:
  1166. /* Clock provided externally and disable internal VCO clock */
  1167. nau8821_configure_mclk_as_sysclk(regmap);
  1168. break;
  1169. case NAU8821_CLK_MCLK:
  1170. nau8821_configure_mclk_as_sysclk(regmap);
  1171. /* MCLK not changed by clock tree */
  1172. regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
  1173. NAU8821_CLK_MCLK_SRC_MASK, 0);
  1174. break;
  1175. case NAU8821_CLK_INTERNAL:
  1176. if (nau8821_is_jack_inserted(regmap)) {
  1177. regmap_update_bits(regmap, NAU8821_R09_FLL6,
  1178. NAU8821_DCO_EN, NAU8821_DCO_EN);
  1179. regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
  1180. NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_VCO);
  1181. /* Decrease the VCO frequency and make DSP operate
  1182. * as default setting for power saving.
  1183. */
  1184. regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
  1185. NAU8821_CLK_MCLK_SRC_MASK, 0xf);
  1186. regmap_update_bits(regmap, NAU8821_R04_FLL1,
  1187. NAU8821_ICTRL_LATCH_MASK |
  1188. NAU8821_FLL_RATIO_MASK, 0x10);
  1189. regmap_update_bits(regmap, NAU8821_R09_FLL6,
  1190. NAU8821_SDM_EN, NAU8821_SDM_EN);
  1191. }
  1192. break;
  1193. case NAU8821_CLK_FLL_MCLK:
  1194. /* Higher FLL reference input frequency can only set lower
  1195. * gain error, such as 0000 for input reference from MCLK
  1196. * 12.288Mhz.
  1197. */
  1198. regmap_update_bits(regmap, NAU8821_R06_FLL3,
  1199. NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK,
  1200. NAU8821_FLL_CLK_SRC_MCLK | 0);
  1201. break;
  1202. case NAU8821_CLK_FLL_BLK:
  1203. /* If FLL reference input is from low frequency source,
  1204. * higher error gain can apply such as 0xf which has
  1205. * the most sensitive gain error correction threshold,
  1206. * Therefore, FLL has the most accurate DCO to
  1207. * target frequency.
  1208. */
  1209. regmap_update_bits(regmap, NAU8821_R06_FLL3,
  1210. NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK,
  1211. NAU8821_FLL_CLK_SRC_BLK |
  1212. (0xf << NAU8821_GAIN_ERR_SFT));
  1213. break;
  1214. case NAU8821_CLK_FLL_FS:
  1215. /* If FLL reference input is from low frequency source,
  1216. * higher error gain can apply such as 0xf which has
  1217. * the most sensitive gain error correction threshold,
  1218. * Therefore, FLL has the most accurate DCO to
  1219. * target frequency.
  1220. */
  1221. regmap_update_bits(regmap, NAU8821_R06_FLL3,
  1222. NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK,
  1223. NAU8821_FLL_CLK_SRC_FS |
  1224. (0xf << NAU8821_GAIN_ERR_SFT));
  1225. break;
  1226. default:
  1227. dev_err(nau8821->dev, "Invalid clock id (%d)\n", clk_id);
  1228. return -EINVAL;
  1229. }
  1230. nau8821->clk_id = clk_id;
  1231. dev_dbg(nau8821->dev, "Sysclk is %dHz and clock id is %d\n", freq,
  1232. nau8821->clk_id);
  1233. return 0;
  1234. }
  1235. static int nau8821_set_sysclk(struct snd_soc_component *component, int clk_id,
  1236. int source, unsigned int freq, int dir)
  1237. {
  1238. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  1239. return nau8821_configure_sysclk(nau8821, clk_id, freq);
  1240. }
  1241. static int nau8821_resume_setup(struct nau8821 *nau8821)
  1242. {
  1243. struct regmap *regmap = nau8821->regmap;
  1244. /* Close clock when jack type detection at manual mode */
  1245. nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0);
  1246. if (nau8821->irq) {
  1247. /* Clear all interruption status */
  1248. nau8821_int_status_clear_all(regmap);
  1249. /* Enable both insertion and ejection interruptions, and then
  1250. * bypass de-bounce circuit.
  1251. */
  1252. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
  1253. NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN, 0);
  1254. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  1255. NAU8821_JACK_DET_DB_BYPASS,
  1256. NAU8821_JACK_DET_DB_BYPASS);
  1257. regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL,
  1258. NAU8821_IRQ_INSERT_DIS | NAU8821_IRQ_EJECT_DIS, 0);
  1259. }
  1260. return 0;
  1261. }
  1262. static int nau8821_set_bias_level(struct snd_soc_component *component,
  1263. enum snd_soc_bias_level level)
  1264. {
  1265. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  1266. struct regmap *regmap = nau8821->regmap;
  1267. switch (level) {
  1268. case SND_SOC_BIAS_ON:
  1269. break;
  1270. case SND_SOC_BIAS_PREPARE:
  1271. break;
  1272. case SND_SOC_BIAS_STANDBY:
  1273. /* Setup codec configuration after resume */
  1274. if (snd_soc_component_get_bias_level(component) ==
  1275. SND_SOC_BIAS_OFF)
  1276. nau8821_resume_setup(nau8821);
  1277. break;
  1278. case SND_SOC_BIAS_OFF:
  1279. /* HPL/HPR short to ground */
  1280. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  1281. NAU8821_SPKR_DWN1R | NAU8821_SPKR_DWN1L, 0);
  1282. if (nau8821->irq) {
  1283. /* Reset the configuration of jack type for detection.
  1284. * Detach 2kOhm Resistors from MICBIAS to MICGND1/2.
  1285. */
  1286. regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
  1287. NAU8821_MICBIAS_JKR2, 0);
  1288. /* Turn off all interruptions before system shutdown.
  1289. * Keep theinterruption quiet before resume
  1290. * setup completes.
  1291. */
  1292. regmap_write(regmap,
  1293. NAU8821_R12_INTERRUPT_DIS_CTRL, 0xffff);
  1294. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
  1295. NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN,
  1296. NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN);
  1297. }
  1298. break;
  1299. default:
  1300. break;
  1301. }
  1302. return 0;
  1303. }
  1304. static int __maybe_unused nau8821_suspend(struct snd_soc_component *component)
  1305. {
  1306. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  1307. if (nau8821->irq)
  1308. disable_irq(nau8821->irq);
  1309. snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
  1310. /* Power down codec power; don't support button wakeup */
  1311. snd_soc_component_disable_pin(component, "MICBIAS");
  1312. snd_soc_dapm_sync(nau8821->dapm);
  1313. regcache_cache_only(nau8821->regmap, true);
  1314. regcache_mark_dirty(nau8821->regmap);
  1315. return 0;
  1316. }
  1317. static int __maybe_unused nau8821_resume(struct snd_soc_component *component)
  1318. {
  1319. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  1320. regcache_cache_only(nau8821->regmap, false);
  1321. regcache_sync(nau8821->regmap);
  1322. if (nau8821->irq)
  1323. enable_irq(nau8821->irq);
  1324. return 0;
  1325. }
  1326. static const struct snd_soc_component_driver nau8821_component_driver = {
  1327. .probe = nau8821_component_probe,
  1328. .set_sysclk = nau8821_set_sysclk,
  1329. .set_pll = nau8821_set_fll,
  1330. .set_bias_level = nau8821_set_bias_level,
  1331. .suspend = nau8821_suspend,
  1332. .resume = nau8821_resume,
  1333. .controls = nau8821_controls,
  1334. .num_controls = ARRAY_SIZE(nau8821_controls),
  1335. .dapm_widgets = nau8821_dapm_widgets,
  1336. .num_dapm_widgets = ARRAY_SIZE(nau8821_dapm_widgets),
  1337. .dapm_routes = nau8821_dapm_routes,
  1338. .num_dapm_routes = ARRAY_SIZE(nau8821_dapm_routes),
  1339. .suspend_bias_off = 1,
  1340. .idle_bias_on = 1,
  1341. .use_pmdown_time = 1,
  1342. .endianness = 1,
  1343. };
  1344. /**
  1345. * nau8821_enable_jack_detect - Specify a jack for event reporting
  1346. *
  1347. * @component: component to register the jack with
  1348. * @jack: jack to use to report headset and button events on
  1349. *
  1350. * After this function has been called the headset insert/remove and button
  1351. * events will be routed to the given jack. Jack can be null to stop
  1352. * reporting.
  1353. */
  1354. int nau8821_enable_jack_detect(struct snd_soc_component *component,
  1355. struct snd_soc_jack *jack)
  1356. {
  1357. struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
  1358. int ret;
  1359. nau8821->jack = jack;
  1360. /* Initiate jack detection work queue */
  1361. INIT_WORK(&nau8821->jdet_work, nau8821_jdet_work);
  1362. ret = devm_request_threaded_irq(nau8821->dev, nau8821->irq, NULL,
  1363. nau8821_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  1364. "nau8821", nau8821);
  1365. if (ret) {
  1366. dev_err(nau8821->dev, "Cannot request irq %d (%d)\n",
  1367. nau8821->irq, ret);
  1368. return ret;
  1369. }
  1370. return ret;
  1371. }
  1372. EXPORT_SYMBOL_GPL(nau8821_enable_jack_detect);
  1373. static void nau8821_reset_chip(struct regmap *regmap)
  1374. {
  1375. regmap_write(regmap, NAU8821_R00_RESET, 0xffff);
  1376. regmap_write(regmap, NAU8821_R00_RESET, 0xffff);
  1377. }
  1378. static void nau8821_print_device_properties(struct nau8821 *nau8821)
  1379. {
  1380. struct device *dev = nau8821->dev;
  1381. dev_dbg(dev, "jkdet-enable: %d\n", nau8821->jkdet_enable);
  1382. dev_dbg(dev, "jkdet-pull-enable: %d\n", nau8821->jkdet_pull_enable);
  1383. dev_dbg(dev, "jkdet-pull-up: %d\n", nau8821->jkdet_pull_up);
  1384. dev_dbg(dev, "jkdet-polarity: %d\n", nau8821->jkdet_polarity);
  1385. dev_dbg(dev, "micbias-voltage: %d\n", nau8821->micbias_voltage);
  1386. dev_dbg(dev, "vref-impedance: %d\n", nau8821->vref_impedance);
  1387. dev_dbg(dev, "jack-insert-debounce: %d\n",
  1388. nau8821->jack_insert_debounce);
  1389. dev_dbg(dev, "jack-eject-debounce: %d\n",
  1390. nau8821->jack_eject_debounce);
  1391. dev_dbg(dev, "dmic-clk-threshold: %d\n",
  1392. nau8821->dmic_clk_threshold);
  1393. dev_dbg(dev, "key_enable: %d\n", nau8821->key_enable);
  1394. }
  1395. static int nau8821_read_device_properties(struct device *dev,
  1396. struct nau8821 *nau8821)
  1397. {
  1398. int ret;
  1399. nau8821->jkdet_enable = device_property_read_bool(dev,
  1400. "nuvoton,jkdet-enable");
  1401. nau8821->jkdet_pull_enable = device_property_read_bool(dev,
  1402. "nuvoton,jkdet-pull-enable");
  1403. nau8821->jkdet_pull_up = device_property_read_bool(dev,
  1404. "nuvoton,jkdet-pull-up");
  1405. nau8821->key_enable = device_property_read_bool(dev,
  1406. "nuvoton,key-enable");
  1407. ret = device_property_read_u32(dev, "nuvoton,jkdet-polarity",
  1408. &nau8821->jkdet_polarity);
  1409. if (ret)
  1410. nau8821->jkdet_polarity = 1;
  1411. ret = device_property_read_u32(dev, "nuvoton,micbias-voltage",
  1412. &nau8821->micbias_voltage);
  1413. if (ret)
  1414. nau8821->micbias_voltage = 6;
  1415. ret = device_property_read_u32(dev, "nuvoton,vref-impedance",
  1416. &nau8821->vref_impedance);
  1417. if (ret)
  1418. nau8821->vref_impedance = 2;
  1419. ret = device_property_read_u32(dev, "nuvoton,jack-insert-debounce",
  1420. &nau8821->jack_insert_debounce);
  1421. if (ret)
  1422. nau8821->jack_insert_debounce = 7;
  1423. ret = device_property_read_u32(dev, "nuvoton,jack-eject-debounce",
  1424. &nau8821->jack_eject_debounce);
  1425. if (ret)
  1426. nau8821->jack_eject_debounce = 0;
  1427. ret = device_property_read_u32(dev, "nuvoton,dmic-clk-threshold",
  1428. &nau8821->dmic_clk_threshold);
  1429. if (ret)
  1430. nau8821->dmic_clk_threshold = 3072000;
  1431. return 0;
  1432. }
  1433. static void nau8821_init_regs(struct nau8821 *nau8821)
  1434. {
  1435. struct regmap *regmap = nau8821->regmap;
  1436. /* Enable Bias/Vmid */
  1437. regmap_update_bits(regmap, NAU8821_R66_BIAS_ADJ,
  1438. NAU8821_BIAS_VMID, NAU8821_BIAS_VMID);
  1439. regmap_update_bits(regmap, NAU8821_R76_BOOST,
  1440. NAU8821_GLOBAL_BIAS_EN, NAU8821_GLOBAL_BIAS_EN);
  1441. /* VMID Tieoff setting and enable TESTDAC.
  1442. * This sets the analog DAC inputs to a '0' input signal to avoid
  1443. * any glitches due to power up transients in both the analog and
  1444. * digital DAC circuit.
  1445. */
  1446. regmap_update_bits(regmap, NAU8821_R66_BIAS_ADJ,
  1447. NAU8821_BIAS_VMID_SEL_MASK | NAU8821_BIAS_TESTDAC_EN,
  1448. (nau8821->vref_impedance << NAU8821_BIAS_VMID_SEL_SFT) |
  1449. NAU8821_BIAS_TESTDAC_EN);
  1450. /* Disable short Frame Sync detection logic */
  1451. regmap_update_bits(regmap, NAU8821_R1E_LEFT_TIME_SLOT,
  1452. NAU8821_DIS_FS_SHORT_DET, NAU8821_DIS_FS_SHORT_DET);
  1453. /* Disable Boost Driver, Automatic Short circuit protection enable */
  1454. regmap_update_bits(regmap, NAU8821_R76_BOOST,
  1455. NAU8821_PRECHARGE_DIS | NAU8821_HP_BOOST_DIS |
  1456. NAU8821_HP_BOOST_G_DIS | NAU8821_SHORT_SHUTDOWN_EN,
  1457. NAU8821_PRECHARGE_DIS | NAU8821_HP_BOOST_DIS |
  1458. NAU8821_HP_BOOST_G_DIS | NAU8821_SHORT_SHUTDOWN_EN);
  1459. /* Class G timer 64ms */
  1460. regmap_update_bits(regmap, NAU8821_R4B_CLASSG_CTRL,
  1461. NAU8821_CLASSG_TIMER_MASK,
  1462. 0x20 << NAU8821_CLASSG_TIMER_SFT);
  1463. /* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */
  1464. regmap_update_bits(regmap, NAU8821_R6A_ANALOG_CONTROL_2,
  1465. NAU8821_HP_NON_CLASSG_CURRENT_2xADJ |
  1466. NAU8821_DAC_CAPACITOR_MSB | NAU8821_DAC_CAPACITOR_LSB,
  1467. NAU8821_HP_NON_CLASSG_CURRENT_2xADJ |
  1468. NAU8821_DAC_CAPACITOR_MSB | NAU8821_DAC_CAPACITOR_LSB);
  1469. /* Disable DACR/L power */
  1470. regmap_update_bits(regmap, NAU8821_R80_CHARGE_PUMP,
  1471. NAU8821_POWER_DOWN_DACR | NAU8821_POWER_DOWN_DACL, 0);
  1472. /* DAC clock delay 2ns, VREF */
  1473. regmap_update_bits(regmap, NAU8821_R73_RDAC,
  1474. NAU8821_DAC_CLK_DELAY_MASK | NAU8821_DAC_VREF_MASK,
  1475. (0x2 << NAU8821_DAC_CLK_DELAY_SFT) |
  1476. (0x3 << NAU8821_DAC_VREF_SFT));
  1477. regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
  1478. NAU8821_MICBIAS_VOLTAGE_MASK, nau8821->micbias_voltage);
  1479. /* Default oversampling/decimations settings are unusable
  1480. * (audible hiss). Set it to something better.
  1481. */
  1482. regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE,
  1483. NAU8821_ADC_SYNC_DOWN_MASK, NAU8821_ADC_SYNC_DOWN_64);
  1484. regmap_update_bits(regmap, NAU8821_R2C_DAC_CTRL1,
  1485. NAU8821_DAC_OVERSAMPLE_MASK, NAU8821_DAC_OVERSAMPLE_64);
  1486. }
  1487. static int nau8821_setup_irq(struct nau8821 *nau8821)
  1488. {
  1489. struct regmap *regmap = nau8821->regmap;
  1490. /* Jack detection */
  1491. regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL,
  1492. NAU8821_JKDET_OUTPUT_EN,
  1493. nau8821->jkdet_enable ? 0 : NAU8821_JKDET_OUTPUT_EN);
  1494. regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL,
  1495. NAU8821_JKDET_PULL_EN,
  1496. nau8821->jkdet_pull_enable ? 0 : NAU8821_JKDET_PULL_EN);
  1497. regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL,
  1498. NAU8821_JKDET_PULL_UP,
  1499. nau8821->jkdet_pull_up ? NAU8821_JKDET_PULL_UP : 0);
  1500. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  1501. NAU8821_JACK_POLARITY,
  1502. /* jkdet_polarity - 1 is for active-low */
  1503. nau8821->jkdet_polarity ? 0 : NAU8821_JACK_POLARITY);
  1504. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  1505. NAU8821_JACK_INSERT_DEBOUNCE_MASK,
  1506. nau8821->jack_insert_debounce <<
  1507. NAU8821_JACK_INSERT_DEBOUNCE_SFT);
  1508. regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
  1509. NAU8821_JACK_EJECT_DEBOUNCE_MASK,
  1510. nau8821->jack_eject_debounce <<
  1511. NAU8821_JACK_EJECT_DEBOUNCE_SFT);
  1512. /* Pull up IRQ pin */
  1513. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
  1514. NAU8821_IRQ_PIN_PULL_UP | NAU8821_IRQ_PIN_PULL_EN |
  1515. NAU8821_IRQ_OUTPUT_EN, NAU8821_IRQ_PIN_PULL_UP |
  1516. NAU8821_IRQ_PIN_PULL_EN | NAU8821_IRQ_OUTPUT_EN);
  1517. /* Disable interruption before codec initiation done */
  1518. /* Mask unneeded IRQs: 1 - disable, 0 - enable */
  1519. regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 0x3f5, 0x3f5);
  1520. return 0;
  1521. }
  1522. /* Please keep this list alphabetically sorted */
  1523. static const struct dmi_system_id nau8821_quirk_table[] = {
  1524. {
  1525. /* Positivo CW14Q01P-V2 */
  1526. .matches = {
  1527. DMI_MATCH(DMI_SYS_VENDOR, "Positivo Tecnologia SA"),
  1528. DMI_MATCH(DMI_BOARD_NAME, "CW14Q01P-V2"),
  1529. },
  1530. .driver_data = (void *)(NAU8821_JD_ACTIVE_HIGH),
  1531. },
  1532. {}
  1533. };
  1534. static void nau8821_check_quirks(void)
  1535. {
  1536. const struct dmi_system_id *dmi_id;
  1537. if (quirk_override != -1) {
  1538. nau8821_quirk = quirk_override;
  1539. return;
  1540. }
  1541. dmi_id = dmi_first_match(nau8821_quirk_table);
  1542. if (dmi_id)
  1543. nau8821_quirk = (unsigned long)dmi_id->driver_data;
  1544. }
  1545. static int nau8821_i2c_probe(struct i2c_client *i2c)
  1546. {
  1547. struct device *dev = &i2c->dev;
  1548. struct nau8821 *nau8821 = dev_get_platdata(&i2c->dev);
  1549. int ret, value;
  1550. if (!nau8821) {
  1551. nau8821 = devm_kzalloc(dev, sizeof(*nau8821), GFP_KERNEL);
  1552. if (!nau8821)
  1553. return -ENOMEM;
  1554. nau8821_read_device_properties(dev, nau8821);
  1555. }
  1556. i2c_set_clientdata(i2c, nau8821);
  1557. nau8821->regmap = devm_regmap_init_i2c(i2c, &nau8821_regmap_config);
  1558. if (IS_ERR(nau8821->regmap))
  1559. return PTR_ERR(nau8821->regmap);
  1560. nau8821->dev = dev;
  1561. nau8821->irq = i2c->irq;
  1562. nau8821_check_quirks();
  1563. if (nau8821_quirk & NAU8821_JD_ACTIVE_HIGH)
  1564. nau8821->jkdet_polarity = 0;
  1565. nau8821_print_device_properties(nau8821);
  1566. nau8821_reset_chip(nau8821->regmap);
  1567. ret = regmap_read(nau8821->regmap, NAU8821_R58_I2C_DEVICE_ID, &value);
  1568. if (ret) {
  1569. dev_err(dev, "Failed to read device id (%d)\n", ret);
  1570. return ret;
  1571. }
  1572. nau8821_init_regs(nau8821);
  1573. if (i2c->irq)
  1574. nau8821_setup_irq(nau8821);
  1575. ret = devm_snd_soc_register_component(&i2c->dev,
  1576. &nau8821_component_driver, &nau8821_dai, 1);
  1577. return ret;
  1578. }
  1579. static const struct i2c_device_id nau8821_i2c_ids[] = {
  1580. { "nau8821", 0 },
  1581. { }
  1582. };
  1583. MODULE_DEVICE_TABLE(i2c, nau8821_i2c_ids);
  1584. #ifdef CONFIG_OF
  1585. static const struct of_device_id nau8821_of_ids[] = {
  1586. { .compatible = "nuvoton,nau8821", },
  1587. {}
  1588. };
  1589. MODULE_DEVICE_TABLE(of, nau8821_of_ids);
  1590. #endif
  1591. #ifdef CONFIG_ACPI
  1592. static const struct acpi_device_id nau8821_acpi_match[] = {
  1593. { "NVTN2020", 0 },
  1594. {},
  1595. };
  1596. MODULE_DEVICE_TABLE(acpi, nau8821_acpi_match);
  1597. #endif
  1598. static struct i2c_driver nau8821_driver = {
  1599. .driver = {
  1600. .name = "nau8821",
  1601. .of_match_table = of_match_ptr(nau8821_of_ids),
  1602. .acpi_match_table = ACPI_PTR(nau8821_acpi_match),
  1603. },
  1604. .probe_new = nau8821_i2c_probe,
  1605. .id_table = nau8821_i2c_ids,
  1606. };
  1607. module_i2c_driver(nau8821_driver);
  1608. MODULE_DESCRIPTION("ASoC nau8821 driver");
  1609. MODULE_AUTHOR("John Hsu <[email protected]>");
  1610. MODULE_AUTHOR("Seven Lee <[email protected]>");
  1611. MODULE_LICENSE("GPL");