tpa6130a2.c 9.1 KB

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