tpa6130a2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_gpio.h>
  33. #include "tpa6130a2.h"
  34. enum tpa_model {
  35. TPA6130A2,
  36. TPA6140A2,
  37. };
  38. static struct i2c_client *tpa6130a2_client;
  39. /* This struct is used to save the context */
  40. struct tpa6130a2_data {
  41. struct mutex mutex;
  42. unsigned char regs[TPA6130A2_CACHEREGNUM];
  43. struct regulator *supply;
  44. int power_gpio;
  45. u8 power_state:1;
  46. enum tpa_model id;
  47. };
  48. static int tpa6130a2_i2c_read(int reg)
  49. {
  50. struct tpa6130a2_data *data;
  51. int val;
  52. BUG_ON(tpa6130a2_client == NULL);
  53. data = i2c_get_clientdata(tpa6130a2_client);
  54. /* If powered off, return the cached value */
  55. if (data->power_state) {
  56. val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
  57. if (val < 0)
  58. dev_err(&tpa6130a2_client->dev, "Read failed\n");
  59. else
  60. data->regs[reg] = val;
  61. } else {
  62. val = data->regs[reg];
  63. }
  64. return val;
  65. }
  66. static int tpa6130a2_i2c_write(int reg, u8 value)
  67. {
  68. struct tpa6130a2_data *data;
  69. int val = 0;
  70. BUG_ON(tpa6130a2_client == NULL);
  71. data = i2c_get_clientdata(tpa6130a2_client);
  72. if (data->power_state) {
  73. val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
  74. if (val < 0) {
  75. dev_err(&tpa6130a2_client->dev, "Write failed\n");
  76. return val;
  77. }
  78. }
  79. /* Either powered on or off, we save the context */
  80. data->regs[reg] = value;
  81. return val;
  82. }
  83. static u8 tpa6130a2_read(int reg)
  84. {
  85. struct tpa6130a2_data *data;
  86. BUG_ON(tpa6130a2_client == NULL);
  87. data = i2c_get_clientdata(tpa6130a2_client);
  88. return data->regs[reg];
  89. }
  90. static int tpa6130a2_initialize(void)
  91. {
  92. struct tpa6130a2_data *data;
  93. int i, ret = 0;
  94. BUG_ON(tpa6130a2_client == NULL);
  95. data = i2c_get_clientdata(tpa6130a2_client);
  96. for (i = 1; i < TPA6130A2_REG_VERSION; i++) {
  97. ret = tpa6130a2_i2c_write(i, data->regs[i]);
  98. if (ret < 0)
  99. break;
  100. }
  101. return ret;
  102. }
  103. static int tpa6130a2_power(u8 power)
  104. {
  105. struct tpa6130a2_data *data;
  106. u8 val;
  107. int ret = 0;
  108. BUG_ON(tpa6130a2_client == NULL);
  109. data = i2c_get_clientdata(tpa6130a2_client);
  110. mutex_lock(&data->mutex);
  111. if (power == data->power_state)
  112. goto exit;
  113. if (power) {
  114. ret = regulator_enable(data->supply);
  115. if (ret != 0) {
  116. dev_err(&tpa6130a2_client->dev,
  117. "Failed to enable supply: %d\n", ret);
  118. goto exit;
  119. }
  120. /* Power on */
  121. if (data->power_gpio >= 0)
  122. gpio_set_value(data->power_gpio, 1);
  123. data->power_state = 1;
  124. ret = tpa6130a2_initialize();
  125. if (ret < 0) {
  126. dev_err(&tpa6130a2_client->dev,
  127. "Failed to initialize chip\n");
  128. if (data->power_gpio >= 0)
  129. gpio_set_value(data->power_gpio, 0);
  130. regulator_disable(data->supply);
  131. data->power_state = 0;
  132. goto exit;
  133. }
  134. } else {
  135. /* set SWS */
  136. val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
  137. val |= TPA6130A2_SWS;
  138. tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
  139. /* Power off */
  140. if (data->power_gpio >= 0)
  141. gpio_set_value(data->power_gpio, 0);
  142. ret = regulator_disable(data->supply);
  143. if (ret != 0) {
  144. dev_err(&tpa6130a2_client->dev,
  145. "Failed to disable supply: %d\n", ret);
  146. goto exit;
  147. }
  148. data->power_state = 0;
  149. }
  150. exit:
  151. mutex_unlock(&data->mutex);
  152. return ret;
  153. }
  154. static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol)
  156. {
  157. struct soc_mixer_control *mc =
  158. (struct soc_mixer_control *)kcontrol->private_value;
  159. struct tpa6130a2_data *data;
  160. unsigned int reg = mc->reg;
  161. unsigned int shift = mc->shift;
  162. int max = mc->max;
  163. unsigned int mask = (1 << fls(max)) - 1;
  164. unsigned int invert = mc->invert;
  165. BUG_ON(tpa6130a2_client == NULL);
  166. data = i2c_get_clientdata(tpa6130a2_client);
  167. mutex_lock(&data->mutex);
  168. ucontrol->value.integer.value[0] =
  169. (tpa6130a2_read(reg) >> shift) & mask;
  170. if (invert)
  171. ucontrol->value.integer.value[0] =
  172. max - ucontrol->value.integer.value[0];
  173. mutex_unlock(&data->mutex);
  174. return 0;
  175. }
  176. static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_value *ucontrol)
  178. {
  179. struct soc_mixer_control *mc =
  180. (struct soc_mixer_control *)kcontrol->private_value;
  181. struct tpa6130a2_data *data;
  182. unsigned int reg = mc->reg;
  183. unsigned int shift = mc->shift;
  184. int max = mc->max;
  185. unsigned int mask = (1 << fls(max)) - 1;
  186. unsigned int invert = mc->invert;
  187. unsigned int val = (ucontrol->value.integer.value[0] & mask);
  188. unsigned int val_reg;
  189. BUG_ON(tpa6130a2_client == NULL);
  190. data = i2c_get_clientdata(tpa6130a2_client);
  191. if (invert)
  192. val = max - val;
  193. mutex_lock(&data->mutex);
  194. val_reg = tpa6130a2_read(reg);
  195. if (((val_reg >> shift) & mask) == val) {
  196. mutex_unlock(&data->mutex);
  197. return 0;
  198. }
  199. val_reg &= ~(mask << shift);
  200. val_reg |= val << shift;
  201. tpa6130a2_i2c_write(reg, val_reg);
  202. mutex_unlock(&data->mutex);
  203. return 1;
  204. }
  205. /*
  206. * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
  207. * down in gain.
  208. */
  209. static const unsigned int tpa6130_tlv[] = {
  210. TLV_DB_RANGE_HEAD(10),
  211. 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
  212. 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
  213. 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
  214. 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
  215. 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
  216. 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
  217. 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
  218. 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
  219. 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
  220. 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
  221. };
  222. static const struct snd_kcontrol_new tpa6130a2_controls[] = {
  223. SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
  224. TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
  225. tpa6130a2_get_volsw, tpa6130a2_put_volsw,
  226. tpa6130_tlv),
  227. };
  228. static const unsigned int tpa6140_tlv[] = {
  229. TLV_DB_RANGE_HEAD(3),
  230. 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
  231. 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
  232. 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
  233. };
  234. static const struct snd_kcontrol_new tpa6140a2_controls[] = {
  235. SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
  236. TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
  237. tpa6130a2_get_volsw, tpa6130a2_put_volsw,
  238. tpa6140_tlv),
  239. };
  240. /*
  241. * Enable or disable channel (left or right)
  242. * The bit number for mute and amplifier are the same per channel:
  243. * bit 6: Right channel
  244. * bit 7: Left channel
  245. * in both registers.
  246. */
  247. static void tpa6130a2_channel_enable(u8 channel, int enable)
  248. {
  249. u8 val;
  250. if (enable) {
  251. /* Enable channel */
  252. /* Enable amplifier */
  253. val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
  254. val |= channel;
  255. val &= ~TPA6130A2_SWS;
  256. tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
  257. /* Unmute channel */
  258. val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
  259. val &= ~channel;
  260. tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
  261. } else {
  262. /* Disable channel */
  263. /* Mute channel */
  264. val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
  265. val |= channel;
  266. tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
  267. /* Disable amplifier */
  268. val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
  269. val &= ~channel;
  270. tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
  271. }
  272. }
  273. int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable)
  274. {
  275. int ret = 0;
  276. if (enable) {
  277. ret = tpa6130a2_power(1);
  278. if (ret < 0)
  279. return ret;
  280. tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
  281. 1);
  282. } else {
  283. tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
  284. 0);
  285. ret = tpa6130a2_power(0);
  286. }
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable);
  290. int tpa6130a2_add_controls(struct snd_soc_codec *codec)
  291. {
  292. struct tpa6130a2_data *data;
  293. if (tpa6130a2_client == NULL)
  294. return -ENODEV;
  295. data = i2c_get_clientdata(tpa6130a2_client);
  296. if (data->id == TPA6140A2)
  297. return snd_soc_add_codec_controls(codec, tpa6140a2_controls,
  298. ARRAY_SIZE(tpa6140a2_controls));
  299. else
  300. return snd_soc_add_codec_controls(codec, tpa6130a2_controls,
  301. ARRAY_SIZE(tpa6130a2_controls));
  302. }
  303. EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
  304. static int tpa6130a2_probe(struct i2c_client *client,
  305. const struct i2c_device_id *id)
  306. {
  307. struct device *dev;
  308. struct tpa6130a2_data *data;
  309. struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
  310. struct device_node *np = client->dev.of_node;
  311. const char *regulator;
  312. int ret;
  313. dev = &client->dev;
  314. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  315. if (data == NULL) {
  316. dev_err(dev, "Can not allocate memory\n");
  317. return -ENOMEM;
  318. }
  319. if (pdata) {
  320. data->power_gpio = pdata->power_gpio;
  321. } else if (np) {
  322. data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
  323. } else {
  324. dev_err(dev, "Platform data not set\n");
  325. dump_stack();
  326. return -ENODEV;
  327. }
  328. tpa6130a2_client = client;
  329. i2c_set_clientdata(tpa6130a2_client, data);
  330. data->id = id->driver_data;
  331. mutex_init(&data->mutex);
  332. /* Set default register values */
  333. data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
  334. data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
  335. TPA6130A2_MUTE_L;
  336. if (data->power_gpio >= 0) {
  337. ret = devm_gpio_request(dev, data->power_gpio,
  338. "tpa6130a2 enable");
  339. if (ret < 0) {
  340. dev_err(dev, "Failed to request power GPIO (%d)\n",
  341. data->power_gpio);
  342. goto err_gpio;
  343. }
  344. gpio_direction_output(data->power_gpio, 0);
  345. }
  346. switch (data->id) {
  347. default:
  348. dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
  349. data->id);
  350. case TPA6130A2:
  351. regulator = "Vdd";
  352. break;
  353. case TPA6140A2:
  354. regulator = "AVdd";
  355. break;
  356. }
  357. data->supply = devm_regulator_get(dev, regulator);
  358. if (IS_ERR(data->supply)) {
  359. ret = PTR_ERR(data->supply);
  360. dev_err(dev, "Failed to request supply: %d\n", ret);
  361. goto err_gpio;
  362. }
  363. ret = tpa6130a2_power(1);
  364. if (ret != 0)
  365. goto err_gpio;
  366. /* Read version */
  367. ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
  368. TPA6130A2_VERSION_MASK;
  369. if ((ret != 1) && (ret != 2))
  370. dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
  371. /* Disable the chip */
  372. ret = tpa6130a2_power(0);
  373. if (ret != 0)
  374. goto err_gpio;
  375. return 0;
  376. err_gpio:
  377. tpa6130a2_client = NULL;
  378. return ret;
  379. }
  380. static int tpa6130a2_remove(struct i2c_client *client)
  381. {
  382. tpa6130a2_power(0);
  383. tpa6130a2_client = NULL;
  384. return 0;
  385. }
  386. static const struct i2c_device_id tpa6130a2_id[] = {
  387. { "tpa6130a2", TPA6130A2 },
  388. { "tpa6140a2", TPA6140A2 },
  389. { }
  390. };
  391. MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
  392. #if IS_ENABLED(CONFIG_OF)
  393. static const struct of_device_id tpa6130a2_of_match[] = {
  394. { .compatible = "ti,tpa6130a2", },
  395. { .compatible = "ti,tpa6140a2" },
  396. {},
  397. };
  398. MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
  399. #endif
  400. static struct i2c_driver tpa6130a2_i2c_driver = {
  401. .driver = {
  402. .name = "tpa6130a2",
  403. .owner = THIS_MODULE,
  404. .of_match_table = of_match_ptr(tpa6130a2_of_match),
  405. },
  406. .probe = tpa6130a2_probe,
  407. .remove = tpa6130a2_remove,
  408. .id_table = tpa6130a2_id,
  409. };
  410. module_i2c_driver(tpa6130a2_i2c_driver);
  411. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  412. MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
  413. MODULE_LICENSE("GPL");