Commit | Line | Data |
---|---|---|
ef72af40 MH |
1 | /* |
2 | * GPIO driver for Analog Devices ADP5520 MFD PMICs | |
3 | * | |
4 | * Copyright 2009 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2 or later. | |
7 | */ | |
8 | ||
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> | |
14 | ||
15 | #include <linux/gpio.h> | |
16 | ||
17 | struct adp5520_gpio { | |
18 | struct device *master; | |
19 | struct gpio_chip gpio_chip; | |
20 | unsigned char lut[ADP5520_MAXGPIOS]; | |
21 | unsigned long output; | |
22 | }; | |
23 | ||
24 | static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off) | |
25 | { | |
26 | struct adp5520_gpio *dev; | |
27 | uint8_t reg_val; | |
28 | ||
29 | dev = container_of(chip, struct adp5520_gpio, gpio_chip); | |
30 | ||
31 | /* | |
32 | * There are dedicated registers for GPIO IN/OUT. | |
33 | * Make sure we return the right value, even when configured as output | |
34 | */ | |
35 | ||
36 | if (test_bit(off, &dev->output)) | |
7c29a476 | 37 | adp5520_read(dev->master, ADP5520_GPIO_OUT, ®_val); |
ef72af40 | 38 | else |
7c29a476 | 39 | adp5520_read(dev->master, ADP5520_GPIO_IN, ®_val); |
ef72af40 MH |
40 | |
41 | return !!(reg_val & dev->lut[off]); | |
42 | } | |
43 | ||
44 | static void adp5520_gpio_set_value(struct gpio_chip *chip, | |
45 | unsigned off, int val) | |
46 | { | |
47 | struct adp5520_gpio *dev; | |
48 | dev = container_of(chip, struct adp5520_gpio, gpio_chip); | |
49 | ||
50 | if (val) | |
7c29a476 | 51 | adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]); |
ef72af40 | 52 | else |
7c29a476 | 53 | adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]); |
ef72af40 MH |
54 | } |
55 | ||
56 | static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off) | |
57 | { | |
58 | struct adp5520_gpio *dev; | |
59 | dev = container_of(chip, struct adp5520_gpio, gpio_chip); | |
60 | ||
61 | clear_bit(off, &dev->output); | |
62 | ||
7c29a476 MH |
63 | return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2, |
64 | dev->lut[off]); | |
ef72af40 MH |
65 | } |
66 | ||
67 | static int adp5520_gpio_direction_output(struct gpio_chip *chip, | |
68 | unsigned off, int val) | |
69 | { | |
70 | struct adp5520_gpio *dev; | |
71 | int ret = 0; | |
72 | dev = container_of(chip, struct adp5520_gpio, gpio_chip); | |
73 | ||
74 | set_bit(off, &dev->output); | |
75 | ||
76 | if (val) | |
7c29a476 MH |
77 | ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, |
78 | dev->lut[off]); | |
ef72af40 | 79 | else |
7c29a476 MH |
80 | ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, |
81 | dev->lut[off]); | |
ef72af40 | 82 | |
7c29a476 MH |
83 | ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2, |
84 | dev->lut[off]); | |
ef72af40 MH |
85 | |
86 | return ret; | |
87 | } | |
88 | ||
89 | static int __devinit adp5520_gpio_probe(struct platform_device *pdev) | |
90 | { | |
7c29a476 | 91 | struct adp5520_gpio_platform_data *pdata = pdev->dev.platform_data; |
ef72af40 MH |
92 | struct adp5520_gpio *dev; |
93 | struct gpio_chip *gc; | |
94 | int ret, i, gpios; | |
95 | unsigned char ctl_mask = 0; | |
96 | ||
97 | if (pdata == NULL) { | |
98 | dev_err(&pdev->dev, "missing platform data\n"); | |
99 | return -ENODEV; | |
100 | } | |
101 | ||
102 | if (pdev->id != ID_ADP5520) { | |
103 | dev_err(&pdev->dev, "only ADP5520 supports GPIO\n"); | |
104 | return -ENODEV; | |
105 | } | |
106 | ||
107 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
108 | if (dev == NULL) { | |
109 | dev_err(&pdev->dev, "failed to alloc memory\n"); | |
110 | return -ENOMEM; | |
111 | } | |
112 | ||
113 | dev->master = pdev->dev.parent; | |
114 | ||
115 | for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++) | |
116 | if (pdata->gpio_en_mask & (1 << i)) | |
117 | dev->lut[gpios++] = 1 << i; | |
118 | ||
119 | if (gpios < 1) { | |
120 | ret = -EINVAL; | |
121 | goto err; | |
122 | } | |
123 | ||
124 | gc = &dev->gpio_chip; | |
125 | gc->direction_input = adp5520_gpio_direction_input; | |
126 | gc->direction_output = adp5520_gpio_direction_output; | |
127 | gc->get = adp5520_gpio_get_value; | |
128 | gc->set = adp5520_gpio_set_value; | |
129 | gc->can_sleep = 1; | |
130 | ||
131 | gc->base = pdata->gpio_start; | |
132 | gc->ngpio = gpios; | |
133 | gc->label = pdev->name; | |
134 | gc->owner = THIS_MODULE; | |
135 | ||
7c29a476 | 136 | ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1, |
ef72af40 MH |
137 | pdata->gpio_en_mask); |
138 | ||
7c29a476 MH |
139 | if (pdata->gpio_en_mask & ADP5520_GPIO_C3) |
140 | ctl_mask |= ADP5520_C3_MODE; | |
ef72af40 | 141 | |
7c29a476 MH |
142 | if (pdata->gpio_en_mask & ADP5520_GPIO_R3) |
143 | ctl_mask |= ADP5520_R3_MODE; | |
ef72af40 MH |
144 | |
145 | if (ctl_mask) | |
7c29a476 | 146 | ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL, |
ef72af40 MH |
147 | ctl_mask); |
148 | ||
7c29a476 | 149 | ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP, |
ef72af40 MH |
150 | pdata->gpio_pullup_mask); |
151 | ||
152 | if (ret) { | |
153 | dev_err(&pdev->dev, "failed to write\n"); | |
154 | goto err; | |
155 | } | |
156 | ||
157 | ret = gpiochip_add(&dev->gpio_chip); | |
158 | if (ret) | |
159 | goto err; | |
160 | ||
161 | platform_set_drvdata(pdev, dev); | |
162 | return 0; | |
163 | ||
164 | err: | |
165 | kfree(dev); | |
166 | return ret; | |
167 | } | |
168 | ||
169 | static int __devexit adp5520_gpio_remove(struct platform_device *pdev) | |
170 | { | |
171 | struct adp5520_gpio *dev; | |
172 | int ret; | |
173 | ||
174 | dev = platform_get_drvdata(pdev); | |
175 | ret = gpiochip_remove(&dev->gpio_chip); | |
176 | if (ret) { | |
177 | dev_err(&pdev->dev, "%s failed, %d\n", | |
178 | "gpiochip_remove()", ret); | |
179 | return ret; | |
180 | } | |
181 | ||
182 | kfree(dev); | |
183 | return 0; | |
184 | } | |
185 | ||
186 | static struct platform_driver adp5520_gpio_driver = { | |
187 | .driver = { | |
188 | .name = "adp5520-gpio", | |
189 | .owner = THIS_MODULE, | |
190 | }, | |
191 | .probe = adp5520_gpio_probe, | |
192 | .remove = __devexit_p(adp5520_gpio_remove), | |
193 | }; | |
194 | ||
195 | static int __init adp5520_gpio_init(void) | |
196 | { | |
197 | return platform_driver_register(&adp5520_gpio_driver); | |
198 | } | |
199 | module_init(adp5520_gpio_init); | |
200 | ||
201 | static void __exit adp5520_gpio_exit(void) | |
202 | { | |
203 | platform_driver_unregister(&adp5520_gpio_driver); | |
204 | } | |
205 | module_exit(adp5520_gpio_exit); | |
206 | ||
207 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | |
208 | MODULE_DESCRIPTION("GPIO ADP5520 Driver"); | |
209 | MODULE_LICENSE("GPL"); | |
210 | MODULE_ALIAS("platform:adp5520-gpio"); |