blob: c2a0a1cfd3b3fe6c9f2da44e8574fe9a20591be5 [file] [log] [blame]
Jonathan Corbet7e0de022009-12-01 20:39:57 -07001/*
2 * Support for viafb GPIO ports.
3 *
4 * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
5 * Distributable under version 2 of the GNU General Public License.
6 */
7
8#include <linux/spinlock.h>
9#include <linux/gpio.h>
Jonathan Corbet7582eb92010-04-22 17:39:34 -060010#include <linux/platform_device.h>
Jonathan Corbetec668412010-05-05 14:44:55 -060011#include <linux/via-core.h>
12#include <linux/via-gpio.h>
Jonathan Corbet7e0de022009-12-01 20:39:57 -070013
14/*
15 * The ports we know about. Note that the port-25 gpios are not
16 * mentioned in the datasheet.
17 */
18
19struct viafb_gpio {
20 char *vg_name; /* Data sheet name */
21 u16 vg_io_port;
22 u8 vg_port_index;
23 int vg_mask_shift;
24};
25
26static struct viafb_gpio viafb_all_gpios[] = {
27 {
28 .vg_name = "VGPIO0", /* Guess - not in datasheet */
29 .vg_io_port = VIASR,
30 .vg_port_index = 0x25,
31 .vg_mask_shift = 1
32 },
33 {
34 .vg_name = "VGPIO1",
35 .vg_io_port = VIASR,
36 .vg_port_index = 0x25,
37 .vg_mask_shift = 0
38 },
39 {
40 .vg_name = "VGPIO2", /* aka DISPCLKI0 */
41 .vg_io_port = VIASR,
42 .vg_port_index = 0x2c,
43 .vg_mask_shift = 1
44 },
45 {
46 .vg_name = "VGPIO3", /* aka DISPCLKO0 */
47 .vg_io_port = VIASR,
48 .vg_port_index = 0x2c,
49 .vg_mask_shift = 0
50 },
51 {
52 .vg_name = "VGPIO4", /* DISPCLKI1 */
53 .vg_io_port = VIASR,
54 .vg_port_index = 0x3d,
55 .vg_mask_shift = 1
56 },
57 {
58 .vg_name = "VGPIO5", /* DISPCLKO1 */
59 .vg_io_port = VIASR,
60 .vg_port_index = 0x3d,
61 .vg_mask_shift = 0
62 },
63};
64
65#define VIAFB_NUM_GPIOS ARRAY_SIZE(viafb_all_gpios)
66
67/*
68 * This structure controls the active GPIOs, which may be a subset
69 * of those which are known.
70 */
71
72struct viafb_gpio_cfg {
73 struct gpio_chip gpio_chip;
74 struct viafb_dev *vdev;
75 struct viafb_gpio *active_gpios[VIAFB_NUM_GPIOS];
Andrew Mortonf2709832010-06-29 14:09:43 -070076 const char *gpio_names[VIAFB_NUM_GPIOS];
Jonathan Corbet7e0de022009-12-01 20:39:57 -070077};
78
79/*
80 * GPIO access functions
81 */
82static void via_gpio_set(struct gpio_chip *chip, unsigned int nr,
83 int value)
84{
85 struct viafb_gpio_cfg *cfg = container_of(chip,
86 struct viafb_gpio_cfg,
87 gpio_chip);
88 u8 reg;
89 struct viafb_gpio *gpio;
90 unsigned long flags;
91
92 spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
93 gpio = cfg->active_gpios[nr];
Jonathan Corbet75b035a2010-04-22 14:36:04 -060094 reg = via_read_reg(VIASR, gpio->vg_port_index);
Jonathan Corbet7e0de022009-12-01 20:39:57 -070095 reg |= 0x40 << gpio->vg_mask_shift; /* output enable */
96 if (value)
97 reg |= 0x10 << gpio->vg_mask_shift;
98 else
99 reg &= ~(0x10 << gpio->vg_mask_shift);
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600100 via_write_reg(VIASR, gpio->vg_port_index, reg);
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700101 spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
102}
103
104static int via_gpio_dir_out(struct gpio_chip *chip, unsigned int nr,
105 int value)
106{
107 via_gpio_set(chip, nr, value);
108 return 0;
109}
110
111/*
112 * Set the input direction. I'm not sure this is right; we should
113 * be able to do input without disabling output.
114 */
115static int via_gpio_dir_input(struct gpio_chip *chip, unsigned int nr)
116{
117 struct viafb_gpio_cfg *cfg = container_of(chip,
118 struct viafb_gpio_cfg,
119 gpio_chip);
120 struct viafb_gpio *gpio;
121 unsigned long flags;
122
123 spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
124 gpio = cfg->active_gpios[nr];
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600125 via_write_reg_mask(VIASR, gpio->vg_port_index, 0,
126 0x40 << gpio->vg_mask_shift);
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700127 spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
128 return 0;
129}
130
131static int via_gpio_get(struct gpio_chip *chip, unsigned int nr)
132{
133 struct viafb_gpio_cfg *cfg = container_of(chip,
134 struct viafb_gpio_cfg,
135 gpio_chip);
136 u8 reg;
137 struct viafb_gpio *gpio;
138 unsigned long flags;
139
140 spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
141 gpio = cfg->active_gpios[nr];
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600142 reg = via_read_reg(VIASR, gpio->vg_port_index);
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700143 spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
144 return reg & (0x04 << gpio->vg_mask_shift);
145}
146
147
148static struct viafb_gpio_cfg gpio_config = {
149 .gpio_chip = {
150 .label = "VIAFB onboard GPIO",
151 .owner = THIS_MODULE,
152 .direction_output = via_gpio_dir_out,
153 .set = via_gpio_set,
154 .direction_input = via_gpio_dir_input,
155 .get = via_gpio_get,
156 .base = -1,
157 .ngpio = 0,
158 .can_sleep = 0
159 }
160};
161
162/*
163 * Manage the software enable bit.
164 */
165static void viafb_gpio_enable(struct viafb_gpio *gpio)
166{
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600167 via_write_reg_mask(VIASR, gpio->vg_port_index, 0x02, 0x02);
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700168}
169
170static void viafb_gpio_disable(struct viafb_gpio *gpio)
171{
Jonathan Corbet75b035a2010-04-22 14:36:04 -0600172 via_write_reg_mask(VIASR, gpio->vg_port_index, 0, 0x02);
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700173}
174
Jonathan Corbet67eb6f92010-10-28 18:23:21 +0100175#ifdef CONFIG_PM
176
177static int viafb_gpio_suspend(void *private)
178{
179 return 0;
180}
181
182static int viafb_gpio_resume(void *private)
183{
184 int i;
185
186 for (i = 0; i < gpio_config.gpio_chip.ngpio; i += 2)
187 viafb_gpio_enable(gpio_config.active_gpios[i]);
188 return 0;
189}
190
191static struct viafb_pm_hooks viafb_gpio_pm_hooks = {
192 .suspend = viafb_gpio_suspend,
193 .resume = viafb_gpio_resume
194};
195#endif /* CONFIG_PM */
196
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600197/*
198 * Look up a specific gpio and return the number it was assigned.
199 */
200int viafb_gpio_lookup(const char *name)
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700201{
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600202 int i;
203
204 for (i = 0; i < gpio_config.gpio_chip.ngpio; i++)
205 if (!strcmp(name, gpio_config.active_gpios[i]->vg_name))
206 return gpio_config.gpio_chip.base + i;
207 return -1;
208}
209EXPORT_SYMBOL_GPL(viafb_gpio_lookup);
210
211/*
212 * Platform device stuff.
213 */
214static __devinit int viafb_gpio_probe(struct platform_device *platdev)
215{
216 struct viafb_dev *vdev = platdev->dev.platform_data;
217 struct via_port_cfg *port_cfg = vdev->port_cfg;
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700218 int i, ngpio = 0, ret;
219 struct viafb_gpio *gpio;
220 unsigned long flags;
221
222 /*
223 * Set up entries for all GPIOs which have been configured to
224 * operate as such (as opposed to as i2c ports).
225 */
226 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
227 if (port_cfg[i].mode != VIA_MODE_GPIO)
228 continue;
229 for (gpio = viafb_all_gpios;
230 gpio < viafb_all_gpios + VIAFB_NUM_GPIOS; gpio++)
231 if (gpio->vg_port_index == port_cfg[i].ioport_index) {
232 gpio_config.active_gpios[ngpio] = gpio;
233 gpio_config.gpio_names[ngpio] = gpio->vg_name;
234 ngpio++;
235 }
236 }
237 gpio_config.gpio_chip.ngpio = ngpio;
238 gpio_config.gpio_chip.names = gpio_config.gpio_names;
239 gpio_config.vdev = vdev;
240 if (ngpio == 0) {
241 printk(KERN_INFO "viafb: no GPIOs configured\n");
242 return 0;
243 }
244 /*
245 * Enable the ports. They come in pairs, with a single
246 * enable bit for both.
247 */
248 spin_lock_irqsave(&gpio_config.vdev->reg_lock, flags);
249 for (i = 0; i < ngpio; i += 2)
250 viafb_gpio_enable(gpio_config.active_gpios[i]);
251 spin_unlock_irqrestore(&gpio_config.vdev->reg_lock, flags);
252 /*
253 * Get registered.
254 */
255 gpio_config.gpio_chip.base = -1; /* Dynamic */
256 ret = gpiochip_add(&gpio_config.gpio_chip);
257 if (ret) {
258 printk(KERN_ERR "viafb: failed to add gpios (%d)\n", ret);
259 gpio_config.gpio_chip.ngpio = 0;
260 }
Jonathan Corbet67eb6f92010-10-28 18:23:21 +0100261#ifdef CONFIG_PM
262 viafb_pm_register(&viafb_gpio_pm_hooks);
263#endif
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700264 return ret;
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700265}
266
267
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600268static int viafb_gpio_remove(struct platform_device *platdev)
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700269{
270 unsigned long flags;
271 int ret = 0, i;
272
Jonathan Corbet67eb6f92010-10-28 18:23:21 +0100273#ifdef CONFIG_PM
274 viafb_pm_unregister(&viafb_gpio_pm_hooks);
275#endif
276
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700277 /*
278 * Get unregistered.
279 */
280 if (gpio_config.gpio_chip.ngpio > 0) {
281 ret = gpiochip_remove(&gpio_config.gpio_chip);
282 if (ret) { /* Somebody still using it? */
283 printk(KERN_ERR "Viafb: GPIO remove failed\n");
Jonathan Corbet27fe9712010-04-30 09:45:51 -0600284 return ret;
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700285 }
286 }
287 /*
288 * Disable the ports.
289 */
Jonathan Corbet27fe9712010-04-30 09:45:51 -0600290 spin_lock_irqsave(&gpio_config.vdev->reg_lock, flags);
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700291 for (i = 0; i < gpio_config.gpio_chip.ngpio; i += 2)
292 viafb_gpio_disable(gpio_config.active_gpios[i]);
293 gpio_config.gpio_chip.ngpio = 0;
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700294 spin_unlock_irqrestore(&gpio_config.vdev->reg_lock, flags);
295 return ret;
296}
297
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600298static struct platform_driver via_gpio_driver = {
299 .driver = {
300 .name = "viafb-gpio",
301 },
302 .probe = viafb_gpio_probe,
303 .remove = viafb_gpio_remove,
304};
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700305
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600306int viafb_gpio_init(void)
307{
308 return platform_driver_register(&via_gpio_driver);
Jonathan Corbet7e0de022009-12-01 20:39:57 -0700309}
Jonathan Corbet7582eb92010-04-22 17:39:34 -0600310
311void viafb_gpio_exit(void)
312{
313 platform_driver_unregister(&via_gpio_driver);
314}