tpa6130a2.c 12 KB

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