[PATCH] w1: Adds a sysfs entry (w1_master_search) that allows you to disable/enable...
[linux-2.6/verdex.git] / sound / i2c / i2c.c
blobe8fa7e1a68e8da63f2e54f70d8cf5ebcf661dbe2
1 /*
2 * Generic i2c interface for ALSA
4 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
5 * Modified for the ALSA driver 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/init.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <sound/core.h>
29 #include <sound/i2c.h>
31 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
32 MODULE_DESCRIPTION("Generic i2c interface for ALSA");
33 MODULE_LICENSE("GPL");
35 static int snd_i2c_bit_sendbytes(snd_i2c_device_t *device, unsigned char *bytes, int count);
36 static int snd_i2c_bit_readbytes(snd_i2c_device_t *device, unsigned char *bytes, int count);
37 static int snd_i2c_bit_probeaddr(snd_i2c_bus_t *bus, unsigned short addr);
39 static snd_i2c_ops_t snd_i2c_bit_ops = {
40 .sendbytes = snd_i2c_bit_sendbytes,
41 .readbytes = snd_i2c_bit_readbytes,
42 .probeaddr = snd_i2c_bit_probeaddr,
45 static int snd_i2c_bus_free(snd_i2c_bus_t *bus)
47 snd_i2c_bus_t *slave;
48 snd_i2c_device_t *device;
50 snd_assert(bus != NULL, return -EINVAL);
51 while (!list_empty(&bus->devices)) {
52 device = snd_i2c_device(bus->devices.next);
53 snd_i2c_device_free(device);
55 if (bus->master)
56 list_del(&bus->buses);
57 else {
58 while (!list_empty(&bus->buses)) {
59 slave = snd_i2c_slave_bus(bus->buses.next);
60 snd_device_free(bus->card, slave);
63 if (bus->private_free)
64 bus->private_free(bus);
65 kfree(bus);
66 return 0;
69 static int snd_i2c_bus_dev_free(snd_device_t *device)
71 snd_i2c_bus_t *bus = device->device_data;
72 return snd_i2c_bus_free(bus);
75 int snd_i2c_bus_create(snd_card_t *card, const char *name, snd_i2c_bus_t *master, snd_i2c_bus_t **ri2c)
77 snd_i2c_bus_t *bus;
78 int err;
79 static snd_device_ops_t ops = {
80 .dev_free = snd_i2c_bus_dev_free,
83 *ri2c = NULL;
84 bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
85 if (bus == NULL)
86 return -ENOMEM;
87 init_MUTEX(&bus->lock_mutex);
88 INIT_LIST_HEAD(&bus->devices);
89 INIT_LIST_HEAD(&bus->buses);
90 bus->card = card;
91 bus->ops = &snd_i2c_bit_ops;
92 if (master) {
93 list_add_tail(&bus->buses, &master->buses);
94 bus->master = master;
96 strlcpy(bus->name, name, sizeof(bus->name));
97 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops)) < 0) {
98 snd_i2c_bus_free(bus);
99 return err;
101 *ri2c = bus;
102 return 0;
105 int snd_i2c_device_create(snd_i2c_bus_t *bus, const char *name, unsigned char addr, snd_i2c_device_t **rdevice)
107 snd_i2c_device_t *device;
109 *rdevice = NULL;
110 snd_assert(bus != NULL, return -EINVAL);
111 device = kcalloc(1, sizeof(*device), GFP_KERNEL);
112 if (device == NULL)
113 return -ENOMEM;
114 device->addr = addr;
115 strlcpy(device->name, name, sizeof(device->name));
116 list_add_tail(&device->list, &bus->devices);
117 device->bus = bus;
118 *rdevice = device;
119 return 0;
122 int snd_i2c_device_free(snd_i2c_device_t *device)
124 if (device->bus)
125 list_del(&device->list);
126 if (device->private_free)
127 device->private_free(device);
128 kfree(device);
129 return 0;
132 int snd_i2c_sendbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
134 return device->bus->ops->sendbytes(device, bytes, count);
138 int snd_i2c_readbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
140 return device->bus->ops->readbytes(device, bytes, count);
143 int snd_i2c_probeaddr(snd_i2c_bus_t *bus, unsigned short addr)
145 return bus->ops->probeaddr(bus, addr);
149 * bit-operations
152 static inline void snd_i2c_bit_hw_start(snd_i2c_bus_t *bus)
154 if (bus->hw_ops.bit->start)
155 bus->hw_ops.bit->start(bus);
158 static inline void snd_i2c_bit_hw_stop(snd_i2c_bus_t *bus)
160 if (bus->hw_ops.bit->stop)
161 bus->hw_ops.bit->stop(bus);
164 static void snd_i2c_bit_direction(snd_i2c_bus_t *bus, int clock, int data)
166 if (bus->hw_ops.bit->direction)
167 bus->hw_ops.bit->direction(bus, clock, data);
170 static void snd_i2c_bit_set(snd_i2c_bus_t *bus, int clock, int data)
172 bus->hw_ops.bit->setlines(bus, clock, data);
175 #if 0
176 static int snd_i2c_bit_clock(snd_i2c_bus_t *bus)
178 if (bus->hw_ops.bit->getclock)
179 return bus->hw_ops.bit->getclock(bus);
180 return -ENXIO;
182 #endif
184 static int snd_i2c_bit_data(snd_i2c_bus_t *bus, int ack)
186 return bus->hw_ops.bit->getdata(bus, ack);
189 static void snd_i2c_bit_start(snd_i2c_bus_t *bus)
191 snd_i2c_bit_hw_start(bus);
192 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
193 snd_i2c_bit_set(bus, 1, 1);
194 snd_i2c_bit_set(bus, 1, 0);
195 snd_i2c_bit_set(bus, 0, 0);
198 static void snd_i2c_bit_stop(snd_i2c_bus_t *bus)
200 snd_i2c_bit_set(bus, 0, 0);
201 snd_i2c_bit_set(bus, 1, 0);
202 snd_i2c_bit_set(bus, 1, 1);
203 snd_i2c_bit_hw_stop(bus);
206 static void snd_i2c_bit_send(snd_i2c_bus_t *bus, int data)
208 snd_i2c_bit_set(bus, 0, data);
209 snd_i2c_bit_set(bus, 1, data);
210 snd_i2c_bit_set(bus, 0, data);
213 static int snd_i2c_bit_ack(snd_i2c_bus_t *bus)
215 int ack;
217 snd_i2c_bit_set(bus, 0, 1);
218 snd_i2c_bit_set(bus, 1, 1);
219 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
220 ack = snd_i2c_bit_data(bus, 1);
221 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
222 snd_i2c_bit_set(bus, 0, 1);
223 return ack ? -EIO : 0;
226 static int snd_i2c_bit_sendbyte(snd_i2c_bus_t *bus, unsigned char data)
228 int i, err;
230 for (i = 7; i >= 0; i--)
231 snd_i2c_bit_send(bus, !!(data & (1 << i)));
232 if ((err = snd_i2c_bit_ack(bus)) < 0)
233 return err;
234 return 0;
237 static int snd_i2c_bit_readbyte(snd_i2c_bus_t *bus, int last)
239 int i;
240 unsigned char data = 0;
242 snd_i2c_bit_set(bus, 0, 1);
243 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
244 for (i = 7; i >= 0; i--) {
245 snd_i2c_bit_set(bus, 1, 1);
246 if (snd_i2c_bit_data(bus, 0))
247 data |= (1 << i);
248 snd_i2c_bit_set(bus, 0, 1);
250 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
251 snd_i2c_bit_send(bus, !!last);
252 return data;
255 static int snd_i2c_bit_sendbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
257 snd_i2c_bus_t *bus = device->bus;
258 int err, res = 0;
260 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
261 return -EIO; /* not yet implemented */
262 snd_i2c_bit_start(bus);
263 if ((err = snd_i2c_bit_sendbyte(bus, device->addr << 1)) < 0) {
264 snd_i2c_bit_hw_stop(bus);
265 return err;
267 while (count-- > 0) {
268 if ((err = snd_i2c_bit_sendbyte(bus, *bytes++)) < 0) {
269 snd_i2c_bit_hw_stop(bus);
270 return err;
272 res++;
274 snd_i2c_bit_stop(bus);
275 return res;
278 static int snd_i2c_bit_readbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
280 snd_i2c_bus_t *bus = device->bus;
281 int err, res = 0;
283 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
284 return -EIO; /* not yet implemented */
285 snd_i2c_bit_start(bus);
286 if ((err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1)) < 0) {
287 snd_i2c_bit_hw_stop(bus);
288 return err;
290 while (count-- > 0) {
291 if ((err = snd_i2c_bit_readbyte(bus, count == 0)) < 0) {
292 snd_i2c_bit_hw_stop(bus);
293 return err;
295 *bytes++ = (unsigned char)err;
296 res++;
298 snd_i2c_bit_stop(bus);
299 return res;
302 static int snd_i2c_bit_probeaddr(snd_i2c_bus_t *bus, unsigned short addr)
304 int err;
306 if (addr & 0x8000) /* 10-bit address */
307 return -EIO; /* not yet implemented */
308 if (addr & 0x7f80) /* invalid address */
309 return -EINVAL;
310 snd_i2c_bit_start(bus);
311 err = snd_i2c_bit_sendbyte(bus, addr << 1);
312 snd_i2c_bit_stop(bus);
313 return err;
316 EXPORT_SYMBOL(snd_i2c_bus_create);
317 EXPORT_SYMBOL(snd_i2c_device_create);
318 EXPORT_SYMBOL(snd_i2c_device_free);
319 EXPORT_SYMBOL(snd_i2c_sendbytes);
320 EXPORT_SYMBOL(snd_i2c_readbytes);
321 EXPORT_SYMBOL(snd_i2c_probeaddr);
323 static int __init alsa_i2c_init(void)
325 return 0;
328 static void __exit alsa_i2c_exit(void)
332 module_init(alsa_i2c_init)
333 module_exit(alsa_i2c_exit)