]> err.no Git - linux-2.6/blob - sound/aoa/soundbus/i2sbus/i2sbus-core.c
[PATCH] Fix snd-aoa irq conversion
[linux-2.6] / sound / aoa / soundbus / i2sbus / i2sbus-core.c
1 /*
2  * i2sbus driver
3  *
4  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5  *
6  * GPL v2, can be found in COPYING.
7  */
8
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"
19
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");
26
27 static struct of_device_id i2sbus_match[] = {
28         { .name = "i2s" },
29         { }
30 };
31
32 static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
33                                        struct dbdma_command_mem *r,
34                                        int numcmds)
35 {
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
40          */
41         r->space = dma_alloc_coherent(
42                         &macio_get_pci_dev(i2sdev->macio)->dev,
43                         r->size,
44                         &r->bus_addr,
45                         GFP_KERNEL);
46
47         if (!r->space) return -ENOMEM;
48
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);
53
54         return 0;
55 }
56
57 static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
58                                        struct dbdma_command_mem *r)
59 {
60         if (!r->space) return;
61         
62         dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
63                             r->size, r->space, r->bus_addr);
64 }
65
66 static void i2sbus_release_dev(struct device *dev)
67 {
68         struct i2sbus_dev *i2sdev;
69         int i;
70
71         i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
72
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);
86 }
87
88 static irqreturn_t i2sbus_bus_intr(int irq, void *devid, struct pt_regs *regs)
89 {
90         struct i2sbus_dev *dev = devid;
91         u32 intreg;
92
93         spin_lock(&dev->low_lock);
94         intreg = in_le32(&dev->intfregs->intr_ctl);
95
96         /* acknowledge interrupt reasons */
97         out_le32(&dev->intfregs->intr_ctl, intreg);
98
99         spin_unlock(&dev->low_lock);
100
101         return IRQ_HANDLED;
102 }
103
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");
108
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)
113 {
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
125         };
126
127         if (strlen(np->name) != 5)
128                 return 0;
129         if (strncmp(np->name, "i2s-", 4))
130                 return 0;
131
132         dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
133         if (!dev)
134                 return 0;
135
136         i = 0;
137         while ((child = of_get_next_child(np, child))) {
138                 if (strcmp(child->name, "sound") == 0) {
139                         i++;
140                         sound = child;
141                 }
142         }
143         if (i == 1) {
144                 u32 *layout_id;
145                 layout_id = (u32*) get_property(sound, "layout-id", NULL);
146                 if (layout_id) {
147                         snprintf(dev->sound.modalias, 32,
148                                  "sound-layout-%d", *layout_id);
149                         force = 1;
150                 }
151         }
152         /* for the time being, until we can handle non-layout-id
153          * things in some fabric, refuse to attach if there is no
154          * layout-id property or we haven't been forced to attach.
155          * When there are two i2s busses and only one has a layout-id,
156          * then this depends on the order, but that isn't important
157          * either as the second one in that case is just a modem. */
158         if (!force) {
159                 kfree(dev);
160                 return -ENODEV;
161         }
162
163         mutex_init(&dev->lock);
164         spin_lock_init(&dev->low_lock);
165         dev->sound.ofdev.node = np;
166         dev->sound.ofdev.dma_mask = macio->ofdev.dma_mask;
167         dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.dma_mask;
168         dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
169         dev->sound.ofdev.dev.release = i2sbus_release_dev;
170         dev->sound.attach_codec = i2sbus_attach_codec;
171         dev->sound.detach_codec = i2sbus_detach_codec;
172         dev->sound.pcmid = -1;
173         dev->macio = macio;
174         dev->control = control;
175         dev->bus_number = np->name[4] - 'a';
176         INIT_LIST_HEAD(&dev->sound.codec_list);
177
178         for (i=0;i<3;i++) {
179                 dev->interrupts[i] = -1;
180                 snprintf(dev->rnames[i], sizeof(dev->rnames[i]), rnames[i], np->name);
181         }
182         for (i=0;i<3;i++) {
183                 int irq = irq_of_parse_and_map(np, i);
184                 if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
185                         goto err;
186                 dev->interrupts[i] = irq;
187         }
188
189         for (i=0;i<3;i++) {
190                 if (of_address_to_resource(np, i, &dev->resources[i]))
191                         goto err;
192                 /* if only we could use our resource dev->resources[i]...
193                  * but request_resource doesn't know about parents and
194                  * contained resources... */
195                 dev->allocated_resource[i] = 
196                         request_mem_region(dev->resources[i].start,
197                                            dev->resources[i].end -
198                                            dev->resources[i].start + 1,
199                                            dev->rnames[i]);
200                 if (!dev->allocated_resource[i]) {
201                         printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
202                         goto err;
203                 }
204         }
205         /* should do sanity checking here about length of them */
206         dev->intfregs = ioremap(dev->resources[0].start,
207                                 dev->resources[0].end-dev->resources[0].start+1);
208         dev->out.dbdma = ioremap(dev->resources[1].start,
209                                  dev->resources[1].end-dev->resources[1].start+1);
210         dev->in.dbdma = ioremap(dev->resources[2].start,
211                                 dev->resources[2].end-dev->resources[2].start+1);
212         if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
213                 goto err;
214
215         if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
216                                         MAX_DBDMA_COMMANDS))
217                 goto err;
218         if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
219                                         MAX_DBDMA_COMMANDS))
220                 goto err;
221
222         if (i2sbus_control_add_dev(dev->control, dev)) {
223                 printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
224                 goto err;
225         }
226
227         if (soundbus_add_one(&dev->sound)) {
228                 printk(KERN_DEBUG "i2sbus: device registration error!\n");
229                 goto err;
230         }
231
232         /* enable this cell */
233         i2sbus_control_cell(dev->control, dev, 1);
234         i2sbus_control_enable(dev->control, dev);
235         i2sbus_control_clock(dev->control, dev, 1);
236
237         return 1;
238  err:
239         for (i=0;i<3;i++)
240                 if (dev->interrupts[i] != -1)
241                         free_irq(dev->interrupts[i], dev);
242         free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
243         free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
244         if (dev->intfregs) iounmap(dev->intfregs);
245         if (dev->out.dbdma) iounmap(dev->out.dbdma);
246         if (dev->in.dbdma) iounmap(dev->in.dbdma);
247         for (i=0;i<3;i++)
248                 if (dev->allocated_resource[i])
249                         release_and_free_resource(dev->allocated_resource[i]);
250         mutex_destroy(&dev->lock);
251         kfree(dev);
252         return 0;
253 }
254
255 static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
256 {
257         struct device_node *np = NULL;
258         int got = 0, err;
259         struct i2sbus_control *control = NULL;
260
261         err = i2sbus_control_init(dev, &control);
262         if (err)
263                 return err;
264         if (!control) {
265                 printk(KERN_ERR "i2sbus_control_init API breakage\n");
266                 return -ENODEV;
267         }
268
269         while ((np = of_get_next_child(dev->ofdev.node, np))) {
270                 if (device_is_compatible(np, "i2sbus") ||
271                     device_is_compatible(np, "i2s-modem")) {
272                         got += i2sbus_add_dev(dev, control, np);
273                 }
274         }
275
276         if (!got) {
277                 /* found none, clean up */
278                 i2sbus_control_destroy(control);
279                 return -ENODEV;
280         }
281
282         dev->ofdev.dev.driver_data = control;
283
284         return 0;
285 }
286
287 static int i2sbus_remove(struct macio_dev* dev)
288 {
289         struct i2sbus_control *control = dev->ofdev.dev.driver_data;
290         struct i2sbus_dev *i2sdev, *tmp;
291
292         list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
293                 soundbus_remove_one(&i2sdev->sound);
294
295         return 0;
296 }
297
298 #ifdef CONFIG_PM
299 static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
300 {
301         struct i2sbus_control *control = dev->ofdev.dev.driver_data;
302         struct codec_info_item *cii;
303         struct i2sbus_dev* i2sdev;
304         int err, ret = 0;
305
306         list_for_each_entry(i2sdev, &control->list, item) {
307                 /* Notify Alsa */
308                 if (i2sdev->sound.pcm) {
309                         /* Suspend PCM streams */
310                         snd_pcm_suspend_all(i2sdev->sound.pcm);
311                         /* Probably useless as we handle
312                          * power transitions ourselves */
313                         snd_power_change_state(i2sdev->sound.pcm->card,
314                                                SNDRV_CTL_POWER_D3hot);
315                 }
316                 /* Notify codecs */
317                 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
318                         err = 0;
319                         if (cii->codec->suspend)
320                                 err = cii->codec->suspend(cii, state);
321                         if (err)
322                                 ret = err;
323                 }
324         }
325         return ret;
326 }
327
328 static int i2sbus_resume(struct macio_dev* dev)
329 {
330         struct i2sbus_control *control = dev->ofdev.dev.driver_data;
331         struct codec_info_item *cii;
332         struct i2sbus_dev* i2sdev;
333         int err, ret = 0;
334
335         list_for_each_entry(i2sdev, &control->list, item) {
336                 /* Notify codecs so they can re-initialize */
337                 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
338                         err = 0;
339                         if (cii->codec->resume)
340                                 err = cii->codec->resume(cii);
341                         if (err)
342                                 ret = err;
343                 }
344                 /* Notify Alsa */
345                 if (i2sdev->sound.pcm) {
346                         /* Same comment as above, probably useless */
347                         snd_power_change_state(i2sdev->sound.pcm->card,
348                                                SNDRV_CTL_POWER_D0);
349                 }
350         }
351
352         return ret;
353 }
354 #endif /* CONFIG_PM */
355
356 static int i2sbus_shutdown(struct macio_dev* dev)
357 {
358         return 0;
359 }
360
361 static struct macio_driver i2sbus_drv = {
362         .name = "soundbus-i2s",
363         .owner = THIS_MODULE,
364         .match_table = i2sbus_match,
365         .probe = i2sbus_probe,
366         .remove = i2sbus_remove,
367 #ifdef CONFIG_PM
368         .suspend = i2sbus_suspend,
369         .resume = i2sbus_resume,
370 #endif
371         .shutdown = i2sbus_shutdown,
372 };
373
374 static int __init soundbus_i2sbus_init(void)
375 {
376         return macio_register_driver(&i2sbus_drv);
377 }
378
379 static void __exit soundbus_i2sbus_exit(void)
380 {
381         macio_unregister_driver(&i2sbus_drv);
382 }
383
384 module_init(soundbus_i2sbus_init);
385 module_exit(soundbus_i2sbus_exit);