tas5086.c 17 KB

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