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.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 const struct of_device_id i2sbus_match[] = {
35 { .name = "i2s" },
36 { }
37 };
38
39 MODULE_DEVICE_TABLE(of, i2sbus_match);
40
alloc_dbdma_descriptor_ring(struct i2sbus_dev * i2sdev,struct dbdma_command_mem * r,int numcmds)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_alloc_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
free_dbdma_descriptor_ring(struct i2sbus_dev * i2sdev,struct dbdma_command_mem * r)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
i2sbus_release_dev(struct device * dev)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 iounmap(i2sdev->intfregs);
78 iounmap(i2sdev->out.dbdma);
79 iounmap(i2sdev->in.dbdma);
80 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
81 release_and_free_resource(i2sdev->allocated_resource[i]);
82 free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
83 free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
84 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
85 free_irq(i2sdev->interrupts[i], i2sdev);
86 i2sbus_control_remove_dev(i2sdev->control, i2sdev);
87 of_node_put(i2sdev->sound.ofdev.dev.of_node);
88 mutex_destroy(&i2sdev->lock);
89 kfree(i2sdev);
90 }
91
i2sbus_bus_intr(int irq,void * devid)92 static irqreturn_t i2sbus_bus_intr(int irq, void *devid)
93 {
94 struct i2sbus_dev *dev = devid;
95 u32 intreg;
96
97 guard(spinlock)(&dev->low_lock);
98 intreg = in_le32(&dev->intfregs->intr_ctl);
99
100 /* acknowledge interrupt reasons */
101 out_le32(&dev->intfregs->intr_ctl, intreg);
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 */
i2sbus_get_and_fixup_rsrc(struct device_node * np,int index,int layout,struct resource * res)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 /* Returns 1 if added, 0 for otherwise; don't return a negative value! */
i2sbus_add_dev(struct macio_dev * macio,struct i2sbus_control * control,struct device_node * np)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[8];
160 static const char *rnames[] = { "i2sbus: %pOFn (control)",
161 "i2sbus: %pOFn (tx)",
162 "i2sbus: %pOFn (rx)" };
163 static const 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_obj(struct i2sbus_dev);
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 of_node_put(sound);
182 i++;
183 sound = of_node_get(child);
184 }
185 }
186 if (i == 1) {
187 const u32 *id = of_get_property(sound, "layout-id", NULL);
188
189 if (id) {
190 layout = *id;
191 snprintf(dev->sound.modalias, 32,
192 "sound-layout-%d", layout);
193 ok = 1;
194 } else {
195 id = of_get_property(sound, "device-id", NULL);
196 /*
197 * We probably cannot handle all device-id machines,
198 * so restrict to those we do handle for now.
199 */
200 if (id && (*id == 22 || *id == 14 || *id == 35 ||
201 *id == 31 || *id == 44)) {
202 snprintf(dev->sound.modalias, 32,
203 "aoa-device-id-%d", *id);
204 ok = 1;
205 layout = -1;
206 }
207 }
208 }
209 of_node_put(sound);
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 0;
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 = of_node_get(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 = node_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);
240 }
241 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
242 int irq = irq_of_parse_and_map(np, i);
243 if (!irq)
244 goto err;
245 if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
246 goto err;
247 dev->interrupts[i] = irq;
248 }
249
250
251 /* Resource handling is problematic as some device-trees contain
252 * useless crap (ugh ugh ugh). We work around that here by calling
253 * specific functions for calculating the appropriate resources.
254 *
255 * This will all be moved to macio_asic.c at one point
256 */
257 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
258 if (i2sbus_get_and_fixup_rsrc(np,i,layout,&dev->resources[i]))
259 goto err;
260 /* If only we could use our resource dev->resources[i]...
261 * but request_resource doesn't know about parents and
262 * contained resources...
263 */
264 dev->allocated_resource[i] =
265 request_mem_region(dev->resources[i].start,
266 resource_size(&dev->resources[i]),
267 dev->rnames[i]);
268 if (!dev->allocated_resource[i]) {
269 printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
270 goto err;
271 }
272 }
273
274 r = &dev->resources[aoa_resource_i2smmio];
275 rlen = resource_size(r);
276 if (rlen < sizeof(struct i2s_interface_regs))
277 goto err;
278 dev->intfregs = ioremap(r->start, rlen);
279
280 r = &dev->resources[aoa_resource_txdbdma];
281 rlen = resource_size(r);
282 if (rlen < sizeof(struct dbdma_regs))
283 goto err;
284 dev->out.dbdma = ioremap(r->start, rlen);
285
286 r = &dev->resources[aoa_resource_rxdbdma];
287 rlen = resource_size(r);
288 if (rlen < sizeof(struct dbdma_regs))
289 goto err;
290 dev->in.dbdma = ioremap(r->start, rlen);
291
292 if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
293 goto err;
294
295 if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
296 MAX_DBDMA_COMMANDS))
297 goto err;
298 if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
299 MAX_DBDMA_COMMANDS))
300 goto err;
301
302 if (i2sbus_control_add_dev(dev->control, dev)) {
303 printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
304 goto err;
305 }
306
307 if (soundbus_add_one(&dev->sound)) {
308 printk(KERN_DEBUG "i2sbus: device registration error!\n");
309 if (dev->sound.ofdev.dev.kobj.state_initialized) {
310 soundbus_dev_put(&dev->sound);
311 return 0;
312 }
313 goto err;
314 }
315
316 /* enable this cell */
317 i2sbus_control_cell(dev->control, dev, 1);
318 i2sbus_control_enable(dev->control, dev);
319 i2sbus_control_clock(dev->control, dev, 1);
320
321 return 1;
322 err:
323 for (i=0;i<3;i++)
324 if (dev->interrupts[i] != -1)
325 free_irq(dev->interrupts[i], dev);
326 free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
327 free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
328 iounmap(dev->intfregs);
329 iounmap(dev->out.dbdma);
330 iounmap(dev->in.dbdma);
331 for (i=0;i<3;i++)
332 release_and_free_resource(dev->allocated_resource[i]);
333 mutex_destroy(&dev->lock);
334 of_node_put(dev->sound.ofdev.dev.of_node);
335 kfree(dev);
336 return 0;
337 }
338
i2sbus_probe(struct macio_dev * dev,const struct of_device_id * match)339 static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
340 {
341 struct device_node *np;
342 int got = 0, err;
343 struct i2sbus_control *control = NULL;
344
345 err = i2sbus_control_init(dev, &control);
346 if (err)
347 return err;
348 if (!control) {
349 printk(KERN_ERR "i2sbus_control_init API breakage\n");
350 return -ENODEV;
351 }
352
353 for_each_child_of_node(dev->ofdev.dev.of_node, np) {
354 if (of_device_is_compatible(np, "i2sbus") ||
355 of_device_is_compatible(np, "i2s-modem")) {
356 got += i2sbus_add_dev(dev, control, np);
357 }
358 }
359
360 if (!got) {
361 /* found none, clean up */
362 i2sbus_control_destroy(control);
363 return -ENODEV;
364 }
365
366 dev_set_drvdata(&dev->ofdev.dev, control);
367
368 return 0;
369 }
370
i2sbus_remove(struct macio_dev * dev)371 static void i2sbus_remove(struct macio_dev *dev)
372 {
373 struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
374 struct i2sbus_dev *i2sdev, *tmp;
375
376 list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
377 soundbus_remove_one(&i2sdev->sound);
378 }
379
380 #ifdef CONFIG_PM
i2sbus_suspend(struct macio_dev * dev,pm_message_t state)381 static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
382 {
383 struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
384 struct codec_info_item *cii;
385 struct i2sbus_dev* i2sdev;
386 int err, ret = 0;
387
388 list_for_each_entry(i2sdev, &control->list, item) {
389 /* Notify codecs */
390 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
391 err = 0;
392 if (cii->codec->suspend)
393 err = cii->codec->suspend(cii, state);
394 if (err)
395 ret = err;
396 }
397
398 /* wait until streams are stopped */
399 i2sbus_wait_for_stop_both(i2sdev);
400 }
401
402 return ret;
403 }
404
i2sbus_resume(struct macio_dev * dev)405 static int i2sbus_resume(struct macio_dev* dev)
406 {
407 struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
408 struct codec_info_item *cii;
409 struct i2sbus_dev* i2sdev;
410 int err, ret = 0;
411
412 list_for_each_entry(i2sdev, &control->list, item) {
413 if (list_empty(&i2sdev->sound.codec_list))
414 continue;
415
416 /* reset i2s bus format etc. */
417 i2sbus_pcm_prepare_both(i2sdev);
418
419 /* Notify codecs so they can re-initialize */
420 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
421 err = 0;
422 if (cii->codec->resume)
423 err = cii->codec->resume(cii);
424 if (err)
425 ret = err;
426 }
427 }
428
429 return ret;
430 }
431 #endif /* CONFIG_PM */
432
i2sbus_shutdown(struct macio_dev * dev)433 static int i2sbus_shutdown(struct macio_dev* dev)
434 {
435 return 0;
436 }
437
438 static struct macio_driver i2sbus_drv = {
439 .driver = {
440 .name = "soundbus-i2s",
441 .owner = THIS_MODULE,
442 .of_match_table = i2sbus_match,
443 },
444 .probe = i2sbus_probe,
445 .remove = i2sbus_remove,
446 #ifdef CONFIG_PM
447 .suspend = i2sbus_suspend,
448 .resume = i2sbus_resume,
449 #endif
450 .shutdown = i2sbus_shutdown,
451 };
452
soundbus_i2sbus_init(void)453 static int __init soundbus_i2sbus_init(void)
454 {
455 return macio_register_driver(&i2sbus_drv);
456 }
457
soundbus_i2sbus_exit(void)458 static void __exit soundbus_i2sbus_exit(void)
459 {
460 macio_unregister_driver(&i2sbus_drv);
461 }
462
463 module_init(soundbus_i2sbus_init);
464 module_exit(soundbus_i2sbus_exit);
465