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>
11 #include <linux/via-core.h>
12 #include <linux/via-gpio.h>
15 * The ports we know about. Note that the port-25 gpios are not
16 * mentioned in the datasheet.
20 char *vg_name
; /* Data sheet name */
26 static struct viafb_gpio viafb_all_gpios
[] = {
28 .vg_name
= "VGPIO0", /* Guess - not in datasheet */
30 .vg_port_index
= 0x25,
36 .vg_port_index
= 0x25,
40 .vg_name
= "VGPIO2", /* aka DISPCLKI0 */
42 .vg_port_index
= 0x2c,
46 .vg_name
= "VGPIO3", /* aka DISPCLKO0 */
48 .vg_port_index
= 0x2c,
52 .vg_name
= "VGPIO4", /* DISPCLKI1 */
54 .vg_port_index
= 0x3d,
58 .vg_name
= "VGPIO5", /* DISPCLKO1 */
60 .vg_port_index
= 0x3d,
65 #define VIAFB_NUM_GPIOS ARRAY_SIZE(viafb_all_gpios)
68 * This structure controls the active GPIOs, which may be a subset
69 * of those which are known.
72 struct viafb_gpio_cfg
{
73 struct gpio_chip gpio_chip
;
74 struct viafb_dev
*vdev
;
75 struct viafb_gpio
*active_gpios
[VIAFB_NUM_GPIOS
];
76 const char *gpio_names
[VIAFB_NUM_GPIOS
];
80 * GPIO access functions
82 static void via_gpio_set(struct gpio_chip
*chip
, unsigned int nr
,
85 struct viafb_gpio_cfg
*cfg
= container_of(chip
,
86 struct viafb_gpio_cfg
,
89 struct viafb_gpio
*gpio
;
92 spin_lock_irqsave(&cfg
->vdev
->reg_lock
, flags
);
93 gpio
= cfg
->active_gpios
[nr
];
94 reg
= via_read_reg(VIASR
, gpio
->vg_port_index
);
95 reg
|= 0x40 << gpio
->vg_mask_shift
; /* output enable */
97 reg
|= 0x10 << gpio
->vg_mask_shift
;
99 reg
&= ~(0x10 << gpio
->vg_mask_shift
);
100 via_write_reg(VIASR
, gpio
->vg_port_index
, reg
);
101 spin_unlock_irqrestore(&cfg
->vdev
->reg_lock
, flags
);
104 static int via_gpio_dir_out(struct gpio_chip
*chip
, unsigned int nr
,
107 via_gpio_set(chip
, nr
, value
);
112 * Set the input direction. I'm not sure this is right; we should
113 * be able to do input without disabling output.
115 static int via_gpio_dir_input(struct gpio_chip
*chip
, unsigned int nr
)
117 struct viafb_gpio_cfg
*cfg
= container_of(chip
,
118 struct viafb_gpio_cfg
,
120 struct viafb_gpio
*gpio
;
123 spin_lock_irqsave(&cfg
->vdev
->reg_lock
, flags
);
124 gpio
= cfg
->active_gpios
[nr
];
125 via_write_reg_mask(VIASR
, gpio
->vg_port_index
, 0,
126 0x40 << gpio
->vg_mask_shift
);
127 spin_unlock_irqrestore(&cfg
->vdev
->reg_lock
, flags
);
131 static int via_gpio_get(struct gpio_chip
*chip
, unsigned int nr
)
133 struct viafb_gpio_cfg
*cfg
= container_of(chip
,
134 struct viafb_gpio_cfg
,
137 struct viafb_gpio
*gpio
;
140 spin_lock_irqsave(&cfg
->vdev
->reg_lock
, flags
);
141 gpio
= cfg
->active_gpios
[nr
];
142 reg
= via_read_reg(VIASR
, gpio
->vg_port_index
);
143 spin_unlock_irqrestore(&cfg
->vdev
->reg_lock
, flags
);
144 return reg
& (0x04 << gpio
->vg_mask_shift
);
148 static struct viafb_gpio_cfg viafb_gpio_config
= {
150 .label
= "VIAFB onboard GPIO",
151 .owner
= THIS_MODULE
,
152 .direction_output
= via_gpio_dir_out
,
154 .direction_input
= via_gpio_dir_input
,
163 * Manage the software enable bit.
165 static void viafb_gpio_enable(struct viafb_gpio
*gpio
)
167 via_write_reg_mask(VIASR
, gpio
->vg_port_index
, 0x02, 0x02);
170 static void viafb_gpio_disable(struct viafb_gpio
*gpio
)
172 via_write_reg_mask(VIASR
, gpio
->vg_port_index
, 0, 0x02);
177 static int viafb_gpio_suspend(void *private)
182 static int viafb_gpio_resume(void *private)
186 for (i
= 0; i
< viafb_gpio_config
.gpio_chip
.ngpio
; i
+= 2)
187 viafb_gpio_enable(viafb_gpio_config
.active_gpios
[i
]);
191 static struct viafb_pm_hooks viafb_gpio_pm_hooks
= {
192 .suspend
= viafb_gpio_suspend
,
193 .resume
= viafb_gpio_resume
195 #endif /* CONFIG_PM */
198 * Look up a specific gpio and return the number it was assigned.
200 int viafb_gpio_lookup(const char *name
)
204 for (i
= 0; i
< viafb_gpio_config
.gpio_chip
.ngpio
; i
++)
205 if (!strcmp(name
, viafb_gpio_config
.active_gpios
[i
]->vg_name
))
206 return viafb_gpio_config
.gpio_chip
.base
+ i
;
209 EXPORT_SYMBOL_GPL(viafb_gpio_lookup
);
212 * Platform device stuff.
214 static __devinit
int viafb_gpio_probe(struct platform_device
*platdev
)
216 struct viafb_dev
*vdev
= platdev
->dev
.platform_data
;
217 struct via_port_cfg
*port_cfg
= vdev
->port_cfg
;
218 int i
, ngpio
= 0, ret
;
219 struct viafb_gpio
*gpio
;
223 * Set up entries for all GPIOs which have been configured to
224 * operate as such (as opposed to as i2c ports).
226 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
227 if (port_cfg
[i
].mode
!= VIA_MODE_GPIO
)
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 viafb_gpio_config
.active_gpios
[ngpio
] = gpio
;
233 viafb_gpio_config
.gpio_names
[ngpio
] =
238 viafb_gpio_config
.gpio_chip
.ngpio
= ngpio
;
239 viafb_gpio_config
.gpio_chip
.names
= viafb_gpio_config
.gpio_names
;
240 viafb_gpio_config
.vdev
= vdev
;
242 printk(KERN_INFO
"viafb: no GPIOs configured\n");
246 * Enable the ports. They come in pairs, with a single
247 * enable bit for both.
249 spin_lock_irqsave(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
250 for (i
= 0; i
< ngpio
; i
+= 2)
251 viafb_gpio_enable(viafb_gpio_config
.active_gpios
[i
]);
252 spin_unlock_irqrestore(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
256 viafb_gpio_config
.gpio_chip
.base
= -1; /* Dynamic */
257 ret
= gpiochip_add(&viafb_gpio_config
.gpio_chip
);
259 printk(KERN_ERR
"viafb: failed to add gpios (%d)\n", ret
);
260 viafb_gpio_config
.gpio_chip
.ngpio
= 0;
263 viafb_pm_register(&viafb_gpio_pm_hooks
);
269 static int viafb_gpio_remove(struct platform_device
*platdev
)
275 viafb_pm_unregister(&viafb_gpio_pm_hooks
);
281 if (viafb_gpio_config
.gpio_chip
.ngpio
> 0) {
282 ret
= gpiochip_remove(&viafb_gpio_config
.gpio_chip
);
283 if (ret
) { /* Somebody still using it? */
284 printk(KERN_ERR
"Viafb: GPIO remove failed\n");
291 spin_lock_irqsave(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
292 for (i
= 0; i
< viafb_gpio_config
.gpio_chip
.ngpio
; i
+= 2)
293 viafb_gpio_disable(viafb_gpio_config
.active_gpios
[i
]);
294 viafb_gpio_config
.gpio_chip
.ngpio
= 0;
295 spin_unlock_irqrestore(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
299 static struct platform_driver via_gpio_driver
= {
301 .name
= "viafb-gpio",
303 .probe
= viafb_gpio_probe
,
304 .remove
= viafb_gpio_remove
,
307 int viafb_gpio_init(void)
309 return platform_driver_register(&via_gpio_driver
);
312 void viafb_gpio_exit(void)
314 platform_driver_unregister(&via_gpio_driver
);