blob: fd11bb646d40447c07cb7b9f7d3f79ccbf956aad [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>
Daniel Mack85d07e42012-07-25 15:28:34 +020032#include <linux/of_device.h>
Daniel Mack02286192012-07-25 15:28:35 +020033#include <linux/of_gpio.h>
Timur Tabib0c813c2007-07-31 18:18:44 +020034
Timur Tabib0c813c2007-07-31 18:18:44 +020035/*
36 * The codec isn't really big-endian or little-endian, since the I2S
37 * interface requires data to be sent serially with the MSbit first.
38 * However, to support BE and LE I2S devices, we specify both here. That
39 * way, ALSA will always match the bit patterns.
40 */
41#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
42 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
43 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
44 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
45 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
46 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
47
Timur Tabib0c813c2007-07-31 18:18:44 +020048/* CS4270 registers addresses */
49#define CS4270_CHIPID 0x01 /* Chip ID */
50#define CS4270_PWRCTL 0x02 /* Power Control */
51#define CS4270_MODE 0x03 /* Mode Control */
52#define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
53#define CS4270_TRANS 0x05 /* Transition Control */
54#define CS4270_MUTE 0x06 /* Mute Control */
55#define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
56#define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
57
58#define CS4270_FIRSTREG 0x01
59#define CS4270_LASTREG 0x08
60#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
Daniel Mack80ab8812009-05-05 11:25:00 +020061#define CS4270_I2C_INCR 0x80
Timur Tabib0c813c2007-07-31 18:18:44 +020062
63/* Bit masks for the CS4270 registers */
64#define CS4270_CHIPID_ID 0xF0
65#define CS4270_CHIPID_REV 0x0F
66#define CS4270_PWRCTL_FREEZE 0x80
67#define CS4270_PWRCTL_PDN_ADC 0x20
68#define CS4270_PWRCTL_PDN_DAC 0x02
69#define CS4270_PWRCTL_PDN 0x01
Daniel Mack5e7c0342009-05-06 01:26:01 +020070#define CS4270_PWRCTL_PDN_ALL \
71 (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
Timur Tabib0c813c2007-07-31 18:18:44 +020072#define CS4270_MODE_SPEED_MASK 0x30
73#define CS4270_MODE_1X 0x00
74#define CS4270_MODE_2X 0x10
75#define CS4270_MODE_4X 0x20
76#define CS4270_MODE_SLAVE 0x30
77#define CS4270_MODE_DIV_MASK 0x0E
78#define CS4270_MODE_DIV1 0x00
79#define CS4270_MODE_DIV15 0x02
80#define CS4270_MODE_DIV2 0x04
81#define CS4270_MODE_DIV3 0x06
82#define CS4270_MODE_DIV4 0x08
83#define CS4270_MODE_POPGUARD 0x01
84#define CS4270_FORMAT_FREEZE_A 0x80
85#define CS4270_FORMAT_FREEZE_B 0x40
86#define CS4270_FORMAT_LOOPBACK 0x20
87#define CS4270_FORMAT_DAC_MASK 0x18
88#define CS4270_FORMAT_DAC_LJ 0x00
89#define CS4270_FORMAT_DAC_I2S 0x08
90#define CS4270_FORMAT_DAC_RJ16 0x18
91#define CS4270_FORMAT_DAC_RJ24 0x10
92#define CS4270_FORMAT_ADC_MASK 0x01
93#define CS4270_FORMAT_ADC_LJ 0x00
94#define CS4270_FORMAT_ADC_I2S 0x01
95#define CS4270_TRANS_ONE_VOL 0x80
96#define CS4270_TRANS_SOFT 0x40
97#define CS4270_TRANS_ZERO 0x20
98#define CS4270_TRANS_INV_ADC_A 0x08
99#define CS4270_TRANS_INV_ADC_B 0x10
100#define CS4270_TRANS_INV_DAC_A 0x02
101#define CS4270_TRANS_INV_DAC_B 0x04
102#define CS4270_TRANS_DEEMPH 0x01
103#define CS4270_MUTE_AUTO 0x20
104#define CS4270_MUTE_ADC_A 0x08
105#define CS4270_MUTE_ADC_B 0x10
106#define CS4270_MUTE_POLARITY 0x04
107#define CS4270_MUTE_DAC_A 0x01
108#define CS4270_MUTE_DAC_B 0x02
109
Timur Tabi11b8fca2011-01-10 13:28:32 -0600110/* Power-on default values for the registers
111 *
112 * This array contains the power-on default values of the registers, with the
113 * exception of the "CHIPID" register (01h). The lower four bits of that
114 * register contain the hardware revision, so it is treated as volatile.
115 *
116 * Also note that on the CS4270, the first readable register is 1, but ASoC
117 * assumes the first register is 0. Therfore, the array must have an entry for
118 * register 0, but we use cs4270_reg_is_readable() to tell ASoC that it can't
119 * be read.
120 */
121static const u8 cs4270_default_reg_cache[CS4270_LASTREG + 1] = {
122 0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x20, 0x00, 0x00
123};
124
Daniel Mackffbfd332009-11-30 17:56:11 +0100125static const char *supply_names[] = {
126 "va", "vd", "vlc"
127};
128
Timur Tabi0db4d072009-01-23 16:31:19 -0600129/* Private data for the CS4270 */
130struct cs4270_private {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000131 enum snd_soc_control_type control_type;
Timur Tabi0db4d072009-01-23 16:31:19 -0600132 unsigned int mclk; /* Input frequency of the MCLK pin */
133 unsigned int mode; /* The mode (I2S or left-justified) */
Daniel Mack4eae0802009-02-25 14:37:21 +0100134 unsigned int slave_mode;
Daniel Mack1a4ba052009-04-24 16:37:45 +0200135 unsigned int manual_mute;
Daniel Mackffbfd332009-11-30 17:56:11 +0100136
137 /* power domain regulators */
138 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
Timur Tabi0db4d072009-01-23 16:31:19 -0600139};
140
Timur Tabiff7bf022009-01-30 11:14:49 -0600141/**
142 * struct cs4270_mode_ratios - clock ratio tables
143 * @ratio: the ratio of MCLK to the sample rate
144 * @speed_mode: the Speed Mode bits to set in the Mode Control register for
145 * this ratio
146 * @mclk: the Ratio Select bits to set in the Mode Control register for this
147 * ratio
Timur Tabi84323952007-12-18 15:42:53 +0100148 *
149 * The data for this chart is taken from Table 5 of the CS4270 reference
150 * manual.
151 *
152 * This table is used to determine how to program the Mode Control register.
153 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
154 * rates the CS4270 currently supports.
155 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600156 * @speed_mode is the corresponding bit pattern to be written to the
Timur Tabi84323952007-12-18 15:42:53 +0100157 * MODE bits of the Mode Control Register
158 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600159 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
Timur Tabi84323952007-12-18 15:42:53 +0100160 * the Mode Control Register.
161 *
162 * In situations where a single ratio is represented by multiple speed
163 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
164 * double-speed instead of quad-speed. However, the CS4270 errata states
Timur Tabiff7bf022009-01-30 11:14:49 -0600165 * that divide-By-1.5 can cause failures, so we avoid that mode where
Timur Tabi84323952007-12-18 15:42:53 +0100166 * possible.
167 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600168 * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
169 * work if Vd is 3.3V. If this effects you, select the
Timur Tabi84323952007-12-18 15:42:53 +0100170 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
171 * never select any sample rates that require divide-by-1.5.
172 */
Timur Tabiff7bf022009-01-30 11:14:49 -0600173struct cs4270_mode_ratios {
Timur Tabi84323952007-12-18 15:42:53 +0100174 unsigned int ratio;
175 u8 speed_mode;
176 u8 mclk;
Timur Tabiff7bf022009-01-30 11:14:49 -0600177};
178
Timur Tabid9fb7fb2009-02-02 14:50:45 -0600179static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
Timur Tabi84323952007-12-18 15:42:53 +0100180 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
181#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
182 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
183#endif
184 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
185 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
186 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
187 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
188 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
189 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
190 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
191};
192
193/* The number of MCLK/LRCK ratios supported by the CS4270 */
194#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
195
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000196static int cs4270_reg_is_readable(struct snd_soc_codec *codec, unsigned int reg)
Timur Tabi11b8fca2011-01-10 13:28:32 -0600197{
198 return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
199}
200
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000201static int cs4270_reg_is_volatile(struct snd_soc_codec *codec, unsigned int reg)
Timur Tabi11b8fca2011-01-10 13:28:32 -0600202{
203 /* Unreadable registers are considered volatile */
204 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
205 return 1;
206
207 return reg == CS4270_CHIPID;
208}
209
Timur Tabiff7bf022009-01-30 11:14:49 -0600210/**
211 * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
212 * @codec_dai: the codec DAI
213 * @clk_id: the clock ID (ignored)
214 * @freq: the MCLK input frequency
215 * @dir: the clock direction (ignored)
Timur Tabi84323952007-12-18 15:42:53 +0100216 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600217 * This function is used to tell the codec driver what the input MCLK
218 * frequency is.
Timur Tabi84323952007-12-18 15:42:53 +0100219 *
220 * The value of MCLK is used to determine which sample rates are supported
221 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
Timur Tabiff7bf022009-01-30 11:14:49 -0600222 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
Timur Tabi84323952007-12-18 15:42:53 +0100223 *
224 * This function calculates the nine ratios and determines which ones match
225 * a standard sample rate. If there's a match, then it is added to the list
Timur Tabiff7bf022009-01-30 11:14:49 -0600226 * of supported sample rates.
Timur Tabi84323952007-12-18 15:42:53 +0100227 *
228 * This function must be called by the machine driver's 'startup' function,
229 * otherwise the list of supported sample rates will not be available in
230 * time for ALSA.
Daniel Mack6aababd2010-01-15 17:36:48 +0100231 *
232 * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
233 * theoretically possible sample rates to be enabled. Call it again with a
234 * proper value set one the external clock is set (most probably you would do
235 * that from a machine's driver 'hw_param' hook.
Timur Tabi84323952007-12-18 15:42:53 +0100236 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100237static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100238 int clk_id, unsigned int freq, int dir)
239{
240 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900241 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabi84323952007-12-18 15:42:53 +0100242
243 cs4270->mclk = freq;
Timur Tabi84323952007-12-18 15:42:53 +0100244 return 0;
245}
246
Timur Tabiff7bf022009-01-30 11:14:49 -0600247/**
248 * cs4270_set_dai_fmt - configure the codec for the selected audio format
249 * @codec_dai: the codec DAI
250 * @format: a SND_SOC_DAIFMT_x value indicating the data format
Timur Tabi84323952007-12-18 15:42:53 +0100251 *
252 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
253 * codec accordingly.
254 *
255 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
256 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
257 * data for playback only, but ASoC currently does not support different
258 * formats for playback vs. record.
259 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100260static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100261 unsigned int format)
262{
263 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900264 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabi84323952007-12-18 15:42:53 +0100265
Daniel Mack4eae0802009-02-25 14:37:21 +0100266 /* set DAI format */
Timur Tabi84323952007-12-18 15:42:53 +0100267 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
268 case SND_SOC_DAIFMT_I2S:
269 case SND_SOC_DAIFMT_LEFT_J:
270 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
271 break;
272 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600273 dev_err(codec->dev, "invalid dai format\n");
Axel Linac601552011-10-06 07:29:56 +0800274 return -EINVAL;
Timur Tabi84323952007-12-18 15:42:53 +0100275 }
276
Daniel Mack4eae0802009-02-25 14:37:21 +0100277 /* set master/slave audio interface */
278 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
279 case SND_SOC_DAIFMT_CBS_CFS:
280 cs4270->slave_mode = 1;
281 break;
282 case SND_SOC_DAIFMT_CBM_CFM:
283 cs4270->slave_mode = 0;
284 break;
Daniel Mack4eae0802009-02-25 14:37:21 +0100285 default:
Daniel Mackff09d492009-02-28 13:21:03 +0100286 /* all other modes are unsupported by the hardware */
Axel Linac601552011-10-06 07:29:56 +0800287 dev_err(codec->dev, "Unknown master/slave configuration\n");
288 return -EINVAL;
Daniel Mack4eae0802009-02-25 14:37:21 +0100289 }
290
Axel Linac601552011-10-06 07:29:56 +0800291 return 0;
Timur Tabi84323952007-12-18 15:42:53 +0100292}
293
Timur Tabiff7bf022009-01-30 11:14:49 -0600294/**
Timur Tabiff7bf022009-01-30 11:14:49 -0600295 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
296 * @substream: the audio stream
297 * @params: the hardware parameters to set
298 * @dai: the SOC DAI (ignored)
Timur Tabib0c813c2007-07-31 18:18:44 +0200299 *
Timur Tabiff7bf022009-01-30 11:14:49 -0600300 * This function programs the hardware with the values provided.
301 * Specifically, the sample rate and the data format.
302 *
303 * The .ops functions are used to provide board-specific data, like input
304 * frequencies, to this driver. This function takes that information,
Timur Tabib0c813c2007-07-31 18:18:44 +0200305 * combines it with the hardware parameters provided, and programs the
306 * hardware accordingly.
307 */
308static int cs4270_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000309 struct snd_pcm_hw_params *params,
310 struct snd_soc_dai *dai)
Timur Tabib0c813c2007-07-31 18:18:44 +0200311{
Mark Browne6968a12012-04-04 15:58:16 +0100312 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900313 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Roel Kluine34ba212008-04-17 18:58:34 +0200314 int ret;
Timur Tabib0c813c2007-07-31 18:18:44 +0200315 unsigned int i;
316 unsigned int rate;
317 unsigned int ratio;
318 int reg;
319
320 /* Figure out which MCLK/LRCK ratio to use */
321
322 rate = params_rate(params); /* Sampling rate, in Hz */
323 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
324
Timur Tabi9dbd6272007-08-01 12:22:07 +0200325 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
Timur Tabi84323952007-12-18 15:42:53 +0100326 if (cs4270_mode_ratios[i].ratio == ratio)
Timur Tabib0c813c2007-07-31 18:18:44 +0200327 break;
328 }
329
Timur Tabi9dbd6272007-08-01 12:22:07 +0200330 if (i == NUM_MCLK_RATIOS) {
Timur Tabib0c813c2007-07-31 18:18:44 +0200331 /* We did not find a matching ratio */
Timur Tabia6c255e2009-02-02 15:08:29 -0600332 dev_err(codec->dev, "could not find matching ratio\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200333 return -EINVAL;
334 }
335
Timur Tabid5e9ba12009-02-03 11:09:32 -0600336 /* Set the sample rate */
Timur Tabib0c813c2007-07-31 18:18:44 +0200337
338 reg = snd_soc_read(codec, CS4270_MODE);
339 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
Daniel Mack4eae0802009-02-25 14:37:21 +0100340 reg |= cs4270_mode_ratios[i].mclk;
341
342 if (cs4270->slave_mode)
343 reg |= CS4270_MODE_SLAVE;
344 else
345 reg |= cs4270_mode_ratios[i].speed_mode;
Timur Tabib0c813c2007-07-31 18:18:44 +0200346
347 ret = snd_soc_write(codec, CS4270_MODE, reg);
348 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600349 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200350 return ret;
351 }
352
Timur Tabid5e9ba12009-02-03 11:09:32 -0600353 /* Set the DAI format */
Timur Tabib0c813c2007-07-31 18:18:44 +0200354
355 reg = snd_soc_read(codec, CS4270_FORMAT);
356 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
357
358 switch (cs4270->mode) {
359 case SND_SOC_DAIFMT_I2S:
360 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
361 break;
362 case SND_SOC_DAIFMT_LEFT_J:
363 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
364 break;
365 default:
Timur Tabia6c255e2009-02-02 15:08:29 -0600366 dev_err(codec->dev, "unknown dai format\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200367 return -EINVAL;
368 }
369
370 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
371 if (ret < 0) {
Timur Tabia6c255e2009-02-02 15:08:29 -0600372 dev_err(codec->dev, "i2c write failed\n");
Timur Tabib0c813c2007-07-31 18:18:44 +0200373 return ret;
374 }
375
Timur Tabib0c813c2007-07-31 18:18:44 +0200376 return ret;
377}
378
Timur Tabiff7bf022009-01-30 11:14:49 -0600379/**
Daniel Mack1a4ba052009-04-24 16:37:45 +0200380 * cs4270_dai_mute - enable/disable the CS4270 external mute
Timur Tabiff7bf022009-01-30 11:14:49 -0600381 * @dai: the SOC DAI
382 * @mute: 0 = disable mute, 1 = enable mute
Timur Tabib0c813c2007-07-31 18:18:44 +0200383 *
384 * This function toggles the mute bits in the MUTE register. The CS4270's
385 * mute capability is intended for external muting circuitry, so if the
386 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
387 * then this function will do nothing.
388 */
Daniel Mack1a4ba052009-04-24 16:37:45 +0200389static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
Timur Tabib0c813c2007-07-31 18:18:44 +0200390{
391 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900392 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabib0c813c2007-07-31 18:18:44 +0200393 int reg6;
394
395 reg6 = snd_soc_read(codec, CS4270_MUTE);
396
397 if (mute)
Timur Tabid5e9ba12009-02-03 11:09:32 -0600398 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
Daniel Mack1a4ba052009-04-24 16:37:45 +0200399 else {
Timur Tabid5e9ba12009-02-03 11:09:32 -0600400 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
Daniel Mack1a4ba052009-04-24 16:37:45 +0200401 reg6 |= cs4270->manual_mute;
402 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200403
404 return snd_soc_write(codec, CS4270_MUTE, reg6);
405}
Timur Tabib0c813c2007-07-31 18:18:44 +0200406
Daniel Mack1a4ba052009-04-24 16:37:45 +0200407/**
408 * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
409 * alsa control.
410 * @kcontrol: mixer control
411 * @ucontrol: control element information
412 *
413 * This function basically passes the arguments on to the generic
414 * snd_soc_put_volsw() function and saves the mute information in
415 * our private data structure. This is because we want to prevent
416 * cs4270_dai_mute() neglecting the user's decision to manually
417 * mute the codec's output.
418 *
419 * Returns 0 for success.
420 */
421static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
422 struct snd_ctl_elem_value *ucontrol)
423{
424 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900425 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Daniel Mack1a4ba052009-04-24 16:37:45 +0200426 int left = !ucontrol->value.integer.value[0];
427 int right = !ucontrol->value.integer.value[1];
428
429 cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
430 (right ? CS4270_MUTE_DAC_B : 0);
431
432 return snd_soc_put_volsw(kcontrol, ucontrol);
433}
434
Timur Tabib0c813c2007-07-31 18:18:44 +0200435/* A list of non-DAPM controls that the CS4270 supports */
436static const struct snd_kcontrol_new cs4270_snd_controls[] = {
437 SOC_DOUBLE_R("Master Playback Volume",
Timur Tabid5e9ba12009-02-03 11:09:32 -0600438 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
439 SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
440 SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
441 SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
Daniel Mack7e1aa1d2009-10-29 02:24:32 +0100442 SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
Timur Tabid5e9ba12009-02-03 11:09:32 -0600443 SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
444 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
Daniel Mack1a4ba052009-04-24 16:37:45 +0200445 SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
446 SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
447 snd_soc_get_volsw, cs4270_soc_put_mute),
Timur Tabib0c813c2007-07-31 18:18:44 +0200448};
449
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100450static const struct snd_soc_dai_ops cs4270_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800451 .hw_params = cs4270_hw_params,
452 .set_sysclk = cs4270_set_dai_sysclk,
453 .set_fmt = cs4270_set_dai_fmt,
Daniel Mack1a4ba052009-04-24 16:37:45 +0200454 .digital_mute = cs4270_dai_mute,
Eric Miao6335d052009-03-03 09:41:00 +0800455};
456
Mark Brown5c758482010-10-06 16:18:17 -0700457static struct snd_soc_dai_driver cs4270_dai = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 .name = "cs4270-hifi",
Timur Tabib0c813c2007-07-31 18:18:44 +0200459 .playback = {
460 .stream_name = "Playback",
461 .channels_min = 1,
462 .channels_max = 2,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 .rates = SNDRV_PCM_RATE_CONTINUOUS,
464 .rate_min = 4000,
465 .rate_max = 216000,
Timur Tabib0c813c2007-07-31 18:18:44 +0200466 .formats = CS4270_FORMATS,
467 },
468 .capture = {
469 .stream_name = "Capture",
470 .channels_min = 1,
471 .channels_max = 2,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 .rates = SNDRV_PCM_RATE_CONTINUOUS,
473 .rate_min = 4000,
474 .rate_max = 216000,
Timur Tabib0c813c2007-07-31 18:18:44 +0200475 .formats = CS4270_FORMATS,
476 },
Eric Miao6335d052009-03-03 09:41:00 +0800477 .ops = &cs4270_dai_ops,
Timur Tabib0c813c2007-07-31 18:18:44 +0200478};
Timur Tabib0c813c2007-07-31 18:18:44 +0200479
Timur Tabiff7bf022009-01-30 11:14:49 -0600480/**
481 * cs4270_probe - ASoC probe function
482 * @pdev: platform device
483 *
484 * This function is called when ASoC has all the pieces it needs to
485 * instantiate a sound driver.
Timur Tabi04eb0932009-01-29 14:28:37 -0600486 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487static int cs4270_probe(struct snd_soc_codec *codec)
Timur Tabi04eb0932009-01-29 14:28:37 -0600488{
Mark Brownb2c812e2010-04-14 15:35:19 +0900489 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabi11b8fca2011-01-10 13:28:32 -0600490 int i, ret;
Timur Tabi04eb0932009-01-29 14:28:37 -0600491
Timur Tabi11b8fca2011-01-10 13:28:32 -0600492 /* Tell ASoC what kind of I/O to use to read the registers. ASoC will
493 * then do the I2C transactions itself.
494 */
495 ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs4270->control_type);
Timur Tabi0db4d072009-01-23 16:31:19 -0600496 if (ret < 0) {
Timur Tabi11b8fca2011-01-10 13:28:32 -0600497 dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 return ret;
Timur Tabi0db4d072009-01-23 16:31:19 -0600499 }
Timur Tabib0c813c2007-07-31 18:18:44 +0200500
Timur Tabid5e9ba12009-02-03 11:09:32 -0600501 /* Disable auto-mute. This feature appears to be buggy. In some
502 * situations, auto-mute will not deactivate when it should, so we want
503 * this feature disabled by default. An application (e.g. alsactl) can
504 * re-enabled it by using the controls.
505 */
Timur Tabi11b8fca2011-01-10 13:28:32 -0600506 ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
Timur Tabid5e9ba12009-02-03 11:09:32 -0600507 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 dev_err(codec->dev, "i2c write failed\n");
Timur Tabid5e9ba12009-02-03 11:09:32 -0600509 return ret;
510 }
511
512 /* Disable automatic volume control. The hardware enables, and it
513 * causes volume change commands to be delayed, sometimes until after
514 * playback has started. An application (e.g. alsactl) can
515 * re-enabled it by using the controls.
516 */
Timur Tabi11b8fca2011-01-10 13:28:32 -0600517 ret = snd_soc_update_bits(codec, CS4270_TRANS,
518 CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
Timur Tabid5e9ba12009-02-03 11:09:32 -0600519 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 dev_err(codec->dev, "i2c write failed\n");
Timur Tabid5e9ba12009-02-03 11:09:32 -0600521 return ret;
522 }
523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 /* Add the non-DAPM controls */
Liam Girdwood022658b2012-02-03 17:43:09 +0000525 ret = snd_soc_add_codec_controls(codec, cs4270_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 ARRAY_SIZE(cs4270_snd_controls));
Timur Tabib0c813c2007-07-31 18:18:44 +0200527 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 dev_err(codec->dev, "failed to add controls\n");
529 return ret;
Timur Tabib0c813c2007-07-31 18:18:44 +0200530 }
531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 /* get the power supply regulators */
533 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
534 cs4270->supplies[i].supply = supply_names[i];
535
536 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
537 cs4270->supplies);
538 if (ret < 0)
539 return ret;
540
541 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
542 cs4270->supplies);
543 if (ret < 0)
544 goto error_free_regulators;
Jean Delvaree3145df2008-09-30 11:40:37 +0200545
Timur Tabi0db4d072009-01-23 16:31:19 -0600546 return 0;
Jean Delvaree3145df2008-09-30 11:40:37 +0200547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548error_free_regulators:
549 regulator_bulk_free(ARRAY_SIZE(cs4270->supplies),
550 cs4270->supplies);
Jean Delvaree3145df2008-09-30 11:40:37 +0200551
Timur Tabib0c813c2007-07-31 18:18:44 +0200552 return ret;
553}
554
Timur Tabiff7bf022009-01-30 11:14:49 -0600555/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 * cs4270_remove - ASoC remove function
557 * @pdev: platform device
Timur Tabiff7bf022009-01-30 11:14:49 -0600558 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 * This function is the counterpart to cs4270_probe().
Timur Tabiff7bf022009-01-30 11:14:49 -0600560 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561static int cs4270_remove(struct snd_soc_codec *codec)
Timur Tabib0c813c2007-07-31 18:18:44 +0200562{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Timur Tabib0c813c2007-07-31 18:18:44 +0200564
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
566 regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
Timur Tabib0c813c2007-07-31 18:18:44 +0200567
Timur Tabi0db4d072009-01-23 16:31:19 -0600568 return 0;
Timur Tabi0db4d072009-01-23 16:31:19 -0600569};
Timur Tabi0db4d072009-01-23 16:31:19 -0600570
Daniel Mack5e7c0342009-05-06 01:26:01 +0200571#ifdef CONFIG_PM
572
573/* This suspend/resume implementation can handle both - a simple standby
574 * where the codec remains powered, and a full suspend, where the voltage
575 * domain the codec is connected to is teared down and/or any other hardware
576 * reset condition is asserted.
577 *
578 * The codec's own power saving features are enabled in the suspend callback,
579 * and all registers are written back to the hardware when resuming.
580 */
581
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100582static int cs4270_soc_suspend(struct snd_soc_codec *codec)
Daniel Mack15b5bda2009-08-05 20:50:43 +0200583{
Mark Brownb2c812e2010-04-14 15:35:19 +0900584 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Daniel Mackffbfd332009-11-30 17:56:11 +0100585 int reg, ret;
Daniel Mack15b5bda2009-08-05 20:50:43 +0200586
Daniel Mackffbfd332009-11-30 17:56:11 +0100587 reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
588 if (reg < 0)
589 return reg;
590
591 ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
592 if (ret < 0)
593 return ret;
594
595 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
596 cs4270->supplies);
597
598 return 0;
Daniel Mack15b5bda2009-08-05 20:50:43 +0200599}
600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601static int cs4270_soc_resume(struct snd_soc_codec *codec)
Daniel Mack15b5bda2009-08-05 20:50:43 +0200602{
Mark Brownb2c812e2010-04-14 15:35:19 +0900603 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
Mark Brownab92d092012-03-19 16:15:43 +0000604 int reg, ret;
Daniel Mack5e7c0342009-05-06 01:26:01 +0200605
Mark Brownab92d092012-03-19 16:15:43 +0000606 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
607 cs4270->supplies);
608 if (ret != 0)
609 return ret;
Daniel Mackffbfd332009-11-30 17:56:11 +0100610
Daniel Mack5e7c0342009-05-06 01:26:01 +0200611 /* In case the device was put to hard reset during sleep, we need to
612 * wait 500ns here before any I2C communication. */
613 ndelay(500);
614
615 /* first restore the entire register cache ... */
Daniel Mackd66b8532011-11-22 17:17:23 +0100616 snd_soc_cache_sync(codec);
Daniel Mack5e7c0342009-05-06 01:26:01 +0200617
618 /* ... then disable the power-down bits */
619 reg = snd_soc_read(codec, CS4270_PWRCTL);
620 reg &= ~CS4270_PWRCTL_PDN_ALL;
621
622 return snd_soc_write(codec, CS4270_PWRCTL, reg);
623}
624#else
Daniel Mack15b5bda2009-08-05 20:50:43 +0200625#define cs4270_soc_suspend NULL
626#define cs4270_soc_resume NULL
Daniel Mack5e7c0342009-05-06 01:26:01 +0200627#endif /* CONFIG_PM */
628
Timur Tabiff7bf022009-01-30 11:14:49 -0600629/*
Daniel Mackb6f7d7c2011-05-25 09:53:12 +0200630 * ASoC codec driver structure
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 */
Timur Tabi11b8fca2011-01-10 13:28:32 -0600632static const struct snd_soc_codec_driver soc_codec_device_cs4270 = {
633 .probe = cs4270_probe,
634 .remove = cs4270_remove,
635 .suspend = cs4270_soc_suspend,
636 .resume = cs4270_soc_resume,
637 .volatile_register = cs4270_reg_is_volatile,
638 .readable_register = cs4270_reg_is_readable,
639 .reg_cache_size = CS4270_LASTREG + 1,
640 .reg_word_size = sizeof(u8),
641 .reg_cache_default = cs4270_default_reg_cache,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642};
643
Daniel Mack85d07e42012-07-25 15:28:34 +0200644/*
645 * cs4270_of_match - the device tree bindings
646 */
647static const struct of_device_id cs4270_of_match[] = {
648 { .compatible = "cirrus,cs4270", },
649 { }
650};
651MODULE_DEVICE_TABLE(of, cs4270_of_match);
652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653/**
654 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
655 * @i2c_client: the I2C client object
656 * @id: the I2C device ID (ignored)
657 *
658 * This function is called whenever the I2C subsystem finds a device that
659 * matches the device ID given via a prior call to i2c_add_driver().
660 */
661static int cs4270_i2c_probe(struct i2c_client *i2c_client,
662 const struct i2c_device_id *id)
663{
Daniel Mack02286192012-07-25 15:28:35 +0200664 struct device_node *np = i2c_client->dev.of_node;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 struct cs4270_private *cs4270;
666 int ret;
667
Daniel Mack02286192012-07-25 15:28:35 +0200668 /* See if we have a way to bring the codec out of reset */
669 if (np) {
670 enum of_gpio_flags flags;
671 int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
672
673 if (gpio_is_valid(gpio)) {
674 ret = devm_gpio_request_one(&i2c_client->dev, gpio,
675 flags & OF_GPIO_ACTIVE_LOW ?
676 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
677 "cs4270 reset");
678 if (ret < 0)
679 return ret;
680 }
681 }
682
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683 /* Verify that we have a CS4270 */
684
685 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
686 if (ret < 0) {
687 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
688 i2c_client->addr);
689 return ret;
690 }
691 /* The top four bits of the chip ID should be 1100. */
692 if ((ret & 0xF0) != 0xC0) {
693 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
694 i2c_client->addr);
695 return -ENODEV;
696 }
697
698 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
699 i2c_client->addr);
700 dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
701
Axel Lin7fd8a672011-12-29 11:58:22 +0800702 cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
703 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 if (!cs4270) {
705 dev_err(&i2c_client->dev, "could not allocate codec\n");
706 return -ENOMEM;
707 }
708
709 i2c_set_clientdata(i2c_client, cs4270);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 cs4270->control_type = SND_SOC_I2C;
711
712 ret = snd_soc_register_codec(&i2c_client->dev,
713 &soc_codec_device_cs4270, &cs4270_dai, 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 return ret;
715}
716
717/**
718 * cs4270_i2c_remove - remove an I2C device
719 * @i2c_client: the I2C client object
720 *
721 * This function is the counterpart to cs4270_i2c_probe().
722 */
723static int cs4270_i2c_remove(struct i2c_client *i2c_client)
724{
725 snd_soc_unregister_codec(&i2c_client->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 return 0;
727}
728
729/*
730 * cs4270_id - I2C device IDs supported by this driver
731 */
Axel Lin79a54ea2011-03-04 15:22:03 +0800732static const struct i2c_device_id cs4270_id[] = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 {"cs4270", 0},
734 {}
735};
736MODULE_DEVICE_TABLE(i2c, cs4270_id);
737
738/*
Timur Tabiff7bf022009-01-30 11:14:49 -0600739 * cs4270_i2c_driver - I2C device identification
740 *
741 * This structure tells the I2C subsystem how to identify and support a
742 * given I2C device type.
743 */
Timur Tabi0db4d072009-01-23 16:31:19 -0600744static struct i2c_driver cs4270_i2c_driver = {
745 .driver = {
Shawn Guo64902b22012-02-24 22:09:38 +0800746 .name = "cs4270",
Timur Tabi0db4d072009-01-23 16:31:19 -0600747 .owner = THIS_MODULE,
Daniel Mack85d07e42012-07-25 15:28:34 +0200748 .of_match_table = cs4270_of_match,
Timur Tabi0db4d072009-01-23 16:31:19 -0600749 },
750 .id_table = cs4270_id,
751 .probe = cs4270_i2c_probe,
752 .remove = cs4270_i2c_remove,
753};
754
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100755static int __init cs4270_init(void)
Mark Brown64089b82008-12-08 19:17:58 +0000756{
Timur Tabi04eb0932009-01-29 14:28:37 -0600757 return i2c_add_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000758}
759module_init(cs4270_init);
760
761static void __exit cs4270_exit(void)
762{
Timur Tabi04eb0932009-01-29 14:28:37 -0600763 i2c_del_driver(&cs4270_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000764}
765module_exit(cs4270_exit);
766
Timur Tabib0c813c2007-07-31 18:18:44 +0200767MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
768MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
769MODULE_LICENSE("GPL");