blob: 9b3ffa16b20454a303c1df223037255fe64e7617 [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;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020059};
60
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020061#define CS42L51_FORMATS ( \
62 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
63 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
64 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
65 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
66
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020067static int cs42l51_get_chan_mix(struct snd_kcontrol *kcontrol,
68 struct snd_ctl_elem_value *ucontrol)
69{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +000070 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
71 unsigned long value = snd_soc_component_read32(component, CS42L51_PCM_MIXER)&3;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020072
73 switch (value) {
74 default:
75 case 0:
Takashi Iwai89300b42016-02-29 18:02:59 +010076 ucontrol->value.enumerated.item[0] = 0;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020077 break;
78 /* same value : (L+R)/2 and (R+L)/2 */
79 case 1:
80 case 2:
Takashi Iwai89300b42016-02-29 18:02:59 +010081 ucontrol->value.enumerated.item[0] = 1;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020082 break;
83 case 3:
Takashi Iwai89300b42016-02-29 18:02:59 +010084 ucontrol->value.enumerated.item[0] = 2;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020085 break;
86 }
87
88 return 0;
89}
90
91#define CHAN_MIX_NORMAL 0x00
92#define CHAN_MIX_BOTH 0x55
93#define CHAN_MIX_SWAP 0xFF
94
95static int cs42l51_set_chan_mix(struct snd_kcontrol *kcontrol,
96 struct snd_ctl_elem_value *ucontrol)
97{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +000098 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020099 unsigned char val;
100
Takashi Iwai89300b42016-02-29 18:02:59 +0100101 switch (ucontrol->value.enumerated.item[0]) {
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200102 default:
103 case 0:
104 val = CHAN_MIX_NORMAL;
105 break;
106 case 1:
107 val = CHAN_MIX_BOTH;
108 break;
109 case 2:
110 val = CHAN_MIX_SWAP;
111 break;
112 }
113
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000114 snd_soc_component_write(component, CS42L51_PCM_MIXER, val);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200115
116 return 1;
117}
118
119static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0);
120static const DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0);
Brian Austin7272e052014-03-19 10:40:02 -0500121
122static const DECLARE_TLV_DB_SCALE(aout_tlv, -10200, 50, 0);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200123
124static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0);
125static const char *chan_mix[] = {
126 "L R",
127 "L+R",
128 "R L",
129};
130
Takashi Iwai6109ab22014-02-18 10:44:57 +0100131static SOC_ENUM_SINGLE_EXT_DECL(cs42l51_chan_mix, chan_mix);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200132
133static const struct snd_kcontrol_new cs42l51_snd_controls[] = {
134 SOC_DOUBLE_R_SX_TLV("PCM Playback Volume",
135 CS42L51_PCMA_VOL, CS42L51_PCMB_VOL,
Brian Austin7272e052014-03-19 10:40:02 -0500136 0, 0x19, 0x7F, adc_pcm_tlv),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200137 SOC_DOUBLE_R("PCM Playback Switch",
138 CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, 7, 1, 1),
139 SOC_DOUBLE_R_SX_TLV("Analog Playback Volume",
140 CS42L51_AOUTA_VOL, CS42L51_AOUTB_VOL,
Brian Austin1d99f242012-03-30 10:43:55 -0500141 0, 0x34, 0xE4, aout_tlv),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200142 SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume",
143 CS42L51_ADCA_VOL, CS42L51_ADCB_VOL,
Brian Austin7272e052014-03-19 10:40:02 -0500144 0, 0x19, 0x7F, adc_pcm_tlv),
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200145 SOC_DOUBLE_R("ADC Mixer Switch",
146 CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1),
147 SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0),
148 SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0),
149 SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0),
150 SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0),
151 SOC_DOUBLE_TLV("Mic Boost Volume",
152 CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv),
153 SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv),
154 SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv),
155 SOC_ENUM_EXT("PCM channel mixer",
156 cs42l51_chan_mix,
157 cs42l51_get_chan_mix, cs42l51_set_chan_mix),
158};
159
160/*
161 * to power down, one must:
162 * 1.) Enable the PDN bit
163 * 2.) enable power-down for the select channels
164 * 3.) disable the PDN bit.
165 */
166static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w,
167 struct snd_kcontrol *kcontrol, int event)
168{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000169 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
Lars-Peter Clausenceb3c062014-11-20 21:05:38 +0100170
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200171 switch (event) {
172 case SND_SOC_DAPM_PRE_PMD:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000173 snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
Axel Line94de1e2011-11-01 15:17:57 +0800174 CS42L51_POWER_CTL1_PDN,
175 CS42L51_POWER_CTL1_PDN);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200176 break;
177 default:
178 case SND_SOC_DAPM_POST_PMD:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000179 snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
Axel Line94de1e2011-11-01 15:17:57 +0800180 CS42L51_POWER_CTL1_PDN, 0);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200181 break;
182 }
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200183
184 return 0;
185}
186
187static const char *cs42l51_dac_names[] = {"Direct PCM",
188 "DSP PCM", "ADC"};
Takashi Iwai6109ab22014-02-18 10:44:57 +0100189static SOC_ENUM_SINGLE_DECL(cs42l51_dac_mux_enum,
190 CS42L51_DAC_CTL, 6, cs42l51_dac_names);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200191static const struct snd_kcontrol_new cs42l51_dac_mux_controls =
192 SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum);
193
194static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left",
195 "MIC Left", "MIC+preamp Left"};
Takashi Iwai6109ab22014-02-18 10:44:57 +0100196static SOC_ENUM_SINGLE_DECL(cs42l51_adcl_mux_enum,
197 CS42L51_ADC_INPUT, 4, cs42l51_adcl_names);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200198static const struct snd_kcontrol_new cs42l51_adcl_mux_controls =
199 SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum);
200
201static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right",
202 "MIC Right", "MIC+preamp Right"};
Takashi Iwai6109ab22014-02-18 10:44:57 +0100203static SOC_ENUM_SINGLE_DECL(cs42l51_adcr_mux_enum,
204 CS42L51_ADC_INPUT, 6, cs42l51_adcr_names);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200205static const struct snd_kcontrol_new cs42l51_adcr_mux_controls =
206 SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum);
207
208static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = {
209 SND_SOC_DAPM_MICBIAS("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1),
210 SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0,
211 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
212 SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0,
213 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
214 SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture",
215 CS42L51_POWER_CTL1, 1, 1,
216 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
217 SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture",
218 CS42L51_POWER_CTL1, 2, 1,
219 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
220 SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback",
221 CS42L51_POWER_CTL1, 5, 1,
222 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
223 SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback",
224 CS42L51_POWER_CTL1, 6, 1,
225 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
226
227 /* analog/mic */
228 SND_SOC_DAPM_INPUT("AIN1L"),
229 SND_SOC_DAPM_INPUT("AIN1R"),
230 SND_SOC_DAPM_INPUT("AIN2L"),
231 SND_SOC_DAPM_INPUT("AIN2R"),
232 SND_SOC_DAPM_INPUT("MICL"),
233 SND_SOC_DAPM_INPUT("MICR"),
234
235 SND_SOC_DAPM_MIXER("Mic Preamp Left",
236 CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0),
237 SND_SOC_DAPM_MIXER("Mic Preamp Right",
238 CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0),
239
240 /* HP */
241 SND_SOC_DAPM_OUTPUT("HPL"),
242 SND_SOC_DAPM_OUTPUT("HPR"),
243
244 /* mux */
245 SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0,
246 &cs42l51_dac_mux_controls),
247 SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0,
248 &cs42l51_adcl_mux_controls),
249 SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0,
250 &cs42l51_adcr_mux_controls),
251};
252
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200253static const struct snd_soc_dapm_widget cs42l51_dapm_mclk_widgets[] = {
254 SND_SOC_DAPM_CLOCK_SUPPLY("MCLK")
255};
256
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200257static const struct snd_soc_dapm_route cs42l51_routes[] = {
258 {"HPL", NULL, "Left DAC"},
259 {"HPR", NULL, "Right DAC"},
260
261 {"Left ADC", NULL, "Left PGA"},
262 {"Right ADC", NULL, "Right PGA"},
263
264 {"Mic Preamp Left", NULL, "MICL"},
265 {"Mic Preamp Right", NULL, "MICR"},
266
267 {"PGA-ADC Mux Left", "AIN1 Left", "AIN1L" },
268 {"PGA-ADC Mux Left", "AIN2 Left", "AIN2L" },
269 {"PGA-ADC Mux Left", "MIC Left", "MICL" },
270 {"PGA-ADC Mux Left", "MIC+preamp Left", "Mic Preamp Left" },
271 {"PGA-ADC Mux Right", "AIN1 Right", "AIN1R" },
272 {"PGA-ADC Mux Right", "AIN2 Right", "AIN2R" },
273 {"PGA-ADC Mux Right", "MIC Right", "MICR" },
274 {"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" },
275
276 {"Left PGA", NULL, "PGA-ADC Mux Left"},
277 {"Right PGA", NULL, "PGA-ADC Mux Right"},
278};
279
280static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai,
281 unsigned int format)
282{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000283 struct snd_soc_component *component = codec_dai->component;
284 struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200285
286 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
287 case SND_SOC_DAIFMT_I2S:
288 case SND_SOC_DAIFMT_LEFT_J:
289 case SND_SOC_DAIFMT_RIGHT_J:
290 cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
291 break;
292 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000293 dev_err(component->dev, "invalid DAI format\n");
Axel Linac601552011-10-06 07:29:56 +0800294 return -EINVAL;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200295 }
296
297 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
298 case SND_SOC_DAIFMT_CBM_CFM:
299 cs42l51->func = MODE_MASTER;
300 break;
301 case SND_SOC_DAIFMT_CBS_CFS:
302 cs42l51->func = MODE_SLAVE_AUTO;
303 break;
304 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000305 dev_err(component->dev, "Unknown master/slave configuration\n");
Axel Linac601552011-10-06 07:29:56 +0800306 return -EINVAL;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200307 }
308
Axel Linac601552011-10-06 07:29:56 +0800309 return 0;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200310}
311
312struct cs42l51_ratios {
313 unsigned int ratio;
314 unsigned char speed_mode;
315 unsigned char mclk;
316};
317
318static struct cs42l51_ratios slave_ratios[] = {
319 { 512, CS42L51_QSM_MODE, 0 }, { 768, CS42L51_QSM_MODE, 0 },
320 { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
321 { 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 },
322 { 256, CS42L51_HSM_MODE, 0 }, { 384, CS42L51_HSM_MODE, 0 },
323 { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
324 { 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 },
325 { 128, CS42L51_SSM_MODE, 0 }, { 192, CS42L51_SSM_MODE, 0 },
326 { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
327 { 512, CS42L51_SSM_MODE, 0 }, { 768, CS42L51_SSM_MODE, 0 },
328 { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
329 { 256, CS42L51_DSM_MODE, 0 }, { 384, CS42L51_DSM_MODE, 0 },
330};
331
332static struct cs42l51_ratios slave_auto_ratios[] = {
333 { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
334 { 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 },
335 { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
336 { 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 },
337 { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
338 { 512, CS42L51_SSM_MODE, 1 }, { 768, CS42L51_SSM_MODE, 1 },
339 { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
340 { 256, CS42L51_DSM_MODE, 1 }, { 384, CS42L51_DSM_MODE, 1 },
341};
342
343static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai,
344 int clk_id, unsigned int freq, int dir)
345{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000346 struct snd_soc_component *component = codec_dai->component;
347 struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200348
349 cs42l51->mclk = freq;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200350 return 0;
351}
352
353static int cs42l51_hw_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *params,
355 struct snd_soc_dai *dai)
356{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000357 struct snd_soc_component *component = dai->component;
358 struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200359 int ret;
360 unsigned int i;
361 unsigned int rate;
362 unsigned int ratio;
363 struct cs42l51_ratios *ratios = NULL;
364 int nr_ratios = 0;
365 int intf_ctl, power_ctl, fmt;
366
367 switch (cs42l51->func) {
368 case MODE_MASTER:
369 return -EINVAL;
370 case MODE_SLAVE:
371 ratios = slave_ratios;
372 nr_ratios = ARRAY_SIZE(slave_ratios);
373 break;
374 case MODE_SLAVE_AUTO:
375 ratios = slave_auto_ratios;
376 nr_ratios = ARRAY_SIZE(slave_auto_ratios);
377 break;
378 }
379
380 /* Figure out which MCLK/LRCK ratio to use */
381 rate = params_rate(params); /* Sampling rate, in Hz */
382 ratio = cs42l51->mclk / rate; /* MCLK/LRCK ratio */
383 for (i = 0; i < nr_ratios; i++) {
384 if (ratios[i].ratio == ratio)
385 break;
386 }
387
388 if (i == nr_ratios) {
389 /* We did not find a matching ratio */
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000390 dev_err(component->dev, "could not find matching ratio\n");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200391 return -EINVAL;
392 }
393
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000394 intf_ctl = snd_soc_component_read32(component, CS42L51_INTF_CTL);
395 power_ctl = snd_soc_component_read32(component, CS42L51_MIC_POWER_CTL);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200396
397 intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S
398 | CS42L51_INTF_CTL_DAC_FORMAT(7));
399 power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3)
400 | CS42L51_MIC_POWER_CTL_MCLK_DIV2);
401
402 switch (cs42l51->func) {
403 case MODE_MASTER:
404 intf_ctl |= CS42L51_INTF_CTL_MASTER;
405 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
406 break;
407 case MODE_SLAVE:
408 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
409 break;
410 case MODE_SLAVE_AUTO:
411 power_ctl |= CS42L51_MIC_POWER_CTL_AUTO;
412 break;
413 }
414
415 switch (cs42l51->audio_mode) {
416 case SND_SOC_DAIFMT_I2S:
417 intf_ctl |= CS42L51_INTF_CTL_ADC_I2S;
418 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S);
419 break;
420 case SND_SOC_DAIFMT_LEFT_J:
421 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24);
422 break;
423 case SND_SOC_DAIFMT_RIGHT_J:
Mark Brown1b6b0df2014-01-08 19:48:20 +0000424 switch (params_width(params)) {
425 case 16:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200426 fmt = CS42L51_DAC_DIF_RJ16;
427 break;
Mark Brown1b6b0df2014-01-08 19:48:20 +0000428 case 18:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200429 fmt = CS42L51_DAC_DIF_RJ18;
430 break;
Mark Brown1b6b0df2014-01-08 19:48:20 +0000431 case 20:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200432 fmt = CS42L51_DAC_DIF_RJ20;
433 break;
Mark Brown1b6b0df2014-01-08 19:48:20 +0000434 case 24:
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200435 fmt = CS42L51_DAC_DIF_RJ24;
436 break;
437 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000438 dev_err(component->dev, "unknown format\n");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200439 return -EINVAL;
440 }
441 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt);
442 break;
443 default:
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000444 dev_err(component->dev, "unknown format\n");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200445 return -EINVAL;
446 }
447
448 if (ratios[i].mclk)
449 power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2;
450
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000451 ret = snd_soc_component_write(component, CS42L51_INTF_CTL, intf_ctl);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200452 if (ret < 0)
453 return ret;
454
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000455 ret = snd_soc_component_write(component, CS42L51_MIC_POWER_CTL, power_ctl);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200456 if (ret < 0)
457 return ret;
458
459 return 0;
460}
461
462static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute)
463{
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000464 struct snd_soc_component *component = dai->component;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200465 int reg;
466 int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE;
467
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000468 reg = snd_soc_component_read32(component, CS42L51_DAC_OUT_CTL);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200469
470 if (mute)
471 reg |= mask;
472 else
473 reg &= ~mask;
474
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000475 return snd_soc_component_write(component, CS42L51_DAC_OUT_CTL, reg);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200476}
477
Olivier Moysanad6bb302019-03-29 16:37:37 +0100478static int cs42l51_of_xlate_dai_id(struct snd_soc_component *component,
479 struct device_node *endpoint)
480{
481 /* return dai id 0, whatever the endpoint index */
482 return 0;
483}
484
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100485static const struct snd_soc_dai_ops cs42l51_dai_ops = {
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200486 .hw_params = cs42l51_hw_params,
487 .set_sysclk = cs42l51_set_dai_sysclk,
488 .set_fmt = cs42l51_set_dai_fmt,
489 .digital_mute = cs42l51_dai_mute,
490};
491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492static struct snd_soc_dai_driver cs42l51_dai = {
493 .name = "cs42l51-hifi",
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200494 .playback = {
495 .stream_name = "Playback",
496 .channels_min = 1,
497 .channels_max = 2,
498 .rates = SNDRV_PCM_RATE_8000_96000,
499 .formats = CS42L51_FORMATS,
500 },
501 .capture = {
502 .stream_name = "Capture",
503 .channels_min = 1,
504 .channels_max = 2,
505 .rates = SNDRV_PCM_RATE_8000_96000,
506 .formats = CS42L51_FORMATS,
507 },
508 .ops = &cs42l51_dai_ops,
509};
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200510
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000511static int cs42l51_component_probe(struct snd_soc_component *component)
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200512{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 int ret, reg;
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200514 struct snd_soc_dapm_context *dapm;
Olivier Moysan318e7412018-10-19 17:56:35 +0200515 struct cs42l51_private *cs42l51;
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200516
Olivier Moysan318e7412018-10-19 17:56:35 +0200517 cs42l51 = snd_soc_component_get_drvdata(component);
Olivier Moysan5e8d63a2018-10-15 16:03:36 +0200518 dapm = snd_soc_component_get_dapm(component);
Olivier Moysan318e7412018-10-19 17:56:35 +0200519
520 if (cs42l51->mclk_handle)
521 snd_soc_dapm_new_controls(dapm, cs42l51_dapm_mclk_widgets, 1);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200522
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 /*
524 * DAC configuration
525 * - Use signal processor
526 * - auto mute
527 * - vol changes immediate
528 * - no de-emphasize
529 */
530 reg = CS42L51_DAC_CTL_DATA_SEL(1)
531 | CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0);
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000532 ret = snd_soc_component_write(component, CS42L51_DAC_CTL, reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 if (ret < 0)
534 return ret;
535
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200536 return 0;
537}
538
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000539static const struct snd_soc_component_driver soc_component_device_cs42l51 = {
540 .probe = cs42l51_component_probe,
541 .controls = cs42l51_snd_controls,
542 .num_controls = ARRAY_SIZE(cs42l51_snd_controls),
543 .dapm_widgets = cs42l51_dapm_widgets,
544 .num_dapm_widgets = ARRAY_SIZE(cs42l51_dapm_widgets),
545 .dapm_routes = cs42l51_routes,
546 .num_dapm_routes = ARRAY_SIZE(cs42l51_routes),
Olivier Moysanad6bb302019-03-29 16:37:37 +0100547 .of_xlate_dai_id = cs42l51_of_xlate_dai_id,
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000548 .idle_bias_on = 1,
549 .use_pmdown_time = 1,
550 .endianness = 1,
551 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552};
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200553
Brian Austina1253ef2014-04-15 15:49:33 -0500554const struct regmap_config cs42l51_regmap = {
Mark Brownda071482014-02-11 19:24:31 +0000555 .max_register = CS42L51_CHARGE_FREQ,
556 .cache_type = REGCACHE_RBTREE,
557};
Brian Austina1253ef2014-04-15 15:49:33 -0500558EXPORT_SYMBOL_GPL(cs42l51_regmap);
Mark Brownda071482014-02-11 19:24:31 +0000559
Brian Austina1253ef2014-04-15 15:49:33 -0500560int cs42l51_probe(struct device *dev, struct regmap *regmap)
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200561{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 struct cs42l51_private *cs42l51;
Mark Brownda071482014-02-11 19:24:31 +0000563 unsigned int val;
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200564 int ret, i;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200565
Brian Austina1253ef2014-04-15 15:49:33 -0500566 if (IS_ERR(regmap))
567 return PTR_ERR(regmap);
568
569 cs42l51 = devm_kzalloc(dev, sizeof(struct cs42l51_private),
570 GFP_KERNEL);
571 if (!cs42l51)
572 return -ENOMEM;
573
574 dev_set_drvdata(dev, cs42l51);
Mark Brownda071482014-02-11 19:24:31 +0000575
Olivier Moysan318e7412018-10-19 17:56:35 +0200576 cs42l51->mclk_handle = devm_clk_get(dev, "MCLK");
577 if (IS_ERR(cs42l51->mclk_handle)) {
578 if (PTR_ERR(cs42l51->mclk_handle) != -ENOENT)
579 return PTR_ERR(cs42l51->mclk_handle);
580 cs42l51->mclk_handle = NULL;
581 }
582
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200583 for (i = 0; i < ARRAY_SIZE(cs42l51->supplies); i++)
584 cs42l51->supplies[i].supply = cs42l51_supply_names[i];
585
586 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(cs42l51->supplies),
587 cs42l51->supplies);
588 if (ret != 0) {
589 dev_err(dev, "Failed to request supplies: %d\n", ret);
590 return ret;
591 }
592
593 ret = regulator_bulk_enable(ARRAY_SIZE(cs42l51->supplies),
594 cs42l51->supplies);
595 if (ret != 0) {
596 dev_err(dev, "Failed to enable supplies: %d\n", ret);
597 return ret;
598 }
599
Olivier Moysan11b9cd72019-04-03 15:23:33 +0200600 cs42l51->reset_gpio = devm_gpiod_get_optional(dev, "reset",
601 GPIOD_OUT_LOW);
602 if (IS_ERR(cs42l51->reset_gpio))
603 return PTR_ERR(cs42l51->reset_gpio);
604
605 if (cs42l51->reset_gpio) {
606 dev_dbg(dev, "Release reset gpio\n");
607 gpiod_set_value_cansleep(cs42l51->reset_gpio, 0);
608 mdelay(2);
609 }
610
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611 /* Verify that we have a CS42L51 */
Mark Brownda071482014-02-11 19:24:31 +0000612 ret = regmap_read(regmap, CS42L51_CHIP_REV_ID, &val);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 if (ret < 0) {
Brian Austina1253ef2014-04-15 15:49:33 -0500614 dev_err(dev, "failed to read I2C\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 goto error;
616 }
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200617
Mark Brownda071482014-02-11 19:24:31 +0000618 if ((val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) &&
619 (val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) {
Brian Austina1253ef2014-04-15 15:49:33 -0500620 dev_err(dev, "Invalid chip id: %x\n", val);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 ret = -ENODEV;
622 goto error;
623 }
Axel Lin1025c052014-04-17 16:35:43 +0800624 dev_info(dev, "Cirrus Logic CS42L51, Revision: %02X\n",
625 val & CS42L51_CHIP_REV_MASK);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626
Kuninori Morimoto1fa49852018-01-29 03:59:02 +0000627 ret = devm_snd_soc_register_component(dev,
628 &soc_component_device_cs42l51, &cs42l51_dai, 1);
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200629 if (ret < 0)
630 goto error;
631
632 return 0;
633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634error:
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200635 regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies),
636 cs42l51->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 return ret;
638}
Brian Austina1253ef2014-04-15 15:49:33 -0500639EXPORT_SYMBOL_GPL(cs42l51_probe);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200641int cs42l51_remove(struct device *dev)
642{
643 struct cs42l51_private *cs42l51 = dev_get_drvdata(dev);
644
Olivier Moysan11b9cd72019-04-03 15:23:33 +0200645 gpiod_set_value_cansleep(cs42l51->reset_gpio, 1);
646
Olivier Moysanf77b6ea2019-04-03 15:23:32 +0200647 return regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies),
648 cs42l51->supplies);
649}
650EXPORT_SYMBOL_GPL(cs42l51_remove);
651
Thomas Petazzoni2cb1e022014-11-12 15:40:44 +0100652const struct of_device_id cs42l51_of_match[] = {
Thomas Petazzonidfd72a62014-01-30 18:14:05 +0100653 { .compatible = "cirrus,cs42l51", },
654 { }
655};
656MODULE_DEVICE_TABLE(of, cs42l51_of_match);
Thomas Petazzoni2cb1e022014-11-12 15:40:44 +0100657EXPORT_SYMBOL_GPL(cs42l51_of_match);
658
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +0200659MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200660MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver");
661MODULE_LICENSE("GPL");