1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ALSA SoC Texas Instruments TAS2770 20-W Digital Input Mono Class-D
4 // Audio Amplifier with Speaker I/V Sense
5 //
6 // Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/
7 // Author: Tracy Yi <tracy-yi@ti.com>
8 // Frank Shi <shifu0704@thundersoft.com>
9
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/hwmon.h>
16 #include <linux/pm.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/firmware.h>
21 #include <linux/regmap.h>
22 #include <linux/of.h>
23 #include <linux/slab.h>
24 #include <sound/soc.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29
30 #include "tas2770.h"
31
32 #define TAS2770_MDELAY 0xFFFFFFFE
33
tas2770_reset(struct tas2770_priv * tas2770)34 static void tas2770_reset(struct tas2770_priv *tas2770)
35 {
36 if (tas2770->reset_gpio) {
37 gpiod_set_value_cansleep(tas2770->reset_gpio, 0);
38 msleep(20);
39 gpiod_set_value_cansleep(tas2770->reset_gpio, 1);
40 usleep_range(1000, 2000);
41 }
42
43 snd_soc_component_write(tas2770->component, TAS2770_SW_RST,
44 TAS2770_RST);
45 usleep_range(1000, 2000);
46 }
47
tas2770_update_pwr_ctrl(struct tas2770_priv * tas2770)48 static int tas2770_update_pwr_ctrl(struct tas2770_priv *tas2770)
49 {
50 struct snd_soc_component *component = tas2770->component;
51 unsigned int val;
52 int ret;
53
54 if (tas2770->dac_powered)
55 val = tas2770->unmuted ?
56 TAS2770_PWR_CTRL_ACTIVE : TAS2770_PWR_CTRL_MUTE;
57 else
58 val = TAS2770_PWR_CTRL_SHUTDOWN;
59
60 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
61 TAS2770_PWR_CTRL_MASK, val);
62 if (ret < 0)
63 return ret;
64
65 return 0;
66 }
67
68 #ifdef CONFIG_PM
tas2770_codec_suspend(struct snd_soc_component * component)69 static int tas2770_codec_suspend(struct snd_soc_component *component)
70 {
71 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
72 int ret = 0;
73
74 regcache_cache_only(tas2770->regmap, true);
75 regcache_mark_dirty(tas2770->regmap);
76
77 if (tas2770->sdz_gpio) {
78 gpiod_set_value_cansleep(tas2770->sdz_gpio, 0);
79 } else {
80 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
81 TAS2770_PWR_CTRL_MASK,
82 TAS2770_PWR_CTRL_SHUTDOWN);
83 if (ret < 0) {
84 regcache_cache_only(tas2770->regmap, false);
85 regcache_sync(tas2770->regmap);
86 return ret;
87 }
88
89 ret = 0;
90 }
91
92 return ret;
93 }
94
tas2770_codec_resume(struct snd_soc_component * component)95 static int tas2770_codec_resume(struct snd_soc_component *component)
96 {
97 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
98 int ret;
99
100 if (tas2770->sdz_gpio) {
101 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
102 usleep_range(1000, 2000);
103 } else {
104 ret = tas2770_update_pwr_ctrl(tas2770);
105 if (ret < 0)
106 return ret;
107 }
108
109 regcache_cache_only(tas2770->regmap, false);
110
111 return regcache_sync(tas2770->regmap);
112 }
113 #else
114 #define tas2770_codec_suspend NULL
115 #define tas2770_codec_resume NULL
116 #endif
117
118 static const char * const tas2770_ASI1_src[] = {
119 "I2C offset", "Left", "Right", "LeftRightDiv2",
120 };
121
122 static SOC_ENUM_SINGLE_DECL(
123 tas2770_ASI1_src_enum, TAS2770_TDM_CFG_REG2,
124 4, tas2770_ASI1_src);
125
126 static const struct snd_kcontrol_new tas2770_asi1_mux =
127 SOC_DAPM_ENUM("ASI1 Source", tas2770_ASI1_src_enum);
128
tas2770_dac_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)129 static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
130 struct snd_kcontrol *kcontrol, int event)
131 {
132 struct snd_soc_component *component =
133 snd_soc_dapm_to_component(w->dapm);
134 struct tas2770_priv *tas2770 =
135 snd_soc_component_get_drvdata(component);
136 int ret;
137
138 switch (event) {
139 case SND_SOC_DAPM_POST_PMU:
140 tas2770->dac_powered = 1;
141 ret = tas2770_update_pwr_ctrl(tas2770);
142 break;
143 case SND_SOC_DAPM_PRE_PMD:
144 tas2770->dac_powered = 0;
145 ret = tas2770_update_pwr_ctrl(tas2770);
146 break;
147 default:
148 dev_err(tas2770->dev, "Not supported evevt\n");
149 return -EINVAL;
150 }
151
152 return ret;
153 }
154
155 static const struct snd_kcontrol_new isense_switch =
156 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 3, 1, 1);
157 static const struct snd_kcontrol_new vsense_switch =
158 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 2, 1, 1);
159
sense_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)160 static int sense_event(struct snd_soc_dapm_widget *w,
161 struct snd_kcontrol *kcontrol, int event)
162 {
163 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
164 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
165
166 /*
167 * Powering up ISENSE/VSENSE requires a trip through the shutdown state.
168 * Do that here to ensure that our changes are applied properly, otherwise
169 * we might end up with non-functional IVSENSE if playback started earlier,
170 * which would break software speaker protection.
171 */
172 switch (event) {
173 case SND_SOC_DAPM_PRE_REG:
174 return snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
175 TAS2770_PWR_CTRL_MASK,
176 TAS2770_PWR_CTRL_SHUTDOWN);
177 case SND_SOC_DAPM_POST_REG:
178 return tas2770_update_pwr_ctrl(tas2770);
179 default:
180 return 0;
181 }
182 }
183
184 static const struct snd_soc_dapm_widget tas2770_dapm_widgets[] = {
185 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
186 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2770_asi1_mux),
187 SND_SOC_DAPM_SWITCH_E("ISENSE", TAS2770_PWR_CTRL, 3, 1, &isense_switch,
188 sense_event, SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG),
189 SND_SOC_DAPM_SWITCH_E("VSENSE", TAS2770_PWR_CTRL, 2, 1, &vsense_switch,
190 sense_event, SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG),
191 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2770_dac_event,
192 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
193 SND_SOC_DAPM_OUTPUT("OUT"),
194 SND_SOC_DAPM_SIGGEN("VMON"),
195 SND_SOC_DAPM_SIGGEN("IMON")
196 };
197
198 static const struct snd_soc_dapm_route tas2770_audio_map[] = {
199 {"ASI1 Sel", "I2C offset", "ASI1"},
200 {"ASI1 Sel", "Left", "ASI1"},
201 {"ASI1 Sel", "Right", "ASI1"},
202 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
203 {"DAC", NULL, "ASI1 Sel"},
204 {"OUT", NULL, "DAC"},
205 {"ISENSE", "Switch", "IMON"},
206 {"VSENSE", "Switch", "VMON"},
207 };
208
tas2770_mute(struct snd_soc_dai * dai,int mute,int direction)209 static int tas2770_mute(struct snd_soc_dai *dai, int mute, int direction)
210 {
211 struct snd_soc_component *component = dai->component;
212 struct tas2770_priv *tas2770 =
213 snd_soc_component_get_drvdata(component);
214
215 tas2770->unmuted = !mute;
216 return tas2770_update_pwr_ctrl(tas2770);
217 }
218
tas2770_set_ivsense_transmit(struct tas2770_priv * tas2770,int i_slot,int v_slot)219 static int tas2770_set_ivsense_transmit(struct tas2770_priv *tas2770,
220 int i_slot, int v_slot)
221 {
222 struct snd_soc_component *component = tas2770->component;
223 int ret;
224
225 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG5,
226 TAS2770_TDM_CFG_REG5_VSNS_MASK |
227 TAS2770_TDM_CFG_REG5_50_MASK,
228 TAS2770_TDM_CFG_REG5_VSNS_ENABLE |
229 v_slot);
230 if (ret < 0)
231 return ret;
232
233 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG6,
234 TAS2770_TDM_CFG_REG6_ISNS_MASK |
235 TAS2770_TDM_CFG_REG6_50_MASK,
236 TAS2770_TDM_CFG_REG6_ISNS_ENABLE |
237 i_slot);
238 if (ret < 0)
239 return ret;
240
241 return 0;
242 }
243
tas2770_set_pdm_transmit(struct tas2770_priv * tas2770,int slot)244 static int tas2770_set_pdm_transmit(struct tas2770_priv *tas2770, int slot)
245 {
246 struct snd_soc_component *component = tas2770->component;
247 int ret;
248
249 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG7,
250 TAS2770_TDM_CFG_REG7_PDM_MASK |
251 TAS2770_TDM_CFG_REG7_50_MASK,
252 TAS2770_TDM_CFG_REG7_PDM_ENABLE |
253 slot);
254 return ret;
255 }
256
tas2770_set_bitwidth(struct tas2770_priv * tas2770,int bitwidth)257 static int tas2770_set_bitwidth(struct tas2770_priv *tas2770, int bitwidth)
258 {
259 int ret;
260 struct snd_soc_component *component = tas2770->component;
261
262 switch (bitwidth) {
263 case SNDRV_PCM_FORMAT_S16_LE:
264 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
265 TAS2770_TDM_CFG_REG2_RXW_MASK,
266 TAS2770_TDM_CFG_REG2_RXW_16BITS);
267 break;
268 case SNDRV_PCM_FORMAT_S24_LE:
269 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
270 TAS2770_TDM_CFG_REG2_RXW_MASK,
271 TAS2770_TDM_CFG_REG2_RXW_24BITS);
272 break;
273 case SNDRV_PCM_FORMAT_S32_LE:
274 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
275 TAS2770_TDM_CFG_REG2_RXW_MASK,
276 TAS2770_TDM_CFG_REG2_RXW_32BITS);
277 break;
278
279 default:
280 return -EINVAL;
281 }
282
283 if (ret < 0)
284 return ret;
285
286 return 0;
287 }
288
tas2770_set_samplerate(struct tas2770_priv * tas2770,int samplerate)289 static int tas2770_set_samplerate(struct tas2770_priv *tas2770, int samplerate)
290 {
291 struct snd_soc_component *component = tas2770->component;
292 int ramp_rate_val;
293 int ret;
294
295 switch (samplerate) {
296 case 48000:
297 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
298 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
299 break;
300 case 44100:
301 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
302 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
303 break;
304 case 96000:
305 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
306 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
307 break;
308 case 88200:
309 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
310 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
311 break;
312 case 192000:
313 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
314 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
315 break;
316 case 176400:
317 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
318 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
319 break;
320 default:
321 return -EINVAL;
322 }
323
324 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
325 TAS2770_TDM_CFG_REG0_SMP_MASK |
326 TAS2770_TDM_CFG_REG0_31_MASK,
327 ramp_rate_val);
328 if (ret < 0)
329 return ret;
330
331 return 0;
332 }
333
tas2770_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)334 static int tas2770_hw_params(struct snd_pcm_substream *substream,
335 struct snd_pcm_hw_params *params,
336 struct snd_soc_dai *dai)
337 {
338 struct snd_soc_component *component = dai->component;
339 struct tas2770_priv *tas2770 =
340 snd_soc_component_get_drvdata(component);
341 int ret;
342
343 ret = tas2770_set_bitwidth(tas2770, params_format(params));
344 if (ret)
345 return ret;
346
347 return tas2770_set_samplerate(tas2770, params_rate(params));
348 }
349
tas2770_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)350 static int tas2770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
351 {
352 struct snd_soc_component *component = dai->component;
353 struct tas2770_priv *tas2770 =
354 snd_soc_component_get_drvdata(component);
355 u8 tdm_rx_start_slot = 0, invert_fpol = 0, fpol_preinv = 0, asi_cfg_1 = 0;
356 int ret;
357
358 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
359 case SND_SOC_DAIFMT_CBC_CFC:
360 break;
361 default:
362 dev_err(tas2770->dev, "ASI invalid DAI clocking\n");
363 return -EINVAL;
364 }
365
366 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
367 case SND_SOC_DAIFMT_NB_IF:
368 invert_fpol = 1;
369 fallthrough;
370 case SND_SOC_DAIFMT_NB_NF:
371 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_RSING;
372 break;
373 case SND_SOC_DAIFMT_IB_IF:
374 invert_fpol = 1;
375 fallthrough;
376 case SND_SOC_DAIFMT_IB_NF:
377 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_FALING;
378 break;
379 default:
380 dev_err(tas2770->dev, "ASI format Inverse is not found\n");
381 return -EINVAL;
382 }
383
384 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
385 TAS2770_TDM_CFG_REG1_RX_MASK,
386 asi_cfg_1);
387 if (ret < 0)
388 return ret;
389
390 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
391 case SND_SOC_DAIFMT_I2S:
392 tdm_rx_start_slot = 1;
393 fpol_preinv = 0;
394 break;
395 case SND_SOC_DAIFMT_DSP_A:
396 tdm_rx_start_slot = 0;
397 fpol_preinv = 1;
398 break;
399 case SND_SOC_DAIFMT_DSP_B:
400 tdm_rx_start_slot = 1;
401 fpol_preinv = 1;
402 break;
403 case SND_SOC_DAIFMT_LEFT_J:
404 tdm_rx_start_slot = 0;
405 fpol_preinv = 1;
406 break;
407 default:
408 dev_err(tas2770->dev,
409 "DAI Format is not found, fmt=0x%x\n", fmt);
410 return -EINVAL;
411 }
412
413 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
414 TAS2770_TDM_CFG_REG1_MASK,
415 (tdm_rx_start_slot << TAS2770_TDM_CFG_REG1_51_SHIFT));
416 if (ret < 0)
417 return ret;
418
419 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
420 TAS2770_TDM_CFG_REG0_FPOL_MASK,
421 (fpol_preinv ^ invert_fpol)
422 ? TAS2770_TDM_CFG_REG0_FPOL_RSING
423 : TAS2770_TDM_CFG_REG0_FPOL_FALING);
424 if (ret < 0)
425 return ret;
426
427 return 0;
428 }
429
tas2770_set_dai_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)430 static int tas2770_set_dai_tdm_slot(struct snd_soc_dai *dai,
431 unsigned int tx_mask,
432 unsigned int rx_mask,
433 int slots, int slot_width)
434 {
435 struct snd_soc_component *component = dai->component;
436 int left_slot, right_slot;
437 int ret;
438
439 if (tx_mask == 0 || rx_mask != 0)
440 return -EINVAL;
441
442 left_slot = __ffs(tx_mask);
443 tx_mask &= ~(1 << left_slot);
444 if (tx_mask == 0) {
445 right_slot = left_slot;
446 } else {
447 right_slot = __ffs(tx_mask);
448 tx_mask &= ~(1 << right_slot);
449 }
450
451 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
452 return -EINVAL;
453
454 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
455 TAS2770_TDM_CFG_REG3_30_MASK,
456 (left_slot << TAS2770_TDM_CFG_REG3_30_SHIFT));
457 if (ret < 0)
458 return ret;
459 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
460 TAS2770_TDM_CFG_REG3_RXS_MASK,
461 (right_slot << TAS2770_TDM_CFG_REG3_RXS_SHIFT));
462 if (ret < 0)
463 return ret;
464
465 switch (slot_width) {
466 case 16:
467 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
468 TAS2770_TDM_CFG_REG2_RXS_MASK,
469 TAS2770_TDM_CFG_REG2_RXS_16BITS);
470 break;
471 case 24:
472 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
473 TAS2770_TDM_CFG_REG2_RXS_MASK,
474 TAS2770_TDM_CFG_REG2_RXS_24BITS);
475 break;
476 case 32:
477 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
478 TAS2770_TDM_CFG_REG2_RXS_MASK,
479 TAS2770_TDM_CFG_REG2_RXS_32BITS);
480 break;
481 case 0:
482 /* Do not change slot width */
483 ret = 0;
484 break;
485 default:
486 ret = -EINVAL;
487 }
488
489 if (ret < 0)
490 return ret;
491
492 return 0;
493 }
494
495 static const struct snd_soc_dai_ops tas2770_dai_ops = {
496 .mute_stream = tas2770_mute,
497 .hw_params = tas2770_hw_params,
498 .set_fmt = tas2770_set_fmt,
499 .set_tdm_slot = tas2770_set_dai_tdm_slot,
500 .no_capture_mute = 1,
501 };
502
503 #define TAS2770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
504 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
505
506 #define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
507 SNDRV_PCM_RATE_96000 |\
508 SNDRV_PCM_RATE_192000\
509 )
510
511 static struct snd_soc_dai_driver tas2770_dai_driver[] = {
512 {
513 .name = "tas2770 ASI1",
514 .id = 0,
515 .playback = {
516 .stream_name = "ASI1 Playback",
517 .channels_min = 1,
518 .channels_max = 2,
519 .rates = TAS2770_RATES,
520 .formats = TAS2770_FORMATS,
521 },
522 .capture = {
523 .stream_name = "ASI1 Capture",
524 .channels_min = 0,
525 .channels_max = 2,
526 .rates = TAS2770_RATES,
527 .formats = TAS2770_FORMATS,
528 },
529 .ops = &tas2770_dai_ops,
530 .symmetric_rate = 1,
531 },
532 };
533
tas2770_read_die_temp(struct tas2770_priv * tas2770,long * result)534 static int tas2770_read_die_temp(struct tas2770_priv *tas2770, long *result)
535 {
536 int ret = 0;
537 int reading, msb, lsb;
538
539 ret = regmap_read(tas2770->regmap, TAS2770_TEMP_MSB, &msb);
540 if (ret)
541 return ret;
542
543 ret = regmap_read(tas2770->regmap, TAS2770_TEMP_LSB, &lsb);
544 if (ret)
545 return ret;
546
547 reading = (msb << 4) | (lsb >> 4);
548
549 /*
550 * As per datasheet: divide register by 16 and subtract 93 to get
551 * degrees Celsius. hwmon requires millidegrees. Let's avoid rounding
552 * errors by subtracting 93 * 16 then multiplying by 1000 / 16.
553 *
554 * NOTE: The ADC registers are initialised to 0 on reset. This means
555 * that the temperature will read -93 *C until the chip is brought out
556 * of software shutdown (e.g. the PCM it's attached to is opened). The
557 * ADC is also shut down in software shutdown/low-power mode, so the
558 * value read back from its registers will be the last value sampled
559 * before entering software shutdown.
560 */
561 *result = (reading - (93 * 16)) * (1000 / 16);
562 return 0;
563 }
564
tas2770_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)565 static umode_t tas2770_hwmon_is_visible(const void *data,
566 enum hwmon_sensor_types type, u32 attr,
567 int channel)
568 {
569 if (type != hwmon_temp)
570 return 0;
571
572 switch (attr) {
573 case hwmon_temp_input:
574 return 0444;
575 default:
576 break;
577 }
578
579 return 0;
580 }
581
tas2770_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)582 static int tas2770_hwmon_read(struct device *dev,
583 enum hwmon_sensor_types type,
584 u32 attr, int channel, long *val)
585 {
586 struct tas2770_priv *tas2770 = dev_get_drvdata(dev);
587 int ret;
588
589 switch (attr) {
590 case hwmon_temp_input:
591 ret = tas2770_read_die_temp(tas2770, val);
592 break;
593 default:
594 ret = -EOPNOTSUPP;
595 break;
596 }
597
598 return ret;
599 }
600
601 static const struct hwmon_channel_info *const tas2770_hwmon_info[] = {
602 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
603 NULL
604 };
605
606 static const struct hwmon_ops tas2770_hwmon_ops = {
607 .is_visible = tas2770_hwmon_is_visible,
608 .read = tas2770_hwmon_read,
609 };
610
611 static const struct hwmon_chip_info tas2770_hwmon_chip_info = {
612 .ops = &tas2770_hwmon_ops,
613 .info = tas2770_hwmon_info,
614 };
615
616 static const struct regmap_config tas2770_i2c_regmap;
617
tas2770_codec_probe(struct snd_soc_component * component)618 static int tas2770_codec_probe(struct snd_soc_component *component)
619 {
620 struct tas2770_priv *tas2770 =
621 snd_soc_component_get_drvdata(component);
622 int ret;
623
624 tas2770->component = component;
625
626 if (tas2770->sdz_gpio) {
627 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
628 usleep_range(1000, 2000);
629 }
630
631 tas2770_reset(tas2770);
632 regmap_reinit_cache(tas2770->regmap, &tas2770_i2c_regmap);
633
634 if (tas2770->i_sense_slot != -1 && tas2770->v_sense_slot != -1) {
635 ret = tas2770_set_ivsense_transmit(tas2770, tas2770->i_sense_slot,
636 tas2770->v_sense_slot);
637
638 if (ret < 0)
639 return ret;
640 }
641
642 if (tas2770->pdm_slot != -1) {
643 ret = tas2770_set_pdm_transmit(tas2770, tas2770->pdm_slot);
644
645 if (ret < 0)
646 return ret;
647 }
648
649 return 0;
650 }
651
652 static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0);
653 static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -10050, 50, 0);
654
655 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
656 SOC_SINGLE_TLV("Speaker Playback Volume", TAS2770_PLAY_CFG_REG2,
657 0, TAS2770_PLAY_CFG_REG2_VMAX, 1, tas2770_playback_volume),
658 SOC_SINGLE_TLV("Amp Gain Volume", TAS2770_PLAY_CFG_REG0, 0, 0x14, 0,
659 tas2770_digital_tlv),
660 };
661
662 static const struct snd_soc_component_driver soc_component_driver_tas2770 = {
663 .probe = tas2770_codec_probe,
664 .suspend = tas2770_codec_suspend,
665 .resume = tas2770_codec_resume,
666 .controls = tas2770_snd_controls,
667 .num_controls = ARRAY_SIZE(tas2770_snd_controls),
668 .dapm_widgets = tas2770_dapm_widgets,
669 .num_dapm_widgets = ARRAY_SIZE(tas2770_dapm_widgets),
670 .dapm_routes = tas2770_audio_map,
671 .num_dapm_routes = ARRAY_SIZE(tas2770_audio_map),
672 .idle_bias_on = 1,
673 .endianness = 1,
674 };
675
tas2770_register_codec(struct tas2770_priv * tas2770)676 static int tas2770_register_codec(struct tas2770_priv *tas2770)
677 {
678 return devm_snd_soc_register_component(tas2770->dev,
679 &soc_component_driver_tas2770,
680 tas2770_dai_driver, ARRAY_SIZE(tas2770_dai_driver));
681 }
682
683 static const struct reg_default tas2770_reg_defaults[] = {
684 { TAS2770_PAGE, 0x00 },
685 { TAS2770_SW_RST, 0x00 },
686 { TAS2770_PWR_CTRL, 0x0e },
687 { TAS2770_PLAY_CFG_REG0, 0x10 },
688 { TAS2770_PLAY_CFG_REG1, 0x01 },
689 { TAS2770_PLAY_CFG_REG2, 0x00 },
690 { TAS2770_MSC_CFG_REG0, 0x07 },
691 { TAS2770_TDM_CFG_REG1, 0x02 },
692 { TAS2770_TDM_CFG_REG2, 0x0a },
693 { TAS2770_TDM_CFG_REG3, 0x10 },
694 { TAS2770_INT_MASK_REG0, 0xfc },
695 { TAS2770_INT_MASK_REG1, 0xb1 },
696 { TAS2770_INT_CFG, 0x05 },
697 { TAS2770_MISC_IRQ, 0x81 },
698 { TAS2770_CLK_CGF, 0x0c },
699
700 };
701
tas2770_volatile(struct device * dev,unsigned int reg)702 static bool tas2770_volatile(struct device *dev, unsigned int reg)
703 {
704 switch (reg) {
705 case TAS2770_PAGE: /* regmap implementation requires this */
706 case TAS2770_SW_RST: /* always clears after write */
707 case TAS2770_BO_PRV_REG0:/* has a self clearing bit */
708 case TAS2770_LVE_INT_REG0:
709 case TAS2770_LVE_INT_REG1:
710 case TAS2770_LAT_INT_REG0:/* Sticky interrupt flags */
711 case TAS2770_LAT_INT_REG1:/* Sticky interrupt flags */
712 case TAS2770_VBAT_MSB:
713 case TAS2770_VBAT_LSB:
714 case TAS2770_TEMP_MSB:
715 case TAS2770_TEMP_LSB:
716 return true;
717 }
718
719 return false;
720 }
721
tas2770_writeable(struct device * dev,unsigned int reg)722 static bool tas2770_writeable(struct device *dev, unsigned int reg)
723 {
724 switch (reg) {
725 case TAS2770_LVE_INT_REG0:
726 case TAS2770_LVE_INT_REG1:
727 case TAS2770_LAT_INT_REG0:
728 case TAS2770_LAT_INT_REG1:
729 case TAS2770_VBAT_MSB:
730 case TAS2770_VBAT_LSB:
731 case TAS2770_TEMP_MSB:
732 case TAS2770_TEMP_LSB:
733 case TAS2770_TDM_CLK_DETC:
734 case TAS2770_REV_AND_GPID:
735 return false;
736 }
737
738 return true;
739 }
740
741 static const struct regmap_range_cfg tas2770_regmap_ranges[] = {
742 {
743 .range_min = 0,
744 .range_max = 1 * 128,
745 .selector_reg = TAS2770_PAGE,
746 .selector_mask = 0xff,
747 .selector_shift = 0,
748 .window_start = 0,
749 .window_len = 128,
750 },
751 };
752
753 static const struct regmap_config tas2770_i2c_regmap = {
754 .reg_bits = 8,
755 .val_bits = 8,
756 .writeable_reg = tas2770_writeable,
757 .volatile_reg = tas2770_volatile,
758 .reg_defaults = tas2770_reg_defaults,
759 .num_reg_defaults = ARRAY_SIZE(tas2770_reg_defaults),
760 .cache_type = REGCACHE_RBTREE,
761 .ranges = tas2770_regmap_ranges,
762 .num_ranges = ARRAY_SIZE(tas2770_regmap_ranges),
763 .max_register = 1 * 128,
764 };
765
tas2770_parse_dt(struct device * dev,struct tas2770_priv * tas2770)766 static int tas2770_parse_dt(struct device *dev, struct tas2770_priv *tas2770)
767 {
768 int rc = 0;
769
770 rc = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
771 &tas2770->i_sense_slot);
772 if (rc) {
773 dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
774 "ti,imon-slot-no");
775
776 tas2770->i_sense_slot = -1;
777 }
778
779 rc = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
780 &tas2770->v_sense_slot);
781 if (rc) {
782 dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
783 "ti,vmon-slot-no");
784
785 tas2770->v_sense_slot = -1;
786 }
787
788 rc = fwnode_property_read_u32(dev->fwnode, "ti,pdm-slot-no",
789 &tas2770->pdm_slot);
790 if (rc)
791 tas2770->pdm_slot = -1;
792
793 tas2770->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
794 if (IS_ERR(tas2770->sdz_gpio)) {
795 if (PTR_ERR(tas2770->sdz_gpio) == -EPROBE_DEFER)
796 return -EPROBE_DEFER;
797
798 tas2770->sdz_gpio = NULL;
799 }
800
801 return 0;
802 }
803
tas2770_i2c_probe(struct i2c_client * client)804 static int tas2770_i2c_probe(struct i2c_client *client)
805 {
806 struct tas2770_priv *tas2770;
807 int result;
808
809 tas2770 = devm_kzalloc(&client->dev, sizeof(struct tas2770_priv),
810 GFP_KERNEL);
811 if (!tas2770)
812 return -ENOMEM;
813
814 tas2770->dev = &client->dev;
815 i2c_set_clientdata(client, tas2770);
816 dev_set_drvdata(&client->dev, tas2770);
817
818 tas2770->regmap = devm_regmap_init_i2c(client, &tas2770_i2c_regmap);
819 if (IS_ERR(tas2770->regmap)) {
820 result = PTR_ERR(tas2770->regmap);
821 dev_err(&client->dev, "Failed to allocate register map: %d\n",
822 result);
823 return result;
824 }
825
826 if (client->dev.of_node) {
827 result = tas2770_parse_dt(&client->dev, tas2770);
828 if (result) {
829 dev_err(tas2770->dev, "%s: Failed to parse devicetree\n",
830 __func__);
831 return result;
832 }
833 }
834
835 tas2770->reset_gpio = devm_gpiod_get_optional(tas2770->dev, "reset",
836 GPIOD_OUT_HIGH);
837 if (IS_ERR(tas2770->reset_gpio)) {
838 if (PTR_ERR(tas2770->reset_gpio) == -EPROBE_DEFER) {
839 tas2770->reset_gpio = NULL;
840 return -EPROBE_DEFER;
841 }
842 }
843
844 if (IS_REACHABLE(CONFIG_HWMON)) {
845 struct device *hwmon;
846
847 hwmon = devm_hwmon_device_register_with_info(&client->dev, "tas2770",
848 tas2770,
849 &tas2770_hwmon_chip_info,
850 NULL);
851 if (IS_ERR(hwmon)) {
852 return dev_err_probe(&client->dev, PTR_ERR(hwmon),
853 "Failed to register temp sensor\n");
854 }
855 }
856
857 result = tas2770_register_codec(tas2770);
858 if (result)
859 dev_err(tas2770->dev, "Register codec failed.\n");
860
861 return result;
862 }
863
864 static const struct i2c_device_id tas2770_i2c_id[] = {
865 { "tas2770"},
866 { }
867 };
868 MODULE_DEVICE_TABLE(i2c, tas2770_i2c_id);
869
870 #if defined(CONFIG_OF)
871 static const struct of_device_id tas2770_of_match[] = {
872 { .compatible = "ti,tas2770" },
873 {},
874 };
875 MODULE_DEVICE_TABLE(of, tas2770_of_match);
876 #endif
877
878 static struct i2c_driver tas2770_i2c_driver = {
879 .driver = {
880 .name = "tas2770",
881 .of_match_table = of_match_ptr(tas2770_of_match),
882 },
883 .probe = tas2770_i2c_probe,
884 .id_table = tas2770_i2c_id,
885 };
886 module_i2c_driver(tas2770_i2c_driver);
887
888 MODULE_AUTHOR("Shi Fu <shifu0704@thundersoft.com>");
889 MODULE_DESCRIPTION("TAS2770 I2C Smart Amplifier driver");
890 MODULE_LICENSE("GPL v2");
891