atmel-pdmic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Atmel PDMIC driver
  3. *
  4. * Copyright (C) 2015 Atmel
  5. *
  6. * Author: Songjun Wu <songjun.wu@atmel.com>
  7. */
  8. #include <linux/of.h>
  9. #include <linux/clk.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <sound/core.h>
  14. #include <sound/dmaengine_pcm.h>
  15. #include <sound/pcm_params.h>
  16. #include <sound/tlv.h>
  17. #include "atmel-pdmic.h"
  18. struct atmel_pdmic_pdata {
  19. u32 mic_min_freq;
  20. u32 mic_max_freq;
  21. s32 mic_offset;
  22. const char *card_name;
  23. };
  24. struct atmel_pdmic {
  25. dma_addr_t phy_base;
  26. struct regmap *regmap;
  27. struct clk *pclk;
  28. struct clk *gclk;
  29. struct device *dev;
  30. int irq;
  31. struct snd_pcm_substream *substream;
  32. const struct atmel_pdmic_pdata *pdata;
  33. };
  34. static const struct of_device_id atmel_pdmic_of_match[] = {
  35. {
  36. .compatible = "atmel,sama5d2-pdmic",
  37. }, {
  38. /* sentinel */
  39. }
  40. };
  41. MODULE_DEVICE_TABLE(of, atmel_pdmic_of_match);
  42. #define PDMIC_OFFSET_MAX_VAL S16_MAX
  43. #define PDMIC_OFFSET_MIN_VAL S16_MIN
  44. static struct atmel_pdmic_pdata *atmel_pdmic_dt_init(struct device *dev)
  45. {
  46. struct device_node *np = dev->of_node;
  47. struct atmel_pdmic_pdata *pdata;
  48. if (!np) {
  49. dev_err(dev, "device node not found\n");
  50. return ERR_PTR(-EINVAL);
  51. }
  52. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  53. if (!pdata)
  54. return ERR_PTR(-ENOMEM);
  55. if (of_property_read_string(np, "atmel,model", &pdata->card_name))
  56. pdata->card_name = "PDMIC";
  57. if (of_property_read_u32(np, "atmel,mic-min-freq",
  58. &pdata->mic_min_freq)) {
  59. dev_err(dev, "failed to get mic-min-freq\n");
  60. return ERR_PTR(-EINVAL);
  61. }
  62. if (of_property_read_u32(np, "atmel,mic-max-freq",
  63. &pdata->mic_max_freq)) {
  64. dev_err(dev, "failed to get mic-max-freq\n");
  65. return ERR_PTR(-EINVAL);
  66. }
  67. if (pdata->mic_max_freq < pdata->mic_min_freq) {
  68. dev_err(dev,
  69. "mic-max-freq should not be less than mic-min-freq\n");
  70. return ERR_PTR(-EINVAL);
  71. }
  72. if (of_property_read_s32(np, "atmel,mic-offset", &pdata->mic_offset))
  73. pdata->mic_offset = 0;
  74. if (pdata->mic_offset > PDMIC_OFFSET_MAX_VAL) {
  75. dev_warn(dev,
  76. "mic-offset value %d is larger than the max value %d, the max value is specified\n",
  77. pdata->mic_offset, PDMIC_OFFSET_MAX_VAL);
  78. pdata->mic_offset = PDMIC_OFFSET_MAX_VAL;
  79. } else if (pdata->mic_offset < PDMIC_OFFSET_MIN_VAL) {
  80. dev_warn(dev,
  81. "mic-offset value %d is less than the min value %d, the min value is specified\n",
  82. pdata->mic_offset, PDMIC_OFFSET_MIN_VAL);
  83. pdata->mic_offset = PDMIC_OFFSET_MIN_VAL;
  84. }
  85. return pdata;
  86. }
  87. /* cpu dai component */
  88. static int atmel_pdmic_cpu_dai_startup(struct snd_pcm_substream *substream,
  89. struct snd_soc_dai *cpu_dai)
  90. {
  91. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  92. struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
  93. int ret;
  94. ret = clk_prepare_enable(dd->gclk);
  95. if (ret)
  96. return ret;
  97. ret = clk_prepare_enable(dd->pclk);
  98. if (ret) {
  99. clk_disable_unprepare(dd->gclk);
  100. return ret;
  101. }
  102. /* Clear all bits in the Control Register(PDMIC_CR) */
  103. regmap_write(dd->regmap, PDMIC_CR, 0);
  104. dd->substream = substream;
  105. /* Enable the overrun error interrupt */
  106. regmap_write(dd->regmap, PDMIC_IER, PDMIC_IER_OVRE);
  107. return 0;
  108. }
  109. static void atmel_pdmic_cpu_dai_shutdown(struct snd_pcm_substream *substream,
  110. struct snd_soc_dai *cpu_dai)
  111. {
  112. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  113. struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
  114. /* Disable the overrun error interrupt */
  115. regmap_write(dd->regmap, PDMIC_IDR, PDMIC_IDR_OVRE);
  116. clk_disable_unprepare(dd->gclk);
  117. clk_disable_unprepare(dd->pclk);
  118. }
  119. static int atmel_pdmic_cpu_dai_prepare(struct snd_pcm_substream *substream,
  120. struct snd_soc_dai *cpu_dai)
  121. {
  122. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  123. struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
  124. struct snd_soc_component *component = cpu_dai->component;
  125. u32 val;
  126. int ret;
  127. /* Clean the PDMIC Converted Data Register */
  128. ret = regmap_read(dd->regmap, PDMIC_CDR, &val);
  129. if (ret < 0)
  130. return 0;
  131. ret = snd_soc_component_update_bits(component, PDMIC_CR,
  132. PDMIC_CR_ENPDM_MASK,
  133. PDMIC_CR_ENPDM_DIS <<
  134. PDMIC_CR_ENPDM_SHIFT);
  135. if (ret < 0)
  136. return ret;
  137. return 0;
  138. }
  139. #define ATMEL_PDMIC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  140. /* platform */
  141. #define ATMEL_PDMIC_MAX_BUF_SIZE (64 * 1024)
  142. #define ATMEL_PDMIC_PREALLOC_BUF_SIZE ATMEL_PDMIC_MAX_BUF_SIZE
  143. static const struct snd_pcm_hardware atmel_pdmic_hw = {
  144. .info = SNDRV_PCM_INFO_MMAP
  145. | SNDRV_PCM_INFO_MMAP_VALID
  146. | SNDRV_PCM_INFO_INTERLEAVED
  147. | SNDRV_PCM_INFO_RESUME
  148. | SNDRV_PCM_INFO_PAUSE,
  149. .formats = ATMEL_PDMIC_FORMATS,
  150. .buffer_bytes_max = ATMEL_PDMIC_MAX_BUF_SIZE,
  151. .period_bytes_min = 256,
  152. .period_bytes_max = 32 * 1024,
  153. .periods_min = 2,
  154. .periods_max = 256,
  155. };
  156. static int
  157. atmel_pdmic_platform_configure_dma(struct snd_pcm_substream *substream,
  158. struct snd_pcm_hw_params *params,
  159. struct dma_slave_config *slave_config)
  160. {
  161. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  162. struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
  163. int ret;
  164. ret = snd_hwparams_to_dma_slave_config(substream, params,
  165. slave_config);
  166. if (ret) {
  167. dev_err(dd->dev,
  168. "hw params to dma slave configure failed\n");
  169. return ret;
  170. }
  171. slave_config->src_addr = dd->phy_base + PDMIC_CDR;
  172. slave_config->src_maxburst = 1;
  173. slave_config->dst_maxburst = 1;
  174. return 0;
  175. }
  176. static const struct snd_dmaengine_pcm_config
  177. atmel_pdmic_dmaengine_pcm_config = {
  178. .prepare_slave_config = atmel_pdmic_platform_configure_dma,
  179. .pcm_hardware = &atmel_pdmic_hw,
  180. .prealloc_buffer_size = ATMEL_PDMIC_PREALLOC_BUF_SIZE,
  181. };
  182. /* codec */
  183. /* Mic Gain = dgain * 2^(-scale) */
  184. struct mic_gain {
  185. unsigned int dgain;
  186. unsigned int scale;
  187. };
  188. /* range from -90 dB to 90 dB */
  189. static const struct mic_gain mic_gain_table[] = {
  190. { 1, 15}, { 1, 14}, /* -90, -84 dB */
  191. { 3, 15}, { 1, 13}, { 3, 14}, { 1, 12}, /* -81, -78, -75, -72 dB */
  192. { 5, 14}, { 13, 15}, /* -70, -68 dB */
  193. { 9, 14}, { 21, 15}, { 23, 15}, { 13, 14}, /* -65 ~ -62 dB */
  194. { 29, 15}, { 33, 15}, { 37, 15}, { 41, 15}, /* -61 ~ -58 dB */
  195. { 23, 14}, { 13, 13}, { 58, 15}, { 65, 15}, /* -57 ~ -54 dB */
  196. { 73, 15}, { 41, 14}, { 23, 13}, { 13, 12}, /* -53 ~ -50 dB */
  197. { 29, 13}, { 65, 14}, { 73, 14}, { 41, 13}, /* -49 ~ -46 dB */
  198. { 23, 12}, { 207, 15}, { 29, 12}, { 65, 13}, /* -45 ~ -42 dB */
  199. { 73, 13}, { 41, 12}, { 23, 11}, { 413, 15}, /* -41 ~ -38 dB */
  200. { 463, 15}, { 519, 15}, { 583, 15}, { 327, 14}, /* -37 ~ -34 dB */
  201. { 367, 14}, { 823, 15}, { 231, 13}, { 1036, 15}, /* -33 ~ -30 dB */
  202. { 1163, 15}, { 1305, 15}, { 183, 12}, { 1642, 15}, /* -29 ~ -26 dB */
  203. { 1843, 15}, { 2068, 15}, { 145, 11}, { 2603, 15}, /* -25 ~ -22 dB */
  204. { 365, 12}, { 3277, 15}, { 3677, 15}, { 4125, 15}, /* -21 ~ -18 dB */
  205. { 4629, 15}, { 5193, 15}, { 5827, 15}, { 3269, 14}, /* -17 ~ -14 dB */
  206. { 917, 12}, { 8231, 15}, { 9235, 15}, { 5181, 14}, /* -13 ~ -10 dB */
  207. {11627, 15}, {13045, 15}, {14637, 15}, {16423, 15}, /* -9 ~ -6 dB */
  208. {18427, 15}, {20675, 15}, { 5799, 13}, {26029, 15}, /* -5 ~ -2 dB */
  209. { 7301, 13}, { 1, 0}, {18383, 14}, {10313, 13}, /* -1 ~ 2 dB */
  210. {23143, 14}, {25967, 14}, {29135, 14}, {16345, 13}, /* 3 ~ 6 dB */
  211. { 4585, 11}, {20577, 13}, { 1443, 9}, {25905, 13}, /* 7 ~ 10 dB */
  212. {14533, 12}, { 8153, 11}, { 2287, 9}, {20529, 12}, /* 11 ~ 14 dB */
  213. {11517, 11}, { 6461, 10}, {28997, 12}, { 4067, 9}, /* 15 ~ 18 dB */
  214. {18253, 11}, { 10, 0}, {22979, 11}, {25783, 11}, /* 19 ~ 22 dB */
  215. {28929, 11}, {32459, 11}, { 9105, 9}, {20431, 10}, /* 23 ~ 26 dB */
  216. {22925, 10}, {12861, 9}, { 7215, 8}, {16191, 9}, /* 27 ~ 30 dB */
  217. { 9083, 8}, {20383, 9}, {11435, 8}, { 6145, 7}, /* 31 ~ 34 dB */
  218. { 3599, 6}, {32305, 9}, {18123, 8}, {20335, 8}, /* 35 ~ 38 dB */
  219. { 713, 3}, { 100, 0}, { 7181, 6}, { 8057, 6}, /* 39 ~ 42 dB */
  220. { 565, 2}, {20287, 7}, {11381, 6}, {25539, 7}, /* 43 ~ 46 dB */
  221. { 1791, 3}, { 4019, 4}, { 9019, 5}, {20239, 6}, /* 47 ~ 50 dB */
  222. { 5677, 4}, {25479, 6}, { 7147, 4}, { 8019, 4}, /* 51 ~ 54 dB */
  223. {17995, 5}, {20191, 5}, {11327, 4}, {12709, 4}, /* 55 ~ 58 dB */
  224. { 3565, 2}, { 1000, 0}, { 1122, 0}, { 1259, 0}, /* 59 ~ 62 dB */
  225. { 2825, 1}, {12679, 3}, { 7113, 2}, { 7981, 2}, /* 63 ~ 66 dB */
  226. { 8955, 2}, {20095, 3}, {22547, 3}, {12649, 2}, /* 67 ~ 70 dB */
  227. {28385, 3}, { 3981, 0}, {17867, 2}, {20047, 2}, /* 71 ~ 74 dB */
  228. {11247, 1}, {12619, 1}, {14159, 1}, {31773, 2}, /* 75 ~ 78 dB */
  229. {17825, 1}, {10000, 0}, {11220, 0}, {12589, 0}, /* 79 ~ 82 dB */
  230. {28251, 1}, {15849, 0}, {17783, 0}, {19953, 0}, /* 83 ~ 86 dB */
  231. {22387, 0}, {25119, 0}, {28184, 0}, {31623, 0}, /* 87 ~ 90 dB */
  232. };
  233. static const DECLARE_TLV_DB_RANGE(mic_gain_tlv,
  234. 0, 1, TLV_DB_SCALE_ITEM(-9000, 600, 0),
  235. 2, 5, TLV_DB_SCALE_ITEM(-8100, 300, 0),
  236. 6, 7, TLV_DB_SCALE_ITEM(-7000, 200, 0),
  237. 8, ARRAY_SIZE(mic_gain_table)-1, TLV_DB_SCALE_ITEM(-6500, 100, 0),
  238. );
  239. static int pdmic_get_mic_volsw(struct snd_kcontrol *kcontrol,
  240. struct snd_ctl_elem_value *ucontrol)
  241. {
  242. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  243. unsigned int dgain_val, scale_val;
  244. int i;
  245. dgain_val = (snd_soc_component_read(component, PDMIC_DSPR1) & PDMIC_DSPR1_DGAIN_MASK)
  246. >> PDMIC_DSPR1_DGAIN_SHIFT;
  247. scale_val = (snd_soc_component_read(component, PDMIC_DSPR0) & PDMIC_DSPR0_SCALE_MASK)
  248. >> PDMIC_DSPR0_SCALE_SHIFT;
  249. for (i = 0; i < ARRAY_SIZE(mic_gain_table); i++) {
  250. if ((mic_gain_table[i].dgain == dgain_val) &&
  251. (mic_gain_table[i].scale == scale_val))
  252. ucontrol->value.integer.value[0] = i;
  253. }
  254. return 0;
  255. }
  256. static int pdmic_put_mic_volsw(struct snd_kcontrol *kcontrol,
  257. struct snd_ctl_elem_value *ucontrol)
  258. {
  259. struct soc_mixer_control *mc =
  260. (struct soc_mixer_control *)kcontrol->private_value;
  261. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  262. int max = mc->max;
  263. unsigned int val;
  264. int ret;
  265. val = ucontrol->value.integer.value[0];
  266. if (val > max)
  267. return -EINVAL;
  268. ret = snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_MASK,
  269. mic_gain_table[val].dgain << PDMIC_DSPR1_DGAIN_SHIFT);
  270. if (ret < 0)
  271. return ret;
  272. ret = snd_soc_component_update_bits(component, PDMIC_DSPR0, PDMIC_DSPR0_SCALE_MASK,
  273. mic_gain_table[val].scale << PDMIC_DSPR0_SCALE_SHIFT);
  274. if (ret < 0)
  275. return ret;
  276. return 0;
  277. }
  278. static const struct snd_kcontrol_new atmel_pdmic_snd_controls[] = {
  279. SOC_SINGLE_EXT_TLV("Mic Capture Volume", PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_SHIFT,
  280. ARRAY_SIZE(mic_gain_table)-1, 0,
  281. pdmic_get_mic_volsw, pdmic_put_mic_volsw, mic_gain_tlv),
  282. SOC_SINGLE("High Pass Filter Switch", PDMIC_DSPR0,
  283. PDMIC_DSPR0_HPFBYP_SHIFT, 1, 1),
  284. SOC_SINGLE("SINCC Filter Switch", PDMIC_DSPR0, PDMIC_DSPR0_SINBYP_SHIFT, 1, 1),
  285. };
  286. static int atmel_pdmic_component_probe(struct snd_soc_component *component)
  287. {
  288. struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
  289. struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
  290. snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_OFFSET_MASK,
  291. (u32)(dd->pdata->mic_offset << PDMIC_DSPR1_OFFSET_SHIFT));
  292. return 0;
  293. }
  294. #define PDMIC_MR_PRESCAL_MAX_VAL 127
  295. static int
  296. atmel_pdmic_cpu_dai_hw_params(struct snd_pcm_substream *substream,
  297. struct snd_pcm_hw_params *params,
  298. struct snd_soc_dai *cpu_dai)
  299. {
  300. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  301. struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
  302. struct snd_soc_component *component = cpu_dai->component;
  303. unsigned int rate_min = substream->runtime->hw.rate_min;
  304. unsigned int rate_max = substream->runtime->hw.rate_max;
  305. int fs = params_rate(params);
  306. int bits = params_width(params);
  307. unsigned long pclk_rate, gclk_rate;
  308. unsigned int f_pdmic;
  309. u32 mr_val, dspr0_val, pclk_prescal, gclk_prescal;
  310. if (params_channels(params) != 1) {
  311. dev_err(component->dev,
  312. "only supports one channel\n");
  313. return -EINVAL;
  314. }
  315. if ((fs < rate_min) || (fs > rate_max)) {
  316. dev_err(component->dev,
  317. "sample rate is %dHz, min rate is %dHz, max rate is %dHz\n",
  318. fs, rate_min, rate_max);
  319. return -EINVAL;
  320. }
  321. switch (bits) {
  322. case 16:
  323. dspr0_val = (PDMIC_DSPR0_SIZE_16_BITS
  324. << PDMIC_DSPR0_SIZE_SHIFT);
  325. break;
  326. case 32:
  327. dspr0_val = (PDMIC_DSPR0_SIZE_32_BITS
  328. << PDMIC_DSPR0_SIZE_SHIFT);
  329. break;
  330. default:
  331. return -EINVAL;
  332. }
  333. if ((fs << 7) > (rate_max << 6)) {
  334. f_pdmic = fs << 6;
  335. dspr0_val |= PDMIC_DSPR0_OSR_64 << PDMIC_DSPR0_OSR_SHIFT;
  336. } else {
  337. f_pdmic = fs << 7;
  338. dspr0_val |= PDMIC_DSPR0_OSR_128 << PDMIC_DSPR0_OSR_SHIFT;
  339. }
  340. pclk_rate = clk_get_rate(dd->pclk);
  341. gclk_rate = clk_get_rate(dd->gclk);
  342. /* PRESCAL = SELCK/(2*f_pdmic) - 1*/
  343. pclk_prescal = (u32)(pclk_rate/(f_pdmic << 1)) - 1;
  344. gclk_prescal = (u32)(gclk_rate/(f_pdmic << 1)) - 1;
  345. if ((pclk_prescal > PDMIC_MR_PRESCAL_MAX_VAL) ||
  346. (gclk_rate/((gclk_prescal + 1) << 1) <
  347. pclk_rate/((pclk_prescal + 1) << 1))) {
  348. mr_val = gclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
  349. mr_val |= PDMIC_MR_CLKS_GCK << PDMIC_MR_CLKS_SHIFT;
  350. } else {
  351. mr_val = pclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
  352. mr_val |= PDMIC_MR_CLKS_PCK << PDMIC_MR_CLKS_SHIFT;
  353. }
  354. snd_soc_component_update_bits(component, PDMIC_MR,
  355. PDMIC_MR_PRESCAL_MASK | PDMIC_MR_CLKS_MASK, mr_val);
  356. snd_soc_component_update_bits(component, PDMIC_DSPR0,
  357. PDMIC_DSPR0_OSR_MASK | PDMIC_DSPR0_SIZE_MASK, dspr0_val);
  358. return 0;
  359. }
  360. static int atmel_pdmic_cpu_dai_trigger(struct snd_pcm_substream *substream,
  361. int cmd, struct snd_soc_dai *cpu_dai)
  362. {
  363. struct snd_soc_component *component = cpu_dai->component;
  364. u32 val;
  365. switch (cmd) {
  366. case SNDRV_PCM_TRIGGER_START:
  367. case SNDRV_PCM_TRIGGER_RESUME:
  368. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  369. val = PDMIC_CR_ENPDM_EN << PDMIC_CR_ENPDM_SHIFT;
  370. break;
  371. case SNDRV_PCM_TRIGGER_STOP:
  372. case SNDRV_PCM_TRIGGER_SUSPEND:
  373. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  374. val = PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT;
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. snd_soc_component_update_bits(component, PDMIC_CR, PDMIC_CR_ENPDM_MASK, val);
  380. return 0;
  381. }
  382. static const struct snd_soc_dai_ops atmel_pdmic_cpu_dai_ops = {
  383. .startup = atmel_pdmic_cpu_dai_startup,
  384. .shutdown = atmel_pdmic_cpu_dai_shutdown,
  385. .prepare = atmel_pdmic_cpu_dai_prepare,
  386. .hw_params = atmel_pdmic_cpu_dai_hw_params,
  387. .trigger = atmel_pdmic_cpu_dai_trigger,
  388. };
  389. static struct snd_soc_dai_driver atmel_pdmic_cpu_dai = {
  390. .capture = {
  391. .stream_name = "Capture",
  392. .channels_min = 1,
  393. .channels_max = 1,
  394. .rates = SNDRV_PCM_RATE_KNOT,
  395. .formats = ATMEL_PDMIC_FORMATS,
  396. },
  397. .ops = &atmel_pdmic_cpu_dai_ops,
  398. };
  399. static const struct snd_soc_component_driver atmel_pdmic_cpu_dai_component = {
  400. .name = "atmel-pdmic",
  401. .probe = atmel_pdmic_component_probe,
  402. .controls = atmel_pdmic_snd_controls,
  403. .num_controls = ARRAY_SIZE(atmel_pdmic_snd_controls),
  404. .idle_bias_on = 1,
  405. .use_pmdown_time = 1,
  406. .legacy_dai_naming = 1,
  407. };
  408. /* ASoC sound card */
  409. static int atmel_pdmic_asoc_card_init(struct device *dev,
  410. struct snd_soc_card *card)
  411. {
  412. struct snd_soc_dai_link *dai_link;
  413. struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
  414. struct snd_soc_dai_link_component *comp;
  415. dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL);
  416. if (!dai_link)
  417. return -ENOMEM;
  418. comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
  419. if (!comp)
  420. return -ENOMEM;
  421. dai_link->cpus = comp;
  422. dai_link->codecs = &snd_soc_dummy_dlc;
  423. dai_link->num_cpus = 1;
  424. dai_link->num_codecs = 1;
  425. dai_link->name = "PDMIC";
  426. dai_link->stream_name = "PDMIC PCM";
  427. dai_link->cpus->dai_name = dev_name(dev);
  428. card->dai_link = dai_link;
  429. card->num_links = 1;
  430. card->name = dd->pdata->card_name;
  431. card->dev = dev;
  432. return 0;
  433. }
  434. static void atmel_pdmic_get_sample_rate(struct atmel_pdmic *dd,
  435. unsigned int *rate_min, unsigned int *rate_max)
  436. {
  437. u32 mic_min_freq = dd->pdata->mic_min_freq;
  438. u32 mic_max_freq = dd->pdata->mic_max_freq;
  439. u32 clk_max_rate = (u32)(clk_get_rate(dd->pclk) >> 1);
  440. u32 clk_min_rate = (u32)(clk_get_rate(dd->gclk) >> 8);
  441. if (mic_max_freq > clk_max_rate)
  442. mic_max_freq = clk_max_rate;
  443. if (mic_min_freq < clk_min_rate)
  444. mic_min_freq = clk_min_rate;
  445. *rate_min = DIV_ROUND_CLOSEST(mic_min_freq, 128);
  446. *rate_max = mic_max_freq >> 6;
  447. }
  448. /* PDMIC interrupt handler */
  449. static irqreturn_t atmel_pdmic_interrupt(int irq, void *dev_id)
  450. {
  451. struct atmel_pdmic *dd = (struct atmel_pdmic *)dev_id;
  452. u32 pdmic_isr;
  453. irqreturn_t ret = IRQ_NONE;
  454. regmap_read(dd->regmap, PDMIC_ISR, &pdmic_isr);
  455. if (pdmic_isr & PDMIC_ISR_OVRE) {
  456. regmap_update_bits(dd->regmap, PDMIC_CR, PDMIC_CR_ENPDM_MASK,
  457. PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT);
  458. snd_pcm_stop_xrun(dd->substream);
  459. ret = IRQ_HANDLED;
  460. }
  461. return ret;
  462. }
  463. /* regmap configuration */
  464. #define ATMEL_PDMIC_REG_MAX 0x124
  465. static const struct regmap_config atmel_pdmic_regmap_config = {
  466. .reg_bits = 32,
  467. .reg_stride = 4,
  468. .val_bits = 32,
  469. .max_register = ATMEL_PDMIC_REG_MAX,
  470. };
  471. static int atmel_pdmic_probe(struct platform_device *pdev)
  472. {
  473. struct device *dev = &pdev->dev;
  474. struct atmel_pdmic *dd;
  475. struct resource *res;
  476. void __iomem *io_base;
  477. const struct atmel_pdmic_pdata *pdata;
  478. struct snd_soc_card *card;
  479. unsigned int rate_min, rate_max;
  480. int ret;
  481. pdata = atmel_pdmic_dt_init(dev);
  482. if (IS_ERR(pdata))
  483. return PTR_ERR(pdata);
  484. dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
  485. if (!dd)
  486. return -ENOMEM;
  487. dd->pdata = pdata;
  488. dd->dev = dev;
  489. dd->irq = platform_get_irq(pdev, 0);
  490. if (dd->irq < 0)
  491. return dd->irq;
  492. dd->pclk = devm_clk_get(dev, "pclk");
  493. if (IS_ERR(dd->pclk)) {
  494. ret = PTR_ERR(dd->pclk);
  495. dev_err(dev, "failed to get peripheral clock: %d\n", ret);
  496. return ret;
  497. }
  498. dd->gclk = devm_clk_get(dev, "gclk");
  499. if (IS_ERR(dd->gclk)) {
  500. ret = PTR_ERR(dd->gclk);
  501. dev_err(dev, "failed to get GCK: %d\n", ret);
  502. return ret;
  503. }
  504. /* The gclk clock frequency must always be three times
  505. * lower than the pclk clock frequency
  506. */
  507. ret = clk_set_rate(dd->gclk, clk_get_rate(dd->pclk)/3);
  508. if (ret) {
  509. dev_err(dev, "failed to set GCK clock rate: %d\n", ret);
  510. return ret;
  511. }
  512. io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  513. if (IS_ERR(io_base))
  514. return PTR_ERR(io_base);
  515. dd->phy_base = res->start;
  516. dd->regmap = devm_regmap_init_mmio(dev, io_base,
  517. &atmel_pdmic_regmap_config);
  518. if (IS_ERR(dd->regmap)) {
  519. ret = PTR_ERR(dd->regmap);
  520. dev_err(dev, "failed to init register map: %d\n", ret);
  521. return ret;
  522. }
  523. ret = devm_request_irq(dev, dd->irq, atmel_pdmic_interrupt, 0,
  524. "PDMIC", (void *)dd);
  525. if (ret < 0) {
  526. dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
  527. dd->irq, ret);
  528. return ret;
  529. }
  530. /* Get the minimal and maximal sample rate that the microphone supports */
  531. atmel_pdmic_get_sample_rate(dd, &rate_min, &rate_max);
  532. /* register cpu dai */
  533. atmel_pdmic_cpu_dai.capture.rate_min = rate_min;
  534. atmel_pdmic_cpu_dai.capture.rate_max = rate_max;
  535. ret = devm_snd_soc_register_component(dev,
  536. &atmel_pdmic_cpu_dai_component,
  537. &atmel_pdmic_cpu_dai, 1);
  538. if (ret) {
  539. dev_err(dev, "could not register CPU DAI: %d\n", ret);
  540. return ret;
  541. }
  542. /* register platform */
  543. ret = devm_snd_dmaengine_pcm_register(dev,
  544. &atmel_pdmic_dmaengine_pcm_config,
  545. 0);
  546. if (ret) {
  547. dev_err(dev, "could not register platform: %d\n", ret);
  548. return ret;
  549. }
  550. /* register sound card */
  551. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  552. if (!card) {
  553. ret = -ENOMEM;
  554. goto unregister_codec;
  555. }
  556. snd_soc_card_set_drvdata(card, dd);
  557. ret = atmel_pdmic_asoc_card_init(dev, card);
  558. if (ret) {
  559. dev_err(dev, "failed to init sound card: %d\n", ret);
  560. goto unregister_codec;
  561. }
  562. ret = devm_snd_soc_register_card(dev, card);
  563. if (ret) {
  564. dev_err(dev, "failed to register sound card: %d\n", ret);
  565. goto unregister_codec;
  566. }
  567. return 0;
  568. unregister_codec:
  569. return ret;
  570. }
  571. static struct platform_driver atmel_pdmic_driver = {
  572. .driver = {
  573. .name = "atmel-pdmic",
  574. .of_match_table = atmel_pdmic_of_match,
  575. .pm = &snd_soc_pm_ops,
  576. },
  577. .probe = atmel_pdmic_probe,
  578. };
  579. module_platform_driver(atmel_pdmic_driver);
  580. MODULE_DESCRIPTION("Atmel PDMIC driver under ALSA SoC architecture");
  581. MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
  582. MODULE_LICENSE("GPL v2");