t9015.c 8.5 KB

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