inno_rk3036.c 14 KB

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