e76ce90c787ffb771c6e7f649107442d7edf034c
[platform/kernel/linux-rpi.git] / sound / soc / codecs / tas2764.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Driver for the Texas Instruments TAS2764 CODEC
4 // Copyright (C) 2020 Texas Instruments Inc.
5
6 #include <linux/module.h>
7 #include <linux/moduleparam.h>
8 #include <linux/err.h>
9 #include <linux/init.h>
10 #include <linux/delay.h>
11 #include <linux/pm.h>
12 #include <linux/i2c.h>
13 #include <linux/gpio.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/regmap.h>
17 #include <linux/of.h>
18 #include <linux/of_gpio.h>
19 #include <linux/slab.h>
20 #include <sound/soc.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/initval.h>
24 #include <sound/tlv.h>
25
26 #include "tas2764.h"
27
28 struct tas2764_priv {
29         struct snd_soc_component *component;
30         struct gpio_desc *reset_gpio;
31         struct gpio_desc *sdz_gpio;
32         struct regmap *regmap;
33         struct device *dev;
34         
35         int v_sense_slot;
36         int i_sense_slot;
37 };
38
39 static void tas2764_reset(struct tas2764_priv *tas2764)
40 {
41         if (tas2764->reset_gpio) {
42                 gpiod_set_value_cansleep(tas2764->reset_gpio, 0);
43                 msleep(20);
44                 gpiod_set_value_cansleep(tas2764->reset_gpio, 1);
45                 usleep_range(1000, 2000);
46         }
47
48         snd_soc_component_write(tas2764->component, TAS2764_SW_RST,
49                                 TAS2764_RST);
50         usleep_range(1000, 2000);
51 }
52
53 #ifdef CONFIG_PM
54 static int tas2764_codec_suspend(struct snd_soc_component *component)
55 {
56         struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
57         int ret;
58
59         ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
60                                             TAS2764_PWR_CTRL_MASK,
61                                             TAS2764_PWR_CTRL_SHUTDOWN);
62
63         if (ret < 0)
64                 return ret;
65
66         if (tas2764->sdz_gpio)
67                 gpiod_set_value_cansleep(tas2764->sdz_gpio, 0);
68
69         regcache_cache_only(tas2764->regmap, true);
70         regcache_mark_dirty(tas2764->regmap);
71
72         return 0;
73 }
74
75 static int tas2764_codec_resume(struct snd_soc_component *component)
76 {
77         struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
78         int ret;
79
80         if (tas2764->sdz_gpio) {
81                 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
82                 usleep_range(1000, 2000);
83         }
84
85         ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
86                                             TAS2764_PWR_CTRL_MASK,
87                                             TAS2764_PWR_CTRL_ACTIVE);
88
89         if (ret < 0)
90                 return ret;
91
92         regcache_cache_only(tas2764->regmap, false);
93
94         return regcache_sync(tas2764->regmap);
95 }
96 #else
97 #define tas2764_codec_suspend NULL
98 #define tas2764_codec_resume NULL
99 #endif
100
101 static const char * const tas2764_ASI1_src[] = {
102         "I2C offset", "Left", "Right", "LeftRightDiv2",
103 };
104
105 static SOC_ENUM_SINGLE_DECL(
106         tas2764_ASI1_src_enum, TAS2764_TDM_CFG2, TAS2764_TDM_CFG2_SCFG_SHIFT,
107         tas2764_ASI1_src);
108
109 static const struct snd_kcontrol_new tas2764_asi1_mux =
110         SOC_DAPM_ENUM("ASI1 Source", tas2764_ASI1_src_enum);
111
112 static int tas2764_dac_event(struct snd_soc_dapm_widget *w,
113                              struct snd_kcontrol *kcontrol, int event)
114 {
115         struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
116         struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
117         int ret;
118
119         switch (event) {
120         case SND_SOC_DAPM_POST_PMU:
121                 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
122                                                     TAS2764_PWR_CTRL_MASK,
123                                                     TAS2764_PWR_CTRL_MUTE);
124                 break;
125         case SND_SOC_DAPM_PRE_PMD:
126                 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
127                                                     TAS2764_PWR_CTRL_MASK,
128                                                     TAS2764_PWR_CTRL_SHUTDOWN);
129                 break;
130         default:
131                 dev_err(tas2764->dev, "Unsupported event\n");
132                 return -EINVAL;
133         }
134
135         if (ret < 0)
136                 return ret;
137
138         return 0;
139 }
140
141 static const struct snd_kcontrol_new isense_switch =
142         SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN, 1, 1);
143 static const struct snd_kcontrol_new vsense_switch =
144         SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN, 1, 1);
145
146 static const struct snd_soc_dapm_widget tas2764_dapm_widgets[] = {
147         SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
148         SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2764_asi1_mux),
149         SND_SOC_DAPM_SWITCH("ISENSE", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN,
150                             1, &isense_switch),
151         SND_SOC_DAPM_SWITCH("VSENSE", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN,
152                             1, &vsense_switch),
153         SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2764_dac_event,
154                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
155         SND_SOC_DAPM_OUTPUT("OUT"),
156         SND_SOC_DAPM_SIGGEN("VMON"),
157         SND_SOC_DAPM_SIGGEN("IMON")
158 };
159
160 static const struct snd_soc_dapm_route tas2764_audio_map[] = {
161         {"ASI1 Sel", "I2C offset", "ASI1"},
162         {"ASI1 Sel", "Left", "ASI1"},
163         {"ASI1 Sel", "Right", "ASI1"},
164         {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
165         {"DAC", NULL, "ASI1 Sel"},
166         {"OUT", NULL, "DAC"},
167         {"ISENSE", "Switch", "IMON"},
168         {"VSENSE", "Switch", "VMON"},
169 };
170
171 static int tas2764_mute(struct snd_soc_dai *dai, int mute, int direction)
172 {
173         struct snd_soc_component *component = dai->component;
174         int ret;
175
176         ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
177                                             TAS2764_PWR_CTRL_MASK,
178                                             mute ? TAS2764_PWR_CTRL_MUTE : 0);
179
180         if (ret < 0)
181                 return ret;
182
183         return 0;
184 }
185
186 static int tas2764_set_bitwidth(struct tas2764_priv *tas2764, int bitwidth)
187 {
188         struct snd_soc_component *component = tas2764->component;
189         int sense_en;
190         int val;
191         int ret;
192
193         switch (bitwidth) {
194         case SNDRV_PCM_FORMAT_S16_LE:
195                 ret = snd_soc_component_update_bits(component,
196                                                     TAS2764_TDM_CFG2,
197                                                     TAS2764_TDM_CFG2_RXW_MASK,
198                                                     TAS2764_TDM_CFG2_RXW_16BITS);
199                 break;
200         case SNDRV_PCM_FORMAT_S24_LE:
201                 ret = snd_soc_component_update_bits(component,
202                                                     TAS2764_TDM_CFG2,
203                                                     TAS2764_TDM_CFG2_RXW_MASK,
204                                                     TAS2764_TDM_CFG2_RXW_24BITS);
205                 break;
206         case SNDRV_PCM_FORMAT_S32_LE:
207                 ret = snd_soc_component_update_bits(component,
208                                                     TAS2764_TDM_CFG2,
209                                                     TAS2764_TDM_CFG2_RXW_MASK,
210                                                     TAS2764_TDM_CFG2_RXW_32BITS);
211                 break;
212
213         default:
214                 return -EINVAL;
215         }
216
217         if (ret < 0)
218                 return ret;
219
220         val = snd_soc_component_read(tas2764->component, TAS2764_PWR_CTRL);
221         if (val < 0)
222                 return val;
223
224         if (val & (1 << TAS2764_VSENSE_POWER_EN))
225                 sense_en = 0;
226         else
227                 sense_en = TAS2764_TDM_CFG5_VSNS_ENABLE;
228
229         ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
230                                             TAS2764_TDM_CFG5_VSNS_ENABLE,
231                                             sense_en);
232         if (ret < 0)
233                 return ret;
234
235         if (val & (1 << TAS2764_ISENSE_POWER_EN))
236                 sense_en = 0;
237         else
238                 sense_en = TAS2764_TDM_CFG6_ISNS_ENABLE;
239
240         ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
241                                             TAS2764_TDM_CFG6_ISNS_ENABLE,
242                                             sense_en);
243         if (ret < 0)
244                 return ret;
245
246         return 0;
247 }
248
249 static int tas2764_set_samplerate(struct tas2764_priv *tas2764, int samplerate)
250 {
251         struct snd_soc_component *component = tas2764->component;
252         int ramp_rate_val;
253         int ret;
254
255         switch (samplerate) {
256         case 48000:
257                 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
258                                 TAS2764_TDM_CFG0_44_1_48KHZ;
259                 break;
260         case 44100:
261                 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
262                                 TAS2764_TDM_CFG0_44_1_48KHZ;
263                 break;
264         case 96000:
265                 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
266                                 TAS2764_TDM_CFG0_88_2_96KHZ;
267                 break;
268         case 88200:
269                 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
270                                 TAS2764_TDM_CFG0_88_2_96KHZ;
271                 break;
272         default:
273                 return -EINVAL;
274         }
275
276         ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
277                                             TAS2764_TDM_CFG0_SMP_MASK |
278                                             TAS2764_TDM_CFG0_MASK,
279                                             ramp_rate_val);
280         if (ret < 0)
281                 return ret;
282
283         return 0;
284 }
285
286 static int tas2764_hw_params(struct snd_pcm_substream *substream,
287                              struct snd_pcm_hw_params *params,
288                              struct snd_soc_dai *dai)
289 {
290         struct snd_soc_component *component = dai->component;
291         struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
292         int ret;
293
294         ret = tas2764_set_bitwidth(tas2764, params_format(params));
295         if (ret < 0)
296                 return ret;
297
298         return tas2764_set_samplerate(tas2764, params_rate(params));
299 }
300
301 static int tas2764_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
302 {
303         struct snd_soc_component *component = dai->component;
304         struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
305         u8 tdm_rx_start_slot = 0, asi_cfg_0 = 0, asi_cfg_1 = 0;
306         int ret;
307
308         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
309         case SND_SOC_DAIFMT_NB_IF:
310                 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
311                 fallthrough;
312         case SND_SOC_DAIFMT_NB_NF:
313                 asi_cfg_1 = TAS2764_TDM_CFG1_RX_RISING;
314                 break;
315         case SND_SOC_DAIFMT_IB_IF:
316                 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
317                 fallthrough;
318         case SND_SOC_DAIFMT_IB_NF:
319                 asi_cfg_1 = TAS2764_TDM_CFG1_RX_FALLING;
320                 break;
321         }
322
323         ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
324                                             TAS2764_TDM_CFG1_RX_MASK,
325                                             asi_cfg_1);
326         if (ret < 0)
327                 return ret;
328
329         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
330         case SND_SOC_DAIFMT_I2S:
331                 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
332                 fallthrough;
333         case SND_SOC_DAIFMT_DSP_A:
334                 tdm_rx_start_slot = 1;
335                 break;
336         case SND_SOC_DAIFMT_DSP_B:
337         case SND_SOC_DAIFMT_LEFT_J:
338                 tdm_rx_start_slot = 0;
339                 break;
340         default:
341                 dev_err(tas2764->dev,
342                         "DAI Format is not found, fmt=0x%x\n", fmt);
343                 return -EINVAL;
344         }
345
346         ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
347                                             TAS2764_TDM_CFG0_FRAME_START,
348                                             asi_cfg_0);
349         if (ret < 0)
350                 return ret;
351
352         ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
353                                             TAS2764_TDM_CFG1_MASK,
354                                             (tdm_rx_start_slot << TAS2764_TDM_CFG1_51_SHIFT));
355         if (ret < 0)
356                 return ret;
357
358         return 0;
359 }
360
361 static int tas2764_set_dai_tdm_slot(struct snd_soc_dai *dai,
362                                 unsigned int tx_mask,
363                                 unsigned int rx_mask,
364                                 int slots, int slot_width)
365 {
366         struct snd_soc_component *component = dai->component;
367         struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
368         int left_slot, right_slot;
369         int slots_cfg;
370         int slot_size;
371         int ret;
372
373         if (tx_mask == 0 || rx_mask != 0)
374                 return -EINVAL;
375
376         if (slots == 1) {
377                 if (tx_mask != 1)
378                         return -EINVAL;
379                 left_slot = 0;
380                 right_slot = 0;
381         } else {
382                 left_slot = __ffs(tx_mask);
383                 tx_mask &= ~(1 << left_slot);
384                 if (tx_mask == 0) {
385                         right_slot = left_slot;
386                 } else {
387                         right_slot = __ffs(tx_mask);
388                         tx_mask &= ~(1 << right_slot);
389                 }
390         }
391
392         if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
393                 return -EINVAL;
394
395         slots_cfg = (right_slot << TAS2764_TDM_CFG3_RXS_SHIFT) | left_slot;
396
397         ret = snd_soc_component_write(component, TAS2764_TDM_CFG3, slots_cfg);
398         if (ret)
399                 return ret;
400
401         switch (slot_width) {
402         case 16:
403                 slot_size = TAS2764_TDM_CFG2_RXS_16BITS;
404                 break;
405         case 24:
406                 slot_size = TAS2764_TDM_CFG2_RXS_24BITS;
407                 break;
408         case 32:
409                 slot_size = TAS2764_TDM_CFG2_RXS_32BITS;
410                 break;
411         default:
412                 return -EINVAL;
413         }
414
415         ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG2,
416                                             TAS2764_TDM_CFG2_RXS_MASK,
417                                             slot_size);
418         if (ret < 0)
419                 return ret;
420
421         ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG5,
422                                             TAS2764_TDM_CFG5_50_MASK,
423                                             tas2764->v_sense_slot);
424         if (ret < 0)
425                 return ret;
426
427         ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG6,
428                                             TAS2764_TDM_CFG6_50_MASK,
429                                             tas2764->i_sense_slot);
430         if (ret < 0)
431                 return ret;
432
433         return 0;
434 }
435
436 static const struct snd_soc_dai_ops tas2764_dai_ops = {
437         .mute_stream = tas2764_mute,
438         .hw_params  = tas2764_hw_params,
439         .set_fmt    = tas2764_set_fmt,
440         .set_tdm_slot = tas2764_set_dai_tdm_slot,
441         .no_capture_mute = 1,
442 };
443
444 #define TAS2764_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
445                          SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
446
447 #define TAS2764_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
448                        SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200)
449
450 static struct snd_soc_dai_driver tas2764_dai_driver[] = {
451         {
452                 .name = "tas2764 ASI1",
453                 .id = 0,
454                 .playback = {
455                         .stream_name    = "ASI1 Playback",
456                         .channels_min   = 1,
457                         .channels_max   = 2,
458                         .rates      = TAS2764_RATES,
459                         .formats    = TAS2764_FORMATS,
460                 },
461                 .capture = {
462                         .stream_name    = "ASI1 Capture",
463                         .channels_min   = 0,
464                         .channels_max   = 2,
465                         .rates = TAS2764_RATES,
466                         .formats = TAS2764_FORMATS,
467                 },
468                 .ops = &tas2764_dai_ops,
469                 .symmetric_rate = 1,
470         },
471 };
472
473 static int tas2764_codec_probe(struct snd_soc_component *component)
474 {
475         struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
476         int ret;
477
478         tas2764->component = component;
479
480         if (tas2764->sdz_gpio) {
481                 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
482                 usleep_range(1000, 2000);
483         }
484
485         tas2764_reset(tas2764);
486
487         ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
488                                             TAS2764_TDM_CFG5_VSNS_ENABLE, 0);
489         if (ret < 0)
490                 return ret;
491
492         ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
493                                             TAS2764_TDM_CFG6_ISNS_ENABLE, 0);
494         if (ret < 0)
495                 return ret;
496
497         ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
498                                             TAS2764_PWR_CTRL_MASK,
499                                             TAS2764_PWR_CTRL_MUTE);
500         if (ret < 0)
501                 return ret;
502
503         return 0;
504 }
505
506 static DECLARE_TLV_DB_SCALE(tas2764_digital_tlv, 1100, 50, 0);
507 static DECLARE_TLV_DB_SCALE(tas2764_playback_volume, -10050, 50, 1);
508
509 static const struct snd_kcontrol_new tas2764_snd_controls[] = {
510         SOC_SINGLE_TLV("Speaker Volume", TAS2764_DVC, 0,
511                        TAS2764_DVC_MAX, 1, tas2764_playback_volume),
512         SOC_SINGLE_TLV("Amp Gain Volume", TAS2764_CHNL_0, 1, 0x14, 0,
513                        tas2764_digital_tlv),
514 };
515
516 static const struct snd_soc_component_driver soc_component_driver_tas2764 = {
517         .probe                  = tas2764_codec_probe,
518         .suspend                = tas2764_codec_suspend,
519         .resume                 = tas2764_codec_resume,
520         .controls               = tas2764_snd_controls,
521         .num_controls           = ARRAY_SIZE(tas2764_snd_controls),
522         .dapm_widgets           = tas2764_dapm_widgets,
523         .num_dapm_widgets       = ARRAY_SIZE(tas2764_dapm_widgets),
524         .dapm_routes            = tas2764_audio_map,
525         .num_dapm_routes        = ARRAY_SIZE(tas2764_audio_map),
526         .idle_bias_on           = 1,
527         .endianness             = 1,
528         .non_legacy_dai_naming  = 1,
529 };
530
531 static const struct reg_default tas2764_reg_defaults[] = {
532         { TAS2764_PAGE, 0x00 },
533         { TAS2764_SW_RST, 0x00 },
534         { TAS2764_PWR_CTRL, 0x1a },
535         { TAS2764_DVC, 0x00 },
536         { TAS2764_CHNL_0, 0x28 },
537         { TAS2764_TDM_CFG0, 0x09 },
538         { TAS2764_TDM_CFG1, 0x02 },
539         { TAS2764_TDM_CFG2, 0x0a },
540         { TAS2764_TDM_CFG3, 0x10 },
541         { TAS2764_TDM_CFG5, 0x42 },
542 };
543
544 static const struct regmap_range_cfg tas2764_regmap_ranges[] = {
545         {
546                 .range_min = 0,
547                 .range_max = 1 * 128,
548                 .selector_reg = TAS2764_PAGE,
549                 .selector_mask = 0xff,
550                 .selector_shift = 0,
551                 .window_start = 0,
552                 .window_len = 128,
553         },
554 };
555
556 static const struct regmap_config tas2764_i2c_regmap = {
557         .reg_bits = 8,
558         .val_bits = 8,
559         .reg_defaults = tas2764_reg_defaults,
560         .num_reg_defaults = ARRAY_SIZE(tas2764_reg_defaults),
561         .cache_type = REGCACHE_RBTREE,
562         .ranges = tas2764_regmap_ranges,
563         .num_ranges = ARRAY_SIZE(tas2764_regmap_ranges),
564         .max_register = 1 * 128,
565 };
566
567 static int tas2764_parse_dt(struct device *dev, struct tas2764_priv *tas2764)
568 {
569         int ret = 0;
570
571         tas2764->reset_gpio = devm_gpiod_get_optional(tas2764->dev, "reset",
572                                                       GPIOD_OUT_HIGH);
573         if (IS_ERR(tas2764->reset_gpio)) {
574                 if (PTR_ERR(tas2764->reset_gpio) == -EPROBE_DEFER) {
575                         tas2764->reset_gpio = NULL;
576                         return -EPROBE_DEFER;
577                 }
578         }
579
580         tas2764->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
581         if (IS_ERR(tas2764->sdz_gpio)) {
582                 if (PTR_ERR(tas2764->sdz_gpio) == -EPROBE_DEFER)
583                         return -EPROBE_DEFER;
584
585                 tas2764->sdz_gpio = NULL;
586         }
587
588         ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
589                                        &tas2764->i_sense_slot);
590         if (ret)
591                 tas2764->i_sense_slot = 0;
592
593         ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
594                                        &tas2764->v_sense_slot);
595         if (ret)
596                 tas2764->v_sense_slot = 2;
597
598         return 0;
599 }
600
601 static int tas2764_i2c_probe(struct i2c_client *client,
602                         const struct i2c_device_id *id)
603 {
604         struct tas2764_priv *tas2764;
605         int result;
606
607         tas2764 = devm_kzalloc(&client->dev, sizeof(struct tas2764_priv),
608                                GFP_KERNEL);
609         if (!tas2764)
610                 return -ENOMEM;
611
612         tas2764->dev = &client->dev;
613         i2c_set_clientdata(client, tas2764);
614         dev_set_drvdata(&client->dev, tas2764);
615
616         tas2764->regmap = devm_regmap_init_i2c(client, &tas2764_i2c_regmap);
617         if (IS_ERR(tas2764->regmap)) {
618                 result = PTR_ERR(tas2764->regmap);
619                 dev_err(&client->dev, "Failed to allocate register map: %d\n",
620                                         result);
621                 return result;
622         }
623
624         if (client->dev.of_node) {
625                 result = tas2764_parse_dt(&client->dev, tas2764);
626                 if (result) {
627                         dev_err(tas2764->dev, "%s: Failed to parse devicetree\n",
628                                 __func__);
629                         return result;
630                 }
631         }
632
633         return devm_snd_soc_register_component(tas2764->dev,
634                                                &soc_component_driver_tas2764,
635                                                tas2764_dai_driver,
636                                                ARRAY_SIZE(tas2764_dai_driver));
637 }
638
639 static const struct i2c_device_id tas2764_i2c_id[] = {
640         { "tas2764", 0},
641         { }
642 };
643 MODULE_DEVICE_TABLE(i2c, tas2764_i2c_id);
644
645 #if defined(CONFIG_OF)
646 static const struct of_device_id tas2764_of_match[] = {
647         { .compatible = "ti,tas2764" },
648         {},
649 };
650 MODULE_DEVICE_TABLE(of, tas2764_of_match);
651 #endif
652
653 static struct i2c_driver tas2764_i2c_driver = {
654         .driver = {
655                 .name   = "tas2764",
656                 .of_match_table = of_match_ptr(tas2764_of_match),
657         },
658         .probe      = tas2764_i2c_probe,
659         .id_table   = tas2764_i2c_id,
660 };
661 module_i2c_driver(tas2764_i2c_driver);
662
663 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
664 MODULE_DESCRIPTION("TAS2764 I2C Smart Amplifier driver");
665 MODULE_LICENSE("GPL v2");