tpa6130a2.c 12 KB

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