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