4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/interrupt.h>
12 #include <linux/dma-mapping.h>
14 #include <sound/driver.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");
26 /* for auto-loading, declare that we handle this weird
27 * string that macio puts into the relevant device */
28 MODULE_ALIAS("of:Ni2sTi2sC");
31 module_param(force
, int, 0444);
32 MODULE_PARM_DESC(force
, "Force loading i2sbus even when"
33 " no layout-id property is present");
35 static struct of_device_id i2sbus_match
[] = {
40 static int alloc_dbdma_descriptor_ring(struct i2sbus_dev
*i2sdev
,
41 struct dbdma_command_mem
*r
,
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
49 r
->space
= dma_alloc_coherent(
50 &macio_get_pci_dev(i2sdev
->macio
)->dev
,
55 if (!r
->space
) return -ENOMEM
;
57 memset(r
->space
, 0, r
->size
);
58 r
->cmds
= (void*)DBDMA_ALIGN(r
->space
);
59 r
->bus_cmd_start
= r
->bus_addr
+
60 (dma_addr_t
)((char*)r
->cmds
- (char*)r
->space
);
65 static void free_dbdma_descriptor_ring(struct i2sbus_dev
*i2sdev
,
66 struct dbdma_command_mem
*r
)
68 if (!r
->space
) return;
70 dma_free_coherent(&macio_get_pci_dev(i2sdev
->macio
)->dev
,
71 r
->size
, r
->space
, r
->bus_addr
);
74 static void i2sbus_release_dev(struct device
*dev
)
76 struct i2sbus_dev
*i2sdev
;
79 i2sdev
= container_of(dev
, struct i2sbus_dev
, sound
.ofdev
.dev
);
81 if (i2sdev
->intfregs
) iounmap(i2sdev
->intfregs
);
82 if (i2sdev
->out
.dbdma
) iounmap(i2sdev
->out
.dbdma
);
83 if (i2sdev
->in
.dbdma
) iounmap(i2sdev
->in
.dbdma
);
84 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++)
85 if (i2sdev
->allocated_resource
[i
])
86 release_and_free_resource(i2sdev
->allocated_resource
[i
]);
87 free_dbdma_descriptor_ring(i2sdev
, &i2sdev
->out
.dbdma_ring
);
88 free_dbdma_descriptor_ring(i2sdev
, &i2sdev
->in
.dbdma_ring
);
89 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++)
90 free_irq(i2sdev
->interrupts
[i
], i2sdev
);
91 i2sbus_control_remove_dev(i2sdev
->control
, i2sdev
);
92 mutex_destroy(&i2sdev
->lock
);
96 static irqreturn_t
i2sbus_bus_intr(int irq
, void *devid
)
98 struct i2sbus_dev
*dev
= devid
;
101 spin_lock(&dev
->low_lock
);
102 intreg
= in_le32(&dev
->intfregs
->intr_ctl
);
104 /* acknowledge interrupt reasons */
105 out_le32(&dev
->intfregs
->intr_ctl
, intreg
);
107 spin_unlock(&dev
->low_lock
);
114 * XXX FIXME: We test the layout_id's here to get the proper way of
115 * mapping in various registers, thanks to bugs in Apple device-trees.
116 * We could instead key off the machine model and the name of the i2s
117 * node (i2s-a). This we'll do when we move it all to macio_asic.c
118 * and have that export items for each sub-node too.
120 static int i2sbus_get_and_fixup_rsrc(struct device_node
*np
, int index
,
121 int layout
, struct resource
*res
)
123 struct device_node
*parent
;
124 int pindex
, rc
= -ENXIO
;
127 /* Machines with layout 76 and 36 (K2 based) have a weird device
128 * tree what we need to special case.
129 * Normal machines just fetch the resource from the i2s-X node.
130 * Darwin further divides normal machines into old and new layouts
131 * with a subtely different code path but that doesn't seem necessary
132 * in practice, they just bloated it. In addition, even on our K2
133 * case the i2s-modem node, if we ever want to handle it, uses the
136 if (layout
!= 76 && layout
!= 36)
137 return of_address_to_resource(np
, index
, res
);
139 parent
= of_get_parent(np
);
140 pindex
= (index
== aoa_resource_i2smmio
) ? 0 : 1;
141 rc
= of_address_to_resource(parent
, pindex
, res
);
144 reg
= of_get_property(np
, "reg", NULL
);
149 res
->start
+= reg
[index
* 2];
150 res
->end
= res
->start
+ reg
[index
* 2 + 1] - 1;
156 /* FIXME: look at device node refcounting */
157 static int i2sbus_add_dev(struct macio_dev
*macio
,
158 struct i2sbus_control
*control
,
159 struct device_node
*np
)
161 struct i2sbus_dev
*dev
;
162 struct device_node
*child
= NULL
, *sound
= NULL
;
164 int i
, layout
= 0, rlen
;
165 static const char *rnames
[] = { "i2sbus: %s (control)",
168 static irq_handler_t ints
[] = {
174 if (strlen(np
->name
) != 5)
176 if (strncmp(np
->name
, "i2s-", 4))
179 dev
= kzalloc(sizeof(struct i2sbus_dev
), GFP_KERNEL
);
184 while ((child
= of_get_next_child(np
, child
))) {
185 if (strcmp(child
->name
, "sound") == 0) {
191 const u32
*layout_id
=
192 of_get_property(sound
, "layout-id", NULL
);
195 snprintf(dev
->sound
.modalias
, 32,
196 "sound-layout-%d", layout
);
200 /* for the time being, until we can handle non-layout-id
201 * things in some fabric, refuse to attach if there is no
202 * layout-id property or we haven't been forced to attach.
203 * When there are two i2s busses and only one has a layout-id,
204 * then this depends on the order, but that isn't important
205 * either as the second one in that case is just a modem. */
211 mutex_init(&dev
->lock
);
212 spin_lock_init(&dev
->low_lock
);
213 dev
->sound
.ofdev
.node
= np
;
214 dev
->sound
.ofdev
.dma_mask
= macio
->ofdev
.dma_mask
;
215 dev
->sound
.ofdev
.dev
.dma_mask
= &dev
->sound
.ofdev
.dma_mask
;
216 dev
->sound
.ofdev
.dev
.parent
= &macio
->ofdev
.dev
;
217 dev
->sound
.ofdev
.dev
.release
= i2sbus_release_dev
;
218 dev
->sound
.attach_codec
= i2sbus_attach_codec
;
219 dev
->sound
.detach_codec
= i2sbus_detach_codec
;
220 dev
->sound
.pcmid
= -1;
222 dev
->control
= control
;
223 dev
->bus_number
= np
->name
[4] - 'a';
224 INIT_LIST_HEAD(&dev
->sound
.codec_list
);
226 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++) {
227 dev
->interrupts
[i
] = -1;
228 snprintf(dev
->rnames
[i
], sizeof(dev
->rnames
[i
]),
229 rnames
[i
], np
->name
);
231 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++) {
232 int irq
= irq_of_parse_and_map(np
, i
);
233 if (request_irq(irq
, ints
[i
], 0, dev
->rnames
[i
], dev
))
235 dev
->interrupts
[i
] = irq
;
239 /* Resource handling is problematic as some device-trees contain
240 * useless crap (ugh ugh ugh). We work around that here by calling
241 * specific functions for calculating the appropriate resources.
243 * This will all be moved to macio_asic.c at one point
245 for (i
= aoa_resource_i2smmio
; i
<= aoa_resource_rxdbdma
; i
++) {
246 if (i2sbus_get_and_fixup_rsrc(np
,i
,layout
,&dev
->resources
[i
]))
248 /* If only we could use our resource dev->resources[i]...
249 * but request_resource doesn't know about parents and
250 * contained resources...
252 dev
->allocated_resource
[i
] =
253 request_mem_region(dev
->resources
[i
].start
,
254 dev
->resources
[i
].end
-
255 dev
->resources
[i
].start
+ 1,
257 if (!dev
->allocated_resource
[i
]) {
258 printk(KERN_ERR
"i2sbus: failed to claim resource %d!\n", i
);
263 r
= &dev
->resources
[aoa_resource_i2smmio
];
264 rlen
= r
->end
- r
->start
+ 1;
265 if (rlen
< sizeof(struct i2s_interface_regs
))
267 dev
->intfregs
= ioremap(r
->start
, rlen
);
269 r
= &dev
->resources
[aoa_resource_txdbdma
];
270 rlen
= r
->end
- r
->start
+ 1;
271 if (rlen
< sizeof(struct dbdma_regs
))
273 dev
->out
.dbdma
= ioremap(r
->start
, rlen
);
275 r
= &dev
->resources
[aoa_resource_rxdbdma
];
276 rlen
= r
->end
- r
->start
+ 1;
277 if (rlen
< sizeof(struct dbdma_regs
))
279 dev
->in
.dbdma
= ioremap(r
->start
, rlen
);
281 if (!dev
->intfregs
|| !dev
->out
.dbdma
|| !dev
->in
.dbdma
)
284 if (alloc_dbdma_descriptor_ring(dev
, &dev
->out
.dbdma_ring
,
287 if (alloc_dbdma_descriptor_ring(dev
, &dev
->in
.dbdma_ring
,
291 if (i2sbus_control_add_dev(dev
->control
, dev
)) {
292 printk(KERN_ERR
"i2sbus: control layer didn't like bus\n");
296 if (soundbus_add_one(&dev
->sound
)) {
297 printk(KERN_DEBUG
"i2sbus: device registration error!\n");
301 /* enable this cell */
302 i2sbus_control_cell(dev
->control
, dev
, 1);
303 i2sbus_control_enable(dev
->control
, dev
);
304 i2sbus_control_clock(dev
->control
, dev
, 1);
309 if (dev
->interrupts
[i
] != -1)
310 free_irq(dev
->interrupts
[i
], dev
);
311 free_dbdma_descriptor_ring(dev
, &dev
->out
.dbdma_ring
);
312 free_dbdma_descriptor_ring(dev
, &dev
->in
.dbdma_ring
);
313 if (dev
->intfregs
) iounmap(dev
->intfregs
);
314 if (dev
->out
.dbdma
) iounmap(dev
->out
.dbdma
);
315 if (dev
->in
.dbdma
) iounmap(dev
->in
.dbdma
);
317 if (dev
->allocated_resource
[i
])
318 release_and_free_resource(dev
->allocated_resource
[i
]);
319 mutex_destroy(&dev
->lock
);
324 static int i2sbus_probe(struct macio_dev
* dev
, const struct of_device_id
*match
)
326 struct device_node
*np
= NULL
;
328 struct i2sbus_control
*control
= NULL
;
330 err
= i2sbus_control_init(dev
, &control
);
334 printk(KERN_ERR
"i2sbus_control_init API breakage\n");
338 while ((np
= of_get_next_child(dev
->ofdev
.node
, np
))) {
339 if (of_device_is_compatible(np
, "i2sbus") ||
340 of_device_is_compatible(np
, "i2s-modem")) {
341 got
+= i2sbus_add_dev(dev
, control
, np
);
346 /* found none, clean up */
347 i2sbus_control_destroy(control
);
351 dev
->ofdev
.dev
.driver_data
= control
;
356 static int i2sbus_remove(struct macio_dev
* dev
)
358 struct i2sbus_control
*control
= dev
->ofdev
.dev
.driver_data
;
359 struct i2sbus_dev
*i2sdev
, *tmp
;
361 list_for_each_entry_safe(i2sdev
, tmp
, &control
->list
, item
)
362 soundbus_remove_one(&i2sdev
->sound
);
368 static int i2sbus_suspend(struct macio_dev
* dev
, pm_message_t state
)
370 struct i2sbus_control
*control
= dev
->ofdev
.dev
.driver_data
;
371 struct codec_info_item
*cii
;
372 struct i2sbus_dev
* i2sdev
;
375 list_for_each_entry(i2sdev
, &control
->list
, item
) {
377 if (i2sdev
->sound
.pcm
) {
378 /* Suspend PCM streams */
379 snd_pcm_suspend_all(i2sdev
->sound
.pcm
);
383 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
385 if (cii
->codec
->suspend
)
386 err
= cii
->codec
->suspend(cii
, state
);
391 /* wait until streams are stopped */
392 i2sbus_wait_for_stop_both(i2sdev
);
398 static int i2sbus_resume(struct macio_dev
* dev
)
400 struct i2sbus_control
*control
= dev
->ofdev
.dev
.driver_data
;
401 struct codec_info_item
*cii
;
402 struct i2sbus_dev
* i2sdev
;
405 list_for_each_entry(i2sdev
, &control
->list
, item
) {
406 /* reset i2s bus format etc. */
407 i2sbus_pcm_prepare_both(i2sdev
);
409 /* Notify codecs so they can re-initialize */
410 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
412 if (cii
->codec
->resume
)
413 err
= cii
->codec
->resume(cii
);
421 #endif /* CONFIG_PM */
423 static int i2sbus_shutdown(struct macio_dev
* dev
)
428 static struct macio_driver i2sbus_drv
= {
429 .name
= "soundbus-i2s",
430 .owner
= THIS_MODULE
,
431 .match_table
= i2sbus_match
,
432 .probe
= i2sbus_probe
,
433 .remove
= i2sbus_remove
,
435 .suspend
= i2sbus_suspend
,
436 .resume
= i2sbus_resume
,
438 .shutdown
= i2sbus_shutdown
,
441 static int __init
soundbus_i2sbus_init(void)
443 return macio_register_driver(&i2sbus_drv
);
446 static void __exit
soundbus_i2sbus_exit(void)
448 macio_unregister_driver(&i2sbus_drv
);
451 module_init(soundbus_i2sbus_init
);
452 module_exit(soundbus_i2sbus_exit
);