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