tas6424.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ALSA SoC Texas Instruments TAS6424 Quad-Channel Audio Amplifier
  4. *
  5. * Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/
  6. * Author: Andreas Dannenberg <[email protected]>
  7. * Andrew F. Davis <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/errno.h>
  11. #include <linux/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regmap.h>
  15. #include <linux/slab.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/tlv.h>
  24. #include "tas6424.h"
  25. /* Define how often to check (and clear) the fault status register (in ms) */
  26. #define TAS6424_FAULT_CHECK_INTERVAL 200
  27. static const char * const tas6424_supply_names[] = {
  28. "dvdd", /* Digital power supply. Connect to 3.3-V supply. */
  29. "vbat", /* Supply used for higher voltage analog circuits. */
  30. "pvdd", /* Class-D amp output FETs supply. */
  31. };
  32. #define TAS6424_NUM_SUPPLIES ARRAY_SIZE(tas6424_supply_names)
  33. struct tas6424_data {
  34. struct device *dev;
  35. struct regmap *regmap;
  36. struct regulator_bulk_data supplies[TAS6424_NUM_SUPPLIES];
  37. struct delayed_work fault_check_work;
  38. unsigned int last_cfault;
  39. unsigned int last_fault1;
  40. unsigned int last_fault2;
  41. unsigned int last_warn;
  42. struct gpio_desc *standby_gpio;
  43. struct gpio_desc *mute_gpio;
  44. };
  45. /*
  46. * DAC digital volumes. From -103.5 to 24 dB in 0.5 dB steps. Note that
  47. * setting the gain below -100 dB (register value <0x7) is effectively a MUTE
  48. * as per device datasheet.
  49. */
  50. static DECLARE_TLV_DB_SCALE(dac_tlv, -10350, 50, 0);
  51. static const struct snd_kcontrol_new tas6424_snd_controls[] = {
  52. SOC_SINGLE_TLV("Speaker Driver CH1 Playback Volume",
  53. TAS6424_CH1_VOL_CTRL, 0, 0xff, 0, dac_tlv),
  54. SOC_SINGLE_TLV("Speaker Driver CH2 Playback Volume",
  55. TAS6424_CH2_VOL_CTRL, 0, 0xff, 0, dac_tlv),
  56. SOC_SINGLE_TLV("Speaker Driver CH3 Playback Volume",
  57. TAS6424_CH3_VOL_CTRL, 0, 0xff, 0, dac_tlv),
  58. SOC_SINGLE_TLV("Speaker Driver CH4 Playback Volume",
  59. TAS6424_CH4_VOL_CTRL, 0, 0xff, 0, dac_tlv),
  60. SOC_SINGLE_STROBE("Auto Diagnostics Switch", TAS6424_DC_DIAG_CTRL1,
  61. TAS6424_LDGBYPASS_SHIFT, 1),
  62. };
  63. static int tas6424_dac_event(struct snd_soc_dapm_widget *w,
  64. struct snd_kcontrol *kcontrol, int event)
  65. {
  66. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  67. struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
  68. dev_dbg(component->dev, "%s() event=0x%0x\n", __func__, event);
  69. if (event & SND_SOC_DAPM_POST_PMU) {
  70. /* Observe codec shutdown-to-active time */
  71. msleep(12);
  72. /* Turn on TAS6424 periodic fault checking/handling */
  73. tas6424->last_fault1 = 0;
  74. tas6424->last_fault2 = 0;
  75. tas6424->last_warn = 0;
  76. schedule_delayed_work(&tas6424->fault_check_work,
  77. msecs_to_jiffies(TAS6424_FAULT_CHECK_INTERVAL));
  78. } else if (event & SND_SOC_DAPM_PRE_PMD) {
  79. /* Disable TAS6424 periodic fault checking/handling */
  80. cancel_delayed_work_sync(&tas6424->fault_check_work);
  81. }
  82. return 0;
  83. }
  84. static const struct snd_soc_dapm_widget tas6424_dapm_widgets[] = {
  85. SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
  86. SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas6424_dac_event,
  87. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  88. SND_SOC_DAPM_OUTPUT("OUT")
  89. };
  90. static const struct snd_soc_dapm_route tas6424_audio_map[] = {
  91. { "DAC", NULL, "DAC IN" },
  92. { "OUT", NULL, "DAC" },
  93. };
  94. static int tas6424_hw_params(struct snd_pcm_substream *substream,
  95. struct snd_pcm_hw_params *params,
  96. struct snd_soc_dai *dai)
  97. {
  98. struct snd_soc_component *component = dai->component;
  99. unsigned int rate = params_rate(params);
  100. unsigned int width = params_width(params);
  101. u8 sap_ctrl = 0;
  102. dev_dbg(component->dev, "%s() rate=%u width=%u\n", __func__, rate, width);
  103. switch (rate) {
  104. case 44100:
  105. sap_ctrl |= TAS6424_SAP_RATE_44100;
  106. break;
  107. case 48000:
  108. sap_ctrl |= TAS6424_SAP_RATE_48000;
  109. break;
  110. case 96000:
  111. sap_ctrl |= TAS6424_SAP_RATE_96000;
  112. break;
  113. default:
  114. dev_err(component->dev, "unsupported sample rate: %u\n", rate);
  115. return -EINVAL;
  116. }
  117. switch (width) {
  118. case 16:
  119. sap_ctrl |= TAS6424_SAP_TDM_SLOT_SZ_16;
  120. break;
  121. case 24:
  122. break;
  123. default:
  124. dev_err(component->dev, "unsupported sample width: %u\n", width);
  125. return -EINVAL;
  126. }
  127. snd_soc_component_update_bits(component, TAS6424_SAP_CTRL,
  128. TAS6424_SAP_RATE_MASK |
  129. TAS6424_SAP_TDM_SLOT_SZ_16,
  130. sap_ctrl);
  131. return 0;
  132. }
  133. static int tas6424_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  134. {
  135. struct snd_soc_component *component = dai->component;
  136. u8 serial_format = 0;
  137. dev_dbg(component->dev, "%s() fmt=0x%0x\n", __func__, fmt);
  138. /* clock masters */
  139. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  140. case SND_SOC_DAIFMT_CBC_CFC:
  141. break;
  142. default:
  143. dev_err(component->dev, "Invalid DAI clocking\n");
  144. return -EINVAL;
  145. }
  146. /* signal polarity */
  147. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  148. case SND_SOC_DAIFMT_NB_NF:
  149. break;
  150. default:
  151. dev_err(component->dev, "Invalid DAI clock signal polarity\n");
  152. return -EINVAL;
  153. }
  154. /* interface format */
  155. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  156. case SND_SOC_DAIFMT_I2S:
  157. serial_format |= TAS6424_SAP_I2S;
  158. break;
  159. case SND_SOC_DAIFMT_DSP_A:
  160. serial_format |= TAS6424_SAP_DSP;
  161. break;
  162. case SND_SOC_DAIFMT_DSP_B:
  163. /*
  164. * We can use the fact that the TAS6424 does not care about the
  165. * LRCLK duty cycle during TDM to receive DSP_B formatted data
  166. * in LEFTJ mode (no delaying of the 1st data bit).
  167. */
  168. serial_format |= TAS6424_SAP_LEFTJ;
  169. break;
  170. case SND_SOC_DAIFMT_LEFT_J:
  171. serial_format |= TAS6424_SAP_LEFTJ;
  172. break;
  173. default:
  174. dev_err(component->dev, "Invalid DAI interface format\n");
  175. return -EINVAL;
  176. }
  177. snd_soc_component_update_bits(component, TAS6424_SAP_CTRL,
  178. TAS6424_SAP_FMT_MASK, serial_format);
  179. return 0;
  180. }
  181. static int tas6424_set_dai_tdm_slot(struct snd_soc_dai *dai,
  182. unsigned int tx_mask, unsigned int rx_mask,
  183. int slots, int slot_width)
  184. {
  185. struct snd_soc_component *component = dai->component;
  186. unsigned int first_slot, last_slot;
  187. bool sap_tdm_slot_last;
  188. dev_dbg(component->dev, "%s() tx_mask=%d rx_mask=%d\n", __func__,
  189. tx_mask, rx_mask);
  190. if (!tx_mask || !rx_mask)
  191. return 0; /* nothing needed to disable TDM mode */
  192. /*
  193. * Determine the first slot and last slot that is being requested so
  194. * we'll be able to more easily enforce certain constraints as the
  195. * TAS6424's TDM interface is not fully configurable.
  196. */
  197. first_slot = __ffs(tx_mask);
  198. last_slot = __fls(rx_mask);
  199. if (last_slot - first_slot != 4) {
  200. dev_err(component->dev, "tdm mask must cover 4 contiguous slots\n");
  201. return -EINVAL;
  202. }
  203. switch (first_slot) {
  204. case 0:
  205. sap_tdm_slot_last = false;
  206. break;
  207. case 4:
  208. sap_tdm_slot_last = true;
  209. break;
  210. default:
  211. dev_err(component->dev, "tdm mask must start at slot 0 or 4\n");
  212. return -EINVAL;
  213. }
  214. snd_soc_component_update_bits(component, TAS6424_SAP_CTRL, TAS6424_SAP_TDM_SLOT_LAST,
  215. sap_tdm_slot_last ? TAS6424_SAP_TDM_SLOT_LAST : 0);
  216. return 0;
  217. }
  218. static int tas6424_mute(struct snd_soc_dai *dai, int mute, int direction)
  219. {
  220. struct snd_soc_component *component = dai->component;
  221. struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
  222. unsigned int val;
  223. dev_dbg(component->dev, "%s() mute=%d\n", __func__, mute);
  224. if (tas6424->mute_gpio) {
  225. gpiod_set_value_cansleep(tas6424->mute_gpio, mute);
  226. return 0;
  227. }
  228. if (mute)
  229. val = TAS6424_ALL_STATE_MUTE;
  230. else
  231. val = TAS6424_ALL_STATE_PLAY;
  232. snd_soc_component_write(component, TAS6424_CH_STATE_CTRL, val);
  233. return 0;
  234. }
  235. static int tas6424_power_off(struct snd_soc_component *component)
  236. {
  237. struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
  238. int ret;
  239. snd_soc_component_write(component, TAS6424_CH_STATE_CTRL, TAS6424_ALL_STATE_HIZ);
  240. regcache_cache_only(tas6424->regmap, true);
  241. regcache_mark_dirty(tas6424->regmap);
  242. ret = regulator_bulk_disable(ARRAY_SIZE(tas6424->supplies),
  243. tas6424->supplies);
  244. if (ret < 0) {
  245. dev_err(component->dev, "failed to disable supplies: %d\n", ret);
  246. return ret;
  247. }
  248. return 0;
  249. }
  250. static int tas6424_power_on(struct snd_soc_component *component)
  251. {
  252. struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
  253. int ret;
  254. u8 chan_states;
  255. int no_auto_diags = 0;
  256. unsigned int reg_val;
  257. if (!regmap_read(tas6424->regmap, TAS6424_DC_DIAG_CTRL1, &reg_val))
  258. no_auto_diags = reg_val & TAS6424_LDGBYPASS_MASK;
  259. ret = regulator_bulk_enable(ARRAY_SIZE(tas6424->supplies),
  260. tas6424->supplies);
  261. if (ret < 0) {
  262. dev_err(component->dev, "failed to enable supplies: %d\n", ret);
  263. return ret;
  264. }
  265. regcache_cache_only(tas6424->regmap, false);
  266. ret = regcache_sync(tas6424->regmap);
  267. if (ret < 0) {
  268. dev_err(component->dev, "failed to sync regcache: %d\n", ret);
  269. return ret;
  270. }
  271. if (tas6424->mute_gpio) {
  272. gpiod_set_value_cansleep(tas6424->mute_gpio, 0);
  273. /*
  274. * channels are muted via the mute pin. Don't also mute
  275. * them via the registers so that subsequent register
  276. * access is not necessary to un-mute the channels
  277. */
  278. chan_states = TAS6424_ALL_STATE_PLAY;
  279. } else {
  280. chan_states = TAS6424_ALL_STATE_MUTE;
  281. }
  282. snd_soc_component_write(component, TAS6424_CH_STATE_CTRL, chan_states);
  283. /* any time we come out of HIZ, the output channels automatically run DC
  284. * load diagnostics if autodiagnotics are enabled. wait here until this
  285. * completes.
  286. */
  287. if (!no_auto_diags)
  288. msleep(230);
  289. return 0;
  290. }
  291. static int tas6424_set_bias_level(struct snd_soc_component *component,
  292. enum snd_soc_bias_level level)
  293. {
  294. dev_dbg(component->dev, "%s() level=%d\n", __func__, level);
  295. switch (level) {
  296. case SND_SOC_BIAS_ON:
  297. case SND_SOC_BIAS_PREPARE:
  298. break;
  299. case SND_SOC_BIAS_STANDBY:
  300. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
  301. tas6424_power_on(component);
  302. break;
  303. case SND_SOC_BIAS_OFF:
  304. tas6424_power_off(component);
  305. break;
  306. }
  307. return 0;
  308. }
  309. static struct snd_soc_component_driver soc_codec_dev_tas6424 = {
  310. .set_bias_level = tas6424_set_bias_level,
  311. .controls = tas6424_snd_controls,
  312. .num_controls = ARRAY_SIZE(tas6424_snd_controls),
  313. .dapm_widgets = tas6424_dapm_widgets,
  314. .num_dapm_widgets = ARRAY_SIZE(tas6424_dapm_widgets),
  315. .dapm_routes = tas6424_audio_map,
  316. .num_dapm_routes = ARRAY_SIZE(tas6424_audio_map),
  317. .use_pmdown_time = 1,
  318. .endianness = 1,
  319. };
  320. static const struct snd_soc_dai_ops tas6424_speaker_dai_ops = {
  321. .hw_params = tas6424_hw_params,
  322. .set_fmt = tas6424_set_dai_fmt,
  323. .set_tdm_slot = tas6424_set_dai_tdm_slot,
  324. .mute_stream = tas6424_mute,
  325. .no_capture_mute = 1,
  326. };
  327. static struct snd_soc_dai_driver tas6424_dai[] = {
  328. {
  329. .name = "tas6424-amplifier",
  330. .playback = {
  331. .stream_name = "Playback",
  332. .channels_min = 1,
  333. .channels_max = 4,
  334. .rates = TAS6424_RATES,
  335. .formats = TAS6424_FORMATS,
  336. },
  337. .ops = &tas6424_speaker_dai_ops,
  338. },
  339. };
  340. static void tas6424_fault_check_work(struct work_struct *work)
  341. {
  342. struct tas6424_data *tas6424 = container_of(work, struct tas6424_data,
  343. fault_check_work.work);
  344. struct device *dev = tas6424->dev;
  345. unsigned int reg;
  346. int ret;
  347. ret = regmap_read(tas6424->regmap, TAS6424_CHANNEL_FAULT, &reg);
  348. if (ret < 0) {
  349. dev_err(dev, "failed to read CHANNEL_FAULT register: %d\n", ret);
  350. goto out;
  351. }
  352. if (!reg) {
  353. tas6424->last_cfault = reg;
  354. goto check_global_fault1_reg;
  355. }
  356. /*
  357. * Only flag errors once for a given occurrence. This is needed as
  358. * the TAS6424 will take time clearing the fault condition internally
  359. * during which we don't want to bombard the system with the same
  360. * error message over and over.
  361. */
  362. if ((reg & TAS6424_FAULT_OC_CH1) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH1))
  363. dev_crit(dev, "experienced a channel 1 overcurrent fault\n");
  364. if ((reg & TAS6424_FAULT_OC_CH2) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH2))
  365. dev_crit(dev, "experienced a channel 2 overcurrent fault\n");
  366. if ((reg & TAS6424_FAULT_OC_CH3) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH3))
  367. dev_crit(dev, "experienced a channel 3 overcurrent fault\n");
  368. if ((reg & TAS6424_FAULT_OC_CH4) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH4))
  369. dev_crit(dev, "experienced a channel 4 overcurrent fault\n");
  370. if ((reg & TAS6424_FAULT_DC_CH1) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH1))
  371. dev_crit(dev, "experienced a channel 1 DC fault\n");
  372. if ((reg & TAS6424_FAULT_DC_CH2) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH2))
  373. dev_crit(dev, "experienced a channel 2 DC fault\n");
  374. if ((reg & TAS6424_FAULT_DC_CH3) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH3))
  375. dev_crit(dev, "experienced a channel 3 DC fault\n");
  376. if ((reg & TAS6424_FAULT_DC_CH4) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH4))
  377. dev_crit(dev, "experienced a channel 4 DC fault\n");
  378. /* Store current fault1 value so we can detect any changes next time */
  379. tas6424->last_cfault = reg;
  380. check_global_fault1_reg:
  381. ret = regmap_read(tas6424->regmap, TAS6424_GLOB_FAULT1, &reg);
  382. if (ret < 0) {
  383. dev_err(dev, "failed to read GLOB_FAULT1 register: %d\n", ret);
  384. goto out;
  385. }
  386. /*
  387. * Ignore any clock faults as there is no clean way to check for them.
  388. * We would need to start checking for those faults *after* the SAIF
  389. * stream has been setup, and stop checking *before* the stream is
  390. * stopped to avoid any false-positives. However there are no
  391. * appropriate hooks to monitor these events.
  392. */
  393. reg &= TAS6424_FAULT_PVDD_OV |
  394. TAS6424_FAULT_VBAT_OV |
  395. TAS6424_FAULT_PVDD_UV |
  396. TAS6424_FAULT_VBAT_UV;
  397. if (!reg) {
  398. tas6424->last_fault1 = reg;
  399. goto check_global_fault2_reg;
  400. }
  401. if ((reg & TAS6424_FAULT_PVDD_OV) && !(tas6424->last_fault1 & TAS6424_FAULT_PVDD_OV))
  402. dev_crit(dev, "experienced a PVDD overvoltage fault\n");
  403. if ((reg & TAS6424_FAULT_VBAT_OV) && !(tas6424->last_fault1 & TAS6424_FAULT_VBAT_OV))
  404. dev_crit(dev, "experienced a VBAT overvoltage fault\n");
  405. if ((reg & TAS6424_FAULT_PVDD_UV) && !(tas6424->last_fault1 & TAS6424_FAULT_PVDD_UV))
  406. dev_crit(dev, "experienced a PVDD undervoltage fault\n");
  407. if ((reg & TAS6424_FAULT_VBAT_UV) && !(tas6424->last_fault1 & TAS6424_FAULT_VBAT_UV))
  408. dev_crit(dev, "experienced a VBAT undervoltage fault\n");
  409. /* Store current fault1 value so we can detect any changes next time */
  410. tas6424->last_fault1 = reg;
  411. check_global_fault2_reg:
  412. ret = regmap_read(tas6424->regmap, TAS6424_GLOB_FAULT2, &reg);
  413. if (ret < 0) {
  414. dev_err(dev, "failed to read GLOB_FAULT2 register: %d\n", ret);
  415. goto out;
  416. }
  417. reg &= TAS6424_FAULT_OTSD |
  418. TAS6424_FAULT_OTSD_CH1 |
  419. TAS6424_FAULT_OTSD_CH2 |
  420. TAS6424_FAULT_OTSD_CH3 |
  421. TAS6424_FAULT_OTSD_CH4;
  422. if (!reg) {
  423. tas6424->last_fault2 = reg;
  424. goto check_warn_reg;
  425. }
  426. if ((reg & TAS6424_FAULT_OTSD) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD))
  427. dev_crit(dev, "experienced a global overtemp shutdown\n");
  428. if ((reg & TAS6424_FAULT_OTSD_CH1) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH1))
  429. dev_crit(dev, "experienced an overtemp shutdown on CH1\n");
  430. if ((reg & TAS6424_FAULT_OTSD_CH2) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH2))
  431. dev_crit(dev, "experienced an overtemp shutdown on CH2\n");
  432. if ((reg & TAS6424_FAULT_OTSD_CH3) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH3))
  433. dev_crit(dev, "experienced an overtemp shutdown on CH3\n");
  434. if ((reg & TAS6424_FAULT_OTSD_CH4) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH4))
  435. dev_crit(dev, "experienced an overtemp shutdown on CH4\n");
  436. /* Store current fault2 value so we can detect any changes next time */
  437. tas6424->last_fault2 = reg;
  438. check_warn_reg:
  439. ret = regmap_read(tas6424->regmap, TAS6424_WARN, &reg);
  440. if (ret < 0) {
  441. dev_err(dev, "failed to read WARN register: %d\n", ret);
  442. goto out;
  443. }
  444. reg &= TAS6424_WARN_VDD_UV |
  445. TAS6424_WARN_VDD_POR |
  446. TAS6424_WARN_VDD_OTW |
  447. TAS6424_WARN_VDD_OTW_CH1 |
  448. TAS6424_WARN_VDD_OTW_CH2 |
  449. TAS6424_WARN_VDD_OTW_CH3 |
  450. TAS6424_WARN_VDD_OTW_CH4;
  451. if (!reg) {
  452. tas6424->last_warn = reg;
  453. goto out;
  454. }
  455. if ((reg & TAS6424_WARN_VDD_UV) && !(tas6424->last_warn & TAS6424_WARN_VDD_UV))
  456. dev_warn(dev, "experienced a VDD under voltage condition\n");
  457. if ((reg & TAS6424_WARN_VDD_POR) && !(tas6424->last_warn & TAS6424_WARN_VDD_POR))
  458. dev_warn(dev, "experienced a VDD POR condition\n");
  459. if ((reg & TAS6424_WARN_VDD_OTW) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW))
  460. dev_warn(dev, "experienced a global overtemp warning\n");
  461. if ((reg & TAS6424_WARN_VDD_OTW_CH1) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH1))
  462. dev_warn(dev, "experienced an overtemp warning on CH1\n");
  463. if ((reg & TAS6424_WARN_VDD_OTW_CH2) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH2))
  464. dev_warn(dev, "experienced an overtemp warning on CH2\n");
  465. if ((reg & TAS6424_WARN_VDD_OTW_CH3) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH3))
  466. dev_warn(dev, "experienced an overtemp warning on CH3\n");
  467. if ((reg & TAS6424_WARN_VDD_OTW_CH4) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH4))
  468. dev_warn(dev, "experienced an overtemp warning on CH4\n");
  469. /* Store current warn value so we can detect any changes next time */
  470. tas6424->last_warn = reg;
  471. /* Clear any warnings by toggling the CLEAR_FAULT control bit */
  472. ret = regmap_write_bits(tas6424->regmap, TAS6424_MISC_CTRL3,
  473. TAS6424_CLEAR_FAULT, TAS6424_CLEAR_FAULT);
  474. if (ret < 0)
  475. dev_err(dev, "failed to write MISC_CTRL3 register: %d\n", ret);
  476. ret = regmap_write_bits(tas6424->regmap, TAS6424_MISC_CTRL3,
  477. TAS6424_CLEAR_FAULT, 0);
  478. if (ret < 0)
  479. dev_err(dev, "failed to write MISC_CTRL3 register: %d\n", ret);
  480. out:
  481. /* Schedule the next fault check at the specified interval */
  482. schedule_delayed_work(&tas6424->fault_check_work,
  483. msecs_to_jiffies(TAS6424_FAULT_CHECK_INTERVAL));
  484. }
  485. static const struct reg_default tas6424_reg_defaults[] = {
  486. { TAS6424_MODE_CTRL, 0x00 },
  487. { TAS6424_MISC_CTRL1, 0x32 },
  488. { TAS6424_MISC_CTRL2, 0x62 },
  489. { TAS6424_SAP_CTRL, 0x04 },
  490. { TAS6424_CH_STATE_CTRL, 0x55 },
  491. { TAS6424_CH1_VOL_CTRL, 0xcf },
  492. { TAS6424_CH2_VOL_CTRL, 0xcf },
  493. { TAS6424_CH3_VOL_CTRL, 0xcf },
  494. { TAS6424_CH4_VOL_CTRL, 0xcf },
  495. { TAS6424_DC_DIAG_CTRL1, 0x00 },
  496. { TAS6424_DC_DIAG_CTRL2, 0x11 },
  497. { TAS6424_DC_DIAG_CTRL3, 0x11 },
  498. { TAS6424_PIN_CTRL, 0xff },
  499. { TAS6424_AC_DIAG_CTRL1, 0x00 },
  500. { TAS6424_MISC_CTRL3, 0x00 },
  501. { TAS6424_CLIP_CTRL, 0x01 },
  502. { TAS6424_CLIP_WINDOW, 0x14 },
  503. { TAS6424_CLIP_WARN, 0x00 },
  504. { TAS6424_CBC_STAT, 0x00 },
  505. { TAS6424_MISC_CTRL4, 0x40 },
  506. };
  507. static bool tas6424_is_writable_reg(struct device *dev, unsigned int reg)
  508. {
  509. switch (reg) {
  510. case TAS6424_MODE_CTRL:
  511. case TAS6424_MISC_CTRL1:
  512. case TAS6424_MISC_CTRL2:
  513. case TAS6424_SAP_CTRL:
  514. case TAS6424_CH_STATE_CTRL:
  515. case TAS6424_CH1_VOL_CTRL:
  516. case TAS6424_CH2_VOL_CTRL:
  517. case TAS6424_CH3_VOL_CTRL:
  518. case TAS6424_CH4_VOL_CTRL:
  519. case TAS6424_DC_DIAG_CTRL1:
  520. case TAS6424_DC_DIAG_CTRL2:
  521. case TAS6424_DC_DIAG_CTRL3:
  522. case TAS6424_PIN_CTRL:
  523. case TAS6424_AC_DIAG_CTRL1:
  524. case TAS6424_MISC_CTRL3:
  525. case TAS6424_CLIP_CTRL:
  526. case TAS6424_CLIP_WINDOW:
  527. case TAS6424_CLIP_WARN:
  528. case TAS6424_CBC_STAT:
  529. case TAS6424_MISC_CTRL4:
  530. return true;
  531. default:
  532. return false;
  533. }
  534. }
  535. static bool tas6424_is_volatile_reg(struct device *dev, unsigned int reg)
  536. {
  537. switch (reg) {
  538. case TAS6424_DC_LOAD_DIAG_REP12:
  539. case TAS6424_DC_LOAD_DIAG_REP34:
  540. case TAS6424_DC_LOAD_DIAG_REPLO:
  541. case TAS6424_CHANNEL_STATE:
  542. case TAS6424_CHANNEL_FAULT:
  543. case TAS6424_GLOB_FAULT1:
  544. case TAS6424_GLOB_FAULT2:
  545. case TAS6424_WARN:
  546. case TAS6424_AC_LOAD_DIAG_REP1:
  547. case TAS6424_AC_LOAD_DIAG_REP2:
  548. case TAS6424_AC_LOAD_DIAG_REP3:
  549. case TAS6424_AC_LOAD_DIAG_REP4:
  550. return true;
  551. default:
  552. return false;
  553. }
  554. }
  555. static const struct regmap_config tas6424_regmap_config = {
  556. .reg_bits = 8,
  557. .val_bits = 8,
  558. .writeable_reg = tas6424_is_writable_reg,
  559. .volatile_reg = tas6424_is_volatile_reg,
  560. .max_register = TAS6424_MAX,
  561. .reg_defaults = tas6424_reg_defaults,
  562. .num_reg_defaults = ARRAY_SIZE(tas6424_reg_defaults),
  563. .cache_type = REGCACHE_RBTREE,
  564. };
  565. #if IS_ENABLED(CONFIG_OF)
  566. static const struct of_device_id tas6424_of_ids[] = {
  567. { .compatible = "ti,tas6424", },
  568. { },
  569. };
  570. MODULE_DEVICE_TABLE(of, tas6424_of_ids);
  571. #endif
  572. static int tas6424_i2c_probe(struct i2c_client *client)
  573. {
  574. struct device *dev = &client->dev;
  575. struct tas6424_data *tas6424;
  576. int ret;
  577. int i;
  578. tas6424 = devm_kzalloc(dev, sizeof(*tas6424), GFP_KERNEL);
  579. if (!tas6424)
  580. return -ENOMEM;
  581. dev_set_drvdata(dev, tas6424);
  582. tas6424->dev = dev;
  583. tas6424->regmap = devm_regmap_init_i2c(client, &tas6424_regmap_config);
  584. if (IS_ERR(tas6424->regmap)) {
  585. ret = PTR_ERR(tas6424->regmap);
  586. dev_err(dev, "unable to allocate register map: %d\n", ret);
  587. return ret;
  588. }
  589. /*
  590. * Get control of the standby pin and set it LOW to take the codec
  591. * out of the stand-by mode.
  592. * Note: The actual pin polarity is taken care of in the GPIO lib
  593. * according the polarity specified in the DTS.
  594. */
  595. tas6424->standby_gpio = devm_gpiod_get_optional(dev, "standby",
  596. GPIOD_OUT_LOW);
  597. if (IS_ERR(tas6424->standby_gpio)) {
  598. if (PTR_ERR(tas6424->standby_gpio) == -EPROBE_DEFER)
  599. return -EPROBE_DEFER;
  600. dev_info(dev, "failed to get standby GPIO: %ld\n",
  601. PTR_ERR(tas6424->standby_gpio));
  602. tas6424->standby_gpio = NULL;
  603. }
  604. /*
  605. * Get control of the mute pin and set it HIGH in order to start with
  606. * all the output muted.
  607. * Note: The actual pin polarity is taken care of in the GPIO lib
  608. * according the polarity specified in the DTS.
  609. */
  610. tas6424->mute_gpio = devm_gpiod_get_optional(dev, "mute",
  611. GPIOD_OUT_HIGH);
  612. if (IS_ERR(tas6424->mute_gpio)) {
  613. if (PTR_ERR(tas6424->mute_gpio) == -EPROBE_DEFER)
  614. return -EPROBE_DEFER;
  615. dev_info(dev, "failed to get nmute GPIO: %ld\n",
  616. PTR_ERR(tas6424->mute_gpio));
  617. tas6424->mute_gpio = NULL;
  618. }
  619. for (i = 0; i < ARRAY_SIZE(tas6424->supplies); i++)
  620. tas6424->supplies[i].supply = tas6424_supply_names[i];
  621. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(tas6424->supplies),
  622. tas6424->supplies);
  623. if (ret) {
  624. dev_err(dev, "unable to request supplies: %d\n", ret);
  625. return ret;
  626. }
  627. ret = regulator_bulk_enable(ARRAY_SIZE(tas6424->supplies),
  628. tas6424->supplies);
  629. if (ret) {
  630. dev_err(dev, "unable to enable supplies: %d\n", ret);
  631. return ret;
  632. }
  633. /* Reset device to establish well-defined startup state */
  634. ret = regmap_update_bits(tas6424->regmap, TAS6424_MODE_CTRL,
  635. TAS6424_RESET, TAS6424_RESET);
  636. if (ret) {
  637. dev_err(dev, "unable to reset device: %d\n", ret);
  638. goto disable_regs;
  639. }
  640. INIT_DELAYED_WORK(&tas6424->fault_check_work, tas6424_fault_check_work);
  641. ret = devm_snd_soc_register_component(dev, &soc_codec_dev_tas6424,
  642. tas6424_dai, ARRAY_SIZE(tas6424_dai));
  643. if (ret < 0) {
  644. dev_err(dev, "unable to register codec: %d\n", ret);
  645. goto disable_regs;
  646. }
  647. return 0;
  648. disable_regs:
  649. regulator_bulk_disable(ARRAY_SIZE(tas6424->supplies), tas6424->supplies);
  650. return ret;
  651. }
  652. static void tas6424_i2c_remove(struct i2c_client *client)
  653. {
  654. struct device *dev = &client->dev;
  655. struct tas6424_data *tas6424 = dev_get_drvdata(dev);
  656. int ret;
  657. cancel_delayed_work_sync(&tas6424->fault_check_work);
  658. /* put the codec in stand-by */
  659. if (tas6424->standby_gpio)
  660. gpiod_set_value_cansleep(tas6424->standby_gpio, 1);
  661. ret = regulator_bulk_disable(ARRAY_SIZE(tas6424->supplies),
  662. tas6424->supplies);
  663. if (ret < 0)
  664. dev_err(dev, "unable to disable supplies: %d\n", ret);
  665. }
  666. static const struct i2c_device_id tas6424_i2c_ids[] = {
  667. { "tas6424", 0 },
  668. { }
  669. };
  670. MODULE_DEVICE_TABLE(i2c, tas6424_i2c_ids);
  671. static struct i2c_driver tas6424_i2c_driver = {
  672. .driver = {
  673. .name = "tas6424",
  674. .of_match_table = of_match_ptr(tas6424_of_ids),
  675. },
  676. .probe_new = tas6424_i2c_probe,
  677. .remove = tas6424_i2c_remove,
  678. .id_table = tas6424_i2c_ids,
  679. };
  680. module_i2c_driver(tas6424_i2c_driver);
  681. MODULE_AUTHOR("Andreas Dannenberg <[email protected]>");
  682. MODULE_AUTHOR("Andrew F. Davis <[email protected]>");
  683. MODULE_DESCRIPTION("TAS6424 Audio amplifier driver");
  684. MODULE_LICENSE("GPL v2");