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@perex.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 <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>
30 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
31 MODULE_DESCRIPTION("Generic i2c interface for ALSA");
32 MODULE_LICENSE("GPL");
34 static int snd_i2c_bit_sendbytes(struct snd_i2c_device
*device
,
35 unsigned char *bytes
, int count
);
36 static int snd_i2c_bit_readbytes(struct snd_i2c_device
*device
,
37 unsigned char *bytes
, int count
);
38 static int snd_i2c_bit_probeaddr(struct snd_i2c_bus
*bus
,
41 static struct snd_i2c_ops snd_i2c_bit_ops
= {
42 .sendbytes
= snd_i2c_bit_sendbytes
,
43 .readbytes
= snd_i2c_bit_readbytes
,
44 .probeaddr
= snd_i2c_bit_probeaddr
,
47 static int snd_i2c_bus_free(struct snd_i2c_bus
*bus
)
49 struct snd_i2c_bus
*slave
;
50 struct snd_i2c_device
*device
;
54 while (!list_empty(&bus
->devices
)) {
55 device
= snd_i2c_device(bus
->devices
.next
);
56 snd_i2c_device_free(device
);
59 list_del(&bus
->buses
);
61 while (!list_empty(&bus
->buses
)) {
62 slave
= snd_i2c_slave_bus(bus
->buses
.next
);
63 snd_device_free(bus
->card
, slave
);
66 if (bus
->private_free
)
67 bus
->private_free(bus
);
72 static int snd_i2c_bus_dev_free(struct snd_device
*device
)
74 struct snd_i2c_bus
*bus
= device
->device_data
;
75 return snd_i2c_bus_free(bus
);
78 int snd_i2c_bus_create(struct snd_card
*card
, const char *name
,
79 struct snd_i2c_bus
*master
, struct snd_i2c_bus
**ri2c
)
81 struct snd_i2c_bus
*bus
;
83 static struct snd_device_ops ops
= {
84 .dev_free
= snd_i2c_bus_dev_free
,
88 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
91 mutex_init(&bus
->lock_mutex
);
92 INIT_LIST_HEAD(&bus
->devices
);
93 INIT_LIST_HEAD(&bus
->buses
);
95 bus
->ops
= &snd_i2c_bit_ops
;
97 list_add_tail(&bus
->buses
, &master
->buses
);
100 strlcpy(bus
->name
, name
, sizeof(bus
->name
));
101 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &ops
);
103 snd_i2c_bus_free(bus
);
110 EXPORT_SYMBOL(snd_i2c_bus_create
);
112 int snd_i2c_device_create(struct snd_i2c_bus
*bus
, const char *name
,
113 unsigned char addr
, struct snd_i2c_device
**rdevice
)
115 struct snd_i2c_device
*device
;
118 if (snd_BUG_ON(!bus
))
120 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
124 strlcpy(device
->name
, name
, sizeof(device
->name
));
125 list_add_tail(&device
->list
, &bus
->devices
);
131 EXPORT_SYMBOL(snd_i2c_device_create
);
133 int snd_i2c_device_free(struct snd_i2c_device
*device
)
136 list_del(&device
->list
);
137 if (device
->private_free
)
138 device
->private_free(device
);
143 EXPORT_SYMBOL(snd_i2c_device_free
);
145 int snd_i2c_sendbytes(struct snd_i2c_device
*device
, unsigned char *bytes
, int count
)
147 return device
->bus
->ops
->sendbytes(device
, bytes
, count
);
150 EXPORT_SYMBOL(snd_i2c_sendbytes
);
152 int snd_i2c_readbytes(struct snd_i2c_device
*device
, unsigned char *bytes
, int count
)
154 return device
->bus
->ops
->readbytes(device
, bytes
, count
);
157 EXPORT_SYMBOL(snd_i2c_readbytes
);
159 int snd_i2c_probeaddr(struct snd_i2c_bus
*bus
, unsigned short addr
)
161 return bus
->ops
->probeaddr(bus
, addr
);
164 EXPORT_SYMBOL(snd_i2c_probeaddr
);
170 static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus
*bus
)
172 if (bus
->hw_ops
.bit
->start
)
173 bus
->hw_ops
.bit
->start(bus
);
176 static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus
*bus
)
178 if (bus
->hw_ops
.bit
->stop
)
179 bus
->hw_ops
.bit
->stop(bus
);
182 static void snd_i2c_bit_direction(struct snd_i2c_bus
*bus
, int clock
, int data
)
184 if (bus
->hw_ops
.bit
->direction
)
185 bus
->hw_ops
.bit
->direction(bus
, clock
, data
);
188 static void snd_i2c_bit_set(struct snd_i2c_bus
*bus
, int clock
, int data
)
190 bus
->hw_ops
.bit
->setlines(bus
, clock
, data
);
194 static int snd_i2c_bit_clock(struct snd_i2c_bus
*bus
)
196 if (bus
->hw_ops
.bit
->getclock
)
197 return bus
->hw_ops
.bit
->getclock(bus
);
202 static int snd_i2c_bit_data(struct snd_i2c_bus
*bus
, int ack
)
204 return bus
->hw_ops
.bit
->getdata(bus
, ack
);
207 static void snd_i2c_bit_start(struct snd_i2c_bus
*bus
)
209 snd_i2c_bit_hw_start(bus
);
210 snd_i2c_bit_direction(bus
, 1, 1); /* SCL - wr, SDA - wr */
211 snd_i2c_bit_set(bus
, 1, 1);
212 snd_i2c_bit_set(bus
, 1, 0);
213 snd_i2c_bit_set(bus
, 0, 0);
216 static void snd_i2c_bit_stop(struct snd_i2c_bus
*bus
)
218 snd_i2c_bit_set(bus
, 0, 0);
219 snd_i2c_bit_set(bus
, 1, 0);
220 snd_i2c_bit_set(bus
, 1, 1);
221 snd_i2c_bit_hw_stop(bus
);
224 static void snd_i2c_bit_send(struct snd_i2c_bus
*bus
, int data
)
226 snd_i2c_bit_set(bus
, 0, data
);
227 snd_i2c_bit_set(bus
, 1, data
);
228 snd_i2c_bit_set(bus
, 0, data
);
231 static int snd_i2c_bit_ack(struct snd_i2c_bus
*bus
)
235 snd_i2c_bit_set(bus
, 0, 1);
236 snd_i2c_bit_set(bus
, 1, 1);
237 snd_i2c_bit_direction(bus
, 1, 0); /* SCL - wr, SDA - rd */
238 ack
= snd_i2c_bit_data(bus
, 1);
239 snd_i2c_bit_direction(bus
, 1, 1); /* SCL - wr, SDA - wr */
240 snd_i2c_bit_set(bus
, 0, 1);
241 return ack
? -EIO
: 0;
244 static int snd_i2c_bit_sendbyte(struct snd_i2c_bus
*bus
, unsigned char data
)
248 for (i
= 7; i
>= 0; i
--)
249 snd_i2c_bit_send(bus
, !!(data
& (1 << i
)));
250 err
= snd_i2c_bit_ack(bus
);
256 static int snd_i2c_bit_readbyte(struct snd_i2c_bus
*bus
, int last
)
259 unsigned char data
= 0;
261 snd_i2c_bit_set(bus
, 0, 1);
262 snd_i2c_bit_direction(bus
, 1, 0); /* SCL - wr, SDA - rd */
263 for (i
= 7; i
>= 0; i
--) {
264 snd_i2c_bit_set(bus
, 1, 1);
265 if (snd_i2c_bit_data(bus
, 0))
267 snd_i2c_bit_set(bus
, 0, 1);
269 snd_i2c_bit_direction(bus
, 1, 1); /* SCL - wr, SDA - wr */
270 snd_i2c_bit_send(bus
, !!last
);
274 static int snd_i2c_bit_sendbytes(struct snd_i2c_device
*device
,
275 unsigned char *bytes
, int count
)
277 struct snd_i2c_bus
*bus
= device
->bus
;
280 if (device
->flags
& SND_I2C_DEVICE_ADDRTEN
)
281 return -EIO
; /* not yet implemented */
282 snd_i2c_bit_start(bus
);
283 err
= snd_i2c_bit_sendbyte(bus
, device
->addr
<< 1);
285 snd_i2c_bit_hw_stop(bus
);
288 while (count
-- > 0) {
289 err
= snd_i2c_bit_sendbyte(bus
, *bytes
++);
291 snd_i2c_bit_hw_stop(bus
);
296 snd_i2c_bit_stop(bus
);
300 static int snd_i2c_bit_readbytes(struct snd_i2c_device
*device
,
301 unsigned char *bytes
, int count
)
303 struct snd_i2c_bus
*bus
= device
->bus
;
306 if (device
->flags
& SND_I2C_DEVICE_ADDRTEN
)
307 return -EIO
; /* not yet implemented */
308 snd_i2c_bit_start(bus
);
309 err
= snd_i2c_bit_sendbyte(bus
, (device
->addr
<< 1) | 1);
311 snd_i2c_bit_hw_stop(bus
);
314 while (count
-- > 0) {
315 err
= snd_i2c_bit_readbyte(bus
, count
== 0);
317 snd_i2c_bit_hw_stop(bus
);
320 *bytes
++ = (unsigned char)err
;
323 snd_i2c_bit_stop(bus
);
327 static int snd_i2c_bit_probeaddr(struct snd_i2c_bus
*bus
, unsigned short addr
)
331 if (addr
& 0x8000) /* 10-bit address */
332 return -EIO
; /* not yet implemented */
333 if (addr
& 0x7f80) /* invalid address */
335 snd_i2c_bit_start(bus
);
336 err
= snd_i2c_bit_sendbyte(bus
, addr
<< 1);
337 snd_i2c_bit_stop(bus
);
342 static int __init
alsa_i2c_init(void)
347 static void __exit
alsa_i2c_exit(void)
351 module_init(alsa_i2c_init
)
352 module_exit(alsa_i2c_exit
)