blob: 991e4ebd7a042ebac5b48cbdf2840bc791fc751b [file] [log] [blame]
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +02001/*
2 * cs42l51.c
3 *
4 * ASoC Driver for Cirrus Logic CS42L51 codecs
5 *
6 * Copyright (c) 2010 Arnaud Patard <apatard@mandriva.com>
7 *
8 * Based on cs4270.c - Copyright (c) Freescale Semiconductor
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * For now:
20 * - Only I2C is support. Not SPI
21 * - master mode *NOT* supported
22 */
23
Olivier Moysan318e7412018-10-19 17:56:35 +020024#include <linux/clk.h>
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020025#include <linux/module.h>
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020026#include <linux/slab.h>
27#include <sound/core.h>
28#include <sound/soc.h>
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020029#include <sound/tlv.h>
30#include <sound/initval.h>
31#include <sound/pcm_params.h>
32#include <sound/pcm.h>
Olivier Moysan11b9cd72019-04-03 15:23:33 +020033#include <linux/gpio/consumer.h>
Mark Brownda071482014-02-11 19:24:31 +000034#include <linux/regmap.h>
Olivier Moysanf77b6ea2019-04-03 15:23:32 +020035#include <linux/regulator/consumer.h>
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020036
37#include "cs42l51.h"
38
39enum master_slave_mode {
40 MODE_SLAVE,
41 MODE_SLAVE_AUTO,
42 MODE_MASTER,
43};
44
Olivier Moysanf77b6ea2019-04-03 15:23:32 +020045static const char * const cs42l51_supply_names[] = {
46 "VL",
47 "VD",
48 "VA",
49 "VAHP",
50};
51
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020052struct cs42l51_private {
53 unsigned int mclk;
Olivier Moysan318e7412018-10-19 17:56:35 +020054 struct clk *mclk_handle;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020055 unsigned int audio_mode; /* The mode (I2S or left-justified) */
56 enum master_slave_mode func;
Olivier Moysanf77b6ea2019-04-03 15:23:32 +020057 struct regulator_bulk_data supplies[ARRAY_SIZE(cs42l51_supply_names)];
Olivier Moysan11b9cd72019-04-03 15:23:33 +020058 struct gpio_desc *reset_gpio;
Olivier Moysan75a71482019-04-03 15:23:36 +020059 struct regmap *regmap;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020060};
61
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020062#define CS42L51_FORMATS ( \
63 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
64 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
65 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
66 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
67
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020068static int cs42l51_get_chan_mix(struct snd_kcontrol *kcontrol,
69 struct snd_ctl_elem_value *ucontrol)
70{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +000071 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
72 unsigned long value = snd_soc_component_read32(component, CS42L51_PCM_MIXER)&3;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020073
74 switch (value) {
75 default:
76 case 0:
Takashi Iwai89300b42016-02-29 18:02:59 +010077 ucontrol->value.enumerated.item[0] = 0;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020078 break;
79 /* same value : (L+R)/2 and (R+L)/2 */
80 case 1:
81 case 2:
Takashi Iwai89300b42016-02-29 18:02:59 +010082 ucontrol->value.enumerated.item[0] = 1;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020083 break;
84 case 3:
Takashi Iwai89300b42016-02-29 18:02:59 +010085 ucontrol->value.enumerated.item[0] = 2;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020086 break;
87 }
88
89 return 0;
90}
91
92#define CHAN_MIX_NORMAL 0x00
93#define CHAN_MIX_BOTH 0x55
94#define CHAN_MIX_SWAP 0xFF
95
96static int cs42l51_set_chan_mix(struct snd_kcontrol *kcontrol,
97 struct snd_ctl_elem_value *ucontrol)
98{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +000099 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200100 unsigned char val;
101
Takashi Iwai89300b42016-02-29 18:02:59 +0100102 switch (ucontrol->value.enumerated.item[0]) {
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200103 default:
104 case 0:
105 val = CHAN_MIX_NORMAL;
106 break;
107 case 1:
108 val = CHAN_MIX_BOTH;
109 break;
110 case 2:
111 val = CHAN_MIX_SWAP;
112 break;
113 }
114
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000115 snd_soc_component_write(component, CS42L51_PCM_MIXER, val);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200116
117 return 1;
118}
119
120static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0);
121static const DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0);
Brian Austin7272e052014-03-19 10:40:02 -0500122
123static const DECLARE_TLV_DB_SCALE(aout_tlv, -10200, 50, 0);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200124
125static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0);
Olivier Moysane04232c2019-04-03 15:23:37 +0200126static const DECLARE_TLV_DB_SCALE(adc_boost_tlv, 2000, 2000, 0);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200127static const char *chan_mix[] = {
128 "L R",
129 "L+R",
130 "R L",
131};
132
Takashi Iwai6109ab22014-02-18 10:44:57 +0100133static SOC_ENUM_SINGLE_EXT_DECL(cs42l51_chan_mix, chan_mix);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200134
135static const struct snd_kcontrol_new cs42l51_snd_controls[] = {
136 SOC_DOUBLE_R_SX_TLV("PCM Playback Volume",
137 CS42L51_PCMA_VOL, CS42L51_PCMB_VOL,
Brian Austin7272e052014-03-19 10:40:02 -0500138 0, 0x19, 0x7F, adc_pcm_tlv),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200139 SOC_DOUBLE_R("PCM Playback Switch",
140 CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, 7, 1, 1),
141 SOC_DOUBLE_R_SX_TLV("Analog Playback Volume",
142 CS42L51_AOUTA_VOL, CS42L51_AOUTB_VOL,
Brian Austin1d99f242012-03-30 10:43:55 -0500143 0, 0x34, 0xE4, aout_tlv),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200144 SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume",
145 CS42L51_ADCA_VOL, CS42L51_ADCB_VOL,
Brian Austin7272e052014-03-19 10:40:02 -0500146 0, 0x19, 0x7F, adc_pcm_tlv),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200147 SOC_DOUBLE_R("ADC Mixer Switch",
148 CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1),
149 SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0),
150 SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0),
151 SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0),
152 SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0),
153 SOC_DOUBLE_TLV("Mic Boost Volume",
154 CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv),
Olivier Moysane04232c2019-04-03 15:23:37 +0200155 SOC_DOUBLE_TLV("ADC Boost Volume",
156 CS42L51_MIC_CTL, 5, 6, 1, 0, adc_boost_tlv),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200157 SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv),
158 SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv),
159 SOC_ENUM_EXT("PCM channel mixer",
160 cs42l51_chan_mix,
161 cs42l51_get_chan_mix, cs42l51_set_chan_mix),
162};
163
164/*
165 * to power down, one must:
166 * 1.) Enable the PDN bit
167 * 2.) enable power-down for the select channels
168 * 3.) disable the PDN bit.
169 */
170static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w,
171 struct snd_kcontrol *kcontrol, int event)
172{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000173 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
Lars-Peter Clausenceb3c062014-11-20 21:05:38 +0100174
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200175 switch (event) {
176 case SND_SOC_DAPM_PRE_PMD:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000177 snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
Axel Line94de1e2011-11-01 15:17:57 +0800178 CS42L51_POWER_CTL1_PDN,
179 CS42L51_POWER_CTL1_PDN);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200180 break;
181 default:
182 case SND_SOC_DAPM_POST_PMD:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000183 snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
Axel Line94de1e2011-11-01 15:17:57 +0800184 CS42L51_POWER_CTL1_PDN, 0);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200185 break;
186 }
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200187
188 return 0;
189}
190
191static const char *cs42l51_dac_names[] = {"Direct PCM",
192 "DSP PCM", "ADC"};
Takashi Iwai6109ab22014-02-18 10:44:57 +0100193static SOC_ENUM_SINGLE_DECL(cs42l51_dac_mux_enum,
194 CS42L51_DAC_CTL, 6, cs42l51_dac_names);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200195static const struct snd_kcontrol_new cs42l51_dac_mux_controls =
196 SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum);
197
198static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left",
199 "MIC Left", "MIC+preamp Left"};
Takashi Iwai6109ab22014-02-18 10:44:57 +0100200static SOC_ENUM_SINGLE_DECL(cs42l51_adcl_mux_enum,
201 CS42L51_ADC_INPUT, 4, cs42l51_adcl_names);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200202static const struct snd_kcontrol_new cs42l51_adcl_mux_controls =
203 SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum);
204
205static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right",
206 "MIC Right", "MIC+preamp Right"};
Takashi Iwai6109ab22014-02-18 10:44:57 +0100207static SOC_ENUM_SINGLE_DECL(cs42l51_adcr_mux_enum,
208 CS42L51_ADC_INPUT, 6, cs42l51_adcr_names);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200209static const struct snd_kcontrol_new cs42l51_adcr_mux_controls =
210 SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum);
211
212static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = {
Olivier Moysan4110e9a2019-04-03 15:23:35 +0200213 SND_SOC_DAPM_SUPPLY("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1, NULL,
214 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200215 SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0,
216 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
217 SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0,
218 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
219 SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture",
220 CS42L51_POWER_CTL1, 1, 1,
221 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
222 SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture",
223 CS42L51_POWER_CTL1, 2, 1,
224 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
225 SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback",
226 CS42L51_POWER_CTL1, 5, 1,
227 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
228 SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback",
229 CS42L51_POWER_CTL1, 6, 1,
230 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
231
232 /* analog/mic */
233 SND_SOC_DAPM_INPUT("AIN1L"),
234 SND_SOC_DAPM_INPUT("AIN1R"),
235 SND_SOC_DAPM_INPUT("AIN2L"),
236 SND_SOC_DAPM_INPUT("AIN2R"),
237 SND_SOC_DAPM_INPUT("MICL"),
238 SND_SOC_DAPM_INPUT("MICR"),
239
240 SND_SOC_DAPM_MIXER("Mic Preamp Left",
241 CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0),
242 SND_SOC_DAPM_MIXER("Mic Preamp Right",
243 CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0),
244
245 /* HP */
246 SND_SOC_DAPM_OUTPUT("HPL"),
247 SND_SOC_DAPM_OUTPUT("HPR"),
248
249 /* mux */
250 SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0,
251 &cs42l51_dac_mux_controls),
252 SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0,
253 &cs42l51_adcl_mux_controls),
254 SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0,
255 &cs42l51_adcr_mux_controls),
256};
257
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200258static const struct snd_soc_dapm_widget cs42l51_dapm_mclk_widgets[] = {
259 SND_SOC_DAPM_CLOCK_SUPPLY("MCLK")
260};
261
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200262static const struct snd_soc_dapm_route cs42l51_routes[] = {
263 {"HPL", NULL, "Left DAC"},
264 {"HPR", NULL, "Right DAC"},
265
266 {"Left ADC", NULL, "Left PGA"},
267 {"Right ADC", NULL, "Right PGA"},
268
269 {"Mic Preamp Left", NULL, "MICL"},
270 {"Mic Preamp Right", NULL, "MICR"},
271
272 {"PGA-ADC Mux Left", "AIN1 Left", "AIN1L" },
273 {"PGA-ADC Mux Left", "AIN2 Left", "AIN2L" },
274 {"PGA-ADC Mux Left", "MIC Left", "MICL" },
275 {"PGA-ADC Mux Left", "MIC+preamp Left", "Mic Preamp Left" },
276 {"PGA-ADC Mux Right", "AIN1 Right", "AIN1R" },
277 {"PGA-ADC Mux Right", "AIN2 Right", "AIN2R" },
278 {"PGA-ADC Mux Right", "MIC Right", "MICR" },
279 {"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" },
280
281 {"Left PGA", NULL, "PGA-ADC Mux Left"},
282 {"Right PGA", NULL, "PGA-ADC Mux Right"},
283};
284
285static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai,
286 unsigned int format)
287{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000288 struct snd_soc_component *component = codec_dai->component;
289 struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200290
291 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
292 case SND_SOC_DAIFMT_I2S:
293 case SND_SOC_DAIFMT_LEFT_J:
294 case SND_SOC_DAIFMT_RIGHT_J:
295 cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
296 break;
297 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000298 dev_err(component->dev, "invalid DAI format\n");
Axel Linac601552011-10-06 07:29:56 +0800299 return -EINVAL;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200300 }
301
302 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
303 case SND_SOC_DAIFMT_CBM_CFM:
304 cs42l51->func = MODE_MASTER;
305 break;
306 case SND_SOC_DAIFMT_CBS_CFS:
307 cs42l51->func = MODE_SLAVE_AUTO;
308 break;
309 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000310 dev_err(component->dev, "Unknown master/slave configuration\n");
Axel Linac601552011-10-06 07:29:56 +0800311 return -EINVAL;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200312 }
313
Axel Linac601552011-10-06 07:29:56 +0800314 return 0;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200315}
316
317struct cs42l51_ratios {
318 unsigned int ratio;
319 unsigned char speed_mode;
320 unsigned char mclk;
321};
322
323static struct cs42l51_ratios slave_ratios[] = {
324 { 512, CS42L51_QSM_MODE, 0 }, { 768, CS42L51_QSM_MODE, 0 },
325 { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
326 { 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 },
327 { 256, CS42L51_HSM_MODE, 0 }, { 384, CS42L51_HSM_MODE, 0 },
328 { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
329 { 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 },
330 { 128, CS42L51_SSM_MODE, 0 }, { 192, CS42L51_SSM_MODE, 0 },
331 { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
332 { 512, CS42L51_SSM_MODE, 0 }, { 768, CS42L51_SSM_MODE, 0 },
333 { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
334 { 256, CS42L51_DSM_MODE, 0 }, { 384, CS42L51_DSM_MODE, 0 },
335};
336
337static struct cs42l51_ratios slave_auto_ratios[] = {
338 { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
339 { 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 },
340 { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
341 { 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 },
342 { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
343 { 512, CS42L51_SSM_MODE, 1 }, { 768, CS42L51_SSM_MODE, 1 },
344 { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
345 { 256, CS42L51_DSM_MODE, 1 }, { 384, CS42L51_DSM_MODE, 1 },
346};
347
Olivier Moysan2f7c4ce2019-04-03 15:23:34 +0200348/*
349 * Master mode mclk/fs ratios.
350 * Recommended configurations are SSM for 4-50khz and DSM for 50-100kHz ranges
351 * The table below provides support of following ratios:
352 * 128: SSM (%128) with div2 disabled
353 * 256: SSM (%128) with div2 enabled
354 * In both cases, if sampling rate is above 50kHz, SSM is overridden
355 * with DSM (%128) configuration
356 */
357static struct cs42l51_ratios master_ratios[] = {
358 { 128, CS42L51_SSM_MODE, 0 }, { 256, CS42L51_SSM_MODE, 1 },
359};
360
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200361static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai,
362 int clk_id, unsigned int freq, int dir)
363{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000364 struct snd_soc_component *component = codec_dai->component;
365 struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200366
367 cs42l51->mclk = freq;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200368 return 0;
369}
370
371static int cs42l51_hw_params(struct snd_pcm_substream *substream,
372 struct snd_pcm_hw_params *params,
373 struct snd_soc_dai *dai)
374{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000375 struct snd_soc_component *component = dai->component;
376 struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200377 int ret;
378 unsigned int i;
379 unsigned int rate;
380 unsigned int ratio;
381 struct cs42l51_ratios *ratios = NULL;
382 int nr_ratios = 0;
Olivier Moysan2f7c4ce2019-04-03 15:23:34 +0200383 int intf_ctl, power_ctl, fmt, mode;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200384
385 switch (cs42l51->func) {
386 case MODE_MASTER:
Olivier Moysan2f7c4ce2019-04-03 15:23:34 +0200387 ratios = master_ratios;
388 nr_ratios = ARRAY_SIZE(master_ratios);
389 break;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200390 case MODE_SLAVE:
391 ratios = slave_ratios;
392 nr_ratios = ARRAY_SIZE(slave_ratios);
393 break;
394 case MODE_SLAVE_AUTO:
395 ratios = slave_auto_ratios;
396 nr_ratios = ARRAY_SIZE(slave_auto_ratios);
397 break;
398 }
399
400 /* Figure out which MCLK/LRCK ratio to use */
401 rate = params_rate(params); /* Sampling rate, in Hz */
402 ratio = cs42l51->mclk / rate; /* MCLK/LRCK ratio */
403 for (i = 0; i < nr_ratios; i++) {
404 if (ratios[i].ratio == ratio)
405 break;
406 }
407
408 if (i == nr_ratios) {
409 /* We did not find a matching ratio */
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000410 dev_err(component->dev, "could not find matching ratio\n");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200411 return -EINVAL;
412 }
413
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000414 intf_ctl = snd_soc_component_read32(component, CS42L51_INTF_CTL);
415 power_ctl = snd_soc_component_read32(component, CS42L51_MIC_POWER_CTL);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200416
417 intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S
418 | CS42L51_INTF_CTL_DAC_FORMAT(7));
419 power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3)
420 | CS42L51_MIC_POWER_CTL_MCLK_DIV2);
421
422 switch (cs42l51->func) {
423 case MODE_MASTER:
424 intf_ctl |= CS42L51_INTF_CTL_MASTER;
Olivier Moysan2f7c4ce2019-04-03 15:23:34 +0200425 mode = ratios[i].speed_mode;
426 /* Force DSM mode if sampling rate is above 50kHz */
427 if (rate > 50000)
428 mode = CS42L51_DSM_MODE;
429 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(mode);
430 /*
431 * Auto detect mode is not applicable for master mode and has to
432 * be disabled. Otherwise SPEED[1:0] bits will be ignored.
433 */
434 power_ctl &= ~CS42L51_MIC_POWER_CTL_AUTO;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200435 break;
436 case MODE_SLAVE:
437 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
438 break;
439 case MODE_SLAVE_AUTO:
440 power_ctl |= CS42L51_MIC_POWER_CTL_AUTO;
441 break;
442 }
443
444 switch (cs42l51->audio_mode) {
445 case SND_SOC_DAIFMT_I2S:
446 intf_ctl |= CS42L51_INTF_CTL_ADC_I2S;
447 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S);
448 break;
449 case SND_SOC_DAIFMT_LEFT_J:
450 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24);
451 break;
452 case SND_SOC_DAIFMT_RIGHT_J:
Mark Brown1b6b0df2014-01-08 19:48:20 +0000453 switch (params_width(params)) {
454 case 16:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200455 fmt = CS42L51_DAC_DIF_RJ16;
456 break;
Mark Brown1b6b0df2014-01-08 19:48:20 +0000457 case 18:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200458 fmt = CS42L51_DAC_DIF_RJ18;
459 break;
Mark Brown1b6b0df2014-01-08 19:48:20 +0000460 case 20:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200461 fmt = CS42L51_DAC_DIF_RJ20;
462 break;
Mark Brown1b6b0df2014-01-08 19:48:20 +0000463 case 24:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200464 fmt = CS42L51_DAC_DIF_RJ24;
465 break;
466 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000467 dev_err(component->dev, "unknown format\n");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200468 return -EINVAL;
469 }
470 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt);
471 break;
472 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000473 dev_err(component->dev, "unknown format\n");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200474 return -EINVAL;
475 }
476
477 if (ratios[i].mclk)
478 power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2;
479
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000480 ret = snd_soc_component_write(component, CS42L51_INTF_CTL, intf_ctl);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200481 if (ret < 0)
482 return ret;
483
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000484 ret = snd_soc_component_write(component, CS42L51_MIC_POWER_CTL, power_ctl);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200485 if (ret < 0)
486 return ret;
487
488 return 0;
489}
490
491static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute)
492{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000493 struct snd_soc_component *component = dai->component;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200494 int reg;
495 int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE;
496
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000497 reg = snd_soc_component_read32(component, CS42L51_DAC_OUT_CTL);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200498
499 if (mute)
500 reg |= mask;
501 else
502 reg &= ~mask;
503
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000504 return snd_soc_component_write(component, CS42L51_DAC_OUT_CTL, reg);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200505}
506
Olivier Moysanad6bb302019-03-29 16:37:37 +0100507static int cs42l51_of_xlate_dai_id(struct snd_soc_component *component,
508 struct device_node *endpoint)
509{
510 /* return dai id 0, whatever the endpoint index */
511 return 0;
512}
513
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100514static const struct snd_soc_dai_ops cs42l51_dai_ops = {
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200515 .hw_params = cs42l51_hw_params,
516 .set_sysclk = cs42l51_set_dai_sysclk,
517 .set_fmt = cs42l51_set_dai_fmt,
518 .digital_mute = cs42l51_dai_mute,
519};
520
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521static struct snd_soc_dai_driver cs42l51_dai = {
522 .name = "cs42l51-hifi",
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200523 .playback = {
524 .stream_name = "Playback",
525 .channels_min = 1,
526 .channels_max = 2,
527 .rates = SNDRV_PCM_RATE_8000_96000,
528 .formats = CS42L51_FORMATS,
529 },
530 .capture = {
531 .stream_name = "Capture",
532 .channels_min = 1,
533 .channels_max = 2,
534 .rates = SNDRV_PCM_RATE_8000_96000,
535 .formats = CS42L51_FORMATS,
536 },
537 .ops = &cs42l51_dai_ops,
538};
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200539
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000540static int cs42l51_component_probe(struct snd_soc_component *component)
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200541{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 int ret, reg;
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200543 struct snd_soc_dapm_context *dapm;
Olivier Moysan318e7412018-10-19 17:56:35 +0200544 struct cs42l51_private *cs42l51;
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200545
Olivier Moysan318e7412018-10-19 17:56:35 +0200546 cs42l51 = snd_soc_component_get_drvdata(component);
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200547 dapm = snd_soc_component_get_dapm(component);
Olivier Moysan318e7412018-10-19 17:56:35 +0200548
549 if (cs42l51->mclk_handle)
550 snd_soc_dapm_new_controls(dapm, cs42l51_dapm_mclk_widgets, 1);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 /*
553 * DAC configuration
554 * - Use signal processor
555 * - auto mute
556 * - vol changes immediate
557 * - no de-emphasize
558 */
559 reg = CS42L51_DAC_CTL_DATA_SEL(1)
560 | CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0);
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000561 ret = snd_soc_component_write(component, CS42L51_DAC_CTL, reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (ret < 0)
563 return ret;
564
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200565 return 0;
566}
567
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000568static const struct snd_soc_component_driver soc_component_device_cs42l51 = {
569 .probe = cs42l51_component_probe,
570 .controls = cs42l51_snd_controls,
571 .num_controls = ARRAY_SIZE(cs42l51_snd_controls),
572 .dapm_widgets = cs42l51_dapm_widgets,
573 .num_dapm_widgets = ARRAY_SIZE(cs42l51_dapm_widgets),
574 .dapm_routes = cs42l51_routes,
575 .num_dapm_routes = ARRAY_SIZE(cs42l51_routes),
Olivier Moysanad6bb302019-03-29 16:37:37 +0100576 .of_xlate_dai_id = cs42l51_of_xlate_dai_id,
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000577 .idle_bias_on = 1,
578 .use_pmdown_time = 1,
579 .endianness = 1,
580 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581};
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200582
Olivier Moysan75a71482019-04-03 15:23:36 +0200583static bool cs42l51_writeable_reg(struct device *dev, unsigned int reg)
584{
585 switch (reg) {
586 case CS42L51_POWER_CTL1:
587 case CS42L51_MIC_POWER_CTL:
588 case CS42L51_INTF_CTL:
589 case CS42L51_MIC_CTL:
590 case CS42L51_ADC_CTL:
591 case CS42L51_ADC_INPUT:
592 case CS42L51_DAC_OUT_CTL:
593 case CS42L51_DAC_CTL:
594 case CS42L51_ALC_PGA_CTL:
595 case CS42L51_ALC_PGB_CTL:
596 case CS42L51_ADCA_ATT:
597 case CS42L51_ADCB_ATT:
598 case CS42L51_ADCA_VOL:
599 case CS42L51_ADCB_VOL:
600 case CS42L51_PCMA_VOL:
601 case CS42L51_PCMB_VOL:
602 case CS42L51_BEEP_FREQ:
603 case CS42L51_BEEP_VOL:
604 case CS42L51_BEEP_CONF:
605 case CS42L51_TONE_CTL:
606 case CS42L51_AOUTA_VOL:
607 case CS42L51_AOUTB_VOL:
608 case CS42L51_PCM_MIXER:
609 case CS42L51_LIMIT_THRES_DIS:
610 case CS42L51_LIMIT_REL:
611 case CS42L51_LIMIT_ATT:
612 case CS42L51_ALC_EN:
613 case CS42L51_ALC_REL:
614 case CS42L51_ALC_THRES:
615 case CS42L51_NOISE_CONF:
616 case CS42L51_CHARGE_FREQ:
617 return true;
618 default:
619 return false;
620 }
621}
622
623static bool cs42l51_volatile_reg(struct device *dev, unsigned int reg)
624{
625 switch (reg) {
626 case CS42L51_STATUS:
627 return true;
628 default:
629 return false;
630 }
631}
632
633static bool cs42l51_readable_reg(struct device *dev, unsigned int reg)
634{
635 switch (reg) {
636 case CS42L51_CHIP_REV_ID:
637 case CS42L51_POWER_CTL1:
638 case CS42L51_MIC_POWER_CTL:
639 case CS42L51_INTF_CTL:
640 case CS42L51_MIC_CTL:
641 case CS42L51_ADC_CTL:
642 case CS42L51_ADC_INPUT:
643 case CS42L51_DAC_OUT_CTL:
644 case CS42L51_DAC_CTL:
645 case CS42L51_ALC_PGA_CTL:
646 case CS42L51_ALC_PGB_CTL:
647 case CS42L51_ADCA_ATT:
648 case CS42L51_ADCB_ATT:
649 case CS42L51_ADCA_VOL:
650 case CS42L51_ADCB_VOL:
651 case CS42L51_PCMA_VOL:
652 case CS42L51_PCMB_VOL:
653 case CS42L51_BEEP_FREQ:
654 case CS42L51_BEEP_VOL:
655 case CS42L51_BEEP_CONF:
656 case CS42L51_TONE_CTL:
657 case CS42L51_AOUTA_VOL:
658 case CS42L51_AOUTB_VOL:
659 case CS42L51_PCM_MIXER:
660 case CS42L51_LIMIT_THRES_DIS:
661 case CS42L51_LIMIT_REL:
662 case CS42L51_LIMIT_ATT:
663 case CS42L51_ALC_EN:
664 case CS42L51_ALC_REL:
665 case CS42L51_ALC_THRES:
666 case CS42L51_NOISE_CONF:
667 case CS42L51_STATUS:
668 case CS42L51_CHARGE_FREQ:
669 return true;
670 default:
671 return false;
672 }
673}
674
Brian Austina1253ef2014-04-15 15:49:33 -0500675const struct regmap_config cs42l51_regmap = {
Olivier Moysan75a71482019-04-03 15:23:36 +0200676 .reg_bits = 8,
677 .reg_stride = 1,
678 .val_bits = 8,
679 .use_single_write = true,
680 .readable_reg = cs42l51_readable_reg,
681 .volatile_reg = cs42l51_volatile_reg,
682 .writeable_reg = cs42l51_writeable_reg,
Mark Brownda071482014-02-11 19:24:31 +0000683 .max_register = CS42L51_CHARGE_FREQ,
684 .cache_type = REGCACHE_RBTREE,
685};
Brian Austina1253ef2014-04-15 15:49:33 -0500686EXPORT_SYMBOL_GPL(cs42l51_regmap);
Mark Brownda071482014-02-11 19:24:31 +0000687
Brian Austina1253ef2014-04-15 15:49:33 -0500688int cs42l51_probe(struct device *dev, struct regmap *regmap)
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200689{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 struct cs42l51_private *cs42l51;
Mark Brownda071482014-02-11 19:24:31 +0000691 unsigned int val;
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200692 int ret, i;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200693
Brian Austina1253ef2014-04-15 15:49:33 -0500694 if (IS_ERR(regmap))
695 return PTR_ERR(regmap);
696
697 cs42l51 = devm_kzalloc(dev, sizeof(struct cs42l51_private),
698 GFP_KERNEL);
699 if (!cs42l51)
700 return -ENOMEM;
701
702 dev_set_drvdata(dev, cs42l51);
Olivier Moysan75a71482019-04-03 15:23:36 +0200703 cs42l51->regmap = regmap;
Mark Brownda071482014-02-11 19:24:31 +0000704
Olivier Moysan318e7412018-10-19 17:56:35 +0200705 cs42l51->mclk_handle = devm_clk_get(dev, "MCLK");
706 if (IS_ERR(cs42l51->mclk_handle)) {
707 if (PTR_ERR(cs42l51->mclk_handle) != -ENOENT)
708 return PTR_ERR(cs42l51->mclk_handle);
709 cs42l51->mclk_handle = NULL;
710 }
711
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200712 for (i = 0; i < ARRAY_SIZE(cs42l51->supplies); i++)
713 cs42l51->supplies[i].supply = cs42l51_supply_names[i];
714
715 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(cs42l51->supplies),
716 cs42l51->supplies);
717 if (ret != 0) {
718 dev_err(dev, "Failed to request supplies: %d\n", ret);
719 return ret;
720 }
721
722 ret = regulator_bulk_enable(ARRAY_SIZE(cs42l51->supplies),
723 cs42l51->supplies);
724 if (ret != 0) {
725 dev_err(dev, "Failed to enable supplies: %d\n", ret);
726 return ret;
727 }
728
Olivier Moysan11b9cd72019-04-03 15:23:33 +0200729 cs42l51->reset_gpio = devm_gpiod_get_optional(dev, "reset",
730 GPIOD_OUT_LOW);
731 if (IS_ERR(cs42l51->reset_gpio))
732 return PTR_ERR(cs42l51->reset_gpio);
733
734 if (cs42l51->reset_gpio) {
735 dev_dbg(dev, "Release reset gpio\n");
736 gpiod_set_value_cansleep(cs42l51->reset_gpio, 0);
737 mdelay(2);
738 }
739
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 /* Verify that we have a CS42L51 */
Mark Brownda071482014-02-11 19:24:31 +0000741 ret = regmap_read(regmap, CS42L51_CHIP_REV_ID, &val);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000742 if (ret < 0) {
Brian Austina1253ef2014-04-15 15:49:33 -0500743 dev_err(dev, "failed to read I2C\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 goto error;
745 }
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200746
Mark Brownda071482014-02-11 19:24:31 +0000747 if ((val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) &&
748 (val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) {
Brian Austina1253ef2014-04-15 15:49:33 -0500749 dev_err(dev, "Invalid chip id: %x\n", val);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 ret = -ENODEV;
751 goto error;
752 }
Axel Lin1025c052014-04-17 16:35:43 +0800753 dev_info(dev, "Cirrus Logic CS42L51, Revision: %02X\n",
754 val & CS42L51_CHIP_REV_MASK);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000756 ret = devm_snd_soc_register_component(dev,
757 &soc_component_device_cs42l51, &cs42l51_dai, 1);
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200758 if (ret < 0)
759 goto error;
760
761 return 0;
762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763error:
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200764 regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies),
765 cs42l51->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000766 return ret;
767}
Brian Austina1253ef2014-04-15 15:49:33 -0500768EXPORT_SYMBOL_GPL(cs42l51_probe);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200770int cs42l51_remove(struct device *dev)
771{
772 struct cs42l51_private *cs42l51 = dev_get_drvdata(dev);
773
Olivier Moysan11b9cd72019-04-03 15:23:33 +0200774 gpiod_set_value_cansleep(cs42l51->reset_gpio, 1);
775
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200776 return regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies),
777 cs42l51->supplies);
778}
779EXPORT_SYMBOL_GPL(cs42l51_remove);
780
Olivier Moysan75a71482019-04-03 15:23:36 +0200781int __maybe_unused cs42l51_suspend(struct device *dev)
782{
783 struct cs42l51_private *cs42l51 = dev_get_drvdata(dev);
784
785 regcache_cache_only(cs42l51->regmap, true);
786 regcache_mark_dirty(cs42l51->regmap);
787
788 return 0;
789}
790EXPORT_SYMBOL_GPL(cs42l51_suspend);
791
792int __maybe_unused cs42l51_resume(struct device *dev)
793{
794 struct cs42l51_private *cs42l51 = dev_get_drvdata(dev);
795
796 regcache_cache_only(cs42l51->regmap, false);
797
798 return regcache_sync(cs42l51->regmap);
799}
800EXPORT_SYMBOL_GPL(cs42l51_resume);
801
Thomas Petazzoni2cb1e022014-11-12 15:40:44 +0100802const struct of_device_id cs42l51_of_match[] = {
Thomas Petazzonidfd72a62014-01-30 18:14:05 +0100803 { .compatible = "cirrus,cs42l51", },
804 { }
805};
806MODULE_DEVICE_TABLE(of, cs42l51_of_match);
Thomas Petazzoni2cb1e022014-11-12 15:40:44 +0100807EXPORT_SYMBOL_GPL(cs42l51_of_match);
808
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +0200809MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200810MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver");
811MODULE_LICENSE("GPL");