t9015.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <[email protected]>
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/module.h>
  8. #include <linux/regmap.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <linux/reset.h>
  11. #include <sound/soc.h>
  12. #include <sound/tlv.h>
  13. #define BLOCK_EN 0x00
  14. #define LORN_EN 0
  15. #define LORP_EN 1
  16. #define LOLN_EN 2
  17. #define LOLP_EN 3
  18. #define DACR_EN 4
  19. #define DACL_EN 5
  20. #define DACR_INV 20
  21. #define DACL_INV 21
  22. #define DACR_SRC 22
  23. #define DACL_SRC 23
  24. #define REFP_BUF_EN BIT(12)
  25. #define BIAS_CURRENT_EN BIT(13)
  26. #define VMID_GEN_FAST BIT(14)
  27. #define VMID_GEN_EN BIT(15)
  28. #define I2S_MODE BIT(30)
  29. #define VOL_CTRL0 0x04
  30. #define GAIN_H 31
  31. #define GAIN_L 23
  32. #define VOL_CTRL1 0x08
  33. #define DAC_MONO 8
  34. #define RAMP_RATE 10
  35. #define VC_RAMP_MODE 12
  36. #define MUTE_MODE 13
  37. #define UNMUTE_MODE 14
  38. #define DAC_SOFT_MUTE 15
  39. #define DACR_VC 16
  40. #define DACL_VC 24
  41. #define LINEOUT_CFG 0x0c
  42. #define LORN_POL 0
  43. #define LORP_POL 4
  44. #define LOLN_POL 8
  45. #define LOLP_POL 12
  46. #define POWER_CFG 0x10
  47. struct t9015 {
  48. struct clk *pclk;
  49. struct regulator *avdd;
  50. };
  51. static int t9015_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  52. {
  53. struct snd_soc_component *component = dai->component;
  54. unsigned int val;
  55. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  56. case SND_SOC_DAIFMT_CBM_CFM:
  57. val = I2S_MODE;
  58. break;
  59. case SND_SOC_DAIFMT_CBS_CFS:
  60. val = 0;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. snd_soc_component_update_bits(component, BLOCK_EN, I2S_MODE, val);
  66. if (((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) &&
  67. ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_LEFT_J))
  68. return -EINVAL;
  69. return 0;
  70. }
  71. static const struct snd_soc_dai_ops t9015_dai_ops = {
  72. .set_fmt = t9015_dai_set_fmt,
  73. };
  74. static struct snd_soc_dai_driver t9015_dai = {
  75. .name = "t9015-hifi",
  76. .playback = {
  77. .stream_name = "Playback",
  78. .channels_min = 1,
  79. .channels_max = 2,
  80. .rates = SNDRV_PCM_RATE_8000_96000,
  81. .formats = (SNDRV_PCM_FMTBIT_S8 |
  82. SNDRV_PCM_FMTBIT_S16_LE |
  83. SNDRV_PCM_FMTBIT_S20_LE |
  84. SNDRV_PCM_FMTBIT_S24_LE),
  85. },
  86. .ops = &t9015_dai_ops,
  87. };
  88. static const DECLARE_TLV_DB_MINMAX_MUTE(dac_vol_tlv, -9525, 0);
  89. static const char * const ramp_rate_txt[] = { "Fast", "Slow" };
  90. static SOC_ENUM_SINGLE_DECL(ramp_rate_enum, VOL_CTRL1, RAMP_RATE,
  91. ramp_rate_txt);
  92. static const char * const dacr_in_txt[] = { "Right", "Left" };
  93. static SOC_ENUM_SINGLE_DECL(dacr_in_enum, BLOCK_EN, DACR_SRC, dacr_in_txt);
  94. static const char * const dacl_in_txt[] = { "Left", "Right" };
  95. static SOC_ENUM_SINGLE_DECL(dacl_in_enum, BLOCK_EN, DACL_SRC, dacl_in_txt);
  96. static const char * const mono_txt[] = { "Stereo", "Mono"};
  97. static SOC_ENUM_SINGLE_DECL(mono_enum, VOL_CTRL1, DAC_MONO, mono_txt);
  98. static const struct snd_kcontrol_new t9015_snd_controls[] = {
  99. /* Volume Controls */
  100. SOC_ENUM("Playback Channel Mode", mono_enum),
  101. SOC_SINGLE("Playback Switch", VOL_CTRL1, DAC_SOFT_MUTE, 1, 1),
  102. SOC_DOUBLE_TLV("Playback Volume", VOL_CTRL1, DACL_VC, DACR_VC,
  103. 0xff, 0, dac_vol_tlv),
  104. /* Ramp Controls */
  105. SOC_ENUM("Ramp Rate", ramp_rate_enum),
  106. SOC_SINGLE("Volume Ramp Switch", VOL_CTRL1, VC_RAMP_MODE, 1, 0),
  107. SOC_SINGLE("Mute Ramp Switch", VOL_CTRL1, MUTE_MODE, 1, 0),
  108. SOC_SINGLE("Unmute Ramp Switch", VOL_CTRL1, UNMUTE_MODE, 1, 0),
  109. };
  110. static const struct snd_kcontrol_new t9015_right_dac_mux =
  111. SOC_DAPM_ENUM("Right DAC Source", dacr_in_enum);
  112. static const struct snd_kcontrol_new t9015_left_dac_mux =
  113. SOC_DAPM_ENUM("Left DAC Source", dacl_in_enum);
  114. static const struct snd_soc_dapm_widget t9015_dapm_widgets[] = {
  115. SND_SOC_DAPM_AIF_IN("Right IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  116. SND_SOC_DAPM_AIF_IN("Left IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  117. SND_SOC_DAPM_MUX("Right DAC Sel", SND_SOC_NOPM, 0, 0,
  118. &t9015_right_dac_mux),
  119. SND_SOC_DAPM_MUX("Left DAC Sel", SND_SOC_NOPM, 0, 0,
  120. &t9015_left_dac_mux),
  121. SND_SOC_DAPM_DAC("Right DAC", NULL, BLOCK_EN, DACR_EN, 0),
  122. SND_SOC_DAPM_DAC("Left DAC", NULL, BLOCK_EN, DACL_EN, 0),
  123. SND_SOC_DAPM_OUT_DRV("Right- Driver", BLOCK_EN, LORN_EN, 0,
  124. NULL, 0),
  125. SND_SOC_DAPM_OUT_DRV("Right+ Driver", BLOCK_EN, LORP_EN, 0,
  126. NULL, 0),
  127. SND_SOC_DAPM_OUT_DRV("Left- Driver", BLOCK_EN, LOLN_EN, 0,
  128. NULL, 0),
  129. SND_SOC_DAPM_OUT_DRV("Left+ Driver", BLOCK_EN, LOLP_EN, 0,
  130. NULL, 0),
  131. SND_SOC_DAPM_OUTPUT("LORN"),
  132. SND_SOC_DAPM_OUTPUT("LORP"),
  133. SND_SOC_DAPM_OUTPUT("LOLN"),
  134. SND_SOC_DAPM_OUTPUT("LOLP"),
  135. };
  136. static const struct snd_soc_dapm_route t9015_dapm_routes[] = {
  137. { "Right IN", NULL, "Playback" },
  138. { "Left IN", NULL, "Playback" },
  139. { "Right DAC Sel", "Right", "Right IN" },
  140. { "Right DAC Sel", "Left", "Left IN" },
  141. { "Left DAC Sel", "Right", "Right IN" },
  142. { "Left DAC Sel", "Left", "Left IN" },
  143. { "Right DAC", NULL, "Right DAC Sel" },
  144. { "Left DAC", NULL, "Left DAC Sel" },
  145. { "Right- Driver", NULL, "Right DAC" },
  146. { "Right+ Driver", NULL, "Right DAC" },
  147. { "Left- Driver", NULL, "Left DAC" },
  148. { "Left+ Driver", NULL, "Left DAC" },
  149. { "LORN", NULL, "Right- Driver", },
  150. { "LORP", NULL, "Right+ Driver", },
  151. { "LOLN", NULL, "Left- Driver", },
  152. { "LOLP", NULL, "Left+ Driver", },
  153. };
  154. static int t9015_set_bias_level(struct snd_soc_component *component,
  155. enum snd_soc_bias_level level)
  156. {
  157. struct t9015 *priv = snd_soc_component_get_drvdata(component);
  158. enum snd_soc_bias_level now =
  159. snd_soc_component_get_bias_level(component);
  160. int ret;
  161. switch (level) {
  162. case SND_SOC_BIAS_ON:
  163. snd_soc_component_update_bits(component, BLOCK_EN,
  164. BIAS_CURRENT_EN,
  165. BIAS_CURRENT_EN);
  166. break;
  167. case SND_SOC_BIAS_PREPARE:
  168. snd_soc_component_update_bits(component, BLOCK_EN,
  169. BIAS_CURRENT_EN,
  170. 0);
  171. break;
  172. case SND_SOC_BIAS_STANDBY:
  173. ret = regulator_enable(priv->avdd);
  174. if (ret) {
  175. dev_err(component->dev, "AVDD enable failed\n");
  176. return ret;
  177. }
  178. if (now == SND_SOC_BIAS_OFF) {
  179. snd_soc_component_update_bits(component, BLOCK_EN,
  180. VMID_GEN_EN | VMID_GEN_FAST | REFP_BUF_EN,
  181. VMID_GEN_EN | VMID_GEN_FAST | REFP_BUF_EN);
  182. mdelay(200);
  183. snd_soc_component_update_bits(component, BLOCK_EN,
  184. VMID_GEN_FAST,
  185. 0);
  186. }
  187. break;
  188. case SND_SOC_BIAS_OFF:
  189. snd_soc_component_update_bits(component, BLOCK_EN,
  190. VMID_GEN_EN | VMID_GEN_FAST | REFP_BUF_EN,
  191. 0);
  192. regulator_disable(priv->avdd);
  193. break;
  194. }
  195. return 0;
  196. }
  197. static const struct snd_soc_component_driver t9015_codec_driver = {
  198. .set_bias_level = t9015_set_bias_level,
  199. .controls = t9015_snd_controls,
  200. .num_controls = ARRAY_SIZE(t9015_snd_controls),
  201. .dapm_widgets = t9015_dapm_widgets,
  202. .num_dapm_widgets = ARRAY_SIZE(t9015_dapm_widgets),
  203. .dapm_routes = t9015_dapm_routes,
  204. .num_dapm_routes = ARRAY_SIZE(t9015_dapm_routes),
  205. .suspend_bias_off = 1,
  206. .endianness = 1,
  207. };
  208. static const struct regmap_config t9015_regmap_config = {
  209. .reg_bits = 32,
  210. .reg_stride = 4,
  211. .val_bits = 32,
  212. .max_register = POWER_CFG,
  213. };
  214. static int t9015_probe(struct platform_device *pdev)
  215. {
  216. struct device *dev = &pdev->dev;
  217. struct t9015 *priv;
  218. void __iomem *regs;
  219. struct regmap *regmap;
  220. int ret;
  221. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  222. if (!priv)
  223. return -ENOMEM;
  224. platform_set_drvdata(pdev, priv);
  225. priv->pclk = devm_clk_get(dev, "pclk");
  226. if (IS_ERR(priv->pclk))
  227. return dev_err_probe(dev, PTR_ERR(priv->pclk), "failed to get core clock\n");
  228. priv->avdd = devm_regulator_get(dev, "AVDD");
  229. if (IS_ERR(priv->avdd))
  230. return dev_err_probe(dev, PTR_ERR(priv->avdd), "failed to AVDD\n");
  231. ret = clk_prepare_enable(priv->pclk);
  232. if (ret) {
  233. dev_err(dev, "core clock enable failed\n");
  234. return ret;
  235. }
  236. ret = devm_add_action_or_reset(dev,
  237. (void(*)(void *))clk_disable_unprepare,
  238. priv->pclk);
  239. if (ret)
  240. return ret;
  241. ret = device_reset(dev);
  242. if (ret) {
  243. dev_err(dev, "reset failed\n");
  244. return ret;
  245. }
  246. regs = devm_platform_ioremap_resource(pdev, 0);
  247. if (IS_ERR(regs)) {
  248. dev_err(dev, "register map failed\n");
  249. return PTR_ERR(regs);
  250. }
  251. regmap = devm_regmap_init_mmio(dev, regs, &t9015_regmap_config);
  252. if (IS_ERR(regmap)) {
  253. dev_err(dev, "regmap init failed\n");
  254. return PTR_ERR(regmap);
  255. }
  256. /*
  257. * Initialize output polarity:
  258. * ATM the output polarity is fixed but in the future it might useful
  259. * to add DT property to set this depending on the platform needs
  260. */
  261. regmap_write(regmap, LINEOUT_CFG, 0x1111);
  262. return devm_snd_soc_register_component(dev, &t9015_codec_driver,
  263. &t9015_dai, 1);
  264. }
  265. static const struct of_device_id t9015_ids[] __maybe_unused = {
  266. { .compatible = "amlogic,t9015", },
  267. { }
  268. };
  269. MODULE_DEVICE_TABLE(of, t9015_ids);
  270. static struct platform_driver t9015_driver = {
  271. .driver = {
  272. .name = "t9015-codec",
  273. .of_match_table = of_match_ptr(t9015_ids),
  274. },
  275. .probe = t9015_probe,
  276. };
  277. module_platform_driver(t9015_driver);
  278. MODULE_DESCRIPTION("ASoC Amlogic T9015 codec driver");
  279. MODULE_AUTHOR("Jerome Brunet <[email protected]>");
  280. MODULE_LICENSE("GPL");