• 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/aoa/soundbus/i2sbus/
1/*
2 * i2sbus driver
3 *
4 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 */
8
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/pci.h>
12#include <linux/interrupt.h>
13#include <linux/dma-mapping.h>
14
15#include <sound/core.h>
16
17#include <asm/macio.h>
18#include <asm/dbdma.h>
19
20#include "../soundbus.h"
21#include "i2sbus.h"
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
25MODULE_DESCRIPTION("Apple Soundbus: I2S support");
26
27static int force;
28module_param(force, int, 0444);
29MODULE_PARM_DESC(force, "Force loading i2sbus even when"
30			" no layout-id property is present");
31
32static struct of_device_id i2sbus_match[] = {
33	{ .name = "i2s" },
34	{ }
35};
36
37MODULE_DEVICE_TABLE(of, i2sbus_match);
38
39static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
40				       struct dbdma_command_mem *r,
41				       int numcmds)
42{
43	/* one more for rounding, one for branch back, one for stop command */
44	r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
45	/* We use the PCI APIs for now until the generic one gets fixed
46	 * enough or until we get some macio-specific versions
47	 */
48	r->space = dma_alloc_coherent(
49			&macio_get_pci_dev(i2sdev->macio)->dev,
50			r->size,
51			&r->bus_addr,
52			GFP_KERNEL);
53
54	if (!r->space) return -ENOMEM;
55
56	memset(r->space, 0, r->size);
57	r->cmds = (void*)DBDMA_ALIGN(r->space);
58	r->bus_cmd_start = r->bus_addr +
59			   (dma_addr_t)((char*)r->cmds - (char*)r->space);
60
61	return 0;
62}
63
64static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
65				       struct dbdma_command_mem *r)
66{
67	if (!r->space) return;
68
69	dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
70			    r->size, r->space, r->bus_addr);
71}
72
73static void i2sbus_release_dev(struct device *dev)
74{
75	struct i2sbus_dev *i2sdev;
76	int i;
77
78	i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
79
80 	if (i2sdev->intfregs) iounmap(i2sdev->intfregs);
81 	if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma);
82 	if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma);
83	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
84		if (i2sdev->allocated_resource[i])
85			release_and_free_resource(i2sdev->allocated_resource[i]);
86	free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
87	free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
88	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
89		free_irq(i2sdev->interrupts[i], i2sdev);
90	i2sbus_control_remove_dev(i2sdev->control, i2sdev);
91	mutex_destroy(&i2sdev->lock);
92	kfree(i2sdev);
93}
94
95static irqreturn_t i2sbus_bus_intr(int irq, void *devid)
96{
97	struct i2sbus_dev *dev = devid;
98	u32 intreg;
99
100	spin_lock(&dev->low_lock);
101	intreg = in_le32(&dev->intfregs->intr_ctl);
102
103	/* acknowledge interrupt reasons */
104	out_le32(&dev->intfregs->intr_ctl, intreg);
105
106	spin_unlock(&dev->low_lock);
107
108	return IRQ_HANDLED;
109}
110
111
112static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index,
113				     int layout, struct resource *res)
114{
115	struct device_node *parent;
116	int pindex, rc = -ENXIO;
117	const u32 *reg;
118
119	/* Machines with layout 76 and 36 (K2 based) have a weird device
120	 * tree what we need to special case.
121	 * Normal machines just fetch the resource from the i2s-X node.
122	 * Darwin further divides normal machines into old and new layouts
123	 * with a subtely different code path but that doesn't seem necessary
124	 * in practice, they just bloated it. In addition, even on our K2
125	 * case the i2s-modem node, if we ever want to handle it, uses the
126	 * normal layout
127	 */
128	if (layout != 76 && layout != 36)
129		return of_address_to_resource(np, index, res);
130
131	parent = of_get_parent(np);
132	pindex = (index == aoa_resource_i2smmio) ? 0 : 1;
133	rc = of_address_to_resource(parent, pindex, res);
134	if (rc)
135		goto bail;
136	reg = of_get_property(np, "reg", NULL);
137	if (reg == NULL) {
138		rc = -ENXIO;
139		goto bail;
140	}
141	res->start += reg[index * 2];
142	res->end = res->start + reg[index * 2 + 1] - 1;
143 bail:
144	of_node_put(parent);
145	return rc;
146}
147
148static int i2sbus_add_dev(struct macio_dev *macio,
149			  struct i2sbus_control *control,
150			  struct device_node *np)
151{
152	struct i2sbus_dev *dev;
153	struct device_node *child = NULL, *sound = NULL;
154	struct resource *r;
155	int i, layout = 0, rlen, ok = force;
156	static const char *rnames[] = { "i2sbus: %s (control)",
157					"i2sbus: %s (tx)",
158					"i2sbus: %s (rx)" };
159	static irq_handler_t ints[] = {
160		i2sbus_bus_intr,
161		i2sbus_tx_intr,
162		i2sbus_rx_intr
163	};
164
165	if (strlen(np->name) != 5)
166		return 0;
167	if (strncmp(np->name, "i2s-", 4))
168		return 0;
169
170	dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
171	if (!dev)
172		return 0;
173
174	i = 0;
175	while ((child = of_get_next_child(np, child))) {
176		if (strcmp(child->name, "sound") == 0) {
177			i++;
178			sound = child;
179		}
180	}
181	if (i == 1) {
182		const u32 *id = of_get_property(sound, "layout-id", NULL);
183
184		if (id) {
185			layout = *id;
186			snprintf(dev->sound.modalias, 32,
187				 "sound-layout-%d", layout);
188			ok = 1;
189		} else {
190			id = of_get_property(sound, "device-id", NULL);
191			/*
192			 * We probably cannot handle all device-id machines,
193			 * so restrict to those we do handle for now.
194			 */
195			if (id && (*id == 22 || *id == 14 || *id == 35)) {
196				snprintf(dev->sound.modalias, 32,
197					 "aoa-device-id-%d", *id);
198				ok = 1;
199				layout = -1;
200			}
201		}
202	}
203	/* for the time being, until we can handle non-layout-id
204	 * things in some fabric, refuse to attach if there is no
205	 * layout-id property or we haven't been forced to attach.
206	 * When there are two i2s busses and only one has a layout-id,
207	 * then this depends on the order, but that isn't important
208	 * either as the second one in that case is just a modem. */
209	if (!ok) {
210		kfree(dev);
211		return -ENODEV;
212	}
213
214	mutex_init(&dev->lock);
215	spin_lock_init(&dev->low_lock);
216	dev->sound.ofdev.archdata.dma_mask = macio->ofdev.archdata.dma_mask;
217	dev->sound.ofdev.dev.of_node = np;
218	dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.archdata.dma_mask;
219	dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
220	dev->sound.ofdev.dev.release = i2sbus_release_dev;
221	dev->sound.attach_codec = i2sbus_attach_codec;
222	dev->sound.detach_codec = i2sbus_detach_codec;
223	dev->sound.pcmid = -1;
224	dev->macio = macio;
225	dev->control = control;
226	dev->bus_number = np->name[4] - 'a';
227	INIT_LIST_HEAD(&dev->sound.codec_list);
228
229	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
230		dev->interrupts[i] = -1;
231		snprintf(dev->rnames[i], sizeof(dev->rnames[i]),
232			 rnames[i], np->name);
233	}
234	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
235		int irq = irq_of_parse_and_map(np, i);
236		if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
237			goto err;
238		dev->interrupts[i] = irq;
239	}
240
241
242	for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
243		if (i2sbus_get_and_fixup_rsrc(np,i,layout,&dev->resources[i]))
244			goto err;
245		/* If only we could use our resource dev->resources[i]...
246		 * but request_resource doesn't know about parents and
247		 * contained resources...
248		 */
249		dev->allocated_resource[i] =
250			request_mem_region(dev->resources[i].start,
251					   dev->resources[i].end -
252					   dev->resources[i].start + 1,
253					   dev->rnames[i]);
254		if (!dev->allocated_resource[i]) {
255			printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
256			goto err;
257		}
258	}
259
260	r = &dev->resources[aoa_resource_i2smmio];
261	rlen = r->end - r->start + 1;
262	if (rlen < sizeof(struct i2s_interface_regs))
263		goto err;
264	dev->intfregs = ioremap(r->start, rlen);
265
266	r = &dev->resources[aoa_resource_txdbdma];
267	rlen = r->end - r->start + 1;
268	if (rlen < sizeof(struct dbdma_regs))
269		goto err;
270	dev->out.dbdma = ioremap(r->start, rlen);
271
272	r = &dev->resources[aoa_resource_rxdbdma];
273	rlen = r->end - r->start + 1;
274	if (rlen < sizeof(struct dbdma_regs))
275		goto err;
276	dev->in.dbdma = ioremap(r->start, rlen);
277
278	if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
279		goto err;
280
281	if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
282					MAX_DBDMA_COMMANDS))
283		goto err;
284	if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
285					MAX_DBDMA_COMMANDS))
286		goto err;
287
288	if (i2sbus_control_add_dev(dev->control, dev)) {
289		printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
290		goto err;
291	}
292
293	if (soundbus_add_one(&dev->sound)) {
294		printk(KERN_DEBUG "i2sbus: device registration error!\n");
295		goto err;
296	}
297
298	/* enable this cell */
299	i2sbus_control_cell(dev->control, dev, 1);
300	i2sbus_control_enable(dev->control, dev);
301	i2sbus_control_clock(dev->control, dev, 1);
302
303	return 1;
304 err:
305	for (i=0;i<3;i++)
306		if (dev->interrupts[i] != -1)
307			free_irq(dev->interrupts[i], dev);
308	free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
309	free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
310	if (dev->intfregs) iounmap(dev->intfregs);
311	if (dev->out.dbdma) iounmap(dev->out.dbdma);
312	if (dev->in.dbdma) iounmap(dev->in.dbdma);
313	for (i=0;i<3;i++)
314		if (dev->allocated_resource[i])
315			release_and_free_resource(dev->allocated_resource[i]);
316	mutex_destroy(&dev->lock);
317	kfree(dev);
318	return 0;
319}
320
321static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
322{
323	struct device_node *np = NULL;
324	int got = 0, err;
325	struct i2sbus_control *control = NULL;
326
327	err = i2sbus_control_init(dev, &control);
328	if (err)
329		return err;
330	if (!control) {
331		printk(KERN_ERR "i2sbus_control_init API breakage\n");
332		return -ENODEV;
333	}
334
335	while ((np = of_get_next_child(dev->ofdev.dev.of_node, np))) {
336		if (of_device_is_compatible(np, "i2sbus") ||
337		    of_device_is_compatible(np, "i2s-modem")) {
338			got += i2sbus_add_dev(dev, control, np);
339		}
340	}
341
342	if (!got) {
343		/* found none, clean up */
344		i2sbus_control_destroy(control);
345		return -ENODEV;
346	}
347
348	dev_set_drvdata(&dev->ofdev.dev, control);
349
350	return 0;
351}
352
353static int i2sbus_remove(struct macio_dev* dev)
354{
355	struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
356	struct i2sbus_dev *i2sdev, *tmp;
357
358	list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
359		soundbus_remove_one(&i2sdev->sound);
360
361	return 0;
362}
363
364#ifdef CONFIG_PM
365static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
366{
367	struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
368	struct codec_info_item *cii;
369	struct i2sbus_dev* i2sdev;
370	int err, ret = 0;
371
372	list_for_each_entry(i2sdev, &control->list, item) {
373		/* Notify Alsa */
374		if (i2sdev->sound.pcm) {
375			/* Suspend PCM streams */
376			snd_pcm_suspend_all(i2sdev->sound.pcm);
377		}
378
379		/* Notify codecs */
380		list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
381			err = 0;
382			if (cii->codec->suspend)
383				err = cii->codec->suspend(cii, state);
384			if (err)
385				ret = err;
386		}
387
388		/* wait until streams are stopped */
389		i2sbus_wait_for_stop_both(i2sdev);
390	}
391
392	return ret;
393}
394
395static int i2sbus_resume(struct macio_dev* dev)
396{
397	struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
398	struct codec_info_item *cii;
399	struct i2sbus_dev* i2sdev;
400	int err, ret = 0;
401
402	list_for_each_entry(i2sdev, &control->list, item) {
403		/* reset i2s bus format etc. */
404		i2sbus_pcm_prepare_both(i2sdev);
405
406		/* Notify codecs so they can re-initialize */
407		list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
408			err = 0;
409			if (cii->codec->resume)
410				err = cii->codec->resume(cii);
411			if (err)
412				ret = err;
413		}
414	}
415
416	return ret;
417}
418#endif /* CONFIG_PM */
419
420static int i2sbus_shutdown(struct macio_dev* dev)
421{
422	return 0;
423}
424
425static struct macio_driver i2sbus_drv = {
426	.driver = {
427		.name = "soundbus-i2s",
428		.owner = THIS_MODULE,
429		.of_match_table = i2sbus_match,
430	},
431	.probe = i2sbus_probe,
432	.remove = i2sbus_remove,
433#ifdef CONFIG_PM
434	.suspend = i2sbus_suspend,
435	.resume = i2sbus_resume,
436#endif
437	.shutdown = i2sbus_shutdown,
438};
439
440static int __init soundbus_i2sbus_init(void)
441{
442	return macio_register_driver(&i2sbus_drv);
443}
444
445static void __exit soundbus_i2sbus_exit(void)
446{
447	macio_unregister_driver(&i2sbus_drv);
448}
449
450module_init(soundbus_i2sbus_init);
451module_exit(soundbus_i2sbus_exit);
452