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