22 #include <linux/module.h>
23 #include <linux/slab.h>
26 #include <linux/i2c.h>
35 #define CS4271_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
36 SNDRV_PCM_FMTBIT_S24_LE | \
37 SNDRV_PCM_FMTBIT_S32_LE)
38 #define CS4271_PCM_RATES SNDRV_PCM_RATE_8000_192000
45 #define CS4271_MODE1 0x2001
46 #define CS4271_DACCTL 0x2002
47 #define CS4271_DACVOL 0x2003
48 #define CS4271_VOLA 0x2004
49 #define CS4271_VOLB 0x2005
50 #define CS4271_ADCCTL 0x2006
51 #define CS4271_MODE2 0x2007
52 #define CS4271_CHIPID 0x2008
54 #define CS4271_FIRSTREG CS4271_MODE1
55 #define CS4271_LASTREG CS4271_MODE2
56 #define CS4271_NR_REGS ((CS4271_LASTREG & 0xFF) + 1)
59 #define CS4271_MODE1_MODE_MASK 0xC0
60 #define CS4271_MODE1_MODE_1X 0x00
61 #define CS4271_MODE1_MODE_2X 0x80
62 #define CS4271_MODE1_MODE_4X 0xC0
64 #define CS4271_MODE1_DIV_MASK 0x30
65 #define CS4271_MODE1_DIV_1 0x00
66 #define CS4271_MODE1_DIV_15 0x10
67 #define CS4271_MODE1_DIV_2 0x20
68 #define CS4271_MODE1_DIV_3 0x30
70 #define CS4271_MODE1_MASTER 0x08
72 #define CS4271_MODE1_DAC_DIF_MASK 0x07
73 #define CS4271_MODE1_DAC_DIF_LJ 0x00
74 #define CS4271_MODE1_DAC_DIF_I2S 0x01
75 #define CS4271_MODE1_DAC_DIF_RJ16 0x02
76 #define CS4271_MODE1_DAC_DIF_RJ24 0x03
77 #define CS4271_MODE1_DAC_DIF_RJ20 0x04
78 #define CS4271_MODE1_DAC_DIF_RJ18 0x05
80 #define CS4271_DACCTL_AMUTE 0x80
81 #define CS4271_DACCTL_IF_SLOW 0x40
83 #define CS4271_DACCTL_DEM_MASK 0x30
84 #define CS4271_DACCTL_DEM_DIS 0x00
85 #define CS4271_DACCTL_DEM_441 0x10
86 #define CS4271_DACCTL_DEM_48 0x20
87 #define CS4271_DACCTL_DEM_32 0x30
89 #define CS4271_DACCTL_SVRU 0x08
90 #define CS4271_DACCTL_SRD 0x04
91 #define CS4271_DACCTL_INVA 0x02
92 #define CS4271_DACCTL_INVB 0x01
94 #define CS4271_DACVOL_BEQUA 0x40
95 #define CS4271_DACVOL_SOFT 0x20
96 #define CS4271_DACVOL_ZEROC 0x10
98 #define CS4271_DACVOL_ATAPI_MASK 0x0F
99 #define CS4271_DACVOL_ATAPI_M_M 0x00
100 #define CS4271_DACVOL_ATAPI_M_BR 0x01
101 #define CS4271_DACVOL_ATAPI_M_BL 0x02
102 #define CS4271_DACVOL_ATAPI_M_BLR2 0x03
103 #define CS4271_DACVOL_ATAPI_AR_M 0x04
104 #define CS4271_DACVOL_ATAPI_AR_BR 0x05
105 #define CS4271_DACVOL_ATAPI_AR_BL 0x06
106 #define CS4271_DACVOL_ATAPI_AR_BLR2 0x07
107 #define CS4271_DACVOL_ATAPI_AL_M 0x08
108 #define CS4271_DACVOL_ATAPI_AL_BR 0x09
109 #define CS4271_DACVOL_ATAPI_AL_BL 0x0A
110 #define CS4271_DACVOL_ATAPI_AL_BLR2 0x0B
111 #define CS4271_DACVOL_ATAPI_ALR2_M 0x0C
112 #define CS4271_DACVOL_ATAPI_ALR2_BR 0x0D
113 #define CS4271_DACVOL_ATAPI_ALR2_BL 0x0E
114 #define CS4271_DACVOL_ATAPI_ALR2_BLR2 0x0F
116 #define CS4271_VOLA_MUTE 0x80
117 #define CS4271_VOLA_VOL_MASK 0x7F
118 #define CS4271_VOLB_MUTE 0x80
119 #define CS4271_VOLB_VOL_MASK 0x7F
121 #define CS4271_ADCCTL_DITHER16 0x20
123 #define CS4271_ADCCTL_ADC_DIF_MASK 0x10
124 #define CS4271_ADCCTL_ADC_DIF_LJ 0x00
125 #define CS4271_ADCCTL_ADC_DIF_I2S 0x10
127 #define CS4271_ADCCTL_MUTEA 0x08
128 #define CS4271_ADCCTL_MUTEB 0x04
129 #define CS4271_ADCCTL_HPFDA 0x02
130 #define CS4271_ADCCTL_HPFDB 0x01
132 #define CS4271_MODE2_LOOP 0x10
133 #define CS4271_MODE2_MUTECAEQUB 0x08
134 #define CS4271_MODE2_FREEZE 0x04
135 #define CS4271_MODE2_CPEN 0x02
136 #define CS4271_MODE2_PDN 0x01
138 #define CS4271_CHIPID_PART_MASK 0xF0
139 #define CS4271_CHIPID_REV_MASK 0x0F
177 static int cs4271_set_dai_sysclk(
struct snd_soc_dai *codec_dai,
178 int clk_id,
unsigned int freq,
int dir)
187 static int cs4271_set_dai_fmt(
struct snd_soc_dai *codec_dai,
192 unsigned int val = 0;
235 static int cs4271_deemph[] = {0, 44100, 48000, 32000};
246 for (i = 2; i <
ARRAY_SIZE(cs4271_deemph); i++)
247 if (
abs(cs4271_deemph[i] - cs4271->
rate) <
248 abs(cs4271_deemph[val] - cs4271->
rate))
260 static int cs4271_get_deemph(
struct snd_kcontrol *kcontrol,
266 ucontrol->
value.enumerated.item[0] = cs4271->
deemph;
270 static int cs4271_put_deemph(
struct snd_kcontrol *kcontrol,
276 cs4271->
deemph = ucontrol->
value.enumerated.item[0];
277 return cs4271_set_deemph(codec);
317 #define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab)
331 if (cs4271->
rate < 50000)
333 else if (cs4271->
rate < 100000)
338 ratio = cs4271->
mclk / cs4271->
rate;
342 (cs4271_clk_tab[i].
ratio == ratio))
345 if (i == CS4171_NR_RATIOS) {
357 return cs4271_set_deemph(codec);
387 0, 0x7F, 1, cs4271_dac_tlv),
392 cs4271_get_deemph, cs4271_put_deemph),
407 .hw_params = cs4271_hw_params,
408 .set_sysclk = cs4271_set_dai_sysclk,
409 .set_fmt = cs4271_set_dai_fmt,
410 .digital_mute = cs4271_digital_mute,
414 .name =
"cs4271-hifi",
416 .stream_name =
"Playback",
423 .stream_name =
"Capture",
429 .ops = &cs4271_dai_ops,
430 .symmetric_rates = 1,
459 #define cs4271_soc_suspend NULL
460 #define cs4271_soc_resume NULL
480 gpio_nreset = of_get_named_gpio(codec->
dev->of_node,
484 if (cs4271plat && gpio_is_valid(cs4271plat->
gpio_nreset))
487 if (gpio_nreset >= 0)
490 if (gpio_nreset >= 0) {
516 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
547 .probe = cs4271_probe,
548 .remove = cs4271_remove,
551 .reg_cache_default = cs4271_dflt_reg,
552 .reg_cache_size =
ARRAY_SIZE(cs4271_dflt_reg),
553 .reg_word_size =
sizeof(cs4271_dflt_reg[0]),
557 #if defined(CONFIG_SPI_MASTER)
566 spi_set_drvdata(spi, cs4271);
579 static struct spi_driver cs4271_spi_driver = {
585 .probe = cs4271_spi_probe,
590 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
606 i2c_set_clientdata(client, cs4271);
619 static struct i2c_driver cs4271_i2c_driver = {
625 .id_table = cs4271_i2c_id,
626 .probe = cs4271_i2c_probe,
638 static int __init cs4271_modinit(
void)
642 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
643 ret = i2c_add_driver(&cs4271_i2c_driver);
645 pr_err(
"Failed to register CS4271 I2C driver: %d\n", ret);
650 #if defined(CONFIG_SPI_MASTER)
653 pr_err(
"Failed to register CS4271 SPI driver: %d\n", ret);
662 static void __exit cs4271_modexit(
void)
664 #if defined(CONFIG_SPI_MASTER)
665 spi_unregister_driver(&cs4271_spi_driver);
668 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)