Linux Audio

Check our new training course

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