Path: blob/master/sound/aoa/soundbus/i2sbus/core.c
15125 views
/*1* i2sbus driver2*3* Copyright 2006-2008 Johannes Berg <[email protected]>4*5* GPL v2, can be found in COPYING.6*/78#include <linux/module.h>9#include <linux/slab.h>10#include <linux/pci.h>11#include <linux/interrupt.h>12#include <linux/dma-mapping.h>1314#include <sound/core.h>1516#include <asm/macio.h>17#include <asm/dbdma.h>1819#include "../soundbus.h"20#include "i2sbus.h"2122MODULE_LICENSE("GPL");23MODULE_AUTHOR("Johannes Berg <[email protected]>");24MODULE_DESCRIPTION("Apple Soundbus: I2S support");2526static int force;27module_param(force, int, 0444);28MODULE_PARM_DESC(force, "Force loading i2sbus even when"29" no layout-id property is present");3031static struct of_device_id i2sbus_match[] = {32{ .name = "i2s" },33{ }34};3536MODULE_DEVICE_TABLE(of, i2sbus_match);3738static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,39struct dbdma_command_mem *r,40int numcmds)41{42/* one more for rounding, one for branch back, one for stop command */43r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);44/* We use the PCI APIs for now until the generic one gets fixed45* enough or until we get some macio-specific versions46*/47r->space = dma_alloc_coherent(48&macio_get_pci_dev(i2sdev->macio)->dev,49r->size,50&r->bus_addr,51GFP_KERNEL);5253if (!r->space) return -ENOMEM;5455memset(r->space, 0, r->size);56r->cmds = (void*)DBDMA_ALIGN(r->space);57r->bus_cmd_start = r->bus_addr +58(dma_addr_t)((char*)r->cmds - (char*)r->space);5960return 0;61}6263static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,64struct dbdma_command_mem *r)65{66if (!r->space) return;6768dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,69r->size, r->space, r->bus_addr);70}7172static void i2sbus_release_dev(struct device *dev)73{74struct i2sbus_dev *i2sdev;75int i;7677i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);7879if (i2sdev->intfregs) iounmap(i2sdev->intfregs);80if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma);81if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma);82for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)83if (i2sdev->allocated_resource[i])84release_and_free_resource(i2sdev->allocated_resource[i]);85free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);86free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);87for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)88free_irq(i2sdev->interrupts[i], i2sdev);89i2sbus_control_remove_dev(i2sdev->control, i2sdev);90mutex_destroy(&i2sdev->lock);91kfree(i2sdev);92}9394static irqreturn_t i2sbus_bus_intr(int irq, void *devid)95{96struct i2sbus_dev *dev = devid;97u32 intreg;9899spin_lock(&dev->low_lock);100intreg = in_le32(&dev->intfregs->intr_ctl);101102/* acknowledge interrupt reasons */103out_le32(&dev->intfregs->intr_ctl, intreg);104105spin_unlock(&dev->low_lock);106107return IRQ_HANDLED;108}109110111/*112* XXX FIXME: We test the layout_id's here to get the proper way of113* 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 i2s115* node (i2s-a). This we'll do when we move it all to macio_asic.c116* and have that export items for each sub-node too.117*/118static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index,119int layout, struct resource *res)120{121struct device_node *parent;122int pindex, rc = -ENXIO;123const u32 *reg;124125/* Machines with layout 76 and 36 (K2 based) have a weird device126* 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 layouts129* with a subtely different code path but that doesn't seem necessary130* in practice, they just bloated it. In addition, even on our K2131* case the i2s-modem node, if we ever want to handle it, uses the132* normal layout133*/134if (layout != 76 && layout != 36)135return of_address_to_resource(np, index, res);136137parent = of_get_parent(np);138pindex = (index == aoa_resource_i2smmio) ? 0 : 1;139rc = of_address_to_resource(parent, pindex, res);140if (rc)141goto bail;142reg = of_get_property(np, "reg", NULL);143if (reg == NULL) {144rc = -ENXIO;145goto bail;146}147res->start += reg[index * 2];148res->end = res->start + reg[index * 2 + 1] - 1;149bail:150of_node_put(parent);151return rc;152}153154/* FIXME: look at device node refcounting */155static int i2sbus_add_dev(struct macio_dev *macio,156struct i2sbus_control *control,157struct device_node *np)158{159struct i2sbus_dev *dev;160struct device_node *child = NULL, *sound = NULL;161struct resource *r;162int i, layout = 0, rlen, ok = force;163static const char *rnames[] = { "i2sbus: %s (control)",164"i2sbus: %s (tx)",165"i2sbus: %s (rx)" };166static irq_handler_t ints[] = {167i2sbus_bus_intr,168i2sbus_tx_intr,169i2sbus_rx_intr170};171172if (strlen(np->name) != 5)173return 0;174if (strncmp(np->name, "i2s-", 4))175return 0;176177dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);178if (!dev)179return 0;180181i = 0;182while ((child = of_get_next_child(np, child))) {183if (strcmp(child->name, "sound") == 0) {184i++;185sound = child;186}187}188if (i == 1) {189const u32 *id = of_get_property(sound, "layout-id", NULL);190191if (id) {192layout = *id;193snprintf(dev->sound.modalias, 32,194"sound-layout-%d", layout);195ok = 1;196} else {197id = 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*/202if (id && (*id == 22 || *id == 14 || *id == 35)) {203snprintf(dev->sound.modalias, 32,204"aoa-device-id-%d", *id);205ok = 1;206layout = -1;207}208}209}210/* for the time being, until we can handle non-layout-id211* things in some fabric, refuse to attach if there is no212* 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 important215* either as the second one in that case is just a modem. */216if (!ok) {217kfree(dev);218return -ENODEV;219}220221mutex_init(&dev->lock);222spin_lock_init(&dev->low_lock);223dev->sound.ofdev.archdata.dma_mask = macio->ofdev.archdata.dma_mask;224dev->sound.ofdev.dev.of_node = np;225dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.archdata.dma_mask;226dev->sound.ofdev.dev.parent = &macio->ofdev.dev;227dev->sound.ofdev.dev.release = i2sbus_release_dev;228dev->sound.attach_codec = i2sbus_attach_codec;229dev->sound.detach_codec = i2sbus_detach_codec;230dev->sound.pcmid = -1;231dev->macio = macio;232dev->control = control;233dev->bus_number = np->name[4] - 'a';234INIT_LIST_HEAD(&dev->sound.codec_list);235236for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {237dev->interrupts[i] = -1;238snprintf(dev->rnames[i], sizeof(dev->rnames[i]),239rnames[i], np->name);240}241for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {242int irq = irq_of_parse_and_map(np, i);243if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))244goto err;245dev->interrupts[i] = irq;246}247248249/* Resource handling is problematic as some device-trees contain250* useless crap (ugh ugh ugh). We work around that here by calling251* specific functions for calculating the appropriate resources.252*253* This will all be moved to macio_asic.c at one point254*/255for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {256if (i2sbus_get_and_fixup_rsrc(np,i,layout,&dev->resources[i]))257goto err;258/* If only we could use our resource dev->resources[i]...259* but request_resource doesn't know about parents and260* contained resources...261*/262dev->allocated_resource[i] =263request_mem_region(dev->resources[i].start,264dev->resources[i].end -265dev->resources[i].start + 1,266dev->rnames[i]);267if (!dev->allocated_resource[i]) {268printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);269goto err;270}271}272273r = &dev->resources[aoa_resource_i2smmio];274rlen = r->end - r->start + 1;275if (rlen < sizeof(struct i2s_interface_regs))276goto err;277dev->intfregs = ioremap(r->start, rlen);278279r = &dev->resources[aoa_resource_txdbdma];280rlen = r->end - r->start + 1;281if (rlen < sizeof(struct dbdma_regs))282goto err;283dev->out.dbdma = ioremap(r->start, rlen);284285r = &dev->resources[aoa_resource_rxdbdma];286rlen = r->end - r->start + 1;287if (rlen < sizeof(struct dbdma_regs))288goto err;289dev->in.dbdma = ioremap(r->start, rlen);290291if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)292goto err;293294if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,295MAX_DBDMA_COMMANDS))296goto err;297if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,298MAX_DBDMA_COMMANDS))299goto err;300301if (i2sbus_control_add_dev(dev->control, dev)) {302printk(KERN_ERR "i2sbus: control layer didn't like bus\n");303goto err;304}305306if (soundbus_add_one(&dev->sound)) {307printk(KERN_DEBUG "i2sbus: device registration error!\n");308goto err;309}310311/* enable this cell */312i2sbus_control_cell(dev->control, dev, 1);313i2sbus_control_enable(dev->control, dev);314i2sbus_control_clock(dev->control, dev, 1);315316return 1;317err:318for (i=0;i<3;i++)319if (dev->interrupts[i] != -1)320free_irq(dev->interrupts[i], dev);321free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);322free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);323if (dev->intfregs) iounmap(dev->intfregs);324if (dev->out.dbdma) iounmap(dev->out.dbdma);325if (dev->in.dbdma) iounmap(dev->in.dbdma);326for (i=0;i<3;i++)327if (dev->allocated_resource[i])328release_and_free_resource(dev->allocated_resource[i]);329mutex_destroy(&dev->lock);330kfree(dev);331return 0;332}333334static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)335{336struct device_node *np = NULL;337int got = 0, err;338struct i2sbus_control *control = NULL;339340err = i2sbus_control_init(dev, &control);341if (err)342return err;343if (!control) {344printk(KERN_ERR "i2sbus_control_init API breakage\n");345return -ENODEV;346}347348while ((np = of_get_next_child(dev->ofdev.dev.of_node, np))) {349if (of_device_is_compatible(np, "i2sbus") ||350of_device_is_compatible(np, "i2s-modem")) {351got += i2sbus_add_dev(dev, control, np);352}353}354355if (!got) {356/* found none, clean up */357i2sbus_control_destroy(control);358return -ENODEV;359}360361dev_set_drvdata(&dev->ofdev.dev, control);362363return 0;364}365366static int i2sbus_remove(struct macio_dev* dev)367{368struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);369struct i2sbus_dev *i2sdev, *tmp;370371list_for_each_entry_safe(i2sdev, tmp, &control->list, item)372soundbus_remove_one(&i2sdev->sound);373374return 0;375}376377#ifdef CONFIG_PM378static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)379{380struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);381struct codec_info_item *cii;382struct i2sbus_dev* i2sdev;383int err, ret = 0;384385list_for_each_entry(i2sdev, &control->list, item) {386/* Notify Alsa */387if (i2sdev->sound.pcm) {388/* Suspend PCM streams */389snd_pcm_suspend_all(i2sdev->sound.pcm);390}391392/* Notify codecs */393list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {394err = 0;395if (cii->codec->suspend)396err = cii->codec->suspend(cii, state);397if (err)398ret = err;399}400401/* wait until streams are stopped */402i2sbus_wait_for_stop_both(i2sdev);403}404405return ret;406}407408static int i2sbus_resume(struct macio_dev* dev)409{410struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);411struct codec_info_item *cii;412struct i2sbus_dev* i2sdev;413int err, ret = 0;414415list_for_each_entry(i2sdev, &control->list, item) {416/* reset i2s bus format etc. */417i2sbus_pcm_prepare_both(i2sdev);418419/* Notify codecs so they can re-initialize */420list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {421err = 0;422if (cii->codec->resume)423err = cii->codec->resume(cii);424if (err)425ret = err;426}427}428429return ret;430}431#endif /* CONFIG_PM */432433static int i2sbus_shutdown(struct macio_dev* dev)434{435return 0;436}437438static struct macio_driver i2sbus_drv = {439.driver = {440.name = "soundbus-i2s",441.owner = THIS_MODULE,442.of_match_table = i2sbus_match,443},444.probe = i2sbus_probe,445.remove = i2sbus_remove,446#ifdef CONFIG_PM447.suspend = i2sbus_suspend,448.resume = i2sbus_resume,449#endif450.shutdown = i2sbus_shutdown,451};452453static int __init soundbus_i2sbus_init(void)454{455return macio_register_driver(&i2sbus_drv);456}457458static void __exit soundbus_i2sbus_exit(void)459{460macio_unregister_driver(&i2sbus_drv);461}462463module_init(soundbus_i2sbus_init);464module_exit(soundbus_i2sbus_exit);465466467