linux/drivers/gpio/gpio-pca953x.c
<<
>>
Prefs
   1/*
   2 *  PCA953x 4/8/16 bit I/O ports
   3 *
   4 *  Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
   5 *  Copyright (C) 2007 Marvell International Ltd.
   6 *
   7 *  Derived from drivers/i2c/chips/pca9539.c
   8 *
   9 *  This program is free software; you can redistribute it and/or modify
  10 *  it under the terms of the GNU General Public License as published by
  11 *  the Free Software Foundation; version 2 of the License.
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/gpio.h>
  17#include <linux/interrupt.h>
  18#include <linux/irq.h>
  19#include <linux/irqdomain.h>
  20#include <linux/i2c.h>
  21#include <linux/i2c/pca953x.h>
  22#include <linux/slab.h>
  23#ifdef CONFIG_OF_GPIO
  24#include <linux/of_platform.h>
  25#endif
  26
  27#define PCA953X_INPUT           0
  28#define PCA953X_OUTPUT          1
  29#define PCA953X_INVERT          2
  30#define PCA953X_DIRECTION       3
  31
  32#define REG_ADDR_AI             0x80
  33
  34#define PCA957X_IN              0
  35#define PCA957X_INVRT           1
  36#define PCA957X_BKEN            2
  37#define PCA957X_PUPD            3
  38#define PCA957X_CFG             4
  39#define PCA957X_OUT             5
  40#define PCA957X_MSK             6
  41#define PCA957X_INTS            7
  42
  43#define PCA_GPIO_MASK           0x00FF
  44#define PCA_INT                 0x0100
  45#define PCA953X_TYPE            0x1000
  46#define PCA957X_TYPE            0x2000
  47
  48static const struct i2c_device_id pca953x_id[] = {
  49        { "pca9534", 8  | PCA953X_TYPE | PCA_INT, },
  50        { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
  51        { "pca9536", 4  | PCA953X_TYPE, },
  52        { "pca9537", 4  | PCA953X_TYPE | PCA_INT, },
  53        { "pca9538", 8  | PCA953X_TYPE | PCA_INT, },
  54        { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
  55        { "pca9554", 8  | PCA953X_TYPE | PCA_INT, },
  56        { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
  57        { "pca9556", 8  | PCA953X_TYPE, },
  58        { "pca9557", 8  | PCA953X_TYPE, },
  59        { "pca9574", 8  | PCA957X_TYPE | PCA_INT, },
  60        { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
  61
  62        { "max7310", 8  | PCA953X_TYPE, },
  63        { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
  64        { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
  65        { "max7315", 8  | PCA953X_TYPE | PCA_INT, },
  66        { "pca6107", 8  | PCA953X_TYPE | PCA_INT, },
  67        { "tca6408", 8  | PCA953X_TYPE | PCA_INT, },
  68        { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
  69        { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
  70        { }
  71};
  72MODULE_DEVICE_TABLE(i2c, pca953x_id);
  73
  74struct pca953x_chip {
  75        unsigned gpio_start;
  76        u32 reg_output;
  77        u32 reg_direction;
  78        struct mutex i2c_lock;
  79
  80#ifdef CONFIG_GPIO_PCA953X_IRQ
  81        struct mutex irq_lock;
  82        u32 irq_mask;
  83        u32 irq_stat;
  84        u32 irq_trig_raise;
  85        u32 irq_trig_fall;
  86        int      irq_base;
  87        struct irq_domain *domain;
  88#endif
  89
  90        struct i2c_client *client;
  91        struct gpio_chip gpio_chip;
  92        const char *const *names;
  93        int     chip_type;
  94};
  95
  96static int pca953x_write_reg(struct pca953x_chip *chip, int reg, u32 val)
  97{
  98        int ret = 0;
  99
 100        if (chip->gpio_chip.ngpio <= 8)
 101                ret = i2c_smbus_write_byte_data(chip->client, reg, val);
 102        else if (chip->gpio_chip.ngpio == 24) {
 103                cpu_to_le32s(&val);
 104                ret = i2c_smbus_write_i2c_block_data(chip->client,
 105                                                (reg << 2) | REG_ADDR_AI,
 106                                                3,
 107                                                (u8 *) &val);
 108        }
 109        else {
 110                switch (chip->chip_type) {
 111                case PCA953X_TYPE:
 112                        ret = i2c_smbus_write_word_data(chip->client,
 113                                                        reg << 1, val);
 114                        break;
 115                case PCA957X_TYPE:
 116                        ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
 117                                                        val & 0xff);
 118                        if (ret < 0)
 119                                break;
 120                        ret = i2c_smbus_write_byte_data(chip->client,
 121                                                        (reg << 1) + 1,
 122                                                        (val & 0xff00) >> 8);
 123                        break;
 124                }
 125        }
 126
 127        if (ret < 0) {
 128                dev_err(&chip->client->dev, "failed writing register\n");
 129                return ret;
 130        }
 131
 132        return 0;
 133}
 134
 135static int pca953x_read_reg(struct pca953x_chip *chip, int reg, u32 *val)
 136{
 137        int ret;
 138
 139        if (chip->gpio_chip.ngpio <= 8) {
 140                ret = i2c_smbus_read_byte_data(chip->client, reg);
 141                *val = ret;
 142        }
 143        else if (chip->gpio_chip.ngpio == 24) {
 144                *val = 0;
 145                ret = i2c_smbus_read_i2c_block_data(chip->client,
 146                                                (reg << 2) | REG_ADDR_AI,
 147                                                3,
 148                                                (u8 *) val);
 149                le32_to_cpus(val);
 150        } else {
 151                ret = i2c_smbus_read_word_data(chip->client, reg << 1);
 152                *val = ret;
 153        }
 154
 155        if (ret < 0) {
 156                dev_err(&chip->client->dev, "failed reading register\n");
 157                return ret;
 158        }
 159
 160        return 0;
 161}
 162
 163static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
 164{
 165        struct pca953x_chip *chip;
 166        uint reg_val;
 167        int ret, offset = 0;
 168
 169        chip = container_of(gc, struct pca953x_chip, gpio_chip);
 170
 171        mutex_lock(&chip->i2c_lock);
 172        reg_val = chip->reg_direction | (1u << off);
 173
 174        switch (chip->chip_type) {
 175        case PCA953X_TYPE:
 176                offset = PCA953X_DIRECTION;
 177                break;
 178        case PCA957X_TYPE:
 179                offset = PCA957X_CFG;
 180                break;
 181        }
 182        ret = pca953x_write_reg(chip, offset, reg_val);
 183        if (ret)
 184                goto exit;
 185
 186        chip->reg_direction = reg_val;
 187        ret = 0;
 188exit:
 189        mutex_unlock(&chip->i2c_lock);
 190        return ret;
 191}
 192
 193static int pca953x_gpio_direction_output(struct gpio_chip *gc,
 194                unsigned off, int val)
 195{
 196        struct pca953x_chip *chip;
 197        uint reg_val;
 198        int ret, offset = 0;
 199
 200        chip = container_of(gc, struct pca953x_chip, gpio_chip);
 201
 202        mutex_lock(&chip->i2c_lock);
 203        /* set output level */
 204        if (val)
 205                reg_val = chip->reg_output | (1u << off);
 206        else
 207                reg_val = chip->reg_output & ~(1u << off);
 208
 209        switch (chip->chip_type) {
 210        case PCA953X_TYPE:
 211                offset = PCA953X_OUTPUT;
 212                break;
 213        case PCA957X_TYPE:
 214                offset = PCA957X_OUT;
 215                break;
 216        }
 217        ret = pca953x_write_reg(chip, offset, reg_val);
 218        if (ret)
 219                goto exit;
 220
 221        chip->reg_output = reg_val;
 222
 223        /* then direction */
 224        reg_val = chip->reg_direction & ~(1u << off);
 225        switch (chip->chip_type) {
 226        case PCA953X_TYPE:
 227                offset = PCA953X_DIRECTION;
 228                break;
 229        case PCA957X_TYPE:
 230                offset = PCA957X_CFG;
 231                break;
 232        }
 233        ret = pca953x_write_reg(chip, offset, reg_val);
 234        if (ret)
 235                goto exit;
 236
 237        chip->reg_direction = reg_val;
 238        ret = 0;
 239exit:
 240        mutex_unlock(&chip->i2c_lock);
 241        return ret;
 242}
 243
 244static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
 245{
 246        struct pca953x_chip *chip;
 247        u32 reg_val;
 248        int ret, offset = 0;
 249
 250        chip = container_of(gc, struct pca953x_chip, gpio_chip);
 251
 252        mutex_lock(&chip->i2c_lock);
 253        switch (chip->chip_type) {
 254        case PCA953X_TYPE:
 255                offset = PCA953X_INPUT;
 256                break;
 257        case PCA957X_TYPE:
 258                offset = PCA957X_IN;
 259                break;
 260        }
 261        ret = pca953x_read_reg(chip, offset, &reg_val);
 262        mutex_unlock(&chip->i2c_lock);
 263        if (ret < 0) {
 264                /* NOTE:  diagnostic already emitted; that's all we should
 265                 * do unless gpio_*_value_cansleep() calls become different
 266                 * from their nonsleeping siblings (and report faults).
 267                 */
 268                return 0;
 269        }
 270
 271        return (reg_val & (1u << off)) ? 1 : 0;
 272}
 273
 274static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
 275{
 276        struct pca953x_chip *chip;
 277        u32 reg_val;
 278        int ret, offset = 0;
 279
 280        chip = container_of(gc, struct pca953x_chip, gpio_chip);
 281
 282        mutex_lock(&chip->i2c_lock);
 283        if (val)
 284                reg_val = chip->reg_output | (1u << off);
 285        else
 286                reg_val = chip->reg_output & ~(1u << off);
 287
 288        switch (chip->chip_type) {
 289        case PCA953X_TYPE:
 290                offset = PCA953X_OUTPUT;
 291                break;
 292        case PCA957X_TYPE:
 293                offset = PCA957X_OUT;
 294                break;
 295        }
 296        ret = pca953x_write_reg(chip, offset, reg_val);
 297        if (ret)
 298                goto exit;
 299
 300        chip->reg_output = reg_val;
 301exit:
 302        mutex_unlock(&chip->i2c_lock);
 303}
 304
 305static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
 306{
 307        struct gpio_chip *gc;
 308
 309        gc = &chip->gpio_chip;
 310
 311        gc->direction_input  = pca953x_gpio_direction_input;
 312        gc->direction_output = pca953x_gpio_direction_output;
 313        gc->get = pca953x_gpio_get_value;
 314        gc->set = pca953x_gpio_set_value;
 315        gc->can_sleep = 1;
 316
 317        gc->base = chip->gpio_start;
 318        gc->ngpio = gpios;
 319        gc->label = chip->client->name;
 320        gc->dev = &chip->client->dev;
 321        gc->owner = THIS_MODULE;
 322        gc->names = chip->names;
 323}
 324
 325#ifdef CONFIG_GPIO_PCA953X_IRQ
 326static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
 327{
 328        struct pca953x_chip *chip;
 329
 330        chip = container_of(gc, struct pca953x_chip, gpio_chip);
 331        return chip->irq_base + off;
 332}
 333
 334static void pca953x_irq_mask(struct irq_data *d)
 335{
 336        struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
 337
 338        chip->irq_mask &= ~(1 << d->hwirq);
 339}
 340
 341static void pca953x_irq_unmask(struct irq_data *d)
 342{
 343        struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
 344
 345        chip->irq_mask |= 1 << d->hwirq;
 346}
 347
 348static void pca953x_irq_bus_lock(struct irq_data *d)
 349{
 350        struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
 351
 352        mutex_lock(&chip->irq_lock);
 353}
 354
 355static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
 356{
 357        struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
 358        u32 new_irqs;
 359        u32 level;
 360
 361        /* Look for any newly setup interrupt */
 362        new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
 363        new_irqs &= ~chip->reg_direction;
 364
 365        while (new_irqs) {
 366                level = __ffs(new_irqs);
 367                pca953x_gpio_direction_input(&chip->gpio_chip, level);
 368                new_irqs &= ~(1 << level);
 369        }
 370
 371        mutex_unlock(&chip->irq_lock);
 372}
 373
 374static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
 375{
 376        struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
 377        u32 mask = 1 << d->hwirq;
 378
 379        if (!(type & IRQ_TYPE_EDGE_BOTH)) {
 380                dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
 381                        d->irq, type);
 382                return -EINVAL;
 383        }
 384
 385        if (type & IRQ_TYPE_EDGE_FALLING)
 386                chip->irq_trig_fall |= mask;
 387        else
 388                chip->irq_trig_fall &= ~mask;
 389
 390        if (type & IRQ_TYPE_EDGE_RISING)
 391                chip->irq_trig_raise |= mask;
 392        else
 393                chip->irq_trig_raise &= ~mask;
 394
 395        return 0;
 396}
 397
 398static struct irq_chip pca953x_irq_chip = {
 399        .name                   = "pca953x",
 400        .irq_mask               = pca953x_irq_mask,
 401        .irq_unmask             = pca953x_irq_unmask,
 402        .irq_bus_lock           = pca953x_irq_bus_lock,
 403        .irq_bus_sync_unlock    = pca953x_irq_bus_sync_unlock,
 404        .irq_set_type           = pca953x_irq_set_type,
 405};
 406
 407static u32 pca953x_irq_pending(struct pca953x_chip *chip)
 408{
 409        u32 cur_stat;
 410        u32 old_stat;
 411        u32 pending;
 412        u32 trigger;
 413        int ret, offset = 0;
 414
 415        switch (chip->chip_type) {
 416        case PCA953X_TYPE:
 417                offset = PCA953X_INPUT;
 418                break;
 419        case PCA957X_TYPE:
 420                offset = PCA957X_IN;
 421                break;
 422        }
 423        ret = pca953x_read_reg(chip, offset, &cur_stat);
 424        if (ret)
 425                return 0;
 426
 427        /* Remove output pins from the equation */
 428        cur_stat &= chip->reg_direction;
 429
 430        old_stat = chip->irq_stat;
 431        trigger = (cur_stat ^ old_stat) & chip->irq_mask;
 432
 433        if (!trigger)
 434                return 0;
 435
 436        chip->irq_stat = cur_stat;
 437
 438        pending = (old_stat & chip->irq_trig_fall) |
 439                  (cur_stat & chip->irq_trig_raise);
 440        pending &= trigger;
 441
 442        return pending;
 443}
 444
 445static irqreturn_t pca953x_irq_handler(int irq, void *devid)
 446{
 447        struct pca953x_chip *chip = devid;
 448        u32 pending;
 449        u32 level;
 450
 451        pending = pca953x_irq_pending(chip);
 452
 453        if (!pending)
 454                return IRQ_HANDLED;
 455
 456        do {
 457                level = __ffs(pending);
 458                handle_nested_irq(irq_find_mapping(chip->domain, level));
 459
 460                pending &= ~(1 << level);
 461        } while (pending);
 462
 463        return IRQ_HANDLED;
 464}
 465
 466static int pca953x_irq_setup(struct pca953x_chip *chip,
 467                             const struct i2c_device_id *id,
 468                             int irq_base)
 469{
 470        struct i2c_client *client = chip->client;
 471        int ret, offset = 0;
 472        u32 temporary;
 473
 474        if (irq_base != -1
 475                        && (id->driver_data & PCA_INT)) {
 476                int lvl;
 477
 478                switch (chip->chip_type) {
 479                case PCA953X_TYPE:
 480                        offset = PCA953X_INPUT;
 481                        break;
 482                case PCA957X_TYPE:
 483                        offset = PCA957X_IN;
 484                        break;
 485                }
 486                ret = pca953x_read_reg(chip, offset, &temporary);
 487                chip->irq_stat = temporary;
 488                if (ret)
 489                        goto out_failed;
 490
 491                /*
 492                 * There is no way to know which GPIO line generated the
 493                 * interrupt.  We have to rely on the previous read for
 494                 * this purpose.
 495                 */
 496                chip->irq_stat &= chip->reg_direction;
 497                mutex_init(&chip->irq_lock);
 498
 499                chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1);
 500                if (chip->irq_base < 0)
 501                        goto out_failed;
 502
 503                chip->domain = irq_domain_add_legacy(client->dev.of_node,
 504                                                chip->gpio_chip.ngpio,
 505                                                chip->irq_base,
 506                                                0,
 507                                                &irq_domain_simple_ops,
 508                                                NULL);
 509                if (!chip->domain) {
 510                        ret = -ENODEV;
 511                        goto out_irqdesc_free;
 512                }
 513
 514                for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
 515                        int irq = lvl + chip->irq_base;
 516
 517                        irq_clear_status_flags(irq, IRQ_NOREQUEST);
 518                        irq_set_chip_data(irq, chip);
 519                        irq_set_chip(irq, &pca953x_irq_chip);
 520                        irq_set_nested_thread(irq, true);
 521#ifdef CONFIG_ARM
 522                        set_irq_flags(irq, IRQF_VALID);
 523#else
 524                        irq_set_noprobe(irq);
 525#endif
 526                }
 527
 528                ret = request_threaded_irq(client->irq,
 529                                           NULL,
 530                                           pca953x_irq_handler,
 531                                           IRQF_TRIGGER_LOW | IRQF_ONESHOT,
 532                                           dev_name(&client->dev), chip);
 533                if (ret) {
 534                        dev_err(&client->dev, "failed to request irq %d\n",
 535                                client->irq);
 536                        goto out_irqdesc_free;
 537                }
 538
 539                chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
 540        }
 541
 542        return 0;
 543
 544out_irqdesc_free:
 545        irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
 546out_failed:
 547        chip->irq_base = -1;
 548        return ret;
 549}
 550
 551static void pca953x_irq_teardown(struct pca953x_chip *chip)
 552{
 553        if (chip->irq_base != -1) {
 554                irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
 555                free_irq(chip->client->irq, chip);
 556        }
 557}
 558#else /* CONFIG_GPIO_PCA953X_IRQ */
 559static int pca953x_irq_setup(struct pca953x_chip *chip,
 560                             const struct i2c_device_id *id,
 561                             int irq_base)
 562{
 563        struct i2c_client *client = chip->client;
 564
 565        if (irq_base != -1 && (id->driver_data & PCA_INT))
 566                dev_warn(&client->dev, "interrupt support not compiled in\n");
 567
 568        return 0;
 569}
 570
 571static void pca953x_irq_teardown(struct pca953x_chip *chip)
 572{
 573}
 574#endif
 575
 576/*
 577 * Handlers for alternative sources of platform_data
 578 */
 579#ifdef CONFIG_OF_GPIO
 580/*
 581 * Translate OpenFirmware node properties into platform_data
 582 * WARNING: This is DEPRECATED and will be removed eventually!
 583 */
 584static void
 585pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
 586{
 587        struct device_node *node;
 588        const __be32 *val;
 589        int size;
 590
 591        node = client->dev.of_node;
 592        if (node == NULL)
 593                return;
 594
 595        *gpio_base = -1;
 596        val = of_get_property(node, "linux,gpio-base", &size);
 597        WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
 598        if (val) {
 599                if (size != sizeof(*val))
 600                        dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
 601                                 node->full_name);
 602                else
 603                        *gpio_base = be32_to_cpup(val);
 604        }
 605
 606        val = of_get_property(node, "polarity", NULL);
 607        WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
 608        if (val)
 609                *invert = *val;
 610}
 611#else
 612static void
 613pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
 614{
 615        *gpio_base = -1;
 616}
 617#endif
 618
 619static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
 620{
 621        int ret;
 622
 623        ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
 624        if (ret)
 625                goto out;
 626
 627        ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
 628                               &chip->reg_direction);
 629        if (ret)
 630                goto out;
 631
 632        /* set platform specific polarity inversion */
 633        ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
 634out:
 635        return ret;
 636}
 637
 638static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
 639{
 640        int ret;
 641        u32 val = 0;
 642
 643        /* Let every port in proper state, that could save power */
 644        pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
 645        pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
 646        pca953x_write_reg(chip, PCA957X_OUT, 0x0);
 647
 648        ret = pca953x_read_reg(chip, PCA957X_IN, &val);
 649        if (ret)
 650                goto out;
 651        ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
 652        if (ret)
 653                goto out;
 654        ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
 655        if (ret)
 656                goto out;
 657
 658        /* set platform specific polarity inversion */
 659        pca953x_write_reg(chip, PCA957X_INVRT, invert);
 660
 661        /* To enable register 6, 7 to controll pull up and pull down */
 662        pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
 663
 664        return 0;
 665out:
 666        return ret;
 667}
 668
 669static int pca953x_probe(struct i2c_client *client,
 670                                   const struct i2c_device_id *id)
 671{
 672        struct pca953x_platform_data *pdata;
 673        struct pca953x_chip *chip;
 674        int irq_base = 0;
 675        int ret;
 676        u32 invert = 0;
 677
 678        chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
 679        if (chip == NULL)
 680                return -ENOMEM;
 681
 682        pdata = client->dev.platform_data;
 683        if (pdata) {
 684                irq_base = pdata->irq_base;
 685                chip->gpio_start = pdata->gpio_base;
 686                invert = pdata->invert;
 687                chip->names = pdata->names;
 688        } else {
 689                pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
 690#ifdef CONFIG_OF_GPIO
 691                /* If I2C node has no interrupts property, disable GPIO interrupts */
 692                if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
 693                        irq_base = -1;
 694#endif
 695        }
 696
 697        chip->client = client;
 698
 699        chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
 700
 701        mutex_init(&chip->i2c_lock);
 702
 703        /* initialize cached registers from their original values.
 704         * we can't share this chip with another i2c master.
 705         */
 706        pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
 707
 708        if (chip->chip_type == PCA953X_TYPE)
 709                ret = device_pca953x_init(chip, invert);
 710        else
 711                ret = device_pca957x_init(chip, invert);
 712        if (ret)
 713                goto out_failed;
 714
 715        ret = pca953x_irq_setup(chip, id, irq_base);
 716        if (ret)
 717                goto out_failed;
 718
 719        ret = gpiochip_add(&chip->gpio_chip);
 720        if (ret)
 721                goto out_failed_irq;
 722
 723        if (pdata && pdata->setup) {
 724                ret = pdata->setup(client, chip->gpio_chip.base,
 725                                chip->gpio_chip.ngpio, pdata->context);
 726                if (ret < 0)
 727                        dev_warn(&client->dev, "setup failed, %d\n", ret);
 728        }
 729
 730        i2c_set_clientdata(client, chip);
 731        return 0;
 732
 733out_failed_irq:
 734        pca953x_irq_teardown(chip);
 735out_failed:
 736        kfree(chip);
 737        return ret;
 738}
 739
 740static int pca953x_remove(struct i2c_client *client)
 741{
 742        struct pca953x_platform_data *pdata = client->dev.platform_data;
 743        struct pca953x_chip *chip = i2c_get_clientdata(client);
 744        int ret = 0;
 745
 746        if (pdata && pdata->teardown) {
 747                ret = pdata->teardown(client, chip->gpio_chip.base,
 748                                chip->gpio_chip.ngpio, pdata->context);
 749                if (ret < 0) {
 750                        dev_err(&client->dev, "%s failed, %d\n",
 751                                        "teardown", ret);
 752                        return ret;
 753                }
 754        }
 755
 756        ret = gpiochip_remove(&chip->gpio_chip);
 757        if (ret) {
 758                dev_err(&client->dev, "%s failed, %d\n",
 759                                "gpiochip_remove()", ret);
 760                return ret;
 761        }
 762
 763        pca953x_irq_teardown(chip);
 764        kfree(chip);
 765        return 0;
 766}
 767
 768static const struct of_device_id pca953x_dt_ids[] = {
 769        { .compatible = "nxp,pca9534", },
 770        { .compatible = "nxp,pca9535", },
 771        { .compatible = "nxp,pca9536", },
 772        { .compatible = "nxp,pca9537", },
 773        { .compatible = "nxp,pca9538", },
 774        { .compatible = "nxp,pca9539", },
 775        { .compatible = "nxp,pca9554", },
 776        { .compatible = "nxp,pca9555", },
 777        { .compatible = "nxp,pca9556", },
 778        { .compatible = "nxp,pca9557", },
 779        { .compatible = "nxp,pca9574", },
 780        { .compatible = "nxp,pca9575", },
 781
 782        { .compatible = "maxim,max7310", },
 783        { .compatible = "maxim,max7312", },
 784        { .compatible = "maxim,max7313", },
 785        { .compatible = "maxim,max7315", },
 786
 787        { .compatible = "ti,pca6107", },
 788        { .compatible = "ti,tca6408", },
 789        { .compatible = "ti,tca6416", },
 790        { .compatible = "ti,tca6424", },
 791        { }
 792};
 793
 794MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
 795
 796static struct i2c_driver pca953x_driver = {
 797        .driver = {
 798                .name   = "pca953x",
 799                .of_match_table = pca953x_dt_ids,
 800        },
 801        .probe          = pca953x_probe,
 802        .remove         = pca953x_remove,
 803        .id_table       = pca953x_id,
 804};
 805
 806static int __init pca953x_init(void)
 807{
 808        return i2c_add_driver(&pca953x_driver);
 809}
 810/* register after i2c postcore initcall and before
 811 * subsys initcalls that may rely on these GPIOs
 812 */
 813subsys_initcall(pca953x_init);
 814
 815static void __exit pca953x_exit(void)
 816{
 817        i2c_del_driver(&pca953x_driver);
 818}
 819module_exit(pca953x_exit);
 820
 821MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
 822MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
 823MODULE_LICENSE("GPL");
 824
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.