treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / codecs / tas2562.c
blob729acd874c485cebd0826fa269430a3a57330878
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Driver for the Texas Instruments TAS2562 CODEC
4 // Copyright (C) 2019 Texas Instruments Inc.
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>
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>
24 #include "tas2562.h"
26 #define TAS2562_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
27 SNDRV_PCM_FORMAT_S32_LE)
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;
39 static int tas2562_set_bias_level(struct snd_soc_component *component,
40 enum snd_soc_bias_level level)
42 struct tas2562_data *tas2562 =
43 snd_soc_component_get_drvdata(component);
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;
63 default:
64 dev_err(tas2562->dev,
65 "wrong power level setting %d\n", level);
66 return -EINVAL;
69 return 0;
72 static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate)
74 int samp_rate;
75 int ramp_rate;
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;
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);
145 return 0;
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)
152 struct snd_soc_component *component = dai->component;
153 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
154 int ret = 0;
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;
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;
184 if (ret < 0)
185 return ret;
187 return 0;
190 static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth)
192 int ret;
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;
217 default:
218 dev_info(tas2562->dev, "Not supported params format\n");
221 ret = snd_soc_component_update_bits(tas2562->component,
222 TAS2562_TDM_CFG5,
223 TAS2562_TDM_CFG5_VSNS_EN | TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
224 TAS2562_TDM_CFG5_VSNS_EN | tas2562->v_sense_slot);
225 if (ret < 0)
226 return ret;
228 ret = snd_soc_component_update_bits(tas2562->component,
229 TAS2562_TDM_CFG6,
230 TAS2562_TDM_CFG6_ISNS_EN | TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
231 TAS2562_TDM_CFG6_ISNS_EN | tas2562->i_sense_slot);
232 if (ret < 0)
233 return ret;
235 return 0;
238 static int tas2562_hw_params(struct snd_pcm_substream *substream,
239 struct snd_pcm_hw_params *params,
240 struct snd_soc_dai *dai)
242 struct snd_soc_component *component = dai->component;
243 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
244 int ret;
246 ret = tas2562_set_bitwidth(tas2562, params_format(params));
247 if (ret) {
248 dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
249 return ret;
252 ret = tas2562_set_samplerate(tas2562, params_rate(params));
253 if (ret)
254 dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
256 return ret;
259 static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
261 struct snd_soc_component *component = dai->component;
262 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
263 u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
264 int ret;
266 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
267 case SND_SOC_DAIFMT_NB_NF:
268 asi_cfg_1 = 0;
269 break;
270 case SND_SOC_DAIFMT_IB_NF:
271 asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING;
272 break;
273 default:
274 dev_err(tas2562->dev, "ASI format Inverse is not found\n");
275 return -EINVAL;
278 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
279 TAS2562_TDM_CFG1_RX_EDGE_MASK,
280 asi_cfg_1);
281 if (ret < 0) {
282 dev_err(tas2562->dev, "Failed to set RX edge\n");
283 return ret;
286 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
287 case (SND_SOC_DAIFMT_I2S):
288 case (SND_SOC_DAIFMT_DSP_A):
289 case (SND_SOC_DAIFMT_DSP_B):
290 tdm_rx_start_slot = BIT(1);
291 break;
292 case (SND_SOC_DAIFMT_LEFT_J):
293 tdm_rx_start_slot = 0;
294 break;
295 default:
296 dev_err(tas2562->dev, "DAI Format is not found, fmt=0x%x\n",
297 fmt);
298 ret = -EINVAL;
299 break;
302 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
303 TAS2562_TDM_CFG1_RX_OFFSET_MASK,
304 tdm_rx_start_slot);
306 if (ret < 0)
307 return ret;
309 return 0;
312 static int tas2562_mute(struct snd_soc_dai *dai, int mute)
314 struct snd_soc_component *component = dai->component;
316 return snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
317 TAS2562_MODE_MASK,
318 mute ? TAS2562_MUTE : 0);
321 static int tas2562_codec_probe(struct snd_soc_component *component)
323 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
324 int ret;
326 tas2562->component = component;
328 if (tas2562->sdz_gpio)
329 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
331 ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
332 TAS2562_MODE_MASK, TAS2562_MUTE);
333 if (ret < 0)
334 return ret;
336 return 0;
339 #ifdef CONFIG_PM
340 static int tas2562_suspend(struct snd_soc_component *component)
342 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
344 regcache_cache_only(tas2562->regmap, true);
345 regcache_mark_dirty(tas2562->regmap);
347 if (tas2562->sdz_gpio)
348 gpiod_set_value_cansleep(tas2562->sdz_gpio, 0);
350 return 0;
353 static int tas2562_resume(struct snd_soc_component *component)
355 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
357 if (tas2562->sdz_gpio)
358 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
360 regcache_cache_only(tas2562->regmap, false);
362 return regcache_sync(tas2562->regmap);
364 #else
365 #define tas2562_suspend NULL
366 #define tas2562_resume NULL
367 #endif
369 static const char * const tas2562_ASI1_src[] = {
370 "I2C offset", "Left", "Right", "LeftRightDiv2",
373 static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4,
374 tas2562_ASI1_src);
376 static const struct snd_kcontrol_new tas2562_asi1_mux =
377 SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum);
379 static int tas2562_dac_event(struct snd_soc_dapm_widget *w,
380 struct snd_kcontrol *kcontrol, int event)
382 struct snd_soc_component *component =
383 snd_soc_dapm_to_component(w->dapm);
384 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
386 switch (event) {
387 case SND_SOC_DAPM_POST_PMU:
388 dev_info(tas2562->dev, "SND_SOC_DAPM_POST_PMU\n");
389 break;
390 case SND_SOC_DAPM_PRE_PMD:
391 dev_info(tas2562->dev, "SND_SOC_DAPM_PRE_PMD\n");
392 break;
393 default:
394 break;
397 return 0;
400 static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0);
402 static const struct snd_kcontrol_new isense_switch =
403 SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN,
404 1, 1);
406 static const struct snd_kcontrol_new vsense_switch =
407 SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN,
408 1, 1);
410 static const struct snd_kcontrol_new tas2562_snd_controls[] = {
411 SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 0, 0x1c, 0,
412 tas2562_dac_tlv),
415 static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = {
416 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
417 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
418 SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
419 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
420 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
421 SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch),
422 SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch),
423 SND_SOC_DAPM_SIGGEN("VMON"),
424 SND_SOC_DAPM_SIGGEN("IMON"),
425 SND_SOC_DAPM_OUTPUT("OUT"),
428 static const struct snd_soc_dapm_route tas2562_audio_map[] = {
429 {"ASI1 Sel", "I2C offset", "ASI1"},
430 {"ASI1 Sel", "Left", "ASI1"},
431 {"ASI1 Sel", "Right", "ASI1"},
432 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
433 { "DAC", NULL, "DAC IN" },
434 { "OUT", NULL, "DAC" },
435 {"ISENSE", "Switch", "IMON"},
436 {"VSENSE", "Switch", "VMON"},
439 static const struct snd_soc_component_driver soc_component_dev_tas2562 = {
440 .probe = tas2562_codec_probe,
441 .suspend = tas2562_suspend,
442 .resume = tas2562_resume,
443 .set_bias_level = tas2562_set_bias_level,
444 .controls = tas2562_snd_controls,
445 .num_controls = ARRAY_SIZE(tas2562_snd_controls),
446 .dapm_widgets = tas2562_dapm_widgets,
447 .num_dapm_widgets = ARRAY_SIZE(tas2562_dapm_widgets),
448 .dapm_routes = tas2562_audio_map,
449 .num_dapm_routes = ARRAY_SIZE(tas2562_audio_map),
450 .idle_bias_on = 1,
451 .use_pmdown_time = 1,
452 .endianness = 1,
453 .non_legacy_dai_naming = 1,
456 static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = {
457 .hw_params = tas2562_hw_params,
458 .set_fmt = tas2562_set_dai_fmt,
459 .set_tdm_slot = tas2562_set_dai_tdm_slot,
460 .digital_mute = tas2562_mute,
463 static struct snd_soc_dai_driver tas2562_dai[] = {
465 .name = "tas2562-amplifier",
466 .id = 0,
467 .playback = {
468 .stream_name = "ASI1 Playback",
469 .channels_min = 2,
470 .channels_max = 2,
471 .rates = SNDRV_PCM_RATE_8000_192000,
472 .formats = TAS2562_FORMATS,
474 .ops = &tas2562_speaker_dai_ops,
478 static const struct regmap_range_cfg tas2562_ranges[] = {
480 .range_min = 0,
481 .range_max = 5 * 128,
482 .selector_reg = TAS2562_PAGE_CTRL,
483 .selector_mask = 0xff,
484 .selector_shift = 0,
485 .window_start = 0,
486 .window_len = 128,
490 static const struct reg_default tas2562_reg_defaults[] = {
491 { TAS2562_PAGE_CTRL, 0x00 },
492 { TAS2562_SW_RESET, 0x00 },
493 { TAS2562_PWR_CTRL, 0x0e },
494 { TAS2562_PB_CFG1, 0x20 },
495 { TAS2562_TDM_CFG0, 0x09 },
496 { TAS2562_TDM_CFG1, 0x02 },
499 static const struct regmap_config tas2562_regmap_config = {
500 .reg_bits = 8,
501 .val_bits = 8,
503 .max_register = 5 * 128,
504 .cache_type = REGCACHE_RBTREE,
505 .reg_defaults = tas2562_reg_defaults,
506 .num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
507 .ranges = tas2562_ranges,
508 .num_ranges = ARRAY_SIZE(tas2562_ranges),
511 static int tas2562_parse_dt(struct tas2562_data *tas2562)
513 struct device *dev = tas2562->dev;
514 int ret = 0;
516 tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down-gpio",
517 GPIOD_OUT_HIGH);
518 if (IS_ERR(tas2562->sdz_gpio)) {
519 if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER) {
520 tas2562->sdz_gpio = NULL;
521 return -EPROBE_DEFER;
525 ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
526 &tas2562->i_sense_slot);
527 if (ret)
528 dev_err(dev, "Looking up %s property failed %d\n",
529 "ti,imon-slot-no", ret);
531 return ret;
534 static int tas2562_probe(struct i2c_client *client,
535 const struct i2c_device_id *id)
537 struct device *dev = &client->dev;
538 struct tas2562_data *data;
539 int ret;
541 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
542 if (!data)
543 return -ENOMEM;
545 data->client = client;
546 data->dev = &client->dev;
548 tas2562_parse_dt(data);
550 data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config);
551 if (IS_ERR(data->regmap)) {
552 ret = PTR_ERR(data->regmap);
553 dev_err(dev, "failed to allocate register map: %d\n", ret);
554 return ret;
557 dev_set_drvdata(&client->dev, data);
559 return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562,
560 tas2562_dai,
561 ARRAY_SIZE(tas2562_dai));
565 static const struct i2c_device_id tas2562_id[] = {
566 { "tas2562", 0 },
569 MODULE_DEVICE_TABLE(i2c, tas2562_id);
571 static const struct of_device_id tas2562_of_match[] = {
572 { .compatible = "ti,tas2562", },
573 { },
575 MODULE_DEVICE_TABLE(of, tas2562_of_match);
577 static struct i2c_driver tas2562_i2c_driver = {
578 .driver = {
579 .name = "tas2562",
580 .of_match_table = of_match_ptr(tas2562_of_match),
582 .probe = tas2562_probe,
583 .id_table = tas2562_id,
586 module_i2c_driver(tas2562_i2c_driver);
588 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
589 MODULE_DESCRIPTION("TAS2562 Audio amplifier driver");
590 MODULE_LICENSE("GPL");