sun8i-codec-analog.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * This driver supports the analog controls for the internal codec
  3. * found in Allwinner's A31s, A23, A33 and H3 SoCs.
  4. *
  5. * Copyright 2016 Chen-Yu Tsai <wens@csie.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/tlv.h>
  27. /* Codec analog control register offsets and bit fields */
  28. #define SUN8I_ADDA_HP_VOLC 0x00
  29. #define SUN8I_ADDA_HP_VOLC_PA_CLK_GATE 7
  30. #define SUN8I_ADDA_HP_VOLC_HP_VOL 0
  31. #define SUN8I_ADDA_LOMIXSC 0x01
  32. #define SUN8I_ADDA_LOMIXSC_MIC1 6
  33. #define SUN8I_ADDA_LOMIXSC_MIC2 5
  34. #define SUN8I_ADDA_LOMIXSC_PHONE 4
  35. #define SUN8I_ADDA_LOMIXSC_PHONEN 3
  36. #define SUN8I_ADDA_LOMIXSC_LINEINL 2
  37. #define SUN8I_ADDA_LOMIXSC_DACL 1
  38. #define SUN8I_ADDA_LOMIXSC_DACR 0
  39. #define SUN8I_ADDA_ROMIXSC 0x02
  40. #define SUN8I_ADDA_ROMIXSC_MIC1 6
  41. #define SUN8I_ADDA_ROMIXSC_MIC2 5
  42. #define SUN8I_ADDA_ROMIXSC_PHONE 4
  43. #define SUN8I_ADDA_ROMIXSC_PHONEP 3
  44. #define SUN8I_ADDA_ROMIXSC_LINEINR 2
  45. #define SUN8I_ADDA_ROMIXSC_DACR 1
  46. #define SUN8I_ADDA_ROMIXSC_DACL 0
  47. #define SUN8I_ADDA_DAC_PA_SRC 0x03
  48. #define SUN8I_ADDA_DAC_PA_SRC_DACAREN 7
  49. #define SUN8I_ADDA_DAC_PA_SRC_DACALEN 6
  50. #define SUN8I_ADDA_DAC_PA_SRC_RMIXEN 5
  51. #define SUN8I_ADDA_DAC_PA_SRC_LMIXEN 4
  52. #define SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE 3
  53. #define SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE 2
  54. #define SUN8I_ADDA_DAC_PA_SRC_RHPIS 1
  55. #define SUN8I_ADDA_DAC_PA_SRC_LHPIS 0
  56. #define SUN8I_ADDA_PHONEIN_GCTRL 0x04
  57. #define SUN8I_ADDA_PHONEIN_GCTRL_PHONEPG 4
  58. #define SUN8I_ADDA_PHONEIN_GCTRL_PHONENG 0
  59. #define SUN8I_ADDA_LINEIN_GCTRL 0x05
  60. #define SUN8I_ADDA_LINEIN_GCTRL_LINEING 4
  61. #define SUN8I_ADDA_LINEIN_GCTRL_PHONEG 0
  62. #define SUN8I_ADDA_MICIN_GCTRL 0x06
  63. #define SUN8I_ADDA_MICIN_GCTRL_MIC1G 4
  64. #define SUN8I_ADDA_MICIN_GCTRL_MIC2G 0
  65. #define SUN8I_ADDA_PAEN_HP_CTRL 0x07
  66. #define SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN 7
  67. #define SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN 7 /* H3 specific */
  68. #define SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC 5
  69. #define SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN 4
  70. #define SUN8I_ADDA_PAEN_HP_CTRL_PA_ANTI_POP_CTRL 2
  71. #define SUN8I_ADDA_PAEN_HP_CTRL_LTRNMUTE 1
  72. #define SUN8I_ADDA_PAEN_HP_CTRL_RTLNMUTE 0
  73. #define SUN8I_ADDA_PHONEOUT_CTRL 0x08
  74. #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTG 5
  75. #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTEN 4
  76. #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC1 3
  77. #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC2 2
  78. #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_RMIX 1
  79. #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_LMIX 0
  80. #define SUN8I_ADDA_PHONE_GAIN_CTRL 0x09
  81. #define SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL 3
  82. #define SUN8I_ADDA_PHONE_GAIN_CTRL_PHONEPREG 0
  83. #define SUN8I_ADDA_MIC2G_CTRL 0x0a
  84. #define SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN 7
  85. #define SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST 4
  86. #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN 3
  87. #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN 2
  88. #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC 1
  89. #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC 0
  90. #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL 0x0b
  91. #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN 7
  92. #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN 6
  93. #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIAS_MODE 5
  94. #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN 3
  95. #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST 0
  96. #define SUN8I_ADDA_LADCMIXSC 0x0c
  97. #define SUN8I_ADDA_LADCMIXSC_MIC1 6
  98. #define SUN8I_ADDA_LADCMIXSC_MIC2 5
  99. #define SUN8I_ADDA_LADCMIXSC_PHONE 4
  100. #define SUN8I_ADDA_LADCMIXSC_PHONEN 3
  101. #define SUN8I_ADDA_LADCMIXSC_LINEINL 2
  102. #define SUN8I_ADDA_LADCMIXSC_OMIXRL 1
  103. #define SUN8I_ADDA_LADCMIXSC_OMIXRR 0
  104. #define SUN8I_ADDA_RADCMIXSC 0x0d
  105. #define SUN8I_ADDA_RADCMIXSC_MIC1 6
  106. #define SUN8I_ADDA_RADCMIXSC_MIC2 5
  107. #define SUN8I_ADDA_RADCMIXSC_PHONE 4
  108. #define SUN8I_ADDA_RADCMIXSC_PHONEP 3
  109. #define SUN8I_ADDA_RADCMIXSC_LINEINR 2
  110. #define SUN8I_ADDA_RADCMIXSC_OMIXR 1
  111. #define SUN8I_ADDA_RADCMIXSC_OMIXL 0
  112. #define SUN8I_ADDA_RES 0x0e
  113. #define SUN8I_ADDA_RES_MMICBIAS_SEL 4
  114. #define SUN8I_ADDA_RES_PA_ANTI_POP_CTRL 0
  115. #define SUN8I_ADDA_ADC_AP_EN 0x0f
  116. #define SUN8I_ADDA_ADC_AP_EN_ADCREN 7
  117. #define SUN8I_ADDA_ADC_AP_EN_ADCLEN 6
  118. #define SUN8I_ADDA_ADC_AP_EN_ADCG 0
  119. /* Analog control register access bits */
  120. #define ADDA_PR 0x0 /* PRCM base + 0x1c0 */
  121. #define ADDA_PR_RESET BIT(28)
  122. #define ADDA_PR_WRITE BIT(24)
  123. #define ADDA_PR_ADDR_SHIFT 16
  124. #define ADDA_PR_ADDR_MASK GENMASK(4, 0)
  125. #define ADDA_PR_DATA_IN_SHIFT 8
  126. #define ADDA_PR_DATA_IN_MASK GENMASK(7, 0)
  127. #define ADDA_PR_DATA_OUT_SHIFT 0
  128. #define ADDA_PR_DATA_OUT_MASK GENMASK(7, 0)
  129. /* regmap access bits */
  130. static int adda_reg_read(void *context, unsigned int reg, unsigned int *val)
  131. {
  132. void __iomem *base = (void __iomem *)context;
  133. u32 tmp;
  134. /* De-assert reset */
  135. writel(readl(base) | ADDA_PR_RESET, base);
  136. /* Clear write bit */
  137. writel(readl(base) & ~ADDA_PR_WRITE, base);
  138. /* Set register address */
  139. tmp = readl(base);
  140. tmp &= ~(ADDA_PR_ADDR_MASK << ADDA_PR_ADDR_SHIFT);
  141. tmp |= (reg & ADDA_PR_ADDR_MASK) << ADDA_PR_ADDR_SHIFT;
  142. writel(tmp, base);
  143. /* Read back value */
  144. *val = readl(base) & ADDA_PR_DATA_OUT_MASK;
  145. return 0;
  146. }
  147. static int adda_reg_write(void *context, unsigned int reg, unsigned int val)
  148. {
  149. void __iomem *base = (void __iomem *)context;
  150. u32 tmp;
  151. /* De-assert reset */
  152. writel(readl(base) | ADDA_PR_RESET, base);
  153. /* Set register address */
  154. tmp = readl(base);
  155. tmp &= ~(ADDA_PR_ADDR_MASK << ADDA_PR_ADDR_SHIFT);
  156. tmp |= (reg & ADDA_PR_ADDR_MASK) << ADDA_PR_ADDR_SHIFT;
  157. writel(tmp, base);
  158. /* Set data to write */
  159. tmp = readl(base);
  160. tmp &= ~(ADDA_PR_DATA_IN_MASK << ADDA_PR_DATA_IN_SHIFT);
  161. tmp |= (val & ADDA_PR_DATA_IN_MASK) << ADDA_PR_DATA_IN_SHIFT;
  162. writel(tmp, base);
  163. /* Set write bit to signal a write */
  164. writel(readl(base) | ADDA_PR_WRITE, base);
  165. /* Clear write bit */
  166. writel(readl(base) & ~ADDA_PR_WRITE, base);
  167. return 0;
  168. }
  169. static const struct regmap_config adda_pr_regmap_cfg = {
  170. .name = "adda-pr",
  171. .reg_bits = 5,
  172. .reg_stride = 1,
  173. .val_bits = 8,
  174. .reg_read = adda_reg_read,
  175. .reg_write = adda_reg_write,
  176. .fast_io = true,
  177. .max_register = 24,
  178. };
  179. /* mixer controls */
  180. static const struct snd_kcontrol_new sun8i_codec_mixer_controls[] = {
  181. SOC_DAPM_DOUBLE_R("DAC Playback Switch",
  182. SUN8I_ADDA_LOMIXSC,
  183. SUN8I_ADDA_ROMIXSC,
  184. SUN8I_ADDA_LOMIXSC_DACL, 1, 0),
  185. SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
  186. SUN8I_ADDA_LOMIXSC,
  187. SUN8I_ADDA_ROMIXSC,
  188. SUN8I_ADDA_LOMIXSC_DACR, 1, 0),
  189. SOC_DAPM_DOUBLE_R("Line In Playback Switch",
  190. SUN8I_ADDA_LOMIXSC,
  191. SUN8I_ADDA_ROMIXSC,
  192. SUN8I_ADDA_LOMIXSC_LINEINL, 1, 0),
  193. SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
  194. SUN8I_ADDA_LOMIXSC,
  195. SUN8I_ADDA_ROMIXSC,
  196. SUN8I_ADDA_LOMIXSC_MIC1, 1, 0),
  197. SOC_DAPM_DOUBLE_R("Mic2 Playback Switch",
  198. SUN8I_ADDA_LOMIXSC,
  199. SUN8I_ADDA_ROMIXSC,
  200. SUN8I_ADDA_LOMIXSC_MIC2, 1, 0),
  201. };
  202. /* ADC mixer controls */
  203. static const struct snd_kcontrol_new sun8i_codec_adc_mixer_controls[] = {
  204. SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
  205. SUN8I_ADDA_LADCMIXSC,
  206. SUN8I_ADDA_RADCMIXSC,
  207. SUN8I_ADDA_LADCMIXSC_OMIXRL, 1, 0),
  208. SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
  209. SUN8I_ADDA_LADCMIXSC,
  210. SUN8I_ADDA_RADCMIXSC,
  211. SUN8I_ADDA_LADCMIXSC_OMIXRR, 1, 0),
  212. SOC_DAPM_DOUBLE_R("Line In Capture Switch",
  213. SUN8I_ADDA_LADCMIXSC,
  214. SUN8I_ADDA_RADCMIXSC,
  215. SUN8I_ADDA_LADCMIXSC_LINEINL, 1, 0),
  216. SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
  217. SUN8I_ADDA_LADCMIXSC,
  218. SUN8I_ADDA_RADCMIXSC,
  219. SUN8I_ADDA_LADCMIXSC_MIC1, 1, 0),
  220. SOC_DAPM_DOUBLE_R("Mic2 Capture Switch",
  221. SUN8I_ADDA_LADCMIXSC,
  222. SUN8I_ADDA_RADCMIXSC,
  223. SUN8I_ADDA_LADCMIXSC_MIC2, 1, 0),
  224. };
  225. /* volume / mute controls */
  226. static const DECLARE_TLV_DB_SCALE(sun8i_codec_out_mixer_pregain_scale,
  227. -450, 150, 0);
  228. static const DECLARE_TLV_DB_RANGE(sun8i_codec_mic_gain_scale,
  229. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  230. 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
  231. );
  232. static const struct snd_kcontrol_new sun8i_codec_common_controls[] = {
  233. /* Mixer pre-gains */
  234. SOC_SINGLE_TLV("Line In Playback Volume", SUN8I_ADDA_LINEIN_GCTRL,
  235. SUN8I_ADDA_LINEIN_GCTRL_LINEING,
  236. 0x7, 0, sun8i_codec_out_mixer_pregain_scale),
  237. SOC_SINGLE_TLV("Mic1 Playback Volume", SUN8I_ADDA_MICIN_GCTRL,
  238. SUN8I_ADDA_MICIN_GCTRL_MIC1G,
  239. 0x7, 0, sun8i_codec_out_mixer_pregain_scale),
  240. SOC_SINGLE_TLV("Mic2 Playback Volume",
  241. SUN8I_ADDA_MICIN_GCTRL, SUN8I_ADDA_MICIN_GCTRL_MIC2G,
  242. 0x7, 0, sun8i_codec_out_mixer_pregain_scale),
  243. /* Microphone Amp boost gains */
  244. SOC_SINGLE_TLV("Mic1 Boost Volume", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
  245. SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST, 0x7, 0,
  246. sun8i_codec_mic_gain_scale),
  247. SOC_SINGLE_TLV("Mic2 Boost Volume", SUN8I_ADDA_MIC2G_CTRL,
  248. SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST, 0x7, 0,
  249. sun8i_codec_mic_gain_scale),
  250. /* ADC */
  251. SOC_SINGLE_TLV("ADC Gain Capture Volume", SUN8I_ADDA_ADC_AP_EN,
  252. SUN8I_ADDA_ADC_AP_EN_ADCG, 0x7, 0,
  253. sun8i_codec_out_mixer_pregain_scale),
  254. };
  255. static const struct snd_soc_dapm_widget sun8i_codec_common_widgets[] = {
  256. /* ADC */
  257. SND_SOC_DAPM_ADC("Left ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
  258. SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0),
  259. SND_SOC_DAPM_ADC("Right ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
  260. SUN8I_ADDA_ADC_AP_EN_ADCREN, 0),
  261. /* DAC */
  262. SND_SOC_DAPM_DAC("Left DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
  263. SUN8I_ADDA_DAC_PA_SRC_DACALEN, 0),
  264. SND_SOC_DAPM_DAC("Right DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
  265. SUN8I_ADDA_DAC_PA_SRC_DACAREN, 0),
  266. /*
  267. * Due to this component and the codec belonging to separate DAPM
  268. * contexts, we need to manually link the above widgets to their
  269. * stream widgets at the card level.
  270. */
  271. /* Line In */
  272. SND_SOC_DAPM_INPUT("LINEIN"),
  273. /* Microphone inputs */
  274. SND_SOC_DAPM_INPUT("MIC1"),
  275. SND_SOC_DAPM_INPUT("MIC2"),
  276. /* Microphone Bias */
  277. SND_SOC_DAPM_SUPPLY("MBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
  278. SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN,
  279. 0, NULL, 0),
  280. /* Mic input path */
  281. SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
  282. SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN, 0, NULL, 0),
  283. SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN8I_ADDA_MIC2G_CTRL,
  284. SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN, 0, NULL, 0),
  285. /* Mixers */
  286. SND_SOC_DAPM_MIXER("Left Mixer", SUN8I_ADDA_DAC_PA_SRC,
  287. SUN8I_ADDA_DAC_PA_SRC_LMIXEN, 0,
  288. sun8i_codec_mixer_controls,
  289. ARRAY_SIZE(sun8i_codec_mixer_controls)),
  290. SND_SOC_DAPM_MIXER("Right Mixer", SUN8I_ADDA_DAC_PA_SRC,
  291. SUN8I_ADDA_DAC_PA_SRC_RMIXEN, 0,
  292. sun8i_codec_mixer_controls,
  293. ARRAY_SIZE(sun8i_codec_mixer_controls)),
  294. SND_SOC_DAPM_MIXER("Left ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
  295. SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0,
  296. sun8i_codec_adc_mixer_controls,
  297. ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
  298. SND_SOC_DAPM_MIXER("Right ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
  299. SUN8I_ADDA_ADC_AP_EN_ADCREN, 0,
  300. sun8i_codec_adc_mixer_controls,
  301. ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
  302. };
  303. static const struct snd_soc_dapm_route sun8i_codec_common_routes[] = {
  304. /* Microphone Routes */
  305. { "Mic1 Amplifier", NULL, "MIC1"},
  306. { "Mic2 Amplifier", NULL, "MIC2"},
  307. /* Left Mixer Routes */
  308. { "Left Mixer", "DAC Playback Switch", "Left DAC" },
  309. { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
  310. { "Left Mixer", "Line In Playback Switch", "LINEIN" },
  311. { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
  312. { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
  313. /* Right Mixer Routes */
  314. { "Right Mixer", "DAC Playback Switch", "Right DAC" },
  315. { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
  316. { "Right Mixer", "Line In Playback Switch", "LINEIN" },
  317. { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
  318. { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
  319. /* Left ADC Mixer Routes */
  320. { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
  321. { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
  322. { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
  323. { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
  324. { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
  325. /* Right ADC Mixer Routes */
  326. { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
  327. { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
  328. { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
  329. { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
  330. { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
  331. /* ADC Routes */
  332. { "Left ADC", NULL, "Left ADC Mixer" },
  333. { "Right ADC", NULL, "Right ADC Mixer" },
  334. };
  335. /* headphone specific controls, widgets, and routes */
  336. static const DECLARE_TLV_DB_SCALE(sun8i_codec_hp_vol_scale, -6300, 100, 1);
  337. static const struct snd_kcontrol_new sun8i_codec_headphone_controls[] = {
  338. SOC_SINGLE_TLV("Headphone Playback Volume",
  339. SUN8I_ADDA_HP_VOLC,
  340. SUN8I_ADDA_HP_VOLC_HP_VOL, 0x3f, 0,
  341. sun8i_codec_hp_vol_scale),
  342. SOC_DOUBLE("Headphone Playback Switch",
  343. SUN8I_ADDA_DAC_PA_SRC,
  344. SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE,
  345. SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE, 1, 0),
  346. };
  347. static const char * const sun8i_codec_hp_src_enum_text[] = {
  348. "DAC", "Mixer",
  349. };
  350. static SOC_ENUM_DOUBLE_DECL(sun8i_codec_hp_src_enum,
  351. SUN8I_ADDA_DAC_PA_SRC,
  352. SUN8I_ADDA_DAC_PA_SRC_LHPIS,
  353. SUN8I_ADDA_DAC_PA_SRC_RHPIS,
  354. sun8i_codec_hp_src_enum_text);
  355. static const struct snd_kcontrol_new sun8i_codec_hp_src[] = {
  356. SOC_DAPM_ENUM("Headphone Source Playback Route",
  357. sun8i_codec_hp_src_enum),
  358. };
  359. static const struct snd_soc_dapm_widget sun8i_codec_headphone_widgets[] = {
  360. SND_SOC_DAPM_MUX("Headphone Source Playback Route",
  361. SND_SOC_NOPM, 0, 0, sun8i_codec_hp_src),
  362. SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN8I_ADDA_PAEN_HP_CTRL,
  363. SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN, 0, NULL, 0),
  364. SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN8I_ADDA_PAEN_HP_CTRL,
  365. SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN, 0, NULL, 0),
  366. SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN8I_ADDA_PAEN_HP_CTRL,
  367. SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC, 0x3, 0x3, 0),
  368. SND_SOC_DAPM_OUTPUT("HP"),
  369. };
  370. static const struct snd_soc_dapm_route sun8i_codec_headphone_routes[] = {
  371. { "Headphone Source Playback Route", "DAC", "Left DAC" },
  372. { "Headphone Source Playback Route", "DAC", "Right DAC" },
  373. { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
  374. { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
  375. { "Headphone Amp", NULL, "Headphone Source Playback Route" },
  376. { "HPCOM", NULL, "HPCOM Protection" },
  377. { "HP", NULL, "Headphone Amp" },
  378. };
  379. static int sun8i_codec_add_headphone(struct snd_soc_component *cmpnt)
  380. {
  381. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
  382. struct device *dev = cmpnt->dev;
  383. int ret;
  384. ret = snd_soc_add_component_controls(cmpnt,
  385. sun8i_codec_headphone_controls,
  386. ARRAY_SIZE(sun8i_codec_headphone_controls));
  387. if (ret) {
  388. dev_err(dev, "Failed to add Headphone controls: %d\n", ret);
  389. return ret;
  390. }
  391. ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_headphone_widgets,
  392. ARRAY_SIZE(sun8i_codec_headphone_widgets));
  393. if (ret) {
  394. dev_err(dev, "Failed to add Headphone DAPM widgets: %d\n", ret);
  395. return ret;
  396. }
  397. ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_headphone_routes,
  398. ARRAY_SIZE(sun8i_codec_headphone_routes));
  399. if (ret) {
  400. dev_err(dev, "Failed to add Headphone DAPM routes: %d\n", ret);
  401. return ret;
  402. }
  403. return 0;
  404. }
  405. /* hmic specific widget */
  406. static const struct snd_soc_dapm_widget sun8i_codec_hmic_widgets[] = {
  407. SND_SOC_DAPM_SUPPLY("HBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
  408. SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN,
  409. 0, NULL, 0),
  410. };
  411. static int sun8i_codec_add_hmic(struct snd_soc_component *cmpnt)
  412. {
  413. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
  414. struct device *dev = cmpnt->dev;
  415. int ret;
  416. ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_hmic_widgets,
  417. ARRAY_SIZE(sun8i_codec_hmic_widgets));
  418. if (ret)
  419. dev_err(dev, "Failed to add Mic3 DAPM widgets: %d\n", ret);
  420. return ret;
  421. }
  422. /* line out specific controls, widgets and routes */
  423. static const DECLARE_TLV_DB_RANGE(sun8i_codec_lineout_vol_scale,
  424. 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  425. 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
  426. );
  427. static const struct snd_kcontrol_new sun8i_codec_lineout_controls[] = {
  428. SOC_SINGLE_TLV("Line Out Playback Volume",
  429. SUN8I_ADDA_PHONE_GAIN_CTRL,
  430. SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL, 0x1f, 0,
  431. sun8i_codec_lineout_vol_scale),
  432. SOC_DOUBLE("Line Out Playback Switch",
  433. SUN8I_ADDA_MIC2G_CTRL,
  434. SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN,
  435. SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN, 1, 0),
  436. };
  437. static const char * const sun8i_codec_lineout_src_enum_text[] = {
  438. "Stereo", "Mono Differential",
  439. };
  440. static SOC_ENUM_DOUBLE_DECL(sun8i_codec_lineout_src_enum,
  441. SUN8I_ADDA_MIC2G_CTRL,
  442. SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC,
  443. SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC,
  444. sun8i_codec_lineout_src_enum_text);
  445. static const struct snd_kcontrol_new sun8i_codec_lineout_src[] = {
  446. SOC_DAPM_ENUM("Line Out Source Playback Route",
  447. sun8i_codec_lineout_src_enum),
  448. };
  449. static const struct snd_soc_dapm_widget sun8i_codec_lineout_widgets[] = {
  450. SND_SOC_DAPM_MUX("Line Out Source Playback Route",
  451. SND_SOC_NOPM, 0, 0, sun8i_codec_lineout_src),
  452. /* It is unclear if this is a buffer or gate, model it as a supply */
  453. SND_SOC_DAPM_SUPPLY("Line Out Enable", SUN8I_ADDA_PAEN_HP_CTRL,
  454. SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN, 0, NULL, 0),
  455. SND_SOC_DAPM_OUTPUT("LINEOUT"),
  456. };
  457. static const struct snd_soc_dapm_route sun8i_codec_lineout_routes[] = {
  458. { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
  459. { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
  460. { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
  461. { "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
  462. { "LINEOUT", NULL, "Line Out Source Playback Route" },
  463. { "LINEOUT", NULL, "Line Out Enable", },
  464. };
  465. static int sun8i_codec_add_lineout(struct snd_soc_component *cmpnt)
  466. {
  467. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
  468. struct device *dev = cmpnt->dev;
  469. int ret;
  470. ret = snd_soc_add_component_controls(cmpnt,
  471. sun8i_codec_lineout_controls,
  472. ARRAY_SIZE(sun8i_codec_lineout_controls));
  473. if (ret) {
  474. dev_err(dev, "Failed to add Line Out controls: %d\n", ret);
  475. return ret;
  476. }
  477. ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_lineout_widgets,
  478. ARRAY_SIZE(sun8i_codec_lineout_widgets));
  479. if (ret) {
  480. dev_err(dev, "Failed to add Line Out DAPM widgets: %d\n", ret);
  481. return ret;
  482. }
  483. ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_lineout_routes,
  484. ARRAY_SIZE(sun8i_codec_lineout_routes));
  485. if (ret) {
  486. dev_err(dev, "Failed to add Line Out DAPM routes: %d\n", ret);
  487. return ret;
  488. }
  489. return 0;
  490. }
  491. struct sun8i_codec_analog_quirks {
  492. bool has_headphone;
  493. bool has_hmic;
  494. bool has_lineout;
  495. };
  496. static const struct sun8i_codec_analog_quirks sun8i_a23_quirks = {
  497. .has_headphone = true,
  498. .has_hmic = true,
  499. };
  500. static const struct sun8i_codec_analog_quirks sun8i_h3_quirks = {
  501. .has_lineout = true,
  502. };
  503. static int sun8i_codec_analog_cmpnt_probe(struct snd_soc_component *cmpnt)
  504. {
  505. struct device *dev = cmpnt->dev;
  506. const struct sun8i_codec_analog_quirks *quirks;
  507. int ret;
  508. /*
  509. * This would never return NULL unless someone directly registers a
  510. * platform device matching this driver's name, without specifying a
  511. * device tree node.
  512. */
  513. quirks = of_device_get_match_data(dev);
  514. /* Add controls, widgets, and routes for individual features */
  515. if (quirks->has_headphone) {
  516. ret = sun8i_codec_add_headphone(cmpnt);
  517. if (ret)
  518. return ret;
  519. }
  520. if (quirks->has_hmic) {
  521. ret = sun8i_codec_add_hmic(cmpnt);
  522. if (ret)
  523. return ret;
  524. }
  525. if (quirks->has_lineout) {
  526. ret = sun8i_codec_add_lineout(cmpnt);
  527. if (ret)
  528. return ret;
  529. }
  530. return 0;
  531. }
  532. static const struct snd_soc_component_driver sun8i_codec_analog_cmpnt_drv = {
  533. .controls = sun8i_codec_common_controls,
  534. .num_controls = ARRAY_SIZE(sun8i_codec_common_controls),
  535. .dapm_widgets = sun8i_codec_common_widgets,
  536. .num_dapm_widgets = ARRAY_SIZE(sun8i_codec_common_widgets),
  537. .dapm_routes = sun8i_codec_common_routes,
  538. .num_dapm_routes = ARRAY_SIZE(sun8i_codec_common_routes),
  539. .probe = sun8i_codec_analog_cmpnt_probe,
  540. };
  541. static const struct of_device_id sun8i_codec_analog_of_match[] = {
  542. {
  543. .compatible = "allwinner,sun8i-a23-codec-analog",
  544. .data = &sun8i_a23_quirks,
  545. },
  546. {
  547. .compatible = "allwinner,sun8i-h3-codec-analog",
  548. .data = &sun8i_h3_quirks,
  549. },
  550. {}
  551. };
  552. MODULE_DEVICE_TABLE(of, sun8i_codec_analog_of_match);
  553. static int sun8i_codec_analog_probe(struct platform_device *pdev)
  554. {
  555. struct resource *res;
  556. struct regmap *regmap;
  557. void __iomem *base;
  558. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  559. base = devm_ioremap_resource(&pdev->dev, res);
  560. if (IS_ERR(base)) {
  561. dev_err(&pdev->dev, "Failed to map the registers\n");
  562. return PTR_ERR(base);
  563. }
  564. regmap = devm_regmap_init(&pdev->dev, NULL, base, &adda_pr_regmap_cfg);
  565. if (IS_ERR(regmap)) {
  566. dev_err(&pdev->dev, "Failed to create regmap\n");
  567. return PTR_ERR(regmap);
  568. }
  569. return devm_snd_soc_register_component(&pdev->dev,
  570. &sun8i_codec_analog_cmpnt_drv,
  571. NULL, 0);
  572. }
  573. static struct platform_driver sun8i_codec_analog_driver = {
  574. .driver = {
  575. .name = "sun8i-codec-analog",
  576. .of_match_table = sun8i_codec_analog_of_match,
  577. },
  578. .probe = sun8i_codec_analog_probe,
  579. };
  580. module_platform_driver(sun8i_codec_analog_driver);
  581. MODULE_DESCRIPTION("Allwinner internal codec analog controls driver");
  582. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  583. MODULE_LICENSE("GPL");
  584. MODULE_ALIAS("platform:sun8i-codec-analog");