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