Linux Audio

Check our new training course

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