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