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/init.h>
13 #include <linux/i2c.h>
14 #include <linux/gpio.h>
16 #include <linux/i2c/adp5588.h>
18 #define DRV_NAME "adp5588-gpio"
20 #define ADP_BANK(offs) ((offs) >> 3)
21 #define ADP_BIT(offs) (1u << ((offs) & 0x7))
24 struct i2c_client
*client
;
25 struct gpio_chip gpio_chip
;
26 struct mutex lock
; /* protect cached dir, dat_out */
32 static int adp5588_gpio_read(struct i2c_client
*client
, u8 reg
)
34 int ret
= i2c_smbus_read_byte_data(client
, reg
);
37 dev_err(&client
->dev
, "Read Error\n");
42 static int adp5588_gpio_write(struct i2c_client
*client
, u8 reg
, u8 val
)
44 int ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
47 dev_err(&client
->dev
, "Write Error\n");
52 static int adp5588_gpio_get_value(struct gpio_chip
*chip
, unsigned off
)
54 struct adp5588_gpio
*dev
=
55 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
57 return !!(adp5588_gpio_read(dev
->client
, GPIO_DAT_STAT1
+ ADP_BANK(off
))
61 static void adp5588_gpio_set_value(struct gpio_chip
*chip
,
62 unsigned off
, int val
)
65 struct adp5588_gpio
*dev
=
66 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
71 mutex_lock(&dev
->lock
);
73 dev
->dat_out
[bank
] |= bit
;
75 dev
->dat_out
[bank
] &= ~bit
;
77 adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
79 mutex_unlock(&dev
->lock
);
82 static int adp5588_gpio_direction_input(struct gpio_chip
*chip
, unsigned off
)
86 struct adp5588_gpio
*dev
=
87 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
91 mutex_lock(&dev
->lock
);
92 dev
->dir
[bank
] &= ~ADP_BIT(off
);
93 ret
= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
, dev
->dir
[bank
]);
94 mutex_unlock(&dev
->lock
);
99 static int adp5588_gpio_direction_output(struct gpio_chip
*chip
,
100 unsigned off
, int val
)
104 struct adp5588_gpio
*dev
=
105 container_of(chip
, struct adp5588_gpio
, gpio_chip
);
107 bank
= ADP_BANK(off
);
110 mutex_lock(&dev
->lock
);
111 dev
->dir
[bank
] |= bit
;
114 dev
->dat_out
[bank
] |= bit
;
116 dev
->dat_out
[bank
] &= ~bit
;
118 ret
= adp5588_gpio_write(dev
->client
, GPIO_DAT_OUT1
+ bank
,
120 ret
|= adp5588_gpio_write(dev
->client
, GPIO_DIR1
+ bank
,
122 mutex_unlock(&dev
->lock
);
127 static int __devinit
adp5588_gpio_probe(struct i2c_client
*client
,
128 const struct i2c_device_id
*id
)
130 struct adp5588_gpio_platform_data
*pdata
= client
->dev
.platform_data
;
131 struct adp5588_gpio
*dev
;
132 struct gpio_chip
*gc
;
136 dev_err(&client
->dev
, "missing platform data\n");
140 if (!i2c_check_functionality(client
->adapter
,
141 I2C_FUNC_SMBUS_BYTE_DATA
)) {
142 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
146 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
148 dev_err(&client
->dev
, "failed to alloc memory\n");
152 dev
->client
= client
;
154 gc
= &dev
->gpio_chip
;
155 gc
->direction_input
= adp5588_gpio_direction_input
;
156 gc
->direction_output
= adp5588_gpio_direction_output
;
157 gc
->get
= adp5588_gpio_get_value
;
158 gc
->set
= adp5588_gpio_set_value
;
161 gc
->base
= pdata
->gpio_start
;
163 gc
->label
= client
->name
;
164 gc
->owner
= THIS_MODULE
;
166 mutex_init(&dev
->lock
);
169 ret
= adp5588_gpio_read(dev
->client
, DEV_ID
);
173 revid
= ret
& ADP5588_DEVICE_ID_MASK
;
175 for (i
= 0, ret
= 0; i
<= ADP_BANK(MAXGPIO
); i
++) {
176 dev
->dat_out
[i
] = adp5588_gpio_read(client
, GPIO_DAT_OUT1
+ i
);
177 dev
->dir
[i
] = adp5588_gpio_read(client
, GPIO_DIR1
+ i
);
178 ret
|= adp5588_gpio_write(client
, KP_GPIO1
+ i
, 0);
179 ret
|= adp5588_gpio_write(client
, GPIO_PULL1
+ i
,
180 (pdata
->pullup_dis_mask
>> (8 * i
)) & 0xFF);
186 ret
= gpiochip_add(&dev
->gpio_chip
);
190 dev_info(&client
->dev
, "gpios %d..%d on a %s Rev. %d\n",
191 gc
->base
, gc
->base
+ gc
->ngpio
- 1,
192 client
->name
, revid
);
195 ret
= pdata
->setup(client
, gc
->base
, gc
->ngpio
, pdata
->context
);
197 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
200 i2c_set_clientdata(client
, dev
);
208 static int __devexit
adp5588_gpio_remove(struct i2c_client
*client
)
210 struct adp5588_gpio_platform_data
*pdata
= client
->dev
.platform_data
;
211 struct adp5588_gpio
*dev
= i2c_get_clientdata(client
);
214 if (pdata
->teardown
) {
215 ret
= pdata
->teardown(client
,
216 dev
->gpio_chip
.base
, dev
->gpio_chip
.ngpio
,
219 dev_err(&client
->dev
, "teardown failed %d\n", ret
);
224 ret
= gpiochip_remove(&dev
->gpio_chip
);
226 dev_err(&client
->dev
, "gpiochip_remove failed %d\n", ret
);
234 static const struct i2c_device_id adp5588_gpio_id
[] = {
239 MODULE_DEVICE_TABLE(i2c
, adp5588_gpio_id
);
241 static struct i2c_driver adp5588_gpio_driver
= {
245 .probe
= adp5588_gpio_probe
,
246 .remove
= __devexit_p(adp5588_gpio_remove
),
247 .id_table
= adp5588_gpio_id
,
250 static int __init
adp5588_gpio_init(void)
252 return i2c_add_driver(&adp5588_gpio_driver
);
255 module_init(adp5588_gpio_init
);
257 static void __exit
adp5588_gpio_exit(void)
259 i2c_del_driver(&adp5588_gpio_driver
);
262 module_exit(adp5588_gpio_exit
);
264 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
265 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
266 MODULE_LICENSE("GPL");