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. |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/gpio.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/irq.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/workqueue.h> |
| 21 | #include <linux/i2c/sx150x.h> |
| 22 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 23 | #define NO_UPDATE_PENDING -1 |
| 24 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 25 | struct sx150x_device_data { |
| 26 | u8 reg_pullup; |
| 27 | u8 reg_pulldn; |
| 28 | u8 reg_drain; |
| 29 | u8 reg_polarity; |
| 30 | u8 reg_dir; |
| 31 | u8 reg_data; |
| 32 | u8 reg_irq_mask; |
| 33 | u8 reg_irq_src; |
| 34 | u8 reg_sense; |
| 35 | u8 reg_clock; |
| 36 | u8 reg_misc; |
| 37 | u8 reg_reset; |
| 38 | u8 ngpios; |
| 39 | }; |
| 40 | |
| 41 | struct sx150x_chip { |
| 42 | struct gpio_chip gpio_chip; |
| 43 | struct i2c_client *client; |
| 44 | const struct sx150x_device_data *dev_cfg; |
| 45 | int irq_summary; |
| 46 | int irq_base; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 47 | int irq_update; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 48 | u32 irq_sense; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 49 | u32 irq_masked; |
| 50 | u32 dev_sense; |
| 51 | u32 dev_masked; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 52 | struct irq_chip irq_chip; |
| 53 | struct mutex lock; |
| 54 | }; |
| 55 | |
| 56 | static const struct sx150x_device_data sx150x_devices[] = { |
| 57 | [0] = { /* sx1508q */ |
| 58 | .reg_pullup = 0x03, |
| 59 | .reg_pulldn = 0x04, |
| 60 | .reg_drain = 0x05, |
| 61 | .reg_polarity = 0x06, |
| 62 | .reg_dir = 0x07, |
| 63 | .reg_data = 0x08, |
| 64 | .reg_irq_mask = 0x09, |
| 65 | .reg_irq_src = 0x0c, |
| 66 | .reg_sense = 0x0b, |
| 67 | .reg_clock = 0x0f, |
| 68 | .reg_misc = 0x10, |
| 69 | .reg_reset = 0x7d, |
| 70 | .ngpios = 8 |
| 71 | }, |
| 72 | [1] = { /* sx1509q */ |
| 73 | .reg_pullup = 0x07, |
| 74 | .reg_pulldn = 0x09, |
| 75 | .reg_drain = 0x0b, |
| 76 | .reg_polarity = 0x0d, |
| 77 | .reg_dir = 0x0f, |
| 78 | .reg_data = 0x11, |
| 79 | .reg_irq_mask = 0x13, |
| 80 | .reg_irq_src = 0x19, |
| 81 | .reg_sense = 0x17, |
| 82 | .reg_clock = 0x1e, |
| 83 | .reg_misc = 0x1f, |
| 84 | .reg_reset = 0x7d, |
| 85 | .ngpios = 16 |
| 86 | }, |
| 87 | }; |
| 88 | |
| 89 | static const struct i2c_device_id sx150x_id[] = { |
| 90 | {"sx1508q", 0}, |
| 91 | {"sx1509q", 1}, |
| 92 | {} |
| 93 | }; |
| 94 | MODULE_DEVICE_TABLE(i2c, sx150x_id); |
| 95 | |
| 96 | static s32 sx150x_i2c_write(struct i2c_client *client, u8 reg, u8 val) |
| 97 | { |
| 98 | s32 err = i2c_smbus_write_byte_data(client, reg, val); |
| 99 | |
| 100 | if (err < 0) |
| 101 | dev_warn(&client->dev, |
| 102 | "i2c write fail: can't write %02x to %02x: %d\n", |
| 103 | val, reg, err); |
| 104 | return err; |
| 105 | } |
| 106 | |
| 107 | static s32 sx150x_i2c_read(struct i2c_client *client, u8 reg, u8 *val) |
| 108 | { |
| 109 | s32 err = i2c_smbus_read_byte_data(client, reg); |
| 110 | |
| 111 | if (err >= 0) |
| 112 | *val = err; |
| 113 | else |
| 114 | dev_warn(&client->dev, |
| 115 | "i2c read fail: can't read from %02x: %d\n", |
| 116 | reg, err); |
| 117 | return err; |
| 118 | } |
| 119 | |
| 120 | static inline bool offset_is_oscio(struct sx150x_chip *chip, unsigned offset) |
| 121 | { |
| 122 | return (chip->dev_cfg->ngpios == offset); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * These utility functions solve the common problem of locating and setting |
| 127 | * configuration bits. Configuration bits are grouped into registers |
| 128 | * whose indexes increase downwards. For example, with eight-bit registers, |
| 129 | * sixteen gpios would have their config bits grouped in the following order: |
| 130 | * REGISTER N-1 [ f e d c b a 9 8 ] |
| 131 | * N [ 7 6 5 4 3 2 1 0 ] |
| 132 | * |
| 133 | * For multi-bit configurations, the pattern gets wider: |
| 134 | * REGISTER N-3 [ f f e e d d c c ] |
| 135 | * N-2 [ b b a a 9 9 8 8 ] |
| 136 | * N-1 [ 7 7 6 6 5 5 4 4 ] |
| 137 | * N [ 3 3 2 2 1 1 0 0 ] |
| 138 | * |
| 139 | * Given the address of the starting register 'N', the index of the gpio |
| 140 | * whose configuration we seek to change, and the width in bits of that |
| 141 | * configuration, these functions allow us to locate the correct |
| 142 | * register and mask the correct bits. |
| 143 | */ |
| 144 | static inline void sx150x_find_cfg(u8 offset, u8 width, |
| 145 | u8 *reg, u8 *mask, u8 *shift) |
| 146 | { |
| 147 | *reg -= offset * width / 8; |
| 148 | *mask = (1 << width) - 1; |
| 149 | *shift = (offset * width) % 8; |
| 150 | *mask <<= *shift; |
| 151 | } |
| 152 | |
| 153 | static s32 sx150x_write_cfg(struct sx150x_chip *chip, |
| 154 | u8 offset, u8 width, u8 reg, u8 val) |
| 155 | { |
| 156 | u8 mask; |
| 157 | u8 data; |
| 158 | u8 shift; |
| 159 | s32 err; |
| 160 | |
| 161 | sx150x_find_cfg(offset, width, ®, &mask, &shift); |
| 162 | err = sx150x_i2c_read(chip->client, reg, &data); |
| 163 | if (err < 0) |
| 164 | return err; |
| 165 | |
| 166 | data &= ~mask; |
| 167 | data |= (val << shift) & mask; |
| 168 | return sx150x_i2c_write(chip->client, reg, data); |
| 169 | } |
| 170 | |
| 171 | static int sx150x_get_io(struct sx150x_chip *chip, unsigned offset) |
| 172 | { |
| 173 | u8 reg = chip->dev_cfg->reg_data; |
| 174 | u8 mask; |
| 175 | u8 data; |
| 176 | u8 shift; |
| 177 | s32 err; |
| 178 | |
| 179 | sx150x_find_cfg(offset, 1, ®, &mask, &shift); |
| 180 | err = sx150x_i2c_read(chip->client, reg, &data); |
| 181 | if (err >= 0) |
| 182 | err = (data & mask) != 0 ? 1 : 0; |
| 183 | |
| 184 | return err; |
| 185 | } |
| 186 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 187 | static s32 sx150x_set_oscio(struct sx150x_chip *chip, int val) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 188 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | return sx150x_i2c_write(chip->client, |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 190 | chip->dev_cfg->reg_clock, |
| 191 | (val ? 0x1f : 0x10)); |
| 192 | } |
| 193 | |
| 194 | static void sx150x_set_io(struct sx150x_chip *chip, unsigned offset, int val) |
| 195 | { |
| 196 | sx150x_write_cfg(chip, |
| 197 | offset, |
| 198 | 1, |
| 199 | chip->dev_cfg->reg_data, |
| 200 | (val ? 1 : 0)); |
| 201 | } |
| 202 | |
| 203 | static int sx150x_io_input(struct sx150x_chip *chip, unsigned offset) |
| 204 | { |
| 205 | return sx150x_write_cfg(chip, |
| 206 | offset, |
| 207 | 1, |
| 208 | chip->dev_cfg->reg_dir, |
| 209 | 1); |
| 210 | } |
| 211 | |
| 212 | static int sx150x_io_output(struct sx150x_chip *chip, unsigned offset, int val) |
| 213 | { |
| 214 | int err; |
| 215 | |
| 216 | err = sx150x_write_cfg(chip, |
| 217 | offset, |
| 218 | 1, |
| 219 | chip->dev_cfg->reg_data, |
| 220 | (val ? 1 : 0)); |
| 221 | if (err >= 0) |
| 222 | err = sx150x_write_cfg(chip, |
| 223 | offset, |
| 224 | 1, |
| 225 | chip->dev_cfg->reg_dir, |
| 226 | 0); |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | static int sx150x_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 231 | { |
| 232 | struct sx150x_chip *chip; |
| 233 | int status = -EINVAL; |
| 234 | |
| 235 | chip = container_of(gc, struct sx150x_chip, gpio_chip); |
| 236 | |
| 237 | if (!offset_is_oscio(chip, offset)) { |
| 238 | mutex_lock(&chip->lock); |
| 239 | status = sx150x_get_io(chip, offset); |
| 240 | mutex_unlock(&chip->lock); |
| 241 | } |
| 242 | |
| 243 | return status; |
| 244 | } |
| 245 | |
| 246 | static void sx150x_gpio_set(struct gpio_chip *gc, unsigned offset, int val) |
| 247 | { |
| 248 | struct sx150x_chip *chip; |
| 249 | |
| 250 | chip = container_of(gc, struct sx150x_chip, gpio_chip); |
| 251 | |
| 252 | mutex_lock(&chip->lock); |
| 253 | if (offset_is_oscio(chip, offset)) |
| 254 | sx150x_set_oscio(chip, val); |
| 255 | else |
| 256 | sx150x_set_io(chip, offset, val); |
| 257 | mutex_unlock(&chip->lock); |
| 258 | } |
| 259 | |
| 260 | static int sx150x_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 261 | { |
| 262 | struct sx150x_chip *chip; |
| 263 | int status = -EINVAL; |
| 264 | |
| 265 | chip = container_of(gc, struct sx150x_chip, gpio_chip); |
| 266 | |
| 267 | if (!offset_is_oscio(chip, offset)) { |
| 268 | mutex_lock(&chip->lock); |
| 269 | status = sx150x_io_input(chip, offset); |
| 270 | mutex_unlock(&chip->lock); |
| 271 | } |
| 272 | return status; |
| 273 | } |
| 274 | |
| 275 | static int sx150x_gpio_direction_output(struct gpio_chip *gc, |
| 276 | unsigned offset, |
| 277 | int val) |
| 278 | { |
| 279 | struct sx150x_chip *chip; |
| 280 | int status = 0; |
| 281 | |
| 282 | chip = container_of(gc, struct sx150x_chip, gpio_chip); |
| 283 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 284 | mutex_lock(&chip->lock); |
| 285 | if (offset_is_oscio(chip, offset)) |
| 286 | status = sx150x_set_oscio(chip, val); |
| 287 | else |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 288 | status = sx150x_io_output(chip, offset, val); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 289 | mutex_unlock(&chip->lock); |
| 290 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 291 | return status; |
| 292 | } |
| 293 | |
| 294 | static int sx150x_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 295 | { |
| 296 | struct sx150x_chip *chip; |
| 297 | |
| 298 | chip = container_of(gc, struct sx150x_chip, gpio_chip); |
| 299 | |
| 300 | if (offset >= chip->dev_cfg->ngpios) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | if (chip->irq_base < 0) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | return chip->irq_base + offset; |
| 307 | } |
| 308 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 309 | static void sx150x_irq_mask(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 310 | { |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 311 | struct irq_chip *ic = irq_data_get_irq_chip(d); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 312 | struct sx150x_chip *chip; |
| 313 | unsigned n; |
| 314 | |
| 315 | chip = container_of(ic, struct sx150x_chip, irq_chip); |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 316 | n = d->irq - chip->irq_base; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 317 | chip->irq_masked |= (1 << n); |
| 318 | chip->irq_update = n; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 321 | static void sx150x_irq_unmask(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 322 | { |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 323 | struct irq_chip *ic = irq_data_get_irq_chip(d); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 324 | struct sx150x_chip *chip; |
| 325 | unsigned n; |
| 326 | |
| 327 | chip = container_of(ic, struct sx150x_chip, irq_chip); |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 328 | n = d->irq - chip->irq_base; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 329 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 330 | chip->irq_masked &= ~(1 << n); |
| 331 | chip->irq_update = n; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 334 | 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] | 335 | { |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 336 | struct irq_chip *ic = irq_data_get_irq_chip(d); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 337 | struct sx150x_chip *chip; |
| 338 | unsigned n, val = 0; |
| 339 | |
| 340 | if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | chip = container_of(ic, struct sx150x_chip, irq_chip); |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 344 | n = d->irq - chip->irq_base; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 345 | |
| 346 | if (flow_type & IRQ_TYPE_EDGE_RISING) |
| 347 | val |= 0x1; |
| 348 | if (flow_type & IRQ_TYPE_EDGE_FALLING) |
| 349 | val |= 0x2; |
| 350 | |
| 351 | chip->irq_sense &= ~(3UL << (n * 2)); |
| 352 | chip->irq_sense |= val << (n * 2); |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 353 | chip->irq_update = n; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id) |
| 358 | { |
| 359 | struct sx150x_chip *chip = (struct sx150x_chip *)dev_id; |
| 360 | unsigned nhandled = 0; |
| 361 | unsigned sub_irq; |
| 362 | unsigned n; |
| 363 | s32 err; |
| 364 | u8 val; |
| 365 | int i; |
| 366 | |
| 367 | for (i = (chip->dev_cfg->ngpios / 8) - 1; i >= 0; --i) { |
| 368 | err = sx150x_i2c_read(chip->client, |
| 369 | chip->dev_cfg->reg_irq_src - i, |
| 370 | &val); |
| 371 | if (err < 0) |
| 372 | continue; |
| 373 | |
| 374 | sx150x_i2c_write(chip->client, |
| 375 | chip->dev_cfg->reg_irq_src - i, |
| 376 | val); |
| 377 | for (n = 0; n < 8; ++n) { |
| 378 | if (val & (1 << n)) { |
| 379 | sub_irq = chip->irq_base + (i * 8) + n; |
| 380 | handle_nested_irq(sub_irq); |
| 381 | ++nhandled; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE); |
| 387 | } |
| 388 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 389 | static void sx150x_irq_bus_lock(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 390 | { |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 391 | struct irq_chip *ic = irq_data_get_irq_chip(d); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 392 | struct sx150x_chip *chip; |
| 393 | |
| 394 | chip = container_of(ic, struct sx150x_chip, irq_chip); |
| 395 | |
| 396 | mutex_lock(&chip->lock); |
| 397 | } |
| 398 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 399 | static void sx150x_irq_bus_sync_unlock(struct irq_data *d) |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 400 | { |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 401 | struct irq_chip *ic = irq_data_get_irq_chip(d); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 402 | struct sx150x_chip *chip; |
| 403 | unsigned n; |
| 404 | |
| 405 | chip = container_of(ic, struct sx150x_chip, irq_chip); |
| 406 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 407 | if (chip->irq_update == NO_UPDATE_PENDING) |
| 408 | goto out; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 409 | |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 410 | n = chip->irq_update; |
| 411 | chip->irq_update = NO_UPDATE_PENDING; |
| 412 | |
| 413 | /* Avoid updates if nothing changed */ |
| 414 | if (chip->dev_sense == chip->irq_sense && |
| 415 | chip->dev_sense == chip->irq_masked) |
| 416 | goto out; |
| 417 | |
| 418 | chip->dev_sense = chip->irq_sense; |
| 419 | chip->dev_masked = chip->irq_masked; |
| 420 | |
| 421 | if (chip->irq_masked & (1 << n)) { |
| 422 | sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 1); |
| 423 | sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, 0); |
| 424 | } else { |
| 425 | sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 0); |
| 426 | sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, |
| 427 | chip->irq_sense >> (n * 2)); |
| 428 | } |
| 429 | out: |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 430 | mutex_unlock(&chip->lock); |
| 431 | } |
| 432 | |
| 433 | static void sx150x_init_chip(struct sx150x_chip *chip, |
| 434 | struct i2c_client *client, |
| 435 | kernel_ulong_t driver_data, |
| 436 | struct sx150x_platform_data *pdata) |
| 437 | { |
| 438 | mutex_init(&chip->lock); |
| 439 | |
| 440 | chip->client = client; |
| 441 | chip->dev_cfg = &sx150x_devices[driver_data]; |
| 442 | chip->gpio_chip.label = client->name; |
| 443 | chip->gpio_chip.direction_input = sx150x_gpio_direction_input; |
| 444 | chip->gpio_chip.direction_output = sx150x_gpio_direction_output; |
| 445 | chip->gpio_chip.get = sx150x_gpio_get; |
| 446 | chip->gpio_chip.set = sx150x_gpio_set; |
| 447 | chip->gpio_chip.to_irq = sx150x_gpio_to_irq; |
| 448 | chip->gpio_chip.base = pdata->gpio_base; |
| 449 | chip->gpio_chip.can_sleep = 1; |
| 450 | chip->gpio_chip.ngpio = chip->dev_cfg->ngpios; |
| 451 | if (pdata->oscio_is_gpo) |
| 452 | ++chip->gpio_chip.ngpio; |
| 453 | |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 454 | chip->irq_chip.name = client->name; |
| 455 | chip->irq_chip.irq_mask = sx150x_irq_mask; |
| 456 | chip->irq_chip.irq_unmask = sx150x_irq_unmask; |
| 457 | chip->irq_chip.irq_set_type = sx150x_irq_set_type; |
| 458 | chip->irq_chip.irq_bus_lock = sx150x_irq_bus_lock; |
| 459 | chip->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock; |
| 460 | chip->irq_summary = -1; |
| 461 | chip->irq_base = -1; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 462 | chip->irq_masked = ~0; |
Lennert Buytenhek | 673860c | 2011-01-12 17:00:18 -0800 | [diff] [blame] | 463 | chip->irq_sense = 0; |
Thomas Gleixner | 0ff56cd | 2011-02-14 18:08:51 +0100 | [diff] [blame] | 464 | chip->dev_masked = ~0; |
| 465 | chip->dev_sense = 0; |
| 466 | chip->irq_update = NO_UPDATE_PENDING; |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static int sx150x_init_io(struct sx150x_chip *chip, u8 base, u16 cfg) |
| 470 | { |
| 471 | int err = 0; |
| 472 | unsigned n; |
| 473 | |
| 474 | for (n = 0; err >= 0 && n < (chip->dev_cfg->ngpios / 8); ++n) |
| 475 | err = sx150x_i2c_write(chip->client, base - n, cfg >> (n * 8)); |
| 476 | return err; |
| 477 | } |
| 478 | |
Gregory Bean | 5affb60 | 2010-09-09 16:38:02 -0700 | [diff] [blame] | 479 | static int sx150x_reset(struct sx150x_chip *chip) |
| 480 | { |
| 481 | int err; |
| 482 | |
| 483 | err = i2c_smbus_write_byte_data(chip->client, |
| 484 | chip->dev_cfg->reg_reset, |
| 485 | 0x12); |
| 486 | if (err < 0) |
| 487 | return err; |
| 488 | |
| 489 | err = i2c_smbus_write_byte_data(chip->client, |
| 490 | chip->dev_cfg->reg_reset, |
| 491 | 0x34); |
| 492 | return err; |
| 493 | } |
| 494 | |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 495 | static int sx150x_init_hw(struct sx150x_chip *chip, |
| 496 | struct sx150x_platform_data *pdata) |
| 497 | { |
| 498 | int err = 0; |
| 499 | |
Gregory Bean | 5affb60 | 2010-09-09 16:38:02 -0700 | [diff] [blame] | 500 | if (pdata->reset_during_probe) { |
| 501 | err = sx150x_reset(chip); |
| 502 | if (err < 0) |
| 503 | return err; |
| 504 | } |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 505 | |
| 506 | err = sx150x_i2c_write(chip->client, |
| 507 | chip->dev_cfg->reg_misc, |
| 508 | 0x01); |
| 509 | if (err < 0) |
| 510 | return err; |
| 511 | |
| 512 | err = sx150x_init_io(chip, chip->dev_cfg->reg_pullup, |
| 513 | pdata->io_pullup_ena); |
| 514 | if (err < 0) |
| 515 | return err; |
| 516 | |
| 517 | err = sx150x_init_io(chip, chip->dev_cfg->reg_pulldn, |
| 518 | pdata->io_pulldn_ena); |
| 519 | if (err < 0) |
| 520 | return err; |
| 521 | |
| 522 | err = sx150x_init_io(chip, chip->dev_cfg->reg_drain, |
| 523 | pdata->io_open_drain_ena); |
| 524 | if (err < 0) |
| 525 | return err; |
| 526 | |
| 527 | err = sx150x_init_io(chip, chip->dev_cfg->reg_polarity, |
| 528 | pdata->io_polarity); |
| 529 | if (err < 0) |
| 530 | return err; |
| 531 | |
| 532 | if (pdata->oscio_is_gpo) |
| 533 | sx150x_set_oscio(chip, 0); |
| 534 | |
| 535 | return err; |
| 536 | } |
| 537 | |
| 538 | static int sx150x_install_irq_chip(struct sx150x_chip *chip, |
| 539 | int irq_summary, |
| 540 | int irq_base) |
| 541 | { |
| 542 | int err; |
| 543 | unsigned n; |
| 544 | unsigned irq; |
| 545 | |
| 546 | chip->irq_summary = irq_summary; |
| 547 | chip->irq_base = irq_base; |
| 548 | |
| 549 | for (n = 0; n < chip->dev_cfg->ngpios; ++n) { |
| 550 | irq = irq_base + n; |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 551 | irq_set_chip_and_handler(irq, &chip->irq_chip, handle_edge_irq); |
| 552 | irq_set_nested_thread(irq, 1); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 553 | #ifdef CONFIG_ARM |
| 554 | set_irq_flags(irq, IRQF_VALID); |
| 555 | #else |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 556 | irq_set_noprobe(irq); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 557 | #endif |
| 558 | } |
| 559 | |
| 560 | err = request_threaded_irq(irq_summary, |
| 561 | NULL, |
| 562 | sx150x_irq_thread_fn, |
| 563 | IRQF_SHARED | IRQF_TRIGGER_FALLING, |
| 564 | chip->irq_chip.name, |
| 565 | chip); |
| 566 | if (err < 0) { |
| 567 | chip->irq_summary = -1; |
| 568 | chip->irq_base = -1; |
| 569 | } |
| 570 | |
| 571 | return err; |
| 572 | } |
| 573 | |
| 574 | static void sx150x_remove_irq_chip(struct sx150x_chip *chip) |
| 575 | { |
| 576 | unsigned n; |
| 577 | unsigned irq; |
| 578 | |
| 579 | free_irq(chip->irq_summary, chip); |
| 580 | |
| 581 | for (n = 0; n < chip->dev_cfg->ngpios; ++n) { |
| 582 | irq = chip->irq_base + n; |
Thomas Gleixner | 08f1b80 | 2011-03-24 21:27:37 +0000 | [diff] [blame] | 583 | irq_set_chip_and_handler(irq, NULL, NULL); |
Gregory Bean | c34f16b | 2010-08-10 18:02:27 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | static int __devinit sx150x_probe(struct i2c_client *client, |
| 588 | const struct i2c_device_id *id) |
| 589 | { |
| 590 | static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA | |
| 591 | I2C_FUNC_SMBUS_WRITE_WORD_DATA; |
| 592 | struct sx150x_platform_data *pdata; |
| 593 | struct sx150x_chip *chip; |
| 594 | int rc; |
| 595 | |
| 596 | pdata = client->dev.platform_data; |
| 597 | if (!pdata) |
| 598 | return -EINVAL; |
| 599 | |
| 600 | if (!i2c_check_functionality(client->adapter, i2c_funcs)) |
| 601 | return -ENOSYS; |
| 602 | |
| 603 | chip = kzalloc(sizeof(struct sx150x_chip), GFP_KERNEL); |
| 604 | if (!chip) |
| 605 | return -ENOMEM; |
| 606 | |
| 607 | sx150x_init_chip(chip, client, id->driver_data, pdata); |
| 608 | rc = sx150x_init_hw(chip, pdata); |
| 609 | if (rc < 0) |
| 610 | goto probe_fail_pre_gpiochip_add; |
| 611 | |
| 612 | rc = gpiochip_add(&chip->gpio_chip); |
| 613 | if (rc < 0) |
| 614 | goto probe_fail_pre_gpiochip_add; |
| 615 | |
| 616 | if (pdata->irq_summary >= 0) { |
| 617 | rc = sx150x_install_irq_chip(chip, |
| 618 | pdata->irq_summary, |
| 619 | pdata->irq_base); |
| 620 | if (rc < 0) |
| 621 | goto probe_fail_post_gpiochip_add; |
| 622 | } |
| 623 | |
| 624 | i2c_set_clientdata(client, chip); |
| 625 | |
| 626 | return 0; |
| 627 | probe_fail_post_gpiochip_add: |
| 628 | WARN_ON(gpiochip_remove(&chip->gpio_chip) < 0); |
| 629 | probe_fail_pre_gpiochip_add: |
| 630 | kfree(chip); |
| 631 | return rc; |
| 632 | } |
| 633 | |
| 634 | static int __devexit sx150x_remove(struct i2c_client *client) |
| 635 | { |
| 636 | struct sx150x_chip *chip; |
| 637 | int rc; |
| 638 | |
| 639 | chip = i2c_get_clientdata(client); |
| 640 | rc = gpiochip_remove(&chip->gpio_chip); |
| 641 | if (rc < 0) |
| 642 | return rc; |
| 643 | |
| 644 | if (chip->irq_summary >= 0) |
| 645 | sx150x_remove_irq_chip(chip); |
| 646 | |
| 647 | kfree(chip); |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static struct i2c_driver sx150x_driver = { |
| 653 | .driver = { |
| 654 | .name = "sx150x", |
| 655 | .owner = THIS_MODULE |
| 656 | }, |
| 657 | .probe = sx150x_probe, |
| 658 | .remove = __devexit_p(sx150x_remove), |
| 659 | .id_table = sx150x_id, |
| 660 | }; |
| 661 | |
| 662 | static int __init sx150x_init(void) |
| 663 | { |
| 664 | return i2c_add_driver(&sx150x_driver); |
| 665 | } |
| 666 | subsys_initcall(sx150x_init); |
| 667 | |
| 668 | static void __exit sx150x_exit(void) |
| 669 | { |
| 670 | return i2c_del_driver(&sx150x_driver); |
| 671 | } |
| 672 | module_exit(sx150x_exit); |
| 673 | |
| 674 | MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>"); |
| 675 | MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders"); |
| 676 | MODULE_LICENSE("GPL v2"); |
| 677 | MODULE_ALIAS("i2c:sx150x"); |