Linux Audio

Check our new training course

Loading...
v6.13.7
  1/*
  2 * Cirrus Logic CS42448/CS42888 Audio CODEC Digital Audio Interface (DAI) driver
  3 *
  4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5 *
  6 * Author: Nicolin Chen <Guangyu.Chen@freescale.com>
  7 *
  8 * This file is licensed under the terms of the GNU General Public License
  9 * version 2. This program is licensed "as is" without any warranty of any
 10 * kind, whether express or implied.
 11 */
 12
 13#include <linux/clk.h>
 14#include <linux/delay.h>
 15#include <linux/module.h>
 
 16#include <linux/gpio/consumer.h>
 17#include <linux/pm_runtime.h>
 18#include <linux/regulator/consumer.h>
 19#include <sound/pcm_params.h>
 20#include <sound/soc.h>
 21#include <sound/tlv.h>
 22
 23#include "cs42xx8.h"
 24
 25#define CS42XX8_NUM_SUPPLIES 4
 26static const char *const cs42xx8_supply_names[CS42XX8_NUM_SUPPLIES] = {
 27	"VA",
 28	"VD",
 29	"VLS",
 30	"VLC",
 31};
 32
 33#define CS42XX8_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
 34			 SNDRV_PCM_FMTBIT_S20_3LE | \
 35			 SNDRV_PCM_FMTBIT_S24_LE | \
 36			 SNDRV_PCM_FMTBIT_S32_LE)
 37
 38/* codec private data */
 39struct cs42xx8_priv {
 40	struct regulator_bulk_data supplies[CS42XX8_NUM_SUPPLIES];
 41	const struct cs42xx8_driver_data *drvdata;
 42	struct regmap *regmap;
 43	struct clk *clk;
 44
 45	bool slave_mode;
 46	unsigned long sysclk;
 47	u32 tx_channels;
 48	struct gpio_desc *gpiod_reset;
 49	u32 rate[2];
 50};
 51
 52/* -127.5dB to 0dB with step of 0.5dB */
 53static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
 54/* -64dB to 24dB with step of 0.5dB */
 55static const DECLARE_TLV_DB_SCALE(adc_tlv, -6400, 50, 0);
 56
 57static const char *const cs42xx8_adc_single[] = { "Differential", "Single-Ended" };
 58static const char *const cs42xx8_szc[] = { "Immediate Change", "Zero Cross",
 59					"Soft Ramp", "Soft Ramp on Zero Cross" };
 60
 61static const struct soc_enum adc1_single_enum =
 62	SOC_ENUM_SINGLE(CS42XX8_ADCCTL, 4, 2, cs42xx8_adc_single);
 63static const struct soc_enum adc2_single_enum =
 64	SOC_ENUM_SINGLE(CS42XX8_ADCCTL, 3, 2, cs42xx8_adc_single);
 65static const struct soc_enum adc3_single_enum =
 66	SOC_ENUM_SINGLE(CS42XX8_ADCCTL, 2, 2, cs42xx8_adc_single);
 67static const struct soc_enum dac_szc_enum =
 68	SOC_ENUM_SINGLE(CS42XX8_TXCTL, 5, 4, cs42xx8_szc);
 69static const struct soc_enum adc_szc_enum =
 70	SOC_ENUM_SINGLE(CS42XX8_TXCTL, 0, 4, cs42xx8_szc);
 71
 72static const struct snd_kcontrol_new cs42xx8_snd_controls[] = {
 73	SOC_DOUBLE_R_TLV("DAC1 Playback Volume", CS42XX8_VOLAOUT1,
 74			 CS42XX8_VOLAOUT2, 0, 0xff, 1, dac_tlv),
 75	SOC_DOUBLE_R_TLV("DAC2 Playback Volume", CS42XX8_VOLAOUT3,
 76			 CS42XX8_VOLAOUT4, 0, 0xff, 1, dac_tlv),
 77	SOC_DOUBLE_R_TLV("DAC3 Playback Volume", CS42XX8_VOLAOUT5,
 78			 CS42XX8_VOLAOUT6, 0, 0xff, 1, dac_tlv),
 79	SOC_DOUBLE_R_TLV("DAC4 Playback Volume", CS42XX8_VOLAOUT7,
 80			 CS42XX8_VOLAOUT8, 0, 0xff, 1, dac_tlv),
 81	SOC_DOUBLE_R_S_TLV("ADC1 Capture Volume", CS42XX8_VOLAIN1,
 82			   CS42XX8_VOLAIN2, 0, -0x80, 0x30, 7, 0, adc_tlv),
 83	SOC_DOUBLE_R_S_TLV("ADC2 Capture Volume", CS42XX8_VOLAIN3,
 84			   CS42XX8_VOLAIN4, 0, -0x80, 0x30, 7, 0, adc_tlv),
 85	SOC_DOUBLE("DAC1 Invert Switch", CS42XX8_DACINV, 0, 1, 1, 0),
 86	SOC_DOUBLE("DAC2 Invert Switch", CS42XX8_DACINV, 2, 3, 1, 0),
 87	SOC_DOUBLE("DAC3 Invert Switch", CS42XX8_DACINV, 4, 5, 1, 0),
 88	SOC_DOUBLE("DAC4 Invert Switch", CS42XX8_DACINV, 6, 7, 1, 0),
 89	SOC_DOUBLE("ADC1 Invert Switch", CS42XX8_ADCINV, 0, 1, 1, 0),
 90	SOC_DOUBLE("ADC2 Invert Switch", CS42XX8_ADCINV, 2, 3, 1, 0),
 91	SOC_SINGLE("ADC High-Pass Filter Switch", CS42XX8_ADCCTL, 7, 1, 1),
 92	SOC_SINGLE("DAC De-emphasis Switch", CS42XX8_ADCCTL, 5, 1, 0),
 93	SOC_ENUM("ADC1 Single Ended Mode Switch", adc1_single_enum),
 94	SOC_ENUM("ADC2 Single Ended Mode Switch", adc2_single_enum),
 95	SOC_SINGLE("DAC Single Volume Control Switch", CS42XX8_TXCTL, 7, 1, 0),
 96	SOC_ENUM("DAC Soft Ramp & Zero Cross Control Switch", dac_szc_enum),
 97	SOC_SINGLE("DAC Auto Mute Switch", CS42XX8_TXCTL, 4, 1, 0),
 98	SOC_SINGLE("Mute ADC Serial Port Switch", CS42XX8_TXCTL, 3, 1, 0),
 99	SOC_SINGLE("ADC Single Volume Control Switch", CS42XX8_TXCTL, 2, 1, 0),
100	SOC_ENUM("ADC Soft Ramp & Zero Cross Control Switch", adc_szc_enum),
101};
102
103static const struct snd_kcontrol_new cs42xx8_adc3_snd_controls[] = {
104	SOC_DOUBLE_R_S_TLV("ADC3 Capture Volume", CS42XX8_VOLAIN5,
105			   CS42XX8_VOLAIN6, 0, -0x80, 0x30, 7, 0, adc_tlv),
106	SOC_DOUBLE("ADC3 Invert Switch", CS42XX8_ADCINV, 4, 5, 1, 0),
107	SOC_ENUM("ADC3 Single Ended Mode Switch", adc3_single_enum),
108};
109
110static const struct snd_soc_dapm_widget cs42xx8_dapm_widgets[] = {
111	SND_SOC_DAPM_DAC("DAC1", "Playback", CS42XX8_PWRCTL, 1, 1),
112	SND_SOC_DAPM_DAC("DAC2", "Playback", CS42XX8_PWRCTL, 2, 1),
113	SND_SOC_DAPM_DAC("DAC3", "Playback", CS42XX8_PWRCTL, 3, 1),
114	SND_SOC_DAPM_DAC("DAC4", "Playback", CS42XX8_PWRCTL, 4, 1),
115
116	SND_SOC_DAPM_OUTPUT("AOUT1L"),
117	SND_SOC_DAPM_OUTPUT("AOUT1R"),
118	SND_SOC_DAPM_OUTPUT("AOUT2L"),
119	SND_SOC_DAPM_OUTPUT("AOUT2R"),
120	SND_SOC_DAPM_OUTPUT("AOUT3L"),
121	SND_SOC_DAPM_OUTPUT("AOUT3R"),
122	SND_SOC_DAPM_OUTPUT("AOUT4L"),
123	SND_SOC_DAPM_OUTPUT("AOUT4R"),
124
125	SND_SOC_DAPM_ADC("ADC1", "Capture", CS42XX8_PWRCTL, 5, 1),
126	SND_SOC_DAPM_ADC("ADC2", "Capture", CS42XX8_PWRCTL, 6, 1),
127
128	SND_SOC_DAPM_INPUT("AIN1L"),
129	SND_SOC_DAPM_INPUT("AIN1R"),
130	SND_SOC_DAPM_INPUT("AIN2L"),
131	SND_SOC_DAPM_INPUT("AIN2R"),
132
133	SND_SOC_DAPM_SUPPLY("PWR", CS42XX8_PWRCTL, 0, 1, NULL, 0),
134};
135
136static const struct snd_soc_dapm_widget cs42xx8_adc3_dapm_widgets[] = {
137	SND_SOC_DAPM_ADC("ADC3", "Capture", CS42XX8_PWRCTL, 7, 1),
138
139	SND_SOC_DAPM_INPUT("AIN3L"),
140	SND_SOC_DAPM_INPUT("AIN3R"),
141};
142
143static const struct snd_soc_dapm_route cs42xx8_dapm_routes[] = {
144	/* Playback */
145	{ "AOUT1L", NULL, "DAC1" },
146	{ "AOUT1R", NULL, "DAC1" },
147	{ "DAC1", NULL, "PWR" },
148
149	{ "AOUT2L", NULL, "DAC2" },
150	{ "AOUT2R", NULL, "DAC2" },
151	{ "DAC2", NULL, "PWR" },
152
153	{ "AOUT3L", NULL, "DAC3" },
154	{ "AOUT3R", NULL, "DAC3" },
155	{ "DAC3", NULL, "PWR" },
156
157	{ "AOUT4L", NULL, "DAC4" },
158	{ "AOUT4R", NULL, "DAC4" },
159	{ "DAC4", NULL, "PWR" },
160
161	/* Capture */
162	{ "ADC1", NULL, "AIN1L" },
163	{ "ADC1", NULL, "AIN1R" },
164	{ "ADC1", NULL, "PWR" },
165
166	{ "ADC2", NULL, "AIN2L" },
167	{ "ADC2", NULL, "AIN2R" },
168	{ "ADC2", NULL, "PWR" },
169};
170
171static const struct snd_soc_dapm_route cs42xx8_adc3_dapm_routes[] = {
172	/* Capture */
173	{ "ADC3", NULL, "AIN3L" },
174	{ "ADC3", NULL, "AIN3R" },
175	{ "ADC3", NULL, "PWR" },
176};
177
178struct cs42xx8_ratios {
179	unsigned int mfreq;
180	unsigned int min_mclk;
181	unsigned int max_mclk;
182	unsigned int ratio[3];
183};
184
185/*
186 * According to reference mannual, define the cs42xx8_ratio struct
187 * MFreq2 | MFreq1 | MFreq0 |     Description     | SSM | DSM | QSM |
188 * 0      | 0      | 0      |1.029MHz to 12.8MHz  | 256 | 128 |  64 |
189 * 0      | 0      | 1      |1.536MHz to 19.2MHz  | 384 | 192 |  96 |
190 * 0      | 1      | 0      |2.048MHz to 25.6MHz  | 512 | 256 | 128 |
191 * 0      | 1      | 1      |3.072MHz to 38.4MHz  | 768 | 384 | 192 |
192 * 1      | x      | x      |4.096MHz to 51.2MHz  |1024 | 512 | 256 |
193 */
194static const struct cs42xx8_ratios cs42xx8_ratios[] = {
195	{ 0, 1029000, 12800000, {256, 128, 64} },
196	{ 2, 1536000, 19200000, {384, 192, 96} },
197	{ 4, 2048000, 25600000, {512, 256, 128} },
198	{ 6, 3072000, 38400000, {768, 384, 192} },
199	{ 8, 4096000, 51200000, {1024, 512, 256} },
200};
201
202static int cs42xx8_set_dai_sysclk(struct snd_soc_dai *codec_dai,
203				  int clk_id, unsigned int freq, int dir)
204{
205	struct snd_soc_component *component = codec_dai->component;
206	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
207
208	cs42xx8->sysclk = freq;
209
210	return 0;
211}
212
213static int cs42xx8_set_dai_fmt(struct snd_soc_dai *codec_dai,
214			       unsigned int format)
215{
216	struct snd_soc_component *component = codec_dai->component;
217	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
218	u32 val;
219
220	/* Set DAI format */
221	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
222	case SND_SOC_DAIFMT_LEFT_J:
223		val = CS42XX8_INTF_DAC_DIF_LEFTJ | CS42XX8_INTF_ADC_DIF_LEFTJ;
224		break;
225	case SND_SOC_DAIFMT_I2S:
226		val = CS42XX8_INTF_DAC_DIF_I2S | CS42XX8_INTF_ADC_DIF_I2S;
227		break;
228	case SND_SOC_DAIFMT_RIGHT_J:
229		val = CS42XX8_INTF_DAC_DIF_RIGHTJ | CS42XX8_INTF_ADC_DIF_RIGHTJ;
230		break;
231	case SND_SOC_DAIFMT_DSP_A:
232		val = CS42XX8_INTF_DAC_DIF_TDM | CS42XX8_INTF_ADC_DIF_TDM;
233		break;
234	default:
235		dev_err(component->dev, "unsupported dai format\n");
236		return -EINVAL;
237	}
238
239	regmap_update_bits(cs42xx8->regmap, CS42XX8_INTF,
240			   CS42XX8_INTF_DAC_DIF_MASK |
241			   CS42XX8_INTF_ADC_DIF_MASK, val);
242
243	/* Set master/slave audio interface */
244	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
245	case SND_SOC_DAIFMT_CBS_CFS:
246		cs42xx8->slave_mode = true;
247		break;
248	case SND_SOC_DAIFMT_CBM_CFM:
249		cs42xx8->slave_mode = false;
250		break;
251	default:
252		dev_err(component->dev, "unsupported master/slave mode\n");
253		return -EINVAL;
254	}
255
256	return 0;
257}
258
259static int cs42xx8_hw_params(struct snd_pcm_substream *substream,
260			     struct snd_pcm_hw_params *params,
261			     struct snd_soc_dai *dai)
262{
263	struct snd_soc_component *component = dai->component;
264	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
265	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
266	u32 ratio[2];
267	u32 rate[2];
268	u32 fm[2];
269	u32 i, val, mask;
270	bool condition1, condition2;
271
272	if (tx)
273		cs42xx8->tx_channels = params_channels(params);
274
275	rate[tx]  = params_rate(params);
276	rate[!tx] = cs42xx8->rate[!tx];
277
278	ratio[tx] = rate[tx] > 0 ? cs42xx8->sysclk / rate[tx] : 0;
279	ratio[!tx] = rate[!tx] > 0 ? cs42xx8->sysclk / rate[!tx] : 0;
280
281	/* Get functional mode for tx and rx according to rate */
282	for (i = 0; i < 2; i++) {
283		if (cs42xx8->slave_mode) {
284			fm[i] = CS42XX8_FM_AUTO;
285		} else {
286			if (rate[i] < 50000) {
287				fm[i] = CS42XX8_FM_SINGLE;
288			} else if (rate[i] > 50000 && rate[i] < 100000) {
289				fm[i] = CS42XX8_FM_DOUBLE;
290			} else if (rate[i] > 100000 && rate[i] < 200000) {
291				fm[i] = CS42XX8_FM_QUAD;
292			} else {
293				dev_err(component->dev,
294					"unsupported sample rate\n");
295				return -EINVAL;
296			}
297		}
298	}
299
300	for (i = 0; i < ARRAY_SIZE(cs42xx8_ratios); i++) {
301		/* Is the ratio[tx] valid ? */
302		condition1 = ((fm[tx] == CS42XX8_FM_AUTO) ?
303			(cs42xx8_ratios[i].ratio[0] == ratio[tx] ||
304			cs42xx8_ratios[i].ratio[1] == ratio[tx] ||
305			cs42xx8_ratios[i].ratio[2] == ratio[tx]) :
306			(cs42xx8_ratios[i].ratio[fm[tx]] == ratio[tx])) &&
307			cs42xx8->sysclk >= cs42xx8_ratios[i].min_mclk &&
308			cs42xx8->sysclk <= cs42xx8_ratios[i].max_mclk;
309
310		if (!ratio[tx])
311			condition1 = true;
312
313		/* Is the ratio[!tx] valid ? */
314		condition2 = ((fm[!tx] == CS42XX8_FM_AUTO) ?
315			(cs42xx8_ratios[i].ratio[0] == ratio[!tx] ||
316			cs42xx8_ratios[i].ratio[1] == ratio[!tx] ||
317			cs42xx8_ratios[i].ratio[2] == ratio[!tx]) :
318			(cs42xx8_ratios[i].ratio[fm[!tx]] == ratio[!tx]));
319
320		if (!ratio[!tx])
321			condition2 = true;
322
323		/*
324		 * Both ratio[tx] and ratio[!tx] is valid, then we get
325		 * a proper MFreq.
326		 */
327		if (condition1 && condition2)
328			break;
329	}
330
331	if (i == ARRAY_SIZE(cs42xx8_ratios)) {
332		dev_err(component->dev, "unsupported sysclk ratio\n");
333		return -EINVAL;
334	}
335
336	cs42xx8->rate[tx] = params_rate(params);
337
338	mask = CS42XX8_FUNCMOD_MFREQ_MASK;
339	val = cs42xx8_ratios[i].mfreq;
340
341	regmap_update_bits(cs42xx8->regmap, CS42XX8_FUNCMOD,
342			   CS42XX8_FUNCMOD_xC_FM_MASK(tx) | mask,
343			   CS42XX8_FUNCMOD_xC_FM(tx, fm[tx]) | val);
344
345	return 0;
346}
347
348static int cs42xx8_hw_free(struct snd_pcm_substream *substream,
349			   struct snd_soc_dai *dai)
350{
351	struct snd_soc_component *component = dai->component;
352	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
353	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
354
355	/* Clear stored rate */
356	cs42xx8->rate[tx] = 0;
357
358	regmap_update_bits(cs42xx8->regmap, CS42XX8_FUNCMOD,
359			   CS42XX8_FUNCMOD_xC_FM_MASK(tx),
360			   CS42XX8_FUNCMOD_xC_FM(tx, CS42XX8_FM_AUTO));
361	return 0;
362}
363
364static int cs42xx8_mute(struct snd_soc_dai *dai, int mute, int direction)
365{
366	struct snd_soc_component *component = dai->component;
367	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
368	u8 dac_unmute = cs42xx8->tx_channels ?
369		        ~((0x1 << cs42xx8->tx_channels) - 1) : 0;
370
371	regmap_write(cs42xx8->regmap, CS42XX8_DACMUTE,
372		     mute ? CS42XX8_DACMUTE_ALL : dac_unmute);
373
374	return 0;
375}
376
377static const struct snd_soc_dai_ops cs42xx8_dai_ops = {
378	.set_fmt	= cs42xx8_set_dai_fmt,
379	.set_sysclk	= cs42xx8_set_dai_sysclk,
380	.hw_params	= cs42xx8_hw_params,
381	.hw_free	= cs42xx8_hw_free,
382	.mute_stream	= cs42xx8_mute,
383	.no_capture_mute = 1,
384};
385
386static struct snd_soc_dai_driver cs42xx8_dai = {
387	.playback = {
388		.stream_name = "Playback",
389		.channels_min = 1,
390		.channels_max = 8,
391		.rates = SNDRV_PCM_RATE_8000_192000,
392		.formats = CS42XX8_FORMATS,
393	},
394	.capture = {
395		.stream_name = "Capture",
396		.channels_min = 1,
397		.rates = SNDRV_PCM_RATE_8000_192000,
398		.formats = CS42XX8_FORMATS,
399	},
400	.ops = &cs42xx8_dai_ops,
401};
402
403static const struct reg_default cs42xx8_reg[] = {
404	{ 0x02, 0x00 },   /* Power Control */
405	{ 0x03, 0xF0 },   /* Functional Mode */
406	{ 0x04, 0x46 },   /* Interface Formats */
407	{ 0x05, 0x00 },   /* ADC Control & DAC De-Emphasis */
408	{ 0x06, 0x10 },   /* Transition Control */
409	{ 0x07, 0x00 },   /* DAC Channel Mute */
410	{ 0x08, 0x00 },   /* Volume Control AOUT1 */
411	{ 0x09, 0x00 },   /* Volume Control AOUT2 */
412	{ 0x0a, 0x00 },   /* Volume Control AOUT3 */
413	{ 0x0b, 0x00 },   /* Volume Control AOUT4 */
414	{ 0x0c, 0x00 },   /* Volume Control AOUT5 */
415	{ 0x0d, 0x00 },   /* Volume Control AOUT6 */
416	{ 0x0e, 0x00 },   /* Volume Control AOUT7 */
417	{ 0x0f, 0x00 },   /* Volume Control AOUT8 */
418	{ 0x10, 0x00 },   /* DAC Channel Invert */
419	{ 0x11, 0x00 },   /* Volume Control AIN1 */
420	{ 0x12, 0x00 },   /* Volume Control AIN2 */
421	{ 0x13, 0x00 },   /* Volume Control AIN3 */
422	{ 0x14, 0x00 },   /* Volume Control AIN4 */
423	{ 0x15, 0x00 },   /* Volume Control AIN5 */
424	{ 0x16, 0x00 },   /* Volume Control AIN6 */
425	{ 0x17, 0x00 },   /* ADC Channel Invert */
426	{ 0x18, 0x00 },   /* Status Control */
427	{ 0x1a, 0x00 },   /* Status Mask */
428	{ 0x1b, 0x00 },   /* MUTEC Pin Control */
429};
430
431static bool cs42xx8_volatile_register(struct device *dev, unsigned int reg)
432{
433	switch (reg) {
434	case CS42XX8_STATUS:
435		return true;
436	default:
437		return false;
438	}
439}
440
441static bool cs42xx8_writeable_register(struct device *dev, unsigned int reg)
442{
443	switch (reg) {
444	case CS42XX8_CHIPID:
445	case CS42XX8_STATUS:
446		return false;
447	default:
448		return true;
449	}
450}
451
452const struct regmap_config cs42xx8_regmap_config = {
453	.reg_bits = 8,
454	.val_bits = 8,
455
456	.max_register = CS42XX8_LASTREG,
457	.reg_defaults = cs42xx8_reg,
458	.num_reg_defaults = ARRAY_SIZE(cs42xx8_reg),
459	.volatile_reg = cs42xx8_volatile_register,
460	.writeable_reg = cs42xx8_writeable_register,
461	.cache_type = REGCACHE_MAPLE,
462};
463EXPORT_SYMBOL_GPL(cs42xx8_regmap_config);
464
465static int cs42xx8_component_probe(struct snd_soc_component *component)
466{
467	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
468	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
469
470	switch (cs42xx8->drvdata->num_adcs) {
471	case 3:
472		snd_soc_add_component_controls(component, cs42xx8_adc3_snd_controls,
473					ARRAY_SIZE(cs42xx8_adc3_snd_controls));
474		snd_soc_dapm_new_controls(dapm, cs42xx8_adc3_dapm_widgets,
475					ARRAY_SIZE(cs42xx8_adc3_dapm_widgets));
476		snd_soc_dapm_add_routes(dapm, cs42xx8_adc3_dapm_routes,
477					ARRAY_SIZE(cs42xx8_adc3_dapm_routes));
478		break;
479	default:
480		break;
481	}
482
483	/* Mute all DAC channels */
484	regmap_write(cs42xx8->regmap, CS42XX8_DACMUTE, CS42XX8_DACMUTE_ALL);
485
486	return 0;
487}
488
489static const struct snd_soc_component_driver cs42xx8_driver = {
490	.probe			= cs42xx8_component_probe,
491	.controls		= cs42xx8_snd_controls,
492	.num_controls		= ARRAY_SIZE(cs42xx8_snd_controls),
493	.dapm_widgets		= cs42xx8_dapm_widgets,
494	.num_dapm_widgets	= ARRAY_SIZE(cs42xx8_dapm_widgets),
495	.dapm_routes		= cs42xx8_dapm_routes,
496	.num_dapm_routes	= ARRAY_SIZE(cs42xx8_dapm_routes),
497	.use_pmdown_time	= 1,
498	.endianness		= 1,
 
499};
500
501const struct cs42xx8_driver_data cs42448_data = {
502	.name = "cs42448",
503	.num_adcs = 3,
504};
505EXPORT_SYMBOL_GPL(cs42448_data);
506
507const struct cs42xx8_driver_data cs42888_data = {
508	.name = "cs42888",
509	.num_adcs = 2,
510};
511EXPORT_SYMBOL_GPL(cs42888_data);
512
513int cs42xx8_probe(struct device *dev, struct regmap *regmap, struct cs42xx8_driver_data *drvdata)
 
 
 
 
 
 
 
 
514{
 
515	struct cs42xx8_priv *cs42xx8;
516	int ret, val, i;
517
518	if (IS_ERR(regmap)) {
519		ret = PTR_ERR(regmap);
520		dev_err(dev, "failed to allocate regmap: %d\n", ret);
521		return ret;
522	}
523
524	cs42xx8 = devm_kzalloc(dev, sizeof(*cs42xx8), GFP_KERNEL);
525	if (cs42xx8 == NULL)
526		return -ENOMEM;
527
 
528	dev_set_drvdata(dev, cs42xx8);
529
530	cs42xx8->regmap = regmap;
 
 
531
532	cs42xx8->drvdata = drvdata;
 
 
 
533
534	cs42xx8->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
535							GPIOD_OUT_HIGH);
536	if (IS_ERR(cs42xx8->gpiod_reset))
537		return PTR_ERR(cs42xx8->gpiod_reset);
538
539	gpiod_set_value_cansleep(cs42xx8->gpiod_reset, 0);
540
541	cs42xx8->clk = devm_clk_get(dev, "mclk");
542	if (IS_ERR(cs42xx8->clk)) {
543		dev_err(dev, "failed to get the clock: %ld\n",
544				PTR_ERR(cs42xx8->clk));
545		return -EINVAL;
546	}
547
548	cs42xx8->sysclk = clk_get_rate(cs42xx8->clk);
549
550	for (i = 0; i < ARRAY_SIZE(cs42xx8->supplies); i++)
551		cs42xx8->supplies[i].supply = cs42xx8_supply_names[i];
552
553	ret = devm_regulator_bulk_get(dev,
554			ARRAY_SIZE(cs42xx8->supplies), cs42xx8->supplies);
555	if (ret) {
556		dev_err(dev, "failed to request supplies: %d\n", ret);
557		return ret;
558	}
559
560	ret = regulator_bulk_enable(ARRAY_SIZE(cs42xx8->supplies),
561				    cs42xx8->supplies);
562	if (ret) {
563		dev_err(dev, "failed to enable supplies: %d\n", ret);
564		return ret;
565	}
566
567	/* Make sure hardware reset done */
568	msleep(5);
569
570	/* Validate the chip ID */
571	ret = regmap_read(cs42xx8->regmap, CS42XX8_CHIPID, &val);
572	if (ret < 0) {
573		dev_err(dev, "failed to get device ID, ret = %d", ret);
574		goto err_enable;
575	}
576
577	/* The top four bits of the chip ID should be 0000 */
578	if (((val & CS42XX8_CHIPID_CHIP_ID_MASK) >> 4) != 0x00) {
579		dev_err(dev, "unmatched chip ID: %d\n",
580			(val & CS42XX8_CHIPID_CHIP_ID_MASK) >> 4);
581		ret = -EINVAL;
582		goto err_enable;
583	}
584
585	dev_info(dev, "found device, revision %X\n",
586			val & CS42XX8_CHIPID_REV_ID_MASK);
587
588	cs42xx8_dai.name = cs42xx8->drvdata->name;
589
590	/* Each adc supports stereo input */
591	cs42xx8_dai.capture.channels_max = cs42xx8->drvdata->num_adcs * 2;
592
593	ret = devm_snd_soc_register_component(dev, &cs42xx8_driver, &cs42xx8_dai, 1);
594	if (ret) {
595		dev_err(dev, "failed to register component:%d\n", ret);
596		goto err_enable;
597	}
598
599	regcache_cache_only(cs42xx8->regmap, true);
600
601err_enable:
602	regulator_bulk_disable(ARRAY_SIZE(cs42xx8->supplies),
603			       cs42xx8->supplies);
604
605	return ret;
606}
607EXPORT_SYMBOL_GPL(cs42xx8_probe);
608
609#ifdef CONFIG_PM
610static int cs42xx8_runtime_resume(struct device *dev)
611{
612	struct cs42xx8_priv *cs42xx8 = dev_get_drvdata(dev);
613	int ret;
614
615	ret = clk_prepare_enable(cs42xx8->clk);
616	if (ret) {
617		dev_err(dev, "failed to enable mclk: %d\n", ret);
618		return ret;
619	}
620
621	gpiod_set_value_cansleep(cs42xx8->gpiod_reset, 0);
622
623	ret = regulator_bulk_enable(ARRAY_SIZE(cs42xx8->supplies),
624				    cs42xx8->supplies);
625	if (ret) {
626		dev_err(dev, "failed to enable supplies: %d\n", ret);
627		goto err_clk;
628	}
629
630	/* Make sure hardware reset done */
631	msleep(5);
632
633	regcache_cache_only(cs42xx8->regmap, false);
634	regcache_mark_dirty(cs42xx8->regmap);
635
636	ret = regcache_sync(cs42xx8->regmap);
637	if (ret) {
638		dev_err(dev, "failed to sync regmap: %d\n", ret);
639		goto err_bulk;
640	}
641
642	return 0;
643
644err_bulk:
645	regulator_bulk_disable(ARRAY_SIZE(cs42xx8->supplies),
646			       cs42xx8->supplies);
647err_clk:
648	clk_disable_unprepare(cs42xx8->clk);
649
650	return ret;
651}
652
653static int cs42xx8_runtime_suspend(struct device *dev)
654{
655	struct cs42xx8_priv *cs42xx8 = dev_get_drvdata(dev);
656
657	regcache_cache_only(cs42xx8->regmap, true);
658
659	regulator_bulk_disable(ARRAY_SIZE(cs42xx8->supplies),
660			       cs42xx8->supplies);
661
662	gpiod_set_value_cansleep(cs42xx8->gpiod_reset, 1);
663
664	clk_disable_unprepare(cs42xx8->clk);
665
666	return 0;
667}
668#endif
669
670const struct dev_pm_ops cs42xx8_pm = {
671	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
672				pm_runtime_force_resume)
673	SET_RUNTIME_PM_OPS(cs42xx8_runtime_suspend, cs42xx8_runtime_resume, NULL)
674};
675EXPORT_SYMBOL_GPL(cs42xx8_pm);
676
677MODULE_DESCRIPTION("Cirrus Logic CS42448/CS42888 ALSA SoC Codec Driver");
678MODULE_AUTHOR("Freescale Semiconductor, Inc.");
679MODULE_LICENSE("GPL");
v5.14.15
  1/*
  2 * Cirrus Logic CS42448/CS42888 Audio CODEC Digital Audio Interface (DAI) driver
  3 *
  4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5 *
  6 * Author: Nicolin Chen <Guangyu.Chen@freescale.com>
  7 *
  8 * This file is licensed under the terms of the GNU General Public License
  9 * version 2. This program is licensed "as is" without any warranty of any
 10 * kind, whether express or implied.
 11 */
 12
 13#include <linux/clk.h>
 14#include <linux/delay.h>
 15#include <linux/module.h>
 16#include <linux/of_device.h>
 17#include <linux/gpio/consumer.h>
 18#include <linux/pm_runtime.h>
 19#include <linux/regulator/consumer.h>
 20#include <sound/pcm_params.h>
 21#include <sound/soc.h>
 22#include <sound/tlv.h>
 23
 24#include "cs42xx8.h"
 25
 26#define CS42XX8_NUM_SUPPLIES 4
 27static const char *const cs42xx8_supply_names[CS42XX8_NUM_SUPPLIES] = {
 28	"VA",
 29	"VD",
 30	"VLS",
 31	"VLC",
 32};
 33
 34#define CS42XX8_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
 35			 SNDRV_PCM_FMTBIT_S20_3LE | \
 36			 SNDRV_PCM_FMTBIT_S24_LE | \
 37			 SNDRV_PCM_FMTBIT_S32_LE)
 38
 39/* codec private data */
 40struct cs42xx8_priv {
 41	struct regulator_bulk_data supplies[CS42XX8_NUM_SUPPLIES];
 42	const struct cs42xx8_driver_data *drvdata;
 43	struct regmap *regmap;
 44	struct clk *clk;
 45
 46	bool slave_mode;
 47	unsigned long sysclk;
 48	u32 tx_channels;
 49	struct gpio_desc *gpiod_reset;
 50	u32 rate[2];
 51};
 52
 53/* -127.5dB to 0dB with step of 0.5dB */
 54static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
 55/* -64dB to 24dB with step of 0.5dB */
 56static const DECLARE_TLV_DB_SCALE(adc_tlv, -6400, 50, 0);
 57
 58static const char *const cs42xx8_adc_single[] = { "Differential", "Single-Ended" };
 59static const char *const cs42xx8_szc[] = { "Immediate Change", "Zero Cross",
 60					"Soft Ramp", "Soft Ramp on Zero Cross" };
 61
 62static const struct soc_enum adc1_single_enum =
 63	SOC_ENUM_SINGLE(CS42XX8_ADCCTL, 4, 2, cs42xx8_adc_single);
 64static const struct soc_enum adc2_single_enum =
 65	SOC_ENUM_SINGLE(CS42XX8_ADCCTL, 3, 2, cs42xx8_adc_single);
 66static const struct soc_enum adc3_single_enum =
 67	SOC_ENUM_SINGLE(CS42XX8_ADCCTL, 2, 2, cs42xx8_adc_single);
 68static const struct soc_enum dac_szc_enum =
 69	SOC_ENUM_SINGLE(CS42XX8_TXCTL, 5, 4, cs42xx8_szc);
 70static const struct soc_enum adc_szc_enum =
 71	SOC_ENUM_SINGLE(CS42XX8_TXCTL, 0, 4, cs42xx8_szc);
 72
 73static const struct snd_kcontrol_new cs42xx8_snd_controls[] = {
 74	SOC_DOUBLE_R_TLV("DAC1 Playback Volume", CS42XX8_VOLAOUT1,
 75			 CS42XX8_VOLAOUT2, 0, 0xff, 1, dac_tlv),
 76	SOC_DOUBLE_R_TLV("DAC2 Playback Volume", CS42XX8_VOLAOUT3,
 77			 CS42XX8_VOLAOUT4, 0, 0xff, 1, dac_tlv),
 78	SOC_DOUBLE_R_TLV("DAC3 Playback Volume", CS42XX8_VOLAOUT5,
 79			 CS42XX8_VOLAOUT6, 0, 0xff, 1, dac_tlv),
 80	SOC_DOUBLE_R_TLV("DAC4 Playback Volume", CS42XX8_VOLAOUT7,
 81			 CS42XX8_VOLAOUT8, 0, 0xff, 1, dac_tlv),
 82	SOC_DOUBLE_R_S_TLV("ADC1 Capture Volume", CS42XX8_VOLAIN1,
 83			   CS42XX8_VOLAIN2, 0, -0x80, 0x30, 7, 0, adc_tlv),
 84	SOC_DOUBLE_R_S_TLV("ADC2 Capture Volume", CS42XX8_VOLAIN3,
 85			   CS42XX8_VOLAIN4, 0, -0x80, 0x30, 7, 0, adc_tlv),
 86	SOC_DOUBLE("DAC1 Invert Switch", CS42XX8_DACINV, 0, 1, 1, 0),
 87	SOC_DOUBLE("DAC2 Invert Switch", CS42XX8_DACINV, 2, 3, 1, 0),
 88	SOC_DOUBLE("DAC3 Invert Switch", CS42XX8_DACINV, 4, 5, 1, 0),
 89	SOC_DOUBLE("DAC4 Invert Switch", CS42XX8_DACINV, 6, 7, 1, 0),
 90	SOC_DOUBLE("ADC1 Invert Switch", CS42XX8_ADCINV, 0, 1, 1, 0),
 91	SOC_DOUBLE("ADC2 Invert Switch", CS42XX8_ADCINV, 2, 3, 1, 0),
 92	SOC_SINGLE("ADC High-Pass Filter Switch", CS42XX8_ADCCTL, 7, 1, 1),
 93	SOC_SINGLE("DAC De-emphasis Switch", CS42XX8_ADCCTL, 5, 1, 0),
 94	SOC_ENUM("ADC1 Single Ended Mode Switch", adc1_single_enum),
 95	SOC_ENUM("ADC2 Single Ended Mode Switch", adc2_single_enum),
 96	SOC_SINGLE("DAC Single Volume Control Switch", CS42XX8_TXCTL, 7, 1, 0),
 97	SOC_ENUM("DAC Soft Ramp & Zero Cross Control Switch", dac_szc_enum),
 98	SOC_SINGLE("DAC Auto Mute Switch", CS42XX8_TXCTL, 4, 1, 0),
 99	SOC_SINGLE("Mute ADC Serial Port Switch", CS42XX8_TXCTL, 3, 1, 0),
100	SOC_SINGLE("ADC Single Volume Control Switch", CS42XX8_TXCTL, 2, 1, 0),
101	SOC_ENUM("ADC Soft Ramp & Zero Cross Control Switch", adc_szc_enum),
102};
103
104static const struct snd_kcontrol_new cs42xx8_adc3_snd_controls[] = {
105	SOC_DOUBLE_R_S_TLV("ADC3 Capture Volume", CS42XX8_VOLAIN5,
106			   CS42XX8_VOLAIN6, 0, -0x80, 0x30, 7, 0, adc_tlv),
107	SOC_DOUBLE("ADC3 Invert Switch", CS42XX8_ADCINV, 4, 5, 1, 0),
108	SOC_ENUM("ADC3 Single Ended Mode Switch", adc3_single_enum),
109};
110
111static const struct snd_soc_dapm_widget cs42xx8_dapm_widgets[] = {
112	SND_SOC_DAPM_DAC("DAC1", "Playback", CS42XX8_PWRCTL, 1, 1),
113	SND_SOC_DAPM_DAC("DAC2", "Playback", CS42XX8_PWRCTL, 2, 1),
114	SND_SOC_DAPM_DAC("DAC3", "Playback", CS42XX8_PWRCTL, 3, 1),
115	SND_SOC_DAPM_DAC("DAC4", "Playback", CS42XX8_PWRCTL, 4, 1),
116
117	SND_SOC_DAPM_OUTPUT("AOUT1L"),
118	SND_SOC_DAPM_OUTPUT("AOUT1R"),
119	SND_SOC_DAPM_OUTPUT("AOUT2L"),
120	SND_SOC_DAPM_OUTPUT("AOUT2R"),
121	SND_SOC_DAPM_OUTPUT("AOUT3L"),
122	SND_SOC_DAPM_OUTPUT("AOUT3R"),
123	SND_SOC_DAPM_OUTPUT("AOUT4L"),
124	SND_SOC_DAPM_OUTPUT("AOUT4R"),
125
126	SND_SOC_DAPM_ADC("ADC1", "Capture", CS42XX8_PWRCTL, 5, 1),
127	SND_SOC_DAPM_ADC("ADC2", "Capture", CS42XX8_PWRCTL, 6, 1),
128
129	SND_SOC_DAPM_INPUT("AIN1L"),
130	SND_SOC_DAPM_INPUT("AIN1R"),
131	SND_SOC_DAPM_INPUT("AIN2L"),
132	SND_SOC_DAPM_INPUT("AIN2R"),
133
134	SND_SOC_DAPM_SUPPLY("PWR", CS42XX8_PWRCTL, 0, 1, NULL, 0),
135};
136
137static const struct snd_soc_dapm_widget cs42xx8_adc3_dapm_widgets[] = {
138	SND_SOC_DAPM_ADC("ADC3", "Capture", CS42XX8_PWRCTL, 7, 1),
139
140	SND_SOC_DAPM_INPUT("AIN3L"),
141	SND_SOC_DAPM_INPUT("AIN3R"),
142};
143
144static const struct snd_soc_dapm_route cs42xx8_dapm_routes[] = {
145	/* Playback */
146	{ "AOUT1L", NULL, "DAC1" },
147	{ "AOUT1R", NULL, "DAC1" },
148	{ "DAC1", NULL, "PWR" },
149
150	{ "AOUT2L", NULL, "DAC2" },
151	{ "AOUT2R", NULL, "DAC2" },
152	{ "DAC2", NULL, "PWR" },
153
154	{ "AOUT3L", NULL, "DAC3" },
155	{ "AOUT3R", NULL, "DAC3" },
156	{ "DAC3", NULL, "PWR" },
157
158	{ "AOUT4L", NULL, "DAC4" },
159	{ "AOUT4R", NULL, "DAC4" },
160	{ "DAC4", NULL, "PWR" },
161
162	/* Capture */
163	{ "ADC1", NULL, "AIN1L" },
164	{ "ADC1", NULL, "AIN1R" },
165	{ "ADC1", NULL, "PWR" },
166
167	{ "ADC2", NULL, "AIN2L" },
168	{ "ADC2", NULL, "AIN2R" },
169	{ "ADC2", NULL, "PWR" },
170};
171
172static const struct snd_soc_dapm_route cs42xx8_adc3_dapm_routes[] = {
173	/* Capture */
174	{ "ADC3", NULL, "AIN3L" },
175	{ "ADC3", NULL, "AIN3R" },
176	{ "ADC3", NULL, "PWR" },
177};
178
179struct cs42xx8_ratios {
180	unsigned int mfreq;
181	unsigned int min_mclk;
182	unsigned int max_mclk;
183	unsigned int ratio[3];
184};
185
186/*
187 * According to reference mannual, define the cs42xx8_ratio struct
188 * MFreq2 | MFreq1 | MFreq0 |     Description     | SSM | DSM | QSM |
189 * 0      | 0      | 0      |1.029MHz to 12.8MHz  | 256 | 128 |  64 |
190 * 0      | 0      | 1      |1.536MHz to 19.2MHz  | 384 | 192 |  96 |
191 * 0      | 1      | 0      |2.048MHz to 25.6MHz  | 512 | 256 | 128 |
192 * 0      | 1      | 1      |3.072MHz to 38.4MHz  | 768 | 384 | 192 |
193 * 1      | x      | x      |4.096MHz to 51.2MHz  |1024 | 512 | 256 |
194 */
195static const struct cs42xx8_ratios cs42xx8_ratios[] = {
196	{ 0, 1029000, 12800000, {256, 128, 64} },
197	{ 2, 1536000, 19200000, {384, 192, 96} },
198	{ 4, 2048000, 25600000, {512, 256, 128} },
199	{ 6, 3072000, 38400000, {768, 384, 192} },
200	{ 8, 4096000, 51200000, {1024, 512, 256} },
201};
202
203static int cs42xx8_set_dai_sysclk(struct snd_soc_dai *codec_dai,
204				  int clk_id, unsigned int freq, int dir)
205{
206	struct snd_soc_component *component = codec_dai->component;
207	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
208
209	cs42xx8->sysclk = freq;
210
211	return 0;
212}
213
214static int cs42xx8_set_dai_fmt(struct snd_soc_dai *codec_dai,
215			       unsigned int format)
216{
217	struct snd_soc_component *component = codec_dai->component;
218	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
219	u32 val;
220
221	/* Set DAI format */
222	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
223	case SND_SOC_DAIFMT_LEFT_J:
224		val = CS42XX8_INTF_DAC_DIF_LEFTJ | CS42XX8_INTF_ADC_DIF_LEFTJ;
225		break;
226	case SND_SOC_DAIFMT_I2S:
227		val = CS42XX8_INTF_DAC_DIF_I2S | CS42XX8_INTF_ADC_DIF_I2S;
228		break;
229	case SND_SOC_DAIFMT_RIGHT_J:
230		val = CS42XX8_INTF_DAC_DIF_RIGHTJ | CS42XX8_INTF_ADC_DIF_RIGHTJ;
231		break;
232	case SND_SOC_DAIFMT_DSP_A:
233		val = CS42XX8_INTF_DAC_DIF_TDM | CS42XX8_INTF_ADC_DIF_TDM;
234		break;
235	default:
236		dev_err(component->dev, "unsupported dai format\n");
237		return -EINVAL;
238	}
239
240	regmap_update_bits(cs42xx8->regmap, CS42XX8_INTF,
241			   CS42XX8_INTF_DAC_DIF_MASK |
242			   CS42XX8_INTF_ADC_DIF_MASK, val);
243
244	/* Set master/slave audio interface */
245	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
246	case SND_SOC_DAIFMT_CBS_CFS:
247		cs42xx8->slave_mode = true;
248		break;
249	case SND_SOC_DAIFMT_CBM_CFM:
250		cs42xx8->slave_mode = false;
251		break;
252	default:
253		dev_err(component->dev, "unsupported master/slave mode\n");
254		return -EINVAL;
255	}
256
257	return 0;
258}
259
260static int cs42xx8_hw_params(struct snd_pcm_substream *substream,
261			     struct snd_pcm_hw_params *params,
262			     struct snd_soc_dai *dai)
263{
264	struct snd_soc_component *component = dai->component;
265	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
266	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
267	u32 ratio[2];
268	u32 rate[2];
269	u32 fm[2];
270	u32 i, val, mask;
271	bool condition1, condition2;
272
273	if (tx)
274		cs42xx8->tx_channels = params_channels(params);
275
276	rate[tx]  = params_rate(params);
277	rate[!tx] = cs42xx8->rate[!tx];
278
279	ratio[tx] = rate[tx] > 0 ? cs42xx8->sysclk / rate[tx] : 0;
280	ratio[!tx] = rate[!tx] > 0 ? cs42xx8->sysclk / rate[!tx] : 0;
281
282	/* Get functional mode for tx and rx according to rate */
283	for (i = 0; i < 2; i++) {
284		if (cs42xx8->slave_mode) {
285			fm[i] = CS42XX8_FM_AUTO;
286		} else {
287			if (rate[i] < 50000) {
288				fm[i] = CS42XX8_FM_SINGLE;
289			} else if (rate[i] > 50000 && rate[i] < 100000) {
290				fm[i] = CS42XX8_FM_DOUBLE;
291			} else if (rate[i] > 100000 && rate[i] < 200000) {
292				fm[i] = CS42XX8_FM_QUAD;
293			} else {
294				dev_err(component->dev,
295					"unsupported sample rate\n");
296				return -EINVAL;
297			}
298		}
299	}
300
301	for (i = 0; i < ARRAY_SIZE(cs42xx8_ratios); i++) {
302		/* Is the ratio[tx] valid ? */
303		condition1 = ((fm[tx] == CS42XX8_FM_AUTO) ?
304			(cs42xx8_ratios[i].ratio[0] == ratio[tx] ||
305			cs42xx8_ratios[i].ratio[1] == ratio[tx] ||
306			cs42xx8_ratios[i].ratio[2] == ratio[tx]) :
307			(cs42xx8_ratios[i].ratio[fm[tx]] == ratio[tx])) &&
308			cs42xx8->sysclk >= cs42xx8_ratios[i].min_mclk &&
309			cs42xx8->sysclk <= cs42xx8_ratios[i].max_mclk;
310
311		if (!ratio[tx])
312			condition1 = true;
313
314		/* Is the ratio[!tx] valid ? */
315		condition2 = ((fm[!tx] == CS42XX8_FM_AUTO) ?
316			(cs42xx8_ratios[i].ratio[0] == ratio[!tx] ||
317			cs42xx8_ratios[i].ratio[1] == ratio[!tx] ||
318			cs42xx8_ratios[i].ratio[2] == ratio[!tx]) :
319			(cs42xx8_ratios[i].ratio[fm[!tx]] == ratio[!tx]));
320
321		if (!ratio[!tx])
322			condition2 = true;
323
324		/*
325		 * Both ratio[tx] and ratio[!tx] is valid, then we get
326		 * a proper MFreq.
327		 */
328		if (condition1 && condition2)
329			break;
330	}
331
332	if (i == ARRAY_SIZE(cs42xx8_ratios)) {
333		dev_err(component->dev, "unsupported sysclk ratio\n");
334		return -EINVAL;
335	}
336
337	cs42xx8->rate[tx] = params_rate(params);
338
339	mask = CS42XX8_FUNCMOD_MFREQ_MASK;
340	val = cs42xx8_ratios[i].mfreq;
341
342	regmap_update_bits(cs42xx8->regmap, CS42XX8_FUNCMOD,
343			   CS42XX8_FUNCMOD_xC_FM_MASK(tx) | mask,
344			   CS42XX8_FUNCMOD_xC_FM(tx, fm[tx]) | val);
345
346	return 0;
347}
348
349static int cs42xx8_hw_free(struct snd_pcm_substream *substream,
350			   struct snd_soc_dai *dai)
351{
352	struct snd_soc_component *component = dai->component;
353	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
354	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
355
356	/* Clear stored rate */
357	cs42xx8->rate[tx] = 0;
358
359	regmap_update_bits(cs42xx8->regmap, CS42XX8_FUNCMOD,
360			   CS42XX8_FUNCMOD_xC_FM_MASK(tx),
361			   CS42XX8_FUNCMOD_xC_FM(tx, CS42XX8_FM_AUTO));
362	return 0;
363}
364
365static int cs42xx8_mute(struct snd_soc_dai *dai, int mute, int direction)
366{
367	struct snd_soc_component *component = dai->component;
368	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
369	u8 dac_unmute = cs42xx8->tx_channels ?
370		        ~((0x1 << cs42xx8->tx_channels) - 1) : 0;
371
372	regmap_write(cs42xx8->regmap, CS42XX8_DACMUTE,
373		     mute ? CS42XX8_DACMUTE_ALL : dac_unmute);
374
375	return 0;
376}
377
378static const struct snd_soc_dai_ops cs42xx8_dai_ops = {
379	.set_fmt	= cs42xx8_set_dai_fmt,
380	.set_sysclk	= cs42xx8_set_dai_sysclk,
381	.hw_params	= cs42xx8_hw_params,
382	.hw_free	= cs42xx8_hw_free,
383	.mute_stream	= cs42xx8_mute,
384	.no_capture_mute = 1,
385};
386
387static struct snd_soc_dai_driver cs42xx8_dai = {
388	.playback = {
389		.stream_name = "Playback",
390		.channels_min = 1,
391		.channels_max = 8,
392		.rates = SNDRV_PCM_RATE_8000_192000,
393		.formats = CS42XX8_FORMATS,
394	},
395	.capture = {
396		.stream_name = "Capture",
397		.channels_min = 1,
398		.rates = SNDRV_PCM_RATE_8000_192000,
399		.formats = CS42XX8_FORMATS,
400	},
401	.ops = &cs42xx8_dai_ops,
402};
403
404static const struct reg_default cs42xx8_reg[] = {
405	{ 0x02, 0x00 },   /* Power Control */
406	{ 0x03, 0xF0 },   /* Functional Mode */
407	{ 0x04, 0x46 },   /* Interface Formats */
408	{ 0x05, 0x00 },   /* ADC Control & DAC De-Emphasis */
409	{ 0x06, 0x10 },   /* Transition Control */
410	{ 0x07, 0x00 },   /* DAC Channel Mute */
411	{ 0x08, 0x00 },   /* Volume Control AOUT1 */
412	{ 0x09, 0x00 },   /* Volume Control AOUT2 */
413	{ 0x0a, 0x00 },   /* Volume Control AOUT3 */
414	{ 0x0b, 0x00 },   /* Volume Control AOUT4 */
415	{ 0x0c, 0x00 },   /* Volume Control AOUT5 */
416	{ 0x0d, 0x00 },   /* Volume Control AOUT6 */
417	{ 0x0e, 0x00 },   /* Volume Control AOUT7 */
418	{ 0x0f, 0x00 },   /* Volume Control AOUT8 */
419	{ 0x10, 0x00 },   /* DAC Channel Invert */
420	{ 0x11, 0x00 },   /* Volume Control AIN1 */
421	{ 0x12, 0x00 },   /* Volume Control AIN2 */
422	{ 0x13, 0x00 },   /* Volume Control AIN3 */
423	{ 0x14, 0x00 },   /* Volume Control AIN4 */
424	{ 0x15, 0x00 },   /* Volume Control AIN5 */
425	{ 0x16, 0x00 },   /* Volume Control AIN6 */
426	{ 0x17, 0x00 },   /* ADC Channel Invert */
427	{ 0x18, 0x00 },   /* Status Control */
428	{ 0x1a, 0x00 },   /* Status Mask */
429	{ 0x1b, 0x00 },   /* MUTEC Pin Control */
430};
431
432static bool cs42xx8_volatile_register(struct device *dev, unsigned int reg)
433{
434	switch (reg) {
435	case CS42XX8_STATUS:
436		return true;
437	default:
438		return false;
439	}
440}
441
442static bool cs42xx8_writeable_register(struct device *dev, unsigned int reg)
443{
444	switch (reg) {
445	case CS42XX8_CHIPID:
446	case CS42XX8_STATUS:
447		return false;
448	default:
449		return true;
450	}
451}
452
453const struct regmap_config cs42xx8_regmap_config = {
454	.reg_bits = 8,
455	.val_bits = 8,
456
457	.max_register = CS42XX8_LASTREG,
458	.reg_defaults = cs42xx8_reg,
459	.num_reg_defaults = ARRAY_SIZE(cs42xx8_reg),
460	.volatile_reg = cs42xx8_volatile_register,
461	.writeable_reg = cs42xx8_writeable_register,
462	.cache_type = REGCACHE_RBTREE,
463};
464EXPORT_SYMBOL_GPL(cs42xx8_regmap_config);
465
466static int cs42xx8_component_probe(struct snd_soc_component *component)
467{
468	struct cs42xx8_priv *cs42xx8 = snd_soc_component_get_drvdata(component);
469	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
470
471	switch (cs42xx8->drvdata->num_adcs) {
472	case 3:
473		snd_soc_add_component_controls(component, cs42xx8_adc3_snd_controls,
474					ARRAY_SIZE(cs42xx8_adc3_snd_controls));
475		snd_soc_dapm_new_controls(dapm, cs42xx8_adc3_dapm_widgets,
476					ARRAY_SIZE(cs42xx8_adc3_dapm_widgets));
477		snd_soc_dapm_add_routes(dapm, cs42xx8_adc3_dapm_routes,
478					ARRAY_SIZE(cs42xx8_adc3_dapm_routes));
479		break;
480	default:
481		break;
482	}
483
484	/* Mute all DAC channels */
485	regmap_write(cs42xx8->regmap, CS42XX8_DACMUTE, CS42XX8_DACMUTE_ALL);
486
487	return 0;
488}
489
490static const struct snd_soc_component_driver cs42xx8_driver = {
491	.probe			= cs42xx8_component_probe,
492	.controls		= cs42xx8_snd_controls,
493	.num_controls		= ARRAY_SIZE(cs42xx8_snd_controls),
494	.dapm_widgets		= cs42xx8_dapm_widgets,
495	.num_dapm_widgets	= ARRAY_SIZE(cs42xx8_dapm_widgets),
496	.dapm_routes		= cs42xx8_dapm_routes,
497	.num_dapm_routes	= ARRAY_SIZE(cs42xx8_dapm_routes),
498	.use_pmdown_time	= 1,
499	.endianness		= 1,
500	.non_legacy_dai_naming	= 1,
501};
502
503const struct cs42xx8_driver_data cs42448_data = {
504	.name = "cs42448",
505	.num_adcs = 3,
506};
507EXPORT_SYMBOL_GPL(cs42448_data);
508
509const struct cs42xx8_driver_data cs42888_data = {
510	.name = "cs42888",
511	.num_adcs = 2,
512};
513EXPORT_SYMBOL_GPL(cs42888_data);
514
515const struct of_device_id cs42xx8_of_match[] = {
516	{ .compatible = "cirrus,cs42448", .data = &cs42448_data, },
517	{ .compatible = "cirrus,cs42888", .data = &cs42888_data, },
518	{ /* sentinel */ }
519};
520MODULE_DEVICE_TABLE(of, cs42xx8_of_match);
521EXPORT_SYMBOL_GPL(cs42xx8_of_match);
522
523int cs42xx8_probe(struct device *dev, struct regmap *regmap)
524{
525	const struct of_device_id *of_id;
526	struct cs42xx8_priv *cs42xx8;
527	int ret, val, i;
528
529	if (IS_ERR(regmap)) {
530		ret = PTR_ERR(regmap);
531		dev_err(dev, "failed to allocate regmap: %d\n", ret);
532		return ret;
533	}
534
535	cs42xx8 = devm_kzalloc(dev, sizeof(*cs42xx8), GFP_KERNEL);
536	if (cs42xx8 == NULL)
537		return -ENOMEM;
538
539	cs42xx8->regmap = regmap;
540	dev_set_drvdata(dev, cs42xx8);
541
542	of_id = of_match_device(cs42xx8_of_match, dev);
543	if (of_id)
544		cs42xx8->drvdata = of_id->data;
545
546	if (!cs42xx8->drvdata) {
547		dev_err(dev, "failed to find driver data\n");
548		return -EINVAL;
549	}
550
551	cs42xx8->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
552							GPIOD_OUT_HIGH);
553	if (IS_ERR(cs42xx8->gpiod_reset))
554		return PTR_ERR(cs42xx8->gpiod_reset);
555
556	gpiod_set_value_cansleep(cs42xx8->gpiod_reset, 0);
557
558	cs42xx8->clk = devm_clk_get(dev, "mclk");
559	if (IS_ERR(cs42xx8->clk)) {
560		dev_err(dev, "failed to get the clock: %ld\n",
561				PTR_ERR(cs42xx8->clk));
562		return -EINVAL;
563	}
564
565	cs42xx8->sysclk = clk_get_rate(cs42xx8->clk);
566
567	for (i = 0; i < ARRAY_SIZE(cs42xx8->supplies); i++)
568		cs42xx8->supplies[i].supply = cs42xx8_supply_names[i];
569
570	ret = devm_regulator_bulk_get(dev,
571			ARRAY_SIZE(cs42xx8->supplies), cs42xx8->supplies);
572	if (ret) {
573		dev_err(dev, "failed to request supplies: %d\n", ret);
574		return ret;
575	}
576
577	ret = regulator_bulk_enable(ARRAY_SIZE(cs42xx8->supplies),
578				    cs42xx8->supplies);
579	if (ret) {
580		dev_err(dev, "failed to enable supplies: %d\n", ret);
581		return ret;
582	}
583
584	/* Make sure hardware reset done */
585	msleep(5);
586
587	/* Validate the chip ID */
588	ret = regmap_read(cs42xx8->regmap, CS42XX8_CHIPID, &val);
589	if (ret < 0) {
590		dev_err(dev, "failed to get device ID, ret = %d", ret);
591		goto err_enable;
592	}
593
594	/* The top four bits of the chip ID should be 0000 */
595	if (((val & CS42XX8_CHIPID_CHIP_ID_MASK) >> 4) != 0x00) {
596		dev_err(dev, "unmatched chip ID: %d\n",
597			(val & CS42XX8_CHIPID_CHIP_ID_MASK) >> 4);
598		ret = -EINVAL;
599		goto err_enable;
600	}
601
602	dev_info(dev, "found device, revision %X\n",
603			val & CS42XX8_CHIPID_REV_ID_MASK);
604
605	cs42xx8_dai.name = cs42xx8->drvdata->name;
606
607	/* Each adc supports stereo input */
608	cs42xx8_dai.capture.channels_max = cs42xx8->drvdata->num_adcs * 2;
609
610	ret = devm_snd_soc_register_component(dev, &cs42xx8_driver, &cs42xx8_dai, 1);
611	if (ret) {
612		dev_err(dev, "failed to register component:%d\n", ret);
613		goto err_enable;
614	}
615
616	regcache_cache_only(cs42xx8->regmap, true);
617
618err_enable:
619	regulator_bulk_disable(ARRAY_SIZE(cs42xx8->supplies),
620			       cs42xx8->supplies);
621
622	return ret;
623}
624EXPORT_SYMBOL_GPL(cs42xx8_probe);
625
626#ifdef CONFIG_PM
627static int cs42xx8_runtime_resume(struct device *dev)
628{
629	struct cs42xx8_priv *cs42xx8 = dev_get_drvdata(dev);
630	int ret;
631
632	ret = clk_prepare_enable(cs42xx8->clk);
633	if (ret) {
634		dev_err(dev, "failed to enable mclk: %d\n", ret);
635		return ret;
636	}
637
638	gpiod_set_value_cansleep(cs42xx8->gpiod_reset, 0);
639
640	ret = regulator_bulk_enable(ARRAY_SIZE(cs42xx8->supplies),
641				    cs42xx8->supplies);
642	if (ret) {
643		dev_err(dev, "failed to enable supplies: %d\n", ret);
644		goto err_clk;
645	}
646
647	/* Make sure hardware reset done */
648	msleep(5);
649
650	regcache_cache_only(cs42xx8->regmap, false);
651	regcache_mark_dirty(cs42xx8->regmap);
652
653	ret = regcache_sync(cs42xx8->regmap);
654	if (ret) {
655		dev_err(dev, "failed to sync regmap: %d\n", ret);
656		goto err_bulk;
657	}
658
659	return 0;
660
661err_bulk:
662	regulator_bulk_disable(ARRAY_SIZE(cs42xx8->supplies),
663			       cs42xx8->supplies);
664err_clk:
665	clk_disable_unprepare(cs42xx8->clk);
666
667	return ret;
668}
669
670static int cs42xx8_runtime_suspend(struct device *dev)
671{
672	struct cs42xx8_priv *cs42xx8 = dev_get_drvdata(dev);
673
674	regcache_cache_only(cs42xx8->regmap, true);
675
676	regulator_bulk_disable(ARRAY_SIZE(cs42xx8->supplies),
677			       cs42xx8->supplies);
678
679	gpiod_set_value_cansleep(cs42xx8->gpiod_reset, 1);
680
681	clk_disable_unprepare(cs42xx8->clk);
682
683	return 0;
684}
685#endif
686
687const struct dev_pm_ops cs42xx8_pm = {
688	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
689				pm_runtime_force_resume)
690	SET_RUNTIME_PM_OPS(cs42xx8_runtime_suspend, cs42xx8_runtime_resume, NULL)
691};
692EXPORT_SYMBOL_GPL(cs42xx8_pm);
693
694MODULE_DESCRIPTION("Cirrus Logic CS42448/CS42888 ALSA SoC Codec Driver");
695MODULE_AUTHOR("Freescale Semiconductor, Inc.");
696MODULE_LICENSE("GPL");