tas5086.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * TAS5086 ASoC codec driver
  3. *
  4. * Copyright (c) 2013 Daniel Mack <zonque@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * TODO:
  17. * - implement DAPM and input muxing
  18. * - implement modulation limit
  19. * - implement non-default PWM start
  20. *
  21. * Note that this chip has a very unusual register layout, specifically
  22. * because the registers are of unequal size, and multi-byte registers
  23. * require bulk writes to take effect. Regmap does not support that kind
  24. * of devices.
  25. *
  26. * Currently, the driver does not touch any of the registers >= 0x20, so
  27. * it doesn't matter because the entire map can be accessed as 8-bit
  28. * array. In case more features will be added in the future
  29. * that require access to higher registers, the entire regmap H/W I/O
  30. * routines have to be open-coded.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <linux/delay.h>
  35. #include <linux/gpio.h>
  36. #include <linux/i2c.h>
  37. #include <linux/regmap.h>
  38. #include <linux/spi/spi.h>
  39. #include <linux/of_device.h>
  40. #include <linux/of_gpio.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/soc.h>
  44. #include <sound/tlv.h>
  45. #include <sound/tas5086.h>
  46. #define TAS5086_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  47. SNDRV_PCM_FMTBIT_S20_3LE | \
  48. SNDRV_PCM_FMTBIT_S24_3LE)
  49. #define TAS5086_PCM_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  50. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
  51. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
  52. SNDRV_PCM_RATE_192000)
  53. /*
  54. * TAS5086 registers
  55. */
  56. #define TAS5086_CLOCK_CONTROL 0x00 /* Clock control register */
  57. #define TAS5086_CLOCK_RATE(val) (val << 5)
  58. #define TAS5086_CLOCK_RATE_MASK (0x7 << 5)
  59. #define TAS5086_CLOCK_RATIO(val) (val << 2)
  60. #define TAS5086_CLOCK_RATIO_MASK (0x7 << 2)
  61. #define TAS5086_CLOCK_SCLK_RATIO_48 (1 << 1)
  62. #define TAS5086_CLOCK_VALID (1 << 0)
  63. #define TAS5086_DEEMPH_MASK 0x03
  64. #define TAS5086_SOFT_MUTE_ALL 0x3f
  65. #define TAS5086_DEV_ID 0x01 /* Device ID register */
  66. #define TAS5086_ERROR_STATUS 0x02 /* Error status register */
  67. #define TAS5086_SYS_CONTROL_1 0x03 /* System control register 1 */
  68. #define TAS5086_SERIAL_DATA_IF 0x04 /* Serial data interface register */
  69. #define TAS5086_SYS_CONTROL_2 0x05 /* System control register 2 */
  70. #define TAS5086_SOFT_MUTE 0x06 /* Soft mute register */
  71. #define TAS5086_MASTER_VOL 0x07 /* Master volume */
  72. #define TAS5086_CHANNEL_VOL(X) (0x08 + (X)) /* Channel 1-6 volume */
  73. #define TAS5086_VOLUME_CONTROL 0x09 /* Volume control register */
  74. #define TAS5086_MOD_LIMIT 0x10 /* Modulation limit register */
  75. #define TAS5086_PWM_START 0x18 /* PWM start register */
  76. #define TAS5086_SURROUND 0x19 /* Surround register */
  77. #define TAS5086_SPLIT_CAP_CHARGE 0x1a /* Split cap charge period register */
  78. #define TAS5086_OSC_TRIM 0x1b /* Oscillator trim register */
  79. #define TAS5086_BKNDERR 0x1c
  80. #define TAS5086_INPUT_MUX 0x20
  81. #define TAS5086_PWM_OUTPUT_MUX 0x25
  82. #define TAS5086_MAX_REGISTER TAS5086_PWM_OUTPUT_MUX
  83. /*
  84. * Default TAS5086 power-up configuration
  85. */
  86. static const struct reg_default tas5086_reg_defaults[] = {
  87. { 0x00, 0x6c },
  88. { 0x01, 0x03 },
  89. { 0x02, 0x00 },
  90. { 0x03, 0xa0 },
  91. { 0x04, 0x05 },
  92. { 0x05, 0x60 },
  93. { 0x06, 0x00 },
  94. { 0x07, 0xff },
  95. { 0x08, 0x30 },
  96. { 0x09, 0x30 },
  97. { 0x0a, 0x30 },
  98. { 0x0b, 0x30 },
  99. { 0x0c, 0x30 },
  100. { 0x0d, 0x30 },
  101. { 0x0e, 0xb1 },
  102. { 0x0f, 0x00 },
  103. { 0x10, 0x02 },
  104. { 0x11, 0x00 },
  105. { 0x12, 0x00 },
  106. { 0x13, 0x00 },
  107. { 0x14, 0x00 },
  108. { 0x15, 0x00 },
  109. { 0x16, 0x00 },
  110. { 0x17, 0x00 },
  111. { 0x18, 0x3f },
  112. { 0x19, 0x00 },
  113. { 0x1a, 0x18 },
  114. { 0x1b, 0x82 },
  115. { 0x1c, 0x05 },
  116. };
  117. static int tas5086_register_size(struct device *dev, unsigned int reg)
  118. {
  119. switch (reg) {
  120. case TAS5086_DEV_ID ... TAS5086_BKNDERR:
  121. return 1;
  122. case TAS5086_INPUT_MUX:
  123. case TAS5086_PWM_OUTPUT_MUX:
  124. return 4;
  125. }
  126. dev_err(dev, "Unsupported register address: %d\n", reg);
  127. return 0;
  128. }
  129. static bool tas5086_accessible_reg(struct device *dev, unsigned int reg)
  130. {
  131. switch (reg) {
  132. case 0x0f:
  133. case 0x11 ... 0x17:
  134. case 0x1d ... 0x1f:
  135. return false;
  136. default:
  137. return true;
  138. }
  139. }
  140. static bool tas5086_volatile_reg(struct device *dev, unsigned int reg)
  141. {
  142. switch (reg) {
  143. case TAS5086_DEV_ID:
  144. case TAS5086_ERROR_STATUS:
  145. return true;
  146. }
  147. return false;
  148. }
  149. static bool tas5086_writeable_reg(struct device *dev, unsigned int reg)
  150. {
  151. return tas5086_accessible_reg(dev, reg) && (reg != TAS5086_DEV_ID);
  152. }
  153. static int tas5086_reg_write(void *context, unsigned int reg,
  154. unsigned int value)
  155. {
  156. struct i2c_client *client = context;
  157. unsigned int i, size;
  158. uint8_t buf[5];
  159. int ret;
  160. size = tas5086_register_size(&client->dev, reg);
  161. if (size == 0)
  162. return -EINVAL;
  163. buf[0] = reg;
  164. for (i = size; i >= 1; --i) {
  165. buf[i] = value;
  166. value >>= 8;
  167. }
  168. ret = i2c_master_send(client, buf, size + 1);
  169. if (ret == size + 1)
  170. return 0;
  171. else if (ret < 0)
  172. return ret;
  173. else
  174. return -EIO;
  175. }
  176. static int tas5086_reg_read(void *context, unsigned int reg,
  177. unsigned int *value)
  178. {
  179. struct i2c_client *client = context;
  180. uint8_t send_buf, recv_buf[4];
  181. struct i2c_msg msgs[2];
  182. unsigned int size;
  183. unsigned int i;
  184. int ret;
  185. size = tas5086_register_size(&client->dev, reg);
  186. if (size == 0)
  187. return -EINVAL;
  188. send_buf = reg;
  189. msgs[0].addr = client->addr;
  190. msgs[0].len = sizeof(send_buf);
  191. msgs[0].buf = &send_buf;
  192. msgs[0].flags = 0;
  193. msgs[1].addr = client->addr;
  194. msgs[1].len = size;
  195. msgs[1].buf = recv_buf;
  196. msgs[1].flags = I2C_M_RD;
  197. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  198. if (ret < 0)
  199. return ret;
  200. else if (ret != ARRAY_SIZE(msgs))
  201. return -EIO;
  202. *value = 0;
  203. for (i = 0; i < size; i++) {
  204. *value <<= 8;
  205. *value |= recv_buf[i];
  206. }
  207. return 0;
  208. }
  209. struct tas5086_private {
  210. struct regmap *regmap;
  211. unsigned int mclk, sclk;
  212. unsigned int format;
  213. bool deemph;
  214. /* Current sample rate for de-emphasis control */
  215. int rate;
  216. /* GPIO driving Reset pin, if any */
  217. int gpio_nreset;
  218. };
  219. static int tas5086_deemph[] = { 0, 32000, 44100, 48000 };
  220. static int tas5086_set_deemph(struct snd_soc_codec *codec)
  221. {
  222. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  223. int i, val = 0;
  224. if (priv->deemph)
  225. for (i = 0; i < ARRAY_SIZE(tas5086_deemph); i++)
  226. if (tas5086_deemph[i] == priv->rate)
  227. val = i;
  228. return regmap_update_bits(priv->regmap, TAS5086_SYS_CONTROL_1,
  229. TAS5086_DEEMPH_MASK, val);
  230. }
  231. static int tas5086_get_deemph(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_value *ucontrol)
  233. {
  234. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  235. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  236. ucontrol->value.enumerated.item[0] = priv->deemph;
  237. return 0;
  238. }
  239. static int tas5086_put_deemph(struct snd_kcontrol *kcontrol,
  240. struct snd_ctl_elem_value *ucontrol)
  241. {
  242. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  243. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  244. priv->deemph = ucontrol->value.enumerated.item[0];
  245. return tas5086_set_deemph(codec);
  246. }
  247. static int tas5086_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  248. int clk_id, unsigned int freq, int dir)
  249. {
  250. struct snd_soc_codec *codec = codec_dai->codec;
  251. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  252. switch (clk_id) {
  253. case TAS5086_CLK_IDX_MCLK:
  254. priv->mclk = freq;
  255. break;
  256. case TAS5086_CLK_IDX_SCLK:
  257. priv->sclk = freq;
  258. break;
  259. }
  260. return 0;
  261. }
  262. static int tas5086_set_dai_fmt(struct snd_soc_dai *codec_dai,
  263. unsigned int format)
  264. {
  265. struct snd_soc_codec *codec = codec_dai->codec;
  266. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  267. /* The TAS5086 can only be slave to all clocks */
  268. if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
  269. dev_err(codec->dev, "Invalid clocking mode\n");
  270. return -EINVAL;
  271. }
  272. /* we need to refer to the data format from hw_params() */
  273. priv->format = format;
  274. return 0;
  275. }
  276. static const int tas5086_sample_rates[] = {
  277. 32000, 38000, 44100, 48000, 88200, 96000, 176400, 192000
  278. };
  279. static const int tas5086_ratios[] = {
  280. 64, 128, 192, 256, 384, 512
  281. };
  282. static int index_in_array(const int *array, int len, int needle)
  283. {
  284. int i;
  285. for (i = 0; i < len; i++)
  286. if (array[i] == needle)
  287. return i;
  288. return -ENOENT;
  289. }
  290. static int tas5086_hw_params(struct snd_pcm_substream *substream,
  291. struct snd_pcm_hw_params *params,
  292. struct snd_soc_dai *dai)
  293. {
  294. struct snd_soc_codec *codec = dai->codec;
  295. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  296. int val;
  297. int ret;
  298. priv->rate = params_rate(params);
  299. /* Look up the sample rate and refer to the offset in the list */
  300. val = index_in_array(tas5086_sample_rates,
  301. ARRAY_SIZE(tas5086_sample_rates), priv->rate);
  302. if (val < 0) {
  303. dev_err(codec->dev, "Invalid sample rate\n");
  304. return -EINVAL;
  305. }
  306. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  307. TAS5086_CLOCK_RATE_MASK,
  308. TAS5086_CLOCK_RATE(val));
  309. if (ret < 0)
  310. return ret;
  311. /* MCLK / Fs ratio */
  312. val = index_in_array(tas5086_ratios, ARRAY_SIZE(tas5086_ratios),
  313. priv->mclk / priv->rate);
  314. if (val < 0) {
  315. dev_err(codec->dev, "Inavlid MCLK / Fs ratio\n");
  316. return -EINVAL;
  317. }
  318. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  319. TAS5086_CLOCK_RATIO_MASK,
  320. TAS5086_CLOCK_RATIO(val));
  321. if (ret < 0)
  322. return ret;
  323. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  324. TAS5086_CLOCK_SCLK_RATIO_48,
  325. (priv->sclk == 48 * priv->rate) ?
  326. TAS5086_CLOCK_SCLK_RATIO_48 : 0);
  327. if (ret < 0)
  328. return ret;
  329. /*
  330. * The chip has a very unituitive register mapping and muxes information
  331. * about data format and sample depth into the same register, but not on
  332. * a logical bit-boundary. Hence, we have to refer to the format passed
  333. * in the set_dai_fmt() callback and set up everything from here.
  334. *
  335. * First, determine the 'base' value, using the format ...
  336. */
  337. switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
  338. case SND_SOC_DAIFMT_RIGHT_J:
  339. val = 0x00;
  340. break;
  341. case SND_SOC_DAIFMT_I2S:
  342. val = 0x03;
  343. break;
  344. case SND_SOC_DAIFMT_LEFT_J:
  345. val = 0x06;
  346. break;
  347. default:
  348. dev_err(codec->dev, "Invalid DAI format\n");
  349. return -EINVAL;
  350. }
  351. /* ... then add the offset for the sample bit depth. */
  352. switch (params_format(params)) {
  353. case SNDRV_PCM_FORMAT_S16_LE:
  354. val += 0;
  355. break;
  356. case SNDRV_PCM_FORMAT_S20_3LE:
  357. val += 1;
  358. break;
  359. case SNDRV_PCM_FORMAT_S24_3LE:
  360. val += 2;
  361. break;
  362. default:
  363. dev_err(codec->dev, "Invalid bit width\n");
  364. return -EINVAL;
  365. };
  366. ret = regmap_write(priv->regmap, TAS5086_SERIAL_DATA_IF, val);
  367. if (ret < 0)
  368. return ret;
  369. /* clock is considered valid now */
  370. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  371. TAS5086_CLOCK_VALID, TAS5086_CLOCK_VALID);
  372. if (ret < 0)
  373. return ret;
  374. return tas5086_set_deemph(codec);
  375. }
  376. static int tas5086_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
  377. {
  378. struct snd_soc_codec *codec = dai->codec;
  379. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  380. unsigned int val = 0;
  381. if (mute)
  382. val = TAS5086_SOFT_MUTE_ALL;
  383. return regmap_write(priv->regmap, TAS5086_SOFT_MUTE, val);
  384. }
  385. /* TAS5086 controls */
  386. static const DECLARE_TLV_DB_SCALE(tas5086_dac_tlv, -10350, 50, 1);
  387. static const struct snd_kcontrol_new tas5086_controls[] = {
  388. SOC_SINGLE_TLV("Master Playback Volume", TAS5086_MASTER_VOL,
  389. 0, 0xff, 1, tas5086_dac_tlv),
  390. SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume",
  391. TAS5086_CHANNEL_VOL(0), TAS5086_CHANNEL_VOL(1),
  392. 0, 0xff, 1, tas5086_dac_tlv),
  393. SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume",
  394. TAS5086_CHANNEL_VOL(2), TAS5086_CHANNEL_VOL(3),
  395. 0, 0xff, 1, tas5086_dac_tlv),
  396. SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume",
  397. TAS5086_CHANNEL_VOL(4), TAS5086_CHANNEL_VOL(5),
  398. 0, 0xff, 1, tas5086_dac_tlv),
  399. SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
  400. tas5086_get_deemph, tas5086_put_deemph),
  401. };
  402. static const struct snd_soc_dai_ops tas5086_dai_ops = {
  403. .hw_params = tas5086_hw_params,
  404. .set_sysclk = tas5086_set_dai_sysclk,
  405. .set_fmt = tas5086_set_dai_fmt,
  406. .mute_stream = tas5086_mute_stream,
  407. };
  408. static struct snd_soc_dai_driver tas5086_dai = {
  409. .name = "tas5086-hifi",
  410. .playback = {
  411. .stream_name = "Playback",
  412. .channels_min = 2,
  413. .channels_max = 6,
  414. .rates = TAS5086_PCM_RATES,
  415. .formats = TAS5086_PCM_FORMATS,
  416. },
  417. .ops = &tas5086_dai_ops,
  418. };
  419. #ifdef CONFIG_PM
  420. static int tas5086_soc_resume(struct snd_soc_codec *codec)
  421. {
  422. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  423. /* Restore codec state */
  424. return regcache_sync(priv->regmap);
  425. }
  426. #else
  427. #define tas5086_soc_resume NULL
  428. #endif /* CONFIG_PM */
  429. #ifdef CONFIG_OF
  430. static const struct of_device_id tas5086_dt_ids[] = {
  431. { .compatible = "ti,tas5086", },
  432. { }
  433. };
  434. MODULE_DEVICE_TABLE(of, tas5086_dt_ids);
  435. #endif
  436. /* charge period values in microseconds */
  437. static const int tas5086_charge_period[] = {
  438. 13000, 16900, 23400, 31200, 41600, 54600, 72800, 96200,
  439. 130000, 156000, 234000, 312000, 416000, 546000, 728000, 962000,
  440. 1300000, 169000, 2340000, 3120000, 4160000, 5460000, 7280000, 9620000,
  441. };
  442. static int tas5086_probe(struct snd_soc_codec *codec)
  443. {
  444. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  445. int charge_period = 1300000; /* hardware default is 1300 ms */
  446. int i, ret;
  447. if (of_match_device(of_match_ptr(tas5086_dt_ids), codec->dev)) {
  448. struct device_node *of_node = codec->dev->of_node;
  449. of_property_read_u32(of_node, "ti,charge-period", &charge_period);
  450. }
  451. /* lookup and set split-capacitor charge period */
  452. if (charge_period == 0) {
  453. regmap_write(priv->regmap, TAS5086_SPLIT_CAP_CHARGE, 0);
  454. } else {
  455. i = index_in_array(tas5086_charge_period,
  456. ARRAY_SIZE(tas5086_charge_period),
  457. charge_period);
  458. if (i >= 0)
  459. regmap_write(priv->regmap, TAS5086_SPLIT_CAP_CHARGE,
  460. i + 0x08);
  461. else
  462. dev_warn(codec->dev,
  463. "Invalid split-cap charge period of %d ns.\n",
  464. charge_period);
  465. }
  466. /* enable factory trim */
  467. ret = regmap_write(priv->regmap, TAS5086_OSC_TRIM, 0x00);
  468. if (ret < 0)
  469. return ret;
  470. /* start all channels */
  471. ret = regmap_write(priv->regmap, TAS5086_SYS_CONTROL_2, 0x20);
  472. if (ret < 0)
  473. return ret;
  474. /* set master volume to 0 dB */
  475. ret = regmap_write(priv->regmap, TAS5086_MASTER_VOL, 0x30);
  476. if (ret < 0)
  477. return ret;
  478. /* mute all channels for now */
  479. ret = regmap_write(priv->regmap, TAS5086_SOFT_MUTE,
  480. TAS5086_SOFT_MUTE_ALL);
  481. if (ret < 0)
  482. return ret;
  483. return 0;
  484. }
  485. static int tas5086_remove(struct snd_soc_codec *codec)
  486. {
  487. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  488. if (gpio_is_valid(priv->gpio_nreset))
  489. /* Set codec to the reset state */
  490. gpio_set_value(priv->gpio_nreset, 0);
  491. return 0;
  492. };
  493. static struct snd_soc_codec_driver soc_codec_dev_tas5086 = {
  494. .probe = tas5086_probe,
  495. .remove = tas5086_remove,
  496. .resume = tas5086_soc_resume,
  497. .controls = tas5086_controls,
  498. .num_controls = ARRAY_SIZE(tas5086_controls),
  499. };
  500. static const struct i2c_device_id tas5086_i2c_id[] = {
  501. { "tas5086", 0 },
  502. { }
  503. };
  504. MODULE_DEVICE_TABLE(i2c, tas5086_i2c_id);
  505. static const struct regmap_config tas5086_regmap = {
  506. .reg_bits = 8,
  507. .val_bits = 32,
  508. .max_register = TAS5086_MAX_REGISTER,
  509. .reg_defaults = tas5086_reg_defaults,
  510. .num_reg_defaults = ARRAY_SIZE(tas5086_reg_defaults),
  511. .cache_type = REGCACHE_RBTREE,
  512. .volatile_reg = tas5086_volatile_reg,
  513. .writeable_reg = tas5086_writeable_reg,
  514. .readable_reg = tas5086_accessible_reg,
  515. .reg_read = tas5086_reg_read,
  516. .reg_write = tas5086_reg_write,
  517. };
  518. static int tas5086_i2c_probe(struct i2c_client *i2c,
  519. const struct i2c_device_id *id)
  520. {
  521. struct tas5086_private *priv;
  522. struct device *dev = &i2c->dev;
  523. int gpio_nreset = -EINVAL;
  524. int i, ret;
  525. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  526. if (!priv)
  527. return -ENOMEM;
  528. priv->regmap = devm_regmap_init(dev, NULL, i2c, &tas5086_regmap);
  529. if (IS_ERR(priv->regmap)) {
  530. ret = PTR_ERR(priv->regmap);
  531. dev_err(&i2c->dev, "Failed to create regmap: %d\n", ret);
  532. return ret;
  533. }
  534. i2c_set_clientdata(i2c, priv);
  535. if (of_match_device(of_match_ptr(tas5086_dt_ids), dev)) {
  536. struct device_node *of_node = dev->of_node;
  537. gpio_nreset = of_get_named_gpio(of_node, "reset-gpio", 0);
  538. }
  539. if (gpio_is_valid(gpio_nreset))
  540. if (devm_gpio_request(dev, gpio_nreset, "TAS5086 Reset"))
  541. gpio_nreset = -EINVAL;
  542. if (gpio_is_valid(gpio_nreset)) {
  543. /* Reset codec - minimum assertion time is 400ns */
  544. gpio_direction_output(gpio_nreset, 0);
  545. udelay(1);
  546. gpio_set_value(gpio_nreset, 1);
  547. /* Codec needs ~15ms to wake up */
  548. msleep(15);
  549. }
  550. priv->gpio_nreset = gpio_nreset;
  551. /* The TAS5086 always returns 0x03 in its TAS5086_DEV_ID register */
  552. ret = regmap_read(priv->regmap, TAS5086_DEV_ID, &i);
  553. if (ret < 0)
  554. return ret;
  555. if (i != 0x3) {
  556. dev_err(dev,
  557. "Failed to identify TAS5086 codec (got %02x)\n", i);
  558. return -ENODEV;
  559. }
  560. return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_tas5086,
  561. &tas5086_dai, 1);
  562. }
  563. static int tas5086_i2c_remove(struct i2c_client *i2c)
  564. {
  565. snd_soc_unregister_codec(&i2c->dev);
  566. return 0;
  567. }
  568. static struct i2c_driver tas5086_i2c_driver = {
  569. .driver = {
  570. .name = "tas5086",
  571. .owner = THIS_MODULE,
  572. .of_match_table = of_match_ptr(tas5086_dt_ids),
  573. },
  574. .id_table = tas5086_i2c_id,
  575. .probe = tas5086_i2c_probe,
  576. .remove = tas5086_i2c_remove,
  577. };
  578. module_i2c_driver(tas5086_i2c_driver);
  579. MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
  580. MODULE_DESCRIPTION("Texas Instruments TAS5086 ALSA SoC Codec Driver");
  581. MODULE_LICENSE("GPL");