tpa6130a2.c 12 KB

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