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 | |
5e7c0344 | 21 | * - Power management is supported |
b0c813ce TT |
22 | */ |
23 | ||
24 | #include <linux/module.h> | |
25 | #include <linux/platform_device.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
b0c813ce TT |
27 | #include <sound/core.h> |
28 | #include <sound/soc.h> | |
29 | #include <sound/initval.h> | |
30 | #include <linux/i2c.h> | |
5e7c0344 | 31 | #include <linux/delay.h> |
ffbfd336 | 32 | #include <linux/regulator/consumer.h> |
b0c813ce | 33 | |
8432395f TT |
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 | ||
8432395f TT |
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) | |
80ab8817 | 60 | #define CS4270_I2C_INCR 0x80 |
9dbd627b | 61 | |
8432395f TT |
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 | |
5e7c0344 DM |
69 | #define CS4270_PWRCTL_PDN_ALL \ |
70 | (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN) | |
8432395f TT |
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 | ||
11b8fca5 TT |
109 | /* Power-on default values for the registers |
110 | * | |
111 | * This array contains the power-on default values of the registers, with the | |
112 | * exception of the "CHIPID" register (01h). The lower four bits of that | |
113 | * register contain the hardware revision, so it is treated as volatile. | |
114 | * | |
115 | * Also note that on the CS4270, the first readable register is 1, but ASoC | |
116 | * assumes the first register is 0. Therfore, the array must have an entry for | |
117 | * register 0, but we use cs4270_reg_is_readable() to tell ASoC that it can't | |
118 | * be read. | |
119 | */ | |
120 | static const u8 cs4270_default_reg_cache[CS4270_LASTREG + 1] = { | |
121 | 0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x20, 0x00, 0x00 | |
122 | }; | |
123 | ||
ffbfd336 DM |
124 | static const char *supply_names[] = { |
125 | "va", "vd", "vlc" | |
126 | }; | |
127 | ||
0db4d070 TT |
128 | /* Private data for the CS4270 */ |
129 | struct cs4270_private { | |
f0fba2ad LG |
130 | enum snd_soc_control_type control_type; |
131 | void *control_data; | |
0db4d070 TT |
132 | unsigned int mclk; /* Input frequency of the MCLK pin */ |
133 | unsigned int mode; /* The mode (I2S or left-justified) */ | |
4eae080d | 134 | unsigned int slave_mode; |
1a4ba05e | 135 | unsigned int manual_mute; |
ffbfd336 DM |
136 | |
137 | /* power domain regulators */ | |
138 | struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)]; | |
0db4d070 TT |
139 | }; |
140 | ||
ff7bf02f TT |
141 | /** |
142 | * struct cs4270_mode_ratios - clock ratio tables | |
143 | * @ratio: the ratio of MCLK to the sample rate | |
144 | * @speed_mode: the Speed Mode bits to set in the Mode Control register for | |
145 | * this ratio | |
146 | * @mclk: the Ratio Select bits to set in the Mode Control register for this | |
147 | * ratio | |
8432395f TT |
148 | * |
149 | * The data for this chart is taken from Table 5 of the CS4270 reference | |
150 | * manual. | |
151 | * | |
152 | * This table is used to determine how to program the Mode Control register. | |
153 | * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling | |
154 | * rates the CS4270 currently supports. | |
155 | * | |
ff7bf02f | 156 | * @speed_mode is the corresponding bit pattern to be written to the |
8432395f TT |
157 | * MODE bits of the Mode Control Register |
158 | * | |
ff7bf02f | 159 | * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of |
8432395f TT |
160 | * the Mode Control Register. |
161 | * | |
162 | * In situations where a single ratio is represented by multiple speed | |
163 | * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick | |
164 | * double-speed instead of quad-speed. However, the CS4270 errata states | |
ff7bf02f | 165 | * that divide-By-1.5 can cause failures, so we avoid that mode where |
8432395f TT |
166 | * possible. |
167 | * | |
ff7bf02f TT |
168 | * Errata: There is an errata for the CS4270 where divide-by-1.5 does not |
169 | * work if Vd is 3.3V. If this effects you, select the | |
8432395f TT |
170 | * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will |
171 | * never select any sample rates that require divide-by-1.5. | |
172 | */ | |
ff7bf02f | 173 | struct cs4270_mode_ratios { |
8432395f TT |
174 | unsigned int ratio; |
175 | u8 speed_mode; | |
176 | u8 mclk; | |
ff7bf02f TT |
177 | }; |
178 | ||
d9fb7fbd | 179 | static struct cs4270_mode_ratios cs4270_mode_ratios[] = { |
8432395f TT |
180 | {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, |
181 | #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA | |
182 | {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, | |
183 | #endif | |
184 | {128, CS4270_MODE_2X, CS4270_MODE_DIV1}, | |
185 | {192, CS4270_MODE_4X, CS4270_MODE_DIV3}, | |
186 | {256, CS4270_MODE_1X, CS4270_MODE_DIV1}, | |
187 | {384, CS4270_MODE_2X, CS4270_MODE_DIV3}, | |
188 | {512, CS4270_MODE_1X, CS4270_MODE_DIV2}, | |
189 | {768, CS4270_MODE_1X, CS4270_MODE_DIV3}, | |
190 | {1024, CS4270_MODE_1X, CS4270_MODE_DIV4} | |
191 | }; | |
192 | ||
193 | /* The number of MCLK/LRCK ratios supported by the CS4270 */ | |
194 | #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) | |
9dbd627b | 195 | |
d4754ec9 | 196 | static int cs4270_reg_is_readable(struct snd_soc_codec *codec, unsigned int reg) |
11b8fca5 TT |
197 | { |
198 | return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG); | |
199 | } | |
200 | ||
d4754ec9 | 201 | static int cs4270_reg_is_volatile(struct snd_soc_codec *codec, unsigned int reg) |
11b8fca5 TT |
202 | { |
203 | /* Unreadable registers are considered volatile */ | |
204 | if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) | |
205 | return 1; | |
206 | ||
207 | return reg == CS4270_CHIPID; | |
208 | } | |
209 | ||
ff7bf02f TT |
210 | /** |
211 | * cs4270_set_dai_sysclk - determine the CS4270 samples rates. | |
212 | * @codec_dai: the codec DAI | |
213 | * @clk_id: the clock ID (ignored) | |
214 | * @freq: the MCLK input frequency | |
215 | * @dir: the clock direction (ignored) | |
9dbd627b | 216 | * |
ff7bf02f TT |
217 | * This function is used to tell the codec driver what the input MCLK |
218 | * frequency is. | |
9dbd627b TT |
219 | * |
220 | * The value of MCLK is used to determine which sample rates are supported | |
221 | * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine | |
ff7bf02f | 222 | * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024. |
9dbd627b TT |
223 | * |
224 | * This function calculates the nine ratios and determines which ones match | |
225 | * a standard sample rate. If there's a match, then it is added to the list | |
ff7bf02f | 226 | * of supported sample rates. |
9dbd627b TT |
227 | * |
228 | * This function must be called by the machine driver's 'startup' function, | |
229 | * otherwise the list of supported sample rates will not be available in | |
230 | * time for ALSA. | |
6aababdf DM |
231 | * |
232 | * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause | |
233 | * theoretically possible sample rates to be enabled. Call it again with a | |
234 | * proper value set one the external clock is set (most probably you would do | |
235 | * that from a machine's driver 'hw_param' hook. | |
9dbd627b | 236 | */ |
e550e17f | 237 | static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
9dbd627b TT |
238 | int clk_id, unsigned int freq, int dir) |
239 | { | |
240 | struct snd_soc_codec *codec = codec_dai->codec; | |
b2c812e2 | 241 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
9dbd627b TT |
242 | |
243 | cs4270->mclk = freq; | |
9dbd627b TT |
244 | return 0; |
245 | } | |
246 | ||
ff7bf02f TT |
247 | /** |
248 | * cs4270_set_dai_fmt - configure the codec for the selected audio format | |
249 | * @codec_dai: the codec DAI | |
250 | * @format: a SND_SOC_DAIFMT_x value indicating the data format | |
9dbd627b TT |
251 | * |
252 | * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the | |
253 | * codec accordingly. | |
254 | * | |
255 | * Currently, this function only supports SND_SOC_DAIFMT_I2S and | |
256 | * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified | |
257 | * data for playback only, but ASoC currently does not support different | |
258 | * formats for playback vs. record. | |
259 | */ | |
e550e17f | 260 | static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, |
9dbd627b TT |
261 | unsigned int format) |
262 | { | |
263 | struct snd_soc_codec *codec = codec_dai->codec; | |
b2c812e2 | 264 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
9dbd627b TT |
265 | int ret = 0; |
266 | ||
4eae080d | 267 | /* set DAI format */ |
9dbd627b TT |
268 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
269 | case SND_SOC_DAIFMT_I2S: | |
270 | case SND_SOC_DAIFMT_LEFT_J: | |
271 | cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; | |
272 | break; | |
273 | default: | |
a6c255e0 | 274 | dev_err(codec->dev, "invalid dai format\n"); |
9dbd627b TT |
275 | ret = -EINVAL; |
276 | } | |
277 | ||
4eae080d DM |
278 | /* set master/slave audio interface */ |
279 | switch (format & SND_SOC_DAIFMT_MASTER_MASK) { | |
280 | case SND_SOC_DAIFMT_CBS_CFS: | |
281 | cs4270->slave_mode = 1; | |
282 | break; | |
283 | case SND_SOC_DAIFMT_CBM_CFM: | |
284 | cs4270->slave_mode = 0; | |
285 | break; | |
4eae080d | 286 | default: |
ff09d49a | 287 | /* all other modes are unsupported by the hardware */ |
4eae080d DM |
288 | ret = -EINVAL; |
289 | } | |
290 | ||
9dbd627b TT |
291 | return ret; |
292 | } | |
293 | ||
ff7bf02f TT |
294 | /** |
295 | * cs4270_hw_params - program the CS4270 with the given hardware parameters. | |
296 | * @substream: the audio stream | |
297 | * @params: the hardware parameters to set | |
298 | * @dai: the SOC DAI (ignored) | |
b0c813ce | 299 | * |
ff7bf02f TT |
300 | * This function programs the hardware with the values provided. |
301 | * Specifically, the sample rate and the data format. | |
302 | * | |
303 | * The .ops functions are used to provide board-specific data, like input | |
304 | * frequencies, to this driver. This function takes that information, | |
b0c813ce TT |
305 | * combines it with the hardware parameters provided, and programs the |
306 | * hardware accordingly. | |
307 | */ | |
308 | static int cs4270_hw_params(struct snd_pcm_substream *substream, | |
dee89c4d MB |
309 | struct snd_pcm_hw_params *params, |
310 | struct snd_soc_dai *dai) | |
b0c813ce TT |
311 | { |
312 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 313 | struct snd_soc_codec *codec = rtd->codec; |
b2c812e2 | 314 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
e34ba212 | 315 | int ret; |
b0c813ce TT |
316 | unsigned int i; |
317 | unsigned int rate; | |
318 | unsigned int ratio; | |
319 | int reg; | |
320 | ||
321 | /* Figure out which MCLK/LRCK ratio to use */ | |
322 | ||
323 | rate = params_rate(params); /* Sampling rate, in Hz */ | |
324 | ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */ | |
325 | ||
9dbd627b | 326 | for (i = 0; i < NUM_MCLK_RATIOS; i++) { |
8432395f | 327 | if (cs4270_mode_ratios[i].ratio == ratio) |
b0c813ce TT |
328 | break; |
329 | } | |
330 | ||
9dbd627b | 331 | if (i == NUM_MCLK_RATIOS) { |
b0c813ce | 332 | /* We did not find a matching ratio */ |
a6c255e0 | 333 | dev_err(codec->dev, "could not find matching ratio\n"); |
b0c813ce TT |
334 | return -EINVAL; |
335 | } | |
336 | ||
d5e9ba1d | 337 | /* Set the sample rate */ |
b0c813ce TT |
338 | |
339 | reg = snd_soc_read(codec, CS4270_MODE); | |
340 | reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK); | |
4eae080d DM |
341 | reg |= cs4270_mode_ratios[i].mclk; |
342 | ||
343 | if (cs4270->slave_mode) | |
344 | reg |= CS4270_MODE_SLAVE; | |
345 | else | |
346 | reg |= cs4270_mode_ratios[i].speed_mode; | |
b0c813ce TT |
347 | |
348 | ret = snd_soc_write(codec, CS4270_MODE, reg); | |
349 | if (ret < 0) { | |
a6c255e0 | 350 | dev_err(codec->dev, "i2c write failed\n"); |
b0c813ce TT |
351 | return ret; |
352 | } | |
353 | ||
d5e9ba1d | 354 | /* Set the DAI format */ |
b0c813ce TT |
355 | |
356 | reg = snd_soc_read(codec, CS4270_FORMAT); | |
357 | reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK); | |
358 | ||
359 | switch (cs4270->mode) { | |
360 | case SND_SOC_DAIFMT_I2S: | |
361 | reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S; | |
362 | break; | |
363 | case SND_SOC_DAIFMT_LEFT_J: | |
364 | reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; | |
365 | break; | |
366 | default: | |
a6c255e0 | 367 | dev_err(codec->dev, "unknown dai format\n"); |
b0c813ce TT |
368 | return -EINVAL; |
369 | } | |
370 | ||
371 | ret = snd_soc_write(codec, CS4270_FORMAT, reg); | |
372 | if (ret < 0) { | |
a6c255e0 | 373 | dev_err(codec->dev, "i2c write failed\n"); |
b0c813ce TT |
374 | return ret; |
375 | } | |
376 | ||
b0c813ce TT |
377 | return ret; |
378 | } | |
379 | ||
ff7bf02f | 380 | /** |
1a4ba05e | 381 | * cs4270_dai_mute - enable/disable the CS4270 external mute |
ff7bf02f TT |
382 | * @dai: the SOC DAI |
383 | * @mute: 0 = disable mute, 1 = enable mute | |
b0c813ce TT |
384 | * |
385 | * This function toggles the mute bits in the MUTE register. The CS4270's | |
386 | * mute capability is intended for external muting circuitry, so if the | |
387 | * board does not have the MUTEA or MUTEB pins connected to such circuitry, | |
388 | * then this function will do nothing. | |
389 | */ | |
1a4ba05e | 390 | static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute) |
b0c813ce TT |
391 | { |
392 | struct snd_soc_codec *codec = dai->codec; | |
b2c812e2 | 393 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
b0c813ce TT |
394 | int reg6; |
395 | ||
396 | reg6 = snd_soc_read(codec, CS4270_MUTE); | |
397 | ||
398 | if (mute) | |
d5e9ba1d | 399 | reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B; |
1a4ba05e | 400 | else { |
d5e9ba1d | 401 | reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B); |
1a4ba05e DM |
402 | reg6 |= cs4270->manual_mute; |
403 | } | |
b0c813ce TT |
404 | |
405 | return snd_soc_write(codec, CS4270_MUTE, reg6); | |
406 | } | |
b0c813ce | 407 | |
1a4ba05e DM |
408 | /** |
409 | * cs4270_soc_put_mute - put callback for the 'Master Playback switch' | |
410 | * alsa control. | |
411 | * @kcontrol: mixer control | |
412 | * @ucontrol: control element information | |
413 | * | |
414 | * This function basically passes the arguments on to the generic | |
415 | * snd_soc_put_volsw() function and saves the mute information in | |
416 | * our private data structure. This is because we want to prevent | |
417 | * cs4270_dai_mute() neglecting the user's decision to manually | |
418 | * mute the codec's output. | |
419 | * | |
420 | * Returns 0 for success. | |
421 | */ | |
422 | static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol, | |
423 | struct snd_ctl_elem_value *ucontrol) | |
424 | { | |
425 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
b2c812e2 | 426 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
1a4ba05e DM |
427 | int left = !ucontrol->value.integer.value[0]; |
428 | int right = !ucontrol->value.integer.value[1]; | |
429 | ||
430 | cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) | | |
431 | (right ? CS4270_MUTE_DAC_B : 0); | |
432 | ||
433 | return snd_soc_put_volsw(kcontrol, ucontrol); | |
434 | } | |
435 | ||
b0c813ce TT |
436 | /* A list of non-DAPM controls that the CS4270 supports */ |
437 | static const struct snd_kcontrol_new cs4270_snd_controls[] = { | |
438 | SOC_DOUBLE_R("Master Playback Volume", | |
d5e9ba1d TT |
439 | CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1), |
440 | SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0), | |
441 | SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0), | |
442 | SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0), | |
7e1aa1dc | 443 | SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0), |
d5e9ba1d TT |
444 | SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1), |
445 | SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0), | |
1a4ba05e DM |
446 | SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1), |
447 | SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1, | |
448 | snd_soc_get_volsw, cs4270_soc_put_mute), | |
b0c813ce TT |
449 | }; |
450 | ||
6335d055 EM |
451 | static struct snd_soc_dai_ops cs4270_dai_ops = { |
452 | .hw_params = cs4270_hw_params, | |
453 | .set_sysclk = cs4270_set_dai_sysclk, | |
454 | .set_fmt = cs4270_set_dai_fmt, | |
1a4ba05e | 455 | .digital_mute = cs4270_dai_mute, |
6335d055 EM |
456 | }; |
457 | ||
5c75848a | 458 | static struct snd_soc_dai_driver cs4270_dai = { |
f0fba2ad | 459 | .name = "cs4270-hifi", |
0db4d070 TT |
460 | .playback = { |
461 | .stream_name = "Playback", | |
462 | .channels_min = 1, | |
463 | .channels_max = 2, | |
f0fba2ad LG |
464 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
465 | .rate_min = 4000, | |
466 | .rate_max = 216000, | |
0db4d070 TT |
467 | .formats = CS4270_FORMATS, |
468 | }, | |
469 | .capture = { | |
470 | .stream_name = "Capture", | |
471 | .channels_min = 1, | |
472 | .channels_max = 2, | |
f0fba2ad LG |
473 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
474 | .rate_min = 4000, | |
475 | .rate_max = 216000, | |
0db4d070 TT |
476 | .formats = CS4270_FORMATS, |
477 | }, | |
6335d055 | 478 | .ops = &cs4270_dai_ops, |
0db4d070 | 479 | }; |
0db4d070 | 480 | |
ff7bf02f TT |
481 | /** |
482 | * cs4270_probe - ASoC probe function | |
483 | * @pdev: platform device | |
484 | * | |
485 | * This function is called when ASoC has all the pieces it needs to | |
486 | * instantiate a sound driver. | |
04eb093c | 487 | */ |
f0fba2ad | 488 | static int cs4270_probe(struct snd_soc_codec *codec) |
04eb093c | 489 | { |
b2c812e2 | 490 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
11b8fca5 | 491 | int i, ret; |
04eb093c | 492 | |
f0fba2ad | 493 | codec->control_data = cs4270->control_data; |
b0c813ce | 494 | |
11b8fca5 TT |
495 | /* Tell ASoC what kind of I/O to use to read the registers. ASoC will |
496 | * then do the I2C transactions itself. | |
497 | */ | |
498 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs4270->control_type); | |
b0c813ce | 499 | if (ret < 0) { |
11b8fca5 | 500 | dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret); |
f0fba2ad | 501 | return ret; |
0db4d070 TT |
502 | } |
503 | ||
d5e9ba1d TT |
504 | /* Disable auto-mute. This feature appears to be buggy. In some |
505 | * situations, auto-mute will not deactivate when it should, so we want | |
506 | * this feature disabled by default. An application (e.g. alsactl) can | |
507 | * re-enabled it by using the controls. | |
508 | */ | |
11b8fca5 | 509 | ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0); |
d5e9ba1d | 510 | if (ret < 0) { |
f0fba2ad | 511 | dev_err(codec->dev, "i2c write failed\n"); |
d5e9ba1d TT |
512 | return ret; |
513 | } | |
514 | ||
515 | /* Disable automatic volume control. The hardware enables, and it | |
516 | * causes volume change commands to be delayed, sometimes until after | |
517 | * playback has started. An application (e.g. alsactl) can | |
518 | * re-enabled it by using the controls. | |
519 | */ | |
11b8fca5 TT |
520 | ret = snd_soc_update_bits(codec, CS4270_TRANS, |
521 | CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0); | |
d5e9ba1d | 522 | if (ret < 0) { |
f0fba2ad | 523 | dev_err(codec->dev, "i2c write failed\n"); |
d5e9ba1d TT |
524 | return ret; |
525 | } | |
526 | ||
f0fba2ad LG |
527 | /* Add the non-DAPM controls */ |
528 | ret = snd_soc_add_controls(codec, cs4270_snd_controls, | |
529 | ARRAY_SIZE(cs4270_snd_controls)); | |
0db4d070 | 530 | if (ret < 0) { |
f0fba2ad LG |
531 | dev_err(codec->dev, "failed to add controls\n"); |
532 | return ret; | |
b0c813ce TT |
533 | } |
534 | ||
f0fba2ad LG |
535 | /* get the power supply regulators */ |
536 | for (i = 0; i < ARRAY_SIZE(supply_names); i++) | |
537 | cs4270->supplies[i].supply = supply_names[i]; | |
538 | ||
539 | ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies), | |
540 | cs4270->supplies); | |
541 | if (ret < 0) | |
542 | return ret; | |
543 | ||
544 | ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies), | |
545 | cs4270->supplies); | |
546 | if (ret < 0) | |
547 | goto error_free_regulators; | |
ec2cd95f | 548 | |
b0c813ce TT |
549 | return 0; |
550 | ||
f0fba2ad LG |
551 | error_free_regulators: |
552 | regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), | |
553 | cs4270->supplies); | |
b0c813ce | 554 | |
b0c813ce TT |
555 | return ret; |
556 | } | |
557 | ||
ff7bf02f | 558 | /** |
f0fba2ad LG |
559 | * cs4270_remove - ASoC remove function |
560 | * @pdev: platform device | |
ff7bf02f | 561 | * |
f0fba2ad | 562 | * This function is the counterpart to cs4270_probe(). |
ff7bf02f | 563 | */ |
f0fba2ad | 564 | static int cs4270_remove(struct snd_soc_codec *codec) |
0db4d070 | 565 | { |
f0fba2ad | 566 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
0db4d070 | 567 | |
f0fba2ad LG |
568 | regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies); |
569 | regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies); | |
0db4d070 TT |
570 | |
571 | return 0; | |
ff637d38 | 572 | }; |
ff637d38 | 573 | |
5e7c0344 DM |
574 | #ifdef CONFIG_PM |
575 | ||
576 | /* This suspend/resume implementation can handle both - a simple standby | |
577 | * where the codec remains powered, and a full suspend, where the voltage | |
578 | * domain the codec is connected to is teared down and/or any other hardware | |
579 | * reset condition is asserted. | |
580 | * | |
581 | * The codec's own power saving features are enabled in the suspend callback, | |
582 | * and all registers are written back to the hardware when resuming. | |
583 | */ | |
584 | ||
f0fba2ad | 585 | static int cs4270_soc_suspend(struct snd_soc_codec *codec, pm_message_t mesg) |
15b5bdae | 586 | { |
b2c812e2 | 587 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
ffbfd336 | 588 | int reg, ret; |
15b5bdae | 589 | |
ffbfd336 DM |
590 | reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL; |
591 | if (reg < 0) | |
592 | return reg; | |
593 | ||
594 | ret = snd_soc_write(codec, CS4270_PWRCTL, reg); | |
595 | if (ret < 0) | |
596 | return ret; | |
597 | ||
598 | regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), | |
599 | cs4270->supplies); | |
600 | ||
601 | return 0; | |
15b5bdae DM |
602 | } |
603 | ||
f0fba2ad | 604 | static int cs4270_soc_resume(struct snd_soc_codec *codec) |
15b5bdae | 605 | { |
b2c812e2 | 606 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
15b5bdae | 607 | struct i2c_client *i2c_client = codec->control_data; |
5e7c0344 DM |
608 | int reg; |
609 | ||
ffbfd336 DM |
610 | regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies), |
611 | cs4270->supplies); | |
612 | ||
5e7c0344 DM |
613 | /* In case the device was put to hard reset during sleep, we need to |
614 | * wait 500ns here before any I2C communication. */ | |
615 | ndelay(500); | |
616 | ||
617 | /* first restore the entire register cache ... */ | |
618 | for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) { | |
619 | u8 val = snd_soc_read(codec, reg); | |
620 | ||
15b5bdae | 621 | if (i2c_smbus_write_byte_data(i2c_client, reg, val)) { |
5e7c0344 DM |
622 | dev_err(codec->dev, "i2c write failed\n"); |
623 | return -EIO; | |
624 | } | |
625 | } | |
626 | ||
627 | /* ... then disable the power-down bits */ | |
628 | reg = snd_soc_read(codec, CS4270_PWRCTL); | |
629 | reg &= ~CS4270_PWRCTL_PDN_ALL; | |
630 | ||
631 | return snd_soc_write(codec, CS4270_PWRCTL, reg); | |
632 | } | |
633 | #else | |
15b5bdae DM |
634 | #define cs4270_soc_suspend NULL |
635 | #define cs4270_soc_resume NULL | |
5e7c0344 DM |
636 | #endif /* CONFIG_PM */ |
637 | ||
f0fba2ad LG |
638 | /* |
639 | * ASoC codec device structure | |
640 | * | |
641 | * Assign this variable to the codec_dev field of the machine driver's | |
642 | * snd_soc_device structure. | |
643 | */ | |
11b8fca5 TT |
644 | static const struct snd_soc_codec_driver soc_codec_device_cs4270 = { |
645 | .probe = cs4270_probe, | |
646 | .remove = cs4270_remove, | |
647 | .suspend = cs4270_soc_suspend, | |
648 | .resume = cs4270_soc_resume, | |
649 | .volatile_register = cs4270_reg_is_volatile, | |
650 | .readable_register = cs4270_reg_is_readable, | |
651 | .reg_cache_size = CS4270_LASTREG + 1, | |
652 | .reg_word_size = sizeof(u8), | |
653 | .reg_cache_default = cs4270_default_reg_cache, | |
f0fba2ad LG |
654 | }; |
655 | ||
656 | /** | |
657 | * cs4270_i2c_probe - initialize the I2C interface of the CS4270 | |
658 | * @i2c_client: the I2C client object | |
659 | * @id: the I2C device ID (ignored) | |
660 | * | |
661 | * This function is called whenever the I2C subsystem finds a device that | |
662 | * matches the device ID given via a prior call to i2c_add_driver(). | |
663 | */ | |
664 | static int cs4270_i2c_probe(struct i2c_client *i2c_client, | |
665 | const struct i2c_device_id *id) | |
666 | { | |
667 | struct cs4270_private *cs4270; | |
668 | int ret; | |
669 | ||
670 | /* Verify that we have a CS4270 */ | |
671 | ||
672 | ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID); | |
673 | if (ret < 0) { | |
674 | dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n", | |
675 | i2c_client->addr); | |
676 | return ret; | |
677 | } | |
678 | /* The top four bits of the chip ID should be 1100. */ | |
679 | if ((ret & 0xF0) != 0xC0) { | |
680 | dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n", | |
681 | i2c_client->addr); | |
682 | return -ENODEV; | |
683 | } | |
684 | ||
685 | dev_info(&i2c_client->dev, "found device at i2c address %X\n", | |
686 | i2c_client->addr); | |
687 | dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF); | |
688 | ||
689 | cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL); | |
690 | if (!cs4270) { | |
691 | dev_err(&i2c_client->dev, "could not allocate codec\n"); | |
692 | return -ENOMEM; | |
693 | } | |
694 | ||
695 | i2c_set_clientdata(i2c_client, cs4270); | |
696 | cs4270->control_data = i2c_client; | |
697 | cs4270->control_type = SND_SOC_I2C; | |
698 | ||
699 | ret = snd_soc_register_codec(&i2c_client->dev, | |
700 | &soc_codec_device_cs4270, &cs4270_dai, 1); | |
701 | if (ret < 0) | |
702 | kfree(cs4270); | |
703 | return ret; | |
704 | } | |
705 | ||
706 | /** | |
707 | * cs4270_i2c_remove - remove an I2C device | |
708 | * @i2c_client: the I2C client object | |
709 | * | |
710 | * This function is the counterpart to cs4270_i2c_probe(). | |
711 | */ | |
712 | static int cs4270_i2c_remove(struct i2c_client *i2c_client) | |
713 | { | |
714 | snd_soc_unregister_codec(&i2c_client->dev); | |
715 | kfree(i2c_get_clientdata(i2c_client)); | |
716 | return 0; | |
717 | } | |
718 | ||
719 | /* | |
720 | * cs4270_id - I2C device IDs supported by this driver | |
721 | */ | |
79a54ea1 | 722 | static const struct i2c_device_id cs4270_id[] = { |
f0fba2ad LG |
723 | {"cs4270", 0}, |
724 | {} | |
725 | }; | |
726 | MODULE_DEVICE_TABLE(i2c, cs4270_id); | |
727 | ||
ff7bf02f TT |
728 | /* |
729 | * cs4270_i2c_driver - I2C device identification | |
730 | * | |
731 | * This structure tells the I2C subsystem how to identify and support a | |
732 | * given I2C device type. | |
733 | */ | |
ff637d38 TT |
734 | static struct i2c_driver cs4270_i2c_driver = { |
735 | .driver = { | |
f0fba2ad | 736 | .name = "cs4270-codec", |
ff637d38 TT |
737 | .owner = THIS_MODULE, |
738 | }, | |
739 | .id_table = cs4270_id, | |
740 | .probe = cs4270_i2c_probe, | |
0db4d070 | 741 | .remove = cs4270_i2c_remove, |
ff637d38 | 742 | }; |
b0c813ce | 743 | |
c9b3a40f | 744 | static int __init cs4270_init(void) |
64089b84 | 745 | { |
04eb093c | 746 | return i2c_add_driver(&cs4270_i2c_driver); |
64089b84 MB |
747 | } |
748 | module_init(cs4270_init); | |
749 | ||
750 | static void __exit cs4270_exit(void) | |
751 | { | |
04eb093c | 752 | i2c_del_driver(&cs4270_i2c_driver); |
64089b84 MB |
753 | } |
754 | module_exit(cs4270_exit); | |
755 | ||
b0c813ce TT |
756 | MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); |
757 | MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver"); | |
758 | MODULE_LICENSE("GPL"); |