blob: edfe76fb007461dc425acd8663e40d8193950174 [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>
5 * Modified for the ALSA driver by Jaroslav Kysela <perex@suse.cz>
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
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>
30
31MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
32MODULE_DESCRIPTION("Generic i2c interface for ALSA");
33MODULE_LICENSE("GPL");
34
Takashi Iwai97f02e02005-11-17 14:17:19 +010035static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
36 unsigned char *bytes, int count);
37static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
38 unsigned char *bytes, int count);
39static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus,
40 unsigned short addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai97f02e02005-11-17 14:17:19 +010042static struct snd_i2c_ops snd_i2c_bit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .sendbytes = snd_i2c_bit_sendbytes,
44 .readbytes = snd_i2c_bit_readbytes,
45 .probeaddr = snd_i2c_bit_probeaddr,
46};
47
Takashi Iwai97f02e02005-11-17 14:17:19 +010048static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Takashi Iwai97f02e02005-11-17 14:17:19 +010050 struct snd_i2c_bus *slave;
51 struct snd_i2c_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 snd_assert(bus != NULL, return -EINVAL);
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
Takashi Iwai97f02e02005-11-17 14:17:19 +010072static int snd_i2c_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Takashi Iwai97f02e02005-11-17 14:17:19 +010074 struct snd_i2c_bus *bus = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return snd_i2c_bus_free(bus);
76}
77
Takashi Iwai97f02e02005-11-17 14:17:19 +010078int snd_i2c_bus_create(struct snd_card *card, const char *name,
79 struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Takashi Iwai97f02e02005-11-17 14:17:19 +010081 struct snd_i2c_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 int err;
Takashi Iwai97f02e02005-11-17 14:17:19 +010083 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .dev_free = snd_i2c_bus_dev_free,
85 };
86
87 *ri2c = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +020088 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (bus == NULL)
90 return -ENOMEM;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010091 mutex_init(&bus->lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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
Takashi Iwai97f02e02005-11-17 14:17:19 +0100109int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
110 unsigned char addr, struct snd_i2c_device **rdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100112 struct snd_i2c_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 *rdevice = NULL;
115 snd_assert(bus != NULL, return -EINVAL);
Takashi Iwai561b2202005-09-09 14:22:34 +0200116 device = kzalloc(sizeof(*device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (device == NULL)
118 return -ENOMEM;
119 device->addr = addr;
120 strlcpy(device->name, name, sizeof(device->name));
121 list_add_tail(&device->list, &bus->devices);
122 device->bus = bus;
123 *rdevice = device;
124 return 0;
125}
126
Takashi Iwai97f02e02005-11-17 14:17:19 +0100127int snd_i2c_device_free(struct snd_i2c_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 if (device->bus)
130 list_del(&device->list);
131 if (device->private_free)
132 device->private_free(device);
133 kfree(device);
134 return 0;
135}
136
Takashi Iwai97f02e02005-11-17 14:17:19 +0100137int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 return device->bus->ops->sendbytes(device, bytes, count);
140}
141
142
Takashi Iwai97f02e02005-11-17 14:17:19 +0100143int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 return device->bus->ops->readbytes(device, bytes, count);
146}
147
Takashi Iwai97f02e02005-11-17 14:17:19 +0100148int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 return bus->ops->probeaddr(bus, addr);
151}
152
153/*
154 * bit-operations
155 */
156
Takashi Iwai97f02e02005-11-17 14:17:19 +0100157static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 if (bus->hw_ops.bit->start)
160 bus->hw_ops.bit->start(bus);
161}
162
Takashi Iwai97f02e02005-11-17 14:17:19 +0100163static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 if (bus->hw_ops.bit->stop)
166 bus->hw_ops.bit->stop(bus);
167}
168
Takashi Iwai97f02e02005-11-17 14:17:19 +0100169static void snd_i2c_bit_direction(struct snd_i2c_bus *bus, int clock, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 if (bus->hw_ops.bit->direction)
172 bus->hw_ops.bit->direction(bus, clock, data);
173}
174
Takashi Iwai97f02e02005-11-17 14:17:19 +0100175static void snd_i2c_bit_set(struct snd_i2c_bus *bus, int clock, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 bus->hw_ops.bit->setlines(bus, clock, data);
178}
179
180#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +0100181static int snd_i2c_bit_clock(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 if (bus->hw_ops.bit->getclock)
184 return bus->hw_ops.bit->getclock(bus);
185 return -ENXIO;
186}
187#endif
188
Takashi Iwai97f02e02005-11-17 14:17:19 +0100189static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 return bus->hw_ops.bit->getdata(bus, ack);
192}
193
Takashi Iwai97f02e02005-11-17 14:17:19 +0100194static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 snd_i2c_bit_hw_start(bus);
197 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
198 snd_i2c_bit_set(bus, 1, 1);
199 snd_i2c_bit_set(bus, 1, 0);
200 snd_i2c_bit_set(bus, 0, 0);
201}
202
Takashi Iwai97f02e02005-11-17 14:17:19 +0100203static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 snd_i2c_bit_set(bus, 0, 0);
206 snd_i2c_bit_set(bus, 1, 0);
207 snd_i2c_bit_set(bus, 1, 1);
208 snd_i2c_bit_hw_stop(bus);
209}
210
Takashi Iwai97f02e02005-11-17 14:17:19 +0100211static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 snd_i2c_bit_set(bus, 0, data);
214 snd_i2c_bit_set(bus, 1, data);
215 snd_i2c_bit_set(bus, 0, data);
216}
217
Takashi Iwai97f02e02005-11-17 14:17:19 +0100218static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int ack;
221
222 snd_i2c_bit_set(bus, 0, 1);
223 snd_i2c_bit_set(bus, 1, 1);
224 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
225 ack = snd_i2c_bit_data(bus, 1);
226 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
227 snd_i2c_bit_set(bus, 0, 1);
228 return ack ? -EIO : 0;
229}
230
Takashi Iwai97f02e02005-11-17 14:17:19 +0100231static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int i, err;
234
235 for (i = 7; i >= 0; i--)
236 snd_i2c_bit_send(bus, !!(data & (1 << i)));
237 if ((err = snd_i2c_bit_ack(bus)) < 0)
238 return err;
239 return 0;
240}
241
Takashi Iwai97f02e02005-11-17 14:17:19 +0100242static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int i;
245 unsigned char data = 0;
246
247 snd_i2c_bit_set(bus, 0, 1);
248 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
249 for (i = 7; i >= 0; i--) {
250 snd_i2c_bit_set(bus, 1, 1);
251 if (snd_i2c_bit_data(bus, 0))
252 data |= (1 << i);
253 snd_i2c_bit_set(bus, 0, 1);
254 }
255 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
256 snd_i2c_bit_send(bus, !!last);
257 return data;
258}
259
Takashi Iwai97f02e02005-11-17 14:17:19 +0100260static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
261 unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100263 struct snd_i2c_bus *bus = device->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 int err, res = 0;
265
266 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
267 return -EIO; /* not yet implemented */
268 snd_i2c_bit_start(bus);
269 if ((err = snd_i2c_bit_sendbyte(bus, device->addr << 1)) < 0) {
270 snd_i2c_bit_hw_stop(bus);
271 return err;
272 }
273 while (count-- > 0) {
274 if ((err = snd_i2c_bit_sendbyte(bus, *bytes++)) < 0) {
275 snd_i2c_bit_hw_stop(bus);
276 return err;
277 }
278 res++;
279 }
280 snd_i2c_bit_stop(bus);
281 return res;
282}
283
Takashi Iwai97f02e02005-11-17 14:17:19 +0100284static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
285 unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100287 struct snd_i2c_bus *bus = device->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int err, res = 0;
289
290 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
291 return -EIO; /* not yet implemented */
292 snd_i2c_bit_start(bus);
293 if ((err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1)) < 0) {
294 snd_i2c_bit_hw_stop(bus);
295 return err;
296 }
297 while (count-- > 0) {
298 if ((err = snd_i2c_bit_readbyte(bus, count == 0)) < 0) {
299 snd_i2c_bit_hw_stop(bus);
300 return err;
301 }
302 *bytes++ = (unsigned char)err;
303 res++;
304 }
305 snd_i2c_bit_stop(bus);
306 return res;
307}
308
Takashi Iwai97f02e02005-11-17 14:17:19 +0100309static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 int err;
312
313 if (addr & 0x8000) /* 10-bit address */
314 return -EIO; /* not yet implemented */
315 if (addr & 0x7f80) /* invalid address */
316 return -EINVAL;
317 snd_i2c_bit_start(bus);
318 err = snd_i2c_bit_sendbyte(bus, addr << 1);
319 snd_i2c_bit_stop(bus);
320 return err;
321}
322
323EXPORT_SYMBOL(snd_i2c_bus_create);
324EXPORT_SYMBOL(snd_i2c_device_create);
325EXPORT_SYMBOL(snd_i2c_device_free);
326EXPORT_SYMBOL(snd_i2c_sendbytes);
327EXPORT_SYMBOL(snd_i2c_readbytes);
328EXPORT_SYMBOL(snd_i2c_probeaddr);
329
330static int __init alsa_i2c_init(void)
331{
332 return 0;
333}
334
335static void __exit alsa_i2c_exit(void)
336{
337}
338
339module_init(alsa_i2c_init)
340module_exit(alsa_i2c_exit)