nau8540.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NAU85L40 ALSA SoC audio driver
  4. *
  5. * Copyright 2016 Nuvoton Technology Corp.
  6. * Author: John Hsu <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/pm.h>
  13. #include <linux/i2c.h>
  14. #include <linux/regmap.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/slab.h>
  18. #include <linux/of_device.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <sound/soc-dapm.h>
  24. #include <sound/initval.h>
  25. #include <sound/tlv.h>
  26. #include "nau8540.h"
  27. #define NAU_FREF_MAX 13500000
  28. #define NAU_FVCO_MAX 100000000
  29. #define NAU_FVCO_MIN 90000000
  30. /* the maximum frequency of CLK_ADC */
  31. #define CLK_ADC_MAX 6144000
  32. /* scaling for mclk from sysclk_src output */
  33. static const struct nau8540_fll_attr mclk_src_scaling[] = {
  34. { 1, 0x0 },
  35. { 2, 0x2 },
  36. { 4, 0x3 },
  37. { 8, 0x4 },
  38. { 16, 0x5 },
  39. { 32, 0x6 },
  40. { 3, 0x7 },
  41. { 6, 0xa },
  42. { 12, 0xb },
  43. { 24, 0xc },
  44. };
  45. /* ratio for input clk freq */
  46. static const struct nau8540_fll_attr fll_ratio[] = {
  47. { 512000, 0x01 },
  48. { 256000, 0x02 },
  49. { 128000, 0x04 },
  50. { 64000, 0x08 },
  51. { 32000, 0x10 },
  52. { 8000, 0x20 },
  53. { 4000, 0x40 },
  54. };
  55. static const struct nau8540_fll_attr fll_pre_scalar[] = {
  56. { 1, 0x0 },
  57. { 2, 0x1 },
  58. { 4, 0x2 },
  59. { 8, 0x3 },
  60. };
  61. /* over sampling rate */
  62. static const struct nau8540_osr_attr osr_adc_sel[] = {
  63. { 32, 3 }, /* OSR 32, SRC 1/8 */
  64. { 64, 2 }, /* OSR 64, SRC 1/4 */
  65. { 128, 1 }, /* OSR 128, SRC 1/2 */
  66. { 256, 0 }, /* OSR 256, SRC 1 */
  67. };
  68. static const struct reg_default nau8540_reg_defaults[] = {
  69. {NAU8540_REG_POWER_MANAGEMENT, 0x0000},
  70. {NAU8540_REG_CLOCK_CTRL, 0x0000},
  71. {NAU8540_REG_CLOCK_SRC, 0x0000},
  72. {NAU8540_REG_FLL1, 0x0001},
  73. {NAU8540_REG_FLL2, 0x3126},
  74. {NAU8540_REG_FLL3, 0x0008},
  75. {NAU8540_REG_FLL4, 0x0010},
  76. {NAU8540_REG_FLL5, 0xC000},
  77. {NAU8540_REG_FLL6, 0x6000},
  78. {NAU8540_REG_FLL_VCO_RSV, 0xF13C},
  79. {NAU8540_REG_PCM_CTRL0, 0x000B},
  80. {NAU8540_REG_PCM_CTRL1, 0x3010},
  81. {NAU8540_REG_PCM_CTRL2, 0x0800},
  82. {NAU8540_REG_PCM_CTRL3, 0x0000},
  83. {NAU8540_REG_PCM_CTRL4, 0x000F},
  84. {NAU8540_REG_ALC_CONTROL_1, 0x0000},
  85. {NAU8540_REG_ALC_CONTROL_2, 0x700B},
  86. {NAU8540_REG_ALC_CONTROL_3, 0x0022},
  87. {NAU8540_REG_ALC_CONTROL_4, 0x1010},
  88. {NAU8540_REG_ALC_CONTROL_5, 0x1010},
  89. {NAU8540_REG_NOTCH_FIL1_CH1, 0x0000},
  90. {NAU8540_REG_NOTCH_FIL2_CH1, 0x0000},
  91. {NAU8540_REG_NOTCH_FIL1_CH2, 0x0000},
  92. {NAU8540_REG_NOTCH_FIL2_CH2, 0x0000},
  93. {NAU8540_REG_NOTCH_FIL1_CH3, 0x0000},
  94. {NAU8540_REG_NOTCH_FIL2_CH3, 0x0000},
  95. {NAU8540_REG_NOTCH_FIL1_CH4, 0x0000},
  96. {NAU8540_REG_NOTCH_FIL2_CH4, 0x0000},
  97. {NAU8540_REG_HPF_FILTER_CH12, 0x0000},
  98. {NAU8540_REG_HPF_FILTER_CH34, 0x0000},
  99. {NAU8540_REG_ADC_SAMPLE_RATE, 0x0002},
  100. {NAU8540_REG_DIGITAL_GAIN_CH1, 0x0400},
  101. {NAU8540_REG_DIGITAL_GAIN_CH2, 0x0400},
  102. {NAU8540_REG_DIGITAL_GAIN_CH3, 0x0400},
  103. {NAU8540_REG_DIGITAL_GAIN_CH4, 0x0400},
  104. {NAU8540_REG_DIGITAL_MUX, 0x00E4},
  105. {NAU8540_REG_GPIO_CTRL, 0x0000},
  106. {NAU8540_REG_MISC_CTRL, 0x0000},
  107. {NAU8540_REG_I2C_CTRL, 0xEFFF},
  108. {NAU8540_REG_VMID_CTRL, 0x0000},
  109. {NAU8540_REG_MUTE, 0x0000},
  110. {NAU8540_REG_ANALOG_ADC1, 0x0011},
  111. {NAU8540_REG_ANALOG_ADC2, 0x0020},
  112. {NAU8540_REG_ANALOG_PWR, 0x0000},
  113. {NAU8540_REG_MIC_BIAS, 0x0004},
  114. {NAU8540_REG_REFERENCE, 0x0000},
  115. {NAU8540_REG_FEPGA1, 0x0000},
  116. {NAU8540_REG_FEPGA2, 0x0000},
  117. {NAU8540_REG_FEPGA3, 0x0101},
  118. {NAU8540_REG_FEPGA4, 0x0101},
  119. {NAU8540_REG_PWR, 0x0000},
  120. };
  121. static bool nau8540_readable_reg(struct device *dev, unsigned int reg)
  122. {
  123. switch (reg) {
  124. case NAU8540_REG_POWER_MANAGEMENT ... NAU8540_REG_FLL_VCO_RSV:
  125. case NAU8540_REG_PCM_CTRL0 ... NAU8540_REG_PCM_CTRL4:
  126. case NAU8540_REG_ALC_CONTROL_1 ... NAU8540_REG_ALC_CONTROL_5:
  127. case NAU8540_REG_ALC_GAIN_CH12 ... NAU8540_REG_ADC_SAMPLE_RATE:
  128. case NAU8540_REG_DIGITAL_GAIN_CH1 ... NAU8540_REG_DIGITAL_MUX:
  129. case NAU8540_REG_P2P_CH1 ... NAU8540_REG_I2C_CTRL:
  130. case NAU8540_REG_I2C_DEVICE_ID:
  131. case NAU8540_REG_VMID_CTRL ... NAU8540_REG_MUTE:
  132. case NAU8540_REG_ANALOG_ADC1 ... NAU8540_REG_PWR:
  133. return true;
  134. default:
  135. return false;
  136. }
  137. }
  138. static bool nau8540_writeable_reg(struct device *dev, unsigned int reg)
  139. {
  140. switch (reg) {
  141. case NAU8540_REG_SW_RESET ... NAU8540_REG_FLL_VCO_RSV:
  142. case NAU8540_REG_PCM_CTRL0 ... NAU8540_REG_PCM_CTRL4:
  143. case NAU8540_REG_ALC_CONTROL_1 ... NAU8540_REG_ALC_CONTROL_5:
  144. case NAU8540_REG_NOTCH_FIL1_CH1 ... NAU8540_REG_ADC_SAMPLE_RATE:
  145. case NAU8540_REG_DIGITAL_GAIN_CH1 ... NAU8540_REG_DIGITAL_MUX:
  146. case NAU8540_REG_GPIO_CTRL ... NAU8540_REG_I2C_CTRL:
  147. case NAU8540_REG_RST:
  148. case NAU8540_REG_VMID_CTRL ... NAU8540_REG_MUTE:
  149. case NAU8540_REG_ANALOG_ADC1 ... NAU8540_REG_PWR:
  150. return true;
  151. default:
  152. return false;
  153. }
  154. }
  155. static bool nau8540_volatile_reg(struct device *dev, unsigned int reg)
  156. {
  157. switch (reg) {
  158. case NAU8540_REG_SW_RESET:
  159. case NAU8540_REG_ALC_GAIN_CH12 ... NAU8540_REG_ALC_STATUS:
  160. case NAU8540_REG_P2P_CH1 ... NAU8540_REG_PEAK_CH4:
  161. case NAU8540_REG_I2C_DEVICE_ID:
  162. case NAU8540_REG_RST:
  163. return true;
  164. default:
  165. return false;
  166. }
  167. }
  168. static const DECLARE_TLV_DB_MINMAX(adc_vol_tlv, -12800, 3600);
  169. static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
  170. static const struct snd_kcontrol_new nau8540_snd_controls[] = {
  171. SOC_SINGLE_TLV("Mic1 Volume", NAU8540_REG_DIGITAL_GAIN_CH1,
  172. 0, 0x520, 0, adc_vol_tlv),
  173. SOC_SINGLE_TLV("Mic2 Volume", NAU8540_REG_DIGITAL_GAIN_CH2,
  174. 0, 0x520, 0, adc_vol_tlv),
  175. SOC_SINGLE_TLV("Mic3 Volume", NAU8540_REG_DIGITAL_GAIN_CH3,
  176. 0, 0x520, 0, adc_vol_tlv),
  177. SOC_SINGLE_TLV("Mic4 Volume", NAU8540_REG_DIGITAL_GAIN_CH4,
  178. 0, 0x520, 0, adc_vol_tlv),
  179. SOC_SINGLE_TLV("Frontend PGA1 Volume", NAU8540_REG_FEPGA3,
  180. 0, 0x25, 0, fepga_gain_tlv),
  181. SOC_SINGLE_TLV("Frontend PGA2 Volume", NAU8540_REG_FEPGA3,
  182. 8, 0x25, 0, fepga_gain_tlv),
  183. SOC_SINGLE_TLV("Frontend PGA3 Volume", NAU8540_REG_FEPGA4,
  184. 0, 0x25, 0, fepga_gain_tlv),
  185. SOC_SINGLE_TLV("Frontend PGA4 Volume", NAU8540_REG_FEPGA4,
  186. 8, 0x25, 0, fepga_gain_tlv),
  187. };
  188. static const char * const adc_channel[] = {
  189. "ADC channel 1", "ADC channel 2", "ADC channel 3", "ADC channel 4"
  190. };
  191. static SOC_ENUM_SINGLE_DECL(
  192. digital_ch4_enum, NAU8540_REG_DIGITAL_MUX, 6, adc_channel);
  193. static const struct snd_kcontrol_new digital_ch4_mux =
  194. SOC_DAPM_ENUM("Digital CH4 Select", digital_ch4_enum);
  195. static SOC_ENUM_SINGLE_DECL(
  196. digital_ch3_enum, NAU8540_REG_DIGITAL_MUX, 4, adc_channel);
  197. static const struct snd_kcontrol_new digital_ch3_mux =
  198. SOC_DAPM_ENUM("Digital CH3 Select", digital_ch3_enum);
  199. static SOC_ENUM_SINGLE_DECL(
  200. digital_ch2_enum, NAU8540_REG_DIGITAL_MUX, 2, adc_channel);
  201. static const struct snd_kcontrol_new digital_ch2_mux =
  202. SOC_DAPM_ENUM("Digital CH2 Select", digital_ch2_enum);
  203. static SOC_ENUM_SINGLE_DECL(
  204. digital_ch1_enum, NAU8540_REG_DIGITAL_MUX, 0, adc_channel);
  205. static const struct snd_kcontrol_new digital_ch1_mux =
  206. SOC_DAPM_ENUM("Digital CH1 Select", digital_ch1_enum);
  207. static int adc_power_control(struct snd_soc_dapm_widget *w,
  208. struct snd_kcontrol *k, int event)
  209. {
  210. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  211. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  212. if (SND_SOC_DAPM_EVENT_ON(event)) {
  213. msleep(300);
  214. /* DO12 and DO34 pad output enable */
  215. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
  216. NAU8540_I2S_DO12_TRI, 0);
  217. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
  218. NAU8540_I2S_DO34_TRI, 0);
  219. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  220. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
  221. NAU8540_I2S_DO12_TRI, NAU8540_I2S_DO12_TRI);
  222. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
  223. NAU8540_I2S_DO34_TRI, NAU8540_I2S_DO34_TRI);
  224. }
  225. return 0;
  226. }
  227. static int aiftx_power_control(struct snd_soc_dapm_widget *w,
  228. struct snd_kcontrol *k, int event)
  229. {
  230. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  231. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  232. if (SND_SOC_DAPM_EVENT_OFF(event)) {
  233. regmap_write(nau8540->regmap, NAU8540_REG_RST, 0x0001);
  234. regmap_write(nau8540->regmap, NAU8540_REG_RST, 0x0000);
  235. }
  236. return 0;
  237. }
  238. static const struct snd_soc_dapm_widget nau8540_dapm_widgets[] = {
  239. SND_SOC_DAPM_SUPPLY("MICBIAS2", NAU8540_REG_MIC_BIAS, 11, 0, NULL, 0),
  240. SND_SOC_DAPM_SUPPLY("MICBIAS1", NAU8540_REG_MIC_BIAS, 10, 0, NULL, 0),
  241. SND_SOC_DAPM_INPUT("MIC1"),
  242. SND_SOC_DAPM_INPUT("MIC2"),
  243. SND_SOC_DAPM_INPUT("MIC3"),
  244. SND_SOC_DAPM_INPUT("MIC4"),
  245. SND_SOC_DAPM_PGA("Frontend PGA1", NAU8540_REG_PWR, 12, 0, NULL, 0),
  246. SND_SOC_DAPM_PGA("Frontend PGA2", NAU8540_REG_PWR, 13, 0, NULL, 0),
  247. SND_SOC_DAPM_PGA("Frontend PGA3", NAU8540_REG_PWR, 14, 0, NULL, 0),
  248. SND_SOC_DAPM_PGA("Frontend PGA4", NAU8540_REG_PWR, 15, 0, NULL, 0),
  249. SND_SOC_DAPM_ADC_E("ADC1", NULL,
  250. NAU8540_REG_POWER_MANAGEMENT, 0, 0, adc_power_control,
  251. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  252. SND_SOC_DAPM_ADC_E("ADC2", NULL,
  253. NAU8540_REG_POWER_MANAGEMENT, 1, 0, adc_power_control,
  254. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  255. SND_SOC_DAPM_ADC_E("ADC3", NULL,
  256. NAU8540_REG_POWER_MANAGEMENT, 2, 0, adc_power_control,
  257. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  258. SND_SOC_DAPM_ADC_E("ADC4", NULL,
  259. NAU8540_REG_POWER_MANAGEMENT, 3, 0, adc_power_control,
  260. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  261. SND_SOC_DAPM_PGA("ADC CH1", NAU8540_REG_ANALOG_PWR, 0, 0, NULL, 0),
  262. SND_SOC_DAPM_PGA("ADC CH2", NAU8540_REG_ANALOG_PWR, 1, 0, NULL, 0),
  263. SND_SOC_DAPM_PGA("ADC CH3", NAU8540_REG_ANALOG_PWR, 2, 0, NULL, 0),
  264. SND_SOC_DAPM_PGA("ADC CH4", NAU8540_REG_ANALOG_PWR, 3, 0, NULL, 0),
  265. SND_SOC_DAPM_MUX("Digital CH4 Mux",
  266. SND_SOC_NOPM, 0, 0, &digital_ch4_mux),
  267. SND_SOC_DAPM_MUX("Digital CH3 Mux",
  268. SND_SOC_NOPM, 0, 0, &digital_ch3_mux),
  269. SND_SOC_DAPM_MUX("Digital CH2 Mux",
  270. SND_SOC_NOPM, 0, 0, &digital_ch2_mux),
  271. SND_SOC_DAPM_MUX("Digital CH1 Mux",
  272. SND_SOC_NOPM, 0, 0, &digital_ch1_mux),
  273. SND_SOC_DAPM_AIF_OUT_E("AIFTX", "Capture", 0, SND_SOC_NOPM, 0, 0,
  274. aiftx_power_control, SND_SOC_DAPM_POST_PMD),
  275. };
  276. static const struct snd_soc_dapm_route nau8540_dapm_routes[] = {
  277. {"Frontend PGA1", NULL, "MIC1"},
  278. {"Frontend PGA2", NULL, "MIC2"},
  279. {"Frontend PGA3", NULL, "MIC3"},
  280. {"Frontend PGA4", NULL, "MIC4"},
  281. {"ADC1", NULL, "Frontend PGA1"},
  282. {"ADC2", NULL, "Frontend PGA2"},
  283. {"ADC3", NULL, "Frontend PGA3"},
  284. {"ADC4", NULL, "Frontend PGA4"},
  285. {"ADC CH1", NULL, "ADC1"},
  286. {"ADC CH2", NULL, "ADC2"},
  287. {"ADC CH3", NULL, "ADC3"},
  288. {"ADC CH4", NULL, "ADC4"},
  289. {"ADC1", NULL, "MICBIAS1"},
  290. {"ADC2", NULL, "MICBIAS1"},
  291. {"ADC3", NULL, "MICBIAS2"},
  292. {"ADC4", NULL, "MICBIAS2"},
  293. {"Digital CH1 Mux", "ADC channel 1", "ADC CH1"},
  294. {"Digital CH1 Mux", "ADC channel 2", "ADC CH2"},
  295. {"Digital CH1 Mux", "ADC channel 3", "ADC CH3"},
  296. {"Digital CH1 Mux", "ADC channel 4", "ADC CH4"},
  297. {"Digital CH2 Mux", "ADC channel 1", "ADC CH1"},
  298. {"Digital CH2 Mux", "ADC channel 2", "ADC CH2"},
  299. {"Digital CH2 Mux", "ADC channel 3", "ADC CH3"},
  300. {"Digital CH2 Mux", "ADC channel 4", "ADC CH4"},
  301. {"Digital CH3 Mux", "ADC channel 1", "ADC CH1"},
  302. {"Digital CH3 Mux", "ADC channel 2", "ADC CH2"},
  303. {"Digital CH3 Mux", "ADC channel 3", "ADC CH3"},
  304. {"Digital CH3 Mux", "ADC channel 4", "ADC CH4"},
  305. {"Digital CH4 Mux", "ADC channel 1", "ADC CH1"},
  306. {"Digital CH4 Mux", "ADC channel 2", "ADC CH2"},
  307. {"Digital CH4 Mux", "ADC channel 3", "ADC CH3"},
  308. {"Digital CH4 Mux", "ADC channel 4", "ADC CH4"},
  309. {"AIFTX", NULL, "Digital CH1 Mux"},
  310. {"AIFTX", NULL, "Digital CH2 Mux"},
  311. {"AIFTX", NULL, "Digital CH3 Mux"},
  312. {"AIFTX", NULL, "Digital CH4 Mux"},
  313. };
  314. static const struct nau8540_osr_attr *
  315. nau8540_get_osr(struct nau8540 *nau8540)
  316. {
  317. unsigned int osr;
  318. regmap_read(nau8540->regmap, NAU8540_REG_ADC_SAMPLE_RATE, &osr);
  319. osr &= NAU8540_ADC_OSR_MASK;
  320. if (osr >= ARRAY_SIZE(osr_adc_sel))
  321. return NULL;
  322. return &osr_adc_sel[osr];
  323. }
  324. static int nau8540_dai_startup(struct snd_pcm_substream *substream,
  325. struct snd_soc_dai *dai)
  326. {
  327. struct snd_soc_component *component = dai->component;
  328. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  329. const struct nau8540_osr_attr *osr;
  330. osr = nau8540_get_osr(nau8540);
  331. if (!osr || !osr->osr)
  332. return -EINVAL;
  333. return snd_pcm_hw_constraint_minmax(substream->runtime,
  334. SNDRV_PCM_HW_PARAM_RATE,
  335. 0, CLK_ADC_MAX / osr->osr);
  336. }
  337. static int nau8540_hw_params(struct snd_pcm_substream *substream,
  338. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  339. {
  340. struct snd_soc_component *component = dai->component;
  341. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  342. unsigned int val_len = 0;
  343. const struct nau8540_osr_attr *osr;
  344. /* CLK_ADC = OSR * FS
  345. * ADC clock frequency is defined as Over Sampling Rate (OSR)
  346. * multiplied by the audio sample rate (Fs). Note that the OSR and Fs
  347. * values must be selected such that the maximum frequency is less
  348. * than 6.144 MHz.
  349. */
  350. osr = nau8540_get_osr(nau8540);
  351. if (!osr || !osr->osr)
  352. return -EINVAL;
  353. if (params_rate(params) * osr->osr > CLK_ADC_MAX)
  354. return -EINVAL;
  355. regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
  356. NAU8540_CLK_ADC_SRC_MASK,
  357. osr->clk_src << NAU8540_CLK_ADC_SRC_SFT);
  358. switch (params_width(params)) {
  359. case 16:
  360. val_len |= NAU8540_I2S_DL_16;
  361. break;
  362. case 20:
  363. val_len |= NAU8540_I2S_DL_20;
  364. break;
  365. case 24:
  366. val_len |= NAU8540_I2S_DL_24;
  367. break;
  368. case 32:
  369. val_len |= NAU8540_I2S_DL_32;
  370. break;
  371. default:
  372. return -EINVAL;
  373. }
  374. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL0,
  375. NAU8540_I2S_DL_MASK, val_len);
  376. return 0;
  377. }
  378. static int nau8540_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  379. {
  380. struct snd_soc_component *component = dai->component;
  381. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  382. unsigned int ctrl1_val = 0, ctrl2_val = 0;
  383. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  384. case SND_SOC_DAIFMT_CBM_CFM:
  385. ctrl2_val |= NAU8540_I2S_MS_MASTER;
  386. break;
  387. case SND_SOC_DAIFMT_CBS_CFS:
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  393. case SND_SOC_DAIFMT_NB_NF:
  394. break;
  395. case SND_SOC_DAIFMT_IB_NF:
  396. ctrl1_val |= NAU8540_I2S_BP_INV;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  402. case SND_SOC_DAIFMT_I2S:
  403. ctrl1_val |= NAU8540_I2S_DF_I2S;
  404. break;
  405. case SND_SOC_DAIFMT_LEFT_J:
  406. ctrl1_val |= NAU8540_I2S_DF_LEFT;
  407. break;
  408. case SND_SOC_DAIFMT_RIGHT_J:
  409. ctrl1_val |= NAU8540_I2S_DF_RIGTH;
  410. break;
  411. case SND_SOC_DAIFMT_DSP_A:
  412. ctrl1_val |= NAU8540_I2S_DF_PCM_AB;
  413. break;
  414. case SND_SOC_DAIFMT_DSP_B:
  415. ctrl1_val |= NAU8540_I2S_DF_PCM_AB;
  416. ctrl1_val |= NAU8540_I2S_PCMB_EN;
  417. break;
  418. default:
  419. return -EINVAL;
  420. }
  421. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL0,
  422. NAU8540_I2S_DL_MASK | NAU8540_I2S_DF_MASK |
  423. NAU8540_I2S_BP_INV | NAU8540_I2S_PCMB_EN, ctrl1_val);
  424. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
  425. NAU8540_I2S_MS_MASK | NAU8540_I2S_DO12_OE, ctrl2_val);
  426. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
  427. NAU8540_I2S_DO34_OE, 0);
  428. return 0;
  429. }
  430. /**
  431. * nau8540_set_tdm_slot - configure DAI TX TDM.
  432. * @dai: DAI
  433. * @tx_mask: bitmask representing active TX slots. Ex.
  434. * 0xf for normal 4 channel TDM.
  435. * 0xf0 for shifted 4 channel TDM
  436. * @rx_mask: no used.
  437. * @slots: Number of slots in use.
  438. * @slot_width: Width in bits for each slot.
  439. *
  440. * Configures a DAI for TDM operation. Only support 4 slots TDM.
  441. */
  442. static int nau8540_set_tdm_slot(struct snd_soc_dai *dai,
  443. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  444. {
  445. struct snd_soc_component *component = dai->component;
  446. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  447. unsigned int ctrl2_val = 0, ctrl4_val = 0;
  448. if (slots > 4 || ((tx_mask & 0xf0) && (tx_mask & 0xf)))
  449. return -EINVAL;
  450. ctrl4_val |= (NAU8540_TDM_MODE | NAU8540_TDM_OFFSET_EN);
  451. if (tx_mask & 0xf0) {
  452. ctrl2_val = 4 * slot_width;
  453. ctrl4_val |= (tx_mask >> 4);
  454. } else {
  455. ctrl4_val |= tx_mask;
  456. }
  457. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL4,
  458. NAU8540_TDM_MODE | NAU8540_TDM_OFFSET_EN |
  459. NAU8540_TDM_TX_MASK, ctrl4_val);
  460. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
  461. NAU8540_I2S_DO12_OE, NAU8540_I2S_DO12_OE);
  462. regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
  463. NAU8540_I2S_DO34_OE | NAU8540_I2S_TSLOT_L_MASK,
  464. NAU8540_I2S_DO34_OE | ctrl2_val);
  465. return 0;
  466. }
  467. static const struct snd_soc_dai_ops nau8540_dai_ops = {
  468. .startup = nau8540_dai_startup,
  469. .hw_params = nau8540_hw_params,
  470. .set_fmt = nau8540_set_fmt,
  471. .set_tdm_slot = nau8540_set_tdm_slot,
  472. };
  473. #define NAU8540_RATES SNDRV_PCM_RATE_8000_48000
  474. #define NAU8540_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  475. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  476. static struct snd_soc_dai_driver nau8540_dai = {
  477. .name = "nau8540-hifi",
  478. .capture = {
  479. .stream_name = "Capture",
  480. .channels_min = 1,
  481. .channels_max = 4,
  482. .rates = NAU8540_RATES,
  483. .formats = NAU8540_FORMATS,
  484. },
  485. .ops = &nau8540_dai_ops,
  486. };
  487. /**
  488. * nau8540_calc_fll_param - Calculate FLL parameters.
  489. * @fll_in: external clock provided to codec.
  490. * @fs: sampling rate.
  491. * @fll_param: Pointer to structure of FLL parameters.
  492. *
  493. * Calculate FLL parameters to configure codec.
  494. *
  495. * Returns 0 for success or negative error code.
  496. */
  497. static int nau8540_calc_fll_param(unsigned int fll_in,
  498. unsigned int fs, struct nau8540_fll *fll_param)
  499. {
  500. u64 fvco, fvco_max;
  501. unsigned int fref, i, fvco_sel;
  502. /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing
  503. * freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
  504. * FREF = freq_in / NAU8540_FLL_REF_DIV_MASK
  505. */
  506. for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
  507. fref = fll_in / fll_pre_scalar[i].param;
  508. if (fref <= NAU_FREF_MAX)
  509. break;
  510. }
  511. if (i == ARRAY_SIZE(fll_pre_scalar))
  512. return -EINVAL;
  513. fll_param->clk_ref_div = fll_pre_scalar[i].val;
  514. /* Choose the FLL ratio based on FREF */
  515. for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
  516. if (fref >= fll_ratio[i].param)
  517. break;
  518. }
  519. if (i == ARRAY_SIZE(fll_ratio))
  520. return -EINVAL;
  521. fll_param->ratio = fll_ratio[i].val;
  522. /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
  523. * FDCO must be within the 90MHz - 124MHz or the FFL cannot be
  524. * guaranteed across the full range of operation.
  525. * FDCO = freq_out * 2 * mclk_src_scaling
  526. */
  527. fvco_max = 0;
  528. fvco_sel = ARRAY_SIZE(mclk_src_scaling);
  529. for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
  530. fvco = 256ULL * fs * 2 * mclk_src_scaling[i].param;
  531. if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
  532. fvco_max < fvco) {
  533. fvco_max = fvco;
  534. fvco_sel = i;
  535. }
  536. }
  537. if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
  538. return -EINVAL;
  539. fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
  540. /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional
  541. * input based on FDCO, FREF and FLL ratio.
  542. */
  543. fvco = div_u64(fvco_max << 16, fref * fll_param->ratio);
  544. fll_param->fll_int = (fvco >> 16) & 0x3FF;
  545. fll_param->fll_frac = fvco & 0xFFFF;
  546. return 0;
  547. }
  548. static void nau8540_fll_apply(struct regmap *regmap,
  549. struct nau8540_fll *fll_param)
  550. {
  551. regmap_update_bits(regmap, NAU8540_REG_CLOCK_SRC,
  552. NAU8540_CLK_SRC_MASK | NAU8540_CLK_MCLK_SRC_MASK,
  553. NAU8540_CLK_SRC_MCLK | fll_param->mclk_src);
  554. regmap_update_bits(regmap, NAU8540_REG_FLL1,
  555. NAU8540_FLL_RATIO_MASK | NAU8540_ICTRL_LATCH_MASK,
  556. fll_param->ratio | (0x6 << NAU8540_ICTRL_LATCH_SFT));
  557. /* FLL 16-bit fractional input */
  558. regmap_write(regmap, NAU8540_REG_FLL2, fll_param->fll_frac);
  559. /* FLL 10-bit integer input */
  560. regmap_update_bits(regmap, NAU8540_REG_FLL3,
  561. NAU8540_FLL_INTEGER_MASK, fll_param->fll_int);
  562. /* FLL pre-scaler */
  563. regmap_update_bits(regmap, NAU8540_REG_FLL4,
  564. NAU8540_FLL_REF_DIV_MASK,
  565. fll_param->clk_ref_div << NAU8540_FLL_REF_DIV_SFT);
  566. regmap_update_bits(regmap, NAU8540_REG_FLL5,
  567. NAU8540_FLL_CLK_SW_MASK, NAU8540_FLL_CLK_SW_REF);
  568. regmap_update_bits(regmap,
  569. NAU8540_REG_FLL6, NAU8540_DCO_EN, 0);
  570. if (fll_param->fll_frac) {
  571. regmap_update_bits(regmap, NAU8540_REG_FLL5,
  572. NAU8540_FLL_PDB_DAC_EN | NAU8540_FLL_LOOP_FTR_EN |
  573. NAU8540_FLL_FTR_SW_MASK,
  574. NAU8540_FLL_PDB_DAC_EN | NAU8540_FLL_LOOP_FTR_EN |
  575. NAU8540_FLL_FTR_SW_FILTER);
  576. regmap_update_bits(regmap, NAU8540_REG_FLL6,
  577. NAU8540_SDM_EN | NAU8540_CUTOFF500,
  578. NAU8540_SDM_EN | NAU8540_CUTOFF500);
  579. } else {
  580. regmap_update_bits(regmap, NAU8540_REG_FLL5,
  581. NAU8540_FLL_PDB_DAC_EN | NAU8540_FLL_LOOP_FTR_EN |
  582. NAU8540_FLL_FTR_SW_MASK, NAU8540_FLL_FTR_SW_ACCU);
  583. regmap_update_bits(regmap, NAU8540_REG_FLL6,
  584. NAU8540_SDM_EN | NAU8540_CUTOFF500, 0);
  585. }
  586. }
  587. /* freq_out must be 256*Fs in order to achieve the best performance */
  588. static int nau8540_set_pll(struct snd_soc_component *component, int pll_id, int source,
  589. unsigned int freq_in, unsigned int freq_out)
  590. {
  591. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  592. struct nau8540_fll fll_param;
  593. int ret, fs;
  594. switch (pll_id) {
  595. case NAU8540_CLK_FLL_MCLK:
  596. regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL3,
  597. NAU8540_FLL_CLK_SRC_MASK | NAU8540_GAIN_ERR_MASK,
  598. NAU8540_FLL_CLK_SRC_MCLK | 0);
  599. break;
  600. case NAU8540_CLK_FLL_BLK:
  601. regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL3,
  602. NAU8540_FLL_CLK_SRC_MASK | NAU8540_GAIN_ERR_MASK,
  603. NAU8540_FLL_CLK_SRC_BLK |
  604. (0xf << NAU8540_GAIN_ERR_SFT));
  605. break;
  606. case NAU8540_CLK_FLL_FS:
  607. regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL3,
  608. NAU8540_FLL_CLK_SRC_MASK | NAU8540_GAIN_ERR_MASK,
  609. NAU8540_FLL_CLK_SRC_FS |
  610. (0xf << NAU8540_GAIN_ERR_SFT));
  611. break;
  612. default:
  613. dev_err(nau8540->dev, "Invalid clock id (%d)\n", pll_id);
  614. return -EINVAL;
  615. }
  616. dev_dbg(nau8540->dev, "Sysclk is %dHz and clock id is %d\n",
  617. freq_out, pll_id);
  618. fs = freq_out / 256;
  619. ret = nau8540_calc_fll_param(freq_in, fs, &fll_param);
  620. if (ret < 0) {
  621. dev_err(nau8540->dev, "Unsupported input clock %d\n", freq_in);
  622. return ret;
  623. }
  624. dev_dbg(nau8540->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
  625. fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac,
  626. fll_param.fll_int, fll_param.clk_ref_div);
  627. nau8540_fll_apply(nau8540->regmap, &fll_param);
  628. mdelay(2);
  629. regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
  630. NAU8540_CLK_SRC_MASK, NAU8540_CLK_SRC_VCO);
  631. return 0;
  632. }
  633. static int nau8540_set_sysclk(struct snd_soc_component *component,
  634. int clk_id, int source, unsigned int freq, int dir)
  635. {
  636. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  637. switch (clk_id) {
  638. case NAU8540_CLK_DIS:
  639. case NAU8540_CLK_MCLK:
  640. regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
  641. NAU8540_CLK_SRC_MASK, NAU8540_CLK_SRC_MCLK);
  642. regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL6,
  643. NAU8540_DCO_EN, 0);
  644. break;
  645. case NAU8540_CLK_INTERNAL:
  646. regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL6,
  647. NAU8540_DCO_EN, NAU8540_DCO_EN);
  648. regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
  649. NAU8540_CLK_SRC_MASK, NAU8540_CLK_SRC_VCO);
  650. break;
  651. default:
  652. dev_err(nau8540->dev, "Invalid clock id (%d)\n", clk_id);
  653. return -EINVAL;
  654. }
  655. dev_dbg(nau8540->dev, "Sysclk is %dHz and clock id is %d\n",
  656. freq, clk_id);
  657. return 0;
  658. }
  659. static void nau8540_reset_chip(struct regmap *regmap)
  660. {
  661. regmap_write(regmap, NAU8540_REG_SW_RESET, 0x00);
  662. regmap_write(regmap, NAU8540_REG_SW_RESET, 0x00);
  663. }
  664. static void nau8540_init_regs(struct nau8540 *nau8540)
  665. {
  666. struct regmap *regmap = nau8540->regmap;
  667. /* Enable Bias/VMID/VMID Tieoff */
  668. regmap_update_bits(regmap, NAU8540_REG_VMID_CTRL,
  669. NAU8540_VMID_EN | NAU8540_VMID_SEL_MASK,
  670. NAU8540_VMID_EN | (0x2 << NAU8540_VMID_SEL_SFT));
  671. regmap_update_bits(regmap, NAU8540_REG_REFERENCE,
  672. NAU8540_PRECHARGE_DIS | NAU8540_GLOBAL_BIAS_EN,
  673. NAU8540_PRECHARGE_DIS | NAU8540_GLOBAL_BIAS_EN);
  674. mdelay(2);
  675. regmap_update_bits(regmap, NAU8540_REG_MIC_BIAS,
  676. NAU8540_PU_PRE, NAU8540_PU_PRE);
  677. regmap_update_bits(regmap, NAU8540_REG_CLOCK_CTRL,
  678. NAU8540_CLK_ADC_EN | NAU8540_CLK_I2S_EN,
  679. NAU8540_CLK_ADC_EN | NAU8540_CLK_I2S_EN);
  680. /* ADC OSR selection, CLK_ADC = Fs * OSR;
  681. * Channel time alignment enable.
  682. */
  683. regmap_update_bits(regmap, NAU8540_REG_ADC_SAMPLE_RATE,
  684. NAU8540_CH_SYNC | NAU8540_ADC_OSR_MASK,
  685. NAU8540_CH_SYNC | NAU8540_ADC_OSR_64);
  686. /* PGA input mode selection */
  687. regmap_update_bits(regmap, NAU8540_REG_FEPGA1,
  688. NAU8540_FEPGA1_MODCH2_SHT | NAU8540_FEPGA1_MODCH1_SHT,
  689. NAU8540_FEPGA1_MODCH2_SHT | NAU8540_FEPGA1_MODCH1_SHT);
  690. regmap_update_bits(regmap, NAU8540_REG_FEPGA2,
  691. NAU8540_FEPGA2_MODCH4_SHT | NAU8540_FEPGA2_MODCH3_SHT,
  692. NAU8540_FEPGA2_MODCH4_SHT | NAU8540_FEPGA2_MODCH3_SHT);
  693. /* DO12 and DO34 pad output disable */
  694. regmap_update_bits(regmap, NAU8540_REG_PCM_CTRL1,
  695. NAU8540_I2S_DO12_TRI, NAU8540_I2S_DO12_TRI);
  696. regmap_update_bits(regmap, NAU8540_REG_PCM_CTRL2,
  697. NAU8540_I2S_DO34_TRI, NAU8540_I2S_DO34_TRI);
  698. }
  699. static int __maybe_unused nau8540_suspend(struct snd_soc_component *component)
  700. {
  701. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  702. regcache_cache_only(nau8540->regmap, true);
  703. regcache_mark_dirty(nau8540->regmap);
  704. return 0;
  705. }
  706. static int __maybe_unused nau8540_resume(struct snd_soc_component *component)
  707. {
  708. struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
  709. regcache_cache_only(nau8540->regmap, false);
  710. regcache_sync(nau8540->regmap);
  711. return 0;
  712. }
  713. static const struct snd_soc_component_driver nau8540_component_driver = {
  714. .set_sysclk = nau8540_set_sysclk,
  715. .set_pll = nau8540_set_pll,
  716. .suspend = nau8540_suspend,
  717. .resume = nau8540_resume,
  718. .controls = nau8540_snd_controls,
  719. .num_controls = ARRAY_SIZE(nau8540_snd_controls),
  720. .dapm_widgets = nau8540_dapm_widgets,
  721. .num_dapm_widgets = ARRAY_SIZE(nau8540_dapm_widgets),
  722. .dapm_routes = nau8540_dapm_routes,
  723. .num_dapm_routes = ARRAY_SIZE(nau8540_dapm_routes),
  724. .suspend_bias_off = 1,
  725. .idle_bias_on = 1,
  726. .use_pmdown_time = 1,
  727. .endianness = 1,
  728. };
  729. static const struct regmap_config nau8540_regmap_config = {
  730. .val_bits = 16,
  731. .reg_bits = 16,
  732. .max_register = NAU8540_REG_MAX,
  733. .readable_reg = nau8540_readable_reg,
  734. .writeable_reg = nau8540_writeable_reg,
  735. .volatile_reg = nau8540_volatile_reg,
  736. .cache_type = REGCACHE_RBTREE,
  737. .reg_defaults = nau8540_reg_defaults,
  738. .num_reg_defaults = ARRAY_SIZE(nau8540_reg_defaults),
  739. };
  740. static int nau8540_i2c_probe(struct i2c_client *i2c)
  741. {
  742. struct device *dev = &i2c->dev;
  743. struct nau8540 *nau8540 = dev_get_platdata(dev);
  744. int ret, value;
  745. if (!nau8540) {
  746. nau8540 = devm_kzalloc(dev, sizeof(*nau8540), GFP_KERNEL);
  747. if (!nau8540)
  748. return -ENOMEM;
  749. }
  750. i2c_set_clientdata(i2c, nau8540);
  751. nau8540->regmap = devm_regmap_init_i2c(i2c, &nau8540_regmap_config);
  752. if (IS_ERR(nau8540->regmap))
  753. return PTR_ERR(nau8540->regmap);
  754. ret = regmap_read(nau8540->regmap, NAU8540_REG_I2C_DEVICE_ID, &value);
  755. if (ret < 0) {
  756. dev_err(dev, "Failed to read device id from the NAU85L40: %d\n",
  757. ret);
  758. return ret;
  759. }
  760. nau8540->dev = dev;
  761. nau8540_reset_chip(nau8540->regmap);
  762. nau8540_init_regs(nau8540);
  763. return devm_snd_soc_register_component(dev,
  764. &nau8540_component_driver, &nau8540_dai, 1);
  765. }
  766. static const struct i2c_device_id nau8540_i2c_ids[] = {
  767. { "nau8540", 0 },
  768. { }
  769. };
  770. MODULE_DEVICE_TABLE(i2c, nau8540_i2c_ids);
  771. #ifdef CONFIG_OF
  772. static const struct of_device_id nau8540_of_ids[] = {
  773. { .compatible = "nuvoton,nau8540", },
  774. {}
  775. };
  776. MODULE_DEVICE_TABLE(of, nau8540_of_ids);
  777. #endif
  778. static struct i2c_driver nau8540_i2c_driver = {
  779. .driver = {
  780. .name = "nau8540",
  781. .of_match_table = of_match_ptr(nau8540_of_ids),
  782. },
  783. .probe_new = nau8540_i2c_probe,
  784. .id_table = nau8540_i2c_ids,
  785. };
  786. module_i2c_driver(nau8540_i2c_driver);
  787. MODULE_DESCRIPTION("ASoC NAU85L40 driver");
  788. MODULE_AUTHOR("John Hsu <[email protected]>");
  789. MODULE_LICENSE("GPL v2");