2 * GPIO Chip driver for Analog Devices
3 * ADP5588 I/O Expander and QWERTY Keypad Controller
5 * Copyright 2009 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/i2c.h>
15 #include <linux/gpio.h>
17 #include <linux/i2c/adp5588.h>
19 #define DRV_NAME "adp5588-gpio"
21 #define ADP_BANK(offs) ((offs) >> 3)
22 #define ADP_BIT(offs) (1u << ((offs) & 0x7))
25 struct i2c_client
*client
;
26 struct gpio_chip gpio_chip
;
27 struct mutex lock
; /* protect cached dir, dat_out */
33 static int adp5588_gpio_read(struct i2c_client
*client
, u8 reg
)
35 int ret
= i2c_smbus_read_byte_data(client
, reg
);
38 dev_err(&client
->dev
, "Read Error\n");
43 static int adp5588_gpio_write(struct i2c_client
*client
, u8 reg
, u8 val
)
45 int ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
48 dev_err(&client
->dev
, "Write Error\n");
53 static int adp5588_gpio_get_value(struct gpio_chip
*chip
, unsigned off
)
55 struct adp5588_gpio
*dev
=
56 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
58 return !!(adp5588_gpio_read(dev
->client
, GPIO_DAT_STAT1
+ ADP_BANK(off
))
62 static void adp5588_gpio_set_value(struct gpio_chip
*chip
,
63 unsigned off
, int val
)
66 struct adp5588_gpio
*dev
=
67 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
72 mutex_lock(&dev
->lock
);
74 dev
->dat_out
[bank
] |= bit
;
76 dev
->dat_out
[bank
] &= ~bit
;
78 adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
80 mutex_unlock(&dev
->lock
);
83 static int adp5588_gpio_direction_input(struct gpio_chip
*chip
, unsigned off
)
87 struct adp5588_gpio
*dev
=
88 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
92 mutex_lock(&dev
->lock
);
93 dev
->dir
[bank
] &= ~ADP_BIT(off
);
94 ret
= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
, dev
->dir
[bank
]);
95 mutex_unlock(&dev
->lock
);
100 static int adp5588_gpio_direction_output(struct gpio_chip
*chip
,
101 unsigned off
, int val
)
105 struct adp5588_gpio
*dev
=
106 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
108 bank
= ADP_BANK(off
);
111 mutex_lock(&dev
->lock
);
112 dev
->dir
[bank
] |= bit
;
115 dev
->dat_out
[bank
] |= bit
;
117 dev
->dat_out
[bank
] &= ~bit
;
119 ret
= adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
121 ret
|= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
,
123 mutex_unlock(&dev
->lock
);
128 static int __devinit
adp5588_gpio_probe(struct i2c_client
*client
,
129 const struct i2c_device_id
*id
)
131 struct adp5588_gpio_platform_data
*pdata
= client
->dev
.platform_data
;
132 struct adp5588_gpio
*dev
;
133 struct gpio_chip
*gc
;
137 dev_err(&client
->dev
, "missing platform data\n");
141 if (!i2c_check_functionality(client
->adapter
,
142 I2C_FUNC_SMBUS_BYTE_DATA
)) {
143 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
147 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
149 dev_err(&client
->dev
, "failed to alloc memory\n");
153 dev
->client
= client
;
155 gc
= &dev
->gpio_chip
;
156 gc
->direction_input
= adp5588_gpio_direction_input
;
157 gc
->direction_output
= adp5588_gpio_direction_output
;
158 gc
->get
= adp5588_gpio_get_value
;
159 gc
->set
= adp5588_gpio_set_value
;
162 gc
->base
= pdata
->gpio_start
;
164 gc
->label
= client
->name
;
165 gc
->owner
= THIS_MODULE
;
167 mutex_init(&dev
->lock
);
170 ret
= adp5588_gpio_read(dev
->client
, DEV_ID
);
174 revid
= ret
& ADP5588_DEVICE_ID_MASK
;
176 for (i
= 0, ret
= 0; i
<= ADP_BANK(MAXGPIO
); i
++) {
177 dev
->dat_out
[i
] = adp5588_gpio_read(client
, GPIO_DAT_OUT1
+ i
);
178 dev
->dir
[i
] = adp5588_gpio_read(client
, GPIO_DIR1
+ i
);
179 ret
|= adp5588_gpio_write(client
, KP_GPIO1
+ i
, 0);
180 ret
|= adp5588_gpio_write(client
, GPIO_PULL1
+ i
,
181 (pdata
->pullup_dis_mask
>> (8 * i
)) & 0xFF);
187 ret
= gpiochip_add(&dev
->gpio_chip
);
191 dev_info(&client
->dev
, "gpios %d..%d on a %s Rev. %d\n",
192 gc
->base
, gc
->base
+ gc
->ngpio
- 1,
193 client
->name
, revid
);
196 ret
= pdata
->setup(client
, gc
->base
, gc
->ngpio
, pdata
->context
);
198 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
201 i2c_set_clientdata(client
, dev
);
209 static int __devexit
adp5588_gpio_remove(struct i2c_client
*client
)
211 struct adp5588_gpio_platform_data
*pdata
= client
->dev
.platform_data
;
212 struct adp5588_gpio
*dev
= i2c_get_clientdata(client
);
215 if (pdata
->teardown
) {
216 ret
= pdata
->teardown(client
,
217 dev
->gpio_chip
.base
, dev
->gpio_chip
.ngpio
,
220 dev_err(&client
->dev
, "teardown failed %d\n", ret
);
225 ret
= gpiochip_remove(&dev
->gpio_chip
);
227 dev_err(&client
->dev
, "gpiochip_remove failed %d\n", ret
);
235 static const struct i2c_device_id adp5588_gpio_id
[] = {
240 MODULE_DEVICE_TABLE(i2c
, adp5588_gpio_id
);
242 static struct i2c_driver adp5588_gpio_driver
= {
246 .probe
= adp5588_gpio_probe
,
247 .remove
= __devexit_p(adp5588_gpio_remove
),
248 .id_table
= adp5588_gpio_id
,
251 static int __init
adp5588_gpio_init(void)
253 return i2c_add_driver(&adp5588_gpio_driver
);
256 module_init(adp5588_gpio_init
);
258 static void __exit
adp5588_gpio_exit(void)
260 i2c_del_driver(&adp5588_gpio_driver
);
263 module_exit(adp5588_gpio_exit
);
265 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
266 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
267 MODULE_LICENSE("GPL");