tpa6130a2.c 13 KB

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