1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for viafb GPIO ports.
5 * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
8 #include <linux/spinlock.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/gpio/machine.h>
11 #include <linux/platform_device.h>
12 #include <linux/via-core.h>
13 #include <linux/export.h>
17 * The ports we know about. Note that the port-25 gpios are not
18 * mentioned in the datasheet.
22 char *vg_name
; /* Data sheet name */
28 static struct viafb_gpio viafb_all_gpios
[] = {
30 .vg_name
= "VGPIO0", /* Guess - not in datasheet */
32 .vg_port_index
= 0x25,
38 .vg_port_index
= 0x25,
42 .vg_name
= "VGPIO2", /* aka DISPCLKI0 */
44 .vg_port_index
= 0x2c,
48 .vg_name
= "VGPIO3", /* aka DISPCLKO0 */
50 .vg_port_index
= 0x2c,
54 .vg_name
= "VGPIO4", /* DISPCLKI1 */
56 .vg_port_index
= 0x3d,
60 .vg_name
= "VGPIO5", /* DISPCLKO1 */
62 .vg_port_index
= 0x3d,
67 #define VIAFB_NUM_GPIOS ARRAY_SIZE(viafb_all_gpios)
70 * This structure controls the active GPIOs, which may be a subset
71 * of those which are known.
74 struct viafb_gpio_cfg
{
75 struct gpio_chip gpio_chip
;
76 struct viafb_dev
*vdev
;
77 struct viafb_gpio
*active_gpios
[VIAFB_NUM_GPIOS
];
78 const char *gpio_names
[VIAFB_NUM_GPIOS
];
82 * GPIO access functions
84 static void via_gpio_set(struct gpio_chip
*chip
, unsigned int nr
,
87 struct viafb_gpio_cfg
*cfg
= gpiochip_get_data(chip
);
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
= gpiochip_get_data(chip
);
118 struct viafb_gpio
*gpio
;
121 spin_lock_irqsave(&cfg
->vdev
->reg_lock
, flags
);
122 gpio
= cfg
->active_gpios
[nr
];
123 via_write_reg_mask(VIASR
, gpio
->vg_port_index
, 0,
124 0x40 << gpio
->vg_mask_shift
);
125 spin_unlock_irqrestore(&cfg
->vdev
->reg_lock
, flags
);
129 static int via_gpio_get(struct gpio_chip
*chip
, unsigned int nr
)
131 struct viafb_gpio_cfg
*cfg
= gpiochip_get_data(chip
);
133 struct viafb_gpio
*gpio
;
136 spin_lock_irqsave(&cfg
->vdev
->reg_lock
, flags
);
137 gpio
= cfg
->active_gpios
[nr
];
138 reg
= via_read_reg(VIASR
, gpio
->vg_port_index
);
139 spin_unlock_irqrestore(&cfg
->vdev
->reg_lock
, flags
);
140 return !!(reg
& (0x04 << gpio
->vg_mask_shift
));
144 static struct viafb_gpio_cfg viafb_gpio_config
= {
146 .label
= "VIAFB onboard GPIO",
147 .owner
= THIS_MODULE
,
148 .direction_output
= via_gpio_dir_out
,
150 .direction_input
= via_gpio_dir_input
,
159 * Manage the software enable bit.
161 static void viafb_gpio_enable(struct viafb_gpio
*gpio
)
163 via_write_reg_mask(VIASR
, gpio
->vg_port_index
, 0x02, 0x02);
166 static void viafb_gpio_disable(struct viafb_gpio
*gpio
)
168 via_write_reg_mask(VIASR
, gpio
->vg_port_index
, 0, 0x02);
173 static int viafb_gpio_suspend(void *private)
178 static int viafb_gpio_resume(void *private)
182 for (i
= 0; i
< viafb_gpio_config
.gpio_chip
.ngpio
; i
+= 2)
183 viafb_gpio_enable(viafb_gpio_config
.active_gpios
[i
]);
187 static struct viafb_pm_hooks viafb_gpio_pm_hooks
= {
188 .suspend
= viafb_gpio_suspend
,
189 .resume
= viafb_gpio_resume
191 #endif /* CONFIG_PM */
193 static struct gpiod_lookup_table viafb_gpio_table
= {
194 .dev_id
= "viafb-camera",
196 GPIO_LOOKUP("via-gpio", 2, "VGPIO2", GPIO_ACTIVE_LOW
),
197 GPIO_LOOKUP("via-gpio", 3, "VGPIO3", GPIO_ACTIVE_HIGH
),
203 * Platform device stuff.
205 static int viafb_gpio_probe(struct platform_device
*platdev
)
207 struct viafb_dev
*vdev
= platdev
->dev
.platform_data
;
208 struct via_port_cfg
*port_cfg
= vdev
->port_cfg
;
209 int i
, ngpio
= 0, ret
;
210 struct viafb_gpio
*gpio
;
214 * Set up entries for all GPIOs which have been configured to
215 * operate as such (as opposed to as i2c ports).
217 for (i
= 0; i
< VIAFB_NUM_PORTS
; i
++) {
218 if (port_cfg
[i
].mode
!= VIA_MODE_GPIO
)
220 for (gpio
= viafb_all_gpios
;
221 gpio
< viafb_all_gpios
+ VIAFB_NUM_GPIOS
; gpio
++)
222 if (gpio
->vg_port_index
== port_cfg
[i
].ioport_index
) {
223 viafb_gpio_config
.active_gpios
[ngpio
] = gpio
;
224 viafb_gpio_config
.gpio_names
[ngpio
] =
229 viafb_gpio_config
.gpio_chip
.ngpio
= ngpio
;
230 viafb_gpio_config
.gpio_chip
.names
= viafb_gpio_config
.gpio_names
;
231 viafb_gpio_config
.vdev
= vdev
;
233 printk(KERN_INFO
"viafb: no GPIOs configured\n");
237 * Enable the ports. They come in pairs, with a single
238 * enable bit for both.
240 spin_lock_irqsave(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
241 for (i
= 0; i
< ngpio
; i
+= 2)
242 viafb_gpio_enable(viafb_gpio_config
.active_gpios
[i
]);
243 spin_unlock_irqrestore(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
247 viafb_gpio_config
.gpio_chip
.base
= -1; /* Dynamic */
248 viafb_gpio_config
.gpio_chip
.label
= "via-gpio";
249 ret
= gpiochip_add_data(&viafb_gpio_config
.gpio_chip
,
252 printk(KERN_ERR
"viafb: failed to add gpios (%d)\n", ret
);
253 viafb_gpio_config
.gpio_chip
.ngpio
= 0;
256 gpiod_add_lookup_table(&viafb_gpio_table
);
259 viafb_pm_register(&viafb_gpio_pm_hooks
);
265 static void viafb_gpio_remove(struct platform_device
*platdev
)
271 viafb_pm_unregister(&viafb_gpio_pm_hooks
);
277 if (viafb_gpio_config
.gpio_chip
.ngpio
> 0) {
278 gpiochip_remove(&viafb_gpio_config
.gpio_chip
);
283 spin_lock_irqsave(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
284 for (i
= 0; i
< viafb_gpio_config
.gpio_chip
.ngpio
; i
+= 2)
285 viafb_gpio_disable(viafb_gpio_config
.active_gpios
[i
]);
286 viafb_gpio_config
.gpio_chip
.ngpio
= 0;
287 spin_unlock_irqrestore(&viafb_gpio_config
.vdev
->reg_lock
, flags
);
290 static struct platform_driver via_gpio_driver
= {
292 .name
= "viafb-gpio",
294 .probe
= viafb_gpio_probe
,
295 .remove
= viafb_gpio_remove
,
298 int viafb_gpio_init(void)
300 return platform_driver_register(&via_gpio_driver
);
303 void viafb_gpio_exit(void)
305 platform_driver_unregister(&via_gpio_driver
);