pistachio-internal-dac.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Pistachio internal dac driver
  3. *
  4. * Copyright (C) 2015 Imagination Technologies Ltd.
  5. *
  6. * Author: Damien Horsley <Damien.Horsley@imgtec.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #define PISTACHIO_INTERNAL_DAC_CTRL 0x40
  22. #define PISTACHIO_INTERNAL_DAC_CTRL_PWR_SEL_MASK 0x2
  23. #define PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK 0x1
  24. #define PISTACHIO_INTERNAL_DAC_SRST 0x44
  25. #define PISTACHIO_INTERNAL_DAC_SRST_MASK 0x1
  26. #define PISTACHIO_INTERNAL_DAC_GTI_CTRL 0x48
  27. #define PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_SHIFT 0
  28. #define PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_MASK 0xFFF
  29. #define PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK 0x1000
  30. #define PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_SHIFT 13
  31. #define PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_MASK 0x1FE000
  32. #define PISTACHIO_INTERNAL_DAC_PWR 0x1
  33. #define PISTACHIO_INTERNAL_DAC_PWR_MASK 0x1
  34. #define PISTACHIO_INTERNAL_DAC_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | \
  35. SNDRV_PCM_FMTBIT_S32_LE)
  36. /* codec private data */
  37. struct pistachio_internal_dac {
  38. struct regmap *regmap;
  39. struct regulator *supply;
  40. bool mute;
  41. };
  42. static const struct snd_kcontrol_new pistachio_internal_dac_snd_controls[] = {
  43. SOC_SINGLE("Playback Switch", PISTACHIO_INTERNAL_DAC_CTRL, 2, 1, 1)
  44. };
  45. static const struct snd_soc_dapm_widget pistachio_internal_dac_widgets[] = {
  46. SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  47. SND_SOC_DAPM_OUTPUT("AOUTL"),
  48. SND_SOC_DAPM_OUTPUT("AOUTR"),
  49. };
  50. static const struct snd_soc_dapm_route pistachio_internal_dac_routes[] = {
  51. { "AOUTL", NULL, "DAC" },
  52. { "AOUTR", NULL, "DAC" },
  53. };
  54. static void pistachio_internal_dac_reg_writel(struct regmap *top_regs,
  55. u32 val, u32 reg)
  56. {
  57. regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
  58. PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_MASK,
  59. reg << PISTACHIO_INTERNAL_DAC_GTI_CTRL_ADDR_SHIFT);
  60. regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
  61. PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_MASK,
  62. val << PISTACHIO_INTERNAL_DAC_GTI_CTRL_WDATA_SHIFT);
  63. regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
  64. PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK,
  65. PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK);
  66. regmap_update_bits(top_regs, PISTACHIO_INTERNAL_DAC_GTI_CTRL,
  67. PISTACHIO_INTERNAL_DAC_GTI_CTRL_WE_MASK, 0);
  68. }
  69. static void pistachio_internal_dac_pwr_off(struct pistachio_internal_dac *dac)
  70. {
  71. regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_CTRL,
  72. PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK,
  73. PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK);
  74. pistachio_internal_dac_reg_writel(dac->regmap, 0,
  75. PISTACHIO_INTERNAL_DAC_PWR);
  76. }
  77. static void pistachio_internal_dac_pwr_on(struct pistachio_internal_dac *dac)
  78. {
  79. regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_SRST,
  80. PISTACHIO_INTERNAL_DAC_SRST_MASK,
  81. PISTACHIO_INTERNAL_DAC_SRST_MASK);
  82. regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_SRST,
  83. PISTACHIO_INTERNAL_DAC_SRST_MASK, 0);
  84. pistachio_internal_dac_reg_writel(dac->regmap,
  85. PISTACHIO_INTERNAL_DAC_PWR_MASK,
  86. PISTACHIO_INTERNAL_DAC_PWR);
  87. regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_CTRL,
  88. PISTACHIO_INTERNAL_DAC_CTRL_PWRDN_MASK, 0);
  89. }
  90. static struct snd_soc_dai_driver pistachio_internal_dac_dais[] = {
  91. {
  92. .name = "pistachio_internal_dac",
  93. .playback = {
  94. .stream_name = "Playback",
  95. .channels_min = 2,
  96. .channels_max = 2,
  97. .rates = SNDRV_PCM_RATE_8000_48000,
  98. .formats = PISTACHIO_INTERNAL_DAC_FORMATS,
  99. }
  100. },
  101. };
  102. static int pistachio_internal_dac_codec_probe(struct snd_soc_codec *codec)
  103. {
  104. struct pistachio_internal_dac *dac = snd_soc_codec_get_drvdata(codec);
  105. snd_soc_codec_init_regmap(codec, dac->regmap);
  106. return 0;
  107. }
  108. static const struct snd_soc_codec_driver pistachio_internal_dac_driver = {
  109. .probe = pistachio_internal_dac_codec_probe,
  110. .idle_bias_off = true,
  111. .component_driver = {
  112. .controls = pistachio_internal_dac_snd_controls,
  113. .num_controls = ARRAY_SIZE(pistachio_internal_dac_snd_controls),
  114. .dapm_widgets = pistachio_internal_dac_widgets,
  115. .num_dapm_widgets = ARRAY_SIZE(pistachio_internal_dac_widgets),
  116. .dapm_routes = pistachio_internal_dac_routes,
  117. .num_dapm_routes = ARRAY_SIZE(pistachio_internal_dac_routes),
  118. },
  119. };
  120. static int pistachio_internal_dac_probe(struct platform_device *pdev)
  121. {
  122. struct pistachio_internal_dac *dac;
  123. int ret, voltage;
  124. struct device *dev = &pdev->dev;
  125. u32 reg;
  126. dac = devm_kzalloc(dev, sizeof(*dac), GFP_KERNEL);
  127. if (!dac)
  128. return -ENOMEM;
  129. platform_set_drvdata(pdev, dac);
  130. dac->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  131. "img,cr-top");
  132. if (IS_ERR(dac->regmap))
  133. return PTR_ERR(dac->regmap);
  134. dac->supply = devm_regulator_get(dev, "VDD");
  135. if (IS_ERR(dac->supply)) {
  136. ret = PTR_ERR(dac->supply);
  137. if (ret != -EPROBE_DEFER)
  138. dev_err(dev, "failed to acquire supply 'VDD-supply': %d\n", ret);
  139. return ret;
  140. }
  141. ret = regulator_enable(dac->supply);
  142. if (ret) {
  143. dev_err(dev, "failed to enable supply: %d\n", ret);
  144. return ret;
  145. }
  146. voltage = regulator_get_voltage(dac->supply);
  147. switch (voltage) {
  148. case 1800000:
  149. reg = 0;
  150. break;
  151. case 3300000:
  152. reg = PISTACHIO_INTERNAL_DAC_CTRL_PWR_SEL_MASK;
  153. break;
  154. default:
  155. dev_err(dev, "invalid voltage: %d\n", voltage);
  156. ret = -EINVAL;
  157. goto err_regulator;
  158. }
  159. regmap_update_bits(dac->regmap, PISTACHIO_INTERNAL_DAC_CTRL,
  160. PISTACHIO_INTERNAL_DAC_CTRL_PWR_SEL_MASK, reg);
  161. pistachio_internal_dac_pwr_off(dac);
  162. pistachio_internal_dac_pwr_on(dac);
  163. pm_runtime_set_active(dev);
  164. pm_runtime_enable(dev);
  165. pm_runtime_idle(dev);
  166. ret = snd_soc_register_codec(dev, &pistachio_internal_dac_driver,
  167. pistachio_internal_dac_dais,
  168. ARRAY_SIZE(pistachio_internal_dac_dais));
  169. if (ret) {
  170. dev_err(dev, "failed to register codec: %d\n", ret);
  171. goto err_pwr;
  172. }
  173. return 0;
  174. err_pwr:
  175. pm_runtime_disable(&pdev->dev);
  176. pistachio_internal_dac_pwr_off(dac);
  177. err_regulator:
  178. regulator_disable(dac->supply);
  179. return ret;
  180. }
  181. static int pistachio_internal_dac_remove(struct platform_device *pdev)
  182. {
  183. struct pistachio_internal_dac *dac = dev_get_drvdata(&pdev->dev);
  184. snd_soc_unregister_codec(&pdev->dev);
  185. pm_runtime_disable(&pdev->dev);
  186. pistachio_internal_dac_pwr_off(dac);
  187. regulator_disable(dac->supply);
  188. return 0;
  189. }
  190. #ifdef CONFIG_PM
  191. static int pistachio_internal_dac_rt_resume(struct device *dev)
  192. {
  193. struct pistachio_internal_dac *dac = dev_get_drvdata(dev);
  194. int ret;
  195. ret = regulator_enable(dac->supply);
  196. if (ret) {
  197. dev_err(dev, "failed to enable supply: %d\n", ret);
  198. return ret;
  199. }
  200. pistachio_internal_dac_pwr_on(dac);
  201. return 0;
  202. }
  203. static int pistachio_internal_dac_rt_suspend(struct device *dev)
  204. {
  205. struct pistachio_internal_dac *dac = dev_get_drvdata(dev);
  206. pistachio_internal_dac_pwr_off(dac);
  207. regulator_disable(dac->supply);
  208. return 0;
  209. }
  210. #endif
  211. static const struct dev_pm_ops pistachio_internal_dac_pm_ops = {
  212. SET_RUNTIME_PM_OPS(pistachio_internal_dac_rt_suspend,
  213. pistachio_internal_dac_rt_resume, NULL)
  214. };
  215. static const struct of_device_id pistachio_internal_dac_of_match[] = {
  216. { .compatible = "img,pistachio-internal-dac" },
  217. {}
  218. };
  219. MODULE_DEVICE_TABLE(of, pistachio_internal_dac_of_match);
  220. static struct platform_driver pistachio_internal_dac_plat_driver = {
  221. .driver = {
  222. .name = "img-pistachio-internal-dac",
  223. .of_match_table = pistachio_internal_dac_of_match,
  224. .pm = &pistachio_internal_dac_pm_ops
  225. },
  226. .probe = pistachio_internal_dac_probe,
  227. .remove = pistachio_internal_dac_remove
  228. };
  229. module_platform_driver(pistachio_internal_dac_plat_driver);
  230. MODULE_DESCRIPTION("Pistachio Internal DAC driver");
  231. MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
  232. MODULE_LICENSE("GPL v2");