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 <sound/core.h>
27 #include <sound/i2c.h>
29 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
30 MODULE_DESCRIPTION("Generic i2c interface for ALSA");
31 MODULE_LICENSE("GPL");
33 static int snd_i2c_bit_sendbytes(snd_i2c_device_t
*device
, unsigned char *bytes
, int count
);
34 static int snd_i2c_bit_readbytes(snd_i2c_device_t
*device
, unsigned char *bytes
, int count
);
35 static int snd_i2c_bit_probeaddr(snd_i2c_bus_t
*bus
, unsigned short addr
);
37 static snd_i2c_ops_t snd_i2c_bit_ops
= {
38 sendbytes
: snd_i2c_bit_sendbytes
,
39 readbytes
: snd_i2c_bit_readbytes
,
40 probeaddr
: snd_i2c_bit_probeaddr
,
43 static int snd_i2c_bus_free(snd_i2c_bus_t
*bus
)
46 snd_i2c_device_t
*device
;
48 snd_assert(bus
!= NULL
, return -EINVAL
);
49 while (!list_empty(&bus
->devices
)) {
50 device
= snd_i2c_device(bus
->devices
.next
);
51 snd_i2c_device_free(device
);
54 list_del(&bus
->buses
);
56 while (!list_empty(&bus
->buses
)) {
57 slave
= snd_i2c_slave_bus(bus
->buses
.next
);
58 snd_device_free(bus
->card
, slave
);
61 if (bus
->private_free
)
62 bus
->private_free(bus
);
67 static int snd_i2c_bus_dev_free(snd_device_t
*device
)
69 snd_i2c_bus_t
*bus
= snd_magic_cast(snd_i2c_bus_t
, device
->device_data
, return -ENXIO
);
70 return snd_i2c_bus_free(bus
);
73 int snd_i2c_bus_create(snd_card_t
*card
, const char *name
, snd_i2c_bus_t
*master
, snd_i2c_bus_t
**ri2c
)
77 static snd_device_ops_t ops
= {
78 dev_free
: snd_i2c_bus_dev_free
,
82 bus
= (snd_i2c_bus_t
*)snd_magic_kcalloc(snd_i2c_bus_t
, 0, GFP_KERNEL
);
85 spin_lock_init(&bus
->lock
);
86 INIT_LIST_HEAD(&bus
->devices
);
87 INIT_LIST_HEAD(&bus
->buses
);
89 bus
->ops
= &snd_i2c_bit_ops
;
91 list_add_tail(&bus
->buses
, &master
->buses
);
94 strncpy(bus
->name
, name
, sizeof(bus
->name
) - 1);
95 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, bus
, &ops
)) < 0) {
96 snd_i2c_bus_free(bus
);
103 int snd_i2c_device_create(snd_i2c_bus_t
*bus
, const char *name
, unsigned char addr
, snd_i2c_device_t
**rdevice
)
105 snd_i2c_device_t
*device
;
108 snd_assert(bus
!= NULL
, return -EINVAL
);
109 device
= (snd_i2c_device_t
*)snd_magic_kcalloc(snd_i2c_device_t
, 0, GFP_KERNEL
);
113 strncpy(device
->name
, name
, sizeof(device
->name
)-1);
114 list_add_tail(&device
->list
, &bus
->devices
);
120 int snd_i2c_device_free(snd_i2c_device_t
*device
)
123 list_del(&device
->list
);
124 if (device
->private_free
)
125 device
->private_free(device
);
126 snd_magic_kfree(device
);
130 int snd_i2c_sendbytes(snd_i2c_device_t
*device
, unsigned char *bytes
, int count
)
132 return device
->bus
->ops
->sendbytes(device
, bytes
, count
);
136 int snd_i2c_readbytes(snd_i2c_device_t
*device
, unsigned char *bytes
, int count
)
138 return device
->bus
->ops
->readbytes(device
, bytes
, count
);
141 int snd_i2c_probeaddr(snd_i2c_bus_t
*bus
, unsigned short addr
)
143 return bus
->ops
->probeaddr(bus
, addr
);
150 static inline void snd_i2c_bit_hw_start(snd_i2c_bus_t
*bus
)
152 if (bus
->hw_ops
.bit
->start
)
153 bus
->hw_ops
.bit
->start(bus
);
156 static inline void snd_i2c_bit_hw_stop(snd_i2c_bus_t
*bus
)
158 if (bus
->hw_ops
.bit
->stop
)
159 bus
->hw_ops
.bit
->stop(bus
);
162 static void snd_i2c_bit_direction(snd_i2c_bus_t
*bus
, int clock
, int data
)
164 if (bus
->hw_ops
.bit
->direction
)
165 bus
->hw_ops
.bit
->direction(bus
, clock
, data
);
168 static void snd_i2c_bit_set(snd_i2c_bus_t
*bus
, int clock
, int data
)
170 bus
->hw_ops
.bit
->setlines(bus
, clock
, data
);
174 static int snd_i2c_bit_clock(snd_i2c_bus_t
*bus
)
176 if (bus
->hw_ops
.bit
->getclock
)
177 return bus
->hw_ops
.bit
->getclock(bus
);
182 static int snd_i2c_bit_data(snd_i2c_bus_t
*bus
, int ack
)
184 return bus
->hw_ops
.bit
->getdata(bus
, ack
);
187 static void snd_i2c_bit_start(snd_i2c_bus_t
*bus
)
189 snd_i2c_bit_hw_start(bus
);
190 snd_i2c_bit_direction(bus
, 1, 1); /* SCL - wr, SDA - wr */
191 snd_i2c_bit_set(bus
, 1, 1);
192 snd_i2c_bit_set(bus
, 1, 0);
193 snd_i2c_bit_set(bus
, 0, 0);
196 static void snd_i2c_bit_stop(snd_i2c_bus_t
*bus
)
198 snd_i2c_bit_set(bus
, 0, 0);
199 snd_i2c_bit_set(bus
, 1, 0);
200 snd_i2c_bit_set(bus
, 1, 1);
201 snd_i2c_bit_hw_stop(bus
);
204 static void snd_i2c_bit_send(snd_i2c_bus_t
*bus
, int data
)
206 snd_i2c_bit_set(bus
, 0, data
);
207 snd_i2c_bit_set(bus
, 1, data
);
208 snd_i2c_bit_set(bus
, 0, data
);
211 static int snd_i2c_bit_ack(snd_i2c_bus_t
*bus
)
215 snd_i2c_bit_set(bus
, 0, 1);
216 snd_i2c_bit_set(bus
, 1, 1);
217 snd_i2c_bit_direction(bus
, 1, 0); /* SCL - wr, SDA - rd */
218 ack
= snd_i2c_bit_data(bus
, 1);
219 snd_i2c_bit_direction(bus
, 1, 1); /* SCL - wr, SDA - wr */
220 snd_i2c_bit_set(bus
, 0, 1);
221 return ack
? -EREMOTEIO
: 0;
224 static int snd_i2c_bit_sendbyte(snd_i2c_bus_t
*bus
, unsigned char data
)
228 for (i
= 7; i
>= 0; i
--)
229 snd_i2c_bit_send(bus
, !!(data
& (1 << i
)));
230 if ((err
= snd_i2c_bit_ack(bus
)) < 0)
235 static int snd_i2c_bit_readbyte(snd_i2c_bus_t
*bus
, int last
)
238 unsigned char data
= 0;
240 snd_i2c_bit_set(bus
, 0, 1);
241 snd_i2c_bit_direction(bus
, 1, 0); /* SCL - wr, SDA - rd */
242 for (i
= 7; i
>= 0; i
--) {
243 snd_i2c_bit_set(bus
, 1, 1);
244 if (snd_i2c_bit_data(bus
, 0))
246 snd_i2c_bit_set(bus
, 0, 1);
248 snd_i2c_bit_direction(bus
, 1, 1); /* SCL - wr, SDA - wr */
249 snd_i2c_bit_send(bus
, !!last
);
253 static int snd_i2c_bit_sendbytes(snd_i2c_device_t
*device
, unsigned char *bytes
, int count
)
255 snd_i2c_bus_t
*bus
= device
->bus
;
258 if (device
->flags
& SND_I2C_DEVICE_ADDRTEN
)
259 return -EIO
; /* not yet implemented */
260 snd_i2c_bit_start(bus
);
261 if ((err
= snd_i2c_bit_sendbyte(bus
, device
->addr
<< 1)) < 0)
263 while (count
-- > 0) {
264 if ((err
= snd_i2c_bit_sendbyte(bus
, *bytes
++)) < 0)
268 snd_i2c_bit_stop(bus
);
272 static int snd_i2c_bit_readbytes(snd_i2c_device_t
*device
, unsigned char *bytes
, int count
)
274 snd_i2c_bus_t
*bus
= device
->bus
;
277 if (device
->flags
& SND_I2C_DEVICE_ADDRTEN
)
278 return -EIO
; /* not yet implemented */
279 snd_i2c_bit_start(bus
);
280 if ((err
= snd_i2c_bit_sendbyte(bus
, (device
->addr
<< 1) | 1)) < 0)
282 while (count
-- > 0) {
283 if ((err
= snd_i2c_bit_readbyte(bus
, count
== 0)) < 0)
285 *bytes
++ = (unsigned char)err
;
288 snd_i2c_bit_stop(bus
);
292 static int snd_i2c_bit_probeaddr(snd_i2c_bus_t
*bus
, unsigned short addr
)
296 if (addr
& 0x8000) /* 10-bit address */
297 return -EIO
; /* not yet implemented */
298 if (addr
& 0x7f80) /* invalid address */
300 snd_i2c_bit_start(bus
);
301 if ((err
= snd_i2c_bit_sendbyte(bus
, addr
<< 1)) < 0)
303 snd_i2c_bit_stop(bus
);
304 return 1; /* present */
307 EXPORT_SYMBOL(snd_i2c_bus_create
);
308 EXPORT_SYMBOL(snd_i2c_device_create
);
309 EXPORT_SYMBOL(snd_i2c_device_free
);
310 EXPORT_SYMBOL(snd_i2c_sendbytes
);
311 EXPORT_SYMBOL(snd_i2c_readbytes
);
312 EXPORT_SYMBOL(snd_i2c_probeaddr
);
314 static int __init
alsa_i2c_init(void)
319 static void __exit
alsa_i2c_exit(void)
323 module_init(alsa_i2c_init
)
324 module_exit(alsa_i2c_exit
)