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