tas5086.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #define TAS5086_PWM_START_MIDZ_FOR_START_1 (1 << 7)
  84. #define TAS5086_PWM_START_MIDZ_FOR_START_2 (1 << 6)
  85. #define TAS5086_PWM_START_CHANNEL_MASK (0x3f)
  86. /*
  87. * Default TAS5086 power-up configuration
  88. */
  89. static const struct reg_default tas5086_reg_defaults[] = {
  90. { 0x00, 0x6c },
  91. { 0x01, 0x03 },
  92. { 0x02, 0x00 },
  93. { 0x03, 0xa0 },
  94. { 0x04, 0x05 },
  95. { 0x05, 0x60 },
  96. { 0x06, 0x00 },
  97. { 0x07, 0xff },
  98. { 0x08, 0x30 },
  99. { 0x09, 0x30 },
  100. { 0x0a, 0x30 },
  101. { 0x0b, 0x30 },
  102. { 0x0c, 0x30 },
  103. { 0x0d, 0x30 },
  104. { 0x0e, 0xb1 },
  105. { 0x0f, 0x00 },
  106. { 0x10, 0x02 },
  107. { 0x11, 0x00 },
  108. { 0x12, 0x00 },
  109. { 0x13, 0x00 },
  110. { 0x14, 0x00 },
  111. { 0x15, 0x00 },
  112. { 0x16, 0x00 },
  113. { 0x17, 0x00 },
  114. { 0x18, 0x3f },
  115. { 0x19, 0x00 },
  116. { 0x1a, 0x18 },
  117. { 0x1b, 0x82 },
  118. { 0x1c, 0x05 },
  119. };
  120. static int tas5086_register_size(struct device *dev, unsigned int reg)
  121. {
  122. switch (reg) {
  123. case TAS5086_CLOCK_CONTROL ... TAS5086_BKNDERR:
  124. return 1;
  125. case TAS5086_INPUT_MUX:
  126. case TAS5086_PWM_OUTPUT_MUX:
  127. return 4;
  128. }
  129. dev_err(dev, "Unsupported register address: %d\n", reg);
  130. return 0;
  131. }
  132. static bool tas5086_accessible_reg(struct device *dev, unsigned int reg)
  133. {
  134. switch (reg) {
  135. case 0x0f:
  136. case 0x11 ... 0x17:
  137. case 0x1d ... 0x1f:
  138. return false;
  139. default:
  140. return true;
  141. }
  142. }
  143. static bool tas5086_volatile_reg(struct device *dev, unsigned int reg)
  144. {
  145. switch (reg) {
  146. case TAS5086_DEV_ID:
  147. case TAS5086_ERROR_STATUS:
  148. return true;
  149. }
  150. return false;
  151. }
  152. static bool tas5086_writeable_reg(struct device *dev, unsigned int reg)
  153. {
  154. return tas5086_accessible_reg(dev, reg) && (reg != TAS5086_DEV_ID);
  155. }
  156. static int tas5086_reg_write(void *context, unsigned int reg,
  157. unsigned int value)
  158. {
  159. struct i2c_client *client = context;
  160. unsigned int i, size;
  161. uint8_t buf[5];
  162. int ret;
  163. size = tas5086_register_size(&client->dev, reg);
  164. if (size == 0)
  165. return -EINVAL;
  166. buf[0] = reg;
  167. for (i = size; i >= 1; --i) {
  168. buf[i] = value;
  169. value >>= 8;
  170. }
  171. ret = i2c_master_send(client, buf, size + 1);
  172. if (ret == size + 1)
  173. return 0;
  174. else if (ret < 0)
  175. return ret;
  176. else
  177. return -EIO;
  178. }
  179. static int tas5086_reg_read(void *context, unsigned int reg,
  180. unsigned int *value)
  181. {
  182. struct i2c_client *client = context;
  183. uint8_t send_buf, recv_buf[4];
  184. struct i2c_msg msgs[2];
  185. unsigned int size;
  186. unsigned int i;
  187. int ret;
  188. size = tas5086_register_size(&client->dev, reg);
  189. if (size == 0)
  190. return -EINVAL;
  191. send_buf = reg;
  192. msgs[0].addr = client->addr;
  193. msgs[0].len = sizeof(send_buf);
  194. msgs[0].buf = &send_buf;
  195. msgs[0].flags = 0;
  196. msgs[1].addr = client->addr;
  197. msgs[1].len = size;
  198. msgs[1].buf = recv_buf;
  199. msgs[1].flags = I2C_M_RD;
  200. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  201. if (ret < 0)
  202. return ret;
  203. else if (ret != ARRAY_SIZE(msgs))
  204. return -EIO;
  205. *value = 0;
  206. for (i = 0; i < size; i++) {
  207. *value <<= 8;
  208. *value |= recv_buf[i];
  209. }
  210. return 0;
  211. }
  212. struct tas5086_private {
  213. struct regmap *regmap;
  214. unsigned int mclk, sclk;
  215. unsigned int format;
  216. bool deemph;
  217. unsigned int charge_period;
  218. unsigned int pwm_start_mid_z;
  219. /* Current sample rate for de-emphasis control */
  220. int rate;
  221. /* GPIO driving Reset pin, if any */
  222. int gpio_nreset;
  223. };
  224. static int tas5086_deemph[] = { 0, 32000, 44100, 48000 };
  225. static int tas5086_set_deemph(struct snd_soc_codec *codec)
  226. {
  227. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  228. int i, val = 0;
  229. if (priv->deemph)
  230. for (i = 0; i < ARRAY_SIZE(tas5086_deemph); i++)
  231. if (tas5086_deemph[i] == priv->rate)
  232. val = i;
  233. return regmap_update_bits(priv->regmap, TAS5086_SYS_CONTROL_1,
  234. TAS5086_DEEMPH_MASK, val);
  235. }
  236. static int tas5086_get_deemph(struct snd_kcontrol *kcontrol,
  237. struct snd_ctl_elem_value *ucontrol)
  238. {
  239. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  240. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  241. ucontrol->value.enumerated.item[0] = priv->deemph;
  242. return 0;
  243. }
  244. static int tas5086_put_deemph(struct snd_kcontrol *kcontrol,
  245. struct snd_ctl_elem_value *ucontrol)
  246. {
  247. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  248. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  249. priv->deemph = ucontrol->value.enumerated.item[0];
  250. return tas5086_set_deemph(codec);
  251. }
  252. static int tas5086_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  253. int clk_id, unsigned int freq, int dir)
  254. {
  255. struct snd_soc_codec *codec = codec_dai->codec;
  256. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  257. switch (clk_id) {
  258. case TAS5086_CLK_IDX_MCLK:
  259. priv->mclk = freq;
  260. break;
  261. case TAS5086_CLK_IDX_SCLK:
  262. priv->sclk = freq;
  263. break;
  264. }
  265. return 0;
  266. }
  267. static int tas5086_set_dai_fmt(struct snd_soc_dai *codec_dai,
  268. unsigned int format)
  269. {
  270. struct snd_soc_codec *codec = codec_dai->codec;
  271. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  272. /* The TAS5086 can only be slave to all clocks */
  273. if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
  274. dev_err(codec->dev, "Invalid clocking mode\n");
  275. return -EINVAL;
  276. }
  277. /* we need to refer to the data format from hw_params() */
  278. priv->format = format;
  279. return 0;
  280. }
  281. static const int tas5086_sample_rates[] = {
  282. 32000, 38000, 44100, 48000, 88200, 96000, 176400, 192000
  283. };
  284. static const int tas5086_ratios[] = {
  285. 64, 128, 192, 256, 384, 512
  286. };
  287. static int index_in_array(const int *array, int len, int needle)
  288. {
  289. int i;
  290. for (i = 0; i < len; i++)
  291. if (array[i] == needle)
  292. return i;
  293. return -ENOENT;
  294. }
  295. static int tas5086_hw_params(struct snd_pcm_substream *substream,
  296. struct snd_pcm_hw_params *params,
  297. struct snd_soc_dai *dai)
  298. {
  299. struct snd_soc_codec *codec = dai->codec;
  300. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  301. int val;
  302. int ret;
  303. priv->rate = params_rate(params);
  304. /* Look up the sample rate and refer to the offset in the list */
  305. val = index_in_array(tas5086_sample_rates,
  306. ARRAY_SIZE(tas5086_sample_rates), priv->rate);
  307. if (val < 0) {
  308. dev_err(codec->dev, "Invalid sample rate\n");
  309. return -EINVAL;
  310. }
  311. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  312. TAS5086_CLOCK_RATE_MASK,
  313. TAS5086_CLOCK_RATE(val));
  314. if (ret < 0)
  315. return ret;
  316. /* MCLK / Fs ratio */
  317. val = index_in_array(tas5086_ratios, ARRAY_SIZE(tas5086_ratios),
  318. priv->mclk / priv->rate);
  319. if (val < 0) {
  320. dev_err(codec->dev, "Inavlid MCLK / Fs ratio\n");
  321. return -EINVAL;
  322. }
  323. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  324. TAS5086_CLOCK_RATIO_MASK,
  325. TAS5086_CLOCK_RATIO(val));
  326. if (ret < 0)
  327. return ret;
  328. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  329. TAS5086_CLOCK_SCLK_RATIO_48,
  330. (priv->sclk == 48 * priv->rate) ?
  331. TAS5086_CLOCK_SCLK_RATIO_48 : 0);
  332. if (ret < 0)
  333. return ret;
  334. /*
  335. * The chip has a very unituitive register mapping and muxes information
  336. * about data format and sample depth into the same register, but not on
  337. * a logical bit-boundary. Hence, we have to refer to the format passed
  338. * in the set_dai_fmt() callback and set up everything from here.
  339. *
  340. * First, determine the 'base' value, using the format ...
  341. */
  342. switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
  343. case SND_SOC_DAIFMT_RIGHT_J:
  344. val = 0x00;
  345. break;
  346. case SND_SOC_DAIFMT_I2S:
  347. val = 0x03;
  348. break;
  349. case SND_SOC_DAIFMT_LEFT_J:
  350. val = 0x06;
  351. break;
  352. default:
  353. dev_err(codec->dev, "Invalid DAI format\n");
  354. return -EINVAL;
  355. }
  356. /* ... then add the offset for the sample bit depth. */
  357. switch (params_format(params)) {
  358. case SNDRV_PCM_FORMAT_S16_LE:
  359. val += 0;
  360. break;
  361. case SNDRV_PCM_FORMAT_S20_3LE:
  362. val += 1;
  363. break;
  364. case SNDRV_PCM_FORMAT_S24_3LE:
  365. val += 2;
  366. break;
  367. default:
  368. dev_err(codec->dev, "Invalid bit width\n");
  369. return -EINVAL;
  370. };
  371. ret = regmap_write(priv->regmap, TAS5086_SERIAL_DATA_IF, val);
  372. if (ret < 0)
  373. return ret;
  374. /* clock is considered valid now */
  375. ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL,
  376. TAS5086_CLOCK_VALID, TAS5086_CLOCK_VALID);
  377. if (ret < 0)
  378. return ret;
  379. return tas5086_set_deemph(codec);
  380. }
  381. static int tas5086_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
  382. {
  383. struct snd_soc_codec *codec = dai->codec;
  384. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  385. unsigned int val = 0;
  386. if (mute)
  387. val = TAS5086_SOFT_MUTE_ALL;
  388. return regmap_write(priv->regmap, TAS5086_SOFT_MUTE, val);
  389. }
  390. static void tas5086_reset(struct tas5086_private *priv)
  391. {
  392. if (gpio_is_valid(priv->gpio_nreset)) {
  393. /* Reset codec - minimum assertion time is 400ns */
  394. gpio_direction_output(priv->gpio_nreset, 0);
  395. udelay(1);
  396. gpio_set_value(priv->gpio_nreset, 1);
  397. /* Codec needs ~15ms to wake up */
  398. msleep(15);
  399. }
  400. }
  401. /* charge period values in microseconds */
  402. static const int tas5086_charge_period[] = {
  403. 13000, 16900, 23400, 31200, 41600, 54600, 72800, 96200,
  404. 130000, 156000, 234000, 312000, 416000, 546000, 728000, 962000,
  405. 1300000, 169000, 2340000, 3120000, 4160000, 5460000, 7280000, 9620000,
  406. };
  407. static int tas5086_init(struct device *dev, struct tas5086_private *priv)
  408. {
  409. int ret, i;
  410. /*
  411. * If any of the channels is configured to start in Mid-Z mode,
  412. * configure 'part 1' of the PWM starts to use Mid-Z, and tell
  413. * all configured mid-z channels to start start under 'part 1'.
  414. */
  415. if (priv->pwm_start_mid_z)
  416. regmap_write(priv->regmap, TAS5086_PWM_START,
  417. TAS5086_PWM_START_MIDZ_FOR_START_1 |
  418. priv->pwm_start_mid_z);
  419. /* lookup and set split-capacitor charge period */
  420. if (priv->charge_period == 0) {
  421. regmap_write(priv->regmap, TAS5086_SPLIT_CAP_CHARGE, 0);
  422. } else {
  423. i = index_in_array(tas5086_charge_period,
  424. ARRAY_SIZE(tas5086_charge_period),
  425. priv->charge_period);
  426. if (i >= 0)
  427. regmap_write(priv->regmap, TAS5086_SPLIT_CAP_CHARGE,
  428. i + 0x08);
  429. else
  430. dev_warn(dev,
  431. "Invalid split-cap charge period of %d ns.\n",
  432. priv->charge_period);
  433. }
  434. /* enable factory trim */
  435. ret = regmap_write(priv->regmap, TAS5086_OSC_TRIM, 0x00);
  436. if (ret < 0)
  437. return ret;
  438. /* start all channels */
  439. ret = regmap_write(priv->regmap, TAS5086_SYS_CONTROL_2, 0x20);
  440. if (ret < 0)
  441. return ret;
  442. /* mute all channels for now */
  443. ret = regmap_write(priv->regmap, TAS5086_SOFT_MUTE,
  444. TAS5086_SOFT_MUTE_ALL);
  445. if (ret < 0)
  446. return ret;
  447. return 0;
  448. }
  449. /* TAS5086 controls */
  450. static const DECLARE_TLV_DB_SCALE(tas5086_dac_tlv, -10350, 50, 1);
  451. static const struct snd_kcontrol_new tas5086_controls[] = {
  452. SOC_SINGLE_TLV("Master Playback Volume", TAS5086_MASTER_VOL,
  453. 0, 0xff, 1, tas5086_dac_tlv),
  454. SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume",
  455. TAS5086_CHANNEL_VOL(0), TAS5086_CHANNEL_VOL(1),
  456. 0, 0xff, 1, tas5086_dac_tlv),
  457. SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume",
  458. TAS5086_CHANNEL_VOL(2), TAS5086_CHANNEL_VOL(3),
  459. 0, 0xff, 1, tas5086_dac_tlv),
  460. SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume",
  461. TAS5086_CHANNEL_VOL(4), TAS5086_CHANNEL_VOL(5),
  462. 0, 0xff, 1, tas5086_dac_tlv),
  463. SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
  464. tas5086_get_deemph, tas5086_put_deemph),
  465. };
  466. /* Input mux controls */
  467. static const char *tas5086_dapm_sdin_texts[] =
  468. {
  469. "SDIN1-L", "SDIN1-R", "SDIN2-L", "SDIN2-R",
  470. "SDIN3-L", "SDIN3-R", "Ground (0)", "nc"
  471. };
  472. static const struct soc_enum tas5086_dapm_input_mux_enum[] = {
  473. SOC_ENUM_SINGLE(TAS5086_INPUT_MUX, 20, 8, tas5086_dapm_sdin_texts),
  474. SOC_ENUM_SINGLE(TAS5086_INPUT_MUX, 16, 8, tas5086_dapm_sdin_texts),
  475. SOC_ENUM_SINGLE(TAS5086_INPUT_MUX, 12, 8, tas5086_dapm_sdin_texts),
  476. SOC_ENUM_SINGLE(TAS5086_INPUT_MUX, 8, 8, tas5086_dapm_sdin_texts),
  477. SOC_ENUM_SINGLE(TAS5086_INPUT_MUX, 4, 8, tas5086_dapm_sdin_texts),
  478. SOC_ENUM_SINGLE(TAS5086_INPUT_MUX, 0, 8, tas5086_dapm_sdin_texts),
  479. };
  480. static const struct snd_kcontrol_new tas5086_dapm_input_mux_controls[] = {
  481. SOC_DAPM_ENUM("Channel 1 input", tas5086_dapm_input_mux_enum[0]),
  482. SOC_DAPM_ENUM("Channel 2 input", tas5086_dapm_input_mux_enum[1]),
  483. SOC_DAPM_ENUM("Channel 3 input", tas5086_dapm_input_mux_enum[2]),
  484. SOC_DAPM_ENUM("Channel 4 input", tas5086_dapm_input_mux_enum[3]),
  485. SOC_DAPM_ENUM("Channel 5 input", tas5086_dapm_input_mux_enum[4]),
  486. SOC_DAPM_ENUM("Channel 6 input", tas5086_dapm_input_mux_enum[5]),
  487. };
  488. /* Output mux controls */
  489. static const char *tas5086_dapm_channel_texts[] =
  490. { "Channel 1 Mux", "Channel 2 Mux", "Channel 3 Mux",
  491. "Channel 4 Mux", "Channel 5 Mux", "Channel 6 Mux" };
  492. static const struct soc_enum tas5086_dapm_output_mux_enum[] = {
  493. SOC_ENUM_SINGLE(TAS5086_PWM_OUTPUT_MUX, 20, 6, tas5086_dapm_channel_texts),
  494. SOC_ENUM_SINGLE(TAS5086_PWM_OUTPUT_MUX, 16, 6, tas5086_dapm_channel_texts),
  495. SOC_ENUM_SINGLE(TAS5086_PWM_OUTPUT_MUX, 12, 6, tas5086_dapm_channel_texts),
  496. SOC_ENUM_SINGLE(TAS5086_PWM_OUTPUT_MUX, 8, 6, tas5086_dapm_channel_texts),
  497. SOC_ENUM_SINGLE(TAS5086_PWM_OUTPUT_MUX, 4, 6, tas5086_dapm_channel_texts),
  498. SOC_ENUM_SINGLE(TAS5086_PWM_OUTPUT_MUX, 0, 6, tas5086_dapm_channel_texts),
  499. };
  500. static const struct snd_kcontrol_new tas5086_dapm_output_mux_controls[] = {
  501. SOC_DAPM_ENUM("PWM1 Output", tas5086_dapm_output_mux_enum[0]),
  502. SOC_DAPM_ENUM("PWM2 Output", tas5086_dapm_output_mux_enum[1]),
  503. SOC_DAPM_ENUM("PWM3 Output", tas5086_dapm_output_mux_enum[2]),
  504. SOC_DAPM_ENUM("PWM4 Output", tas5086_dapm_output_mux_enum[3]),
  505. SOC_DAPM_ENUM("PWM5 Output", tas5086_dapm_output_mux_enum[4]),
  506. SOC_DAPM_ENUM("PWM6 Output", tas5086_dapm_output_mux_enum[5]),
  507. };
  508. static const struct snd_soc_dapm_widget tas5086_dapm_widgets[] = {
  509. SND_SOC_DAPM_INPUT("SDIN1-L"),
  510. SND_SOC_DAPM_INPUT("SDIN1-R"),
  511. SND_SOC_DAPM_INPUT("SDIN2-L"),
  512. SND_SOC_DAPM_INPUT("SDIN2-R"),
  513. SND_SOC_DAPM_INPUT("SDIN3-L"),
  514. SND_SOC_DAPM_INPUT("SDIN3-R"),
  515. SND_SOC_DAPM_INPUT("SDIN4-L"),
  516. SND_SOC_DAPM_INPUT("SDIN4-R"),
  517. SND_SOC_DAPM_OUTPUT("PWM1"),
  518. SND_SOC_DAPM_OUTPUT("PWM2"),
  519. SND_SOC_DAPM_OUTPUT("PWM3"),
  520. SND_SOC_DAPM_OUTPUT("PWM4"),
  521. SND_SOC_DAPM_OUTPUT("PWM5"),
  522. SND_SOC_DAPM_OUTPUT("PWM6"),
  523. SND_SOC_DAPM_MUX("Channel 1 Mux", SND_SOC_NOPM, 0, 0,
  524. &tas5086_dapm_input_mux_controls[0]),
  525. SND_SOC_DAPM_MUX("Channel 2 Mux", SND_SOC_NOPM, 0, 0,
  526. &tas5086_dapm_input_mux_controls[1]),
  527. SND_SOC_DAPM_MUX("Channel 3 Mux", SND_SOC_NOPM, 0, 0,
  528. &tas5086_dapm_input_mux_controls[2]),
  529. SND_SOC_DAPM_MUX("Channel 4 Mux", SND_SOC_NOPM, 0, 0,
  530. &tas5086_dapm_input_mux_controls[3]),
  531. SND_SOC_DAPM_MUX("Channel 5 Mux", SND_SOC_NOPM, 0, 0,
  532. &tas5086_dapm_input_mux_controls[4]),
  533. SND_SOC_DAPM_MUX("Channel 6 Mux", SND_SOC_NOPM, 0, 0,
  534. &tas5086_dapm_input_mux_controls[5]),
  535. SND_SOC_DAPM_MUX("PWM1 Mux", SND_SOC_NOPM, 0, 0,
  536. &tas5086_dapm_output_mux_controls[0]),
  537. SND_SOC_DAPM_MUX("PWM2 Mux", SND_SOC_NOPM, 0, 0,
  538. &tas5086_dapm_output_mux_controls[1]),
  539. SND_SOC_DAPM_MUX("PWM3 Mux", SND_SOC_NOPM, 0, 0,
  540. &tas5086_dapm_output_mux_controls[2]),
  541. SND_SOC_DAPM_MUX("PWM4 Mux", SND_SOC_NOPM, 0, 0,
  542. &tas5086_dapm_output_mux_controls[3]),
  543. SND_SOC_DAPM_MUX("PWM5 Mux", SND_SOC_NOPM, 0, 0,
  544. &tas5086_dapm_output_mux_controls[4]),
  545. SND_SOC_DAPM_MUX("PWM6 Mux", SND_SOC_NOPM, 0, 0,
  546. &tas5086_dapm_output_mux_controls[5]),
  547. };
  548. static const struct snd_soc_dapm_route tas5086_dapm_routes[] = {
  549. /* SDIN inputs -> channel muxes */
  550. { "Channel 1 Mux", "SDIN1-L", "SDIN1-L" },
  551. { "Channel 1 Mux", "SDIN1-R", "SDIN1-R" },
  552. { "Channel 1 Mux", "SDIN2-L", "SDIN2-L" },
  553. { "Channel 1 Mux", "SDIN2-R", "SDIN2-R" },
  554. { "Channel 1 Mux", "SDIN3-L", "SDIN3-L" },
  555. { "Channel 1 Mux", "SDIN3-R", "SDIN3-R" },
  556. { "Channel 2 Mux", "SDIN1-L", "SDIN1-L" },
  557. { "Channel 2 Mux", "SDIN1-R", "SDIN1-R" },
  558. { "Channel 2 Mux", "SDIN2-L", "SDIN2-L" },
  559. { "Channel 2 Mux", "SDIN2-R", "SDIN2-R" },
  560. { "Channel 2 Mux", "SDIN3-L", "SDIN3-L" },
  561. { "Channel 2 Mux", "SDIN3-R", "SDIN3-R" },
  562. { "Channel 2 Mux", "SDIN1-L", "SDIN1-L" },
  563. { "Channel 2 Mux", "SDIN1-R", "SDIN1-R" },
  564. { "Channel 2 Mux", "SDIN2-L", "SDIN2-L" },
  565. { "Channel 2 Mux", "SDIN2-R", "SDIN2-R" },
  566. { "Channel 2 Mux", "SDIN3-L", "SDIN3-L" },
  567. { "Channel 2 Mux", "SDIN3-R", "SDIN3-R" },
  568. { "Channel 3 Mux", "SDIN1-L", "SDIN1-L" },
  569. { "Channel 3 Mux", "SDIN1-R", "SDIN1-R" },
  570. { "Channel 3 Mux", "SDIN2-L", "SDIN2-L" },
  571. { "Channel 3 Mux", "SDIN2-R", "SDIN2-R" },
  572. { "Channel 3 Mux", "SDIN3-L", "SDIN3-L" },
  573. { "Channel 3 Mux", "SDIN3-R", "SDIN3-R" },
  574. { "Channel 4 Mux", "SDIN1-L", "SDIN1-L" },
  575. { "Channel 4 Mux", "SDIN1-R", "SDIN1-R" },
  576. { "Channel 4 Mux", "SDIN2-L", "SDIN2-L" },
  577. { "Channel 4 Mux", "SDIN2-R", "SDIN2-R" },
  578. { "Channel 4 Mux", "SDIN3-L", "SDIN3-L" },
  579. { "Channel 4 Mux", "SDIN3-R", "SDIN3-R" },
  580. { "Channel 5 Mux", "SDIN1-L", "SDIN1-L" },
  581. { "Channel 5 Mux", "SDIN1-R", "SDIN1-R" },
  582. { "Channel 5 Mux", "SDIN2-L", "SDIN2-L" },
  583. { "Channel 5 Mux", "SDIN2-R", "SDIN2-R" },
  584. { "Channel 5 Mux", "SDIN3-L", "SDIN3-L" },
  585. { "Channel 5 Mux", "SDIN3-R", "SDIN3-R" },
  586. { "Channel 6 Mux", "SDIN1-L", "SDIN1-L" },
  587. { "Channel 6 Mux", "SDIN1-R", "SDIN1-R" },
  588. { "Channel 6 Mux", "SDIN2-L", "SDIN2-L" },
  589. { "Channel 6 Mux", "SDIN2-R", "SDIN2-R" },
  590. { "Channel 6 Mux", "SDIN3-L", "SDIN3-L" },
  591. { "Channel 6 Mux", "SDIN3-R", "SDIN3-R" },
  592. /* Channel muxes -> PWM muxes */
  593. { "PWM1 Mux", "Channel 1 Mux", "Channel 1 Mux" },
  594. { "PWM2 Mux", "Channel 1 Mux", "Channel 1 Mux" },
  595. { "PWM3 Mux", "Channel 1 Mux", "Channel 1 Mux" },
  596. { "PWM4 Mux", "Channel 1 Mux", "Channel 1 Mux" },
  597. { "PWM5 Mux", "Channel 1 Mux", "Channel 1 Mux" },
  598. { "PWM6 Mux", "Channel 1 Mux", "Channel 1 Mux" },
  599. { "PWM1 Mux", "Channel 2 Mux", "Channel 2 Mux" },
  600. { "PWM2 Mux", "Channel 2 Mux", "Channel 2 Mux" },
  601. { "PWM3 Mux", "Channel 2 Mux", "Channel 2 Mux" },
  602. { "PWM4 Mux", "Channel 2 Mux", "Channel 2 Mux" },
  603. { "PWM5 Mux", "Channel 2 Mux", "Channel 2 Mux" },
  604. { "PWM6 Mux", "Channel 2 Mux", "Channel 2 Mux" },
  605. { "PWM1 Mux", "Channel 3 Mux", "Channel 3 Mux" },
  606. { "PWM2 Mux", "Channel 3 Mux", "Channel 3 Mux" },
  607. { "PWM3 Mux", "Channel 3 Mux", "Channel 3 Mux" },
  608. { "PWM4 Mux", "Channel 3 Mux", "Channel 3 Mux" },
  609. { "PWM5 Mux", "Channel 3 Mux", "Channel 3 Mux" },
  610. { "PWM6 Mux", "Channel 3 Mux", "Channel 3 Mux" },
  611. { "PWM1 Mux", "Channel 4 Mux", "Channel 4 Mux" },
  612. { "PWM2 Mux", "Channel 4 Mux", "Channel 4 Mux" },
  613. { "PWM3 Mux", "Channel 4 Mux", "Channel 4 Mux" },
  614. { "PWM4 Mux", "Channel 4 Mux", "Channel 4 Mux" },
  615. { "PWM5 Mux", "Channel 4 Mux", "Channel 4 Mux" },
  616. { "PWM6 Mux", "Channel 4 Mux", "Channel 4 Mux" },
  617. { "PWM1 Mux", "Channel 5 Mux", "Channel 5 Mux" },
  618. { "PWM2 Mux", "Channel 5 Mux", "Channel 5 Mux" },
  619. { "PWM3 Mux", "Channel 5 Mux", "Channel 5 Mux" },
  620. { "PWM4 Mux", "Channel 5 Mux", "Channel 5 Mux" },
  621. { "PWM5 Mux", "Channel 5 Mux", "Channel 5 Mux" },
  622. { "PWM6 Mux", "Channel 5 Mux", "Channel 5 Mux" },
  623. { "PWM1 Mux", "Channel 6 Mux", "Channel 6 Mux" },
  624. { "PWM2 Mux", "Channel 6 Mux", "Channel 6 Mux" },
  625. { "PWM3 Mux", "Channel 6 Mux", "Channel 6 Mux" },
  626. { "PWM4 Mux", "Channel 6 Mux", "Channel 6 Mux" },
  627. { "PWM5 Mux", "Channel 6 Mux", "Channel 6 Mux" },
  628. { "PWM6 Mux", "Channel 6 Mux", "Channel 6 Mux" },
  629. /* The PWM muxes are directly connected to the PWM outputs */
  630. { "PWM1", NULL, "PWM1 Mux" },
  631. { "PWM2", NULL, "PWM2 Mux" },
  632. { "PWM3", NULL, "PWM3 Mux" },
  633. { "PWM4", NULL, "PWM4 Mux" },
  634. { "PWM5", NULL, "PWM5 Mux" },
  635. { "PWM6", NULL, "PWM6 Mux" },
  636. };
  637. static const struct snd_soc_dai_ops tas5086_dai_ops = {
  638. .hw_params = tas5086_hw_params,
  639. .set_sysclk = tas5086_set_dai_sysclk,
  640. .set_fmt = tas5086_set_dai_fmt,
  641. .mute_stream = tas5086_mute_stream,
  642. };
  643. static struct snd_soc_dai_driver tas5086_dai = {
  644. .name = "tas5086-hifi",
  645. .playback = {
  646. .stream_name = "Playback",
  647. .channels_min = 2,
  648. .channels_max = 6,
  649. .rates = TAS5086_PCM_RATES,
  650. .formats = TAS5086_PCM_FORMATS,
  651. },
  652. .ops = &tas5086_dai_ops,
  653. };
  654. #ifdef CONFIG_PM
  655. static int tas5086_soc_resume(struct snd_soc_codec *codec)
  656. {
  657. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  658. /* Restore codec state */
  659. return regcache_sync(priv->regmap);
  660. }
  661. #else
  662. #define tas5086_soc_resume NULL
  663. #endif /* CONFIG_PM */
  664. #ifdef CONFIG_OF
  665. static const struct of_device_id tas5086_dt_ids[] = {
  666. { .compatible = "ti,tas5086", },
  667. { }
  668. };
  669. MODULE_DEVICE_TABLE(of, tas5086_dt_ids);
  670. #endif
  671. static int tas5086_probe(struct snd_soc_codec *codec)
  672. {
  673. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  674. int i, ret;
  675. priv->pwm_start_mid_z = 0;
  676. priv->charge_period = 1300000; /* hardware default is 1300 ms */
  677. if (of_match_device(of_match_ptr(tas5086_dt_ids), codec->dev)) {
  678. struct device_node *of_node = codec->dev->of_node;
  679. of_property_read_u32(of_node, "ti,charge-period",
  680. &priv->charge_period);
  681. for (i = 0; i < 6; i++) {
  682. char name[25];
  683. snprintf(name, sizeof(name),
  684. "ti,mid-z-channel-%d", i + 1);
  685. if (of_get_property(of_node, name, NULL) != NULL)
  686. priv->pwm_start_mid_z |= 1 << i;
  687. }
  688. }
  689. ret = tas5086_init(codec->dev, priv);
  690. if (ret < 0)
  691. return ret;
  692. /* set master volume to 0 dB */
  693. ret = regmap_write(priv->regmap, TAS5086_MASTER_VOL, 0x30);
  694. if (ret < 0)
  695. return ret;
  696. return 0;
  697. }
  698. static int tas5086_remove(struct snd_soc_codec *codec)
  699. {
  700. struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec);
  701. if (gpio_is_valid(priv->gpio_nreset))
  702. /* Set codec to the reset state */
  703. gpio_set_value(priv->gpio_nreset, 0);
  704. return 0;
  705. };
  706. static struct snd_soc_codec_driver soc_codec_dev_tas5086 = {
  707. .probe = tas5086_probe,
  708. .remove = tas5086_remove,
  709. .resume = tas5086_soc_resume,
  710. .controls = tas5086_controls,
  711. .num_controls = ARRAY_SIZE(tas5086_controls),
  712. .dapm_widgets = tas5086_dapm_widgets,
  713. .num_dapm_widgets = ARRAY_SIZE(tas5086_dapm_widgets),
  714. .dapm_routes = tas5086_dapm_routes,
  715. .num_dapm_routes = ARRAY_SIZE(tas5086_dapm_routes),
  716. };
  717. static const struct i2c_device_id tas5086_i2c_id[] = {
  718. { "tas5086", 0 },
  719. { }
  720. };
  721. MODULE_DEVICE_TABLE(i2c, tas5086_i2c_id);
  722. static const struct regmap_config tas5086_regmap = {
  723. .reg_bits = 8,
  724. .val_bits = 32,
  725. .max_register = TAS5086_MAX_REGISTER,
  726. .reg_defaults = tas5086_reg_defaults,
  727. .num_reg_defaults = ARRAY_SIZE(tas5086_reg_defaults),
  728. .cache_type = REGCACHE_RBTREE,
  729. .volatile_reg = tas5086_volatile_reg,
  730. .writeable_reg = tas5086_writeable_reg,
  731. .readable_reg = tas5086_accessible_reg,
  732. .reg_read = tas5086_reg_read,
  733. .reg_write = tas5086_reg_write,
  734. };
  735. static int tas5086_i2c_probe(struct i2c_client *i2c,
  736. const struct i2c_device_id *id)
  737. {
  738. struct tas5086_private *priv;
  739. struct device *dev = &i2c->dev;
  740. int gpio_nreset = -EINVAL;
  741. int i, ret;
  742. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  743. if (!priv)
  744. return -ENOMEM;
  745. priv->regmap = devm_regmap_init(dev, NULL, i2c, &tas5086_regmap);
  746. if (IS_ERR(priv->regmap)) {
  747. ret = PTR_ERR(priv->regmap);
  748. dev_err(&i2c->dev, "Failed to create regmap: %d\n", ret);
  749. return ret;
  750. }
  751. i2c_set_clientdata(i2c, priv);
  752. if (of_match_device(of_match_ptr(tas5086_dt_ids), dev)) {
  753. struct device_node *of_node = dev->of_node;
  754. gpio_nreset = of_get_named_gpio(of_node, "reset-gpio", 0);
  755. }
  756. if (gpio_is_valid(gpio_nreset))
  757. if (devm_gpio_request(dev, gpio_nreset, "TAS5086 Reset"))
  758. gpio_nreset = -EINVAL;
  759. priv->gpio_nreset = gpio_nreset;
  760. tas5086_reset(priv);
  761. /* The TAS5086 always returns 0x03 in its TAS5086_DEV_ID register */
  762. ret = regmap_read(priv->regmap, TAS5086_DEV_ID, &i);
  763. if (ret < 0)
  764. return ret;
  765. if (i != 0x3) {
  766. dev_err(dev,
  767. "Failed to identify TAS5086 codec (got %02x)\n", i);
  768. return -ENODEV;
  769. }
  770. return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_tas5086,
  771. &tas5086_dai, 1);
  772. }
  773. static int tas5086_i2c_remove(struct i2c_client *i2c)
  774. {
  775. snd_soc_unregister_codec(&i2c->dev);
  776. return 0;
  777. }
  778. static struct i2c_driver tas5086_i2c_driver = {
  779. .driver = {
  780. .name = "tas5086",
  781. .owner = THIS_MODULE,
  782. .of_match_table = of_match_ptr(tas5086_dt_ids),
  783. },
  784. .id_table = tas5086_i2c_id,
  785. .probe = tas5086_i2c_probe,
  786. .remove = tas5086_i2c_remove,
  787. };
  788. module_i2c_driver(tas5086_i2c_driver);
  789. MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
  790. MODULE_DESCRIPTION("Texas Instruments TAS5086 ALSA SoC Codec Driver");
  791. MODULE_LICENSE("GPL");