tpa6130a2.c 13 KB

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