tas2780.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Driver for the Texas Instruments TAS2780 Mono
  3. // Audio amplifier
  4. // Copyright (C) 2022 Texas Instruments Inc.
  5. #include <linux/module.h>
  6. #include <linux/err.h>
  7. #include <linux/pm.h>
  8. #include <linux/i2c.h>
  9. #include <linux/gpio.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/regmap.h>
  12. #include <linux/of.h>
  13. #include <linux/of_gpio.h>
  14. #include <sound/soc.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/tlv.h>
  18. #include "tas2780.h"
  19. struct tas2780_priv {
  20. struct snd_soc_component *component;
  21. struct gpio_desc *reset_gpio;
  22. struct regmap *regmap;
  23. struct device *dev;
  24. int v_sense_slot;
  25. int i_sense_slot;
  26. };
  27. static void tas2780_reset(struct tas2780_priv *tas2780)
  28. {
  29. int ret = 0;
  30. if (tas2780->reset_gpio) {
  31. gpiod_set_value_cansleep(tas2780->reset_gpio, 0);
  32. usleep_range(2000, 2050);
  33. gpiod_set_value_cansleep(tas2780->reset_gpio, 1);
  34. usleep_range(2000, 2050);
  35. }
  36. ret = snd_soc_component_write(tas2780->component, TAS2780_SW_RST,
  37. TAS2780_RST);
  38. if (ret)
  39. dev_err(tas2780->dev, "%s:errCode:0x%x Reset error!\n",
  40. __func__, ret);
  41. }
  42. #ifdef CONFIG_PM
  43. static int tas2780_codec_suspend(struct snd_soc_component *component)
  44. {
  45. struct tas2780_priv *tas2780 =
  46. snd_soc_component_get_drvdata(component);
  47. int ret = 0;
  48. ret = snd_soc_component_update_bits(component, TAS2780_PWR_CTRL,
  49. TAS2780_PWR_CTRL_MASK, TAS2780_PWR_CTRL_SHUTDOWN);
  50. if (ret < 0) {
  51. dev_err(tas2780->dev, "%s:errCode:0x%0x:power down error\n",
  52. __func__, ret);
  53. goto err;
  54. }
  55. ret = 0;
  56. regcache_cache_only(tas2780->regmap, true);
  57. regcache_mark_dirty(tas2780->regmap);
  58. err:
  59. return ret;
  60. }
  61. static int tas2780_codec_resume(struct snd_soc_component *component)
  62. {
  63. struct tas2780_priv *tas2780 =
  64. snd_soc_component_get_drvdata(component);
  65. int ret = 0;
  66. ret = snd_soc_component_update_bits(component, TAS2780_PWR_CTRL,
  67. TAS2780_PWR_CTRL_MASK, TAS2780_PWR_CTRL_ACTIVE);
  68. if (ret < 0) {
  69. dev_err(tas2780->dev, "%s:errCode:0x%0x:power down error\n",
  70. __func__, ret);
  71. goto err;
  72. }
  73. ret = 0;
  74. regcache_cache_only(tas2780->regmap, false);
  75. ret = regcache_sync(tas2780->regmap);
  76. err:
  77. return ret;
  78. }
  79. #endif
  80. static const char * const tas2780_ASI1_src[] = {
  81. "I2C offset", "Left", "Right", "LeftRightDiv2",
  82. };
  83. static SOC_ENUM_SINGLE_DECL(
  84. tas2780_ASI1_src_enum, TAS2780_TDM_CFG2, 4, tas2780_ASI1_src);
  85. static const struct snd_kcontrol_new tas2780_asi1_mux =
  86. SOC_DAPM_ENUM("ASI1 Source", tas2780_ASI1_src_enum);
  87. static const struct snd_kcontrol_new isense_switch =
  88. SOC_DAPM_SINGLE("Switch", TAS2780_PWR_CTRL,
  89. TAS2780_ISENSE_POWER_EN, 1, 1);
  90. static const struct snd_kcontrol_new vsense_switch =
  91. SOC_DAPM_SINGLE("Switch", TAS2780_PWR_CTRL,
  92. TAS2780_VSENSE_POWER_EN, 1, 1);
  93. static const struct snd_soc_dapm_widget tas2780_dapm_widgets[] = {
  94. SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
  95. SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2780_asi1_mux),
  96. SND_SOC_DAPM_SWITCH("ISENSE", TAS2780_PWR_CTRL,
  97. TAS2780_ISENSE_POWER_EN, 1, &isense_switch),
  98. SND_SOC_DAPM_SWITCH("VSENSE", TAS2780_PWR_CTRL,
  99. TAS2780_VSENSE_POWER_EN, 1, &vsense_switch),
  100. SND_SOC_DAPM_OUTPUT("OUT"),
  101. SND_SOC_DAPM_SIGGEN("VMON"),
  102. SND_SOC_DAPM_SIGGEN("IMON")
  103. };
  104. static const struct snd_soc_dapm_route tas2780_audio_map[] = {
  105. {"ASI1 Sel", "I2C offset", "ASI1"},
  106. {"ASI1 Sel", "Left", "ASI1"},
  107. {"ASI1 Sel", "Right", "ASI1"},
  108. {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
  109. {"OUT", NULL, "ASI1 Sel"},
  110. {"ISENSE", "Switch", "IMON"},
  111. {"VSENSE", "Switch", "VMON"},
  112. };
  113. static int tas2780_mute(struct snd_soc_dai *dai, int mute, int direction)
  114. {
  115. struct snd_soc_component *component = dai->component;
  116. struct tas2780_priv *tas2780 =
  117. snd_soc_component_get_drvdata(component);
  118. int ret = 0;
  119. ret = snd_soc_component_update_bits(component, TAS2780_PWR_CTRL,
  120. TAS2780_PWR_CTRL_MASK,
  121. mute ? TAS2780_PWR_CTRL_MUTE : 0);
  122. if (ret < 0) {
  123. dev_err(tas2780->dev, "%s: Failed to set powercontrol\n",
  124. __func__);
  125. goto err;
  126. }
  127. ret = 0;
  128. err:
  129. return ret;
  130. }
  131. static int tas2780_set_bitwidth(struct tas2780_priv *tas2780, int bitwidth)
  132. {
  133. struct snd_soc_component *component = tas2780->component;
  134. int sense_en;
  135. int val;
  136. int ret;
  137. int slot_size;
  138. switch (bitwidth) {
  139. case SNDRV_PCM_FORMAT_S16_LE:
  140. ret = snd_soc_component_update_bits(component,
  141. TAS2780_TDM_CFG2,
  142. TAS2780_TDM_CFG2_RXW_MASK,
  143. TAS2780_TDM_CFG2_RXW_16BITS);
  144. slot_size = TAS2780_TDM_CFG2_RXS_16BITS;
  145. break;
  146. case SNDRV_PCM_FORMAT_S24_LE:
  147. ret = snd_soc_component_update_bits(component,
  148. TAS2780_TDM_CFG2,
  149. TAS2780_TDM_CFG2_RXW_MASK,
  150. TAS2780_TDM_CFG2_RXW_24BITS);
  151. slot_size = TAS2780_TDM_CFG2_RXS_24BITS;
  152. break;
  153. case SNDRV_PCM_FORMAT_S32_LE:
  154. ret = snd_soc_component_update_bits(component,
  155. TAS2780_TDM_CFG2,
  156. TAS2780_TDM_CFG2_RXW_MASK,
  157. TAS2780_TDM_CFG2_RXW_32BITS);
  158. slot_size = TAS2780_TDM_CFG2_RXS_32BITS;
  159. break;
  160. default:
  161. ret = -EINVAL;
  162. }
  163. if (ret < 0) {
  164. dev_err(tas2780->dev, "%s:errCode:0x%x set bitwidth error\n",
  165. __func__, ret);
  166. goto err;
  167. }
  168. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG2,
  169. TAS2780_TDM_CFG2_RXS_MASK, slot_size);
  170. if (ret < 0) {
  171. dev_err(tas2780->dev,
  172. "%s:errCode:0x%x set RX slot size error\n",
  173. __func__, ret);
  174. goto err;
  175. }
  176. val = snd_soc_component_read(tas2780->component, TAS2780_PWR_CTRL);
  177. if (val < 0) {
  178. dev_err(tas2780->dev, "%s:errCode:0x%x read PWR_CTRL error\n",
  179. __func__, val);
  180. ret = val;
  181. goto err;
  182. }
  183. if (val & (1 << TAS2780_VSENSE_POWER_EN))
  184. sense_en = 0;
  185. else
  186. sense_en = TAS2780_TDM_CFG5_VSNS_ENABLE;
  187. ret = snd_soc_component_update_bits(tas2780->component,
  188. TAS2780_TDM_CFG5, TAS2780_TDM_CFG5_VSNS_ENABLE, sense_en);
  189. if (ret < 0) {
  190. dev_err(tas2780->dev, "%s:errCode:0x%x enable vSNS error\n",
  191. __func__, ret);
  192. goto err;
  193. }
  194. if (val & (1 << TAS2780_ISENSE_POWER_EN))
  195. sense_en = 0;
  196. else
  197. sense_en = TAS2780_TDM_CFG6_ISNS_ENABLE;
  198. ret = snd_soc_component_update_bits(tas2780->component,
  199. TAS2780_TDM_CFG6, TAS2780_TDM_CFG6_ISNS_ENABLE, sense_en);
  200. if (ret < 0) {
  201. dev_err(tas2780->dev, "%s:errCode:0x%x enable iSNS error\n",
  202. __func__, ret);
  203. goto err;
  204. }
  205. ret = 0;
  206. err:
  207. return ret;
  208. }
  209. static int tas2780_set_samplerate(
  210. struct tas2780_priv *tas2780, int samplerate)
  211. {
  212. struct snd_soc_component *component = tas2780->component;
  213. int ramp_rate_val;
  214. int ret;
  215. switch (samplerate) {
  216. case 48000:
  217. ramp_rate_val = TAS2780_TDM_CFG0_SMP_48KHZ |
  218. TAS2780_TDM_CFG0_44_1_48KHZ;
  219. break;
  220. case 44100:
  221. ramp_rate_val = TAS2780_TDM_CFG0_SMP_44_1KHZ |
  222. TAS2780_TDM_CFG0_44_1_48KHZ;
  223. break;
  224. case 96000:
  225. ramp_rate_val = TAS2780_TDM_CFG0_SMP_48KHZ |
  226. TAS2780_TDM_CFG0_88_2_96KHZ;
  227. break;
  228. case 88200:
  229. ramp_rate_val = TAS2780_TDM_CFG0_SMP_44_1KHZ |
  230. TAS2780_TDM_CFG0_88_2_96KHZ;
  231. break;
  232. default:
  233. return -EINVAL;
  234. }
  235. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG0,
  236. TAS2780_TDM_CFG0_SMP_MASK | TAS2780_TDM_CFG0_MASK,
  237. ramp_rate_val);
  238. if (ret < 0) {
  239. dev_err(tas2780->dev,
  240. "%s:errCode:0x%x Failed to set ramp_rate_val\n",
  241. __func__, ret);
  242. goto err;
  243. }
  244. ret = 0;
  245. err:
  246. return ret;
  247. }
  248. static int tas2780_hw_params(struct snd_pcm_substream *substream,
  249. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  250. {
  251. struct snd_soc_component *component = dai->component;
  252. struct tas2780_priv *tas2780 =
  253. snd_soc_component_get_drvdata(component);
  254. int ret;
  255. ret = tas2780_set_bitwidth(tas2780, params_format(params));
  256. if (ret < 0)
  257. return ret;
  258. return tas2780_set_samplerate(tas2780, params_rate(params));
  259. }
  260. static int tas2780_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  261. {
  262. struct snd_soc_component *component = dai->component;
  263. struct tas2780_priv *tas2780 =
  264. snd_soc_component_get_drvdata(component);
  265. u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
  266. int iface;
  267. int ret = 0;
  268. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  269. case SND_SOC_DAIFMT_NB_NF:
  270. asi_cfg_1 = TAS2780_TDM_CFG1_RX_RISING;
  271. break;
  272. case SND_SOC_DAIFMT_IB_NF:
  273. asi_cfg_1 = TAS2780_TDM_CFG1_RX_FALLING;
  274. break;
  275. default:
  276. dev_err(tas2780->dev, "ASI format Inverse is not found\n");
  277. return -EINVAL;
  278. }
  279. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG1,
  280. TAS2780_TDM_CFG1_RX_MASK, asi_cfg_1);
  281. if (ret < 0) {
  282. dev_err(tas2780->dev,
  283. "%s:errCode:0x%x Failed to set asi_cfg_1\n",
  284. __func__, ret);
  285. goto err;
  286. }
  287. if (((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_I2S)
  288. || ((fmt & SND_SOC_DAIFMT_FORMAT_MASK)
  289. == SND_SOC_DAIFMT_DSP_A)){
  290. iface = TAS2780_TDM_CFG2_SCFG_I2S;
  291. tdm_rx_start_slot = 1;
  292. } else {
  293. if (((fmt & SND_SOC_DAIFMT_FORMAT_MASK)
  294. == SND_SOC_DAIFMT_DSP_B)
  295. || ((fmt & SND_SOC_DAIFMT_FORMAT_MASK)
  296. == SND_SOC_DAIFMT_LEFT_J)) {
  297. iface = TAS2780_TDM_CFG2_SCFG_LEFT_J;
  298. tdm_rx_start_slot = 0;
  299. } else {
  300. dev_err(tas2780->dev,
  301. "%s:DAI Format is not found, fmt=0x%x\n",
  302. __func__, fmt);
  303. ret = -EINVAL;
  304. goto err;
  305. }
  306. }
  307. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG1,
  308. TAS2780_TDM_CFG1_MASK,
  309. (tdm_rx_start_slot << TAS2780_TDM_CFG1_51_SHIFT));
  310. if (ret < 0) {
  311. dev_err(tas2780->dev,
  312. "%s:errCode:0x%x Failed to set tdm_rx_start_slot\n",
  313. __func__, ret);
  314. goto err;
  315. }
  316. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG2,
  317. TAS2780_TDM_CFG2_SCFG_MASK, iface);
  318. if (ret < 0) {
  319. dev_err(tas2780->dev, "%s:errCode:0x%x Failed to set iface\n",
  320. __func__, ret);
  321. goto err;
  322. }
  323. ret = 0;
  324. err:
  325. return ret;
  326. }
  327. static int tas2780_set_dai_tdm_slot(struct snd_soc_dai *dai,
  328. unsigned int tx_mask,
  329. unsigned int rx_mask,
  330. int slots, int slot_width)
  331. {
  332. struct snd_soc_component *component = dai->component;
  333. struct tas2780_priv *tas2780 =
  334. snd_soc_component_get_drvdata(component);
  335. int left_slot, right_slot;
  336. int slots_cfg;
  337. int slot_size;
  338. int ret = 0;
  339. if (tx_mask == 0 || rx_mask != 0)
  340. return -EINVAL;
  341. left_slot = __ffs(tx_mask);
  342. tx_mask &= ~(1 << left_slot);
  343. if (tx_mask == 0) {
  344. right_slot = left_slot;
  345. } else {
  346. right_slot = __ffs(tx_mask);
  347. tx_mask &= ~(1 << right_slot);
  348. }
  349. if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
  350. return -EINVAL;
  351. slots_cfg = (right_slot << TAS2780_TDM_CFG3_RXS_SHIFT) | left_slot;
  352. ret = snd_soc_component_write(component, TAS2780_TDM_CFG3, slots_cfg);
  353. if (ret) {
  354. dev_err(tas2780->dev,
  355. "%s:errCode:0x%x Failed to set slots_cfg\n",
  356. __func__, ret);
  357. goto err;
  358. }
  359. switch (slot_width) {
  360. case 16:
  361. slot_size = TAS2780_TDM_CFG2_RXS_16BITS;
  362. break;
  363. case 24:
  364. slot_size = TAS2780_TDM_CFG2_RXS_24BITS;
  365. break;
  366. case 32:
  367. slot_size = TAS2780_TDM_CFG2_RXS_32BITS;
  368. break;
  369. default:
  370. ret = -EINVAL;
  371. goto err;
  372. }
  373. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG2,
  374. TAS2780_TDM_CFG2_RXS_MASK, slot_size);
  375. if (ret < 0) {
  376. dev_err(tas2780->dev,
  377. "%s:errCode:0x%x Failed to set slot_size\n",
  378. __func__, ret);
  379. goto err;
  380. }
  381. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG5,
  382. TAS2780_TDM_CFG5_50_MASK, tas2780->v_sense_slot);
  383. if (ret < 0) {
  384. dev_err(tas2780->dev,
  385. "%s:errCode:0x%x Failed to set v_sense_slot\n",
  386. __func__, ret);
  387. goto err;
  388. }
  389. ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG6,
  390. TAS2780_TDM_CFG6_50_MASK, tas2780->i_sense_slot);
  391. if (ret < 0) {
  392. dev_err(tas2780->dev,
  393. "%s:errCode:0x%x Failed to set i_sense_slot\n",
  394. __func__, ret);
  395. goto err;
  396. }
  397. ret = 0;
  398. err:
  399. return ret;
  400. }
  401. static const struct snd_soc_dai_ops tas2780_dai_ops = {
  402. .mute_stream = tas2780_mute,
  403. .hw_params = tas2780_hw_params,
  404. .set_fmt = tas2780_set_fmt,
  405. .set_tdm_slot = tas2780_set_dai_tdm_slot,
  406. .no_capture_mute = 1,
  407. };
  408. #define TAS2780_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  409. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  410. #define TAS2780_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  411. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200)
  412. static struct snd_soc_dai_driver tas2780_dai_driver[] = {
  413. {
  414. .name = "tas2780 ASI1",
  415. .id = 0,
  416. .playback = {
  417. .stream_name = "ASI1 Playback",
  418. .channels_min = 2,
  419. .channels_max = 2,
  420. .rates = TAS2780_RATES,
  421. .formats = TAS2780_FORMATS,
  422. },
  423. .capture = {
  424. .stream_name = "ASI1 Capture",
  425. .channels_min = 1,
  426. .channels_max = 2,
  427. .rates = TAS2780_RATES,
  428. .formats = TAS2780_FORMATS,
  429. },
  430. .ops = &tas2780_dai_ops,
  431. .symmetric_rate = 1,
  432. },
  433. };
  434. static int tas2780_codec_probe(struct snd_soc_component *component)
  435. {
  436. struct tas2780_priv *tas2780 =
  437. snd_soc_component_get_drvdata(component);
  438. int ret = 0;
  439. tas2780->component = component;
  440. tas2780_reset(tas2780);
  441. ret = snd_soc_component_update_bits(component,
  442. TAS2780_IC_CFG, TAS2780_IC_CFG_MASK,
  443. TAS2780_IC_CFG_ENABLE);
  444. if (ret < 0)
  445. dev_err(tas2780->dev, "%s:errCode:0x%0x\n",
  446. __func__, ret);
  447. return ret;
  448. }
  449. static DECLARE_TLV_DB_SCALE(tas2780_digital_tlv, 1100, 50, 0);
  450. static DECLARE_TLV_DB_SCALE(tas2780_playback_volume, -10000, 50, 0);
  451. static const struct snd_kcontrol_new tas2780_snd_controls[] = {
  452. SOC_SINGLE_TLV("Speaker Volume", TAS2780_DVC, 0,
  453. TAS2780_DVC_MAX, 1, tas2780_playback_volume),
  454. SOC_SINGLE_TLV("Amp Gain Volume", TAS2780_CHNL_0, 0, 0x14, 0,
  455. tas2780_digital_tlv),
  456. };
  457. static const struct snd_soc_component_driver soc_component_driver_tas2780 = {
  458. .probe = tas2780_codec_probe,
  459. #ifdef CONFIG_PM
  460. .suspend = tas2780_codec_suspend,
  461. .resume = tas2780_codec_resume,
  462. #endif
  463. .controls = tas2780_snd_controls,
  464. .num_controls = ARRAY_SIZE(tas2780_snd_controls),
  465. .dapm_widgets = tas2780_dapm_widgets,
  466. .num_dapm_widgets = ARRAY_SIZE(tas2780_dapm_widgets),
  467. .dapm_routes = tas2780_audio_map,
  468. .num_dapm_routes = ARRAY_SIZE(tas2780_audio_map),
  469. .idle_bias_on = 1,
  470. .endianness = 1,
  471. };
  472. static const struct reg_default tas2780_reg_defaults[] = {
  473. { TAS2780_PAGE, 0x00 },
  474. { TAS2780_SW_RST, 0x00 },
  475. { TAS2780_PWR_CTRL, 0x1a },
  476. { TAS2780_DVC, 0x00 },
  477. { TAS2780_CHNL_0, 0x00 },
  478. { TAS2780_TDM_CFG0, 0x09 },
  479. { TAS2780_TDM_CFG1, 0x02 },
  480. { TAS2780_TDM_CFG2, 0x0a },
  481. { TAS2780_TDM_CFG3, 0x10 },
  482. { TAS2780_TDM_CFG5, 0x42 },
  483. };
  484. static const struct regmap_range_cfg tas2780_regmap_ranges[] = {
  485. {
  486. .range_min = 0,
  487. .range_max = 1 * 128,
  488. .selector_reg = TAS2780_PAGE,
  489. .selector_mask = 0xff,
  490. .selector_shift = 0,
  491. .window_start = 0,
  492. .window_len = 128,
  493. },
  494. };
  495. static const struct regmap_config tas2780_i2c_regmap = {
  496. .reg_bits = 8,
  497. .val_bits = 8,
  498. .reg_defaults = tas2780_reg_defaults,
  499. .num_reg_defaults = ARRAY_SIZE(tas2780_reg_defaults),
  500. .cache_type = REGCACHE_RBTREE,
  501. .ranges = tas2780_regmap_ranges,
  502. .num_ranges = ARRAY_SIZE(tas2780_regmap_ranges),
  503. .max_register = 1 * 128,
  504. };
  505. static int tas2780_parse_dt(struct device *dev, struct tas2780_priv *tas2780)
  506. {
  507. int ret = 0;
  508. tas2780->reset_gpio = devm_gpiod_get_optional(tas2780->dev, "reset",
  509. GPIOD_OUT_HIGH);
  510. if (IS_ERR(tas2780->reset_gpio)) {
  511. if (PTR_ERR(tas2780->reset_gpio) == -EPROBE_DEFER) {
  512. tas2780->reset_gpio = NULL;
  513. return -EPROBE_DEFER;
  514. }
  515. }
  516. ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
  517. &tas2780->i_sense_slot);
  518. if (ret)
  519. tas2780->i_sense_slot = 0;
  520. ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
  521. &tas2780->v_sense_slot);
  522. if (ret)
  523. tas2780->v_sense_slot = 2;
  524. return 0;
  525. }
  526. static int tas2780_i2c_probe(struct i2c_client *client,
  527. const struct i2c_device_id *id)
  528. {
  529. struct tas2780_priv *tas2780;
  530. int result;
  531. tas2780 = devm_kzalloc(&client->dev, sizeof(struct tas2780_priv),
  532. GFP_KERNEL);
  533. if (!tas2780)
  534. return -ENOMEM;
  535. tas2780->dev = &client->dev;
  536. i2c_set_clientdata(client, tas2780);
  537. dev_set_drvdata(&client->dev, tas2780);
  538. tas2780->regmap = devm_regmap_init_i2c(client, &tas2780_i2c_regmap);
  539. if (IS_ERR(tas2780->regmap)) {
  540. result = PTR_ERR(tas2780->regmap);
  541. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  542. result);
  543. return result;
  544. }
  545. if (client->dev.of_node) {
  546. result = tas2780_parse_dt(&client->dev, tas2780);
  547. if (result) {
  548. dev_err(tas2780->dev,
  549. "%s: Failed to parse devicetree\n", __func__);
  550. return result;
  551. }
  552. }
  553. return devm_snd_soc_register_component(tas2780->dev,
  554. &soc_component_driver_tas2780, tas2780_dai_driver,
  555. ARRAY_SIZE(tas2780_dai_driver));
  556. }
  557. static const struct i2c_device_id tas2780_i2c_id[] = {
  558. { "tas2780", 0},
  559. { }
  560. };
  561. MODULE_DEVICE_TABLE(i2c, tas2780_i2c_id);
  562. #if defined(CONFIG_OF)
  563. static const struct of_device_id tas2780_of_match[] = {
  564. { .compatible = "ti,tas2780" },
  565. {},
  566. };
  567. MODULE_DEVICE_TABLE(of, tas2780_of_match);
  568. #endif
  569. static struct i2c_driver tas2780_i2c_driver = {
  570. .driver = {
  571. .name = "tas2780",
  572. .of_match_table = of_match_ptr(tas2780_of_match),
  573. },
  574. .probe = tas2780_i2c_probe,
  575. .id_table = tas2780_i2c_id,
  576. };
  577. module_i2c_driver(tas2780_i2c_driver);
  578. MODULE_AUTHOR("Raphael Xu <[email protected]>");
  579. MODULE_DESCRIPTION("TAS2780 I2C Smart Amplifier driver");
  580. MODULE_LICENSE("GPL");