ALSA: hda - Remove superfluous init verb entries for ALC88[235]
[firewire-audio.git] / drivers / gpio / adp5588-gpio.c
blobafc097a16b3330393f08b544d4baf87b19d81029
1 /*
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.
8 */
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"
19 #define MAXGPIO 18
20 #define ADP_BANK(offs) ((offs) >> 3)
21 #define ADP_BIT(offs) (1u << ((offs) & 0x7))
23 struct adp5588_gpio {
24 struct i2c_client *client;
25 struct gpio_chip gpio_chip;
26 struct mutex lock; /* protect cached dir, dat_out */
27 unsigned gpio_start;
28 uint8_t dat_out[3];
29 uint8_t dir[3];
32 static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
34 int ret = i2c_smbus_read_byte_data(client, reg);
36 if (ret < 0)
37 dev_err(&client->dev, "Read Error\n");
39 return ret;
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);
46 if (ret < 0)
47 dev_err(&client->dev, "Write Error\n");
49 return ret;
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))
58 & ADP_BIT(off));
61 static void adp5588_gpio_set_value(struct gpio_chip *chip,
62 unsigned off, int val)
64 unsigned bank, bit;
65 struct adp5588_gpio *dev =
66 container_of(chip, struct adp5588_gpio, gpio_chip);
68 bank = ADP_BANK(off);
69 bit = ADP_BIT(off);
71 mutex_lock(&dev->lock);
72 if (val)
73 dev->dat_out[bank] |= bit;
74 else
75 dev->dat_out[bank] &= ~bit;
77 adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
78 dev->dat_out[bank]);
79 mutex_unlock(&dev->lock);
82 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
84 int ret;
85 unsigned bank;
86 struct adp5588_gpio *dev =
87 container_of(chip, struct adp5588_gpio, gpio_chip);
89 bank = ADP_BANK(off);
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);
96 return ret;
99 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
100 unsigned off, int val)
102 int ret;
103 unsigned bank, bit;
104 struct adp5588_gpio *dev =
105 container_of(chip, struct adp5588_gpio, gpio_chip);
107 bank = ADP_BANK(off);
108 bit = ADP_BIT(off);
110 mutex_lock(&dev->lock);
111 dev->dir[bank] |= bit;
113 if (val)
114 dev->dat_out[bank] |= bit;
115 else
116 dev->dat_out[bank] &= ~bit;
118 ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
119 dev->dat_out[bank]);
120 ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
121 dev->dir[bank]);
122 mutex_unlock(&dev->lock);
124 return ret;
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;
133 int ret, i, revid;
135 if (pdata == NULL) {
136 dev_err(&client->dev, "missing platform data\n");
137 return -ENODEV;
140 if (!i2c_check_functionality(client->adapter,
141 I2C_FUNC_SMBUS_BYTE_DATA)) {
142 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
143 return -EIO;
146 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
147 if (dev == NULL) {
148 dev_err(&client->dev, "failed to alloc memory\n");
149 return -ENOMEM;
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;
159 gc->can_sleep = 1;
161 gc->base = pdata->gpio_start;
162 gc->ngpio = MAXGPIO;
163 gc->label = client->name;
164 gc->owner = THIS_MODULE;
166 mutex_init(&dev->lock);
169 ret = adp5588_gpio_read(dev->client, DEV_ID);
170 if (ret < 0)
171 goto err;
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);
182 if (ret)
183 goto err;
186 ret = gpiochip_add(&dev->gpio_chip);
187 if (ret)
188 goto err;
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);
194 if (pdata->setup) {
195 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
196 if (ret < 0)
197 dev_warn(&client->dev, "setup failed, %d\n", ret);
200 i2c_set_clientdata(client, dev);
201 return 0;
203 err:
204 kfree(dev);
205 return ret;
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);
212 int ret;
214 if (pdata->teardown) {
215 ret = pdata->teardown(client,
216 dev->gpio_chip.base, dev->gpio_chip.ngpio,
217 pdata->context);
218 if (ret < 0) {
219 dev_err(&client->dev, "teardown failed %d\n", ret);
220 return ret;
224 ret = gpiochip_remove(&dev->gpio_chip);
225 if (ret) {
226 dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
227 return ret;
230 kfree(dev);
231 return 0;
234 static const struct i2c_device_id adp5588_gpio_id[] = {
235 {DRV_NAME, 0},
239 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
241 static struct i2c_driver adp5588_gpio_driver = {
242 .driver = {
243 .name = DRV_NAME,
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");