10 #include <linux/i2c.h>
13 #include <linux/module.h>
16 #include <linux/slab.h>
18 #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift)
19 #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift)
20 #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift)
21 #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift)
22 #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift)
42 static inline struct adnp *to_adnp(
struct gpio_chip *
chip)
54 "i2c_smbus_read_byte_data()", err);
62 static int adnp_write(
struct adnp *adnp,
unsigned offset,
uint8_t value)
69 "i2c_smbus_write_byte_data()", err);
76 static int adnp_gpio_get(
struct gpio_chip *
chip,
unsigned offset)
78 struct adnp *adnp = to_adnp(chip);
80 unsigned int pos = offset & 7;
84 err = adnp_read(adnp,
GPIO_PLR(adnp) + reg, &value);
88 return (value &
BIT(pos)) ? 1 : 0;
91 static void __adnp_gpio_set(
struct adnp *adnp,
unsigned offset,
int value)
93 unsigned int reg = offset >> adnp->
reg_shift;
94 unsigned int pos = offset & 7;
98 err = adnp_read(adnp,
GPIO_PLR(adnp) + reg, &val);
107 adnp_write(adnp,
GPIO_PLR(adnp) + reg, val);
110 static void adnp_gpio_set(
struct gpio_chip *chip,
unsigned offset,
int value)
112 struct adnp *adnp = to_adnp(chip);
115 __adnp_gpio_set(adnp, offset, value);
119 static int adnp_gpio_direction_input(
struct gpio_chip *chip,
unsigned offset)
121 struct adnp *adnp = to_adnp(chip);
122 unsigned int reg = offset >> adnp->
reg_shift;
123 unsigned int pos = offset & 7;
129 err = adnp_read(adnp,
GPIO_DDR(adnp) + reg, &value);
135 err = adnp_write(adnp,
GPIO_DDR(adnp) + reg, value);
139 err = adnp_read(adnp,
GPIO_DDR(adnp) + reg, &value);
153 static int adnp_gpio_direction_output(
struct gpio_chip *chip,
unsigned offset,
156 struct adnp *adnp = to_adnp(chip);
157 unsigned int reg = offset >> adnp->
reg_shift;
158 unsigned int pos = offset & 7;
164 err = adnp_read(adnp,
GPIO_DDR(adnp) + reg, &val);
170 err = adnp_write(adnp,
GPIO_DDR(adnp) + reg, val);
174 err = adnp_read(adnp,
GPIO_DDR(adnp) + reg, &val);
178 if (!(val &
BIT(pos))) {
183 __adnp_gpio_set(adnp, offset, value);
191 static void adnp_gpio_dbg_show(
struct seq_file *
s,
struct gpio_chip *chip)
193 struct adnp *adnp = to_adnp(chip);
194 unsigned int num_regs = 1 << adnp->
reg_shift,
i,
j;
197 for (
i = 0;
i < num_regs;
i++) {
202 err = adnp_read(adnp,
GPIO_DDR(adnp) +
i, &ddr);
208 err = adnp_read(adnp,
GPIO_PLR(adnp) +
i, &plr);
214 err = adnp_read(adnp,
GPIO_IER(adnp) +
i, &ier);
220 err = adnp_read(adnp,
GPIO_ISR(adnp) +
i, &isr);
228 for (j = 0; j < 8; j++) {
231 const char *
level =
"low ";
233 const char *pending =
"";
236 direction =
"output";
242 interrupt =
"enabled ";
248 direction, level, interrupt, pending);
253 static int adnp_gpio_setup(
struct adnp *adnp,
unsigned int num_gpios)
255 struct gpio_chip *chip = &adnp->
gpio;
257 adnp->
reg_shift = get_count_order(num_gpios) - 3;
259 chip->direction_input = adnp_gpio_direction_input;
260 chip->direction_output = adnp_gpio_direction_output;
261 chip->get = adnp_gpio_get;
262 chip->set = adnp_gpio_set;
266 chip->dbg_show = adnp_gpio_dbg_show;
269 chip->ngpio = num_gpios;
270 chip->label = adnp->
client->name;
271 chip->dev = &adnp->
client->dev;
272 chip->of_node = chip->dev->of_node;
280 struct adnp *adnp =
data;
281 unsigned int num_regs,
i;
285 for (i = 0; i < num_regs; i++) {
288 unsigned long pending;
293 err = adnp_read(adnp,
GPIO_PLR(adnp) + i, &level);
299 err = adnp_read(adnp,
GPIO_ISR(adnp) + i, &isr);
305 err = adnp_read(adnp,
GPIO_IER(adnp) + i, &ier);
317 pending = changed & ((adnp->
irq_fall[
i] & ~level) |
325 pending &= isr &
ier;
337 static int adnp_gpio_to_irq(
struct gpio_chip *chip,
unsigned offset)
339 struct adnp *adnp = to_adnp(chip);
343 static void adnp_irq_mask(
struct irq_data *data)
345 struct adnp *adnp = irq_data_get_irq_chip_data(data);
347 unsigned int pos = data->
hwirq & 7;
352 static void adnp_irq_unmask(
struct irq_data *data)
354 struct adnp *adnp = irq_data_get_irq_chip_data(data);
356 unsigned int pos = data->
hwirq & 7;
361 static int adnp_irq_set_type(
struct irq_data *data,
unsigned int type)
363 struct adnp *adnp = irq_data_get_irq_chip_data(data);
365 unsigned int pos = data->
hwirq & 7;
390 static void adnp_irq_bus_lock(
struct irq_data *data)
392 struct adnp *adnp = irq_data_get_irq_chip_data(data);
397 static void adnp_irq_bus_unlock(
struct irq_data *data)
399 struct adnp *adnp = irq_data_get_irq_chip_data(data);
400 unsigned int num_regs = 1 << adnp->
reg_shift,
i;
404 for (i = 0; i < num_regs; i++)
411 static struct irq_chip adnp_irq_chip = {
413 .irq_mask = adnp_irq_mask,
414 .irq_unmask = adnp_irq_unmask,
415 .irq_set_type = adnp_irq_set_type,
416 .irq_bus_lock = adnp_irq_bus_lock,
417 .irq_bus_sync_unlock = adnp_irq_bus_unlock,
420 static int adnp_irq_map(
struct irq_domain *domain,
unsigned int irq,
425 irq_set_nested_thread(irq,
true);
430 irq_set_noprobe(irq);
441 static int adnp_irq_setup(
struct adnp *adnp)
443 unsigned int num_regs = 1 << adnp->
reg_shift,
i;
444 struct gpio_chip *chip = &adnp->
gpio;
467 for (i = 0; i < num_regs; i++) {
477 err = adnp_write(adnp,
GPIO_IER(adnp) + i, 0);
485 &adnp_irq_domain_ops, adnp);
489 dev_name(chip->dev), adnp);
491 dev_err(chip->dev,
"can't request IRQ#%d: %d\n",
496 chip->to_irq = adnp_gpio_to_irq;
504 static void adnp_irq_teardown(
struct adnp *adnp)
510 for (i = 0; i < adnp->
gpio.ngpio; i++) {
527 err = of_property_read_u32(np,
"nr-gpios", &num_gpios);
542 err = adnp_gpio_setup(adnp, num_gpios);
547 err = adnp_irq_setup(adnp);
556 i2c_set_clientdata(client, adnp);
561 adnp_irq_teardown(adnp);
568 struct adnp *adnp = i2c_get_clientdata(client);
574 dev_err(&client->
dev,
"%s failed: %d\n",
"gpiochip_remove()",
580 adnp_irq_teardown(adnp);
591 static const struct of_device_id adnp_of_match[] __devinitconst = {
592 { .compatible =
"ad,gpio-adnp", },
603 .probe = adnp_i2c_probe,
605 .id_table = adnp_i2c_id,