tpa6130a2.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
  4. *
  5. * Copyright (C) Nokia Corporation
  6. *
  7. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/errno.h>
  11. #include <linux/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/gpio.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/slab.h>
  16. #include <sound/tpa6130a2-plat.h>
  17. #include <sound/soc.h>
  18. #include <sound/tlv.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/regmap.h>
  22. #include "tpa6130a2.h"
  23. enum tpa_model {
  24. TPA6130A2,
  25. TPA6140A2,
  26. };
  27. /* This struct is used to save the context */
  28. struct tpa6130a2_data {
  29. struct device *dev;
  30. struct regmap *regmap;
  31. struct regulator *supply;
  32. int power_gpio;
  33. enum tpa_model id;
  34. };
  35. static int tpa6130a2_power(struct tpa6130a2_data *data, bool enable)
  36. {
  37. int ret = 0, ret2;
  38. if (enable) {
  39. ret = regulator_enable(data->supply);
  40. if (ret != 0) {
  41. dev_err(data->dev,
  42. "Failed to enable supply: %d\n", ret);
  43. return ret;
  44. }
  45. /* Power on */
  46. if (data->power_gpio >= 0)
  47. gpio_set_value(data->power_gpio, 1);
  48. /* Sync registers */
  49. regcache_cache_only(data->regmap, false);
  50. ret = regcache_sync(data->regmap);
  51. if (ret != 0) {
  52. dev_err(data->dev,
  53. "Failed to sync registers: %d\n", ret);
  54. regcache_cache_only(data->regmap, true);
  55. if (data->power_gpio >= 0)
  56. gpio_set_value(data->power_gpio, 0);
  57. ret2 = regulator_disable(data->supply);
  58. if (ret2 != 0)
  59. dev_err(data->dev,
  60. "Failed to disable supply: %d\n", ret2);
  61. return ret;
  62. }
  63. } else {
  64. /* Powered off device does not retain registers. While device
  65. * is off, any register updates (i.e. volume changes) should
  66. * happen in cache only.
  67. */
  68. regcache_mark_dirty(data->regmap);
  69. regcache_cache_only(data->regmap, true);
  70. /* Power off */
  71. if (data->power_gpio >= 0)
  72. gpio_set_value(data->power_gpio, 0);
  73. ret = regulator_disable(data->supply);
  74. if (ret != 0) {
  75. dev_err(data->dev,
  76. "Failed to disable supply: %d\n", ret);
  77. return ret;
  78. }
  79. }
  80. return ret;
  81. }
  82. static int tpa6130a2_power_event(struct snd_soc_dapm_widget *w,
  83. struct snd_kcontrol *kctrl, int event)
  84. {
  85. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  86. struct tpa6130a2_data *data = snd_soc_component_get_drvdata(c);
  87. if (SND_SOC_DAPM_EVENT_ON(event)) {
  88. /* Before widget power up: turn chip on, sync registers */
  89. return tpa6130a2_power(data, true);
  90. } else {
  91. /* After widget power down: turn chip off */
  92. return tpa6130a2_power(data, false);
  93. }
  94. }
  95. /*
  96. * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
  97. * down in gain.
  98. */
  99. static const DECLARE_TLV_DB_RANGE(tpa6130_tlv,
  100. 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
  101. 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
  102. 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
  103. 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
  104. 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
  105. 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
  106. 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
  107. 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
  108. 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
  109. 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
  110. );
  111. static const struct snd_kcontrol_new tpa6130a2_controls[] = {
  112. SOC_SINGLE_TLV("Headphone Playback Volume",
  113. TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
  114. tpa6130_tlv),
  115. };
  116. static const DECLARE_TLV_DB_RANGE(tpa6140_tlv,
  117. 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
  118. 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
  119. 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
  120. );
  121. static const struct snd_kcontrol_new tpa6140a2_controls[] = {
  122. SOC_SINGLE_TLV("Headphone Playback Volume",
  123. TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
  124. tpa6140_tlv),
  125. };
  126. static int tpa6130a2_component_probe(struct snd_soc_component *component)
  127. {
  128. struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component);
  129. if (data->id == TPA6140A2)
  130. return snd_soc_add_component_controls(component,
  131. tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls));
  132. else
  133. return snd_soc_add_component_controls(component,
  134. tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls));
  135. }
  136. static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
  137. SND_SOC_DAPM_INPUT("LEFTIN"),
  138. SND_SOC_DAPM_INPUT("RIGHTIN"),
  139. SND_SOC_DAPM_OUTPUT("HPLEFT"),
  140. SND_SOC_DAPM_OUTPUT("HPRIGHT"),
  141. SND_SOC_DAPM_PGA("Left Mute", TPA6130A2_REG_VOL_MUTE,
  142. TPA6130A2_HP_EN_L_SHIFT, 1, NULL, 0),
  143. SND_SOC_DAPM_PGA("Right Mute", TPA6130A2_REG_VOL_MUTE,
  144. TPA6130A2_HP_EN_R_SHIFT, 1, NULL, 0),
  145. SND_SOC_DAPM_PGA("Left PGA", TPA6130A2_REG_CONTROL,
  146. TPA6130A2_HP_EN_L_SHIFT, 0, NULL, 0),
  147. SND_SOC_DAPM_PGA("Right PGA", TPA6130A2_REG_CONTROL,
  148. TPA6130A2_HP_EN_R_SHIFT, 0, NULL, 0),
  149. SND_SOC_DAPM_SUPPLY("Power", TPA6130A2_REG_CONTROL,
  150. TPA6130A2_SWS_SHIFT, 1, tpa6130a2_power_event,
  151. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  152. };
  153. static const struct snd_soc_dapm_route tpa6130a2_dapm_routes[] = {
  154. { "Left PGA", NULL, "LEFTIN" },
  155. { "Right PGA", NULL, "RIGHTIN" },
  156. { "Left Mute", NULL, "Left PGA" },
  157. { "Right Mute", NULL, "Right PGA" },
  158. { "HPLEFT", NULL, "Left Mute" },
  159. { "HPRIGHT", NULL, "Right Mute" },
  160. { "Left PGA", NULL, "Power" },
  161. { "Right PGA", NULL, "Power" },
  162. };
  163. static const struct snd_soc_component_driver tpa6130a2_component_driver = {
  164. .name = "tpa6130a2",
  165. .probe = tpa6130a2_component_probe,
  166. .dapm_widgets = tpa6130a2_dapm_widgets,
  167. .num_dapm_widgets = ARRAY_SIZE(tpa6130a2_dapm_widgets),
  168. .dapm_routes = tpa6130a2_dapm_routes,
  169. .num_dapm_routes = ARRAY_SIZE(tpa6130a2_dapm_routes),
  170. };
  171. static const struct reg_default tpa6130a2_reg_defaults[] = {
  172. { TPA6130A2_REG_CONTROL, TPA6130A2_SWS },
  173. { TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L },
  174. };
  175. static const struct regmap_config tpa6130a2_regmap_config = {
  176. .reg_bits = 8,
  177. .val_bits = 8,
  178. .max_register = TPA6130A2_REG_VERSION,
  179. .reg_defaults = tpa6130a2_reg_defaults,
  180. .num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults),
  181. .cache_type = REGCACHE_RBTREE,
  182. };
  183. static int tpa6130a2_probe(struct i2c_client *client,
  184. const struct i2c_device_id *id)
  185. {
  186. struct device *dev;
  187. struct tpa6130a2_data *data;
  188. struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
  189. struct device_node *np = client->dev.of_node;
  190. const char *regulator;
  191. unsigned int version;
  192. int ret;
  193. dev = &client->dev;
  194. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  195. if (!data)
  196. return -ENOMEM;
  197. data->dev = dev;
  198. data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config);
  199. if (IS_ERR(data->regmap))
  200. return PTR_ERR(data->regmap);
  201. if (pdata) {
  202. data->power_gpio = pdata->power_gpio;
  203. } else if (np) {
  204. data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
  205. } else {
  206. dev_err(dev, "Platform data not set\n");
  207. dump_stack();
  208. return -ENODEV;
  209. }
  210. i2c_set_clientdata(client, data);
  211. data->id = id->driver_data;
  212. if (data->power_gpio >= 0) {
  213. ret = devm_gpio_request(dev, data->power_gpio,
  214. "tpa6130a2 enable");
  215. if (ret < 0) {
  216. dev_err(dev, "Failed to request power GPIO (%d)\n",
  217. data->power_gpio);
  218. return ret;
  219. }
  220. gpio_direction_output(data->power_gpio, 0);
  221. }
  222. switch (data->id) {
  223. default:
  224. dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
  225. data->id);
  226. fallthrough;
  227. case TPA6130A2:
  228. regulator = "Vdd";
  229. break;
  230. case TPA6140A2:
  231. regulator = "AVdd";
  232. break;
  233. }
  234. data->supply = devm_regulator_get(dev, regulator);
  235. if (IS_ERR(data->supply)) {
  236. ret = PTR_ERR(data->supply);
  237. dev_err(dev, "Failed to request supply: %d\n", ret);
  238. return ret;
  239. }
  240. ret = tpa6130a2_power(data, true);
  241. if (ret != 0)
  242. return ret;
  243. /* Read version */
  244. regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version);
  245. version &= TPA6130A2_VERSION_MASK;
  246. if ((version != 1) && (version != 2))
  247. dev_warn(dev, "UNTESTED version detected (%d)\n", version);
  248. /* Disable the chip */
  249. ret = tpa6130a2_power(data, false);
  250. if (ret != 0)
  251. return ret;
  252. return devm_snd_soc_register_component(&client->dev,
  253. &tpa6130a2_component_driver, NULL, 0);
  254. }
  255. static const struct i2c_device_id tpa6130a2_id[] = {
  256. { "tpa6130a2", TPA6130A2 },
  257. { "tpa6140a2", TPA6140A2 },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
  261. #if IS_ENABLED(CONFIG_OF)
  262. static const struct of_device_id tpa6130a2_of_match[] = {
  263. { .compatible = "ti,tpa6130a2", },
  264. { .compatible = "ti,tpa6140a2" },
  265. {},
  266. };
  267. MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
  268. #endif
  269. static struct i2c_driver tpa6130a2_i2c_driver = {
  270. .driver = {
  271. .name = "tpa6130a2",
  272. .of_match_table = of_match_ptr(tpa6130a2_of_match),
  273. },
  274. .probe = tpa6130a2_probe,
  275. .id_table = tpa6130a2_id,
  276. };
  277. module_i2c_driver(tpa6130a2_i2c_driver);
  278. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  279. MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
  280. MODULE_LICENSE("GPL");