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