2 * Support for viafb GPIO ports.
4 * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
5 * Distributable under version 2 of the GNU General Public License.
8 #include <linux/spinlock.h>
9 #include <linux/gpio.h>
10 #include <linux/platform_device.h>
16 * The ports we know about. Note that the port-25 gpios are not
17 * mentioned in the datasheet.
21 char *vg_name; /* Data sheet name */
27 static struct viafb_gpio viafb_all_gpios[] = {
29 .vg_name = "VGPIO0", /* Guess - not in datasheet */
31 .vg_port_index = 0x25,
37 .vg_port_index = 0x25,
41 .vg_name = "VGPIO2", /* aka DISPCLKI0 */
43 .vg_port_index = 0x2c,
47 .vg_name = "VGPIO3", /* aka DISPCLKO0 */
49 .vg_port_index = 0x2c,
53 .vg_name = "VGPIO4", /* DISPCLKI1 */
55 .vg_port_index = 0x3d,
59 .vg_name = "VGPIO5", /* DISPCLKO1 */
61 .vg_port_index = 0x3d,
66 #define VIAFB_NUM_GPIOS ARRAY_SIZE(viafb_all_gpios)
69 * This structure controls the active GPIOs, which may be a subset
70 * of those which are known.
73 struct viafb_gpio_cfg {
74 struct gpio_chip gpio_chip;
75 struct viafb_dev *vdev;
76 struct viafb_gpio *active_gpios[VIAFB_NUM_GPIOS];
77 char *gpio_names[VIAFB_NUM_GPIOS];
81 * GPIO access functions
83 static void via_gpio_set(struct gpio_chip *chip, unsigned int nr,
86 struct viafb_gpio_cfg *cfg = container_of(chip,
87 struct viafb_gpio_cfg,
90 struct viafb_gpio *gpio;
93 spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
94 gpio = cfg->active_gpios[nr];
95 reg = via_read_reg(VIASR, gpio->vg_port_index);
96 reg |= 0x40 << gpio->vg_mask_shift; /* output enable */
98 reg |= 0x10 << gpio->vg_mask_shift;
100 reg &= ~(0x10 << gpio->vg_mask_shift);
101 via_write_reg(VIASR, gpio->vg_port_index, reg);
102 spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
105 static int via_gpio_dir_out(struct gpio_chip *chip, unsigned int nr,
108 via_gpio_set(chip, nr, value);
113 * Set the input direction. I'm not sure this is right; we should
114 * be able to do input without disabling output.
116 static int via_gpio_dir_input(struct gpio_chip *chip, unsigned int nr)
118 struct viafb_gpio_cfg *cfg = container_of(chip,
119 struct viafb_gpio_cfg,
121 struct viafb_gpio *gpio;
124 spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
125 gpio = cfg->active_gpios[nr];
126 via_write_reg_mask(VIASR, gpio->vg_port_index, 0,
127 0x40 << gpio->vg_mask_shift);
128 spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
132 static int via_gpio_get(struct gpio_chip *chip, unsigned int nr)
134 struct viafb_gpio_cfg *cfg = container_of(chip,
135 struct viafb_gpio_cfg,
138 struct viafb_gpio *gpio;
141 spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
142 gpio = cfg->active_gpios[nr];
143 reg = via_read_reg(VIASR, gpio->vg_port_index);
144 spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
145 return reg & (0x04 << gpio->vg_mask_shift);
149 static struct viafb_gpio_cfg gpio_config = {
151 .label = "VIAFB onboard GPIO",
152 .owner = THIS_MODULE,
153 .direction_output = via_gpio_dir_out,
155 .direction_input = via_gpio_dir_input,
164 * Manage the software enable bit.
166 static void viafb_gpio_enable(struct viafb_gpio *gpio)
168 via_write_reg_mask(VIASR, gpio->vg_port_index, 0x02, 0x02);
171 static void viafb_gpio_disable(struct viafb_gpio *gpio)
173 via_write_reg_mask(VIASR, gpio->vg_port_index, 0, 0x02);
177 * Look up a specific gpio and return the number it was assigned.
179 int viafb_gpio_lookup(const char *name)
183 for (i = 0; i < gpio_config.gpio_chip.ngpio; i++)
184 if (!strcmp(name, gpio_config.active_gpios[i]->vg_name))
185 return gpio_config.gpio_chip.base + i;
188 EXPORT_SYMBOL_GPL(viafb_gpio_lookup);
191 * Platform device stuff.
193 static __devinit int viafb_gpio_probe(struct platform_device *platdev)
195 struct viafb_dev *vdev = platdev->dev.platform_data;
196 struct via_port_cfg *port_cfg = vdev->port_cfg;
197 int i, ngpio = 0, ret;
198 struct viafb_gpio *gpio;
202 * Set up entries for all GPIOs which have been configured to
203 * operate as such (as opposed to as i2c ports).
205 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
206 if (port_cfg[i].mode != VIA_MODE_GPIO)
208 for (gpio = viafb_all_gpios;
209 gpio < viafb_all_gpios + VIAFB_NUM_GPIOS; gpio++)
210 if (gpio->vg_port_index == port_cfg[i].ioport_index) {
211 gpio_config.active_gpios[ngpio] = gpio;
212 gpio_config.gpio_names[ngpio] = gpio->vg_name;
216 gpio_config.gpio_chip.ngpio = ngpio;
217 gpio_config.gpio_chip.names = gpio_config.gpio_names;
218 gpio_config.vdev = vdev;
220 printk(KERN_INFO "viafb: no GPIOs configured\n");
224 * Enable the ports. They come in pairs, with a single
225 * enable bit for both.
227 spin_lock_irqsave(&gpio_config.vdev->reg_lock, flags);
228 for (i = 0; i < ngpio; i += 2)
229 viafb_gpio_enable(gpio_config.active_gpios[i]);
230 spin_unlock_irqrestore(&gpio_config.vdev->reg_lock, flags);
234 gpio_config.gpio_chip.base = -1; /* Dynamic */
235 ret = gpiochip_add(&gpio_config.gpio_chip);
237 printk(KERN_ERR "viafb: failed to add gpios (%d)\n", ret);
238 gpio_config.gpio_chip.ngpio = 0;
244 static int viafb_gpio_remove(struct platform_device *platdev)
249 spin_lock_irqsave(&gpio_config.vdev->reg_lock, flags);
253 if (gpio_config.gpio_chip.ngpio > 0) {
254 ret = gpiochip_remove(&gpio_config.gpio_chip);
255 if (ret) { /* Somebody still using it? */
256 printk(KERN_ERR "Viafb: GPIO remove failed\n");
263 for (i = 0; i < gpio_config.gpio_chip.ngpio; i += 2)
264 viafb_gpio_disable(gpio_config.active_gpios[i]);
265 gpio_config.gpio_chip.ngpio = 0;
267 spin_unlock_irqrestore(&gpio_config.vdev->reg_lock, flags);
271 static struct platform_driver via_gpio_driver = {
273 .name = "viafb-gpio",
275 .probe = viafb_gpio_probe,
276 .remove = viafb_gpio_remove,
279 int viafb_gpio_init(void)
281 return platform_driver_register(&via_gpio_driver);
284 void viafb_gpio_exit(void)
286 platform_driver_unregister(&via_gpio_driver);