aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc')
-rw-r--r--sound/soc/codecs/cs4270.c319
1 files changed, 172 insertions, 147 deletions
diff --git a/sound/soc/codecs/cs4270.c b/sound/soc/codecs/cs4270.c
index b659a5dfd776..8beae65d083c 100644
--- a/sound/soc/codecs/cs4270.c
+++ b/sound/soc/codecs/cs4270.c
@@ -12,9 +12,11 @@
12 * 12 *
13 * Current features/limitations: 13 * Current features/limitations:
14 * 14 *
15 * 1) Stand-alone and software mode is supported. Stand-alone is 15 * 1) Software mode is supported. Stand-alone mode is automatically
16 * automatically selected if I2C is disabled or if a CS4270 is not found 16 * selected if I2C is disabled or if a CS4270 is not found on the I2C
17 * on the I2C bus. 17 * bus. However, stand-alone mode is only partially implemented because
18 * there is no mechanism yet for this driver and the machine driver to
19 * communicate the values of the M0, M1, MCLK1, and MCLK2 pins.
18 * 2) Only I2C is supported, not SPI 20 * 2) Only I2C is supported, not SPI
19 * 3) Only Master mode is supported, not Slave. 21 * 3) Only Master mode is supported, not Slave.
20 * 4) The machine driver's 'startup' function must call 22 * 4) The machine driver's 'startup' function must call
@@ -34,12 +36,157 @@
34 36
35#include "cs4270.h" 37#include "cs4270.h"
36 38
39/* If I2C is defined, then we support software mode. However, if we're
40 not compiled as module but I2C is, then we can't use I2C calls. */
41#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
42#define USE_I2C
43#endif
44
37/* Private data for the CS4270 */ 45/* Private data for the CS4270 */
38struct cs4270_private { 46struct cs4270_private {
39 unsigned int mclk; /* Input frequency of the MCLK pin */ 47 unsigned int mclk; /* Input frequency of the MCLK pin */
40 unsigned int mode; /* The mode (I2S or left-justified) */ 48 unsigned int mode; /* The mode (I2S or left-justified) */
41}; 49};
42 50
51/* The number of MCLK/LRCK ratios supported by the CS4270 */
52#define NUM_MCLK_RATIOS 9
53
54/* The actual MCLK/LRCK ratios, in increasing numerical order */
55static unsigned int mclk_ratios[NUM_MCLK_RATIOS] =
56 {64, 96, 128, 192, 256, 384, 512, 768, 1024};
57
58/*
59 * Sampling rate <-> bit patter mapping
60 *
61 * This array maps sampling rates to their SNDRV_PCM_RATE_x equivalent.
62 *
63 * This is really something that ALSA should provide.
64 *
65 * This table is used by cs4270_set_dai_sysclk() to tell ALSA which sampling
66 * rates the CS4270 currently supports.
67 */
68static struct {
69 unsigned int rate;
70 unsigned int bit;
71} rate_map[] = {
72 {5512, SNDRV_PCM_RATE_5512},
73 {8000, SNDRV_PCM_RATE_8000},
74 {11025, SNDRV_PCM_RATE_11025},
75 {16000, SNDRV_PCM_RATE_16000},
76 {22050, SNDRV_PCM_RATE_22050},
77 {32000, SNDRV_PCM_RATE_32000},
78 {44100, SNDRV_PCM_RATE_44100},
79 {48000, SNDRV_PCM_RATE_48000},
80 {64000, SNDRV_PCM_RATE_64000},
81 {88200, SNDRV_PCM_RATE_88200},
82 {96000, SNDRV_PCM_RATE_96000},
83 {176400, SNDRV_PCM_RATE_176400},
84 {192000, SNDRV_PCM_RATE_192000}
85};
86
87/*
88 * Determine the CS4270 samples rates.
89 *
90 * 'freq' is the input frequency to MCLK. The other parameters are ignored.
91 *
92 * The value of MCLK is used to determine which sample rates are supported
93 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
94 * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
95 *
96 * This function calculates the nine ratios and determines which ones match
97 * a standard sample rate. If there's a match, then it is added to the list
98 * of support sample rates.
99 *
100 * This function must be called by the machine driver's 'startup' function,
101 * otherwise the list of supported sample rates will not be available in
102 * time for ALSA.
103 *
104 * Note that in stand-alone mode, the sample rate is determined by input
105 * pins M0, M1, MDIV1, and MDIV2. Also in stand-alone mode, divide-by-3
106 * is not a programmable option. However, divide-by-3 is not an available
107 * option in stand-alone mode. This cases two problems: a ratio of 768 is
108 * not available (it requires divide-by-3) and B) ratios 192 and 384 can
109 * only be selected with divide-by-1.5, but there is an errate that make
110 * this selection difficult.
111 *
112 * In addition, there is no mechanism for communicating with the machine
113 * driver what the input settings can be. This would need to be implemented
114 * for stand-alone mode to work.
115 */
116static int cs4270_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
117 int clk_id, unsigned int freq, int dir)
118{
119 struct snd_soc_codec *codec = codec_dai->codec;
120 struct cs4270_private *cs4270 = codec->private_data;
121 unsigned int rates = 0;
122 unsigned int rate_min = -1;
123 unsigned int rate_max = 0;
124 unsigned int i;
125
126 cs4270->mclk = freq;
127
128 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
129 unsigned int rate;
130 unsigned int j;
131 rate = freq / mclk_ratios[i];
132 for (j = 0; j < ARRAY_SIZE(rate_map); j++) {
133 if (rate == rate_map[j].rate) {
134 rates |= rate_map[j].bit;
135 if (rate < rate_min)
136 rate_min = rate;
137 if (rate > rate_max)
138 rate_max = rate;
139 }
140 }
141 }
142
143 if (!rates) {
144 printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
145 return -EINVAL;
146 }
147
148 codec_dai->playback.rates = rates;
149 codec_dai->playback.rate_min = rate_min;
150 codec_dai->playback.rate_max = rate_max;
151
152 codec_dai->capture.rates = rates;
153 codec_dai->capture.rate_min = rate_min;
154 codec_dai->capture.rate_max = rate_max;
155
156 return 0;
157}
158
159/*
160 * Configure the codec for the selected audio format
161 *
162 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
163 * codec accordingly.
164 *
165 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
166 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
167 * data for playback only, but ASoC currently does not support different
168 * formats for playback vs. record.
169 */
170static int cs4270_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
171 unsigned int format)
172{
173 struct snd_soc_codec *codec = codec_dai->codec;
174 struct cs4270_private *cs4270 = codec->private_data;
175 int ret = 0;
176
177 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
178 case SND_SOC_DAIFMT_I2S:
179 case SND_SOC_DAIFMT_LEFT_J:
180 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
181 break;
182 default:
183 printk(KERN_ERR "cs4270: invalid DAI format\n");
184 ret = -EINVAL;
185 }
186
187 return ret;
188}
189
43/* 190/*
44 * The codec isn't really big-endian or little-endian, since the I2S 191 * The codec isn't really big-endian or little-endian, since the I2S
45 * interface requires data to be sent serially with the MSbit first. 192 * interface requires data to be sent serially with the MSbit first.
@@ -53,7 +200,7 @@ struct cs4270_private {
53 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ 200 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
54 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE) 201 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
55 202
56#ifdef CONFIG_I2C 203#ifdef USE_I2C
57 204
58/* CS4270 registers addresses */ 205/* CS4270 registers addresses */
59#define CS4270_CHIPID 0x01 /* Chip ID */ 206#define CS4270_CHIPID 0x01 /* Chip ID */
@@ -141,7 +288,7 @@ static int cs4270_fill_cache(struct snd_soc_codec *codec)
141 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache); 288 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
142 289
143 if (length != CS4270_NUMREGS) { 290 if (length != CS4270_NUMREGS) {
144 printk(KERN_ERR "cs4270: I2C read failure, addr=%u\n", 291 printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
145 i2c_client->addr); 292 i2c_client->addr);
146 return -EIO; 293 return -EIO;
147 } 294 }
@@ -189,13 +336,14 @@ static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
189 cache[reg - CS4270_FIRSTREG] = value; 336 cache[reg - CS4270_FIRSTREG] = value;
190 return 0; 337 return 0;
191 } else { 338 } else {
192 printk(KERN_ERR "cs4270: I2C write failed\n"); 339 printk(KERN_ERR "cs4270: I2C write of register %u failed\n",
340 reg);
193 return -EIO; 341 return -EIO;
194 } 342 }
195} 343}
196 344
197/* 345/*
198 * Clock Ratio Selection for Master Mode. 346 * Clock Ratio Selection for Master Mode with I2C enabled
199 * 347 *
200 * The data for this chart is taken from Table 5 of the CS4270 reference 348 * The data for this chart is taken from Table 5 of the CS4270 reference
201 * manual. 349 * manual.
@@ -204,10 +352,8 @@ static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
204 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling 352 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
205 * rates the CS4270 currently supports. 353 * rates the CS4270 currently supports.
206 * 354 *
207 * 'ratio' is the MCLK/LRCK ratio. MCLK is usually a fixed input frequency, 355 * Each element in this array corresponds to the ratios in mclk_ratios[].
208 * and LRCK is equal to the sampling rate. The CS4270 only supports sampling 356 * These two arrays need to be in sync.
209 * rates where this ratio is one of: 64, 96, 128, 192, 256, 384, 512, 768 or
210 * 1024.
211 * 357 *
212 * 'speed_mode' is the corresponding bit pattern to be written to the 358 * 'speed_mode' is the corresponding bit pattern to be written to the
213 * MODE bits of the Mode Control Register 359 * MODE bits of the Mode Control Register
@@ -227,21 +373,20 @@ static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
227 * never select any sample rates that require divide-by-1.5. 373 * never select any sample rates that require divide-by-1.5.
228 */ 374 */
229static struct { 375static struct {
230 unsigned int ratio;
231 u8 speed_mode; 376 u8 speed_mode;
232 u8 mclk; 377 u8 mclk;
233} cs4270_mode_ratios[] = { 378} cs4270_mode_ratios[NUM_MCLK_RATIOS] = {
234 {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, 379 {CS4270_MODE_4X, CS4270_MODE_DIV1}, /* 64 */
235#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA 380#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
236 {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, 381 {CS4270_MODE_4X, CS4270_MODE_DIV15}, /* 96 */
237#endif 382#endif
238 {128, CS4270_MODE_2X, CS4270_MODE_DIV1}, 383 {CS4270_MODE_2X, CS4270_MODE_DIV1}, /* 128 */
239 {192, CS4270_MODE_4X, CS4270_MODE_DIV3}, 384 {CS4270_MODE_4X, CS4270_MODE_DIV3}, /* 192 */
240 {256, CS4270_MODE_1X, CS4270_MODE_DIV1}, 385 {CS4270_MODE_1X, CS4270_MODE_DIV1}, /* 256 */
241 {384, CS4270_MODE_2X, CS4270_MODE_DIV3}, 386 {CS4270_MODE_2X, CS4270_MODE_DIV3}, /* 384 */
242 {512, CS4270_MODE_1X, CS4270_MODE_DIV2}, 387 {CS4270_MODE_1X, CS4270_MODE_DIV2}, /* 512 */
243 {768, CS4270_MODE_1X, CS4270_MODE_DIV3}, 388 {CS4270_MODE_1X, CS4270_MODE_DIV3}, /* 768 */
244 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4} 389 {CS4270_MODE_1X, CS4270_MODE_DIV4} /* 1024 */
245}; 390};
246 391
247/* 392/*
@@ -270,12 +415,12 @@ static int cs4270_hw_params(struct snd_pcm_substream *substream,
270 rate = params_rate(params); /* Sampling rate, in Hz */ 415 rate = params_rate(params); /* Sampling rate, in Hz */
271 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */ 416 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
272 417
273 for (i = 0; i < ARRAY_SIZE(cs4270_mode_ratios); i++) { 418 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
274 if (cs4270_mode_ratios[i].ratio == ratio) 419 if (mclk_ratios[i] == ratio)
275 break; 420 break;
276 } 421 }
277 422
278 if (i == ARRAY_SIZE(cs4270_mode_ratios)) { 423 if (i == NUM_MCLK_RATIOS) {
279 /* We did not find a matching ratio */ 424 /* We did not find a matching ratio */
280 printk(KERN_ERR "cs4270: could not find matching ratio\n"); 425 printk(KERN_ERR "cs4270: could not find matching ratio\n");
281 return -EINVAL; 426 return -EINVAL;
@@ -377,126 +522,6 @@ static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
377 522
378#endif 523#endif
379 524
380/*
381 * Sampling rate <-> bit patter mapping
382 *
383 * This array maps sampling rates to their SNDRV_PCM_RATE_x equivalent.
384 *
385 * This is really something that ALSA should provide.
386 *
387 * This table is used by cs4270_set_dai_sysclk() to tell ALSA which sampling
388 * rates the CS4270 currently supports.
389 */
390static struct {
391 unsigned int rate;
392 unsigned int bit;
393} rate_map[] = {
394 {5512, SNDRV_PCM_RATE_5512},
395 {8000, SNDRV_PCM_RATE_8000},
396 {11025, SNDRV_PCM_RATE_11025},
397 {16000, SNDRV_PCM_RATE_16000},
398 {22050, SNDRV_PCM_RATE_22050},
399 {32000, SNDRV_PCM_RATE_32000},
400 {44100, SNDRV_PCM_RATE_44100},
401 {48000, SNDRV_PCM_RATE_48000},
402 {64000, SNDRV_PCM_RATE_64000},
403 {88200, SNDRV_PCM_RATE_88200},
404 {96000, SNDRV_PCM_RATE_96000},
405 {176400, SNDRV_PCM_RATE_176400},
406 {192000, SNDRV_PCM_RATE_192000}
407};
408
409/*
410 * Determine the CS4270 samples rates.
411 *
412 * 'freq' is the input frequency to MCLK. The other parameters are ignored.
413 *
414 * The value of MCLK is used to determine which sample rates are supported
415 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
416 * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
417 *
418 * This function calculates the nine ratios and determines which ones match
419 * a standard sample rate. If there's a match, then it is added to the list
420 * of support sample rates.
421 *
422 * This function must be called by the machine driver's 'startup' function,
423 * otherwise the list of supported sample rates will not be available in
424 * time for ALSA.
425 */
426static int cs4270_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
427 int clk_id, unsigned int freq, int dir)
428{
429 struct snd_soc_codec *codec = codec_dai->codec;
430 struct cs4270_private *cs4270 = codec->private_data;
431 unsigned int rates = 0;
432 unsigned int rate_min = -1;
433 unsigned int rate_max = 0;
434 unsigned int i;
435
436 cs4270->mclk = freq;
437
438 for (i = 0; i < ARRAY_SIZE(cs4270_mode_ratios); i++) {
439 unsigned int rate;
440 unsigned int j;
441 rate = freq / cs4270_mode_ratios[i].ratio;
442 for (j = 0; j < ARRAY_SIZE(rate_map); j++) {
443 if (rate == rate_map[j].rate) {
444 rates |= rate_map[j].bit;
445 if (rate < rate_min)
446 rate_min = rate;
447 if (rate > rate_max)
448 rate_max = rate;
449 }
450 }
451 }
452
453 if (!rate_max) {
454 printk(KERN_ERR "cs4270: could not find a valid rate\n");
455 return -EINVAL;
456 }
457
458 codec_dai->playback.rates = rates;
459 codec_dai->playback.rate_min = rate_min;
460 codec_dai->playback.rate_max = rate_max;
461
462 codec_dai->capture.rates = rates;
463 codec_dai->capture.rate_min = rate_min;
464 codec_dai->capture.rate_max = rate_max;
465
466 return 0;
467}
468
469/*
470 * Configure the codec for the selected audio format
471 *
472 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
473 * codec accordingly.
474 *
475 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
476 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
477 * data for playback only, but ASoC currently does not support different
478 * formats for playback vs. record.
479 */
480static int cs4270_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
481 unsigned int format)
482{
483 struct snd_soc_codec *codec = codec_dai->codec;
484 struct cs4270_private *cs4270 = codec->private_data;
485 int ret = 0;
486
487 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
488 case SND_SOC_DAIFMT_I2S:
489 case SND_SOC_DAIFMT_LEFT_J:
490 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
491 break;
492 default:
493 printk(KERN_ERR "cs4270: invalid DAI format\n");
494 ret = -EINVAL;
495 }
496
497 return ret;
498}
499
500static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind); 525static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
501 526
502/* 527/*
@@ -740,7 +765,7 @@ static int cs4270_probe(struct platform_device *pdev)
740 return ret; 765 return ret;
741 } 766 }
742 767
743#ifdef CONFIG_I2C 768#ifdef USE_I2C
744 cs4270_socdev = socdev; 769 cs4270_socdev = socdev;
745 770
746 ret = i2c_add_driver(&cs4270_i2c_driver); 771 ret = i2c_add_driver(&cs4270_i2c_driver);
@@ -780,7 +805,7 @@ static int cs4270_remove(struct platform_device *pdev)
780 805
781 snd_soc_free_pcms(socdev); 806 snd_soc_free_pcms(socdev);
782 807
783#ifdef CONFIG_I2C 808#ifdef USE_I2C
784 if (socdev->codec->control_data) 809 if (socdev->codec->control_data)
785 i2c_del_driver(&cs4270_i2c_driver); 810 i2c_del_driver(&cs4270_i2c_driver);
786#endif 811#endif