blob: a684faa771ef55f03191e53054a15cc989918575 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Generic i2c interface for ALSA
4 *
5 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02006 * Modified for the ALSA driver by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
13#include <linux/errno.h>
14#include <sound/core.h>
15#include <sound/i2c.h>
16
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020017MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070018MODULE_DESCRIPTION("Generic i2c interface for ALSA");
19MODULE_LICENSE("GPL");
20
Takashi Iwai97f02e02005-11-17 14:17:19 +010021static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
22 unsigned char *bytes, int count);
23static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
24 unsigned char *bytes, int count);
25static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus,
26 unsigned short addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Julia Lawall5df29bc2015-11-29 18:25:24 +010028static const struct snd_i2c_ops snd_i2c_bit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .sendbytes = snd_i2c_bit_sendbytes,
30 .readbytes = snd_i2c_bit_readbytes,
31 .probeaddr = snd_i2c_bit_probeaddr,
32};
33
Takashi Iwai97f02e02005-11-17 14:17:19 +010034static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Takashi Iwai97f02e02005-11-17 14:17:19 +010036 struct snd_i2c_bus *slave;
37 struct snd_i2c_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Takashi Iwai5e246b82008-08-08 17:12:47 +020039 if (snd_BUG_ON(!bus))
40 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 while (!list_empty(&bus->devices)) {
42 device = snd_i2c_device(bus->devices.next);
43 snd_i2c_device_free(device);
44 }
45 if (bus->master)
46 list_del(&bus->buses);
47 else {
48 while (!list_empty(&bus->buses)) {
49 slave = snd_i2c_slave_bus(bus->buses.next);
50 snd_device_free(bus->card, slave);
51 }
52 }
53 if (bus->private_free)
54 bus->private_free(bus);
55 kfree(bus);
56 return 0;
57}
58
Takashi Iwai97f02e02005-11-17 14:17:19 +010059static int snd_i2c_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Takashi Iwai97f02e02005-11-17 14:17:19 +010061 struct snd_i2c_bus *bus = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return snd_i2c_bus_free(bus);
63}
64
Takashi Iwai97f02e02005-11-17 14:17:19 +010065int snd_i2c_bus_create(struct snd_card *card, const char *name,
66 struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Takashi Iwai97f02e02005-11-17 14:17:19 +010068 struct snd_i2c_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int err;
Takashi Iwaid23015c2020-01-03 09:16:22 +010070 static const struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .dev_free = snd_i2c_bus_dev_free,
72 };
73
74 *ri2c = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +020075 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (bus == NULL)
77 return -ENOMEM;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010078 mutex_init(&bus->lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 INIT_LIST_HEAD(&bus->devices);
80 INIT_LIST_HEAD(&bus->buses);
81 bus->card = card;
82 bus->ops = &snd_i2c_bit_ops;
83 if (master) {
84 list_add_tail(&bus->buses, &master->buses);
85 bus->master = master;
86 }
87 strlcpy(bus->name, name, sizeof(bus->name));
Brian Waters1cff3992010-04-15 04:03:29 -040088 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops);
89 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 snd_i2c_bus_free(bus);
91 return err;
92 }
93 *ri2c = bus;
94 return 0;
95}
96
Takashi Iwai57c65c12006-04-28 15:13:40 +020097EXPORT_SYMBOL(snd_i2c_bus_create);
98
Takashi Iwai97f02e02005-11-17 14:17:19 +010099int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
100 unsigned char addr, struct snd_i2c_device **rdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100102 struct snd_i2c_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 *rdevice = NULL;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200105 if (snd_BUG_ON(!bus))
106 return -EINVAL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200107 device = kzalloc(sizeof(*device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (device == NULL)
109 return -ENOMEM;
110 device->addr = addr;
111 strlcpy(device->name, name, sizeof(device->name));
112 list_add_tail(&device->list, &bus->devices);
113 device->bus = bus;
114 *rdevice = device;
115 return 0;
116}
117
Takashi Iwai57c65c12006-04-28 15:13:40 +0200118EXPORT_SYMBOL(snd_i2c_device_create);
119
Takashi Iwai97f02e02005-11-17 14:17:19 +0100120int snd_i2c_device_free(struct snd_i2c_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 if (device->bus)
123 list_del(&device->list);
124 if (device->private_free)
125 device->private_free(device);
126 kfree(device);
127 return 0;
128}
129
Takashi Iwai57c65c12006-04-28 15:13:40 +0200130EXPORT_SYMBOL(snd_i2c_device_free);
131
Takashi Iwai97f02e02005-11-17 14:17:19 +0100132int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 return device->bus->ops->sendbytes(device, bytes, count);
135}
136
Takashi Iwai57c65c12006-04-28 15:13:40 +0200137EXPORT_SYMBOL(snd_i2c_sendbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Takashi Iwai97f02e02005-11-17 14:17:19 +0100139int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 return device->bus->ops->readbytes(device, bytes, count);
142}
143
Takashi Iwai57c65c12006-04-28 15:13:40 +0200144EXPORT_SYMBOL(snd_i2c_readbytes);
145
Takashi Iwai97f02e02005-11-17 14:17:19 +0100146int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 return bus->ops->probeaddr(bus, addr);
149}
150
Takashi Iwai57c65c12006-04-28 15:13:40 +0200151EXPORT_SYMBOL(snd_i2c_probeaddr);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * bit-operations
155 */
156
Takashi Iwai97f02e02005-11-17 14:17:19 +0100157static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 if (bus->hw_ops.bit->start)
160 bus->hw_ops.bit->start(bus);
161}
162
Takashi Iwai97f02e02005-11-17 14:17:19 +0100163static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 if (bus->hw_ops.bit->stop)
166 bus->hw_ops.bit->stop(bus);
167}
168
Takashi Iwai97f02e02005-11-17 14:17:19 +0100169static void snd_i2c_bit_direction(struct snd_i2c_bus *bus, int clock, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 if (bus->hw_ops.bit->direction)
172 bus->hw_ops.bit->direction(bus, clock, data);
173}
174
Takashi Iwai97f02e02005-11-17 14:17:19 +0100175static void snd_i2c_bit_set(struct snd_i2c_bus *bus, int clock, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 bus->hw_ops.bit->setlines(bus, clock, data);
178}
179
180#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +0100181static int snd_i2c_bit_clock(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 if (bus->hw_ops.bit->getclock)
184 return bus->hw_ops.bit->getclock(bus);
185 return -ENXIO;
186}
187#endif
188
Takashi Iwai97f02e02005-11-17 14:17:19 +0100189static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 return bus->hw_ops.bit->getdata(bus, ack);
192}
193
Takashi Iwai97f02e02005-11-17 14:17:19 +0100194static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 snd_i2c_bit_hw_start(bus);
197 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
198 snd_i2c_bit_set(bus, 1, 1);
199 snd_i2c_bit_set(bus, 1, 0);
200 snd_i2c_bit_set(bus, 0, 0);
201}
202
Takashi Iwai97f02e02005-11-17 14:17:19 +0100203static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 snd_i2c_bit_set(bus, 0, 0);
206 snd_i2c_bit_set(bus, 1, 0);
207 snd_i2c_bit_set(bus, 1, 1);
208 snd_i2c_bit_hw_stop(bus);
209}
210
Takashi Iwai97f02e02005-11-17 14:17:19 +0100211static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 snd_i2c_bit_set(bus, 0, data);
214 snd_i2c_bit_set(bus, 1, data);
215 snd_i2c_bit_set(bus, 0, data);
216}
217
Takashi Iwai97f02e02005-11-17 14:17:19 +0100218static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int ack;
221
222 snd_i2c_bit_set(bus, 0, 1);
223 snd_i2c_bit_set(bus, 1, 1);
224 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
225 ack = snd_i2c_bit_data(bus, 1);
226 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
227 snd_i2c_bit_set(bus, 0, 1);
228 return ack ? -EIO : 0;
229}
230
Takashi Iwai97f02e02005-11-17 14:17:19 +0100231static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int i, err;
234
235 for (i = 7; i >= 0; i--)
236 snd_i2c_bit_send(bus, !!(data & (1 << i)));
Brian Waters1cff3992010-04-15 04:03:29 -0400237 err = snd_i2c_bit_ack(bus);
238 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return err;
240 return 0;
241}
242
Takashi Iwai97f02e02005-11-17 14:17:19 +0100243static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int i;
246 unsigned char data = 0;
247
248 snd_i2c_bit_set(bus, 0, 1);
249 snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
250 for (i = 7; i >= 0; i--) {
251 snd_i2c_bit_set(bus, 1, 1);
252 if (snd_i2c_bit_data(bus, 0))
253 data |= (1 << i);
254 snd_i2c_bit_set(bus, 0, 1);
255 }
256 snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
257 snd_i2c_bit_send(bus, !!last);
258 return data;
259}
260
Takashi Iwai97f02e02005-11-17 14:17:19 +0100261static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
262 unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100264 struct snd_i2c_bus *bus = device->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int err, res = 0;
266
267 if (device->flags & SND_I2C_DEVICE_ADDRTEN)
268 return -EIO; /* not yet implemented */
269 snd_i2c_bit_start(bus);
Brian Waters1cff3992010-04-15 04:03:29 -0400270 err = snd_i2c_bit_sendbyte(bus, device->addr << 1);
271 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 snd_i2c_bit_hw_stop(bus);
273 return err;
274 }
275 while (count-- > 0) {
Brian Waters1cff3992010-04-15 04:03:29 -0400276 err = snd_i2c_bit_sendbyte(bus, *bytes++);
277 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 snd_i2c_bit_hw_stop(bus);
279 return err;
280 }
281 res++;
282 }
283 snd_i2c_bit_stop(bus);
284 return res;
285}
286
Takashi Iwai97f02e02005-11-17 14:17:19 +0100287static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
288 unsigned char *bytes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100290 struct snd_i2c_bus *bus = device->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 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);
Brian Waters1cff3992010-04-15 04:03:29 -0400296 err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1);
297 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 snd_i2c_bit_hw_stop(bus);
299 return err;
300 }
301 while (count-- > 0) {
Brian Waters1cff3992010-04-15 04:03:29 -0400302 err = snd_i2c_bit_readbyte(bus, count == 0);
303 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 snd_i2c_bit_hw_stop(bus);
305 return err;
306 }
307 *bytes++ = (unsigned char)err;
308 res++;
309 }
310 snd_i2c_bit_stop(bus);
311 return res;
312}
313
Takashi Iwai97f02e02005-11-17 14:17:19 +0100314static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 int err;
317
318 if (addr & 0x8000) /* 10-bit address */
319 return -EIO; /* not yet implemented */
320 if (addr & 0x7f80) /* invalid address */
321 return -EINVAL;
322 snd_i2c_bit_start(bus);
323 err = snd_i2c_bit_sendbyte(bus, addr << 1);
324 snd_i2c_bit_stop(bus);
325 return err;
326}