0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/init.h>
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/string.h>
0013 #include <linux/errno.h>
0014 #include <sound/core.h>
0015 #include <sound/i2c.h>
0016
0017 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0018 MODULE_DESCRIPTION("Generic i2c interface for ALSA");
0019 MODULE_LICENSE("GPL");
0020
0021 static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
0022 unsigned char *bytes, int count);
0023 static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
0024 unsigned char *bytes, int count);
0025 static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus,
0026 unsigned short addr);
0027
0028 static const struct snd_i2c_ops snd_i2c_bit_ops = {
0029 .sendbytes = snd_i2c_bit_sendbytes,
0030 .readbytes = snd_i2c_bit_readbytes,
0031 .probeaddr = snd_i2c_bit_probeaddr,
0032 };
0033
0034 static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
0035 {
0036 struct snd_i2c_bus *slave;
0037 struct snd_i2c_device *device;
0038
0039 if (snd_BUG_ON(!bus))
0040 return -EINVAL;
0041 while (!list_empty(&bus->devices)) {
0042 device = snd_i2c_device(bus->devices.next);
0043 snd_i2c_device_free(device);
0044 }
0045 if (bus->master)
0046 list_del(&bus->buses);
0047 else {
0048 while (!list_empty(&bus->buses)) {
0049 slave = snd_i2c_slave_bus(bus->buses.next);
0050 snd_device_free(bus->card, slave);
0051 }
0052 }
0053 if (bus->private_free)
0054 bus->private_free(bus);
0055 kfree(bus);
0056 return 0;
0057 }
0058
0059 static int snd_i2c_bus_dev_free(struct snd_device *device)
0060 {
0061 struct snd_i2c_bus *bus = device->device_data;
0062 return snd_i2c_bus_free(bus);
0063 }
0064
0065 int snd_i2c_bus_create(struct snd_card *card, const char *name,
0066 struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
0067 {
0068 struct snd_i2c_bus *bus;
0069 int err;
0070 static const struct snd_device_ops ops = {
0071 .dev_free = snd_i2c_bus_dev_free,
0072 };
0073
0074 *ri2c = NULL;
0075 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
0076 if (bus == NULL)
0077 return -ENOMEM;
0078 mutex_init(&bus->lock_mutex);
0079 INIT_LIST_HEAD(&bus->devices);
0080 INIT_LIST_HEAD(&bus->buses);
0081 bus->card = card;
0082 bus->ops = &snd_i2c_bit_ops;
0083 if (master) {
0084 list_add_tail(&bus->buses, &master->buses);
0085 bus->master = master;
0086 }
0087 strscpy(bus->name, name, sizeof(bus->name));
0088 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops);
0089 if (err < 0) {
0090 snd_i2c_bus_free(bus);
0091 return err;
0092 }
0093 *ri2c = bus;
0094 return 0;
0095 }
0096
0097 EXPORT_SYMBOL(snd_i2c_bus_create);
0098
0099 int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
0100 unsigned char addr, struct snd_i2c_device **rdevice)
0101 {
0102 struct snd_i2c_device *device;
0103
0104 *rdevice = NULL;
0105 if (snd_BUG_ON(!bus))
0106 return -EINVAL;
0107 device = kzalloc(sizeof(*device), GFP_KERNEL);
0108 if (device == NULL)
0109 return -ENOMEM;
0110 device->addr = addr;
0111 strscpy(device->name, name, sizeof(device->name));
0112 list_add_tail(&device->list, &bus->devices);
0113 device->bus = bus;
0114 *rdevice = device;
0115 return 0;
0116 }
0117
0118 EXPORT_SYMBOL(snd_i2c_device_create);
0119
0120 int snd_i2c_device_free(struct snd_i2c_device *device)
0121 {
0122 if (device->bus)
0123 list_del(&device->list);
0124 if (device->private_free)
0125 device->private_free(device);
0126 kfree(device);
0127 return 0;
0128 }
0129
0130 EXPORT_SYMBOL(snd_i2c_device_free);
0131
0132 int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
0133 {
0134 return device->bus->ops->sendbytes(device, bytes, count);
0135 }
0136
0137 EXPORT_SYMBOL(snd_i2c_sendbytes);
0138
0139 int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
0140 {
0141 return device->bus->ops->readbytes(device, bytes, count);
0142 }
0143
0144 EXPORT_SYMBOL(snd_i2c_readbytes);
0145
0146 int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
0147 {
0148 return bus->ops->probeaddr(bus, addr);
0149 }
0150
0151 EXPORT_SYMBOL(snd_i2c_probeaddr);
0152
0153
0154
0155
0156
0157 static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *bus)
0158 {
0159 if (bus->hw_ops.bit->start)
0160 bus->hw_ops.bit->start(bus);
0161 }
0162
0163 static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus *bus)
0164 {
0165 if (bus->hw_ops.bit->stop)
0166 bus->hw_ops.bit->stop(bus);
0167 }
0168
0169 static void snd_i2c_bit_direction(struct snd_i2c_bus *bus, int clock, int data)
0170 {
0171 if (bus->hw_ops.bit->direction)
0172 bus->hw_ops.bit->direction(bus, clock, data);
0173 }
0174
0175 static void snd_i2c_bit_set(struct snd_i2c_bus *bus, int clock, int data)
0176 {
0177 bus->hw_ops.bit->setlines(bus, clock, data);
0178 }
0179
0180 #if 0
0181 static int snd_i2c_bit_clock(struct snd_i2c_bus *bus)
0182 {
0183 if (bus->hw_ops.bit->getclock)
0184 return bus->hw_ops.bit->getclock(bus);
0185 return -ENXIO;
0186 }
0187 #endif
0188
0189 static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
0190 {
0191 return bus->hw_ops.bit->getdata(bus, ack);
0192 }
0193
0194 static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
0195 {
0196 snd_i2c_bit_hw_start(bus);
0197 snd_i2c_bit_direction(bus, 1, 1);
0198 snd_i2c_bit_set(bus, 1, 1);
0199 snd_i2c_bit_set(bus, 1, 0);
0200 snd_i2c_bit_set(bus, 0, 0);
0201 }
0202
0203 static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
0204 {
0205 snd_i2c_bit_set(bus, 0, 0);
0206 snd_i2c_bit_set(bus, 1, 0);
0207 snd_i2c_bit_set(bus, 1, 1);
0208 snd_i2c_bit_hw_stop(bus);
0209 }
0210
0211 static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
0212 {
0213 snd_i2c_bit_set(bus, 0, data);
0214 snd_i2c_bit_set(bus, 1, data);
0215 snd_i2c_bit_set(bus, 0, data);
0216 }
0217
0218 static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
0219 {
0220 int ack;
0221
0222 snd_i2c_bit_set(bus, 0, 1);
0223 snd_i2c_bit_set(bus, 1, 1);
0224 snd_i2c_bit_direction(bus, 1, 0);
0225 ack = snd_i2c_bit_data(bus, 1);
0226 snd_i2c_bit_direction(bus, 1, 1);
0227 snd_i2c_bit_set(bus, 0, 1);
0228 return ack ? -EIO : 0;
0229 }
0230
0231 static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
0232 {
0233 int i, err;
0234
0235 for (i = 7; i >= 0; i--)
0236 snd_i2c_bit_send(bus, !!(data & (1 << i)));
0237 err = snd_i2c_bit_ack(bus);
0238 if (err < 0)
0239 return err;
0240 return 0;
0241 }
0242
0243 static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
0244 {
0245 int i;
0246 unsigned char data = 0;
0247
0248 snd_i2c_bit_set(bus, 0, 1);
0249 snd_i2c_bit_direction(bus, 1, 0);
0250 for (i = 7; i >= 0; i--) {
0251 snd_i2c_bit_set(bus, 1, 1);
0252 if (snd_i2c_bit_data(bus, 0))
0253 data |= (1 << i);
0254 snd_i2c_bit_set(bus, 0, 1);
0255 }
0256 snd_i2c_bit_direction(bus, 1, 1);
0257 snd_i2c_bit_send(bus, !!last);
0258 return data;
0259 }
0260
0261 static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
0262 unsigned char *bytes, int count)
0263 {
0264 struct snd_i2c_bus *bus = device->bus;
0265 int err, res = 0;
0266
0267 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
0268 return -EIO;
0269 snd_i2c_bit_start(bus);
0270 err = snd_i2c_bit_sendbyte(bus, device->addr << 1);
0271 if (err < 0) {
0272 snd_i2c_bit_hw_stop(bus);
0273 return err;
0274 }
0275 while (count-- > 0) {
0276 err = snd_i2c_bit_sendbyte(bus, *bytes++);
0277 if (err < 0) {
0278 snd_i2c_bit_hw_stop(bus);
0279 return err;
0280 }
0281 res++;
0282 }
0283 snd_i2c_bit_stop(bus);
0284 return res;
0285 }
0286
0287 static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
0288 unsigned char *bytes, int count)
0289 {
0290 struct snd_i2c_bus *bus = device->bus;
0291 int err, res = 0;
0292
0293 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
0294 return -EIO;
0295 snd_i2c_bit_start(bus);
0296 err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1);
0297 if (err < 0) {
0298 snd_i2c_bit_hw_stop(bus);
0299 return err;
0300 }
0301 while (count-- > 0) {
0302 err = snd_i2c_bit_readbyte(bus, count == 0);
0303 if (err < 0) {
0304 snd_i2c_bit_hw_stop(bus);
0305 return err;
0306 }
0307 *bytes++ = (unsigned char)err;
0308 res++;
0309 }
0310 snd_i2c_bit_stop(bus);
0311 return res;
0312 }
0313
0314 static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
0315 {
0316 int err;
0317
0318 if (addr & 0x8000)
0319 return -EIO;
0320 if (addr & 0x7f80)
0321 return -EINVAL;
0322 snd_i2c_bit_start(bus);
0323 err = snd_i2c_bit_sendbyte(bus, addr << 1);
0324 snd_i2c_bit_stop(bus);
0325 return err;
0326 }