ASoC: cs4270: allow passing freq=0 in set_dai_sysclk()
[safe/jmp/linux-2.6] / 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 <sound/core.h>
27 #include <sound/soc.h>
28 #include <sound/initval.h>
29 #include <linux/i2c.h>
30 #include <linux/delay.h>
31 #include <linux/regulator/consumer.h>
32
33 #include "cs4270.h"
34
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
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)
61 #define CS4270_I2C_INCR 0x80
62
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
70 #define CS4270_PWRCTL_PDN_ALL   \
71         (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
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
110 static const char *supply_names[] = {
111         "va", "vd", "vlc"
112 };
113
114 /* Private data for the CS4270 */
115 struct cs4270_private {
116         struct snd_soc_codec codec;
117         u8 reg_cache[CS4270_NUMREGS];
118         unsigned int mclk; /* Input frequency of the MCLK pin */
119         unsigned int mode; /* The mode (I2S or left-justified) */
120         unsigned int slave_mode;
121         unsigned int manual_mute;
122
123         /* power domain regulators */
124         struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
125 };
126
127 /**
128  * struct cs4270_mode_ratios - clock ratio tables
129  * @ratio: the ratio of MCLK to the sample rate
130  * @speed_mode: the Speed Mode bits to set in the Mode Control register for
131  *              this ratio
132  * @mclk: the Ratio Select bits to set in the Mode Control register for this
133  *        ratio
134  *
135  * The data for this chart is taken from Table 5 of the CS4270 reference
136  * manual.
137  *
138  * This table is used to determine how to program the Mode Control register.
139  * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
140  * rates the CS4270 currently supports.
141  *
142  * @speed_mode is the corresponding bit pattern to be written to the
143  * MODE bits of the Mode Control Register
144  *
145  * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
146  * the Mode Control Register.
147  *
148  * In situations where a single ratio is represented by multiple speed
149  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
150  * double-speed instead of quad-speed.  However, the CS4270 errata states
151  * that divide-By-1.5 can cause failures, so we avoid that mode where
152  * possible.
153  *
154  * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
155  * work if Vd is 3.3V.  If this effects you, select the
156  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
157  * never select any sample rates that require divide-by-1.5.
158  */
159 struct cs4270_mode_ratios {
160         unsigned int ratio;
161         u8 speed_mode;
162         u8 mclk;
163 };
164
165 static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
166         {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
167 #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
168         {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
169 #endif
170         {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
171         {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
172         {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
173         {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
174         {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
175         {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
176         {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
177 };
178
179 /* The number of MCLK/LRCK ratios supported by the CS4270 */
180 #define NUM_MCLK_RATIOS         ARRAY_SIZE(cs4270_mode_ratios)
181
182 /**
183  * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
184  * @codec_dai: the codec DAI
185  * @clk_id: the clock ID (ignored)
186  * @freq: the MCLK input frequency
187  * @dir: the clock direction (ignored)
188  *
189  * This function is used to tell the codec driver what the input MCLK
190  * frequency is.
191  *
192  * The value of MCLK is used to determine which sample rates are supported
193  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
194  * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
195  *
196  * This function calculates the nine ratios and determines which ones match
197  * a standard sample rate.  If there's a match, then it is added to the list
198  * of supported sample rates.
199  *
200  * This function must be called by the machine driver's 'startup' function,
201  * otherwise the list of supported sample rates will not be available in
202  * time for ALSA.
203  *
204  * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
205  * theoretically possible sample rates to be enabled. Call it again with a
206  * proper value set one the external clock is set (most probably you would do
207  * that from a machine's driver 'hw_param' hook.
208  */
209 static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
210                                  int clk_id, unsigned int freq, int dir)
211 {
212         struct snd_soc_codec *codec = codec_dai->codec;
213         struct cs4270_private *cs4270 = codec->private_data;
214         unsigned int rates = 0;
215         unsigned int rate_min = -1;
216         unsigned int rate_max = 0;
217         unsigned int i;
218
219         cs4270->mclk = freq;
220
221         if (cs4270->mclk) {
222                 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
223                         unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
224                         rates |= snd_pcm_rate_to_rate_bit(rate);
225                         if (rate < rate_min)
226                                 rate_min = rate;
227                         if (rate > rate_max)
228                                 rate_max = rate;
229                 }
230                 /* FIXME: soc should support a rate list */
231                 rates &= ~SNDRV_PCM_RATE_KNOT;
232
233                 if (!rates) {
234                         dev_err(codec->dev, "could not find a valid sample rate\n");
235                         return -EINVAL;
236                 }
237         } else {
238                 /* enable all possible rates */
239                 rates = SNDRV_PCM_RATE_8000_192000;
240                 rate_min = 8000;
241                 rate_max = 192000;
242         }
243
244         codec_dai->playback.rates = rates;
245         codec_dai->playback.rate_min = rate_min;
246         codec_dai->playback.rate_max = rate_max;
247
248         codec_dai->capture.rates = rates;
249         codec_dai->capture.rate_min = rate_min;
250         codec_dai->capture.rate_max = rate_max;
251
252         return 0;
253 }
254
255 /**
256  * cs4270_set_dai_fmt - configure the codec for the selected audio format
257  * @codec_dai: the codec DAI
258  * @format: a SND_SOC_DAIFMT_x value indicating the data format
259  *
260  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
261  * codec accordingly.
262  *
263  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
264  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
265  * data for playback only, but ASoC currently does not support different
266  * formats for playback vs. record.
267  */
268 static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
269                               unsigned int format)
270 {
271         struct snd_soc_codec *codec = codec_dai->codec;
272         struct cs4270_private *cs4270 = codec->private_data;
273         int ret = 0;
274
275         /* set DAI format */
276         switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
277         case SND_SOC_DAIFMT_I2S:
278         case SND_SOC_DAIFMT_LEFT_J:
279                 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
280                 break;
281         default:
282                 dev_err(codec->dev, "invalid dai format\n");
283                 ret = -EINVAL;
284         }
285
286         /* set master/slave audio interface */
287         switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
288         case SND_SOC_DAIFMT_CBS_CFS:
289                 cs4270->slave_mode = 1;
290                 break;
291         case SND_SOC_DAIFMT_CBM_CFM:
292                 cs4270->slave_mode = 0;
293                 break;
294         default:
295                 /* all other modes are unsupported by the hardware */
296                 ret = -EINVAL;
297         }
298
299         return ret;
300 }
301
302 /**
303  * cs4270_fill_cache - pre-fill the CS4270 register cache.
304  * @codec: the codec for this CS4270
305  *
306  * This function fills in the CS4270 register cache by reading the register
307  * values from the hardware.
308  *
309  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
310  * After the initial read to pre-fill the cache, the CS4270 never updates
311  * the register values, so we won't have a cache coherency problem.
312  *
313  * We use the auto-increment feature of the CS4270 to read all registers in
314  * one shot.
315  */
316 static int cs4270_fill_cache(struct snd_soc_codec *codec)
317 {
318         u8 *cache = codec->reg_cache;
319         struct i2c_client *i2c_client = codec->control_data;
320         s32 length;
321
322         length = i2c_smbus_read_i2c_block_data(i2c_client,
323                 CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache);
324
325         if (length != CS4270_NUMREGS) {
326                 dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
327                        i2c_client->addr);
328                 return -EIO;
329         }
330
331         return 0;
332 }
333
334 /**
335  * cs4270_read_reg_cache - read from the CS4270 register cache.
336  * @codec: the codec for this CS4270
337  * @reg: the register to read
338  *
339  * This function returns the value for a given register.  It reads only from
340  * the register cache, not the hardware itself.
341  *
342  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
343  * After the initial read to pre-fill the cache, the CS4270 never updates
344  * the register values, so we won't have a cache coherency problem.
345  */
346 static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
347         unsigned int reg)
348 {
349         u8 *cache = codec->reg_cache;
350
351         if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
352                 return -EIO;
353
354         return cache[reg - CS4270_FIRSTREG];
355 }
356
357 /**
358  * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
359  * @codec: the codec for this CS4270
360  * @reg: the register to write
361  * @value: the value to write to the register
362  *
363  * This function writes the given value to the given CS4270 register, and
364  * also updates the register cache.
365  *
366  * Note that we don't use the hw_write function pointer of snd_soc_codec.
367  * That's because it's too clunky: the hw_write_t prototype does not match
368  * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
369  */
370 static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
371                             unsigned int value)
372 {
373         u8 *cache = codec->reg_cache;
374
375         if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
376                 return -EIO;
377
378         /* Only perform an I2C operation if the new value is different */
379         if (cache[reg - CS4270_FIRSTREG] != value) {
380                 struct i2c_client *client = codec->control_data;
381                 if (i2c_smbus_write_byte_data(client, reg, value)) {
382                         dev_err(codec->dev, "i2c write failed\n");
383                         return -EIO;
384                 }
385
386                 /* We've written to the hardware, so update the cache */
387                 cache[reg - CS4270_FIRSTREG] = value;
388         }
389
390         return 0;
391 }
392
393 /**
394  * cs4270_hw_params - program the CS4270 with the given hardware parameters.
395  * @substream: the audio stream
396  * @params: the hardware parameters to set
397  * @dai: the SOC DAI (ignored)
398  *
399  * This function programs the hardware with the values provided.
400  * Specifically, the sample rate and the data format.
401  *
402  * The .ops functions are used to provide board-specific data, like input
403  * frequencies, to this driver.  This function takes that information,
404  * combines it with the hardware parameters provided, and programs the
405  * hardware accordingly.
406  */
407 static int cs4270_hw_params(struct snd_pcm_substream *substream,
408                             struct snd_pcm_hw_params *params,
409                             struct snd_soc_dai *dai)
410 {
411         struct snd_soc_pcm_runtime *rtd = substream->private_data;
412         struct snd_soc_device *socdev = rtd->socdev;
413         struct snd_soc_codec *codec = socdev->card->codec;
414         struct cs4270_private *cs4270 = codec->private_data;
415         int ret;
416         unsigned int i;
417         unsigned int rate;
418         unsigned int ratio;
419         int reg;
420
421         /* Figure out which MCLK/LRCK ratio to use */
422
423         rate = params_rate(params);     /* Sampling rate, in Hz */
424         ratio = cs4270->mclk / rate;    /* MCLK/LRCK ratio */
425
426         for (i = 0; i < NUM_MCLK_RATIOS; i++) {
427                 if (cs4270_mode_ratios[i].ratio == ratio)
428                         break;
429         }
430
431         if (i == NUM_MCLK_RATIOS) {
432                 /* We did not find a matching ratio */
433                 dev_err(codec->dev, "could not find matching ratio\n");
434                 return -EINVAL;
435         }
436
437         /* Set the sample rate */
438
439         reg = snd_soc_read(codec, CS4270_MODE);
440         reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
441         reg |= cs4270_mode_ratios[i].mclk;
442
443         if (cs4270->slave_mode)
444                 reg |= CS4270_MODE_SLAVE;
445         else
446                 reg |= cs4270_mode_ratios[i].speed_mode;
447
448         ret = snd_soc_write(codec, CS4270_MODE, reg);
449         if (ret < 0) {
450                 dev_err(codec->dev, "i2c write failed\n");
451                 return ret;
452         }
453
454         /* Set the DAI format */
455
456         reg = snd_soc_read(codec, CS4270_FORMAT);
457         reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
458
459         switch (cs4270->mode) {
460         case SND_SOC_DAIFMT_I2S:
461                 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
462                 break;
463         case SND_SOC_DAIFMT_LEFT_J:
464                 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
465                 break;
466         default:
467                 dev_err(codec->dev, "unknown dai format\n");
468                 return -EINVAL;
469         }
470
471         ret = snd_soc_write(codec, CS4270_FORMAT, reg);
472         if (ret < 0) {
473                 dev_err(codec->dev, "i2c write failed\n");
474                 return ret;
475         }
476
477         return ret;
478 }
479
480 /**
481  * cs4270_dai_mute - enable/disable the CS4270 external mute
482  * @dai: the SOC DAI
483  * @mute: 0 = disable mute, 1 = enable mute
484  *
485  * This function toggles the mute bits in the MUTE register.  The CS4270's
486  * mute capability is intended for external muting circuitry, so if the
487  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
488  * then this function will do nothing.
489  */
490 static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
491 {
492         struct snd_soc_codec *codec = dai->codec;
493         struct cs4270_private *cs4270 = codec->private_data;
494         int reg6;
495
496         reg6 = snd_soc_read(codec, CS4270_MUTE);
497
498         if (mute)
499                 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
500         else {
501                 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
502                 reg6 |= cs4270->manual_mute;
503         }
504
505         return snd_soc_write(codec, CS4270_MUTE, reg6);
506 }
507
508 /**
509  * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
510  *                       alsa control.
511  * @kcontrol: mixer control
512  * @ucontrol: control element information
513  *
514  * This function basically passes the arguments on to the generic
515  * snd_soc_put_volsw() function and saves the mute information in
516  * our private data structure. This is because we want to prevent
517  * cs4270_dai_mute() neglecting the user's decision to manually
518  * mute the codec's output.
519  *
520  * Returns 0 for success.
521  */
522 static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
523                                 struct snd_ctl_elem_value *ucontrol)
524 {
525         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
526         struct cs4270_private *cs4270 = codec->private_data;
527         int left = !ucontrol->value.integer.value[0];
528         int right = !ucontrol->value.integer.value[1];
529
530         cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
531                               (right ? CS4270_MUTE_DAC_B : 0);
532
533         return snd_soc_put_volsw(kcontrol, ucontrol);
534 }
535
536 /* A list of non-DAPM controls that the CS4270 supports */
537 static const struct snd_kcontrol_new cs4270_snd_controls[] = {
538         SOC_DOUBLE_R("Master Playback Volume",
539                 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
540         SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
541         SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
542         SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
543         SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
544         SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
545         SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
546         SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
547         SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
548                 snd_soc_get_volsw, cs4270_soc_put_mute),
549 };
550
551 /*
552  * cs4270_codec - global variable to store codec for the ASoC probe function
553  *
554  * If struct i2c_driver had a private_data field, we wouldn't need to use
555  * cs4270_codec.  This is the only way to pass the codec structure from
556  * cs4270_i2c_probe() to cs4270_probe().  Unfortunately, there is no good
557  * way to synchronize these two functions.  cs4270_i2c_probe() can be called
558  * multiple times before cs4270_probe() is called even once.  So for now, we
559  * also only allow cs4270_i2c_probe() to be run once.  That means that we do
560  * not support more than one cs4270 device in the system, at least for now.
561  */
562 static struct snd_soc_codec *cs4270_codec;
563
564 static struct snd_soc_dai_ops cs4270_dai_ops = {
565         .hw_params      = cs4270_hw_params,
566         .set_sysclk     = cs4270_set_dai_sysclk,
567         .set_fmt        = cs4270_set_dai_fmt,
568         .digital_mute   = cs4270_dai_mute,
569 };
570
571 struct snd_soc_dai cs4270_dai = {
572         .name = "cs4270",
573         .playback = {
574                 .stream_name = "Playback",
575                 .channels_min = 1,
576                 .channels_max = 2,
577                 .rates = 0,
578                 .formats = CS4270_FORMATS,
579         },
580         .capture = {
581                 .stream_name = "Capture",
582                 .channels_min = 1,
583                 .channels_max = 2,
584                 .rates = 0,
585                 .formats = CS4270_FORMATS,
586         },
587         .ops = &cs4270_dai_ops,
588 };
589 EXPORT_SYMBOL_GPL(cs4270_dai);
590
591 /**
592  * cs4270_probe - ASoC probe function
593  * @pdev: platform device
594  *
595  * This function is called when ASoC has all the pieces it needs to
596  * instantiate a sound driver.
597  */
598 static int cs4270_probe(struct platform_device *pdev)
599 {
600         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
601         struct snd_soc_codec *codec = cs4270_codec;
602         struct cs4270_private *cs4270 = codec->private_data;
603         int i, ret;
604
605         /* Connect the codec to the socdev.  snd_soc_new_pcms() needs this. */
606         socdev->card->codec = codec;
607
608         /* Register PCMs */
609         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
610         if (ret < 0) {
611                 dev_err(codec->dev, "failed to create pcms\n");
612                 return ret;
613         }
614
615         /* Add the non-DAPM controls */
616         ret = snd_soc_add_controls(codec, cs4270_snd_controls,
617                                 ARRAY_SIZE(cs4270_snd_controls));
618         if (ret < 0) {
619                 dev_err(codec->dev, "failed to add controls\n");
620                 goto error_free_pcms;
621         }
622
623         /* get the power supply regulators */
624         for (i = 0; i < ARRAY_SIZE(supply_names); i++)
625                 cs4270->supplies[i].supply = supply_names[i];
626
627         ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
628                                  cs4270->supplies);
629         if (ret < 0)
630                 goto error_free_pcms;
631
632         return 0;
633
634 error_free_pcms:
635         snd_soc_free_pcms(socdev);
636
637         return ret;
638 }
639
640 /**
641  * cs4270_remove - ASoC remove function
642  * @pdev: platform device
643  *
644  * This function is the counterpart to cs4270_probe().
645  */
646 static int cs4270_remove(struct platform_device *pdev)
647 {
648         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
649         struct snd_soc_codec *codec = cs4270_codec;
650         struct cs4270_private *cs4270 = codec->private_data;
651
652         snd_soc_free_pcms(socdev);
653         regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
654
655         return 0;
656 };
657
658 /**
659  * cs4270_i2c_probe - initialize the I2C interface of the CS4270
660  * @i2c_client: the I2C client object
661  * @id: the I2C device ID (ignored)
662  *
663  * This function is called whenever the I2C subsystem finds a device that
664  * matches the device ID given via a prior call to i2c_add_driver().
665  */
666 static int cs4270_i2c_probe(struct i2c_client *i2c_client,
667         const struct i2c_device_id *id)
668 {
669         struct snd_soc_codec *codec;
670         struct cs4270_private *cs4270;
671         unsigned int reg;
672         int ret;
673
674         /* For now, we only support one cs4270 device in the system.  See the
675          * comment for cs4270_codec.
676          */
677         if (cs4270_codec) {
678                 dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
679                        i2c_client->addr);
680                 dev_err(&i2c_client->dev, "only one per board allowed\n");
681                 /* Should we return something other than ENODEV here? */
682                 return -ENODEV;
683         }
684
685         /* Verify that we have a CS4270 */
686
687         ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
688         if (ret < 0) {
689                 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
690                        i2c_client->addr);
691                 return ret;
692         }
693         /* The top four bits of the chip ID should be 1100. */
694         if ((ret & 0xF0) != 0xC0) {
695                 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
696                        i2c_client->addr);
697                 return -ENODEV;
698         }
699
700         dev_info(&i2c_client->dev, "found device at i2c address %X\n",
701                 i2c_client->addr);
702         dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
703
704         /* Allocate enough space for the snd_soc_codec structure
705            and our private data together. */
706         cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
707         if (!cs4270) {
708                 dev_err(&i2c_client->dev, "could not allocate codec\n");
709                 return -ENOMEM;
710         }
711         codec = &cs4270->codec;
712
713         mutex_init(&codec->mutex);
714         INIT_LIST_HEAD(&codec->dapm_widgets);
715         INIT_LIST_HEAD(&codec->dapm_paths);
716
717         codec->dev = &i2c_client->dev;
718         codec->name = "CS4270";
719         codec->owner = THIS_MODULE;
720         codec->dai = &cs4270_dai;
721         codec->num_dai = 1;
722         codec->private_data = cs4270;
723         codec->control_data = i2c_client;
724         codec->read = cs4270_read_reg_cache;
725         codec->write = cs4270_i2c_write;
726         codec->reg_cache = cs4270->reg_cache;
727         codec->reg_cache_size = CS4270_NUMREGS;
728
729         /* The I2C interface is set up, so pre-fill our register cache */
730
731         ret = cs4270_fill_cache(codec);
732         if (ret < 0) {
733                 dev_err(&i2c_client->dev, "failed to fill register cache\n");
734                 goto error_free_codec;
735         }
736
737         /* Disable auto-mute.  This feature appears to be buggy.  In some
738          * situations, auto-mute will not deactivate when it should, so we want
739          * this feature disabled by default.  An application (e.g. alsactl) can
740          * re-enabled it by using the controls.
741          */
742
743         reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
744         reg &= ~CS4270_MUTE_AUTO;
745         ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
746         if (ret < 0) {
747                 dev_err(&i2c_client->dev, "i2c write failed\n");
748                 return ret;
749         }
750
751         /* Disable automatic volume control.  The hardware enables, and it
752          * causes volume change commands to be delayed, sometimes until after
753          * playback has started.  An application (e.g. alsactl) can
754          * re-enabled it by using the controls.
755          */
756
757         reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
758         reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
759         ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
760         if (ret < 0) {
761                 dev_err(&i2c_client->dev, "i2c write failed\n");
762                 return ret;
763         }
764
765         /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
766          * structure for each CS4270 device, but the machine driver needs to
767          * have a pointer to the DAI structure, so for now it must be a global
768          * variable.
769          */
770         cs4270_dai.dev = &i2c_client->dev;
771
772         /* Register the DAI.  If all the other ASoC driver have already
773          * registered, then this will call our probe function, so
774          * cs4270_codec needs to be ready.
775          */
776         cs4270_codec = codec;
777         ret = snd_soc_register_dai(&cs4270_dai);
778         if (ret < 0) {
779                 dev_err(&i2c_client->dev, "failed to register DAIe\n");
780                 goto error_free_codec;
781         }
782
783         i2c_set_clientdata(i2c_client, cs4270);
784
785         return 0;
786
787 error_free_codec:
788         kfree(cs4270);
789         cs4270_codec = NULL;
790         cs4270_dai.dev = NULL;
791
792         return ret;
793 }
794
795 /**
796  * cs4270_i2c_remove - remove an I2C device
797  * @i2c_client: the I2C client object
798  *
799  * This function is the counterpart to cs4270_i2c_probe().
800  */
801 static int cs4270_i2c_remove(struct i2c_client *i2c_client)
802 {
803         struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
804
805         kfree(cs4270);
806         cs4270_codec = NULL;
807         cs4270_dai.dev = NULL;
808
809         return 0;
810 }
811
812 /*
813  * cs4270_id - I2C device IDs supported by this driver
814  */
815 static struct i2c_device_id cs4270_id[] = {
816         {"cs4270", 0},
817         {}
818 };
819 MODULE_DEVICE_TABLE(i2c, cs4270_id);
820
821 #ifdef CONFIG_PM
822
823 /* This suspend/resume implementation can handle both - a simple standby
824  * where the codec remains powered, and a full suspend, where the voltage
825  * domain the codec is connected to is teared down and/or any other hardware
826  * reset condition is asserted.
827  *
828  * The codec's own power saving features are enabled in the suspend callback,
829  * and all registers are written back to the hardware when resuming.
830  */
831
832 static int cs4270_soc_suspend(struct platform_device *pdev, pm_message_t mesg)
833 {
834         struct snd_soc_codec *codec = cs4270_codec;
835         struct cs4270_private *cs4270 = codec->private_data;
836         int reg, ret;
837
838         reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
839         if (reg < 0)
840                 return reg;
841
842         ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
843         if (ret < 0)
844                 return ret;
845
846         regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
847                                cs4270->supplies);
848
849         return 0;
850 }
851
852 static int cs4270_soc_resume(struct platform_device *pdev)
853 {
854         struct snd_soc_codec *codec = cs4270_codec;
855         struct cs4270_private *cs4270 = codec->private_data;
856         struct i2c_client *i2c_client = codec->control_data;
857         int reg;
858
859         regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
860                               cs4270->supplies);
861
862         /* In case the device was put to hard reset during sleep, we need to
863          * wait 500ns here before any I2C communication. */
864         ndelay(500);
865
866         /* first restore the entire register cache ... */
867         for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
868                 u8 val = snd_soc_read(codec, reg);
869
870                 if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
871                         dev_err(codec->dev, "i2c write failed\n");
872                         return -EIO;
873                 }
874         }
875
876         /* ... then disable the power-down bits */
877         reg = snd_soc_read(codec, CS4270_PWRCTL);
878         reg &= ~CS4270_PWRCTL_PDN_ALL;
879
880         return snd_soc_write(codec, CS4270_PWRCTL, reg);
881 }
882 #else
883 #define cs4270_soc_suspend      NULL
884 #define cs4270_soc_resume       NULL
885 #endif /* CONFIG_PM */
886
887 /*
888  * cs4270_i2c_driver - I2C device identification
889  *
890  * This structure tells the I2C subsystem how to identify and support a
891  * given I2C device type.
892  */
893 static struct i2c_driver cs4270_i2c_driver = {
894         .driver = {
895                 .name = "cs4270",
896                 .owner = THIS_MODULE,
897         },
898         .id_table = cs4270_id,
899         .probe = cs4270_i2c_probe,
900         .remove = cs4270_i2c_remove,
901 };
902
903 /*
904  * ASoC codec device structure
905  *
906  * Assign this variable to the codec_dev field of the machine driver's
907  * snd_soc_device structure.
908  */
909 struct snd_soc_codec_device soc_codec_device_cs4270 = {
910         .probe =        cs4270_probe,
911         .remove =       cs4270_remove,
912         .suspend =      cs4270_soc_suspend,
913         .resume =       cs4270_soc_resume,
914 };
915 EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
916
917 static int __init cs4270_init(void)
918 {
919         pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
920
921         return i2c_add_driver(&cs4270_i2c_driver);
922 }
923 module_init(cs4270_init);
924
925 static void __exit cs4270_exit(void)
926 {
927         i2c_del_driver(&cs4270_i2c_driver);
928 }
929 module_exit(cs4270_exit);
930
931 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
932 MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
933 MODULE_LICENSE("GPL");