sun4i-codec.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/soc.h>
  34. #include <sound/tlv.h>
  35. #include <sound/initval.h>
  36. #include <sound/dmaengine_pcm.h>
  37. /* Codec DAC register offsets and bit fields */
  38. #define SUN4I_CODEC_DAC_DPC (0x00)
  39. #define SUN4I_CODEC_DAC_DPC_EN_DA (31)
  40. #define SUN4I_CODEC_DAC_DPC_DVOL (12)
  41. #define SUN4I_CODEC_DAC_FIFOC (0x04)
  42. #define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
  43. #define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
  44. #define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
  45. #define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
  46. #define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
  47. #define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
  48. #define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
  49. #define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
  50. #define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
  51. #define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
  52. #define SUN4I_CODEC_DAC_FIFOS (0x08)
  53. #define SUN4I_CODEC_DAC_TXDATA (0x0c)
  54. #define SUN4I_CODEC_DAC_ACTL (0x10)
  55. #define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
  56. #define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
  57. #define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
  58. #define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
  59. #define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
  60. #define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
  61. #define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
  62. #define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
  63. #define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
  64. #define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
  65. #define SUN4I_CODEC_DAC_TUNE (0x14)
  66. #define SUN4I_CODEC_DAC_DEBUG (0x18)
  67. /* Codec ADC register offsets and bit fields */
  68. #define SUN4I_CODEC_ADC_FIFOC (0x1c)
  69. #define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
  70. #define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
  71. #define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
  72. #define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
  73. #define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
  74. #define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
  75. #define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
  76. #define SUN4I_CODEC_ADC_FIFOS (0x20)
  77. #define SUN4I_CODEC_ADC_RXDATA (0x24)
  78. #define SUN4I_CODEC_ADC_ACTL (0x28)
  79. #define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
  80. #define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
  81. #define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
  82. #define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
  83. #define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
  84. #define SUN4I_CODEC_ADC_ACTL_VADCG (20)
  85. #define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
  86. #define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
  87. #define SUN4I_CODEC_ADC_ACTL_DDE (3)
  88. #define SUN4I_CODEC_ADC_DEBUG (0x2c)
  89. /* Other various ADC registers */
  90. #define SUN4I_CODEC_DAC_TXCNT (0x30)
  91. #define SUN4I_CODEC_ADC_RXCNT (0x34)
  92. #define SUN4I_CODEC_AC_SYS_VERI (0x38)
  93. #define SUN4I_CODEC_AC_MIC_PHONE_CAL (0x3c)
  94. struct sun4i_codec {
  95. struct device *dev;
  96. struct regmap *regmap;
  97. struct clk *clk_apb;
  98. struct clk *clk_module;
  99. struct snd_dmaengine_dai_dma_data playback_dma_data;
  100. };
  101. static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
  102. {
  103. /*
  104. * FIXME: according to the BSP, we might need to drive a PA
  105. * GPIO high here on some boards
  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. /*
  119. * FIXME: according to the BSP, we might need to drive a PA
  120. * GPIO low here on some boards
  121. */
  122. /* Disable DAC DRQ */
  123. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  124. BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
  125. 0);
  126. }
  127. static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
  128. struct snd_soc_dai *dai)
  129. {
  130. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  131. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  132. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  133. return -ENOTSUPP;
  134. switch (cmd) {
  135. case SNDRV_PCM_TRIGGER_START:
  136. case SNDRV_PCM_TRIGGER_RESUME:
  137. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  138. sun4i_codec_start_playback(scodec);
  139. break;
  140. case SNDRV_PCM_TRIGGER_STOP:
  141. case SNDRV_PCM_TRIGGER_SUSPEND:
  142. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  143. sun4i_codec_stop_playback(scodec);
  144. break;
  145. default:
  146. return -EINVAL;
  147. }
  148. return 0;
  149. }
  150. static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
  151. struct snd_soc_dai *dai)
  152. {
  153. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  154. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  155. u32 val;
  156. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  157. return -ENOTSUPP;
  158. /* Flush the TX FIFO */
  159. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  160. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
  161. BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
  162. /* Set TX FIFO Empty Trigger Level */
  163. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  164. 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
  165. 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
  166. if (substream->runtime->rate > 32000)
  167. /* Use 64 bits FIR filter */
  168. val = 0;
  169. else
  170. /* Use 32 bits FIR filter */
  171. val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
  172. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  173. BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
  174. val);
  175. /* Send zeros when we have an underrun */
  176. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  177. BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
  178. 0);
  179. return 0;
  180. }
  181. static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
  182. {
  183. unsigned int rate = params_rate(params);
  184. switch (rate) {
  185. case 176400:
  186. case 88200:
  187. case 44100:
  188. case 33075:
  189. case 22050:
  190. case 14700:
  191. case 11025:
  192. case 7350:
  193. return 22579200;
  194. case 192000:
  195. case 96000:
  196. case 48000:
  197. case 32000:
  198. case 24000:
  199. case 16000:
  200. case 12000:
  201. case 8000:
  202. return 24576000;
  203. default:
  204. return 0;
  205. }
  206. }
  207. static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
  208. {
  209. unsigned int rate = params_rate(params);
  210. switch (rate) {
  211. case 192000:
  212. case 176400:
  213. return 6;
  214. case 96000:
  215. case 88200:
  216. return 7;
  217. case 48000:
  218. case 44100:
  219. return 0;
  220. case 32000:
  221. case 33075:
  222. return 1;
  223. case 24000:
  224. case 22050:
  225. return 2;
  226. case 16000:
  227. case 14700:
  228. return 3;
  229. case 12000:
  230. case 11025:
  231. return 4;
  232. case 8000:
  233. case 7350:
  234. return 5;
  235. default:
  236. return -EINVAL;
  237. }
  238. }
  239. static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
  240. struct snd_pcm_hw_params *params,
  241. struct snd_soc_dai *dai)
  242. {
  243. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  244. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  245. unsigned long clk_freq;
  246. int ret, hwrate;
  247. u32 val;
  248. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  249. return -ENOTSUPP;
  250. clk_freq = sun4i_codec_get_mod_freq(params);
  251. if (!clk_freq)
  252. return -EINVAL;
  253. ret = clk_set_rate(scodec->clk_module, clk_freq);
  254. if (ret)
  255. return ret;
  256. hwrate = sun4i_codec_get_hw_rate(params);
  257. if (hwrate < 0)
  258. return hwrate;
  259. /* Set DAC sample rate */
  260. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  261. 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
  262. hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
  263. /* Set the number of channels we want to use */
  264. if (params_channels(params) == 1)
  265. val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
  266. else
  267. val = 0;
  268. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  269. BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
  270. val);
  271. /* Set the number of sample bits to either 16 or 24 bits */
  272. if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
  273. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  274. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
  275. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
  276. /* Set TX FIFO mode to padding the LSBs with 0 */
  277. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  278. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
  279. 0);
  280. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  281. } else {
  282. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  283. BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
  284. 0);
  285. /* Set TX FIFO mode to repeat the MSB */
  286. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  287. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
  288. BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
  289. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  290. }
  291. return 0;
  292. }
  293. static int sun4i_codec_startup(struct snd_pcm_substream *substream,
  294. struct snd_soc_dai *dai)
  295. {
  296. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  297. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  298. /*
  299. * Stop issuing DRQ when we have room for less than 16 samples
  300. * in our TX FIFO
  301. */
  302. regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
  303. 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
  304. 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
  305. return clk_prepare_enable(scodec->clk_module);
  306. }
  307. static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
  308. struct snd_soc_dai *dai)
  309. {
  310. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  311. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
  312. clk_disable_unprepare(scodec->clk_module);
  313. }
  314. static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
  315. .startup = sun4i_codec_startup,
  316. .shutdown = sun4i_codec_shutdown,
  317. .trigger = sun4i_codec_trigger,
  318. .hw_params = sun4i_codec_hw_params,
  319. .prepare = sun4i_codec_prepare,
  320. };
  321. static struct snd_soc_dai_driver sun4i_codec_dai = {
  322. .name = "Codec",
  323. .ops = &sun4i_codec_dai_ops,
  324. .playback = {
  325. .stream_name = "Codec Playback",
  326. .channels_min = 1,
  327. .channels_max = 2,
  328. .rate_min = 8000,
  329. .rate_max = 192000,
  330. .rates = SNDRV_PCM_RATE_8000_48000 |
  331. SNDRV_PCM_RATE_96000 |
  332. SNDRV_PCM_RATE_192000,
  333. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  334. SNDRV_PCM_FMTBIT_S32_LE,
  335. .sig_bits = 24,
  336. },
  337. };
  338. /*** Codec ***/
  339. static const struct snd_kcontrol_new sun4i_codec_pa_mute =
  340. SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
  341. SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
  342. static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
  343. static const struct snd_kcontrol_new sun4i_codec_widgets[] = {
  344. SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
  345. SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
  346. sun4i_codec_pa_volume_scale),
  347. };
  348. static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls[] = {
  349. SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  350. SUN4I_CODEC_DAC_ACTL_LDACLMIXS, 1, 0),
  351. };
  352. static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls[] = {
  353. SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  354. SUN4I_CODEC_DAC_ACTL_RDACRMIXS, 1, 0),
  355. SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  356. SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
  357. };
  358. static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
  359. SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
  360. SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
  361. SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
  362. SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
  363. };
  364. static const struct snd_soc_dapm_widget sun4i_codec_dapm_widgets[] = {
  365. /* Digital parts of the DACs */
  366. SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
  367. SUN4I_CODEC_DAC_DPC_EN_DA, 0,
  368. NULL, 0),
  369. /* Analog parts of the DACs */
  370. SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
  371. SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
  372. SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
  373. SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
  374. /* Mixers */
  375. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  376. sun4i_codec_left_mixer_controls,
  377. ARRAY_SIZE(sun4i_codec_left_mixer_controls)),
  378. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  379. sun4i_codec_right_mixer_controls,
  380. ARRAY_SIZE(sun4i_codec_right_mixer_controls)),
  381. /* Global Mixer Enable */
  382. SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
  383. SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
  384. /* Power Amplifier */
  385. SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
  386. SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
  387. sun4i_codec_pa_mixer_controls,
  388. ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
  389. SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
  390. &sun4i_codec_pa_mute),
  391. SND_SOC_DAPM_OUTPUT("HP Right"),
  392. SND_SOC_DAPM_OUTPUT("HP Left"),
  393. };
  394. static const struct snd_soc_dapm_route sun4i_codec_dapm_routes[] = {
  395. /* Left DAC Routes */
  396. { "Left DAC", NULL, "DAC" },
  397. /* Right DAC Routes */
  398. { "Right DAC", NULL, "DAC" },
  399. /* Right Mixer Routes */
  400. { "Right Mixer", NULL, "Mixer Enable" },
  401. { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
  402. { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
  403. /* Left Mixer Routes */
  404. { "Left Mixer", NULL, "Mixer Enable" },
  405. { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
  406. /* Power Amplifier Routes */
  407. { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
  408. { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
  409. { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
  410. { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
  411. /* Headphone Output Routes */
  412. { "Power Amplifier Mute", "Switch", "Power Amplifier" },
  413. { "HP Right", NULL, "Power Amplifier Mute" },
  414. { "HP Left", NULL, "Power Amplifier Mute" },
  415. };
  416. static struct snd_soc_codec_driver sun4i_codec_codec = {
  417. .controls = sun4i_codec_widgets,
  418. .num_controls = ARRAY_SIZE(sun4i_codec_widgets),
  419. .dapm_widgets = sun4i_codec_dapm_widgets,
  420. .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_dapm_widgets),
  421. .dapm_routes = sun4i_codec_dapm_routes,
  422. .num_dapm_routes = ARRAY_SIZE(sun4i_codec_dapm_routes),
  423. };
  424. static const struct snd_soc_component_driver sun4i_codec_component = {
  425. .name = "sun4i-codec",
  426. };
  427. #define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
  428. #define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  429. SNDRV_PCM_FMTBIT_S32_LE)
  430. static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
  431. {
  432. struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
  433. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
  434. snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
  435. NULL);
  436. return 0;
  437. }
  438. static struct snd_soc_dai_driver dummy_cpu_dai = {
  439. .name = "sun4i-codec-cpu-dai",
  440. .probe = sun4i_codec_dai_probe,
  441. .playback = {
  442. .stream_name = "Playback",
  443. .channels_min = 1,
  444. .channels_max = 2,
  445. .rates = SUN4I_CODEC_RATES,
  446. .formats = SUN4I_CODEC_FORMATS,
  447. .sig_bits = 24,
  448. },
  449. };
  450. static const struct regmap_config sun4i_codec_regmap_config = {
  451. .reg_bits = 32,
  452. .reg_stride = 4,
  453. .val_bits = 32,
  454. .max_register = SUN4I_CODEC_AC_MIC_PHONE_CAL,
  455. };
  456. static const struct of_device_id sun4i_codec_of_match[] = {
  457. { .compatible = "allwinner,sun4i-a10-codec" },
  458. { .compatible = "allwinner,sun7i-a20-codec" },
  459. {}
  460. };
  461. MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
  462. static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
  463. int *num_links)
  464. {
  465. struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
  466. GFP_KERNEL);
  467. if (!link)
  468. return NULL;
  469. link->name = "cdc";
  470. link->stream_name = "CDC PCM";
  471. link->codec_dai_name = "Codec";
  472. link->cpu_dai_name = dev_name(dev);
  473. link->codec_name = dev_name(dev);
  474. link->platform_name = dev_name(dev);
  475. link->dai_fmt = SND_SOC_DAIFMT_I2S;
  476. *num_links = 1;
  477. return link;
  478. };
  479. static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
  480. {
  481. struct snd_soc_card *card;
  482. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  483. if (!card)
  484. return ERR_PTR(-ENOMEM);
  485. card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
  486. if (!card->dai_link)
  487. return ERR_PTR(-ENOMEM);
  488. card->dev = dev;
  489. card->name = "sun4i-codec";
  490. return card;
  491. };
  492. static int sun4i_codec_probe(struct platform_device *pdev)
  493. {
  494. struct snd_soc_card *card;
  495. struct sun4i_codec *scodec;
  496. struct resource *res;
  497. void __iomem *base;
  498. int ret;
  499. scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
  500. if (!scodec)
  501. return -ENOMEM;
  502. scodec->dev = &pdev->dev;
  503. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  504. base = devm_ioremap_resource(&pdev->dev, res);
  505. if (IS_ERR(base)) {
  506. dev_err(&pdev->dev, "Failed to map the registers\n");
  507. return PTR_ERR(base);
  508. }
  509. scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  510. &sun4i_codec_regmap_config);
  511. if (IS_ERR(scodec->regmap)) {
  512. dev_err(&pdev->dev, "Failed to create our regmap\n");
  513. return PTR_ERR(scodec->regmap);
  514. }
  515. /* Get the clocks from the DT */
  516. scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
  517. if (IS_ERR(scodec->clk_apb)) {
  518. dev_err(&pdev->dev, "Failed to get the APB clock\n");
  519. return PTR_ERR(scodec->clk_apb);
  520. }
  521. scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
  522. if (IS_ERR(scodec->clk_module)) {
  523. dev_err(&pdev->dev, "Failed to get the module clock\n");
  524. return PTR_ERR(scodec->clk_module);
  525. }
  526. /* Enable the bus clock */
  527. if (clk_prepare_enable(scodec->clk_apb)) {
  528. dev_err(&pdev->dev, "Failed to enable the APB clock\n");
  529. return -EINVAL;
  530. }
  531. /* DMA configuration for TX FIFO */
  532. scodec->playback_dma_data.addr = res->start + SUN4I_CODEC_DAC_TXDATA;
  533. scodec->playback_dma_data.maxburst = 4;
  534. scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  535. ret = snd_soc_register_codec(&pdev->dev, &sun4i_codec_codec,
  536. &sun4i_codec_dai, 1);
  537. if (ret) {
  538. dev_err(&pdev->dev, "Failed to register our codec\n");
  539. goto err_clk_disable;
  540. }
  541. ret = devm_snd_soc_register_component(&pdev->dev,
  542. &sun4i_codec_component,
  543. &dummy_cpu_dai, 1);
  544. if (ret) {
  545. dev_err(&pdev->dev, "Failed to register our DAI\n");
  546. goto err_unregister_codec;
  547. }
  548. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  549. if (ret) {
  550. dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
  551. goto err_unregister_codec;
  552. }
  553. card = sun4i_codec_create_card(&pdev->dev);
  554. if (IS_ERR(card)) {
  555. ret = PTR_ERR(card);
  556. dev_err(&pdev->dev, "Failed to create our card\n");
  557. goto err_unregister_codec;
  558. }
  559. platform_set_drvdata(pdev, card);
  560. snd_soc_card_set_drvdata(card, scodec);
  561. ret = snd_soc_register_card(card);
  562. if (ret) {
  563. dev_err(&pdev->dev, "Failed to register our card\n");
  564. goto err_unregister_codec;
  565. }
  566. return 0;
  567. err_unregister_codec:
  568. snd_soc_unregister_codec(&pdev->dev);
  569. err_clk_disable:
  570. clk_disable_unprepare(scodec->clk_apb);
  571. return ret;
  572. }
  573. static int sun4i_codec_remove(struct platform_device *pdev)
  574. {
  575. struct snd_soc_card *card = platform_get_drvdata(pdev);
  576. struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
  577. snd_soc_unregister_card(card);
  578. snd_soc_unregister_codec(&pdev->dev);
  579. clk_disable_unprepare(scodec->clk_apb);
  580. return 0;
  581. }
  582. static struct platform_driver sun4i_codec_driver = {
  583. .driver = {
  584. .name = "sun4i-codec",
  585. .of_match_table = sun4i_codec_of_match,
  586. },
  587. .probe = sun4i_codec_probe,
  588. .remove = sun4i_codec_remove,
  589. };
  590. module_platform_driver(sun4i_codec_driver);
  591. MODULE_DESCRIPTION("Allwinner A10 codec driver");
  592. MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
  593. MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
  594. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  595. MODULE_LICENSE("GPL");