ASoC: cs4270: Move regulator acquisition to I2C probe()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / codecs / cs4270.c
CommitLineData
b0c813ce
TT
1/*
2 * CS4270 ALSA SoC (ASoC) codec driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
ff7bf02f
TT
6 * 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.
b0c813ce
TT
10 *
11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
12 *
13 * Current features/limitations:
14 *
b191f63c
DM
15 * - 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
5e7c0344 21 * - Power management is supported
b0c813ce
TT
22 */
23
24#include <linux/module.h>
5a0e3ad6 25#include <linux/slab.h>
b0c813ce
TT
26#include <sound/core.h>
27#include <sound/soc.h>
28#include <sound/initval.h>
29#include <linux/i2c.h>
5e7c0344 30#include <linux/delay.h>
ffbfd336 31#include <linux/regulator/consumer.h>
85d07e4d 32#include <linux/of_device.h>
02286190 33#include <linux/of_gpio.h>
b0c813ce 34
8432395f
TT
35/*
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
8432395f
TT
48/* 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)
80ab8817 61#define CS4270_I2C_INCR 0x80
9dbd627b 62
8432395f
TT
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
5e7c0344
DM
70#define CS4270_PWRCTL_PDN_ALL \
71 (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
8432395f
TT
72#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
11b8fca5
TT
110/* 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
ffbfd336
DM
125static const char *supply_names[] = {
126 "va", "vd", "vlc"
127};
128
0db4d070
TT
129/* Private data for the CS4270 */
130struct cs4270_private {
f0fba2ad 131 enum snd_soc_control_type control_type;
0db4d070
TT
132 unsigned int mclk; /* Input frequency of the MCLK pin */
133 unsigned int mode; /* The mode (I2S or left-justified) */
4eae080d 134 unsigned int slave_mode;
1a4ba05e 135 unsigned int manual_mute;
ffbfd336
DM
136
137 /* power domain regulators */
138 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
0db4d070
TT
139};
140
ff7bf02f
TT
141/**
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
8432395f
TT
148 *
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 *
ff7bf02f 156 * @speed_mode is the corresponding bit pattern to be written to the
8432395f
TT
157 * MODE bits of the Mode Control Register
158 *
ff7bf02f 159 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
8432395f
TT
160 * 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
ff7bf02f 165 * that divide-By-1.5 can cause failures, so we avoid that mode where
8432395f
TT
166 * possible.
167 *
ff7bf02f
TT
168 * 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
8432395f
TT
170 * 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 */
ff7bf02f 173struct cs4270_mode_ratios {
8432395f
TT
174 unsigned int ratio;
175 u8 speed_mode;
176 u8 mclk;
ff7bf02f
TT
177};
178
d9fb7fbd 179static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
8432395f
TT
180 {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)
9dbd627b 195
d4754ec9 196static int cs4270_reg_is_readable(struct snd_soc_codec *codec, unsigned int reg)
11b8fca5
TT
197{
198 return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
199}
200
d4754ec9 201static int cs4270_reg_is_volatile(struct snd_soc_codec *codec, unsigned int reg)
11b8fca5
TT
202{
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
ff7bf02f
TT
210/**
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)
9dbd627b 216 *
ff7bf02f
TT
217 * This function is used to tell the codec driver what the input MCLK
218 * frequency is.
9dbd627b
TT
219 *
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
ff7bf02f 222 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
9dbd627b
TT
223 *
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
ff7bf02f 226 * of supported sample rates.
9dbd627b
TT
227 *
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.
6aababdf
DM
231 *
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.
9dbd627b 236 */
e550e17f 237static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
9dbd627b
TT
238 int clk_id, unsigned int freq, int dir)
239{
240 struct snd_soc_codec *codec = codec_dai->codec;
b2c812e2 241 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
9dbd627b
TT
242
243 cs4270->mclk = freq;
9dbd627b
TT
244 return 0;
245}
246
ff7bf02f
TT
247/**
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
9dbd627b
TT
251 *
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 */
e550e17f 260static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
9dbd627b
TT
261 unsigned int format)
262{
263 struct snd_soc_codec *codec = codec_dai->codec;
b2c812e2 264 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
9dbd627b 265
4eae080d 266 /* set DAI format */
9dbd627b
TT
267 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:
a6c255e0 273 dev_err(codec->dev, "invalid dai format\n");
ac60155f 274 return -EINVAL;
9dbd627b
TT
275 }
276
4eae080d
DM
277 /* 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;
4eae080d 285 default:
ff09d49a 286 /* all other modes are unsupported by the hardware */
ac60155f
AL
287 dev_err(codec->dev, "Unknown master/slave configuration\n");
288 return -EINVAL;
4eae080d
DM
289 }
290
ac60155f 291 return 0;
9dbd627b
TT
292}
293
ff7bf02f
TT
294/**
295 * 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)
b0c813ce 299 *
ff7bf02f
TT
300 * 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,
b0c813ce
TT
305 * combines it with the hardware parameters provided, and programs the
306 * hardware accordingly.
307 */
308static int cs4270_hw_params(struct snd_pcm_substream *substream,
dee89c4d
MB
309 struct snd_pcm_hw_params *params,
310 struct snd_soc_dai *dai)
b0c813ce 311{
e6968a17 312 struct snd_soc_codec *codec = dai->codec;
b2c812e2 313 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
e34ba212 314 int ret;
b0c813ce
TT
315 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
9dbd627b 325 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
8432395f 326 if (cs4270_mode_ratios[i].ratio == ratio)
b0c813ce
TT
327 break;
328 }
329
9dbd627b 330 if (i == NUM_MCLK_RATIOS) {
b0c813ce 331 /* We did not find a matching ratio */
a6c255e0 332 dev_err(codec->dev, "could not find matching ratio\n");
b0c813ce
TT
333 return -EINVAL;
334 }
335
d5e9ba1d 336 /* Set the sample rate */
b0c813ce
TT
337
338 reg = snd_soc_read(codec, CS4270_MODE);
339 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
4eae080d
DM
340 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;
b0c813ce
TT
346
347 ret = snd_soc_write(codec, CS4270_MODE, reg);
348 if (ret < 0) {
a6c255e0 349 dev_err(codec->dev, "i2c write failed\n");
b0c813ce
TT
350 return ret;
351 }
352
d5e9ba1d 353 /* Set the DAI format */
b0c813ce
TT
354
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:
a6c255e0 366 dev_err(codec->dev, "unknown dai format\n");
b0c813ce
TT
367 return -EINVAL;
368 }
369
370 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
371 if (ret < 0) {
a6c255e0 372 dev_err(codec->dev, "i2c write failed\n");
b0c813ce
TT
373 return ret;
374 }
375
b0c813ce
TT
376 return ret;
377}
378
ff7bf02f 379/**
1a4ba05e 380 * cs4270_dai_mute - enable/disable the CS4270 external mute
ff7bf02f
TT
381 * @dai: the SOC DAI
382 * @mute: 0 = disable mute, 1 = enable mute
b0c813ce
TT
383 *
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 */
1a4ba05e 389static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
b0c813ce
TT
390{
391 struct snd_soc_codec *codec = dai->codec;
b2c812e2 392 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
b0c813ce
TT
393 int reg6;
394
395 reg6 = snd_soc_read(codec, CS4270_MUTE);
396
397 if (mute)
d5e9ba1d 398 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
1a4ba05e 399 else {
d5e9ba1d 400 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
1a4ba05e
DM
401 reg6 |= cs4270->manual_mute;
402 }
b0c813ce
TT
403
404 return snd_soc_write(codec, CS4270_MUTE, reg6);
405}
b0c813ce 406
1a4ba05e
DM
407/**
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);
b2c812e2 425 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
1a4ba05e
DM
426 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
b0c813ce
TT
435/* 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",
d5e9ba1d
TT
438 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),
7e1aa1dc 442 SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
d5e9ba1d
TT
443 SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
444 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
1a4ba05e
DM
445 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),
b0c813ce
TT
448};
449
85e7652d 450static const struct snd_soc_dai_ops cs4270_dai_ops = {
6335d055
EM
451 .hw_params = cs4270_hw_params,
452 .set_sysclk = cs4270_set_dai_sysclk,
453 .set_fmt = cs4270_set_dai_fmt,
1a4ba05e 454 .digital_mute = cs4270_dai_mute,
6335d055
EM
455};
456
5c75848a 457static struct snd_soc_dai_driver cs4270_dai = {
f0fba2ad 458 .name = "cs4270-hifi",
0db4d070
TT
459 .playback = {
460 .stream_name = "Playback",
461 .channels_min = 1,
462 .channels_max = 2,
f0fba2ad
LG
463 .rates = SNDRV_PCM_RATE_CONTINUOUS,
464 .rate_min = 4000,
465 .rate_max = 216000,
0db4d070
TT
466 .formats = CS4270_FORMATS,
467 },
468 .capture = {
469 .stream_name = "Capture",
470 .channels_min = 1,
471 .channels_max = 2,
f0fba2ad
LG
472 .rates = SNDRV_PCM_RATE_CONTINUOUS,
473 .rate_min = 4000,
474 .rate_max = 216000,
0db4d070
TT
475 .formats = CS4270_FORMATS,
476 },
6335d055 477 .ops = &cs4270_dai_ops,
0db4d070 478};
0db4d070 479
ff7bf02f
TT
480/**
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.
04eb093c 486 */
f0fba2ad 487static int cs4270_probe(struct snd_soc_codec *codec)
04eb093c 488{
b2c812e2 489 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
b61d6d40 490 int ret;
04eb093c 491
11b8fca5
TT
492 /* 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);
b0c813ce 496 if (ret < 0) {
11b8fca5 497 dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret);
f0fba2ad 498 return ret;
0db4d070
TT
499 }
500
d5e9ba1d
TT
501 /* 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 */
11b8fca5 506 ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
d5e9ba1d 507 if (ret < 0) {
f0fba2ad 508 dev_err(codec->dev, "i2c write failed\n");
d5e9ba1d
TT
509 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 */
11b8fca5
TT
517 ret = snd_soc_update_bits(codec, CS4270_TRANS,
518 CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
d5e9ba1d 519 if (ret < 0) {
f0fba2ad 520 dev_err(codec->dev, "i2c write failed\n");
d5e9ba1d
TT
521 return ret;
522 }
523
f0fba2ad
LG
524 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
525 cs4270->supplies);
b0c813ce 526
b0c813ce
TT
527 return ret;
528}
529
ff7bf02f 530/**
f0fba2ad
LG
531 * cs4270_remove - ASoC remove function
532 * @pdev: platform device
ff7bf02f 533 *
f0fba2ad 534 * This function is the counterpart to cs4270_probe().
ff7bf02f 535 */
f0fba2ad 536static int cs4270_remove(struct snd_soc_codec *codec)
0db4d070 537{
f0fba2ad 538 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
0db4d070 539
f0fba2ad 540 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
0db4d070
TT
541
542 return 0;
ff637d38 543};
ff637d38 544
5e7c0344
DM
545#ifdef CONFIG_PM
546
547/* This suspend/resume implementation can handle both - a simple standby
548 * where the codec remains powered, and a full suspend, where the voltage
549 * domain the codec is connected to is teared down and/or any other hardware
550 * reset condition is asserted.
551 *
552 * The codec's own power saving features are enabled in the suspend callback,
553 * and all registers are written back to the hardware when resuming.
554 */
555
84b315ee 556static int cs4270_soc_suspend(struct snd_soc_codec *codec)
15b5bdae 557{
b2c812e2 558 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
ffbfd336 559 int reg, ret;
15b5bdae 560
ffbfd336
DM
561 reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
562 if (reg < 0)
563 return reg;
564
565 ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
566 if (ret < 0)
567 return ret;
568
569 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
570 cs4270->supplies);
571
572 return 0;
15b5bdae
DM
573}
574
f0fba2ad 575static int cs4270_soc_resume(struct snd_soc_codec *codec)
15b5bdae 576{
b2c812e2 577 struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
ab92d09d 578 int reg, ret;
5e7c0344 579
ab92d09d
MB
580 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
581 cs4270->supplies);
582 if (ret != 0)
583 return ret;
ffbfd336 584
5e7c0344
DM
585 /* In case the device was put to hard reset during sleep, we need to
586 * wait 500ns here before any I2C communication. */
587 ndelay(500);
588
589 /* first restore the entire register cache ... */
d66b8537 590 snd_soc_cache_sync(codec);
5e7c0344
DM
591
592 /* ... then disable the power-down bits */
593 reg = snd_soc_read(codec, CS4270_PWRCTL);
594 reg &= ~CS4270_PWRCTL_PDN_ALL;
595
596 return snd_soc_write(codec, CS4270_PWRCTL, reg);
597}
598#else
15b5bdae
DM
599#define cs4270_soc_suspend NULL
600#define cs4270_soc_resume NULL
5e7c0344
DM
601#endif /* CONFIG_PM */
602
f0fba2ad 603/*
b6f7d7c8 604 * ASoC codec driver structure
f0fba2ad 605 */
11b8fca5
TT
606static const struct snd_soc_codec_driver soc_codec_device_cs4270 = {
607 .probe = cs4270_probe,
608 .remove = cs4270_remove,
609 .suspend = cs4270_soc_suspend,
610 .resume = cs4270_soc_resume,
19ace0e9
MB
611
612 .controls = cs4270_snd_controls,
613 .num_controls = ARRAY_SIZE(cs4270_snd_controls),
11b8fca5
TT
614 .volatile_register = cs4270_reg_is_volatile,
615 .readable_register = cs4270_reg_is_readable,
616 .reg_cache_size = CS4270_LASTREG + 1,
617 .reg_word_size = sizeof(u8),
618 .reg_cache_default = cs4270_default_reg_cache,
f0fba2ad
LG
619};
620
85d07e4d
DM
621/*
622 * cs4270_of_match - the device tree bindings
623 */
624static const struct of_device_id cs4270_of_match[] = {
625 { .compatible = "cirrus,cs4270", },
626 { }
627};
628MODULE_DEVICE_TABLE(of, cs4270_of_match);
629
f0fba2ad
LG
630/**
631 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
632 * @i2c_client: the I2C client object
633 * @id: the I2C device ID (ignored)
634 *
635 * This function is called whenever the I2C subsystem finds a device that
636 * matches the device ID given via a prior call to i2c_add_driver().
637 */
638static int cs4270_i2c_probe(struct i2c_client *i2c_client,
639 const struct i2c_device_id *id)
640{
02286190 641 struct device_node *np = i2c_client->dev.of_node;
f0fba2ad 642 struct cs4270_private *cs4270;
b61d6d40
MB
643 int ret, i;
644
645 cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
646 GFP_KERNEL);
647 if (!cs4270) {
648 dev_err(&i2c_client->dev, "could not allocate codec\n");
649 return -ENOMEM;
650 }
651
652 /* get the power supply regulators */
653 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
654 cs4270->supplies[i].supply = supply_names[i];
655
656 ret = devm_regulator_bulk_get(&i2c_client->dev,
657 ARRAY_SIZE(cs4270->supplies),
658 cs4270->supplies);
659 if (ret < 0)
660 return ret;
f0fba2ad 661
02286190
DM
662 /* See if we have a way to bring the codec out of reset */
663 if (np) {
664 enum of_gpio_flags flags;
665 int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
666
667 if (gpio_is_valid(gpio)) {
668 ret = devm_gpio_request_one(&i2c_client->dev, gpio,
669 flags & OF_GPIO_ACTIVE_LOW ?
670 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
671 "cs4270 reset");
672 if (ret < 0)
673 return ret;
674 }
675 }
676
f0fba2ad
LG
677 /* Verify that we have a CS4270 */
678
679 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
680 if (ret < 0) {
681 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
682 i2c_client->addr);
683 return ret;
684 }
685 /* The top four bits of the chip ID should be 1100. */
686 if ((ret & 0xF0) != 0xC0) {
687 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
688 i2c_client->addr);
689 return -ENODEV;
690 }
691
692 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
693 i2c_client->addr);
694 dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
695
f0fba2ad 696 i2c_set_clientdata(i2c_client, cs4270);
f0fba2ad
LG
697 cs4270->control_type = SND_SOC_I2C;
698
699 ret = snd_soc_register_codec(&i2c_client->dev,
700 &soc_codec_device_cs4270, &cs4270_dai, 1);
f0fba2ad
LG
701 return ret;
702}
703
704/**
705 * cs4270_i2c_remove - remove an I2C device
706 * @i2c_client: the I2C client object
707 *
708 * This function is the counterpart to cs4270_i2c_probe().
709 */
710static int cs4270_i2c_remove(struct i2c_client *i2c_client)
711{
712 snd_soc_unregister_codec(&i2c_client->dev);
f0fba2ad
LG
713 return 0;
714}
715
716/*
717 * cs4270_id - I2C device IDs supported by this driver
718 */
79a54ea1 719static const struct i2c_device_id cs4270_id[] = {
f0fba2ad
LG
720 {"cs4270", 0},
721 {}
722};
723MODULE_DEVICE_TABLE(i2c, cs4270_id);
724
ff7bf02f
TT
725/*
726 * cs4270_i2c_driver - I2C device identification
727 *
728 * This structure tells the I2C subsystem how to identify and support a
729 * given I2C device type.
730 */
ff637d38
TT
731static struct i2c_driver cs4270_i2c_driver = {
732 .driver = {
64902b29 733 .name = "cs4270",
ff637d38 734 .owner = THIS_MODULE,
85d07e4d 735 .of_match_table = cs4270_of_match,
ff637d38
TT
736 },
737 .id_table = cs4270_id,
738 .probe = cs4270_i2c_probe,
0db4d070 739 .remove = cs4270_i2c_remove,
ff637d38 740};
b0c813ce 741
5e383f53 742module_i2c_driver(cs4270_i2c_driver);
64089b84 743
b0c813ce
TT
744MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
745MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
746MODULE_LICENSE("GPL");