tas5086.c 28 KB

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