Merge branch 'for-2.6.38' of git://git.kernel.org/pub/scm/linux/kernel/git/lrg/asoc...
[pandora-kernel.git] / sound / soc / codecs / cs4270.c
1 /*
2  * CS4270 ALSA SoC (ASoC) codec driver
3  *
4  * Author: Timur Tabi <timur@freescale.com>
5  *
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.
10  *
11  * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
12  *
13  * Current features/limitations:
14  *
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
21  * - Power management is supported
22  */
23
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <sound/core.h>
28 #include <sound/soc.h>
29 #include <sound/initval.h>
30 #include <linux/i2c.h>
31 #include <linux/delay.h>
32 #include <linux/regulator/consumer.h>
33
34 /*
35  * The codec isn't really big-endian or little-endian, since the I2S
36  * interface requires data to be sent serially with the MSbit first.
37  * However, to support BE and LE I2S devices, we specify both here.  That
38  * way, ALSA will always match the bit patterns.
39  */
40 #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | \
41                         SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
42                         SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
43                         SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
44                         SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
45                         SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
46
47 /* CS4270 registers addresses */
48 #define CS4270_CHIPID   0x01    /* Chip ID */
49 #define CS4270_PWRCTL   0x02    /* Power Control */
50 #define CS4270_MODE     0x03    /* Mode Control */
51 #define CS4270_FORMAT   0x04    /* Serial Format, ADC/DAC Control */
52 #define CS4270_TRANS    0x05    /* Transition Control */
53 #define CS4270_MUTE     0x06    /* Mute Control */
54 #define CS4270_VOLA     0x07    /* DAC Channel A Volume Control */
55 #define CS4270_VOLB     0x08    /* DAC Channel B Volume Control */
56
57 #define CS4270_FIRSTREG 0x01
58 #define CS4270_LASTREG  0x08
59 #define CS4270_NUMREGS  (CS4270_LASTREG - CS4270_FIRSTREG + 1)
60 #define CS4270_I2C_INCR 0x80
61
62 /* Bit masks for the CS4270 registers */
63 #define CS4270_CHIPID_ID        0xF0
64 #define CS4270_CHIPID_REV       0x0F
65 #define CS4270_PWRCTL_FREEZE    0x80
66 #define CS4270_PWRCTL_PDN_ADC   0x20
67 #define CS4270_PWRCTL_PDN_DAC   0x02
68 #define CS4270_PWRCTL_PDN       0x01
69 #define CS4270_PWRCTL_PDN_ALL   \
70         (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
71 #define CS4270_MODE_SPEED_MASK  0x30
72 #define CS4270_MODE_1X          0x00
73 #define CS4270_MODE_2X          0x10
74 #define CS4270_MODE_4X          0x20
75 #define CS4270_MODE_SLAVE       0x30
76 #define CS4270_MODE_DIV_MASK    0x0E
77 #define CS4270_MODE_DIV1        0x00
78 #define CS4270_MODE_DIV15       0x02
79 #define CS4270_MODE_DIV2        0x04
80 #define CS4270_MODE_DIV3        0x06
81 #define CS4270_MODE_DIV4        0x08
82 #define CS4270_MODE_POPGUARD    0x01
83 #define CS4270_FORMAT_FREEZE_A  0x80
84 #define CS4270_FORMAT_FREEZE_B  0x40
85 #define CS4270_FORMAT_LOOPBACK  0x20
86 #define CS4270_FORMAT_DAC_MASK  0x18
87 #define CS4270_FORMAT_DAC_LJ    0x00
88 #define CS4270_FORMAT_DAC_I2S   0x08
89 #define CS4270_FORMAT_DAC_RJ16  0x18
90 #define CS4270_FORMAT_DAC_RJ24  0x10
91 #define CS4270_FORMAT_ADC_MASK  0x01
92 #define CS4270_FORMAT_ADC_LJ    0x00
93 #define CS4270_FORMAT_ADC_I2S   0x01
94 #define CS4270_TRANS_ONE_VOL    0x80
95 #define CS4270_TRANS_SOFT       0x40
96 #define CS4270_TRANS_ZERO       0x20
97 #define CS4270_TRANS_INV_ADC_A  0x08
98 #define CS4270_TRANS_INV_ADC_B  0x10
99 #define CS4270_TRANS_INV_DAC_A  0x02
100 #define CS4270_TRANS_INV_DAC_B  0x04
101 #define CS4270_TRANS_DEEMPH     0x01
102 #define CS4270_MUTE_AUTO        0x20
103 #define CS4270_MUTE_ADC_A       0x08
104 #define CS4270_MUTE_ADC_B       0x10
105 #define CS4270_MUTE_POLARITY    0x04
106 #define CS4270_MUTE_DAC_A       0x01
107 #define CS4270_MUTE_DAC_B       0x02
108
109 static const char *supply_names[] = {
110         "va", "vd", "vlc"
111 };
112
113 /* Private data for the CS4270 */
114 struct cs4270_private {
115         enum snd_soc_control_type control_type;
116         void *control_data;
117         unsigned int mclk; /* Input frequency of the MCLK pin */
118         unsigned int mode; /* The mode (I2S or left-justified) */
119         unsigned int slave_mode;
120         unsigned int manual_mute;
121
122         /* power domain regulators */
123         struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
124 };
125
126 /**
127  * struct cs4270_mode_ratios - clock ratio tables
128  * @ratio: the ratio of MCLK to the sample rate
129  * @speed_mode: the Speed Mode bits to set in the Mode Control register for
130  *              this ratio
131  * @mclk: the Ratio Select bits to set in the Mode Control register for this
132  *        ratio
133  *
134  * The data for this chart is taken from Table 5 of the CS4270 reference
135  * manual.
136  *
137  * This table is used to determine how to program the Mode Control register.
138  * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
139  * rates the CS4270 currently supports.
140  *
141  * @speed_mode is the corresponding bit pattern to be written to the
142  * MODE bits of the Mode Control Register
143  *
144  * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
145  * the Mode Control Register.
146  *
147  * In situations where a single ratio is represented by multiple speed
148  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
149  * double-speed instead of quad-speed.  However, the CS4270 errata states
150  * that divide-By-1.5 can cause failures, so we avoid that mode where
151  * possible.
152  *
153  * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
154  * work if Vd is 3.3V.  If this effects you, select the
155  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
156  * never select any sample rates that require divide-by-1.5.
157  */
158 struct cs4270_mode_ratios {
159         unsigned int ratio;
160         u8 speed_mode;
161         u8 mclk;
162 };
163
164 static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
165         {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
166 #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
167         {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
168 #endif
169         {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
170         {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
171         {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
172         {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
173         {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
174         {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
175         {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
176 };
177
178 /* The number of MCLK/LRCK ratios supported by the CS4270 */
179 #define NUM_MCLK_RATIOS         ARRAY_SIZE(cs4270_mode_ratios)
180
181 /**
182  * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
183  * @codec_dai: the codec DAI
184  * @clk_id: the clock ID (ignored)
185  * @freq: the MCLK input frequency
186  * @dir: the clock direction (ignored)
187  *
188  * This function is used to tell the codec driver what the input MCLK
189  * frequency is.
190  *
191  * The value of MCLK is used to determine which sample rates are supported
192  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
193  * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
194  *
195  * This function calculates the nine ratios and determines which ones match
196  * a standard sample rate.  If there's a match, then it is added to the list
197  * of supported sample rates.
198  *
199  * This function must be called by the machine driver's 'startup' function,
200  * otherwise the list of supported sample rates will not be available in
201  * time for ALSA.
202  *
203  * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
204  * theoretically possible sample rates to be enabled. Call it again with a
205  * proper value set one the external clock is set (most probably you would do
206  * that from a machine's driver 'hw_param' hook.
207  */
208 static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
209                                  int clk_id, unsigned int freq, int dir)
210 {
211         struct snd_soc_codec *codec = codec_dai->codec;
212         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
213
214         cs4270->mclk = freq;
215         return 0;
216 }
217
218 /**
219  * cs4270_set_dai_fmt - configure the codec for the selected audio format
220  * @codec_dai: the codec DAI
221  * @format: a SND_SOC_DAIFMT_x value indicating the data format
222  *
223  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
224  * codec accordingly.
225  *
226  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
227  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
228  * data for playback only, but ASoC currently does not support different
229  * formats for playback vs. record.
230  */
231 static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
232                               unsigned int format)
233 {
234         struct snd_soc_codec *codec = codec_dai->codec;
235         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
236         int ret = 0;
237
238         /* set DAI format */
239         switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
240         case SND_SOC_DAIFMT_I2S:
241         case SND_SOC_DAIFMT_LEFT_J:
242                 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
243                 break;
244         default:
245                 dev_err(codec->dev, "invalid dai format\n");
246                 ret = -EINVAL;
247         }
248
249         /* set master/slave audio interface */
250         switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
251         case SND_SOC_DAIFMT_CBS_CFS:
252                 cs4270->slave_mode = 1;
253                 break;
254         case SND_SOC_DAIFMT_CBM_CFM:
255                 cs4270->slave_mode = 0;
256                 break;
257         default:
258                 /* all other modes are unsupported by the hardware */
259                 ret = -EINVAL;
260         }
261
262         return ret;
263 }
264
265 /**
266  * cs4270_fill_cache - pre-fill the CS4270 register cache.
267  * @codec: the codec for this CS4270
268  *
269  * This function fills in the CS4270 register cache by reading the register
270  * values from the hardware.
271  *
272  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
273  * After the initial read to pre-fill the cache, the CS4270 never updates
274  * the register values, so we won't have a cache coherency problem.
275  *
276  * We use the auto-increment feature of the CS4270 to read all registers in
277  * one shot.
278  */
279 static int cs4270_fill_cache(struct snd_soc_codec *codec)
280 {
281         u8 *cache = codec->reg_cache;
282         struct i2c_client *i2c_client = codec->control_data;
283         s32 length;
284
285         length = i2c_smbus_read_i2c_block_data(i2c_client,
286                 CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache);
287
288         if (length != CS4270_NUMREGS) {
289                 dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
290                        i2c_client->addr);
291                 return -EIO;
292         }
293
294         return 0;
295 }
296
297 /**
298  * cs4270_read_reg_cache - read from the CS4270 register cache.
299  * @codec: the codec for this CS4270
300  * @reg: the register to read
301  *
302  * This function returns the value for a given register.  It reads only from
303  * the register cache, not the hardware itself.
304  *
305  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
306  * After the initial read to pre-fill the cache, the CS4270 never updates
307  * the register values, so we won't have a cache coherency problem.
308  */
309 static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
310         unsigned int reg)
311 {
312         u8 *cache = codec->reg_cache;
313
314         if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
315                 return -EIO;
316
317         return cache[reg - CS4270_FIRSTREG];
318 }
319
320 /**
321  * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
322  * @codec: the codec for this CS4270
323  * @reg: the register to write
324  * @value: the value to write to the register
325  *
326  * This function writes the given value to the given CS4270 register, and
327  * also updates the register cache.
328  *
329  * Note that we don't use the hw_write function pointer of snd_soc_codec.
330  * That's because it's too clunky: the hw_write_t prototype does not match
331  * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
332  */
333 static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
334                             unsigned int value)
335 {
336         u8 *cache = codec->reg_cache;
337
338         if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
339                 return -EIO;
340
341         /* Only perform an I2C operation if the new value is different */
342         if (cache[reg - CS4270_FIRSTREG] != value) {
343                 struct i2c_client *client = codec->control_data;
344                 if (i2c_smbus_write_byte_data(client, reg, value)) {
345                         dev_err(codec->dev, "i2c write failed\n");
346                         return -EIO;
347                 }
348
349                 /* We've written to the hardware, so update the cache */
350                 cache[reg - CS4270_FIRSTREG] = value;
351         }
352
353         return 0;
354 }
355
356 /**
357  * cs4270_hw_params - program the CS4270 with the given hardware parameters.
358  * @substream: the audio stream
359  * @params: the hardware parameters to set
360  * @dai: the SOC DAI (ignored)
361  *
362  * This function programs the hardware with the values provided.
363  * Specifically, the sample rate and the data format.
364  *
365  * The .ops functions are used to provide board-specific data, like input
366  * frequencies, to this driver.  This function takes that information,
367  * combines it with the hardware parameters provided, and programs the
368  * hardware accordingly.
369  */
370 static int cs4270_hw_params(struct snd_pcm_substream *substream,
371                             struct snd_pcm_hw_params *params,
372                             struct snd_soc_dai *dai)
373 {
374         struct snd_soc_pcm_runtime *rtd = substream->private_data;
375         struct snd_soc_codec *codec = rtd->codec;
376         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
377         int ret;
378         unsigned int i;
379         unsigned int rate;
380         unsigned int ratio;
381         int reg;
382
383         /* Figure out which MCLK/LRCK ratio to use */
384
385         rate = params_rate(params);     /* Sampling rate, in Hz */
386         ratio = cs4270->mclk / rate;    /* MCLK/LRCK ratio */
387
388         for (i = 0; i < NUM_MCLK_RATIOS; i++) {
389                 if (cs4270_mode_ratios[i].ratio == ratio)
390                         break;
391         }
392
393         if (i == NUM_MCLK_RATIOS) {
394                 /* We did not find a matching ratio */
395                 dev_err(codec->dev, "could not find matching ratio\n");
396                 return -EINVAL;
397         }
398
399         /* Set the sample rate */
400
401         reg = snd_soc_read(codec, CS4270_MODE);
402         reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
403         reg |= cs4270_mode_ratios[i].mclk;
404
405         if (cs4270->slave_mode)
406                 reg |= CS4270_MODE_SLAVE;
407         else
408                 reg |= cs4270_mode_ratios[i].speed_mode;
409
410         ret = snd_soc_write(codec, CS4270_MODE, reg);
411         if (ret < 0) {
412                 dev_err(codec->dev, "i2c write failed\n");
413                 return ret;
414         }
415
416         /* Set the DAI format */
417
418         reg = snd_soc_read(codec, CS4270_FORMAT);
419         reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
420
421         switch (cs4270->mode) {
422         case SND_SOC_DAIFMT_I2S:
423                 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
424                 break;
425         case SND_SOC_DAIFMT_LEFT_J:
426                 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
427                 break;
428         default:
429                 dev_err(codec->dev, "unknown dai format\n");
430                 return -EINVAL;
431         }
432
433         ret = snd_soc_write(codec, CS4270_FORMAT, reg);
434         if (ret < 0) {
435                 dev_err(codec->dev, "i2c write failed\n");
436                 return ret;
437         }
438
439         return ret;
440 }
441
442 /**
443  * cs4270_dai_mute - enable/disable the CS4270 external mute
444  * @dai: the SOC DAI
445  * @mute: 0 = disable mute, 1 = enable mute
446  *
447  * This function toggles the mute bits in the MUTE register.  The CS4270's
448  * mute capability is intended for external muting circuitry, so if the
449  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
450  * then this function will do nothing.
451  */
452 static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
453 {
454         struct snd_soc_codec *codec = dai->codec;
455         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
456         int reg6;
457
458         reg6 = snd_soc_read(codec, CS4270_MUTE);
459
460         if (mute)
461                 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
462         else {
463                 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
464                 reg6 |= cs4270->manual_mute;
465         }
466
467         return snd_soc_write(codec, CS4270_MUTE, reg6);
468 }
469
470 /**
471  * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
472  *                       alsa control.
473  * @kcontrol: mixer control
474  * @ucontrol: control element information
475  *
476  * This function basically passes the arguments on to the generic
477  * snd_soc_put_volsw() function and saves the mute information in
478  * our private data structure. This is because we want to prevent
479  * cs4270_dai_mute() neglecting the user's decision to manually
480  * mute the codec's output.
481  *
482  * Returns 0 for success.
483  */
484 static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
485                                 struct snd_ctl_elem_value *ucontrol)
486 {
487         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
488         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
489         int left = !ucontrol->value.integer.value[0];
490         int right = !ucontrol->value.integer.value[1];
491
492         cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
493                               (right ? CS4270_MUTE_DAC_B : 0);
494
495         return snd_soc_put_volsw(kcontrol, ucontrol);
496 }
497
498 /* A list of non-DAPM controls that the CS4270 supports */
499 static const struct snd_kcontrol_new cs4270_snd_controls[] = {
500         SOC_DOUBLE_R("Master Playback Volume",
501                 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
502         SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
503         SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
504         SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
505         SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
506         SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
507         SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
508         SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
509         SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
510                 snd_soc_get_volsw, cs4270_soc_put_mute),
511 };
512
513 static struct snd_soc_dai_ops cs4270_dai_ops = {
514         .hw_params      = cs4270_hw_params,
515         .set_sysclk     = cs4270_set_dai_sysclk,
516         .set_fmt        = cs4270_set_dai_fmt,
517         .digital_mute   = cs4270_dai_mute,
518 };
519
520 static struct snd_soc_dai_driver cs4270_dai = {
521         .name = "cs4270-hifi",
522         .playback = {
523                 .stream_name = "Playback",
524                 .channels_min = 1,
525                 .channels_max = 2,
526                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
527                 .rate_min = 4000,
528                 .rate_max = 216000,
529                 .formats = CS4270_FORMATS,
530         },
531         .capture = {
532                 .stream_name = "Capture",
533                 .channels_min = 1,
534                 .channels_max = 2,
535                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
536                 .rate_min = 4000,
537                 .rate_max = 216000,
538                 .formats = CS4270_FORMATS,
539         },
540         .ops = &cs4270_dai_ops,
541 };
542
543 /**
544  * cs4270_probe - ASoC probe function
545  * @pdev: platform device
546  *
547  * This function is called when ASoC has all the pieces it needs to
548  * instantiate a sound driver.
549  */
550 static int cs4270_probe(struct snd_soc_codec *codec)
551 {
552         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
553         int i, ret, reg;
554
555         codec->control_data = cs4270->control_data;
556
557         /* The I2C interface is set up, so pre-fill our register cache */
558
559         ret = cs4270_fill_cache(codec);
560         if (ret < 0) {
561                 dev_err(codec->dev, "failed to fill register cache\n");
562                 return ret;
563         }
564
565         /* Disable auto-mute.  This feature appears to be buggy.  In some
566          * situations, auto-mute will not deactivate when it should, so we want
567          * this feature disabled by default.  An application (e.g. alsactl) can
568          * re-enabled it by using the controls.
569          */
570
571         reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
572         reg &= ~CS4270_MUTE_AUTO;
573         ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
574         if (ret < 0) {
575                 dev_err(codec->dev, "i2c write failed\n");
576                 return ret;
577         }
578
579         /* Disable automatic volume control.  The hardware enables, and it
580          * causes volume change commands to be delayed, sometimes until after
581          * playback has started.  An application (e.g. alsactl) can
582          * re-enabled it by using the controls.
583          */
584
585         reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
586         reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
587         ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
588         if (ret < 0) {
589                 dev_err(codec->dev, "i2c write failed\n");
590                 return ret;
591         }
592
593         /* Add the non-DAPM controls */
594         ret = snd_soc_add_controls(codec, cs4270_snd_controls,
595                                 ARRAY_SIZE(cs4270_snd_controls));
596         if (ret < 0) {
597                 dev_err(codec->dev, "failed to add controls\n");
598                 return ret;
599         }
600
601         /* get the power supply regulators */
602         for (i = 0; i < ARRAY_SIZE(supply_names); i++)
603                 cs4270->supplies[i].supply = supply_names[i];
604
605         ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
606                                  cs4270->supplies);
607         if (ret < 0)
608                 return ret;
609
610         ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
611                                     cs4270->supplies);
612         if (ret < 0)
613                 goto error_free_regulators;
614
615         return 0;
616
617 error_free_regulators:
618         regulator_bulk_free(ARRAY_SIZE(cs4270->supplies),
619                             cs4270->supplies);
620
621         return ret;
622 }
623
624 /**
625  * cs4270_remove - ASoC remove function
626  * @pdev: platform device
627  *
628  * This function is the counterpart to cs4270_probe().
629  */
630 static int cs4270_remove(struct snd_soc_codec *codec)
631 {
632         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
633
634         regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
635         regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
636
637         return 0;
638 };
639
640 #ifdef CONFIG_PM
641
642 /* This suspend/resume implementation can handle both - a simple standby
643  * where the codec remains powered, and a full suspend, where the voltage
644  * domain the codec is connected to is teared down and/or any other hardware
645  * reset condition is asserted.
646  *
647  * The codec's own power saving features are enabled in the suspend callback,
648  * and all registers are written back to the hardware when resuming.
649  */
650
651 static int cs4270_soc_suspend(struct snd_soc_codec *codec, pm_message_t mesg)
652 {
653         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
654         int reg, ret;
655
656         reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
657         if (reg < 0)
658                 return reg;
659
660         ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
661         if (ret < 0)
662                 return ret;
663
664         regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
665                                cs4270->supplies);
666
667         return 0;
668 }
669
670 static int cs4270_soc_resume(struct snd_soc_codec *codec)
671 {
672         struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
673         struct i2c_client *i2c_client = codec->control_data;
674         int reg;
675
676         regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
677                               cs4270->supplies);
678
679         /* In case the device was put to hard reset during sleep, we need to
680          * wait 500ns here before any I2C communication. */
681         ndelay(500);
682
683         /* first restore the entire register cache ... */
684         for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
685                 u8 val = snd_soc_read(codec, reg);
686
687                 if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
688                         dev_err(codec->dev, "i2c write failed\n");
689                         return -EIO;
690                 }
691         }
692
693         /* ... then disable the power-down bits */
694         reg = snd_soc_read(codec, CS4270_PWRCTL);
695         reg &= ~CS4270_PWRCTL_PDN_ALL;
696
697         return snd_soc_write(codec, CS4270_PWRCTL, reg);
698 }
699 #else
700 #define cs4270_soc_suspend      NULL
701 #define cs4270_soc_resume       NULL
702 #endif /* CONFIG_PM */
703
704 /*
705  * ASoC codec device structure
706  *
707  * Assign this variable to the codec_dev field of the machine driver's
708  * snd_soc_device structure.
709  */
710 static struct snd_soc_codec_driver soc_codec_device_cs4270 = {
711         .probe =        cs4270_probe,
712         .remove =       cs4270_remove,
713         .suspend =      cs4270_soc_suspend,
714         .resume =       cs4270_soc_resume,
715         .read = cs4270_read_reg_cache,
716         .write = cs4270_i2c_write,
717         .reg_cache_size = CS4270_NUMREGS,
718         .reg_word_size = sizeof(u8),
719 };
720
721 /**
722  * cs4270_i2c_probe - initialize the I2C interface of the CS4270
723  * @i2c_client: the I2C client object
724  * @id: the I2C device ID (ignored)
725  *
726  * This function is called whenever the I2C subsystem finds a device that
727  * matches the device ID given via a prior call to i2c_add_driver().
728  */
729 static int cs4270_i2c_probe(struct i2c_client *i2c_client,
730         const struct i2c_device_id *id)
731 {
732         struct cs4270_private *cs4270;
733         int ret;
734
735         /* Verify that we have a CS4270 */
736
737         ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
738         if (ret < 0) {
739                 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
740                        i2c_client->addr);
741                 return ret;
742         }
743         /* The top four bits of the chip ID should be 1100. */
744         if ((ret & 0xF0) != 0xC0) {
745                 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
746                        i2c_client->addr);
747                 return -ENODEV;
748         }
749
750         dev_info(&i2c_client->dev, "found device at i2c address %X\n",
751                 i2c_client->addr);
752         dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
753
754         cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
755         if (!cs4270) {
756                 dev_err(&i2c_client->dev, "could not allocate codec\n");
757                 return -ENOMEM;
758         }
759
760         i2c_set_clientdata(i2c_client, cs4270);
761         cs4270->control_data = i2c_client;
762         cs4270->control_type = SND_SOC_I2C;
763
764         ret = snd_soc_register_codec(&i2c_client->dev,
765                         &soc_codec_device_cs4270, &cs4270_dai, 1);
766         if (ret < 0)
767                 kfree(cs4270);
768         return ret;
769 }
770
771 /**
772  * cs4270_i2c_remove - remove an I2C device
773  * @i2c_client: the I2C client object
774  *
775  * This function is the counterpart to cs4270_i2c_probe().
776  */
777 static int cs4270_i2c_remove(struct i2c_client *i2c_client)
778 {
779         snd_soc_unregister_codec(&i2c_client->dev);
780         kfree(i2c_get_clientdata(i2c_client));
781         return 0;
782 }
783
784 /*
785  * cs4270_id - I2C device IDs supported by this driver
786  */
787 static struct i2c_device_id cs4270_id[] = {
788         {"cs4270", 0},
789         {}
790 };
791 MODULE_DEVICE_TABLE(i2c, cs4270_id);
792
793 /*
794  * cs4270_i2c_driver - I2C device identification
795  *
796  * This structure tells the I2C subsystem how to identify and support a
797  * given I2C device type.
798  */
799 static struct i2c_driver cs4270_i2c_driver = {
800         .driver = {
801                 .name = "cs4270-codec",
802                 .owner = THIS_MODULE,
803         },
804         .id_table = cs4270_id,
805         .probe = cs4270_i2c_probe,
806         .remove = cs4270_i2c_remove,
807 };
808
809 static int __init cs4270_init(void)
810 {
811         pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
812
813         return i2c_add_driver(&cs4270_i2c_driver);
814 }
815 module_init(cs4270_init);
816
817 static void __exit cs4270_exit(void)
818 {
819         i2c_del_driver(&cs4270_i2c_driver);
820 }
821 module_exit(cs4270_exit);
822
823 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
824 MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
825 MODULE_LICENSE("GPL");