blob: cd4a9ee38e46836edfcb67a5ce13c67a43b21469 [file] [log] [blame]
Timur Tabib0c813c2007-07-31 18:18:44 +02001/*
2 * CS4270 ALSA SoC (ASoC) codec driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
Timur Tabiff7bf022009-01-30 11:14:49 -06006 * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed
7 * under the terms of the GNU General Public License version 2. This
8 * program is licensed "as is" without any warranty of any kind, whether
9 * express or implied.
Timur Tabib0c813c2007-07-31 18:18:44 +020010 *
11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
12 *
13 * Current features/limitations:
14 *
Timur Tabiff637d32009-01-22 18:23:39 -060015 * 1) Software mode is supported. Stand-alone mode is not supported.
Timur Tabib0c813c2007-07-31 18:18:44 +020016 * 2) Only I2C is supported, not SPI
17 * 3) Only Master mode is supported, not Slave.
18 * 4) The machine driver's 'startup' function must call
19 * cs4270_set_dai_sysclk() with the value of MCLK.
20 * 5) Only I2S and left-justified modes are supported
21 * 6) Power management is not supported
22 * 7) The only supported control is volume and hardware mute (if enabled)
23 */
24
25#include <linux/module.h>
26#include <linux/platform_device.h>
Timur Tabib0c813c2007-07-31 18:18:44 +020027#include <sound/core.h>
28#include <sound/soc.h>
29#include <sound/initval.h>
30#include <linux/i2c.h>
31
Mark Brown01e097d2009-01-23 15:07:45 +000032#include "cs4270.h"
33
Timur Tabib0c813c2007-07-31 18:18:44 +020034/*
35 * The codec isn't really big-endian or little-endian, since the I2S
36 * interface requires data to be sent serially with the MSbit first.
37 * However, to support BE and LE I2S devices, we specify both here. That
38 * way, ALSA will always match the bit patterns.
39 */
40#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
41 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
42 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
43 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
44 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
45 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
46
Timur Tabib0c813c2007-07-31 18:18:44 +020047/* CS4270 registers addresses */
48#define CS4270_CHIPID 0x01 /* Chip ID */
49#define CS4270_PWRCTL 0x02 /* Power Control */
50#define CS4270_MODE 0x03 /* Mode Control */
51#define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
52#define CS4270_TRANS 0x05 /* Transition Control */
53#define CS4270_MUTE 0x06 /* Mute Control */
54#define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
55#define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
56
57#define CS4270_FIRSTREG 0x01
58#define CS4270_LASTREG 0x08
59#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
60
61/* Bit masks for the CS4270 registers */
62#define CS4270_CHIPID_ID 0xF0
63#define CS4270_CHIPID_REV 0x0F
64#define CS4270_PWRCTL_FREEZE 0x80
65#define CS4270_PWRCTL_PDN_ADC 0x20
66#define CS4270_PWRCTL_PDN_DAC 0x02
67#define CS4270_PWRCTL_PDN 0x01
68#define CS4270_MODE_SPEED_MASK 0x30
69#define CS4270_MODE_1X 0x00
70#define CS4270_MODE_2X 0x10
71#define CS4270_MODE_4X 0x20
72#define CS4270_MODE_SLAVE 0x30
73#define CS4270_MODE_DIV_MASK 0x0E
74#define CS4270_MODE_DIV1 0x00
75#define CS4270_MODE_DIV15 0x02
76#define CS4270_MODE_DIV2 0x04
77#define CS4270_MODE_DIV3 0x06
78#define CS4270_MODE_DIV4 0x08
79#define CS4270_MODE_POPGUARD 0x01
80#define CS4270_FORMAT_FREEZE_A 0x80
81#define CS4270_FORMAT_FREEZE_B 0x40
82#define CS4270_FORMAT_LOOPBACK 0x20
83#define CS4270_FORMAT_DAC_MASK 0x18
84#define CS4270_FORMAT_DAC_LJ 0x00
85#define CS4270_FORMAT_DAC_I2S 0x08
86#define CS4270_FORMAT_DAC_RJ16 0x18
87#define CS4270_FORMAT_DAC_RJ24 0x10
88#define CS4270_FORMAT_ADC_MASK 0x01
89#define CS4270_FORMAT_ADC_LJ 0x00
90#define CS4270_FORMAT_ADC_I2S 0x01
91#define CS4270_TRANS_ONE_VOL 0x80
92#define CS4270_TRANS_SOFT 0x40
93#define CS4270_TRANS_ZERO 0x20
94#define CS4270_TRANS_INV_ADC_A 0x08
95#define CS4270_TRANS_INV_ADC_B 0x10
96#define CS4270_TRANS_INV_DAC_A 0x02
97#define CS4270_TRANS_INV_DAC_B 0x04
98#define CS4270_TRANS_DEEMPH 0x01
99#define CS4270_MUTE_AUTO 0x20
100#define CS4270_MUTE_ADC_A 0x08
101#define CS4270_MUTE_ADC_B 0x10
102#define CS4270_MUTE_POLARITY 0x04
103#define CS4270_MUTE_DAC_A 0x01
104#define CS4270_MUTE_DAC_B 0x02
105
Timur Tabi0db4d072009-01-23 16:31:19 -0600106/* Private data for the CS4270 */
107struct cs4270_private {
108 struct snd_soc_codec codec;
109 u8 reg_cache[CS4270_NUMREGS];
110 unsigned int mclk; /* Input frequency of the MCLK pin */
111 unsigned int mode; /* The mode (I2S or left-justified) */
112};
113
Timur Tabiff7bf022009-01-30 11:14:49 -0600114/**
115 * struct cs4270_mode_ratios - clock ratio tables
116 * @ratio: the ratio of MCLK to the sample rate
117 * @speed_mode: the Speed Mode bits to set in the Mode Control register for
118 * this ratio
119 * @mclk: the Ratio Select bits to set in the Mode Control register for this
120 * ratio
Timur Tabi84323952007-12-18 15:42:53 +0100121 *
122 * The data for this chart is taken from Table 5 of the CS4270 reference
123 * manual.
124 *
125 * This table is used to determine how to program the Mode Control register.
126 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
127 * rates the CS4270 currently supports.
128 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600129 * @speed_mode is the corresponding bit pattern to be written to the
Timur Tabi84323952007-12-18 15:42:53 +0100130 * MODE bits of the Mode Control Register
131 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600132 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
Timur Tabi84323952007-12-18 15:42:53 +0100133 * the Mode Control Register.
134 *
135 * In situations where a single ratio is represented by multiple speed
136 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
137 * double-speed instead of quad-speed. However, the CS4270 errata states
Timur Tabiff7bf022009-01-30 11:14:49 -0600138 * that divide-By-1.5 can cause failures, so we avoid that mode where
Timur Tabi84323952007-12-18 15:42:53 +0100139 * possible.
140 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600141 * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
142 * work if Vd is 3.3V. If this effects you, select the
Timur Tabi84323952007-12-18 15:42:53 +0100143 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
144 * never select any sample rates that require divide-by-1.5.
145 */
Timur Tabiff7bf022009-01-30 11:14:49 -0600146struct cs4270_mode_ratios {
Timur Tabi84323952007-12-18 15:42:53 +0100147 unsigned int ratio;
148 u8 speed_mode;
149 u8 mclk;
Timur Tabiff7bf022009-01-30 11:14:49 -0600150};
151
Timur Tabid9fb7fb2009-02-02 14:50:45 -0600152static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
Timur Tabi84323952007-12-18 15:42:53 +0100153 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
154#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
155 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
156#endif
157 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
158 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
159 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
160 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
161 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
162 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
163 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
164};
165
166/* The number of MCLK/LRCK ratios supported by the CS4270 */
167#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
168
Timur Tabiff7bf022009-01-30 11:14:49 -0600169/**
170 * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
171 * @codec_dai: the codec DAI
172 * @clk_id: the clock ID (ignored)
173 * @freq: the MCLK input frequency
174 * @dir: the clock direction (ignored)
Timur Tabi84323952007-12-18 15:42:53 +0100175 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600176 * This function is used to tell the codec driver what the input MCLK
177 * frequency is.
Timur Tabi84323952007-12-18 15:42:53 +0100178 *
179 * The value of MCLK is used to determine which sample rates are supported
180 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
Timur Tabiff7bf022009-01-30 11:14:49 -0600181 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
Timur Tabi84323952007-12-18 15:42:53 +0100182 *
183 * This function calculates the nine ratios and determines which ones match
184 * a standard sample rate. If there's a match, then it is added to the list
Timur Tabiff7bf022009-01-30 11:14:49 -0600185 * of supported sample rates.
Timur Tabi84323952007-12-18 15:42:53 +0100186 *
187 * This function must be called by the machine driver's 'startup' function,
188 * otherwise the list of supported sample rates will not be available in
189 * time for ALSA.
Timur Tabi84323952007-12-18 15:42:53 +0100190 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100191static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100192 int clk_id, unsigned int freq, int dir)
193{
194 struct snd_soc_codec *codec = codec_dai->codec;
195 struct cs4270_private *cs4270 = codec->private_data;
196 unsigned int rates = 0;
197 unsigned int rate_min = -1;
198 unsigned int rate_max = 0;
199 unsigned int i;
200
201 cs4270->mclk = freq;
202
203 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
204 unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
205 rates |= snd_pcm_rate_to_rate_bit(rate);
206 if (rate < rate_min)
207 rate_min = rate;
208 if (rate > rate_max)
209 rate_max = rate;
210 }
211 /* FIXME: soc should support a rate list */
212 rates &= ~SNDRV_PCM_RATE_KNOT;
213
214 if (!rates) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600215 dev_err(codec->dev, "could not find a valid sample rate\n");
Timur Tabi84323952007-12-18 15:42:53 +0100216 return -EINVAL;
217 }
218
219 codec_dai->playback.rates = rates;
220 codec_dai->playback.rate_min = rate_min;
221 codec_dai->playback.rate_max = rate_max;
222
223 codec_dai->capture.rates = rates;
224 codec_dai->capture.rate_min = rate_min;
225 codec_dai->capture.rate_max = rate_max;
226
227 return 0;
228}
229
Timur Tabiff7bf022009-01-30 11:14:49 -0600230/**
231 * cs4270_set_dai_fmt - configure the codec for the selected audio format
232 * @codec_dai: the codec DAI
233 * @format: a SND_SOC_DAIFMT_x value indicating the data format
Timur Tabi84323952007-12-18 15:42:53 +0100234 *
235 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
236 * codec accordingly.
237 *
238 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
239 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
240 * data for playback only, but ASoC currently does not support different
241 * formats for playback vs. record.
242 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100243static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100244 unsigned int format)
245{
246 struct snd_soc_codec *codec = codec_dai->codec;
247 struct cs4270_private *cs4270 = codec->private_data;
248 int ret = 0;
249
250 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
251 case SND_SOC_DAIFMT_I2S:
252 case SND_SOC_DAIFMT_LEFT_J:
253 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
254 break;
255 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600256 dev_err(codec->dev, "invalid dai format\n");
Timur Tabi84323952007-12-18 15:42:53 +0100257 ret = -EINVAL;
258 }
259
260 return ret;
261}
262
Timur Tabiff7bf022009-01-30 11:14:49 -0600263/**
264 * cs4270_fill_cache - pre-fill the CS4270 register cache.
265 * @codec: the codec for this CS4270
266 *
267 * This function fills in the CS4270 register cache by reading the register
268 * values from the hardware.
269 *
270 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
271 * After the initial read to pre-fill the cache, the CS4270 never updates
272 * the register values, so we won't have a cache coherency problem.
Timur Tabib0c813c2007-07-31 18:18:44 +0200273 *
274 * We use the auto-increment feature of the CS4270 to read all registers in
275 * one shot.
276 */
277static int cs4270_fill_cache(struct snd_soc_codec *codec)
278{
279 u8 *cache = codec->reg_cache;
280 struct i2c_client *i2c_client = codec->control_data;
281 s32 length;
282
283 length = i2c_smbus_read_i2c_block_data(i2c_client,
284 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
285
286 if (length != CS4270_NUMREGS) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600287 dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
Timur Tabib0c813c2007-07-31 18:18:44 +0200288 i2c_client->addr);
289 return -EIO;
290 }
291
292 return 0;
293}
294
Timur Tabiff7bf022009-01-30 11:14:49 -0600295/**
296 * cs4270_read_reg_cache - read from the CS4270 register cache.
297 * @codec: the codec for this CS4270
298 * @reg: the register to read
299 *
300 * This function returns the value for a given register. It reads only from
301 * the register cache, not the hardware itself.
Timur Tabib0c813c2007-07-31 18:18:44 +0200302 *
303 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
304 * After the initial read to pre-fill the cache, the CS4270 never updates
Timur Tabiff7bf022009-01-30 11:14:49 -0600305 * the register values, so we won't have a cache coherency problem.
Timur Tabib0c813c2007-07-31 18:18:44 +0200306 */
307static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
308 unsigned int reg)
309{
310 u8 *cache = codec->reg_cache;
311
312 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
313 return -EIO;
314
315 return cache[reg - CS4270_FIRSTREG];
316}
317
Timur Tabiff7bf022009-01-30 11:14:49 -0600318/**
319 * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
320 * @codec: the codec for this CS4270
321 * @reg: the register to write
322 * @value: the value to write to the register
Timur Tabib0c813c2007-07-31 18:18:44 +0200323 *
324 * This function writes the given value to the given CS4270 register, and
325 * also updates the register cache.
326 *
327 * Note that we don't use the hw_write function pointer of snd_soc_codec.
328 * That's because it's too clunky: the hw_write_t prototype does not match
329 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
330 */
331static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
332 unsigned int value)
333{
Timur Tabibfc4e862007-09-11 00:45:50 +0200334 u8 *cache = codec->reg_cache;
335
Timur Tabib0c813c2007-07-31 18:18:44 +0200336 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
337 return -EIO;
338
Timur Tabibfc4e862007-09-11 00:45:50 +0200339 /* Only perform an I2C operation if the new value is different */
340 if (cache[reg - CS4270_FIRSTREG] != value) {
341 struct i2c_client *client = codec->control_data;
342 if (i2c_smbus_write_byte_data(client, reg, value)) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600343 dev_err(codec->dev, "i2c write failed\n");
Timur Tabibfc4e862007-09-11 00:45:50 +0200344 return -EIO;
345 }
346
Timur Tabib0c813c2007-07-31 18:18:44 +0200347 /* We've written to the hardware, so update the cache */
Timur Tabib0c813c2007-07-31 18:18:44 +0200348 cache[reg - CS4270_FIRSTREG] = value;
Timur Tabib0c813c2007-07-31 18:18:44 +0200349 }
Timur Tabibfc4e862007-09-11 00:45:50 +0200350
351 return 0;
Timur Tabib0c813c2007-07-31 18:18:44 +0200352}
353
Timur Tabiff7bf022009-01-30 11:14:49 -0600354/**
355 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
356 * @substream: the audio stream
357 * @params: the hardware parameters to set
358 * @dai: the SOC DAI (ignored)
Timur Tabib0c813c2007-07-31 18:18:44 +0200359 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600360 * This function programs the hardware with the values provided.
361 * Specifically, the sample rate and the data format.
362 *
363 * The .ops functions are used to provide board-specific data, like input
364 * frequencies, to this driver. This function takes that information,
Timur Tabib0c813c2007-07-31 18:18:44 +0200365 * combines it with the hardware parameters provided, and programs the
366 * hardware accordingly.
367 */
368static int cs4270_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000369 struct snd_pcm_hw_params *params,
370 struct snd_soc_dai *dai)
Timur Tabib0c813c2007-07-31 18:18:44 +0200371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000374 struct snd_soc_codec *codec = socdev->card->codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200375 struct cs4270_private *cs4270 = codec->private_data;
Roel Kluine34ba212008-04-17 18:58:34 +0200376 int ret;
Timur Tabib0c813c2007-07-31 18:18:44 +0200377 unsigned int i;
378 unsigned int rate;
379 unsigned int ratio;
380 int reg;
381
382 /* Figure out which MCLK/LRCK ratio to use */
383
384 rate = params_rate(params); /* Sampling rate, in Hz */
385 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
386
Timur Tabi9dbd6272007-08-01 12:22:07 +0200387 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
Timur Tabi84323952007-12-18 15:42:53 +0100388 if (cs4270_mode_ratios[i].ratio == ratio)
Timur Tabib0c813c2007-07-31 18:18:44 +0200389 break;
390 }
391
Timur Tabi9dbd6272007-08-01 12:22:07 +0200392 if (i == NUM_MCLK_RATIOS) {
Timur Tabib0c813c2007-07-31 18:18:44 +0200393 /* We did not find a matching ratio */
Timur Tabia6c255e2009-02-02 15:08:29 -0600394 dev_err(codec->dev, "could not find matching ratio\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200395 return -EINVAL;
396 }
397
Timur Tabid5e9ba12009-02-03 11:09:32 -0600398 /* Set the sample rate */
Timur Tabib0c813c2007-07-31 18:18:44 +0200399
400 reg = snd_soc_read(codec, CS4270_MODE);
401 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
402 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
403
404 ret = snd_soc_write(codec, CS4270_MODE, reg);
405 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600406 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200407 return ret;
408 }
409
Timur Tabid5e9ba12009-02-03 11:09:32 -0600410 /* Set the DAI format */
Timur Tabib0c813c2007-07-31 18:18:44 +0200411
412 reg = snd_soc_read(codec, CS4270_FORMAT);
413 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
414
415 switch (cs4270->mode) {
416 case SND_SOC_DAIFMT_I2S:
417 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
418 break;
419 case SND_SOC_DAIFMT_LEFT_J:
420 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
421 break;
422 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600423 dev_err(codec->dev, "unknown dai format\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200424 return -EINVAL;
425 }
426
427 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
428 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600429 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200430 return ret;
431 }
432
Timur Tabib0c813c2007-07-31 18:18:44 +0200433 return ret;
434}
435
Timur Tabiff7bf022009-01-30 11:14:49 -0600436/**
437 * cs4270_mute - enable/disable the CS4270 external mute
438 * @dai: the SOC DAI
439 * @mute: 0 = disable mute, 1 = enable mute
Timur Tabib0c813c2007-07-31 18:18:44 +0200440 *
441 * This function toggles the mute bits in the MUTE register. The CS4270's
442 * mute capability is intended for external muting circuitry, so if the
443 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
444 * then this function will do nothing.
445 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100446static int cs4270_mute(struct snd_soc_dai *dai, int mute)
Timur Tabib0c813c2007-07-31 18:18:44 +0200447{
448 struct snd_soc_codec *codec = dai->codec;
449 int reg6;
450
451 reg6 = snd_soc_read(codec, CS4270_MUTE);
452
453 if (mute)
Timur Tabid5e9ba12009-02-03 11:09:32 -0600454 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
Timur Tabib0c813c2007-07-31 18:18:44 +0200455 else
Timur Tabid5e9ba12009-02-03 11:09:32 -0600456 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
Timur Tabib0c813c2007-07-31 18:18:44 +0200457
458 return snd_soc_write(codec, CS4270_MUTE, reg6);
459}
Timur Tabib0c813c2007-07-31 18:18:44 +0200460
Timur Tabib0c813c2007-07-31 18:18:44 +0200461/* A list of non-DAPM controls that the CS4270 supports */
462static const struct snd_kcontrol_new cs4270_snd_controls[] = {
463 SOC_DOUBLE_R("Master Playback Volume",
Timur Tabid5e9ba12009-02-03 11:09:32 -0600464 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
465 SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
466 SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
467 SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
468 SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
469 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
470 SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 0)
Timur Tabib0c813c2007-07-31 18:18:44 +0200471};
472
Timur Tabib0c813c2007-07-31 18:18:44 +0200473/*
Timur Tabiff7bf022009-01-30 11:14:49 -0600474 * cs4270_codec - global variable to store codec for the ASoC probe function
Timur Tabib0c813c2007-07-31 18:18:44 +0200475 *
476 * If struct i2c_driver had a private_data field, we wouldn't need to use
Timur Tabi04eb0932009-01-29 14:28:37 -0600477 * cs4270_codec. This is the only way to pass the codec structure from
478 * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
479 * way to synchronize these two functions. cs4270_i2c_probe() can be called
480 * multiple times before cs4270_probe() is called even once. So for now, we
481 * also only allow cs4270_i2c_probe() to be run once. That means that we do
482 * not support more than one cs4270 device in the system, at least for now.
Timur Tabib0c813c2007-07-31 18:18:44 +0200483 */
Timur Tabi04eb0932009-01-29 14:28:37 -0600484static struct snd_soc_codec *cs4270_codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200485
Liam Girdwoode550e172008-07-07 16:07:52 +0100486struct snd_soc_dai cs4270_dai = {
Timur Tabi0db4d072009-01-23 16:31:19 -0600487 .name = "cs4270",
Timur Tabib0c813c2007-07-31 18:18:44 +0200488 .playback = {
489 .stream_name = "Playback",
490 .channels_min = 1,
491 .channels_max = 2,
492 .rates = 0,
493 .formats = CS4270_FORMATS,
494 },
495 .capture = {
496 .stream_name = "Capture",
497 .channels_min = 1,
498 .channels_max = 2,
499 .rates = 0,
500 .formats = CS4270_FORMATS,
501 },
Timur Tabi0db4d072009-01-23 16:31:19 -0600502 .ops = {
503 .hw_params = cs4270_hw_params,
504 .set_sysclk = cs4270_set_dai_sysclk,
505 .set_fmt = cs4270_set_dai_fmt,
506 .digital_mute = cs4270_mute,
507 },
Timur Tabib0c813c2007-07-31 18:18:44 +0200508};
509EXPORT_SYMBOL_GPL(cs4270_dai);
510
Timur Tabiff7bf022009-01-30 11:14:49 -0600511/**
512 * cs4270_probe - ASoC probe function
513 * @pdev: platform device
514 *
515 * This function is called when ASoC has all the pieces it needs to
516 * instantiate a sound driver.
Timur Tabi04eb0932009-01-29 14:28:37 -0600517 */
518static int cs4270_probe(struct platform_device *pdev)
519{
520 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
521 struct snd_soc_codec *codec = cs4270_codec;
522 unsigned int i;
523 int ret;
524
525 /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
526 socdev->card->codec = codec;
527
528 /* Register PCMs */
529 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
530 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600531 dev_err(codec->dev, "failed to create pcms\n");
Timur Tabi04eb0932009-01-29 14:28:37 -0600532 return ret;
533 }
534
535 /* Add the non-DAPM controls */
536 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
537 struct snd_kcontrol *kctrl;
538
539 kctrl = snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
540 if (!kctrl) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600541 dev_err(codec->dev, "error creating control '%s'\n",
Timur Tabi04eb0932009-01-29 14:28:37 -0600542 cs4270_snd_controls[i].name);
543 ret = -ENOMEM;
544 goto error_free_pcms;
545 }
546
547 ret = snd_ctl_add(codec->card, kctrl);
548 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600549 dev_err(codec->dev, "error adding control '%s'\n",
Timur Tabi04eb0932009-01-29 14:28:37 -0600550 cs4270_snd_controls[i].name);
551 goto error_free_pcms;
552 }
553 }
554
555 /* And finally, register the socdev */
556 ret = snd_soc_init_card(socdev);
557 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600558 dev_err(codec->dev, "failed to register card\n");
Timur Tabi04eb0932009-01-29 14:28:37 -0600559 goto error_free_pcms;
560 }
561
562 return 0;
563
564error_free_pcms:
565 snd_soc_free_pcms(socdev);
566
567 return ret;
568}
569
Timur Tabiff7bf022009-01-30 11:14:49 -0600570/**
571 * cs4270_remove - ASoC remove function
572 * @pdev: platform device
573 *
574 * This function is the counterpart to cs4270_probe().
575 */
Timur Tabi04eb0932009-01-29 14:28:37 -0600576static int cs4270_remove(struct platform_device *pdev)
577{
578 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
579
580 snd_soc_free_pcms(socdev);
581
582 return 0;
583};
584
Timur Tabiff7bf022009-01-30 11:14:49 -0600585/**
586 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
587 * @i2c_client: the I2C client object
588 * @id: the I2C device ID (ignored)
Timur Tabib0c813c2007-07-31 18:18:44 +0200589 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600590 * This function is called whenever the I2C subsystem finds a device that
591 * matches the device ID given via a prior call to i2c_add_driver().
Timur Tabib0c813c2007-07-31 18:18:44 +0200592 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600593static int cs4270_i2c_probe(struct i2c_client *i2c_client,
594 const struct i2c_device_id *id)
Timur Tabib0c813c2007-07-31 18:18:44 +0200595{
Timur Tabib0c813c2007-07-31 18:18:44 +0200596 struct snd_soc_codec *codec;
Timur Tabi0db4d072009-01-23 16:31:19 -0600597 struct cs4270_private *cs4270;
Timur Tabid5e9ba12009-02-03 11:09:32 -0600598 unsigned int reg;
Timur Tabi04eb0932009-01-29 14:28:37 -0600599 int ret;
600
601 /* For now, we only support one cs4270 device in the system. See the
602 * comment for cs4270_codec.
603 */
604 if (cs4270_codec) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600605 dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
Timur Tabi04eb0932009-01-29 14:28:37 -0600606 i2c_client->addr);
Timur Tabia6c255e2009-02-02 15:08:29 -0600607 dev_err(&i2c_client->dev, "only one per board allowed\n");
Timur Tabi04eb0932009-01-29 14:28:37 -0600608 /* Should we return something other than ENODEV here? */
609 return -ENODEV;
610 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200611
Timur Tabi0db4d072009-01-23 16:31:19 -0600612 /* Verify that we have a CS4270 */
613
614 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
615 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600616 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
Timur Tabi04eb0932009-01-29 14:28:37 -0600617 i2c_client->addr);
Timur Tabi0db4d072009-01-23 16:31:19 -0600618 return ret;
619 }
620 /* The top four bits of the chip ID should be 1100. */
621 if ((ret & 0xF0) != 0xC0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600622 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
Timur Tabi0db4d072009-01-23 16:31:19 -0600623 i2c_client->addr);
624 return -ENODEV;
625 }
626
Timur Tabia6c255e2009-02-02 15:08:29 -0600627 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
Timur Tabi0db4d072009-01-23 16:31:19 -0600628 i2c_client->addr);
Timur Tabia6c255e2009-02-02 15:08:29 -0600629 dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
Timur Tabib0c813c2007-07-31 18:18:44 +0200630
631 /* Allocate enough space for the snd_soc_codec structure
632 and our private data together. */
Timur Tabi0db4d072009-01-23 16:31:19 -0600633 cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
634 if (!cs4270) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600635 dev_err(&i2c_client->dev, "could not allocate codec\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200636 return -ENOMEM;
637 }
Timur Tabi0db4d072009-01-23 16:31:19 -0600638 codec = &cs4270->codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200639
640 mutex_init(&codec->mutex);
641 INIT_LIST_HEAD(&codec->dapm_widgets);
642 INIT_LIST_HEAD(&codec->dapm_paths);
643
Timur Tabia6c255e2009-02-02 15:08:29 -0600644 codec->dev = &i2c_client->dev;
Timur Tabib0c813c2007-07-31 18:18:44 +0200645 codec->name = "CS4270";
646 codec->owner = THIS_MODULE;
647 codec->dai = &cs4270_dai;
648 codec->num_dai = 1;
Timur Tabi0db4d072009-01-23 16:31:19 -0600649 codec->private_data = cs4270;
650 codec->control_data = i2c_client;
651 codec->read = cs4270_read_reg_cache;
652 codec->write = cs4270_i2c_write;
653 codec->reg_cache = cs4270->reg_cache;
654 codec->reg_cache_size = CS4270_NUMREGS;
Timur Tabib0c813c2007-07-31 18:18:44 +0200655
Timur Tabi0db4d072009-01-23 16:31:19 -0600656 /* The I2C interface is set up, so pre-fill our register cache */
657
658 ret = cs4270_fill_cache(codec);
659 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600660 dev_err(&i2c_client->dev, "failed to fill register cache\n");
Timur Tabi0db4d072009-01-23 16:31:19 -0600661 goto error_free_codec;
662 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200663
Timur Tabid5e9ba12009-02-03 11:09:32 -0600664 /* Disable auto-mute. This feature appears to be buggy. In some
665 * situations, auto-mute will not deactivate when it should, so we want
666 * this feature disabled by default. An application (e.g. alsactl) can
667 * re-enabled it by using the controls.
668 */
669
670 reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
671 reg &= ~CS4270_MUTE_AUTO;
672 ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
673 if (ret < 0) {
674 dev_err(&i2c_client->dev, "i2c write failed\n");
675 return ret;
676 }
677
678 /* Disable automatic volume control. The hardware enables, and it
679 * causes volume change commands to be delayed, sometimes until after
680 * playback has started. An application (e.g. alsactl) can
681 * re-enabled it by using the controls.
682 */
683
684 reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
685 reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
686 ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
687 if (ret < 0) {
688 dev_err(&i2c_client->dev, "i2c write failed\n");
689 return ret;
690 }
691
Timur Tabia6c255e2009-02-02 15:08:29 -0600692 /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
693 * structure for each CS4270 device, but the machine driver needs to
694 * have a pointer to the DAI structure, so for now it must be a global
695 * variable.
696 */
697 cs4270_dai.dev = &i2c_client->dev;
698
Timur Tabi04eb0932009-01-29 14:28:37 -0600699 /* Register the DAI. If all the other ASoC driver have already
700 * registered, then this will call our probe function, so
701 * cs4270_codec needs to be ready.
702 */
Timur Tabia6c255e2009-02-02 15:08:29 -0600703 cs4270_codec = codec;
Timur Tabi04eb0932009-01-29 14:28:37 -0600704 ret = snd_soc_register_dai(&cs4270_dai);
Timur Tabib0c813c2007-07-31 18:18:44 +0200705 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600706 dev_err(&i2c_client->dev, "failed to register DAIe\n");
Jean Delvaree3145df2008-09-30 11:40:37 +0200707 goto error_free_codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200708 }
709
Timur Tabi04eb0932009-01-29 14:28:37 -0600710 i2c_set_clientdata(i2c_client, cs4270);
Jean Delvaree3145df2008-09-30 11:40:37 +0200711
Timur Tabi0db4d072009-01-23 16:31:19 -0600712 return 0;
Jean Delvaree3145df2008-09-30 11:40:37 +0200713
Jean Delvaree3145df2008-09-30 11:40:37 +0200714error_free_codec:
Timur Tabi0db4d072009-01-23 16:31:19 -0600715 kfree(cs4270);
Timur Tabia6c255e2009-02-02 15:08:29 -0600716 cs4270_codec = NULL;
717 cs4270_dai.dev = NULL;
Jean Delvaree3145df2008-09-30 11:40:37 +0200718
Timur Tabib0c813c2007-07-31 18:18:44 +0200719 return ret;
720}
721
Timur Tabiff7bf022009-01-30 11:14:49 -0600722/**
723 * cs4270_i2c_remove - remove an I2C device
724 * @i2c_client: the I2C client object
725 *
726 * This function is the counterpart to cs4270_i2c_probe().
727 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600728static int cs4270_i2c_remove(struct i2c_client *i2c_client)
Timur Tabib0c813c2007-07-31 18:18:44 +0200729{
Timur Tabi04eb0932009-01-29 14:28:37 -0600730 struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
Timur Tabib0c813c2007-07-31 18:18:44 +0200731
Timur Tabi0db4d072009-01-23 16:31:19 -0600732 kfree(cs4270);
Timur Tabia6c255e2009-02-02 15:08:29 -0600733 cs4270_codec = NULL;
734 cs4270_dai.dev = NULL;
Timur Tabib0c813c2007-07-31 18:18:44 +0200735
Timur Tabi0db4d072009-01-23 16:31:19 -0600736 return 0;
737}
738
Timur Tabiff7bf022009-01-30 11:14:49 -0600739/*
740 * cs4270_id - I2C device IDs supported by this driver
741 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600742static struct i2c_device_id cs4270_id[] = {
743 {"cs4270", 0},
744 {}
745};
746MODULE_DEVICE_TABLE(i2c, cs4270_id);
747
Timur Tabiff7bf022009-01-30 11:14:49 -0600748/*
749 * cs4270_i2c_driver - I2C device identification
750 *
751 * This structure tells the I2C subsystem how to identify and support a
752 * given I2C device type.
753 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600754static struct i2c_driver cs4270_i2c_driver = {
755 .driver = {
756 .name = "cs4270",
757 .owner = THIS_MODULE,
758 },
759 .id_table = cs4270_id,
760 .probe = cs4270_i2c_probe,
761 .remove = cs4270_i2c_remove,
762};
763
764/*
Timur Tabib0c813c2007-07-31 18:18:44 +0200765 * ASoC codec device structure
766 *
767 * Assign this variable to the codec_dev field of the machine driver's
768 * snd_soc_device structure.
769 */
770struct snd_soc_codec_device soc_codec_device_cs4270 = {
771 .probe = cs4270_probe,
772 .remove = cs4270_remove
773};
774EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
775
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100776static int __init cs4270_init(void)
Mark Brown64089b82008-12-08 19:17:58 +0000777{
Timur Tabia6c255e2009-02-02 15:08:29 -0600778 pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
Timur Tabi0db4d072009-01-23 16:31:19 -0600779
Timur Tabi04eb0932009-01-29 14:28:37 -0600780 return i2c_add_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000781}
782module_init(cs4270_init);
783
784static void __exit cs4270_exit(void)
785{
Timur Tabi04eb0932009-01-29 14:28:37 -0600786 i2c_del_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000787}
788module_exit(cs4270_exit);
789
Timur Tabib0c813c2007-07-31 18:18:44 +0200790MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
791MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
792MODULE_LICENSE("GPL");