24 #include <linux/module.h>
29 #include <linux/i2c.h>
33 #include <linux/slab.h>
50 #define DAC33_FIFO_SIZE_16BIT 6144
51 #define DAC33_FIFO_SIZE_24BIT 4096
52 #define DAC33_MODE7_MARGIN 10
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))))
84 #define DAC33_NUM_SUPPLIES 3
130 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00,
140 0x00, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x00, 0x80,
142 0x80, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00,
149 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00,
158 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00,
163 0x00, 0x00, 0x00, 0x00,
164 0x00, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0x00, 0x00,
166 0x00, 0x00, 0x00, 0x00,
167 0x00, 0x00, 0x00, 0x00,
168 0x00, 0x00, 0x00, 0x00,
185 static inline void dac33_write_reg_cache(
struct snd_soc_codec *codec,
195 static int dac33_read(
struct snd_soc_codec *codec,
unsigned int reg,
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,
233 data[0] = reg & 0xff;
234 data[1] = value & 0xff;
236 dac33_write_reg_cache(codec, data[0], data[1]);
240 dev_err(codec->
dev,
"Write failed (%d)\n", ret);
248 static int dac33_write_locked(
struct snd_soc_codec *codec,
unsigned int reg,
255 ret = dac33_write(codec, reg, value);
261 #define DAC33_I2C_ADDR_AUTOINC 0x80
262 static int dac33_write16(
struct snd_soc_codec *codec,
unsigned int reg,
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]);
287 dev_err(codec->
dev,
"Write failed (%d)\n", ret);
339 for (i = 0; i < 3; i++) {
348 static inline void dac33_soft_power(
struct snd_soc_codec *codec,
int power)
361 static inline void dac33_disable_digital(
struct snd_soc_codec *codec)
376 static int dac33_hard_power(
struct snd_soc_codec *codec,
int power)
385 dev_dbg(codec->
dev,
"Trying to set the same power state: %s\n",
386 power ?
"ON" :
"OFF");
395 "Failed to enable supplies: %d\n", ret);
404 dac33_soft_power(codec, 0);
412 "Failed to disable supplies: %d\n", ret);
437 dac33_disable_digital(w->
codec);
443 static int dac33_get_fifo_mode(
struct snd_kcontrol *kcontrol,
454 static int dac33_set_fifo_mode(
struct snd_kcontrol *kcontrol,
467 if (ucontrol->
value.integer.value[0] < 0 ||
477 static const char *dac33_fifo_mode_texts[] = {
478 "Bypass",
"Mode 1",
"Mode 7"
481 static const struct soc_enum dac33_fifo_mode_enum =
483 dac33_fifo_mode_texts);
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 =
494 lr_lineout_gain_texts);
496 static const struct soc_enum r_lineout_gain_enum =
499 lr_lineout_gain_texts);
513 0, 0x7f, 1, dac_digivol_tlv),
518 SOC_ENUM(
"Left Line Output Gain", l_lineout_gain_enum),
519 SOC_ENUM(
"Right Line Output Gain", r_lineout_gain_enum),
524 dac33_get_fifo_mode, dac33_set_fifo_mode),
535 static const char *dac33_lr_lom_texts[] = {
"DAC",
"LOP"};
537 static const struct soc_enum dac33_left_lom_enum =
545 static const struct soc_enum dac33_right_lom_enum =
565 &dac33_dapm_abypassl_control),
567 &dac33_dapm_abypassr_control),
570 &dac33_dapm_left_lom_control),
572 &dac33_dapm_right_lom_control),
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"},
644 ret = dac33_hard_power(codec, 1);
648 dac33_init_chip(codec);
655 ret = dac33_hard_power(codec, 0);
680 spin_unlock_irqrestore(&dac33->
lock, flags);
696 spin_unlock_irqrestore(&dac33->
lock, flags);
721 spin_unlock_irqrestore(&dac33->
lock, flags);
743 codec = dac33->
codec;
746 switch (dac33->
state) {
749 dac33_prefill_handler(dac33);
752 dac33_playback_handler(dac33);
778 spin_unlock_irqrestore(&dac33->
lock, flags);
798 "internal oscillator calibration failed\n");
822 #define CALC_BURST_RATE(bclkdiv, bclk_per_sample) \
823 (BURST_BASEFREQ_HZ / bclkdiv / bclk_per_sample)
852 dev_err(codec->
dev,
"unsupported format %d\n",
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)
874 unsigned int oscset, ratioset, pwr_ctrl, reg_tmp;
875 u8 aictrl_a, aictrl_b, fifoctrl_a;
877 switch (substream->
runtime->rate) {
898 switch (substream->
runtime->format) {
907 dev_err(codec->
dev,
"unsupported format %d\n",
923 dac33_soft_power(codec, 0);
924 dac33_soft_power(codec, 1);
946 dac33_oscwait(codec);
1072 unsigned int period_size = substream->
runtime->period_size;
1074 unsigned int nsample_limit;
1092 dac33->
nsample = period_size *
1096 else if (period_size > nsample_limit)
1097 dac33->
nsample = nsample_limit;
1162 unsigned long long t0,
t1, t_now;
1163 unsigned int time_delta,
uthr;
1164 int samples_out, samples_in, samples;
1166 unsigned long flags;
1175 spin_unlock_irqrestore(&dac33->
lock, flags);
1187 time_delta = t_now -
t0;
1201 time_delta = t_now -
t0;
1206 time_delta = t_now -
t1;
1212 samples += (samples_in - samples_out);
1223 time_delta = t_now -
t0;
1230 samples += (samples_in - samples_out);
1243 spin_unlock_irqrestore(&dac33->
lock, flags);
1259 time_delta = t_now -
t0;
1269 if (
likely(uthr > samples_out))
1270 delay = uthr - samples_out;
1301 static int dac33_set_dai_sysclk(
struct snd_soc_dai *codec_dai,
1302 int clk_id,
unsigned int freq,
int dir)
1306 u8 ioc_reg, asrcb_reg;
1320 dev_err(codec->
dev,
"Invalid clock ID (%d)\n", clk_id);
1331 static int dac33_set_dai_fmt(
struct snd_soc_dai *codec_dai,
1336 u8 aictrl_a, aictrl_b;
1349 dev_err(codec->
dev,
"FIFO mode requires master mode\n");
1375 dev_err(codec->
dev,
"Unsupported format (%u)\n",
1376 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
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);
1411 if (dac33->
irq >= 0) {
1414 codec->
name, codec);
1416 dev_err(codec->
dev,
"Could not request IRQ%d (%d)\n",
1420 if (dac33->
irq != -1) {
1434 if (dac33->
irq >= 0)
1448 if (dac33->
irq >= 0) {
1470 .read = dac33_read_reg_cache,
1471 .write = dac33_write_locked,
1472 .set_bias_level = dac33_set_bias_level,
1473 .idle_bias_off =
true,
1475 .reg_word_size =
sizeof(
u8),
1477 .
probe = dac33_soc_probe,
1478 .
remove = dac33_soc_remove,
1480 .
resume = dac33_soc_resume,
1484 .dapm_widgets = dac33_dapm_widgets,
1485 .num_dapm_widgets =
ARRAY_SIZE(dac33_dapm_widgets),
1486 .dapm_routes = audio_map,
1490 #define DAC33_RATES (SNDRV_PCM_RATE_44100 | \
1491 SNDRV_PCM_RATE_48000)
1492 #define DAC33_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1495 .startup = dac33_startup,
1496 .shutdown = dac33_shutdown,
1497 .hw_params = dac33_hw_params,
1498 .trigger = dac33_pcm_trigger,
1499 .delay = dac33_dai_delay,
1500 .set_sysclk = dac33_set_dai_sysclk,
1501 .set_fmt = dac33_set_dai_fmt,
1505 .name =
"tlv320dac33-hifi",
1507 .stream_name =
"Playback",
1514 .ops = &dac33_dai_ops,
1524 if (client->
dev.platform_data ==
NULL) {
1525 dev_err(&client->
dev,
"Platform data not set\n");
1528 pdata = client->
dev.platform_data;
1539 i2c_set_clientdata(client, dac33);
1547 dac33->
irq = client->
irq;
1556 "Failed to request reset GPIO (%d)\n",
1564 dac33->
supplies[i].supply = dac33_supply_names[i];
1570 dev_err(&client->
dev,
"Failed to request supplies: %d\n", ret);
1575 &soc_codec_dev_tlv320dac33, &dac33_dai, 1);
1594 dac33_hard_power(dac33->
codec, 0);
1607 .name =
"tlv320dac33",
1614 static struct i2c_driver tlv320dac33_i2c_driver = {
1616 .name =
"tlv320dac33-codec",
1619 .probe = dac33_i2c_probe,
1621 .id_table = tlv320dac33_i2c_id,