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