tpa6130a2.c 12 KB

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