tas2562.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Driver for the Texas Instruments TAS2562 CODEC
  4. // Copyright (C) 2019 Texas Instruments Inc.
  5. #include <linux/module.h>
  6. #include <linux/errno.h>
  7. #include <linux/device.h>
  8. #include <linux/i2c.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/regmap.h>
  11. #include <linux/slab.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/delay.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <sound/soc-dapm.h>
  19. #include <sound/tlv.h>
  20. #include "tas2562.h"
  21. #define TAS2562_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
  22. SNDRV_PCM_FORMAT_S32_LE)
  23. /* DVC equation involves floating point math
  24. * round(10^(volume in dB/20)*2^30)
  25. * so create a lookup table for 2dB step
  26. */
  27. static const unsigned int float_vol_db_lookup[] = {
  28. 0x00000d43, 0x000010b2, 0x00001505, 0x00001a67, 0x00002151,
  29. 0x000029f1, 0x000034cd, 0x00004279, 0x000053af, 0x0000695b,
  30. 0x0000695b, 0x0000a6fa, 0x0000d236, 0x000108a4, 0x00014d2a,
  31. 0x0001a36e, 0x00021008, 0x000298c0, 0x000344df, 0x00041d8f,
  32. 0x00052e5a, 0x000685c8, 0x00083621, 0x000a566d, 0x000d03a7,
  33. 0x0010624d, 0x0014a050, 0x0019f786, 0x0020b0bc, 0x0029279d,
  34. 0x0033cf8d, 0x004139d3, 0x00521d50, 0x00676044, 0x0082248a,
  35. 0x00a3d70a, 0x00ce4328, 0x0103ab3d, 0x0146e75d, 0x019b8c27,
  36. 0x02061b89, 0x028c423f, 0x03352529, 0x0409c2b0, 0x05156d68,
  37. 0x080e9f96, 0x0a24b062, 0x0cc509ab, 0x10137987, 0x143d1362,
  38. 0x197a967f, 0x2013739e, 0x28619ae9, 0x32d64617, 0x40000000
  39. };
  40. struct tas2562_data {
  41. struct snd_soc_component *component;
  42. struct gpio_desc *sdz_gpio;
  43. struct regmap *regmap;
  44. struct device *dev;
  45. struct i2c_client *client;
  46. int v_sense_slot;
  47. int i_sense_slot;
  48. int volume_lvl;
  49. int model_id;
  50. bool dac_powered;
  51. bool unmuted;
  52. };
  53. enum tas256x_model {
  54. TAS2562,
  55. TAS2563,
  56. TAS2564,
  57. TAS2110,
  58. };
  59. static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate)
  60. {
  61. int samp_rate;
  62. int ramp_rate;
  63. switch (samplerate) {
  64. case 7350:
  65. ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
  66. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
  67. break;
  68. case 8000:
  69. ramp_rate = 0;
  70. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
  71. break;
  72. case 14700:
  73. ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
  74. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
  75. break;
  76. case 16000:
  77. ramp_rate = 0;
  78. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
  79. break;
  80. case 22050:
  81. ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
  82. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
  83. break;
  84. case 24000:
  85. ramp_rate = 0;
  86. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
  87. break;
  88. case 29400:
  89. ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
  90. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
  91. break;
  92. case 32000:
  93. ramp_rate = 0;
  94. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
  95. break;
  96. case 44100:
  97. ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
  98. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
  99. break;
  100. case 48000:
  101. ramp_rate = 0;
  102. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
  103. break;
  104. case 88200:
  105. ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
  106. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
  107. break;
  108. case 96000:
  109. ramp_rate = 0;
  110. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
  111. break;
  112. case 176400:
  113. ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
  114. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
  115. break;
  116. case 192000:
  117. ramp_rate = 0;
  118. samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
  119. break;
  120. default:
  121. dev_info(tas2562->dev, "%s, unsupported sample rate, %d\n",
  122. __func__, samplerate);
  123. return -EINVAL;
  124. }
  125. snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
  126. TAS2562_TDM_CFG0_RAMPRATE_MASK, ramp_rate);
  127. snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
  128. TAS2562_TDM_CFG0_SAMPRATE_MASK, samp_rate);
  129. return 0;
  130. }
  131. static int tas2562_set_dai_tdm_slot(struct snd_soc_dai *dai,
  132. unsigned int tx_mask, unsigned int rx_mask,
  133. int slots, int slot_width)
  134. {
  135. struct snd_soc_component *component = dai->component;
  136. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  137. int left_slot, right_slot;
  138. int slots_cfg;
  139. int ret;
  140. if (!tx_mask) {
  141. dev_err(component->dev, "tx masks must not be 0\n");
  142. return -EINVAL;
  143. }
  144. if (slots == 1) {
  145. if (tx_mask != 1)
  146. return -EINVAL;
  147. left_slot = 0;
  148. right_slot = 0;
  149. } else {
  150. left_slot = __ffs(tx_mask);
  151. tx_mask &= ~(1 << left_slot);
  152. if (tx_mask == 0) {
  153. right_slot = left_slot;
  154. } else {
  155. right_slot = __ffs(tx_mask);
  156. }
  157. }
  158. slots_cfg = (right_slot << TAS2562_RIGHT_SLOT_SHIFT) | left_slot;
  159. ret = snd_soc_component_write(component, TAS2562_TDM_CFG3, slots_cfg);
  160. if (ret < 0)
  161. return ret;
  162. switch (slot_width) {
  163. case 16:
  164. ret = snd_soc_component_update_bits(component,
  165. TAS2562_TDM_CFG2,
  166. TAS2562_TDM_CFG2_RXLEN_MASK,
  167. TAS2562_TDM_CFG2_RXLEN_16B);
  168. break;
  169. case 24:
  170. ret = snd_soc_component_update_bits(component,
  171. TAS2562_TDM_CFG2,
  172. TAS2562_TDM_CFG2_RXLEN_MASK,
  173. TAS2562_TDM_CFG2_RXLEN_24B);
  174. break;
  175. case 32:
  176. ret = snd_soc_component_update_bits(component,
  177. TAS2562_TDM_CFG2,
  178. TAS2562_TDM_CFG2_RXLEN_MASK,
  179. TAS2562_TDM_CFG2_RXLEN_32B);
  180. break;
  181. case 0:
  182. /* Do not change slot width */
  183. break;
  184. default:
  185. dev_err(tas2562->dev, "slot width not supported");
  186. ret = -EINVAL;
  187. }
  188. if (ret < 0)
  189. return ret;
  190. ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG5,
  191. TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
  192. tas2562->v_sense_slot);
  193. if (ret < 0)
  194. return ret;
  195. ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG6,
  196. TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
  197. tas2562->i_sense_slot);
  198. if (ret < 0)
  199. return ret;
  200. return 0;
  201. }
  202. static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth)
  203. {
  204. int ret;
  205. int val;
  206. int sense_en;
  207. switch (bitwidth) {
  208. case SNDRV_PCM_FORMAT_S16_LE:
  209. snd_soc_component_update_bits(tas2562->component,
  210. TAS2562_TDM_CFG2,
  211. TAS2562_TDM_CFG2_RXWLEN_MASK,
  212. TAS2562_TDM_CFG2_RXWLEN_16B);
  213. break;
  214. case SNDRV_PCM_FORMAT_S24_LE:
  215. snd_soc_component_update_bits(tas2562->component,
  216. TAS2562_TDM_CFG2,
  217. TAS2562_TDM_CFG2_RXWLEN_MASK,
  218. TAS2562_TDM_CFG2_RXWLEN_24B);
  219. break;
  220. case SNDRV_PCM_FORMAT_S32_LE:
  221. snd_soc_component_update_bits(tas2562->component,
  222. TAS2562_TDM_CFG2,
  223. TAS2562_TDM_CFG2_RXWLEN_MASK,
  224. TAS2562_TDM_CFG2_RXWLEN_32B);
  225. break;
  226. default:
  227. dev_info(tas2562->dev, "Unsupported bitwidth format\n");
  228. return -EINVAL;
  229. }
  230. val = snd_soc_component_read(tas2562->component, TAS2562_PWR_CTRL);
  231. if (val < 0)
  232. return val;
  233. if (val & (1 << TAS2562_VSENSE_POWER_EN))
  234. sense_en = 0;
  235. else
  236. sense_en = TAS2562_TDM_CFG5_VSNS_EN;
  237. ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG5,
  238. TAS2562_TDM_CFG5_VSNS_EN, sense_en);
  239. if (ret < 0)
  240. return ret;
  241. if (val & (1 << TAS2562_ISENSE_POWER_EN))
  242. sense_en = 0;
  243. else
  244. sense_en = TAS2562_TDM_CFG6_ISNS_EN;
  245. ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG6,
  246. TAS2562_TDM_CFG6_ISNS_EN, sense_en);
  247. if (ret < 0)
  248. return ret;
  249. return 0;
  250. }
  251. static int tas2562_hw_params(struct snd_pcm_substream *substream,
  252. struct snd_pcm_hw_params *params,
  253. struct snd_soc_dai *dai)
  254. {
  255. struct snd_soc_component *component = dai->component;
  256. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  257. int ret;
  258. ret = tas2562_set_bitwidth(tas2562, params_format(params));
  259. if (ret) {
  260. dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
  261. return ret;
  262. }
  263. ret = tas2562_set_samplerate(tas2562, params_rate(params));
  264. if (ret)
  265. dev_err(tas2562->dev, "set sample rate failed, %d\n", ret);
  266. return ret;
  267. }
  268. static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  269. {
  270. struct snd_soc_component *component = dai->component;
  271. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  272. u8 asi_cfg_1 = 0;
  273. u8 tdm_rx_start_slot = 0;
  274. int ret;
  275. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  276. case SND_SOC_DAIFMT_NB_NF:
  277. asi_cfg_1 = 0;
  278. break;
  279. case SND_SOC_DAIFMT_IB_NF:
  280. asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING;
  281. break;
  282. default:
  283. dev_err(tas2562->dev, "ASI format Inverse is not found\n");
  284. return -EINVAL;
  285. }
  286. ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
  287. TAS2562_TDM_CFG1_RX_EDGE_MASK,
  288. asi_cfg_1);
  289. if (ret < 0) {
  290. dev_err(tas2562->dev, "Failed to set RX edge\n");
  291. return ret;
  292. }
  293. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  294. case SND_SOC_DAIFMT_LEFT_J:
  295. case SND_SOC_DAIFMT_DSP_B:
  296. tdm_rx_start_slot = 0;
  297. break;
  298. case SND_SOC_DAIFMT_I2S:
  299. case SND_SOC_DAIFMT_DSP_A:
  300. tdm_rx_start_slot = 1;
  301. break;
  302. default:
  303. dev_err(tas2562->dev,
  304. "DAI Format is not found, fmt=0x%x\n", fmt);
  305. return -EINVAL;
  306. }
  307. ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
  308. TAS2562_RX_OFF_MASK, (tdm_rx_start_slot << 1));
  309. if (ret < 0)
  310. return ret;
  311. return 0;
  312. }
  313. static int tas2562_update_pwr_ctrl(struct tas2562_data *tas2562)
  314. {
  315. struct snd_soc_component *component = tas2562->component;
  316. unsigned int val;
  317. int ret;
  318. if (tas2562->dac_powered)
  319. val = tas2562->unmuted ?
  320. TAS2562_ACTIVE : TAS2562_MUTE;
  321. else
  322. val = TAS2562_SHUTDOWN;
  323. ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
  324. TAS2562_MODE_MASK, val);
  325. if (ret < 0)
  326. return ret;
  327. return 0;
  328. }
  329. static int tas2562_mute(struct snd_soc_dai *dai, int mute, int direction)
  330. {
  331. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(dai->component);
  332. tas2562->unmuted = !mute;
  333. return tas2562_update_pwr_ctrl(tas2562);
  334. }
  335. static int tas2562_codec_probe(struct snd_soc_component *component)
  336. {
  337. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  338. tas2562->component = component;
  339. if (tas2562->sdz_gpio)
  340. gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
  341. return 0;
  342. }
  343. #ifdef CONFIG_PM
  344. static int tas2562_suspend(struct snd_soc_component *component)
  345. {
  346. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  347. regcache_cache_only(tas2562->regmap, true);
  348. regcache_mark_dirty(tas2562->regmap);
  349. if (tas2562->sdz_gpio)
  350. gpiod_set_value_cansleep(tas2562->sdz_gpio, 0);
  351. return 0;
  352. }
  353. static int tas2562_resume(struct snd_soc_component *component)
  354. {
  355. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  356. if (tas2562->sdz_gpio)
  357. gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
  358. regcache_cache_only(tas2562->regmap, false);
  359. return regcache_sync(tas2562->regmap);
  360. }
  361. #else
  362. #define tas2562_suspend NULL
  363. #define tas2562_resume NULL
  364. #endif
  365. static const char * const tas2562_ASI1_src[] = {
  366. "I2C offset", "Left", "Right", "LeftRightDiv2",
  367. };
  368. static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4,
  369. tas2562_ASI1_src);
  370. static const struct snd_kcontrol_new tas2562_asi1_mux =
  371. SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum);
  372. static int tas2562_dac_event(struct snd_soc_dapm_widget *w,
  373. struct snd_kcontrol *kcontrol, int event)
  374. {
  375. struct snd_soc_component *component =
  376. snd_soc_dapm_to_component(w->dapm);
  377. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  378. int ret = 0;
  379. switch (event) {
  380. case SND_SOC_DAPM_POST_PMU:
  381. tas2562->dac_powered = true;
  382. ret = tas2562_update_pwr_ctrl(tas2562);
  383. break;
  384. case SND_SOC_DAPM_PRE_PMD:
  385. tas2562->dac_powered = false;
  386. ret = tas2562_update_pwr_ctrl(tas2562);
  387. break;
  388. default:
  389. dev_err(tas2562->dev, "Not supported evevt\n");
  390. return -EINVAL;
  391. }
  392. return ret;
  393. }
  394. static int tas2562_volume_control_get(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_value *ucontrol)
  396. {
  397. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  398. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  399. ucontrol->value.integer.value[0] = tas2562->volume_lvl;
  400. return 0;
  401. }
  402. static int tas2562_volume_control_put(struct snd_kcontrol *kcontrol,
  403. struct snd_ctl_elem_value *ucontrol)
  404. {
  405. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  406. struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
  407. int ret;
  408. u32 reg_val;
  409. reg_val = float_vol_db_lookup[ucontrol->value.integer.value[0]/2];
  410. ret = snd_soc_component_write(component, TAS2562_DVC_CFG4,
  411. (reg_val & 0xff));
  412. if (ret)
  413. return ret;
  414. ret = snd_soc_component_write(component, TAS2562_DVC_CFG3,
  415. ((reg_val >> 8) & 0xff));
  416. if (ret)
  417. return ret;
  418. ret = snd_soc_component_write(component, TAS2562_DVC_CFG2,
  419. ((reg_val >> 16) & 0xff));
  420. if (ret)
  421. return ret;
  422. ret = snd_soc_component_write(component, TAS2562_DVC_CFG1,
  423. ((reg_val >> 24) & 0xff));
  424. if (ret)
  425. return ret;
  426. tas2562->volume_lvl = ucontrol->value.integer.value[0];
  427. return 0;
  428. }
  429. /* Digital Volume Control. From 0 dB to -110 dB in 1 dB steps */
  430. static const DECLARE_TLV_DB_SCALE(dvc_tlv, -11000, 100, 0);
  431. static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0);
  432. static const struct snd_kcontrol_new isense_switch =
  433. SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN,
  434. 1, 1);
  435. static const struct snd_kcontrol_new vsense_switch =
  436. SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN,
  437. 1, 1);
  438. static const struct snd_kcontrol_new tas2562_snd_controls[] = {
  439. SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 1, 0x1c, 0,
  440. tas2562_dac_tlv),
  441. {
  442. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  443. .name = "Digital Volume Control",
  444. .index = 0,
  445. .tlv.p = dvc_tlv,
  446. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | SNDRV_CTL_ELEM_ACCESS_READWRITE,
  447. .info = snd_soc_info_volsw,
  448. .get = tas2562_volume_control_get,
  449. .put = tas2562_volume_control_put,
  450. .private_value = SOC_SINGLE_VALUE(TAS2562_DVC_CFG1, 0, 110, 0, 0),
  451. },
  452. };
  453. static const struct snd_soc_dapm_widget tas2110_dapm_widgets[] = {
  454. SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
  455. SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
  456. SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
  457. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  458. SND_SOC_DAPM_OUTPUT("OUT"),
  459. };
  460. static const struct snd_soc_dapm_route tas2110_audio_map[] = {
  461. {"ASI1 Sel", "I2C offset", "ASI1"},
  462. {"ASI1 Sel", "Left", "ASI1"},
  463. {"ASI1 Sel", "Right", "ASI1"},
  464. {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
  465. { "DAC", NULL, "ASI1 Sel" },
  466. { "OUT", NULL, "DAC" },
  467. };
  468. static const struct snd_soc_component_driver soc_component_dev_tas2110 = {
  469. .probe = tas2562_codec_probe,
  470. .suspend = tas2562_suspend,
  471. .resume = tas2562_resume,
  472. .controls = tas2562_snd_controls,
  473. .num_controls = ARRAY_SIZE(tas2562_snd_controls),
  474. .dapm_widgets = tas2110_dapm_widgets,
  475. .num_dapm_widgets = ARRAY_SIZE(tas2110_dapm_widgets),
  476. .dapm_routes = tas2110_audio_map,
  477. .num_dapm_routes = ARRAY_SIZE(tas2110_audio_map),
  478. .idle_bias_on = 1,
  479. .use_pmdown_time = 1,
  480. .endianness = 1,
  481. };
  482. static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = {
  483. SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
  484. SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
  485. SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
  486. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  487. SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch),
  488. SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch),
  489. SND_SOC_DAPM_SIGGEN("VMON"),
  490. SND_SOC_DAPM_SIGGEN("IMON"),
  491. SND_SOC_DAPM_OUTPUT("OUT"),
  492. };
  493. static const struct snd_soc_dapm_route tas2562_audio_map[] = {
  494. {"ASI1 Sel", "I2C offset", "ASI1"},
  495. {"ASI1 Sel", "Left", "ASI1"},
  496. {"ASI1 Sel", "Right", "ASI1"},
  497. {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
  498. { "DAC", NULL, "ASI1 Sel" },
  499. { "OUT", NULL, "DAC" },
  500. {"ISENSE", "Switch", "IMON"},
  501. {"VSENSE", "Switch", "VMON"},
  502. };
  503. static const struct snd_soc_component_driver soc_component_dev_tas2562 = {
  504. .probe = tas2562_codec_probe,
  505. .suspend = tas2562_suspend,
  506. .resume = tas2562_resume,
  507. .controls = tas2562_snd_controls,
  508. .num_controls = ARRAY_SIZE(tas2562_snd_controls),
  509. .dapm_widgets = tas2562_dapm_widgets,
  510. .num_dapm_widgets = ARRAY_SIZE(tas2562_dapm_widgets),
  511. .dapm_routes = tas2562_audio_map,
  512. .num_dapm_routes = ARRAY_SIZE(tas2562_audio_map),
  513. .idle_bias_on = 1,
  514. .use_pmdown_time = 1,
  515. .endianness = 1,
  516. };
  517. static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = {
  518. .hw_params = tas2562_hw_params,
  519. .set_fmt = tas2562_set_dai_fmt,
  520. .set_tdm_slot = tas2562_set_dai_tdm_slot,
  521. .mute_stream = tas2562_mute,
  522. .no_capture_mute = 1,
  523. };
  524. static struct snd_soc_dai_driver tas2562_dai[] = {
  525. {
  526. .name = "tas2562-amplifier",
  527. .id = 0,
  528. .playback = {
  529. .stream_name = "ASI1 Playback",
  530. .channels_min = 2,
  531. .channels_max = 2,
  532. .rates = SNDRV_PCM_RATE_8000_192000,
  533. .formats = TAS2562_FORMATS,
  534. },
  535. .capture = {
  536. .stream_name = "ASI1 Capture",
  537. .channels_min = 0,
  538. .channels_max = 2,
  539. .rates = SNDRV_PCM_RATE_8000_192000,
  540. .formats = TAS2562_FORMATS,
  541. },
  542. .ops = &tas2562_speaker_dai_ops,
  543. },
  544. };
  545. static const struct regmap_range_cfg tas2562_ranges[] = {
  546. {
  547. .range_min = 0,
  548. .range_max = 5 * 128,
  549. .selector_reg = TAS2562_PAGE_CTRL,
  550. .selector_mask = 0xff,
  551. .selector_shift = 0,
  552. .window_start = 0,
  553. .window_len = 128,
  554. },
  555. };
  556. static const struct reg_default tas2562_reg_defaults[] = {
  557. { TAS2562_PAGE_CTRL, 0x00 },
  558. { TAS2562_SW_RESET, 0x00 },
  559. { TAS2562_PWR_CTRL, 0x0e },
  560. { TAS2562_PB_CFG1, 0x20 },
  561. { TAS2562_TDM_CFG0, 0x09 },
  562. { TAS2562_TDM_CFG1, 0x02 },
  563. { TAS2562_DVC_CFG1, 0x40 },
  564. { TAS2562_DVC_CFG2, 0x40 },
  565. { TAS2562_DVC_CFG3, 0x00 },
  566. { TAS2562_DVC_CFG4, 0x00 },
  567. };
  568. static const struct regmap_config tas2562_regmap_config = {
  569. .reg_bits = 8,
  570. .val_bits = 8,
  571. .max_register = 5 * 128,
  572. .cache_type = REGCACHE_RBTREE,
  573. .reg_defaults = tas2562_reg_defaults,
  574. .num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
  575. .ranges = tas2562_ranges,
  576. .num_ranges = ARRAY_SIZE(tas2562_ranges),
  577. };
  578. static int tas2562_parse_dt(struct tas2562_data *tas2562)
  579. {
  580. struct device *dev = tas2562->dev;
  581. int ret = 0;
  582. tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
  583. if (IS_ERR(tas2562->sdz_gpio)) {
  584. if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER)
  585. return -EPROBE_DEFER;
  586. tas2562->sdz_gpio = NULL;
  587. }
  588. /*
  589. * The shut-down property is deprecated but needs to be checked for
  590. * backwards compatibility.
  591. */
  592. if (tas2562->sdz_gpio == NULL) {
  593. tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down",
  594. GPIOD_OUT_HIGH);
  595. if (IS_ERR(tas2562->sdz_gpio))
  596. if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER)
  597. return -EPROBE_DEFER;
  598. tas2562->sdz_gpio = NULL;
  599. }
  600. if (tas2562->model_id == TAS2110)
  601. return ret;
  602. ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
  603. &tas2562->i_sense_slot);
  604. if (ret) {
  605. dev_err(dev, "Property %s is missing setting default slot\n",
  606. "ti,imon-slot-no");
  607. tas2562->i_sense_slot = 0;
  608. }
  609. ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
  610. &tas2562->v_sense_slot);
  611. if (ret) {
  612. dev_info(dev, "Property %s is missing setting default slot\n",
  613. "ti,vmon-slot-no");
  614. tas2562->v_sense_slot = 2;
  615. }
  616. if (tas2562->v_sense_slot < tas2562->i_sense_slot) {
  617. dev_err(dev, "Vsense slot must be greater than Isense slot\n");
  618. return -EINVAL;
  619. }
  620. return ret;
  621. }
  622. static const struct i2c_device_id tas2562_id[] = {
  623. { "tas2562", TAS2562 },
  624. { "tas2563", TAS2563 },
  625. { "tas2564", TAS2564 },
  626. { "tas2110", TAS2110 },
  627. { }
  628. };
  629. MODULE_DEVICE_TABLE(i2c, tas2562_id);
  630. static int tas2562_probe(struct i2c_client *client)
  631. {
  632. struct device *dev = &client->dev;
  633. struct tas2562_data *data;
  634. int ret;
  635. const struct i2c_device_id *id;
  636. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  637. if (!data)
  638. return -ENOMEM;
  639. id = i2c_match_id(tas2562_id, client);
  640. data->client = client;
  641. data->dev = &client->dev;
  642. data->model_id = id->driver_data;
  643. tas2562_parse_dt(data);
  644. data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config);
  645. if (IS_ERR(data->regmap)) {
  646. ret = PTR_ERR(data->regmap);
  647. dev_err(dev, "failed to allocate register map: %d\n", ret);
  648. return ret;
  649. }
  650. dev_set_drvdata(&client->dev, data);
  651. if (data->model_id == TAS2110)
  652. return devm_snd_soc_register_component(dev,
  653. &soc_component_dev_tas2110,
  654. tas2562_dai,
  655. ARRAY_SIZE(tas2562_dai));
  656. return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562,
  657. tas2562_dai,
  658. ARRAY_SIZE(tas2562_dai));
  659. }
  660. #ifdef CONFIG_OF
  661. static const struct of_device_id tas2562_of_match[] = {
  662. { .compatible = "ti,tas2562", },
  663. { .compatible = "ti,tas2563", },
  664. { .compatible = "ti,tas2564", },
  665. { .compatible = "ti,tas2110", },
  666. { },
  667. };
  668. MODULE_DEVICE_TABLE(of, tas2562_of_match);
  669. #endif
  670. static struct i2c_driver tas2562_i2c_driver = {
  671. .driver = {
  672. .name = "tas2562",
  673. .of_match_table = of_match_ptr(tas2562_of_match),
  674. },
  675. .probe_new = tas2562_probe,
  676. .id_table = tas2562_id,
  677. };
  678. module_i2c_driver(tas2562_i2c_driver);
  679. MODULE_AUTHOR("Dan Murphy <[email protected]>");
  680. MODULE_DESCRIPTION("TAS2562 Audio amplifier driver");
  681. MODULE_LICENSE("GPL");