• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/sound/i2c/
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
194static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
195{
196	return bus->hw_ops.bit->getdata(bus, ack);
197}
198
199static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
200{
201	snd_i2c_bit_hw_start(bus);
202	snd_i2c_bit_direction(bus, 1, 1);	/* SCL - wr, SDA - wr */
203	snd_i2c_bit_set(bus, 1, 1);
204	snd_i2c_bit_set(bus, 1, 0);
205	snd_i2c_bit_set(bus, 0, 0);
206}
207
208static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
209{
210	snd_i2c_bit_set(bus, 0, 0);
211	snd_i2c_bit_set(bus, 1, 0);
212	snd_i2c_bit_set(bus, 1, 1);
213	snd_i2c_bit_hw_stop(bus);
214}
215
216static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
217{
218	snd_i2c_bit_set(bus, 0, data);
219	snd_i2c_bit_set(bus, 1, data);
220	snd_i2c_bit_set(bus, 0, data);
221}
222
223static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
224{
225	int ack;
226
227	snd_i2c_bit_set(bus, 0, 1);
228	snd_i2c_bit_set(bus, 1, 1);
229	snd_i2c_bit_direction(bus, 1, 0);	/* SCL - wr, SDA - rd */
230	ack = snd_i2c_bit_data(bus, 1);
231	snd_i2c_bit_direction(bus, 1, 1);	/* SCL - wr, SDA - wr */
232	snd_i2c_bit_set(bus, 0, 1);
233	return ack ? -EIO : 0;
234}
235
236static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
237{
238	int i, err;
239
240	for (i = 7; i >= 0; i--)
241		snd_i2c_bit_send(bus, !!(data & (1 << i)));
242	err = snd_i2c_bit_ack(bus);
243	if (err < 0)
244		return err;
245	return 0;
246}
247
248static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
249{
250	int i;
251	unsigned char data = 0;
252
253	snd_i2c_bit_set(bus, 0, 1);
254	snd_i2c_bit_direction(bus, 1, 0);	/* SCL - wr, SDA - rd */
255	for (i = 7; i >= 0; i--) {
256		snd_i2c_bit_set(bus, 1, 1);
257		if (snd_i2c_bit_data(bus, 0))
258			data |= (1 << i);
259		snd_i2c_bit_set(bus, 0, 1);
260	}
261	snd_i2c_bit_direction(bus, 1, 1);	/* SCL - wr, SDA - wr */
262	snd_i2c_bit_send(bus, !!last);
263	return data;
264}
265
266static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
267				 unsigned char *bytes, int count)
268{
269	struct snd_i2c_bus *bus = device->bus;
270	int err, res = 0;
271
272	if (device->flags & SND_I2C_DEVICE_ADDRTEN)
273		return -EIO;		/* not yet implemented */
274	snd_i2c_bit_start(bus);
275	err = snd_i2c_bit_sendbyte(bus, device->addr << 1);
276	if (err < 0) {
277		snd_i2c_bit_hw_stop(bus);
278		return err;
279	}
280	while (count-- > 0) {
281		err = snd_i2c_bit_sendbyte(bus, *bytes++);
282		if (err < 0) {
283			snd_i2c_bit_hw_stop(bus);
284			return err;
285		}
286		res++;
287	}
288	snd_i2c_bit_stop(bus);
289	return res;
290}
291
292static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
293				 unsigned char *bytes, int count)
294{
295	struct snd_i2c_bus *bus = device->bus;
296	int err, res = 0;
297
298	if (device->flags & SND_I2C_DEVICE_ADDRTEN)
299		return -EIO;		/* not yet implemented */
300	snd_i2c_bit_start(bus);
301	err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1);
302	if (err < 0) {
303		snd_i2c_bit_hw_stop(bus);
304		return err;
305	}
306	while (count-- > 0) {
307		err = snd_i2c_bit_readbyte(bus, count == 0);
308		if (err < 0) {
309			snd_i2c_bit_hw_stop(bus);
310			return err;
311		}
312		*bytes++ = (unsigned char)err;
313		res++;
314	}
315	snd_i2c_bit_stop(bus);
316	return res;
317}
318
319static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
320{
321	int err;
322
323	if (addr & 0x8000)	/* 10-bit address */
324		return -EIO;	/* not yet implemented */
325	if (addr & 0x7f80)	/* invalid address */
326		return -EINVAL;
327	snd_i2c_bit_start(bus);
328	err = snd_i2c_bit_sendbyte(bus, addr << 1);
329	snd_i2c_bit_stop(bus);
330	return err;
331}
332
333
334static int __init alsa_i2c_init(void)
335{
336	return 0;
337}
338
339static void __exit alsa_i2c_exit(void)
340{
341}
342
343module_init(alsa_i2c_init)
344module_exit(alsa_i2c_exit)
345