Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * i2sbus driver |
| 3 | * |
Johannes Berg | 45e513b | 2009-01-15 18:21:48 +0100 | [diff] [blame] | 4 | * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net> |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 5 | * |
| 6 | * GPL v2, can be found in COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 11 | #include <linux/pci.h> |
| 12 | #include <linux/interrupt.h> |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 14 | #include <linux/of_address.h> |
| 15 | #include <linux/of_irq.h> |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 16 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 17 | #include <sound/core.h> |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/macio.h> |
| 20 | #include <asm/dbdma.h> |
| 21 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 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"); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 28 | |
Benjamin Herrenschmidt | f9d08de | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 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 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 34 | static struct of_device_id i2sbus_match[] = { |
| 35 | { .name = "i2s" }, |
| 36 | { } |
| 37 | }; |
| 38 | |
Johannes Berg | e3f9678 | 2007-03-28 13:42:25 +0200 | [diff] [blame] | 39 | MODULE_DEVICE_TABLE(of, i2sbus_match); |
| 40 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 41 | static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev, |
| 42 | struct dbdma_command_mem *r, |
| 43 | int numcmds) |
| 44 | { |
Paul Mackerras | 547ac2a | 2007-02-08 14:25:39 +0100 | [diff] [blame] | 45 | /* one more for rounding, one for branch back, one for stop command */ |
| 46 | r->size = (numcmds + 3) * sizeof(struct dbdma_cmd); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 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 | */ |
Joe Perches | 7f0f204 | 2014-06-15 13:37:55 -0700 | [diff] [blame] | 50 | r->space = dma_zalloc_coherent(&macio_get_pci_dev(i2sdev->macio)->dev, |
| 51 | r->size, &r->bus_addr, GFP_KERNEL); |
| 52 | if (!r->space) |
| 53 | return -ENOMEM; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 54 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 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 | |
| 62 | static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev, |
| 63 | struct dbdma_command_mem *r) |
| 64 | { |
| 65 | if (!r->space) return; |
Johannes Berg | 888dcb7 | 2008-10-23 15:47:56 +0200 | [diff] [blame] | 66 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 67 | dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev, |
| 68 | r->size, r->space, r->bus_addr); |
| 69 | } |
| 70 | |
| 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 | |
| 78 | if (i2sdev->intfregs) iounmap(i2sdev->intfregs); |
| 79 | if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma); |
| 80 | if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma); |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 81 | for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) |
Markus Elfring | 492a7ea0 | 2014-12-02 22:50:24 +0100 | [diff] [blame] | 82 | release_and_free_resource(i2sdev->allocated_resource[i]); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 83 | free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring); |
| 84 | free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring); |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 85 | for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 86 | free_irq(i2sdev->interrupts[i], i2sdev); |
| 87 | i2sbus_control_remove_dev(i2sdev->control, i2sdev); |
| 88 | mutex_destroy(&i2sdev->lock); |
| 89 | kfree(i2sdev); |
| 90 | } |
| 91 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 92 | static irqreturn_t i2sbus_bus_intr(int irq, void *devid) |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 93 | { |
| 94 | struct i2sbus_dev *dev = devid; |
| 95 | u32 intreg; |
| 96 | |
| 97 | spin_lock(&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 | spin_unlock(&dev->low_lock); |
| 104 | |
| 105 | return IRQ_HANDLED; |
| 106 | } |
| 107 | |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * XXX FIXME: We test the layout_id's here to get the proper way of |
| 111 | * mapping in various registers, thanks to bugs in Apple device-trees. |
| 112 | * We could instead key off the machine model and the name of the i2s |
| 113 | * node (i2s-a). This we'll do when we move it all to macio_asic.c |
| 114 | * and have that export items for each sub-node too. |
| 115 | */ |
| 116 | static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index, |
| 117 | int layout, struct resource *res) |
| 118 | { |
| 119 | struct device_node *parent; |
| 120 | int pindex, rc = -ENXIO; |
Stephen Rothwell | a7edd0e | 2007-04-03 10:52:17 +1000 | [diff] [blame] | 121 | const u32 *reg; |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 122 | |
| 123 | /* Machines with layout 76 and 36 (K2 based) have a weird device |
| 124 | * tree what we need to special case. |
| 125 | * Normal machines just fetch the resource from the i2s-X node. |
| 126 | * Darwin further divides normal machines into old and new layouts |
| 127 | * with a subtely different code path but that doesn't seem necessary |
| 128 | * in practice, they just bloated it. In addition, even on our K2 |
| 129 | * case the i2s-modem node, if we ever want to handle it, uses the |
| 130 | * normal layout |
| 131 | */ |
| 132 | if (layout != 76 && layout != 36) |
| 133 | return of_address_to_resource(np, index, res); |
| 134 | |
| 135 | parent = of_get_parent(np); |
| 136 | pindex = (index == aoa_resource_i2smmio) ? 0 : 1; |
| 137 | rc = of_address_to_resource(parent, pindex, res); |
| 138 | if (rc) |
| 139 | goto bail; |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 140 | reg = of_get_property(np, "reg", NULL); |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 141 | if (reg == NULL) { |
| 142 | rc = -ENXIO; |
| 143 | goto bail; |
| 144 | } |
| 145 | res->start += reg[index * 2]; |
| 146 | res->end = res->start + reg[index * 2 + 1] - 1; |
| 147 | bail: |
| 148 | of_node_put(parent); |
| 149 | return rc; |
| 150 | } |
| 151 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 152 | /* FIXME: look at device node refcounting */ |
| 153 | static int i2sbus_add_dev(struct macio_dev *macio, |
| 154 | struct i2sbus_control *control, |
| 155 | struct device_node *np) |
| 156 | { |
| 157 | struct i2sbus_dev *dev; |
| 158 | struct device_node *child = NULL, *sound = NULL; |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 159 | struct resource *r; |
Johannes Berg | 9f50bba | 2008-10-23 13:57:39 +0200 | [diff] [blame] | 160 | int i, layout = 0, rlen, ok = force; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 161 | static const char *rnames[] = { "i2sbus: %s (control)", |
| 162 | "i2sbus: %s (tx)", |
| 163 | "i2sbus: %s (rx)" }; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 164 | static irq_handler_t ints[] = { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 165 | i2sbus_bus_intr, |
| 166 | i2sbus_tx_intr, |
| 167 | i2sbus_rx_intr |
| 168 | }; |
| 169 | |
| 170 | if (strlen(np->name) != 5) |
| 171 | return 0; |
| 172 | if (strncmp(np->name, "i2s-", 4)) |
| 173 | return 0; |
| 174 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 175 | dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL); |
| 176 | if (!dev) |
| 177 | return 0; |
| 178 | |
| 179 | i = 0; |
| 180 | while ((child = of_get_next_child(np, child))) { |
| 181 | if (strcmp(child->name, "sound") == 0) { |
| 182 | i++; |
| 183 | sound = child; |
| 184 | } |
| 185 | } |
| 186 | if (i == 1) { |
Johannes Berg | 45e513b | 2009-01-15 18:21:48 +0100 | [diff] [blame] | 187 | const u32 *id = of_get_property(sound, "layout-id", NULL); |
| 188 | |
| 189 | if (id) { |
| 190 | layout = *id; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 191 | snprintf(dev->sound.modalias, 32, |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 192 | "sound-layout-%d", layout); |
Johannes Berg | 9f50bba | 2008-10-23 13:57:39 +0200 | [diff] [blame] | 193 | ok = 1; |
Johannes Berg | 45e513b | 2009-01-15 18:21:48 +0100 | [diff] [blame] | 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 | */ |
Michael Ellerman | 0885786 | 2013-05-06 11:01:05 +1000 | [diff] [blame] | 200 | if (id && (*id == 22 || *id == 14 || *id == 35 || |
| 201 | *id == 44)) { |
Johannes Berg | 45e513b | 2009-01-15 18:21:48 +0100 | [diff] [blame] | 202 | snprintf(dev->sound.modalias, 32, |
| 203 | "aoa-device-id-%d", *id); |
| 204 | ok = 1; |
| 205 | layout = -1; |
| 206 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | /* for the time being, until we can handle non-layout-id |
| 210 | * things in some fabric, refuse to attach if there is no |
| 211 | * layout-id property or we haven't been forced to attach. |
| 212 | * When there are two i2s busses and only one has a layout-id, |
| 213 | * then this depends on the order, but that isn't important |
| 214 | * either as the second one in that case is just a modem. */ |
Johannes Berg | 9f50bba | 2008-10-23 13:57:39 +0200 | [diff] [blame] | 215 | if (!ok) { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 216 | kfree(dev); |
| 217 | return -ENODEV; |
| 218 | } |
| 219 | |
| 220 | mutex_init(&dev->lock); |
| 221 | spin_lock_init(&dev->low_lock); |
Grant Likely | cb6dc51 | 2010-04-13 16:12:59 -0700 | [diff] [blame] | 222 | dev->sound.ofdev.archdata.dma_mask = macio->ofdev.archdata.dma_mask; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 223 | dev->sound.ofdev.dev.of_node = np; |
Grant Likely | cb6dc51 | 2010-04-13 16:12:59 -0700 | [diff] [blame] | 224 | dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.archdata.dma_mask; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 225 | dev->sound.ofdev.dev.parent = &macio->ofdev.dev; |
| 226 | dev->sound.ofdev.dev.release = i2sbus_release_dev; |
| 227 | dev->sound.attach_codec = i2sbus_attach_codec; |
| 228 | dev->sound.detach_codec = i2sbus_detach_codec; |
| 229 | dev->sound.pcmid = -1; |
| 230 | dev->macio = macio; |
| 231 | dev->control = control; |
| 232 | dev->bus_number = np->name[4] - 'a'; |
| 233 | INIT_LIST_HEAD(&dev->sound.codec_list); |
| 234 | |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 235 | for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 236 | dev->interrupts[i] = -1; |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 237 | snprintf(dev->rnames[i], sizeof(dev->rnames[i]), |
| 238 | rnames[i], np->name); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 239 | } |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 240 | for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) { |
Andreas Schwab | 4a14cf4 | 2006-07-10 04:44:33 -0700 | [diff] [blame] | 241 | int irq = irq_of_parse_and_map(np, i); |
| 242 | if (request_irq(irq, ints[i], 0, dev->rnames[i], dev)) |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 243 | goto err; |
Andreas Schwab | 4a14cf4 | 2006-07-10 04:44:33 -0700 | [diff] [blame] | 244 | dev->interrupts[i] = irq; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 247 | |
| 248 | /* Resource handling is problematic as some device-trees contain |
| 249 | * useless crap (ugh ugh ugh). We work around that here by calling |
| 250 | * specific functions for calculating the appropriate resources. |
| 251 | * |
| 252 | * This will all be moved to macio_asic.c at one point |
| 253 | */ |
| 254 | for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) { |
| 255 | if (i2sbus_get_and_fixup_rsrc(np,i,layout,&dev->resources[i])) |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 256 | goto err; |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 257 | /* If only we could use our resource dev->resources[i]... |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 258 | * but request_resource doesn't know about parents and |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 259 | * contained resources... |
| 260 | */ |
Johannes Berg | 888dcb7 | 2008-10-23 15:47:56 +0200 | [diff] [blame] | 261 | dev->allocated_resource[i] = |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 262 | request_mem_region(dev->resources[i].start, |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 263 | resource_size(&dev->resources[i]), |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 264 | dev->rnames[i]); |
| 265 | if (!dev->allocated_resource[i]) { |
| 266 | printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i); |
| 267 | goto err; |
| 268 | } |
| 269 | } |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 270 | |
| 271 | r = &dev->resources[aoa_resource_i2smmio]; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 272 | rlen = resource_size(r); |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 273 | if (rlen < sizeof(struct i2s_interface_regs)) |
| 274 | goto err; |
| 275 | dev->intfregs = ioremap(r->start, rlen); |
| 276 | |
| 277 | r = &dev->resources[aoa_resource_txdbdma]; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 278 | rlen = resource_size(r); |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 279 | if (rlen < sizeof(struct dbdma_regs)) |
| 280 | goto err; |
| 281 | dev->out.dbdma = ioremap(r->start, rlen); |
| 282 | |
| 283 | r = &dev->resources[aoa_resource_rxdbdma]; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 284 | rlen = resource_size(r); |
Benjamin Herrenschmidt | 389ba79 | 2006-07-10 04:44:34 -0700 | [diff] [blame] | 285 | if (rlen < sizeof(struct dbdma_regs)) |
| 286 | goto err; |
| 287 | dev->in.dbdma = ioremap(r->start, rlen); |
| 288 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 289 | if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma) |
| 290 | goto err; |
| 291 | |
| 292 | if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring, |
| 293 | MAX_DBDMA_COMMANDS)) |
| 294 | goto err; |
| 295 | if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring, |
| 296 | MAX_DBDMA_COMMANDS)) |
| 297 | goto err; |
| 298 | |
| 299 | if (i2sbus_control_add_dev(dev->control, dev)) { |
| 300 | printk(KERN_ERR "i2sbus: control layer didn't like bus\n"); |
| 301 | goto err; |
| 302 | } |
| 303 | |
| 304 | if (soundbus_add_one(&dev->sound)) { |
| 305 | printk(KERN_DEBUG "i2sbus: device registration error!\n"); |
| 306 | goto err; |
| 307 | } |
| 308 | |
| 309 | /* enable this cell */ |
| 310 | i2sbus_control_cell(dev->control, dev, 1); |
| 311 | i2sbus_control_enable(dev->control, dev); |
| 312 | i2sbus_control_clock(dev->control, dev, 1); |
| 313 | |
| 314 | return 1; |
| 315 | err: |
| 316 | for (i=0;i<3;i++) |
| 317 | if (dev->interrupts[i] != -1) |
| 318 | free_irq(dev->interrupts[i], dev); |
| 319 | free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring); |
| 320 | free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring); |
| 321 | if (dev->intfregs) iounmap(dev->intfregs); |
| 322 | if (dev->out.dbdma) iounmap(dev->out.dbdma); |
| 323 | if (dev->in.dbdma) iounmap(dev->in.dbdma); |
| 324 | for (i=0;i<3;i++) |
Markus Elfring | 492a7ea0 | 2014-12-02 22:50:24 +0100 | [diff] [blame] | 325 | release_and_free_resource(dev->allocated_resource[i]); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 326 | mutex_destroy(&dev->lock); |
| 327 | kfree(dev); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match) |
| 332 | { |
| 333 | struct device_node *np = NULL; |
| 334 | int got = 0, err; |
| 335 | struct i2sbus_control *control = NULL; |
| 336 | |
| 337 | err = i2sbus_control_init(dev, &control); |
| 338 | if (err) |
| 339 | return err; |
| 340 | if (!control) { |
| 341 | printk(KERN_ERR "i2sbus_control_init API breakage\n"); |
| 342 | return -ENODEV; |
| 343 | } |
| 344 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 345 | while ((np = of_get_next_child(dev->ofdev.dev.of_node, np))) { |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 346 | if (of_device_is_compatible(np, "i2sbus") || |
| 347 | of_device_is_compatible(np, "i2s-modem")) { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 348 | got += i2sbus_add_dev(dev, control, np); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (!got) { |
| 353 | /* found none, clean up */ |
| 354 | i2sbus_control_destroy(control); |
| 355 | return -ENODEV; |
| 356 | } |
| 357 | |
Greg Kroah-Hartman | ae31c1f | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 358 | dev_set_drvdata(&dev->ofdev.dev, control); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int i2sbus_remove(struct macio_dev* dev) |
| 364 | { |
Greg Kroah-Hartman | ae31c1f | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 365 | struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 366 | struct i2sbus_dev *i2sdev, *tmp; |
| 367 | |
| 368 | list_for_each_entry_safe(i2sdev, tmp, &control->list, item) |
| 369 | soundbus_remove_one(&i2sdev->sound); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | #ifdef CONFIG_PM |
| 375 | static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state) |
| 376 | { |
Greg Kroah-Hartman | ae31c1f | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 377 | struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 378 | struct codec_info_item *cii; |
| 379 | struct i2sbus_dev* i2sdev; |
| 380 | int err, ret = 0; |
| 381 | |
| 382 | list_for_each_entry(i2sdev, &control->list, item) { |
| 383 | /* Notify Alsa */ |
Markus Elfring | aa57b93 | 2015-01-03 20:43:01 +0100 | [diff] [blame^] | 384 | /* Suspend PCM streams */ |
| 385 | snd_pcm_suspend_all(i2sdev->sound.pcm); |
Paul Mackerras | 547ac2a | 2007-02-08 14:25:39 +0100 | [diff] [blame] | 386 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 387 | /* Notify codecs */ |
| 388 | list_for_each_entry(cii, &i2sdev->sound.codec_list, list) { |
| 389 | err = 0; |
| 390 | if (cii->codec->suspend) |
| 391 | err = cii->codec->suspend(cii, state); |
| 392 | if (err) |
| 393 | ret = err; |
| 394 | } |
Paul Mackerras | 547ac2a | 2007-02-08 14:25:39 +0100 | [diff] [blame] | 395 | |
| 396 | /* wait until streams are stopped */ |
| 397 | i2sbus_wait_for_stop_both(i2sdev); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 398 | } |
Paul Mackerras | 547ac2a | 2007-02-08 14:25:39 +0100 | [diff] [blame] | 399 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | static int i2sbus_resume(struct macio_dev* dev) |
| 404 | { |
Greg Kroah-Hartman | ae31c1f | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 405 | struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 406 | struct codec_info_item *cii; |
| 407 | struct i2sbus_dev* i2sdev; |
| 408 | int err, ret = 0; |
| 409 | |
| 410 | list_for_each_entry(i2sdev, &control->list, item) { |
Paul Mackerras | 547ac2a | 2007-02-08 14:25:39 +0100 | [diff] [blame] | 411 | /* reset i2s bus format etc. */ |
| 412 | i2sbus_pcm_prepare_both(i2sdev); |
| 413 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 414 | /* Notify codecs so they can re-initialize */ |
| 415 | list_for_each_entry(cii, &i2sdev->sound.codec_list, list) { |
| 416 | err = 0; |
| 417 | if (cii->codec->resume) |
| 418 | err = cii->codec->resume(cii); |
| 419 | if (err) |
| 420 | ret = err; |
| 421 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | #endif /* CONFIG_PM */ |
| 427 | |
| 428 | static int i2sbus_shutdown(struct macio_dev* dev) |
| 429 | { |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static struct macio_driver i2sbus_drv = { |
Benjamin Herrenschmidt | c2cdf6a | 2010-06-02 17:09:18 +1000 | [diff] [blame] | 434 | .driver = { |
| 435 | .name = "soundbus-i2s", |
| 436 | .owner = THIS_MODULE, |
| 437 | .of_match_table = i2sbus_match, |
| 438 | }, |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 439 | .probe = i2sbus_probe, |
| 440 | .remove = i2sbus_remove, |
| 441 | #ifdef CONFIG_PM |
| 442 | .suspend = i2sbus_suspend, |
| 443 | .resume = i2sbus_resume, |
| 444 | #endif |
| 445 | .shutdown = i2sbus_shutdown, |
| 446 | }; |
| 447 | |
| 448 | static int __init soundbus_i2sbus_init(void) |
| 449 | { |
| 450 | return macio_register_driver(&i2sbus_drv); |
| 451 | } |
| 452 | |
| 453 | static void __exit soundbus_i2sbus_exit(void) |
| 454 | { |
| 455 | macio_unregister_driver(&i2sbus_drv); |
| 456 | } |
| 457 | |
| 458 | module_init(soundbus_i2sbus_init); |
| 459 | module_exit(soundbus_i2sbus_exit); |