Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.17-rc4 690 lines 18 kB view raw
1/* 2 * htc-i2cpld.c 3 * Chip driver for an unknown CPLD chip found on omap850 HTC devices like 4 * the HTC Wizard and HTC Herald. 5 * The cpld is located on the i2c bus and acts as an input/output GPIO 6 * extender. 7 * 8 * Copyright (C) 2009 Cory Maccarrone <darkstar6262@gmail.com> 9 * 10 * Based on work done in the linwizard project 11 * Copyright (C) 2008-2009 Angelo Arrifano <miknix@gmail.com> 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 26 */ 27 28#include <linux/kernel.h> 29#include <linux/init.h> 30#include <linux/module.h> 31#include <linux/interrupt.h> 32#include <linux/platform_device.h> 33#include <linux/i2c.h> 34#include <linux/irq.h> 35#include <linux/spinlock.h> 36#include <linux/htcpld.h> 37#include <linux/gpio.h> 38#include <linux/slab.h> 39 40struct htcpld_chip { 41 spinlock_t lock; 42 43 /* chip info */ 44 u8 reset; 45 u8 addr; 46 struct device *dev; 47 struct i2c_client *client; 48 49 /* Output details */ 50 u8 cache_out; 51 struct gpio_chip chip_out; 52 53 /* Input details */ 54 u8 cache_in; 55 struct gpio_chip chip_in; 56 57 u16 irqs_enabled; 58 uint irq_start; 59 int nirqs; 60 61 unsigned int flow_type; 62 /* 63 * Work structure to allow for setting values outside of any 64 * possible interrupt context 65 */ 66 struct work_struct set_val_work; 67}; 68 69struct htcpld_data { 70 /* irq info */ 71 u16 irqs_enabled; 72 uint irq_start; 73 int nirqs; 74 uint chained_irq; 75 unsigned int int_reset_gpio_hi; 76 unsigned int int_reset_gpio_lo; 77 78 /* htcpld info */ 79 struct htcpld_chip *chip; 80 unsigned int nchips; 81}; 82 83/* There does not appear to be a way to proactively mask interrupts 84 * on the htcpld chip itself. So, we simply ignore interrupts that 85 * aren't desired. */ 86static void htcpld_mask(struct irq_data *data) 87{ 88 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data); 89 chip->irqs_enabled &= ~(1 << (data->irq - chip->irq_start)); 90 pr_debug("HTCPLD mask %d %04x\n", data->irq, chip->irqs_enabled); 91} 92static void htcpld_unmask(struct irq_data *data) 93{ 94 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data); 95 chip->irqs_enabled |= 1 << (data->irq - chip->irq_start); 96 pr_debug("HTCPLD unmask %d %04x\n", data->irq, chip->irqs_enabled); 97} 98 99static int htcpld_set_type(struct irq_data *data, unsigned int flags) 100{ 101 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data); 102 103 if (flags & ~IRQ_TYPE_SENSE_MASK) 104 return -EINVAL; 105 106 /* We only allow edge triggering */ 107 if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)) 108 return -EINVAL; 109 110 chip->flow_type = flags; 111 return 0; 112} 113 114static struct irq_chip htcpld_muxed_chip = { 115 .name = "htcpld", 116 .irq_mask = htcpld_mask, 117 .irq_unmask = htcpld_unmask, 118 .irq_set_type = htcpld_set_type, 119}; 120 121/* To properly dispatch IRQ events, we need to read from the 122 * chip. This is an I2C action that could possibly sleep 123 * (which is bad in interrupt context) -- so we use a threaded 124 * interrupt handler to get around that. 125 */ 126static irqreturn_t htcpld_handler(int irq, void *dev) 127{ 128 struct htcpld_data *htcpld = dev; 129 unsigned int i; 130 unsigned long flags; 131 int irqpin; 132 133 if (!htcpld) { 134 pr_debug("htcpld is null in ISR\n"); 135 return IRQ_HANDLED; 136 } 137 138 /* 139 * For each chip, do a read of the chip and trigger any interrupts 140 * desired. The interrupts will be triggered from LSB to MSB (i.e. 141 * bit 0 first, then bit 1, etc.) 142 * 143 * For chips that have no interrupt range specified, just skip 'em. 144 */ 145 for (i = 0; i < htcpld->nchips; i++) { 146 struct htcpld_chip *chip = &htcpld->chip[i]; 147 struct i2c_client *client; 148 int val; 149 unsigned long uval, old_val; 150 151 if (!chip) { 152 pr_debug("chip %d is null in ISR\n", i); 153 continue; 154 } 155 156 if (chip->nirqs == 0) 157 continue; 158 159 client = chip->client; 160 if (!client) { 161 pr_debug("client %d is null in ISR\n", i); 162 continue; 163 } 164 165 /* Scan the chip */ 166 val = i2c_smbus_read_byte_data(client, chip->cache_out); 167 if (val < 0) { 168 /* Throw a warning and skip this chip */ 169 dev_warn(chip->dev, "Unable to read from chip: %d\n", 170 val); 171 continue; 172 } 173 174 uval = (unsigned long)val; 175 176 spin_lock_irqsave(&chip->lock, flags); 177 178 /* Save away the old value so we can compare it */ 179 old_val = chip->cache_in; 180 181 /* Write the new value */ 182 chip->cache_in = uval; 183 184 spin_unlock_irqrestore(&chip->lock, flags); 185 186 /* 187 * For each bit in the data (starting at bit 0), trigger 188 * associated interrupts. 189 */ 190 for (irqpin = 0; irqpin < chip->nirqs; irqpin++) { 191 unsigned oldb, newb, type = chip->flow_type; 192 193 irq = chip->irq_start + irqpin; 194 195 /* Run the IRQ handler, but only if the bit value 196 * changed, and the proper flags are set */ 197 oldb = (old_val >> irqpin) & 1; 198 newb = (uval >> irqpin) & 1; 199 200 if ((!oldb && newb && (type & IRQ_TYPE_EDGE_RISING)) || 201 (oldb && !newb && (type & IRQ_TYPE_EDGE_FALLING))) { 202 pr_debug("fire IRQ %d\n", irqpin); 203 generic_handle_irq(irq); 204 } 205 } 206 } 207 208 /* 209 * In order to continue receiving interrupts, the int_reset_gpio must 210 * be asserted. 211 */ 212 if (htcpld->int_reset_gpio_hi) 213 gpio_set_value(htcpld->int_reset_gpio_hi, 1); 214 if (htcpld->int_reset_gpio_lo) 215 gpio_set_value(htcpld->int_reset_gpio_lo, 0); 216 217 return IRQ_HANDLED; 218} 219 220/* 221 * The GPIO set routines can be called from interrupt context, especially if, 222 * for example they're attached to the led-gpio framework and a trigger is 223 * enabled. As such, we declared work above in the htcpld_chip structure, 224 * and that work is scheduled in the set routine. The kernel can then run 225 * the I2C functions, which will sleep, in process context. 226 */ 227static void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val) 228{ 229 struct i2c_client *client; 230 struct htcpld_chip *chip_data; 231 unsigned long flags; 232 233 chip_data = container_of(chip, struct htcpld_chip, chip_out); 234 if (!chip_data) 235 return; 236 237 client = chip_data->client; 238 if (client == NULL) 239 return; 240 241 spin_lock_irqsave(&chip_data->lock, flags); 242 if (val) 243 chip_data->cache_out |= (1 << offset); 244 else 245 chip_data->cache_out &= ~(1 << offset); 246 spin_unlock_irqrestore(&chip_data->lock, flags); 247 248 schedule_work(&(chip_data->set_val_work)); 249} 250 251static void htcpld_chip_set_ni(struct work_struct *work) 252{ 253 struct htcpld_chip *chip_data; 254 struct i2c_client *client; 255 256 chip_data = container_of(work, struct htcpld_chip, set_val_work); 257 client = chip_data->client; 258 i2c_smbus_read_byte_data(client, chip_data->cache_out); 259} 260 261static int htcpld_chip_get(struct gpio_chip *chip, unsigned offset) 262{ 263 struct htcpld_chip *chip_data; 264 int val = 0; 265 int is_input = 0; 266 267 /* Try out first */ 268 chip_data = container_of(chip, struct htcpld_chip, chip_out); 269 if (!chip_data) { 270 /* Try in */ 271 is_input = 1; 272 chip_data = container_of(chip, struct htcpld_chip, chip_in); 273 if (!chip_data) 274 return -EINVAL; 275 } 276 277 /* Determine if this is an input or output GPIO */ 278 if (!is_input) 279 /* Use the output cache */ 280 val = (chip_data->cache_out >> offset) & 1; 281 else 282 /* Use the input cache */ 283 val = (chip_data->cache_in >> offset) & 1; 284 285 if (val) 286 return 1; 287 else 288 return 0; 289} 290 291static int htcpld_direction_output(struct gpio_chip *chip, 292 unsigned offset, int value) 293{ 294 htcpld_chip_set(chip, offset, value); 295 return 0; 296} 297 298static int htcpld_direction_input(struct gpio_chip *chip, 299 unsigned offset) 300{ 301 /* 302 * No-op: this function can only be called on the input chip. 303 * We do however make sure the offset is within range. 304 */ 305 return (offset < chip->ngpio) ? 0 : -EINVAL; 306} 307 308static int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset) 309{ 310 struct htcpld_chip *chip_data; 311 312 chip_data = container_of(chip, struct htcpld_chip, chip_in); 313 314 if (offset < chip_data->nirqs) 315 return chip_data->irq_start + offset; 316 else 317 return -EINVAL; 318} 319 320static void htcpld_chip_reset(struct i2c_client *client) 321{ 322 struct htcpld_chip *chip_data = i2c_get_clientdata(client); 323 if (!chip_data) 324 return; 325 326 i2c_smbus_read_byte_data( 327 client, (chip_data->cache_out = chip_data->reset)); 328} 329 330static int htcpld_setup_chip_irq( 331 struct platform_device *pdev, 332 int chip_index) 333{ 334 struct htcpld_data *htcpld; 335 struct htcpld_chip *chip; 336 unsigned int irq, irq_end; 337 int ret = 0; 338 339 /* Get the platform and driver data */ 340 htcpld = platform_get_drvdata(pdev); 341 chip = &htcpld->chip[chip_index]; 342 343 /* Setup irq handlers */ 344 irq_end = chip->irq_start + chip->nirqs; 345 for (irq = chip->irq_start; irq < irq_end; irq++) { 346 irq_set_chip_and_handler(irq, &htcpld_muxed_chip, 347 handle_simple_irq); 348 irq_set_chip_data(irq, chip); 349#ifdef CONFIG_ARM 350 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE); 351#else 352 irq_set_probe(irq); 353#endif 354 } 355 356 return ret; 357} 358 359static int htcpld_register_chip_i2c( 360 struct platform_device *pdev, 361 int chip_index) 362{ 363 struct htcpld_data *htcpld; 364 struct device *dev = &pdev->dev; 365 struct htcpld_core_platform_data *pdata; 366 struct htcpld_chip *chip; 367 struct htcpld_chip_platform_data *plat_chip_data; 368 struct i2c_adapter *adapter; 369 struct i2c_client *client; 370 struct i2c_board_info info; 371 372 /* Get the platform and driver data */ 373 pdata = dev_get_platdata(dev); 374 htcpld = platform_get_drvdata(pdev); 375 chip = &htcpld->chip[chip_index]; 376 plat_chip_data = &pdata->chip[chip_index]; 377 378 adapter = i2c_get_adapter(pdata->i2c_adapter_id); 379 if (adapter == NULL) { 380 /* Eek, no such I2C adapter! Bail out. */ 381 dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n", 382 plat_chip_data->addr, pdata->i2c_adapter_id); 383 return -ENODEV; 384 } 385 386 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) { 387 dev_warn(dev, "i2c adapter %d non-functional\n", 388 pdata->i2c_adapter_id); 389 return -EINVAL; 390 } 391 392 memset(&info, 0, sizeof(struct i2c_board_info)); 393 info.addr = plat_chip_data->addr; 394 strlcpy(info.type, "htcpld-chip", I2C_NAME_SIZE); 395 info.platform_data = chip; 396 397 /* Add the I2C device. This calls the probe() function. */ 398 client = i2c_new_device(adapter, &info); 399 if (!client) { 400 /* I2C device registration failed, contineu with the next */ 401 dev_warn(dev, "Unable to add I2C device for 0x%x\n", 402 plat_chip_data->addr); 403 return -ENODEV; 404 } 405 406 i2c_set_clientdata(client, chip); 407 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%x", client->addr); 408 chip->client = client; 409 410 /* Reset the chip */ 411 htcpld_chip_reset(client); 412 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out); 413 414 return 0; 415} 416 417static void htcpld_unregister_chip_i2c( 418 struct platform_device *pdev, 419 int chip_index) 420{ 421 struct htcpld_data *htcpld; 422 struct htcpld_chip *chip; 423 424 /* Get the platform and driver data */ 425 htcpld = platform_get_drvdata(pdev); 426 chip = &htcpld->chip[chip_index]; 427 428 if (chip->client) 429 i2c_unregister_device(chip->client); 430} 431 432static int htcpld_register_chip_gpio( 433 struct platform_device *pdev, 434 int chip_index) 435{ 436 struct htcpld_data *htcpld; 437 struct device *dev = &pdev->dev; 438 struct htcpld_core_platform_data *pdata; 439 struct htcpld_chip *chip; 440 struct htcpld_chip_platform_data *plat_chip_data; 441 struct gpio_chip *gpio_chip; 442 int ret = 0; 443 444 /* Get the platform and driver data */ 445 pdata = dev_get_platdata(dev); 446 htcpld = platform_get_drvdata(pdev); 447 chip = &htcpld->chip[chip_index]; 448 plat_chip_data = &pdata->chip[chip_index]; 449 450 /* Setup the GPIO chips */ 451 gpio_chip = &(chip->chip_out); 452 gpio_chip->label = "htcpld-out"; 453 gpio_chip->dev = dev; 454 gpio_chip->owner = THIS_MODULE; 455 gpio_chip->get = htcpld_chip_get; 456 gpio_chip->set = htcpld_chip_set; 457 gpio_chip->direction_input = NULL; 458 gpio_chip->direction_output = htcpld_direction_output; 459 gpio_chip->base = plat_chip_data->gpio_out_base; 460 gpio_chip->ngpio = plat_chip_data->num_gpios; 461 462 gpio_chip = &(chip->chip_in); 463 gpio_chip->label = "htcpld-in"; 464 gpio_chip->dev = dev; 465 gpio_chip->owner = THIS_MODULE; 466 gpio_chip->get = htcpld_chip_get; 467 gpio_chip->set = NULL; 468 gpio_chip->direction_input = htcpld_direction_input; 469 gpio_chip->direction_output = NULL; 470 gpio_chip->to_irq = htcpld_chip_to_irq; 471 gpio_chip->base = plat_chip_data->gpio_in_base; 472 gpio_chip->ngpio = plat_chip_data->num_gpios; 473 474 /* Add the GPIO chips */ 475 ret = gpiochip_add(&(chip->chip_out)); 476 if (ret) { 477 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n", 478 plat_chip_data->addr, ret); 479 return ret; 480 } 481 482 ret = gpiochip_add(&(chip->chip_in)); 483 if (ret) { 484 int error; 485 486 dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n", 487 plat_chip_data->addr, ret); 488 489 error = gpiochip_remove(&(chip->chip_out)); 490 if (error) 491 dev_warn(dev, "Error while trying to unregister gpio chip: %d\n", error); 492 493 return ret; 494 } 495 496 return 0; 497} 498 499static int htcpld_setup_chips(struct platform_device *pdev) 500{ 501 struct htcpld_data *htcpld; 502 struct device *dev = &pdev->dev; 503 struct htcpld_core_platform_data *pdata; 504 int i; 505 506 /* Get the platform and driver data */ 507 pdata = dev_get_platdata(dev); 508 htcpld = platform_get_drvdata(pdev); 509 510 /* Setup each chip's output GPIOs */ 511 htcpld->nchips = pdata->num_chip; 512 htcpld->chip = devm_kzalloc(dev, sizeof(struct htcpld_chip) * htcpld->nchips, 513 GFP_KERNEL); 514 if (!htcpld->chip) { 515 dev_warn(dev, "Unable to allocate memory for chips\n"); 516 return -ENOMEM; 517 } 518 519 /* Add the chips as best we can */ 520 for (i = 0; i < htcpld->nchips; i++) { 521 int ret; 522 523 /* Setup the HTCPLD chips */ 524 htcpld->chip[i].reset = pdata->chip[i].reset; 525 htcpld->chip[i].cache_out = pdata->chip[i].reset; 526 htcpld->chip[i].cache_in = 0; 527 htcpld->chip[i].dev = dev; 528 htcpld->chip[i].irq_start = pdata->chip[i].irq_base; 529 htcpld->chip[i].nirqs = pdata->chip[i].num_irqs; 530 531 INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni); 532 spin_lock_init(&(htcpld->chip[i].lock)); 533 534 /* Setup the interrupts for the chip */ 535 if (htcpld->chained_irq) { 536 ret = htcpld_setup_chip_irq(pdev, i); 537 if (ret) 538 continue; 539 } 540 541 /* Register the chip with I2C */ 542 ret = htcpld_register_chip_i2c(pdev, i); 543 if (ret) 544 continue; 545 546 547 /* Register the chips with the GPIO subsystem */ 548 ret = htcpld_register_chip_gpio(pdev, i); 549 if (ret) { 550 /* Unregister the chip from i2c and continue */ 551 htcpld_unregister_chip_i2c(pdev, i); 552 continue; 553 } 554 555 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr); 556 } 557 558 return 0; 559} 560 561static int htcpld_core_probe(struct platform_device *pdev) 562{ 563 struct htcpld_data *htcpld; 564 struct device *dev = &pdev->dev; 565 struct htcpld_core_platform_data *pdata; 566 struct resource *res; 567 int ret = 0; 568 569 if (!dev) 570 return -ENODEV; 571 572 pdata = dev_get_platdata(dev); 573 if (!pdata) { 574 dev_warn(dev, "Platform data not found for htcpld core!\n"); 575 return -ENXIO; 576 } 577 578 htcpld = devm_kzalloc(dev, sizeof(struct htcpld_data), GFP_KERNEL); 579 if (!htcpld) 580 return -ENOMEM; 581 582 /* Find chained irq */ 583 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 584 if (res) { 585 int flags; 586 htcpld->chained_irq = res->start; 587 588 /* Setup the chained interrupt handler */ 589 flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 590 ret = request_threaded_irq(htcpld->chained_irq, 591 NULL, htcpld_handler, 592 flags, pdev->name, htcpld); 593 if (ret) { 594 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret); 595 return ret; 596 } else 597 device_init_wakeup(dev, 0); 598 } 599 600 /* Set the driver data */ 601 platform_set_drvdata(pdev, htcpld); 602 603 /* Setup the htcpld chips */ 604 ret = htcpld_setup_chips(pdev); 605 if (ret) 606 return ret; 607 608 /* Request the GPIO(s) for the int reset and set them up */ 609 if (pdata->int_reset_gpio_hi) { 610 ret = gpio_request(pdata->int_reset_gpio_hi, "htcpld-core"); 611 if (ret) { 612 /* 613 * If it failed, that sucks, but we can probably 614 * continue on without it. 615 */ 616 dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n"); 617 htcpld->int_reset_gpio_hi = 0; 618 } else { 619 htcpld->int_reset_gpio_hi = pdata->int_reset_gpio_hi; 620 gpio_set_value(htcpld->int_reset_gpio_hi, 1); 621 } 622 } 623 624 if (pdata->int_reset_gpio_lo) { 625 ret = gpio_request(pdata->int_reset_gpio_lo, "htcpld-core"); 626 if (ret) { 627 /* 628 * If it failed, that sucks, but we can probably 629 * continue on without it. 630 */ 631 dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n"); 632 htcpld->int_reset_gpio_lo = 0; 633 } else { 634 htcpld->int_reset_gpio_lo = pdata->int_reset_gpio_lo; 635 gpio_set_value(htcpld->int_reset_gpio_lo, 0); 636 } 637 } 638 639 dev_info(dev, "Initialized successfully\n"); 640 return 0; 641} 642 643/* The I2C Driver -- used internally */ 644static const struct i2c_device_id htcpld_chip_id[] = { 645 { "htcpld-chip", 0 }, 646 { } 647}; 648MODULE_DEVICE_TABLE(i2c, htcpld_chip_id); 649 650 651static struct i2c_driver htcpld_chip_driver = { 652 .driver = { 653 .name = "htcpld-chip", 654 }, 655 .id_table = htcpld_chip_id, 656}; 657 658/* The Core Driver */ 659static struct platform_driver htcpld_core_driver = { 660 .driver = { 661 .name = "i2c-htcpld", 662 }, 663}; 664 665static int __init htcpld_core_init(void) 666{ 667 int ret; 668 669 /* Register the I2C Chip driver */ 670 ret = i2c_add_driver(&htcpld_chip_driver); 671 if (ret) 672 return ret; 673 674 /* Probe for our chips */ 675 return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe); 676} 677 678static void __exit htcpld_core_exit(void) 679{ 680 i2c_del_driver(&htcpld_chip_driver); 681 platform_driver_unregister(&htcpld_core_driver); 682} 683 684module_init(htcpld_core_init); 685module_exit(htcpld_core_exit); 686 687MODULE_AUTHOR("Cory Maccarrone <darkstar6262@gmail.com>"); 688MODULE_DESCRIPTION("I2C HTC PLD Driver"); 689MODULE_LICENSE("GPL"); 690