tpa6130a2.c 13 KB

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