Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * cs42l51.c
  4 *
  5 * ASoC Driver for Cirrus Logic CS42L51 codecs
  6 *
  7 * Copyright (c) 2010 Arnaud Patard <apatard@mandriva.com>
  8 *
  9 * Based on cs4270.c - Copyright (c) Freescale Semiconductor
 10 *
 
 
 
 
 
 
 
 
 
 11 * For now:
 12 *  - Only I2C is support. Not SPI
 13 *  - master mode *NOT* supported
 14 */
 15
 16#include <linux/clk.h>
 17#include <linux/module.h>
 18#include <linux/slab.h>
 19#include <sound/core.h>
 20#include <sound/soc.h>
 21#include <sound/tlv.h>
 22#include <sound/initval.h>
 23#include <sound/pcm_params.h>
 24#include <sound/pcm.h>
 25#include <linux/gpio/consumer.h>
 26#include <linux/regmap.h>
 27#include <linux/regulator/consumer.h>
 28
 29#include "cs42l51.h"
 30
 31enum master_slave_mode {
 32	MODE_SLAVE,
 33	MODE_SLAVE_AUTO,
 34	MODE_MASTER,
 35};
 36
 37static const char * const cs42l51_supply_names[] = {
 38	"VL",
 39	"VD",
 40	"VA",
 41	"VAHP",
 42};
 43
 44struct cs42l51_private {
 45	unsigned int mclk;
 46	struct clk *mclk_handle;
 47	unsigned int audio_mode;	/* The mode (I2S or left-justified) */
 48	enum master_slave_mode func;
 49	struct regulator_bulk_data supplies[ARRAY_SIZE(cs42l51_supply_names)];
 50	struct gpio_desc *reset_gpio;
 51	struct regmap *regmap;
 52};
 53
 54#define CS42L51_FORMATS ( \
 55		SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
 56		SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
 57		SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
 58		SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
 59
 60static int cs42l51_get_chan_mix(struct snd_kcontrol *kcontrol,
 61			struct snd_ctl_elem_value *ucontrol)
 62{
 63	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
 64	unsigned long value = snd_soc_component_read(component, CS42L51_PCM_MIXER)&3;
 65
 66	switch (value) {
 67	default:
 68	case 0:
 69		ucontrol->value.enumerated.item[0] = 0;
 70		break;
 71	/* same value : (L+R)/2 and (R+L)/2 */
 72	case 1:
 73	case 2:
 74		ucontrol->value.enumerated.item[0] = 1;
 75		break;
 76	case 3:
 77		ucontrol->value.enumerated.item[0] = 2;
 78		break;
 79	}
 80
 81	return 0;
 82}
 83
 84#define CHAN_MIX_NORMAL	0x00
 85#define CHAN_MIX_BOTH	0x55
 86#define CHAN_MIX_SWAP	0xFF
 87
 88static int cs42l51_set_chan_mix(struct snd_kcontrol *kcontrol,
 89			struct snd_ctl_elem_value *ucontrol)
 90{
 91	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
 92	unsigned char val;
 93
 94	switch (ucontrol->value.enumerated.item[0]) {
 95	default:
 96	case 0:
 97		val = CHAN_MIX_NORMAL;
 98		break;
 99	case 1:
100		val = CHAN_MIX_BOTH;
101		break;
102	case 2:
103		val = CHAN_MIX_SWAP;
104		break;
105	}
106
107	snd_soc_component_write(component, CS42L51_PCM_MIXER, val);
108
109	return 1;
110}
111
112static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0);
113static const DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0);
114
115static const DECLARE_TLV_DB_SCALE(aout_tlv, -10200, 50, 0);
116
117static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0);
118static const DECLARE_TLV_DB_SCALE(adc_boost_tlv, 2000, 2000, 0);
119static const char *chan_mix[] = {
120	"L R",
121	"L+R",
122	"R L",
123};
124
125static SOC_ENUM_SINGLE_EXT_DECL(cs42l51_chan_mix, chan_mix);
126
127static const struct snd_kcontrol_new cs42l51_snd_controls[] = {
128	SOC_DOUBLE_R_SX_TLV("PCM Playback Volume",
129			CS42L51_PCMA_VOL, CS42L51_PCMB_VOL,
130			0, 0x19, 0x7F, adc_pcm_tlv),
131	SOC_DOUBLE_R("PCM Playback Switch",
132			CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, 7, 1, 1),
133	SOC_DOUBLE_R_SX_TLV("Analog Playback Volume",
134			CS42L51_AOUTA_VOL, CS42L51_AOUTB_VOL,
135			0, 0x34, 0xE4, aout_tlv),
136	SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume",
137			CS42L51_ADCA_VOL, CS42L51_ADCB_VOL,
138			0, 0x19, 0x7F, adc_pcm_tlv),
139	SOC_DOUBLE_R("ADC Mixer Switch",
140			CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1),
141	SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0),
142	SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0),
143	SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0),
144	SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0),
145	SOC_DOUBLE_TLV("Mic Boost Volume",
146			CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv),
147	SOC_DOUBLE_TLV("ADC Boost Volume",
148		       CS42L51_MIC_CTL, 5, 6, 1, 0, adc_boost_tlv),
149	SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv),
150	SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv),
151	SOC_ENUM_EXT("PCM channel mixer",
152			cs42l51_chan_mix,
153			cs42l51_get_chan_mix, cs42l51_set_chan_mix),
154};
155
156/*
157 * to power down, one must:
158 * 1.) Enable the PDN bit
159 * 2.) enable power-down for the select channels
160 * 3.) disable the PDN bit.
161 */
162static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w,
163		struct snd_kcontrol *kcontrol, int event)
164{
165	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
166
167	switch (event) {
168	case SND_SOC_DAPM_PRE_PMD:
169		snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
170				    CS42L51_POWER_CTL1_PDN,
171				    CS42L51_POWER_CTL1_PDN);
172		break;
173	default:
174	case SND_SOC_DAPM_POST_PMD:
175		snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
176				    CS42L51_POWER_CTL1_PDN, 0);
177		break;
178	}
179
180	return 0;
181}
182
183static const char *cs42l51_dac_names[] = {"Direct PCM",
184	"DSP PCM", "ADC"};
185static SOC_ENUM_SINGLE_DECL(cs42l51_dac_mux_enum,
186			    CS42L51_DAC_CTL, 6, cs42l51_dac_names);
187static const struct snd_kcontrol_new cs42l51_dac_mux_controls =
188	SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum);
189
190static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left",
191	"MIC Left", "MIC+preamp Left"};
192static SOC_ENUM_SINGLE_DECL(cs42l51_adcl_mux_enum,
193			    CS42L51_ADC_INPUT, 4, cs42l51_adcl_names);
194static const struct snd_kcontrol_new cs42l51_adcl_mux_controls =
195	SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum);
196
197static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right",
198	"MIC Right", "MIC+preamp Right"};
199static SOC_ENUM_SINGLE_DECL(cs42l51_adcr_mux_enum,
200			    CS42L51_ADC_INPUT, 6, cs42l51_adcr_names);
201static const struct snd_kcontrol_new cs42l51_adcr_mux_controls =
202	SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum);
203
204static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = {
205	SND_SOC_DAPM_SUPPLY("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1, NULL,
206			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
207	SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0,
208		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
209	SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0,
210		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
211	SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture",
212		CS42L51_POWER_CTL1, 1, 1,
213		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
214	SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture",
215		CS42L51_POWER_CTL1, 2, 1,
216		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
217	SND_SOC_DAPM_DAC_E("Left DAC", NULL, CS42L51_POWER_CTL1, 5, 1,
218			   cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
219	SND_SOC_DAPM_DAC_E("Right DAC", NULL, CS42L51_POWER_CTL1, 6, 1,
220			   cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
 
 
221
222	/* analog/mic */
223	SND_SOC_DAPM_INPUT("AIN1L"),
224	SND_SOC_DAPM_INPUT("AIN1R"),
225	SND_SOC_DAPM_INPUT("AIN2L"),
226	SND_SOC_DAPM_INPUT("AIN2R"),
227	SND_SOC_DAPM_INPUT("MICL"),
228	SND_SOC_DAPM_INPUT("MICR"),
229
230	SND_SOC_DAPM_MIXER("Mic Preamp Left",
231		CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0),
232	SND_SOC_DAPM_MIXER("Mic Preamp Right",
233		CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0),
234
235	/* HP */
236	SND_SOC_DAPM_OUTPUT("HPL"),
237	SND_SOC_DAPM_OUTPUT("HPR"),
238
239	/* mux */
240	SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0,
241		&cs42l51_dac_mux_controls),
242	SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0,
243		&cs42l51_adcl_mux_controls),
244	SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0,
245		&cs42l51_adcr_mux_controls),
246};
247
248static const struct snd_soc_dapm_widget cs42l51_dapm_mclk_widgets[] = {
249	SND_SOC_DAPM_CLOCK_SUPPLY("MCLK")
250};
251
252static const struct snd_soc_dapm_route cs42l51_routes[] = {
253	{"HPL", NULL, "Left DAC"},
254	{"HPR", NULL, "Right DAC"},
255
256	{"Right DAC", NULL, "DAC Mux"},
257	{"Left DAC", NULL, "DAC Mux"},
258
259	{"DAC Mux", "Direct PCM", "Playback"},
260	{"DAC Mux", "DSP PCM", "Playback"},
261
262	{"Left ADC", NULL, "Left PGA"},
263	{"Right ADC", NULL, "Right PGA"},
264
265	{"Mic Preamp Left",  NULL,  "MICL"},
266	{"Mic Preamp Right", NULL,  "MICR"},
267
268	{"PGA-ADC Mux Left",  "AIN1 Left",        "AIN1L" },
269	{"PGA-ADC Mux Left",  "AIN2 Left",        "AIN2L" },
270	{"PGA-ADC Mux Left",  "MIC Left",         "MICL"  },
271	{"PGA-ADC Mux Left",  "MIC+preamp Left",  "Mic Preamp Left" },
272	{"PGA-ADC Mux Right", "AIN1 Right",       "AIN1R" },
273	{"PGA-ADC Mux Right", "AIN2 Right",       "AIN2R" },
274	{"PGA-ADC Mux Right", "MIC Right",        "MICR" },
275	{"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" },
276
277	{"Left PGA", NULL, "PGA-ADC Mux Left"},
278	{"Right PGA", NULL, "PGA-ADC Mux Right"},
279};
280
281static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai,
282		unsigned int format)
283{
284	struct snd_soc_component *component = codec_dai->component;
285	struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
286
287	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
288	case SND_SOC_DAIFMT_I2S:
289	case SND_SOC_DAIFMT_LEFT_J:
290	case SND_SOC_DAIFMT_RIGHT_J:
291		cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
292		break;
293	default:
294		dev_err(component->dev, "invalid DAI format\n");
295		return -EINVAL;
296	}
297
298	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
299	case SND_SOC_DAIFMT_CBM_CFM:
300		cs42l51->func = MODE_MASTER;
301		break;
302	case SND_SOC_DAIFMT_CBS_CFS:
303		cs42l51->func = MODE_SLAVE_AUTO;
304		break;
305	default:
306		dev_err(component->dev, "Unknown master/slave configuration\n");
307		return -EINVAL;
308	}
309
310	return 0;
311}
312
313struct cs42l51_ratios {
314	unsigned int ratio;
315	unsigned char speed_mode;
316	unsigned char mclk;
317};
318
319static struct cs42l51_ratios slave_ratios[] = {
320	{  512, CS42L51_QSM_MODE, 0 }, {  768, CS42L51_QSM_MODE, 0 },
321	{ 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
322	{ 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 },
323	{  256, CS42L51_HSM_MODE, 0 }, {  384, CS42L51_HSM_MODE, 0 },
324	{  512, CS42L51_HSM_MODE, 0 }, {  768, CS42L51_HSM_MODE, 0 },
325	{ 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 },
326	{  128, CS42L51_SSM_MODE, 0 }, {  192, CS42L51_SSM_MODE, 0 },
327	{  256, CS42L51_SSM_MODE, 0 }, {  384, CS42L51_SSM_MODE, 0 },
328	{  512, CS42L51_SSM_MODE, 0 }, {  768, CS42L51_SSM_MODE, 0 },
329	{  128, CS42L51_DSM_MODE, 0 }, {  192, CS42L51_DSM_MODE, 0 },
330	{  256, CS42L51_DSM_MODE, 0 }, {  384, CS42L51_DSM_MODE, 0 },
331};
332
333static struct cs42l51_ratios slave_auto_ratios[] = {
334	{ 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
335	{ 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 },
336	{  512, CS42L51_HSM_MODE, 0 }, {  768, CS42L51_HSM_MODE, 0 },
337	{ 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 },
338	{  256, CS42L51_SSM_MODE, 0 }, {  384, CS42L51_SSM_MODE, 0 },
339	{  512, CS42L51_SSM_MODE, 1 }, {  768, CS42L51_SSM_MODE, 1 },
340	{  128, CS42L51_DSM_MODE, 0 }, {  192, CS42L51_DSM_MODE, 0 },
341	{  256, CS42L51_DSM_MODE, 1 }, {  384, CS42L51_DSM_MODE, 1 },
342};
343
344/*
345 * Master mode mclk/fs ratios.
346 * Recommended configurations are SSM for 4-50khz and DSM for 50-100kHz ranges
347 * The table below provides support of following ratios:
348 * 128: SSM (%128) with div2 disabled
349 * 256: SSM (%128) with div2 enabled
350 * In both cases, if sampling rate is above 50kHz, SSM is overridden
351 * with DSM (%128) configuration
352 */
353static struct cs42l51_ratios master_ratios[] = {
354	{ 128, CS42L51_SSM_MODE, 0 }, { 256, CS42L51_SSM_MODE, 1 },
355};
356
357static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai,
358		int clk_id, unsigned int freq, int dir)
359{
360	struct snd_soc_component *component = codec_dai->component;
361	struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
362
363	cs42l51->mclk = freq;
364	return 0;
365}
366
367static int cs42l51_hw_params(struct snd_pcm_substream *substream,
368		struct snd_pcm_hw_params *params,
369		struct snd_soc_dai *dai)
370{
371	struct snd_soc_component *component = dai->component;
372	struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
373	int ret;
374	unsigned int i;
375	unsigned int rate;
376	unsigned int ratio;
377	struct cs42l51_ratios *ratios = NULL;
378	int nr_ratios = 0;
379	int intf_ctl, power_ctl, fmt, mode;
380
381	switch (cs42l51->func) {
382	case MODE_MASTER:
383		ratios = master_ratios;
384		nr_ratios = ARRAY_SIZE(master_ratios);
385		break;
386	case MODE_SLAVE:
387		ratios = slave_ratios;
388		nr_ratios = ARRAY_SIZE(slave_ratios);
389		break;
390	case MODE_SLAVE_AUTO:
391		ratios = slave_auto_ratios;
392		nr_ratios = ARRAY_SIZE(slave_auto_ratios);
393		break;
394	}
395
396	/* Figure out which MCLK/LRCK ratio to use */
397	rate = params_rate(params);     /* Sampling rate, in Hz */
398	ratio = cs42l51->mclk / rate;    /* MCLK/LRCK ratio */
399	for (i = 0; i < nr_ratios; i++) {
400		if (ratios[i].ratio == ratio)
401			break;
402	}
403
404	if (i == nr_ratios) {
405		/* We did not find a matching ratio */
406		dev_err(component->dev, "could not find matching ratio\n");
407		return -EINVAL;
408	}
409
410	intf_ctl = snd_soc_component_read(component, CS42L51_INTF_CTL);
411	power_ctl = snd_soc_component_read(component, CS42L51_MIC_POWER_CTL);
412
413	intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S
414			| CS42L51_INTF_CTL_DAC_FORMAT(7));
415	power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3)
416			| CS42L51_MIC_POWER_CTL_MCLK_DIV2);
417
418	switch (cs42l51->func) {
419	case MODE_MASTER:
420		intf_ctl |= CS42L51_INTF_CTL_MASTER;
421		mode = ratios[i].speed_mode;
422		/* Force DSM mode if sampling rate is above 50kHz */
423		if (rate > 50000)
424			mode = CS42L51_DSM_MODE;
425		power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(mode);
426		/*
427		 * Auto detect mode is not applicable for master mode and has to
428		 * be disabled. Otherwise SPEED[1:0] bits will be ignored.
429		 */
430		power_ctl &= ~CS42L51_MIC_POWER_CTL_AUTO;
431		break;
432	case MODE_SLAVE:
433		power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
434		break;
435	case MODE_SLAVE_AUTO:
436		power_ctl |= CS42L51_MIC_POWER_CTL_AUTO;
437		break;
438	}
439
440	switch (cs42l51->audio_mode) {
441	case SND_SOC_DAIFMT_I2S:
442		intf_ctl |= CS42L51_INTF_CTL_ADC_I2S;
443		intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S);
444		break;
445	case SND_SOC_DAIFMT_LEFT_J:
446		intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24);
447		break;
448	case SND_SOC_DAIFMT_RIGHT_J:
449		switch (params_width(params)) {
450		case 16:
451			fmt = CS42L51_DAC_DIF_RJ16;
452			break;
453		case 18:
454			fmt = CS42L51_DAC_DIF_RJ18;
455			break;
456		case 20:
457			fmt = CS42L51_DAC_DIF_RJ20;
458			break;
459		case 24:
460			fmt = CS42L51_DAC_DIF_RJ24;
461			break;
462		default:
463			dev_err(component->dev, "unknown format\n");
464			return -EINVAL;
465		}
466		intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt);
467		break;
468	default:
469		dev_err(component->dev, "unknown format\n");
470		return -EINVAL;
471	}
472
473	if (ratios[i].mclk)
474		power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2;
475
476	ret = snd_soc_component_write(component, CS42L51_INTF_CTL, intf_ctl);
477	if (ret < 0)
478		return ret;
479
480	ret = snd_soc_component_write(component, CS42L51_MIC_POWER_CTL, power_ctl);
481	if (ret < 0)
482		return ret;
483
484	return 0;
485}
486
487static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute, int direction)
488{
489	struct snd_soc_component *component = dai->component;
490	int reg;
491	int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE;
492
493	reg = snd_soc_component_read(component, CS42L51_DAC_OUT_CTL);
494
495	if (mute)
496		reg |= mask;
497	else
498		reg &= ~mask;
499
500	return snd_soc_component_write(component, CS42L51_DAC_OUT_CTL, reg);
501}
502
503static int cs42l51_of_xlate_dai_id(struct snd_soc_component *component,
504				   struct device_node *endpoint)
505{
506	/* return dai id 0, whatever the endpoint index */
507	return 0;
508}
509
510static const struct snd_soc_dai_ops cs42l51_dai_ops = {
511	.hw_params      = cs42l51_hw_params,
512	.set_sysclk     = cs42l51_set_dai_sysclk,
513	.set_fmt        = cs42l51_set_dai_fmt,
514	.mute_stream    = cs42l51_dai_mute,
515	.no_capture_mute = 1,
516};
517
518static struct snd_soc_dai_driver cs42l51_dai = {
519	.name = "cs42l51-hifi",
520	.playback = {
521		.stream_name = "Playback",
522		.channels_min = 1,
523		.channels_max = 2,
524		.rates = SNDRV_PCM_RATE_8000_96000,
525		.formats = CS42L51_FORMATS,
526	},
527	.capture = {
528		.stream_name = "Capture",
529		.channels_min = 1,
530		.channels_max = 2,
531		.rates = SNDRV_PCM_RATE_8000_96000,
532		.formats = CS42L51_FORMATS,
533	},
534	.ops = &cs42l51_dai_ops,
535};
536
537static int cs42l51_component_probe(struct snd_soc_component *component)
538{
539	int ret, reg;
540	struct snd_soc_dapm_context *dapm;
541	struct cs42l51_private *cs42l51;
542
543	cs42l51 = snd_soc_component_get_drvdata(component);
544	dapm = snd_soc_component_get_dapm(component);
545
546	if (cs42l51->mclk_handle)
547		snd_soc_dapm_new_controls(dapm, cs42l51_dapm_mclk_widgets, 1);
548
549	/*
550	 * DAC configuration
551	 * - Use signal processor
552	 * - auto mute
553	 * - vol changes immediate
554	 * - no de-emphasize
555	 */
556	reg = CS42L51_DAC_CTL_DATA_SEL(1)
557		| CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0);
558	ret = snd_soc_component_write(component, CS42L51_DAC_CTL, reg);
559	if (ret < 0)
560		return ret;
561
562	return 0;
563}
564
565static const struct snd_soc_component_driver soc_component_device_cs42l51 = {
566	.probe			= cs42l51_component_probe,
567	.controls		= cs42l51_snd_controls,
568	.num_controls		= ARRAY_SIZE(cs42l51_snd_controls),
569	.dapm_widgets		= cs42l51_dapm_widgets,
570	.num_dapm_widgets	= ARRAY_SIZE(cs42l51_dapm_widgets),
571	.dapm_routes		= cs42l51_routes,
572	.num_dapm_routes	= ARRAY_SIZE(cs42l51_routes),
573	.of_xlate_dai_id	= cs42l51_of_xlate_dai_id,
574	.idle_bias_on		= 1,
575	.use_pmdown_time	= 1,
576	.endianness		= 1,
577	.non_legacy_dai_naming	= 1,
578};
579
580static bool cs42l51_writeable_reg(struct device *dev, unsigned int reg)
581{
582	switch (reg) {
583	case CS42L51_POWER_CTL1:
584	case CS42L51_MIC_POWER_CTL:
585	case CS42L51_INTF_CTL:
586	case CS42L51_MIC_CTL:
587	case CS42L51_ADC_CTL:
588	case CS42L51_ADC_INPUT:
589	case CS42L51_DAC_OUT_CTL:
590	case CS42L51_DAC_CTL:
591	case CS42L51_ALC_PGA_CTL:
592	case CS42L51_ALC_PGB_CTL:
593	case CS42L51_ADCA_ATT:
594	case CS42L51_ADCB_ATT:
595	case CS42L51_ADCA_VOL:
596	case CS42L51_ADCB_VOL:
597	case CS42L51_PCMA_VOL:
598	case CS42L51_PCMB_VOL:
599	case CS42L51_BEEP_FREQ:
600	case CS42L51_BEEP_VOL:
601	case CS42L51_BEEP_CONF:
602	case CS42L51_TONE_CTL:
603	case CS42L51_AOUTA_VOL:
604	case CS42L51_AOUTB_VOL:
605	case CS42L51_PCM_MIXER:
606	case CS42L51_LIMIT_THRES_DIS:
607	case CS42L51_LIMIT_REL:
608	case CS42L51_LIMIT_ATT:
609	case CS42L51_ALC_EN:
610	case CS42L51_ALC_REL:
611	case CS42L51_ALC_THRES:
612	case CS42L51_NOISE_CONF:
613	case CS42L51_CHARGE_FREQ:
614		return true;
615	default:
616		return false;
617	}
618}
619
620static bool cs42l51_volatile_reg(struct device *dev, unsigned int reg)
621{
622	switch (reg) {
623	case CS42L51_STATUS:
624		return true;
625	default:
626		return false;
627	}
628}
629
630static bool cs42l51_readable_reg(struct device *dev, unsigned int reg)
631{
632	switch (reg) {
633	case CS42L51_CHIP_REV_ID:
634	case CS42L51_POWER_CTL1:
635	case CS42L51_MIC_POWER_CTL:
636	case CS42L51_INTF_CTL:
637	case CS42L51_MIC_CTL:
638	case CS42L51_ADC_CTL:
639	case CS42L51_ADC_INPUT:
640	case CS42L51_DAC_OUT_CTL:
641	case CS42L51_DAC_CTL:
642	case CS42L51_ALC_PGA_CTL:
643	case CS42L51_ALC_PGB_CTL:
644	case CS42L51_ADCA_ATT:
645	case CS42L51_ADCB_ATT:
646	case CS42L51_ADCA_VOL:
647	case CS42L51_ADCB_VOL:
648	case CS42L51_PCMA_VOL:
649	case CS42L51_PCMB_VOL:
650	case CS42L51_BEEP_FREQ:
651	case CS42L51_BEEP_VOL:
652	case CS42L51_BEEP_CONF:
653	case CS42L51_TONE_CTL:
654	case CS42L51_AOUTA_VOL:
655	case CS42L51_AOUTB_VOL:
656	case CS42L51_PCM_MIXER:
657	case CS42L51_LIMIT_THRES_DIS:
658	case CS42L51_LIMIT_REL:
659	case CS42L51_LIMIT_ATT:
660	case CS42L51_ALC_EN:
661	case CS42L51_ALC_REL:
662	case CS42L51_ALC_THRES:
663	case CS42L51_NOISE_CONF:
664	case CS42L51_STATUS:
665	case CS42L51_CHARGE_FREQ:
666		return true;
667	default:
668		return false;
669	}
670}
671
672const struct regmap_config cs42l51_regmap = {
673	.reg_bits = 8,
674	.reg_stride = 1,
675	.val_bits = 8,
676	.use_single_write = true,
677	.readable_reg = cs42l51_readable_reg,
678	.volatile_reg = cs42l51_volatile_reg,
679	.writeable_reg = cs42l51_writeable_reg,
680	.max_register = CS42L51_CHARGE_FREQ,
681	.cache_type = REGCACHE_RBTREE,
682};
683EXPORT_SYMBOL_GPL(cs42l51_regmap);
684
685int cs42l51_probe(struct device *dev, struct regmap *regmap)
686{
687	struct cs42l51_private *cs42l51;
688	unsigned int val;
689	int ret, i;
690
691	if (IS_ERR(regmap))
692		return PTR_ERR(regmap);
693
694	cs42l51 = devm_kzalloc(dev, sizeof(struct cs42l51_private),
695			       GFP_KERNEL);
696	if (!cs42l51)
697		return -ENOMEM;
698
699	dev_set_drvdata(dev, cs42l51);
700	cs42l51->regmap = regmap;
701
702	cs42l51->mclk_handle = devm_clk_get(dev, "MCLK");
703	if (IS_ERR(cs42l51->mclk_handle)) {
704		if (PTR_ERR(cs42l51->mclk_handle) != -ENOENT)
705			return PTR_ERR(cs42l51->mclk_handle);
706		cs42l51->mclk_handle = NULL;
707	}
708
709	for (i = 0; i < ARRAY_SIZE(cs42l51->supplies); i++)
710		cs42l51->supplies[i].supply = cs42l51_supply_names[i];
711
712	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(cs42l51->supplies),
713				      cs42l51->supplies);
714	if (ret != 0) {
715		dev_err(dev, "Failed to request supplies: %d\n", ret);
716		return ret;
717	}
718
719	ret = regulator_bulk_enable(ARRAY_SIZE(cs42l51->supplies),
720				    cs42l51->supplies);
721	if (ret != 0) {
722		dev_err(dev, "Failed to enable supplies: %d\n", ret);
723		return ret;
724	}
725
726	cs42l51->reset_gpio = devm_gpiod_get_optional(dev, "reset",
727						      GPIOD_OUT_LOW);
728	if (IS_ERR(cs42l51->reset_gpio))
729		return PTR_ERR(cs42l51->reset_gpio);
730
731	if (cs42l51->reset_gpio) {
732		dev_dbg(dev, "Release reset gpio\n");
733		gpiod_set_value_cansleep(cs42l51->reset_gpio, 0);
734		mdelay(2);
735	}
736
737	/* Verify that we have a CS42L51 */
738	ret = regmap_read(regmap, CS42L51_CHIP_REV_ID, &val);
739	if (ret < 0) {
740		dev_err(dev, "failed to read I2C\n");
741		goto error;
742	}
743
744	if ((val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) &&
745	    (val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) {
746		dev_err(dev, "Invalid chip id: %x\n", val);
747		ret = -ENODEV;
748		goto error;
749	}
750	dev_info(dev, "Cirrus Logic CS42L51, Revision: %02X\n",
751		 val & CS42L51_CHIP_REV_MASK);
752
753	ret = devm_snd_soc_register_component(dev,
754			&soc_component_device_cs42l51, &cs42l51_dai, 1);
755	if (ret < 0)
756		goto error;
757
758	return 0;
759
760error:
761	regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies),
762			       cs42l51->supplies);
763	return ret;
764}
765EXPORT_SYMBOL_GPL(cs42l51_probe);
766
767int cs42l51_remove(struct device *dev)
768{
769	struct cs42l51_private *cs42l51 = dev_get_drvdata(dev);
770
771	gpiod_set_value_cansleep(cs42l51->reset_gpio, 1);
772
773	return regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies),
774				      cs42l51->supplies);
775}
776EXPORT_SYMBOL_GPL(cs42l51_remove);
777
778int __maybe_unused cs42l51_suspend(struct device *dev)
779{
780	struct cs42l51_private *cs42l51 = dev_get_drvdata(dev);
781
782	regcache_cache_only(cs42l51->regmap, true);
783	regcache_mark_dirty(cs42l51->regmap);
784
785	return 0;
786}
787EXPORT_SYMBOL_GPL(cs42l51_suspend);
788
789int __maybe_unused cs42l51_resume(struct device *dev)
790{
791	struct cs42l51_private *cs42l51 = dev_get_drvdata(dev);
792
793	regcache_cache_only(cs42l51->regmap, false);
794
795	return regcache_sync(cs42l51->regmap);
796}
797EXPORT_SYMBOL_GPL(cs42l51_resume);
798
799const struct of_device_id cs42l51_of_match[] = {
800	{ .compatible = "cirrus,cs42l51", },
801	{ }
802};
803MODULE_DEVICE_TABLE(of, cs42l51_of_match);
804EXPORT_SYMBOL_GPL(cs42l51_of_match);
805
806MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
807MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver");
808MODULE_LICENSE("GPL");
v4.17
 
  1/*
  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
 
 24#include <linux/module.h>
 25#include <linux/slab.h>
 26#include <sound/core.h>
 27#include <sound/soc.h>
 28#include <sound/tlv.h>
 29#include <sound/initval.h>
 30#include <sound/pcm_params.h>
 31#include <sound/pcm.h>
 
 32#include <linux/regmap.h>
 
 33
 34#include "cs42l51.h"
 35
 36enum master_slave_mode {
 37	MODE_SLAVE,
 38	MODE_SLAVE_AUTO,
 39	MODE_MASTER,
 40};
 41
 
 
 
 
 
 
 
 42struct cs42l51_private {
 43	unsigned int mclk;
 
 44	unsigned int audio_mode;	/* The mode (I2S or left-justified) */
 45	enum master_slave_mode func;
 
 
 
 46};
 47
 48#define CS42L51_FORMATS ( \
 49		SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
 50		SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
 51		SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
 52		SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
 53
 54static int cs42l51_get_chan_mix(struct snd_kcontrol *kcontrol,
 55			struct snd_ctl_elem_value *ucontrol)
 56{
 57	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
 58	unsigned long value = snd_soc_component_read32(component, CS42L51_PCM_MIXER)&3;
 59
 60	switch (value) {
 61	default:
 62	case 0:
 63		ucontrol->value.enumerated.item[0] = 0;
 64		break;
 65	/* same value : (L+R)/2 and (R+L)/2 */
 66	case 1:
 67	case 2:
 68		ucontrol->value.enumerated.item[0] = 1;
 69		break;
 70	case 3:
 71		ucontrol->value.enumerated.item[0] = 2;
 72		break;
 73	}
 74
 75	return 0;
 76}
 77
 78#define CHAN_MIX_NORMAL	0x00
 79#define CHAN_MIX_BOTH	0x55
 80#define CHAN_MIX_SWAP	0xFF
 81
 82static int cs42l51_set_chan_mix(struct snd_kcontrol *kcontrol,
 83			struct snd_ctl_elem_value *ucontrol)
 84{
 85	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
 86	unsigned char val;
 87
 88	switch (ucontrol->value.enumerated.item[0]) {
 89	default:
 90	case 0:
 91		val = CHAN_MIX_NORMAL;
 92		break;
 93	case 1:
 94		val = CHAN_MIX_BOTH;
 95		break;
 96	case 2:
 97		val = CHAN_MIX_SWAP;
 98		break;
 99	}
100
101	snd_soc_component_write(component, CS42L51_PCM_MIXER, val);
102
103	return 1;
104}
105
106static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0);
107static const DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0);
108
109static const DECLARE_TLV_DB_SCALE(aout_tlv, -10200, 50, 0);
110
111static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0);
 
112static const char *chan_mix[] = {
113	"L R",
114	"L+R",
115	"R L",
116};
117
118static SOC_ENUM_SINGLE_EXT_DECL(cs42l51_chan_mix, chan_mix);
119
120static const struct snd_kcontrol_new cs42l51_snd_controls[] = {
121	SOC_DOUBLE_R_SX_TLV("PCM Playback Volume",
122			CS42L51_PCMA_VOL, CS42L51_PCMB_VOL,
123			0, 0x19, 0x7F, adc_pcm_tlv),
124	SOC_DOUBLE_R("PCM Playback Switch",
125			CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, 7, 1, 1),
126	SOC_DOUBLE_R_SX_TLV("Analog Playback Volume",
127			CS42L51_AOUTA_VOL, CS42L51_AOUTB_VOL,
128			0, 0x34, 0xE4, aout_tlv),
129	SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume",
130			CS42L51_ADCA_VOL, CS42L51_ADCB_VOL,
131			0, 0x19, 0x7F, adc_pcm_tlv),
132	SOC_DOUBLE_R("ADC Mixer Switch",
133			CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1),
134	SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0),
135	SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0),
136	SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0),
137	SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0),
138	SOC_DOUBLE_TLV("Mic Boost Volume",
139			CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv),
 
 
140	SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv),
141	SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv),
142	SOC_ENUM_EXT("PCM channel mixer",
143			cs42l51_chan_mix,
144			cs42l51_get_chan_mix, cs42l51_set_chan_mix),
145};
146
147/*
148 * to power down, one must:
149 * 1.) Enable the PDN bit
150 * 2.) enable power-down for the select channels
151 * 3.) disable the PDN bit.
152 */
153static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w,
154		struct snd_kcontrol *kcontrol, int event)
155{
156	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
157
158	switch (event) {
159	case SND_SOC_DAPM_PRE_PMD:
160		snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
161				    CS42L51_POWER_CTL1_PDN,
162				    CS42L51_POWER_CTL1_PDN);
163		break;
164	default:
165	case SND_SOC_DAPM_POST_PMD:
166		snd_soc_component_update_bits(component, CS42L51_POWER_CTL1,
167				    CS42L51_POWER_CTL1_PDN, 0);
168		break;
169	}
170
171	return 0;
172}
173
174static const char *cs42l51_dac_names[] = {"Direct PCM",
175	"DSP PCM", "ADC"};
176static SOC_ENUM_SINGLE_DECL(cs42l51_dac_mux_enum,
177			    CS42L51_DAC_CTL, 6, cs42l51_dac_names);
178static const struct snd_kcontrol_new cs42l51_dac_mux_controls =
179	SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum);
180
181static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left",
182	"MIC Left", "MIC+preamp Left"};
183static SOC_ENUM_SINGLE_DECL(cs42l51_adcl_mux_enum,
184			    CS42L51_ADC_INPUT, 4, cs42l51_adcl_names);
185static const struct snd_kcontrol_new cs42l51_adcl_mux_controls =
186	SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum);
187
188static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right",
189	"MIC Right", "MIC+preamp Right"};
190static SOC_ENUM_SINGLE_DECL(cs42l51_adcr_mux_enum,
191			    CS42L51_ADC_INPUT, 6, cs42l51_adcr_names);
192static const struct snd_kcontrol_new cs42l51_adcr_mux_controls =
193	SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum);
194
195static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = {
196	SND_SOC_DAPM_MICBIAS("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1),
 
197	SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0,
198		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
199	SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0,
200		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
201	SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture",
202		CS42L51_POWER_CTL1, 1, 1,
203		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
204	SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture",
205		CS42L51_POWER_CTL1, 2, 1,
206		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
207	SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback",
208		CS42L51_POWER_CTL1, 5, 1,
209		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
210	SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback",
211		CS42L51_POWER_CTL1, 6, 1,
212		cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
213
214	/* analog/mic */
215	SND_SOC_DAPM_INPUT("AIN1L"),
216	SND_SOC_DAPM_INPUT("AIN1R"),
217	SND_SOC_DAPM_INPUT("AIN2L"),
218	SND_SOC_DAPM_INPUT("AIN2R"),
219	SND_SOC_DAPM_INPUT("MICL"),
220	SND_SOC_DAPM_INPUT("MICR"),
221
222	SND_SOC_DAPM_MIXER("Mic Preamp Left",
223		CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0),
224	SND_SOC_DAPM_MIXER("Mic Preamp Right",
225		CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0),
226
227	/* HP */
228	SND_SOC_DAPM_OUTPUT("HPL"),
229	SND_SOC_DAPM_OUTPUT("HPR"),
230
231	/* mux */
232	SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0,
233		&cs42l51_dac_mux_controls),
234	SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0,
235		&cs42l51_adcl_mux_controls),
236	SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0,
237		&cs42l51_adcr_mux_controls),
238};
239
 
 
 
 
240static const struct snd_soc_dapm_route cs42l51_routes[] = {
241	{"HPL", NULL, "Left DAC"},
242	{"HPR", NULL, "Right DAC"},
243
 
 
 
 
 
 
244	{"Left ADC", NULL, "Left PGA"},
245	{"Right ADC", NULL, "Right PGA"},
246
247	{"Mic Preamp Left",  NULL,  "MICL"},
248	{"Mic Preamp Right", NULL,  "MICR"},
249
250	{"PGA-ADC Mux Left",  "AIN1 Left",        "AIN1L" },
251	{"PGA-ADC Mux Left",  "AIN2 Left",        "AIN2L" },
252	{"PGA-ADC Mux Left",  "MIC Left",         "MICL"  },
253	{"PGA-ADC Mux Left",  "MIC+preamp Left",  "Mic Preamp Left" },
254	{"PGA-ADC Mux Right", "AIN1 Right",       "AIN1R" },
255	{"PGA-ADC Mux Right", "AIN2 Right",       "AIN2R" },
256	{"PGA-ADC Mux Right", "MIC Right",        "MICR" },
257	{"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" },
258
259	{"Left PGA", NULL, "PGA-ADC Mux Left"},
260	{"Right PGA", NULL, "PGA-ADC Mux Right"},
261};
262
263static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai,
264		unsigned int format)
265{
266	struct snd_soc_component *component = codec_dai->component;
267	struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
268
269	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
270	case SND_SOC_DAIFMT_I2S:
271	case SND_SOC_DAIFMT_LEFT_J:
272	case SND_SOC_DAIFMT_RIGHT_J:
273		cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
274		break;
275	default:
276		dev_err(component->dev, "invalid DAI format\n");
277		return -EINVAL;
278	}
279
280	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
281	case SND_SOC_DAIFMT_CBM_CFM:
282		cs42l51->func = MODE_MASTER;
283		break;
284	case SND_SOC_DAIFMT_CBS_CFS:
285		cs42l51->func = MODE_SLAVE_AUTO;
286		break;
287	default:
288		dev_err(component->dev, "Unknown master/slave configuration\n");
289		return -EINVAL;
290	}
291
292	return 0;
293}
294
295struct cs42l51_ratios {
296	unsigned int ratio;
297	unsigned char speed_mode;
298	unsigned char mclk;
299};
300
301static struct cs42l51_ratios slave_ratios[] = {
302	{  512, CS42L51_QSM_MODE, 0 }, {  768, CS42L51_QSM_MODE, 0 },
303	{ 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
304	{ 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 },
305	{  256, CS42L51_HSM_MODE, 0 }, {  384, CS42L51_HSM_MODE, 0 },
306	{  512, CS42L51_HSM_MODE, 0 }, {  768, CS42L51_HSM_MODE, 0 },
307	{ 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 },
308	{  128, CS42L51_SSM_MODE, 0 }, {  192, CS42L51_SSM_MODE, 0 },
309	{  256, CS42L51_SSM_MODE, 0 }, {  384, CS42L51_SSM_MODE, 0 },
310	{  512, CS42L51_SSM_MODE, 0 }, {  768, CS42L51_SSM_MODE, 0 },
311	{  128, CS42L51_DSM_MODE, 0 }, {  192, CS42L51_DSM_MODE, 0 },
312	{  256, CS42L51_DSM_MODE, 0 }, {  384, CS42L51_DSM_MODE, 0 },
313};
314
315static struct cs42l51_ratios slave_auto_ratios[] = {
316	{ 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
317	{ 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 },
318	{  512, CS42L51_HSM_MODE, 0 }, {  768, CS42L51_HSM_MODE, 0 },
319	{ 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 },
320	{  256, CS42L51_SSM_MODE, 0 }, {  384, CS42L51_SSM_MODE, 0 },
321	{  512, CS42L51_SSM_MODE, 1 }, {  768, CS42L51_SSM_MODE, 1 },
322	{  128, CS42L51_DSM_MODE, 0 }, {  192, CS42L51_DSM_MODE, 0 },
323	{  256, CS42L51_DSM_MODE, 1 }, {  384, CS42L51_DSM_MODE, 1 },
324};
325
 
 
 
 
 
 
 
 
 
 
 
 
 
326static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai,
327		int clk_id, unsigned int freq, int dir)
328{
329	struct snd_soc_component *component = codec_dai->component;
330	struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
331
332	cs42l51->mclk = freq;
333	return 0;
334}
335
336static int cs42l51_hw_params(struct snd_pcm_substream *substream,
337		struct snd_pcm_hw_params *params,
338		struct snd_soc_dai *dai)
339{
340	struct snd_soc_component *component = dai->component;
341	struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(component);
342	int ret;
343	unsigned int i;
344	unsigned int rate;
345	unsigned int ratio;
346	struct cs42l51_ratios *ratios = NULL;
347	int nr_ratios = 0;
348	int intf_ctl, power_ctl, fmt;
349
350	switch (cs42l51->func) {
351	case MODE_MASTER:
352		return -EINVAL;
 
 
353	case MODE_SLAVE:
354		ratios = slave_ratios;
355		nr_ratios = ARRAY_SIZE(slave_ratios);
356		break;
357	case MODE_SLAVE_AUTO:
358		ratios = slave_auto_ratios;
359		nr_ratios = ARRAY_SIZE(slave_auto_ratios);
360		break;
361	}
362
363	/* Figure out which MCLK/LRCK ratio to use */
364	rate = params_rate(params);     /* Sampling rate, in Hz */
365	ratio = cs42l51->mclk / rate;    /* MCLK/LRCK ratio */
366	for (i = 0; i < nr_ratios; i++) {
367		if (ratios[i].ratio == ratio)
368			break;
369	}
370
371	if (i == nr_ratios) {
372		/* We did not find a matching ratio */
373		dev_err(component->dev, "could not find matching ratio\n");
374		return -EINVAL;
375	}
376
377	intf_ctl = snd_soc_component_read32(component, CS42L51_INTF_CTL);
378	power_ctl = snd_soc_component_read32(component, CS42L51_MIC_POWER_CTL);
379
380	intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S
381			| CS42L51_INTF_CTL_DAC_FORMAT(7));
382	power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3)
383			| CS42L51_MIC_POWER_CTL_MCLK_DIV2);
384
385	switch (cs42l51->func) {
386	case MODE_MASTER:
387		intf_ctl |= CS42L51_INTF_CTL_MASTER;
388		power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
 
 
 
 
 
 
 
 
 
389		break;
390	case MODE_SLAVE:
391		power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
392		break;
393	case MODE_SLAVE_AUTO:
394		power_ctl |= CS42L51_MIC_POWER_CTL_AUTO;
395		break;
396	}
397
398	switch (cs42l51->audio_mode) {
399	case SND_SOC_DAIFMT_I2S:
400		intf_ctl |= CS42L51_INTF_CTL_ADC_I2S;
401		intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S);
402		break;
403	case SND_SOC_DAIFMT_LEFT_J:
404		intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24);
405		break;
406	case SND_SOC_DAIFMT_RIGHT_J:
407		switch (params_width(params)) {
408		case 16:
409			fmt = CS42L51_DAC_DIF_RJ16;
410			break;
411		case 18:
412			fmt = CS42L51_DAC_DIF_RJ18;
413			break;
414		case 20:
415			fmt = CS42L51_DAC_DIF_RJ20;
416			break;
417		case 24:
418			fmt = CS42L51_DAC_DIF_RJ24;
419			break;
420		default:
421			dev_err(component->dev, "unknown format\n");
422			return -EINVAL;
423		}
424		intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt);
425		break;
426	default:
427		dev_err(component->dev, "unknown format\n");
428		return -EINVAL;
429	}
430
431	if (ratios[i].mclk)
432		power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2;
433
434	ret = snd_soc_component_write(component, CS42L51_INTF_CTL, intf_ctl);
435	if (ret < 0)
436		return ret;
437
438	ret = snd_soc_component_write(component, CS42L51_MIC_POWER_CTL, power_ctl);
439	if (ret < 0)
440		return ret;
441
442	return 0;
443}
444
445static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute)
446{
447	struct snd_soc_component *component = dai->component;
448	int reg;
449	int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE;
450
451	reg = snd_soc_component_read32(component, CS42L51_DAC_OUT_CTL);
452
453	if (mute)
454		reg |= mask;
455	else
456		reg &= ~mask;
457
458	return snd_soc_component_write(component, CS42L51_DAC_OUT_CTL, reg);
459}
460
 
 
 
 
 
 
 
461static const struct snd_soc_dai_ops cs42l51_dai_ops = {
462	.hw_params      = cs42l51_hw_params,
463	.set_sysclk     = cs42l51_set_dai_sysclk,
464	.set_fmt        = cs42l51_set_dai_fmt,
465	.digital_mute   = cs42l51_dai_mute,
 
466};
467
468static struct snd_soc_dai_driver cs42l51_dai = {
469	.name = "cs42l51-hifi",
470	.playback = {
471		.stream_name = "Playback",
472		.channels_min = 1,
473		.channels_max = 2,
474		.rates = SNDRV_PCM_RATE_8000_96000,
475		.formats = CS42L51_FORMATS,
476	},
477	.capture = {
478		.stream_name = "Capture",
479		.channels_min = 1,
480		.channels_max = 2,
481		.rates = SNDRV_PCM_RATE_8000_96000,
482		.formats = CS42L51_FORMATS,
483	},
484	.ops = &cs42l51_dai_ops,
485};
486
487static int cs42l51_component_probe(struct snd_soc_component *component)
488{
489	int ret, reg;
 
 
 
 
 
 
 
 
490
491	/*
492	 * DAC configuration
493	 * - Use signal processor
494	 * - auto mute
495	 * - vol changes immediate
496	 * - no de-emphasize
497	 */
498	reg = CS42L51_DAC_CTL_DATA_SEL(1)
499		| CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0);
500	ret = snd_soc_component_write(component, CS42L51_DAC_CTL, reg);
501	if (ret < 0)
502		return ret;
503
504	return 0;
505}
506
507static const struct snd_soc_component_driver soc_component_device_cs42l51 = {
508	.probe			= cs42l51_component_probe,
509	.controls		= cs42l51_snd_controls,
510	.num_controls		= ARRAY_SIZE(cs42l51_snd_controls),
511	.dapm_widgets		= cs42l51_dapm_widgets,
512	.num_dapm_widgets	= ARRAY_SIZE(cs42l51_dapm_widgets),
513	.dapm_routes		= cs42l51_routes,
514	.num_dapm_routes	= ARRAY_SIZE(cs42l51_routes),
 
515	.idle_bias_on		= 1,
516	.use_pmdown_time	= 1,
517	.endianness		= 1,
518	.non_legacy_dai_naming	= 1,
519};
520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521const struct regmap_config cs42l51_regmap = {
 
 
 
 
 
 
 
522	.max_register = CS42L51_CHARGE_FREQ,
523	.cache_type = REGCACHE_RBTREE,
524};
525EXPORT_SYMBOL_GPL(cs42l51_regmap);
526
527int cs42l51_probe(struct device *dev, struct regmap *regmap)
528{
529	struct cs42l51_private *cs42l51;
530	unsigned int val;
531	int ret;
532
533	if (IS_ERR(regmap))
534		return PTR_ERR(regmap);
535
536	cs42l51 = devm_kzalloc(dev, sizeof(struct cs42l51_private),
537			       GFP_KERNEL);
538	if (!cs42l51)
539		return -ENOMEM;
540
541	dev_set_drvdata(dev, cs42l51);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
543	/* Verify that we have a CS42L51 */
544	ret = regmap_read(regmap, CS42L51_CHIP_REV_ID, &val);
545	if (ret < 0) {
546		dev_err(dev, "failed to read I2C\n");
547		goto error;
548	}
549
550	if ((val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) &&
551	    (val != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) {
552		dev_err(dev, "Invalid chip id: %x\n", val);
553		ret = -ENODEV;
554		goto error;
555	}
556	dev_info(dev, "Cirrus Logic CS42L51, Revision: %02X\n",
557		 val & CS42L51_CHIP_REV_MASK);
558
559	ret = devm_snd_soc_register_component(dev,
560			&soc_component_device_cs42l51, &cs42l51_dai, 1);
 
 
 
 
 
561error:
 
 
562	return ret;
563}
564EXPORT_SYMBOL_GPL(cs42l51_probe);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
566const struct of_device_id cs42l51_of_match[] = {
567	{ .compatible = "cirrus,cs42l51", },
568	{ }
569};
570MODULE_DEVICE_TABLE(of, cs42l51_of_match);
571EXPORT_SYMBOL_GPL(cs42l51_of_match);
572
573MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
574MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver");
575MODULE_LICENSE("GPL");