blob: 4e3b819d49930f07b7ce641c1de0a32b878f8bcd [file] [log] [blame]
Johannes Bergf3d94782006-06-21 15:42:43 +02001/*
2 * i2sbus driver
3 *
Johannes Berg45e513b2009-01-15 18:21:48 +01004 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergf3d94782006-06-21 15:42:43 +02005 *
6 * GPL v2, can be found in COPYING.
7 */
8
9#include <linux/module.h>
Johannes Bergf3d94782006-06-21 15:42:43 +020010#include <linux/pci.h>
11#include <linux/interrupt.h>
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -070012#include <linux/dma-mapping.h>
13
Johannes Bergf3d94782006-06-21 15:42:43 +020014#include <sound/core.h>
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -070015
16#include <asm/macio.h>
17#include <asm/dbdma.h>
18
Johannes Bergf3d94782006-06-21 15:42:43 +020019#include "../soundbus.h"
20#include "i2sbus.h"
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
24MODULE_DESCRIPTION("Apple Soundbus: I2S support");
Johannes Bergf3d94782006-06-21 15:42:43 +020025
Benjamin Herrenschmidtf9d08de2006-07-10 04:44:34 -070026static int force;
27module_param(force, int, 0444);
28MODULE_PARM_DESC(force, "Force loading i2sbus even when"
29 " no layout-id property is present");
30
Johannes Bergf3d94782006-06-21 15:42:43 +020031static struct of_device_id i2sbus_match[] = {
32 { .name = "i2s" },
33 { }
34};
35
Johannes Berge3f96782007-03-28 13:42:25 +020036MODULE_DEVICE_TABLE(of, i2sbus_match);
37
Johannes Bergf3d94782006-06-21 15:42:43 +020038static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
39 struct dbdma_command_mem *r,
40 int numcmds)
41{
Paul Mackerras547ac2a2007-02-08 14:25:39 +010042 /* one more for rounding, one for branch back, one for stop command */
43 r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
Johannes Bergf3d94782006-06-21 15:42:43 +020044 /* We use the PCI APIs for now until the generic one gets fixed
45 * enough or until we get some macio-specific versions
46 */
47 r->space = dma_alloc_coherent(
48 &macio_get_pci_dev(i2sdev->macio)->dev,
49 r->size,
50 &r->bus_addr,
51 GFP_KERNEL);
52
53 if (!r->space) return -ENOMEM;
54
55 memset(r->space, 0, r->size);
56 r->cmds = (void*)DBDMA_ALIGN(r->space);
57 r->bus_cmd_start = r->bus_addr +
58 (dma_addr_t)((char*)r->cmds - (char*)r->space);
59
60 return 0;
61}
62
63static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
64 struct dbdma_command_mem *r)
65{
66 if (!r->space) return;
Johannes Berg888dcb72008-10-23 15:47:56 +020067
Johannes Bergf3d94782006-06-21 15:42:43 +020068 dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
69 r->size, r->space, r->bus_addr);
70}
71
72static void i2sbus_release_dev(struct device *dev)
73{
74 struct i2sbus_dev *i2sdev;
75 int i;
76
77 i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
78
79 if (i2sdev->intfregs) iounmap(i2sdev->intfregs);
80 if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma);
81 if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma);
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -070082 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
Johannes Bergf3d94782006-06-21 15:42:43 +020083 if (i2sdev->allocated_resource[i])
84 release_and_free_resource(i2sdev->allocated_resource[i]);
85 free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
86 free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -070087 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
Johannes Bergf3d94782006-06-21 15:42:43 +020088 free_irq(i2sdev->interrupts[i], i2sdev);
89 i2sbus_control_remove_dev(i2sdev->control, i2sdev);
90 mutex_destroy(&i2sdev->lock);
91 kfree(i2sdev);
92}
93
David Howells7d12e782006-10-05 14:55:46 +010094static irqreturn_t i2sbus_bus_intr(int irq, void *devid)
Johannes Bergf3d94782006-06-21 15:42:43 +020095{
96 struct i2sbus_dev *dev = devid;
97 u32 intreg;
98
99 spin_lock(&dev->low_lock);
100 intreg = in_le32(&dev->intfregs->intr_ctl);
101
102 /* acknowledge interrupt reasons */
103 out_le32(&dev->intfregs->intr_ctl, intreg);
104
105 spin_unlock(&dev->low_lock);
106
107 return IRQ_HANDLED;
108}
109
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700110
111/*
112 * XXX FIXME: We test the layout_id's here to get the proper way of
113 * mapping in various registers, thanks to bugs in Apple device-trees.
114 * We could instead key off the machine model and the name of the i2s
115 * node (i2s-a). This we'll do when we move it all to macio_asic.c
116 * and have that export items for each sub-node too.
117 */
118static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index,
119 int layout, struct resource *res)
120{
121 struct device_node *parent;
122 int pindex, rc = -ENXIO;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000123 const u32 *reg;
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700124
125 /* Machines with layout 76 and 36 (K2 based) have a weird device
126 * tree what we need to special case.
127 * Normal machines just fetch the resource from the i2s-X node.
128 * Darwin further divides normal machines into old and new layouts
129 * with a subtely different code path but that doesn't seem necessary
130 * in practice, they just bloated it. In addition, even on our K2
131 * case the i2s-modem node, if we ever want to handle it, uses the
132 * normal layout
133 */
134 if (layout != 76 && layout != 36)
135 return of_address_to_resource(np, index, res);
136
137 parent = of_get_parent(np);
138 pindex = (index == aoa_resource_i2smmio) ? 0 : 1;
139 rc = of_address_to_resource(parent, pindex, res);
140 if (rc)
141 goto bail;
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000142 reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700143 if (reg == NULL) {
144 rc = -ENXIO;
145 goto bail;
146 }
147 res->start += reg[index * 2];
148 res->end = res->start + reg[index * 2 + 1] - 1;
149 bail:
150 of_node_put(parent);
151 return rc;
152}
153
Johannes Bergf3d94782006-06-21 15:42:43 +0200154/* FIXME: look at device node refcounting */
155static int i2sbus_add_dev(struct macio_dev *macio,
156 struct i2sbus_control *control,
157 struct device_node *np)
158{
159 struct i2sbus_dev *dev;
160 struct device_node *child = NULL, *sound = NULL;
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700161 struct resource *r;
Johannes Berg9f50bba2008-10-23 13:57:39 +0200162 int i, layout = 0, rlen, ok = force;
Johannes Bergf3d94782006-06-21 15:42:43 +0200163 static const char *rnames[] = { "i2sbus: %s (control)",
164 "i2sbus: %s (tx)",
165 "i2sbus: %s (rx)" };
David Howells7d12e782006-10-05 14:55:46 +0100166 static irq_handler_t ints[] = {
Johannes Bergf3d94782006-06-21 15:42:43 +0200167 i2sbus_bus_intr,
168 i2sbus_tx_intr,
169 i2sbus_rx_intr
170 };
171
172 if (strlen(np->name) != 5)
173 return 0;
174 if (strncmp(np->name, "i2s-", 4))
175 return 0;
176
Johannes Bergf3d94782006-06-21 15:42:43 +0200177 dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
178 if (!dev)
179 return 0;
180
181 i = 0;
182 while ((child = of_get_next_child(np, child))) {
183 if (strcmp(child->name, "sound") == 0) {
184 i++;
185 sound = child;
186 }
187 }
188 if (i == 1) {
Johannes Berg45e513b2009-01-15 18:21:48 +0100189 const u32 *id = of_get_property(sound, "layout-id", NULL);
190
191 if (id) {
192 layout = *id;
Johannes Bergf3d94782006-06-21 15:42:43 +0200193 snprintf(dev->sound.modalias, 32,
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700194 "sound-layout-%d", layout);
Johannes Berg9f50bba2008-10-23 13:57:39 +0200195 ok = 1;
Johannes Berg45e513b2009-01-15 18:21:48 +0100196 } else {
197 id = of_get_property(sound, "device-id", NULL);
198 /*
199 * We probably cannot handle all device-id machines,
200 * so restrict to those we do handle for now.
201 */
202 if (id && (*id == 22 || *id == 14 || *id == 35)) {
203 snprintf(dev->sound.modalias, 32,
204 "aoa-device-id-%d", *id);
205 ok = 1;
206 layout = -1;
207 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200208 }
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. */
Johannes Berg9f50bba2008-10-23 13:57:39 +0200216 if (!ok) {
Johannes Bergf3d94782006-06-21 15:42:43 +0200217 kfree(dev);
218 return -ENODEV;
219 }
220
221 mutex_init(&dev->lock);
222 spin_lock_init(&dev->low_lock);
223 dev->sound.ofdev.node = np;
224 dev->sound.ofdev.dma_mask = macio->ofdev.dma_mask;
225 dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.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
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700236 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
Johannes Bergf3d94782006-06-21 15:42:43 +0200237 dev->interrupts[i] = -1;
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700238 snprintf(dev->rnames[i], sizeof(dev->rnames[i]),
239 rnames[i], np->name);
Johannes Bergf3d94782006-06-21 15:42:43 +0200240 }
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700241 for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
Andreas Schwab4a14cf42006-07-10 04:44:33 -0700242 int irq = irq_of_parse_and_map(np, i);
243 if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
Johannes Bergf3d94782006-06-21 15:42:43 +0200244 goto err;
Andreas Schwab4a14cf42006-07-10 04:44:33 -0700245 dev->interrupts[i] = irq;
Johannes Bergf3d94782006-06-21 15:42:43 +0200246 }
247
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700248
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]))
Johannes Bergf3d94782006-06-21 15:42:43 +0200257 goto err;
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700258 /* If only we could use our resource dev->resources[i]...
Johannes Bergf3d94782006-06-21 15:42:43 +0200259 * but request_resource doesn't know about parents and
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700260 * contained resources...
261 */
Johannes Berg888dcb72008-10-23 15:47:56 +0200262 dev->allocated_resource[i] =
Johannes Bergf3d94782006-06-21 15:42:43 +0200263 request_mem_region(dev->resources[i].start,
264 dev->resources[i].end -
265 dev->resources[i].start + 1,
266 dev->rnames[i]);
267 if (!dev->allocated_resource[i]) {
268 printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
269 goto err;
270 }
271 }
Benjamin Herrenschmidt389ba792006-07-10 04:44:34 -0700272
273 r = &dev->resources[aoa_resource_i2smmio];
274 rlen = r->end - r->start + 1;
275 if (rlen < sizeof(struct i2s_interface_regs))
276 goto err;
277 dev->intfregs = ioremap(r->start, rlen);
278
279 r = &dev->resources[aoa_resource_txdbdma];
280 rlen = r->end - r->start + 1;
281 if (rlen < sizeof(struct dbdma_regs))
282 goto err;
283 dev->out.dbdma = ioremap(r->start, rlen);
284
285 r = &dev->resources[aoa_resource_rxdbdma];
286 rlen = r->end - r->start + 1;
287 if (rlen < sizeof(struct dbdma_regs))
288 goto err;
289 dev->in.dbdma = ioremap(r->start, rlen);
290
Johannes Bergf3d94782006-06-21 15:42:43 +0200291 if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
292 goto err;
293
294 if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
295 MAX_DBDMA_COMMANDS))
296 goto err;
297 if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
298 MAX_DBDMA_COMMANDS))
299 goto err;
300
301 if (i2sbus_control_add_dev(dev->control, dev)) {
302 printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
303 goto err;
304 }
305
306 if (soundbus_add_one(&dev->sound)) {
307 printk(KERN_DEBUG "i2sbus: device registration error!\n");
308 goto err;
309 }
310
311 /* enable this cell */
312 i2sbus_control_cell(dev->control, dev, 1);
313 i2sbus_control_enable(dev->control, dev);
314 i2sbus_control_clock(dev->control, dev, 1);
315
316 return 1;
317 err:
318 for (i=0;i<3;i++)
319 if (dev->interrupts[i] != -1)
320 free_irq(dev->interrupts[i], dev);
321 free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
322 free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
323 if (dev->intfregs) iounmap(dev->intfregs);
324 if (dev->out.dbdma) iounmap(dev->out.dbdma);
325 if (dev->in.dbdma) iounmap(dev->in.dbdma);
326 for (i=0;i<3;i++)
327 if (dev->allocated_resource[i])
328 release_and_free_resource(dev->allocated_resource[i]);
329 mutex_destroy(&dev->lock);
330 kfree(dev);
331 return 0;
332}
333
334static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
335{
336 struct device_node *np = NULL;
337 int got = 0, err;
338 struct i2sbus_control *control = NULL;
339
340 err = i2sbus_control_init(dev, &control);
341 if (err)
342 return err;
343 if (!control) {
344 printk(KERN_ERR "i2sbus_control_init API breakage\n");
345 return -ENODEV;
346 }
347
348 while ((np = of_get_next_child(dev->ofdev.node, np))) {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000349 if (of_device_is_compatible(np, "i2sbus") ||
350 of_device_is_compatible(np, "i2s-modem")) {
Johannes Bergf3d94782006-06-21 15:42:43 +0200351 got += i2sbus_add_dev(dev, control, np);
352 }
353 }
354
355 if (!got) {
356 /* found none, clean up */
357 i2sbus_control_destroy(control);
358 return -ENODEV;
359 }
360
Greg Kroah-Hartmanae31c1f2009-05-04 12:40:54 -0700361 dev_set_drvdata(&dev->ofdev.dev, control);
Johannes Bergf3d94782006-06-21 15:42:43 +0200362
363 return 0;
364}
365
366static int i2sbus_remove(struct macio_dev* dev)
367{
Greg Kroah-Hartmanae31c1f2009-05-04 12:40:54 -0700368 struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
Johannes Bergf3d94782006-06-21 15:42:43 +0200369 struct i2sbus_dev *i2sdev, *tmp;
370
371 list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
372 soundbus_remove_one(&i2sdev->sound);
373
374 return 0;
375}
376
377#ifdef CONFIG_PM
378static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
379{
Greg Kroah-Hartmanae31c1f2009-05-04 12:40:54 -0700380 struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
Johannes Bergf3d94782006-06-21 15:42:43 +0200381 struct codec_info_item *cii;
382 struct i2sbus_dev* i2sdev;
383 int err, ret = 0;
384
385 list_for_each_entry(i2sdev, &control->list, item) {
386 /* Notify Alsa */
387 if (i2sdev->sound.pcm) {
388 /* Suspend PCM streams */
389 snd_pcm_suspend_all(i2sdev->sound.pcm);
Johannes Bergf3d94782006-06-21 15:42:43 +0200390 }
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100391
Johannes Bergf3d94782006-06-21 15:42:43 +0200392 /* Notify codecs */
393 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
394 err = 0;
395 if (cii->codec->suspend)
396 err = cii->codec->suspend(cii, state);
397 if (err)
398 ret = err;
399 }
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100400
401 /* wait until streams are stopped */
402 i2sbus_wait_for_stop_both(i2sdev);
Johannes Bergf3d94782006-06-21 15:42:43 +0200403 }
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100404
Johannes Bergf3d94782006-06-21 15:42:43 +0200405 return ret;
406}
407
408static int i2sbus_resume(struct macio_dev* dev)
409{
Greg Kroah-Hartmanae31c1f2009-05-04 12:40:54 -0700410 struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
Johannes Bergf3d94782006-06-21 15:42:43 +0200411 struct codec_info_item *cii;
412 struct i2sbus_dev* i2sdev;
413 int err, ret = 0;
414
415 list_for_each_entry(i2sdev, &control->list, item) {
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100416 /* reset i2s bus format etc. */
417 i2sbus_pcm_prepare_both(i2sdev);
418
Johannes Bergf3d94782006-06-21 15:42:43 +0200419 /* 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 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200427 }
428
429 return ret;
430}
431#endif /* CONFIG_PM */
432
433static int i2sbus_shutdown(struct macio_dev* dev)
434{
435 return 0;
436}
437
438static struct macio_driver i2sbus_drv = {
439 .name = "soundbus-i2s",
440 .owner = THIS_MODULE,
441 .match_table = i2sbus_match,
442 .probe = i2sbus_probe,
443 .remove = i2sbus_remove,
444#ifdef CONFIG_PM
445 .suspend = i2sbus_suspend,
446 .resume = i2sbus_resume,
447#endif
448 .shutdown = i2sbus_shutdown,
449};
450
451static int __init soundbus_i2sbus_init(void)
452{
453 return macio_register_driver(&i2sbus_drv);
454}
455
456static void __exit soundbus_i2sbus_exit(void)
457{
458 macio_unregister_driver(&i2sbus_drv);
459}
460
461module_init(soundbus_i2sbus_init);
462module_exit(soundbus_i2sbus_exit);