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