sun4i-codec.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * Copyright 2014 Emilio López <emilio@elopez.com.ar>
  3. * Copyright 2014 Jon Smirl <jonsmirl@gmail.com>
  4. * Copyright 2015 Maxime Ripard <maxime.ripard@free-electrons.com>
  5. * Copyright 2015 Adam Sampson <ats@offog.org>
  6. *
  7. * Based on the Allwinner SDK driver, released under the GPL.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/of_address.h>
  28. #include <linux/clk.h>
  29. #include <linux/regmap.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <sound/core.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include <sound/tlv.h>
  36. #include <sound/initval.h>
  37. #include <sound/dmaengine_pcm.h>
  38. /* Codec DAC register offsets and bit fields */
  39. #define SUN4I_CODEC_DAC_DPC (0x00)
  40. #define SUN4I_CODEC_DAC_DPC_EN_DA (31)
  41. #define SUN4I_CODEC_DAC_DPC_DVOL (12)
  42. #define SUN4I_CODEC_DAC_FIFOC (0x04)
  43. #define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
  44. #define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
  45. #define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
  46. #define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
  47. #define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
  48. #define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
  49. #define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
  50. #define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
  51. #define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
  52. #define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
  53. #define SUN4I_CODEC_DAC_FIFOS (0x08)
  54. #define SUN4I_CODEC_DAC_TXDATA (0x0c)
  55. #define SUN4I_CODEC_DAC_ACTL (0x10)
  56. #define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
  57. #define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
  58. #define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
  59. #define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
  60. #define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
  61. #define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
  62. #define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
  63. #define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
  64. #define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
  65. #define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
  66. #define SUN4I_CODEC_DAC_TUNE (0x14)
  67. #define SUN4I_CODEC_DAC_DEBUG (0x18)
  68. /* Codec ADC register offsets and bit fields */
  69. #define SUN4I_CODEC_ADC_FIFOC (0x1c)
  70. #define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
  71. #define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
  72. #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
  73. #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
  74. #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
  75. #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
  76. #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
  77. #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
  78. #define SUN4I_CODEC_ADC_FIFOS (0x20)
  79. #define SUN4I_CODEC_ADC_RXDATA (0x24)
  80. #define SUN4I_CODEC_ADC_ACTL (0x28)
  81. #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
  82. #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
  83. #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
  84. #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
  85. #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
  86. #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
  87. #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
  88. #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
  89. #define SUN4I_CODEC_ADC_ACTL_DDE (3)
  90. #define SUN4I_CODEC_ADC_DEBUG (0x2c)
  91. /* Other various ADC registers */
  92. #define SUN4I_CODEC_DAC_TXCNT (0x30)
  93. #define SUN4I_CODEC_ADC_RXCNT (0x34)
  94. #define SUN7I_CODEC_AC_DAC_CAL (0x38)
  95. #define SUN7I_CODEC_AC_MIC_PHONE_CAL (0x3c)
  96. struct sun4i_codec {
  97. struct device *dev;
  98. struct regmap *regmap;
  99. struct clk *clk_apb;
  100. struct clk *clk_module;
  101. struct gpio_desc *gpio_pa;
  102. struct snd_dmaengine_dai_dma_data capture_dma_data;
  103. struct snd_dmaengine_dai_dma_data playback_dma_data;
  104. };
  105. static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
  106. {
  107. /* Flush TX FIFO */
  108. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  109. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
  110. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
  111. /* Enable DAC DRQ */
  112. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  113. BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
  114. BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
  115. }
  116. static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
  117. {
  118. /* Disable DAC DRQ */
  119. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  120. BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
  121. 0);
  122. }
  123. static void sun4i_codec_start_capture(struct sun4i_codec *scodec)
  124. {
  125. /* Enable ADC DRQ */
  126. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  127. BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN),
  128. BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
  129. }
  130. static void sun4i_codec_stop_capture(struct sun4i_codec *scodec)
  131. {
  132. /* Disable ADC DRQ */
  133. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  134. BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN), 0);
  135. }
  136. static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
  137. struct snd_soc_dai *dai)
  138. {
  139. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  140. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  141. switch (cmd) {
  142. case SNDRV_PCM_TRIGGER_START:
  143. case SNDRV_PCM_TRIGGER_RESUME:
  144. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  145. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  146. sun4i_codec_start_playback(scodec);
  147. else
  148. sun4i_codec_start_capture(scodec);
  149. break;
  150. case SNDRV_PCM_TRIGGER_STOP:
  151. case SNDRV_PCM_TRIGGER_SUSPEND:
  152. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  153. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  154. sun4i_codec_stop_playback(scodec);
  155. else
  156. sun4i_codec_stop_capture(scodec);
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. static int sun4i_codec_prepare_capture(struct snd_pcm_substream *substream,
  164. struct snd_soc_dai *dai)
  165. {
  166. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  167. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  168. /* Flush RX FIFO */
  169. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  170. BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH),
  171. BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH));
  172. /* Set RX FIFO trigger level */
  173. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  174. 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL,
  175. 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL);
  176. /*
  177. * FIXME: Undocumented in the datasheet, but
  178. * Allwinner's code mentions that it is related
  179. * related to microphone gain
  180. */
  181. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_ACTL,
  182. 0x3 << 25,
  183. 0x1 << 25);
  184. if (of_device_is_compatible(scodec->dev->of_node,
  185. "allwinner,sun7i-a20-codec"))
  186. /* FIXME: Undocumented bits */
  187. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_TUNE,
  188. 0x3 << 8,
  189. 0x1 << 8);
  190. /* Fill most significant bits with valid data MSB */
  191. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  192. BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
  193. BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
  194. return 0;
  195. }
  196. static int sun4i_codec_prepare_playback(struct snd_pcm_substream *substream,
  197. struct snd_soc_dai *dai)
  198. {
  199. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  200. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  201. u32 val;
  202. /* Flush the TX FIFO */
  203. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  204. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
  205. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
  206. /* Set TX FIFO Empty Trigger Level */
  207. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  208. 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
  209. 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
  210. if (substream->runtime->rate > 32000)
  211. /* Use 64 bits FIR filter */
  212. val = 0;
  213. else
  214. /* Use 32 bits FIR filter */
  215. val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
  216. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  217. BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
  218. val);
  219. /* Send zeros when we have an underrun */
  220. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  221. BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
  222. 0);
  223. return 0;
  224. };
  225. static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
  226. struct snd_soc_dai *dai)
  227. {
  228. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  229. return sun4i_codec_prepare_playback(substream, dai);
  230. return sun4i_codec_prepare_capture(substream, dai);
  231. }
  232. static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
  233. {
  234. unsigned int rate = params_rate(params);
  235. switch (rate) {
  236. case 176400:
  237. case 88200:
  238. case 44100:
  239. case 33075:
  240. case 22050:
  241. case 14700:
  242. case 11025:
  243. case 7350:
  244. return 22579200;
  245. case 192000:
  246. case 96000:
  247. case 48000:
  248. case 32000:
  249. case 24000:
  250. case 16000:
  251. case 12000:
  252. case 8000:
  253. return 24576000;
  254. default:
  255. return 0;
  256. }
  257. }
  258. static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
  259. {
  260. unsigned int rate = params_rate(params);
  261. switch (rate) {
  262. case 192000:
  263. case 176400:
  264. return 6;
  265. case 96000:
  266. case 88200:
  267. return 7;
  268. case 48000:
  269. case 44100:
  270. return 0;
  271. case 32000:
  272. case 33075:
  273. return 1;
  274. case 24000:
  275. case 22050:
  276. return 2;
  277. case 16000:
  278. case 14700:
  279. return 3;
  280. case 12000:
  281. case 11025:
  282. return 4;
  283. case 8000:
  284. case 7350:
  285. return 5;
  286. default:
  287. return -EINVAL;
  288. }
  289. }
  290. static int sun4i_codec_hw_params_capture(struct sun4i_codec *scodec,
  291. struct snd_pcm_hw_params *params,
  292. unsigned int hwrate)
  293. {
  294. /* Set ADC sample rate */
  295. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  296. 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS,
  297. hwrate << SUN4I_CODEC_ADC_FIFOC_ADC_FS);
  298. /* Set the number of channels we want to use */
  299. if (params_channels(params) == 1)
  300. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  301. BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
  302. BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
  303. else
  304. regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_FIFOC,
  305. BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN), 0);
  306. return 0;
  307. }
  308. static int sun4i_codec_hw_params_playback(struct sun4i_codec *scodec,
  309. struct snd_pcm_hw_params *params,
  310. unsigned int hwrate)
  311. {
  312. u32 val;
  313. /* Set DAC sample rate */
  314. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  315. 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
  316. hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
  317. /* Set the number of channels we want to use */
  318. if (params_channels(params) == 1)
  319. val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
  320. else
  321. val = 0;
  322. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  323. BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
  324. val);
  325. /* Set the number of sample bits to either 16 or 24 bits */
  326. if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
  327. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  328. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
  329. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
  330. /* Set TX FIFO mode to padding the LSBs with 0 */
  331. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  332. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
  333. 0);
  334. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  335. } else {
  336. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  337. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
  338. 0);
  339. /* Set TX FIFO mode to repeat the MSB */
  340. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  341. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
  342. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
  343. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  344. }
  345. return 0;
  346. }
  347. static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
  348. struct snd_pcm_hw_params *params,
  349. struct snd_soc_dai *dai)
  350. {
  351. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  352. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  353. unsigned long clk_freq;
  354. int ret, hwrate;
  355. clk_freq = sun4i_codec_get_mod_freq(params);
  356. if (!clk_freq)
  357. return -EINVAL;
  358. ret = clk_set_rate(scodec->clk_module, clk_freq);
  359. if (ret)
  360. return ret;
  361. hwrate = sun4i_codec_get_hw_rate(params);
  362. if (hwrate < 0)
  363. return hwrate;
  364. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  365. return sun4i_codec_hw_params_playback(scodec, params,
  366. hwrate);
  367. return sun4i_codec_hw_params_capture(scodec, params,
  368. hwrate);
  369. }
  370. static int sun4i_codec_startup(struct snd_pcm_substream *substream,
  371. struct snd_soc_dai *dai)
  372. {
  373. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  374. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  375. /*
  376. * Stop issuing DRQ when we have room for less than 16 samples
  377. * in our TX FIFO
  378. */
  379. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  380. 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
  381. 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
  382. return clk_prepare_enable(scodec->clk_module);
  383. }
  384. static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
  385. struct snd_soc_dai *dai)
  386. {
  387. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  388. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  389. clk_disable_unprepare(scodec->clk_module);
  390. }
  391. static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
  392. .startup = sun4i_codec_startup,
  393. .shutdown = sun4i_codec_shutdown,
  394. .trigger = sun4i_codec_trigger,
  395. .hw_params = sun4i_codec_hw_params,
  396. .prepare = sun4i_codec_prepare,
  397. };
  398. static struct snd_soc_dai_driver sun4i_codec_dai = {
  399. .name = "Codec",
  400. .ops = &sun4i_codec_dai_ops,
  401. .playback = {
  402. .stream_name = "Codec Playback",
  403. .channels_min = 1,
  404. .channels_max = 2,
  405. .rate_min = 8000,
  406. .rate_max = 192000,
  407. .rates = SNDRV_PCM_RATE_8000_48000 |
  408. SNDRV_PCM_RATE_96000 |
  409. SNDRV_PCM_RATE_192000,
  410. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  411. SNDRV_PCM_FMTBIT_S32_LE,
  412. .sig_bits = 24,
  413. },
  414. .capture = {
  415. .stream_name = "Codec Capture",
  416. .channels_min = 1,
  417. .channels_max = 2,
  418. .rate_min = 8000,
  419. .rate_max = 192000,
  420. .rates = SNDRV_PCM_RATE_8000_48000 |
  421. SNDRV_PCM_RATE_96000 |
  422. SNDRV_PCM_RATE_192000 |
  423. SNDRV_PCM_RATE_KNOT,
  424. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  425. SNDRV_PCM_FMTBIT_S32_LE,
  426. .sig_bits = 24,
  427. },
  428. };
  429. /*** Codec ***/
  430. static const struct snd_kcontrol_new sun4i_codec_pa_mute =
  431. SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
  432. SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
  433. static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
  434. static const struct snd_kcontrol_new sun4i_codec_controls[] = {
  435. SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
  436. SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
  437. sun4i_codec_pa_volume_scale),
  438. };
  439. static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls[] = {
  440. SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  441. SUN4I_CODEC_DAC_ACTL_LDACLMIXS, 1, 0),
  442. };
  443. static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls[] = {
  444. SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  445. SUN4I_CODEC_DAC_ACTL_RDACRMIXS, 1, 0),
  446. SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  447. SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
  448. };
  449. static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
  450. SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  451. SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
  452. SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
  453. SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
  454. };
  455. static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets[] = {
  456. /* Digital parts of the ADCs */
  457. SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC,
  458. SUN4I_CODEC_ADC_FIFOC_EN_AD, 0,
  459. NULL, 0),
  460. /* Digital parts of the DACs */
  461. SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
  462. SUN4I_CODEC_DAC_DPC_EN_DA, 0,
  463. NULL, 0),
  464. /* Analog parts of the ADCs */
  465. SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
  466. SUN4I_CODEC_ADC_ACTL_ADC_L_EN, 0),
  467. SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
  468. SUN4I_CODEC_ADC_ACTL_ADC_R_EN, 0),
  469. /* Analog parts of the DACs */
  470. SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
  471. SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
  472. SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
  473. SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
  474. /* Mixers */
  475. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  476. sun4i_codec_left_mixer_controls,
  477. ARRAY_SIZE(sun4i_codec_left_mixer_controls)),
  478. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  479. sun4i_codec_right_mixer_controls,
  480. ARRAY_SIZE(sun4i_codec_right_mixer_controls)),
  481. /* Global Mixer Enable */
  482. SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
  483. SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
  484. /* VMIC */
  485. SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL,
  486. SUN4I_CODEC_ADC_ACTL_VMICEN, 0, NULL, 0),
  487. /* Mic Pre-Amplifiers */
  488. SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
  489. SUN4I_CODEC_ADC_ACTL_PREG1EN, 0, NULL, 0),
  490. /* Power Amplifier */
  491. SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
  492. SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
  493. sun4i_codec_pa_mixer_controls,
  494. ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
  495. SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
  496. &sun4i_codec_pa_mute),
  497. SND_SOC_DAPM_INPUT("Mic1"),
  498. SND_SOC_DAPM_OUTPUT("HP Right"),
  499. SND_SOC_DAPM_OUTPUT("HP Left"),
  500. };
  501. static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes[] = {
  502. /* Left ADC / DAC Routes */
  503. { "Left ADC", NULL, "ADC" },
  504. { "Left DAC", NULL, "DAC" },
  505. /* Right ADC / DAC Routes */
  506. { "Right ADC", NULL, "ADC" },
  507. { "Right DAC", NULL, "DAC" },
  508. /* Right Mixer Routes */
  509. { "Right Mixer", NULL, "Mixer Enable" },
  510. { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
  511. { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
  512. /* Left Mixer Routes */
  513. { "Left Mixer", NULL, "Mixer Enable" },
  514. { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
  515. /* Power Amplifier Routes */
  516. { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
  517. { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
  518. { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
  519. { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
  520. /* Headphone Output Routes */
  521. { "Power Amplifier Mute", "Switch", "Power Amplifier" },
  522. { "HP Right", NULL, "Power Amplifier Mute" },
  523. { "HP Left", NULL, "Power Amplifier Mute" },
  524. /* Mic1 Routes */
  525. { "Left ADC", NULL, "MIC1 Pre-Amplifier" },
  526. { "Right ADC", NULL, "MIC1 Pre-Amplifier" },
  527. { "MIC1 Pre-Amplifier", NULL, "Mic1"},
  528. { "Mic1", NULL, "VMIC" },
  529. };
  530. static struct snd_soc_codec_driver sun4i_codec_codec = {
  531. .component_driver = {
  532. .controls = sun4i_codec_controls,
  533. .num_controls = ARRAY_SIZE(sun4i_codec_controls),
  534. .dapm_widgets = sun4i_codec_codec_dapm_widgets,
  535. .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
  536. .dapm_routes = sun4i_codec_codec_dapm_routes,
  537. .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
  538. },
  539. };
  540. static const struct snd_soc_component_driver sun4i_codec_component = {
  541. .name = "sun4i-codec",
  542. };
  543. #define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
  544. #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  545. SNDRV_PCM_FMTBIT_S32_LE)
  546. static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
  547. {
  548. struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
  549. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
  550. snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
  551. &scodec->capture_dma_data);
  552. return 0;
  553. }
  554. static struct snd_soc_dai_driver dummy_cpu_dai = {
  555. .name = "sun4i-codec-cpu-dai",
  556. .probe = sun4i_codec_dai_probe,
  557. .playback = {
  558. .stream_name = "Playback",
  559. .channels_min = 1,
  560. .channels_max = 2,
  561. .rates = SUN4I_CODEC_RATES,
  562. .formats = SUN4I_CODEC_FORMATS,
  563. .sig_bits = 24,
  564. },
  565. .capture = {
  566. .stream_name = "Capture",
  567. .channels_min = 1,
  568. .channels_max = 2,
  569. .rates = SUN4I_CODEC_RATES,
  570. .formats = SUN4I_CODEC_FORMATS,
  571. .sig_bits = 24,
  572. },
  573. };
  574. static const struct regmap_config sun4i_codec_regmap_config = {
  575. .reg_bits = 32,
  576. .reg_stride = 4,
  577. .val_bits = 32,
  578. .max_register = SUN4I_CODEC_ADC_RXCNT,
  579. };
  580. static const struct regmap_config sun7i_codec_regmap_config = {
  581. .reg_bits = 32,
  582. .reg_stride = 4,
  583. .val_bits = 32,
  584. .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
  585. };
  586. struct sun4i_codec_quirks {
  587. const struct regmap_config *regmap_config;
  588. };
  589. static const struct sun4i_codec_quirks sun4i_codec_quirks = {
  590. .regmap_config = &sun4i_codec_regmap_config,
  591. };
  592. static const struct sun4i_codec_quirks sun7i_codec_quirks = {
  593. .regmap_config = &sun7i_codec_regmap_config,
  594. };
  595. static const struct of_device_id sun4i_codec_of_match[] = {
  596. {
  597. .compatible = "allwinner,sun4i-a10-codec",
  598. .data = &sun4i_codec_quirks,
  599. },
  600. {
  601. .compatible = "allwinner,sun7i-a20-codec",
  602. .data = &sun7i_codec_quirks,
  603. },
  604. {}
  605. };
  606. MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
  607. static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
  608. int *num_links)
  609. {
  610. struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
  611. GFP_KERNEL);
  612. if (!link)
  613. return NULL;
  614. link->name = "cdc";
  615. link->stream_name = "CDC PCM";
  616. link->codec_dai_name = "Codec";
  617. link->cpu_dai_name = dev_name(dev);
  618. link->codec_name = dev_name(dev);
  619. link->platform_name = dev_name(dev);
  620. link->dai_fmt = SND_SOC_DAIFMT_I2S;
  621. *num_links = 1;
  622. return link;
  623. };
  624. static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
  625. struct snd_kcontrol *k, int event)
  626. {
  627. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
  628. if (scodec->gpio_pa)
  629. gpiod_set_value_cansleep(scodec->gpio_pa,
  630. !!SND_SOC_DAPM_EVENT_ON(event));
  631. return 0;
  632. }
  633. static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
  634. SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
  635. };
  636. static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
  637. { "Speaker", NULL, "HP Right" },
  638. { "Speaker", NULL, "HP Left" },
  639. };
  640. static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
  641. {
  642. struct snd_soc_card *card;
  643. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  644. if (!card)
  645. return ERR_PTR(-ENOMEM);
  646. card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
  647. if (!card->dai_link)
  648. return ERR_PTR(-ENOMEM);
  649. card->dev = dev;
  650. card->name = "sun4i-codec";
  651. card->dapm_widgets = sun4i_codec_card_dapm_widgets;
  652. card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
  653. card->dapm_routes = sun4i_codec_card_dapm_routes;
  654. card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
  655. return card;
  656. };
  657. static int sun4i_codec_probe(struct platform_device *pdev)
  658. {
  659. struct snd_soc_card *card;
  660. struct sun4i_codec *scodec;
  661. const struct sun4i_codec_quirks *quirks;
  662. struct resource *res;
  663. void __iomem *base;
  664. int ret;
  665. scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
  666. if (!scodec)
  667. return -ENOMEM;
  668. scodec->dev = &pdev->dev;
  669. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  670. base = devm_ioremap_resource(&pdev->dev, res);
  671. if (IS_ERR(base)) {
  672. dev_err(&pdev->dev, "Failed to map the registers\n");
  673. return PTR_ERR(base);
  674. }
  675. quirks = of_device_get_match_data(&pdev->dev);
  676. if (quirks == NULL) {
  677. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  678. return -ENODEV;
  679. }
  680. scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  681. quirks->regmap_config);
  682. if (IS_ERR(scodec->regmap)) {
  683. dev_err(&pdev->dev, "Failed to create our regmap\n");
  684. return PTR_ERR(scodec->regmap);
  685. }
  686. /* Get the clocks from the DT */
  687. scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
  688. if (IS_ERR(scodec->clk_apb)) {
  689. dev_err(&pdev->dev, "Failed to get the APB clock\n");
  690. return PTR_ERR(scodec->clk_apb);
  691. }
  692. scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
  693. if (IS_ERR(scodec->clk_module)) {
  694. dev_err(&pdev->dev, "Failed to get the module clock\n");
  695. return PTR_ERR(scodec->clk_module);
  696. }
  697. scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
  698. GPIOD_OUT_LOW);
  699. if (IS_ERR(scodec->gpio_pa)) {
  700. ret = PTR_ERR(scodec->gpio_pa);
  701. if (ret != -EPROBE_DEFER)
  702. dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
  703. return ret;
  704. }
  705. /* Enable the bus clock */
  706. if (clk_prepare_enable(scodec->clk_apb)) {
  707. dev_err(&pdev->dev, "Failed to enable the APB clock\n");
  708. return -EINVAL;
  709. }
  710. /* DMA configuration for TX FIFO */
  711. scodec->playback_dma_data.addr = res->start + SUN4I_CODEC_DAC_TXDATA;
  712. scodec->playback_dma_data.maxburst = 4;
  713. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  714. /* DMA configuration for RX FIFO */
  715. scodec->capture_dma_data.addr = res->start + SUN4I_CODEC_ADC_RXDATA;
  716. scodec->capture_dma_data.maxburst = 4;
  717. scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  718. ret = snd_soc_register_codec(&pdev->dev, &sun4i_codec_codec,
  719. &sun4i_codec_dai, 1);
  720. if (ret) {
  721. dev_err(&pdev->dev, "Failed to register our codec\n");
  722. goto err_clk_disable;
  723. }
  724. ret = devm_snd_soc_register_component(&pdev->dev,
  725. &sun4i_codec_component,
  726. &dummy_cpu_dai, 1);
  727. if (ret) {
  728. dev_err(&pdev->dev, "Failed to register our DAI\n");
  729. goto err_unregister_codec;
  730. }
  731. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  732. if (ret) {
  733. dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
  734. goto err_unregister_codec;
  735. }
  736. card = sun4i_codec_create_card(&pdev->dev);
  737. if (IS_ERR(card)) {
  738. ret = PTR_ERR(card);
  739. dev_err(&pdev->dev, "Failed to create our card\n");
  740. goto err_unregister_codec;
  741. }
  742. platform_set_drvdata(pdev, card);
  743. snd_soc_card_set_drvdata(card, scodec);
  744. ret = snd_soc_register_card(card);
  745. if (ret) {
  746. dev_err(&pdev->dev, "Failed to register our card\n");
  747. goto err_unregister_codec;
  748. }
  749. return 0;
  750. err_unregister_codec:
  751. snd_soc_unregister_codec(&pdev->dev);
  752. err_clk_disable:
  753. clk_disable_unprepare(scodec->clk_apb);
  754. return ret;
  755. }
  756. static int sun4i_codec_remove(struct platform_device *pdev)
  757. {
  758. struct snd_soc_card *card = platform_get_drvdata(pdev);
  759. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
  760. snd_soc_unregister_card(card);
  761. snd_soc_unregister_codec(&pdev->dev);
  762. clk_disable_unprepare(scodec->clk_apb);
  763. return 0;
  764. }
  765. static struct platform_driver sun4i_codec_driver = {
  766. .driver = {
  767. .name = "sun4i-codec",
  768. .of_match_table = sun4i_codec_of_match,
  769. },
  770. .probe = sun4i_codec_probe,
  771. .remove = sun4i_codec_remove,
  772. };
  773. module_platform_driver(sun4i_codec_driver);
  774. MODULE_DESCRIPTION("Allwinner A10 codec driver");
  775. MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
  776. MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
  777. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  778. MODULE_LICENSE("GPL");