linux/arch/arm/common/locomo.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * linux/arch/arm/common/locomo.c
   4 *
   5 * Sharp LoCoMo support
   6 *
   7 * This file contains all generic LoCoMo support.
   8 *
   9 * All initialization functions provided here are intended to be called
  10 * from machine specific code with proper arguments when required.
  11 *
  12 * Based on sa1111.c
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/init.h>
  17#include <linux/kernel.h>
  18#include <linux/delay.h>
  19#include <linux/errno.h>
  20#include <linux/ioport.h>
  21#include <linux/platform_device.h>
  22#include <linux/slab.h>
  23#include <linux/spinlock.h>
  24#include <linux/io.h>
  25
  26#include <mach/hardware.h>
  27#include <asm/irq.h>
  28#include <asm/mach/irq.h>
  29
  30#include <asm/hardware/locomo.h>
  31
  32/* LoCoMo Interrupts */
  33#define IRQ_LOCOMO_KEY          (0)
  34#define IRQ_LOCOMO_GPIO         (1)
  35#define IRQ_LOCOMO_LT           (2)
  36#define IRQ_LOCOMO_SPI          (3)
  37
  38/* M62332 output channel selection */
  39#define M62332_EVR_CH   1       /* M62332 volume channel number  */
  40                                /*   0 : CH.1 , 1 : CH. 2        */
  41/* DAC send data */
  42#define M62332_SLAVE_ADDR       0x4e    /* Slave address  */
  43#define M62332_W_BIT            0x00    /* W bit (0 only) */
  44#define M62332_SUB_ADDR         0x00    /* Sub address    */
  45#define M62332_A_BIT            0x00    /* A bit (0 only) */
  46
  47/* DAC setup and hold times (expressed in us) */
  48#define DAC_BUS_FREE_TIME       5       /*   4.7 us */
  49#define DAC_START_SETUP_TIME    5       /*   4.7 us */
  50#define DAC_STOP_SETUP_TIME     4       /*   4.0 us */
  51#define DAC_START_HOLD_TIME     5       /*   4.7 us */
  52#define DAC_SCL_LOW_HOLD_TIME   5       /*   4.7 us */
  53#define DAC_SCL_HIGH_HOLD_TIME  4       /*   4.0 us */
  54#define DAC_DATA_SETUP_TIME     1       /*   250 ns */
  55#define DAC_DATA_HOLD_TIME      1       /*   300 ns */
  56#define DAC_LOW_SETUP_TIME      1       /*   300 ns */
  57#define DAC_HIGH_SETUP_TIME     1       /*  1000 ns */
  58
  59/* the following is the overall data for the locomo chip */
  60struct locomo {
  61        struct device *dev;
  62        unsigned long phys;
  63        unsigned int irq;
  64        int irq_base;
  65        spinlock_t lock;
  66        void __iomem *base;
  67#ifdef CONFIG_PM
  68        void *saved_state;
  69#endif
  70};
  71
  72struct locomo_dev_info {
  73        unsigned long   offset;
  74        unsigned long   length;
  75        unsigned int    devid;
  76        unsigned int    irq[1];
  77        const char *    name;
  78};
  79
  80/* All the locomo devices.  If offset is non-zero, the mapbase for the
  81 * locomo_dev will be set to the chip base plus offset.  If offset is
  82 * zero, then the mapbase for the locomo_dev will be set to zero.  An
  83 * offset of zero means the device only uses GPIOs or other helper
  84 * functions inside this file */
  85static struct locomo_dev_info locomo_devices[] = {
  86        {
  87                .devid          = LOCOMO_DEVID_KEYBOARD,
  88                .irq            = { IRQ_LOCOMO_KEY },
  89                .name           = "locomo-keyboard",
  90                .offset         = LOCOMO_KEYBOARD,
  91                .length         = 16,
  92        },
  93        {
  94                .devid          = LOCOMO_DEVID_FRONTLIGHT,
  95                .irq            = {},
  96                .name           = "locomo-frontlight",
  97                .offset         = LOCOMO_FRONTLIGHT,
  98                .length         = 8,
  99
 100        },
 101        {
 102                .devid          = LOCOMO_DEVID_BACKLIGHT,
 103                .irq            = {},
 104                .name           = "locomo-backlight",
 105                .offset         = LOCOMO_BACKLIGHT,
 106                .length         = 8,
 107        },
 108        {
 109                .devid          = LOCOMO_DEVID_AUDIO,
 110                .irq            = {},
 111                .name           = "locomo-audio",
 112                .offset         = LOCOMO_AUDIO,
 113                .length         = 4,
 114        },
 115        {
 116                .devid          = LOCOMO_DEVID_LED,
 117                .irq            = {},
 118                .name           = "locomo-led",
 119                .offset         = LOCOMO_LED,
 120                .length         = 8,
 121        },
 122        {
 123                .devid          = LOCOMO_DEVID_UART,
 124                .irq            = {},
 125                .name           = "locomo-uart",
 126                .offset         = 0,
 127                .length         = 0,
 128        },
 129        {
 130                .devid          = LOCOMO_DEVID_SPI,
 131                .irq            = {},
 132                .name           = "locomo-spi",
 133                .offset         = LOCOMO_SPI,
 134                .length         = 0x30,
 135        },
 136};
 137
 138static void locomo_handler(struct irq_desc *desc)
 139{
 140        struct locomo *lchip = irq_desc_get_handler_data(desc);
 141        int req, i;
 142
 143        /* Acknowledge the parent IRQ */
 144        desc->irq_data.chip->irq_ack(&desc->irq_data);
 145
 146        /* check why this interrupt was generated */
 147        req = locomo_readl(lchip->base + LOCOMO_ICR) & 0x0f00;
 148
 149        if (req) {
 150                unsigned int irq;
 151
 152                /* generate the next interrupt(s) */
 153                irq = lchip->irq_base;
 154                for (i = 0; i <= 3; i++, irq++) {
 155                        if (req & (0x0100 << i)) {
 156                                generic_handle_irq(irq);
 157                        }
 158
 159                }
 160        }
 161}
 162
 163static void locomo_ack_irq(struct irq_data *d)
 164{
 165}
 166
 167static void locomo_mask_irq(struct irq_data *d)
 168{
 169        struct locomo *lchip = irq_data_get_irq_chip_data(d);
 170        unsigned int r;
 171        r = locomo_readl(lchip->base + LOCOMO_ICR);
 172        r &= ~(0x0010 << (d->irq - lchip->irq_base));
 173        locomo_writel(r, lchip->base + LOCOMO_ICR);
 174}
 175
 176static void locomo_unmask_irq(struct irq_data *d)
 177{
 178        struct locomo *lchip = irq_data_get_irq_chip_data(d);
 179        unsigned int r;
 180        r = locomo_readl(lchip->base + LOCOMO_ICR);
 181        r |= (0x0010 << (d->irq - lchip->irq_base));
 182        locomo_writel(r, lchip->base + LOCOMO_ICR);
 183}
 184
 185static struct irq_chip locomo_chip = {
 186        .name           = "LOCOMO",
 187        .irq_ack        = locomo_ack_irq,
 188        .irq_mask       = locomo_mask_irq,
 189        .irq_unmask     = locomo_unmask_irq,
 190};
 191
 192static void locomo_setup_irq(struct locomo *lchip)
 193{
 194        int irq = lchip->irq_base;
 195
 196        /*
 197         * Install handler for IRQ_LOCOMO_HW.
 198         */
 199        irq_set_irq_type(lchip->irq, IRQ_TYPE_EDGE_FALLING);
 200        irq_set_chained_handler_and_data(lchip->irq, locomo_handler, lchip);
 201
 202        /* Install handlers for IRQ_LOCOMO_* */
 203        for ( ; irq <= lchip->irq_base + 3; irq++) {
 204                irq_set_chip_and_handler(irq, &locomo_chip, handle_level_irq);
 205                irq_set_chip_data(irq, lchip);
 206                irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
 207        }
 208}
 209
 210
 211static void locomo_dev_release(struct device *_dev)
 212{
 213        struct locomo_dev *dev = LOCOMO_DEV(_dev);
 214
 215        kfree(dev);
 216}
 217
 218static int
 219locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
 220{
 221        struct locomo_dev *dev;
 222        int ret;
 223
 224        dev = kzalloc(sizeof(struct locomo_dev), GFP_KERNEL);
 225        if (!dev) {
 226                ret = -ENOMEM;
 227                goto out;
 228        }
 229
 230        /*
 231         * If the parent device has a DMA mask associated with it,
 232         * propagate it down to the children.
 233         */
 234        if (lchip->dev->dma_mask) {
 235                dev->dma_mask = *lchip->dev->dma_mask;
 236                dev->dev.dma_mask = &dev->dma_mask;
 237        }
 238
 239        dev_set_name(&dev->dev, "%s", info->name);
 240        dev->devid       = info->devid;
 241        dev->dev.parent  = lchip->dev;
 242        dev->dev.bus     = &locomo_bus_type;
 243        dev->dev.release = locomo_dev_release;
 244        dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
 245
 246        if (info->offset)
 247                dev->mapbase = lchip->base + info->offset;
 248        else
 249                dev->mapbase = 0;
 250        dev->length = info->length;
 251
 252        dev->irq[0] = (lchip->irq_base == NO_IRQ) ?
 253                        NO_IRQ : lchip->irq_base + info->irq[0];
 254
 255        ret = device_register(&dev->dev);
 256        if (ret) {
 257 out:
 258                kfree(dev);
 259        }
 260        return ret;
 261}
 262
 263#ifdef CONFIG_PM
 264
 265struct locomo_save_data {
 266        u16     LCM_GPO;
 267        u16     LCM_SPICT;
 268        u16     LCM_GPE;
 269        u16     LCM_ASD;
 270        u16     LCM_SPIMD;
 271};
 272
 273static int locomo_suspend(struct platform_device *dev, pm_message_t state)
 274{
 275        struct locomo *lchip = platform_get_drvdata(dev);
 276        struct locomo_save_data *save;
 277        unsigned long flags;
 278
 279        save = kmalloc(sizeof(struct locomo_save_data), GFP_KERNEL);
 280        if (!save)
 281                return -ENOMEM;
 282
 283        lchip->saved_state = save;
 284
 285        spin_lock_irqsave(&lchip->lock, flags);
 286
 287        save->LCM_GPO     = locomo_readl(lchip->base + LOCOMO_GPO);     /* GPIO */
 288        locomo_writel(0x00, lchip->base + LOCOMO_GPO);
 289        save->LCM_SPICT   = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPICT);      /* SPI */
 290        locomo_writel(0x40, lchip->base + LOCOMO_SPI + LOCOMO_SPICT);
 291        save->LCM_GPE     = locomo_readl(lchip->base + LOCOMO_GPE);     /* GPIO */
 292        locomo_writel(0x00, lchip->base + LOCOMO_GPE);
 293        save->LCM_ASD     = locomo_readl(lchip->base + LOCOMO_ASD);     /* ADSTART */
 294        locomo_writel(0x00, lchip->base + LOCOMO_ASD);
 295        save->LCM_SPIMD   = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);      /* SPI */
 296        locomo_writel(0x3C14, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
 297
 298        locomo_writel(0x00, lchip->base + LOCOMO_PAIF);
 299        locomo_writel(0x00, lchip->base + LOCOMO_DAC);
 300        locomo_writel(0x00, lchip->base + LOCOMO_BACKLIGHT + LOCOMO_TC);
 301
 302        if ((locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT0) & 0x88) && (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT1) & 0x88))
 303                locomo_writel(0x00, lchip->base + LOCOMO_C32K);         /* CLK32 off */
 304        else
 305                /* 18MHz already enabled, so no wait */
 306                locomo_writel(0xc1, lchip->base + LOCOMO_C32K);         /* CLK32 on */
 307
 308        locomo_writel(0x00, lchip->base + LOCOMO_TADC);         /* 18MHz clock off*/
 309        locomo_writel(0x00, lchip->base + LOCOMO_AUDIO + LOCOMO_ACC);                   /* 22MHz/24MHz clock off */
 310        locomo_writel(0x00, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);                      /* FL */
 311
 312        spin_unlock_irqrestore(&lchip->lock, flags);
 313
 314        return 0;
 315}
 316
 317static int locomo_resume(struct platform_device *dev)
 318{
 319        struct locomo *lchip = platform_get_drvdata(dev);
 320        struct locomo_save_data *save;
 321        unsigned long r;
 322        unsigned long flags;
 323
 324        save = lchip->saved_state;
 325        if (!save)
 326                return 0;
 327
 328        spin_lock_irqsave(&lchip->lock, flags);
 329
 330        locomo_writel(save->LCM_GPO, lchip->base + LOCOMO_GPO);
 331        locomo_writel(save->LCM_SPICT, lchip->base + LOCOMO_SPI + LOCOMO_SPICT);
 332        locomo_writel(save->LCM_GPE, lchip->base + LOCOMO_GPE);
 333        locomo_writel(save->LCM_ASD, lchip->base + LOCOMO_ASD);
 334        locomo_writel(save->LCM_SPIMD, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
 335
 336        locomo_writel(0x00, lchip->base + LOCOMO_C32K);
 337        locomo_writel(0x90, lchip->base + LOCOMO_TADC);
 338
 339        locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KSC);
 340        r = locomo_readl(lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
 341        r &= 0xFEFF;
 342        locomo_writel(r, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
 343        locomo_writel(0x1, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KCMD);
 344
 345        spin_unlock_irqrestore(&lchip->lock, flags);
 346
 347        lchip->saved_state = NULL;
 348        kfree(save);
 349
 350        return 0;
 351}
 352#endif
 353
 354
 355/**
 356 *      locomo_probe - probe for a single LoCoMo chip.
 357 *      @phys_addr: physical address of device.
 358 *
 359 *      Probe for a LoCoMo chip.  This must be called
 360 *      before any other locomo-specific code.
 361 *
 362 *      Returns:
 363 *      %-ENODEV        device not found.
 364 *      %-EBUSY         physical address already marked in-use.
 365 *      %0              successful.
 366 */
 367static int
 368__locomo_probe(struct device *me, struct resource *mem, int irq)
 369{
 370        struct locomo_platform_data *pdata = me->platform_data;
 371        struct locomo *lchip;
 372        unsigned long r;
 373        int i, ret = -ENODEV;
 374
 375        lchip = kzalloc(sizeof(struct locomo), GFP_KERNEL);
 376        if (!lchip)
 377                return -ENOMEM;
 378
 379        spin_lock_init(&lchip->lock);
 380
 381        lchip->dev = me;
 382        dev_set_drvdata(lchip->dev, lchip);
 383
 384        lchip->phys = mem->start;
 385        lchip->irq = irq;
 386        lchip->irq_base = (pdata) ? pdata->irq_base : NO_IRQ;
 387
 388        /*
 389         * Map the whole region.  This also maps the
 390         * registers for our children.
 391         */
 392        lchip->base = ioremap(mem->start, PAGE_SIZE);
 393        if (!lchip->base) {
 394                ret = -ENOMEM;
 395                goto out;
 396        }
 397
 398        /* locomo initialize */
 399        locomo_writel(0, lchip->base + LOCOMO_ICR);
 400        /* KEYBOARD */
 401        locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
 402
 403        /* GPIO */
 404        locomo_writel(0, lchip->base + LOCOMO_GPO);
 405        locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
 406                        , lchip->base + LOCOMO_GPE);
 407        locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
 408                        , lchip->base + LOCOMO_GPD);
 409        locomo_writel(0, lchip->base + LOCOMO_GIE);
 410
 411        /* Frontlight */
 412        locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
 413        locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
 414
 415        /* Longtime timer */
 416        locomo_writel(0, lchip->base + LOCOMO_LTINT);
 417        /* SPI */
 418        locomo_writel(0, lchip->base + LOCOMO_SPI + LOCOMO_SPIIE);
 419
 420        locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
 421        r = locomo_readl(lchip->base + LOCOMO_ASD);
 422        r |= 0x8000;
 423        locomo_writel(r, lchip->base + LOCOMO_ASD);
 424
 425        locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
 426        r = locomo_readl(lchip->base + LOCOMO_HSD);
 427        r |= 0x8000;
 428        locomo_writel(r, lchip->base + LOCOMO_HSD);
 429
 430        locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
 431
 432        /* XON */
 433        locomo_writel(0x80, lchip->base + LOCOMO_TADC);
 434        udelay(1000);
 435        /* CLK9MEN */
 436        r = locomo_readl(lchip->base + LOCOMO_TADC);
 437        r |= 0x10;
 438        locomo_writel(r, lchip->base + LOCOMO_TADC);
 439        udelay(100);
 440
 441        /* init DAC */
 442        r = locomo_readl(lchip->base + LOCOMO_DAC);
 443        r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
 444        locomo_writel(r, lchip->base + LOCOMO_DAC);
 445
 446        r = locomo_readl(lchip->base + LOCOMO_VER);
 447        printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
 448
 449        /*
 450         * The interrupt controller must be initialised before any
 451         * other device to ensure that the interrupts are available.
 452         */
 453        if (lchip->irq != NO_IRQ && lchip->irq_base != NO_IRQ)
 454                locomo_setup_irq(lchip);
 455
 456        for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
 457                locomo_init_one_child(lchip, &locomo_devices[i]);
 458        return 0;
 459
 460 out:
 461        kfree(lchip);
 462        return ret;
 463}
 464
 465static int locomo_remove_child(struct device *dev, void *data)
 466{
 467        device_unregister(dev);
 468        return 0;
 469} 
 470
 471static void __locomo_remove(struct locomo *lchip)
 472{
 473        device_for_each_child(lchip->dev, NULL, locomo_remove_child);
 474
 475        if (lchip->irq != NO_IRQ) {
 476                irq_set_chained_handler_and_data(lchip->irq, NULL, NULL);
 477        }
 478
 479        iounmap(lchip->base);
 480        kfree(lchip);
 481}
 482
 483static int locomo_probe(struct platform_device *dev)
 484{
 485        struct resource *mem;
 486        int irq;
 487
 488        mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
 489        if (!mem)
 490                return -EINVAL;
 491        irq = platform_get_irq(dev, 0);
 492        if (irq < 0)
 493                return -ENXIO;
 494
 495        return __locomo_probe(&dev->dev, mem, irq);
 496}
 497
 498static int locomo_remove(struct platform_device *dev)
 499{
 500        struct locomo *lchip = platform_get_drvdata(dev);
 501
 502        if (lchip) {
 503                __locomo_remove(lchip);
 504                platform_set_drvdata(dev, NULL);
 505        }
 506
 507        return 0;
 508}
 509
 510/*
 511 *      Not sure if this should be on the system bus or not yet.
 512 *      We really want some way to register a system device at
 513 *      the per-machine level, and then have this driver pick
 514 *      up the registered devices.
 515 */
 516static struct platform_driver locomo_device_driver = {
 517        .probe          = locomo_probe,
 518        .remove         = locomo_remove,
 519#ifdef CONFIG_PM
 520        .suspend        = locomo_suspend,
 521        .resume         = locomo_resume,
 522#endif
 523        .driver         = {
 524                .name   = "locomo",
 525        },
 526};
 527
 528/*
 529 *      Get the parent device driver (us) structure
 530 *      from a child function device
 531 */
 532static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
 533{
 534        return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
 535}
 536
 537void locomo_gpio_set_dir(struct device *dev, unsigned int bits, unsigned int dir)
 538{
 539        struct locomo *lchip = dev_get_drvdata(dev);
 540        unsigned long flags;
 541        unsigned int r;
 542
 543        if (!lchip)
 544                return;
 545
 546        spin_lock_irqsave(&lchip->lock, flags);
 547
 548        r = locomo_readl(lchip->base + LOCOMO_GPD);
 549        if (dir)
 550                r |= bits;
 551        else
 552                r &= ~bits;
 553        locomo_writel(r, lchip->base + LOCOMO_GPD);
 554
 555        r = locomo_readl(lchip->base + LOCOMO_GPE);
 556        if (dir)
 557                r |= bits;
 558        else
 559                r &= ~bits;
 560        locomo_writel(r, lchip->base + LOCOMO_GPE);
 561
 562        spin_unlock_irqrestore(&lchip->lock, flags);
 563}
 564EXPORT_SYMBOL(locomo_gpio_set_dir);
 565
 566int locomo_gpio_read_level(struct device *dev, unsigned int bits)
 567{
 568        struct locomo *lchip = dev_get_drvdata(dev);
 569        unsigned long flags;
 570        unsigned int ret;
 571
 572        if (!lchip)
 573                return -ENODEV;
 574
 575        spin_lock_irqsave(&lchip->lock, flags);
 576        ret = locomo_readl(lchip->base + LOCOMO_GPL);
 577        spin_unlock_irqrestore(&lchip->lock, flags);
 578
 579        ret &= bits;
 580        return ret;
 581}
 582EXPORT_SYMBOL(locomo_gpio_read_level);
 583
 584int locomo_gpio_read_output(struct device *dev, unsigned int bits)
 585{
 586        struct locomo *lchip = dev_get_drvdata(dev);
 587        unsigned long flags;
 588        unsigned int ret;
 589
 590        if (!lchip)
 591                return -ENODEV;
 592
 593        spin_lock_irqsave(&lchip->lock, flags);
 594        ret = locomo_readl(lchip->base + LOCOMO_GPO);
 595        spin_unlock_irqrestore(&lchip->lock, flags);
 596
 597        ret &= bits;
 598        return ret;
 599}
 600EXPORT_SYMBOL(locomo_gpio_read_output);
 601
 602void locomo_gpio_write(struct device *dev, unsigned int bits, unsigned int set)
 603{
 604        struct locomo *lchip = dev_get_drvdata(dev);
 605        unsigned long flags;
 606        unsigned int r;
 607
 608        if (!lchip)
 609                return;
 610
 611        spin_lock_irqsave(&lchip->lock, flags);
 612
 613        r = locomo_readl(lchip->base + LOCOMO_GPO);
 614        if (set)
 615                r |= bits;
 616        else
 617                r &= ~bits;
 618        locomo_writel(r, lchip->base + LOCOMO_GPO);
 619
 620        spin_unlock_irqrestore(&lchip->lock, flags);
 621}
 622EXPORT_SYMBOL(locomo_gpio_write);
 623
 624static void locomo_m62332_sendbit(void *mapbase, int bit)
 625{
 626        unsigned int r;
 627
 628        r = locomo_readl(mapbase + LOCOMO_DAC);
 629        r &=  ~(LOCOMO_DAC_SCLOEB);
 630        locomo_writel(r, mapbase + LOCOMO_DAC);
 631        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 632        udelay(DAC_DATA_HOLD_TIME);     /* 300 nsec */
 633        r = locomo_readl(mapbase + LOCOMO_DAC);
 634        r &=  ~(LOCOMO_DAC_SCLOEB);
 635        locomo_writel(r, mapbase + LOCOMO_DAC);
 636        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 637        udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
 638
 639        if (bit & 1) {
 640                r = locomo_readl(mapbase + LOCOMO_DAC);
 641                r |=  LOCOMO_DAC_SDAOEB;
 642                locomo_writel(r, mapbase + LOCOMO_DAC);
 643                udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 644        } else {
 645                r = locomo_readl(mapbase + LOCOMO_DAC);
 646                r &=  ~(LOCOMO_DAC_SDAOEB);
 647                locomo_writel(r, mapbase + LOCOMO_DAC);
 648                udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 649        }
 650
 651        udelay(DAC_DATA_SETUP_TIME);    /* 250 nsec */
 652        r = locomo_readl(mapbase + LOCOMO_DAC);
 653        r |=  LOCOMO_DAC_SCLOEB;
 654        locomo_writel(r, mapbase + LOCOMO_DAC);
 655        udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 656        udelay(DAC_SCL_HIGH_HOLD_TIME); /*  4.0 usec */
 657}
 658
 659void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
 660{
 661        struct locomo *lchip = locomo_chip_driver(ldev);
 662        int i;
 663        unsigned char data;
 664        unsigned int r;
 665        void *mapbase = lchip->base;
 666        unsigned long flags;
 667
 668        spin_lock_irqsave(&lchip->lock, flags);
 669
 670        /* Start */
 671        udelay(DAC_BUS_FREE_TIME);      /* 5.0 usec */
 672        r = locomo_readl(mapbase + LOCOMO_DAC);
 673        r |=  LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
 674        locomo_writel(r, mapbase + LOCOMO_DAC);
 675        udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 676        udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
 677        r = locomo_readl(mapbase + LOCOMO_DAC);
 678        r &=  ~(LOCOMO_DAC_SDAOEB);
 679        locomo_writel(r, mapbase + LOCOMO_DAC);
 680        udelay(DAC_START_HOLD_TIME);    /* 5.0 usec */
 681        udelay(DAC_DATA_HOLD_TIME);     /* 300 nsec */
 682
 683        /* Send slave address and W bit (LSB is W bit) */
 684        data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
 685        for (i = 1; i <= 8; i++) {
 686                locomo_m62332_sendbit(mapbase, data >> (8 - i));
 687        }
 688
 689        /* Check A bit */
 690        r = locomo_readl(mapbase + LOCOMO_DAC);
 691        r &=  ~(LOCOMO_DAC_SCLOEB);
 692        locomo_writel(r, mapbase + LOCOMO_DAC);
 693        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 694        udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
 695        r = locomo_readl(mapbase + LOCOMO_DAC);
 696        r &=  ~(LOCOMO_DAC_SDAOEB);
 697        locomo_writel(r, mapbase + LOCOMO_DAC);
 698        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 699        r = locomo_readl(mapbase + LOCOMO_DAC);
 700        r |=  LOCOMO_DAC_SCLOEB;
 701        locomo_writel(r, mapbase + LOCOMO_DAC);
 702        udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 703        udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
 704        if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) {   /* High is error */
 705                printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
 706                goto out;
 707        }
 708
 709        /* Send Sub address (LSB is channel select) */
 710        /*    channel = 0 : ch1 select              */
 711        /*            = 1 : ch2 select              */
 712        data = M62332_SUB_ADDR + channel;
 713        for (i = 1; i <= 8; i++) {
 714                locomo_m62332_sendbit(mapbase, data >> (8 - i));
 715        }
 716
 717        /* Check A bit */
 718        r = locomo_readl(mapbase + LOCOMO_DAC);
 719        r &=  ~(LOCOMO_DAC_SCLOEB);
 720        locomo_writel(r, mapbase + LOCOMO_DAC);
 721        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 722        udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
 723        r = locomo_readl(mapbase + LOCOMO_DAC);
 724        r &=  ~(LOCOMO_DAC_SDAOEB);
 725        locomo_writel(r, mapbase + LOCOMO_DAC);
 726        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 727        r = locomo_readl(mapbase + LOCOMO_DAC);
 728        r |=  LOCOMO_DAC_SCLOEB;
 729        locomo_writel(r, mapbase + LOCOMO_DAC);
 730        udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 731        udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
 732        if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) {   /* High is error */
 733                printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
 734                goto out;
 735        }
 736
 737        /* Send DAC data */
 738        for (i = 1; i <= 8; i++) {
 739                locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
 740        }
 741
 742        /* Check A bit */
 743        r = locomo_readl(mapbase + LOCOMO_DAC);
 744        r &=  ~(LOCOMO_DAC_SCLOEB);
 745        locomo_writel(r, mapbase + LOCOMO_DAC);
 746        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 747        udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
 748        r = locomo_readl(mapbase + LOCOMO_DAC);
 749        r &=  ~(LOCOMO_DAC_SDAOEB);
 750        locomo_writel(r, mapbase + LOCOMO_DAC);
 751        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 752        r = locomo_readl(mapbase + LOCOMO_DAC);
 753        r |=  LOCOMO_DAC_SCLOEB;
 754        locomo_writel(r, mapbase + LOCOMO_DAC);
 755        udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 756        udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
 757        if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) {   /* High is error */
 758                printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
 759        }
 760
 761out:
 762        /* stop */
 763        r = locomo_readl(mapbase + LOCOMO_DAC);
 764        r &=  ~(LOCOMO_DAC_SCLOEB);
 765        locomo_writel(r, mapbase + LOCOMO_DAC);
 766        udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
 767        udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
 768        r = locomo_readl(mapbase + LOCOMO_DAC);
 769        r |=  LOCOMO_DAC_SCLOEB;
 770        locomo_writel(r, mapbase + LOCOMO_DAC);
 771        udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 772        udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
 773        r = locomo_readl(mapbase + LOCOMO_DAC);
 774        r |=  LOCOMO_DAC_SDAOEB;
 775        locomo_writel(r, mapbase + LOCOMO_DAC);
 776        udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
 777        udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
 778
 779        r = locomo_readl(mapbase + LOCOMO_DAC);
 780        r |=  LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
 781        locomo_writel(r, mapbase + LOCOMO_DAC);
 782        udelay(DAC_LOW_SETUP_TIME);     /* 1000 nsec */
 783        udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
 784
 785        spin_unlock_irqrestore(&lchip->lock, flags);
 786}
 787EXPORT_SYMBOL(locomo_m62332_senddata);
 788
 789/*
 790 *      Frontlight control
 791 */
 792
 793void locomo_frontlight_set(struct locomo_dev *dev, int duty, int vr, int bpwf)
 794{
 795        unsigned long flags;
 796        struct locomo *lchip = locomo_chip_driver(dev);
 797
 798        if (vr)
 799                locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 1);
 800        else
 801                locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
 802
 803        spin_lock_irqsave(&lchip->lock, flags);
 804        locomo_writel(bpwf, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
 805        udelay(100);
 806        locomo_writel(duty, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
 807        locomo_writel(bpwf | LOCOMO_ALC_EN, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
 808        spin_unlock_irqrestore(&lchip->lock, flags);
 809}
 810EXPORT_SYMBOL(locomo_frontlight_set);
 811
 812/*
 813 *      LoCoMo "Register Access Bus."
 814 *
 815 *      We model this as a regular bus type, and hang devices directly
 816 *      off this.
 817 */
 818static int locomo_match(struct device *_dev, struct device_driver *_drv)
 819{
 820        struct locomo_dev *dev = LOCOMO_DEV(_dev);
 821        struct locomo_driver *drv = LOCOMO_DRV(_drv);
 822
 823        return dev->devid == drv->devid;
 824}
 825
 826static int locomo_bus_probe(struct device *dev)
 827{
 828        struct locomo_dev *ldev = LOCOMO_DEV(dev);
 829        struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
 830        int ret = -ENODEV;
 831
 832        if (drv->probe)
 833                ret = drv->probe(ldev);
 834        return ret;
 835}
 836
 837static void locomo_bus_remove(struct device *dev)
 838{
 839        struct locomo_dev *ldev = LOCOMO_DEV(dev);
 840        struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
 841
 842        if (drv->remove)
 843                drv->remove(ldev);
 844}
 845
 846struct bus_type locomo_bus_type = {
 847        .name           = "locomo-bus",
 848        .match          = locomo_match,
 849        .probe          = locomo_bus_probe,
 850        .remove         = locomo_bus_remove,
 851};
 852
 853int locomo_driver_register(struct locomo_driver *driver)
 854{
 855        driver->drv.bus = &locomo_bus_type;
 856        return driver_register(&driver->drv);
 857}
 858EXPORT_SYMBOL(locomo_driver_register);
 859
 860void locomo_driver_unregister(struct locomo_driver *driver)
 861{
 862        driver_unregister(&driver->drv);
 863}
 864EXPORT_SYMBOL(locomo_driver_unregister);
 865
 866static int __init locomo_init(void)
 867{
 868        int ret = bus_register(&locomo_bus_type);
 869        if (ret == 0)
 870                platform_driver_register(&locomo_device_driver);
 871        return ret;
 872}
 873
 874static void __exit locomo_exit(void)
 875{
 876        platform_driver_unregister(&locomo_device_driver);
 877        bus_unregister(&locomo_bus_type);
 878}
 879
 880module_init(locomo_init);
 881module_exit(locomo_exit);
 882
 883MODULE_DESCRIPTION("Sharp LoCoMo core driver");
 884MODULE_LICENSE("GPL");
 885MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
 886