bd28623.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // ROHM BD28623MUV class D speaker amplifier codec driver.
  4. //
  5. // Copyright (c) 2018 Socionext Inc.
  6. #include <linux/delay.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <sound/pcm.h>
  12. #include <sound/soc.h>
  13. #define BD28623_NUM_SUPPLIES 3
  14. static const char *const bd28623_supply_names[BD28623_NUM_SUPPLIES] = {
  15. "VCCA",
  16. "VCCP1",
  17. "VCCP2",
  18. };
  19. struct bd28623_priv {
  20. struct device *dev;
  21. struct regulator_bulk_data supplies[BD28623_NUM_SUPPLIES];
  22. struct gpio_desc *reset_gpio;
  23. struct gpio_desc *mute_gpio;
  24. int switch_spk;
  25. };
  26. static const struct snd_soc_dapm_widget bd28623_widgets[] = {
  27. SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  28. SND_SOC_DAPM_OUTPUT("OUT1P"),
  29. SND_SOC_DAPM_OUTPUT("OUT1N"),
  30. SND_SOC_DAPM_OUTPUT("OUT2P"),
  31. SND_SOC_DAPM_OUTPUT("OUT2N"),
  32. };
  33. static const struct snd_soc_dapm_route bd28623_routes[] = {
  34. { "OUT1P", NULL, "DAC" },
  35. { "OUT1N", NULL, "DAC" },
  36. { "OUT2P", NULL, "DAC" },
  37. { "OUT2N", NULL, "DAC" },
  38. };
  39. static int bd28623_power_on(struct bd28623_priv *bd)
  40. {
  41. int ret;
  42. ret = regulator_bulk_enable(ARRAY_SIZE(bd->supplies), bd->supplies);
  43. if (ret) {
  44. dev_err(bd->dev, "Failed to enable supplies: %d\n", ret);
  45. return ret;
  46. }
  47. gpiod_set_value_cansleep(bd->reset_gpio, 0);
  48. usleep_range(300000, 400000);
  49. return 0;
  50. }
  51. static void bd28623_power_off(struct bd28623_priv *bd)
  52. {
  53. gpiod_set_value_cansleep(bd->reset_gpio, 1);
  54. regulator_bulk_disable(ARRAY_SIZE(bd->supplies), bd->supplies);
  55. }
  56. static int bd28623_get_switch_spk(struct snd_kcontrol *kcontrol,
  57. struct snd_ctl_elem_value *ucontrol)
  58. {
  59. struct snd_soc_component *component =
  60. snd_soc_kcontrol_component(kcontrol);
  61. struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
  62. ucontrol->value.integer.value[0] = bd->switch_spk;
  63. return 0;
  64. }
  65. static int bd28623_set_switch_spk(struct snd_kcontrol *kcontrol,
  66. struct snd_ctl_elem_value *ucontrol)
  67. {
  68. struct snd_soc_component *component =
  69. snd_soc_kcontrol_component(kcontrol);
  70. struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
  71. if (bd->switch_spk == ucontrol->value.integer.value[0])
  72. return 0;
  73. bd->switch_spk = ucontrol->value.integer.value[0];
  74. gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
  75. return 0;
  76. }
  77. static const struct snd_kcontrol_new bd28623_controls[] = {
  78. SOC_SINGLE_BOOL_EXT("Speaker Switch", 0,
  79. bd28623_get_switch_spk, bd28623_set_switch_spk),
  80. };
  81. static int bd28623_codec_probe(struct snd_soc_component *component)
  82. {
  83. struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
  84. int ret;
  85. bd->switch_spk = 1;
  86. ret = bd28623_power_on(bd);
  87. if (ret)
  88. return ret;
  89. gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
  90. return 0;
  91. }
  92. static void bd28623_codec_remove(struct snd_soc_component *component)
  93. {
  94. struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
  95. bd28623_power_off(bd);
  96. }
  97. static int bd28623_codec_suspend(struct snd_soc_component *component)
  98. {
  99. struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
  100. bd28623_power_off(bd);
  101. return 0;
  102. }
  103. static int bd28623_codec_resume(struct snd_soc_component *component)
  104. {
  105. struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
  106. int ret;
  107. ret = bd28623_power_on(bd);
  108. if (ret)
  109. return ret;
  110. gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
  111. return 0;
  112. }
  113. static const struct snd_soc_component_driver soc_codec_bd = {
  114. .probe = bd28623_codec_probe,
  115. .remove = bd28623_codec_remove,
  116. .suspend = bd28623_codec_suspend,
  117. .resume = bd28623_codec_resume,
  118. .dapm_widgets = bd28623_widgets,
  119. .num_dapm_widgets = ARRAY_SIZE(bd28623_widgets),
  120. .dapm_routes = bd28623_routes,
  121. .num_dapm_routes = ARRAY_SIZE(bd28623_routes),
  122. .controls = bd28623_controls,
  123. .num_controls = ARRAY_SIZE(bd28623_controls),
  124. .idle_bias_on = 1,
  125. .use_pmdown_time = 1,
  126. .endianness = 1,
  127. };
  128. static struct snd_soc_dai_driver soc_dai_bd = {
  129. .name = "bd28623-speaker",
  130. .playback = {
  131. .stream_name = "Playback",
  132. .formats = SNDRV_PCM_FMTBIT_S32_LE |
  133. SNDRV_PCM_FMTBIT_S24_LE |
  134. SNDRV_PCM_FMTBIT_S16_LE,
  135. .rates = SNDRV_PCM_RATE_48000 |
  136. SNDRV_PCM_RATE_44100 |
  137. SNDRV_PCM_RATE_32000,
  138. .channels_min = 2,
  139. .channels_max = 2,
  140. },
  141. };
  142. static int bd28623_probe(struct platform_device *pdev)
  143. {
  144. struct bd28623_priv *bd;
  145. struct device *dev = &pdev->dev;
  146. int i, ret;
  147. bd = devm_kzalloc(&pdev->dev, sizeof(struct bd28623_priv), GFP_KERNEL);
  148. if (!bd)
  149. return -ENOMEM;
  150. for (i = 0; i < ARRAY_SIZE(bd->supplies); i++)
  151. bd->supplies[i].supply = bd28623_supply_names[i];
  152. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(bd->supplies),
  153. bd->supplies);
  154. if (ret) {
  155. dev_err(dev, "Failed to get supplies: %d\n", ret);
  156. return ret;
  157. }
  158. bd->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  159. GPIOD_OUT_HIGH);
  160. if (IS_ERR(bd->reset_gpio)) {
  161. dev_err(dev, "Failed to request reset_gpio: %ld\n",
  162. PTR_ERR(bd->reset_gpio));
  163. return PTR_ERR(bd->reset_gpio);
  164. }
  165. bd->mute_gpio = devm_gpiod_get_optional(dev, "mute",
  166. GPIOD_OUT_HIGH);
  167. if (IS_ERR(bd->mute_gpio)) {
  168. dev_err(dev, "Failed to request mute_gpio: %ld\n",
  169. PTR_ERR(bd->mute_gpio));
  170. return PTR_ERR(bd->mute_gpio);
  171. }
  172. platform_set_drvdata(pdev, bd);
  173. bd->dev = dev;
  174. return devm_snd_soc_register_component(dev, &soc_codec_bd,
  175. &soc_dai_bd, 1);
  176. }
  177. static const struct of_device_id bd28623_of_match[] __maybe_unused = {
  178. { .compatible = "rohm,bd28623", },
  179. {}
  180. };
  181. MODULE_DEVICE_TABLE(of, bd28623_of_match);
  182. static struct platform_driver bd28623_codec_driver = {
  183. .driver = {
  184. .name = "bd28623",
  185. .of_match_table = of_match_ptr(bd28623_of_match),
  186. },
  187. .probe = bd28623_probe,
  188. };
  189. module_platform_driver(bd28623_codec_driver);
  190. MODULE_AUTHOR("Katsuhiro Suzuki <[email protected]>");
  191. MODULE_DESCRIPTION("ROHM BD28623 speaker amplifier driver");
  192. MODULE_LICENSE("GPL v2");