Merge branch 'linus' into cpumask-for-linus
[linux-block.git] / sound / i2c / i2c.c
CommitLineData
1da177e4
LT
1/*
2 * Generic i2c interface for ALSA
3 *
4 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
c1017a4c 5 * Modified for the ALSA driver by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
1da177e4
LT
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/string.h>
26#include <linux/errno.h>
27#include <sound/core.h>
28#include <sound/i2c.h>
29
c1017a4c 30MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
31MODULE_DESCRIPTION("Generic i2c interface for ALSA");
32MODULE_LICENSE("GPL");
33
97f02e05
TI
34static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
35 unsigned char *bytes, int count);
36static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
37 unsigned char *bytes, int count);
38static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus,
39 unsigned short addr);
40
41static struct snd_i2c_ops snd_i2c_bit_ops = {
1da177e4
LT
42 .sendbytes = snd_i2c_bit_sendbytes,
43 .readbytes = snd_i2c_bit_readbytes,
44 .probeaddr = snd_i2c_bit_probeaddr,
45};
46
97f02e05 47static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
1da177e4 48{
97f02e05
TI
49 struct snd_i2c_bus *slave;
50 struct snd_i2c_device *device;
1da177e4 51
5e246b85
TI
52 if (snd_BUG_ON(!bus))
53 return -EINVAL;
1da177e4
LT
54 while (!list_empty(&bus->devices)) {
55 device = snd_i2c_device(bus->devices.next);
56 snd_i2c_device_free(device);
57 }
58 if (bus->master)
59 list_del(&bus->buses);
60 else {
61 while (!list_empty(&bus->buses)) {
62 slave = snd_i2c_slave_bus(bus->buses.next);
63 snd_device_free(bus->card, slave);
64 }
65 }
66 if (bus->private_free)
67 bus->private_free(bus);
68 kfree(bus);
69 return 0;
70}
71
97f02e05 72static int snd_i2c_bus_dev_free(struct snd_device *device)
1da177e4 73{
97f02e05 74 struct snd_i2c_bus *bus = device->device_data;
1da177e4
LT
75 return snd_i2c_bus_free(bus);
76}
77
97f02e05
TI
78int snd_i2c_bus_create(struct snd_card *card, const char *name,
79 struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
1da177e4 80{
97f02e05 81 struct snd_i2c_bus *bus;
1da177e4 82 int err;
97f02e05 83 static struct snd_device_ops ops = {
1da177e4
LT
84 .dev_free = snd_i2c_bus_dev_free,
85 };
86
87 *ri2c = NULL;
561b220a 88 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1da177e4
LT
89 if (bus == NULL)
90 return -ENOMEM;
ef9f0a42 91 mutex_init(&bus->lock_mutex);
1da177e4
LT
92 INIT_LIST_HEAD(&bus->devices);
93 INIT_LIST_HEAD(&bus->buses);
94 bus->card = card;
95 bus->ops = &snd_i2c_bit_ops;
96 if (master) {
97 list_add_tail(&bus->buses, &master->buses);
98 bus->master = master;
99 }
100 strlcpy(bus->name, name, sizeof(bus->name));
101 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops)) < 0) {
102 snd_i2c_bus_free(bus);
103 return err;
104 }
105 *ri2c = bus;
106 return 0;
107}
108
57c65c11
TI
109EXPORT_SYMBOL(snd_i2c_bus_create);
110
97f02e05
TI
111int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
112 unsigned char addr, struct snd_i2c_device **rdevice)
1da177e4 113{
97f02e05 114 struct snd_i2c_device *device;
1da177e4
LT
115
116 *rdevice = NULL;
5e246b85
TI
117 if (snd_BUG_ON(!bus))
118 return -EINVAL;
561b220a 119 device = kzalloc(sizeof(*device), GFP_KERNEL);
1da177e4
LT
120 if (device == NULL)
121 return -ENOMEM;
122 device->addr = addr;
123 strlcpy(device->name, name, sizeof(device->name));
124 list_add_tail(&device->list, &bus->devices);
125 device->bus = bus;
126 *rdevice = device;
127 return 0;
128}
129
57c65c11
TI
130EXPORT_SYMBOL(snd_i2c_device_create);
131
97f02e05 132int snd_i2c_device_free(struct snd_i2c_device *device)
1da177e4
LT
133{
134 if (device->bus)
135 list_del(&device->list);
136 if (device->private_free)
137 device->private_free(device);
138 kfree(device);
139 return 0;
140}
141
57c65c11
TI
142EXPORT_SYMBOL(snd_i2c_device_free);
143
97f02e05 144int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
1da177e4
LT
145{
146 return device->bus->ops->sendbytes(device, bytes, count);
147}
148
57c65c11 149EXPORT_SYMBOL(snd_i2c_sendbytes);
1da177e4 150
97f02e05 151int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
1da177e4
LT
152{
153 return device->bus->ops->readbytes(device, bytes, count);
154}
155
57c65c11
TI
156EXPORT_SYMBOL(snd_i2c_readbytes);
157
97f02e05 158int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
1da177e4
LT
159{
160 return bus->ops->probeaddr(bus, addr);
161}
162
57c65c11
TI
163EXPORT_SYMBOL(snd_i2c_probeaddr);
164
1da177e4
LT
165/*
166 * bit-operations
167 */
168
97f02e05 169static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *bus)
1da177e4
LT
170{
171 if (bus->hw_ops.bit->start)
172 bus->hw_ops.bit->start(bus);
173}
174
97f02e05 175static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus *bus)
1da177e4
LT
176{
177 if (bus->hw_ops.bit->stop)
178 bus->hw_ops.bit->stop(bus);
179}
180
97f02e05 181static void snd_i2c_bit_direction(struct snd_i2c_bus *bus, int clock, int data)
1da177e4
LT
182{
183 if (bus->hw_ops.bit->direction)
184 bus->hw_ops.bit->direction(bus, clock, data);
185}
186
97f02e05 187static void snd_i2c_bit_set(struct snd_i2c_bus *bus, int clock, int data)
1da177e4
LT
188{
189 bus->hw_ops.bit->setlines(bus, clock, data);
190}
191
192#if 0
97f02e05 193static int snd_i2c_bit_clock(struct snd_i2c_bus *bus)
1da177e4
LT
194{
195 if (bus->hw_ops.bit->getclock)
196 return bus->hw_ops.bit->getclock(bus);
197 return -ENXIO;
198}
199#endif
200
97f02e05 201static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
1da177e4
LT
202{
203 return bus->hw_ops.bit->getdata(bus, ack);
204}
205
97f02e05 206static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
1da177e4
LT
207{
208 snd_i2c_bit_hw_start(bus);
209 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
210 snd_i2c_bit_set(bus, 1, 1);
211 snd_i2c_bit_set(bus, 1, 0);
212 snd_i2c_bit_set(bus, 0, 0);
213}
214
97f02e05 215static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
1da177e4
LT
216{
217 snd_i2c_bit_set(bus, 0, 0);
218 snd_i2c_bit_set(bus, 1, 0);
219 snd_i2c_bit_set(bus, 1, 1);
220 snd_i2c_bit_hw_stop(bus);
221}
222
97f02e05 223static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
1da177e4
LT
224{
225 snd_i2c_bit_set(bus, 0, data);
226 snd_i2c_bit_set(bus, 1, data);
227 snd_i2c_bit_set(bus, 0, data);
228}
229
97f02e05 230static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
1da177e4
LT
231{
232 int ack;
233
234 snd_i2c_bit_set(bus, 0, 1);
235 snd_i2c_bit_set(bus, 1, 1);
236 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
237 ack = snd_i2c_bit_data(bus, 1);
238 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
239 snd_i2c_bit_set(bus, 0, 1);
240 return ack ? -EIO : 0;
241}
242
97f02e05 243static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
1da177e4
LT
244{
245 int i, err;
246
247 for (i = 7; i >= 0; i--)
248 snd_i2c_bit_send(bus, !!(data & (1 << i)));
249 if ((err = snd_i2c_bit_ack(bus)) < 0)
250 return err;
251 return 0;
252}
253
97f02e05 254static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
1da177e4
LT
255{
256 int i;
257 unsigned char data = 0;
258
259 snd_i2c_bit_set(bus, 0, 1);
260 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
261 for (i = 7; i >= 0; i--) {
262 snd_i2c_bit_set(bus, 1, 1);
263 if (snd_i2c_bit_data(bus, 0))
264 data |= (1 << i);
265 snd_i2c_bit_set(bus, 0, 1);
266 }
267 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
268 snd_i2c_bit_send(bus, !!last);
269 return data;
270}
271
97f02e05
TI
272static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
273 unsigned char *bytes, int count)
1da177e4 274{
97f02e05 275 struct snd_i2c_bus *bus = device->bus;
1da177e4
LT
276 int err, res = 0;
277
278 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
279 return -EIO; /* not yet implemented */
280 snd_i2c_bit_start(bus);
281 if ((err = snd_i2c_bit_sendbyte(bus, device->addr << 1)) < 0) {
282 snd_i2c_bit_hw_stop(bus);
283 return err;
284 }
285 while (count-- > 0) {
286 if ((err = snd_i2c_bit_sendbyte(bus, *bytes++)) < 0) {
287 snd_i2c_bit_hw_stop(bus);
288 return err;
289 }
290 res++;
291 }
292 snd_i2c_bit_stop(bus);
293 return res;
294}
295
97f02e05
TI
296static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
297 unsigned char *bytes, int count)
1da177e4 298{
97f02e05 299 struct snd_i2c_bus *bus = device->bus;
1da177e4
LT
300 int err, res = 0;
301
302 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
303 return -EIO; /* not yet implemented */
304 snd_i2c_bit_start(bus);
305 if ((err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1)) < 0) {
306 snd_i2c_bit_hw_stop(bus);
307 return err;
308 }
309 while (count-- > 0) {
310 if ((err = snd_i2c_bit_readbyte(bus, count == 0)) < 0) {
311 snd_i2c_bit_hw_stop(bus);
312 return err;
313 }
314 *bytes++ = (unsigned char)err;
315 res++;
316 }
317 snd_i2c_bit_stop(bus);
318 return res;
319}
320
97f02e05 321static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
1da177e4
LT
322{
323 int err;
324
325 if (addr & 0x8000) /* 10-bit address */
326 return -EIO; /* not yet implemented */
327 if (addr & 0x7f80) /* invalid address */
328 return -EINVAL;
329 snd_i2c_bit_start(bus);
330 err = snd_i2c_bit_sendbyte(bus, addr << 1);
331 snd_i2c_bit_stop(bus);
332 return err;
333}
334
1da177e4
LT
335
336static int __init alsa_i2c_init(void)
337{
338 return 0;
339}
340
341static void __exit alsa_i2c_exit(void)
342{
343}
344
345module_init(alsa_i2c_init)
346module_exit(alsa_i2c_exit)