blob: df8fe387a66f27eebd2f0860333029994bde23d9 [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
Daniel Mack5e7c0342009-05-06 01:26:01 +020021 * - Power management is supported
Timur Tabib0c813c2007-07-31 18:18:44 +020022 */
23
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.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>
Daniel Mack5e7c0342009-05-06 01:26:01 +020030#include <linux/delay.h>
Daniel Mackffbfd332009-11-30 17:56:11 +010031#include <linux/regulator/consumer.h>
Timur Tabib0c813c2007-07-31 18:18:44 +020032
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)
Daniel Mack80ab8812009-05-05 11:25:00 +020059#define CS4270_I2C_INCR 0x80
Timur Tabib0c813c2007-07-31 18:18:44 +020060
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
Daniel Mack5e7c0342009-05-06 01:26:01 +020068#define CS4270_PWRCTL_PDN_ALL \
69 (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
Timur Tabib0c813c2007-07-31 18:18:44 +020070#define CS4270_MODE_SPEED_MASK 0x30
71#define CS4270_MODE_1X 0x00
72#define CS4270_MODE_2X 0x10
73#define CS4270_MODE_4X 0x20
74#define CS4270_MODE_SLAVE 0x30
75#define CS4270_MODE_DIV_MASK 0x0E
76#define CS4270_MODE_DIV1 0x00
77#define CS4270_MODE_DIV15 0x02
78#define CS4270_MODE_DIV2 0x04
79#define CS4270_MODE_DIV3 0x06
80#define CS4270_MODE_DIV4 0x08
81#define CS4270_MODE_POPGUARD 0x01
82#define CS4270_FORMAT_FREEZE_A 0x80
83#define CS4270_FORMAT_FREEZE_B 0x40
84#define CS4270_FORMAT_LOOPBACK 0x20
85#define CS4270_FORMAT_DAC_MASK 0x18
86#define CS4270_FORMAT_DAC_LJ 0x00
87#define CS4270_FORMAT_DAC_I2S 0x08
88#define CS4270_FORMAT_DAC_RJ16 0x18
89#define CS4270_FORMAT_DAC_RJ24 0x10
90#define CS4270_FORMAT_ADC_MASK 0x01
91#define CS4270_FORMAT_ADC_LJ 0x00
92#define CS4270_FORMAT_ADC_I2S 0x01
93#define CS4270_TRANS_ONE_VOL 0x80
94#define CS4270_TRANS_SOFT 0x40
95#define CS4270_TRANS_ZERO 0x20
96#define CS4270_TRANS_INV_ADC_A 0x08
97#define CS4270_TRANS_INV_ADC_B 0x10
98#define CS4270_TRANS_INV_DAC_A 0x02
99#define CS4270_TRANS_INV_DAC_B 0x04
100#define CS4270_TRANS_DEEMPH 0x01
101#define CS4270_MUTE_AUTO 0x20
102#define CS4270_MUTE_ADC_A 0x08
103#define CS4270_MUTE_ADC_B 0x10
104#define CS4270_MUTE_POLARITY 0x04
105#define CS4270_MUTE_DAC_A 0x01
106#define CS4270_MUTE_DAC_B 0x02
107
Timur Tabi11b8fca2011-01-10 13:28:32 -0600108/* Power-on default values for the registers
109 *
110 * This array contains the power-on default values of the registers, with the
111 * exception of the "CHIPID" register (01h). The lower four bits of that
112 * register contain the hardware revision, so it is treated as volatile.
113 *
114 * Also note that on the CS4270, the first readable register is 1, but ASoC
115 * assumes the first register is 0. Therfore, the array must have an entry for
116 * register 0, but we use cs4270_reg_is_readable() to tell ASoC that it can't
117 * be read.
118 */
119static const u8 cs4270_default_reg_cache[CS4270_LASTREG + 1] = {
120 0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x20, 0x00, 0x00
121};
122
Daniel Mackffbfd332009-11-30 17:56:11 +0100123static const char *supply_names[] = {
124 "va", "vd", "vlc"
125};
126
Timur Tabi0db4d072009-01-23 16:31:19 -0600127/* Private data for the CS4270 */
128struct cs4270_private {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000129 enum snd_soc_control_type control_type;
Timur Tabi0db4d072009-01-23 16:31:19 -0600130 unsigned int mclk; /* Input frequency of the MCLK pin */
131 unsigned int mode; /* The mode (I2S or left-justified) */
Daniel Mack4eae0802009-02-25 14:37:21 +0100132 unsigned int slave_mode;
Daniel Mack1a4ba052009-04-24 16:37:45 +0200133 unsigned int manual_mute;
Daniel Mackffbfd332009-11-30 17:56:11 +0100134
135 /* power domain regulators */
136 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
Timur Tabi0db4d072009-01-23 16:31:19 -0600137};
138
Timur Tabiff7bf022009-01-30 11:14:49 -0600139/**
140 * struct cs4270_mode_ratios - clock ratio tables
141 * @ratio: the ratio of MCLK to the sample rate
142 * @speed_mode: the Speed Mode bits to set in the Mode Control register for
143 * this ratio
144 * @mclk: the Ratio Select bits to set in the Mode Control register for this
145 * ratio
Timur Tabi84323952007-12-18 15:42:53 +0100146 *
147 * The data for this chart is taken from Table 5 of the CS4270 reference
148 * manual.
149 *
150 * This table is used to determine how to program the Mode Control register.
151 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
152 * rates the CS4270 currently supports.
153 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600154 * @speed_mode is the corresponding bit pattern to be written to the
Timur Tabi84323952007-12-18 15:42:53 +0100155 * MODE bits of the Mode Control Register
156 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600157 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
Timur Tabi84323952007-12-18 15:42:53 +0100158 * the Mode Control Register.
159 *
160 * In situations where a single ratio is represented by multiple speed
161 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
162 * double-speed instead of quad-speed. However, the CS4270 errata states
Timur Tabiff7bf022009-01-30 11:14:49 -0600163 * that divide-By-1.5 can cause failures, so we avoid that mode where
Timur Tabi84323952007-12-18 15:42:53 +0100164 * possible.
165 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600166 * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
167 * work if Vd is 3.3V. If this effects you, select the
Timur Tabi84323952007-12-18 15:42:53 +0100168 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
169 * never select any sample rates that require divide-by-1.5.
170 */
Timur Tabiff7bf022009-01-30 11:14:49 -0600171struct cs4270_mode_ratios {
Timur Tabi84323952007-12-18 15:42:53 +0100172 unsigned int ratio;
173 u8 speed_mode;
174 u8 mclk;
Timur Tabiff7bf022009-01-30 11:14:49 -0600175};
176
Timur Tabid9fb7fb2009-02-02 14:50:45 -0600177static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
Timur Tabi84323952007-12-18 15:42:53 +0100178 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
179#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
180 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
181#endif
182 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
183 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
184 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
185 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
186 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
187 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
188 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
189};
190
191/* The number of MCLK/LRCK ratios supported by the CS4270 */
192#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
193
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000194static int cs4270_reg_is_readable(struct snd_soc_codec *codec, unsigned int reg)
Timur Tabi11b8fca2011-01-10 13:28:32 -0600195{
196 return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
197}
198
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000199static int cs4270_reg_is_volatile(struct snd_soc_codec *codec, unsigned int reg)
Timur Tabi11b8fca2011-01-10 13:28:32 -0600200{
201 /* Unreadable registers are considered volatile */
202 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
203 return 1;
204
205 return reg == CS4270_CHIPID;
206}
207
Timur Tabiff7bf022009-01-30 11:14:49 -0600208/**
209 * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
210 * @codec_dai: the codec DAI
211 * @clk_id: the clock ID (ignored)
212 * @freq: the MCLK input frequency
213 * @dir: the clock direction (ignored)
Timur Tabi84323952007-12-18 15:42:53 +0100214 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600215 * This function is used to tell the codec driver what the input MCLK
216 * frequency is.
Timur Tabi84323952007-12-18 15:42:53 +0100217 *
218 * The value of MCLK is used to determine which sample rates are supported
219 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
Timur Tabiff7bf022009-01-30 11:14:49 -0600220 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
Timur Tabi84323952007-12-18 15:42:53 +0100221 *
222 * This function calculates the nine ratios and determines which ones match
223 * a standard sample rate. If there's a match, then it is added to the list
Timur Tabiff7bf022009-01-30 11:14:49 -0600224 * of supported sample rates.
Timur Tabi84323952007-12-18 15:42:53 +0100225 *
226 * This function must be called by the machine driver's 'startup' function,
227 * otherwise the list of supported sample rates will not be available in
228 * time for ALSA.
Daniel Mack6aababd2010-01-15 17:36:48 +0100229 *
230 * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
231 * theoretically possible sample rates to be enabled. Call it again with a
232 * proper value set one the external clock is set (most probably you would do
233 * that from a machine's driver 'hw_param' hook.
Timur Tabi84323952007-12-18 15:42:53 +0100234 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100235static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100236 int clk_id, unsigned int freq, int dir)
237{
238 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900239 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabi84323952007-12-18 15:42:53 +0100240
241 cs4270->mclk = freq;
Timur Tabi84323952007-12-18 15:42:53 +0100242 return 0;
243}
244
Timur Tabiff7bf022009-01-30 11:14:49 -0600245/**
246 * cs4270_set_dai_fmt - configure the codec for the selected audio format
247 * @codec_dai: the codec DAI
248 * @format: a SND_SOC_DAIFMT_x value indicating the data format
Timur Tabi84323952007-12-18 15:42:53 +0100249 *
250 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
251 * codec accordingly.
252 *
253 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
254 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
255 * data for playback only, but ASoC currently does not support different
256 * formats for playback vs. record.
257 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100258static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100259 unsigned int format)
260{
261 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900262 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabi84323952007-12-18 15:42:53 +0100263
Daniel Mack4eae0802009-02-25 14:37:21 +0100264 /* set DAI format */
Timur Tabi84323952007-12-18 15:42:53 +0100265 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
266 case SND_SOC_DAIFMT_I2S:
267 case SND_SOC_DAIFMT_LEFT_J:
268 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
269 break;
270 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600271 dev_err(codec->dev, "invalid dai format\n");
Axel Linac601552011-10-06 07:29:56 +0800272 return -EINVAL;
Timur Tabi84323952007-12-18 15:42:53 +0100273 }
274
Daniel Mack4eae0802009-02-25 14:37:21 +0100275 /* set master/slave audio interface */
276 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
277 case SND_SOC_DAIFMT_CBS_CFS:
278 cs4270->slave_mode = 1;
279 break;
280 case SND_SOC_DAIFMT_CBM_CFM:
281 cs4270->slave_mode = 0;
282 break;
Daniel Mack4eae0802009-02-25 14:37:21 +0100283 default:
Daniel Mackff09d492009-02-28 13:21:03 +0100284 /* all other modes are unsupported by the hardware */
Axel Linac601552011-10-06 07:29:56 +0800285 dev_err(codec->dev, "Unknown master/slave configuration\n");
286 return -EINVAL;
Daniel Mack4eae0802009-02-25 14:37:21 +0100287 }
288
Axel Linac601552011-10-06 07:29:56 +0800289 return 0;
Timur Tabi84323952007-12-18 15:42:53 +0100290}
291
Timur Tabiff7bf022009-01-30 11:14:49 -0600292/**
Timur Tabiff7bf022009-01-30 11:14:49 -0600293 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
294 * @substream: the audio stream
295 * @params: the hardware parameters to set
296 * @dai: the SOC DAI (ignored)
Timur Tabib0c813c2007-07-31 18:18:44 +0200297 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600298 * This function programs the hardware with the values provided.
299 * Specifically, the sample rate and the data format.
300 *
301 * The .ops functions are used to provide board-specific data, like input
302 * frequencies, to this driver. This function takes that information,
Timur Tabib0c813c2007-07-31 18:18:44 +0200303 * combines it with the hardware parameters provided, and programs the
304 * hardware accordingly.
305 */
306static int cs4270_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000307 struct snd_pcm_hw_params *params,
308 struct snd_soc_dai *dai)
Timur Tabib0c813c2007-07-31 18:18:44 +0200309{
310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000311 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900312 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Roel Kluine34ba212008-04-17 18:58:34 +0200313 int ret;
Timur Tabib0c813c2007-07-31 18:18:44 +0200314 unsigned int i;
315 unsigned int rate;
316 unsigned int ratio;
317 int reg;
318
319 /* Figure out which MCLK/LRCK ratio to use */
320
321 rate = params_rate(params); /* Sampling rate, in Hz */
322 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
323
Timur Tabi9dbd6272007-08-01 12:22:07 +0200324 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
Timur Tabi84323952007-12-18 15:42:53 +0100325 if (cs4270_mode_ratios[i].ratio == ratio)
Timur Tabib0c813c2007-07-31 18:18:44 +0200326 break;
327 }
328
Timur Tabi9dbd6272007-08-01 12:22:07 +0200329 if (i == NUM_MCLK_RATIOS) {
Timur Tabib0c813c2007-07-31 18:18:44 +0200330 /* We did not find a matching ratio */
Timur Tabia6c255e2009-02-02 15:08:29 -0600331 dev_err(codec->dev, "could not find matching ratio\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200332 return -EINVAL;
333 }
334
Timur Tabid5e9ba12009-02-03 11:09:32 -0600335 /* Set the sample rate */
Timur Tabib0c813c2007-07-31 18:18:44 +0200336
337 reg = snd_soc_read(codec, CS4270_MODE);
338 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
Daniel Mack4eae0802009-02-25 14:37:21 +0100339 reg |= cs4270_mode_ratios[i].mclk;
340
341 if (cs4270->slave_mode)
342 reg |= CS4270_MODE_SLAVE;
343 else
344 reg |= cs4270_mode_ratios[i].speed_mode;
Timur Tabib0c813c2007-07-31 18:18:44 +0200345
346 ret = snd_soc_write(codec, CS4270_MODE, reg);
347 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600348 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200349 return ret;
350 }
351
Timur Tabid5e9ba12009-02-03 11:09:32 -0600352 /* Set the DAI format */
Timur Tabib0c813c2007-07-31 18:18:44 +0200353
354 reg = snd_soc_read(codec, CS4270_FORMAT);
355 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
356
357 switch (cs4270->mode) {
358 case SND_SOC_DAIFMT_I2S:
359 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
360 break;
361 case SND_SOC_DAIFMT_LEFT_J:
362 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
363 break;
364 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600365 dev_err(codec->dev, "unknown dai format\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200366 return -EINVAL;
367 }
368
369 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
370 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600371 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200372 return ret;
373 }
374
Timur Tabib0c813c2007-07-31 18:18:44 +0200375 return ret;
376}
377
Timur Tabiff7bf022009-01-30 11:14:49 -0600378/**
Daniel Mack1a4ba052009-04-24 16:37:45 +0200379 * cs4270_dai_mute - enable/disable the CS4270 external mute
Timur Tabiff7bf022009-01-30 11:14:49 -0600380 * @dai: the SOC DAI
381 * @mute: 0 = disable mute, 1 = enable mute
Timur Tabib0c813c2007-07-31 18:18:44 +0200382 *
383 * This function toggles the mute bits in the MUTE register. The CS4270's
384 * mute capability is intended for external muting circuitry, so if the
385 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
386 * then this function will do nothing.
387 */
Daniel Mack1a4ba052009-04-24 16:37:45 +0200388static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
Timur Tabib0c813c2007-07-31 18:18:44 +0200389{
390 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900391 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabib0c813c2007-07-31 18:18:44 +0200392 int reg6;
393
394 reg6 = snd_soc_read(codec, CS4270_MUTE);
395
396 if (mute)
Timur Tabid5e9ba12009-02-03 11:09:32 -0600397 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
Daniel Mack1a4ba052009-04-24 16:37:45 +0200398 else {
Timur Tabid5e9ba12009-02-03 11:09:32 -0600399 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
Daniel Mack1a4ba052009-04-24 16:37:45 +0200400 reg6 |= cs4270->manual_mute;
401 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200402
403 return snd_soc_write(codec, CS4270_MUTE, reg6);
404}
Timur Tabib0c813c2007-07-31 18:18:44 +0200405
Daniel Mack1a4ba052009-04-24 16:37:45 +0200406/**
407 * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
408 * alsa control.
409 * @kcontrol: mixer control
410 * @ucontrol: control element information
411 *
412 * This function basically passes the arguments on to the generic
413 * snd_soc_put_volsw() function and saves the mute information in
414 * our private data structure. This is because we want to prevent
415 * cs4270_dai_mute() neglecting the user's decision to manually
416 * mute the codec's output.
417 *
418 * Returns 0 for success.
419 */
420static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
421 struct snd_ctl_elem_value *ucontrol)
422{
423 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900424 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Daniel Mack1a4ba052009-04-24 16:37:45 +0200425 int left = !ucontrol->value.integer.value[0];
426 int right = !ucontrol->value.integer.value[1];
427
428 cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
429 (right ? CS4270_MUTE_DAC_B : 0);
430
431 return snd_soc_put_volsw(kcontrol, ucontrol);
432}
433
Timur Tabib0c813c2007-07-31 18:18:44 +0200434/* A list of non-DAPM controls that the CS4270 supports */
435static const struct snd_kcontrol_new cs4270_snd_controls[] = {
436 SOC_DOUBLE_R("Master Playback Volume",
Timur Tabid5e9ba12009-02-03 11:09:32 -0600437 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
438 SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
439 SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
440 SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
Daniel Mack7e1aa1d2009-10-29 02:24:32 +0100441 SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
Timur Tabid5e9ba12009-02-03 11:09:32 -0600442 SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
443 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
Daniel Mack1a4ba052009-04-24 16:37:45 +0200444 SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
445 SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
446 snd_soc_get_volsw, cs4270_soc_put_mute),
Timur Tabib0c813c2007-07-31 18:18:44 +0200447};
448
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100449static const struct snd_soc_dai_ops cs4270_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800450 .hw_params = cs4270_hw_params,
451 .set_sysclk = cs4270_set_dai_sysclk,
452 .set_fmt = cs4270_set_dai_fmt,
Daniel Mack1a4ba052009-04-24 16:37:45 +0200453 .digital_mute = cs4270_dai_mute,
Eric Miao6335d052009-03-03 09:41:00 +0800454};
455
Mark Brown5c758482010-10-06 16:18:17 -0700456static struct snd_soc_dai_driver cs4270_dai = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000457 .name = "cs4270-hifi",
Timur Tabib0c813c2007-07-31 18:18:44 +0200458 .playback = {
459 .stream_name = "Playback",
460 .channels_min = 1,
461 .channels_max = 2,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000462 .rates = SNDRV_PCM_RATE_CONTINUOUS,
463 .rate_min = 4000,
464 .rate_max = 216000,
Timur Tabib0c813c2007-07-31 18:18:44 +0200465 .formats = CS4270_FORMATS,
466 },
467 .capture = {
468 .stream_name = "Capture",
469 .channels_min = 1,
470 .channels_max = 2,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000471 .rates = SNDRV_PCM_RATE_CONTINUOUS,
472 .rate_min = 4000,
473 .rate_max = 216000,
Timur Tabib0c813c2007-07-31 18:18:44 +0200474 .formats = CS4270_FORMATS,
475 },
Eric Miao6335d052009-03-03 09:41:00 +0800476 .ops = &cs4270_dai_ops,
Timur Tabib0c813c2007-07-31 18:18:44 +0200477};
Timur Tabib0c813c2007-07-31 18:18:44 +0200478
Timur Tabiff7bf022009-01-30 11:14:49 -0600479/**
480 * cs4270_probe - ASoC probe function
481 * @pdev: platform device
482 *
483 * This function is called when ASoC has all the pieces it needs to
484 * instantiate a sound driver.
Timur Tabi04eb0932009-01-29 14:28:37 -0600485 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486static int cs4270_probe(struct snd_soc_codec *codec)
Timur Tabi04eb0932009-01-29 14:28:37 -0600487{
Mark Brownb2c812e2010-04-14 15:35:19 +0900488 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabi11b8fca2011-01-10 13:28:32 -0600489 int i, ret;
Timur Tabi04eb0932009-01-29 14:28:37 -0600490
Timur Tabi11b8fca2011-01-10 13:28:32 -0600491 /* Tell ASoC what kind of I/O to use to read the registers. ASoC will
492 * then do the I2C transactions itself.
493 */
494 ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs4270->control_type);
Timur Tabi0db4d072009-01-23 16:31:19 -0600495 if (ret < 0) {
Timur Tabi11b8fca2011-01-10 13:28:32 -0600496 dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497 return ret;
Timur Tabi0db4d072009-01-23 16:31:19 -0600498 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200499
Timur Tabid5e9ba12009-02-03 11:09:32 -0600500 /* Disable auto-mute. This feature appears to be buggy. In some
501 * situations, auto-mute will not deactivate when it should, so we want
502 * this feature disabled by default. An application (e.g. alsactl) can
503 * re-enabled it by using the controls.
504 */
Timur Tabi11b8fca2011-01-10 13:28:32 -0600505 ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
Timur Tabid5e9ba12009-02-03 11:09:32 -0600506 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000507 dev_err(codec->dev, "i2c write failed\n");
Timur Tabid5e9ba12009-02-03 11:09:32 -0600508 return ret;
509 }
510
511 /* Disable automatic volume control. The hardware enables, and it
512 * causes volume change commands to be delayed, sometimes until after
513 * playback has started. An application (e.g. alsactl) can
514 * re-enabled it by using the controls.
515 */
Timur Tabi11b8fca2011-01-10 13:28:32 -0600516 ret = snd_soc_update_bits(codec, CS4270_TRANS,
517 CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
Timur Tabid5e9ba12009-02-03 11:09:32 -0600518 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 dev_err(codec->dev, "i2c write failed\n");
Timur Tabid5e9ba12009-02-03 11:09:32 -0600520 return ret;
521 }
522
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 /* Add the non-DAPM controls */
Liam Girdwood022658b2012-02-03 17:43:09 +0000524 ret = snd_soc_add_codec_controls(codec, cs4270_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525 ARRAY_SIZE(cs4270_snd_controls));
Timur Tabib0c813c2007-07-31 18:18:44 +0200526 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 dev_err(codec->dev, "failed to add controls\n");
528 return ret;
Timur Tabib0c813c2007-07-31 18:18:44 +0200529 }
530
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 /* get the power supply regulators */
532 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
533 cs4270->supplies[i].supply = supply_names[i];
534
535 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
536 cs4270->supplies);
537 if (ret < 0)
538 return ret;
539
540 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
541 cs4270->supplies);
542 if (ret < 0)
543 goto error_free_regulators;
Jean Delvaree3145df2008-09-30 11:40:37 +0200544
Timur Tabi0db4d072009-01-23 16:31:19 -0600545 return 0;
Jean Delvaree3145df2008-09-30 11:40:37 +0200546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547error_free_regulators:
548 regulator_bulk_free(ARRAY_SIZE(cs4270->supplies),
549 cs4270->supplies);
Jean Delvaree3145df2008-09-30 11:40:37 +0200550
Timur Tabib0c813c2007-07-31 18:18:44 +0200551 return ret;
552}
553
Timur Tabiff7bf022009-01-30 11:14:49 -0600554/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 * cs4270_remove - ASoC remove function
556 * @pdev: platform device
Timur Tabiff7bf022009-01-30 11:14:49 -0600557 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 * This function is the counterpart to cs4270_probe().
Timur Tabiff7bf022009-01-30 11:14:49 -0600559 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560static int cs4270_remove(struct snd_soc_codec *codec)
Timur Tabib0c813c2007-07-31 18:18:44 +0200561{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabib0c813c2007-07-31 18:18:44 +0200563
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
565 regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
Timur Tabib0c813c2007-07-31 18:18:44 +0200566
Timur Tabi0db4d072009-01-23 16:31:19 -0600567 return 0;
Timur Tabi0db4d072009-01-23 16:31:19 -0600568};
Timur Tabi0db4d072009-01-23 16:31:19 -0600569
Daniel Mack5e7c0342009-05-06 01:26:01 +0200570#ifdef CONFIG_PM
571
572/* This suspend/resume implementation can handle both - a simple standby
573 * where the codec remains powered, and a full suspend, where the voltage
574 * domain the codec is connected to is teared down and/or any other hardware
575 * reset condition is asserted.
576 *
577 * The codec's own power saving features are enabled in the suspend callback,
578 * and all registers are written back to the hardware when resuming.
579 */
580
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100581static int cs4270_soc_suspend(struct snd_soc_codec *codec)
Daniel Mack15b5bda2009-08-05 20:50:43 +0200582{
Mark Brownb2c812e2010-04-14 15:35:19 +0900583 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Daniel Mackffbfd332009-11-30 17:56:11 +0100584 int reg, ret;
Daniel Mack15b5bda2009-08-05 20:50:43 +0200585
Daniel Mackffbfd332009-11-30 17:56:11 +0100586 reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
587 if (reg < 0)
588 return reg;
589
590 ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
591 if (ret < 0)
592 return ret;
593
594 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
595 cs4270->supplies);
596
597 return 0;
Daniel Mack15b5bda2009-08-05 20:50:43 +0200598}
599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600static int cs4270_soc_resume(struct snd_soc_codec *codec)
Daniel Mack15b5bda2009-08-05 20:50:43 +0200601{
Mark Brownb2c812e2010-04-14 15:35:19 +0900602 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Mark Brownab92d092012-03-19 16:15:43 +0000603 int reg, ret;
Daniel Mack5e7c0342009-05-06 01:26:01 +0200604
Mark Brownab92d092012-03-19 16:15:43 +0000605 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
606 cs4270->supplies);
607 if (ret != 0)
608 return ret;
Daniel Mackffbfd332009-11-30 17:56:11 +0100609
Daniel Mack5e7c0342009-05-06 01:26:01 +0200610 /* In case the device was put to hard reset during sleep, we need to
611 * wait 500ns here before any I2C communication. */
612 ndelay(500);
613
614 /* first restore the entire register cache ... */
Daniel Mackd66b8532011-11-22 17:17:23 +0100615 snd_soc_cache_sync(codec);
Daniel Mack5e7c0342009-05-06 01:26:01 +0200616
617 /* ... then disable the power-down bits */
618 reg = snd_soc_read(codec, CS4270_PWRCTL);
619 reg &= ~CS4270_PWRCTL_PDN_ALL;
620
621 return snd_soc_write(codec, CS4270_PWRCTL, reg);
622}
623#else
Daniel Mack15b5bda2009-08-05 20:50:43 +0200624#define cs4270_soc_suspend NULL
625#define cs4270_soc_resume NULL
Daniel Mack5e7c0342009-05-06 01:26:01 +0200626#endif /* CONFIG_PM */
627
Timur Tabiff7bf022009-01-30 11:14:49 -0600628/*
Daniel Mackb6f7d7c2011-05-25 09:53:12 +0200629 * ASoC codec driver structure
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 */
Timur Tabi11b8fca2011-01-10 13:28:32 -0600631static const struct snd_soc_codec_driver soc_codec_device_cs4270 = {
632 .probe = cs4270_probe,
633 .remove = cs4270_remove,
634 .suspend = cs4270_soc_suspend,
635 .resume = cs4270_soc_resume,
636 .volatile_register = cs4270_reg_is_volatile,
637 .readable_register = cs4270_reg_is_readable,
638 .reg_cache_size = CS4270_LASTREG + 1,
639 .reg_word_size = sizeof(u8),
640 .reg_cache_default = cs4270_default_reg_cache,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641};
642
643/**
644 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
645 * @i2c_client: the I2C client object
646 * @id: the I2C device ID (ignored)
647 *
648 * This function is called whenever the I2C subsystem finds a device that
649 * matches the device ID given via a prior call to i2c_add_driver().
650 */
651static int cs4270_i2c_probe(struct i2c_client *i2c_client,
652 const struct i2c_device_id *id)
653{
654 struct cs4270_private *cs4270;
655 int ret;
656
657 /* Verify that we have a CS4270 */
658
659 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
660 if (ret < 0) {
661 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
662 i2c_client->addr);
663 return ret;
664 }
665 /* The top four bits of the chip ID should be 1100. */
666 if ((ret & 0xF0) != 0xC0) {
667 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
668 i2c_client->addr);
669 return -ENODEV;
670 }
671
672 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
673 i2c_client->addr);
674 dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
675
Axel Lin7fd8a672011-12-29 11:58:22 +0800676 cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
677 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 if (!cs4270) {
679 dev_err(&i2c_client->dev, "could not allocate codec\n");
680 return -ENOMEM;
681 }
682
683 i2c_set_clientdata(i2c_client, cs4270);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684 cs4270->control_type = SND_SOC_I2C;
685
686 ret = snd_soc_register_codec(&i2c_client->dev,
687 &soc_codec_device_cs4270, &cs4270_dai, 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 return ret;
689}
690
691/**
692 * cs4270_i2c_remove - remove an I2C device
693 * @i2c_client: the I2C client object
694 *
695 * This function is the counterpart to cs4270_i2c_probe().
696 */
697static int cs4270_i2c_remove(struct i2c_client *i2c_client)
698{
699 snd_soc_unregister_codec(&i2c_client->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 return 0;
701}
702
703/*
704 * cs4270_id - I2C device IDs supported by this driver
705 */
Axel Lin79a54ea2011-03-04 15:22:03 +0800706static const struct i2c_device_id cs4270_id[] = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 {"cs4270", 0},
708 {}
709};
710MODULE_DEVICE_TABLE(i2c, cs4270_id);
711
712/*
Timur Tabiff7bf022009-01-30 11:14:49 -0600713 * cs4270_i2c_driver - I2C device identification
714 *
715 * This structure tells the I2C subsystem how to identify and support a
716 * given I2C device type.
717 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600718static struct i2c_driver cs4270_i2c_driver = {
719 .driver = {
Shawn Guo64902b22012-02-24 22:09:38 +0800720 .name = "cs4270",
Timur Tabi0db4d072009-01-23 16:31:19 -0600721 .owner = THIS_MODULE,
722 },
723 .id_table = cs4270_id,
724 .probe = cs4270_i2c_probe,
725 .remove = cs4270_i2c_remove,
726};
727
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100728static int __init cs4270_init(void)
Mark Brown64089b82008-12-08 19:17:58 +0000729{
Timur Tabi04eb0932009-01-29 14:28:37 -0600730 return i2c_add_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000731}
732module_init(cs4270_init);
733
734static void __exit cs4270_exit(void)
735{
Timur Tabi04eb0932009-01-29 14:28:37 -0600736 i2c_del_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000737}
738module_exit(cs4270_exit);
739
Timur Tabib0c813c2007-07-31 18:18:44 +0200740MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
741MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
742MODULE_LICENSE("GPL");