inno_rk3036.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver of Inno codec for rk3036 by Rockchip Inc.
  4. *
  5. * Author: Rockchip Inc.
  6. * Author: Zheng ShunQian<[email protected]>
  7. */
  8. #include <sound/soc.h>
  9. #include <sound/tlv.h>
  10. #include <sound/soc-dapm.h>
  11. #include <sound/soc-dai.h>
  12. #include <sound/pcm.h>
  13. #include <sound/pcm_params.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/clk.h>
  17. #include <linux/regmap.h>
  18. #include <linux/device.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include "inno_rk3036.h"
  23. struct rk3036_codec_priv {
  24. void __iomem *base;
  25. struct clk *pclk;
  26. struct regmap *regmap;
  27. struct device *dev;
  28. };
  29. static const DECLARE_TLV_DB_MINMAX(rk3036_codec_hp_tlv, -39, 0);
  30. static int rk3036_codec_antipop_info(struct snd_kcontrol *kcontrol,
  31. struct snd_ctl_elem_info *uinfo)
  32. {
  33. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  34. uinfo->count = 2;
  35. uinfo->value.integer.min = 0;
  36. uinfo->value.integer.max = 1;
  37. return 0;
  38. }
  39. static int rk3036_codec_antipop_get(struct snd_kcontrol *kcontrol,
  40. struct snd_ctl_elem_value *ucontrol)
  41. {
  42. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  43. int val, regval;
  44. regval = snd_soc_component_read(component, INNO_R09);
  45. val = ((regval >> INNO_R09_HPL_ANITPOP_SHIFT) &
  46. INNO_R09_HP_ANTIPOP_MSK) == INNO_R09_HP_ANTIPOP_ON;
  47. ucontrol->value.integer.value[0] = val;
  48. val = ((regval >> INNO_R09_HPR_ANITPOP_SHIFT) &
  49. INNO_R09_HP_ANTIPOP_MSK) == INNO_R09_HP_ANTIPOP_ON;
  50. ucontrol->value.integer.value[1] = val;
  51. return 0;
  52. }
  53. static int rk3036_codec_antipop_put(struct snd_kcontrol *kcontrol,
  54. struct snd_ctl_elem_value *ucontrol)
  55. {
  56. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  57. int val, ret, regmsk;
  58. val = (ucontrol->value.integer.value[0] ?
  59. INNO_R09_HP_ANTIPOP_ON : INNO_R09_HP_ANTIPOP_OFF) <<
  60. INNO_R09_HPL_ANITPOP_SHIFT;
  61. val |= (ucontrol->value.integer.value[1] ?
  62. INNO_R09_HP_ANTIPOP_ON : INNO_R09_HP_ANTIPOP_OFF) <<
  63. INNO_R09_HPR_ANITPOP_SHIFT;
  64. regmsk = INNO_R09_HP_ANTIPOP_MSK << INNO_R09_HPL_ANITPOP_SHIFT |
  65. INNO_R09_HP_ANTIPOP_MSK << INNO_R09_HPR_ANITPOP_SHIFT;
  66. ret = snd_soc_component_update_bits(component, INNO_R09,
  67. regmsk, val);
  68. if (ret < 0)
  69. return ret;
  70. return 0;
  71. }
  72. #define SOC_RK3036_CODEC_ANTIPOP_DECL(xname) \
  73. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  74. .info = rk3036_codec_antipop_info, .get = rk3036_codec_antipop_get, \
  75. .put = rk3036_codec_antipop_put, }
  76. static const struct snd_kcontrol_new rk3036_codec_dapm_controls[] = {
  77. SOC_DOUBLE_R_RANGE_TLV("Headphone Volume", INNO_R07, INNO_R08,
  78. INNO_HP_GAIN_SHIFT, INNO_HP_GAIN_N39DB,
  79. INNO_HP_GAIN_0DB, 0, rk3036_codec_hp_tlv),
  80. SOC_DOUBLE("Zero Cross Switch", INNO_R06, INNO_R06_VOUTL_CZ_SHIFT,
  81. INNO_R06_VOUTR_CZ_SHIFT, 1, 0),
  82. SOC_DOUBLE("Headphone Switch", INNO_R09, INNO_R09_HPL_MUTE_SHIFT,
  83. INNO_R09_HPR_MUTE_SHIFT, 1, 0),
  84. SOC_RK3036_CODEC_ANTIPOP_DECL("Anti-pop Switch"),
  85. };
  86. static const struct snd_kcontrol_new rk3036_codec_hpl_mixer_controls[] = {
  87. SOC_DAPM_SINGLE("DAC Left Out Switch", INNO_R09,
  88. INNO_R09_DACL_SWITCH_SHIFT, 1, 0),
  89. };
  90. static const struct snd_kcontrol_new rk3036_codec_hpr_mixer_controls[] = {
  91. SOC_DAPM_SINGLE("DAC Right Out Switch", INNO_R09,
  92. INNO_R09_DACR_SWITCH_SHIFT, 1, 0),
  93. };
  94. static const struct snd_kcontrol_new rk3036_codec_hpl_switch_controls[] = {
  95. SOC_DAPM_SINGLE("HP Left Out Switch", INNO_R05,
  96. INNO_R05_HPL_WORK_SHIFT, 1, 0),
  97. };
  98. static const struct snd_kcontrol_new rk3036_codec_hpr_switch_controls[] = {
  99. SOC_DAPM_SINGLE("HP Right Out Switch", INNO_R05,
  100. INNO_R05_HPR_WORK_SHIFT, 1, 0),
  101. };
  102. static const struct snd_soc_dapm_widget rk3036_codec_dapm_widgets[] = {
  103. SND_SOC_DAPM_SUPPLY_S("DAC PWR", 1, INNO_R06,
  104. INNO_R06_DAC_EN_SHIFT, 0, NULL, 0),
  105. SND_SOC_DAPM_SUPPLY_S("DACL VREF", 2, INNO_R04,
  106. INNO_R04_DACL_VREF_SHIFT, 0, NULL, 0),
  107. SND_SOC_DAPM_SUPPLY_S("DACR VREF", 2, INNO_R04,
  108. INNO_R04_DACR_VREF_SHIFT, 0, NULL, 0),
  109. SND_SOC_DAPM_SUPPLY_S("DACL HiLo VREF", 3, INNO_R06,
  110. INNO_R06_DACL_HILO_VREF_SHIFT, 0, NULL, 0),
  111. SND_SOC_DAPM_SUPPLY_S("DACR HiLo VREF", 3, INNO_R06,
  112. INNO_R06_DACR_HILO_VREF_SHIFT, 0, NULL, 0),
  113. SND_SOC_DAPM_SUPPLY_S("DACR CLK", 3, INNO_R04,
  114. INNO_R04_DACR_CLK_SHIFT, 0, NULL, 0),
  115. SND_SOC_DAPM_SUPPLY_S("DACL CLK", 3, INNO_R04,
  116. INNO_R04_DACL_CLK_SHIFT, 0, NULL, 0),
  117. SND_SOC_DAPM_DAC("DACL", "Left Playback", INNO_R04,
  118. INNO_R04_DACL_SW_SHIFT, 0),
  119. SND_SOC_DAPM_DAC("DACR", "Right Playback", INNO_R04,
  120. INNO_R04_DACR_SW_SHIFT, 0),
  121. SND_SOC_DAPM_MIXER("Left Headphone Mixer", SND_SOC_NOPM, 0, 0,
  122. rk3036_codec_hpl_mixer_controls,
  123. ARRAY_SIZE(rk3036_codec_hpl_mixer_controls)),
  124. SND_SOC_DAPM_MIXER("Right Headphone Mixer", SND_SOC_NOPM, 0, 0,
  125. rk3036_codec_hpr_mixer_controls,
  126. ARRAY_SIZE(rk3036_codec_hpr_mixer_controls)),
  127. SND_SOC_DAPM_PGA("HP Left Out", INNO_R05,
  128. INNO_R05_HPL_EN_SHIFT, 0, NULL, 0),
  129. SND_SOC_DAPM_PGA("HP Right Out", INNO_R05,
  130. INNO_R05_HPR_EN_SHIFT, 0, NULL, 0),
  131. SND_SOC_DAPM_MIXER("HP Left Switch", SND_SOC_NOPM, 0, 0,
  132. rk3036_codec_hpl_switch_controls,
  133. ARRAY_SIZE(rk3036_codec_hpl_switch_controls)),
  134. SND_SOC_DAPM_MIXER("HP Right Switch", SND_SOC_NOPM, 0, 0,
  135. rk3036_codec_hpr_switch_controls,
  136. ARRAY_SIZE(rk3036_codec_hpr_switch_controls)),
  137. SND_SOC_DAPM_OUTPUT("HPL"),
  138. SND_SOC_DAPM_OUTPUT("HPR"),
  139. };
  140. static const struct snd_soc_dapm_route rk3036_codec_dapm_routes[] = {
  141. {"DACL VREF", NULL, "DAC PWR"},
  142. {"DACR VREF", NULL, "DAC PWR"},
  143. {"DACL HiLo VREF", NULL, "DAC PWR"},
  144. {"DACR HiLo VREF", NULL, "DAC PWR"},
  145. {"DACL CLK", NULL, "DAC PWR"},
  146. {"DACR CLK", NULL, "DAC PWR"},
  147. {"DACL", NULL, "DACL VREF"},
  148. {"DACL", NULL, "DACL HiLo VREF"},
  149. {"DACL", NULL, "DACL CLK"},
  150. {"DACR", NULL, "DACR VREF"},
  151. {"DACR", NULL, "DACR HiLo VREF"},
  152. {"DACR", NULL, "DACR CLK"},
  153. {"Left Headphone Mixer", "DAC Left Out Switch", "DACL"},
  154. {"Right Headphone Mixer", "DAC Right Out Switch", "DACR"},
  155. {"HP Left Out", NULL, "Left Headphone Mixer"},
  156. {"HP Right Out", NULL, "Right Headphone Mixer"},
  157. {"HP Left Switch", "HP Left Out Switch", "HP Left Out"},
  158. {"HP Right Switch", "HP Right Out Switch", "HP Right Out"},
  159. {"HPL", NULL, "HP Left Switch"},
  160. {"HPR", NULL, "HP Right Switch"},
  161. };
  162. static int rk3036_codec_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  163. {
  164. struct snd_soc_component *component = dai->component;
  165. unsigned int reg01_val = 0, reg02_val = 0, reg03_val = 0;
  166. dev_dbg(component->dev, "rk3036_codec dai set fmt : %08x\n", fmt);
  167. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  168. case SND_SOC_DAIFMT_CBC_CFC:
  169. reg01_val |= INNO_R01_PINDIR_IN_SLAVE |
  170. INNO_R01_I2SMODE_SLAVE;
  171. break;
  172. case SND_SOC_DAIFMT_CBP_CFP:
  173. reg01_val |= INNO_R01_PINDIR_OUT_MASTER |
  174. INNO_R01_I2SMODE_MASTER;
  175. break;
  176. default:
  177. dev_err(component->dev, "invalid fmt\n");
  178. return -EINVAL;
  179. }
  180. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  181. case SND_SOC_DAIFMT_DSP_A:
  182. reg02_val |= INNO_R02_DACM_PCM;
  183. break;
  184. case SND_SOC_DAIFMT_I2S:
  185. reg02_val |= INNO_R02_DACM_I2S;
  186. break;
  187. case SND_SOC_DAIFMT_RIGHT_J:
  188. reg02_val |= INNO_R02_DACM_RJM;
  189. break;
  190. case SND_SOC_DAIFMT_LEFT_J:
  191. reg02_val |= INNO_R02_DACM_LJM;
  192. break;
  193. default:
  194. dev_err(component->dev, "set dai format failed\n");
  195. return -EINVAL;
  196. }
  197. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  198. case SND_SOC_DAIFMT_NB_NF:
  199. reg02_val |= INNO_R02_LRCP_NORMAL;
  200. reg03_val |= INNO_R03_BCP_NORMAL;
  201. break;
  202. case SND_SOC_DAIFMT_IB_IF:
  203. reg02_val |= INNO_R02_LRCP_REVERSAL;
  204. reg03_val |= INNO_R03_BCP_REVERSAL;
  205. break;
  206. case SND_SOC_DAIFMT_IB_NF:
  207. reg02_val |= INNO_R02_LRCP_REVERSAL;
  208. reg03_val |= INNO_R03_BCP_NORMAL;
  209. break;
  210. case SND_SOC_DAIFMT_NB_IF:
  211. reg02_val |= INNO_R02_LRCP_NORMAL;
  212. reg03_val |= INNO_R03_BCP_REVERSAL;
  213. break;
  214. default:
  215. dev_err(component->dev, "set dai format failed\n");
  216. return -EINVAL;
  217. }
  218. snd_soc_component_update_bits(component, INNO_R01, INNO_R01_I2SMODE_MSK |
  219. INNO_R01_PINDIR_MSK, reg01_val);
  220. snd_soc_component_update_bits(component, INNO_R02, INNO_R02_LRCP_MSK |
  221. INNO_R02_DACM_MSK, reg02_val);
  222. snd_soc_component_update_bits(component, INNO_R03, INNO_R03_BCP_MSK, reg03_val);
  223. return 0;
  224. }
  225. static int rk3036_codec_dai_hw_params(struct snd_pcm_substream *substream,
  226. struct snd_pcm_hw_params *hw_params,
  227. struct snd_soc_dai *dai)
  228. {
  229. struct snd_soc_component *component = dai->component;
  230. unsigned int reg02_val = 0, reg03_val = 0;
  231. switch (params_format(hw_params)) {
  232. case SNDRV_PCM_FORMAT_S16_LE:
  233. reg02_val |= INNO_R02_VWL_16BIT;
  234. break;
  235. case SNDRV_PCM_FORMAT_S20_3LE:
  236. reg02_val |= INNO_R02_VWL_20BIT;
  237. break;
  238. case SNDRV_PCM_FORMAT_S24_LE:
  239. reg02_val |= INNO_R02_VWL_24BIT;
  240. break;
  241. case SNDRV_PCM_FORMAT_S32_LE:
  242. reg02_val |= INNO_R02_VWL_32BIT;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. reg02_val |= INNO_R02_LRCP_NORMAL;
  248. reg03_val |= INNO_R03_FWL_32BIT | INNO_R03_DACR_WORK;
  249. snd_soc_component_update_bits(component, INNO_R02, INNO_R02_LRCP_MSK |
  250. INNO_R02_VWL_MSK, reg02_val);
  251. snd_soc_component_update_bits(component, INNO_R03, INNO_R03_DACR_MSK |
  252. INNO_R03_FWL_MSK, reg03_val);
  253. return 0;
  254. }
  255. #define RK3036_CODEC_RATES (SNDRV_PCM_RATE_8000 | \
  256. SNDRV_PCM_RATE_16000 | \
  257. SNDRV_PCM_RATE_32000 | \
  258. SNDRV_PCM_RATE_44100 | \
  259. SNDRV_PCM_RATE_48000 | \
  260. SNDRV_PCM_RATE_96000)
  261. #define RK3036_CODEC_FMTS (SNDRV_PCM_FMTBIT_S16_LE | \
  262. SNDRV_PCM_FMTBIT_S20_3LE | \
  263. SNDRV_PCM_FMTBIT_S24_LE | \
  264. SNDRV_PCM_FMTBIT_S32_LE)
  265. static const struct snd_soc_dai_ops rk3036_codec_dai_ops = {
  266. .set_fmt = rk3036_codec_dai_set_fmt,
  267. .hw_params = rk3036_codec_dai_hw_params,
  268. };
  269. static struct snd_soc_dai_driver rk3036_codec_dai_driver[] = {
  270. {
  271. .name = "rk3036-codec-dai",
  272. .playback = {
  273. .stream_name = "Playback",
  274. .channels_min = 1,
  275. .channels_max = 2,
  276. .rates = RK3036_CODEC_RATES,
  277. .formats = RK3036_CODEC_FMTS,
  278. },
  279. .ops = &rk3036_codec_dai_ops,
  280. .symmetric_rate = 1,
  281. },
  282. };
  283. static void rk3036_codec_reset(struct snd_soc_component *component)
  284. {
  285. snd_soc_component_write(component, INNO_R00,
  286. INNO_R00_CSR_RESET | INNO_R00_CDCR_RESET);
  287. snd_soc_component_write(component, INNO_R00,
  288. INNO_R00_CSR_WORK | INNO_R00_CDCR_WORK);
  289. }
  290. static int rk3036_codec_probe(struct snd_soc_component *component)
  291. {
  292. rk3036_codec_reset(component);
  293. return 0;
  294. }
  295. static void rk3036_codec_remove(struct snd_soc_component *component)
  296. {
  297. rk3036_codec_reset(component);
  298. }
  299. static int rk3036_codec_set_bias_level(struct snd_soc_component *component,
  300. enum snd_soc_bias_level level)
  301. {
  302. switch (level) {
  303. case SND_SOC_BIAS_STANDBY:
  304. /* set a big current for capacitor charging. */
  305. snd_soc_component_write(component, INNO_R10, INNO_R10_MAX_CUR);
  306. /* start precharge */
  307. snd_soc_component_write(component, INNO_R06, INNO_R06_DAC_PRECHARGE);
  308. break;
  309. case SND_SOC_BIAS_OFF:
  310. /* set a big current for capacitor discharging. */
  311. snd_soc_component_write(component, INNO_R10, INNO_R10_MAX_CUR);
  312. /* start discharge. */
  313. snd_soc_component_write(component, INNO_R06, INNO_R06_DAC_DISCHARGE);
  314. break;
  315. default:
  316. break;
  317. }
  318. return 0;
  319. }
  320. static const struct snd_soc_component_driver rk3036_codec_driver = {
  321. .probe = rk3036_codec_probe,
  322. .remove = rk3036_codec_remove,
  323. .set_bias_level = rk3036_codec_set_bias_level,
  324. .controls = rk3036_codec_dapm_controls,
  325. .num_controls = ARRAY_SIZE(rk3036_codec_dapm_controls),
  326. .dapm_routes = rk3036_codec_dapm_routes,
  327. .num_dapm_routes = ARRAY_SIZE(rk3036_codec_dapm_routes),
  328. .dapm_widgets = rk3036_codec_dapm_widgets,
  329. .num_dapm_widgets = ARRAY_SIZE(rk3036_codec_dapm_widgets),
  330. .idle_bias_on = 1,
  331. .use_pmdown_time = 1,
  332. .endianness = 1,
  333. };
  334. static const struct regmap_config rk3036_codec_regmap_config = {
  335. .reg_bits = 32,
  336. .reg_stride = 4,
  337. .val_bits = 32,
  338. };
  339. #define GRF_SOC_CON0 0x00140
  340. #define GRF_ACODEC_SEL (BIT(10) | BIT(16 + 10))
  341. static int rk3036_codec_platform_probe(struct platform_device *pdev)
  342. {
  343. struct rk3036_codec_priv *priv;
  344. struct device_node *of_node = pdev->dev.of_node;
  345. void __iomem *base;
  346. struct regmap *grf;
  347. int ret;
  348. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  349. if (!priv)
  350. return -ENOMEM;
  351. base = devm_platform_ioremap_resource(pdev, 0);
  352. if (IS_ERR(base))
  353. return PTR_ERR(base);
  354. priv->base = base;
  355. priv->regmap = devm_regmap_init_mmio(&pdev->dev, priv->base,
  356. &rk3036_codec_regmap_config);
  357. if (IS_ERR(priv->regmap)) {
  358. dev_err(&pdev->dev, "init regmap failed\n");
  359. return PTR_ERR(priv->regmap);
  360. }
  361. grf = syscon_regmap_lookup_by_phandle(of_node, "rockchip,grf");
  362. if (IS_ERR(grf)) {
  363. dev_err(&pdev->dev, "needs 'rockchip,grf' property\n");
  364. return PTR_ERR(grf);
  365. }
  366. ret = regmap_write(grf, GRF_SOC_CON0, GRF_ACODEC_SEL);
  367. if (ret) {
  368. dev_err(&pdev->dev, "Could not write to GRF: %d\n", ret);
  369. return ret;
  370. }
  371. priv->pclk = devm_clk_get(&pdev->dev, "acodec_pclk");
  372. if (IS_ERR(priv->pclk))
  373. return PTR_ERR(priv->pclk);
  374. ret = clk_prepare_enable(priv->pclk);
  375. if (ret < 0) {
  376. dev_err(&pdev->dev, "failed to enable clk\n");
  377. return ret;
  378. }
  379. priv->dev = &pdev->dev;
  380. dev_set_drvdata(&pdev->dev, priv);
  381. ret = devm_snd_soc_register_component(&pdev->dev, &rk3036_codec_driver,
  382. rk3036_codec_dai_driver,
  383. ARRAY_SIZE(rk3036_codec_dai_driver));
  384. if (ret) {
  385. clk_disable_unprepare(priv->pclk);
  386. dev_set_drvdata(&pdev->dev, NULL);
  387. }
  388. return ret;
  389. }
  390. static int rk3036_codec_platform_remove(struct platform_device *pdev)
  391. {
  392. struct rk3036_codec_priv *priv = dev_get_drvdata(&pdev->dev);
  393. clk_disable_unprepare(priv->pclk);
  394. return 0;
  395. }
  396. static const struct of_device_id rk3036_codec_of_match[] __maybe_unused = {
  397. { .compatible = "rockchip,rk3036-codec", },
  398. {}
  399. };
  400. MODULE_DEVICE_TABLE(of, rk3036_codec_of_match);
  401. static struct platform_driver rk3036_codec_platform_driver = {
  402. .driver = {
  403. .name = "rk3036-codec-platform",
  404. .of_match_table = of_match_ptr(rk3036_codec_of_match),
  405. },
  406. .probe = rk3036_codec_platform_probe,
  407. .remove = rk3036_codec_platform_remove,
  408. };
  409. module_platform_driver(rk3036_codec_platform_driver);
  410. MODULE_AUTHOR("Rockchip Inc.");
  411. MODULE_DESCRIPTION("Rockchip rk3036 codec driver");
  412. MODULE_LICENSE("GPL");