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