Merge branch 'for-5.6' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[platform/kernel/linux-rpi.git] / sound / soc / codecs / tas2562.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Driver for the Texas Instruments TAS2562 CODEC
4 // Copyright (C) 2019 Texas Instruments Inc.
5
6
7 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regmap.h>
13 #include <linux/slab.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/delay.h>
17
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/tlv.h>
23
24 #include "tas2562.h"
25
26 #define TAS2562_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
27                          SNDRV_PCM_FORMAT_S32_LE)
28
29 /* DVC equation involves floating point math
30  * round(10^(volume in dB/20)*2^30)
31  * so create a lookup table for 2dB step
32  */
33 static const unsigned int float_vol_db_lookup[] = {
34 0x00000d43, 0x000010b2, 0x00001505, 0x00001a67, 0x00002151,
35 0x000029f1, 0x000034cd, 0x00004279, 0x000053af, 0x0000695b,
36 0x0000695b, 0x0000a6fa, 0x0000d236, 0x000108a4, 0x00014d2a,
37 0x0001a36e, 0x00021008, 0x000298c0, 0x000344df, 0x00041d8f,
38 0x00052e5a, 0x000685c8, 0x00083621, 0x000a566d, 0x000d03a7,
39 0x0010624d, 0x0014a050, 0x0019f786, 0x0020b0bc, 0x0029279d,
40 0x0033cf8d, 0x004139d3, 0x00521d50, 0x00676044, 0x0082248a,
41 0x00a3d70a, 0x00ce4328, 0x0103ab3d, 0x0146e75d, 0x019b8c27,
42 0x02061b89, 0x028c423f, 0x03352529, 0x0409c2b0, 0x05156d68,
43 0x080e9f96, 0x0a24b062, 0x0cc509ab, 0x10137987, 0x143d1362,
44 0x197a967f, 0x2013739e, 0x28619ae9, 0x32d64617, 0x40000000
45 };
46
47 struct tas2562_data {
48         struct snd_soc_component *component;
49         struct gpio_desc *sdz_gpio;
50         struct regmap *regmap;
51         struct device *dev;
52         struct i2c_client *client;
53         int v_sense_slot;
54         int i_sense_slot;
55         int volume_lvl;
56 };
57
58 static int tas2562_set_bias_level(struct snd_soc_component *component,
59                                  enum snd_soc_bias_level level)
60 {
61         struct tas2562_data *tas2562 =
62                         snd_soc_component_get_drvdata(component);
63
64         switch (level) {
65         case SND_SOC_BIAS_ON:
66                 snd_soc_component_update_bits(component,
67                         TAS2562_PWR_CTRL,
68                         TAS2562_MODE_MASK, TAS2562_ACTIVE);
69                 break;
70         case SND_SOC_BIAS_STANDBY:
71         case SND_SOC_BIAS_PREPARE:
72                 snd_soc_component_update_bits(component,
73                         TAS2562_PWR_CTRL,
74                         TAS2562_MODE_MASK, TAS2562_MUTE);
75                 break;
76         case SND_SOC_BIAS_OFF:
77                 snd_soc_component_update_bits(component,
78                         TAS2562_PWR_CTRL,
79                         TAS2562_MODE_MASK, TAS2562_SHUTDOWN);
80                 break;
81
82         default:
83                 dev_err(tas2562->dev,
84                                 "wrong power level setting %d\n", level);
85                 return -EINVAL;
86         }
87
88         return 0;
89 }
90
91 static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate)
92 {
93         int samp_rate;
94         int ramp_rate;
95
96         switch (samplerate) {
97         case 7350:
98                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
99                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
100                 break;
101         case 8000:
102                 ramp_rate = 0;
103                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
104                 break;
105         case 14700:
106                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
107                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
108                 break;
109         case 16000:
110                 ramp_rate = 0;
111                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
112                 break;
113         case 22050:
114                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
115                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
116                 break;
117         case 24000:
118                 ramp_rate = 0;
119                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
120                 break;
121         case 29400:
122                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
123                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
124                 break;
125         case 32000:
126                 ramp_rate = 0;
127                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
128                 break;
129         case 44100:
130                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
131                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
132                 break;
133         case 48000:
134                 ramp_rate = 0;
135                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
136                 break;
137         case 88200:
138                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
139                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
140                 break;
141         case 96000:
142                 ramp_rate = 0;
143                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
144                 break;
145         case 176400:
146                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
147                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
148                 break;
149         case 192000:
150                 ramp_rate = 0;
151                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
152                 break;
153         default:
154                 dev_info(tas2562->dev, "%s, unsupported sample rate, %d\n",
155                         __func__, samplerate);
156                 return -EINVAL;
157         }
158
159         snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
160                 TAS2562_TDM_CFG0_RAMPRATE_MASK, ramp_rate);
161         snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
162                 TAS2562_TDM_CFG0_SAMPRATE_MASK, samp_rate);
163
164         return 0;
165 }
166
167 static int tas2562_set_dai_tdm_slot(struct snd_soc_dai *dai,
168                 unsigned int tx_mask, unsigned int rx_mask,
169                 int slots, int slot_width)
170 {
171         struct snd_soc_component *component = dai->component;
172         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
173         int ret = 0;
174
175         switch (slot_width) {
176         case 16:
177                 ret = snd_soc_component_update_bits(component,
178                                                     TAS2562_TDM_CFG2,
179                                                     TAS2562_TDM_CFG2_RXLEN_MASK,
180                                                     TAS2562_TDM_CFG2_RXLEN_16B);
181                 break;
182         case 24:
183                 ret = snd_soc_component_update_bits(component,
184                                                     TAS2562_TDM_CFG2,
185                                                     TAS2562_TDM_CFG2_RXLEN_MASK,
186                                                     TAS2562_TDM_CFG2_RXLEN_24B);
187                 break;
188         case 32:
189                 ret = snd_soc_component_update_bits(component,
190                                                     TAS2562_TDM_CFG2,
191                                                     TAS2562_TDM_CFG2_RXLEN_MASK,
192                                                     TAS2562_TDM_CFG2_RXLEN_32B);
193                 break;
194
195         case 0:
196                 /* Do not change slot width */
197                 break;
198         default:
199                 dev_err(tas2562->dev, "slot width not supported");
200                 ret = -EINVAL;
201         }
202
203         if (ret < 0)
204                 return ret;
205
206         return 0;
207 }
208
209 static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth)
210 {
211         int ret;
212
213         switch (bitwidth) {
214         case SNDRV_PCM_FORMAT_S16_LE:
215                 snd_soc_component_update_bits(tas2562->component,
216                                               TAS2562_TDM_CFG2,
217                                               TAS2562_TDM_CFG2_RXWLEN_MASK,
218                                               TAS2562_TDM_CFG2_RXWLEN_16B);
219                 tas2562->v_sense_slot = tas2562->i_sense_slot + 2;
220                 break;
221         case SNDRV_PCM_FORMAT_S24_LE:
222                 snd_soc_component_update_bits(tas2562->component,
223                                               TAS2562_TDM_CFG2,
224                                               TAS2562_TDM_CFG2_RXWLEN_MASK,
225                                               TAS2562_TDM_CFG2_RXWLEN_24B);
226                 tas2562->v_sense_slot = tas2562->i_sense_slot + 4;
227                 break;
228         case SNDRV_PCM_FORMAT_S32_LE:
229                 snd_soc_component_update_bits(tas2562->component,
230                                               TAS2562_TDM_CFG2,
231                                               TAS2562_TDM_CFG2_RXWLEN_MASK,
232                                               TAS2562_TDM_CFG2_RXWLEN_32B);
233                 tas2562->v_sense_slot = tas2562->i_sense_slot + 4;
234                 break;
235
236         default:
237                 dev_info(tas2562->dev, "Unsupported bitwidth format\n");
238                 return -EINVAL;
239         }
240
241         ret = snd_soc_component_update_bits(tas2562->component,
242                 TAS2562_TDM_CFG5,
243                 TAS2562_TDM_CFG5_VSNS_EN | TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
244                 TAS2562_TDM_CFG5_VSNS_EN | tas2562->v_sense_slot);
245         if (ret < 0)
246                 return ret;
247
248         ret = snd_soc_component_update_bits(tas2562->component,
249                 TAS2562_TDM_CFG6,
250                 TAS2562_TDM_CFG6_ISNS_EN | TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
251                 TAS2562_TDM_CFG6_ISNS_EN | tas2562->i_sense_slot);
252         if (ret < 0)
253                 return ret;
254
255         return 0;
256 }
257
258 static int tas2562_hw_params(struct snd_pcm_substream *substream,
259                              struct snd_pcm_hw_params *params,
260                              struct snd_soc_dai *dai)
261 {
262         struct snd_soc_component *component = dai->component;
263         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
264         int ret;
265
266         ret = tas2562_set_bitwidth(tas2562, params_format(params));
267         if (ret) {
268                 dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
269                 return ret;
270         }
271
272         ret = tas2562_set_samplerate(tas2562, params_rate(params));
273         if (ret)
274                 dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
275
276         return ret;
277 }
278
279 static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
280 {
281         struct snd_soc_component *component = dai->component;
282         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
283         u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
284         int ret;
285
286         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
287         case SND_SOC_DAIFMT_NB_NF:
288                 asi_cfg_1 = 0;
289                 break;
290         case SND_SOC_DAIFMT_IB_NF:
291                 asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING;
292                 break;
293         default:
294                 dev_err(tas2562->dev, "ASI format Inverse is not found\n");
295                 return -EINVAL;
296         }
297
298         ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
299                                             TAS2562_TDM_CFG1_RX_EDGE_MASK,
300                                             asi_cfg_1);
301         if (ret < 0) {
302                 dev_err(tas2562->dev, "Failed to set RX edge\n");
303                 return ret;
304         }
305
306         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
307         case (SND_SOC_DAIFMT_I2S):
308         case (SND_SOC_DAIFMT_DSP_A):
309         case (SND_SOC_DAIFMT_DSP_B):
310                 tdm_rx_start_slot = BIT(1);
311                 break;
312         case (SND_SOC_DAIFMT_LEFT_J):
313                 tdm_rx_start_slot = 0;
314                 break;
315         default:
316                 dev_err(tas2562->dev, "DAI Format is not found, fmt=0x%x\n",
317                         fmt);
318                 ret = -EINVAL;
319                 break;
320         }
321
322         ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
323                                             TAS2562_TDM_CFG1_RX_OFFSET_MASK,
324                                             tdm_rx_start_slot);
325
326         if (ret < 0)
327                 return ret;
328
329         return 0;
330 }
331
332 static int tas2562_mute(struct snd_soc_dai *dai, int mute)
333 {
334         struct snd_soc_component *component = dai->component;
335
336         return snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
337                                              TAS2562_MODE_MASK,
338                                              mute ? TAS2562_MUTE : 0);
339 }
340
341 static int tas2562_codec_probe(struct snd_soc_component *component)
342 {
343         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
344         int ret;
345
346         tas2562->component = component;
347
348         if (tas2562->sdz_gpio)
349                 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
350
351         ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
352                                             TAS2562_MODE_MASK, TAS2562_MUTE);
353         if (ret < 0)
354                 return ret;
355
356         return 0;
357 }
358
359 #ifdef CONFIG_PM
360 static int tas2562_suspend(struct snd_soc_component *component)
361 {
362         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
363
364         regcache_cache_only(tas2562->regmap, true);
365         regcache_mark_dirty(tas2562->regmap);
366
367         if (tas2562->sdz_gpio)
368                 gpiod_set_value_cansleep(tas2562->sdz_gpio, 0);
369
370         return 0;
371 }
372
373 static int tas2562_resume(struct snd_soc_component *component)
374 {
375         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
376
377         if (tas2562->sdz_gpio)
378                 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
379
380         regcache_cache_only(tas2562->regmap, false);
381
382         return regcache_sync(tas2562->regmap);
383 }
384 #else
385 #define tas2562_suspend NULL
386 #define tas2562_resume NULL
387 #endif
388
389 static const char * const tas2562_ASI1_src[] = {
390         "I2C offset", "Left", "Right", "LeftRightDiv2",
391 };
392
393 static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4,
394                             tas2562_ASI1_src);
395
396 static const struct snd_kcontrol_new tas2562_asi1_mux =
397         SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum);
398
399 static int tas2562_dac_event(struct snd_soc_dapm_widget *w,
400                              struct snd_kcontrol *kcontrol, int event)
401 {
402         struct snd_soc_component *component =
403                                         snd_soc_dapm_to_component(w->dapm);
404         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
405         int ret;
406
407         switch (event) {
408         case SND_SOC_DAPM_POST_PMU:
409                 ret = snd_soc_component_update_bits(component,
410                         TAS2562_PWR_CTRL,
411                         TAS2562_MODE_MASK,
412                         TAS2562_MUTE);
413                 if (ret)
414                         goto end;
415                 break;
416         case SND_SOC_DAPM_PRE_PMD:
417                 ret = snd_soc_component_update_bits(component,
418                         TAS2562_PWR_CTRL,
419                         TAS2562_MODE_MASK,
420                         TAS2562_SHUTDOWN);
421                 if (ret)
422                         goto end;
423                 break;
424         default:
425                 dev_err(tas2562->dev, "Not supported evevt\n");
426                 return -EINVAL;
427         }
428
429 end:
430         if (ret < 0)
431                 return ret;
432
433         return 0;
434 }
435
436 static int tas2562_volume_control_get(struct snd_kcontrol *kcontrol,
437                                       struct snd_ctl_elem_value *ucontrol)
438 {
439         struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
440         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
441
442         ucontrol->value.integer.value[0] = tas2562->volume_lvl;
443         return 0;
444 }
445
446 static int tas2562_volume_control_put(struct snd_kcontrol *kcontrol,
447                                       struct snd_ctl_elem_value *ucontrol)
448 {
449         struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
450         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
451         int ret;
452         u32 reg_val;
453
454         reg_val = float_vol_db_lookup[ucontrol->value.integer.value[0]/2];
455         ret = snd_soc_component_write(component, TAS2562_DVC_CFG4,
456                                       (reg_val & 0xff));
457         if (ret)
458                 return ret;
459         ret = snd_soc_component_write(component, TAS2562_DVC_CFG3,
460                                       ((reg_val >> 8) & 0xff));
461         if (ret)
462                 return ret;
463         ret = snd_soc_component_write(component, TAS2562_DVC_CFG2,
464                                       ((reg_val >> 16) & 0xff));
465         if (ret)
466                 return ret;
467         ret = snd_soc_component_write(component, TAS2562_DVC_CFG1,
468                                       ((reg_val >> 24) & 0xff));
469         if (ret)
470                 return ret;
471
472         tas2562->volume_lvl = ucontrol->value.integer.value[0];
473
474         return ret;
475 }
476
477 /* Digital Volume Control. From 0 dB to -110 dB in 1 dB steps */
478 static const DECLARE_TLV_DB_SCALE(dvc_tlv, -11000, 100, 0);
479
480 static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0);
481
482 static const struct snd_kcontrol_new isense_switch =
483         SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN,
484                         1, 1);
485
486 static const struct snd_kcontrol_new vsense_switch =
487         SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN,
488                         1, 1);
489
490 static const struct snd_kcontrol_new tas2562_snd_controls[] = {
491         SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 0, 0x1c, 0,
492                        tas2562_dac_tlv),
493         {
494                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
495                 .name = "Digital Volume Control",
496                 .index = 0,
497                 .tlv.p = dvc_tlv,
498                 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | SNDRV_CTL_ELEM_ACCESS_READWRITE,
499                 .info = snd_soc_info_volsw,
500                 .get = tas2562_volume_control_get,
501                 .put = tas2562_volume_control_put,
502                 .private_value = SOC_SINGLE_VALUE(TAS2562_DVC_CFG1, 0, 110, 0, 0) ,
503         },
504 };
505
506 static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = {
507         SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
508         SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
509         SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
510                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
511         SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch),
512         SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch),
513         SND_SOC_DAPM_SIGGEN("VMON"),
514         SND_SOC_DAPM_SIGGEN("IMON"),
515         SND_SOC_DAPM_OUTPUT("OUT"),
516 };
517
518 static const struct snd_soc_dapm_route tas2562_audio_map[] = {
519         {"ASI1 Sel", "I2C offset", "ASI1"},
520         {"ASI1 Sel", "Left", "ASI1"},
521         {"ASI1 Sel", "Right", "ASI1"},
522         {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
523         { "DAC", NULL, "ASI1 Sel" },
524         { "OUT", NULL, "DAC" },
525         {"ISENSE", "Switch", "IMON"},
526         {"VSENSE", "Switch", "VMON"},
527 };
528
529 static const struct snd_soc_component_driver soc_component_dev_tas2562 = {
530         .probe                  = tas2562_codec_probe,
531         .suspend                = tas2562_suspend,
532         .resume                 = tas2562_resume,
533         .set_bias_level         = tas2562_set_bias_level,
534         .controls               = tas2562_snd_controls,
535         .num_controls           = ARRAY_SIZE(tas2562_snd_controls),
536         .dapm_widgets           = tas2562_dapm_widgets,
537         .num_dapm_widgets       = ARRAY_SIZE(tas2562_dapm_widgets),
538         .dapm_routes            = tas2562_audio_map,
539         .num_dapm_routes        = ARRAY_SIZE(tas2562_audio_map),
540         .idle_bias_on           = 1,
541         .use_pmdown_time        = 1,
542         .endianness             = 1,
543         .non_legacy_dai_naming  = 1,
544 };
545
546 static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = {
547         .hw_params      = tas2562_hw_params,
548         .set_fmt        = tas2562_set_dai_fmt,
549         .set_tdm_slot   = tas2562_set_dai_tdm_slot,
550         .digital_mute   = tas2562_mute,
551 };
552
553 static struct snd_soc_dai_driver tas2562_dai[] = {
554         {
555                 .name = "tas2562-amplifier",
556                 .id = 0,
557                 .playback = {
558                         .stream_name    = "ASI1 Playback",
559                         .channels_min   = 2,
560                         .channels_max   = 2,
561                         .rates      = SNDRV_PCM_RATE_8000_192000,
562                         .formats    = TAS2562_FORMATS,
563                 },
564                 .capture = {
565                         .stream_name    = "ASI1 Capture",
566                         .channels_min   = 0,
567                         .channels_max   = 2,
568                         .rates          = SNDRV_PCM_RATE_8000_192000,
569                         .formats        = TAS2562_FORMATS,
570                 },
571                 .ops = &tas2562_speaker_dai_ops,
572         },
573 };
574
575 static const struct regmap_range_cfg tas2562_ranges[] = {
576         {
577                 .range_min = 0,
578                 .range_max = 5 * 128,
579                 .selector_reg = TAS2562_PAGE_CTRL,
580                 .selector_mask = 0xff,
581                 .selector_shift = 0,
582                 .window_start = 0,
583                 .window_len = 128,
584         },
585 };
586
587 static const struct reg_default tas2562_reg_defaults[] = {
588         { TAS2562_PAGE_CTRL, 0x00 },
589         { TAS2562_SW_RESET, 0x00 },
590         { TAS2562_PWR_CTRL, 0x0e },
591         { TAS2562_PB_CFG1, 0x20 },
592         { TAS2562_TDM_CFG0, 0x09 },
593         { TAS2562_TDM_CFG1, 0x02 },
594         { TAS2562_DVC_CFG1, 0x40 },
595         { TAS2562_DVC_CFG2, 0x40 },
596         { TAS2562_DVC_CFG3, 0x00 },
597         { TAS2562_DVC_CFG4, 0x00 },
598 };
599
600 static const struct regmap_config tas2562_regmap_config = {
601         .reg_bits = 8,
602         .val_bits = 8,
603
604         .max_register = 5 * 128,
605         .cache_type = REGCACHE_RBTREE,
606         .reg_defaults = tas2562_reg_defaults,
607         .num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
608         .ranges = tas2562_ranges,
609         .num_ranges = ARRAY_SIZE(tas2562_ranges),
610 };
611
612 static int tas2562_parse_dt(struct tas2562_data *tas2562)
613 {
614         struct device *dev = tas2562->dev;
615         int ret = 0;
616
617         tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down-gpio",
618                                                       GPIOD_OUT_HIGH);
619         if (IS_ERR(tas2562->sdz_gpio)) {
620                 if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER) {
621                         tas2562->sdz_gpio = NULL;
622                         return -EPROBE_DEFER;
623                 }
624         }
625
626         ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
627                         &tas2562->i_sense_slot);
628         if (ret)
629                 dev_err(dev, "Looking up %s property failed %d\n",
630                         "ti,imon-slot-no", ret);
631
632         return ret;
633 }
634
635 static int tas2562_probe(struct i2c_client *client,
636                          const struct i2c_device_id *id)
637 {
638         struct device *dev = &client->dev;
639         struct tas2562_data *data;
640         int ret;
641
642         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
643         if (!data)
644                 return -ENOMEM;
645
646         data->client = client;
647         data->dev = &client->dev;
648
649         tas2562_parse_dt(data);
650
651         data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config);
652         if (IS_ERR(data->regmap)) {
653                 ret = PTR_ERR(data->regmap);
654                 dev_err(dev, "failed to allocate register map: %d\n", ret);
655                 return ret;
656         }
657
658         dev_set_drvdata(&client->dev, data);
659
660         return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562,
661                                                tas2562_dai,
662                                                ARRAY_SIZE(tas2562_dai));
663
664 }
665
666 static const struct i2c_device_id tas2562_id[] = {
667         { "tas2562", 0 },
668         { }
669 };
670 MODULE_DEVICE_TABLE(i2c, tas2562_id);
671
672 static const struct of_device_id tas2562_of_match[] = {
673         { .compatible = "ti,tas2562", },
674         { },
675 };
676 MODULE_DEVICE_TABLE(of, tas2562_of_match);
677
678 static struct i2c_driver tas2562_i2c_driver = {
679         .driver = {
680                 .name = "tas2562",
681                 .of_match_table = of_match_ptr(tas2562_of_match),
682         },
683         .probe = tas2562_probe,
684         .id_table = tas2562_id,
685 };
686
687 module_i2c_driver(tas2562_i2c_driver);
688
689 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
690 MODULE_DESCRIPTION("TAS2562 Audio amplifier driver");
691 MODULE_LICENSE("GPL");