blob: ece6ed6a844ff04ab7af0a7037aa3a12582ab58b [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 *
Daniel Mackb191f632009-03-08 17:51:52 +010015 * - Software mode is supported. Stand-alone mode is not supported.
16 * - Only I2C is supported, not SPI
17 * - Support for master and slave mode
18 * - The machine driver's 'startup' function must call
19 * cs4270_set_dai_sysclk() with the value of MCLK.
20 * - Only I2S and left-justified modes are supported
21 * - Power management is not supported
Timur Tabib0c813c2007-07-31 18:18:44 +020022 */
23
24#include <linux/module.h>
25#include <linux/platform_device.h>
Timur Tabib0c813c2007-07-31 18:18:44 +020026#include <sound/core.h>
27#include <sound/soc.h>
28#include <sound/initval.h>
29#include <linux/i2c.h>
30
Mark Brown01e097d2009-01-23 15:07:45 +000031#include "cs4270.h"
32
Timur Tabib0c813c2007-07-31 18:18:44 +020033/*
34 * The codec isn't really big-endian or little-endian, since the I2S
35 * interface requires data to be sent serially with the MSbit first.
36 * However, to support BE and LE I2S devices, we specify both here. That
37 * way, ALSA will always match the bit patterns.
38 */
39#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
40 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
41 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
42 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
43 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
44 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
45
Timur Tabib0c813c2007-07-31 18:18:44 +020046/* CS4270 registers addresses */
47#define CS4270_CHIPID 0x01 /* Chip ID */
48#define CS4270_PWRCTL 0x02 /* Power Control */
49#define CS4270_MODE 0x03 /* Mode Control */
50#define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
51#define CS4270_TRANS 0x05 /* Transition Control */
52#define CS4270_MUTE 0x06 /* Mute Control */
53#define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
54#define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
55
56#define CS4270_FIRSTREG 0x01
57#define CS4270_LASTREG 0x08
58#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
59
60/* Bit masks for the CS4270 registers */
61#define CS4270_CHIPID_ID 0xF0
62#define CS4270_CHIPID_REV 0x0F
63#define CS4270_PWRCTL_FREEZE 0x80
64#define CS4270_PWRCTL_PDN_ADC 0x20
65#define CS4270_PWRCTL_PDN_DAC 0x02
66#define CS4270_PWRCTL_PDN 0x01
67#define CS4270_MODE_SPEED_MASK 0x30
68#define CS4270_MODE_1X 0x00
69#define CS4270_MODE_2X 0x10
70#define CS4270_MODE_4X 0x20
71#define CS4270_MODE_SLAVE 0x30
72#define CS4270_MODE_DIV_MASK 0x0E
73#define CS4270_MODE_DIV1 0x00
74#define CS4270_MODE_DIV15 0x02
75#define CS4270_MODE_DIV2 0x04
76#define CS4270_MODE_DIV3 0x06
77#define CS4270_MODE_DIV4 0x08
78#define CS4270_MODE_POPGUARD 0x01
79#define CS4270_FORMAT_FREEZE_A 0x80
80#define CS4270_FORMAT_FREEZE_B 0x40
81#define CS4270_FORMAT_LOOPBACK 0x20
82#define CS4270_FORMAT_DAC_MASK 0x18
83#define CS4270_FORMAT_DAC_LJ 0x00
84#define CS4270_FORMAT_DAC_I2S 0x08
85#define CS4270_FORMAT_DAC_RJ16 0x18
86#define CS4270_FORMAT_DAC_RJ24 0x10
87#define CS4270_FORMAT_ADC_MASK 0x01
88#define CS4270_FORMAT_ADC_LJ 0x00
89#define CS4270_FORMAT_ADC_I2S 0x01
90#define CS4270_TRANS_ONE_VOL 0x80
91#define CS4270_TRANS_SOFT 0x40
92#define CS4270_TRANS_ZERO 0x20
93#define CS4270_TRANS_INV_ADC_A 0x08
94#define CS4270_TRANS_INV_ADC_B 0x10
95#define CS4270_TRANS_INV_DAC_A 0x02
96#define CS4270_TRANS_INV_DAC_B 0x04
97#define CS4270_TRANS_DEEMPH 0x01
98#define CS4270_MUTE_AUTO 0x20
99#define CS4270_MUTE_ADC_A 0x08
100#define CS4270_MUTE_ADC_B 0x10
101#define CS4270_MUTE_POLARITY 0x04
102#define CS4270_MUTE_DAC_A 0x01
103#define CS4270_MUTE_DAC_B 0x02
104
Timur Tabi0db4d072009-01-23 16:31:19 -0600105/* Private data for the CS4270 */
106struct cs4270_private {
107 struct snd_soc_codec codec;
108 u8 reg_cache[CS4270_NUMREGS];
109 unsigned int mclk; /* Input frequency of the MCLK pin */
110 unsigned int mode; /* The mode (I2S or left-justified) */
Daniel Mack4eae0802009-02-25 14:37:21 +0100111 unsigned int slave_mode;
Daniel Mack1a4ba052009-04-24 16:37:45 +0200112 unsigned int manual_mute;
Timur Tabi0db4d072009-01-23 16:31:19 -0600113};
114
Timur Tabiff7bf022009-01-30 11:14:49 -0600115/**
116 * struct cs4270_mode_ratios - clock ratio tables
117 * @ratio: the ratio of MCLK to the sample rate
118 * @speed_mode: the Speed Mode bits to set in the Mode Control register for
119 * this ratio
120 * @mclk: the Ratio Select bits to set in the Mode Control register for this
121 * ratio
Timur Tabi84323952007-12-18 15:42:53 +0100122 *
123 * The data for this chart is taken from Table 5 of the CS4270 reference
124 * manual.
125 *
126 * This table is used to determine how to program the Mode Control register.
127 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
128 * rates the CS4270 currently supports.
129 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600130 * @speed_mode is the corresponding bit pattern to be written to the
Timur Tabi84323952007-12-18 15:42:53 +0100131 * MODE bits of the Mode Control Register
132 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600133 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
Timur Tabi84323952007-12-18 15:42:53 +0100134 * the Mode Control Register.
135 *
136 * In situations where a single ratio is represented by multiple speed
137 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
138 * double-speed instead of quad-speed. However, the CS4270 errata states
Timur Tabiff7bf022009-01-30 11:14:49 -0600139 * that divide-By-1.5 can cause failures, so we avoid that mode where
Timur Tabi84323952007-12-18 15:42:53 +0100140 * possible.
141 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600142 * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
143 * work if Vd is 3.3V. If this effects you, select the
Timur Tabi84323952007-12-18 15:42:53 +0100144 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
145 * never select any sample rates that require divide-by-1.5.
146 */
Timur Tabiff7bf022009-01-30 11:14:49 -0600147struct cs4270_mode_ratios {
Timur Tabi84323952007-12-18 15:42:53 +0100148 unsigned int ratio;
149 u8 speed_mode;
150 u8 mclk;
Timur Tabiff7bf022009-01-30 11:14:49 -0600151};
152
Timur Tabid9fb7fb2009-02-02 14:50:45 -0600153static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
Timur Tabi84323952007-12-18 15:42:53 +0100154 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
155#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
156 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
157#endif
158 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
159 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
160 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
161 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
162 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
163 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
164 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
165};
166
167/* The number of MCLK/LRCK ratios supported by the CS4270 */
168#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
169
Timur Tabiff7bf022009-01-30 11:14:49 -0600170/**
171 * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
172 * @codec_dai: the codec DAI
173 * @clk_id: the clock ID (ignored)
174 * @freq: the MCLK input frequency
175 * @dir: the clock direction (ignored)
Timur Tabi84323952007-12-18 15:42:53 +0100176 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600177 * This function is used to tell the codec driver what the input MCLK
178 * frequency is.
Timur Tabi84323952007-12-18 15:42:53 +0100179 *
180 * The value of MCLK is used to determine which sample rates are supported
181 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
Timur Tabiff7bf022009-01-30 11:14:49 -0600182 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
Timur Tabi84323952007-12-18 15:42:53 +0100183 *
184 * This function calculates the nine ratios and determines which ones match
185 * a standard sample rate. If there's a match, then it is added to the list
Timur Tabiff7bf022009-01-30 11:14:49 -0600186 * of supported sample rates.
Timur Tabi84323952007-12-18 15:42:53 +0100187 *
188 * This function must be called by the machine driver's 'startup' function,
189 * otherwise the list of supported sample rates will not be available in
190 * time for ALSA.
Timur Tabi84323952007-12-18 15:42:53 +0100191 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100192static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100193 int clk_id, unsigned int freq, int dir)
194{
195 struct snd_soc_codec *codec = codec_dai->codec;
196 struct cs4270_private *cs4270 = codec->private_data;
197 unsigned int rates = 0;
198 unsigned int rate_min = -1;
199 unsigned int rate_max = 0;
200 unsigned int i;
201
202 cs4270->mclk = freq;
203
204 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
205 unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
206 rates |= snd_pcm_rate_to_rate_bit(rate);
207 if (rate < rate_min)
208 rate_min = rate;
209 if (rate > rate_max)
210 rate_max = rate;
211 }
212 /* FIXME: soc should support a rate list */
213 rates &= ~SNDRV_PCM_RATE_KNOT;
214
215 if (!rates) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600216 dev_err(codec->dev, "could not find a valid sample rate\n");
Timur Tabi84323952007-12-18 15:42:53 +0100217 return -EINVAL;
218 }
219
220 codec_dai->playback.rates = rates;
221 codec_dai->playback.rate_min = rate_min;
222 codec_dai->playback.rate_max = rate_max;
223
224 codec_dai->capture.rates = rates;
225 codec_dai->capture.rate_min = rate_min;
226 codec_dai->capture.rate_max = rate_max;
227
228 return 0;
229}
230
Timur Tabiff7bf022009-01-30 11:14:49 -0600231/**
232 * cs4270_set_dai_fmt - configure the codec for the selected audio format
233 * @codec_dai: the codec DAI
234 * @format: a SND_SOC_DAIFMT_x value indicating the data format
Timur Tabi84323952007-12-18 15:42:53 +0100235 *
236 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
237 * codec accordingly.
238 *
239 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
240 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
241 * data for playback only, but ASoC currently does not support different
242 * formats for playback vs. record.
243 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100244static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100245 unsigned int format)
246{
247 struct snd_soc_codec *codec = codec_dai->codec;
248 struct cs4270_private *cs4270 = codec->private_data;
249 int ret = 0;
250
Daniel Mack4eae0802009-02-25 14:37:21 +0100251 /* set DAI format */
Timur Tabi84323952007-12-18 15:42:53 +0100252 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
253 case SND_SOC_DAIFMT_I2S:
254 case SND_SOC_DAIFMT_LEFT_J:
255 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
256 break;
257 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600258 dev_err(codec->dev, "invalid dai format\n");
Timur Tabi84323952007-12-18 15:42:53 +0100259 ret = -EINVAL;
260 }
261
Daniel Mack4eae0802009-02-25 14:37:21 +0100262 /* set master/slave audio interface */
263 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
264 case SND_SOC_DAIFMT_CBS_CFS:
265 cs4270->slave_mode = 1;
266 break;
267 case SND_SOC_DAIFMT_CBM_CFM:
268 cs4270->slave_mode = 0;
269 break;
Daniel Mack4eae0802009-02-25 14:37:21 +0100270 default:
Daniel Mackff09d492009-02-28 13:21:03 +0100271 /* all other modes are unsupported by the hardware */
Daniel Mack4eae0802009-02-25 14:37:21 +0100272 ret = -EINVAL;
273 }
274
Timur Tabi84323952007-12-18 15:42:53 +0100275 return ret;
276}
277
Timur Tabiff7bf022009-01-30 11:14:49 -0600278/**
279 * cs4270_fill_cache - pre-fill the CS4270 register cache.
280 * @codec: the codec for this CS4270
281 *
282 * This function fills in the CS4270 register cache by reading the register
283 * values from the hardware.
284 *
285 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
286 * After the initial read to pre-fill the cache, the CS4270 never updates
287 * the register values, so we won't have a cache coherency problem.
Timur Tabib0c813c2007-07-31 18:18:44 +0200288 *
289 * We use the auto-increment feature of the CS4270 to read all registers in
290 * one shot.
291 */
292static int cs4270_fill_cache(struct snd_soc_codec *codec)
293{
294 u8 *cache = codec->reg_cache;
295 struct i2c_client *i2c_client = codec->control_data;
296 s32 length;
297
298 length = i2c_smbus_read_i2c_block_data(i2c_client,
299 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
300
301 if (length != CS4270_NUMREGS) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600302 dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
Timur Tabib0c813c2007-07-31 18:18:44 +0200303 i2c_client->addr);
304 return -EIO;
305 }
306
307 return 0;
308}
309
Timur Tabiff7bf022009-01-30 11:14:49 -0600310/**
311 * cs4270_read_reg_cache - read from the CS4270 register cache.
312 * @codec: the codec for this CS4270
313 * @reg: the register to read
314 *
315 * This function returns the value for a given register. It reads only from
316 * the register cache, not the hardware itself.
Timur Tabib0c813c2007-07-31 18:18:44 +0200317 *
318 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
319 * After the initial read to pre-fill the cache, the CS4270 never updates
Timur Tabiff7bf022009-01-30 11:14:49 -0600320 * the register values, so we won't have a cache coherency problem.
Timur Tabib0c813c2007-07-31 18:18:44 +0200321 */
322static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
323 unsigned int reg)
324{
325 u8 *cache = codec->reg_cache;
326
327 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
328 return -EIO;
329
330 return cache[reg - CS4270_FIRSTREG];
331}
332
Timur Tabiff7bf022009-01-30 11:14:49 -0600333/**
334 * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
335 * @codec: the codec for this CS4270
336 * @reg: the register to write
337 * @value: the value to write to the register
Timur Tabib0c813c2007-07-31 18:18:44 +0200338 *
339 * This function writes the given value to the given CS4270 register, and
340 * also updates the register cache.
341 *
342 * Note that we don't use the hw_write function pointer of snd_soc_codec.
343 * That's because it's too clunky: the hw_write_t prototype does not match
344 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
345 */
346static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
347 unsigned int value)
348{
Timur Tabibfc4e862007-09-11 00:45:50 +0200349 u8 *cache = codec->reg_cache;
350
Timur Tabib0c813c2007-07-31 18:18:44 +0200351 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
352 return -EIO;
353
Timur Tabibfc4e862007-09-11 00:45:50 +0200354 /* Only perform an I2C operation if the new value is different */
355 if (cache[reg - CS4270_FIRSTREG] != value) {
356 struct i2c_client *client = codec->control_data;
357 if (i2c_smbus_write_byte_data(client, reg, value)) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600358 dev_err(codec->dev, "i2c write failed\n");
Timur Tabibfc4e862007-09-11 00:45:50 +0200359 return -EIO;
360 }
361
Timur Tabib0c813c2007-07-31 18:18:44 +0200362 /* We've written to the hardware, so update the cache */
Timur Tabib0c813c2007-07-31 18:18:44 +0200363 cache[reg - CS4270_FIRSTREG] = value;
Timur Tabib0c813c2007-07-31 18:18:44 +0200364 }
Timur Tabibfc4e862007-09-11 00:45:50 +0200365
366 return 0;
Timur Tabib0c813c2007-07-31 18:18:44 +0200367}
368
Timur Tabiff7bf022009-01-30 11:14:49 -0600369/**
370 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
371 * @substream: the audio stream
372 * @params: the hardware parameters to set
373 * @dai: the SOC DAI (ignored)
Timur Tabib0c813c2007-07-31 18:18:44 +0200374 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600375 * This function programs the hardware with the values provided.
376 * Specifically, the sample rate and the data format.
377 *
378 * The .ops functions are used to provide board-specific data, like input
379 * frequencies, to this driver. This function takes that information,
Timur Tabib0c813c2007-07-31 18:18:44 +0200380 * combines it with the hardware parameters provided, and programs the
381 * hardware accordingly.
382 */
383static int cs4270_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000384 struct snd_pcm_hw_params *params,
385 struct snd_soc_dai *dai)
Timur Tabib0c813c2007-07-31 18:18:44 +0200386{
387 struct snd_soc_pcm_runtime *rtd = substream->private_data;
388 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000389 struct snd_soc_codec *codec = socdev->card->codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200390 struct cs4270_private *cs4270 = codec->private_data;
Roel Kluine34ba212008-04-17 18:58:34 +0200391 int ret;
Timur Tabib0c813c2007-07-31 18:18:44 +0200392 unsigned int i;
393 unsigned int rate;
394 unsigned int ratio;
395 int reg;
396
397 /* Figure out which MCLK/LRCK ratio to use */
398
399 rate = params_rate(params); /* Sampling rate, in Hz */
400 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
401
Timur Tabi9dbd6272007-08-01 12:22:07 +0200402 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
Timur Tabi84323952007-12-18 15:42:53 +0100403 if (cs4270_mode_ratios[i].ratio == ratio)
Timur Tabib0c813c2007-07-31 18:18:44 +0200404 break;
405 }
406
Timur Tabi9dbd6272007-08-01 12:22:07 +0200407 if (i == NUM_MCLK_RATIOS) {
Timur Tabib0c813c2007-07-31 18:18:44 +0200408 /* We did not find a matching ratio */
Timur Tabia6c255e2009-02-02 15:08:29 -0600409 dev_err(codec->dev, "could not find matching ratio\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200410 return -EINVAL;
411 }
412
Timur Tabid5e9ba12009-02-03 11:09:32 -0600413 /* Set the sample rate */
Timur Tabib0c813c2007-07-31 18:18:44 +0200414
415 reg = snd_soc_read(codec, CS4270_MODE);
416 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
Daniel Mack4eae0802009-02-25 14:37:21 +0100417 reg |= cs4270_mode_ratios[i].mclk;
418
419 if (cs4270->slave_mode)
420 reg |= CS4270_MODE_SLAVE;
421 else
422 reg |= cs4270_mode_ratios[i].speed_mode;
Timur Tabib0c813c2007-07-31 18:18:44 +0200423
424 ret = snd_soc_write(codec, CS4270_MODE, reg);
425 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600426 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200427 return ret;
428 }
429
Timur Tabid5e9ba12009-02-03 11:09:32 -0600430 /* Set the DAI format */
Timur Tabib0c813c2007-07-31 18:18:44 +0200431
432 reg = snd_soc_read(codec, CS4270_FORMAT);
433 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
434
435 switch (cs4270->mode) {
436 case SND_SOC_DAIFMT_I2S:
437 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
438 break;
439 case SND_SOC_DAIFMT_LEFT_J:
440 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
441 break;
442 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600443 dev_err(codec->dev, "unknown dai format\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200444 return -EINVAL;
445 }
446
447 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
448 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600449 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200450 return ret;
451 }
452
Timur Tabib0c813c2007-07-31 18:18:44 +0200453 return ret;
454}
455
Timur Tabiff7bf022009-01-30 11:14:49 -0600456/**
Daniel Mack1a4ba052009-04-24 16:37:45 +0200457 * cs4270_dai_mute - enable/disable the CS4270 external mute
Timur Tabiff7bf022009-01-30 11:14:49 -0600458 * @dai: the SOC DAI
459 * @mute: 0 = disable mute, 1 = enable mute
Timur Tabib0c813c2007-07-31 18:18:44 +0200460 *
461 * This function toggles the mute bits in the MUTE register. The CS4270's
462 * mute capability is intended for external muting circuitry, so if the
463 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
464 * then this function will do nothing.
465 */
Daniel Mack1a4ba052009-04-24 16:37:45 +0200466static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
Timur Tabib0c813c2007-07-31 18:18:44 +0200467{
468 struct snd_soc_codec *codec = dai->codec;
Daniel Mack1a4ba052009-04-24 16:37:45 +0200469 struct cs4270_private *cs4270 = codec->private_data;
Timur Tabib0c813c2007-07-31 18:18:44 +0200470 int reg6;
471
472 reg6 = snd_soc_read(codec, CS4270_MUTE);
473
474 if (mute)
Timur Tabid5e9ba12009-02-03 11:09:32 -0600475 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
Daniel Mack1a4ba052009-04-24 16:37:45 +0200476 else {
Timur Tabid5e9ba12009-02-03 11:09:32 -0600477 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
Daniel Mack1a4ba052009-04-24 16:37:45 +0200478 reg6 |= cs4270->manual_mute;
479 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200480
481 return snd_soc_write(codec, CS4270_MUTE, reg6);
482}
Timur Tabib0c813c2007-07-31 18:18:44 +0200483
Daniel Mack1a4ba052009-04-24 16:37:45 +0200484/**
485 * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
486 * alsa control.
487 * @kcontrol: mixer control
488 * @ucontrol: control element information
489 *
490 * This function basically passes the arguments on to the generic
491 * snd_soc_put_volsw() function and saves the mute information in
492 * our private data structure. This is because we want to prevent
493 * cs4270_dai_mute() neglecting the user's decision to manually
494 * mute the codec's output.
495 *
496 * Returns 0 for success.
497 */
498static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
499 struct snd_ctl_elem_value *ucontrol)
500{
501 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
502 struct cs4270_private *cs4270 = codec->private_data;
503 int left = !ucontrol->value.integer.value[0];
504 int right = !ucontrol->value.integer.value[1];
505
506 cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
507 (right ? CS4270_MUTE_DAC_B : 0);
508
509 return snd_soc_put_volsw(kcontrol, ucontrol);
510}
511
Timur Tabib0c813c2007-07-31 18:18:44 +0200512/* A list of non-DAPM controls that the CS4270 supports */
513static const struct snd_kcontrol_new cs4270_snd_controls[] = {
514 SOC_DOUBLE_R("Master Playback Volume",
Timur Tabid5e9ba12009-02-03 11:09:32 -0600515 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
516 SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
517 SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
518 SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
519 SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
520 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
Daniel Mack1a4ba052009-04-24 16:37:45 +0200521 SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
522 SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
523 snd_soc_get_volsw, cs4270_soc_put_mute),
Timur Tabib0c813c2007-07-31 18:18:44 +0200524};
525
Timur Tabib0c813c2007-07-31 18:18:44 +0200526/*
Timur Tabiff7bf022009-01-30 11:14:49 -0600527 * cs4270_codec - global variable to store codec for the ASoC probe function
Timur Tabib0c813c2007-07-31 18:18:44 +0200528 *
529 * If struct i2c_driver had a private_data field, we wouldn't need to use
Timur Tabi04eb0932009-01-29 14:28:37 -0600530 * cs4270_codec. This is the only way to pass the codec structure from
531 * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
532 * way to synchronize these two functions. cs4270_i2c_probe() can be called
533 * multiple times before cs4270_probe() is called even once. So for now, we
534 * also only allow cs4270_i2c_probe() to be run once. That means that we do
535 * not support more than one cs4270 device in the system, at least for now.
Timur Tabib0c813c2007-07-31 18:18:44 +0200536 */
Timur Tabi04eb0932009-01-29 14:28:37 -0600537static struct snd_soc_codec *cs4270_codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200538
Eric Miao6335d052009-03-03 09:41:00 +0800539static struct snd_soc_dai_ops cs4270_dai_ops = {
540 .hw_params = cs4270_hw_params,
541 .set_sysclk = cs4270_set_dai_sysclk,
542 .set_fmt = cs4270_set_dai_fmt,
Daniel Mack1a4ba052009-04-24 16:37:45 +0200543 .digital_mute = cs4270_dai_mute,
Eric Miao6335d052009-03-03 09:41:00 +0800544};
545
Liam Girdwoode550e172008-07-07 16:07:52 +0100546struct snd_soc_dai cs4270_dai = {
Timur Tabi0db4d072009-01-23 16:31:19 -0600547 .name = "cs4270",
Timur Tabib0c813c2007-07-31 18:18:44 +0200548 .playback = {
549 .stream_name = "Playback",
550 .channels_min = 1,
551 .channels_max = 2,
552 .rates = 0,
553 .formats = CS4270_FORMATS,
554 },
555 .capture = {
556 .stream_name = "Capture",
557 .channels_min = 1,
558 .channels_max = 2,
559 .rates = 0,
560 .formats = CS4270_FORMATS,
561 },
Eric Miao6335d052009-03-03 09:41:00 +0800562 .ops = &cs4270_dai_ops,
Timur Tabib0c813c2007-07-31 18:18:44 +0200563};
564EXPORT_SYMBOL_GPL(cs4270_dai);
565
Timur Tabiff7bf022009-01-30 11:14:49 -0600566/**
567 * cs4270_probe - ASoC probe function
568 * @pdev: platform device
569 *
570 * This function is called when ASoC has all the pieces it needs to
571 * instantiate a sound driver.
Timur Tabi04eb0932009-01-29 14:28:37 -0600572 */
573static int cs4270_probe(struct platform_device *pdev)
574{
575 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
576 struct snd_soc_codec *codec = cs4270_codec;
Timur Tabi04eb0932009-01-29 14:28:37 -0600577 int ret;
578
579 /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
580 socdev->card->codec = codec;
581
582 /* Register PCMs */
583 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
584 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600585 dev_err(codec->dev, "failed to create pcms\n");
Timur Tabi04eb0932009-01-29 14:28:37 -0600586 return ret;
587 }
588
589 /* Add the non-DAPM controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100590 ret = snd_soc_add_controls(codec, cs4270_snd_controls,
591 ARRAY_SIZE(cs4270_snd_controls));
592 if (ret < 0) {
593 dev_err(codec->dev, "failed to add controls\n");
594 goto error_free_pcms;
Timur Tabi04eb0932009-01-29 14:28:37 -0600595 }
596
597 /* And finally, register the socdev */
598 ret = snd_soc_init_card(socdev);
599 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600600 dev_err(codec->dev, "failed to register card\n");
Timur Tabi04eb0932009-01-29 14:28:37 -0600601 goto error_free_pcms;
602 }
603
604 return 0;
605
606error_free_pcms:
607 snd_soc_free_pcms(socdev);
608
609 return ret;
610}
611
Timur Tabiff7bf022009-01-30 11:14:49 -0600612/**
613 * cs4270_remove - ASoC remove function
614 * @pdev: platform device
615 *
616 * This function is the counterpart to cs4270_probe().
617 */
Timur Tabi04eb0932009-01-29 14:28:37 -0600618static int cs4270_remove(struct platform_device *pdev)
619{
620 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
621
622 snd_soc_free_pcms(socdev);
623
624 return 0;
625};
626
Timur Tabiff7bf022009-01-30 11:14:49 -0600627/**
628 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
629 * @i2c_client: the I2C client object
630 * @id: the I2C device ID (ignored)
Timur Tabib0c813c2007-07-31 18:18:44 +0200631 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600632 * This function is called whenever the I2C subsystem finds a device that
633 * matches the device ID given via a prior call to i2c_add_driver().
Timur Tabib0c813c2007-07-31 18:18:44 +0200634 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600635static int cs4270_i2c_probe(struct i2c_client *i2c_client,
636 const struct i2c_device_id *id)
Timur Tabib0c813c2007-07-31 18:18:44 +0200637{
Timur Tabib0c813c2007-07-31 18:18:44 +0200638 struct snd_soc_codec *codec;
Timur Tabi0db4d072009-01-23 16:31:19 -0600639 struct cs4270_private *cs4270;
Timur Tabid5e9ba12009-02-03 11:09:32 -0600640 unsigned int reg;
Timur Tabi04eb0932009-01-29 14:28:37 -0600641 int ret;
642
643 /* For now, we only support one cs4270 device in the system. See the
644 * comment for cs4270_codec.
645 */
646 if (cs4270_codec) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600647 dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
Timur Tabi04eb0932009-01-29 14:28:37 -0600648 i2c_client->addr);
Timur Tabia6c255e2009-02-02 15:08:29 -0600649 dev_err(&i2c_client->dev, "only one per board allowed\n");
Timur Tabi04eb0932009-01-29 14:28:37 -0600650 /* Should we return something other than ENODEV here? */
651 return -ENODEV;
652 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200653
Timur Tabi0db4d072009-01-23 16:31:19 -0600654 /* Verify that we have a CS4270 */
655
656 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
657 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600658 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
Timur Tabi04eb0932009-01-29 14:28:37 -0600659 i2c_client->addr);
Timur Tabi0db4d072009-01-23 16:31:19 -0600660 return ret;
661 }
662 /* The top four bits of the chip ID should be 1100. */
663 if ((ret & 0xF0) != 0xC0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600664 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
Timur Tabi0db4d072009-01-23 16:31:19 -0600665 i2c_client->addr);
666 return -ENODEV;
667 }
668
Timur Tabia6c255e2009-02-02 15:08:29 -0600669 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
Timur Tabi0db4d072009-01-23 16:31:19 -0600670 i2c_client->addr);
Timur Tabia6c255e2009-02-02 15:08:29 -0600671 dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
Timur Tabib0c813c2007-07-31 18:18:44 +0200672
673 /* Allocate enough space for the snd_soc_codec structure
674 and our private data together. */
Timur Tabi0db4d072009-01-23 16:31:19 -0600675 cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
676 if (!cs4270) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600677 dev_err(&i2c_client->dev, "could not allocate codec\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200678 return -ENOMEM;
679 }
Timur Tabi0db4d072009-01-23 16:31:19 -0600680 codec = &cs4270->codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200681
682 mutex_init(&codec->mutex);
683 INIT_LIST_HEAD(&codec->dapm_widgets);
684 INIT_LIST_HEAD(&codec->dapm_paths);
685
Timur Tabia6c255e2009-02-02 15:08:29 -0600686 codec->dev = &i2c_client->dev;
Timur Tabib0c813c2007-07-31 18:18:44 +0200687 codec->name = "CS4270";
688 codec->owner = THIS_MODULE;
689 codec->dai = &cs4270_dai;
690 codec->num_dai = 1;
Timur Tabi0db4d072009-01-23 16:31:19 -0600691 codec->private_data = cs4270;
692 codec->control_data = i2c_client;
693 codec->read = cs4270_read_reg_cache;
694 codec->write = cs4270_i2c_write;
695 codec->reg_cache = cs4270->reg_cache;
696 codec->reg_cache_size = CS4270_NUMREGS;
Timur Tabib0c813c2007-07-31 18:18:44 +0200697
Timur Tabi0db4d072009-01-23 16:31:19 -0600698 /* The I2C interface is set up, so pre-fill our register cache */
699
700 ret = cs4270_fill_cache(codec);
701 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600702 dev_err(&i2c_client->dev, "failed to fill register cache\n");
Timur Tabi0db4d072009-01-23 16:31:19 -0600703 goto error_free_codec;
704 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200705
Timur Tabid5e9ba12009-02-03 11:09:32 -0600706 /* Disable auto-mute. This feature appears to be buggy. In some
707 * situations, auto-mute will not deactivate when it should, so we want
708 * this feature disabled by default. An application (e.g. alsactl) can
709 * re-enabled it by using the controls.
710 */
711
712 reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
713 reg &= ~CS4270_MUTE_AUTO;
714 ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
715 if (ret < 0) {
716 dev_err(&i2c_client->dev, "i2c write failed\n");
717 return ret;
718 }
719
720 /* Disable automatic volume control. The hardware enables, and it
721 * causes volume change commands to be delayed, sometimes until after
722 * playback has started. An application (e.g. alsactl) can
723 * re-enabled it by using the controls.
724 */
725
726 reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
727 reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
728 ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
729 if (ret < 0) {
730 dev_err(&i2c_client->dev, "i2c write failed\n");
731 return ret;
732 }
733
Timur Tabia6c255e2009-02-02 15:08:29 -0600734 /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
735 * structure for each CS4270 device, but the machine driver needs to
736 * have a pointer to the DAI structure, so for now it must be a global
737 * variable.
738 */
739 cs4270_dai.dev = &i2c_client->dev;
740
Timur Tabi04eb0932009-01-29 14:28:37 -0600741 /* Register the DAI. If all the other ASoC driver have already
742 * registered, then this will call our probe function, so
743 * cs4270_codec needs to be ready.
744 */
Timur Tabia6c255e2009-02-02 15:08:29 -0600745 cs4270_codec = codec;
Timur Tabi04eb0932009-01-29 14:28:37 -0600746 ret = snd_soc_register_dai(&cs4270_dai);
Timur Tabib0c813c2007-07-31 18:18:44 +0200747 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600748 dev_err(&i2c_client->dev, "failed to register DAIe\n");
Jean Delvaree3145df2008-09-30 11:40:37 +0200749 goto error_free_codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200750 }
751
Timur Tabi04eb0932009-01-29 14:28:37 -0600752 i2c_set_clientdata(i2c_client, cs4270);
Jean Delvaree3145df2008-09-30 11:40:37 +0200753
Timur Tabi0db4d072009-01-23 16:31:19 -0600754 return 0;
Jean Delvaree3145df2008-09-30 11:40:37 +0200755
Jean Delvaree3145df2008-09-30 11:40:37 +0200756error_free_codec:
Timur Tabi0db4d072009-01-23 16:31:19 -0600757 kfree(cs4270);
Timur Tabia6c255e2009-02-02 15:08:29 -0600758 cs4270_codec = NULL;
759 cs4270_dai.dev = NULL;
Jean Delvaree3145df2008-09-30 11:40:37 +0200760
Timur Tabib0c813c2007-07-31 18:18:44 +0200761 return ret;
762}
763
Timur Tabiff7bf022009-01-30 11:14:49 -0600764/**
765 * cs4270_i2c_remove - remove an I2C device
766 * @i2c_client: the I2C client object
767 *
768 * This function is the counterpart to cs4270_i2c_probe().
769 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600770static int cs4270_i2c_remove(struct i2c_client *i2c_client)
Timur Tabib0c813c2007-07-31 18:18:44 +0200771{
Timur Tabi04eb0932009-01-29 14:28:37 -0600772 struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
Timur Tabib0c813c2007-07-31 18:18:44 +0200773
Timur Tabi0db4d072009-01-23 16:31:19 -0600774 kfree(cs4270);
Timur Tabia6c255e2009-02-02 15:08:29 -0600775 cs4270_codec = NULL;
776 cs4270_dai.dev = NULL;
Timur Tabib0c813c2007-07-31 18:18:44 +0200777
Timur Tabi0db4d072009-01-23 16:31:19 -0600778 return 0;
779}
780
Timur Tabiff7bf022009-01-30 11:14:49 -0600781/*
782 * cs4270_id - I2C device IDs supported by this driver
783 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600784static struct i2c_device_id cs4270_id[] = {
785 {"cs4270", 0},
786 {}
787};
788MODULE_DEVICE_TABLE(i2c, cs4270_id);
789
Timur Tabiff7bf022009-01-30 11:14:49 -0600790/*
791 * cs4270_i2c_driver - I2C device identification
792 *
793 * This structure tells the I2C subsystem how to identify and support a
794 * given I2C device type.
795 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600796static struct i2c_driver cs4270_i2c_driver = {
797 .driver = {
798 .name = "cs4270",
799 .owner = THIS_MODULE,
800 },
801 .id_table = cs4270_id,
802 .probe = cs4270_i2c_probe,
803 .remove = cs4270_i2c_remove,
804};
805
806/*
Timur Tabib0c813c2007-07-31 18:18:44 +0200807 * ASoC codec device structure
808 *
809 * Assign this variable to the codec_dev field of the machine driver's
810 * snd_soc_device structure.
811 */
812struct snd_soc_codec_device soc_codec_device_cs4270 = {
813 .probe = cs4270_probe,
814 .remove = cs4270_remove
815};
816EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
817
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100818static int __init cs4270_init(void)
Mark Brown64089b82008-12-08 19:17:58 +0000819{
Timur Tabia6c255e2009-02-02 15:08:29 -0600820 pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
Timur Tabi0db4d072009-01-23 16:31:19 -0600821
Timur Tabi04eb0932009-01-29 14:28:37 -0600822 return i2c_add_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000823}
824module_init(cs4270_init);
825
826static void __exit cs4270_exit(void)
827{
Timur Tabi04eb0932009-01-29 14:28:37 -0600828 i2c_del_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000829}
830module_exit(cs4270_exit);
831
Timur Tabib0c813c2007-07-31 18:18:44 +0200832MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
833MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
834MODULE_LICENSE("GPL");