max9768.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MAX9768 AMP driver
  4. *
  5. * Copyright (C) 2011, 2012 by Wolfram Sang, Pengutronix e.K.
  6. */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/slab.h>
  11. #include <linux/gpio.h>
  12. #include <linux/regmap.h>
  13. #include <sound/core.h>
  14. #include <sound/soc.h>
  15. #include <sound/tlv.h>
  16. #include <sound/max9768.h>
  17. /* "Registers" */
  18. #define MAX9768_VOL 0
  19. #define MAX9768_CTRL 3
  20. /* Commands */
  21. #define MAX9768_CTRL_PWM 0x15
  22. #define MAX9768_CTRL_FILTERLESS 0x16
  23. struct max9768 {
  24. struct regmap *regmap;
  25. int mute_gpio;
  26. int shdn_gpio;
  27. u32 flags;
  28. };
  29. static const struct reg_default max9768_default_regs[] = {
  30. { 0, 0 },
  31. { 3, MAX9768_CTRL_FILTERLESS},
  32. };
  33. static int max9768_get_gpio(struct snd_kcontrol *kcontrol,
  34. struct snd_ctl_elem_value *ucontrol)
  35. {
  36. struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
  37. struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
  38. int val = gpio_get_value_cansleep(max9768->mute_gpio);
  39. ucontrol->value.integer.value[0] = !val;
  40. return 0;
  41. }
  42. static int max9768_set_gpio(struct snd_kcontrol *kcontrol,
  43. struct snd_ctl_elem_value *ucontrol)
  44. {
  45. struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
  46. struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
  47. gpio_set_value_cansleep(max9768->mute_gpio, !ucontrol->value.integer.value[0]);
  48. return 0;
  49. }
  50. static const DECLARE_TLV_DB_RANGE(volume_tlv,
  51. 0, 0, TLV_DB_SCALE_ITEM(-16150, 0, 0),
  52. 1, 1, TLV_DB_SCALE_ITEM(-9280, 0, 0),
  53. 2, 2, TLV_DB_SCALE_ITEM(-9030, 0, 0),
  54. 3, 3, TLV_DB_SCALE_ITEM(-8680, 0, 0),
  55. 4, 4, TLV_DB_SCALE_ITEM(-8430, 0, 0),
  56. 5, 5, TLV_DB_SCALE_ITEM(-8080, 0, 0),
  57. 6, 6, TLV_DB_SCALE_ITEM(-7830, 0, 0),
  58. 7, 7, TLV_DB_SCALE_ITEM(-7470, 0, 0),
  59. 8, 8, TLV_DB_SCALE_ITEM(-7220, 0, 0),
  60. 9, 9, TLV_DB_SCALE_ITEM(-6870, 0, 0),
  61. 10, 10, TLV_DB_SCALE_ITEM(-6620, 0, 0),
  62. 11, 11, TLV_DB_SCALE_ITEM(-6270, 0, 0),
  63. 12, 12, TLV_DB_SCALE_ITEM(-6020, 0, 0),
  64. 13, 13, TLV_DB_SCALE_ITEM(-5670, 0, 0),
  65. 14, 14, TLV_DB_SCALE_ITEM(-5420, 0, 0),
  66. 15, 17, TLV_DB_SCALE_ITEM(-5060, 250, 0),
  67. 18, 18, TLV_DB_SCALE_ITEM(-4370, 0, 0),
  68. 19, 19, TLV_DB_SCALE_ITEM(-4210, 0, 0),
  69. 20, 20, TLV_DB_SCALE_ITEM(-3960, 0, 0),
  70. 21, 21, TLV_DB_SCALE_ITEM(-3760, 0, 0),
  71. 22, 22, TLV_DB_SCALE_ITEM(-3600, 0, 0),
  72. 23, 23, TLV_DB_SCALE_ITEM(-3340, 0, 0),
  73. 24, 24, TLV_DB_SCALE_ITEM(-3150, 0, 0),
  74. 25, 25, TLV_DB_SCALE_ITEM(-2980, 0, 0),
  75. 26, 26, TLV_DB_SCALE_ITEM(-2720, 0, 0),
  76. 27, 27, TLV_DB_SCALE_ITEM(-2520, 0, 0),
  77. 28, 30, TLV_DB_SCALE_ITEM(-2350, 190, 0),
  78. 31, 31, TLV_DB_SCALE_ITEM(-1750, 0, 0),
  79. 32, 34, TLV_DB_SCALE_ITEM(-1640, 100, 0),
  80. 35, 37, TLV_DB_SCALE_ITEM(-1310, 110, 0),
  81. 38, 39, TLV_DB_SCALE_ITEM(-990, 100, 0),
  82. 40, 40, TLV_DB_SCALE_ITEM(-710, 0, 0),
  83. 41, 41, TLV_DB_SCALE_ITEM(-600, 0, 0),
  84. 42, 42, TLV_DB_SCALE_ITEM(-500, 0, 0),
  85. 43, 43, TLV_DB_SCALE_ITEM(-340, 0, 0),
  86. 44, 44, TLV_DB_SCALE_ITEM(-190, 0, 0),
  87. 45, 45, TLV_DB_SCALE_ITEM(-50, 0, 0),
  88. 46, 46, TLV_DB_SCALE_ITEM(50, 0, 0),
  89. 47, 50, TLV_DB_SCALE_ITEM(120, 40, 0),
  90. 51, 57, TLV_DB_SCALE_ITEM(290, 50, 0),
  91. 58, 58, TLV_DB_SCALE_ITEM(650, 0, 0),
  92. 59, 62, TLV_DB_SCALE_ITEM(700, 60, 0),
  93. 63, 63, TLV_DB_SCALE_ITEM(950, 0, 0)
  94. );
  95. static const struct snd_kcontrol_new max9768_volume[] = {
  96. SOC_SINGLE_TLV("Playback Volume", MAX9768_VOL, 0, 63, 0, volume_tlv),
  97. };
  98. static const struct snd_kcontrol_new max9768_mute[] = {
  99. SOC_SINGLE_BOOL_EXT("Playback Switch", 0, max9768_get_gpio, max9768_set_gpio),
  100. };
  101. static const struct snd_soc_dapm_widget max9768_dapm_widgets[] = {
  102. SND_SOC_DAPM_INPUT("IN"),
  103. SND_SOC_DAPM_OUTPUT("OUT+"),
  104. SND_SOC_DAPM_OUTPUT("OUT-"),
  105. };
  106. static const struct snd_soc_dapm_route max9768_dapm_routes[] = {
  107. { "OUT+", NULL, "IN" },
  108. { "OUT-", NULL, "IN" },
  109. };
  110. static int max9768_probe(struct snd_soc_component *component)
  111. {
  112. struct max9768 *max9768 = snd_soc_component_get_drvdata(component);
  113. int ret;
  114. if (max9768->flags & MAX9768_FLAG_CLASSIC_PWM) {
  115. ret = regmap_write(max9768->regmap, MAX9768_CTRL,
  116. MAX9768_CTRL_PWM);
  117. if (ret)
  118. return ret;
  119. }
  120. if (gpio_is_valid(max9768->mute_gpio)) {
  121. ret = snd_soc_add_component_controls(component, max9768_mute,
  122. ARRAY_SIZE(max9768_mute));
  123. if (ret)
  124. return ret;
  125. }
  126. return 0;
  127. }
  128. static const struct snd_soc_component_driver max9768_component_driver = {
  129. .probe = max9768_probe,
  130. .controls = max9768_volume,
  131. .num_controls = ARRAY_SIZE(max9768_volume),
  132. .dapm_widgets = max9768_dapm_widgets,
  133. .num_dapm_widgets = ARRAY_SIZE(max9768_dapm_widgets),
  134. .dapm_routes = max9768_dapm_routes,
  135. .num_dapm_routes = ARRAY_SIZE(max9768_dapm_routes),
  136. };
  137. static const struct regmap_config max9768_i2c_regmap_config = {
  138. .reg_bits = 2,
  139. .val_bits = 6,
  140. .max_register = 3,
  141. .reg_defaults = max9768_default_regs,
  142. .num_reg_defaults = ARRAY_SIZE(max9768_default_regs),
  143. .cache_type = REGCACHE_RBTREE,
  144. };
  145. static int max9768_i2c_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id)
  147. {
  148. struct max9768 *max9768;
  149. struct max9768_pdata *pdata = client->dev.platform_data;
  150. int err;
  151. max9768 = devm_kzalloc(&client->dev, sizeof(*max9768), GFP_KERNEL);
  152. if (!max9768)
  153. return -ENOMEM;
  154. if (pdata) {
  155. /* Mute on powerup to avoid clicks */
  156. err = devm_gpio_request_one(&client->dev, pdata->mute_gpio,
  157. GPIOF_INIT_HIGH, "MAX9768 Mute");
  158. max9768->mute_gpio = err ?: pdata->mute_gpio;
  159. /* Activate chip by releasing shutdown, enables I2C */
  160. err = devm_gpio_request_one(&client->dev, pdata->shdn_gpio,
  161. GPIOF_INIT_HIGH, "MAX9768 Shutdown");
  162. max9768->shdn_gpio = err ?: pdata->shdn_gpio;
  163. max9768->flags = pdata->flags;
  164. } else {
  165. max9768->shdn_gpio = -EINVAL;
  166. max9768->mute_gpio = -EINVAL;
  167. }
  168. i2c_set_clientdata(client, max9768);
  169. max9768->regmap = devm_regmap_init_i2c(client, &max9768_i2c_regmap_config);
  170. if (IS_ERR(max9768->regmap))
  171. return PTR_ERR(max9768->regmap);
  172. return devm_snd_soc_register_component(&client->dev,
  173. &max9768_component_driver, NULL, 0);
  174. }
  175. static const struct i2c_device_id max9768_i2c_id[] = {
  176. { "max9768", 0 },
  177. { }
  178. };
  179. MODULE_DEVICE_TABLE(i2c, max9768_i2c_id);
  180. static struct i2c_driver max9768_i2c_driver = {
  181. .driver = {
  182. .name = "max9768",
  183. },
  184. .probe = max9768_i2c_probe,
  185. .id_table = max9768_i2c_id,
  186. };
  187. module_i2c_driver(max9768_i2c_driver);
  188. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
  189. MODULE_DESCRIPTION("ASoC MAX9768 amplifier driver");
  190. MODULE_LICENSE("GPL v2");