2 * GPIO driver for Analog Devices ADP5520 MFD PMICs
4 * Copyright 2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/platform_device.h>
13 #include <linux/mfd/adp5520.h>
15 #include <linux/gpio.h>
18 struct device *master;
19 struct gpio_chip gpio_chip;
20 unsigned char lut[ADP5520_MAXGPIOS];
24 static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
26 struct adp5520_gpio *dev;
29 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
32 * There are dedicated registers for GPIO IN/OUT.
33 * Make sure we return the right value, even when configured as output
36 if (test_bit(off, &dev->output))
37 adp5520_read(dev->master, GPIO_OUT, ®_val);
39 adp5520_read(dev->master, GPIO_IN, ®_val);
41 return !!(reg_val & dev->lut[off]);
44 static void adp5520_gpio_set_value(struct gpio_chip *chip,
45 unsigned off, int val)
47 struct adp5520_gpio *dev;
48 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
51 adp5520_set_bits(dev->master, GPIO_OUT, dev->lut[off]);
53 adp5520_clr_bits(dev->master, GPIO_OUT, dev->lut[off]);
56 static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
58 struct adp5520_gpio *dev;
59 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
61 clear_bit(off, &dev->output);
63 return adp5520_clr_bits(dev->master, GPIO_CFG_2, dev->lut[off]);
66 static int adp5520_gpio_direction_output(struct gpio_chip *chip,
67 unsigned off, int val)
69 struct adp5520_gpio *dev;
71 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
73 set_bit(off, &dev->output);
76 ret |= adp5520_set_bits(dev->master, GPIO_OUT, dev->lut[off]);
78 ret |= adp5520_clr_bits(dev->master, GPIO_OUT, dev->lut[off]);
80 ret |= adp5520_set_bits(dev->master, GPIO_CFG_2, dev->lut[off]);
85 static int __devinit adp5520_gpio_probe(struct platform_device *pdev)
87 struct adp5520_gpio_platfrom_data *pdata = pdev->dev.platform_data;
88 struct adp5520_gpio *dev;
91 unsigned char ctl_mask = 0;
94 dev_err(&pdev->dev, "missing platform data\n");
98 if (pdev->id != ID_ADP5520) {
99 dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
103 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
105 dev_err(&pdev->dev, "failed to alloc memory\n");
109 dev->master = pdev->dev.parent;
111 for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
112 if (pdata->gpio_en_mask & (1 << i))
113 dev->lut[gpios++] = 1 << i;
120 gc = &dev->gpio_chip;
121 gc->direction_input = adp5520_gpio_direction_input;
122 gc->direction_output = adp5520_gpio_direction_output;
123 gc->get = adp5520_gpio_get_value;
124 gc->set = adp5520_gpio_set_value;
127 gc->base = pdata->gpio_start;
129 gc->label = pdev->name;
130 gc->owner = THIS_MODULE;
132 ret = adp5520_clr_bits(dev->master, GPIO_CFG_1,
133 pdata->gpio_en_mask);
135 if (pdata->gpio_en_mask & GPIO_C3)
138 if (pdata->gpio_en_mask & GPIO_R3)
142 ret = adp5520_set_bits(dev->master, LED_CONTROL,
145 ret |= adp5520_set_bits(dev->master, GPIO_PULLUP,
146 pdata->gpio_pullup_mask);
149 dev_err(&pdev->dev, "failed to write\n");
153 ret = gpiochip_add(&dev->gpio_chip);
157 platform_set_drvdata(pdev, dev);
165 static int __devexit adp5520_gpio_remove(struct platform_device *pdev)
167 struct adp5520_gpio *dev;
170 dev = platform_get_drvdata(pdev);
171 ret = gpiochip_remove(&dev->gpio_chip);
173 dev_err(&pdev->dev, "%s failed, %d\n",
174 "gpiochip_remove()", ret);
182 static struct platform_driver adp5520_gpio_driver = {
184 .name = "adp5520-gpio",
185 .owner = THIS_MODULE,
187 .probe = adp5520_gpio_probe,
188 .remove = __devexit_p(adp5520_gpio_remove),
191 static int __init adp5520_gpio_init(void)
193 return platform_driver_register(&adp5520_gpio_driver);
195 module_init(adp5520_gpio_init);
197 static void __exit adp5520_gpio_exit(void)
199 platform_driver_unregister(&adp5520_gpio_driver);
201 module_exit(adp5520_gpio_exit);
203 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
204 MODULE_DESCRIPTION("GPIO ADP5520 Driver");
205 MODULE_LICENSE("GPL");
206 MODULE_ALIAS("platform:adp5520-gpio");