Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 15 | * 02110-1301, USA. |
| 16 | */ |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/irq.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/slab.h> |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 25 | #include <linux/i2c/sx150x.h> |
Wei Chen | 04d2264 | 2015-01-15 08:16:10 +0800 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_address.h> |
| 28 | #include <linux/of_irq.h> |
| 29 | #include <linux/of_gpio.h> |
| 30 | #include <linux/of_device.h> |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 31 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 32 | #define NO_UPDATE_PENDING -1 |
| 33 | |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 34 | /* The chip models of sx150x */ |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 35 | #define SX150X_123 0 |
| 36 | #define SX150X_456 1 |
| 37 | #define SX150X_789 2 |
| 38 | |
| 39 | struct sx150x_123_pri { |
| 40 | u8 reg_pld_mode; |
| 41 | u8 reg_pld_table0; |
| 42 | u8 reg_pld_table1; |
| 43 | u8 reg_pld_table2; |
| 44 | u8 reg_pld_table3; |
| 45 | u8 reg_pld_table4; |
| 46 | u8 reg_advance; |
| 47 | }; |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 48 | |
| 49 | struct sx150x_456_pri { |
| 50 | u8 reg_pld_mode; |
| 51 | u8 reg_pld_table0; |
| 52 | u8 reg_pld_table1; |
| 53 | u8 reg_pld_table2; |
| 54 | u8 reg_pld_table3; |
| 55 | u8 reg_pld_table4; |
| 56 | u8 reg_advance; |
| 57 | }; |
| 58 | |
| 59 | struct sx150x_789_pri { |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 60 | u8 reg_drain; |
| 61 | u8 reg_polarity; |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 62 | u8 reg_clock; |
| 63 | u8 reg_misc; |
| 64 | u8 reg_reset; |
| 65 | u8 ngpios; |
| 66 | }; |
| 67 | |
| 68 | struct sx150x_device_data { |
| 69 | u8 model; |
| 70 | u8 reg_pullup; |
| 71 | u8 reg_pulldn; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 72 | u8 reg_dir; |
| 73 | u8 reg_data; |
| 74 | u8 reg_irq_mask; |
| 75 | u8 reg_irq_src; |
| 76 | u8 reg_sense; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 77 | u8 ngpios; |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 78 | union { |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 79 | struct sx150x_123_pri x123; |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 80 | struct sx150x_456_pri x456; |
| 81 | struct sx150x_789_pri x789; |
| 82 | } pri; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | struct sx150x_chip { |
| 86 | struct gpio_chip gpio_chip; |
| 87 | struct i2c_client *client; |
| 88 | const struct sx150x_device_data *dev_cfg; |
| 89 | int irq_summary; |
| 90 | int irq_base; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 91 | int irq_update; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 92 | u32 irq_sense; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 93 | u32 irq_masked; |
| 94 | u32 dev_sense; |
| 95 | u32 dev_masked; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 96 | struct irq_chip irq_chip; |
| 97 | struct mutex lock; |
| 98 | }; |
| 99 | |
| 100 | static const struct sx150x_device_data sx150x_devices[] = { |
| 101 | [0] = { /* sx1508q */ |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 102 | .model = SX150X_789, |
| 103 | .reg_pullup = 0x03, |
| 104 | .reg_pulldn = 0x04, |
| 105 | .reg_dir = 0x07, |
| 106 | .reg_data = 0x08, |
| 107 | .reg_irq_mask = 0x09, |
| 108 | .reg_irq_src = 0x0c, |
| 109 | .reg_sense = 0x0b, |
| 110 | .pri.x789 = { |
| 111 | .reg_drain = 0x05, |
| 112 | .reg_polarity = 0x06, |
| 113 | .reg_clock = 0x0f, |
| 114 | .reg_misc = 0x10, |
| 115 | .reg_reset = 0x7d, |
| 116 | }, |
| 117 | .ngpios = 8, |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 118 | }, |
| 119 | [1] = { /* sx1509q */ |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 120 | .model = SX150X_789, |
| 121 | .reg_pullup = 0x07, |
| 122 | .reg_pulldn = 0x09, |
| 123 | .reg_dir = 0x0f, |
| 124 | .reg_data = 0x11, |
| 125 | .reg_irq_mask = 0x13, |
| 126 | .reg_irq_src = 0x19, |
| 127 | .reg_sense = 0x17, |
| 128 | .pri.x789 = { |
| 129 | .reg_drain = 0x0b, |
| 130 | .reg_polarity = 0x0d, |
| 131 | .reg_clock = 0x1e, |
| 132 | .reg_misc = 0x1f, |
| 133 | .reg_reset = 0x7d, |
| 134 | }, |
| 135 | .ngpios = 16 |
| 136 | }, |
| 137 | [2] = { /* sx1506q */ |
| 138 | .model = SX150X_456, |
| 139 | .reg_pullup = 0x05, |
| 140 | .reg_pulldn = 0x07, |
| 141 | .reg_dir = 0x03, |
| 142 | .reg_data = 0x01, |
| 143 | .reg_irq_mask = 0x09, |
| 144 | .reg_irq_src = 0x0f, |
| 145 | .reg_sense = 0x0d, |
| 146 | .pri.x456 = { |
| 147 | .reg_pld_mode = 0x21, |
| 148 | .reg_pld_table0 = 0x23, |
| 149 | .reg_pld_table1 = 0x25, |
| 150 | .reg_pld_table2 = 0x27, |
| 151 | .reg_pld_table3 = 0x29, |
| 152 | .reg_pld_table4 = 0x2b, |
| 153 | .reg_advance = 0xad, |
| 154 | }, |
| 155 | .ngpios = 16 |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 156 | }, |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 157 | [3] = { /* sx1502q */ |
| 158 | .model = SX150X_123, |
| 159 | .reg_pullup = 0x02, |
| 160 | .reg_pulldn = 0x03, |
| 161 | .reg_dir = 0x01, |
| 162 | .reg_data = 0x00, |
| 163 | .reg_irq_mask = 0x05, |
| 164 | .reg_irq_src = 0x08, |
| 165 | .reg_sense = 0x07, |
| 166 | .pri.x123 = { |
| 167 | .reg_pld_mode = 0x10, |
| 168 | .reg_pld_table0 = 0x11, |
| 169 | .reg_pld_table1 = 0x12, |
| 170 | .reg_pld_table2 = 0x13, |
| 171 | .reg_pld_table3 = 0x14, |
| 172 | .reg_pld_table4 = 0x15, |
| 173 | .reg_advance = 0xad, |
| 174 | }, |
| 175 | .ngpios = 8, |
| 176 | }, |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static const struct i2c_device_id sx150x_id[] = { |
| 180 | {"sx1508q", 0}, |
| 181 | {"sx1509q", 1}, |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 182 | {"sx1506q", 2}, |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 183 | {"sx1502q", 3}, |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 184 | {} |
| 185 | }; |
| 186 | MODULE_DEVICE_TABLE(i2c, sx150x_id); |
| 187 | |
Linus Walleij | 16858cc | 2015-01-22 09:45:24 +0100 | [diff] [blame] | 188 | static const struct of_device_id sx150x_of_match[] = { |
Wei Chen | 04d2264 | 2015-01-15 08:16:10 +0800 | [diff] [blame] | 189 | { .compatible = "semtech,sx1508q" }, |
| 190 | { .compatible = "semtech,sx1509q" }, |
| 191 | { .compatible = "semtech,sx1506q" }, |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 192 | { .compatible = "semtech,sx1502q" }, |
Wei Chen | 04d2264 | 2015-01-15 08:16:10 +0800 | [diff] [blame] | 193 | {}, |
| 194 | }; |
Linus Walleij | 16858cc | 2015-01-22 09:45:24 +0100 | [diff] [blame] | 195 | MODULE_DEVICE_TABLE(of, sx150x_of_match); |
Wei Chen | 04d2264 | 2015-01-15 08:16:10 +0800 | [diff] [blame] | 196 | |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 197 | struct sx150x_chip *to_sx150x(struct gpio_chip *gc) |
| 198 | { |
| 199 | return container_of(gc, struct sx150x_chip, gpio_chip); |
| 200 | } |
| 201 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 202 | static s32 sx150x_i2c_write(struct i2c_client *client, u8 reg, u8 val) |
| 203 | { |
| 204 | s32 err = i2c_smbus_write_byte_data(client, reg, val); |
| 205 | |
| 206 | if (err < 0) |
| 207 | dev_warn(&client->dev, |
| 208 | "i2c write fail: can't write %02x to %02x: %d\n", |
| 209 | val, reg, err); |
| 210 | return err; |
| 211 | } |
| 212 | |
| 213 | static s32 sx150x_i2c_read(struct i2c_client *client, u8 reg, u8 *val) |
| 214 | { |
| 215 | s32 err = i2c_smbus_read_byte_data(client, reg); |
| 216 | |
| 217 | if (err >= 0) |
| 218 | *val = err; |
| 219 | else |
| 220 | dev_warn(&client->dev, |
| 221 | "i2c read fail: can't read from %02x: %d\n", |
| 222 | reg, err); |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | static inline bool offset_is_oscio(struct sx150x_chip *chip, unsigned offset) |
| 227 | { |
| 228 | return (chip->dev_cfg->ngpios == offset); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * These utility functions solve the common problem of locating and setting |
| 233 | * configuration bits. Configuration bits are grouped into registers |
| 234 | * whose indexes increase downwards. For example, with eight-bit registers, |
| 235 | * sixteen gpios would have their config bits grouped in the following order: |
| 236 | * REGISTER N-1 [ f e d c b a 9 8 ] |
| 237 | * N [ 7 6 5 4 3 2 1 0 ] |
| 238 | * |
| 239 | * For multi-bit configurations, the pattern gets wider: |
| 240 | * REGISTER N-3 [ f f e e d d c c ] |
| 241 | * N-2 [ b b a a 9 9 8 8 ] |
| 242 | * N-1 [ 7 7 6 6 5 5 4 4 ] |
| 243 | * N [ 3 3 2 2 1 1 0 0 ] |
| 244 | * |
| 245 | * Given the address of the starting register 'N', the index of the gpio |
| 246 | * whose configuration we seek to change, and the width in bits of that |
| 247 | * configuration, these functions allow us to locate the correct |
| 248 | * register and mask the correct bits. |
| 249 | */ |
| 250 | static inline void sx150x_find_cfg(u8 offset, u8 width, |
| 251 | u8 *reg, u8 *mask, u8 *shift) |
| 252 | { |
| 253 | *reg -= offset * width / 8; |
| 254 | *mask = (1 << width) - 1; |
| 255 | *shift = (offset * width) % 8; |
| 256 | *mask <<= *shift; |
| 257 | } |
| 258 | |
| 259 | static s32 sx150x_write_cfg(struct sx150x_chip *chip, |
| 260 | u8 offset, u8 width, u8 reg, u8 val) |
| 261 | { |
| 262 | u8 mask; |
| 263 | u8 data; |
| 264 | u8 shift; |
| 265 | s32 err; |
| 266 | |
| 267 | sx150x_find_cfg(offset, width, ®, &mask, &shift); |
| 268 | err = sx150x_i2c_read(chip->client, reg, &data); |
| 269 | if (err < 0) |
| 270 | return err; |
| 271 | |
| 272 | data &= ~mask; |
| 273 | data |= (val << shift) & mask; |
| 274 | return sx150x_i2c_write(chip->client, reg, data); |
| 275 | } |
| 276 | |
| 277 | static int sx150x_get_io(struct sx150x_chip *chip, unsigned offset) |
| 278 | { |
| 279 | u8 reg = chip->dev_cfg->reg_data; |
| 280 | u8 mask; |
| 281 | u8 data; |
| 282 | u8 shift; |
| 283 | s32 err; |
| 284 | |
| 285 | sx150x_find_cfg(offset, 1, ®, &mask, &shift); |
| 286 | err = sx150x_i2c_read(chip->client, reg, &data); |
| 287 | if (err >= 0) |
| 288 | err = (data & mask) != 0 ? 1 : 0; |
| 289 | |
| 290 | return err; |
| 291 | } |
| 292 | |
| 293 | static void sx150x_set_oscio(struct sx150x_chip *chip, int val) |
| 294 | { |
| 295 | sx150x_i2c_write(chip->client, |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 296 | chip->dev_cfg->pri.x789.reg_clock, |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 297 | (val ? 0x1f : 0x10)); |
| 298 | } |
| 299 | |
| 300 | static void sx150x_set_io(struct sx150x_chip *chip, unsigned offset, int val) |
| 301 | { |
| 302 | sx150x_write_cfg(chip, |
| 303 | offset, |
| 304 | 1, |
| 305 | chip->dev_cfg->reg_data, |
| 306 | (val ? 1 : 0)); |
| 307 | } |
| 308 | |
| 309 | static int sx150x_io_input(struct sx150x_chip *chip, unsigned offset) |
| 310 | { |
| 311 | return sx150x_write_cfg(chip, |
| 312 | offset, |
| 313 | 1, |
| 314 | chip->dev_cfg->reg_dir, |
| 315 | 1); |
| 316 | } |
| 317 | |
| 318 | static int sx150x_io_output(struct sx150x_chip *chip, unsigned offset, int val) |
| 319 | { |
| 320 | int err; |
| 321 | |
| 322 | err = sx150x_write_cfg(chip, |
| 323 | offset, |
| 324 | 1, |
| 325 | chip->dev_cfg->reg_data, |
| 326 | (val ? 1 : 0)); |
| 327 | if (err >= 0) |
| 328 | err = sx150x_write_cfg(chip, |
| 329 | offset, |
| 330 | 1, |
| 331 | chip->dev_cfg->reg_dir, |
| 332 | 0); |
| 333 | return err; |
| 334 | } |
| 335 | |
| 336 | static int sx150x_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 337 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 338 | struct sx150x_chip *chip = to_sx150x(gc); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 339 | int status = -EINVAL; |
| 340 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 341 | if (!offset_is_oscio(chip, offset)) { |
| 342 | mutex_lock(&chip->lock); |
| 343 | status = sx150x_get_io(chip, offset); |
| 344 | mutex_unlock(&chip->lock); |
| 345 | } |
| 346 | |
Linus Walleij | f2e13d2 | 2015-12-21 11:41:06 +0100 | [diff] [blame^] | 347 | return (status < 0) ? status : !!status; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static void sx150x_gpio_set(struct gpio_chip *gc, unsigned offset, int val) |
| 351 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 352 | struct sx150x_chip *chip = to_sx150x(gc); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 353 | |
| 354 | mutex_lock(&chip->lock); |
| 355 | if (offset_is_oscio(chip, offset)) |
| 356 | sx150x_set_oscio(chip, val); |
| 357 | else |
| 358 | sx150x_set_io(chip, offset, val); |
| 359 | mutex_unlock(&chip->lock); |
| 360 | } |
| 361 | |
| 362 | static int sx150x_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 363 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 364 | struct sx150x_chip *chip = to_sx150x(gc); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 365 | int status = -EINVAL; |
| 366 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 367 | if (!offset_is_oscio(chip, offset)) { |
| 368 | mutex_lock(&chip->lock); |
| 369 | status = sx150x_io_input(chip, offset); |
| 370 | mutex_unlock(&chip->lock); |
| 371 | } |
| 372 | return status; |
| 373 | } |
| 374 | |
| 375 | static int sx150x_gpio_direction_output(struct gpio_chip *gc, |
| 376 | unsigned offset, |
| 377 | int val) |
| 378 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 379 | struct sx150x_chip *chip = to_sx150x(gc); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 380 | int status = 0; |
| 381 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 382 | if (!offset_is_oscio(chip, offset)) { |
| 383 | mutex_lock(&chip->lock); |
| 384 | status = sx150x_io_output(chip, offset, val); |
| 385 | mutex_unlock(&chip->lock); |
| 386 | } |
| 387 | return status; |
| 388 | } |
| 389 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 390 | static void sx150x_irq_mask(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 391 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 392 | struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d)); |
Wei Chen | 093e943 | 2014-12-04 20:12:08 +0800 | [diff] [blame] | 393 | unsigned n = d->hwirq; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 394 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 395 | chip->irq_masked |= (1 << n); |
| 396 | chip->irq_update = n; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 399 | static void sx150x_irq_unmask(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 400 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 401 | struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d)); |
Wei Chen | 093e943 | 2014-12-04 20:12:08 +0800 | [diff] [blame] | 402 | unsigned n = d->hwirq; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 403 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 404 | chip->irq_masked &= ~(1 << n); |
| 405 | chip->irq_update = n; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 408 | static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 409 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 410 | struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d)); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 411 | unsigned n, val = 0; |
| 412 | |
| 413 | if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) |
| 414 | return -EINVAL; |
| 415 | |
Wei Chen | 093e943 | 2014-12-04 20:12:08 +0800 | [diff] [blame] | 416 | n = d->hwirq; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 417 | |
| 418 | if (flow_type & IRQ_TYPE_EDGE_RISING) |
| 419 | val |= 0x1; |
| 420 | if (flow_type & IRQ_TYPE_EDGE_FALLING) |
| 421 | val |= 0x2; |
| 422 | |
| 423 | chip->irq_sense &= ~(3UL << (n * 2)); |
| 424 | chip->irq_sense |= val << (n * 2); |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 425 | chip->irq_update = n; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id) |
| 430 | { |
| 431 | struct sx150x_chip *chip = (struct sx150x_chip *)dev_id; |
| 432 | unsigned nhandled = 0; |
| 433 | unsigned sub_irq; |
| 434 | unsigned n; |
| 435 | s32 err; |
| 436 | u8 val; |
| 437 | int i; |
| 438 | |
| 439 | for (i = (chip->dev_cfg->ngpios / 8) - 1; i >= 0; --i) { |
| 440 | err = sx150x_i2c_read(chip->client, |
| 441 | chip->dev_cfg->reg_irq_src - i, |
| 442 | &val); |
| 443 | if (err < 0) |
| 444 | continue; |
| 445 | |
| 446 | sx150x_i2c_write(chip->client, |
| 447 | chip->dev_cfg->reg_irq_src - i, |
| 448 | val); |
| 449 | for (n = 0; n < 8; ++n) { |
| 450 | if (val & (1 << n)) { |
Wei Chen | 093e943 | 2014-12-04 20:12:08 +0800 | [diff] [blame] | 451 | sub_irq = irq_find_mapping( |
| 452 | chip->gpio_chip.irqdomain, |
| 453 | (i * 8) + n); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 454 | handle_nested_irq(sub_irq); |
| 455 | ++nhandled; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE); |
| 461 | } |
| 462 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 463 | static void sx150x_irq_bus_lock(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 464 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 465 | struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d)); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 466 | |
| 467 | mutex_lock(&chip->lock); |
| 468 | } |
| 469 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 470 | static void sx150x_irq_bus_sync_unlock(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 471 | { |
Linus Walleij | 218f1f8 | 2015-08-25 11:26:04 +0200 | [diff] [blame] | 472 | struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d)); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 473 | unsigned n; |
| 474 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 475 | if (chip->irq_update == NO_UPDATE_PENDING) |
| 476 | goto out; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 477 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 478 | n = chip->irq_update; |
| 479 | chip->irq_update = NO_UPDATE_PENDING; |
| 480 | |
| 481 | /* Avoid updates if nothing changed */ |
| 482 | if (chip->dev_sense == chip->irq_sense && |
Axel Lin | aab0b12 | 2014-12-16 14:22:27 +0800 | [diff] [blame] | 483 | chip->dev_masked == chip->irq_masked) |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 484 | goto out; |
| 485 | |
| 486 | chip->dev_sense = chip->irq_sense; |
| 487 | chip->dev_masked = chip->irq_masked; |
| 488 | |
| 489 | if (chip->irq_masked & (1 << n)) { |
| 490 | sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 1); |
| 491 | sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, 0); |
| 492 | } else { |
| 493 | sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 0); |
| 494 | sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, |
| 495 | chip->irq_sense >> (n * 2)); |
| 496 | } |
| 497 | out: |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 498 | mutex_unlock(&chip->lock); |
| 499 | } |
| 500 | |
| 501 | static void sx150x_init_chip(struct sx150x_chip *chip, |
| 502 | struct i2c_client *client, |
| 503 | kernel_ulong_t driver_data, |
| 504 | struct sx150x_platform_data *pdata) |
| 505 | { |
| 506 | mutex_init(&chip->lock); |
| 507 | |
| 508 | chip->client = client; |
| 509 | chip->dev_cfg = &sx150x_devices[driver_data]; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 510 | chip->gpio_chip.parent = &client->dev; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 511 | chip->gpio_chip.label = client->name; |
| 512 | chip->gpio_chip.direction_input = sx150x_gpio_direction_input; |
| 513 | chip->gpio_chip.direction_output = sx150x_gpio_direction_output; |
| 514 | chip->gpio_chip.get = sx150x_gpio_get; |
| 515 | chip->gpio_chip.set = sx150x_gpio_set; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 516 | chip->gpio_chip.base = pdata->gpio_base; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 517 | chip->gpio_chip.can_sleep = true; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 518 | chip->gpio_chip.ngpio = chip->dev_cfg->ngpios; |
Linus Walleij | 16858cc | 2015-01-22 09:45:24 +0100 | [diff] [blame] | 519 | #ifdef CONFIG_OF_GPIO |
Wei Chen | 04d2264 | 2015-01-15 08:16:10 +0800 | [diff] [blame] | 520 | chip->gpio_chip.of_node = client->dev.of_node; |
| 521 | chip->gpio_chip.of_gpio_n_cells = 2; |
Linus Walleij | 16858cc | 2015-01-22 09:45:24 +0100 | [diff] [blame] | 522 | #endif |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 523 | if (pdata->oscio_is_gpo) |
| 524 | ++chip->gpio_chip.ngpio; |
| 525 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 526 | chip->irq_chip.name = client->name; |
| 527 | chip->irq_chip.irq_mask = sx150x_irq_mask; |
| 528 | chip->irq_chip.irq_unmask = sx150x_irq_unmask; |
| 529 | chip->irq_chip.irq_set_type = sx150x_irq_set_type; |
| 530 | chip->irq_chip.irq_bus_lock = sx150x_irq_bus_lock; |
| 531 | chip->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock; |
| 532 | chip->irq_summary = -1; |
| 533 | chip->irq_base = -1; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 534 | chip->irq_masked = ~0; |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 535 | chip->irq_sense = 0; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 536 | chip->dev_masked = ~0; |
| 537 | chip->dev_sense = 0; |
| 538 | chip->irq_update = NO_UPDATE_PENDING; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static int sx150x_init_io(struct sx150x_chip *chip, u8 base, u16 cfg) |
| 542 | { |
| 543 | int err = 0; |
| 544 | unsigned n; |
| 545 | |
| 546 | for (n = 0; err >= 0 && n < (chip->dev_cfg->ngpios / 8); ++n) |
| 547 | err = sx150x_i2c_write(chip->client, base - n, cfg >> (n * 8)); |
| 548 | return err; |
| 549 | } |
| 550 | |
Gregory Bean | 5affb60 | 2010-09-09 16:38:02 -0700 | [diff] [blame] | 551 | static int sx150x_reset(struct sx150x_chip *chip) |
| 552 | { |
| 553 | int err; |
| 554 | |
| 555 | err = i2c_smbus_write_byte_data(chip->client, |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 556 | chip->dev_cfg->pri.x789.reg_reset, |
Gregory Bean | 5affb60 | 2010-09-09 16:38:02 -0700 | [diff] [blame] | 557 | 0x12); |
| 558 | if (err < 0) |
| 559 | return err; |
| 560 | |
| 561 | err = i2c_smbus_write_byte_data(chip->client, |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 562 | chip->dev_cfg->pri.x789.reg_reset, |
Gregory Bean | 5affb60 | 2010-09-09 16:38:02 -0700 | [diff] [blame] | 563 | 0x34); |
| 564 | return err; |
| 565 | } |
| 566 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 567 | static int sx150x_init_hw(struct sx150x_chip *chip, |
| 568 | struct sx150x_platform_data *pdata) |
| 569 | { |
| 570 | int err = 0; |
| 571 | |
Gregory Bean | 5affb60 | 2010-09-09 16:38:02 -0700 | [diff] [blame] | 572 | if (pdata->reset_during_probe) { |
| 573 | err = sx150x_reset(chip); |
| 574 | if (err < 0) |
| 575 | return err; |
| 576 | } |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 577 | |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 578 | if (chip->dev_cfg->model == SX150X_789) |
| 579 | err = sx150x_i2c_write(chip->client, |
| 580 | chip->dev_cfg->pri.x789.reg_misc, |
| 581 | 0x01); |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 582 | else if (chip->dev_cfg->model == SX150X_456) |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 583 | err = sx150x_i2c_write(chip->client, |
| 584 | chip->dev_cfg->pri.x456.reg_advance, |
| 585 | 0x04); |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 586 | else |
| 587 | err = sx150x_i2c_write(chip->client, |
| 588 | chip->dev_cfg->pri.x123.reg_advance, |
| 589 | 0x00); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 590 | if (err < 0) |
| 591 | return err; |
| 592 | |
| 593 | err = sx150x_init_io(chip, chip->dev_cfg->reg_pullup, |
| 594 | pdata->io_pullup_ena); |
| 595 | if (err < 0) |
| 596 | return err; |
| 597 | |
| 598 | err = sx150x_init_io(chip, chip->dev_cfg->reg_pulldn, |
| 599 | pdata->io_pulldn_ena); |
| 600 | if (err < 0) |
| 601 | return err; |
| 602 | |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 603 | if (chip->dev_cfg->model == SX150X_789) { |
| 604 | err = sx150x_init_io(chip, |
| 605 | chip->dev_cfg->pri.x789.reg_drain, |
| 606 | pdata->io_open_drain_ena); |
| 607 | if (err < 0) |
| 608 | return err; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 609 | |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 610 | err = sx150x_init_io(chip, |
| 611 | chip->dev_cfg->pri.x789.reg_polarity, |
| 612 | pdata->io_polarity); |
| 613 | if (err < 0) |
| 614 | return err; |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 615 | } else if (chip->dev_cfg->model == SX150X_456) { |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 616 | /* Set all pins to work in normal mode */ |
| 617 | err = sx150x_init_io(chip, |
| 618 | chip->dev_cfg->pri.x456.reg_pld_mode, |
| 619 | 0); |
| 620 | if (err < 0) |
| 621 | return err; |
Peter Rosin | 50e8df0 | 2015-12-15 23:01:34 +0100 | [diff] [blame] | 622 | } else { |
| 623 | /* Set all pins to work in normal mode */ |
| 624 | err = sx150x_init_io(chip, |
| 625 | chip->dev_cfg->pri.x123.reg_pld_mode, |
| 626 | 0); |
| 627 | if (err < 0) |
| 628 | return err; |
Wei Chen | ae9ca49 | 2014-12-04 20:12:09 +0800 | [diff] [blame] | 629 | } |
| 630 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 631 | |
| 632 | if (pdata->oscio_is_gpo) |
| 633 | sx150x_set_oscio(chip, 0); |
| 634 | |
| 635 | return err; |
| 636 | } |
| 637 | |
| 638 | static int sx150x_install_irq_chip(struct sx150x_chip *chip, |
| 639 | int irq_summary, |
| 640 | int irq_base) |
| 641 | { |
| 642 | int err; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 643 | |
| 644 | chip->irq_summary = irq_summary; |
| 645 | chip->irq_base = irq_base; |
| 646 | |
Wei Chen | 093e943 | 2014-12-04 20:12:08 +0800 | [diff] [blame] | 647 | /* Add gpio chip to irq subsystem */ |
| 648 | err = gpiochip_irqchip_add(&chip->gpio_chip, |
| 649 | &chip->irq_chip, chip->irq_base, |
| 650 | handle_edge_irq, IRQ_TYPE_EDGE_BOTH); |
| 651 | if (err) { |
| 652 | dev_err(&chip->client->dev, |
| 653 | "could not connect irqchip to gpiochip\n"); |
| 654 | return err; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Nikolay Balandin | 644c8df | 2013-05-28 15:02:50 -0700 | [diff] [blame] | 657 | err = devm_request_threaded_irq(&chip->client->dev, |
Wei Chen | 093e943 | 2014-12-04 20:12:08 +0800 | [diff] [blame] | 658 | irq_summary, NULL, sx150x_irq_thread_fn, |
| 659 | IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_FALLING, |
| 660 | chip->irq_chip.name, chip); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 661 | if (err < 0) { |
| 662 | chip->irq_summary = -1; |
| 663 | chip->irq_base = -1; |
| 664 | } |
| 665 | |
| 666 | return err; |
| 667 | } |
| 668 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 669 | static int sx150x_probe(struct i2c_client *client, |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 670 | const struct i2c_device_id *id) |
| 671 | { |
| 672 | static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA | |
| 673 | I2C_FUNC_SMBUS_WRITE_WORD_DATA; |
| 674 | struct sx150x_platform_data *pdata; |
| 675 | struct sx150x_chip *chip; |
| 676 | int rc; |
| 677 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 678 | pdata = dev_get_platdata(&client->dev); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 679 | if (!pdata) |
| 680 | return -EINVAL; |
| 681 | |
| 682 | if (!i2c_check_functionality(client->adapter, i2c_funcs)) |
| 683 | return -ENOSYS; |
| 684 | |
Nikolay Balandin | 644c8df | 2013-05-28 15:02:50 -0700 | [diff] [blame] | 685 | chip = devm_kzalloc(&client->dev, |
| 686 | sizeof(struct sx150x_chip), GFP_KERNEL); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 687 | if (!chip) |
| 688 | return -ENOMEM; |
| 689 | |
| 690 | sx150x_init_chip(chip, client, id->driver_data, pdata); |
| 691 | rc = sx150x_init_hw(chip, pdata); |
| 692 | if (rc < 0) |
Nikolay Balandin | 644c8df | 2013-05-28 15:02:50 -0700 | [diff] [blame] | 693 | return rc; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 694 | |
| 695 | rc = gpiochip_add(&chip->gpio_chip); |
Nikolay Balandin | 644c8df | 2013-05-28 15:02:50 -0700 | [diff] [blame] | 696 | if (rc) |
| 697 | return rc; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 698 | |
| 699 | if (pdata->irq_summary >= 0) { |
| 700 | rc = sx150x_install_irq_chip(chip, |
| 701 | pdata->irq_summary, |
| 702 | pdata->irq_base); |
| 703 | if (rc < 0) |
| 704 | goto probe_fail_post_gpiochip_add; |
| 705 | } |
| 706 | |
| 707 | i2c_set_clientdata(client, chip); |
| 708 | |
| 709 | return 0; |
| 710 | probe_fail_post_gpiochip_add: |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 711 | gpiochip_remove(&chip->gpio_chip); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 712 | return rc; |
| 713 | } |
| 714 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 715 | static int sx150x_remove(struct i2c_client *client) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 716 | { |
| 717 | struct sx150x_chip *chip; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 718 | |
| 719 | chip = i2c_get_clientdata(client); |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 720 | gpiochip_remove(&chip->gpio_chip); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 721 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | static struct i2c_driver sx150x_driver = { |
| 726 | .driver = { |
| 727 | .name = "sx150x", |
Linus Walleij | 16858cc | 2015-01-22 09:45:24 +0100 | [diff] [blame] | 728 | .of_match_table = of_match_ptr(sx150x_of_match), |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 729 | }, |
| 730 | .probe = sx150x_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 731 | .remove = sx150x_remove, |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 732 | .id_table = sx150x_id, |
| 733 | }; |
| 734 | |
| 735 | static int __init sx150x_init(void) |
| 736 | { |
| 737 | return i2c_add_driver(&sx150x_driver); |
| 738 | } |
| 739 | subsys_initcall(sx150x_init); |
| 740 | |
| 741 | static void __exit sx150x_exit(void) |
| 742 | { |
| 743 | return i2c_del_driver(&sx150x_driver); |
| 744 | } |
| 745 | module_exit(sx150x_exit); |
| 746 | |
| 747 | MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>"); |
| 748 | MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders"); |
| 749 | MODULE_LICENSE("GPL v2"); |