Consolidate all the MIPS PCI code in arch/mips/pci.
[linux-2.6/linux-mips.git] / sound / i2c / cs8427.c
blob2dfbcede36dcf3dd05245a8c6372e6db34738dbf
1 /*
2 * Routines for control of the CS8427 via i2c bus
3 * IEC958 (S/PDIF) receiver & transmitter by Cirrus Logic
4 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/pcm.h>
28 #include <sound/cs8427.h>
29 #include <sound/asoundef.h>
31 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
32 MODULE_DESCRIPTION("IEC958 (S/PDIF) receiver & transmitter by Cirrus Logic");
33 MODULE_LICENSE("GPL");
35 #define chip_t snd_i2c_device_t
37 #define CS8427_ADDR (0x20>>1) /* fixed address */
39 typedef struct {
40 snd_pcm_substream_t *substream;
41 char hw_status[24]; /* hardware status */
42 char def_status[24]; /* default status */
43 char pcm_status[24]; /* PCM private status */
44 char hw_udata[32];
45 snd_kcontrol_t *pcm_ctl;
46 } cs8427_stream_t;
48 typedef struct {
49 unsigned char regmap[0x14]; /* map of first 1 + 13 registers */
50 cs8427_stream_t playback;
51 cs8427_stream_t capture;
52 } cs8427_t;
54 static unsigned char swapbits(unsigned char val)
56 int bit;
57 unsigned char res = 0;
58 for (bit = 0; bit < 8; bit++) {
59 res |= val & 1;
60 res <<= 1;
61 val >>= 1;
63 return res;
66 int snd_cs8427_detect(snd_i2c_bus_t *bus, unsigned char addr)
68 int res;
70 snd_i2c_lock(bus);
71 res = snd_i2c_probeaddr(bus, CS8427_ADDR | (addr & 7));
72 snd_i2c_unlock(bus);
73 return res;
76 static int snd_cs8427_reg_write(snd_i2c_device_t *device, unsigned char reg, unsigned char val)
78 int err;
79 unsigned char buf[2];
81 buf[0] = reg & 0x7f;
82 buf[1] = val;
83 if ((err = snd_i2c_sendbytes(device, buf, 2)) != 2) {
84 snd_printk("unable to send bytes 0x%02x:0x%02x to CS8427 (%i)\n", buf[0], buf[1], err);
85 return err < 0 ? err : -EIO;
87 return 0;
90 static int snd_cs8427_reg_read(snd_i2c_device_t *device, unsigned char reg)
92 int err;
93 unsigned char buf;
95 if ((err = snd_i2c_sendbytes(device, &reg, 1)) != 1) {
96 snd_printk("unable to send register 0x%x byte to CS8427\n", reg);
97 return err < 0 ? err : -EIO;
99 if ((err = snd_i2c_readbytes(device, &buf, 1)) != 1) {
100 snd_printk("unable to read register 0x%x byte from CS8427\n", reg);
101 return err < 0 ? err : -EIO;
103 return buf;
106 static int snd_cs8427_select_corudata(snd_i2c_device_t *device, int udata)
108 cs8427_t *chip = snd_magic_cast(cs8427_t, device->private_data, return -ENXIO);
109 int err;
111 udata = udata ? CS8427_BSEL : 0;
112 if (udata != (chip->regmap[CS8427_REG_CSDATABUF] & udata)) {
113 chip->regmap[CS8427_REG_CSDATABUF] &= ~CS8427_BSEL;
114 chip->regmap[CS8427_REG_CSDATABUF] |= udata;
115 err = snd_cs8427_reg_write(device, CS8427_REG_CSDATABUF, chip->regmap[CS8427_REG_CSDATABUF]);
116 if (err < 0)
117 return err;
119 return 0;
122 static int snd_cs8427_send_corudata(snd_i2c_device_t *device,
123 int udata,
124 unsigned char *ndata,
125 int count)
127 cs8427_t *chip = snd_magic_cast(cs8427_t, device->private_data, return -ENXIO);
128 char *hw_data = udata ? chip->playback.hw_udata : chip->playback.hw_status;
129 char data[32];
130 int err, idx;
132 if (!memcmp(hw_data, ndata, count))
133 return 0;
134 if ((err = snd_cs8427_select_corudata(device, udata)) < 0)
135 return err;
136 memcpy(hw_data, data, count);
137 if (udata) {
138 memset(data, 0, sizeof(data));
139 if (memcmp(hw_data, data, 32) == 0) {
140 chip->regmap[CS8427_REG_UDATABUF] &= ~CS8427_UBMMASK;
141 chip->regmap[CS8427_REG_UDATABUF] |= CS8427_UBMZEROS | CS8427_EFTUI;
142 if ((err = snd_cs8427_reg_write(device, CS8427_REG_UDATABUF, chip->regmap[CS8427_REG_UDATABUF])) < 0)
143 return err;
144 return 0;
147 data[0] = CS8427_REG_AUTOINC | CS8427_REG_CORU_DATABUF;
148 for (idx = 0; idx < count; idx++)
149 data[idx + 1] = swapbits(ndata[idx]);
150 if (snd_i2c_sendbytes(device, data, count) != count)
151 return -EIO;
152 return 1;
155 static void snd_cs8427_free(snd_i2c_device_t *device)
157 if (device->private_data)
158 snd_magic_kfree(device->private_data);
161 int snd_cs8427_create(snd_i2c_bus_t *bus,
162 unsigned char addr,
163 snd_i2c_device_t **r_cs8427)
165 static unsigned char initvals1[] = {
166 CS8427_REG_CONTROL1 | CS8427_REG_AUTOINC,
167 /* CS8427_REG_CLOCKSOURCE: RMCK to OMCK, no validity, disable mutes, TCBL=output */
168 CS8427_SWCLK,
169 /* CS8427_REG_CONTROL2: hold last valid audio sample, RMCK=256*Fs, normal stereo operation */
170 0x00,
171 /* CS8427_REG_DATAFLOW: output drivers normal operation, Tx<=serial, Rx=>serial */
172 CS8427_TXDSERIAL | CS8427_SPDAES3RECEIVER,
173 /* CS8427_REG_CLOCKSOURCE: Run off, CMCK=256*Fs, output time base = OMCK, input time base =
174 covered input clock, recovered input clock source is Envy24 */
175 CS8427_INC,
176 /* CS8427_REG_SERIALINPUT: Serial audio input port data format = I2S, 24-bit, 64*Fsi */
177 CS8427_SIDEL | CS8427_SILRPOL,
178 /* CS8427_REG_SERIALOUTPUT: Serial audio output port data format = I2S, 24-bit, 64*Fsi */
179 CS8427_SODEL | CS8427_SOLRPOL,
181 static unsigned char initvals2[] = {
182 CS8427_REG_RECVERRMASK | CS8427_REG_AUTOINC,
183 /* CS8427_REG_RECVERRMASK: unmask the input PLL clock, V, confidence, biphase, parity status bits */
184 /* CS8427_UNLOCK | CS8427_V | CS8427_CONF | CS8427_BIP | CS8427_PAR,
185 Why setting CS8427_V causes clicks and glitches? */
186 CS8427_UNLOCK | CS8427_CONF | CS8427_BIP | CS8427_PAR,
187 /* CS8427_REG_CSDATABUF:
188 Registers 32-55 window to CS buffer
189 Inhibit D->E transfers from overwriting first 5 bytes of CS data.
190 Inhibit D->E transfers (all) of CS data.
191 Allow E->F transfer of CS data.
192 One byte mode; both A/B channels get same written CB data.
193 A channel info is output to chip's EMPH* pin. */
194 CS8427_CBMR | CS8427_DETCI,
195 /* CS8427_REG_UDATABUF:
196 Use internal buffer to transmit User (U) data.
197 Chip's U pin is an output.
198 Transmit all O's for user data.
199 Inhibit D->E transfers.
200 Inhibit E->F transfers. */
201 CS8427_UD | CS8427_EFTUI | CS8427_DETUI,
203 int err;
204 cs8427_t *chip;
205 snd_i2c_device_t *device;
206 unsigned char buf[32 + 1];
208 if ((err = snd_i2c_device_create(bus, "CS8427", CS8427_ADDR | (addr & 7), &device)) < 0)
209 return err;
210 chip = device->private_data = snd_magic_kcalloc(cs8427_t, 0, GFP_KERNEL);
211 if (chip == NULL) {
212 snd_i2c_device_free(device);
213 return -ENOMEM;
215 device->private_free = snd_cs8427_free;
217 snd_i2c_lock(bus);
218 if ((err = snd_cs8427_reg_read(device, CS8427_REG_ID_AND_VER)) != CS8427_VER8427A) {
219 snd_i2c_unlock(bus);
220 snd_printk("unable to find CS8427 signature (expected 0x%x, read 0x%x), initialization is not completed\n", CS8427_VER8427A, err);
221 return -EFAULT;
223 /* turn off run bit while making changes to configuration */
224 if ((err = snd_cs8427_reg_write(device, CS8427_REG_CLOCKSOURCE, 0x00)) < 0)
225 goto __fail;
226 /* send initial values */
227 memcpy(chip->regmap + (initvals1[0] & 0x7f), initvals1 + 1, 6);
228 if ((err = snd_i2c_sendbytes(device, initvals1, 7)) != 7) {
229 err = err < 0 ? err : -EIO;
230 goto __fail;
232 /* Turn off CS8427 interrupt stuff that is not used in hardware */
233 memset(buf, 0, 7);
234 /* from address 9 to 15 */
235 buf[0] = 9; /* register */
236 if ((err = snd_i2c_sendbytes(device, buf, 7)) != 7)
237 goto __fail;
238 /* send transfer initialization sequence */
239 memcpy(chip->regmap + (initvals2[0] & 0x7f), initvals2 + 1, 3);
240 if ((err = snd_i2c_sendbytes(device, initvals2, 4)) != 4) {
241 err = err < 0 ? err : -EIO;
242 goto __fail;
244 /* write default channel status bytes */
245 buf[0] = CS8427_REG_AUTOINC | CS8427_REG_CORU_DATABUF;
246 buf[1] = swapbits((unsigned char)(SNDRV_PCM_DEFAULT_CON_SPDIF >> 0));
247 buf[2] = swapbits((unsigned char)(SNDRV_PCM_DEFAULT_CON_SPDIF >> 8));
248 buf[3] = swapbits((unsigned char)(SNDRV_PCM_DEFAULT_CON_SPDIF >> 16));
249 buf[4] = swapbits((unsigned char)(SNDRV_PCM_DEFAULT_CON_SPDIF >> 24));
250 memset(buf + 5, 0, sizeof(buf)-5);
251 memcpy(chip->playback.def_status, buf + 1, 24);
252 memcpy(chip->playback.pcm_status, buf + 1, 24);
253 if ((err = snd_i2c_sendbytes(device, buf, 33)) != 33)
254 goto __fail;
255 /* turn on run bit and rock'n'roll */
256 chip->regmap[CS8427_REG_CLOCKSOURCE] = initvals1[4] | CS8427_RUN;
257 if ((err = snd_cs8427_reg_write(device, CS8427_REG_CLOCKSOURCE, chip->regmap[CS8427_REG_CLOCKSOURCE])) < 0)
258 goto __fail;
260 #if 0 // it's nice for read tests
262 char buf[128];
263 buf[0] = 0x81;
264 snd_i2c_sendbytes(device, buf, 1);
265 snd_i2c_readbytes(device, buf, 127);
267 #endif
269 snd_i2c_unlock(bus);
270 if (r_cs8427)
271 *r_cs8427 = device;
272 return 0;
274 __fail:
275 snd_i2c_unlock(bus);
276 snd_i2c_device_free(device);
277 return err < 0 ? err : -EIO;
280 static int snd_cs8427_in_status_info(snd_kcontrol_t *kcontrol,
281 snd_ctl_elem_info_t *uinfo)
283 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
284 uinfo->count = 1;
285 uinfo->value.integer.min = 0;
286 uinfo->value.integer.max = 255;
287 return 0;
290 static int snd_cs8427_in_status_get(snd_kcontrol_t *kcontrol,
291 snd_ctl_elem_value_t *ucontrol)
293 snd_i2c_device_t *device = snd_kcontrol_chip(kcontrol);
294 int data;
296 snd_i2c_lock(device->bus);
297 data = snd_cs8427_reg_read(device, 15);
298 snd_i2c_unlock(device->bus);
299 if (data < 0)
300 return data;
301 ucontrol->value.integer.value[0] = data;
302 return 0;
305 static int snd_cs8427_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
307 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
308 uinfo->count = 1;
309 return 0;
312 static int snd_cs8427_spdif_get(snd_kcontrol_t * kcontrol,
313 snd_ctl_elem_value_t * ucontrol)
315 snd_i2c_device_t *device = snd_kcontrol_chip(kcontrol);
316 cs8427_t *chip = snd_magic_cast(cs8427_t, device->private_data, return -ENXIO);
318 snd_i2c_lock(device->bus);
319 memcpy(ucontrol->value.iec958.status, chip->playback.def_status, 23);
320 snd_i2c_unlock(device->bus);
321 return 0;
324 static int snd_cs8427_spdif_put(snd_kcontrol_t * kcontrol,
325 snd_ctl_elem_value_t * ucontrol)
327 snd_i2c_device_t *device = snd_kcontrol_chip(kcontrol);
328 cs8427_t *chip = snd_magic_cast(cs8427_t, device->private_data, return -ENXIO);
329 unsigned char *status = kcontrol->private_value ? chip->playback.pcm_status : chip->playback.def_status;
330 int err, change;
332 snd_i2c_lock(device->bus);
333 change = memcmp(ucontrol->value.iec958.status, status, 23) != 0;
334 memcpy(status, ucontrol->value.iec958.status, 23);
335 if (change && (kcontrol->private_value ? chip->playback.substream != NULL : chip->playback.substream == NULL)) {
336 err = snd_cs8427_send_corudata(device, 0, status, 23);
337 if (err < 0)
338 change = err;
340 snd_i2c_unlock(device->bus);
341 return change;
344 static int snd_cs8427_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
346 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
347 uinfo->count = 1;
348 return 0;
351 static int snd_cs8427_spdif_mask_get(snd_kcontrol_t * kcontrol,
352 snd_ctl_elem_value_t * ucontrol)
354 memset(ucontrol->value.iec958.status, 0xff, 23);
355 return 0;
358 #define CONTROLS (sizeof(snd_cs8427_iec958_controls)/sizeof(snd_kcontrol_new_t))
360 static snd_kcontrol_new_t snd_cs8427_iec958_controls[] = {
362 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
363 .info = snd_cs8427_in_status_info,
364 .name = "IEC958 CS8427 Input Status",
365 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
366 .get = snd_cs8427_in_status_get,
369 .access = SNDRV_CTL_ELEM_ACCESS_READ,
370 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
371 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
372 .info = snd_cs8427_spdif_mask_info,
373 .get = snd_cs8427_spdif_mask_get,
376 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
377 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
378 .info = snd_cs8427_spdif_info,
379 .get = snd_cs8427_spdif_get,
380 .put = snd_cs8427_spdif_put,
381 .private_value = 0
384 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
385 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
386 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
387 .info = snd_cs8427_spdif_info,
388 .get = snd_cs8427_spdif_get,
389 .put = snd_cs8427_spdif_put,
390 .private_value = 1
393 int snd_cs8427_iec958_build(snd_i2c_device_t *cs8427,
394 snd_pcm_substream_t *play_substream,
395 snd_pcm_substream_t *cap_substream)
397 cs8427_t *chip = snd_magic_cast(cs8427_t, cs8427->private_data, return -ENXIO);
398 snd_kcontrol_t *kctl;
399 unsigned int idx;
400 int err;
402 snd_assert(play_substream && cap_substream, return -EINVAL);
403 for (idx = 0; idx < CONTROLS; idx++) {
404 kctl = snd_ctl_new1(&snd_cs8427_iec958_controls[idx], cs8427);
405 if (kctl == NULL)
406 return -ENOMEM;
407 kctl->id.device = play_substream->pcm->device;
408 kctl->id.subdevice = play_substream->number;
409 err = snd_ctl_add(cs8427->bus->card, kctl);
410 if (err < 0)
411 return err;
412 if (!strcmp(kctl->id.name, SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM)))
413 chip->playback.pcm_ctl = kctl;
416 snd_assert(chip->playback.pcm_ctl, return -EIO);
417 return 0;
420 int snd_cs8427_iec958_active(snd_i2c_device_t *cs8427, int active)
422 cs8427_t *chip;
424 snd_assert(cs8427, return -ENXIO);
425 chip = snd_magic_cast(cs8427_t, cs8427->private_data, return -ENXIO);
426 if (active)
427 memcpy(chip->playback.pcm_status, chip->playback.def_status, 24);
428 chip->playback.pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
429 snd_ctl_notify(cs8427->bus->card, SNDRV_CTL_EVENT_MASK_VALUE |
430 SNDRV_CTL_EVENT_MASK_INFO, &chip->playback.pcm_ctl->id);
431 return 0;
434 int snd_cs8427_iec958_pcm(snd_i2c_device_t *cs8427, unsigned int rate)
436 cs8427_t *chip;
437 char *status;
438 int err;
440 snd_assert(cs8427, return -ENXIO);
441 chip = snd_magic_cast(cs8427_t, cs8427->private_data, return -ENXIO);
442 status = chip->playback.pcm_status;
443 snd_i2c_lock(cs8427->bus);
444 if (status[0] & IEC958_AES0_PROFESSIONAL) {
445 status[0] &= ~IEC958_AES0_PRO_FS;
446 switch (rate) {
447 case 32000: status[0] |= IEC958_AES0_PRO_FS_32000; break;
448 case 44100: status[0] |= IEC958_AES0_PRO_FS_44100; break;
449 case 48000: status[0] |= IEC958_AES0_PRO_FS_48000; break;
450 default: status[0] |= IEC958_AES0_PRO_FS_NOTID; break;
452 } else {
453 status[3] &= ~IEC958_AES3_CON_FS;
454 switch (rate) {
455 case 32000: status[3] |= IEC958_AES3_CON_FS_32000; break;
456 case 44100: status[3] |= IEC958_AES3_CON_FS_44100; break;
457 case 48000: status[3] |= IEC958_AES3_CON_FS_48000; break;
460 err = snd_cs8427_send_corudata(cs8427, 0, status, 23);
461 if (err > 0)
462 snd_ctl_notify(cs8427->bus->card,
463 SNDRV_CTL_EVENT_MASK_VALUE,
464 &chip->playback.pcm_ctl->id);
465 snd_i2c_unlock(cs8427->bus);
466 return err < 0 ? err : 0;
469 EXPORT_SYMBOL(snd_cs8427_detect);
470 EXPORT_SYMBOL(snd_cs8427_create);
471 EXPORT_SYMBOL(snd_cs8427_iec958_build);
472 EXPORT_SYMBOL(snd_cs8427_iec958_active);
473 EXPORT_SYMBOL(snd_cs8427_iec958_pcm);