1 // SPDX-License-Identifier: GPL-2.0-only
3 * cs35l35.c -- CS35L35 ALSA SoC audio driver
5 * Copyright 2017 Cirrus Logic, Inc.
7 * Author: Brian Austin <brian.austin@cirrus.com>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/gpio/consumer.h>
21 #include <linux/regmap.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <sound/cs35l35.h>
30 #include <linux/completion.h>
33 #include "cirrus_legacy.h"
36 * Some fields take zero as a valid value so use a high bit flag that won't
37 * get written to the device to mark those.
39 #define CS35L35_VALID_PDATA 0x80000000
41 static const struct reg_default cs35l35_reg
[] = {
42 {CS35L35_PWRCTL1
, 0x01},
43 {CS35L35_PWRCTL2
, 0x11},
44 {CS35L35_PWRCTL3
, 0x00},
45 {CS35L35_CLK_CTL1
, 0x04},
46 {CS35L35_CLK_CTL2
, 0x12},
47 {CS35L35_CLK_CTL3
, 0xCF},
48 {CS35L35_SP_FMT_CTL1
, 0x20},
49 {CS35L35_SP_FMT_CTL2
, 0x00},
50 {CS35L35_SP_FMT_CTL3
, 0x02},
51 {CS35L35_MAG_COMP_CTL
, 0x00},
52 {CS35L35_AMP_INP_DRV_CTL
, 0x01},
53 {CS35L35_AMP_DIG_VOL_CTL
, 0x12},
54 {CS35L35_AMP_DIG_VOL
, 0x00},
55 {CS35L35_ADV_DIG_VOL
, 0x00},
56 {CS35L35_PROTECT_CTL
, 0x06},
57 {CS35L35_AMP_GAIN_AUD_CTL
, 0x13},
58 {CS35L35_AMP_GAIN_PDM_CTL
, 0x00},
59 {CS35L35_AMP_GAIN_ADV_CTL
, 0x00},
60 {CS35L35_GPI_CTL
, 0x00},
61 {CS35L35_BST_CVTR_V_CTL
, 0x00},
62 {CS35L35_BST_PEAK_I
, 0x07},
63 {CS35L35_BST_RAMP_CTL
, 0x85},
64 {CS35L35_BST_CONV_COEF_1
, 0x24},
65 {CS35L35_BST_CONV_COEF_2
, 0x24},
66 {CS35L35_BST_CONV_SLOPE_COMP
, 0x4E},
67 {CS35L35_BST_CONV_SW_FREQ
, 0x04},
68 {CS35L35_CLASS_H_CTL
, 0x0B},
69 {CS35L35_CLASS_H_HEADRM_CTL
, 0x0B},
70 {CS35L35_CLASS_H_RELEASE_RATE
, 0x08},
71 {CS35L35_CLASS_H_FET_DRIVE_CTL
, 0x41},
72 {CS35L35_CLASS_H_VP_CTL
, 0xC5},
73 {CS35L35_VPBR_CTL
, 0x0A},
74 {CS35L35_VPBR_VOL_CTL
, 0x90},
75 {CS35L35_VPBR_TIMING_CTL
, 0x6A},
76 {CS35L35_VPBR_MODE_VOL_CTL
, 0x00},
77 {CS35L35_SPKR_MON_CTL
, 0xC0},
78 {CS35L35_IMON_SCALE_CTL
, 0x30},
79 {CS35L35_AUDIN_RXLOC_CTL
, 0x00},
80 {CS35L35_ADVIN_RXLOC_CTL
, 0x80},
81 {CS35L35_VMON_TXLOC_CTL
, 0x00},
82 {CS35L35_IMON_TXLOC_CTL
, 0x80},
83 {CS35L35_VPMON_TXLOC_CTL
, 0x04},
84 {CS35L35_VBSTMON_TXLOC_CTL
, 0x84},
85 {CS35L35_VPBR_STATUS_TXLOC_CTL
, 0x04},
86 {CS35L35_ZERO_FILL_LOC_CTL
, 0x00},
87 {CS35L35_AUDIN_DEPTH_CTL
, 0x0F},
88 {CS35L35_SPKMON_DEPTH_CTL
, 0x0F},
89 {CS35L35_SUPMON_DEPTH_CTL
, 0x0F},
90 {CS35L35_ZEROFILL_DEPTH_CTL
, 0x00},
91 {CS35L35_MULT_DEV_SYNCH1
, 0x02},
92 {CS35L35_MULT_DEV_SYNCH2
, 0x80},
93 {CS35L35_PROT_RELEASE_CTL
, 0x00},
94 {CS35L35_DIAG_MODE_REG_LOCK
, 0x00},
95 {CS35L35_DIAG_MODE_CTL_1
, 0x40},
96 {CS35L35_DIAG_MODE_CTL_2
, 0x00},
97 {CS35L35_INT_MASK_1
, 0xFF},
98 {CS35L35_INT_MASK_2
, 0xFF},
99 {CS35L35_INT_MASK_3
, 0xFF},
100 {CS35L35_INT_MASK_4
, 0xFF},
104 static bool cs35l35_volatile_register(struct device
*dev
, unsigned int reg
)
107 case CS35L35_INT_STATUS_1
:
108 case CS35L35_INT_STATUS_2
:
109 case CS35L35_INT_STATUS_3
:
110 case CS35L35_INT_STATUS_4
:
111 case CS35L35_PLL_STATUS
:
112 case CS35L35_OTP_TRIM_STATUS
:
119 static bool cs35l35_readable_register(struct device
*dev
, unsigned int reg
)
122 case CS35L35_DEVID_AB
... CS35L35_PWRCTL3
:
123 case CS35L35_CLK_CTL1
... CS35L35_SP_FMT_CTL3
:
124 case CS35L35_MAG_COMP_CTL
... CS35L35_AMP_GAIN_AUD_CTL
:
125 case CS35L35_AMP_GAIN_PDM_CTL
... CS35L35_BST_PEAK_I
:
126 case CS35L35_BST_RAMP_CTL
... CS35L35_BST_CONV_SW_FREQ
:
127 case CS35L35_CLASS_H_CTL
... CS35L35_CLASS_H_VP_CTL
:
128 case CS35L35_CLASS_H_STATUS
:
129 case CS35L35_VPBR_CTL
... CS35L35_VPBR_MODE_VOL_CTL
:
130 case CS35L35_VPBR_ATTEN_STATUS
:
131 case CS35L35_SPKR_MON_CTL
:
132 case CS35L35_IMON_SCALE_CTL
... CS35L35_ZEROFILL_DEPTH_CTL
:
133 case CS35L35_MULT_DEV_SYNCH1
... CS35L35_PROT_RELEASE_CTL
:
134 case CS35L35_DIAG_MODE_REG_LOCK
... CS35L35_DIAG_MODE_CTL_2
:
135 case CS35L35_INT_MASK_1
... CS35L35_PLL_STATUS
:
136 case CS35L35_OTP_TRIM_STATUS
:
143 static bool cs35l35_precious_register(struct device
*dev
, unsigned int reg
)
146 case CS35L35_INT_STATUS_1
:
147 case CS35L35_INT_STATUS_2
:
148 case CS35L35_INT_STATUS_3
:
149 case CS35L35_INT_STATUS_4
:
150 case CS35L35_PLL_STATUS
:
151 case CS35L35_OTP_TRIM_STATUS
:
158 static void cs35l35_reset(struct cs35l35_private
*cs35l35
)
160 gpiod_set_value_cansleep(cs35l35
->reset_gpio
, 0);
161 usleep_range(2000, 2100);
162 gpiod_set_value_cansleep(cs35l35
->reset_gpio
, 1);
163 usleep_range(1000, 1100);
166 static int cs35l35_wait_for_pdn(struct cs35l35_private
*cs35l35
)
170 if (cs35l35
->pdata
.ext_bst
) {
171 usleep_range(5000, 5500);
175 reinit_completion(&cs35l35
->pdn_done
);
177 ret
= wait_for_completion_timeout(&cs35l35
->pdn_done
,
178 msecs_to_jiffies(100));
180 dev_err(cs35l35
->dev
, "PDN_DONE did not complete\n");
187 static int cs35l35_sdin_event(struct snd_soc_dapm_widget
*w
,
188 struct snd_kcontrol
*kcontrol
, int event
)
190 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
191 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
195 case SND_SOC_DAPM_PRE_PMU
:
196 regmap_update_bits(cs35l35
->regmap
, CS35L35_CLK_CTL1
,
197 CS35L35_MCLK_DIS_MASK
,
198 0 << CS35L35_MCLK_DIS_SHIFT
);
199 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL1
,
200 CS35L35_DISCHG_FILT_MASK
,
201 0 << CS35L35_DISCHG_FILT_SHIFT
);
202 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL1
,
203 CS35L35_PDN_ALL_MASK
, 0);
205 case SND_SOC_DAPM_POST_PMD
:
206 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL1
,
207 CS35L35_DISCHG_FILT_MASK
,
208 1 << CS35L35_DISCHG_FILT_SHIFT
);
209 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL1
,
210 CS35L35_PDN_ALL_MASK
, 1);
212 /* Already muted, so disable volume ramp for faster shutdown */
213 regmap_update_bits(cs35l35
->regmap
, CS35L35_AMP_DIG_VOL_CTL
,
214 CS35L35_AMP_DIGSFT_MASK
, 0);
216 ret
= cs35l35_wait_for_pdn(cs35l35
);
218 regmap_update_bits(cs35l35
->regmap
, CS35L35_CLK_CTL1
,
219 CS35L35_MCLK_DIS_MASK
,
220 1 << CS35L35_MCLK_DIS_SHIFT
);
222 regmap_update_bits(cs35l35
->regmap
, CS35L35_AMP_DIG_VOL_CTL
,
223 CS35L35_AMP_DIGSFT_MASK
,
224 1 << CS35L35_AMP_DIGSFT_SHIFT
);
227 dev_err(component
->dev
, "Invalid event = 0x%x\n", event
);
233 static int cs35l35_main_amp_event(struct snd_soc_dapm_widget
*w
,
234 struct snd_kcontrol
*kcontrol
, int event
)
236 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
237 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
242 case SND_SOC_DAPM_PRE_PMU
:
243 if (cs35l35
->pdata
.bst_pdn_fet_on
)
244 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
245 CS35L35_PDN_BST_MASK
,
246 0 << CS35L35_PDN_BST_FETON_SHIFT
);
248 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
249 CS35L35_PDN_BST_MASK
,
250 0 << CS35L35_PDN_BST_FETOFF_SHIFT
);
252 case SND_SOC_DAPM_POST_PMU
:
253 usleep_range(5000, 5100);
254 /* If in PDM mode we must use VP for Voltage control */
255 if (cs35l35
->pdm_mode
)
256 regmap_update_bits(cs35l35
->regmap
,
257 CS35L35_BST_CVTR_V_CTL
,
258 CS35L35_BST_CTL_MASK
,
259 0 << CS35L35_BST_CTL_SHIFT
);
261 regmap_update_bits(cs35l35
->regmap
, CS35L35_PROTECT_CTL
,
262 CS35L35_AMP_MUTE_MASK
, 0);
264 for (i
= 0; i
< 2; i
++)
265 regmap_bulk_read(cs35l35
->regmap
, CS35L35_INT_STATUS_1
,
266 ®
, ARRAY_SIZE(reg
));
269 case SND_SOC_DAPM_PRE_PMD
:
270 regmap_update_bits(cs35l35
->regmap
, CS35L35_PROTECT_CTL
,
271 CS35L35_AMP_MUTE_MASK
,
272 1 << CS35L35_AMP_MUTE_SHIFT
);
273 if (cs35l35
->pdata
.bst_pdn_fet_on
)
274 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
275 CS35L35_PDN_BST_MASK
,
276 1 << CS35L35_PDN_BST_FETON_SHIFT
);
278 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
279 CS35L35_PDN_BST_MASK
,
280 1 << CS35L35_PDN_BST_FETOFF_SHIFT
);
282 case SND_SOC_DAPM_POST_PMD
:
283 usleep_range(5000, 5100);
285 * If PDM mode we should switch back to pdata value
286 * for Voltage control when we go down
288 if (cs35l35
->pdm_mode
)
289 regmap_update_bits(cs35l35
->regmap
,
290 CS35L35_BST_CVTR_V_CTL
,
291 CS35L35_BST_CTL_MASK
,
292 cs35l35
->pdata
.bst_vctl
293 << CS35L35_BST_CTL_SHIFT
);
297 dev_err(component
->dev
, "Invalid event = 0x%x\n", event
);
302 static DECLARE_TLV_DB_SCALE(amp_gain_tlv
, 0, 1, 1);
303 static DECLARE_TLV_DB_SCALE(dig_vol_tlv
, -10200, 50, 0);
305 static const struct snd_kcontrol_new cs35l35_aud_controls
[] = {
306 SOC_SINGLE_SX_TLV("Digital Audio Volume", CS35L35_AMP_DIG_VOL
,
307 0, 0x34, 0xE4, dig_vol_tlv
),
308 SOC_SINGLE_TLV("Analog Audio Volume", CS35L35_AMP_GAIN_AUD_CTL
, 0, 19, 0,
310 SOC_SINGLE_TLV("PDM Volume", CS35L35_AMP_GAIN_PDM_CTL
, 0, 19, 0,
314 static const struct snd_kcontrol_new cs35l35_adv_controls
[] = {
315 SOC_SINGLE_SX_TLV("Digital Advisory Volume", CS35L35_ADV_DIG_VOL
,
316 0, 0x34, 0xE4, dig_vol_tlv
),
317 SOC_SINGLE_TLV("Analog Advisory Volume", CS35L35_AMP_GAIN_ADV_CTL
, 0, 19, 0,
321 static const struct snd_soc_dapm_widget cs35l35_dapm_widgets
[] = {
322 SND_SOC_DAPM_AIF_IN_E("SDIN", NULL
, 0, CS35L35_PWRCTL3
, 1, 1,
323 cs35l35_sdin_event
, SND_SOC_DAPM_PRE_PMU
|
324 SND_SOC_DAPM_POST_PMD
),
325 SND_SOC_DAPM_AIF_OUT("SDOUT", NULL
, 0, CS35L35_PWRCTL3
, 2, 1),
327 SND_SOC_DAPM_OUTPUT("SPK"),
329 SND_SOC_DAPM_INPUT("VP"),
330 SND_SOC_DAPM_INPUT("VBST"),
331 SND_SOC_DAPM_INPUT("ISENSE"),
332 SND_SOC_DAPM_INPUT("VSENSE"),
334 SND_SOC_DAPM_ADC("VMON ADC", NULL
, CS35L35_PWRCTL2
, 7, 1),
335 SND_SOC_DAPM_ADC("IMON ADC", NULL
, CS35L35_PWRCTL2
, 6, 1),
336 SND_SOC_DAPM_ADC("VPMON ADC", NULL
, CS35L35_PWRCTL3
, 3, 1),
337 SND_SOC_DAPM_ADC("VBSTMON ADC", NULL
, CS35L35_PWRCTL3
, 4, 1),
338 SND_SOC_DAPM_ADC("CLASS H", NULL
, CS35L35_PWRCTL2
, 5, 1),
340 SND_SOC_DAPM_OUT_DRV_E("Main AMP", CS35L35_PWRCTL2
, 0, 1, NULL
, 0,
341 cs35l35_main_amp_event
, SND_SOC_DAPM_PRE_PMU
|
342 SND_SOC_DAPM_POST_PMD
| SND_SOC_DAPM_POST_PMU
|
343 SND_SOC_DAPM_PRE_PMD
),
346 static const struct snd_soc_dapm_route cs35l35_audio_map
[] = {
347 {"VPMON ADC", NULL
, "VP"},
348 {"VBSTMON ADC", NULL
, "VBST"},
349 {"IMON ADC", NULL
, "ISENSE"},
350 {"VMON ADC", NULL
, "VSENSE"},
351 {"SDOUT", NULL
, "IMON ADC"},
352 {"SDOUT", NULL
, "VMON ADC"},
353 {"SDOUT", NULL
, "VBSTMON ADC"},
354 {"SDOUT", NULL
, "VPMON ADC"},
355 {"AMP Capture", NULL
, "SDOUT"},
357 {"SDIN", NULL
, "AMP Playback"},
358 {"CLASS H", NULL
, "SDIN"},
359 {"Main AMP", NULL
, "CLASS H"},
360 {"SPK", NULL
, "Main AMP"},
363 static int cs35l35_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
365 struct snd_soc_component
*component
= codec_dai
->component
;
366 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
368 switch (fmt
& SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK
) {
369 case SND_SOC_DAIFMT_CBP_CFP
:
370 regmap_update_bits(cs35l35
->regmap
, CS35L35_CLK_CTL1
,
371 CS35L35_MS_MASK
, 1 << CS35L35_MS_SHIFT
);
372 cs35l35
->clock_consumer
= false;
374 case SND_SOC_DAIFMT_CBC_CFC
:
375 regmap_update_bits(cs35l35
->regmap
, CS35L35_CLK_CTL1
,
376 CS35L35_MS_MASK
, 0 << CS35L35_MS_SHIFT
);
377 cs35l35
->clock_consumer
= true;
383 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
384 case SND_SOC_DAIFMT_I2S
:
385 cs35l35
->i2s_mode
= true;
386 cs35l35
->pdm_mode
= false;
388 case SND_SOC_DAIFMT_PDM
:
389 cs35l35
->pdm_mode
= true;
390 cs35l35
->i2s_mode
= false;
399 struct cs35l35_sysclk_config
{
405 static struct cs35l35_sysclk_config cs35l35_clk_ctl
[] = {
407 /* SYSCLK, Sample Rate, Serial Port Cfg */
408 {5644800, 44100, 0x00},
409 {5644800, 88200, 0x40},
410 {6144000, 48000, 0x10},
411 {6144000, 96000, 0x50},
412 {11289600, 44100, 0x01},
413 {11289600, 88200, 0x41},
414 {11289600, 176400, 0x81},
415 {12000000, 44100, 0x03},
416 {12000000, 48000, 0x13},
417 {12000000, 88200, 0x43},
418 {12000000, 96000, 0x53},
419 {12000000, 176400, 0x83},
420 {12000000, 192000, 0x93},
421 {12288000, 48000, 0x11},
422 {12288000, 96000, 0x51},
423 {12288000, 192000, 0x91},
424 {13000000, 44100, 0x07},
425 {13000000, 48000, 0x17},
426 {13000000, 88200, 0x47},
427 {13000000, 96000, 0x57},
428 {13000000, 176400, 0x87},
429 {13000000, 192000, 0x97},
430 {22579200, 44100, 0x02},
431 {22579200, 88200, 0x42},
432 {22579200, 176400, 0x82},
433 {24000000, 44100, 0x0B},
434 {24000000, 48000, 0x1B},
435 {24000000, 88200, 0x4B},
436 {24000000, 96000, 0x5B},
437 {24000000, 176400, 0x8B},
438 {24000000, 192000, 0x9B},
439 {24576000, 48000, 0x12},
440 {24576000, 96000, 0x52},
441 {24576000, 192000, 0x92},
442 {26000000, 44100, 0x0F},
443 {26000000, 48000, 0x1F},
444 {26000000, 88200, 0x4F},
445 {26000000, 96000, 0x5F},
446 {26000000, 176400, 0x8F},
447 {26000000, 192000, 0x9F},
450 static int cs35l35_get_clk_config(int sysclk
, int srate
)
454 for (i
= 0; i
< ARRAY_SIZE(cs35l35_clk_ctl
); i
++) {
455 if (cs35l35_clk_ctl
[i
].sysclk
== sysclk
&&
456 cs35l35_clk_ctl
[i
].srate
== srate
)
457 return cs35l35_clk_ctl
[i
].clk_cfg
;
462 static int cs35l35_hw_params(struct snd_pcm_substream
*substream
,
463 struct snd_pcm_hw_params
*params
,
464 struct snd_soc_dai
*dai
)
466 struct snd_soc_component
*component
= dai
->component
;
467 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
468 struct classh_cfg
*classh
= &cs35l35
->pdata
.classh_algo
;
469 int srate
= params_rate(params
);
475 int clk_ctl
= cs35l35_get_clk_config(cs35l35
->sysclk
, srate
);
478 dev_err(component
->dev
, "Invalid CLK:Rate %d:%d\n",
479 cs35l35
->sysclk
, srate
);
483 ret
= regmap_update_bits(cs35l35
->regmap
, CS35L35_CLK_CTL2
,
484 CS35L35_CLK_CTL2_MASK
, clk_ctl
);
486 dev_err(component
->dev
, "Failed to set port config %d\n", ret
);
492 * When configured for the weak-drive detection path (CH_WKFET_DIS = 0)
493 * the Class H algorithm does not enable weak-drive operation for
494 * nonzero values of CH_WKFET_DELAY if SP_RATE = 01 or 10
496 errata_chk
= (clk_ctl
& CS35L35_SP_RATE_MASK
) >> CS35L35_SP_RATE_SHIFT
;
498 if (classh
->classh_wk_fet_disable
== 0x00 &&
499 (errata_chk
== 0x01 || errata_chk
== 0x02)) {
500 ret
= regmap_update_bits(cs35l35
->regmap
,
501 CS35L35_CLASS_H_FET_DRIVE_CTL
,
502 CS35L35_CH_WKFET_DEL_MASK
,
503 0 << CS35L35_CH_WKFET_DEL_SHIFT
);
505 dev_err(component
->dev
, "Failed to set fet config %d\n",
512 * You can pull more Monitor data from the SDOUT pin than going to SDIN
513 * Just make sure your SCLK is fast enough to fill the frame
515 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
516 switch (params_width(params
)) {
518 audin_format
= CS35L35_SDIN_DEPTH_8
;
521 audin_format
= CS35L35_SDIN_DEPTH_16
;
524 audin_format
= CS35L35_SDIN_DEPTH_24
;
527 dev_err(component
->dev
, "Unsupported Width %d\n",
528 params_width(params
));
531 regmap_update_bits(cs35l35
->regmap
,
532 CS35L35_AUDIN_DEPTH_CTL
,
533 CS35L35_AUDIN_DEPTH_MASK
,
535 CS35L35_AUDIN_DEPTH_SHIFT
);
536 if (cs35l35
->pdata
.stereo
) {
537 regmap_update_bits(cs35l35
->regmap
,
538 CS35L35_AUDIN_DEPTH_CTL
,
539 CS35L35_ADVIN_DEPTH_MASK
,
541 CS35L35_ADVIN_DEPTH_SHIFT
);
545 if (cs35l35
->i2s_mode
) {
546 /* We have to take the SCLK to derive num sclks
547 * to configure the CLOCK_CTL3 register correctly
549 if ((cs35l35
->sclk
/ srate
) % 4) {
550 dev_err(component
->dev
, "Unsupported sclk/fs ratio %d:%d\n",
551 cs35l35
->sclk
, srate
);
554 sp_sclks
= ((cs35l35
->sclk
/ srate
) / 4) - 1;
556 /* Only certain ratios supported when device is a clock consumer */
557 if (cs35l35
->clock_consumer
) {
559 case CS35L35_SP_SCLKS_32FS
:
560 case CS35L35_SP_SCLKS_48FS
:
561 case CS35L35_SP_SCLKS_64FS
:
564 dev_err(component
->dev
, "ratio not supported\n");
568 /* Only certain ratios supported when device is a clock provider */
570 case CS35L35_SP_SCLKS_32FS
:
571 case CS35L35_SP_SCLKS_64FS
:
574 dev_err(component
->dev
, "ratio not supported\n");
578 ret
= regmap_update_bits(cs35l35
->regmap
,
580 CS35L35_SP_SCLKS_MASK
, sp_sclks
<<
581 CS35L35_SP_SCLKS_SHIFT
);
583 dev_err(component
->dev
, "Failed to set fsclk %d\n", ret
);
591 static const unsigned int cs35l35_src_rates
[] = {
592 44100, 48000, 88200, 96000, 176400, 192000
595 static const struct snd_pcm_hw_constraint_list cs35l35_constraints
= {
596 .count
= ARRAY_SIZE(cs35l35_src_rates
),
597 .list
= cs35l35_src_rates
,
600 static int cs35l35_pcm_startup(struct snd_pcm_substream
*substream
,
601 struct snd_soc_dai
*dai
)
603 struct snd_soc_component
*component
= dai
->component
;
604 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
606 if (!substream
->runtime
)
609 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
610 SNDRV_PCM_HW_PARAM_RATE
, &cs35l35_constraints
);
612 regmap_update_bits(cs35l35
->regmap
, CS35L35_AMP_INP_DRV_CTL
,
613 CS35L35_PDM_MODE_MASK
,
614 0 << CS35L35_PDM_MODE_SHIFT
);
619 static const unsigned int cs35l35_pdm_rates
[] = {
620 44100, 48000, 88200, 96000
623 static const struct snd_pcm_hw_constraint_list cs35l35_pdm_constraints
= {
624 .count
= ARRAY_SIZE(cs35l35_pdm_rates
),
625 .list
= cs35l35_pdm_rates
,
628 static int cs35l35_pdm_startup(struct snd_pcm_substream
*substream
,
629 struct snd_soc_dai
*dai
)
631 struct snd_soc_component
*component
= dai
->component
;
632 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
634 if (!substream
->runtime
)
637 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
638 SNDRV_PCM_HW_PARAM_RATE
,
639 &cs35l35_pdm_constraints
);
641 regmap_update_bits(cs35l35
->regmap
, CS35L35_AMP_INP_DRV_CTL
,
642 CS35L35_PDM_MODE_MASK
,
643 1 << CS35L35_PDM_MODE_SHIFT
);
648 static int cs35l35_dai_set_sysclk(struct snd_soc_dai
*dai
,
649 int clk_id
, unsigned int freq
, int dir
)
651 struct snd_soc_component
*component
= dai
->component
;
652 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
654 /* Need the SCLK Frequency regardless of sysclk source for I2S */
655 cs35l35
->sclk
= freq
;
660 static const struct snd_soc_dai_ops cs35l35_ops
= {
661 .startup
= cs35l35_pcm_startup
,
662 .set_fmt
= cs35l35_set_dai_fmt
,
663 .hw_params
= cs35l35_hw_params
,
664 .set_sysclk
= cs35l35_dai_set_sysclk
,
667 static const struct snd_soc_dai_ops cs35l35_pdm_ops
= {
668 .startup
= cs35l35_pdm_startup
,
669 .set_fmt
= cs35l35_set_dai_fmt
,
670 .hw_params
= cs35l35_hw_params
,
673 static struct snd_soc_dai_driver cs35l35_dai
[] = {
675 .name
= "cs35l35-pcm",
678 .stream_name
= "AMP Playback",
681 .rates
= SNDRV_PCM_RATE_KNOT
,
682 .formats
= CS35L35_FORMATS
,
685 .stream_name
= "AMP Capture",
688 .rates
= SNDRV_PCM_RATE_KNOT
,
689 .formats
= CS35L35_FORMATS
,
695 .name
= "cs35l35-pdm",
698 .stream_name
= "PDM Playback",
701 .rates
= SNDRV_PCM_RATE_KNOT
,
702 .formats
= CS35L35_FORMATS
,
704 .ops
= &cs35l35_pdm_ops
,
708 static int cs35l35_component_set_sysclk(struct snd_soc_component
*component
,
709 int clk_id
, int source
, unsigned int freq
,
712 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
718 clksrc
= CS35L35_CLK_SOURCE_MCLK
;
721 clksrc
= CS35L35_CLK_SOURCE_SCLK
;
724 clksrc
= CS35L35_CLK_SOURCE_PDM
;
727 dev_err(component
->dev
, "Invalid CLK Source\n");
742 cs35l35
->sysclk
= freq
;
745 dev_err(component
->dev
, "Invalid CLK Frequency Input : %d\n", freq
);
749 ret
= regmap_update_bits(cs35l35
->regmap
, CS35L35_CLK_CTL1
,
750 CS35L35_CLK_SOURCE_MASK
,
751 clksrc
<< CS35L35_CLK_SOURCE_SHIFT
);
753 dev_err(component
->dev
, "Failed to set sysclk %d\n", ret
);
760 static int cs35l35_boost_inductor(struct cs35l35_private
*cs35l35
,
763 struct regmap
*regmap
= cs35l35
->regmap
;
764 unsigned int bst_ipk
= 0;
767 * Digital Boost Converter Configuration for feedback,
768 * ramping, switching frequency, and estimation block seeding.
771 regmap_update_bits(regmap
, CS35L35_BST_CONV_SW_FREQ
,
772 CS35L35_BST_CONV_SWFREQ_MASK
, 0x00);
774 regmap_read(regmap
, CS35L35_BST_PEAK_I
, &bst_ipk
);
775 bst_ipk
&= CS35L35_BST_IPK_MASK
;
778 case 1000: /* 1 uH */
779 regmap_write(regmap
, CS35L35_BST_CONV_COEF_1
, 0x24);
780 regmap_write(regmap
, CS35L35_BST_CONV_COEF_2
, 0x24);
781 regmap_update_bits(regmap
, CS35L35_BST_CONV_SW_FREQ
,
782 CS35L35_BST_CONV_LBST_MASK
, 0x00);
785 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x1B);
787 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x4E);
789 case 1200: /* 1.2 uH */
790 regmap_write(regmap
, CS35L35_BST_CONV_COEF_1
, 0x20);
791 regmap_write(regmap
, CS35L35_BST_CONV_COEF_2
, 0x20);
792 regmap_update_bits(regmap
, CS35L35_BST_CONV_SW_FREQ
,
793 CS35L35_BST_CONV_LBST_MASK
, 0x01);
796 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x1B);
798 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x47);
800 case 1500: /* 1.5uH */
801 regmap_write(regmap
, CS35L35_BST_CONV_COEF_1
, 0x20);
802 regmap_write(regmap
, CS35L35_BST_CONV_COEF_2
, 0x20);
803 regmap_update_bits(regmap
, CS35L35_BST_CONV_SW_FREQ
,
804 CS35L35_BST_CONV_LBST_MASK
, 0x02);
807 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x1B);
809 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x3C);
811 case 2200: /* 2.2uH */
812 regmap_write(regmap
, CS35L35_BST_CONV_COEF_1
, 0x19);
813 regmap_write(regmap
, CS35L35_BST_CONV_COEF_2
, 0x25);
814 regmap_update_bits(regmap
, CS35L35_BST_CONV_SW_FREQ
,
815 CS35L35_BST_CONV_LBST_MASK
, 0x03);
818 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x1B);
820 regmap_write(regmap
, CS35L35_BST_CONV_SLOPE_COMP
, 0x23);
823 dev_err(cs35l35
->dev
, "Invalid Inductor Value %d uH\n",
830 static int cs35l35_component_probe(struct snd_soc_component
*component
)
832 struct cs35l35_private
*cs35l35
= snd_soc_component_get_drvdata(component
);
833 struct classh_cfg
*classh
= &cs35l35
->pdata
.classh_algo
;
834 struct monitor_cfg
*monitor_config
= &cs35l35
->pdata
.mon_cfg
;
837 /* Set Platform Data */
838 if (cs35l35
->pdata
.bst_vctl
)
839 regmap_update_bits(cs35l35
->regmap
, CS35L35_BST_CVTR_V_CTL
,
840 CS35L35_BST_CTL_MASK
,
841 cs35l35
->pdata
.bst_vctl
);
843 if (cs35l35
->pdata
.bst_ipk
)
844 regmap_update_bits(cs35l35
->regmap
, CS35L35_BST_PEAK_I
,
845 CS35L35_BST_IPK_MASK
,
846 cs35l35
->pdata
.bst_ipk
<<
847 CS35L35_BST_IPK_SHIFT
);
849 ret
= cs35l35_boost_inductor(cs35l35
, cs35l35
->pdata
.boost_ind
);
853 if (cs35l35
->pdata
.gain_zc
)
854 regmap_update_bits(cs35l35
->regmap
, CS35L35_PROTECT_CTL
,
855 CS35L35_AMP_GAIN_ZC_MASK
,
856 cs35l35
->pdata
.gain_zc
<<
857 CS35L35_AMP_GAIN_ZC_SHIFT
);
859 if (cs35l35
->pdata
.aud_channel
)
860 regmap_update_bits(cs35l35
->regmap
,
861 CS35L35_AUDIN_RXLOC_CTL
,
862 CS35L35_AUD_IN_LR_MASK
,
863 cs35l35
->pdata
.aud_channel
<<
864 CS35L35_AUD_IN_LR_SHIFT
);
866 if (cs35l35
->pdata
.stereo
) {
867 regmap_update_bits(cs35l35
->regmap
,
868 CS35L35_ADVIN_RXLOC_CTL
,
869 CS35L35_ADV_IN_LR_MASK
,
870 cs35l35
->pdata
.adv_channel
<<
871 CS35L35_ADV_IN_LR_SHIFT
);
872 if (cs35l35
->pdata
.shared_bst
)
873 regmap_update_bits(cs35l35
->regmap
, CS35L35_CLASS_H_CTL
,
874 CS35L35_CH_STEREO_MASK
,
875 1 << CS35L35_CH_STEREO_SHIFT
);
876 ret
= snd_soc_add_component_controls(component
, cs35l35_adv_controls
,
877 ARRAY_SIZE(cs35l35_adv_controls
));
882 if (cs35l35
->pdata
.sp_drv_str
)
883 regmap_update_bits(cs35l35
->regmap
, CS35L35_CLK_CTL1
,
885 cs35l35
->pdata
.sp_drv_str
<<
886 CS35L35_SP_DRV_SHIFT
);
887 if (cs35l35
->pdata
.sp_drv_unused
)
888 regmap_update_bits(cs35l35
->regmap
, CS35L35_SP_FMT_CTL3
,
889 CS35L35_SP_I2S_DRV_MASK
,
890 cs35l35
->pdata
.sp_drv_unused
<<
891 CS35L35_SP_I2S_DRV_SHIFT
);
893 if (classh
->classh_algo_enable
) {
894 if (classh
->classh_bst_override
)
895 regmap_update_bits(cs35l35
->regmap
,
897 CS35L35_CH_BST_OVR_MASK
,
898 classh
->classh_bst_override
<<
899 CS35L35_CH_BST_OVR_SHIFT
);
900 if (classh
->classh_bst_max_limit
)
901 regmap_update_bits(cs35l35
->regmap
,
903 CS35L35_CH_BST_LIM_MASK
,
904 classh
->classh_bst_max_limit
<<
905 CS35L35_CH_BST_LIM_SHIFT
);
906 if (classh
->classh_mem_depth
)
907 regmap_update_bits(cs35l35
->regmap
,
909 CS35L35_CH_MEM_DEPTH_MASK
,
910 classh
->classh_mem_depth
<<
911 CS35L35_CH_MEM_DEPTH_SHIFT
);
912 if (classh
->classh_headroom
)
913 regmap_update_bits(cs35l35
->regmap
,
914 CS35L35_CLASS_H_HEADRM_CTL
,
915 CS35L35_CH_HDRM_CTL_MASK
,
916 classh
->classh_headroom
<<
917 CS35L35_CH_HDRM_CTL_SHIFT
);
918 if (classh
->classh_release_rate
)
919 regmap_update_bits(cs35l35
->regmap
,
920 CS35L35_CLASS_H_RELEASE_RATE
,
921 CS35L35_CH_REL_RATE_MASK
,
922 classh
->classh_release_rate
<<
923 CS35L35_CH_REL_RATE_SHIFT
);
924 if (classh
->classh_wk_fet_disable
)
925 regmap_update_bits(cs35l35
->regmap
,
926 CS35L35_CLASS_H_FET_DRIVE_CTL
,
927 CS35L35_CH_WKFET_DIS_MASK
,
928 classh
->classh_wk_fet_disable
<<
929 CS35L35_CH_WKFET_DIS_SHIFT
);
930 if (classh
->classh_wk_fet_delay
)
931 regmap_update_bits(cs35l35
->regmap
,
932 CS35L35_CLASS_H_FET_DRIVE_CTL
,
933 CS35L35_CH_WKFET_DEL_MASK
,
934 classh
->classh_wk_fet_delay
<<
935 CS35L35_CH_WKFET_DEL_SHIFT
);
936 if (classh
->classh_wk_fet_thld
)
937 regmap_update_bits(cs35l35
->regmap
,
938 CS35L35_CLASS_H_FET_DRIVE_CTL
,
939 CS35L35_CH_WKFET_THLD_MASK
,
940 classh
->classh_wk_fet_thld
<<
941 CS35L35_CH_WKFET_THLD_SHIFT
);
942 if (classh
->classh_vpch_auto
)
943 regmap_update_bits(cs35l35
->regmap
,
944 CS35L35_CLASS_H_VP_CTL
,
945 CS35L35_CH_VP_AUTO_MASK
,
946 classh
->classh_vpch_auto
<<
947 CS35L35_CH_VP_AUTO_SHIFT
);
948 if (classh
->classh_vpch_rate
)
949 regmap_update_bits(cs35l35
->regmap
,
950 CS35L35_CLASS_H_VP_CTL
,
951 CS35L35_CH_VP_RATE_MASK
,
952 classh
->classh_vpch_rate
<<
953 CS35L35_CH_VP_RATE_SHIFT
);
954 if (classh
->classh_vpch_man
)
955 regmap_update_bits(cs35l35
->regmap
,
956 CS35L35_CLASS_H_VP_CTL
,
957 CS35L35_CH_VP_MAN_MASK
,
958 classh
->classh_vpch_man
<<
959 CS35L35_CH_VP_MAN_SHIFT
);
962 if (monitor_config
->is_present
) {
963 if (monitor_config
->vmon_specs
) {
964 regmap_update_bits(cs35l35
->regmap
,
965 CS35L35_SPKMON_DEPTH_CTL
,
966 CS35L35_VMON_DEPTH_MASK
,
967 monitor_config
->vmon_dpth
<<
968 CS35L35_VMON_DEPTH_SHIFT
);
969 regmap_update_bits(cs35l35
->regmap
,
970 CS35L35_VMON_TXLOC_CTL
,
971 CS35L35_MON_TXLOC_MASK
,
972 monitor_config
->vmon_loc
<<
973 CS35L35_MON_TXLOC_SHIFT
);
974 regmap_update_bits(cs35l35
->regmap
,
975 CS35L35_VMON_TXLOC_CTL
,
976 CS35L35_MON_FRM_MASK
,
977 monitor_config
->vmon_frm
<<
978 CS35L35_MON_FRM_SHIFT
);
980 if (monitor_config
->imon_specs
) {
981 regmap_update_bits(cs35l35
->regmap
,
982 CS35L35_SPKMON_DEPTH_CTL
,
983 CS35L35_IMON_DEPTH_MASK
,
984 monitor_config
->imon_dpth
<<
985 CS35L35_IMON_DEPTH_SHIFT
);
986 regmap_update_bits(cs35l35
->regmap
,
987 CS35L35_IMON_TXLOC_CTL
,
988 CS35L35_MON_TXLOC_MASK
,
989 monitor_config
->imon_loc
<<
990 CS35L35_MON_TXLOC_SHIFT
);
991 regmap_update_bits(cs35l35
->regmap
,
992 CS35L35_IMON_TXLOC_CTL
,
993 CS35L35_MON_FRM_MASK
,
994 monitor_config
->imon_frm
<<
995 CS35L35_MON_FRM_SHIFT
);
996 regmap_update_bits(cs35l35
->regmap
,
997 CS35L35_IMON_SCALE_CTL
,
998 CS35L35_IMON_SCALE_MASK
,
999 monitor_config
->imon_scale
<<
1000 CS35L35_IMON_SCALE_SHIFT
);
1002 if (monitor_config
->vpmon_specs
) {
1003 regmap_update_bits(cs35l35
->regmap
,
1004 CS35L35_SUPMON_DEPTH_CTL
,
1005 CS35L35_VPMON_DEPTH_MASK
,
1006 monitor_config
->vpmon_dpth
<<
1007 CS35L35_VPMON_DEPTH_SHIFT
);
1008 regmap_update_bits(cs35l35
->regmap
,
1009 CS35L35_VPMON_TXLOC_CTL
,
1010 CS35L35_MON_TXLOC_MASK
,
1011 monitor_config
->vpmon_loc
<<
1012 CS35L35_MON_TXLOC_SHIFT
);
1013 regmap_update_bits(cs35l35
->regmap
,
1014 CS35L35_VPMON_TXLOC_CTL
,
1015 CS35L35_MON_FRM_MASK
,
1016 monitor_config
->vpmon_frm
<<
1017 CS35L35_MON_FRM_SHIFT
);
1019 if (monitor_config
->vbstmon_specs
) {
1020 regmap_update_bits(cs35l35
->regmap
,
1021 CS35L35_SUPMON_DEPTH_CTL
,
1022 CS35L35_VBSTMON_DEPTH_MASK
,
1023 monitor_config
->vpmon_dpth
<<
1024 CS35L35_VBSTMON_DEPTH_SHIFT
);
1025 regmap_update_bits(cs35l35
->regmap
,
1026 CS35L35_VBSTMON_TXLOC_CTL
,
1027 CS35L35_MON_TXLOC_MASK
,
1028 monitor_config
->vbstmon_loc
<<
1029 CS35L35_MON_TXLOC_SHIFT
);
1030 regmap_update_bits(cs35l35
->regmap
,
1031 CS35L35_VBSTMON_TXLOC_CTL
,
1032 CS35L35_MON_FRM_MASK
,
1033 monitor_config
->vbstmon_frm
<<
1034 CS35L35_MON_FRM_SHIFT
);
1036 if (monitor_config
->vpbrstat_specs
) {
1037 regmap_update_bits(cs35l35
->regmap
,
1038 CS35L35_SUPMON_DEPTH_CTL
,
1039 CS35L35_VPBRSTAT_DEPTH_MASK
,
1040 monitor_config
->vpbrstat_dpth
<<
1041 CS35L35_VPBRSTAT_DEPTH_SHIFT
);
1042 regmap_update_bits(cs35l35
->regmap
,
1043 CS35L35_VPBR_STATUS_TXLOC_CTL
,
1044 CS35L35_MON_TXLOC_MASK
,
1045 monitor_config
->vpbrstat_loc
<<
1046 CS35L35_MON_TXLOC_SHIFT
);
1047 regmap_update_bits(cs35l35
->regmap
,
1048 CS35L35_VPBR_STATUS_TXLOC_CTL
,
1049 CS35L35_MON_FRM_MASK
,
1050 monitor_config
->vpbrstat_frm
<<
1051 CS35L35_MON_FRM_SHIFT
);
1053 if (monitor_config
->zerofill_specs
) {
1054 regmap_update_bits(cs35l35
->regmap
,
1055 CS35L35_SUPMON_DEPTH_CTL
,
1056 CS35L35_ZEROFILL_DEPTH_MASK
,
1057 monitor_config
->zerofill_dpth
<<
1058 CS35L35_ZEROFILL_DEPTH_SHIFT
);
1059 regmap_update_bits(cs35l35
->regmap
,
1060 CS35L35_ZERO_FILL_LOC_CTL
,
1061 CS35L35_MON_TXLOC_MASK
,
1062 monitor_config
->zerofill_loc
<<
1063 CS35L35_MON_TXLOC_SHIFT
);
1064 regmap_update_bits(cs35l35
->regmap
,
1065 CS35L35_ZERO_FILL_LOC_CTL
,
1066 CS35L35_MON_FRM_MASK
,
1067 monitor_config
->zerofill_frm
<<
1068 CS35L35_MON_FRM_SHIFT
);
1075 static const struct snd_soc_component_driver soc_component_dev_cs35l35
= {
1076 .probe
= cs35l35_component_probe
,
1077 .set_sysclk
= cs35l35_component_set_sysclk
,
1078 .dapm_widgets
= cs35l35_dapm_widgets
,
1079 .num_dapm_widgets
= ARRAY_SIZE(cs35l35_dapm_widgets
),
1080 .dapm_routes
= cs35l35_audio_map
,
1081 .num_dapm_routes
= ARRAY_SIZE(cs35l35_audio_map
),
1082 .controls
= cs35l35_aud_controls
,
1083 .num_controls
= ARRAY_SIZE(cs35l35_aud_controls
),
1085 .use_pmdown_time
= 1,
1089 static const struct regmap_config cs35l35_regmap
= {
1093 .max_register
= CS35L35_MAX_REGISTER
,
1094 .reg_defaults
= cs35l35_reg
,
1095 .num_reg_defaults
= ARRAY_SIZE(cs35l35_reg
),
1096 .volatile_reg
= cs35l35_volatile_register
,
1097 .readable_reg
= cs35l35_readable_register
,
1098 .precious_reg
= cs35l35_precious_register
,
1099 .cache_type
= REGCACHE_MAPLE
,
1100 .use_single_read
= true,
1101 .use_single_write
= true,
1104 static irqreturn_t
cs35l35_irq(int irq
, void *data
)
1106 struct cs35l35_private
*cs35l35
= data
;
1107 unsigned int sticky1
, sticky2
, sticky3
, sticky4
;
1108 unsigned int mask1
, mask2
, mask3
, mask4
, current1
;
1110 /* ack the irq by reading all status registers */
1111 regmap_read(cs35l35
->regmap
, CS35L35_INT_STATUS_4
, &sticky4
);
1112 regmap_read(cs35l35
->regmap
, CS35L35_INT_STATUS_3
, &sticky3
);
1113 regmap_read(cs35l35
->regmap
, CS35L35_INT_STATUS_2
, &sticky2
);
1114 regmap_read(cs35l35
->regmap
, CS35L35_INT_STATUS_1
, &sticky1
);
1116 regmap_read(cs35l35
->regmap
, CS35L35_INT_MASK_4
, &mask4
);
1117 regmap_read(cs35l35
->regmap
, CS35L35_INT_MASK_3
, &mask3
);
1118 regmap_read(cs35l35
->regmap
, CS35L35_INT_MASK_2
, &mask2
);
1119 regmap_read(cs35l35
->regmap
, CS35L35_INT_MASK_1
, &mask1
);
1121 /* Check to see if unmasked bits are active */
1122 if (!(sticky1
& ~mask1
) && !(sticky2
& ~mask2
) && !(sticky3
& ~mask3
)
1123 && !(sticky4
& ~mask4
))
1126 if (sticky2
& CS35L35_PDN_DONE
)
1127 complete(&cs35l35
->pdn_done
);
1129 /* read the current values */
1130 regmap_read(cs35l35
->regmap
, CS35L35_INT_STATUS_1
, ¤t1
);
1132 /* handle the interrupts */
1133 if (sticky1
& CS35L35_CAL_ERR
) {
1134 dev_crit(cs35l35
->dev
, "Calibration Error\n");
1136 /* error is no longer asserted; safe to reset */
1137 if (!(current1
& CS35L35_CAL_ERR
)) {
1138 pr_debug("%s : Cal error release\n", __func__
);
1139 regmap_update_bits(cs35l35
->regmap
,
1140 CS35L35_PROT_RELEASE_CTL
,
1141 CS35L35_CAL_ERR_RLS
, 0);
1142 regmap_update_bits(cs35l35
->regmap
,
1143 CS35L35_PROT_RELEASE_CTL
,
1144 CS35L35_CAL_ERR_RLS
,
1145 CS35L35_CAL_ERR_RLS
);
1146 regmap_update_bits(cs35l35
->regmap
,
1147 CS35L35_PROT_RELEASE_CTL
,
1148 CS35L35_CAL_ERR_RLS
, 0);
1152 if (sticky1
& CS35L35_AMP_SHORT
) {
1153 dev_crit(cs35l35
->dev
, "AMP Short Error\n");
1154 /* error is no longer asserted; safe to reset */
1155 if (!(current1
& CS35L35_AMP_SHORT
)) {
1156 dev_dbg(cs35l35
->dev
, "Amp short error release\n");
1157 regmap_update_bits(cs35l35
->regmap
,
1158 CS35L35_PROT_RELEASE_CTL
,
1159 CS35L35_SHORT_RLS
, 0);
1160 regmap_update_bits(cs35l35
->regmap
,
1161 CS35L35_PROT_RELEASE_CTL
,
1164 regmap_update_bits(cs35l35
->regmap
,
1165 CS35L35_PROT_RELEASE_CTL
,
1166 CS35L35_SHORT_RLS
, 0);
1170 if (sticky1
& CS35L35_OTW
) {
1171 dev_warn(cs35l35
->dev
, "Over temperature warning\n");
1173 /* error is no longer asserted; safe to reset */
1174 if (!(current1
& CS35L35_OTW
)) {
1175 dev_dbg(cs35l35
->dev
, "Over temperature warn release\n");
1176 regmap_update_bits(cs35l35
->regmap
,
1177 CS35L35_PROT_RELEASE_CTL
,
1178 CS35L35_OTW_RLS
, 0);
1179 regmap_update_bits(cs35l35
->regmap
,
1180 CS35L35_PROT_RELEASE_CTL
,
1183 regmap_update_bits(cs35l35
->regmap
,
1184 CS35L35_PROT_RELEASE_CTL
,
1185 CS35L35_OTW_RLS
, 0);
1189 if (sticky1
& CS35L35_OTE
) {
1190 dev_crit(cs35l35
->dev
, "Over temperature error\n");
1191 /* error is no longer asserted; safe to reset */
1192 if (!(current1
& CS35L35_OTE
)) {
1193 dev_dbg(cs35l35
->dev
, "Over temperature error release\n");
1194 regmap_update_bits(cs35l35
->regmap
,
1195 CS35L35_PROT_RELEASE_CTL
,
1196 CS35L35_OTE_RLS
, 0);
1197 regmap_update_bits(cs35l35
->regmap
,
1198 CS35L35_PROT_RELEASE_CTL
,
1201 regmap_update_bits(cs35l35
->regmap
,
1202 CS35L35_PROT_RELEASE_CTL
,
1203 CS35L35_OTE_RLS
, 0);
1207 if (sticky3
& CS35L35_BST_HIGH
) {
1208 dev_crit(cs35l35
->dev
, "VBST error: powering off!\n");
1209 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
1210 CS35L35_PDN_AMP
, CS35L35_PDN_AMP
);
1211 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL1
,
1212 CS35L35_PDN_ALL
, CS35L35_PDN_ALL
);
1215 if (sticky3
& CS35L35_LBST_SHORT
) {
1216 dev_crit(cs35l35
->dev
, "LBST error: powering off!\n");
1217 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
1218 CS35L35_PDN_AMP
, CS35L35_PDN_AMP
);
1219 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL1
,
1220 CS35L35_PDN_ALL
, CS35L35_PDN_ALL
);
1223 if (sticky2
& CS35L35_VPBR_ERR
)
1224 dev_dbg(cs35l35
->dev
, "Error: Reactive Brownout\n");
1226 if (sticky4
& CS35L35_VMON_OVFL
)
1227 dev_dbg(cs35l35
->dev
, "Error: VMON overflow\n");
1229 if (sticky4
& CS35L35_IMON_OVFL
)
1230 dev_dbg(cs35l35
->dev
, "Error: IMON overflow\n");
1236 static int cs35l35_handle_of_data(struct i2c_client
*i2c_client
,
1237 struct cs35l35_platform_data
*pdata
)
1239 struct device_node
*np
= i2c_client
->dev
.of_node
;
1240 struct device_node
*classh
, *signal_format
;
1241 struct classh_cfg
*classh_config
= &pdata
->classh_algo
;
1242 struct monitor_cfg
*monitor_config
= &pdata
->mon_cfg
;
1243 unsigned int val32
= 0;
1244 u8 monitor_array
[4];
1245 const int imon_array_size
= ARRAY_SIZE(monitor_array
);
1246 const int mon_array_size
= imon_array_size
- 1;
1252 pdata
->bst_pdn_fet_on
= of_property_read_bool(np
,
1253 "cirrus,boost-pdn-fet-on");
1255 ret
= of_property_read_u32(np
, "cirrus,boost-ctl-millivolt", &val32
);
1257 if (val32
< 2600 || val32
> 9000) {
1258 dev_err(&i2c_client
->dev
,
1259 "Invalid Boost Voltage %d mV\n", val32
);
1262 pdata
->bst_vctl
= ((val32
- 2600) / 100) + 1;
1265 ret
= of_property_read_u32(np
, "cirrus,boost-peak-milliamp", &val32
);
1267 if (val32
< 1680 || val32
> 4480) {
1268 dev_err(&i2c_client
->dev
,
1269 "Invalid Boost Peak Current %u mA\n", val32
);
1273 pdata
->bst_ipk
= ((val32
- 1680) / 110) | CS35L35_VALID_PDATA
;
1276 ret
= of_property_read_u32(np
, "cirrus,boost-ind-nanohenry", &val32
);
1278 pdata
->boost_ind
= val32
;
1280 dev_err(&i2c_client
->dev
, "Inductor not specified.\n");
1284 if (of_property_read_u32(np
, "cirrus,sp-drv-strength", &val32
) >= 0)
1285 pdata
->sp_drv_str
= val32
;
1286 if (of_property_read_u32(np
, "cirrus,sp-drv-unused", &val32
) >= 0)
1287 pdata
->sp_drv_unused
= val32
| CS35L35_VALID_PDATA
;
1289 pdata
->stereo
= of_property_read_bool(np
, "cirrus,stereo-config");
1291 if (pdata
->stereo
) {
1292 ret
= of_property_read_u32(np
, "cirrus,audio-channel", &val32
);
1294 pdata
->aud_channel
= val32
;
1296 ret
= of_property_read_u32(np
, "cirrus,advisory-channel",
1299 pdata
->adv_channel
= val32
;
1301 pdata
->shared_bst
= of_property_read_bool(np
,
1302 "cirrus,shared-boost");
1305 pdata
->ext_bst
= of_property_read_bool(np
, "cirrus,external-boost");
1307 pdata
->gain_zc
= of_property_read_bool(np
, "cirrus,amp-gain-zc");
1309 classh
= of_get_child_by_name(np
, "cirrus,classh-internal-algo");
1310 classh_config
->classh_algo_enable
= (classh
!= NULL
);
1312 if (classh_config
->classh_algo_enable
) {
1313 classh_config
->classh_bst_override
=
1314 of_property_read_bool(np
, "cirrus,classh-bst-overide");
1316 ret
= of_property_read_u32(classh
,
1317 "cirrus,classh-bst-max-limit",
1320 val32
|= CS35L35_VALID_PDATA
;
1321 classh_config
->classh_bst_max_limit
= val32
;
1324 ret
= of_property_read_u32(classh
,
1325 "cirrus,classh-bst-max-limit",
1328 val32
|= CS35L35_VALID_PDATA
;
1329 classh_config
->classh_bst_max_limit
= val32
;
1332 ret
= of_property_read_u32(classh
, "cirrus,classh-mem-depth",
1335 val32
|= CS35L35_VALID_PDATA
;
1336 classh_config
->classh_mem_depth
= val32
;
1339 ret
= of_property_read_u32(classh
, "cirrus,classh-release-rate",
1342 classh_config
->classh_release_rate
= val32
;
1344 ret
= of_property_read_u32(classh
, "cirrus,classh-headroom",
1347 val32
|= CS35L35_VALID_PDATA
;
1348 classh_config
->classh_headroom
= val32
;
1351 ret
= of_property_read_u32(classh
,
1352 "cirrus,classh-wk-fet-disable",
1355 classh_config
->classh_wk_fet_disable
= val32
;
1357 ret
= of_property_read_u32(classh
, "cirrus,classh-wk-fet-delay",
1360 val32
|= CS35L35_VALID_PDATA
;
1361 classh_config
->classh_wk_fet_delay
= val32
;
1364 ret
= of_property_read_u32(classh
, "cirrus,classh-wk-fet-thld",
1367 classh_config
->classh_wk_fet_thld
= val32
;
1369 ret
= of_property_read_u32(classh
, "cirrus,classh-vpch-auto",
1372 val32
|= CS35L35_VALID_PDATA
;
1373 classh_config
->classh_vpch_auto
= val32
;
1376 ret
= of_property_read_u32(classh
, "cirrus,classh-vpch-rate",
1379 val32
|= CS35L35_VALID_PDATA
;
1380 classh_config
->classh_vpch_rate
= val32
;
1383 ret
= of_property_read_u32(classh
, "cirrus,classh-vpch-man",
1386 classh_config
->classh_vpch_man
= val32
;
1388 of_node_put(classh
);
1390 /* frame depth location */
1391 signal_format
= of_get_child_by_name(np
, "cirrus,monitor-signal-format");
1392 monitor_config
->is_present
= signal_format
? true : false;
1393 if (monitor_config
->is_present
) {
1394 ret
= of_property_read_u8_array(signal_format
, "cirrus,imon",
1395 monitor_array
, imon_array_size
);
1397 monitor_config
->imon_specs
= true;
1398 monitor_config
->imon_dpth
= monitor_array
[0];
1399 monitor_config
->imon_loc
= monitor_array
[1];
1400 monitor_config
->imon_frm
= monitor_array
[2];
1401 monitor_config
->imon_scale
= monitor_array
[3];
1403 ret
= of_property_read_u8_array(signal_format
, "cirrus,vmon",
1404 monitor_array
, mon_array_size
);
1406 monitor_config
->vmon_specs
= true;
1407 monitor_config
->vmon_dpth
= monitor_array
[0];
1408 monitor_config
->vmon_loc
= monitor_array
[1];
1409 monitor_config
->vmon_frm
= monitor_array
[2];
1411 ret
= of_property_read_u8_array(signal_format
, "cirrus,vpmon",
1412 monitor_array
, mon_array_size
);
1414 monitor_config
->vpmon_specs
= true;
1415 monitor_config
->vpmon_dpth
= monitor_array
[0];
1416 monitor_config
->vpmon_loc
= monitor_array
[1];
1417 monitor_config
->vpmon_frm
= monitor_array
[2];
1419 ret
= of_property_read_u8_array(signal_format
, "cirrus,vbstmon",
1420 monitor_array
, mon_array_size
);
1422 monitor_config
->vbstmon_specs
= true;
1423 monitor_config
->vbstmon_dpth
= monitor_array
[0];
1424 monitor_config
->vbstmon_loc
= monitor_array
[1];
1425 monitor_config
->vbstmon_frm
= monitor_array
[2];
1427 ret
= of_property_read_u8_array(signal_format
, "cirrus,vpbrstat",
1428 monitor_array
, mon_array_size
);
1430 monitor_config
->vpbrstat_specs
= true;
1431 monitor_config
->vpbrstat_dpth
= monitor_array
[0];
1432 monitor_config
->vpbrstat_loc
= monitor_array
[1];
1433 monitor_config
->vpbrstat_frm
= monitor_array
[2];
1435 ret
= of_property_read_u8_array(signal_format
, "cirrus,zerofill",
1436 monitor_array
, mon_array_size
);
1438 monitor_config
->zerofill_specs
= true;
1439 monitor_config
->zerofill_dpth
= monitor_array
[0];
1440 monitor_config
->zerofill_loc
= monitor_array
[1];
1441 monitor_config
->zerofill_frm
= monitor_array
[2];
1444 of_node_put(signal_format
);
1450 static const struct reg_sequence cs35l35_errata_patch
[] = {
1465 static int cs35l35_i2c_probe(struct i2c_client
*i2c_client
)
1467 struct cs35l35_private
*cs35l35
;
1468 struct device
*dev
= &i2c_client
->dev
;
1469 struct cs35l35_platform_data
*pdata
= dev_get_platdata(dev
);
1474 cs35l35
= devm_kzalloc(dev
, sizeof(struct cs35l35_private
), GFP_KERNEL
);
1480 i2c_set_clientdata(i2c_client
, cs35l35
);
1481 cs35l35
->regmap
= devm_regmap_init_i2c(i2c_client
, &cs35l35_regmap
);
1482 if (IS_ERR(cs35l35
->regmap
)) {
1483 ret
= PTR_ERR(cs35l35
->regmap
);
1484 dev_err(dev
, "regmap_init() failed: %d\n", ret
);
1488 for (i
= 0; i
< ARRAY_SIZE(cs35l35_supplies
); i
++)
1489 cs35l35
->supplies
[i
].supply
= cs35l35_supplies
[i
];
1491 cs35l35
->num_supplies
= ARRAY_SIZE(cs35l35_supplies
);
1493 ret
= devm_regulator_bulk_get(dev
, cs35l35
->num_supplies
,
1496 dev_err(dev
, "Failed to request core supplies: %d\n", ret
);
1501 cs35l35
->pdata
= *pdata
;
1503 pdata
= devm_kzalloc(dev
, sizeof(struct cs35l35_platform_data
),
1507 if (i2c_client
->dev
.of_node
) {
1508 ret
= cs35l35_handle_of_data(i2c_client
, pdata
);
1513 cs35l35
->pdata
= *pdata
;
1516 ret
= regulator_bulk_enable(cs35l35
->num_supplies
,
1519 dev_err(dev
, "Failed to enable core supplies: %d\n", ret
);
1523 /* returning NULL can be valid if in stereo mode */
1524 cs35l35
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
1526 if (IS_ERR(cs35l35
->reset_gpio
)) {
1527 ret
= PTR_ERR(cs35l35
->reset_gpio
);
1528 cs35l35
->reset_gpio
= NULL
;
1529 if (ret
== -EBUSY
) {
1531 "Reset line busy, assuming shared reset\n");
1533 dev_err(dev
, "Failed to get reset GPIO: %d\n", ret
);
1538 cs35l35_reset(cs35l35
);
1540 init_completion(&cs35l35
->pdn_done
);
1542 ret
= devm_request_threaded_irq(dev
, i2c_client
->irq
, NULL
, cs35l35_irq
,
1543 IRQF_ONESHOT
| IRQF_TRIGGER_LOW
|
1544 IRQF_SHARED
, "cs35l35", cs35l35
);
1546 dev_err(dev
, "Failed to request IRQ: %d\n", ret
);
1549 /* initialize codec */
1550 devid
= cirrus_read_device_id(cs35l35
->regmap
, CS35L35_DEVID_AB
);
1553 dev_err(dev
, "Failed to read device ID: %d\n", ret
);
1557 if (devid
!= CS35L35_CHIP_ID
) {
1558 dev_err(dev
, "CS35L35 Device ID (%X). Expected ID %X\n",
1559 devid
, CS35L35_CHIP_ID
);
1564 ret
= regmap_read(cs35l35
->regmap
, CS35L35_REV_ID
, ®
);
1566 dev_err(dev
, "Get Revision ID failed: %d\n", ret
);
1570 ret
= regmap_register_patch(cs35l35
->regmap
, cs35l35_errata_patch
,
1571 ARRAY_SIZE(cs35l35_errata_patch
));
1573 dev_err(dev
, "Failed to apply errata patch: %d\n", ret
);
1577 dev_info(dev
, "Cirrus Logic CS35L35 (%x), Revision: %02X\n",
1580 /* Set the INT Masks for critical errors */
1581 regmap_write(cs35l35
->regmap
, CS35L35_INT_MASK_1
,
1582 CS35L35_INT1_CRIT_MASK
);
1583 regmap_write(cs35l35
->regmap
, CS35L35_INT_MASK_2
,
1584 CS35L35_INT2_CRIT_MASK
);
1585 regmap_write(cs35l35
->regmap
, CS35L35_INT_MASK_3
,
1586 CS35L35_INT3_CRIT_MASK
);
1587 regmap_write(cs35l35
->regmap
, CS35L35_INT_MASK_4
,
1588 CS35L35_INT4_CRIT_MASK
);
1590 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
1591 CS35L35_PWR2_PDN_MASK
,
1592 CS35L35_PWR2_PDN_MASK
);
1594 if (cs35l35
->pdata
.bst_pdn_fet_on
)
1595 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
1596 CS35L35_PDN_BST_MASK
,
1597 1 << CS35L35_PDN_BST_FETON_SHIFT
);
1599 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL2
,
1600 CS35L35_PDN_BST_MASK
,
1601 1 << CS35L35_PDN_BST_FETOFF_SHIFT
);
1603 regmap_update_bits(cs35l35
->regmap
, CS35L35_PWRCTL3
,
1604 CS35L35_PWR3_PDN_MASK
,
1605 CS35L35_PWR3_PDN_MASK
);
1607 regmap_update_bits(cs35l35
->regmap
, CS35L35_PROTECT_CTL
,
1608 CS35L35_AMP_MUTE_MASK
, 1 << CS35L35_AMP_MUTE_SHIFT
);
1610 ret
= devm_snd_soc_register_component(dev
, &soc_component_dev_cs35l35
,
1611 cs35l35_dai
, ARRAY_SIZE(cs35l35_dai
));
1613 dev_err(dev
, "Failed to register component: %d\n", ret
);
1620 regulator_bulk_disable(cs35l35
->num_supplies
,
1622 gpiod_set_value_cansleep(cs35l35
->reset_gpio
, 0);
1627 static void cs35l35_i2c_remove(struct i2c_client
*i2c_client
)
1629 struct cs35l35_private
*cs35l35
= i2c_get_clientdata(i2c_client
);
1631 regulator_bulk_disable(cs35l35
->num_supplies
, cs35l35
->supplies
);
1632 gpiod_set_value_cansleep(cs35l35
->reset_gpio
, 0);
1635 static const struct of_device_id cs35l35_of_match
[] = {
1636 {.compatible
= "cirrus,cs35l35"},
1639 MODULE_DEVICE_TABLE(of
, cs35l35_of_match
);
1641 static const struct i2c_device_id cs35l35_id
[] = {
1646 MODULE_DEVICE_TABLE(i2c
, cs35l35_id
);
1648 static struct i2c_driver cs35l35_i2c_driver
= {
1651 .of_match_table
= cs35l35_of_match
,
1653 .id_table
= cs35l35_id
,
1654 .probe
= cs35l35_i2c_probe
,
1655 .remove
= cs35l35_i2c_remove
,
1658 module_i2c_driver(cs35l35_i2c_driver
);
1660 MODULE_DESCRIPTION("ASoC CS35L35 driver");
1661 MODULE_AUTHOR("Brian Austin, Cirrus Logic Inc, <brian.austin@cirrus.com>");
1662 MODULE_LICENSE("GPL");