nau8810.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * nau8810.c -- NAU8810 ALSA Soc Audio driver
  3. *
  4. * Copyright 2016 Nuvoton Technology Corp.
  5. *
  6. * Author: David Lin <ctlin0@nuvoton.com>
  7. *
  8. * Based on WM8974.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/pm.h>
  20. #include <linux/i2c.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "nau8810.h"
  30. #define NAU_PLL_FREQ_MAX 100000000
  31. #define NAU_PLL_FREQ_MIN 90000000
  32. #define NAU_PLL_REF_MAX 33000000
  33. #define NAU_PLL_REF_MIN 8000000
  34. #define NAU_PLL_OPTOP_MIN 6
  35. static const int nau8810_mclk_scaler[] = { 10, 15, 20, 30, 40, 60, 80, 120 };
  36. static const struct reg_default nau8810_reg_defaults[] = {
  37. { NAU8810_REG_POWER1, 0x0000 },
  38. { NAU8810_REG_POWER2, 0x0000 },
  39. { NAU8810_REG_POWER3, 0x0000 },
  40. { NAU8810_REG_IFACE, 0x0050 },
  41. { NAU8810_REG_COMP, 0x0000 },
  42. { NAU8810_REG_CLOCK, 0x0140 },
  43. { NAU8810_REG_SMPLR, 0x0000 },
  44. { NAU8810_REG_DAC, 0x0000 },
  45. { NAU8810_REG_DACGAIN, 0x00FF },
  46. { NAU8810_REG_ADC, 0x0100 },
  47. { NAU8810_REG_ADCGAIN, 0x00FF },
  48. { NAU8810_REG_EQ1, 0x012C },
  49. { NAU8810_REG_EQ2, 0x002C },
  50. { NAU8810_REG_EQ3, 0x002C },
  51. { NAU8810_REG_EQ4, 0x002C },
  52. { NAU8810_REG_EQ5, 0x002C },
  53. { NAU8810_REG_DACLIM1, 0x0032 },
  54. { NAU8810_REG_DACLIM2, 0x0000 },
  55. { NAU8810_REG_NOTCH1, 0x0000 },
  56. { NAU8810_REG_NOTCH2, 0x0000 },
  57. { NAU8810_REG_NOTCH3, 0x0000 },
  58. { NAU8810_REG_NOTCH4, 0x0000 },
  59. { NAU8810_REG_ALC1, 0x0038 },
  60. { NAU8810_REG_ALC2, 0x000B },
  61. { NAU8810_REG_ALC3, 0x0032 },
  62. { NAU8810_REG_NOISEGATE, 0x0000 },
  63. { NAU8810_REG_PLLN, 0x0008 },
  64. { NAU8810_REG_PLLK1, 0x000C },
  65. { NAU8810_REG_PLLK2, 0x0093 },
  66. { NAU8810_REG_PLLK3, 0x00E9 },
  67. { NAU8810_REG_ATTEN, 0x0000 },
  68. { NAU8810_REG_INPUT_SIGNAL, 0x0003 },
  69. { NAU8810_REG_PGAGAIN, 0x0010 },
  70. { NAU8810_REG_ADCBOOST, 0x0100 },
  71. { NAU8810_REG_OUTPUT, 0x0002 },
  72. { NAU8810_REG_SPKMIX, 0x0001 },
  73. { NAU8810_REG_SPKGAIN, 0x0039 },
  74. { NAU8810_REG_MONOMIX, 0x0001 },
  75. { NAU8810_REG_POWER4, 0x0000 },
  76. { NAU8810_REG_TSLOTCTL1, 0x0000 },
  77. { NAU8810_REG_TSLOTCTL2, 0x0020 },
  78. { NAU8810_REG_DEVICE_REVID, 0x0000 },
  79. { NAU8810_REG_I2C_DEVICEID, 0x001A },
  80. { NAU8810_REG_ADDITIONID, 0x00CA },
  81. { NAU8810_REG_RESERVE, 0x0124 },
  82. { NAU8810_REG_OUTCTL, 0x0001 },
  83. { NAU8810_REG_ALC1ENHAN1, 0x0010 },
  84. { NAU8810_REG_ALC1ENHAN2, 0x0000 },
  85. { NAU8810_REG_MISCCTL, 0x0000 },
  86. { NAU8810_REG_OUTTIEOFF, 0x0000 },
  87. { NAU8810_REG_AGCP2POUT, 0x0000 },
  88. { NAU8810_REG_AGCPOUT, 0x0000 },
  89. { NAU8810_REG_AMTCTL, 0x0000 },
  90. { NAU8810_REG_OUTTIEOFFMAN, 0x0000 },
  91. };
  92. static bool nau8810_readable_reg(struct device *dev, unsigned int reg)
  93. {
  94. switch (reg) {
  95. case NAU8810_REG_RESET ... NAU8810_REG_SMPLR:
  96. case NAU8810_REG_DAC ... NAU8810_REG_DACGAIN:
  97. case NAU8810_REG_ADC ... NAU8810_REG_ADCGAIN:
  98. case NAU8810_REG_EQ1 ... NAU8810_REG_EQ5:
  99. case NAU8810_REG_DACLIM1 ... NAU8810_REG_DACLIM2:
  100. case NAU8810_REG_NOTCH1 ... NAU8810_REG_NOTCH4:
  101. case NAU8810_REG_ALC1 ... NAU8810_REG_ATTEN:
  102. case NAU8810_REG_INPUT_SIGNAL ... NAU8810_REG_PGAGAIN:
  103. case NAU8810_REG_ADCBOOST:
  104. case NAU8810_REG_OUTPUT ... NAU8810_REG_SPKMIX:
  105. case NAU8810_REG_SPKGAIN:
  106. case NAU8810_REG_MONOMIX:
  107. case NAU8810_REG_POWER4 ... NAU8810_REG_TSLOTCTL2:
  108. case NAU8810_REG_DEVICE_REVID ... NAU8810_REG_RESERVE:
  109. case NAU8810_REG_OUTCTL ... NAU8810_REG_ALC1ENHAN2:
  110. case NAU8810_REG_MISCCTL:
  111. case NAU8810_REG_OUTTIEOFF ... NAU8810_REG_OUTTIEOFFMAN:
  112. return true;
  113. default:
  114. return false;
  115. }
  116. }
  117. static bool nau8810_writeable_reg(struct device *dev, unsigned int reg)
  118. {
  119. switch (reg) {
  120. case NAU8810_REG_RESET ... NAU8810_REG_SMPLR:
  121. case NAU8810_REG_DAC ... NAU8810_REG_DACGAIN:
  122. case NAU8810_REG_ADC ... NAU8810_REG_ADCGAIN:
  123. case NAU8810_REG_EQ1 ... NAU8810_REG_EQ5:
  124. case NAU8810_REG_DACLIM1 ... NAU8810_REG_DACLIM2:
  125. case NAU8810_REG_NOTCH1 ... NAU8810_REG_NOTCH4:
  126. case NAU8810_REG_ALC1 ... NAU8810_REG_ATTEN:
  127. case NAU8810_REG_INPUT_SIGNAL ... NAU8810_REG_PGAGAIN:
  128. case NAU8810_REG_ADCBOOST:
  129. case NAU8810_REG_OUTPUT ... NAU8810_REG_SPKMIX:
  130. case NAU8810_REG_SPKGAIN:
  131. case NAU8810_REG_MONOMIX:
  132. case NAU8810_REG_POWER4 ... NAU8810_REG_TSLOTCTL2:
  133. case NAU8810_REG_OUTCTL ... NAU8810_REG_ALC1ENHAN2:
  134. case NAU8810_REG_MISCCTL:
  135. case NAU8810_REG_OUTTIEOFF ... NAU8810_REG_OUTTIEOFFMAN:
  136. return true;
  137. default:
  138. return false;
  139. }
  140. }
  141. static bool nau8810_volatile_reg(struct device *dev, unsigned int reg)
  142. {
  143. switch (reg) {
  144. case NAU8810_REG_RESET:
  145. case NAU8810_REG_DEVICE_REVID ... NAU8810_REG_RESERVE:
  146. return true;
  147. default:
  148. return false;
  149. }
  150. }
  151. /* The EQ parameters get function is to get the 5 band equalizer control.
  152. * The regmap raw read can't work here because regmap doesn't provide
  153. * value format for value width of 9 bits. Therefore, the driver reads data
  154. * from cache and makes value format according to the endianness of
  155. * bytes type control element.
  156. */
  157. static int nau8810_eq_get(struct snd_kcontrol *kcontrol,
  158. struct snd_ctl_elem_value *ucontrol)
  159. {
  160. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  161. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  162. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  163. int i, reg, reg_val;
  164. u16 *val;
  165. val = (u16 *)ucontrol->value.bytes.data;
  166. reg = NAU8810_REG_EQ1;
  167. for (i = 0; i < params->max / sizeof(u16); i++) {
  168. regmap_read(nau8810->regmap, reg + i, &reg_val);
  169. /* conversion of 16-bit integers between native CPU format
  170. * and big endian format
  171. */
  172. reg_val = cpu_to_be16(reg_val);
  173. memcpy(val + i, &reg_val, sizeof(reg_val));
  174. }
  175. return 0;
  176. }
  177. /* The EQ parameters put function is to make configuration of 5 band equalizer
  178. * control. These configuration includes central frequency, equalizer gain,
  179. * cut-off frequency, bandwidth control, and equalizer path.
  180. * The regmap raw write can't work here because regmap doesn't provide
  181. * register and value format for register with address 7 bits and value 9 bits.
  182. * Therefore, the driver makes value format according to the endianness of
  183. * bytes type control element and writes data to codec.
  184. */
  185. static int nau8810_eq_put(struct snd_kcontrol *kcontrol,
  186. struct snd_ctl_elem_value *ucontrol)
  187. {
  188. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  189. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  190. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  191. void *data;
  192. u16 *val, value;
  193. int i, reg, ret;
  194. data = kmemdup(ucontrol->value.bytes.data,
  195. params->max, GFP_KERNEL | GFP_DMA);
  196. if (!data)
  197. return -ENOMEM;
  198. val = (u16 *)data;
  199. reg = NAU8810_REG_EQ1;
  200. for (i = 0; i < params->max / sizeof(u16); i++) {
  201. /* conversion of 16-bit integers between native CPU format
  202. * and big endian format
  203. */
  204. value = be16_to_cpu(*(val + i));
  205. ret = regmap_write(nau8810->regmap, reg + i, value);
  206. if (ret) {
  207. dev_err(codec->dev, "EQ configuration fail, register: %x ret: %d\n",
  208. reg + i, ret);
  209. kfree(data);
  210. return ret;
  211. }
  212. }
  213. kfree(data);
  214. return 0;
  215. }
  216. static const char * const nau8810_companding[] = {
  217. "Off", "NC", "u-law", "A-law" };
  218. static const struct soc_enum nau8810_companding_adc_enum =
  219. SOC_ENUM_SINGLE(NAU8810_REG_COMP, NAU8810_ADCCM_SFT,
  220. ARRAY_SIZE(nau8810_companding), nau8810_companding);
  221. static const struct soc_enum nau8810_companding_dac_enum =
  222. SOC_ENUM_SINGLE(NAU8810_REG_COMP, NAU8810_DACCM_SFT,
  223. ARRAY_SIZE(nau8810_companding), nau8810_companding);
  224. static const char * const nau8810_deemp[] = {
  225. "None", "32kHz", "44.1kHz", "48kHz" };
  226. static const struct soc_enum nau8810_deemp_enum =
  227. SOC_ENUM_SINGLE(NAU8810_REG_DAC, NAU8810_DEEMP_SFT,
  228. ARRAY_SIZE(nau8810_deemp), nau8810_deemp);
  229. static const char * const nau8810_eqmode[] = {"Capture", "Playback" };
  230. static const struct soc_enum nau8810_eqmode_enum =
  231. SOC_ENUM_SINGLE(NAU8810_REG_EQ1, NAU8810_EQM_SFT,
  232. ARRAY_SIZE(nau8810_eqmode), nau8810_eqmode);
  233. static const char * const nau8810_alc[] = {"Normal", "Limiter" };
  234. static const struct soc_enum nau8810_alc_enum =
  235. SOC_ENUM_SINGLE(NAU8810_REG_ALC3, NAU8810_ALCM_SFT,
  236. ARRAY_SIZE(nau8810_alc), nau8810_alc);
  237. static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
  238. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  239. static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1200, 75, 0);
  240. static const DECLARE_TLV_DB_SCALE(spk_tlv, -5700, 100, 0);
  241. static const struct snd_kcontrol_new nau8810_snd_controls[] = {
  242. SOC_ENUM("ADC Companding", nau8810_companding_adc_enum),
  243. SOC_ENUM("DAC Companding", nau8810_companding_dac_enum),
  244. SOC_ENUM("DAC De-emphasis", nau8810_deemp_enum),
  245. SOC_ENUM("EQ Function", nau8810_eqmode_enum),
  246. SND_SOC_BYTES_EXT("EQ Parameters", 10,
  247. nau8810_eq_get, nau8810_eq_put),
  248. SOC_SINGLE("DAC Inversion Switch", NAU8810_REG_DAC,
  249. NAU8810_DACPL_SFT, 1, 0),
  250. SOC_SINGLE_TLV("Playback Volume", NAU8810_REG_DACGAIN,
  251. NAU8810_DACGAIN_SFT, 0xff, 0, digital_tlv),
  252. SOC_SINGLE("High Pass Filter Switch", NAU8810_REG_ADC,
  253. NAU8810_HPFEN_SFT, 1, 0),
  254. SOC_SINGLE("High Pass Cut Off", NAU8810_REG_ADC,
  255. NAU8810_HPF_SFT, 0x7, 0),
  256. SOC_SINGLE("ADC Inversion Switch", NAU8810_REG_ADC,
  257. NAU8810_ADCPL_SFT, 1, 0),
  258. SOC_SINGLE_TLV("Capture Volume", NAU8810_REG_ADCGAIN,
  259. NAU8810_ADCGAIN_SFT, 0xff, 0, digital_tlv),
  260. SOC_SINGLE_TLV("EQ1 Volume", NAU8810_REG_EQ1,
  261. NAU8810_EQ1GC_SFT, 0x18, 1, eq_tlv),
  262. SOC_SINGLE_TLV("EQ2 Volume", NAU8810_REG_EQ2,
  263. NAU8810_EQ2GC_SFT, 0x18, 1, eq_tlv),
  264. SOC_SINGLE_TLV("EQ3 Volume", NAU8810_REG_EQ3,
  265. NAU8810_EQ3GC_SFT, 0x18, 1, eq_tlv),
  266. SOC_SINGLE_TLV("EQ4 Volume", NAU8810_REG_EQ4,
  267. NAU8810_EQ4GC_SFT, 0x18, 1, eq_tlv),
  268. SOC_SINGLE_TLV("EQ5 Volume", NAU8810_REG_EQ5,
  269. NAU8810_EQ5GC_SFT, 0x18, 1, eq_tlv),
  270. SOC_SINGLE("DAC Limiter Switch", NAU8810_REG_DACLIM1,
  271. NAU8810_DACLIMEN_SFT, 1, 0),
  272. SOC_SINGLE("DAC Limiter Decay", NAU8810_REG_DACLIM1,
  273. NAU8810_DACLIMDCY_SFT, 0xf, 0),
  274. SOC_SINGLE("DAC Limiter Attack", NAU8810_REG_DACLIM1,
  275. NAU8810_DACLIMATK_SFT, 0xf, 0),
  276. SOC_SINGLE("DAC Limiter Threshold", NAU8810_REG_DACLIM2,
  277. NAU8810_DACLIMTHL_SFT, 0x7, 0),
  278. SOC_SINGLE("DAC Limiter Boost", NAU8810_REG_DACLIM2,
  279. NAU8810_DACLIMBST_SFT, 0xf, 0),
  280. SOC_ENUM("ALC Mode", nau8810_alc_enum),
  281. SOC_SINGLE("ALC Enable Switch", NAU8810_REG_ALC1,
  282. NAU8810_ALCEN_SFT, 1, 0),
  283. SOC_SINGLE("ALC Max Volume", NAU8810_REG_ALC1,
  284. NAU8810_ALCMXGAIN_SFT, 0x7, 0),
  285. SOC_SINGLE("ALC Min Volume", NAU8810_REG_ALC1,
  286. NAU8810_ALCMINGAIN_SFT, 0x7, 0),
  287. SOC_SINGLE("ALC ZC Switch", NAU8810_REG_ALC2,
  288. NAU8810_ALCZC_SFT, 1, 0),
  289. SOC_SINGLE("ALC Hold", NAU8810_REG_ALC2,
  290. NAU8810_ALCHT_SFT, 0xf, 0),
  291. SOC_SINGLE("ALC Target", NAU8810_REG_ALC2,
  292. NAU8810_ALCSL_SFT, 0xf, 0),
  293. SOC_SINGLE("ALC Decay", NAU8810_REG_ALC3,
  294. NAU8810_ALCDCY_SFT, 0xf, 0),
  295. SOC_SINGLE("ALC Attack", NAU8810_REG_ALC3,
  296. NAU8810_ALCATK_SFT, 0xf, 0),
  297. SOC_SINGLE("ALC Noise Gate Switch", NAU8810_REG_NOISEGATE,
  298. NAU8810_ALCNEN_SFT, 1, 0),
  299. SOC_SINGLE("ALC Noise Gate Threshold", NAU8810_REG_NOISEGATE,
  300. NAU8810_ALCNTH_SFT, 0x7, 0),
  301. SOC_SINGLE("PGA ZC Switch", NAU8810_REG_PGAGAIN,
  302. NAU8810_PGAZC_SFT, 1, 0),
  303. SOC_SINGLE_TLV("PGA Volume", NAU8810_REG_PGAGAIN,
  304. NAU8810_PGAGAIN_SFT, 0x3f, 0, inpga_tlv),
  305. SOC_SINGLE("Speaker ZC Switch", NAU8810_REG_SPKGAIN,
  306. NAU8810_SPKZC_SFT, 1, 0),
  307. SOC_SINGLE("Speaker Mute Switch", NAU8810_REG_SPKGAIN,
  308. NAU8810_SPKMT_SFT, 1, 0),
  309. SOC_SINGLE_TLV("Speaker Volume", NAU8810_REG_SPKGAIN,
  310. NAU8810_SPKGAIN_SFT, 0x3f, 0, spk_tlv),
  311. SOC_SINGLE("Capture Boost(+20dB)", NAU8810_REG_ADCBOOST,
  312. NAU8810_PGABST_SFT, 1, 0),
  313. SOC_SINGLE("Mono Mute Switch", NAU8810_REG_MONOMIX,
  314. NAU8810_MOUTMXMT_SFT, 1, 0),
  315. SOC_SINGLE("DAC Oversampling Rate(128x) Switch", NAU8810_REG_DAC,
  316. NAU8810_DACOS_SFT, 1, 0),
  317. SOC_SINGLE("ADC Oversampling Rate(128x) Switch", NAU8810_REG_ADC,
  318. NAU8810_ADCOS_SFT, 1, 0),
  319. };
  320. /* Speaker Output Mixer */
  321. static const struct snd_kcontrol_new nau8810_speaker_mixer_controls[] = {
  322. SOC_DAPM_SINGLE("Line Bypass Switch", NAU8810_REG_SPKMIX,
  323. NAU8810_BYPSPK_SFT, 1, 0),
  324. SOC_DAPM_SINGLE("PCM Playback Switch", NAU8810_REG_SPKMIX,
  325. NAU8810_DACSPK_SFT, 1, 0),
  326. };
  327. /* Mono Output Mixer */
  328. static const struct snd_kcontrol_new nau8810_mono_mixer_controls[] = {
  329. SOC_DAPM_SINGLE("Line Bypass Switch", NAU8810_REG_MONOMIX,
  330. NAU8810_BYPMOUT_SFT, 1, 0),
  331. SOC_DAPM_SINGLE("PCM Playback Switch", NAU8810_REG_MONOMIX,
  332. NAU8810_DACMOUT_SFT, 1, 0),
  333. };
  334. /* PGA Mute */
  335. static const struct snd_kcontrol_new nau8810_inpga_mute[] = {
  336. SOC_DAPM_SINGLE("PGA Mute Switch", NAU8810_REG_PGAGAIN,
  337. NAU8810_PGAMT_SFT, 1, 0),
  338. };
  339. /* Input PGA */
  340. static const struct snd_kcontrol_new nau8810_inpga[] = {
  341. SOC_DAPM_SINGLE("MicN Switch", NAU8810_REG_INPUT_SIGNAL,
  342. NAU8810_NMICPGA_SFT, 1, 0),
  343. SOC_DAPM_SINGLE("MicP Switch", NAU8810_REG_INPUT_SIGNAL,
  344. NAU8810_PMICPGA_SFT, 1, 0),
  345. };
  346. /* Mic Input boost vol */
  347. static const struct snd_kcontrol_new nau8810_mic_boost_controls =
  348. SOC_DAPM_SINGLE("Mic Volume", NAU8810_REG_ADCBOOST,
  349. NAU8810_PMICBSTGAIN_SFT, 0x7, 0);
  350. /* Loopback Switch */
  351. static const struct snd_kcontrol_new nau8810_loopback =
  352. SOC_DAPM_SINGLE("Switch", NAU8810_REG_COMP,
  353. NAU8810_ADDAP_SFT, 1, 0);
  354. static int check_mclk_select_pll(struct snd_soc_dapm_widget *source,
  355. struct snd_soc_dapm_widget *sink)
  356. {
  357. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  358. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  359. unsigned int value;
  360. regmap_read(nau8810->regmap, NAU8810_REG_CLOCK, &value);
  361. return (value & NAU8810_CLKM_MASK);
  362. }
  363. static const struct snd_soc_dapm_widget nau8810_dapm_widgets[] = {
  364. SND_SOC_DAPM_MIXER("Speaker Mixer", NAU8810_REG_POWER3,
  365. NAU8810_SPKMX_EN_SFT, 0, &nau8810_speaker_mixer_controls[0],
  366. ARRAY_SIZE(nau8810_speaker_mixer_controls)),
  367. SND_SOC_DAPM_MIXER("Mono Mixer", NAU8810_REG_POWER3,
  368. NAU8810_MOUTMX_EN_SFT, 0, &nau8810_mono_mixer_controls[0],
  369. ARRAY_SIZE(nau8810_mono_mixer_controls)),
  370. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", NAU8810_REG_POWER3,
  371. NAU8810_DAC_EN_SFT, 0),
  372. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", NAU8810_REG_POWER2,
  373. NAU8810_ADC_EN_SFT, 0),
  374. SND_SOC_DAPM_PGA("SpkN Out", NAU8810_REG_POWER3,
  375. NAU8810_NSPK_EN_SFT, 0, NULL, 0),
  376. SND_SOC_DAPM_PGA("SpkP Out", NAU8810_REG_POWER3,
  377. NAU8810_PSPK_EN_SFT, 0, NULL, 0),
  378. SND_SOC_DAPM_PGA("Mono Out", NAU8810_REG_POWER3,
  379. NAU8810_MOUT_EN_SFT, 0, NULL, 0),
  380. SND_SOC_DAPM_MIXER("Input PGA", NAU8810_REG_POWER2,
  381. NAU8810_PGA_EN_SFT, 0, nau8810_inpga,
  382. ARRAY_SIZE(nau8810_inpga)),
  383. SND_SOC_DAPM_MIXER("Input Boost Stage", NAU8810_REG_POWER2,
  384. NAU8810_BST_EN_SFT, 0, nau8810_inpga_mute,
  385. ARRAY_SIZE(nau8810_inpga_mute)),
  386. SND_SOC_DAPM_SUPPLY("Mic Bias", NAU8810_REG_POWER1,
  387. NAU8810_MICBIAS_EN_SFT, 0, NULL, 0),
  388. SND_SOC_DAPM_SUPPLY("PLL", NAU8810_REG_POWER1,
  389. NAU8810_PLL_EN_SFT, 0, NULL, 0),
  390. SND_SOC_DAPM_SWITCH("Digital Loopback", SND_SOC_NOPM, 0, 0,
  391. &nau8810_loopback),
  392. SND_SOC_DAPM_INPUT("MICN"),
  393. SND_SOC_DAPM_INPUT("MICP"),
  394. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  395. SND_SOC_DAPM_OUTPUT("SPKOUTP"),
  396. SND_SOC_DAPM_OUTPUT("SPKOUTN"),
  397. };
  398. static const struct snd_soc_dapm_route nau8810_dapm_routes[] = {
  399. {"DAC", NULL, "PLL", check_mclk_select_pll},
  400. /* Mono output mixer */
  401. {"Mono Mixer", "PCM Playback Switch", "DAC"},
  402. {"Mono Mixer", "Line Bypass Switch", "Input Boost Stage"},
  403. /* Speaker output mixer */
  404. {"Speaker Mixer", "PCM Playback Switch", "DAC"},
  405. {"Speaker Mixer", "Line Bypass Switch", "Input Boost Stage"},
  406. /* Outputs */
  407. {"Mono Out", NULL, "Mono Mixer"},
  408. {"MONOOUT", NULL, "Mono Out"},
  409. {"SpkN Out", NULL, "Speaker Mixer"},
  410. {"SpkP Out", NULL, "Speaker Mixer"},
  411. {"SPKOUTN", NULL, "SpkN Out"},
  412. {"SPKOUTP", NULL, "SpkP Out"},
  413. /* Input Boost Stage */
  414. {"ADC", NULL, "Input Boost Stage"},
  415. {"ADC", NULL, "PLL", check_mclk_select_pll},
  416. {"Input Boost Stage", NULL, "Input PGA"},
  417. {"Input Boost Stage", NULL, "MICP"},
  418. /* Input PGA */
  419. {"Input PGA", NULL, "Mic Bias"},
  420. {"Input PGA", "MicN Switch", "MICN"},
  421. {"Input PGA", "MicP Switch", "MICP"},
  422. /* Digital Looptack */
  423. {"Digital Loopback", "Switch", "ADC"},
  424. {"DAC", NULL, "Digital Loopback"},
  425. };
  426. static int nau8810_set_sysclk(struct snd_soc_dai *dai,
  427. int clk_id, unsigned int freq, int dir)
  428. {
  429. struct snd_soc_codec *codec = dai->codec;
  430. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  431. nau8810->clk_id = clk_id;
  432. nau8810->sysclk = freq;
  433. dev_dbg(nau8810->dev, "master sysclk %dHz, source %s\n",
  434. freq, clk_id == NAU8810_SCLK_PLL ? "PLL" : "MCLK");
  435. return 0;
  436. }
  437. static int nau88l0_calc_pll(unsigned int pll_in,
  438. unsigned int fs, struct nau8810_pll *pll_param)
  439. {
  440. u64 f2, f2_max, pll_ratio;
  441. int i, scal_sel;
  442. if (pll_in > NAU_PLL_REF_MAX || pll_in < NAU_PLL_REF_MIN)
  443. return -EINVAL;
  444. f2_max = 0;
  445. scal_sel = ARRAY_SIZE(nau8810_mclk_scaler);
  446. for (i = 0; i < ARRAY_SIZE(nau8810_mclk_scaler); i++) {
  447. f2 = 256 * fs * 4 * nau8810_mclk_scaler[i] / 10;
  448. if (f2 > NAU_PLL_FREQ_MIN && f2 < NAU_PLL_FREQ_MAX &&
  449. f2_max < f2) {
  450. f2_max = f2;
  451. scal_sel = i;
  452. }
  453. }
  454. if (ARRAY_SIZE(nau8810_mclk_scaler) == scal_sel)
  455. return -EINVAL;
  456. pll_param->mclk_scaler = scal_sel;
  457. f2 = f2_max;
  458. /* Calculate the PLL 4-bit integer input and the PLL 24-bit fractional
  459. * input; round up the 24+4bit.
  460. */
  461. pll_ratio = div_u64(f2 << 28, pll_in);
  462. pll_param->pre_factor = 0;
  463. if (((pll_ratio >> 28) & 0xF) < NAU_PLL_OPTOP_MIN) {
  464. pll_ratio <<= 1;
  465. pll_param->pre_factor = 1;
  466. }
  467. pll_param->pll_int = (pll_ratio >> 28) & 0xF;
  468. pll_param->pll_frac = ((pll_ratio & 0xFFFFFFF) >> 4);
  469. return 0;
  470. }
  471. static int nau8810_set_pll(struct snd_soc_dai *codec_dai, int pll_id,
  472. int source, unsigned int freq_in, unsigned int freq_out)
  473. {
  474. struct snd_soc_codec *codec = codec_dai->codec;
  475. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  476. struct regmap *map = nau8810->regmap;
  477. struct nau8810_pll *pll_param = &nau8810->pll;
  478. int ret, fs;
  479. fs = freq_out / 256;
  480. ret = nau88l0_calc_pll(freq_in, fs, pll_param);
  481. if (ret < 0) {
  482. dev_err(nau8810->dev, "Unsupported input clock %d\n", freq_in);
  483. return ret;
  484. }
  485. dev_info(nau8810->dev, "pll_int=%x pll_frac=%x mclk_scaler=%x pre_factor=%x\n",
  486. pll_param->pll_int, pll_param->pll_frac, pll_param->mclk_scaler,
  487. pll_param->pre_factor);
  488. regmap_update_bits(map, NAU8810_REG_PLLN,
  489. NAU8810_PLLMCLK_DIV2 | NAU8810_PLLN_MASK,
  490. (pll_param->pre_factor ? NAU8810_PLLMCLK_DIV2 : 0) |
  491. pll_param->pll_int);
  492. regmap_write(map, NAU8810_REG_PLLK1,
  493. (pll_param->pll_frac >> NAU8810_PLLK1_SFT) &
  494. NAU8810_PLLK1_MASK);
  495. regmap_write(map, NAU8810_REG_PLLK2,
  496. (pll_param->pll_frac >> NAU8810_PLLK2_SFT) &
  497. NAU8810_PLLK2_MASK);
  498. regmap_write(map, NAU8810_REG_PLLK3,
  499. pll_param->pll_frac & NAU8810_PLLK3_MASK);
  500. regmap_update_bits(map, NAU8810_REG_CLOCK, NAU8810_MCLKSEL_MASK,
  501. pll_param->mclk_scaler << NAU8810_MCLKSEL_SFT);
  502. regmap_update_bits(map, NAU8810_REG_CLOCK,
  503. NAU8810_CLKM_MASK, NAU8810_CLKM_PLL);
  504. return 0;
  505. }
  506. static int nau8810_set_dai_fmt(struct snd_soc_dai *codec_dai,
  507. unsigned int fmt)
  508. {
  509. struct snd_soc_codec *codec = codec_dai->codec;
  510. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  511. u16 ctrl1_val = 0, ctrl2_val = 0;
  512. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  513. case SND_SOC_DAIFMT_CBM_CFM:
  514. ctrl2_val |= NAU8810_CLKIO_MASTER;
  515. break;
  516. case SND_SOC_DAIFMT_CBS_CFS:
  517. break;
  518. default:
  519. return -EINVAL;
  520. }
  521. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  522. case SND_SOC_DAIFMT_I2S:
  523. ctrl1_val |= NAU8810_AIFMT_I2S;
  524. break;
  525. case SND_SOC_DAIFMT_RIGHT_J:
  526. break;
  527. case SND_SOC_DAIFMT_LEFT_J:
  528. ctrl1_val |= NAU8810_AIFMT_LEFT;
  529. break;
  530. case SND_SOC_DAIFMT_DSP_A:
  531. ctrl1_val |= NAU8810_AIFMT_PCM_A;
  532. break;
  533. default:
  534. return -EINVAL;
  535. }
  536. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  537. case SND_SOC_DAIFMT_NB_NF:
  538. break;
  539. case SND_SOC_DAIFMT_IB_IF:
  540. ctrl1_val |= NAU8810_BCLKP_IB | NAU8810_FSP_IF;
  541. break;
  542. case SND_SOC_DAIFMT_IB_NF:
  543. ctrl1_val |= NAU8810_BCLKP_IB;
  544. break;
  545. case SND_SOC_DAIFMT_NB_IF:
  546. ctrl1_val |= NAU8810_FSP_IF;
  547. break;
  548. default:
  549. return -EINVAL;
  550. }
  551. regmap_update_bits(nau8810->regmap, NAU8810_REG_IFACE,
  552. NAU8810_AIFMT_MASK | NAU8810_FSP_IF |
  553. NAU8810_BCLKP_IB, ctrl1_val);
  554. regmap_update_bits(nau8810->regmap, NAU8810_REG_CLOCK,
  555. NAU8810_CLKIO_MASK, ctrl2_val);
  556. return 0;
  557. }
  558. static int nau8810_mclk_clkdiv(struct nau8810 *nau8810, int rate)
  559. {
  560. int i, sclk, imclk = rate * 256, div = 0;
  561. if (!nau8810->sysclk) {
  562. dev_err(nau8810->dev, "Make mclk div configuration fail because of invalid system clock\n");
  563. return -EINVAL;
  564. }
  565. /* Configure the master clock prescaler div to make system
  566. * clock to approximate the internal master clock (IMCLK);
  567. * and large or equal to IMCLK.
  568. */
  569. for (i = 1; i < ARRAY_SIZE(nau8810_mclk_scaler); i++) {
  570. sclk = (nau8810->sysclk * 10) /
  571. nau8810_mclk_scaler[i];
  572. if (sclk < imclk)
  573. break;
  574. div = i;
  575. }
  576. dev_dbg(nau8810->dev,
  577. "master clock prescaler %x for fs %d\n", div, rate);
  578. /* master clock from MCLK and disable PLL */
  579. regmap_update_bits(nau8810->regmap, NAU8810_REG_CLOCK,
  580. NAU8810_MCLKSEL_MASK, (div << NAU8810_MCLKSEL_SFT));
  581. regmap_update_bits(nau8810->regmap, NAU8810_REG_CLOCK,
  582. NAU8810_CLKM_MASK, NAU8810_CLKM_MCLK);
  583. return 0;
  584. }
  585. static int nau8810_pcm_hw_params(struct snd_pcm_substream *substream,
  586. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  587. {
  588. struct snd_soc_codec *codec = dai->codec;
  589. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  590. int val_len = 0, val_rate = 0, ret = 0;
  591. switch (params_width(params)) {
  592. case 16:
  593. break;
  594. case 20:
  595. val_len |= NAU8810_WLEN_20;
  596. break;
  597. case 24:
  598. val_len |= NAU8810_WLEN_24;
  599. break;
  600. case 32:
  601. val_len |= NAU8810_WLEN_32;
  602. break;
  603. }
  604. switch (params_rate(params)) {
  605. case 8000:
  606. val_rate |= NAU8810_SMPLR_8K;
  607. break;
  608. case 11025:
  609. val_rate |= NAU8810_SMPLR_12K;
  610. break;
  611. case 16000:
  612. val_rate |= NAU8810_SMPLR_16K;
  613. break;
  614. case 22050:
  615. val_rate |= NAU8810_SMPLR_24K;
  616. break;
  617. case 32000:
  618. val_rate |= NAU8810_SMPLR_32K;
  619. break;
  620. case 44100:
  621. case 48000:
  622. break;
  623. }
  624. regmap_update_bits(nau8810->regmap, NAU8810_REG_IFACE,
  625. NAU8810_WLEN_MASK, val_len);
  626. regmap_update_bits(nau8810->regmap, NAU8810_REG_SMPLR,
  627. NAU8810_SMPLR_MASK, val_rate);
  628. /* If the master clock is from MCLK, provide the runtime FS for driver
  629. * to get the master clock prescaler configuration.
  630. */
  631. if (nau8810->clk_id == NAU8810_SCLK_MCLK) {
  632. ret = nau8810_mclk_clkdiv(nau8810, params_rate(params));
  633. if (ret < 0)
  634. dev_err(nau8810->dev, "MCLK div configuration fail\n");
  635. }
  636. return ret;
  637. }
  638. static int nau8810_set_bias_level(struct snd_soc_codec *codec,
  639. enum snd_soc_bias_level level)
  640. {
  641. struct nau8810 *nau8810 = snd_soc_codec_get_drvdata(codec);
  642. struct regmap *map = nau8810->regmap;
  643. switch (level) {
  644. case SND_SOC_BIAS_ON:
  645. case SND_SOC_BIAS_PREPARE:
  646. regmap_update_bits(map, NAU8810_REG_POWER1,
  647. NAU8810_REFIMP_MASK, NAU8810_REFIMP_80K);
  648. break;
  649. case SND_SOC_BIAS_STANDBY:
  650. regmap_update_bits(map, NAU8810_REG_POWER1,
  651. NAU8810_IOBUF_EN | NAU8810_ABIAS_EN,
  652. NAU8810_IOBUF_EN | NAU8810_ABIAS_EN);
  653. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  654. regcache_sync(map);
  655. regmap_update_bits(map, NAU8810_REG_POWER1,
  656. NAU8810_REFIMP_MASK, NAU8810_REFIMP_3K);
  657. mdelay(100);
  658. }
  659. regmap_update_bits(map, NAU8810_REG_POWER1,
  660. NAU8810_REFIMP_MASK, NAU8810_REFIMP_300K);
  661. break;
  662. case SND_SOC_BIAS_OFF:
  663. regmap_write(map, NAU8810_REG_POWER1, 0);
  664. regmap_write(map, NAU8810_REG_POWER2, 0);
  665. regmap_write(map, NAU8810_REG_POWER3, 0);
  666. break;
  667. }
  668. return 0;
  669. }
  670. #define NAU8810_RATES (SNDRV_PCM_RATE_8000_48000)
  671. #define NAU8810_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  672. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  673. static const struct snd_soc_dai_ops nau8810_ops = {
  674. .hw_params = nau8810_pcm_hw_params,
  675. .set_fmt = nau8810_set_dai_fmt,
  676. .set_sysclk = nau8810_set_sysclk,
  677. .set_pll = nau8810_set_pll,
  678. };
  679. static struct snd_soc_dai_driver nau8810_dai = {
  680. .name = "nau8810-hifi",
  681. .playback = {
  682. .stream_name = "Playback",
  683. .channels_min = 1,
  684. .channels_max = 2, /* Only 1 channel of data */
  685. .rates = NAU8810_RATES,
  686. .formats = NAU8810_FORMATS,
  687. },
  688. .capture = {
  689. .stream_name = "Capture",
  690. .channels_min = 1,
  691. .channels_max = 2, /* Only 1 channel of data */
  692. .rates = NAU8810_RATES,
  693. .formats = NAU8810_FORMATS,
  694. },
  695. .ops = &nau8810_ops,
  696. .symmetric_rates = 1,
  697. };
  698. static const struct regmap_config nau8810_regmap_config = {
  699. .reg_bits = 7,
  700. .val_bits = 9,
  701. .max_register = NAU8810_REG_MAX,
  702. .readable_reg = nau8810_readable_reg,
  703. .writeable_reg = nau8810_writeable_reg,
  704. .volatile_reg = nau8810_volatile_reg,
  705. .cache_type = REGCACHE_RBTREE,
  706. .reg_defaults = nau8810_reg_defaults,
  707. .num_reg_defaults = ARRAY_SIZE(nau8810_reg_defaults),
  708. };
  709. static struct snd_soc_codec_driver nau8810_codec_driver = {
  710. .set_bias_level = nau8810_set_bias_level,
  711. .suspend_bias_off = true,
  712. .component_driver = {
  713. .controls = nau8810_snd_controls,
  714. .num_controls = ARRAY_SIZE(nau8810_snd_controls),
  715. .dapm_widgets = nau8810_dapm_widgets,
  716. .num_dapm_widgets = ARRAY_SIZE(nau8810_dapm_widgets),
  717. .dapm_routes = nau8810_dapm_routes,
  718. .num_dapm_routes = ARRAY_SIZE(nau8810_dapm_routes),
  719. },
  720. };
  721. static int nau8810_i2c_probe(struct i2c_client *i2c,
  722. const struct i2c_device_id *id)
  723. {
  724. struct device *dev = &i2c->dev;
  725. struct nau8810 *nau8810 = dev_get_platdata(dev);
  726. if (!nau8810) {
  727. nau8810 = devm_kzalloc(dev, sizeof(*nau8810), GFP_KERNEL);
  728. if (!nau8810)
  729. return -ENOMEM;
  730. }
  731. i2c_set_clientdata(i2c, nau8810);
  732. nau8810->regmap = devm_regmap_init_i2c(i2c, &nau8810_regmap_config);
  733. if (IS_ERR(nau8810->regmap))
  734. return PTR_ERR(nau8810->regmap);
  735. nau8810->dev = dev;
  736. regmap_write(nau8810->regmap, NAU8810_REG_RESET, 0x00);
  737. return snd_soc_register_codec(dev,
  738. &nau8810_codec_driver, &nau8810_dai, 1);
  739. }
  740. static int nau8810_i2c_remove(struct i2c_client *client)
  741. {
  742. snd_soc_unregister_codec(&client->dev);
  743. return 0;
  744. }
  745. static const struct i2c_device_id nau8810_i2c_id[] = {
  746. { "nau8810", 0 },
  747. { }
  748. };
  749. MODULE_DEVICE_TABLE(i2c, nau8810_i2c_id);
  750. #ifdef CONFIG_OF
  751. static const struct of_device_id nau8810_of_match[] = {
  752. { .compatible = "nuvoton,nau8810", },
  753. { }
  754. };
  755. MODULE_DEVICE_TABLE(of, nau8810_of_match);
  756. #endif
  757. static struct i2c_driver nau8810_i2c_driver = {
  758. .driver = {
  759. .name = "nau8810",
  760. .of_match_table = of_match_ptr(nau8810_of_match),
  761. },
  762. .probe = nau8810_i2c_probe,
  763. .remove = nau8810_i2c_remove,
  764. .id_table = nau8810_i2c_id,
  765. };
  766. module_i2c_driver(nau8810_i2c_driver);
  767. MODULE_DESCRIPTION("ASoC NAU8810 driver");
  768. MODULE_AUTHOR("David Lin <ctlin0@nuvoton.com>");
  769. MODULE_LICENSE("GPL v2");