Merge remote-tracking branches 'asoc/topic/tlv320dac33', 'asoc/topic/ts3a227e', ...
[linux-2.6-microblaze.git] / sound / soc / codecs / tas6424.c
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_codec *codec = snd_soc_dapm_to_codec(w->dapm);
70         struct tas6424_data *tas6424 = snd_soc_codec_get_drvdata(codec);
71
72         dev_dbg(codec->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_codec *codec = dai->codec;
109         unsigned int rate = params_rate(params);
110         unsigned int width = params_width(params);
111         u8 sap_ctrl = 0;
112
113         dev_dbg(codec->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(codec->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(codec->dev, "unsupported sample width: %u\n", width);
138                 return -EINVAL;
139         }
140
141         snd_soc_update_bits(codec, 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_codec *codec = dai->codec;
152         u8 serial_format = 0;
153
154         dev_dbg(codec->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(codec->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(codec->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(codec->dev, "Invalid DAI interface format\n");
195                 return -EINVAL;
196         }
197
198         snd_soc_update_bits(codec, 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_codec *codec = dai->codec;
209         unsigned int first_slot, last_slot;
210         bool sap_tdm_slot_last;
211
212         dev_dbg(codec->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(codec->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(codec->dev, "tdm mask must start at slot 0 or 4\n");
240                 return -EINVAL;
241         }
242
243         snd_soc_update_bits(codec, 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_codec *codec = dai->codec;
252         unsigned int val;
253
254         dev_dbg(codec->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_write(codec, TAS6424_CH_STATE_CTRL, val);
262
263         return 0;
264 }
265
266 static int tas6424_power_off(struct snd_soc_codec *codec)
267 {
268         struct tas6424_data *tas6424 = snd_soc_codec_get_drvdata(codec);
269         int ret;
270
271         snd_soc_write(codec, 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(codec->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_codec *codec)
287 {
288         struct tas6424_data *tas6424 = snd_soc_codec_get_drvdata(codec);
289         int ret;
290
291         ret = regulator_bulk_enable(ARRAY_SIZE(tas6424->supplies),
292                                     tas6424->supplies);
293         if (ret < 0) {
294                 dev_err(codec->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(codec->dev, "failed to sync regcache: %d\n", ret);
303                 return ret;
304         }
305
306         snd_soc_write(codec, 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_codec *codec,
317                                   enum snd_soc_bias_level level)
318 {
319         dev_dbg(codec->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_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
327                         tas6424_power_on(codec);
328                 break;
329         case SND_SOC_BIAS_OFF:
330                 tas6424_power_off(codec);
331                 break;
332         }
333
334         return 0;
335 }
336
337 static struct snd_soc_codec_driver soc_codec_dev_tas6424 = {
338         .set_bias_level = tas6424_set_bias_level,
339         .idle_bias_off = true,
340
341         .component_driver = {
342                 .controls = tas6424_snd_controls,
343                 .num_controls = ARRAY_SIZE(tas6424_snd_controls),
344                 .dapm_widgets = tas6424_dapm_widgets,
345                 .num_dapm_widgets = ARRAY_SIZE(tas6424_dapm_widgets),
346                 .dapm_routes = tas6424_audio_map,
347                 .num_dapm_routes = ARRAY_SIZE(tas6424_audio_map),
348         },
349 };
350
351 static struct snd_soc_dai_ops tas6424_speaker_dai_ops = {
352         .hw_params      = tas6424_hw_params,
353         .set_fmt        = tas6424_set_dai_fmt,
354         .set_tdm_slot   = tas6424_set_dai_tdm_slot,
355         .digital_mute   = tas6424_mute,
356 };
357
358 static struct snd_soc_dai_driver tas6424_dai[] = {
359         {
360                 .name = "tas6424-amplifier",
361                 .playback = {
362                         .stream_name = "Playback",
363                         .channels_min = 1,
364                         .channels_max = 4,
365                         .rates = TAS6424_RATES,
366                         .formats = TAS6424_FORMATS,
367                 },
368                 .ops = &tas6424_speaker_dai_ops,
369         },
370 };
371
372 static void tas6424_fault_check_work(struct work_struct *work)
373 {
374         struct tas6424_data *tas6424 = container_of(work, struct tas6424_data,
375                                                     fault_check_work.work);
376         struct device *dev = tas6424->dev;
377         unsigned int reg;
378         int ret;
379
380         ret = regmap_read(tas6424->regmap, TAS6424_GLOB_FAULT1, &reg);
381         if (ret < 0) {
382                 dev_err(dev, "failed to read FAULT1 register: %d\n", ret);
383                 goto out;
384         }
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
398         if (reg)
399                 goto check_global_fault2_reg;
400
401         /*
402          * Only flag errors once for a given occurrence. This is needed as
403          * the TAS6424 will take time clearing the fault condition internally
404          * during which we don't want to bombard the system with the same
405          * error message over and over.
406          */
407         if ((reg & TAS6424_FAULT_PVDD_OV) && !(tas6424->last_fault1 & TAS6424_FAULT_PVDD_OV))
408                 dev_crit(dev, "experienced a PVDD overvoltage fault\n");
409
410         if ((reg & TAS6424_FAULT_VBAT_OV) && !(tas6424->last_fault1 & TAS6424_FAULT_VBAT_OV))
411                 dev_crit(dev, "experienced a VBAT overvoltage fault\n");
412
413         if ((reg & TAS6424_FAULT_PVDD_UV) && !(tas6424->last_fault1 & TAS6424_FAULT_PVDD_UV))
414                 dev_crit(dev, "experienced a PVDD undervoltage fault\n");
415
416         if ((reg & TAS6424_FAULT_VBAT_UV) && !(tas6424->last_fault1 & TAS6424_FAULT_VBAT_UV))
417                 dev_crit(dev, "experienced a VBAT undervoltage fault\n");
418
419         /* Store current fault1 value so we can detect any changes next time */
420         tas6424->last_fault1 = reg;
421
422 check_global_fault2_reg:
423         ret = regmap_read(tas6424->regmap, TAS6424_GLOB_FAULT2, &reg);
424         if (ret < 0) {
425                 dev_err(dev, "failed to read FAULT2 register: %d\n", ret);
426                 goto out;
427         }
428
429         reg &= TAS6424_FAULT_OTSD |
430                TAS6424_FAULT_OTSD_CH1 |
431                TAS6424_FAULT_OTSD_CH2 |
432                TAS6424_FAULT_OTSD_CH3 |
433                TAS6424_FAULT_OTSD_CH4;
434
435         if (!reg)
436                 goto check_warn_reg;
437
438         if ((reg & TAS6424_FAULT_OTSD) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD))
439                 dev_crit(dev, "experienced a global overtemp shutdown\n");
440
441         if ((reg & TAS6424_FAULT_OTSD_CH1) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH1))
442                 dev_crit(dev, "experienced an overtemp shutdown on CH1\n");
443
444         if ((reg & TAS6424_FAULT_OTSD_CH2) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH2))
445                 dev_crit(dev, "experienced an overtemp shutdown on CH2\n");
446
447         if ((reg & TAS6424_FAULT_OTSD_CH3) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH3))
448                 dev_crit(dev, "experienced an overtemp shutdown on CH3\n");
449
450         if ((reg & TAS6424_FAULT_OTSD_CH4) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH4))
451                 dev_crit(dev, "experienced an overtemp shutdown on CH4\n");
452
453         /* Store current fault2 value so we can detect any changes next time */
454         tas6424->last_fault2 = reg;
455
456 check_warn_reg:
457         ret = regmap_read(tas6424->regmap, TAS6424_WARN, &reg);
458         if (ret < 0) {
459                 dev_err(dev, "failed to read WARN register: %d\n", ret);
460                 goto out;
461         }
462
463         reg &= TAS6424_WARN_VDD_UV |
464                TAS6424_WARN_VDD_POR |
465                TAS6424_WARN_VDD_OTW |
466                TAS6424_WARN_VDD_OTW_CH1 |
467                TAS6424_WARN_VDD_OTW_CH2 |
468                TAS6424_WARN_VDD_OTW_CH3 |
469                TAS6424_WARN_VDD_OTW_CH4;
470
471         if (!reg)
472                 goto out;
473
474         if ((reg & TAS6424_WARN_VDD_UV) && !(tas6424->last_warn & TAS6424_WARN_VDD_UV))
475                 dev_warn(dev, "experienced a VDD under voltage condition\n");
476
477         if ((reg & TAS6424_WARN_VDD_POR) && !(tas6424->last_warn & TAS6424_WARN_VDD_POR))
478                 dev_warn(dev, "experienced a VDD POR condition\n");
479
480         if ((reg & TAS6424_WARN_VDD_OTW) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW))
481                 dev_warn(dev, "experienced a global overtemp warning\n");
482
483         if ((reg & TAS6424_WARN_VDD_OTW_CH1) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH1))
484                 dev_warn(dev, "experienced an overtemp warning on CH1\n");
485
486         if ((reg & TAS6424_WARN_VDD_OTW_CH2) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH2))
487                 dev_warn(dev, "experienced an overtemp warning on CH2\n");
488
489         if ((reg & TAS6424_WARN_VDD_OTW_CH3) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH3))
490                 dev_warn(dev, "experienced an overtemp warning on CH3\n");
491
492         if ((reg & TAS6424_WARN_VDD_OTW_CH4) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH4))
493                 dev_warn(dev, "experienced an overtemp warning on CH4\n");
494
495         /* Store current warn value so we can detect any changes next time */
496         tas6424->last_warn = reg;
497
498         /* Clear any faults by toggling the CLEAR_FAULT control bit */
499         ret = regmap_write_bits(tas6424->regmap, TAS6424_MISC_CTRL3,
500                                 TAS6424_CLEAR_FAULT, TAS6424_CLEAR_FAULT);
501         if (ret < 0)
502                 dev_err(dev, "failed to write MISC_CTRL3 register: %d\n", ret);
503
504         ret = regmap_write_bits(tas6424->regmap, TAS6424_MISC_CTRL3,
505                                 TAS6424_CLEAR_FAULT, 0);
506         if (ret < 0)
507                 dev_err(dev, "failed to write MISC_CTRL3 register: %d\n", ret);
508
509 out:
510         /* Schedule the next fault check at the specified interval */
511         schedule_delayed_work(&tas6424->fault_check_work,
512                               msecs_to_jiffies(TAS6424_FAULT_CHECK_INTERVAL));
513 }
514
515 static const struct reg_default tas6424_reg_defaults[] = {
516         { TAS6424_MODE_CTRL,            0x00 },
517         { TAS6424_MISC_CTRL1,           0x32 },
518         { TAS6424_MISC_CTRL2,           0x62 },
519         { TAS6424_SAP_CTRL,             0x04 },
520         { TAS6424_CH_STATE_CTRL,        0x55 },
521         { TAS6424_CH1_VOL_CTRL,         0xcf },
522         { TAS6424_CH2_VOL_CTRL,         0xcf },
523         { TAS6424_CH3_VOL_CTRL,         0xcf },
524         { TAS6424_CH4_VOL_CTRL,         0xcf },
525         { TAS6424_DC_DIAG_CTRL1,        0x00 },
526         { TAS6424_DC_DIAG_CTRL2,        0x11 },
527         { TAS6424_DC_DIAG_CTRL3,        0x11 },
528         { TAS6424_PIN_CTRL,             0xff },
529         { TAS6424_AC_DIAG_CTRL1,        0x00 },
530         { TAS6424_MISC_CTRL3,           0x00 },
531         { TAS6424_CLIP_CTRL,            0x01 },
532         { TAS6424_CLIP_WINDOW,          0x14 },
533         { TAS6424_CLIP_WARN,            0x00 },
534         { TAS6424_CBC_STAT,             0x00 },
535         { TAS6424_MISC_CTRL4,           0x40 },
536 };
537
538 static bool tas6424_is_writable_reg(struct device *dev, unsigned int reg)
539 {
540         switch (reg) {
541         case TAS6424_MODE_CTRL:
542         case TAS6424_MISC_CTRL1:
543         case TAS6424_MISC_CTRL2:
544         case TAS6424_SAP_CTRL:
545         case TAS6424_CH_STATE_CTRL:
546         case TAS6424_CH1_VOL_CTRL:
547         case TAS6424_CH2_VOL_CTRL:
548         case TAS6424_CH3_VOL_CTRL:
549         case TAS6424_CH4_VOL_CTRL:
550         case TAS6424_DC_DIAG_CTRL1:
551         case TAS6424_DC_DIAG_CTRL2:
552         case TAS6424_DC_DIAG_CTRL3:
553         case TAS6424_PIN_CTRL:
554         case TAS6424_AC_DIAG_CTRL1:
555         case TAS6424_MISC_CTRL3:
556         case TAS6424_CLIP_CTRL:
557         case TAS6424_CLIP_WINDOW:
558         case TAS6424_CLIP_WARN:
559         case TAS6424_CBC_STAT:
560         case TAS6424_MISC_CTRL4:
561                 return true;
562         default:
563                 return false;
564         }
565 }
566
567 static bool tas6424_is_volatile_reg(struct device *dev, unsigned int reg)
568 {
569         switch (reg) {
570         case TAS6424_DC_LOAD_DIAG_REP12:
571         case TAS6424_DC_LOAD_DIAG_REP34:
572         case TAS6424_DC_LOAD_DIAG_REPLO:
573         case TAS6424_CHANNEL_STATE:
574         case TAS6424_CHANNEL_FAULT:
575         case TAS6424_GLOB_FAULT1:
576         case TAS6424_GLOB_FAULT2:
577         case TAS6424_WARN:
578         case TAS6424_AC_LOAD_DIAG_REP1:
579         case TAS6424_AC_LOAD_DIAG_REP2:
580         case TAS6424_AC_LOAD_DIAG_REP3:
581         case TAS6424_AC_LOAD_DIAG_REP4:
582                 return true;
583         default:
584                 return false;
585         }
586 }
587
588 static const struct regmap_config tas6424_regmap_config = {
589         .reg_bits = 8,
590         .val_bits = 8,
591
592         .writeable_reg = tas6424_is_writable_reg,
593         .volatile_reg = tas6424_is_volatile_reg,
594
595         .max_register = TAS6424_MAX,
596         .reg_defaults = tas6424_reg_defaults,
597         .num_reg_defaults = ARRAY_SIZE(tas6424_reg_defaults),
598         .cache_type = REGCACHE_RBTREE,
599 };
600
601 #if IS_ENABLED(CONFIG_OF)
602 static const struct of_device_id tas6424_of_ids[] = {
603         { .compatible = "ti,tas6424", },
604         { },
605 };
606 MODULE_DEVICE_TABLE(of, tas6424_of_ids);
607 #endif
608
609 static int tas6424_i2c_probe(struct i2c_client *client,
610                              const struct i2c_device_id *id)
611 {
612         struct device *dev = &client->dev;
613         struct tas6424_data *tas6424;
614         int ret;
615         int i;
616
617         tas6424 = devm_kzalloc(dev, sizeof(*tas6424), GFP_KERNEL);
618         if (!tas6424)
619                 return -ENOMEM;
620         dev_set_drvdata(dev, tas6424);
621
622         tas6424->dev = dev;
623
624         tas6424->regmap = devm_regmap_init_i2c(client, &tas6424_regmap_config);
625         if (IS_ERR(tas6424->regmap)) {
626                 ret = PTR_ERR(tas6424->regmap);
627                 dev_err(dev, "unable to allocate register map: %d\n", ret);
628                 return ret;
629         }
630
631         for (i = 0; i < ARRAY_SIZE(tas6424->supplies); i++)
632                 tas6424->supplies[i].supply = tas6424_supply_names[i];
633         ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(tas6424->supplies),
634                                       tas6424->supplies);
635         if (ret) {
636                 dev_err(dev, "unable to request supplies: %d\n", ret);
637                 return ret;
638         }
639
640         ret = regulator_bulk_enable(ARRAY_SIZE(tas6424->supplies),
641                                     tas6424->supplies);
642         if (ret) {
643                 dev_err(dev, "unable to enable supplies: %d\n", ret);
644                 return ret;
645         }
646
647         /* Reset device to establish well-defined startup state */
648         ret = regmap_update_bits(tas6424->regmap, TAS6424_MODE_CTRL,
649                                  TAS6424_RESET, TAS6424_RESET);
650         if (ret) {
651                 dev_err(dev, "unable to reset device: %d\n", ret);
652                 return ret;
653         }
654
655         INIT_DELAYED_WORK(&tas6424->fault_check_work, tas6424_fault_check_work);
656
657         ret = snd_soc_register_codec(dev, &soc_codec_dev_tas6424,
658                                      tas6424_dai, ARRAY_SIZE(tas6424_dai));
659         if (ret < 0) {
660                 dev_err(dev, "unable to register codec: %d\n", ret);
661                 return ret;
662         }
663
664         return 0;
665 }
666
667 static int tas6424_i2c_remove(struct i2c_client *client)
668 {
669         struct device *dev = &client->dev;
670         struct tas6424_data *tas6424 = dev_get_drvdata(dev);
671         int ret;
672
673         snd_soc_unregister_codec(dev);
674
675         cancel_delayed_work_sync(&tas6424->fault_check_work);
676
677         ret = regulator_bulk_disable(ARRAY_SIZE(tas6424->supplies),
678                                      tas6424->supplies);
679         if (ret < 0) {
680                 dev_err(dev, "unable to disable supplies: %d\n", ret);
681                 return ret;
682         }
683
684         return 0;
685 }
686
687 static const struct i2c_device_id tas6424_i2c_ids[] = {
688         { "tas6424", 0 },
689         { }
690 };
691 MODULE_DEVICE_TABLE(i2c, tas6424_i2c_ids);
692
693 static struct i2c_driver tas6424_i2c_driver = {
694         .driver = {
695                 .name = "tas6424",
696                 .of_match_table = of_match_ptr(tas6424_of_ids),
697         },
698         .probe = tas6424_i2c_probe,
699         .remove = tas6424_i2c_remove,
700         .id_table = tas6424_i2c_ids,
701 };
702 module_i2c_driver(tas6424_i2c_driver);
703
704 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>");
705 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
706 MODULE_DESCRIPTION("TAS6424 Audio amplifier driver");
707 MODULE_LICENSE("GPL v2");