1 // SPDX-License-Identifier: GPL-2.0
3 // peb2466.c -- Infineon PEB2466 ALSA SoC driver
5 // Copyright 2023 CS GROUP France
9 #include <asm/unaligned.h>
10 #include <linux/clk.h>
11 #include <linux/firmware.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/spi/spi.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/tlv.h>
22 #define PEB2466_NB_CHANNEL 4
24 struct peb2466_lookup {
29 #define PEB2466_TLV_SIZE (sizeof((unsigned int []){TLV_DB_SCALE_ITEM(0, 0, 0)}) / \
32 struct peb2466_lkup_ctrl {
35 const struct peb2466_lookup *lookup;
36 unsigned int tlv_array[PEB2466_TLV_SIZE];
40 struct spi_device *spi;
42 struct gpio_desc *reset_gpio;
43 u8 spi_tx_buf[2 + 8]; /* Cannot use stack area for SPI (dma-safe memory) */
44 u8 spi_rx_buf[2 + 8]; /* Cannot use stack area for SPI (dma-safe memory) */
45 struct regmap *regmap;
47 struct peb2466_lookup ax_lookup;
48 struct peb2466_lookup ar_lookup;
49 struct peb2466_lkup_ctrl ax_lkup_ctrl;
50 struct peb2466_lkup_ctrl ar_lkup_ctrl;
51 unsigned int tg1_freq_item;
52 unsigned int tg2_freq_item;
53 } ch[PEB2466_NB_CHANNEL];
54 int max_chan_playback;
57 struct gpio_chip gpio_chip;
68 #define PEB2466_CMD_R (1 << 5)
69 #define PEB2466_CMD_W (0 << 5)
71 #define PEB2466_CMD_MASK 0x18
72 #define PEB2466_CMD_XOP 0x18 /* XOP is 0bxxx11xxx */
73 #define PEB2466_CMD_SOP 0x10 /* SOP is 0bxxx10xxx */
74 #define PEB2466_CMD_COP 0x00 /* COP is 0bxxx0xxxx, handle 0bxxx00xxx */
75 #define PEB2466_CMD_COP1 0x08 /* COP is 0bxxx0xxxx, handle 0bxxx01xxx */
77 #define PEB2466_MAKE_XOP(_lsel) (PEB2466_CMD_XOP | (_lsel))
78 #define PEB2466_MAKE_SOP(_ad, _lsel) (PEB2466_CMD_SOP | ((_ad) << 6) | (_lsel))
79 #define PEB2466_MAKE_COP(_ad, _code) (PEB2466_CMD_COP | ((_ad) << 6) | (_code))
81 #define PEB2466_CR0(_ch) PEB2466_MAKE_SOP(_ch, 0x0)
82 #define PEB2466_CR0_TH (1 << 7)
83 #define PEB2466_CR0_IMR1 (1 << 6)
84 #define PEB2466_CR0_FRX (1 << 5)
85 #define PEB2466_CR0_FRR (1 << 4)
86 #define PEB2466_CR0_AX (1 << 3)
87 #define PEB2466_CR0_AR (1 << 2)
88 #define PEB2466_CR0_THSEL_MASK (0x3 << 0)
89 #define PEB2466_CR0_THSEL(_set) ((_set) << 0)
91 #define PEB2466_CR1(_ch) PEB2466_MAKE_SOP(_ch, 0x1)
92 #define PEB2466_CR1_ETG2 (1 << 7)
93 #define PEB2466_CR1_ETG1 (1 << 6)
94 #define PEB2466_CR1_PTG2 (1 << 5)
95 #define PEB2466_CR1_PTG1 (1 << 4)
96 #define PEB2466_CR1_LAW_MASK (1 << 3)
97 #define PEB2466_CR1_LAW_ALAW (0 << 3)
98 #define PEB2466_CR1_LAW_MULAW (1 << 3)
99 #define PEB2466_CR1_PU (1 << 0)
101 #define PEB2466_CR2(_ch) PEB2466_MAKE_SOP(_ch, 0x2)
102 #define PEB2466_CR3(_ch) PEB2466_MAKE_SOP(_ch, 0x3)
103 #define PEB2466_CR4(_ch) PEB2466_MAKE_SOP(_ch, 0x4)
104 #define PEB2466_CR5(_ch) PEB2466_MAKE_SOP(_ch, 0x5)
106 #define PEB2466_XR0 PEB2466_MAKE_XOP(0x0)
107 #define PEB2466_XR1 PEB2466_MAKE_XOP(0x1)
108 #define PEB2466_XR2 PEB2466_MAKE_XOP(0x2)
109 #define PEB2466_XR3 PEB2466_MAKE_XOP(0x3)
110 #define PEB2466_XR4 PEB2466_MAKE_XOP(0x4)
111 #define PEB2466_XR5 PEB2466_MAKE_XOP(0x5)
112 #define PEB2466_XR5_MCLK_1536 (0x0 << 6)
113 #define PEB2466_XR5_MCLK_2048 (0x1 << 6)
114 #define PEB2466_XR5_MCLK_4096 (0x2 << 6)
115 #define PEB2466_XR5_MCLK_8192 (0x3 << 6)
117 #define PEB2466_XR6 PEB2466_MAKE_XOP(0x6)
118 #define PEB2466_XR6_PCM_OFFSET(_off) ((_off) << 0)
120 #define PEB2466_XR7 PEB2466_MAKE_XOP(0x7)
122 #define PEB2466_TH_FILTER_P1(_ch) PEB2466_MAKE_COP(_ch, 0x0)
123 #define PEB2466_TH_FILTER_P2(_ch) PEB2466_MAKE_COP(_ch, 0x1)
124 #define PEB2466_TH_FILTER_P3(_ch) PEB2466_MAKE_COP(_ch, 0x2)
125 #define PEB2466_IMR1_FILTER_P1(_ch) PEB2466_MAKE_COP(_ch, 0x4)
126 #define PEB2466_IMR1_FILTER_P2(_ch) PEB2466_MAKE_COP(_ch, 0x5)
127 #define PEB2466_FRX_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x6)
128 #define PEB2466_FRR_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x7)
129 #define PEB2466_AX_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x8)
130 #define PEB2466_AR_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x9)
131 #define PEB2466_TG1(_ch) PEB2466_MAKE_COP(_ch, 0xc)
132 #define PEB2466_TG2(_ch) PEB2466_MAKE_COP(_ch, 0xd)
134 static int peb2466_write_byte(struct peb2466 *peb2466, u8 cmd, u8 val)
136 struct spi_transfer xfer = {
137 .tx_buf = &peb2466->spi_tx_buf,
141 peb2466->spi_tx_buf[0] = cmd | PEB2466_CMD_W;
142 peb2466->spi_tx_buf[1] = val;
144 dev_dbg(&peb2466->spi->dev, "write byte (cmd %02x) %02x\n",
145 peb2466->spi_tx_buf[0], peb2466->spi_tx_buf[1]);
147 return spi_sync_transfer(peb2466->spi, &xfer, 1);
150 static int peb2466_read_byte(struct peb2466 *peb2466, u8 cmd, u8 *val)
152 struct spi_transfer xfer = {
153 .tx_buf = &peb2466->spi_tx_buf,
154 .rx_buf = &peb2466->spi_rx_buf,
159 peb2466->spi_tx_buf[0] = cmd | PEB2466_CMD_R;
161 ret = spi_sync_transfer(peb2466->spi, &xfer, 1);
165 if (peb2466->spi_rx_buf[1] != 0x81) {
166 dev_err(&peb2466->spi->dev,
167 "spi xfer rd (cmd %02x) invalid ident byte (0x%02x)\n",
168 peb2466->spi_tx_buf[0], peb2466->spi_rx_buf[1]);
172 *val = peb2466->spi_rx_buf[2];
174 dev_dbg(&peb2466->spi->dev, "read byte (cmd %02x) %02x\n",
175 peb2466->spi_tx_buf[0], *val);
180 static int peb2466_write_buf(struct peb2466 *peb2466, u8 cmd, const u8 *buf, unsigned int len)
182 struct spi_transfer xfer = {
183 .tx_buf = &peb2466->spi_tx_buf,
190 peb2466->spi_tx_buf[0] = cmd | PEB2466_CMD_W;
191 memcpy(&peb2466->spi_tx_buf[1], buf, len);
193 dev_dbg(&peb2466->spi->dev, "write buf (cmd %02x, %u) %*ph\n",
194 peb2466->spi_tx_buf[0], len, len, &peb2466->spi_tx_buf[1]);
196 return spi_sync_transfer(peb2466->spi, &xfer, 1);
199 static int peb2466_reg_write(void *context, unsigned int reg, unsigned int val)
201 struct peb2466 *peb2466 = context;
205 * Only XOP and SOP commands can be handled as registers.
206 * COP commands are handled using direct peb2466_write_buf() calls.
208 switch (reg & PEB2466_CMD_MASK) {
209 case PEB2466_CMD_XOP:
210 case PEB2466_CMD_SOP:
211 ret = peb2466_write_byte(peb2466, reg, val);
214 dev_err(&peb2466->spi->dev, "Not a XOP or SOP command\n");
221 static int peb2466_reg_read(void *context, unsigned int reg, unsigned int *val)
223 struct peb2466 *peb2466 = context;
227 /* Only XOP and SOP commands can be handled as registers */
228 switch (reg & PEB2466_CMD_MASK) {
229 case PEB2466_CMD_XOP:
230 case PEB2466_CMD_SOP:
231 ret = peb2466_read_byte(peb2466, reg, &tmp);
236 dev_err(&peb2466->spi->dev, "Not a XOP or SOP command\n");
243 static const struct regmap_config peb2466_regmap_config = {
246 .max_register = 0xFF,
247 .reg_write = peb2466_reg_write,
248 .reg_read = peb2466_reg_read,
249 .cache_type = REGCACHE_NONE,
252 static int peb2466_lkup_ctrl_info(struct snd_kcontrol *kcontrol,
253 struct snd_ctl_elem_info *uinfo)
255 struct peb2466_lkup_ctrl *lkup_ctrl =
256 (struct peb2466_lkup_ctrl *)kcontrol->private_value;
258 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
260 uinfo->value.integer.min = 0;
261 uinfo->value.integer.max = lkup_ctrl->lookup->count - 1;
265 static int peb2466_lkup_ctrl_get(struct snd_kcontrol *kcontrol,
266 struct snd_ctl_elem_value *ucontrol)
268 struct peb2466_lkup_ctrl *lkup_ctrl =
269 (struct peb2466_lkup_ctrl *)kcontrol->private_value;
271 ucontrol->value.integer.value[0] = lkup_ctrl->index;
275 static int peb2466_lkup_ctrl_put(struct snd_kcontrol *kcontrol,
276 struct snd_ctl_elem_value *ucontrol)
278 struct peb2466_lkup_ctrl *lkup_ctrl =
279 (struct peb2466_lkup_ctrl *)kcontrol->private_value;
280 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
281 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
285 index = ucontrol->value.integer.value[0];
286 if (index >= lkup_ctrl->lookup->count)
289 if (index == lkup_ctrl->index)
292 ret = peb2466_write_buf(peb2466, lkup_ctrl->reg,
293 lkup_ctrl->lookup->table[index], 4);
297 lkup_ctrl->index = index;
298 return 1; /* The value changed */
301 static int peb2466_add_lkup_ctrl(struct snd_soc_component *component,
302 struct peb2466_lkup_ctrl *lkup_ctrl,
303 const char *name, int min_val, int step)
305 DECLARE_TLV_DB_SCALE(tlv_array, min_val, step, 0);
306 struct snd_kcontrol_new control = {0};
308 BUILD_BUG_ON(sizeof(lkup_ctrl->tlv_array) < sizeof(tlv_array));
309 memcpy(lkup_ctrl->tlv_array, tlv_array, sizeof(tlv_array));
311 control.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
313 control.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
314 SNDRV_CTL_ELEM_ACCESS_READWRITE;
315 control.tlv.p = lkup_ctrl->tlv_array;
316 control.info = peb2466_lkup_ctrl_info;
317 control.get = peb2466_lkup_ctrl_get;
318 control.put = peb2466_lkup_ctrl_put;
319 control.private_value = (unsigned long)lkup_ctrl;
321 return snd_soc_add_component_controls(component, &control, 1);
324 enum peb2466_tone_freq {
333 static const u8 peb2466_tone_lookup[][4] = {
334 [PEB2466_TONE_697HZ] = {0x0a, 0x33, 0x5a, 0x2c},
335 [PEB2466_TONE_800HZ] = {0x12, 0xD6, 0x5a, 0xc0},
336 [PEB2466_TONE_950HZ] = {0x1c, 0xf0, 0x5c, 0xc0},
337 [PEB2466_TONE_1000HZ] = {0}, /* lookup value not used for 1000Hz */
338 [PEB2466_TONE_1008HZ] = {0x1a, 0xae, 0x57, 0x70},
339 [PEB2466_TONE_2000HZ] = {0x00, 0x80, 0x50, 0x09},
342 static const char * const peb2466_tone_freq_txt[] = {
343 [PEB2466_TONE_697HZ] = "697Hz",
344 [PEB2466_TONE_800HZ] = "800Hz",
345 [PEB2466_TONE_950HZ] = "950Hz",
346 [PEB2466_TONE_1000HZ] = "1000Hz",
347 [PEB2466_TONE_1008HZ] = "1008Hz",
348 [PEB2466_TONE_2000HZ] = "2000Hz"
351 static const struct soc_enum peb2466_tg_freq[][2] = {
353 SOC_ENUM_SINGLE(PEB2466_TG1(0), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
354 peb2466_tone_freq_txt),
355 SOC_ENUM_SINGLE(PEB2466_TG2(0), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
356 peb2466_tone_freq_txt)
359 SOC_ENUM_SINGLE(PEB2466_TG1(1), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
360 peb2466_tone_freq_txt),
361 SOC_ENUM_SINGLE(PEB2466_TG2(1), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
362 peb2466_tone_freq_txt)
365 SOC_ENUM_SINGLE(PEB2466_TG1(2), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
366 peb2466_tone_freq_txt),
367 SOC_ENUM_SINGLE(PEB2466_TG2(2), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
368 peb2466_tone_freq_txt)
371 SOC_ENUM_SINGLE(PEB2466_TG1(3), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
372 peb2466_tone_freq_txt),
373 SOC_ENUM_SINGLE(PEB2466_TG2(3), 0, ARRAY_SIZE(peb2466_tone_freq_txt),
374 peb2466_tone_freq_txt)
378 static int peb2466_tg_freq_get(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
381 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
382 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
383 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
387 ucontrol->value.enumerated.item[0] = peb2466->ch[0].tg1_freq_item;
390 ucontrol->value.enumerated.item[0] = peb2466->ch[0].tg2_freq_item;
393 ucontrol->value.enumerated.item[0] = peb2466->ch[1].tg1_freq_item;
396 ucontrol->value.enumerated.item[0] = peb2466->ch[1].tg2_freq_item;
399 ucontrol->value.enumerated.item[0] = peb2466->ch[2].tg1_freq_item;
402 ucontrol->value.enumerated.item[0] = peb2466->ch[2].tg2_freq_item;
405 ucontrol->value.enumerated.item[0] = peb2466->ch[3].tg1_freq_item;
408 ucontrol->value.enumerated.item[0] = peb2466->ch[3].tg2_freq_item;
416 static int peb2466_tg_freq_put(struct snd_kcontrol *kcontrol,
417 struct snd_ctl_elem_value *ucontrol)
419 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
420 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
421 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
422 unsigned int *tg_freq_item;
423 u8 cr1_reg, cr1_mask;
427 index = ucontrol->value.enumerated.item[0];
429 if (index >= ARRAY_SIZE(peb2466_tone_lookup))
434 tg_freq_item = &peb2466->ch[0].tg1_freq_item;
435 cr1_reg = PEB2466_CR1(0);
436 cr1_mask = PEB2466_CR1_PTG1;
439 tg_freq_item = &peb2466->ch[0].tg2_freq_item;
440 cr1_reg = PEB2466_CR1(0);
441 cr1_mask = PEB2466_CR1_PTG2;
444 tg_freq_item = &peb2466->ch[1].tg1_freq_item;
445 cr1_reg = PEB2466_CR1(1);
446 cr1_mask = PEB2466_CR1_PTG1;
449 tg_freq_item = &peb2466->ch[1].tg2_freq_item;
450 cr1_reg = PEB2466_CR1(1);
451 cr1_mask = PEB2466_CR1_PTG2;
454 tg_freq_item = &peb2466->ch[2].tg1_freq_item;
455 cr1_reg = PEB2466_CR1(2);
456 cr1_mask = PEB2466_CR1_PTG1;
459 tg_freq_item = &peb2466->ch[2].tg2_freq_item;
460 cr1_reg = PEB2466_CR1(2);
461 cr1_mask = PEB2466_CR1_PTG2;
464 tg_freq_item = &peb2466->ch[3].tg1_freq_item;
465 cr1_reg = PEB2466_CR1(3);
466 cr1_mask = PEB2466_CR1_PTG1;
469 tg_freq_item = &peb2466->ch[3].tg2_freq_item;
470 cr1_reg = PEB2466_CR1(3);
471 cr1_mask = PEB2466_CR1_PTG2;
477 if (index == *tg_freq_item)
480 if (index == PEB2466_TONE_1000HZ) {
481 ret = regmap_update_bits(peb2466->regmap, cr1_reg, cr1_mask, 0);
485 ret = peb2466_write_buf(peb2466, e->reg, peb2466_tone_lookup[index], 4);
488 ret = regmap_update_bits(peb2466->regmap, cr1_reg, cr1_mask, cr1_mask);
493 *tg_freq_item = index;
494 return 1; /* The value changed */
497 static const struct snd_kcontrol_new peb2466_ch0_out_mix_controls[] = {
498 SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(0), 6, 1, 0),
499 SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(0), 7, 1, 0),
500 SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(0), 0, 1, 0)
503 static const struct snd_kcontrol_new peb2466_ch1_out_mix_controls[] = {
504 SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(1), 6, 1, 0),
505 SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(1), 7, 1, 0),
506 SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(1), 0, 1, 0)
509 static const struct snd_kcontrol_new peb2466_ch2_out_mix_controls[] = {
510 SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(2), 6, 1, 0),
511 SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(2), 7, 1, 0),
512 SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(2), 0, 1, 0)
515 static const struct snd_kcontrol_new peb2466_ch3_out_mix_controls[] = {
516 SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(3), 6, 1, 0),
517 SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(3), 7, 1, 0),
518 SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(3), 0, 1, 0)
521 static const struct snd_kcontrol_new peb2466_controls[] = {
523 SOC_SINGLE("DAC0 -6dB Playback Switch", PEB2466_CR3(0), 2, 1, 0),
524 SOC_SINGLE("DAC1 -6dB Playback Switch", PEB2466_CR3(1), 2, 1, 0),
525 SOC_SINGLE("DAC2 -6dB Playback Switch", PEB2466_CR3(2), 2, 1, 0),
526 SOC_SINGLE("DAC3 -6dB Playback Switch", PEB2466_CR3(3), 2, 1, 0),
529 SOC_SINGLE("ADC0 +6dB Capture Switch", PEB2466_CR3(0), 3, 1, 0),
530 SOC_SINGLE("ADC1 +6dB Capture Switch", PEB2466_CR3(1), 3, 1, 0),
531 SOC_SINGLE("ADC2 +6dB Capture Switch", PEB2466_CR3(2), 3, 1, 0),
532 SOC_SINGLE("ADC3 +6dB Capture Switch", PEB2466_CR3(3), 3, 1, 0),
534 /* Tone generators */
535 SOC_ENUM_EXT("DAC0 TG1 Freq", peb2466_tg_freq[0][0],
536 peb2466_tg_freq_get, peb2466_tg_freq_put),
537 SOC_ENUM_EXT("DAC1 TG1 Freq", peb2466_tg_freq[1][0],
538 peb2466_tg_freq_get, peb2466_tg_freq_put),
539 SOC_ENUM_EXT("DAC2 TG1 Freq", peb2466_tg_freq[2][0],
540 peb2466_tg_freq_get, peb2466_tg_freq_put),
541 SOC_ENUM_EXT("DAC3 TG1 Freq", peb2466_tg_freq[3][0],
542 peb2466_tg_freq_get, peb2466_tg_freq_put),
544 SOC_ENUM_EXT("DAC0 TG2 Freq", peb2466_tg_freq[0][1],
545 peb2466_tg_freq_get, peb2466_tg_freq_put),
546 SOC_ENUM_EXT("DAC1 TG2 Freq", peb2466_tg_freq[1][1],
547 peb2466_tg_freq_get, peb2466_tg_freq_put),
548 SOC_ENUM_EXT("DAC2 TG2 Freq", peb2466_tg_freq[2][1],
549 peb2466_tg_freq_get, peb2466_tg_freq_put),
550 SOC_ENUM_EXT("DAC3 TG2 Freq", peb2466_tg_freq[3][1],
551 peb2466_tg_freq_get, peb2466_tg_freq_put),
554 static const struct snd_soc_dapm_widget peb2466_dapm_widgets[] = {
555 SND_SOC_DAPM_SUPPLY("CH0 PWR", PEB2466_CR1(0), 0, 0, NULL, 0),
556 SND_SOC_DAPM_SUPPLY("CH1 PWR", PEB2466_CR1(1), 0, 0, NULL, 0),
557 SND_SOC_DAPM_SUPPLY("CH2 PWR", PEB2466_CR1(2), 0, 0, NULL, 0),
558 SND_SOC_DAPM_SUPPLY("CH3 PWR", PEB2466_CR1(3), 0, 0, NULL, 0),
560 SND_SOC_DAPM_DAC("CH0 DIN", "Playback", SND_SOC_NOPM, 0, 0),
561 SND_SOC_DAPM_DAC("CH1 DIN", "Playback", SND_SOC_NOPM, 0, 0),
562 SND_SOC_DAPM_DAC("CH2 DIN", "Playback", SND_SOC_NOPM, 0, 0),
563 SND_SOC_DAPM_DAC("CH3 DIN", "Playback", SND_SOC_NOPM, 0, 0),
565 SND_SOC_DAPM_SIGGEN("CH0 TG1"),
566 SND_SOC_DAPM_SIGGEN("CH1 TG1"),
567 SND_SOC_DAPM_SIGGEN("CH2 TG1"),
568 SND_SOC_DAPM_SIGGEN("CH3 TG1"),
570 SND_SOC_DAPM_SIGGEN("CH0 TG2"),
571 SND_SOC_DAPM_SIGGEN("CH1 TG2"),
572 SND_SOC_DAPM_SIGGEN("CH2 TG2"),
573 SND_SOC_DAPM_SIGGEN("CH3 TG2"),
575 SND_SOC_DAPM_MIXER("DAC0 Mixer", SND_SOC_NOPM, 0, 0,
576 peb2466_ch0_out_mix_controls,
577 ARRAY_SIZE(peb2466_ch0_out_mix_controls)),
578 SND_SOC_DAPM_MIXER("DAC1 Mixer", SND_SOC_NOPM, 0, 0,
579 peb2466_ch1_out_mix_controls,
580 ARRAY_SIZE(peb2466_ch1_out_mix_controls)),
581 SND_SOC_DAPM_MIXER("DAC2 Mixer", SND_SOC_NOPM, 0, 0,
582 peb2466_ch2_out_mix_controls,
583 ARRAY_SIZE(peb2466_ch2_out_mix_controls)),
584 SND_SOC_DAPM_MIXER("DAC3 Mixer", SND_SOC_NOPM, 0, 0,
585 peb2466_ch3_out_mix_controls,
586 ARRAY_SIZE(peb2466_ch3_out_mix_controls)),
588 SND_SOC_DAPM_PGA("DAC0 PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
589 SND_SOC_DAPM_PGA("DAC1 PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
590 SND_SOC_DAPM_PGA("DAC2 PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
591 SND_SOC_DAPM_PGA("DAC3 PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
593 SND_SOC_DAPM_OUTPUT("OUT0"),
594 SND_SOC_DAPM_OUTPUT("OUT1"),
595 SND_SOC_DAPM_OUTPUT("OUT2"),
596 SND_SOC_DAPM_OUTPUT("OUT3"),
598 SND_SOC_DAPM_INPUT("IN0"),
599 SND_SOC_DAPM_INPUT("IN1"),
600 SND_SOC_DAPM_INPUT("IN2"),
601 SND_SOC_DAPM_INPUT("IN3"),
603 SND_SOC_DAPM_DAC("ADC0", "Capture", SND_SOC_NOPM, 0, 0),
604 SND_SOC_DAPM_DAC("ADC1", "Capture", SND_SOC_NOPM, 0, 0),
605 SND_SOC_DAPM_DAC("ADC2", "Capture", SND_SOC_NOPM, 0, 0),
606 SND_SOC_DAPM_DAC("ADC3", "Capture", SND_SOC_NOPM, 0, 0),
609 static const struct snd_soc_dapm_route peb2466_dapm_routes[] = {
610 { "CH0 DIN", NULL, "CH0 PWR" },
611 { "CH1 DIN", NULL, "CH1 PWR" },
612 { "CH2 DIN", NULL, "CH2 PWR" },
613 { "CH3 DIN", NULL, "CH3 PWR" },
615 { "CH0 TG1", NULL, "CH0 PWR" },
616 { "CH1 TG1", NULL, "CH1 PWR" },
617 { "CH2 TG1", NULL, "CH2 PWR" },
618 { "CH3 TG1", NULL, "CH3 PWR" },
620 { "CH0 TG2", NULL, "CH0 PWR" },
621 { "CH1 TG2", NULL, "CH1 PWR" },
622 { "CH2 TG2", NULL, "CH2 PWR" },
623 { "CH3 TG2", NULL, "CH3 PWR" },
625 { "DAC0 Mixer", "TG1 Switch", "CH0 TG1" },
626 { "DAC0 Mixer", "TG2 Switch", "CH0 TG2" },
627 { "DAC0 Mixer", "Voice Switch", "CH0 DIN" },
628 { "DAC0 Mixer", NULL, "CH0 DIN" },
630 { "DAC1 Mixer", "TG1 Switch", "CH1 TG1" },
631 { "DAC1 Mixer", "TG2 Switch", "CH1 TG2" },
632 { "DAC1 Mixer", "Voice Switch", "CH1 DIN" },
633 { "DAC1 Mixer", NULL, "CH1 DIN" },
635 { "DAC2 Mixer", "TG1 Switch", "CH2 TG1" },
636 { "DAC2 Mixer", "TG2 Switch", "CH2 TG2" },
637 { "DAC2 Mixer", "Voice Switch", "CH2 DIN" },
638 { "DAC2 Mixer", NULL, "CH2 DIN" },
640 { "DAC3 Mixer", "TG1 Switch", "CH3 TG1" },
641 { "DAC3 Mixer", "TG2 Switch", "CH3 TG2" },
642 { "DAC3 Mixer", "Voice Switch", "CH3 DIN" },
643 { "DAC3 Mixer", NULL, "CH3 DIN" },
645 { "DAC0 PGA", NULL, "DAC0 Mixer" },
646 { "DAC1 PGA", NULL, "DAC1 Mixer" },
647 { "DAC2 PGA", NULL, "DAC2 Mixer" },
648 { "DAC3 PGA", NULL, "DAC3 Mixer" },
650 { "OUT0", NULL, "DAC0 PGA" },
651 { "OUT1", NULL, "DAC1 PGA" },
652 { "OUT2", NULL, "DAC2 PGA" },
653 { "OUT3", NULL, "DAC3 PGA" },
655 { "ADC0", NULL, "IN0" },
656 { "ADC1", NULL, "IN1" },
657 { "ADC2", NULL, "IN2" },
658 { "ADC3", NULL, "IN3" },
660 { "ADC0", NULL, "CH0 PWR" },
661 { "ADC1", NULL, "CH1 PWR" },
662 { "ADC2", NULL, "CH2 PWR" },
663 { "ADC3", NULL, "CH3 PWR" },
666 static int peb2466_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
667 unsigned int rx_mask, int slots, int width)
669 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component);
677 /* Not set -> default 8 */
681 dev_err(dai->dev, "tdm slot width %d not supported\n", width);
688 while (mask && chan < PEB2466_NB_CHANNEL) {
690 ret = regmap_write(peb2466->regmap, PEB2466_CR5(chan), slot);
692 dev_err(dai->dev, "chan %d set tx tdm slot failed (%d)\n",
702 dev_err(dai->dev, "too much tx slots defined (mask = 0x%x) support max %d\n",
703 tx_mask, PEB2466_NB_CHANNEL);
706 peb2466->max_chan_playback = chan;
711 while (mask && chan < PEB2466_NB_CHANNEL) {
713 ret = regmap_write(peb2466->regmap, PEB2466_CR4(chan), slot);
715 dev_err(dai->dev, "chan %d set rx tdm slot failed (%d)\n",
725 dev_err(dai->dev, "too much rx slots defined (mask = 0x%x) support max %d\n",
726 rx_mask, PEB2466_NB_CHANNEL);
729 peb2466->max_chan_capture = chan;
734 static int peb2466_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
736 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component);
739 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
740 case SND_SOC_DAIFMT_DSP_A:
741 xr6 = PEB2466_XR6_PCM_OFFSET(1);
743 case SND_SOC_DAIFMT_DSP_B:
744 xr6 = PEB2466_XR6_PCM_OFFSET(0);
747 dev_err(dai->dev, "Unsupported format 0x%x\n",
748 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
751 return regmap_write(peb2466->regmap, PEB2466_XR6, xr6);
754 static int peb2466_dai_hw_params(struct snd_pcm_substream *substream,
755 struct snd_pcm_hw_params *params,
756 struct snd_soc_dai *dai)
758 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component);
763 switch (params_format(params)) {
764 case SNDRV_PCM_FORMAT_MU_LAW:
765 cr1 = PEB2466_CR1_LAW_MULAW;
767 case SNDRV_PCM_FORMAT_A_LAW:
768 cr1 = PEB2466_CR1_LAW_ALAW;
771 dev_err(&peb2466->spi->dev, "Unsupported format 0x%x\n",
772 params_format(params));
776 for (ch = 0; ch < PEB2466_NB_CHANNEL; ch++) {
777 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR1(ch),
778 PEB2466_CR1_LAW_MASK, cr1);
786 static const unsigned int peb2466_sample_bits[] = {8};
788 static struct snd_pcm_hw_constraint_list peb2466_sample_bits_constr = {
789 .list = peb2466_sample_bits,
790 .count = ARRAY_SIZE(peb2466_sample_bits),
793 static int peb2466_dai_startup(struct snd_pcm_substream *substream,
794 struct snd_soc_dai *dai)
796 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component);
800 max_ch = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
801 peb2466->max_chan_playback : peb2466->max_chan_capture;
804 * Disable stream support (min = 0, max = 0) if no timeslots were
807 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
808 SNDRV_PCM_HW_PARAM_CHANNELS,
809 max_ch ? 1 : 0, max_ch);
813 return snd_pcm_hw_constraint_list(substream->runtime, 0,
814 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
815 &peb2466_sample_bits_constr);
818 static const u64 peb2466_dai_formats[] = {
819 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
820 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
823 static const struct snd_soc_dai_ops peb2466_dai_ops = {
824 .startup = peb2466_dai_startup,
825 .hw_params = peb2466_dai_hw_params,
826 .set_tdm_slot = peb2466_dai_set_tdm_slot,
827 .set_fmt = peb2466_dai_set_fmt,
828 .auto_selectable_formats = peb2466_dai_formats,
829 .num_auto_selectable_formats = ARRAY_SIZE(peb2466_dai_formats),
832 static struct snd_soc_dai_driver peb2466_dai_driver = {
835 .stream_name = "Playback",
837 .channels_max = PEB2466_NB_CHANNEL,
838 .rates = SNDRV_PCM_RATE_8000,
839 .formats = SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
842 .stream_name = "Capture",
844 .channels_max = PEB2466_NB_CHANNEL,
845 .rates = SNDRV_PCM_RATE_8000,
846 .formats = SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
848 .ops = &peb2466_dai_ops,
851 static int peb2466_reset_audio(struct peb2466 *peb2466)
853 static const struct reg_sequence reg_reset[] = {
854 { .reg = PEB2466_XR6, .def = 0x00 },
856 { .reg = PEB2466_CR5(0), .def = 0x00 },
857 { .reg = PEB2466_CR4(0), .def = 0x00 },
858 { .reg = PEB2466_CR3(0), .def = 0x00 },
859 { .reg = PEB2466_CR2(0), .def = 0x00 },
860 { .reg = PEB2466_CR1(0), .def = 0x00 },
861 { .reg = PEB2466_CR0(0), .def = PEB2466_CR0_IMR1 },
863 { .reg = PEB2466_CR5(1), .def = 0x00 },
864 { .reg = PEB2466_CR4(1), .def = 0x00 },
865 { .reg = PEB2466_CR3(1), .def = 0x00 },
866 { .reg = PEB2466_CR2(1), .def = 0x00 },
867 { .reg = PEB2466_CR1(1), .def = 0x00 },
868 { .reg = PEB2466_CR0(1), .def = PEB2466_CR0_IMR1 },
870 { .reg = PEB2466_CR5(2), .def = 0x00 },
871 { .reg = PEB2466_CR4(2), .def = 0x00 },
872 { .reg = PEB2466_CR3(2), .def = 0x00 },
873 { .reg = PEB2466_CR2(2), .def = 0x00 },
874 { .reg = PEB2466_CR1(2), .def = 0x00 },
875 { .reg = PEB2466_CR0(2), .def = PEB2466_CR0_IMR1 },
877 { .reg = PEB2466_CR5(3), .def = 0x00 },
878 { .reg = PEB2466_CR4(3), .def = 0x00 },
879 { .reg = PEB2466_CR3(3), .def = 0x00 },
880 { .reg = PEB2466_CR2(3), .def = 0x00 },
881 { .reg = PEB2466_CR1(3), .def = 0x00 },
882 { .reg = PEB2466_CR0(3), .def = PEB2466_CR0_IMR1 },
884 static const u8 imr1_p1[8] = {0x00, 0x90, 0x09, 0x00, 0x90, 0x09, 0x00, 0x00};
885 static const u8 imr1_p2[8] = {0x7F, 0xFF, 0x00, 0x00, 0x90, 0x14, 0x40, 0x08};
886 static const u8 zero[8] = {0};
890 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
891 peb2466->ch[i].tg1_freq_item = PEB2466_TONE_1000HZ;
892 peb2466->ch[i].tg2_freq_item = PEB2466_TONE_1000HZ;
895 * Even if not used, disabling IM/R1 filter is not recommended.
896 * Instead, we must configure it with default coefficients and
898 * The filter will be enabled right after (in the following
899 * regmap_multi_reg_write() call).
901 ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P1(i), imr1_p1, 8);
904 ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P2(i), imr1_p2, 8);
908 /* Set all other filters coefficients to zero */
909 ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P1(i), zero, 8);
912 ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P2(i), zero, 8);
915 ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P3(i), zero, 8);
918 ret = peb2466_write_buf(peb2466, PEB2466_FRX_FILTER(i), zero, 8);
921 ret = peb2466_write_buf(peb2466, PEB2466_FRR_FILTER(i), zero, 8);
924 ret = peb2466_write_buf(peb2466, PEB2466_AX_FILTER(i), zero, 4);
927 ret = peb2466_write_buf(peb2466, PEB2466_AR_FILTER(i), zero, 4);
932 return regmap_multi_reg_write(peb2466->regmap, reg_reset, ARRAY_SIZE(reg_reset));
935 static int peb2466_fw_parse_thfilter(struct snd_soc_component *component,
936 u16 tag, u32 lng, const u8 *data)
938 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
943 dev_info(component->dev, "fw TH filter: mask %x, %*phN\n", *data,
947 * TH_FILTER TLV data:
948 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
949 * - @1 8 bytes: TH-Filter coefficients part1
950 * - @9 8 bytes: TH-Filter coefficients part2
951 * - @17 8 bytes: TH-Filter coefficients part3
954 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
955 if (!(mask & (1 << i)))
958 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
963 ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P1(i), data + 1, 8);
967 ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P2(i), data + 9, 8);
971 ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P3(i), data + 17, 8);
975 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
976 PEB2466_CR0_TH | PEB2466_CR0_THSEL_MASK,
977 PEB2466_CR0_TH | PEB2466_CR0_THSEL(i));
984 static int peb2466_fw_parse_imr1filter(struct snd_soc_component *component,
985 u16 tag, u32 lng, const u8 *data)
987 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
992 dev_info(component->dev, "fw IM/R1 filter: mask %x, %*phN\n", *data,
996 * IMR1_FILTER TLV data:
997 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
998 * - @1 8 bytes: IM/R1-Filter coefficients part1
999 * - @9 8 bytes: IM/R1-Filter coefficients part2
1002 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
1003 if (!(mask & (1 << i)))
1006 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1007 PEB2466_CR0_IMR1, 0);
1011 ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P1(i), data + 1, 8);
1015 ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P2(i), data + 9, 8);
1019 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1020 PEB2466_CR0_IMR1, PEB2466_CR0_IMR1);
1027 static int peb2466_fw_parse_frxfilter(struct snd_soc_component *component,
1028 u16 tag, u32 lng, const u8 *data)
1030 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
1035 dev_info(component->dev, "fw FRX filter: mask %x, %*phN\n", *data,
1039 * FRX_FILTER TLV data:
1040 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
1041 * - @1 8 bytes: FRX-Filter coefficients
1044 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
1045 if (!(mask & (1 << i)))
1048 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1049 PEB2466_CR0_FRX, 0);
1053 ret = peb2466_write_buf(peb2466, PEB2466_FRX_FILTER(i), data + 1, 8);
1057 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1058 PEB2466_CR0_FRX, PEB2466_CR0_FRX);
1065 static int peb2466_fw_parse_frrfilter(struct snd_soc_component *component,
1066 u16 tag, u32 lng, const u8 *data)
1068 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
1073 dev_info(component->dev, "fw FRR filter: mask %x, %*phN\n", *data,
1077 * FRR_FILTER TLV data:
1078 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
1079 * - @1 8 bytes: FRR-Filter coefficients
1082 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
1083 if (!(mask & (1 << i)))
1086 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1087 PEB2466_CR0_FRR, 0);
1091 ret = peb2466_write_buf(peb2466, PEB2466_FRR_FILTER(i), data + 1, 8);
1095 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1096 PEB2466_CR0_FRR, PEB2466_CR0_FRR);
1103 static int peb2466_fw_parse_axfilter(struct snd_soc_component *component,
1104 u16 tag, u32 lng, const u8 *data)
1106 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
1111 dev_info(component->dev, "fw AX filter: mask %x, %*phN\n", *data,
1115 * AX_FILTER TLV data:
1116 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
1117 * - @1 4 bytes: AX-Filter coefficients
1120 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
1121 if (!(mask & (1 << i)))
1124 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1129 ret = peb2466_write_buf(peb2466, PEB2466_AX_FILTER(i), data + 1, 4);
1133 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1134 PEB2466_CR0_AX, PEB2466_CR0_AX);
1141 static int peb2466_fw_parse_arfilter(struct snd_soc_component *component,
1142 u16 tag, u32 lng, const u8 *data)
1144 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
1149 dev_info(component->dev, "fw AR filter: mask %x, %*phN\n", *data,
1153 * AR_FILTER TLV data:
1154 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
1155 * - @1 4 bytes: AR-Filter coefficients
1158 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
1159 if (!(mask & (1 << i)))
1162 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1167 ret = peb2466_write_buf(peb2466, PEB2466_AR_FILTER(i), data + 1, 4);
1171 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1172 PEB2466_CR0_AR, PEB2466_CR0_AR);
1179 static const char * const peb2466_ax_ctrl_names[] = {
1180 "ADC0 Capture Volume",
1181 "ADC1 Capture Volume",
1182 "ADC2 Capture Volume",
1183 "ADC3 Capture Volume",
1186 static int peb2466_fw_parse_axtable(struct snd_soc_component *component,
1187 u16 tag, u32 lng, const u8 *data)
1189 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
1190 struct peb2466_lkup_ctrl *lkup_ctrl;
1191 struct peb2466_lookup *lookup;
1202 * AX_TABLE TLV data:
1203 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
1204 * - @1 32bits signed: Min table value in centi dB (MinVal)
1205 * ie -300 means -3.0 dB
1206 * - @5 32bits signed: Step from on item to other item in centi dB (Step)
1207 * ie 25 means 0.25 dB)
1208 * - @9 32bits unsigned: Item index in the table to use for the initial
1210 * - @13 N*4 bytes: Table composed of 4 bytes items.
1211 * Each item correspond to an AX filter value.
1213 * The conversion from raw value item in the table to/from the value in
1214 * dB is: Raw value at index i <-> (MinVal + i * Step) in centi dB.
1217 /* Check Lng and extract the table size. */
1218 if (lng < 13 || ((lng - 13) % 4)) {
1219 dev_err(component->dev, "fw AX table lng %u invalid\n", lng);
1222 table_size = lng - 13;
1224 min_val = get_unaligned_be32(data + 1);
1225 step = get_unaligned_be32(data + 5);
1226 init_index = get_unaligned_be32(data + 9);
1227 if (init_index >= (table_size / 4)) {
1228 dev_err(component->dev, "fw AX table index %u out of table[%u]\n",
1229 init_index, table_size / 4);
1233 dev_info(component->dev,
1234 "fw AX table: mask %x, min %d, step %d, %u items, tbl[%u] %*phN\n",
1235 *data, min_val, step, table_size / 4, init_index,
1236 4, data + 13 + (init_index * 4));
1238 BUILD_BUG_ON(sizeof(*table) != 4);
1239 table = devm_kzalloc(&peb2466->spi->dev, table_size, GFP_KERNEL);
1242 memcpy(table, data + 13, table_size);
1245 BUILD_BUG_ON(ARRAY_SIZE(peb2466_ax_ctrl_names) != ARRAY_SIZE(peb2466->ch));
1246 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
1247 if (!(mask & (1 << i)))
1250 lookup = &peb2466->ch[i].ax_lookup;
1251 lookup->table = table;
1252 lookup->count = table_size / 4;
1254 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1259 ret = peb2466_write_buf(peb2466, PEB2466_AX_FILTER(i),
1260 lookup->table[init_index], 4);
1264 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1265 PEB2466_CR0_AX, PEB2466_CR0_AX);
1269 lkup_ctrl = &peb2466->ch[i].ax_lkup_ctrl;
1270 lkup_ctrl->lookup = lookup;
1271 lkup_ctrl->reg = PEB2466_AX_FILTER(i);
1272 lkup_ctrl->index = init_index;
1274 ret = peb2466_add_lkup_ctrl(component, lkup_ctrl,
1275 peb2466_ax_ctrl_names[i],
1283 static const char * const peb2466_ar_ctrl_names[] = {
1284 "DAC0 Playback Volume",
1285 "DAC1 Playback Volume",
1286 "DAC2 Playback Volume",
1287 "DAC3 Playback Volume",
1290 static int peb2466_fw_parse_artable(struct snd_soc_component *component,
1291 u16 tag, u32 lng, const u8 *data)
1293 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
1294 struct peb2466_lkup_ctrl *lkup_ctrl;
1295 struct peb2466_lookup *lookup;
1306 * AR_TABLE TLV data:
1307 * - @0 1 byte: Chan mask (bit set means related channel is concerned)
1308 * - @1 32bits signed: Min table value in centi dB (MinVal)
1309 * ie -300 means -3.0 dB
1310 * - @5 32bits signed: Step from on item to other item in centi dB (Step)
1311 * ie 25 means 0.25 dB)
1312 * - @9 32bits unsigned: Item index in the table to use for the initial
1314 * - @13 N*4 bytes: Table composed of 4 bytes items.
1315 * Each item correspond to an AR filter value.
1317 * The conversion from raw value item in the table to/from the value in
1318 * dB is: Raw value at index i <-> (MinVal + i * Step) in centi dB.
1321 /* Check Lng and extract the table size. */
1322 if (lng < 13 || ((lng - 13) % 4)) {
1323 dev_err(component->dev, "fw AR table lng %u invalid\n", lng);
1326 table_size = lng - 13;
1328 min_val = get_unaligned_be32(data + 1);
1329 step = get_unaligned_be32(data + 5);
1330 init_index = get_unaligned_be32(data + 9);
1331 if (init_index >= (table_size / 4)) {
1332 dev_err(component->dev, "fw AR table index %u out of table[%u]\n",
1333 init_index, table_size / 4);
1337 dev_info(component->dev,
1338 "fw AR table: mask %x, min %d, step %d, %u items, tbl[%u] %*phN\n",
1339 *data, min_val, step, table_size / 4, init_index,
1340 4, data + 13 + (init_index * 4));
1342 BUILD_BUG_ON(sizeof(*table) != 4);
1343 table = devm_kzalloc(&peb2466->spi->dev, table_size, GFP_KERNEL);
1346 memcpy(table, data + 13, table_size);
1349 BUILD_BUG_ON(ARRAY_SIZE(peb2466_ar_ctrl_names) != ARRAY_SIZE(peb2466->ch));
1350 for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) {
1351 if (!(mask & (1 << i)))
1354 lookup = &peb2466->ch[i].ar_lookup;
1355 lookup->table = table;
1356 lookup->count = table_size / 4;
1358 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1363 ret = peb2466_write_buf(peb2466, PEB2466_AR_FILTER(i),
1364 lookup->table[init_index], 4);
1368 ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i),
1369 PEB2466_CR0_AR, PEB2466_CR0_AR);
1373 lkup_ctrl = &peb2466->ch[i].ar_lkup_ctrl;
1374 lkup_ctrl->lookup = lookup;
1375 lkup_ctrl->reg = PEB2466_AR_FILTER(i);
1376 lkup_ctrl->index = init_index;
1378 ret = peb2466_add_lkup_ctrl(component, lkup_ctrl,
1379 peb2466_ar_ctrl_names[i],
1387 struct peb2466_fw_tag_def {
1391 int (*parse)(struct snd_soc_component *component,
1392 u16 tag, u32 lng, const u8 *data);
1395 #define PEB2466_TAG_DEF_LNG_EQ(__tag, __lng, __parse) { \
1402 #define PEB2466_TAG_DEF_LNG_MIN(__tag, __lng_min, __parse) { \
1404 .lng_min = __lng_min, \
1405 .lng_max = U32_MAX, \
1409 static const struct peb2466_fw_tag_def peb2466_fw_tag_defs[] = {
1411 PEB2466_TAG_DEF_LNG_EQ(0x0001, 1 + 3 * 8, peb2466_fw_parse_thfilter),
1413 PEB2466_TAG_DEF_LNG_EQ(0x0002, 1 + 2 * 8, peb2466_fw_parse_imr1filter),
1415 PEB2466_TAG_DEF_LNG_EQ(0x0003, 1 + 8, peb2466_fw_parse_frxfilter),
1417 PEB2466_TAG_DEF_LNG_EQ(0x0004, 1 + 8, peb2466_fw_parse_frrfilter),
1419 PEB2466_TAG_DEF_LNG_EQ(0x0005, 1 + 4, peb2466_fw_parse_axfilter),
1421 PEB2466_TAG_DEF_LNG_EQ(0x0006, 1 + 4, peb2466_fw_parse_arfilter),
1423 PEB2466_TAG_DEF_LNG_MIN(0x0105, 1 + 3 * 4, peb2466_fw_parse_axtable),
1425 PEB2466_TAG_DEF_LNG_MIN(0x0106, 1 + 3 * 4, peb2466_fw_parse_artable),
1428 static const struct peb2466_fw_tag_def *peb2466_fw_get_tag_def(u16 tag)
1432 for (i = 0; i < ARRAY_SIZE(peb2466_fw_tag_defs); i++) {
1433 if (peb2466_fw_tag_defs[i].tag == tag)
1434 return &peb2466_fw_tag_defs[i];
1439 static int peb2466_fw_parse(struct snd_soc_component *component,
1440 const u8 *data, size_t size)
1442 const struct peb2466_fw_tag_def *tag_def;
1451 * Coefficients firmware binary structure (16bits and 32bits are
1452 * big-endian values).
1454 * @0, 16bits: Magic (0x2466)
1455 * @2, 16bits: Version (0x0100 for version 1.0)
1456 * @4, 2+4+N bytes: TLV block
1457 * @4+(2+4+N) bytes: Next TLV block
1460 * Detail of a TLV block:
1463 * @6, lng bytes: Data
1465 * The detail the Data for a given TLV Tag is provided in the related
1473 dev_err(component->dev, "fw size %zu, exp at least 4\n", left);
1478 val16 = get_unaligned_be16(buf);
1479 if (val16 != 0x2466) {
1480 dev_err(component->dev, "fw magic 0x%04x exp 0x2466\n", val16);
1487 val16 = get_unaligned_be16(buf);
1488 if (val16 != 0x0100) {
1489 dev_err(component->dev, "fw magic 0x%04x exp 0x0100\n", val16);
1497 dev_err(component->dev, "fw %td/%zu left %zu, exp at least 6\n",
1498 buf - data, size, left);
1501 /* Check tag and lng */
1502 tag = get_unaligned_be16(buf);
1503 lng = get_unaligned_be32(buf + 2);
1504 tag_def = peb2466_fw_get_tag_def(tag);
1506 dev_err(component->dev, "fw %td/%zu tag 0x%04x unknown\n",
1507 buf - data, size, tag);
1510 if (lng < tag_def->lng_min || lng > tag_def->lng_max) {
1511 dev_err(component->dev, "fw %td/%zu tag 0x%04x lng %u, exp [%u;%u]\n",
1512 buf - data, size, tag, lng, tag_def->lng_min, tag_def->lng_max);
1518 dev_err(component->dev, "fw %td/%zu tag 0x%04x lng %u, left %zu\n",
1519 buf - data, size, tag, lng, left);
1523 /* TLV block is valid -> parse the data part */
1524 ret = tag_def->parse(component, tag, lng, buf);
1526 dev_err(component->dev, "fw %td/%zu tag 0x%04x lng %u parse failed\n",
1527 buf - data, size, tag, lng);
1537 static int peb2466_load_coeffs(struct snd_soc_component *component, const char *fw_name)
1539 const struct firmware *fw;
1542 ret = request_firmware(&fw, fw_name, component->dev);
1546 ret = peb2466_fw_parse(component, fw->data, fw->size);
1547 release_firmware(fw);
1552 static int peb2466_component_probe(struct snd_soc_component *component)
1554 struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component);
1555 const char *firmware_name;
1558 /* reset peb2466 audio part */
1559 ret = peb2466_reset_audio(peb2466);
1563 ret = of_property_read_string(peb2466->spi->dev.of_node,
1564 "firmware-name", &firmware_name);
1566 return (ret == -EINVAL) ? 0 : ret;
1568 return peb2466_load_coeffs(component, firmware_name);
1571 static const struct snd_soc_component_driver peb2466_component_driver = {
1572 .probe = peb2466_component_probe,
1573 .controls = peb2466_controls,
1574 .num_controls = ARRAY_SIZE(peb2466_controls),
1575 .dapm_widgets = peb2466_dapm_widgets,
1576 .num_dapm_widgets = ARRAY_SIZE(peb2466_dapm_widgets),
1577 .dapm_routes = peb2466_dapm_routes,
1578 .num_dapm_routes = ARRAY_SIZE(peb2466_dapm_routes),
1583 * The mapping used for the relationship between the gpio offset and the
1584 * physical pin is the following:
1617 static int peb2466_chip_gpio_offset_to_data_regmask(unsigned int offset,
1618 unsigned int *xr_reg,
1623 * SIx_{0,1} and SOx_{0,1}
1624 * Read accesses read SIx_{0,1} values
1625 * Write accesses write SOx_{0,1} values
1627 *xr_reg = PEB2466_XR0;
1628 *mask = (1 << (offset % 8));
1633 *xr_reg = PEB2466_XR1;
1634 *mask = (1 << (offset - 16));
1639 *xr_reg = PEB2466_XR3;
1640 *mask = (1 << (offset - 24 + 4));
1646 static int peb2466_chip_gpio_offset_to_dir_regmask(unsigned int offset,
1647 unsigned int *xr_reg,
1651 /* Direction cannot be changed for these GPIOs */
1655 *xr_reg = PEB2466_XR2;
1656 *mask = (1 << (offset - 16));
1660 *xr_reg = PEB2466_XR3;
1661 *mask = (1 << (offset - 24));
1667 static unsigned int *peb2466_chip_gpio_get_cache(struct peb2466 *peb2466,
1668 unsigned int xr_reg)
1670 unsigned int *cache;
1674 cache = &peb2466->gpio.cache.xr0;
1677 cache = &peb2466->gpio.cache.xr1;
1680 cache = &peb2466->gpio.cache.xr2;
1683 cache = &peb2466->gpio.cache.xr3;
1692 static int peb2466_chip_gpio_update_bits(struct peb2466 *peb2466, unsigned int xr_reg,
1693 unsigned int mask, unsigned int val)
1696 unsigned int *cache;
1700 * Read and write accesses use different peb2466 internal signals (input
1701 * signals on reads and output signals on writes). regmap_update_bits
1702 * cannot be used to read/modify/write the value.
1703 * So, a specific cache value is used.
1706 mutex_lock(&peb2466->gpio.lock);
1708 cache = peb2466_chip_gpio_get_cache(peb2466, xr_reg);
1718 ret = regmap_write(peb2466->regmap, xr_reg, tmp);
1726 mutex_unlock(&peb2466->gpio.lock);
1730 static void peb2466_chip_gpio_set(struct gpio_chip *c, unsigned int offset, int val)
1732 struct peb2466 *peb2466 = gpiochip_get_data(c);
1733 unsigned int xr_reg;
1739 * SIx_{0,1} signals cannot be set and writing the related
1740 * register will change the SOx_{0,1} signals
1742 dev_warn(&peb2466->spi->dev, "cannot set gpio %d (read-only)\n",
1747 ret = peb2466_chip_gpio_offset_to_data_regmask(offset, &xr_reg, &mask);
1749 dev_err(&peb2466->spi->dev, "cannot set gpio %d (%d)\n",
1754 ret = peb2466_chip_gpio_update_bits(peb2466, xr_reg, mask, val ? mask : 0);
1756 dev_err(&peb2466->spi->dev, "set gpio %d (0x%x, 0x%x) failed (%d)\n",
1757 offset, xr_reg, mask, ret);
1761 static int peb2466_chip_gpio_get(struct gpio_chip *c, unsigned int offset)
1763 struct peb2466 *peb2466 = gpiochip_get_data(c);
1764 bool use_cache = false;
1765 unsigned int *cache;
1766 unsigned int xr_reg;
1771 if (offset >= 8 && offset < 16) {
1773 * SOx_{0,1} signals cannot be read. Reading the related
1774 * register will read the SIx_{0,1} signals.
1775 * Use the cache to get value;
1780 ret = peb2466_chip_gpio_offset_to_data_regmask(offset, &xr_reg, &mask);
1782 dev_err(&peb2466->spi->dev, "cannot get gpio %d (%d)\n",
1788 cache = peb2466_chip_gpio_get_cache(peb2466, xr_reg);
1793 ret = regmap_read(peb2466->regmap, xr_reg, &val);
1795 dev_err(&peb2466->spi->dev, "get gpio %d (0x%x, 0x%x) failed (%d)\n",
1796 offset, xr_reg, mask, ret);
1801 return !!(val & mask);
1804 static int peb2466_chip_get_direction(struct gpio_chip *c, unsigned int offset)
1806 struct peb2466 *peb2466 = gpiochip_get_data(c);
1807 unsigned int xr_reg;
1814 return GPIO_LINE_DIRECTION_IN;
1818 return GPIO_LINE_DIRECTION_OUT;
1821 ret = peb2466_chip_gpio_offset_to_dir_regmask(offset, &xr_reg, &mask);
1823 dev_err(&peb2466->spi->dev, "cannot get gpio %d direction (%d)\n",
1828 ret = regmap_read(peb2466->regmap, xr_reg, &val);
1830 dev_err(&peb2466->spi->dev, "get dir gpio %d (0x%x, 0x%x) failed (%d)\n",
1831 offset, xr_reg, mask, ret);
1835 return val & mask ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
1838 static int peb2466_chip_direction_input(struct gpio_chip *c, unsigned int offset)
1840 struct peb2466 *peb2466 = gpiochip_get_data(c);
1841 unsigned int xr_reg;
1854 ret = peb2466_chip_gpio_offset_to_dir_regmask(offset, &xr_reg, &mask);
1856 dev_err(&peb2466->spi->dev, "cannot set gpio %d direction (%d)\n",
1861 ret = peb2466_chip_gpio_update_bits(peb2466, xr_reg, mask, 0);
1863 dev_err(&peb2466->spi->dev, "Set dir in gpio %d (0x%x, 0x%x) failed (%d)\n",
1864 offset, xr_reg, mask, ret);
1871 static int peb2466_chip_direction_output(struct gpio_chip *c, unsigned int offset, int val)
1873 struct peb2466 *peb2466 = gpiochip_get_data(c);
1874 unsigned int xr_reg;
1883 peb2466_chip_gpio_set(c, offset, val);
1890 ret = peb2466_chip_gpio_offset_to_dir_regmask(offset, &xr_reg, &mask);
1892 dev_err(&peb2466->spi->dev, "cannot set gpio %d direction (%d)\n",
1897 ret = peb2466_chip_gpio_update_bits(peb2466, xr_reg, mask, mask);
1899 dev_err(&peb2466->spi->dev, "Set dir in gpio %d (0x%x, 0x%x) failed (%d)\n",
1900 offset, xr_reg, mask, ret);
1907 static int peb2466_reset_gpio(struct peb2466 *peb2466)
1909 static const struct reg_sequence reg_reset[] = {
1910 /* Output pins at 0, input/output pins as input */
1911 { .reg = PEB2466_XR0, .def = 0 },
1912 { .reg = PEB2466_XR1, .def = 0 },
1913 { .reg = PEB2466_XR2, .def = 0 },
1914 { .reg = PEB2466_XR3, .def = 0 },
1917 peb2466->gpio.cache.xr0 = 0;
1918 peb2466->gpio.cache.xr1 = 0;
1919 peb2466->gpio.cache.xr2 = 0;
1920 peb2466->gpio.cache.xr3 = 0;
1922 return regmap_multi_reg_write(peb2466->regmap, reg_reset, ARRAY_SIZE(reg_reset));
1925 static int peb2466_gpio_init(struct peb2466 *peb2466)
1929 mutex_init(&peb2466->gpio.lock);
1931 ret = peb2466_reset_gpio(peb2466);
1935 peb2466->gpio.gpio_chip.owner = THIS_MODULE;
1936 peb2466->gpio.gpio_chip.label = dev_name(&peb2466->spi->dev);
1937 peb2466->gpio.gpio_chip.parent = &peb2466->spi->dev;
1938 peb2466->gpio.gpio_chip.base = -1;
1939 peb2466->gpio.gpio_chip.ngpio = 28;
1940 peb2466->gpio.gpio_chip.get_direction = peb2466_chip_get_direction;
1941 peb2466->gpio.gpio_chip.direction_input = peb2466_chip_direction_input;
1942 peb2466->gpio.gpio_chip.direction_output = peb2466_chip_direction_output;
1943 peb2466->gpio.gpio_chip.get = peb2466_chip_gpio_get;
1944 peb2466->gpio.gpio_chip.set = peb2466_chip_gpio_set;
1945 peb2466->gpio.gpio_chip.can_sleep = true;
1947 return devm_gpiochip_add_data(&peb2466->spi->dev, &peb2466->gpio.gpio_chip,
1951 static int peb2466_spi_probe(struct spi_device *spi)
1953 struct peb2466 *peb2466;
1954 unsigned long mclk_rate;
1958 spi->bits_per_word = 8;
1959 ret = spi_setup(spi);
1963 peb2466 = devm_kzalloc(&spi->dev, sizeof(*peb2466), GFP_KERNEL);
1969 peb2466->regmap = devm_regmap_init(&peb2466->spi->dev, NULL, peb2466,
1970 &peb2466_regmap_config);
1971 if (IS_ERR(peb2466->regmap))
1972 return PTR_ERR(peb2466->regmap);
1974 peb2466->reset_gpio = devm_gpiod_get_optional(&peb2466->spi->dev,
1975 "reset", GPIOD_OUT_LOW);
1976 if (IS_ERR(peb2466->reset_gpio))
1977 return PTR_ERR(peb2466->reset_gpio);
1979 peb2466->mclk = devm_clk_get_enabled(&peb2466->spi->dev, "mclk");
1980 if (IS_ERR(peb2466->mclk))
1981 return PTR_ERR(peb2466->mclk);
1983 if (peb2466->reset_gpio) {
1984 gpiod_set_value_cansleep(peb2466->reset_gpio, 1);
1986 gpiod_set_value_cansleep(peb2466->reset_gpio, 0);
1990 spi_set_drvdata(spi, peb2466);
1992 mclk_rate = clk_get_rate(peb2466->mclk);
1993 switch (mclk_rate) {
1995 xr5 = PEB2466_XR5_MCLK_1536;
1998 xr5 = PEB2466_XR5_MCLK_2048;
2001 xr5 = PEB2466_XR5_MCLK_4096;
2004 xr5 = PEB2466_XR5_MCLK_8192;
2007 dev_err(&peb2466->spi->dev, "Unsupported clock rate %lu\n",
2012 ret = regmap_write(peb2466->regmap, PEB2466_XR5, xr5);
2014 dev_err(&peb2466->spi->dev, "Setting MCLK failed (%d)\n", ret);
2018 ret = devm_snd_soc_register_component(&spi->dev, &peb2466_component_driver,
2019 &peb2466_dai_driver, 1);
2023 if (IS_ENABLED(CONFIG_GPIOLIB)) {
2024 ret = peb2466_gpio_init(peb2466);
2035 static const struct of_device_id peb2466_of_match[] = {
2036 { .compatible = "infineon,peb2466", },
2039 MODULE_DEVICE_TABLE(of, peb2466_of_match);
2041 static const struct spi_device_id peb2466_id_table[] = {
2045 MODULE_DEVICE_TABLE(spi, peb2466_id_table);
2047 static struct spi_driver peb2466_spi_driver = {
2050 .of_match_table = peb2466_of_match,
2052 .id_table = peb2466_id_table,
2053 .probe = peb2466_spi_probe,
2056 module_spi_driver(peb2466_spi_driver);
2059 MODULE_DESCRIPTION("PEB2466 ALSA SoC driver");
2060 MODULE_LICENSE("GPL");