4 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
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>
15 #include <sound/core.h>
17 #include <asm/macio.h>
18 #include <asm/dbdma.h>
20 #include "../soundbus.h"
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
25 MODULE_DESCRIPTION("Apple Soundbus: I2S support");
28 module_param(force
, int, 0444);
29 MODULE_PARM_DESC(force
, "Force loading i2sbus even when"
30 " no layout-id property is present");
32 static struct of_device_id i2sbus_match
[] = {
37 MODULE_DEVICE_TABLE(of
, i2sbus_match
);
39 static int alloc_dbdma_descriptor_ring(struct i2sbus_dev
*i2sdev
,
40 struct dbdma_command_mem
*r
,
43 /* one more for rounding, one for branch back, one for stop command */
44 r
->size
= (numcmds
+ 3) * sizeof(struct dbdma_cmd
);
45 /* We use the PCI APIs for now until the generic one gets fixed
46 * enough or until we get some macio-specific versions
48 r
->space
= dma_alloc_coherent(
49 &macio_get_pci_dev(i2sdev
->macio
)->dev
,
54 if (!r
->space
) return -ENOMEM
;
56 memset(r
->space
, 0, r
->size
);
57 r
->cmds
= (void*)DBDMA_ALIGN(r
->space
);
58 r
->bus_cmd_start
= r
->bus_addr
+
59 (dma_addr_t
)((char*)r
->cmds
- (char*)r
->space
);
64 static void free_dbdma_descriptor_ring(struct i2sbus_dev
*i2sdev
,
65 struct dbdma_command_mem
*r
)
67 if (!r
->space
) return;
69 dma_free_coherent(&macio_get_pci_dev(i2sdev
->macio
)->dev
,
70 r
->size
, r
->space
, r
->bus_addr
);
73 static void i2sbus_release_dev(struct device
*dev
)
75 struct i2sbus_dev
*i2sdev
;
78 i2sdev
= container_of(dev
, struct i2sbus_dev
, sound
.ofdev
.dev
);
80 if (i2sdev
->intfregs
) iounmap(i2sdev
->intfregs
);
81 if (i2sdev
->out
.dbdma
) iounmap(i2sdev
->out
.dbdma
);
82 if (i2sdev
->in
.dbdma
) iounmap(i2sdev
->in
.dbdma
);
83 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++)
84 if (i2sdev
->allocated_resource
[i
])
85 release_and_free_resource(i2sdev
->allocated_resource
[i
]);
86 free_dbdma_descriptor_ring(i2sdev
, &i2sdev
->out
.dbdma_ring
);
87 free_dbdma_descriptor_ring(i2sdev
, &i2sdev
->in
.dbdma_ring
);
88 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++)
89 free_irq(i2sdev
->interrupts
[i
], i2sdev
);
90 i2sbus_control_remove_dev(i2sdev
->control
, i2sdev
);
91 mutex_destroy(&i2sdev
->lock
);
95 static irqreturn_t
i2sbus_bus_intr(int irq
, void *devid
)
97 struct i2sbus_dev
*dev
= devid
;
100 spin_lock(&dev
->low_lock
);
101 intreg
= in_le32(&dev
->intfregs
->intr_ctl
);
103 /* acknowledge interrupt reasons */
104 out_le32(&dev
->intfregs
->intr_ctl
, intreg
);
106 spin_unlock(&dev
->low_lock
);
113 * XXX FIXME: We test the layout_id's here to get the proper way of
114 * mapping in various registers, thanks to bugs in Apple device-trees.
115 * We could instead key off the machine model and the name of the i2s
116 * node (i2s-a). This we'll do when we move it all to macio_asic.c
117 * and have that export items for each sub-node too.
119 static int i2sbus_get_and_fixup_rsrc(struct device_node
*np
, int index
,
120 int layout
, struct resource
*res
)
122 struct device_node
*parent
;
123 int pindex
, rc
= -ENXIO
;
126 /* Machines with layout 76 and 36 (K2 based) have a weird device
127 * tree what we need to special case.
128 * Normal machines just fetch the resource from the i2s-X node.
129 * Darwin further divides normal machines into old and new layouts
130 * with a subtely different code path but that doesn't seem necessary
131 * in practice, they just bloated it. In addition, even on our K2
132 * case the i2s-modem node, if we ever want to handle it, uses the
135 if (layout
!= 76 && layout
!= 36)
136 return of_address_to_resource(np
, index
, res
);
138 parent
= of_get_parent(np
);
139 pindex
= (index
== aoa_resource_i2smmio
) ? 0 : 1;
140 rc
= of_address_to_resource(parent
, pindex
, res
);
143 reg
= of_get_property(np
, "reg", NULL
);
148 res
->start
+= reg
[index
* 2];
149 res
->end
= res
->start
+ reg
[index
* 2 + 1] - 1;
155 /* FIXME: look at device node refcounting */
156 static int i2sbus_add_dev(struct macio_dev
*macio
,
157 struct i2sbus_control
*control
,
158 struct device_node
*np
)
160 struct i2sbus_dev
*dev
;
161 struct device_node
*child
= NULL
, *sound
= NULL
;
163 int i
, layout
= 0, rlen
, ok
= force
;
164 static const char *rnames
[] = { "i2sbus: %s (control)",
167 static irq_handler_t ints
[] = {
173 if (strlen(np
->name
) != 5)
175 if (strncmp(np
->name
, "i2s-", 4))
178 dev
= kzalloc(sizeof(struct i2sbus_dev
), GFP_KERNEL
);
183 while ((child
= of_get_next_child(np
, child
))) {
184 if (strcmp(child
->name
, "sound") == 0) {
190 const u32
*id
= of_get_property(sound
, "layout-id", NULL
);
194 snprintf(dev
->sound
.modalias
, 32,
195 "sound-layout-%d", layout
);
198 id
= of_get_property(sound
, "device-id", NULL
);
200 * We probably cannot handle all device-id machines,
201 * so restrict to those we do handle for now.
203 if (id
&& (*id
== 22 || *id
== 14 || *id
== 35)) {
204 snprintf(dev
->sound
.modalias
, 32,
205 "aoa-device-id-%d", *id
);
211 /* for the time being, until we can handle non-layout-id
212 * things in some fabric, refuse to attach if there is no
213 * layout-id property or we haven't been forced to attach.
214 * When there are two i2s busses and only one has a layout-id,
215 * then this depends on the order, but that isn't important
216 * either as the second one in that case is just a modem. */
222 mutex_init(&dev
->lock
);
223 spin_lock_init(&dev
->low_lock
);
224 dev
->sound
.ofdev
.archdata
.dma_mask
= macio
->ofdev
.archdata
.dma_mask
;
225 dev
->sound
.ofdev
.dev
.of_node
= np
;
226 dev
->sound
.ofdev
.dev
.dma_mask
= &dev
->sound
.ofdev
.archdata
.dma_mask
;
227 dev
->sound
.ofdev
.dev
.parent
= &macio
->ofdev
.dev
;
228 dev
->sound
.ofdev
.dev
.release
= i2sbus_release_dev
;
229 dev
->sound
.attach_codec
= i2sbus_attach_codec
;
230 dev
->sound
.detach_codec
= i2sbus_detach_codec
;
231 dev
->sound
.pcmid
= -1;
233 dev
->control
= control
;
234 dev
->bus_number
= np
->name
[4] - 'a';
235 INIT_LIST_HEAD(&dev
->sound
.codec_list
);
237 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++) {
238 dev
->interrupts
[i
] = -1;
239 snprintf(dev
->rnames
[i
], sizeof(dev
->rnames
[i
]),
240 rnames
[i
], np
->name
);
242 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++) {
243 int irq
= irq_of_parse_and_map(np
, i
);
244 if (request_irq(irq
, ints
[i
], 0, dev
->rnames
[i
], dev
))
246 dev
->interrupts
[i
] = irq
;
250 /* Resource handling is problematic as some device-trees contain
251 * useless crap (ugh ugh ugh). We work around that here by calling
252 * specific functions for calculating the appropriate resources.
254 * This will all be moved to macio_asic.c at one point
256 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++) {
257 if (i2sbus_get_and_fixup_rsrc(np
,i
,layout
,&dev
->resources
[i
]))
259 /* If only we could use our resource dev->resources[i]...
260 * but request_resource doesn't know about parents and
261 * contained resources...
263 dev
->allocated_resource
[i
] =
264 request_mem_region(dev
->resources
[i
].start
,
265 resource_size(&dev
->resources
[i
]),
267 if (!dev
->allocated_resource
[i
]) {
268 printk(KERN_ERR
"i2sbus: failed to claim resource %d!\n", i
);
273 r
= &dev
->resources
[aoa_resource_i2smmio
];
274 rlen
= resource_size(r
);
275 if (rlen
< sizeof(struct i2s_interface_regs
))
277 dev
->intfregs
= ioremap(r
->start
, rlen
);
279 r
= &dev
->resources
[aoa_resource_txdbdma
];
280 rlen
= resource_size(r
);
281 if (rlen
< sizeof(struct dbdma_regs
))
283 dev
->out
.dbdma
= ioremap(r
->start
, rlen
);
285 r
= &dev
->resources
[aoa_resource_rxdbdma
];
286 rlen
= resource_size(r
);
287 if (rlen
< sizeof(struct dbdma_regs
))
289 dev
->in
.dbdma
= ioremap(r
->start
, rlen
);
291 if (!dev
->intfregs
|| !dev
->out
.dbdma
|| !dev
->in
.dbdma
)
294 if (alloc_dbdma_descriptor_ring(dev
, &dev
->out
.dbdma_ring
,
297 if (alloc_dbdma_descriptor_ring(dev
, &dev
->in
.dbdma_ring
,
301 if (i2sbus_control_add_dev(dev
->control
, dev
)) {
302 printk(KERN_ERR
"i2sbus: control layer didn't like bus\n");
306 if (soundbus_add_one(&dev
->sound
)) {
307 printk(KERN_DEBUG
"i2sbus: device registration error!\n");
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);
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
);
327 if (dev
->allocated_resource
[i
])
328 release_and_free_resource(dev
->allocated_resource
[i
]);
329 mutex_destroy(&dev
->lock
);
334 static int i2sbus_probe(struct macio_dev
* dev
, const struct of_device_id
*match
)
336 struct device_node
*np
= NULL
;
338 struct i2sbus_control
*control
= NULL
;
340 err
= i2sbus_control_init(dev
, &control
);
344 printk(KERN_ERR
"i2sbus_control_init API breakage\n");
348 while ((np
= of_get_next_child(dev
->ofdev
.dev
.of_node
, np
))) {
349 if (of_device_is_compatible(np
, "i2sbus") ||
350 of_device_is_compatible(np
, "i2s-modem")) {
351 got
+= i2sbus_add_dev(dev
, control
, np
);
356 /* found none, clean up */
357 i2sbus_control_destroy(control
);
361 dev_set_drvdata(&dev
->ofdev
.dev
, control
);
366 static int i2sbus_remove(struct macio_dev
* dev
)
368 struct i2sbus_control
*control
= dev_get_drvdata(&dev
->ofdev
.dev
);
369 struct i2sbus_dev
*i2sdev
, *tmp
;
371 list_for_each_entry_safe(i2sdev
, tmp
, &control
->list
, item
)
372 soundbus_remove_one(&i2sdev
->sound
);
378 static int i2sbus_suspend(struct macio_dev
* dev
, pm_message_t state
)
380 struct i2sbus_control
*control
= dev_get_drvdata(&dev
->ofdev
.dev
);
381 struct codec_info_item
*cii
;
382 struct i2sbus_dev
* i2sdev
;
385 list_for_each_entry(i2sdev
, &control
->list
, item
) {
387 if (i2sdev
->sound
.pcm
) {
388 /* Suspend PCM streams */
389 snd_pcm_suspend_all(i2sdev
->sound
.pcm
);
393 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
395 if (cii
->codec
->suspend
)
396 err
= cii
->codec
->suspend(cii
, state
);
401 /* wait until streams are stopped */
402 i2sbus_wait_for_stop_both(i2sdev
);
408 static int i2sbus_resume(struct macio_dev
* dev
)
410 struct i2sbus_control
*control
= dev_get_drvdata(&dev
->ofdev
.dev
);
411 struct codec_info_item
*cii
;
412 struct i2sbus_dev
* i2sdev
;
415 list_for_each_entry(i2sdev
, &control
->list
, item
) {
416 /* reset i2s bus format etc. */
417 i2sbus_pcm_prepare_both(i2sdev
);
419 /* Notify codecs so they can re-initialize */
420 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
422 if (cii
->codec
->resume
)
423 err
= cii
->codec
->resume(cii
);
431 #endif /* CONFIG_PM */
433 static int i2sbus_shutdown(struct macio_dev
* dev
)
438 static struct macio_driver i2sbus_drv
= {
440 .name
= "soundbus-i2s",
441 .owner
= THIS_MODULE
,
442 .of_match_table
= i2sbus_match
,
444 .probe
= i2sbus_probe
,
445 .remove
= i2sbus_remove
,
447 .suspend
= i2sbus_suspend
,
448 .resume
= i2sbus_resume
,
450 .shutdown
= i2sbus_shutdown
,
453 static int __init
soundbus_i2sbus_init(void)
455 return macio_register_driver(&i2sbus_drv
);
458 static void __exit
soundbus_i2sbus_exit(void)
460 macio_unregister_driver(&i2sbus_drv
);
463 module_init(soundbus_i2sbus_init
);
464 module_exit(soundbus_i2sbus_exit
);