ASoC: Intel: bdw-rt5650: Revert SSP0 link to use dummy components
[linux-block.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 struct tas2562_data {
30         struct snd_soc_component *component;
31         struct gpio_desc *sdz_gpio;
32         struct regmap *regmap;
33         struct device *dev;
34         struct i2c_client *client;
35         int v_sense_slot;
36         int i_sense_slot;
37 };
38
39 static int tas2562_set_bias_level(struct snd_soc_component *component,
40                                  enum snd_soc_bias_level level)
41 {
42         struct tas2562_data *tas2562 =
43                         snd_soc_component_get_drvdata(component);
44
45         switch (level) {
46         case SND_SOC_BIAS_ON:
47                 snd_soc_component_update_bits(component,
48                         TAS2562_PWR_CTRL,
49                         TAS2562_MODE_MASK, TAS2562_ACTIVE);
50                 break;
51         case SND_SOC_BIAS_STANDBY:
52         case SND_SOC_BIAS_PREPARE:
53                 snd_soc_component_update_bits(component,
54                         TAS2562_PWR_CTRL,
55                         TAS2562_MODE_MASK, TAS2562_MUTE);
56                 break;
57         case SND_SOC_BIAS_OFF:
58                 snd_soc_component_update_bits(component,
59                         TAS2562_PWR_CTRL,
60                         TAS2562_MODE_MASK, TAS2562_SHUTDOWN);
61                 break;
62
63         default:
64                 dev_err(tas2562->dev,
65                                 "wrong power level setting %d\n", level);
66                 return -EINVAL;
67         }
68
69         return 0;
70 }
71
72 static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate)
73 {
74         int samp_rate;
75         int ramp_rate;
76
77         switch (samplerate) {
78         case 7350:
79                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
80                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
81                 break;
82         case 8000:
83                 ramp_rate = 0;
84                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
85                 break;
86         case 14700:
87                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
88                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
89                 break;
90         case 16000:
91                 ramp_rate = 0;
92                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
93                 break;
94         case 22050:
95                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
96                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
97                 break;
98         case 24000:
99                 ramp_rate = 0;
100                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
101                 break;
102         case 29400:
103                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
104                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
105                 break;
106         case 32000:
107                 ramp_rate = 0;
108                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
109                 break;
110         case 44100:
111                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
112                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
113                 break;
114         case 48000:
115                 ramp_rate = 0;
116                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
117                 break;
118         case 88200:
119                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
120                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
121                 break;
122         case 96000:
123                 ramp_rate = 0;
124                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
125                 break;
126         case 176400:
127                 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
128                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
129                 break;
130         case 192000:
131                 ramp_rate = 0;
132                 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
133                 break;
134         default:
135                 dev_info(tas2562->dev, "%s, unsupported sample rate, %d\n",
136                         __func__, samplerate);
137                 return -EINVAL;
138         }
139
140         snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
141                 TAS2562_TDM_CFG0_RAMPRATE_MASK, ramp_rate);
142         snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
143                 TAS2562_TDM_CFG0_SAMPRATE_MASK, samp_rate);
144
145         return 0;
146 }
147
148 static int tas2562_set_dai_tdm_slot(struct snd_soc_dai *dai,
149                 unsigned int tx_mask, unsigned int rx_mask,
150                 int slots, int slot_width)
151 {
152         struct snd_soc_component *component = dai->component;
153         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
154         int ret = 0;
155
156         switch (slot_width) {
157         case 16:
158                 ret = snd_soc_component_update_bits(component,
159                                                     TAS2562_TDM_CFG2,
160                                                     TAS2562_TDM_CFG2_RXLEN_MASK,
161                                                     TAS2562_TDM_CFG2_RXLEN_16B);
162                 break;
163         case 24:
164                 ret = snd_soc_component_update_bits(component,
165                                                     TAS2562_TDM_CFG2,
166                                                     TAS2562_TDM_CFG2_RXLEN_MASK,
167                                                     TAS2562_TDM_CFG2_RXLEN_24B);
168                 break;
169         case 32:
170                 ret = snd_soc_component_update_bits(component,
171                                                     TAS2562_TDM_CFG2,
172                                                     TAS2562_TDM_CFG2_RXLEN_MASK,
173                                                     TAS2562_TDM_CFG2_RXLEN_32B);
174                 break;
175
176         case 0:
177                 /* Do not change slot width */
178                 break;
179         default:
180                 dev_err(tas2562->dev, "slot width not supported");
181                 ret = -EINVAL;
182         }
183
184         if (ret < 0)
185                 return ret;
186
187         return 0;
188 }
189
190 static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth)
191 {
192         int ret;
193
194         switch (bitwidth) {
195         case SNDRV_PCM_FORMAT_S16_LE:
196                 snd_soc_component_update_bits(tas2562->component,
197                                               TAS2562_TDM_CFG2,
198                                               TAS2562_TDM_CFG2_RXWLEN_MASK,
199                                               TAS2562_TDM_CFG2_RXWLEN_16B);
200                 tas2562->v_sense_slot = tas2562->i_sense_slot + 2;
201                 break;
202         case SNDRV_PCM_FORMAT_S24_LE:
203                 snd_soc_component_update_bits(tas2562->component,
204                                               TAS2562_TDM_CFG2,
205                                               TAS2562_TDM_CFG2_RXWLEN_MASK,
206                                               TAS2562_TDM_CFG2_RXWLEN_24B);
207                 tas2562->v_sense_slot = tas2562->i_sense_slot + 4;
208                 break;
209         case SNDRV_PCM_FORMAT_S32_LE:
210                 snd_soc_component_update_bits(tas2562->component,
211                                               TAS2562_TDM_CFG2,
212                                               TAS2562_TDM_CFG2_RXWLEN_MASK,
213                                               TAS2562_TDM_CFG2_RXWLEN_32B);
214                 tas2562->v_sense_slot = tas2562->i_sense_slot + 4;
215                 break;
216
217         default:
218                 dev_info(tas2562->dev, "Unsupported bitwidth format\n");
219                 return -EINVAL;
220         }
221
222         ret = snd_soc_component_update_bits(tas2562->component,
223                 TAS2562_TDM_CFG5,
224                 TAS2562_TDM_CFG5_VSNS_EN | TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
225                 TAS2562_TDM_CFG5_VSNS_EN | tas2562->v_sense_slot);
226         if (ret < 0)
227                 return ret;
228
229         ret = snd_soc_component_update_bits(tas2562->component,
230                 TAS2562_TDM_CFG6,
231                 TAS2562_TDM_CFG6_ISNS_EN | TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
232                 TAS2562_TDM_CFG6_ISNS_EN | tas2562->i_sense_slot);
233         if (ret < 0)
234                 return ret;
235
236         return 0;
237 }
238
239 static int tas2562_hw_params(struct snd_pcm_substream *substream,
240                              struct snd_pcm_hw_params *params,
241                              struct snd_soc_dai *dai)
242 {
243         struct snd_soc_component *component = dai->component;
244         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
245         int ret;
246
247         ret = tas2562_set_bitwidth(tas2562, params_format(params));
248         if (ret) {
249                 dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
250                 return ret;
251         }
252
253         ret = tas2562_set_samplerate(tas2562, params_rate(params));
254         if (ret)
255                 dev_err(tas2562->dev, "set sample rate failed, %d\n", ret);
256
257         return ret;
258 }
259
260 static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
261 {
262         struct snd_soc_component *component = dai->component;
263         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
264         u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
265         int ret;
266
267         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
268         case SND_SOC_DAIFMT_NB_NF:
269                 asi_cfg_1 = 0;
270                 break;
271         case SND_SOC_DAIFMT_IB_NF:
272                 asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING;
273                 break;
274         default:
275                 dev_err(tas2562->dev, "ASI format Inverse is not found\n");
276                 return -EINVAL;
277         }
278
279         ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
280                                             TAS2562_TDM_CFG1_RX_EDGE_MASK,
281                                             asi_cfg_1);
282         if (ret < 0) {
283                 dev_err(tas2562->dev, "Failed to set RX edge\n");
284                 return ret;
285         }
286
287         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
288         case (SND_SOC_DAIFMT_I2S):
289         case (SND_SOC_DAIFMT_DSP_A):
290         case (SND_SOC_DAIFMT_DSP_B):
291                 tdm_rx_start_slot = BIT(1);
292                 break;
293         case (SND_SOC_DAIFMT_LEFT_J):
294                 tdm_rx_start_slot = 0;
295                 break;
296         default:
297                 dev_err(tas2562->dev, "DAI Format is not found, fmt=0x%x\n",
298                         fmt);
299                 ret = -EINVAL;
300                 break;
301         }
302
303         ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
304                                             TAS2562_TDM_CFG1_RX_OFFSET_MASK,
305                                             tdm_rx_start_slot);
306
307         if (ret < 0)
308                 return ret;
309
310         return 0;
311 }
312
313 static int tas2562_mute(struct snd_soc_dai *dai, int mute)
314 {
315         struct snd_soc_component *component = dai->component;
316
317         return snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
318                                              TAS2562_MODE_MASK,
319                                              mute ? TAS2562_MUTE : 0);
320 }
321
322 static int tas2562_codec_probe(struct snd_soc_component *component)
323 {
324         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
325         int ret;
326
327         tas2562->component = component;
328
329         if (tas2562->sdz_gpio)
330                 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
331
332         ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
333                                             TAS2562_MODE_MASK, TAS2562_MUTE);
334         if (ret < 0)
335                 return ret;
336
337         return 0;
338 }
339
340 #ifdef CONFIG_PM
341 static int tas2562_suspend(struct snd_soc_component *component)
342 {
343         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
344
345         regcache_cache_only(tas2562->regmap, true);
346         regcache_mark_dirty(tas2562->regmap);
347
348         if (tas2562->sdz_gpio)
349                 gpiod_set_value_cansleep(tas2562->sdz_gpio, 0);
350
351         return 0;
352 }
353
354 static int tas2562_resume(struct snd_soc_component *component)
355 {
356         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
357
358         if (tas2562->sdz_gpio)
359                 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
360
361         regcache_cache_only(tas2562->regmap, false);
362
363         return regcache_sync(tas2562->regmap);
364 }
365 #else
366 #define tas2562_suspend NULL
367 #define tas2562_resume NULL
368 #endif
369
370 static const char * const tas2562_ASI1_src[] = {
371         "I2C offset", "Left", "Right", "LeftRightDiv2",
372 };
373
374 static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4,
375                             tas2562_ASI1_src);
376
377 static const struct snd_kcontrol_new tas2562_asi1_mux =
378         SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum);
379
380 static int tas2562_dac_event(struct snd_soc_dapm_widget *w,
381                              struct snd_kcontrol *kcontrol, int event)
382 {
383         struct snd_soc_component *component =
384                                         snd_soc_dapm_to_component(w->dapm);
385         struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
386
387         switch (event) {
388         case SND_SOC_DAPM_POST_PMU:
389                 dev_info(tas2562->dev, "SND_SOC_DAPM_POST_PMU\n");
390                 break;
391         case SND_SOC_DAPM_PRE_PMD:
392                 dev_info(tas2562->dev, "SND_SOC_DAPM_PRE_PMD\n");
393                 break;
394         default:
395                 break;
396         }
397
398         return 0;
399 }
400
401 static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0);
402
403 static const struct snd_kcontrol_new isense_switch =
404         SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN,
405                         1, 1);
406
407 static const struct snd_kcontrol_new vsense_switch =
408         SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN,
409                         1, 1);
410
411 static const struct snd_kcontrol_new tas2562_snd_controls[] = {
412         SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 1, 0x1c, 0,
413                        tas2562_dac_tlv),
414 };
415
416 static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = {
417         SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
418         SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
419         SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
420         SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
421                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
422         SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch),
423         SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch),
424         SND_SOC_DAPM_SIGGEN("VMON"),
425         SND_SOC_DAPM_SIGGEN("IMON"),
426         SND_SOC_DAPM_OUTPUT("OUT"),
427 };
428
429 static const struct snd_soc_dapm_route tas2562_audio_map[] = {
430         {"ASI1 Sel", "I2C offset", "ASI1"},
431         {"ASI1 Sel", "Left", "ASI1"},
432         {"ASI1 Sel", "Right", "ASI1"},
433         {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
434         { "DAC", NULL, "DAC IN" },
435         { "OUT", NULL, "DAC" },
436         {"ISENSE", "Switch", "IMON"},
437         {"VSENSE", "Switch", "VMON"},
438 };
439
440 static const struct snd_soc_component_driver soc_component_dev_tas2562 = {
441         .probe                  = tas2562_codec_probe,
442         .suspend                = tas2562_suspend,
443         .resume                 = tas2562_resume,
444         .set_bias_level         = tas2562_set_bias_level,
445         .controls               = tas2562_snd_controls,
446         .num_controls           = ARRAY_SIZE(tas2562_snd_controls),
447         .dapm_widgets           = tas2562_dapm_widgets,
448         .num_dapm_widgets       = ARRAY_SIZE(tas2562_dapm_widgets),
449         .dapm_routes            = tas2562_audio_map,
450         .num_dapm_routes        = ARRAY_SIZE(tas2562_audio_map),
451         .idle_bias_on           = 1,
452         .use_pmdown_time        = 1,
453         .endianness             = 1,
454         .non_legacy_dai_naming  = 1,
455 };
456
457 static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = {
458         .hw_params      = tas2562_hw_params,
459         .set_fmt        = tas2562_set_dai_fmt,
460         .set_tdm_slot   = tas2562_set_dai_tdm_slot,
461         .digital_mute   = tas2562_mute,
462 };
463
464 static struct snd_soc_dai_driver tas2562_dai[] = {
465         {
466                 .name = "tas2562-amplifier",
467                 .id = 0,
468                 .playback = {
469                         .stream_name    = "ASI1 Playback",
470                         .channels_min   = 2,
471                         .channels_max   = 2,
472                         .rates      = SNDRV_PCM_RATE_8000_192000,
473                         .formats    = TAS2562_FORMATS,
474                 },
475                 .ops = &tas2562_speaker_dai_ops,
476         },
477 };
478
479 static const struct regmap_range_cfg tas2562_ranges[] = {
480         {
481                 .range_min = 0,
482                 .range_max = 5 * 128,
483                 .selector_reg = TAS2562_PAGE_CTRL,
484                 .selector_mask = 0xff,
485                 .selector_shift = 0,
486                 .window_start = 0,
487                 .window_len = 128,
488         },
489 };
490
491 static const struct reg_default tas2562_reg_defaults[] = {
492         { TAS2562_PAGE_CTRL, 0x00 },
493         { TAS2562_SW_RESET, 0x00 },
494         { TAS2562_PWR_CTRL, 0x0e },
495         { TAS2562_PB_CFG1, 0x20 },
496         { TAS2562_TDM_CFG0, 0x09 },
497         { TAS2562_TDM_CFG1, 0x02 },
498 };
499
500 static const struct regmap_config tas2562_regmap_config = {
501         .reg_bits = 8,
502         .val_bits = 8,
503
504         .max_register = 5 * 128,
505         .cache_type = REGCACHE_RBTREE,
506         .reg_defaults = tas2562_reg_defaults,
507         .num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
508         .ranges = tas2562_ranges,
509         .num_ranges = ARRAY_SIZE(tas2562_ranges),
510 };
511
512 static int tas2562_parse_dt(struct tas2562_data *tas2562)
513 {
514         struct device *dev = tas2562->dev;
515         int ret = 0;
516
517         tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down-gpio",
518                                                       GPIOD_OUT_HIGH);
519         if (IS_ERR(tas2562->sdz_gpio)) {
520                 if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER) {
521                         tas2562->sdz_gpio = NULL;
522                         return -EPROBE_DEFER;
523                 }
524         }
525
526         ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
527                         &tas2562->i_sense_slot);
528         if (ret)
529                 dev_err(dev, "Looking up %s property failed %d\n",
530                         "ti,imon-slot-no", ret);
531
532         return ret;
533 }
534
535 static int tas2562_probe(struct i2c_client *client,
536                          const struct i2c_device_id *id)
537 {
538         struct device *dev = &client->dev;
539         struct tas2562_data *data;
540         int ret;
541
542         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
543         if (!data)
544                 return -ENOMEM;
545
546         data->client = client;
547         data->dev = &client->dev;
548
549         tas2562_parse_dt(data);
550
551         data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config);
552         if (IS_ERR(data->regmap)) {
553                 ret = PTR_ERR(data->regmap);
554                 dev_err(dev, "failed to allocate register map: %d\n", ret);
555                 return ret;
556         }
557
558         dev_set_drvdata(&client->dev, data);
559
560         return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562,
561                                                tas2562_dai,
562                                                ARRAY_SIZE(tas2562_dai));
563
564 }
565
566 static const struct i2c_device_id tas2562_id[] = {
567         { "tas2562", 0 },
568         { }
569 };
570 MODULE_DEVICE_TABLE(i2c, tas2562_id);
571
572 static const struct of_device_id tas2562_of_match[] = {
573         { .compatible = "ti,tas2562", },
574         { },
575 };
576 MODULE_DEVICE_TABLE(of, tas2562_of_match);
577
578 static struct i2c_driver tas2562_i2c_driver = {
579         .driver = {
580                 .name = "tas2562",
581                 .of_match_table = of_match_ptr(tas2562_of_match),
582         },
583         .probe = tas2562_probe,
584         .id_table = tas2562_id,
585 };
586
587 module_i2c_driver(tas2562_i2c_driver);
588
589 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
590 MODULE_DESCRIPTION("TAS2562 Audio amplifier driver");
591 MODULE_LICENSE("GPL");