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