18 #include <linux/i2c.h>
22 #include <linux/module.h>
24 #include <linux/slab.h>
28 #define NO_UPDATE_PENDING -1
107 "i2c write fail: can't write %02x to %02x: %d\n",
120 "i2c read fail: can't read from %02x: %d\n",
127 return (chip->
dev_cfg->ngpios == offset);
149 static inline void sx150x_find_cfg(
u8 offset,
u8 width,
152 *reg -= offset * width / 8;
153 *mask = (1 <<
width) - 1;
154 *shift = (offset *
width) % 8;
166 sx150x_find_cfg(offset, width, ®, &mask, &shift);
167 err = sx150x_i2c_read(chip->
client, reg, &data);
172 data |= (val << shift) & mask;
173 return sx150x_i2c_write(chip->
client, reg, data);
176 static int sx150x_get_io(
struct sx150x_chip *chip,
unsigned offset)
184 sx150x_find_cfg(offset, 1, ®, &mask, &shift);
185 err = sx150x_i2c_read(chip->
client, reg, &data);
187 err = (data &
mask) != 0 ? 1 : 0;
192 static void sx150x_set_oscio(
struct sx150x_chip *chip,
int val)
194 sx150x_i2c_write(chip->
client,
196 (val ? 0x1f : 0x10));
199 static void sx150x_set_io(
struct sx150x_chip *chip,
unsigned offset,
int val)
201 sx150x_write_cfg(chip,
208 static int sx150x_io_input(
struct sx150x_chip *chip,
unsigned offset)
210 return sx150x_write_cfg(chip,
217 static int sx150x_io_output(
struct sx150x_chip *chip,
unsigned offset,
int val)
221 err = sx150x_write_cfg(chip,
227 err = sx150x_write_cfg(chip,
235 static int sx150x_gpio_get(
struct gpio_chip *
gc,
unsigned offset)
242 if (!offset_is_oscio(chip, offset)) {
244 status = sx150x_get_io(chip, offset);
251 static void sx150x_gpio_set(
struct gpio_chip *gc,
unsigned offset,
int val)
258 if (offset_is_oscio(chip, offset))
259 sx150x_set_oscio(chip, val);
261 sx150x_set_io(chip, offset, val);
265 static int sx150x_gpio_direction_input(
struct gpio_chip *gc,
unsigned offset)
272 if (!offset_is_oscio(chip, offset)) {
274 status = sx150x_io_input(chip, offset);
280 static int sx150x_gpio_direction_output(
struct gpio_chip *gc,
289 if (!offset_is_oscio(chip, offset)) {
291 status = sx150x_io_output(chip, offset, val);
297 static int sx150x_gpio_to_irq(
struct gpio_chip *gc,
unsigned offset)
303 if (offset >= chip->
dev_cfg->ngpios)
312 static void sx150x_irq_mask(
struct irq_data *
d)
314 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
322 static void sx150x_irq_unmask(
struct irq_data *
d)
324 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
332 static int sx150x_irq_set_type(
struct irq_data *
d,
unsigned int flow_type)
334 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
356 unsigned nhandled = 0;
363 for (i = (chip->
dev_cfg->ngpios / 8) - 1; i >= 0; --
i) {
364 err = sx150x_i2c_read(chip->
client,
365 chip->
dev_cfg->reg_irq_src - i,
370 sx150x_i2c_write(chip->
client,
371 chip->
dev_cfg->reg_irq_src - i,
373 for (n = 0; n < 8; ++
n) {
374 if (val & (1 << n)) {
375 sub_irq = chip->
irq_base + (i * 8) + n;
385 static void sx150x_irq_bus_lock(
struct irq_data *d)
387 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
392 static void sx150x_irq_bus_sync_unlock(
struct irq_data *d)
394 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
412 sx150x_write_cfg(chip, n, 1, chip->
dev_cfg->reg_irq_mask, 1);
413 sx150x_write_cfg(chip, n, 2, chip->
dev_cfg->reg_sense, 0);
415 sx150x_write_cfg(chip, n, 1, chip->
dev_cfg->reg_irq_mask, 0);
416 sx150x_write_cfg(chip, n, 2, chip->
dev_cfg->reg_sense,
423 static void sx150x_init_chip(
struct sx150x_chip *chip,
431 chip->
dev_cfg = &sx150x_devices[driver_data];
433 chip->
gpio_chip.direction_input = sx150x_gpio_direction_input;
434 chip->
gpio_chip.direction_output = sx150x_gpio_direction_output;
437 chip->
gpio_chip.to_irq = sx150x_gpio_to_irq;
445 chip->
irq_chip.irq_mask = sx150x_irq_mask;
446 chip->
irq_chip.irq_unmask = sx150x_irq_unmask;
447 chip->
irq_chip.irq_set_type = sx150x_irq_set_type;
448 chip->
irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
449 chip->
irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
464 for (n = 0; err >= 0 && n < (chip->
dev_cfg->ngpios / 8); ++
n)
465 err = sx150x_i2c_write(chip->
client, base - n, cfg >> (n * 8));
485 static int sx150x_init_hw(
struct sx150x_chip *chip,
491 err = sx150x_reset(chip);
496 err = sx150x_i2c_write(chip->
client,
502 err = sx150x_init_io(chip, chip->
dev_cfg->reg_pullup,
507 err = sx150x_init_io(chip, chip->
dev_cfg->reg_pulldn,
512 err = sx150x_init_io(chip, chip->
dev_cfg->reg_drain,
517 err = sx150x_init_io(chip, chip->
dev_cfg->reg_polarity,
523 sx150x_set_oscio(chip, 0);
528 static int sx150x_install_irq_chip(
struct sx150x_chip *chip,
539 for (n = 0; n < chip->
dev_cfg->ngpios; ++
n) {
543 irq_set_nested_thread(irq, 1);
547 irq_set_noprobe(irq);
553 sx150x_irq_thread_fn,
565 static void sx150x_remove_irq_chip(
struct sx150x_chip *chip)
572 for (n = 0; n < chip->
dev_cfg->ngpios; ++
n) {
574 irq_set_chip_and_handler(irq,
NULL,
NULL);
587 pdata = client->
dev.platform_data;
591 if (!i2c_check_functionality(client->
adapter, i2c_funcs))
598 sx150x_init_chip(chip, client, id->driver_data, pdata);
599 rc = sx150x_init_hw(chip, pdata);
601 goto probe_fail_pre_gpiochip_add;
605 goto probe_fail_pre_gpiochip_add;
608 rc = sx150x_install_irq_chip(chip,
612 goto probe_fail_post_gpiochip_add;
615 i2c_set_clientdata(client, chip);
618 probe_fail_post_gpiochip_add:
620 probe_fail_pre_gpiochip_add:
630 chip = i2c_get_clientdata(client);
636 sx150x_remove_irq_chip(chip);
648 .probe = sx150x_probe,
650 .id_table = sx150x_id,
653 static int __init sx150x_init(
void)
655 return i2c_add_driver(&sx150x_driver);
659 static void __exit sx150x_exit(
void)