blob: b1e74e40cba050656879dbe529a72adb218397c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic i2c interface for ALSA
3 *
4 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02005 * Modified for the ALSA driver by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020030MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070031MODULE_DESCRIPTION("Generic i2c interface for ALSA");
32MODULE_LICENSE("GPL");
33
Takashi Iwai97f02e02005-11-17 14:17:19 +010034static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai97f02e02005-11-17 14:17:19 +010041static struct snd_i2c_ops snd_i2c_bit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .sendbytes = snd_i2c_bit_sendbytes,
43 .readbytes = snd_i2c_bit_readbytes,
44 .probeaddr = snd_i2c_bit_probeaddr,
45};
46
Takashi Iwai97f02e02005-11-17 14:17:19 +010047static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Takashi Iwai97f02e02005-11-17 14:17:19 +010049 struct snd_i2c_bus *slave;
50 struct snd_i2c_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 snd_assert(bus != NULL, return -EINVAL);
53 while (!list_empty(&bus->devices)) {
54 device = snd_i2c_device(bus->devices.next);
55 snd_i2c_device_free(device);
56 }
57 if (bus->master)
58 list_del(&bus->buses);
59 else {
60 while (!list_empty(&bus->buses)) {
61 slave = snd_i2c_slave_bus(bus->buses.next);
62 snd_device_free(bus->card, slave);
63 }
64 }
65 if (bus->private_free)
66 bus->private_free(bus);
67 kfree(bus);
68 return 0;
69}
70
Takashi Iwai97f02e02005-11-17 14:17:19 +010071static int snd_i2c_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Takashi Iwai97f02e02005-11-17 14:17:19 +010073 struct snd_i2c_bus *bus = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return snd_i2c_bus_free(bus);
75}
76
Takashi Iwai97f02e02005-11-17 14:17:19 +010077int snd_i2c_bus_create(struct snd_card *card, const char *name,
78 struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Takashi Iwai97f02e02005-11-17 14:17:19 +010080 struct snd_i2c_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int err;
Takashi Iwai97f02e02005-11-17 14:17:19 +010082 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 .dev_free = snd_i2c_bus_dev_free,
84 };
85
86 *ri2c = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +020087 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (bus == NULL)
89 return -ENOMEM;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010090 mutex_init(&bus->lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 INIT_LIST_HEAD(&bus->devices);
92 INIT_LIST_HEAD(&bus->buses);
93 bus->card = card;
94 bus->ops = &snd_i2c_bit_ops;
95 if (master) {
96 list_add_tail(&bus->buses, &master->buses);
97 bus->master = master;
98 }
99 strlcpy(bus->name, name, sizeof(bus->name));
100 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops)) < 0) {
101 snd_i2c_bus_free(bus);
102 return err;
103 }
104 *ri2c = bus;
105 return 0;
106}
107
Takashi Iwai57c65c12006-04-28 15:13:40 +0200108EXPORT_SYMBOL(snd_i2c_bus_create);
109
Takashi Iwai97f02e02005-11-17 14:17:19 +0100110int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
111 unsigned char addr, struct snd_i2c_device **rdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100113 struct snd_i2c_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 *rdevice = NULL;
116 snd_assert(bus != NULL, return -EINVAL);
Takashi Iwai561b2202005-09-09 14:22:34 +0200117 device = kzalloc(sizeof(*device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (device == NULL)
119 return -ENOMEM;
120 device->addr = addr;
121 strlcpy(device->name, name, sizeof(device->name));
122 list_add_tail(&device->list, &bus->devices);
123 device->bus = bus;
124 *rdevice = device;
125 return 0;
126}
127
Takashi Iwai57c65c12006-04-28 15:13:40 +0200128EXPORT_SYMBOL(snd_i2c_device_create);
129
Takashi Iwai97f02e02005-11-17 14:17:19 +0100130int snd_i2c_device_free(struct snd_i2c_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 if (device->bus)
133 list_del(&device->list);
134 if (device->private_free)
135 device->private_free(device);
136 kfree(device);
137 return 0;
138}
139
Takashi Iwai57c65c12006-04-28 15:13:40 +0200140EXPORT_SYMBOL(snd_i2c_device_free);
141
Takashi Iwai97f02e02005-11-17 14:17:19 +0100142int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 return device->bus->ops->sendbytes(device, bytes, count);
145}
146
Takashi Iwai57c65c12006-04-28 15:13:40 +0200147EXPORT_SYMBOL(snd_i2c_sendbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Takashi Iwai97f02e02005-11-17 14:17:19 +0100149int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 return device->bus->ops->readbytes(device, bytes, count);
152}
153
Takashi Iwai57c65c12006-04-28 15:13:40 +0200154EXPORT_SYMBOL(snd_i2c_readbytes);
155
Takashi Iwai97f02e02005-11-17 14:17:19 +0100156int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 return bus->ops->probeaddr(bus, addr);
159}
160
Takashi Iwai57c65c12006-04-28 15:13:40 +0200161EXPORT_SYMBOL(snd_i2c_probeaddr);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
164 * bit-operations
165 */
166
Takashi Iwai97f02e02005-11-17 14:17:19 +0100167static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 if (bus->hw_ops.bit->start)
170 bus->hw_ops.bit->start(bus);
171}
172
Takashi Iwai97f02e02005-11-17 14:17:19 +0100173static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 if (bus->hw_ops.bit->stop)
176 bus->hw_ops.bit->stop(bus);
177}
178
Takashi Iwai97f02e02005-11-17 14:17:19 +0100179static void snd_i2c_bit_direction(struct snd_i2c_bus *bus, int clock, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 if (bus->hw_ops.bit->direction)
182 bus->hw_ops.bit->direction(bus, clock, data);
183}
184
Takashi Iwai97f02e02005-11-17 14:17:19 +0100185static void snd_i2c_bit_set(struct snd_i2c_bus *bus, int clock, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 bus->hw_ops.bit->setlines(bus, clock, data);
188}
189
190#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +0100191static int snd_i2c_bit_clock(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 if (bus->hw_ops.bit->getclock)
194 return bus->hw_ops.bit->getclock(bus);
195 return -ENXIO;
196}
197#endif
198
Takashi Iwai97f02e02005-11-17 14:17:19 +0100199static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 return bus->hw_ops.bit->getdata(bus, ack);
202}
203
Takashi Iwai97f02e02005-11-17 14:17:19 +0100204static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 snd_i2c_bit_hw_start(bus);
207 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
208 snd_i2c_bit_set(bus, 1, 1);
209 snd_i2c_bit_set(bus, 1, 0);
210 snd_i2c_bit_set(bus, 0, 0);
211}
212
Takashi Iwai97f02e02005-11-17 14:17:19 +0100213static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 snd_i2c_bit_set(bus, 0, 0);
216 snd_i2c_bit_set(bus, 1, 0);
217 snd_i2c_bit_set(bus, 1, 1);
218 snd_i2c_bit_hw_stop(bus);
219}
220
Takashi Iwai97f02e02005-11-17 14:17:19 +0100221static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 snd_i2c_bit_set(bus, 0, data);
224 snd_i2c_bit_set(bus, 1, data);
225 snd_i2c_bit_set(bus, 0, data);
226}
227
Takashi Iwai97f02e02005-11-17 14:17:19 +0100228static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 int ack;
231
232 snd_i2c_bit_set(bus, 0, 1);
233 snd_i2c_bit_set(bus, 1, 1);
234 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
235 ack = snd_i2c_bit_data(bus, 1);
236 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
237 snd_i2c_bit_set(bus, 0, 1);
238 return ack ? -EIO : 0;
239}
240
Takashi Iwai97f02e02005-11-17 14:17:19 +0100241static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 int i, err;
244
245 for (i = 7; i >= 0; i--)
246 snd_i2c_bit_send(bus, !!(data & (1 << i)));
247 if ((err = snd_i2c_bit_ack(bus)) < 0)
248 return err;
249 return 0;
250}
251
Takashi Iwai97f02e02005-11-17 14:17:19 +0100252static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 int i;
255 unsigned char data = 0;
256
257 snd_i2c_bit_set(bus, 0, 1);
258 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
259 for (i = 7; i >= 0; i--) {
260 snd_i2c_bit_set(bus, 1, 1);
261 if (snd_i2c_bit_data(bus, 0))
262 data |= (1 << i);
263 snd_i2c_bit_set(bus, 0, 1);
264 }
265 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
266 snd_i2c_bit_send(bus, !!last);
267 return data;
268}
269
Takashi Iwai97f02e02005-11-17 14:17:19 +0100270static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
271 unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100273 struct snd_i2c_bus *bus = device->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int err, res = 0;
275
276 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
277 return -EIO; /* not yet implemented */
278 snd_i2c_bit_start(bus);
279 if ((err = snd_i2c_bit_sendbyte(bus, device->addr << 1)) < 0) {
280 snd_i2c_bit_hw_stop(bus);
281 return err;
282 }
283 while (count-- > 0) {
284 if ((err = snd_i2c_bit_sendbyte(bus, *bytes++)) < 0) {
285 snd_i2c_bit_hw_stop(bus);
286 return err;
287 }
288 res++;
289 }
290 snd_i2c_bit_stop(bus);
291 return res;
292}
293
Takashi Iwai97f02e02005-11-17 14:17:19 +0100294static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
295 unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100297 struct snd_i2c_bus *bus = device->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int err, res = 0;
299
300 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
301 return -EIO; /* not yet implemented */
302 snd_i2c_bit_start(bus);
303 if ((err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1)) < 0) {
304 snd_i2c_bit_hw_stop(bus);
305 return err;
306 }
307 while (count-- > 0) {
308 if ((err = snd_i2c_bit_readbyte(bus, count == 0)) < 0) {
309 snd_i2c_bit_hw_stop(bus);
310 return err;
311 }
312 *bytes++ = (unsigned char)err;
313 res++;
314 }
315 snd_i2c_bit_stop(bus);
316 return res;
317}
318
Takashi Iwai97f02e02005-11-17 14:17:19 +0100319static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int err;
322
323 if (addr & 0x8000) /* 10-bit address */
324 return -EIO; /* not yet implemented */
325 if (addr & 0x7f80) /* invalid address */
326 return -EINVAL;
327 snd_i2c_bit_start(bus);
328 err = snd_i2c_bit_sendbyte(bus, addr << 1);
329 snd_i2c_bit_stop(bus);
330 return err;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334static int __init alsa_i2c_init(void)
335{
336 return 0;
337}
338
339static void __exit alsa_i2c_exit(void)
340{
341}
342
343module_init(alsa_i2c_init)
344module_exit(alsa_i2c_exit)