tas5086.c 28 KB

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