bd28623.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. .non_legacy_dai_naming = 1,
  128. };
  129. static struct snd_soc_dai_driver soc_dai_bd = {
  130. .name = "bd28623-speaker",
  131. .playback = {
  132. .stream_name = "Playback",
  133. .formats = SNDRV_PCM_FMTBIT_S32_LE |
  134. SNDRV_PCM_FMTBIT_S24_LE |
  135. SNDRV_PCM_FMTBIT_S16_LE,
  136. .rates = SNDRV_PCM_RATE_48000 |
  137. SNDRV_PCM_RATE_44100 |
  138. SNDRV_PCM_RATE_32000,
  139. .channels_min = 2,
  140. .channels_max = 2,
  141. },
  142. };
  143. static int bd28623_probe(struct platform_device *pdev)
  144. {
  145. struct bd28623_priv *bd;
  146. struct device *dev = &pdev->dev;
  147. int i, ret;
  148. bd = devm_kzalloc(&pdev->dev, sizeof(struct bd28623_priv), GFP_KERNEL);
  149. if (!bd)
  150. return -ENOMEM;
  151. for (i = 0; i < ARRAY_SIZE(bd->supplies); i++)
  152. bd->supplies[i].supply = bd28623_supply_names[i];
  153. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(bd->supplies),
  154. bd->supplies);
  155. if (ret) {
  156. dev_err(dev, "Failed to get supplies: %d\n", ret);
  157. return ret;
  158. }
  159. bd->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  160. GPIOD_OUT_HIGH);
  161. if (IS_ERR(bd->reset_gpio)) {
  162. dev_err(dev, "Failed to request reset_gpio: %ld\n",
  163. PTR_ERR(bd->reset_gpio));
  164. return PTR_ERR(bd->reset_gpio);
  165. }
  166. bd->mute_gpio = devm_gpiod_get_optional(dev, "mute",
  167. GPIOD_OUT_HIGH);
  168. if (IS_ERR(bd->mute_gpio)) {
  169. dev_err(dev, "Failed to request mute_gpio: %ld\n",
  170. PTR_ERR(bd->mute_gpio));
  171. return PTR_ERR(bd->mute_gpio);
  172. }
  173. platform_set_drvdata(pdev, bd);
  174. bd->dev = dev;
  175. return devm_snd_soc_register_component(dev, &soc_codec_bd,
  176. &soc_dai_bd, 1);
  177. }
  178. static const struct of_device_id bd28623_of_match[] = {
  179. { .compatible = "rohm,bd28623", },
  180. {}
  181. };
  182. MODULE_DEVICE_TABLE(of, bd28623_of_match);
  183. static struct platform_driver bd28623_codec_driver = {
  184. .driver = {
  185. .name = "bd28623",
  186. .of_match_table = of_match_ptr(bd28623_of_match),
  187. },
  188. .probe = bd28623_probe,
  189. };
  190. module_platform_driver(bd28623_codec_driver);
  191. MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
  192. MODULE_DESCRIPTION("ROHM BD28623 speaker amplifier driver");
  193. MODULE_LICENSE("GPL v2");