1 // SPDX-License-Identifier: GPL-2.0+
3 * Nano River Technologies viperboard GPIO lib driver
5 * (C) 2012 by Lemonage GmbH
6 * Author: Lars Poeschel <poeschel@lemonage.de>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/usb.h>
18 #include <linux/gpio/driver.h>
20 #include <linux/mfd/viperboard.h>
22 #define VPRBRD_GPIOA_CLK_1MHZ 0
23 #define VPRBRD_GPIOA_CLK_100KHZ 1
24 #define VPRBRD_GPIOA_CLK_10KHZ 2
25 #define VPRBRD_GPIOA_CLK_1KHZ 3
26 #define VPRBRD_GPIOA_CLK_100HZ 4
27 #define VPRBRD_GPIOA_CLK_10HZ 5
29 #define VPRBRD_GPIOA_FREQ_DEFAULT 1000
31 #define VPRBRD_GPIOA_CMD_CONT 0x00
32 #define VPRBRD_GPIOA_CMD_PULSE 0x01
33 #define VPRBRD_GPIOA_CMD_PWM 0x02
34 #define VPRBRD_GPIOA_CMD_SETOUT 0x03
35 #define VPRBRD_GPIOA_CMD_SETIN 0x04
36 #define VPRBRD_GPIOA_CMD_SETINT 0x05
37 #define VPRBRD_GPIOA_CMD_GETIN 0x06
39 #define VPRBRD_GPIOB_CMD_SETDIR 0x00
40 #define VPRBRD_GPIOB_CMD_SETVAL 0x01
42 struct vprbrd_gpioa_msg
{
56 struct vprbrd_gpiob_msg
{
63 struct gpio_chip gpioa
; /* gpio a related things */
66 struct gpio_chip gpiob
; /* gpio b related things */
72 /* gpioa sampling clock module parameter */
73 static unsigned char gpioa_clk
;
74 static unsigned int gpioa_freq
= VPRBRD_GPIOA_FREQ_DEFAULT
;
75 module_param(gpioa_freq
, uint
, 0);
76 MODULE_PARM_DESC(gpioa_freq
,
77 "gpio-a sampling freq in Hz (default is 1000Hz) valid values: 10, 100, 1000, 10000, 100000, 1000000");
79 /* ----- begin of gipo a chip -------------------------------------------- */
81 static int vprbrd_gpioa_get(struct gpio_chip
*chip
,
84 int ret
, answer
, error
= 0;
85 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
86 struct vprbrd
*vb
= gpio
->vb
;
87 struct vprbrd_gpioa_msg
*gamsg
= (struct vprbrd_gpioa_msg
*)vb
->buf
;
89 /* if io is set to output, just return the saved value */
90 if (gpio
->gpioa_out
& (1 << offset
))
91 return !!(gpio
->gpioa_val
& (1 << offset
));
93 mutex_lock(&vb
->lock
);
95 gamsg
->cmd
= VPRBRD_GPIOA_CMD_GETIN
;
97 gamsg
->offset
= offset
;
100 gamsg
->invert
= 0x00;
101 gamsg
->pwmlevel
= 0x00;
102 gamsg
->outval
= 0x00;
103 gamsg
->risefall
= 0x00;
104 gamsg
->answer
= 0x00;
105 gamsg
->__fill
= 0x00;
107 ret
= usb_control_msg(vb
->usb_dev
, usb_sndctrlpipe(vb
->usb_dev
, 0),
108 VPRBRD_USB_REQUEST_GPIOA
, VPRBRD_USB_TYPE_OUT
, 0x0000,
109 0x0000, gamsg
, sizeof(struct vprbrd_gpioa_msg
),
110 VPRBRD_USB_TIMEOUT_MS
);
111 if (ret
!= sizeof(struct vprbrd_gpioa_msg
))
114 ret
= usb_control_msg(vb
->usb_dev
, usb_rcvctrlpipe(vb
->usb_dev
, 0),
115 VPRBRD_USB_REQUEST_GPIOA
, VPRBRD_USB_TYPE_IN
, 0x0000,
116 0x0000, gamsg
, sizeof(struct vprbrd_gpioa_msg
),
117 VPRBRD_USB_TIMEOUT_MS
);
118 answer
= gamsg
->answer
& 0x01;
120 mutex_unlock(&vb
->lock
);
122 if (ret
!= sizeof(struct vprbrd_gpioa_msg
))
131 static void vprbrd_gpioa_set(struct gpio_chip
*chip
,
132 unsigned int offset
, int value
)
135 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
136 struct vprbrd
*vb
= gpio
->vb
;
137 struct vprbrd_gpioa_msg
*gamsg
= (struct vprbrd_gpioa_msg
*)vb
->buf
;
139 if (gpio
->gpioa_out
& (1 << offset
)) {
141 gpio
->gpioa_val
|= (1 << offset
);
143 gpio
->gpioa_val
&= ~(1 << offset
);
145 mutex_lock(&vb
->lock
);
147 gamsg
->cmd
= VPRBRD_GPIOA_CMD_SETOUT
;
149 gamsg
->offset
= offset
;
152 gamsg
->invert
= 0x00;
153 gamsg
->pwmlevel
= 0x00;
154 gamsg
->outval
= value
;
155 gamsg
->risefall
= 0x00;
156 gamsg
->answer
= 0x00;
157 gamsg
->__fill
= 0x00;
159 ret
= usb_control_msg(vb
->usb_dev
,
160 usb_sndctrlpipe(vb
->usb_dev
, 0),
161 VPRBRD_USB_REQUEST_GPIOA
, VPRBRD_USB_TYPE_OUT
,
162 0x0000, 0x0000, gamsg
,
163 sizeof(struct vprbrd_gpioa_msg
), VPRBRD_USB_TIMEOUT_MS
);
165 mutex_unlock(&vb
->lock
);
167 if (ret
!= sizeof(struct vprbrd_gpioa_msg
))
168 dev_err(chip
->parent
, "usb error setting pin value\n");
172 static int vprbrd_gpioa_direction_input(struct gpio_chip
*chip
,
176 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
177 struct vprbrd
*vb
= gpio
->vb
;
178 struct vprbrd_gpioa_msg
*gamsg
= (struct vprbrd_gpioa_msg
*)vb
->buf
;
180 gpio
->gpioa_out
&= ~(1 << offset
);
182 mutex_lock(&vb
->lock
);
184 gamsg
->cmd
= VPRBRD_GPIOA_CMD_SETIN
;
185 gamsg
->clk
= gpioa_clk
;
186 gamsg
->offset
= offset
;
189 gamsg
->invert
= 0x00;
190 gamsg
->pwmlevel
= 0x00;
191 gamsg
->outval
= 0x00;
192 gamsg
->risefall
= 0x00;
193 gamsg
->answer
= 0x00;
194 gamsg
->__fill
= 0x00;
196 ret
= usb_control_msg(vb
->usb_dev
, usb_sndctrlpipe(vb
->usb_dev
, 0),
197 VPRBRD_USB_REQUEST_GPIOA
, VPRBRD_USB_TYPE_OUT
, 0x0000,
198 0x0000, gamsg
, sizeof(struct vprbrd_gpioa_msg
),
199 VPRBRD_USB_TIMEOUT_MS
);
201 mutex_unlock(&vb
->lock
);
203 if (ret
!= sizeof(struct vprbrd_gpioa_msg
))
209 static int vprbrd_gpioa_direction_output(struct gpio_chip
*chip
,
210 unsigned int offset
, int value
)
213 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
214 struct vprbrd
*vb
= gpio
->vb
;
215 struct vprbrd_gpioa_msg
*gamsg
= (struct vprbrd_gpioa_msg
*)vb
->buf
;
217 gpio
->gpioa_out
|= (1 << offset
);
219 gpio
->gpioa_val
|= (1 << offset
);
221 gpio
->gpioa_val
&= ~(1 << offset
);
223 mutex_lock(&vb
->lock
);
225 gamsg
->cmd
= VPRBRD_GPIOA_CMD_SETOUT
;
227 gamsg
->offset
= offset
;
230 gamsg
->invert
= 0x00;
231 gamsg
->pwmlevel
= 0x00;
232 gamsg
->outval
= value
;
233 gamsg
->risefall
= 0x00;
234 gamsg
->answer
= 0x00;
235 gamsg
->__fill
= 0x00;
237 ret
= usb_control_msg(vb
->usb_dev
, usb_sndctrlpipe(vb
->usb_dev
, 0),
238 VPRBRD_USB_REQUEST_GPIOA
, VPRBRD_USB_TYPE_OUT
, 0x0000,
239 0x0000, gamsg
, sizeof(struct vprbrd_gpioa_msg
),
240 VPRBRD_USB_TIMEOUT_MS
);
242 mutex_unlock(&vb
->lock
);
244 if (ret
!= sizeof(struct vprbrd_gpioa_msg
))
250 /* ----- end of gpio a chip ---------------------------------------------- */
252 /* ----- begin of gipo b chip -------------------------------------------- */
254 static int vprbrd_gpiob_setdir(struct vprbrd
*vb
, unsigned int offset
,
257 struct vprbrd_gpiob_msg
*gbmsg
= (struct vprbrd_gpiob_msg
*)vb
->buf
;
260 gbmsg
->cmd
= VPRBRD_GPIOB_CMD_SETDIR
;
261 gbmsg
->val
= cpu_to_be16(dir
<< offset
);
262 gbmsg
->mask
= cpu_to_be16(0x0001 << offset
);
264 ret
= usb_control_msg(vb
->usb_dev
, usb_sndctrlpipe(vb
->usb_dev
, 0),
265 VPRBRD_USB_REQUEST_GPIOB
, VPRBRD_USB_TYPE_OUT
, 0x0000,
266 0x0000, gbmsg
, sizeof(struct vprbrd_gpiob_msg
),
267 VPRBRD_USB_TIMEOUT_MS
);
269 if (ret
!= sizeof(struct vprbrd_gpiob_msg
))
275 static int vprbrd_gpiob_get(struct gpio_chip
*chip
,
280 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
281 struct vprbrd
*vb
= gpio
->vb
;
282 struct vprbrd_gpiob_msg
*gbmsg
= (struct vprbrd_gpiob_msg
*)vb
->buf
;
284 /* if io is set to output, just return the saved value */
285 if (gpio
->gpiob_out
& (1 << offset
))
286 return gpio
->gpiob_val
& (1 << offset
);
288 mutex_lock(&vb
->lock
);
290 ret
= usb_control_msg(vb
->usb_dev
, usb_rcvctrlpipe(vb
->usb_dev
, 0),
291 VPRBRD_USB_REQUEST_GPIOB
, VPRBRD_USB_TYPE_IN
, 0x0000,
292 0x0000, gbmsg
, sizeof(struct vprbrd_gpiob_msg
),
293 VPRBRD_USB_TIMEOUT_MS
);
296 mutex_unlock(&vb
->lock
);
298 if (ret
!= sizeof(struct vprbrd_gpiob_msg
))
301 /* cache the read values */
302 gpio
->gpiob_val
= be16_to_cpu(val
);
304 return (gpio
->gpiob_val
>> offset
) & 0x1;
307 static void vprbrd_gpiob_set(struct gpio_chip
*chip
,
308 unsigned int offset
, int value
)
311 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
312 struct vprbrd
*vb
= gpio
->vb
;
313 struct vprbrd_gpiob_msg
*gbmsg
= (struct vprbrd_gpiob_msg
*)vb
->buf
;
315 if (gpio
->gpiob_out
& (1 << offset
)) {
317 gpio
->gpiob_val
|= (1 << offset
);
319 gpio
->gpiob_val
&= ~(1 << offset
);
321 mutex_lock(&vb
->lock
);
323 gbmsg
->cmd
= VPRBRD_GPIOB_CMD_SETVAL
;
324 gbmsg
->val
= cpu_to_be16(value
<< offset
);
325 gbmsg
->mask
= cpu_to_be16(0x0001 << offset
);
327 ret
= usb_control_msg(vb
->usb_dev
,
328 usb_sndctrlpipe(vb
->usb_dev
, 0),
329 VPRBRD_USB_REQUEST_GPIOB
, VPRBRD_USB_TYPE_OUT
,
330 0x0000, 0x0000, gbmsg
,
331 sizeof(struct vprbrd_gpiob_msg
), VPRBRD_USB_TIMEOUT_MS
);
333 mutex_unlock(&vb
->lock
);
335 if (ret
!= sizeof(struct vprbrd_gpiob_msg
))
336 dev_err(chip
->parent
, "usb error setting pin value\n");
340 static int vprbrd_gpiob_direction_input(struct gpio_chip
*chip
,
344 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
345 struct vprbrd
*vb
= gpio
->vb
;
347 gpio
->gpiob_out
&= ~(1 << offset
);
349 mutex_lock(&vb
->lock
);
351 ret
= vprbrd_gpiob_setdir(vb
, offset
, 0);
353 mutex_unlock(&vb
->lock
);
356 dev_err(chip
->parent
, "usb error setting pin to input\n");
361 static int vprbrd_gpiob_direction_output(struct gpio_chip
*chip
,
362 unsigned int offset
, int value
)
365 struct vprbrd_gpio
*gpio
= gpiochip_get_data(chip
);
366 struct vprbrd
*vb
= gpio
->vb
;
368 gpio
->gpiob_out
|= (1 << offset
);
370 mutex_lock(&vb
->lock
);
372 ret
= vprbrd_gpiob_setdir(vb
, offset
, 1);
374 dev_err(chip
->parent
, "usb error setting pin to output\n");
376 mutex_unlock(&vb
->lock
);
378 vprbrd_gpiob_set(chip
, offset
, value
);
383 /* ----- end of gpio b chip ---------------------------------------------- */
385 static int vprbrd_gpio_probe(struct platform_device
*pdev
)
387 struct vprbrd
*vb
= dev_get_drvdata(pdev
->dev
.parent
);
388 struct vprbrd_gpio
*vb_gpio
;
391 vb_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*vb_gpio
), GFP_KERNEL
);
396 /* registering gpio a */
397 vb_gpio
->gpioa
.label
= "viperboard gpio a";
398 vb_gpio
->gpioa
.parent
= &pdev
->dev
;
399 vb_gpio
->gpioa
.owner
= THIS_MODULE
;
400 vb_gpio
->gpioa
.base
= -1;
401 vb_gpio
->gpioa
.ngpio
= 16;
402 vb_gpio
->gpioa
.can_sleep
= true;
403 vb_gpio
->gpioa
.set
= vprbrd_gpioa_set
;
404 vb_gpio
->gpioa
.get
= vprbrd_gpioa_get
;
405 vb_gpio
->gpioa
.direction_input
= vprbrd_gpioa_direction_input
;
406 vb_gpio
->gpioa
.direction_output
= vprbrd_gpioa_direction_output
;
407 ret
= devm_gpiochip_add_data(&pdev
->dev
, &vb_gpio
->gpioa
, vb_gpio
);
409 dev_err(vb_gpio
->gpioa
.parent
, "could not add gpio a");
413 /* registering gpio b */
414 vb_gpio
->gpiob
.label
= "viperboard gpio b";
415 vb_gpio
->gpiob
.parent
= &pdev
->dev
;
416 vb_gpio
->gpiob
.owner
= THIS_MODULE
;
417 vb_gpio
->gpiob
.base
= -1;
418 vb_gpio
->gpiob
.ngpio
= 16;
419 vb_gpio
->gpiob
.can_sleep
= true;
420 vb_gpio
->gpiob
.set
= vprbrd_gpiob_set
;
421 vb_gpio
->gpiob
.get
= vprbrd_gpiob_get
;
422 vb_gpio
->gpiob
.direction_input
= vprbrd_gpiob_direction_input
;
423 vb_gpio
->gpiob
.direction_output
= vprbrd_gpiob_direction_output
;
424 ret
= devm_gpiochip_add_data(&pdev
->dev
, &vb_gpio
->gpiob
, vb_gpio
);
426 dev_err(vb_gpio
->gpiob
.parent
, "could not add gpio b");
430 platform_set_drvdata(pdev
, vb_gpio
);
435 static struct platform_driver vprbrd_gpio_driver
= {
436 .driver
.name
= "viperboard-gpio",
437 .probe
= vprbrd_gpio_probe
,
440 static int __init
vprbrd_gpio_init(void)
442 switch (gpioa_freq
) {
444 gpioa_clk
= VPRBRD_GPIOA_CLK_1MHZ
;
447 gpioa_clk
= VPRBRD_GPIOA_CLK_100KHZ
;
450 gpioa_clk
= VPRBRD_GPIOA_CLK_10KHZ
;
453 gpioa_clk
= VPRBRD_GPIOA_CLK_1KHZ
;
456 gpioa_clk
= VPRBRD_GPIOA_CLK_100HZ
;
459 gpioa_clk
= VPRBRD_GPIOA_CLK_10HZ
;
462 pr_warn("invalid gpioa_freq (%d)\n", gpioa_freq
);
463 gpioa_clk
= VPRBRD_GPIOA_CLK_1KHZ
;
466 return platform_driver_register(&vprbrd_gpio_driver
);
468 subsys_initcall(vprbrd_gpio_init
);
470 static void __exit
vprbrd_gpio_exit(void)
472 platform_driver_unregister(&vprbrd_gpio_driver
);
474 module_exit(vprbrd_gpio_exit
);
476 MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
477 MODULE_DESCRIPTION("GPIO driver for Nano River Techs Viperboard");
478 MODULE_LICENSE("GPL");
479 MODULE_ALIAS("platform:viperboard-gpio");