2 * ALSA SoC Texas Instruments TLV320DAC33 codec driver
4 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
6 * Copyright: (C) 2009 Nokia Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
29 #include <linux/i2c.h>
30 #include <linux/interrupt.h>
31 #include <linux/gpio.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
39 #include <sound/tlv.h>
41 #include <sound/tlv320dac33-plat.h>
42 #include "tlv320dac33.h"
45 * The internal FIFO is 24576 bytes long
46 * It can be configured to hold 16bit or 24bit samples
47 * In 16bit configuration the FIFO can hold 6144 stereo samples
48 * In 24bit configuration the FIFO can hold 4096 stereo samples
50 #define DAC33_FIFO_SIZE_16BIT 6144
51 #define DAC33_FIFO_SIZE_24BIT 4096
52 #define DAC33_MODE7_MARGIN 10 /* Safety margin for FIFO in Mode7 */
54 #define BURST_BASEFREQ_HZ 49152000
56 #define SAMPLES_TO_US(rate, samples) \
57 (1000000000 / (((rate) * 1000) / (samples)))
59 #define US_TO_SAMPLES(rate, us) \
60 ((rate) / (1000000 / ((us) < 1000000 ? (us) : 1000000)))
62 #define UTHR_FROM_PERIOD_SIZE(samples, playrate, burstrate) \
63 (((samples)*5000) / (((burstrate)*5000) / ((burstrate) - (playrate))))
65 static void dac33_calculate_times(struct snd_pcm_substream
*substream
,
66 struct snd_soc_codec
*codec
);
67 static int dac33_prepare_chip(struct snd_pcm_substream
*substream
,
68 struct snd_soc_codec
*codec
);
77 enum dac33_fifo_modes
{
78 DAC33_FIFO_BYPASS
= 0,
84 #define DAC33_NUM_SUPPLIES 3
85 static const char *dac33_supply_names
[DAC33_NUM_SUPPLIES
] = {
91 struct tlv320dac33_priv
{
93 struct workqueue_struct
*dac33_wq
;
94 struct work_struct work
;
95 struct snd_soc_codec
*codec
;
96 struct regulator_bulk_data supplies
[DAC33_NUM_SUPPLIES
];
97 struct snd_pcm_substream
*substream
;
103 unsigned int alarm_threshold
; /* set to be half of LATENCY_TIME_MS */
104 enum dac33_fifo_modes fifo_mode
;/* FIFO mode selection */
105 unsigned int fifo_size
; /* Size of the FIFO in samples */
106 unsigned int nsample
; /* burst read amount from host */
107 int mode1_latency
; /* latency caused by the i2c writes in
109 u8 burst_bclkdiv
; /* BCLK divider value in burst mode */
110 unsigned int burst_rate
; /* Interface speed in Burst modes */
112 int keep_bclk
; /* Keep the BCLK continuously running
115 unsigned long long t_stamp1
; /* Time stamp for FIFO modes to */
116 unsigned long long t_stamp2
; /* calculate the FIFO caused delay */
118 unsigned int mode1_us_burst
; /* Time to burst read n number of
120 unsigned int mode7_us_to_lthr
; /* Time to reach lthr from uthr */
124 enum dac33_state state
;
125 enum snd_soc_control_type control_type
;
129 static const u8 dac33_reg
[DAC33_CACHEREGNUM
] = {
130 0x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */
131 0x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */
132 0x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */
133 0x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */
134 0x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */
135 0x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */
136 0x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */
137 0x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */
138 0x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */
139 0x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */
140 0x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */
141 0x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */
142 0x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */
143 0x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */
144 0x00, 0x00, /* 0x38 - 0x39 */
145 /* Registers 0x3a - 0x3f are reserved */
146 0x00, 0x00, /* 0x3a - 0x3b */
147 0x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */
149 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */
150 0x00, 0x80, /* 0x44 - 0x45 */
151 /* Registers 0x46 - 0x47 are reserved */
152 0x80, 0x80, /* 0x46 - 0x47 */
154 0x80, 0x00, 0x00, /* 0x48 - 0x4a */
155 /* Registers 0x4b - 0x7c are reserved */
157 0x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */
158 0x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */
159 0x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */
160 0x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */
161 0x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */
162 0x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */
163 0x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */
164 0x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */
165 0x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */
166 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */
167 0x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */
168 0x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */
171 0xda, 0x33, 0x03, /* 0x7d - 0x7f */
174 /* Register read and write */
175 static inline unsigned int dac33_read_reg_cache(struct snd_soc_codec
*codec
,
178 u8
*cache
= codec
->reg_cache
;
179 if (reg
>= DAC33_CACHEREGNUM
)
185 static inline void dac33_write_reg_cache(struct snd_soc_codec
*codec
,
188 u8
*cache
= codec
->reg_cache
;
189 if (reg
>= DAC33_CACHEREGNUM
)
195 static int dac33_read(struct snd_soc_codec
*codec
, unsigned int reg
,
198 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
203 /* If powered off, return the cached value */
204 if (dac33
->chip_power
) {
205 val
= i2c_smbus_read_byte_data(codec
->control_data
, value
[0]);
207 dev_err(codec
->dev
, "Read failed (%d)\n", val
);
208 value
[0] = dac33_read_reg_cache(codec
, reg
);
212 dac33_write_reg_cache(codec
, reg
, val
);
215 value
[0] = dac33_read_reg_cache(codec
, reg
);
221 static int dac33_write(struct snd_soc_codec
*codec
, unsigned int reg
,
224 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
230 * D15..D8 dac33 register offset
231 * D7...D0 register data
233 data
[0] = reg
& 0xff;
234 data
[1] = value
& 0xff;
236 dac33_write_reg_cache(codec
, data
[0], data
[1]);
237 if (dac33
->chip_power
) {
238 ret
= codec
->hw_write(codec
->control_data
, data
, 2);
240 dev_err(codec
->dev
, "Write failed (%d)\n", ret
);
248 static int dac33_write_locked(struct snd_soc_codec
*codec
, unsigned int reg
,
251 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
254 mutex_lock(&dac33
->mutex
);
255 ret
= dac33_write(codec
, reg
, value
);
256 mutex_unlock(&dac33
->mutex
);
261 #define DAC33_I2C_ADDR_AUTOINC 0x80
262 static int dac33_write16(struct snd_soc_codec
*codec
, unsigned int reg
,
265 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
271 * D23..D16 dac33 register offset
272 * D15..D8 register data MSB
273 * D7...D0 register data LSB
275 data
[0] = reg
& 0xff;
276 data
[1] = (value
>> 8) & 0xff;
277 data
[2] = value
& 0xff;
279 dac33_write_reg_cache(codec
, data
[0], data
[1]);
280 dac33_write_reg_cache(codec
, data
[0] + 1, data
[2]);
282 if (dac33
->chip_power
) {
283 /* We need to set autoincrement mode for 16 bit writes */
284 data
[0] |= DAC33_I2C_ADDR_AUTOINC
;
285 ret
= codec
->hw_write(codec
->control_data
, data
, 3);
287 dev_err(codec
->dev
, "Write failed (%d)\n", ret
);
295 static void dac33_init_chip(struct snd_soc_codec
*codec
)
297 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
299 if (unlikely(!dac33
->chip_power
))
302 /* A : DAC sample rate Fsref/1.5 */
303 dac33_write(codec
, DAC33_DAC_CTRL_A
, DAC33_DACRATE(0));
304 /* B : DAC src=normal, not muted */
305 dac33_write(codec
, DAC33_DAC_CTRL_B
, DAC33_DACSRCR_RIGHT
|
308 dac33_write(codec
, DAC33_DAC_CTRL_C
, 0x00);
310 /* 73 : volume soft stepping control,
311 clock source = internal osc (?) */
312 dac33_write(codec
, DAC33_ANA_VOL_SOFT_STEP_CTRL
, DAC33_VOLCLKEN
);
314 /* Restore only selected registers (gains mostly) */
315 dac33_write(codec
, DAC33_LDAC_DIG_VOL_CTRL
,
316 dac33_read_reg_cache(codec
, DAC33_LDAC_DIG_VOL_CTRL
));
317 dac33_write(codec
, DAC33_RDAC_DIG_VOL_CTRL
,
318 dac33_read_reg_cache(codec
, DAC33_RDAC_DIG_VOL_CTRL
));
320 dac33_write(codec
, DAC33_LINEL_TO_LLO_VOL
,
321 dac33_read_reg_cache(codec
, DAC33_LINEL_TO_LLO_VOL
));
322 dac33_write(codec
, DAC33_LINER_TO_RLO_VOL
,
323 dac33_read_reg_cache(codec
, DAC33_LINER_TO_RLO_VOL
));
325 dac33_write(codec
, DAC33_OUT_AMP_CTRL
,
326 dac33_read_reg_cache(codec
, DAC33_OUT_AMP_CTRL
));
328 dac33_write(codec
, DAC33_LDAC_PWR_CTRL
,
329 dac33_read_reg_cache(codec
, DAC33_LDAC_PWR_CTRL
));
330 dac33_write(codec
, DAC33_RDAC_PWR_CTRL
,
331 dac33_read_reg_cache(codec
, DAC33_RDAC_PWR_CTRL
));
334 static inline int dac33_read_id(struct snd_soc_codec
*codec
)
339 for (i
= 0; i
< 3; i
++) {
340 ret
= dac33_read(codec
, DAC33_DEVICE_ID_MSB
+ i
, ®
);
348 static inline void dac33_soft_power(struct snd_soc_codec
*codec
, int power
)
352 reg
= dac33_read_reg_cache(codec
, DAC33_PWR_CTRL
);
354 reg
|= DAC33_PDNALLB
;
356 reg
&= ~(DAC33_PDNALLB
| DAC33_OSCPDNB
|
357 DAC33_DACRPDNB
| DAC33_DACLPDNB
);
358 dac33_write(codec
, DAC33_PWR_CTRL
, reg
);
361 static inline void dac33_disable_digital(struct snd_soc_codec
*codec
)
365 /* Stop the DAI clock */
366 reg
= dac33_read_reg_cache(codec
, DAC33_SER_AUDIOIF_CTRL_B
);
367 reg
&= ~DAC33_BCLKON
;
368 dac33_write(codec
, DAC33_SER_AUDIOIF_CTRL_B
, reg
);
370 /* Power down the Oscillator, and DACs */
371 reg
= dac33_read_reg_cache(codec
, DAC33_PWR_CTRL
);
372 reg
&= ~(DAC33_OSCPDNB
| DAC33_DACRPDNB
| DAC33_DACLPDNB
);
373 dac33_write(codec
, DAC33_PWR_CTRL
, reg
);
376 static int dac33_hard_power(struct snd_soc_codec
*codec
, int power
)
378 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
381 mutex_lock(&dac33
->mutex
);
384 if (unlikely(power
== dac33
->chip_power
)) {
385 dev_dbg(codec
->dev
, "Trying to set the same power state: %s\n",
386 power
? "ON" : "OFF");
391 ret
= regulator_bulk_enable(ARRAY_SIZE(dac33
->supplies
),
395 "Failed to enable supplies: %d\n", ret
);
399 if (dac33
->power_gpio
>= 0)
400 gpio_set_value(dac33
->power_gpio
, 1);
402 dac33
->chip_power
= 1;
404 dac33_soft_power(codec
, 0);
405 if (dac33
->power_gpio
>= 0)
406 gpio_set_value(dac33
->power_gpio
, 0);
408 ret
= regulator_bulk_disable(ARRAY_SIZE(dac33
->supplies
),
412 "Failed to disable supplies: %d\n", ret
);
416 dac33
->chip_power
= 0;
420 mutex_unlock(&dac33
->mutex
);
424 static int dac33_playback_event(struct snd_soc_dapm_widget
*w
,
425 struct snd_kcontrol
*kcontrol
, int event
)
427 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(w
->codec
);
430 case SND_SOC_DAPM_PRE_PMU
:
431 if (likely(dac33
->substream
)) {
432 dac33_calculate_times(dac33
->substream
, w
->codec
);
433 dac33_prepare_chip(dac33
->substream
, w
->codec
);
436 case SND_SOC_DAPM_POST_PMD
:
437 dac33_disable_digital(w
->codec
);
443 static int dac33_get_fifo_mode(struct snd_kcontrol
*kcontrol
,
444 struct snd_ctl_elem_value
*ucontrol
)
446 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
447 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
449 ucontrol
->value
.integer
.value
[0] = dac33
->fifo_mode
;
454 static int dac33_set_fifo_mode(struct snd_kcontrol
*kcontrol
,
455 struct snd_ctl_elem_value
*ucontrol
)
457 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
458 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
461 if (dac33
->fifo_mode
== ucontrol
->value
.integer
.value
[0])
463 /* Do not allow changes while stream is running*/
467 if (ucontrol
->value
.integer
.value
[0] < 0 ||
468 ucontrol
->value
.integer
.value
[0] >= DAC33_FIFO_LAST_MODE
)
471 dac33
->fifo_mode
= ucontrol
->value
.integer
.value
[0];
476 /* Codec operation modes */
477 static const char *dac33_fifo_mode_texts
[] = {
478 "Bypass", "Mode 1", "Mode 7"
481 static const struct soc_enum dac33_fifo_mode_enum
=
482 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(dac33_fifo_mode_texts
),
483 dac33_fifo_mode_texts
);
485 /* L/R Line Output Gain */
486 static const char *lr_lineout_gain_texts
[] = {
487 "Line -12dB DAC 0dB", "Line -6dB DAC 6dB",
488 "Line 0dB DAC 12dB", "Line 6dB DAC 18dB",
491 static const struct soc_enum l_lineout_gain_enum
=
492 SOC_ENUM_SINGLE(DAC33_LDAC_PWR_CTRL
, 0,
493 ARRAY_SIZE(lr_lineout_gain_texts
),
494 lr_lineout_gain_texts
);
496 static const struct soc_enum r_lineout_gain_enum
=
497 SOC_ENUM_SINGLE(DAC33_RDAC_PWR_CTRL
, 0,
498 ARRAY_SIZE(lr_lineout_gain_texts
),
499 lr_lineout_gain_texts
);
502 * DACL/R digital volume control:
503 * from 0 dB to -63.5 in 0.5 dB steps
504 * Need to be inverted later on:
508 static DECLARE_TLV_DB_SCALE(dac_digivol_tlv
, -6350, 50, 0);
510 static const struct snd_kcontrol_new dac33_snd_controls
[] = {
511 SOC_DOUBLE_R_TLV("DAC Digital Playback Volume",
512 DAC33_LDAC_DIG_VOL_CTRL
, DAC33_RDAC_DIG_VOL_CTRL
,
513 0, 0x7f, 1, dac_digivol_tlv
),
514 SOC_DOUBLE_R("DAC Digital Playback Switch",
515 DAC33_LDAC_DIG_VOL_CTRL
, DAC33_RDAC_DIG_VOL_CTRL
, 7, 1, 1),
516 SOC_DOUBLE_R("Line to Line Out Volume",
517 DAC33_LINEL_TO_LLO_VOL
, DAC33_LINER_TO_RLO_VOL
, 0, 127, 1),
518 SOC_ENUM("Left Line Output Gain", l_lineout_gain_enum
),
519 SOC_ENUM("Right Line Output Gain", r_lineout_gain_enum
),
522 static const struct snd_kcontrol_new dac33_mode_snd_controls
[] = {
523 SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum
,
524 dac33_get_fifo_mode
, dac33_set_fifo_mode
),
528 static const struct snd_kcontrol_new dac33_dapm_abypassl_control
=
529 SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL
, 7, 1, 1);
531 static const struct snd_kcontrol_new dac33_dapm_abypassr_control
=
532 SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL
, 7, 1, 1);
534 /* LOP L/R invert selection */
535 static const char *dac33_lr_lom_texts
[] = {"DAC", "LOP"};
537 static const struct soc_enum dac33_left_lom_enum
=
538 SOC_ENUM_SINGLE(DAC33_OUT_AMP_CTRL
, 3,
539 ARRAY_SIZE(dac33_lr_lom_texts
),
542 static const struct snd_kcontrol_new dac33_dapm_left_lom_control
=
543 SOC_DAPM_ENUM("Route", dac33_left_lom_enum
);
545 static const struct soc_enum dac33_right_lom_enum
=
546 SOC_ENUM_SINGLE(DAC33_OUT_AMP_CTRL
, 2,
547 ARRAY_SIZE(dac33_lr_lom_texts
),
550 static const struct snd_kcontrol_new dac33_dapm_right_lom_control
=
551 SOC_DAPM_ENUM("Route", dac33_right_lom_enum
);
553 static const struct snd_soc_dapm_widget dac33_dapm_widgets
[] = {
554 SND_SOC_DAPM_OUTPUT("LEFT_LO"),
555 SND_SOC_DAPM_OUTPUT("RIGHT_LO"),
557 SND_SOC_DAPM_INPUT("LINEL"),
558 SND_SOC_DAPM_INPUT("LINER"),
560 SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM
, 0, 0),
561 SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM
, 0, 0),
564 SND_SOC_DAPM_SWITCH("Analog Left Bypass", SND_SOC_NOPM
, 0, 0,
565 &dac33_dapm_abypassl_control
),
566 SND_SOC_DAPM_SWITCH("Analog Right Bypass", SND_SOC_NOPM
, 0, 0,
567 &dac33_dapm_abypassr_control
),
569 SND_SOC_DAPM_MUX("Left LOM Inverted From", SND_SOC_NOPM
, 0, 0,
570 &dac33_dapm_left_lom_control
),
571 SND_SOC_DAPM_MUX("Right LOM Inverted From", SND_SOC_NOPM
, 0, 0,
572 &dac33_dapm_right_lom_control
),
574 * For DAPM path, when only the anlog bypass path is enabled, and the
575 * LOP inverted from the corresponding DAC side.
576 * This is needed, so we can attach the DAC power supply in this case.
578 SND_SOC_DAPM_PGA("Left Bypass PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
579 SND_SOC_DAPM_PGA("Right Bypass PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
581 SND_SOC_DAPM_REG(snd_soc_dapm_mixer
, "Output Left Amplifier",
582 DAC33_OUT_AMP_PWR_CTRL
, 6, 3, 3, 0),
583 SND_SOC_DAPM_REG(snd_soc_dapm_mixer
, "Output Right Amplifier",
584 DAC33_OUT_AMP_PWR_CTRL
, 4, 3, 3, 0),
586 SND_SOC_DAPM_SUPPLY("Left DAC Power",
587 DAC33_LDAC_PWR_CTRL
, 2, 0, NULL
, 0),
588 SND_SOC_DAPM_SUPPLY("Right DAC Power",
589 DAC33_RDAC_PWR_CTRL
, 2, 0, NULL
, 0),
591 SND_SOC_DAPM_SUPPLY("Codec Power",
592 DAC33_PWR_CTRL
, 4, 0, NULL
, 0),
594 SND_SOC_DAPM_PRE("Pre Playback", dac33_playback_event
),
595 SND_SOC_DAPM_POST("Post Playback", dac33_playback_event
),
598 static const struct snd_soc_dapm_route audio_map
[] = {
600 {"Analog Left Bypass", "Switch", "LINEL"},
601 {"Analog Right Bypass", "Switch", "LINER"},
603 {"Output Left Amplifier", NULL
, "DACL"},
604 {"Output Right Amplifier", NULL
, "DACR"},
606 {"Left Bypass PGA", NULL
, "Analog Left Bypass"},
607 {"Right Bypass PGA", NULL
, "Analog Right Bypass"},
609 {"Left LOM Inverted From", "DAC", "Left Bypass PGA"},
610 {"Right LOM Inverted From", "DAC", "Right Bypass PGA"},
611 {"Left LOM Inverted From", "LOP", "Analog Left Bypass"},
612 {"Right LOM Inverted From", "LOP", "Analog Right Bypass"},
614 {"Output Left Amplifier", NULL
, "Left LOM Inverted From"},
615 {"Output Right Amplifier", NULL
, "Right LOM Inverted From"},
617 {"DACL", NULL
, "Left DAC Power"},
618 {"DACR", NULL
, "Right DAC Power"},
620 {"Left Bypass PGA", NULL
, "Left DAC Power"},
621 {"Right Bypass PGA", NULL
, "Right DAC Power"},
624 {"LEFT_LO", NULL
, "Output Left Amplifier"},
625 {"RIGHT_LO", NULL
, "Output Right Amplifier"},
627 {"LEFT_LO", NULL
, "Codec Power"},
628 {"RIGHT_LO", NULL
, "Codec Power"},
631 static int dac33_set_bias_level(struct snd_soc_codec
*codec
,
632 enum snd_soc_bias_level level
)
637 case SND_SOC_BIAS_ON
:
639 case SND_SOC_BIAS_PREPARE
:
641 case SND_SOC_BIAS_STANDBY
:
642 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
643 /* Coming from OFF, switch on the codec */
644 ret
= dac33_hard_power(codec
, 1);
648 dac33_init_chip(codec
);
651 case SND_SOC_BIAS_OFF
:
652 /* Do not power off, when the codec is already off */
653 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
)
655 ret
= dac33_hard_power(codec
, 0);
660 codec
->dapm
.bias_level
= level
;
665 static inline void dac33_prefill_handler(struct tlv320dac33_priv
*dac33
)
667 struct snd_soc_codec
*codec
= dac33
->codec
;
671 switch (dac33
->fifo_mode
) {
672 case DAC33_FIFO_MODE1
:
673 dac33_write16(codec
, DAC33_NSAMPLE_MSB
,
674 DAC33_THRREG(dac33
->nsample
));
676 /* Take the timestamps */
677 spin_lock_irqsave(&dac33
->lock
, flags
);
678 dac33
->t_stamp2
= ktime_to_us(ktime_get());
679 dac33
->t_stamp1
= dac33
->t_stamp2
;
680 spin_unlock_irqrestore(&dac33
->lock
, flags
);
682 dac33_write16(codec
, DAC33_PREFILL_MSB
,
683 DAC33_THRREG(dac33
->alarm_threshold
));
684 /* Enable Alarm Threshold IRQ with a delay */
685 delay
= SAMPLES_TO_US(dac33
->burst_rate
,
686 dac33
->alarm_threshold
) + 1000;
687 usleep_range(delay
, delay
+ 500);
688 dac33_write(codec
, DAC33_FIFO_IRQ_MASK
, DAC33_MAT
);
690 case DAC33_FIFO_MODE7
:
691 /* Take the timestamp */
692 spin_lock_irqsave(&dac33
->lock
, flags
);
693 dac33
->t_stamp1
= ktime_to_us(ktime_get());
694 /* Move back the timestamp with drain time */
695 dac33
->t_stamp1
-= dac33
->mode7_us_to_lthr
;
696 spin_unlock_irqrestore(&dac33
->lock
, flags
);
698 dac33_write16(codec
, DAC33_PREFILL_MSB
,
699 DAC33_THRREG(DAC33_MODE7_MARGIN
));
701 /* Enable Upper Threshold IRQ */
702 dac33_write(codec
, DAC33_FIFO_IRQ_MASK
, DAC33_MUT
);
705 dev_warn(codec
->dev
, "Unhandled FIFO mode: %d\n",
711 static inline void dac33_playback_handler(struct tlv320dac33_priv
*dac33
)
713 struct snd_soc_codec
*codec
= dac33
->codec
;
716 switch (dac33
->fifo_mode
) {
717 case DAC33_FIFO_MODE1
:
718 /* Take the timestamp */
719 spin_lock_irqsave(&dac33
->lock
, flags
);
720 dac33
->t_stamp2
= ktime_to_us(ktime_get());
721 spin_unlock_irqrestore(&dac33
->lock
, flags
);
723 dac33_write16(codec
, DAC33_NSAMPLE_MSB
,
724 DAC33_THRREG(dac33
->nsample
));
726 case DAC33_FIFO_MODE7
:
727 /* At the moment we are not using interrupts in mode7 */
730 dev_warn(codec
->dev
, "Unhandled FIFO mode: %d\n",
736 static void dac33_work(struct work_struct
*work
)
738 struct snd_soc_codec
*codec
;
739 struct tlv320dac33_priv
*dac33
;
742 dac33
= container_of(work
, struct tlv320dac33_priv
, work
);
743 codec
= dac33
->codec
;
745 mutex_lock(&dac33
->mutex
);
746 switch (dac33
->state
) {
748 dac33
->state
= DAC33_PLAYBACK
;
749 dac33_prefill_handler(dac33
);
752 dac33_playback_handler(dac33
);
757 dac33
->state
= DAC33_IDLE
;
758 /* Mask all interrupts from dac33 */
759 dac33_write(codec
, DAC33_FIFO_IRQ_MASK
, 0);
762 reg
= dac33_read_reg_cache(codec
, DAC33_FIFO_CTRL_A
);
763 reg
|= DAC33_FIFOFLUSH
;
764 dac33_write(codec
, DAC33_FIFO_CTRL_A
, reg
);
767 mutex_unlock(&dac33
->mutex
);
770 static irqreturn_t
dac33_interrupt_handler(int irq
, void *dev
)
772 struct snd_soc_codec
*codec
= dev
;
773 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
776 spin_lock_irqsave(&dac33
->lock
, flags
);
777 dac33
->t_stamp1
= ktime_to_us(ktime_get());
778 spin_unlock_irqrestore(&dac33
->lock
, flags
);
780 /* Do not schedule the workqueue in Mode7 */
781 if (dac33
->fifo_mode
!= DAC33_FIFO_MODE7
)
782 queue_work(dac33
->dac33_wq
, &dac33
->work
);
787 static void dac33_oscwait(struct snd_soc_codec
*codec
)
793 usleep_range(1000, 2000);
794 dac33_read(codec
, DAC33_INT_OSC_STATUS
, ®
);
795 } while (((reg
& 0x03) != DAC33_OSCSTATUS_NORMAL
) && timeout
--);
796 if ((reg
& 0x03) != DAC33_OSCSTATUS_NORMAL
)
798 "internal oscillator calibration failed\n");
801 static int dac33_startup(struct snd_pcm_substream
*substream
,
802 struct snd_soc_dai
*dai
)
804 struct snd_soc_codec
*codec
= dai
->codec
;
805 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
807 /* Stream started, save the substream pointer */
808 dac33
->substream
= substream
;
813 static void dac33_shutdown(struct snd_pcm_substream
*substream
,
814 struct snd_soc_dai
*dai
)
816 struct snd_soc_codec
*codec
= dai
->codec
;
817 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
819 dac33
->substream
= NULL
;
822 #define CALC_BURST_RATE(bclkdiv, bclk_per_sample) \
823 (BURST_BASEFREQ_HZ / bclkdiv / bclk_per_sample)
824 static int dac33_hw_params(struct snd_pcm_substream
*substream
,
825 struct snd_pcm_hw_params
*params
,
826 struct snd_soc_dai
*dai
)
828 struct snd_soc_codec
*codec
= dai
->codec
;
829 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
831 /* Check parameters for validity */
832 switch (params_rate(params
)) {
837 dev_err(codec
->dev
, "unsupported rate %d\n",
838 params_rate(params
));
842 switch (params_format(params
)) {
843 case SNDRV_PCM_FORMAT_S16_LE
:
844 dac33
->fifo_size
= DAC33_FIFO_SIZE_16BIT
;
845 dac33
->burst_rate
= CALC_BURST_RATE(dac33
->burst_bclkdiv
, 32);
847 case SNDRV_PCM_FORMAT_S32_LE
:
848 dac33
->fifo_size
= DAC33_FIFO_SIZE_24BIT
;
849 dac33
->burst_rate
= CALC_BURST_RATE(dac33
->burst_bclkdiv
, 64);
852 dev_err(codec
->dev
, "unsupported format %d\n",
853 params_format(params
));
860 #define CALC_OSCSET(rate, refclk) ( \
861 ((((rate * 10000) / refclk) * 4096) + 7000) / 10000)
862 #define CALC_RATIOSET(rate, refclk) ( \
863 ((((refclk * 100000) / rate) * 16384) + 50000) / 100000)
866 * tlv320dac33 is strict on the sequence of the register writes, if the register
867 * writes happens in different order, than dac33 might end up in unknown state.
868 * Use the known, working sequence of register writes to initialize the dac33.
870 static int dac33_prepare_chip(struct snd_pcm_substream
*substream
,
871 struct snd_soc_codec
*codec
)
873 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
874 unsigned int oscset
, ratioset
, pwr_ctrl
, reg_tmp
;
875 u8 aictrl_a
, aictrl_b
, fifoctrl_a
;
877 switch (substream
->runtime
->rate
) {
880 oscset
= CALC_OSCSET(substream
->runtime
->rate
, dac33
->refclk
);
881 ratioset
= CALC_RATIOSET(substream
->runtime
->rate
,
885 dev_err(codec
->dev
, "unsupported rate %d\n",
886 substream
->runtime
->rate
);
891 aictrl_a
= dac33_read_reg_cache(codec
, DAC33_SER_AUDIOIF_CTRL_A
);
892 aictrl_a
&= ~(DAC33_NCYCL_MASK
| DAC33_WLEN_MASK
);
893 /* Read FIFO control A, and clear FIFO flush bit */
894 fifoctrl_a
= dac33_read_reg_cache(codec
, DAC33_FIFO_CTRL_A
);
895 fifoctrl_a
&= ~DAC33_FIFOFLUSH
;
897 fifoctrl_a
&= ~DAC33_WIDTH
;
898 switch (substream
->runtime
->format
) {
899 case SNDRV_PCM_FORMAT_S16_LE
:
900 aictrl_a
|= (DAC33_NCYCL_16
| DAC33_WLEN_16
);
901 fifoctrl_a
|= DAC33_WIDTH
;
903 case SNDRV_PCM_FORMAT_S32_LE
:
904 aictrl_a
|= (DAC33_NCYCL_32
| DAC33_WLEN_24
);
907 dev_err(codec
->dev
, "unsupported format %d\n",
908 substream
->runtime
->format
);
912 mutex_lock(&dac33
->mutex
);
914 if (!dac33
->chip_power
) {
916 * Chip is not powered yet.
917 * Do the init in the dac33_set_bias_level later.
919 mutex_unlock(&dac33
->mutex
);
923 dac33_soft_power(codec
, 0);
924 dac33_soft_power(codec
, 1);
926 reg_tmp
= dac33_read_reg_cache(codec
, DAC33_INT_OSC_CTRL
);
927 dac33_write(codec
, DAC33_INT_OSC_CTRL
, reg_tmp
);
929 /* Write registers 0x08 and 0x09 (MSB, LSB) */
930 dac33_write16(codec
, DAC33_INT_OSC_FREQ_RAT_A
, oscset
);
932 /* OSC calibration time */
933 dac33_write(codec
, DAC33_CALIB_TIME
, 96);
935 /* adjustment treshold & step */
936 dac33_write(codec
, DAC33_INT_OSC_CTRL_B
, DAC33_ADJTHRSHLD(2) |
939 /* div=4 / gain=1 / div */
940 dac33_write(codec
, DAC33_INT_OSC_CTRL_C
, DAC33_REFDIV(4));
942 pwr_ctrl
= dac33_read_reg_cache(codec
, DAC33_PWR_CTRL
);
943 pwr_ctrl
|= DAC33_OSCPDNB
| DAC33_DACRPDNB
| DAC33_DACLPDNB
;
944 dac33_write(codec
, DAC33_PWR_CTRL
, pwr_ctrl
);
946 dac33_oscwait(codec
);
948 if (dac33
->fifo_mode
) {
949 /* Generic for all FIFO modes */
950 /* 50-51 : ASRC Control registers */
951 dac33_write(codec
, DAC33_ASRC_CTRL_A
, DAC33_SRCLKDIV(1));
952 dac33_write(codec
, DAC33_ASRC_CTRL_B
, 1); /* ??? */
954 /* Write registers 0x34 and 0x35 (MSB, LSB) */
955 dac33_write16(codec
, DAC33_SRC_REF_CLK_RATIO_A
, ratioset
);
957 /* Set interrupts to high active */
958 dac33_write(codec
, DAC33_INTP_CTRL_A
, DAC33_INTPM_AHIGH
);
960 /* FIFO bypass mode */
961 /* 50-51 : ASRC Control registers */
962 dac33_write(codec
, DAC33_ASRC_CTRL_A
, DAC33_SRCBYP
);
963 dac33_write(codec
, DAC33_ASRC_CTRL_B
, 0); /* ??? */
966 /* Interrupt behaviour configuration */
967 switch (dac33
->fifo_mode
) {
968 case DAC33_FIFO_MODE1
:
969 dac33_write(codec
, DAC33_FIFO_IRQ_MODE_B
,
970 DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL
));
972 case DAC33_FIFO_MODE7
:
973 dac33_write(codec
, DAC33_FIFO_IRQ_MODE_A
,
974 DAC33_UTM(DAC33_FIFO_IRQ_MODE_LEVEL
));
977 /* in FIFO bypass mode, the interrupts are not used */
981 aictrl_b
= dac33_read_reg_cache(codec
, DAC33_SER_AUDIOIF_CTRL_B
);
983 switch (dac33
->fifo_mode
) {
984 case DAC33_FIFO_MODE1
:
987 * Disable the FIFO bypass (Enable the use of FIFO)
988 * Select nSample mode
989 * BCLK is only running when data is needed by DAC33
991 fifoctrl_a
&= ~DAC33_FBYPAS
;
992 fifoctrl_a
&= ~DAC33_FAUTO
;
993 if (dac33
->keep_bclk
)
994 aictrl_b
|= DAC33_BCLKON
;
996 aictrl_b
&= ~DAC33_BCLKON
;
998 case DAC33_FIFO_MODE7
:
1001 * Disable the FIFO bypass (Enable the use of FIFO)
1002 * Select Threshold mode
1003 * BCLK is only running when data is needed by DAC33
1005 fifoctrl_a
&= ~DAC33_FBYPAS
;
1006 fifoctrl_a
|= DAC33_FAUTO
;
1007 if (dac33
->keep_bclk
)
1008 aictrl_b
|= DAC33_BCLKON
;
1010 aictrl_b
&= ~DAC33_BCLKON
;
1014 * For FIFO bypass mode:
1015 * Enable the FIFO bypass (Disable the FIFO use)
1016 * Set the BCLK as continuous
1018 fifoctrl_a
|= DAC33_FBYPAS
;
1019 aictrl_b
|= DAC33_BCLKON
;
1023 dac33_write(codec
, DAC33_FIFO_CTRL_A
, fifoctrl_a
);
1024 dac33_write(codec
, DAC33_SER_AUDIOIF_CTRL_A
, aictrl_a
);
1025 dac33_write(codec
, DAC33_SER_AUDIOIF_CTRL_B
, aictrl_b
);
1036 if (dac33
->fifo_mode
)
1037 dac33_write(codec
, DAC33_SER_AUDIOIF_CTRL_C
,
1038 dac33
->burst_bclkdiv
);
1040 if (substream
->runtime
->format
== SNDRV_PCM_FORMAT_S16_LE
)
1041 dac33_write(codec
, DAC33_SER_AUDIOIF_CTRL_C
, 32);
1043 dac33_write(codec
, DAC33_SER_AUDIOIF_CTRL_C
, 16);
1045 switch (dac33
->fifo_mode
) {
1046 case DAC33_FIFO_MODE1
:
1047 dac33_write16(codec
, DAC33_ATHR_MSB
,
1048 DAC33_THRREG(dac33
->alarm_threshold
));
1050 case DAC33_FIFO_MODE7
:
1052 * Configure the threshold levels, and leave 10 sample space
1053 * at the bottom, and also at the top of the FIFO
1055 dac33_write16(codec
, DAC33_UTHR_MSB
, DAC33_THRREG(dac33
->uthr
));
1056 dac33_write16(codec
, DAC33_LTHR_MSB
,
1057 DAC33_THRREG(DAC33_MODE7_MARGIN
));
1063 mutex_unlock(&dac33
->mutex
);
1068 static void dac33_calculate_times(struct snd_pcm_substream
*substream
,
1069 struct snd_soc_codec
*codec
)
1071 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
1072 unsigned int period_size
= substream
->runtime
->period_size
;
1073 unsigned int rate
= substream
->runtime
->rate
;
1074 unsigned int nsample_limit
;
1076 /* In bypass mode we don't need to calculate */
1077 if (!dac33
->fifo_mode
)
1080 switch (dac33
->fifo_mode
) {
1081 case DAC33_FIFO_MODE1
:
1082 /* Number of samples under i2c latency */
1083 dac33
->alarm_threshold
= US_TO_SAMPLES(rate
,
1084 dac33
->mode1_latency
);
1085 nsample_limit
= dac33
->fifo_size
- dac33
->alarm_threshold
;
1087 if (period_size
<= dac33
->alarm_threshold
)
1089 * Configure nSamaple to number of periods,
1090 * which covers the latency requironment.
1092 dac33
->nsample
= period_size
*
1093 ((dac33
->alarm_threshold
/ period_size
) +
1094 (dac33
->alarm_threshold
% period_size
?
1096 else if (period_size
> nsample_limit
)
1097 dac33
->nsample
= nsample_limit
;
1099 dac33
->nsample
= period_size
;
1101 dac33
->mode1_us_burst
= SAMPLES_TO_US(dac33
->burst_rate
,
1103 dac33
->t_stamp1
= 0;
1104 dac33
->t_stamp2
= 0;
1106 case DAC33_FIFO_MODE7
:
1107 dac33
->uthr
= UTHR_FROM_PERIOD_SIZE(period_size
, rate
,
1108 dac33
->burst_rate
) + 9;
1109 if (dac33
->uthr
> (dac33
->fifo_size
- DAC33_MODE7_MARGIN
))
1110 dac33
->uthr
= dac33
->fifo_size
- DAC33_MODE7_MARGIN
;
1111 if (dac33
->uthr
< (DAC33_MODE7_MARGIN
+ 10))
1112 dac33
->uthr
= (DAC33_MODE7_MARGIN
+ 10);
1114 dac33
->mode7_us_to_lthr
=
1115 SAMPLES_TO_US(substream
->runtime
->rate
,
1116 dac33
->uthr
- DAC33_MODE7_MARGIN
+ 1);
1117 dac33
->t_stamp1
= 0;
1125 static int dac33_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
,
1126 struct snd_soc_dai
*dai
)
1128 struct snd_soc_codec
*codec
= dai
->codec
;
1129 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
1133 case SNDRV_PCM_TRIGGER_START
:
1134 case SNDRV_PCM_TRIGGER_RESUME
:
1135 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1136 if (dac33
->fifo_mode
) {
1137 dac33
->state
= DAC33_PREFILL
;
1138 queue_work(dac33
->dac33_wq
, &dac33
->work
);
1141 case SNDRV_PCM_TRIGGER_STOP
:
1142 case SNDRV_PCM_TRIGGER_SUSPEND
:
1143 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1144 if (dac33
->fifo_mode
) {
1145 dac33
->state
= DAC33_FLUSH
;
1146 queue_work(dac33
->dac33_wq
, &dac33
->work
);
1156 static snd_pcm_sframes_t
dac33_dai_delay(
1157 struct snd_pcm_substream
*substream
,
1158 struct snd_soc_dai
*dai
)
1160 struct snd_soc_codec
*codec
= dai
->codec
;
1161 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
1162 unsigned long long t0
, t1
, t_now
;
1163 unsigned int time_delta
, uthr
;
1164 int samples_out
, samples_in
, samples
;
1165 snd_pcm_sframes_t delay
= 0;
1166 unsigned long flags
;
1168 switch (dac33
->fifo_mode
) {
1169 case DAC33_FIFO_BYPASS
:
1171 case DAC33_FIFO_MODE1
:
1172 spin_lock_irqsave(&dac33
->lock
, flags
);
1173 t0
= dac33
->t_stamp1
;
1174 t1
= dac33
->t_stamp2
;
1175 spin_unlock_irqrestore(&dac33
->lock
, flags
);
1176 t_now
= ktime_to_us(ktime_get());
1178 /* We have not started to fill the FIFO yet, delay is 0 */
1185 * After Alarm threshold, and before nSample write
1187 time_delta
= t_now
- t0
;
1188 samples_out
= time_delta
? US_TO_SAMPLES(
1189 substream
->runtime
->rate
,
1192 if (likely(dac33
->alarm_threshold
> samples_out
))
1193 delay
= dac33
->alarm_threshold
- samples_out
;
1196 } else if ((t_now
- t1
) <= dac33
->mode1_us_burst
) {
1199 * After nSample write (during burst operation)
1201 time_delta
= t_now
- t0
;
1202 samples_out
= time_delta
? US_TO_SAMPLES(
1203 substream
->runtime
->rate
,
1206 time_delta
= t_now
- t1
;
1207 samples_in
= time_delta
? US_TO_SAMPLES(
1211 samples
= dac33
->alarm_threshold
;
1212 samples
+= (samples_in
- samples_out
);
1214 if (likely(samples
> 0))
1221 * After burst operation, before next alarm threshold
1223 time_delta
= t_now
- t0
;
1224 samples_out
= time_delta
? US_TO_SAMPLES(
1225 substream
->runtime
->rate
,
1228 samples_in
= dac33
->nsample
;
1229 samples
= dac33
->alarm_threshold
;
1230 samples
+= (samples_in
- samples_out
);
1232 if (likely(samples
> 0))
1233 delay
= samples
> dac33
->fifo_size
?
1234 dac33
->fifo_size
: samples
;
1239 case DAC33_FIFO_MODE7
:
1240 spin_lock_irqsave(&dac33
->lock
, flags
);
1241 t0
= dac33
->t_stamp1
;
1243 spin_unlock_irqrestore(&dac33
->lock
, flags
);
1244 t_now
= ktime_to_us(ktime_get());
1246 /* We have not started to fill the FIFO yet, delay is 0 */
1252 * Either the timestamps are messed or equal. Report
1259 time_delta
= t_now
- t0
;
1260 if (time_delta
<= dac33
->mode7_us_to_lthr
) {
1263 * After burst (draining phase)
1265 samples_out
= US_TO_SAMPLES(
1266 substream
->runtime
->rate
,
1269 if (likely(uthr
> samples_out
))
1270 delay
= uthr
- samples_out
;
1276 * During burst operation
1278 time_delta
= time_delta
- dac33
->mode7_us_to_lthr
;
1280 samples_out
= US_TO_SAMPLES(
1281 substream
->runtime
->rate
,
1283 samples_in
= US_TO_SAMPLES(
1286 delay
= DAC33_MODE7_MARGIN
+ samples_in
- samples_out
;
1288 if (unlikely(delay
> uthr
))
1293 dev_warn(codec
->dev
, "Unhandled FIFO mode: %d\n",
1301 static int dac33_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
1302 int clk_id
, unsigned int freq
, int dir
)
1304 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1305 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
1306 u8 ioc_reg
, asrcb_reg
;
1308 ioc_reg
= dac33_read_reg_cache(codec
, DAC33_INT_OSC_CTRL
);
1309 asrcb_reg
= dac33_read_reg_cache(codec
, DAC33_ASRC_CTRL_B
);
1311 case TLV320DAC33_MCLK
:
1312 ioc_reg
|= DAC33_REFSEL
;
1313 asrcb_reg
|= DAC33_SRCREFSEL
;
1315 case TLV320DAC33_SLEEPCLK
:
1316 ioc_reg
&= ~DAC33_REFSEL
;
1317 asrcb_reg
&= ~DAC33_SRCREFSEL
;
1320 dev_err(codec
->dev
, "Invalid clock ID (%d)\n", clk_id
);
1323 dac33
->refclk
= freq
;
1325 dac33_write_reg_cache(codec
, DAC33_INT_OSC_CTRL
, ioc_reg
);
1326 dac33_write_reg_cache(codec
, DAC33_ASRC_CTRL_B
, asrcb_reg
);
1331 static int dac33_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
1334 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1335 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
1336 u8 aictrl_a
, aictrl_b
;
1338 aictrl_a
= dac33_read_reg_cache(codec
, DAC33_SER_AUDIOIF_CTRL_A
);
1339 aictrl_b
= dac33_read_reg_cache(codec
, DAC33_SER_AUDIOIF_CTRL_B
);
1340 /* set master/slave audio interface */
1341 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1342 case SND_SOC_DAIFMT_CBM_CFM
:
1344 aictrl_a
|= (DAC33_MSBCLK
| DAC33_MSWCLK
);
1346 case SND_SOC_DAIFMT_CBS_CFS
:
1348 if (dac33
->fifo_mode
) {
1349 dev_err(codec
->dev
, "FIFO mode requires master mode\n");
1352 aictrl_a
&= ~(DAC33_MSBCLK
| DAC33_MSWCLK
);
1358 aictrl_a
&= ~DAC33_AFMT_MASK
;
1359 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1360 case SND_SOC_DAIFMT_I2S
:
1361 aictrl_a
|= DAC33_AFMT_I2S
;
1363 case SND_SOC_DAIFMT_DSP_A
:
1364 aictrl_a
|= DAC33_AFMT_DSP
;
1365 aictrl_b
&= ~DAC33_DATA_DELAY_MASK
;
1366 aictrl_b
|= DAC33_DATA_DELAY(0);
1368 case SND_SOC_DAIFMT_RIGHT_J
:
1369 aictrl_a
|= DAC33_AFMT_RIGHT_J
;
1371 case SND_SOC_DAIFMT_LEFT_J
:
1372 aictrl_a
|= DAC33_AFMT_LEFT_J
;
1375 dev_err(codec
->dev
, "Unsupported format (%u)\n",
1376 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
1380 dac33_write_reg_cache(codec
, DAC33_SER_AUDIOIF_CTRL_A
, aictrl_a
);
1381 dac33_write_reg_cache(codec
, DAC33_SER_AUDIOIF_CTRL_B
, aictrl_b
);
1386 static int dac33_soc_probe(struct snd_soc_codec
*codec
)
1388 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
1391 codec
->control_data
= dac33
->control_data
;
1392 codec
->hw_write
= (hw_write_t
) i2c_master_send
;
1393 dac33
->codec
= codec
;
1395 /* Read the tlv320dac33 ID registers */
1396 ret
= dac33_hard_power(codec
, 1);
1398 dev_err(codec
->dev
, "Failed to power up codec: %d\n", ret
);
1401 ret
= dac33_read_id(codec
);
1402 dac33_hard_power(codec
, 0);
1405 dev_err(codec
->dev
, "Failed to read chip ID: %d\n", ret
);
1410 /* Check if the IRQ number is valid and request it */
1411 if (dac33
->irq
>= 0) {
1412 ret
= request_irq(dac33
->irq
, dac33_interrupt_handler
,
1413 IRQF_TRIGGER_RISING
,
1414 codec
->name
, codec
);
1416 dev_err(codec
->dev
, "Could not request IRQ%d (%d)\n",
1420 if (dac33
->irq
!= -1) {
1421 /* Setup work queue */
1423 create_singlethread_workqueue("tlv320dac33");
1424 if (dac33
->dac33_wq
== NULL
) {
1425 free_irq(dac33
->irq
, codec
);
1429 INIT_WORK(&dac33
->work
, dac33_work
);
1433 /* Only add the FIFO controls, if we have valid IRQ number */
1434 if (dac33
->irq
>= 0)
1435 snd_soc_add_codec_controls(codec
, dac33_mode_snd_controls
,
1436 ARRAY_SIZE(dac33_mode_snd_controls
));
1442 static int dac33_soc_remove(struct snd_soc_codec
*codec
)
1444 struct tlv320dac33_priv
*dac33
= snd_soc_codec_get_drvdata(codec
);
1446 dac33_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1448 if (dac33
->irq
>= 0) {
1449 free_irq(dac33
->irq
, dac33
->codec
);
1450 destroy_workqueue(dac33
->dac33_wq
);
1455 static struct snd_soc_codec_driver soc_codec_dev_tlv320dac33
= {
1456 .read
= dac33_read_reg_cache
,
1457 .write
= dac33_write_locked
,
1458 .set_bias_level
= dac33_set_bias_level
,
1459 .idle_bias_off
= true,
1460 .reg_cache_size
= ARRAY_SIZE(dac33_reg
),
1461 .reg_word_size
= sizeof(u8
),
1462 .reg_cache_default
= dac33_reg
,
1463 .probe
= dac33_soc_probe
,
1464 .remove
= dac33_soc_remove
,
1466 .controls
= dac33_snd_controls
,
1467 .num_controls
= ARRAY_SIZE(dac33_snd_controls
),
1468 .dapm_widgets
= dac33_dapm_widgets
,
1469 .num_dapm_widgets
= ARRAY_SIZE(dac33_dapm_widgets
),
1470 .dapm_routes
= audio_map
,
1471 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
1474 #define DAC33_RATES (SNDRV_PCM_RATE_44100 | \
1475 SNDRV_PCM_RATE_48000)
1476 #define DAC33_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1478 static const struct snd_soc_dai_ops dac33_dai_ops
= {
1479 .startup
= dac33_startup
,
1480 .shutdown
= dac33_shutdown
,
1481 .hw_params
= dac33_hw_params
,
1482 .trigger
= dac33_pcm_trigger
,
1483 .delay
= dac33_dai_delay
,
1484 .set_sysclk
= dac33_set_dai_sysclk
,
1485 .set_fmt
= dac33_set_dai_fmt
,
1488 static struct snd_soc_dai_driver dac33_dai
= {
1489 .name
= "tlv320dac33-hifi",
1491 .stream_name
= "Playback",
1494 .rates
= DAC33_RATES
,
1495 .formats
= DAC33_FORMATS
,
1498 .ops
= &dac33_dai_ops
,
1501 static int dac33_i2c_probe(struct i2c_client
*client
,
1502 const struct i2c_device_id
*id
)
1504 struct tlv320dac33_platform_data
*pdata
;
1505 struct tlv320dac33_priv
*dac33
;
1508 if (client
->dev
.platform_data
== NULL
) {
1509 dev_err(&client
->dev
, "Platform data not set\n");
1512 pdata
= client
->dev
.platform_data
;
1514 dac33
= devm_kzalloc(&client
->dev
, sizeof(struct tlv320dac33_priv
),
1519 dac33
->control_data
= client
;
1520 mutex_init(&dac33
->mutex
);
1521 spin_lock_init(&dac33
->lock
);
1523 i2c_set_clientdata(client
, dac33
);
1525 dac33
->power_gpio
= pdata
->power_gpio
;
1526 dac33
->burst_bclkdiv
= pdata
->burst_bclkdiv
;
1527 dac33
->keep_bclk
= pdata
->keep_bclk
;
1528 dac33
->mode1_latency
= pdata
->mode1_latency
;
1529 if (!dac33
->mode1_latency
)
1530 dac33
->mode1_latency
= 10000; /* 10ms */
1531 dac33
->irq
= client
->irq
;
1532 /* Disable FIFO use by default */
1533 dac33
->fifo_mode
= DAC33_FIFO_BYPASS
;
1535 /* Check if the reset GPIO number is valid and request it */
1536 if (dac33
->power_gpio
>= 0) {
1537 ret
= gpio_request(dac33
->power_gpio
, "tlv320dac33 reset");
1539 dev_err(&client
->dev
,
1540 "Failed to request reset GPIO (%d)\n",
1544 gpio_direction_output(dac33
->power_gpio
, 0);
1547 for (i
= 0; i
< ARRAY_SIZE(dac33
->supplies
); i
++)
1548 dac33
->supplies
[i
].supply
= dac33_supply_names
[i
];
1550 ret
= regulator_bulk_get(&client
->dev
, ARRAY_SIZE(dac33
->supplies
),
1554 dev_err(&client
->dev
, "Failed to request supplies: %d\n", ret
);
1558 ret
= snd_soc_register_codec(&client
->dev
,
1559 &soc_codec_dev_tlv320dac33
, &dac33_dai
, 1);
1565 regulator_bulk_free(ARRAY_SIZE(dac33
->supplies
), dac33
->supplies
);
1567 if (dac33
->power_gpio
>= 0)
1568 gpio_free(dac33
->power_gpio
);
1573 static int dac33_i2c_remove(struct i2c_client
*client
)
1575 struct tlv320dac33_priv
*dac33
= i2c_get_clientdata(client
);
1577 if (unlikely(dac33
->chip_power
))
1578 dac33_hard_power(dac33
->codec
, 0);
1580 if (dac33
->power_gpio
>= 0)
1581 gpio_free(dac33
->power_gpio
);
1583 regulator_bulk_free(ARRAY_SIZE(dac33
->supplies
), dac33
->supplies
);
1585 snd_soc_unregister_codec(&client
->dev
);
1589 static const struct i2c_device_id tlv320dac33_i2c_id
[] = {
1591 .name
= "tlv320dac33",
1596 MODULE_DEVICE_TABLE(i2c
, tlv320dac33_i2c_id
);
1598 static struct i2c_driver tlv320dac33_i2c_driver
= {
1600 .name
= "tlv320dac33-codec",
1601 .owner
= THIS_MODULE
,
1603 .probe
= dac33_i2c_probe
,
1604 .remove
= dac33_i2c_remove
,
1605 .id_table
= tlv320dac33_i2c_id
,
1608 module_i2c_driver(tlv320dac33_i2c_driver
);
1610 MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver");
1611 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
1612 MODULE_LICENSE("GPL");