blob: 1d03a1348162f5c3736ff680f0335e426930a4f3 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alexander Sverdlin67b22512011-01-19 21:22:06 +03002/*
3 * CS4271 ASoC codec driver
4 *
5 * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru>
6 *
Alexander Sverdlin67b22512011-01-19 21:22:06 +03007 * This driver support CS4271 codec being master or slave, working
8 * in control port mode, connected either via SPI or I2C.
9 * The data format accepted is I2S or left-justified.
10 * DAPM support not implemented.
11 */
12
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/delay.h>
Alexander Sverdlin67b22512011-01-19 21:22:06 +030016#include <linux/gpio.h>
Sachin Kamata7ea1b72013-10-11 17:23:56 +053017#include <linux/of.h>
Daniel Macka31ebc32012-09-28 01:36:44 +020018#include <linux/of_device.h>
19#include <linux/of_gpio.h>
Pascal Huerst9a397f42016-02-16 16:19:06 +010020#include <linux/regulator/consumer.h>
Daniel Macka31ebc32012-09-28 01:36:44 +020021#include <sound/pcm.h>
22#include <sound/soc.h>
23#include <sound/tlv.h>
Alexander Sverdlin67b22512011-01-19 21:22:06 +030024#include <sound/cs4271.h>
Axel Linc973b8a2014-10-06 23:09:47 +080025#include "cs4271.h"
Alexander Sverdlin67b22512011-01-19 21:22:06 +030026
27#define CS4271_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
28 SNDRV_PCM_FMTBIT_S24_LE | \
29 SNDRV_PCM_FMTBIT_S32_LE)
Alexander Sverdlin383f8462011-03-07 20:29:36 +030030#define CS4271_PCM_RATES SNDRV_PCM_RATE_8000_192000
Alexander Sverdlin67b22512011-01-19 21:22:06 +030031
32/*
33 * CS4271 registers
Alexander Sverdlin67b22512011-01-19 21:22:06 +030034 */
Daniel Mack1b1861e2013-03-07 23:53:12 +010035#define CS4271_MODE1 0x01 /* Mode Control 1 */
36#define CS4271_DACCTL 0x02 /* DAC Control */
37#define CS4271_DACVOL 0x03 /* DAC Volume & Mixing Control */
38#define CS4271_VOLA 0x04 /* DAC Channel A Volume Control */
39#define CS4271_VOLB 0x05 /* DAC Channel B Volume Control */
40#define CS4271_ADCCTL 0x06 /* ADC Control */
41#define CS4271_MODE2 0x07 /* Mode Control 2 */
42#define CS4271_CHIPID 0x08 /* Chip ID */
Alexander Sverdlin67b22512011-01-19 21:22:06 +030043
44#define CS4271_FIRSTREG CS4271_MODE1
45#define CS4271_LASTREG CS4271_MODE2
46#define CS4271_NR_REGS ((CS4271_LASTREG & 0xFF) + 1)
47
48/* Bit masks for the CS4271 registers */
49#define CS4271_MODE1_MODE_MASK 0xC0
50#define CS4271_MODE1_MODE_1X 0x00
51#define CS4271_MODE1_MODE_2X 0x80
52#define CS4271_MODE1_MODE_4X 0xC0
53
54#define CS4271_MODE1_DIV_MASK 0x30
55#define CS4271_MODE1_DIV_1 0x00
56#define CS4271_MODE1_DIV_15 0x10
57#define CS4271_MODE1_DIV_2 0x20
58#define CS4271_MODE1_DIV_3 0x30
59
60#define CS4271_MODE1_MASTER 0x08
61
62#define CS4271_MODE1_DAC_DIF_MASK 0x07
63#define CS4271_MODE1_DAC_DIF_LJ 0x00
64#define CS4271_MODE1_DAC_DIF_I2S 0x01
65#define CS4271_MODE1_DAC_DIF_RJ16 0x02
66#define CS4271_MODE1_DAC_DIF_RJ24 0x03
67#define CS4271_MODE1_DAC_DIF_RJ20 0x04
68#define CS4271_MODE1_DAC_DIF_RJ18 0x05
69
70#define CS4271_DACCTL_AMUTE 0x80
71#define CS4271_DACCTL_IF_SLOW 0x40
72
73#define CS4271_DACCTL_DEM_MASK 0x30
74#define CS4271_DACCTL_DEM_DIS 0x00
75#define CS4271_DACCTL_DEM_441 0x10
76#define CS4271_DACCTL_DEM_48 0x20
77#define CS4271_DACCTL_DEM_32 0x30
78
79#define CS4271_DACCTL_SVRU 0x08
80#define CS4271_DACCTL_SRD 0x04
81#define CS4271_DACCTL_INVA 0x02
82#define CS4271_DACCTL_INVB 0x01
83
84#define CS4271_DACVOL_BEQUA 0x40
85#define CS4271_DACVOL_SOFT 0x20
86#define CS4271_DACVOL_ZEROC 0x10
87
88#define CS4271_DACVOL_ATAPI_MASK 0x0F
89#define CS4271_DACVOL_ATAPI_M_M 0x00
90#define CS4271_DACVOL_ATAPI_M_BR 0x01
91#define CS4271_DACVOL_ATAPI_M_BL 0x02
92#define CS4271_DACVOL_ATAPI_M_BLR2 0x03
93#define CS4271_DACVOL_ATAPI_AR_M 0x04
94#define CS4271_DACVOL_ATAPI_AR_BR 0x05
95#define CS4271_DACVOL_ATAPI_AR_BL 0x06
96#define CS4271_DACVOL_ATAPI_AR_BLR2 0x07
97#define CS4271_DACVOL_ATAPI_AL_M 0x08
98#define CS4271_DACVOL_ATAPI_AL_BR 0x09
99#define CS4271_DACVOL_ATAPI_AL_BL 0x0A
100#define CS4271_DACVOL_ATAPI_AL_BLR2 0x0B
101#define CS4271_DACVOL_ATAPI_ALR2_M 0x0C
102#define CS4271_DACVOL_ATAPI_ALR2_BR 0x0D
103#define CS4271_DACVOL_ATAPI_ALR2_BL 0x0E
104#define CS4271_DACVOL_ATAPI_ALR2_BLR2 0x0F
105
106#define CS4271_VOLA_MUTE 0x80
107#define CS4271_VOLA_VOL_MASK 0x7F
108#define CS4271_VOLB_MUTE 0x80
109#define CS4271_VOLB_VOL_MASK 0x7F
110
111#define CS4271_ADCCTL_DITHER16 0x20
112
113#define CS4271_ADCCTL_ADC_DIF_MASK 0x10
114#define CS4271_ADCCTL_ADC_DIF_LJ 0x00
115#define CS4271_ADCCTL_ADC_DIF_I2S 0x10
116
117#define CS4271_ADCCTL_MUTEA 0x08
118#define CS4271_ADCCTL_MUTEB 0x04
119#define CS4271_ADCCTL_HPFDA 0x02
120#define CS4271_ADCCTL_HPFDB 0x01
121
122#define CS4271_MODE2_LOOP 0x10
123#define CS4271_MODE2_MUTECAEQUB 0x08
124#define CS4271_MODE2_FREEZE 0x04
125#define CS4271_MODE2_CPEN 0x02
126#define CS4271_MODE2_PDN 0x01
127
128#define CS4271_CHIPID_PART_MASK 0xF0
129#define CS4271_CHIPID_REV_MASK 0x0F
130
131/*
132 * Default CS4271 power-up configuration
133 * Array contains non-existing in hw register at address 0
134 * Array do not include Chip ID, as codec driver does not use
135 * registers read operations at all
136 */
Daniel Mack1b1861e2013-03-07 23:53:12 +0100137static const struct reg_default cs4271_reg_defaults[] = {
138 { CS4271_MODE1, 0, },
139 { CS4271_DACCTL, CS4271_DACCTL_AMUTE, },
140 { CS4271_DACVOL, CS4271_DACVOL_SOFT | CS4271_DACVOL_ATAPI_AL_BR, },
141 { CS4271_VOLA, 0, },
142 { CS4271_VOLB, 0, },
143 { CS4271_ADCCTL, 0, },
144 { CS4271_MODE2, 0, },
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300145};
146
Daniel Mack1b1861e2013-03-07 23:53:12 +0100147static bool cs4271_volatile_reg(struct device *dev, unsigned int reg)
148{
149 return reg == CS4271_CHIPID;
150}
151
Pascal Huerst9a397f42016-02-16 16:19:06 +0100152static const char * const supply_names[] = {
153 "vd", "vl", "va"
154};
155
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300156struct cs4271_private {
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300157 unsigned int mclk;
158 bool master;
159 bool deemph;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100160 struct regmap *regmap;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300161 /* Current sample rate for de-emphasis control */
162 int rate;
163 /* GPIO driving Reset pin, if any */
164 int gpio_nreset;
165 /* GPIO that disable serial bus, if any */
166 int gpio_disable;
Daniel Mackfd23fb92012-12-10 10:30:04 +0100167 /* enable soft reset workaround */
168 bool enable_soft_reset;
Pascal Huerst9a397f42016-02-16 16:19:06 +0100169 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300170};
171
Mark Brown2e7fb942013-08-11 13:15:10 +0100172static const struct snd_soc_dapm_widget cs4271_dapm_widgets[] = {
173SND_SOC_DAPM_INPUT("AINA"),
174SND_SOC_DAPM_INPUT("AINB"),
175
176SND_SOC_DAPM_OUTPUT("AOUTA+"),
177SND_SOC_DAPM_OUTPUT("AOUTA-"),
178SND_SOC_DAPM_OUTPUT("AOUTB+"),
179SND_SOC_DAPM_OUTPUT("AOUTB-"),
180};
181
182static const struct snd_soc_dapm_route cs4271_dapm_routes[] = {
183 { "Capture", NULL, "AINA" },
184 { "Capture", NULL, "AINB" },
185
186 { "AOUTA+", NULL, "Playback" },
187 { "AOUTA-", NULL, "Playback" },
188 { "AOUTB+", NULL, "Playback" },
189 { "AOUTB-", NULL, "Playback" },
190};
191
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300192/*
193 * @freq is the desired MCLK rate
194 * MCLK rate should (c) be the sample rate, multiplied by one of the
195 * ratios listed in cs4271_mclk_fs_ratios table
196 */
197static int cs4271_set_dai_sysclk(struct snd_soc_dai *codec_dai,
198 int clk_id, unsigned int freq, int dir)
199{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000200 struct snd_soc_component *component = codec_dai->component;
201 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300202
203 cs4271->mclk = freq;
204 return 0;
205}
206
207static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai,
208 unsigned int format)
209{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000210 struct snd_soc_component *component = codec_dai->component;
211 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300212 unsigned int val = 0;
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300213 int ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300214
215 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
216 case SND_SOC_DAIFMT_CBS_CFS:
Pierre-Louis Bossart3c17bcf2019-01-04 20:02:37 -0600217 cs4271->master = false;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300218 break;
219 case SND_SOC_DAIFMT_CBM_CFM:
Pierre-Louis Bossart3c17bcf2019-01-04 20:02:37 -0600220 cs4271->master = true;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300221 val |= CS4271_MODE1_MASTER;
222 break;
223 default:
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000224 dev_err(component->dev, "Invalid DAI format\n");
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300225 return -EINVAL;
226 }
227
228 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
229 case SND_SOC_DAIFMT_LEFT_J:
230 val |= CS4271_MODE1_DAC_DIF_LJ;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100231 ret = regmap_update_bits(cs4271->regmap, CS4271_ADCCTL,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300232 CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_LJ);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300233 if (ret < 0)
234 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300235 break;
236 case SND_SOC_DAIFMT_I2S:
237 val |= CS4271_MODE1_DAC_DIF_I2S;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100238 ret = regmap_update_bits(cs4271->regmap, CS4271_ADCCTL,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300239 CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_I2S);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300240 if (ret < 0)
241 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300242 break;
243 default:
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000244 dev_err(component->dev, "Invalid DAI format\n");
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300245 return -EINVAL;
246 }
247
Daniel Mack1b1861e2013-03-07 23:53:12 +0100248 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE1,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300249 CS4271_MODE1_DAC_DIF_MASK | CS4271_MODE1_MASTER, val);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300250 if (ret < 0)
251 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300252 return 0;
253}
254
255static int cs4271_deemph[] = {0, 44100, 48000, 32000};
256
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000257static int cs4271_set_deemph(struct snd_soc_component *component)
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300258{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000259 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300260 int i, ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300261 int val = CS4271_DACCTL_DEM_DIS;
262
263 if (cs4271->deemph) {
264 /* Find closest de-emphasis freq */
265 val = 1;
266 for (i = 2; i < ARRAY_SIZE(cs4271_deemph); i++)
267 if (abs(cs4271_deemph[i] - cs4271->rate) <
268 abs(cs4271_deemph[val] - cs4271->rate))
269 val = i;
270 val <<= 4;
271 }
272
Daniel Mack1b1861e2013-03-07 23:53:12 +0100273 ret = regmap_update_bits(cs4271->regmap, CS4271_DACCTL,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300274 CS4271_DACCTL_DEM_MASK, val);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300275 if (ret < 0)
276 return ret;
277 return 0;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300278}
279
280static int cs4271_get_deemph(struct snd_kcontrol *kcontrol,
281 struct snd_ctl_elem_value *ucontrol)
282{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000283 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
284 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300285
Takashi Iwaie8371aa2015-03-10 12:39:05 +0100286 ucontrol->value.integer.value[0] = cs4271->deemph;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300287 return 0;
288}
289
290static int cs4271_put_deemph(struct snd_kcontrol *kcontrol,
291 struct snd_ctl_elem_value *ucontrol)
292{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000293 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
294 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300295
Takashi Iwaie8371aa2015-03-10 12:39:05 +0100296 cs4271->deemph = ucontrol->value.integer.value[0];
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000297 return cs4271_set_deemph(component);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300298}
299
Alexander Sverdlin5c3a12e2011-03-07 20:29:45 +0300300struct cs4271_clk_cfg {
301 bool master; /* codec mode */
302 u8 speed_mode; /* codec speed mode: 1x, 2x, 4x */
303 unsigned short ratio; /* MCLK / sample rate */
304 u8 ratio_mask; /* ratio bit mask for Master mode */
305};
306
307static struct cs4271_clk_cfg cs4271_clk_tab[] = {
308 {1, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
309 {1, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_15},
310 {1, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_2},
311 {1, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_3},
312 {1, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
313 {1, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_15},
314 {1, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_2},
315 {1, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_3},
316 {1, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
317 {1, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_15},
318 {1, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_2},
319 {1, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_3},
320 {0, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
321 {0, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_1},
322 {0, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_1},
323 {0, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_2},
324 {0, CS4271_MODE1_MODE_1X, 1024, CS4271_MODE1_DIV_2},
325 {0, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
326 {0, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_1},
327 {0, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_1},
328 {0, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_2},
329 {0, CS4271_MODE1_MODE_2X, 512, CS4271_MODE1_DIV_2},
330 {0, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
331 {0, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_1},
332 {0, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_1},
333 {0, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_2},
334 {0, CS4271_MODE1_MODE_4X, 256, CS4271_MODE1_DIV_2},
335};
336
337#define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab)
338
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300339static int cs4271_hw_params(struct snd_pcm_substream *substream,
340 struct snd_pcm_hw_params *params,
341 struct snd_soc_dai *dai)
342{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000343 struct snd_soc_component *component = dai->component;
344 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300345 int i, ret;
346 unsigned int ratio, val;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300347
Daniel Mackfd23fb92012-12-10 10:30:04 +0100348 if (cs4271->enable_soft_reset) {
349 /*
350 * Put the codec in soft reset and back again in case it's not
351 * currently streaming data. This way of bringing the codec in
352 * sync to the current clocks is not explicitly documented in
353 * the data sheet, but it seems to work fine, and in contrast
354 * to a read hardware reset, we don't have to sync back all
355 * registers every time.
356 */
357
358 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
359 !dai->capture_active) ||
360 (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
361 !dai->playback_active)) {
Daniel Mack1b1861e2013-03-07 23:53:12 +0100362 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
363 CS4271_MODE2_PDN,
364 CS4271_MODE2_PDN);
Daniel Mackfd23fb92012-12-10 10:30:04 +0100365 if (ret < 0)
366 return ret;
367
Daniel Mack1b1861e2013-03-07 23:53:12 +0100368 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
369 CS4271_MODE2_PDN, 0);
Daniel Mackfd23fb92012-12-10 10:30:04 +0100370 if (ret < 0)
371 return ret;
372 }
373 }
374
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300375 cs4271->rate = params_rate(params);
Alexander Sverdlin5c3a12e2011-03-07 20:29:45 +0300376
377 /* Configure DAC */
378 if (cs4271->rate < 50000)
379 val = CS4271_MODE1_MODE_1X;
380 else if (cs4271->rate < 100000)
381 val = CS4271_MODE1_MODE_2X;
382 else
383 val = CS4271_MODE1_MODE_4X;
384
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300385 ratio = cs4271->mclk / cs4271->rate;
386 for (i = 0; i < CS4171_NR_RATIOS; i++)
Alexander Sverdlin5c3a12e2011-03-07 20:29:45 +0300387 if ((cs4271_clk_tab[i].master == cs4271->master) &&
388 (cs4271_clk_tab[i].speed_mode == val) &&
389 (cs4271_clk_tab[i].ratio == ratio))
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300390 break;
391
Alexander Sverdlin5c3a12e2011-03-07 20:29:45 +0300392 if (i == CS4171_NR_RATIOS) {
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000393 dev_err(component->dev, "Invalid sample rate\n");
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300394 return -EINVAL;
395 }
396
Alexander Sverdlin5c3a12e2011-03-07 20:29:45 +0300397 val |= cs4271_clk_tab[i].ratio_mask;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300398
Daniel Mack1b1861e2013-03-07 23:53:12 +0100399 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE1,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300400 CS4271_MODE1_MODE_MASK | CS4271_MODE1_DIV_MASK, val);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300401 if (ret < 0)
402 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300403
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000404 return cs4271_set_deemph(component);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300405}
406
Daniel Mackc24a34d2013-03-21 20:43:54 +0100407static int cs4271_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300408{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000409 struct snd_soc_component *component = dai->component;
410 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300411 int ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300412 int val_a = 0;
413 int val_b = 0;
414
Daniel Mackc24a34d2013-03-21 20:43:54 +0100415 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
416 return 0;
417
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300418 if (mute) {
419 val_a = CS4271_VOLA_MUTE;
420 val_b = CS4271_VOLB_MUTE;
421 }
422
Daniel Mack1b1861e2013-03-07 23:53:12 +0100423 ret = regmap_update_bits(cs4271->regmap, CS4271_VOLA,
424 CS4271_VOLA_MUTE, val_a);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300425 if (ret < 0)
426 return ret;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100427
428 ret = regmap_update_bits(cs4271->regmap, CS4271_VOLB,
429 CS4271_VOLB_MUTE, val_b);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300430 if (ret < 0)
431 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300432
433 return 0;
434}
435
436/* CS4271 controls */
437static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0);
438
439static const struct snd_kcontrol_new cs4271_snd_controls[] = {
440 SOC_DOUBLE_R_TLV("Master Playback Volume", CS4271_VOLA, CS4271_VOLB,
441 0, 0x7F, 1, cs4271_dac_tlv),
442 SOC_SINGLE("Digital Loopback Switch", CS4271_MODE2, 4, 1, 0),
443 SOC_SINGLE("Soft Ramp Switch", CS4271_DACVOL, 5, 1, 0),
444 SOC_SINGLE("Zero Cross Switch", CS4271_DACVOL, 4, 1, 0),
445 SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
446 cs4271_get_deemph, cs4271_put_deemph),
447 SOC_SINGLE("Auto-Mute Switch", CS4271_DACCTL, 7, 1, 0),
448 SOC_SINGLE("Slow Roll Off Filter Switch", CS4271_DACCTL, 6, 1, 0),
449 SOC_SINGLE("Soft Volume Ramp-Up Switch", CS4271_DACCTL, 3, 1, 0),
450 SOC_SINGLE("Soft Ramp-Down Switch", CS4271_DACCTL, 2, 1, 0),
451 SOC_SINGLE("Left Channel Inversion Switch", CS4271_DACCTL, 1, 1, 0),
452 SOC_SINGLE("Right Channel Inversion Switch", CS4271_DACCTL, 0, 1, 0),
453 SOC_DOUBLE("Master Capture Switch", CS4271_ADCCTL, 3, 2, 1, 1),
454 SOC_SINGLE("Dither 16-Bit Data Switch", CS4271_ADCCTL, 5, 1, 0),
455 SOC_DOUBLE("High Pass Filter Switch", CS4271_ADCCTL, 1, 0, 1, 1),
456 SOC_DOUBLE_R("Master Playback Switch", CS4271_VOLA, CS4271_VOLB,
457 7, 1, 1),
458};
459
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100460static const struct snd_soc_dai_ops cs4271_dai_ops = {
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300461 .hw_params = cs4271_hw_params,
462 .set_sysclk = cs4271_set_dai_sysclk,
463 .set_fmt = cs4271_set_dai_fmt,
Daniel Mackc24a34d2013-03-21 20:43:54 +0100464 .mute_stream = cs4271_mute_stream,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300465};
466
Mark Brown16af7d62011-01-26 11:35:28 +0000467static struct snd_soc_dai_driver cs4271_dai = {
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300468 .name = "cs4271-hifi",
469 .playback = {
470 .stream_name = "Playback",
471 .channels_min = 2,
472 .channels_max = 2,
Alexander Sverdlin383f8462011-03-07 20:29:36 +0300473 .rates = CS4271_PCM_RATES,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300474 .formats = CS4271_PCM_FORMATS,
475 },
476 .capture = {
477 .stream_name = "Capture",
478 .channels_min = 2,
479 .channels_max = 2,
Alexander Sverdlin383f8462011-03-07 20:29:36 +0300480 .rates = CS4271_PCM_RATES,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300481 .formats = CS4271_PCM_FORMATS,
482 },
483 .ops = &cs4271_dai_ops,
484 .symmetric_rates = 1,
485};
486
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000487static int cs4271_reset(struct snd_soc_component *component)
Pascal Huerst9a397f42016-02-16 16:19:06 +0100488{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000489 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Pascal Huerst9a397f42016-02-16 16:19:06 +0100490
491 if (gpio_is_valid(cs4271->gpio_nreset)) {
Alexander Sverdlin49b2e272017-04-29 12:19:33 +0200492 gpio_direction_output(cs4271->gpio_nreset, 0);
Pascal Huerst9a397f42016-02-16 16:19:06 +0100493 mdelay(1);
494 gpio_set_value(cs4271->gpio_nreset, 1);
495 mdelay(1);
496 }
497
498 return 0;
499}
500
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300501#ifdef CONFIG_PM
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000502static int cs4271_soc_suspend(struct snd_soc_component *component)
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300503{
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300504 int ret;
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000505 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Daniel Mack1b1861e2013-03-07 23:53:12 +0100506
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300507 /* Set power-down bit */
Daniel Mack1b1861e2013-03-07 23:53:12 +0100508 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
509 CS4271_MODE2_PDN, CS4271_MODE2_PDN);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300510 if (ret < 0)
511 return ret;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100512
Pascal Huerst9a397f42016-02-16 16:19:06 +0100513 regcache_mark_dirty(cs4271->regmap);
514 regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies);
515
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300516 return 0;
517}
518
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000519static int cs4271_soc_resume(struct snd_soc_component *component)
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300520{
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300521 int ret;
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000522 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Daniel Mack1b1861e2013-03-07 23:53:12 +0100523
Pascal Huerst9a397f42016-02-16 16:19:06 +0100524 ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies),
525 cs4271->supplies);
526 if (ret < 0) {
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000527 dev_err(component->dev, "Failed to enable regulators: %d\n", ret);
Pascal Huerst9a397f42016-02-16 16:19:06 +0100528 return ret;
529 }
530
531 /* Do a proper reset after power up */
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000532 cs4271_reset(component);
Pascal Huerst9a397f42016-02-16 16:19:06 +0100533
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300534 /* Restore codec state */
Daniel Mack1b1861e2013-03-07 23:53:12 +0100535 ret = regcache_sync(cs4271->regmap);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300536 if (ret < 0)
537 return ret;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100538
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300539 /* then disable the power-down bit */
Daniel Mack1b1861e2013-03-07 23:53:12 +0100540 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
541 CS4271_MODE2_PDN, 0);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300542 if (ret < 0)
543 return ret;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100544
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300545 return 0;
546}
547#else
548#define cs4271_soc_suspend NULL
549#define cs4271_soc_resume NULL
550#endif /* CONFIG_PM */
551
Daniel Macka31ebc32012-09-28 01:36:44 +0200552#ifdef CONFIG_OF
Axel Linc973b8a2014-10-06 23:09:47 +0800553const struct of_device_id cs4271_dt_ids[] = {
Daniel Macka31ebc32012-09-28 01:36:44 +0200554 { .compatible = "cirrus,cs4271", },
555 { }
556};
557MODULE_DEVICE_TABLE(of, cs4271_dt_ids);
Axel Linc973b8a2014-10-06 23:09:47 +0800558EXPORT_SYMBOL_GPL(cs4271_dt_ids);
Daniel Macka31ebc32012-09-28 01:36:44 +0200559#endif
560
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000561static int cs4271_component_probe(struct snd_soc_component *component)
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300562{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000563 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
564 struct cs4271_platform_data *cs4271plat = component->dev->platform_data;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300565 int ret;
Daniel Mack26047e22012-11-30 11:28:55 +0100566 bool amutec_eq_bmutec = false;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300567
Daniel Macka31ebc32012-09-28 01:36:44 +0200568#ifdef CONFIG_OF
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000569 if (of_match_device(cs4271_dt_ids, component->dev)) {
570 if (of_get_property(component->dev->of_node,
Daniel Mack293750f2012-10-04 14:03:23 +0200571 "cirrus,amutec-eq-bmutec", NULL))
Daniel Mack26047e22012-11-30 11:28:55 +0100572 amutec_eq_bmutec = true;
Daniel Mackfd23fb92012-12-10 10:30:04 +0100573
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000574 if (of_get_property(component->dev->of_node,
Daniel Mackfd23fb92012-12-10 10:30:04 +0100575 "cirrus,enable-soft-reset", NULL))
576 cs4271->enable_soft_reset = true;
Daniel Mack293750f2012-10-04 14:03:23 +0200577 }
Daniel Macka31ebc32012-09-28 01:36:44 +0200578#endif
579
Pascal Huerst9a397f42016-02-16 16:19:06 +0100580 ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies),
581 cs4271->supplies);
582 if (ret < 0) {
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000583 dev_err(component->dev, "Failed to enable regulators: %d\n", ret);
Pascal Huerst9a397f42016-02-16 16:19:06 +0100584 return ret;
585 }
586
Daniel Mack293750f2012-10-04 14:03:23 +0200587 if (cs4271plat) {
Daniel Mack293750f2012-10-04 14:03:23 +0200588 amutec_eq_bmutec = cs4271plat->amutec_eq_bmutec;
Daniel Mackfd23fb92012-12-10 10:30:04 +0100589 cs4271->enable_soft_reset = cs4271plat->enable_soft_reset;
Daniel Mack293750f2012-10-04 14:03:23 +0200590 }
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300591
Pascal Huerst9a397f42016-02-16 16:19:06 +0100592 /* Reset codec */
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000593 cs4271_reset(component);
Pascal Huerst9a397f42016-02-16 16:19:06 +0100594
595 ret = regcache_sync(cs4271->regmap);
596 if (ret < 0)
597 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300598
Daniel Mack1b1861e2013-03-07 23:53:12 +0100599 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
600 CS4271_MODE2_PDN | CS4271_MODE2_CPEN,
601 CS4271_MODE2_PDN | CS4271_MODE2_CPEN);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300602 if (ret < 0)
603 return ret;
Daniel Mack1b1861e2013-03-07 23:53:12 +0100604 ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
605 CS4271_MODE2_PDN, 0);
Alexander Sverdlin0d42e6e2011-01-21 22:22:07 +0300606 if (ret < 0)
607 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300608 /* Power-up sequence requires 85 uS */
609 udelay(85);
610
Daniel Mack293750f2012-10-04 14:03:23 +0200611 if (amutec_eq_bmutec)
Daniel Mack1b1861e2013-03-07 23:53:12 +0100612 regmap_update_bits(cs4271->regmap, CS4271_MODE2,
613 CS4271_MODE2_MUTECAEQUB,
614 CS4271_MODE2_MUTECAEQUB);
Daniel Mack293750f2012-10-04 14:03:23 +0200615
Mark Brownbad268f2013-08-11 13:12:13 +0100616 return 0;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300617}
618
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000619static void cs4271_component_remove(struct snd_soc_component *component)
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300620{
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000621 struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300622
Daniel Mack5574f772012-11-10 19:52:50 +0100623 if (gpio_is_valid(cs4271->gpio_nreset))
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300624 /* Set codec to the reset state */
Daniel Mack5574f772012-11-10 19:52:50 +0100625 gpio_set_value(cs4271->gpio_nreset, 0);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300626
Pascal Huerst9a397f42016-02-16 16:19:06 +0100627 regcache_mark_dirty(cs4271->regmap);
628 regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300629};
630
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000631static const struct snd_soc_component_driver soc_component_dev_cs4271 = {
632 .probe = cs4271_component_probe,
633 .remove = cs4271_component_remove,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300634 .suspend = cs4271_soc_suspend,
635 .resume = cs4271_soc_resume,
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000636 .controls = cs4271_snd_controls,
637 .num_controls = ARRAY_SIZE(cs4271_snd_controls),
638 .dapm_widgets = cs4271_dapm_widgets,
639 .num_dapm_widgets = ARRAY_SIZE(cs4271_dapm_widgets),
640 .dapm_routes = cs4271_dapm_routes,
641 .num_dapm_routes = ARRAY_SIZE(cs4271_dapm_routes),
642 .idle_bias_on = 1,
643 .use_pmdown_time = 1,
644 .endianness = 1,
645 .non_legacy_dai_naming = 1,
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300646};
647
Daniel Mackd6cf89e2014-02-19 14:05:54 +0100648static int cs4271_common_probe(struct device *dev,
649 struct cs4271_private **c)
650{
651 struct cs4271_platform_data *cs4271plat = dev->platform_data;
652 struct cs4271_private *cs4271;
Pascal Huerst9a397f42016-02-16 16:19:06 +0100653 int i, ret;
Daniel Mackd6cf89e2014-02-19 14:05:54 +0100654
655 cs4271 = devm_kzalloc(dev, sizeof(*cs4271), GFP_KERNEL);
656 if (!cs4271)
657 return -ENOMEM;
658
659 if (of_match_device(cs4271_dt_ids, dev))
660 cs4271->gpio_nreset =
661 of_get_named_gpio(dev->of_node, "reset-gpio", 0);
662
663 if (cs4271plat)
664 cs4271->gpio_nreset = cs4271plat->gpio_nreset;
665
666 if (gpio_is_valid(cs4271->gpio_nreset)) {
Daniel Mackd6cf89e2014-02-19 14:05:54 +0100667 ret = devm_gpio_request(dev, cs4271->gpio_nreset,
668 "CS4271 Reset");
669 if (ret < 0)
670 return ret;
671 }
672
Pascal Huerst9a397f42016-02-16 16:19:06 +0100673 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
674 cs4271->supplies[i].supply = supply_names[i];
675
676 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(cs4271->supplies),
677 cs4271->supplies);
678
679 if (ret < 0) {
680 dev_err(dev, "Failed to get regulators: %d\n", ret);
681 return ret;
682 }
683
Daniel Mackd6cf89e2014-02-19 14:05:54 +0100684 *c = cs4271;
685 return 0;
686}
687
Axel Linc973b8a2014-10-06 23:09:47 +0800688const struct regmap_config cs4271_regmap_config = {
Daniel Mack1b1861e2013-03-07 23:53:12 +0100689 .max_register = CS4271_LASTREG,
690
691 .reg_defaults = cs4271_reg_defaults,
692 .num_reg_defaults = ARRAY_SIZE(cs4271_reg_defaults),
693 .cache_type = REGCACHE_RBTREE,
694
695 .volatile_reg = cs4271_volatile_reg,
696};
Axel Linc973b8a2014-10-06 23:09:47 +0800697EXPORT_SYMBOL_GPL(cs4271_regmap_config);
Daniel Mack1b1861e2013-03-07 23:53:12 +0100698
Axel Linc973b8a2014-10-06 23:09:47 +0800699int cs4271_probe(struct device *dev, struct regmap *regmap)
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300700{
701 struct cs4271_private *cs4271;
Daniel Mackd6cf89e2014-02-19 14:05:54 +0100702 int ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300703
Axel Linc973b8a2014-10-06 23:09:47 +0800704 if (IS_ERR(regmap))
705 return PTR_ERR(regmap);
706
707 ret = cs4271_common_probe(dev, &cs4271);
Daniel Mackd6cf89e2014-02-19 14:05:54 +0100708 if (ret < 0)
709 return ret;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300710
Axel Linc973b8a2014-10-06 23:09:47 +0800711 dev_set_drvdata(dev, cs4271);
712 cs4271->regmap = regmap;
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300713
Kuninori Morimotocac308f2018-01-29 03:50:28 +0000714 return devm_snd_soc_register_component(dev, &soc_component_dev_cs4271,
715 &cs4271_dai, 1);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300716}
Axel Linc973b8a2014-10-06 23:09:47 +0800717EXPORT_SYMBOL_GPL(cs4271_probe);
Alexander Sverdlin67b22512011-01-19 21:22:06 +0300718
719MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>");
720MODULE_DESCRIPTION("Cirrus Logic CS4271 ALSA SoC Codec Driver");
721MODULE_LICENSE("GPL");