1 // SPDX-License-Identifier: GPL-2.0
3 // IDT821034 ALSA SoC driver
5 // Copyright 2022 CS GROUP France
7 // Author: Herve Codina <herve.codina@bootlin.com>
9 #include <linux/bitrev.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
16 #include <sound/tlv.h>
18 #define IDT821034_NB_CHANNEL 4
20 struct idt821034_amp
{
26 struct spi_device
*spi
;
28 u8 spi_tx_buf
; /* Cannot use stack area for SPI (dma-safe memory) */
29 u8 spi_rx_buf
; /* Cannot use stack area for SPI (dma-safe memory) */
38 } ch
[IDT821034_NB_CHANNEL
];
42 struct idt821034_amp amp_out
;
43 struct idt821034_amp amp_in
;
44 } ch
[IDT821034_NB_CHANNEL
];
48 struct gpio_chip gpio_chip
;
51 static int idt821034_8bit_write(struct idt821034
*idt821034
, u8 val
)
53 struct spi_transfer xfer
[] = {
55 .tx_buf
= &idt821034
->spi_tx_buf
,
59 .tx_buf
= &idt821034
->spi_tx_buf
,
64 idt821034
->spi_tx_buf
= val
;
66 dev_vdbg(&idt821034
->spi
->dev
, "spi xfer wr 0x%x\n", val
);
68 return spi_sync_transfer(idt821034
->spi
, xfer
, 2);
71 static int idt821034_2x8bit_write(struct idt821034
*idt821034
, u8 val1
, u8 val2
)
75 ret
= idt821034_8bit_write(idt821034
, val1
);
78 return idt821034_8bit_write(idt821034
, val2
);
81 static int idt821034_8bit_read(struct idt821034
*idt821034
, u8 valw
, u8
*valr
)
83 struct spi_transfer xfer
[] = {
85 .tx_buf
= &idt821034
->spi_tx_buf
,
86 .rx_buf
= &idt821034
->spi_rx_buf
,
90 .tx_buf
= &idt821034
->spi_tx_buf
,
96 idt821034
->spi_tx_buf
= valw
;
98 ret
= spi_sync_transfer(idt821034
->spi
, xfer
, 2);
102 *valr
= idt821034
->spi_rx_buf
;
104 dev_vdbg(&idt821034
->spi
->dev
, "spi xfer wr 0x%x, rd 0x%x\n",
110 /* Available mode for the programming sequence */
111 #define IDT821034_MODE_CODEC(_ch) (0x80 | ((_ch) << 2))
112 #define IDT821034_MODE_SLIC(_ch) (0xD0 | ((_ch) << 2))
113 #define IDT821034_MODE_GAIN(_ch) (0xC0 | ((_ch) << 2))
115 /* Power values that can be used in 'power' (can be ORed) */
116 #define IDT821034_CONF_PWRUP_TX BIT(1) /* from analog input to PCM */
117 #define IDT821034_CONF_PWRUP_RX BIT(0) /* from PCM to analog output */
119 static int idt821034_set_channel_power(struct idt821034
*idt821034
, u8 ch
, u8 power
)
124 dev_dbg(&idt821034
->spi
->dev
, "set_channel_power(%u, 0x%x)\n", ch
, power
);
126 conf
= IDT821034_MODE_CODEC(ch
) | idt821034
->cache
.codec_conf
;
128 if (power
& IDT821034_CONF_PWRUP_RX
) {
129 ret
= idt821034_2x8bit_write(idt821034
,
130 conf
| IDT821034_CONF_PWRUP_RX
,
131 idt821034
->cache
.ch
[ch
].rx_slot
);
135 if (power
& IDT821034_CONF_PWRUP_TX
) {
136 ret
= idt821034_2x8bit_write(idt821034
,
137 conf
| IDT821034_CONF_PWRUP_TX
,
138 idt821034
->cache
.ch
[ch
].tx_slot
);
142 if (!(power
& (IDT821034_CONF_PWRUP_TX
| IDT821034_CONF_PWRUP_RX
))) {
143 ret
= idt821034_2x8bit_write(idt821034
, conf
, 0);
148 idt821034
->cache
.ch
[ch
].power
= power
;
153 static u8
idt821034_get_channel_power(struct idt821034
*idt821034
, u8 ch
)
155 return idt821034
->cache
.ch
[ch
].power
;
158 /* Codec configuration values that can be used in 'codec_conf' (can be ORed) */
159 #define IDT821034_CONF_ALAW_MODE BIT(5)
160 #define IDT821034_CONF_DELAY_MODE BIT(4)
162 static int idt821034_set_codec_conf(struct idt821034
*idt821034
, u8 codec_conf
)
168 dev_dbg(&idt821034
->spi
->dev
, "set_codec_conf(0x%x)\n", codec_conf
);
170 /* codec conf fields are common to all channel.
171 * Arbitrary use of channel 0 for this configuration.
174 /* Set Configuration Register */
175 conf
= IDT821034_MODE_CODEC(0) | codec_conf
;
177 /* Update conf value and timeslot register value according
180 if (idt821034
->cache
.ch
[0].power
& IDT821034_CONF_PWRUP_RX
) {
181 conf
|= IDT821034_CONF_PWRUP_RX
;
182 ts
= idt821034
->cache
.ch
[0].rx_slot
;
183 } else if (idt821034
->cache
.ch
[0].power
& IDT821034_CONF_PWRUP_TX
) {
184 conf
|= IDT821034_CONF_PWRUP_TX
;
185 ts
= idt821034
->cache
.ch
[0].tx_slot
;
190 /* Write configuration register and time-slot register */
191 ret
= idt821034_2x8bit_write(idt821034
, conf
, ts
);
195 idt821034
->cache
.codec_conf
= codec_conf
;
199 static u8
idt821034_get_codec_conf(struct idt821034
*idt821034
)
201 return idt821034
->cache
.codec_conf
;
204 /* Channel direction values that can be used in 'ch_dir' (can be ORed) */
205 #define IDT821034_CH_RX BIT(0) /* from PCM to analog output */
206 #define IDT821034_CH_TX BIT(1) /* from analog input to PCM */
208 static int idt821034_set_channel_ts(struct idt821034
*idt821034
, u8 ch
, u8 ch_dir
, u8 ts_num
)
213 dev_dbg(&idt821034
->spi
->dev
, "set_channel_ts(%u, 0x%x, %d)\n", ch
, ch_dir
, ts_num
);
215 conf
= IDT821034_MODE_CODEC(ch
) | idt821034
->cache
.codec_conf
;
217 if (ch_dir
& IDT821034_CH_RX
) {
218 if (idt821034
->cache
.ch
[ch
].power
& IDT821034_CONF_PWRUP_RX
) {
219 ret
= idt821034_2x8bit_write(idt821034
,
220 conf
| IDT821034_CONF_PWRUP_RX
,
225 idt821034
->cache
.ch
[ch
].rx_slot
= ts_num
;
227 if (ch_dir
& IDT821034_CH_TX
) {
228 if (idt821034
->cache
.ch
[ch
].power
& IDT821034_CONF_PWRUP_TX
) {
229 ret
= idt821034_2x8bit_write(idt821034
,
230 conf
| IDT821034_CONF_PWRUP_TX
,
235 idt821034
->cache
.ch
[ch
].tx_slot
= ts_num
;
241 /* SLIC direction values that can be used in 'slic_dir' (can be ORed) */
242 #define IDT821034_SLIC_IO1_IN BIT(1)
243 #define IDT821034_SLIC_IO0_IN BIT(0)
245 static int idt821034_set_slic_conf(struct idt821034
*idt821034
, u8 ch
, u8 slic_dir
)
250 dev_dbg(&idt821034
->spi
->dev
, "set_slic_conf(%u, 0x%x)\n", ch
, slic_dir
);
252 conf
= IDT821034_MODE_SLIC(ch
) | slic_dir
;
253 ret
= idt821034_2x8bit_write(idt821034
, conf
, idt821034
->cache
.ch
[ch
].slic_control
);
257 idt821034
->cache
.ch
[ch
].slic_conf
= slic_dir
;
262 static u8
idt821034_get_slic_conf(struct idt821034
*idt821034
, u8 ch
)
264 return idt821034
->cache
.ch
[ch
].slic_conf
;
267 static int idt821034_write_slic_raw(struct idt821034
*idt821034
, u8 ch
, u8 slic_raw
)
272 dev_dbg(&idt821034
->spi
->dev
, "write_slic_raw(%u, 0x%x)\n", ch
, slic_raw
);
275 * On write, slic_raw is mapped as follow :
283 conf
= IDT821034_MODE_SLIC(ch
) | idt821034
->cache
.ch
[ch
].slic_conf
;
284 ret
= idt821034_2x8bit_write(idt821034
, conf
, slic_raw
);
288 idt821034
->cache
.ch
[ch
].slic_control
= slic_raw
;
292 static u8
idt821034_get_written_slic_raw(struct idt821034
*idt821034
, u8 ch
)
294 return idt821034
->cache
.ch
[ch
].slic_control
;
297 static int idt821034_read_slic_raw(struct idt821034
*idt821034
, u8 ch
, u8
*slic_raw
)
303 * On read, slic_raw is mapped as follow :
309 * b2: I/O1_0, I/O_0 from channel 1 (no matter ch value)
310 * b1: I/O2_0, I/O_0 from channel 2 (no matter ch value)
311 * b2: I/O3_0, I/O_0 from channel 3 (no matter ch value)
314 val
= IDT821034_MODE_SLIC(ch
) | idt821034
->cache
.ch
[ch
].slic_conf
;
315 ret
= idt821034_8bit_write(idt821034
, val
);
319 ret
= idt821034_8bit_read(idt821034
, idt821034
->cache
.ch
[ch
].slic_control
, slic_raw
);
323 dev_dbg(&idt821034
->spi
->dev
, "read_slic_raw(%i) 0x%x\n", ch
, *slic_raw
);
328 /* Gain type values that can be used in 'gain_type' (cannot be ORed) */
329 #define IDT821034_GAIN_RX (0 << 1) /* from PCM to analog output */
330 #define IDT821034_GAIN_TX (1 << 1) /* from analog input to PCM */
332 static int idt821034_set_gain_channel(struct idt821034
*idt821034
, u8 ch
,
333 u8 gain_type
, u16 gain_val
)
338 dev_dbg(&idt821034
->spi
->dev
, "set_gain_channel(%u, 0x%x, 0x%x-%d)\n",
339 ch
, gain_type
, gain_val
, gain_val
);
342 * The gain programming coefficients should be calculated as:
343 * Transmit : Coeff_X = round [ gain_X0dB × gain_X ]
344 * Receive: Coeff_R = round [ gain_R0dB × gain_R ]
347 * gain_X is the target gain;
348 * Coeff_X should be in the range of 0 to 8192.
350 * gain_R is the target gain;
351 * Coeff_R should be in the range of 0 to 8192.
353 * A gain programming coefficient is 14-bit wide and in binary format.
354 * The 7 Most Significant Bits of the coefficient is called
355 * GA_MSB_Transmit for transmit path, or is called GA_MSB_Receive for
356 * receive path; The 7 Least Significant Bits of the coefficient is
357 * called GA_LSB_ Transmit for transmit path, or is called
358 * GA_LSB_Receive for receive path.
360 * An example is given below to clarify the calculation of the
361 * coefficient. To program a +3 dB gain in transmit path and a -3.5 dB
362 * gain in receive path:
364 * Linear Code of +3dB = 10^(3/20)= 1.412537545
365 * Coeff_X = round (1820 × 1.412537545) = 2571
366 * = 0b001010_00001011
367 * GA_MSB_Transmit = 0b0010100
368 * GA_LSB_Transmit = 0b0001011
370 * Linear Code of -3.5dB = 10^(-3.5/20) = 0.668343917
371 * Coeff_R= round (2506 × 0.668343917) = 1675
372 * = 0b0001101_0001011
373 * GA_MSB_Receive = 0b0001101
374 * GA_LSB_Receive = 0b0001011
377 conf
= IDT821034_MODE_GAIN(ch
) | gain_type
;
379 ret
= idt821034_2x8bit_write(idt821034
, conf
| 0x00, gain_val
& 0x007F);
383 ret
= idt821034_2x8bit_write(idt821034
, conf
| 0x01, (gain_val
>> 7) & 0x7F);
390 /* Id helpers used in controls and dapm */
391 #define IDT821034_DIR_OUT (1 << 3)
392 #define IDT821034_DIR_IN (0 << 3)
393 #define IDT821034_ID(_ch, _dir) (((_ch) & 0x03) | (_dir))
394 #define IDT821034_ID_OUT(_ch) IDT821034_ID(_ch, IDT821034_DIR_OUT)
395 #define IDT821034_ID_IN(_ch) IDT821034_ID(_ch, IDT821034_DIR_IN)
397 #define IDT821034_ID_GET_CHAN(_id) ((_id) & 0x03)
398 #define IDT821034_ID_GET_DIR(_id) ((_id) & (1 << 3))
399 #define IDT821034_ID_IS_OUT(_id) (IDT821034_ID_GET_DIR(_id) == IDT821034_DIR_OUT)
401 static int idt821034_kctrl_gain_get(struct snd_kcontrol
*kcontrol
,
402 struct snd_ctl_elem_value
*ucontrol
)
404 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
405 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
406 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(component
);
409 unsigned int mask
= (1 << fls(max
)) - 1;
410 unsigned int invert
= mc
->invert
;
414 ch
= IDT821034_ID_GET_CHAN(mc
->reg
);
416 mutex_lock(&idt821034
->mutex
);
417 if (IDT821034_ID_IS_OUT(mc
->reg
))
418 val
= idt821034
->amps
.ch
[ch
].amp_out
.gain
;
420 val
= idt821034
->amps
.ch
[ch
].amp_in
.gain
;
421 mutex_unlock(&idt821034
->mutex
);
423 ucontrol
->value
.integer
.value
[0] = val
& mask
;
425 ucontrol
->value
.integer
.value
[0] = max
- ucontrol
->value
.integer
.value
[0];
427 ucontrol
->value
.integer
.value
[0] = ucontrol
->value
.integer
.value
[0] - min
;
432 static int idt821034_kctrl_gain_put(struct snd_kcontrol
*kcontrol
,
433 struct snd_ctl_elem_value
*ucontrol
)
435 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)kcontrol
->private_value
;
436 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
437 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(component
);
438 struct idt821034_amp
*amp
;
441 unsigned int mask
= (1 << fls(max
)) - 1;
442 unsigned int invert
= mc
->invert
;
448 val
= ucontrol
->value
.integer
.value
[0];
453 val
= (max
- val
) & mask
;
455 val
= (val
+ min
) & mask
;
457 ch
= IDT821034_ID_GET_CHAN(mc
->reg
);
459 mutex_lock(&idt821034
->mutex
);
461 if (IDT821034_ID_IS_OUT(mc
->reg
)) {
462 amp
= &idt821034
->amps
.ch
[ch
].amp_out
;
463 gain_type
= IDT821034_GAIN_RX
;
465 amp
= &idt821034
->amps
.ch
[ch
].amp_in
;
466 gain_type
= IDT821034_GAIN_TX
;
469 if (amp
->gain
== val
) {
474 if (!amp
->is_muted
) {
475 ret
= idt821034_set_gain_channel(idt821034
, ch
, gain_type
, val
);
481 ret
= 1; /* The value changed */
483 mutex_unlock(&idt821034
->mutex
);
487 static int idt821034_kctrl_mute_get(struct snd_kcontrol
*kcontrol
,
488 struct snd_ctl_elem_value
*ucontrol
)
490 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
491 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(component
);
492 int id
= kcontrol
->private_value
;
496 ch
= IDT821034_ID_GET_CHAN(id
);
498 mutex_lock(&idt821034
->mutex
);
499 is_muted
= IDT821034_ID_IS_OUT(id
) ?
500 idt821034
->amps
.ch
[ch
].amp_out
.is_muted
:
501 idt821034
->amps
.ch
[ch
].amp_in
.is_muted
;
502 mutex_unlock(&idt821034
->mutex
);
504 ucontrol
->value
.integer
.value
[0] = !is_muted
;
509 static int idt821034_kctrl_mute_put(struct snd_kcontrol
*kcontrol
,
510 struct snd_ctl_elem_value
*ucontrol
)
512 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
513 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(component
);
514 int id
= kcontrol
->private_value
;
515 struct idt821034_amp
*amp
;
521 ch
= IDT821034_ID_GET_CHAN(id
);
522 is_mute
= !ucontrol
->value
.integer
.value
[0];
524 mutex_lock(&idt821034
->mutex
);
526 if (IDT821034_ID_IS_OUT(id
)) {
527 amp
= &idt821034
->amps
.ch
[ch
].amp_out
;
528 gain_type
= IDT821034_GAIN_RX
;
530 amp
= &idt821034
->amps
.ch
[ch
].amp_in
;
531 gain_type
= IDT821034_GAIN_TX
;
534 if (amp
->is_muted
== is_mute
) {
539 ret
= idt821034_set_gain_channel(idt821034
, ch
, gain_type
,
540 is_mute
? 0 : amp
->gain
);
544 amp
->is_muted
= is_mute
;
545 ret
= 1; /* The value changed */
547 mutex_unlock(&idt821034
->mutex
);
551 static const DECLARE_TLV_DB_LINEAR(idt821034_gain_in
, -6520, 1306);
552 #define IDT821034_GAIN_IN_MIN_RAW 1 /* -65.20 dB -> 10^(-65.2/20.0) * 1820 = 1 */
553 #define IDT821034_GAIN_IN_MAX_RAW 8191 /* 13.06 dB -> 10^(13.06/20.0) * 1820 = 8191 */
554 #define IDT821034_GAIN_IN_INIT_RAW 1820 /* 0dB -> 10^(0/20) * 1820 = 1820 */
556 static const DECLARE_TLV_DB_LINEAR(idt821034_gain_out
, -6798, 1029);
557 #define IDT821034_GAIN_OUT_MIN_RAW 1 /* -67.98 dB -> 10^(-67.98/20.0) * 2506 = 1*/
558 #define IDT821034_GAIN_OUT_MAX_RAW 8191 /* 10.29 dB -> 10^(10.29/20.0) * 2506 = 8191 */
559 #define IDT821034_GAIN_OUT_INIT_RAW 2506 /* 0dB -> 10^(0/20) * 2506 = 2506 */
561 static const struct snd_kcontrol_new idt821034_controls
[] = {
562 /* DAC volume control */
563 SOC_SINGLE_RANGE_EXT_TLV("DAC0 Playback Volume", IDT821034_ID_OUT(0), 0,
564 IDT821034_GAIN_OUT_MIN_RAW
, IDT821034_GAIN_OUT_MAX_RAW
,
565 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
567 SOC_SINGLE_RANGE_EXT_TLV("DAC1 Playback Volume", IDT821034_ID_OUT(1), 0,
568 IDT821034_GAIN_OUT_MIN_RAW
, IDT821034_GAIN_OUT_MAX_RAW
,
569 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
571 SOC_SINGLE_RANGE_EXT_TLV("DAC2 Playback Volume", IDT821034_ID_OUT(2), 0,
572 IDT821034_GAIN_OUT_MIN_RAW
, IDT821034_GAIN_OUT_MAX_RAW
,
573 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
575 SOC_SINGLE_RANGE_EXT_TLV("DAC3 Playback Volume", IDT821034_ID_OUT(3), 0,
576 IDT821034_GAIN_OUT_MIN_RAW
, IDT821034_GAIN_OUT_MAX_RAW
,
577 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
580 /* DAC mute control */
581 SOC_SINGLE_BOOL_EXT("DAC0 Playback Switch", IDT821034_ID_OUT(0),
582 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
583 SOC_SINGLE_BOOL_EXT("DAC1 Playback Switch", IDT821034_ID_OUT(1),
584 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
585 SOC_SINGLE_BOOL_EXT("DAC2 Playback Switch", IDT821034_ID_OUT(2),
586 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
587 SOC_SINGLE_BOOL_EXT("DAC3 Playback Switch", IDT821034_ID_OUT(3),
588 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
590 /* ADC volume control */
591 SOC_SINGLE_RANGE_EXT_TLV("ADC0 Capture Volume", IDT821034_ID_IN(0), 0,
592 IDT821034_GAIN_IN_MIN_RAW
, IDT821034_GAIN_IN_MAX_RAW
,
593 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
595 SOC_SINGLE_RANGE_EXT_TLV("ADC1 Capture Volume", IDT821034_ID_IN(1), 0,
596 IDT821034_GAIN_IN_MIN_RAW
, IDT821034_GAIN_IN_MAX_RAW
,
597 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
599 SOC_SINGLE_RANGE_EXT_TLV("ADC2 Capture Volume", IDT821034_ID_IN(2), 0,
600 IDT821034_GAIN_IN_MIN_RAW
, IDT821034_GAIN_IN_MAX_RAW
,
601 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
603 SOC_SINGLE_RANGE_EXT_TLV("ADC3 Capture Volume", IDT821034_ID_IN(3), 0,
604 IDT821034_GAIN_IN_MIN_RAW
, IDT821034_GAIN_IN_MAX_RAW
,
605 0, idt821034_kctrl_gain_get
, idt821034_kctrl_gain_put
,
608 /* ADC mute control */
609 SOC_SINGLE_BOOL_EXT("ADC0 Capture Switch", IDT821034_ID_IN(0),
610 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
611 SOC_SINGLE_BOOL_EXT("ADC1 Capture Switch", IDT821034_ID_IN(1),
612 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
613 SOC_SINGLE_BOOL_EXT("ADC2 Capture Switch", IDT821034_ID_IN(2),
614 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
615 SOC_SINGLE_BOOL_EXT("ADC3 Capture Switch", IDT821034_ID_IN(3),
616 idt821034_kctrl_mute_get
, idt821034_kctrl_mute_put
),
619 static int idt821034_power_event(struct snd_soc_dapm_widget
*w
,
620 struct snd_kcontrol
*kcontrol
, int event
)
622 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
623 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(component
);
624 unsigned int id
= w
->shift
;
629 ch
= IDT821034_ID_GET_CHAN(id
);
630 mask
= IDT821034_ID_IS_OUT(id
) ? IDT821034_CONF_PWRUP_RX
: IDT821034_CONF_PWRUP_TX
;
632 mutex_lock(&idt821034
->mutex
);
634 power
= idt821034_get_channel_power(idt821034
, ch
);
635 if (SND_SOC_DAPM_EVENT_ON(event
))
639 ret
= idt821034_set_channel_power(idt821034
, ch
, power
);
641 mutex_unlock(&idt821034
->mutex
);
646 static const struct snd_soc_dapm_widget idt821034_dapm_widgets
[] = {
647 SND_SOC_DAPM_DAC_E("DAC0", "Playback", SND_SOC_NOPM
, IDT821034_ID_OUT(0), 0,
648 idt821034_power_event
,
649 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
650 SND_SOC_DAPM_DAC_E("DAC1", "Playback", SND_SOC_NOPM
, IDT821034_ID_OUT(1), 0,
651 idt821034_power_event
,
652 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
653 SND_SOC_DAPM_DAC_E("DAC2", "Playback", SND_SOC_NOPM
, IDT821034_ID_OUT(2), 0,
654 idt821034_power_event
,
655 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
656 SND_SOC_DAPM_DAC_E("DAC3", "Playback", SND_SOC_NOPM
, IDT821034_ID_OUT(3), 0,
657 idt821034_power_event
,
658 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
660 SND_SOC_DAPM_OUTPUT("OUT0"),
661 SND_SOC_DAPM_OUTPUT("OUT1"),
662 SND_SOC_DAPM_OUTPUT("OUT2"),
663 SND_SOC_DAPM_OUTPUT("OUT3"),
665 SND_SOC_DAPM_DAC_E("ADC0", "Capture", SND_SOC_NOPM
, IDT821034_ID_IN(0), 0,
666 idt821034_power_event
,
667 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
668 SND_SOC_DAPM_DAC_E("ADC1", "Capture", SND_SOC_NOPM
, IDT821034_ID_IN(1), 0,
669 idt821034_power_event
,
670 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
671 SND_SOC_DAPM_DAC_E("ADC2", "Capture", SND_SOC_NOPM
, IDT821034_ID_IN(2), 0,
672 idt821034_power_event
,
673 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
674 SND_SOC_DAPM_DAC_E("ADC3", "Capture", SND_SOC_NOPM
, IDT821034_ID_IN(3), 0,
675 idt821034_power_event
,
676 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
678 SND_SOC_DAPM_INPUT("IN0"),
679 SND_SOC_DAPM_INPUT("IN1"),
680 SND_SOC_DAPM_INPUT("IN2"),
681 SND_SOC_DAPM_INPUT("IN3"),
684 static const struct snd_soc_dapm_route idt821034_dapm_routes
[] = {
685 { "OUT0", NULL
, "DAC0" },
686 { "OUT1", NULL
, "DAC1" },
687 { "OUT2", NULL
, "DAC2" },
688 { "OUT3", NULL
, "DAC3" },
690 { "ADC0", NULL
, "IN0" },
691 { "ADC1", NULL
, "IN1" },
692 { "ADC2", NULL
, "IN2" },
693 { "ADC3", NULL
, "IN3" },
696 static int idt821034_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
697 unsigned int tx_mask
, unsigned int rx_mask
,
698 int slots
, int width
)
700 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(dai
->component
);
707 case 0: /* Not set -> default 8 */
711 dev_err(dai
->dev
, "tdm slot width %d not supported\n", width
);
718 while (mask
&& ch
< IDT821034_NB_CHANNEL
) {
720 mutex_lock(&idt821034
->mutex
);
721 ret
= idt821034_set_channel_ts(idt821034
, ch
, IDT821034_CH_RX
, slot
);
722 mutex_unlock(&idt821034
->mutex
);
724 dev_err(dai
->dev
, "ch%u set tx tdm slot failed (%d)\n",
734 dev_err(dai
->dev
, "too much tx slots defined (mask = 0x%x) support max %d\n",
735 tx_mask
, IDT821034_NB_CHANNEL
);
738 idt821034
->max_ch_playback
= ch
;
743 while (mask
&& ch
< IDT821034_NB_CHANNEL
) {
745 mutex_lock(&idt821034
->mutex
);
746 ret
= idt821034_set_channel_ts(idt821034
, ch
, IDT821034_CH_TX
, slot
);
747 mutex_unlock(&idt821034
->mutex
);
749 dev_err(dai
->dev
, "ch%u set rx tdm slot failed (%d)\n",
759 dev_err(dai
->dev
, "too much rx slots defined (mask = 0x%x) support max %d\n",
760 rx_mask
, IDT821034_NB_CHANNEL
);
763 idt821034
->max_ch_capture
= ch
;
768 static int idt821034_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
770 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(dai
->component
);
774 mutex_lock(&idt821034
->mutex
);
776 conf
= idt821034_get_codec_conf(idt821034
);
778 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
779 case SND_SOC_DAIFMT_DSP_A
:
780 conf
|= IDT821034_CONF_DELAY_MODE
;
782 case SND_SOC_DAIFMT_DSP_B
:
783 conf
&= ~IDT821034_CONF_DELAY_MODE
;
786 dev_err(dai
->dev
, "Unsupported DAI format 0x%x\n",
787 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
791 ret
= idt821034_set_codec_conf(idt821034
, conf
);
793 mutex_unlock(&idt821034
->mutex
);
797 static int idt821034_dai_hw_params(struct snd_pcm_substream
*substream
,
798 struct snd_pcm_hw_params
*params
,
799 struct snd_soc_dai
*dai
)
801 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(dai
->component
);
805 mutex_lock(&idt821034
->mutex
);
807 conf
= idt821034_get_codec_conf(idt821034
);
809 switch (params_format(params
)) {
810 case SNDRV_PCM_FORMAT_A_LAW
:
811 conf
|= IDT821034_CONF_ALAW_MODE
;
813 case SNDRV_PCM_FORMAT_MU_LAW
:
814 conf
&= ~IDT821034_CONF_ALAW_MODE
;
817 dev_err(dai
->dev
, "Unsupported PCM format 0x%x\n",
818 params_format(params
));
822 ret
= idt821034_set_codec_conf(idt821034
, conf
);
824 mutex_unlock(&idt821034
->mutex
);
828 static const unsigned int idt821034_sample_bits
[] = {8};
830 static struct snd_pcm_hw_constraint_list idt821034_sample_bits_constr
= {
831 .list
= idt821034_sample_bits
,
832 .count
= ARRAY_SIZE(idt821034_sample_bits
),
835 static int idt821034_dai_startup(struct snd_pcm_substream
*substream
,
836 struct snd_soc_dai
*dai
)
838 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(dai
->component
);
839 unsigned int max_ch
= 0;
842 max_ch
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
843 idt821034
->max_ch_playback
: idt821034
->max_ch_capture
;
846 * Disable stream support (min = 0, max = 0) if no timeslots were
847 * configured otherwise, limit the number of channels to those
850 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
851 max_ch
? 1 : 0, max_ch
);
855 ret
= snd_pcm_hw_constraint_list(substream
->runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
856 &idt821034_sample_bits_constr
);
863 static const u64 idt821034_dai_formats
[] = {
864 SND_SOC_POSSIBLE_DAIFMT_DSP_A
|
865 SND_SOC_POSSIBLE_DAIFMT_DSP_B
,
868 static const struct snd_soc_dai_ops idt821034_dai_ops
= {
869 .startup
= idt821034_dai_startup
,
870 .hw_params
= idt821034_dai_hw_params
,
871 .set_tdm_slot
= idt821034_dai_set_tdm_slot
,
872 .set_fmt
= idt821034_dai_set_fmt
,
873 .auto_selectable_formats
= idt821034_dai_formats
,
874 .num_auto_selectable_formats
= ARRAY_SIZE(idt821034_dai_formats
),
877 static struct snd_soc_dai_driver idt821034_dai_driver
= {
880 .stream_name
= "Playback",
882 .channels_max
= IDT821034_NB_CHANNEL
,
883 .rates
= SNDRV_PCM_RATE_8000
,
884 .formats
= SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
,
887 .stream_name
= "Capture",
889 .channels_max
= IDT821034_NB_CHANNEL
,
890 .rates
= SNDRV_PCM_RATE_8000
,
891 .formats
= SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
,
893 .ops
= &idt821034_dai_ops
,
896 static int idt821034_reset_audio(struct idt821034
*idt821034
)
901 mutex_lock(&idt821034
->mutex
);
903 ret
= idt821034_set_codec_conf(idt821034
, 0);
907 for (i
= 0; i
< IDT821034_NB_CHANNEL
; i
++) {
908 idt821034
->amps
.ch
[i
].amp_out
.gain
= IDT821034_GAIN_OUT_INIT_RAW
;
909 idt821034
->amps
.ch
[i
].amp_out
.is_muted
= false;
910 ret
= idt821034_set_gain_channel(idt821034
, i
, IDT821034_GAIN_RX
,
911 idt821034
->amps
.ch
[i
].amp_out
.gain
);
915 idt821034
->amps
.ch
[i
].amp_in
.gain
= IDT821034_GAIN_IN_INIT_RAW
;
916 idt821034
->amps
.ch
[i
].amp_in
.is_muted
= false;
917 ret
= idt821034_set_gain_channel(idt821034
, i
, IDT821034_GAIN_TX
,
918 idt821034
->amps
.ch
[i
].amp_in
.gain
);
922 ret
= idt821034_set_channel_power(idt821034
, i
, 0);
929 mutex_unlock(&idt821034
->mutex
);
933 static int idt821034_component_probe(struct snd_soc_component
*component
)
935 struct idt821034
*idt821034
= snd_soc_component_get_drvdata(component
);
938 /* reset idt821034 audio part*/
939 ret
= idt821034_reset_audio(idt821034
);
946 static const struct snd_soc_component_driver idt821034_component_driver
= {
947 .probe
= idt821034_component_probe
,
948 .controls
= idt821034_controls
,
949 .num_controls
= ARRAY_SIZE(idt821034_controls
),
950 .dapm_widgets
= idt821034_dapm_widgets
,
951 .num_dapm_widgets
= ARRAY_SIZE(idt821034_dapm_widgets
),
952 .dapm_routes
= idt821034_dapm_routes
,
953 .num_dapm_routes
= ARRAY_SIZE(idt821034_dapm_routes
),
957 #define IDT821034_GPIO_OFFSET_TO_SLIC_CHANNEL(_offset) (((_offset) / 5) % 4)
958 #define IDT821034_GPIO_OFFSET_TO_SLIC_MASK(_offset) BIT((_offset) % 5)
960 static void idt821034_chip_gpio_set(struct gpio_chip
*c
, unsigned int offset
, int val
)
962 u8 ch
= IDT821034_GPIO_OFFSET_TO_SLIC_CHANNEL(offset
);
963 u8 mask
= IDT821034_GPIO_OFFSET_TO_SLIC_MASK(offset
);
964 struct idt821034
*idt821034
= gpiochip_get_data(c
);
968 mutex_lock(&idt821034
->mutex
);
970 slic_raw
= idt821034_get_written_slic_raw(idt821034
, ch
);
975 ret
= idt821034_write_slic_raw(idt821034
, ch
, slic_raw
);
977 dev_err(&idt821034
->spi
->dev
, "set gpio %d (%u, 0x%x) failed (%d)\n",
978 offset
, ch
, mask
, ret
);
981 mutex_unlock(&idt821034
->mutex
);
984 static int idt821034_chip_gpio_get(struct gpio_chip
*c
, unsigned int offset
)
986 u8 ch
= IDT821034_GPIO_OFFSET_TO_SLIC_CHANNEL(offset
);
987 u8 mask
= IDT821034_GPIO_OFFSET_TO_SLIC_MASK(offset
);
988 struct idt821034
*idt821034
= gpiochip_get_data(c
);
992 mutex_lock(&idt821034
->mutex
);
993 ret
= idt821034_read_slic_raw(idt821034
, ch
, &slic_raw
);
994 mutex_unlock(&idt821034
->mutex
);
996 dev_err(&idt821034
->spi
->dev
, "get gpio %d (%u, 0x%x) failed (%d)\n",
997 offset
, ch
, mask
, ret
);
1002 * SLIC IOs are read in reverse order compared to write.
1003 * Reverse the read value here in order to have IO0 at lsb (ie same
1006 return !!(bitrev8(slic_raw
) & mask
);
1009 static int idt821034_chip_get_direction(struct gpio_chip
*c
, unsigned int offset
)
1011 u8 ch
= IDT821034_GPIO_OFFSET_TO_SLIC_CHANNEL(offset
);
1012 u8 mask
= IDT821034_GPIO_OFFSET_TO_SLIC_MASK(offset
);
1013 struct idt821034
*idt821034
= gpiochip_get_data(c
);
1016 mutex_lock(&idt821034
->mutex
);
1017 slic_dir
= idt821034_get_slic_conf(idt821034
, ch
);
1018 mutex_unlock(&idt821034
->mutex
);
1020 return slic_dir
& mask
? GPIO_LINE_DIRECTION_IN
: GPIO_LINE_DIRECTION_OUT
;
1023 static int idt821034_chip_direction_input(struct gpio_chip
*c
, unsigned int offset
)
1025 u8 ch
= IDT821034_GPIO_OFFSET_TO_SLIC_CHANNEL(offset
);
1026 u8 mask
= IDT821034_GPIO_OFFSET_TO_SLIC_MASK(offset
);
1027 struct idt821034
*idt821034
= gpiochip_get_data(c
);
1031 /* Only IO0 and IO1 can be set as input */
1032 if (mask
& ~(IDT821034_SLIC_IO1_IN
| IDT821034_SLIC_IO0_IN
))
1035 mutex_lock(&idt821034
->mutex
);
1037 slic_conf
= idt821034_get_slic_conf(idt821034
, ch
) | mask
;
1039 ret
= idt821034_set_slic_conf(idt821034
, ch
, slic_conf
);
1041 dev_err(&idt821034
->spi
->dev
, "dir in gpio %d (%u, 0x%x) failed (%d)\n",
1042 offset
, ch
, mask
, ret
);
1045 mutex_unlock(&idt821034
->mutex
);
1049 static int idt821034_chip_direction_output(struct gpio_chip
*c
, unsigned int offset
, int val
)
1051 u8 ch
= IDT821034_GPIO_OFFSET_TO_SLIC_CHANNEL(offset
);
1052 u8 mask
= IDT821034_GPIO_OFFSET_TO_SLIC_MASK(offset
);
1053 struct idt821034
*idt821034
= gpiochip_get_data(c
);
1057 idt821034_chip_gpio_set(c
, offset
, val
);
1059 mutex_lock(&idt821034
->mutex
);
1061 slic_conf
= idt821034_get_slic_conf(idt821034
, ch
) & ~mask
;
1063 ret
= idt821034_set_slic_conf(idt821034
, ch
, slic_conf
);
1065 dev_err(&idt821034
->spi
->dev
, "dir in gpio %d (%u, 0x%x) failed (%d)\n",
1066 offset
, ch
, mask
, ret
);
1069 mutex_unlock(&idt821034
->mutex
);
1073 static int idt821034_reset_gpio(struct idt821034
*idt821034
)
1078 mutex_lock(&idt821034
->mutex
);
1080 /* IO0 and IO1 as input for all channels and output IO set to 0 */
1081 for (i
= 0; i
< IDT821034_NB_CHANNEL
; i
++) {
1082 ret
= idt821034_set_slic_conf(idt821034
, i
,
1083 IDT821034_SLIC_IO1_IN
| IDT821034_SLIC_IO0_IN
);
1087 ret
= idt821034_write_slic_raw(idt821034
, i
, 0);
1094 mutex_unlock(&idt821034
->mutex
);
1098 static int idt821034_gpio_init(struct idt821034
*idt821034
)
1102 ret
= idt821034_reset_gpio(idt821034
);
1106 idt821034
->gpio_chip
.owner
= THIS_MODULE
;
1107 idt821034
->gpio_chip
.label
= dev_name(&idt821034
->spi
->dev
);
1108 idt821034
->gpio_chip
.parent
= &idt821034
->spi
->dev
;
1109 idt821034
->gpio_chip
.base
= -1;
1110 idt821034
->gpio_chip
.ngpio
= 5 * 4; /* 5 GPIOs on 4 channels */
1111 idt821034
->gpio_chip
.get_direction
= idt821034_chip_get_direction
;
1112 idt821034
->gpio_chip
.direction_input
= idt821034_chip_direction_input
;
1113 idt821034
->gpio_chip
.direction_output
= idt821034_chip_direction_output
;
1114 idt821034
->gpio_chip
.get
= idt821034_chip_gpio_get
;
1115 idt821034
->gpio_chip
.set
= idt821034_chip_gpio_set
;
1116 idt821034
->gpio_chip
.can_sleep
= true;
1118 return devm_gpiochip_add_data(&idt821034
->spi
->dev
, &idt821034
->gpio_chip
,
1122 static int idt821034_spi_probe(struct spi_device
*spi
)
1124 struct idt821034
*idt821034
;
1127 spi
->bits_per_word
= 8;
1128 ret
= spi_setup(spi
);
1132 idt821034
= devm_kzalloc(&spi
->dev
, sizeof(*idt821034
), GFP_KERNEL
);
1136 idt821034
->spi
= spi
;
1138 mutex_init(&idt821034
->mutex
);
1140 spi_set_drvdata(spi
, idt821034
);
1142 ret
= devm_snd_soc_register_component(&spi
->dev
, &idt821034_component_driver
,
1143 &idt821034_dai_driver
, 1);
1147 if (IS_ENABLED(CONFIG_GPIOLIB
))
1148 return idt821034_gpio_init(idt821034
);
1153 static const struct of_device_id idt821034_of_match
[] = {
1154 { .compatible
= "renesas,idt821034", },
1157 MODULE_DEVICE_TABLE(of
, idt821034_of_match
);
1159 static const struct spi_device_id idt821034_id_table
[] = {
1163 MODULE_DEVICE_TABLE(spi
, idt821034_id_table
);
1165 static struct spi_driver idt821034_spi_driver
= {
1167 .name
= "idt821034",
1168 .of_match_table
= idt821034_of_match
,
1170 .id_table
= idt821034_id_table
,
1171 .probe
= idt821034_spi_probe
,
1174 module_spi_driver(idt821034_spi_driver
);
1176 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
1177 MODULE_DESCRIPTION("IDT821034 ALSA SoC driver");
1178 MODULE_LICENSE("GPL");