[POWERPC] Add new interrupt mapping core and change platforms to use it
[linux-2.6/btrfs-unstable.git] / sound / aoa / soundbus / i2sbus / i2sbus-core.c
blob01c0724335a3058652e878de8299726cc65a116b
1 /*
2 * i2sbus driver
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
7 */
9 #include <linux/module.h>
10 #include <asm/macio.h>
11 #include <asm/dbdma.h>
12 #include <linux/pci.h>
13 #include <linux/interrupt.h>
14 #include <sound/driver.h>
15 #include <sound/core.h>
16 #include <linux/dma-mapping.h>
17 #include "../soundbus.h"
18 #include "i2sbus.h"
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
22 MODULE_DESCRIPTION("Apple Soundbus: I2S support");
23 /* for auto-loading, declare that we handle this weird
24 * string that macio puts into the relevant device */
25 MODULE_ALIAS("of:Ni2sTi2sC");
27 static struct of_device_id i2sbus_match[] = {
28 { .name = "i2s" },
29 { }
32 static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
33 struct dbdma_command_mem *r,
34 int numcmds)
36 /* one more for rounding */
37 r->size = (numcmds+1) * sizeof(struct dbdma_cmd);
38 /* We use the PCI APIs for now until the generic one gets fixed
39 * enough or until we get some macio-specific versions
41 r->space = dma_alloc_coherent(
42 &macio_get_pci_dev(i2sdev->macio)->dev,
43 r->size,
44 &r->bus_addr,
45 GFP_KERNEL);
47 if (!r->space) return -ENOMEM;
49 memset(r->space, 0, r->size);
50 r->cmds = (void*)DBDMA_ALIGN(r->space);
51 r->bus_cmd_start = r->bus_addr +
52 (dma_addr_t)((char*)r->cmds - (char*)r->space);
54 return 0;
57 static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
58 struct dbdma_command_mem *r)
60 if (!r->space) return;
62 dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
63 r->size, r->space, r->bus_addr);
66 static void i2sbus_release_dev(struct device *dev)
68 struct i2sbus_dev *i2sdev;
69 int i;
71 i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
73 if (i2sdev->intfregs) iounmap(i2sdev->intfregs);
74 if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma);
75 if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma);
76 for (i=0;i<3;i++)
77 if (i2sdev->allocated_resource[i])
78 release_and_free_resource(i2sdev->allocated_resource[i]);
79 free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
80 free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
81 for (i=0;i<3;i++)
82 free_irq(i2sdev->interrupts[i], i2sdev);
83 i2sbus_control_remove_dev(i2sdev->control, i2sdev);
84 mutex_destroy(&i2sdev->lock);
85 kfree(i2sdev);
88 static irqreturn_t i2sbus_bus_intr(int irq, void *devid, struct pt_regs *regs)
90 struct i2sbus_dev *dev = devid;
91 u32 intreg;
93 spin_lock(&dev->low_lock);
94 intreg = in_le32(&dev->intfregs->intr_ctl);
96 /* acknowledge interrupt reasons */
97 out_le32(&dev->intfregs->intr_ctl, intreg);
99 spin_unlock(&dev->low_lock);
101 return IRQ_HANDLED;
104 static int force;
105 module_param(force, int, 0444);
106 MODULE_PARM_DESC(force, "Force loading i2sbus even when"
107 " no layout-id property is present");
109 /* FIXME: look at device node refcounting */
110 static int i2sbus_add_dev(struct macio_dev *macio,
111 struct i2sbus_control *control,
112 struct device_node *np)
114 struct i2sbus_dev *dev;
115 struct device_node *child = NULL, *sound = NULL;
116 int i;
117 static const char *rnames[] = { "i2sbus: %s (control)",
118 "i2sbus: %s (tx)",
119 "i2sbus: %s (rx)" };
120 static irqreturn_t (*ints[])(int irq, void *devid,
121 struct pt_regs *regs) = {
122 i2sbus_bus_intr,
123 i2sbus_tx_intr,
124 i2sbus_rx_intr
127 if (strlen(np->name) != 5)
128 return 0;
129 if (strncmp(np->name, "i2s-", 4))
130 return 0;
132 if (macio_irq_count(macio) != 3)
133 return 0;
135 dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
136 if (!dev)
137 return 0;
139 i = 0;
140 while ((child = of_get_next_child(np, child))) {
141 if (strcmp(child->name, "sound") == 0) {
142 i++;
143 sound = child;
146 if (i == 1) {
147 u32 *layout_id;
148 layout_id = (u32*) get_property(sound, "layout-id", NULL);
149 if (layout_id) {
150 snprintf(dev->sound.modalias, 32,
151 "sound-layout-%d", *layout_id);
152 force = 1;
155 /* for the time being, until we can handle non-layout-id
156 * things in some fabric, refuse to attach if there is no
157 * layout-id property or we haven't been forced to attach.
158 * When there are two i2s busses and only one has a layout-id,
159 * then this depends on the order, but that isn't important
160 * either as the second one in that case is just a modem. */
161 if (!force) {
162 kfree(dev);
163 return -ENODEV;
166 mutex_init(&dev->lock);
167 spin_lock_init(&dev->low_lock);
168 dev->sound.ofdev.node = np;
169 dev->sound.ofdev.dma_mask = macio->ofdev.dma_mask;
170 dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.dma_mask;
171 dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
172 dev->sound.ofdev.dev.release = i2sbus_release_dev;
173 dev->sound.attach_codec = i2sbus_attach_codec;
174 dev->sound.detach_codec = i2sbus_detach_codec;
175 dev->sound.pcmid = -1;
176 dev->macio = macio;
177 dev->control = control;
178 dev->bus_number = np->name[4] - 'a';
179 INIT_LIST_HEAD(&dev->sound.codec_list);
181 for (i=0;i<3;i++) {
182 dev->interrupts[i] = -1;
183 snprintf(dev->rnames[i], sizeof(dev->rnames[i]), rnames[i], np->name);
185 for (i=0;i<3;i++) {
186 if (request_irq(macio_irq(macio, i), ints[i], 0,
187 dev->rnames[i], dev))
188 goto err;
189 dev->interrupts[i] = macio_irq(macio, i);
192 for (i=0;i<3;i++) {
193 if (of_address_to_resource(np, i, &dev->resources[i]))
194 goto err;
195 /* if only we could use our resource dev->resources[i]...
196 * but request_resource doesn't know about parents and
197 * contained resources... */
198 dev->allocated_resource[i] =
199 request_mem_region(dev->resources[i].start,
200 dev->resources[i].end -
201 dev->resources[i].start + 1,
202 dev->rnames[i]);
203 if (!dev->allocated_resource[i]) {
204 printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
205 goto err;
208 /* should do sanity checking here about length of them */
209 dev->intfregs = ioremap(dev->resources[0].start,
210 dev->resources[0].end-dev->resources[0].start+1);
211 dev->out.dbdma = ioremap(dev->resources[1].start,
212 dev->resources[1].end-dev->resources[1].start+1);
213 dev->in.dbdma = ioremap(dev->resources[2].start,
214 dev->resources[2].end-dev->resources[2].start+1);
215 if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
216 goto err;
218 if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
219 MAX_DBDMA_COMMANDS))
220 goto err;
221 if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
222 MAX_DBDMA_COMMANDS))
223 goto err;
225 if (i2sbus_control_add_dev(dev->control, dev)) {
226 printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
227 goto err;
230 if (soundbus_add_one(&dev->sound)) {
231 printk(KERN_DEBUG "i2sbus: device registration error!\n");
232 goto err;
235 /* enable this cell */
236 i2sbus_control_cell(dev->control, dev, 1);
237 i2sbus_control_enable(dev->control, dev);
238 i2sbus_control_clock(dev->control, dev, 1);
240 return 1;
241 err:
242 for (i=0;i<3;i++)
243 if (dev->interrupts[i] != -1)
244 free_irq(dev->interrupts[i], dev);
245 free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
246 free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
247 if (dev->intfregs) iounmap(dev->intfregs);
248 if (dev->out.dbdma) iounmap(dev->out.dbdma);
249 if (dev->in.dbdma) iounmap(dev->in.dbdma);
250 for (i=0;i<3;i++)
251 if (dev->allocated_resource[i])
252 release_and_free_resource(dev->allocated_resource[i]);
253 mutex_destroy(&dev->lock);
254 kfree(dev);
255 return 0;
258 static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
260 struct device_node *np = NULL;
261 int got = 0, err;
262 struct i2sbus_control *control = NULL;
264 err = i2sbus_control_init(dev, &control);
265 if (err)
266 return err;
267 if (!control) {
268 printk(KERN_ERR "i2sbus_control_init API breakage\n");
269 return -ENODEV;
272 while ((np = of_get_next_child(dev->ofdev.node, np))) {
273 if (device_is_compatible(np, "i2sbus") ||
274 device_is_compatible(np, "i2s-modem")) {
275 got += i2sbus_add_dev(dev, control, np);
279 if (!got) {
280 /* found none, clean up */
281 i2sbus_control_destroy(control);
282 return -ENODEV;
285 dev->ofdev.dev.driver_data = control;
287 return 0;
290 static int i2sbus_remove(struct macio_dev* dev)
292 struct i2sbus_control *control = dev->ofdev.dev.driver_data;
293 struct i2sbus_dev *i2sdev, *tmp;
295 list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
296 soundbus_remove_one(&i2sdev->sound);
298 return 0;
301 #ifdef CONFIG_PM
302 static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
304 struct i2sbus_control *control = dev->ofdev.dev.driver_data;
305 struct codec_info_item *cii;
306 struct i2sbus_dev* i2sdev;
307 int err, ret = 0;
309 list_for_each_entry(i2sdev, &control->list, item) {
310 /* Notify Alsa */
311 if (i2sdev->sound.pcm) {
312 /* Suspend PCM streams */
313 snd_pcm_suspend_all(i2sdev->sound.pcm);
314 /* Probably useless as we handle
315 * power transitions ourselves */
316 snd_power_change_state(i2sdev->sound.pcm->card,
317 SNDRV_CTL_POWER_D3hot);
319 /* Notify codecs */
320 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
321 err = 0;
322 if (cii->codec->suspend)
323 err = cii->codec->suspend(cii, state);
324 if (err)
325 ret = err;
328 return ret;
331 static int i2sbus_resume(struct macio_dev* dev)
333 struct i2sbus_control *control = dev->ofdev.dev.driver_data;
334 struct codec_info_item *cii;
335 struct i2sbus_dev* i2sdev;
336 int err, ret = 0;
338 list_for_each_entry(i2sdev, &control->list, item) {
339 /* Notify codecs so they can re-initialize */
340 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
341 err = 0;
342 if (cii->codec->resume)
343 err = cii->codec->resume(cii);
344 if (err)
345 ret = err;
347 /* Notify Alsa */
348 if (i2sdev->sound.pcm) {
349 /* Same comment as above, probably useless */
350 snd_power_change_state(i2sdev->sound.pcm->card,
351 SNDRV_CTL_POWER_D0);
355 return ret;
357 #endif /* CONFIG_PM */
359 static int i2sbus_shutdown(struct macio_dev* dev)
361 return 0;
364 static struct macio_driver i2sbus_drv = {
365 .name = "soundbus-i2s",
366 .owner = THIS_MODULE,
367 .match_table = i2sbus_match,
368 .probe = i2sbus_probe,
369 .remove = i2sbus_remove,
370 #ifdef CONFIG_PM
371 .suspend = i2sbus_suspend,
372 .resume = i2sbus_resume,
373 #endif
374 .shutdown = i2sbus_shutdown,
377 static int __init soundbus_i2sbus_init(void)
379 return macio_register_driver(&i2sbus_drv);
382 static void __exit soundbus_i2sbus_exit(void)
384 macio_unregister_driver(&i2sbus_drv);
387 module_init(soundbus_i2sbus_init);
388 module_exit(soundbus_i2sbus_exit);