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