blob: 76c4aa0da5d4305793f57ddff44c017f88e0c63c [file] [log] [blame]
Lars Poeschel9d5b72d2012-11-05 15:48:24 +01001/*
2 * Nano River Technologies viperboard GPIO lib driver
3 *
4 * (C) 2012 by Lemonage GmbH
5 * Author: Lars Poeschel <poeschel@lemonage.de>
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/types.h>
20#include <linux/mutex.h>
21#include <linux/platform_device.h>
Lars Poeschel9d5b72d2012-11-05 15:48:24 +010022#include <linux/usb.h>
Linus Walleijd3007ec2018-09-12 13:47:35 +020023#include <linux/gpio/driver.h>
Lars Poeschel9d5b72d2012-11-05 15:48:24 +010024
25#include <linux/mfd/viperboard.h>
26
27#define VPRBRD_GPIOA_CLK_1MHZ 0
28#define VPRBRD_GPIOA_CLK_100KHZ 1
29#define VPRBRD_GPIOA_CLK_10KHZ 2
30#define VPRBRD_GPIOA_CLK_1KHZ 3
31#define VPRBRD_GPIOA_CLK_100HZ 4
32#define VPRBRD_GPIOA_CLK_10HZ 5
33
34#define VPRBRD_GPIOA_FREQ_DEFAULT 1000
35
36#define VPRBRD_GPIOA_CMD_CONT 0x00
37#define VPRBRD_GPIOA_CMD_PULSE 0x01
38#define VPRBRD_GPIOA_CMD_PWM 0x02
39#define VPRBRD_GPIOA_CMD_SETOUT 0x03
40#define VPRBRD_GPIOA_CMD_SETIN 0x04
41#define VPRBRD_GPIOA_CMD_SETINT 0x05
42#define VPRBRD_GPIOA_CMD_GETIN 0x06
43
44#define VPRBRD_GPIOB_CMD_SETDIR 0x00
45#define VPRBRD_GPIOB_CMD_SETVAL 0x01
46
47struct vprbrd_gpioa_msg {
48 u8 cmd;
49 u8 clk;
50 u8 offset;
51 u8 t1;
52 u8 t2;
53 u8 invert;
54 u8 pwmlevel;
55 u8 outval;
56 u8 risefall;
57 u8 answer;
58 u8 __fill;
59} __packed;
60
61struct vprbrd_gpiob_msg {
62 u8 cmd;
63 u16 val;
64 u16 mask;
65} __packed;
66
67struct vprbrd_gpio {
68 struct gpio_chip gpioa; /* gpio a related things */
69 u32 gpioa_out;
70 u32 gpioa_val;
71 struct gpio_chip gpiob; /* gpio b related things */
72 u32 gpiob_out;
73 u32 gpiob_val;
74 struct vprbrd *vb;
75};
76
77/* gpioa sampling clock module parameter */
78static unsigned char gpioa_clk;
79static unsigned int gpioa_freq = VPRBRD_GPIOA_FREQ_DEFAULT;
80module_param(gpioa_freq, uint, 0);
81MODULE_PARM_DESC(gpioa_freq,
82 "gpio-a sampling freq in Hz (default is 1000Hz) valid values: 10, 100, 1000, 10000, 100000, 1000000");
83
84/* ----- begin of gipo a chip -------------------------------------------- */
85
86static int vprbrd_gpioa_get(struct gpio_chip *chip,
87 unsigned offset)
88{
89 int ret, answer, error = 0;
Linus Walleij2a873c82015-12-07 15:08:00 +010090 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +010091 struct vprbrd *vb = gpio->vb;
92 struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
93
94 /* if io is set to output, just return the saved value */
95 if (gpio->gpioa_out & (1 << offset))
Linus Walleij80776df2015-12-21 14:58:52 +010096 return !!(gpio->gpioa_val & (1 << offset));
Lars Poeschel9d5b72d2012-11-05 15:48:24 +010097
98 mutex_lock(&vb->lock);
99
100 gamsg->cmd = VPRBRD_GPIOA_CMD_GETIN;
101 gamsg->clk = 0x00;
102 gamsg->offset = offset;
103 gamsg->t1 = 0x00;
104 gamsg->t2 = 0x00;
105 gamsg->invert = 0x00;
106 gamsg->pwmlevel = 0x00;
107 gamsg->outval = 0x00;
108 gamsg->risefall = 0x00;
109 gamsg->answer = 0x00;
110 gamsg->__fill = 0x00;
111
112 ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
113 VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
114 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
115 VPRBRD_USB_TIMEOUT_MS);
116 if (ret != sizeof(struct vprbrd_gpioa_msg))
117 error = -EREMOTEIO;
118
119 ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
120 VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_IN, 0x0000,
121 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
122 VPRBRD_USB_TIMEOUT_MS);
123 answer = gamsg->answer & 0x01;
124
125 mutex_unlock(&vb->lock);
126
127 if (ret != sizeof(struct vprbrd_gpioa_msg))
128 error = -EREMOTEIO;
129
130 if (error)
131 return error;
132
133 return answer;
134}
135
136static void vprbrd_gpioa_set(struct gpio_chip *chip,
137 unsigned offset, int value)
138{
139 int ret;
Linus Walleij2a873c82015-12-07 15:08:00 +0100140 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100141 struct vprbrd *vb = gpio->vb;
142 struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
143
144 if (gpio->gpioa_out & (1 << offset)) {
145 if (value)
146 gpio->gpioa_val |= (1 << offset);
147 else
148 gpio->gpioa_val &= ~(1 << offset);
149
150 mutex_lock(&vb->lock);
151
152 gamsg->cmd = VPRBRD_GPIOA_CMD_SETOUT;
153 gamsg->clk = 0x00;
154 gamsg->offset = offset;
155 gamsg->t1 = 0x00;
156 gamsg->t2 = 0x00;
157 gamsg->invert = 0x00;
158 gamsg->pwmlevel = 0x00;
159 gamsg->outval = value;
160 gamsg->risefall = 0x00;
161 gamsg->answer = 0x00;
162 gamsg->__fill = 0x00;
163
164 ret = usb_control_msg(vb->usb_dev,
165 usb_sndctrlpipe(vb->usb_dev, 0),
166 VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT,
167 0x0000, 0x0000, gamsg,
168 sizeof(struct vprbrd_gpioa_msg), VPRBRD_USB_TIMEOUT_MS);
169
170 mutex_unlock(&vb->lock);
171
172 if (ret != sizeof(struct vprbrd_gpioa_msg))
Linus Walleij58383c782015-11-04 09:56:26 +0100173 dev_err(chip->parent, "usb error setting pin value\n");
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100174 }
175}
176
177static int vprbrd_gpioa_direction_input(struct gpio_chip *chip,
178 unsigned offset)
179{
180 int ret;
Linus Walleij2a873c82015-12-07 15:08:00 +0100181 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100182 struct vprbrd *vb = gpio->vb;
183 struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
184
185 gpio->gpioa_out &= ~(1 << offset);
186
187 mutex_lock(&vb->lock);
188
189 gamsg->cmd = VPRBRD_GPIOA_CMD_SETIN;
190 gamsg->clk = gpioa_clk;
191 gamsg->offset = offset;
192 gamsg->t1 = 0x00;
193 gamsg->t2 = 0x00;
194 gamsg->invert = 0x00;
195 gamsg->pwmlevel = 0x00;
196 gamsg->outval = 0x00;
197 gamsg->risefall = 0x00;
198 gamsg->answer = 0x00;
199 gamsg->__fill = 0x00;
200
201 ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
202 VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
203 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
204 VPRBRD_USB_TIMEOUT_MS);
205
206 mutex_unlock(&vb->lock);
207
208 if (ret != sizeof(struct vprbrd_gpioa_msg))
209 return -EREMOTEIO;
210
211 return 0;
212}
213
214static int vprbrd_gpioa_direction_output(struct gpio_chip *chip,
215 unsigned offset, int value)
216{
217 int ret;
Linus Walleij2a873c82015-12-07 15:08:00 +0100218 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100219 struct vprbrd *vb = gpio->vb;
220 struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
221
222 gpio->gpioa_out |= (1 << offset);
223 if (value)
224 gpio->gpioa_val |= (1 << offset);
225 else
226 gpio->gpioa_val &= ~(1 << offset);
227
228 mutex_lock(&vb->lock);
229
230 gamsg->cmd = VPRBRD_GPIOA_CMD_SETOUT;
231 gamsg->clk = 0x00;
232 gamsg->offset = offset;
233 gamsg->t1 = 0x00;
234 gamsg->t2 = 0x00;
235 gamsg->invert = 0x00;
236 gamsg->pwmlevel = 0x00;
237 gamsg->outval = value;
238 gamsg->risefall = 0x00;
239 gamsg->answer = 0x00;
240 gamsg->__fill = 0x00;
241
242 ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
243 VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
244 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
245 VPRBRD_USB_TIMEOUT_MS);
246
247 mutex_unlock(&vb->lock);
248
249 if (ret != sizeof(struct vprbrd_gpioa_msg))
250 return -EREMOTEIO;
251
252 return 0;
253}
254
255/* ----- end of gpio a chip ---------------------------------------------- */
256
257/* ----- begin of gipo b chip -------------------------------------------- */
258
259static int vprbrd_gpiob_setdir(struct vprbrd *vb, unsigned offset,
260 unsigned dir)
261{
262 struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
263 int ret;
264
265 gbmsg->cmd = VPRBRD_GPIOB_CMD_SETDIR;
266 gbmsg->val = cpu_to_be16(dir << offset);
267 gbmsg->mask = cpu_to_be16(0x0001 << offset);
268
269 ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
270 VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_OUT, 0x0000,
271 0x0000, gbmsg, sizeof(struct vprbrd_gpiob_msg),
272 VPRBRD_USB_TIMEOUT_MS);
273
274 if (ret != sizeof(struct vprbrd_gpiob_msg))
275 return -EREMOTEIO;
276
277 return 0;
278}
279
280static int vprbrd_gpiob_get(struct gpio_chip *chip,
281 unsigned offset)
282{
283 int ret;
284 u16 val;
Linus Walleij2a873c82015-12-07 15:08:00 +0100285 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100286 struct vprbrd *vb = gpio->vb;
287 struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
288
289 /* if io is set to output, just return the saved value */
290 if (gpio->gpiob_out & (1 << offset))
291 return gpio->gpiob_val & (1 << offset);
292
293 mutex_lock(&vb->lock);
294
295 ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
296 VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_IN, 0x0000,
297 0x0000, gbmsg, sizeof(struct vprbrd_gpiob_msg),
298 VPRBRD_USB_TIMEOUT_MS);
299 val = gbmsg->val;
300
301 mutex_unlock(&vb->lock);
302
303 if (ret != sizeof(struct vprbrd_gpiob_msg))
304 return ret;
305
306 /* cache the read values */
307 gpio->gpiob_val = be16_to_cpu(val);
308
309 return (gpio->gpiob_val >> offset) & 0x1;
310}
311
312static void vprbrd_gpiob_set(struct gpio_chip *chip,
313 unsigned offset, int value)
314{
315 int ret;
Linus Walleij2a873c82015-12-07 15:08:00 +0100316 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100317 struct vprbrd *vb = gpio->vb;
318 struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
319
320 if (gpio->gpiob_out & (1 << offset)) {
321 if (value)
322 gpio->gpiob_val |= (1 << offset);
323 else
324 gpio->gpiob_val &= ~(1 << offset);
325
326 mutex_lock(&vb->lock);
327
328 gbmsg->cmd = VPRBRD_GPIOB_CMD_SETVAL;
329 gbmsg->val = cpu_to_be16(value << offset);
330 gbmsg->mask = cpu_to_be16(0x0001 << offset);
331
332 ret = usb_control_msg(vb->usb_dev,
333 usb_sndctrlpipe(vb->usb_dev, 0),
334 VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_OUT,
335 0x0000, 0x0000, gbmsg,
336 sizeof(struct vprbrd_gpiob_msg), VPRBRD_USB_TIMEOUT_MS);
337
338 mutex_unlock(&vb->lock);
339
340 if (ret != sizeof(struct vprbrd_gpiob_msg))
Linus Walleij58383c782015-11-04 09:56:26 +0100341 dev_err(chip->parent, "usb error setting pin value\n");
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100342 }
343}
344
345static int vprbrd_gpiob_direction_input(struct gpio_chip *chip,
346 unsigned offset)
347{
348 int ret;
Linus Walleij2a873c82015-12-07 15:08:00 +0100349 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100350 struct vprbrd *vb = gpio->vb;
351
352 gpio->gpiob_out &= ~(1 << offset);
353
354 mutex_lock(&vb->lock);
355
356 ret = vprbrd_gpiob_setdir(vb, offset, 0);
357
358 mutex_unlock(&vb->lock);
359
360 if (ret)
Linus Walleij58383c782015-11-04 09:56:26 +0100361 dev_err(chip->parent, "usb error setting pin to input\n");
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100362
363 return ret;
364}
365
366static int vprbrd_gpiob_direction_output(struct gpio_chip *chip,
367 unsigned offset, int value)
368{
369 int ret;
Linus Walleij2a873c82015-12-07 15:08:00 +0100370 struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100371 struct vprbrd *vb = gpio->vb;
372
373 gpio->gpiob_out |= (1 << offset);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100374
375 mutex_lock(&vb->lock);
376
377 ret = vprbrd_gpiob_setdir(vb, offset, 1);
378 if (ret)
Linus Walleij58383c782015-11-04 09:56:26 +0100379 dev_err(chip->parent, "usb error setting pin to output\n");
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100380
381 mutex_unlock(&vb->lock);
382
383 vprbrd_gpiob_set(chip, offset, value);
384
385 return ret;
386}
387
388/* ----- end of gpio b chip ---------------------------------------------- */
389
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800390static int vprbrd_gpio_probe(struct platform_device *pdev)
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100391{
392 struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
393 struct vprbrd_gpio *vb_gpio;
394 int ret;
395
396 vb_gpio = devm_kzalloc(&pdev->dev, sizeof(*vb_gpio), GFP_KERNEL);
397 if (vb_gpio == NULL)
398 return -ENOMEM;
399
400 vb_gpio->vb = vb;
401 /* registering gpio a */
402 vb_gpio->gpioa.label = "viperboard gpio a";
Linus Walleij58383c782015-11-04 09:56:26 +0100403 vb_gpio->gpioa.parent = &pdev->dev;
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100404 vb_gpio->gpioa.owner = THIS_MODULE;
405 vb_gpio->gpioa.base = -1;
406 vb_gpio->gpioa.ngpio = 16;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100407 vb_gpio->gpioa.can_sleep = true;
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100408 vb_gpio->gpioa.set = vprbrd_gpioa_set;
409 vb_gpio->gpioa.get = vprbrd_gpioa_get;
410 vb_gpio->gpioa.direction_input = vprbrd_gpioa_direction_input;
411 vb_gpio->gpioa.direction_output = vprbrd_gpioa_direction_output;
Laxman Dewangan45338c32016-02-22 17:43:28 +0530412 ret = devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpioa, vb_gpio);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100413 if (ret < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +0100414 dev_err(vb_gpio->gpioa.parent, "could not add gpio a");
Laxman Dewangan45338c32016-02-22 17:43:28 +0530415 return ret;
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100416 }
417
418 /* registering gpio b */
419 vb_gpio->gpiob.label = "viperboard gpio b";
Linus Walleij58383c782015-11-04 09:56:26 +0100420 vb_gpio->gpiob.parent = &pdev->dev;
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100421 vb_gpio->gpiob.owner = THIS_MODULE;
422 vb_gpio->gpiob.base = -1;
423 vb_gpio->gpiob.ngpio = 16;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100424 vb_gpio->gpiob.can_sleep = true;
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100425 vb_gpio->gpiob.set = vprbrd_gpiob_set;
426 vb_gpio->gpiob.get = vprbrd_gpiob_get;
427 vb_gpio->gpiob.direction_input = vprbrd_gpiob_direction_input;
428 vb_gpio->gpiob.direction_output = vprbrd_gpiob_direction_output;
Laxman Dewangan45338c32016-02-22 17:43:28 +0530429 ret = devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpiob, vb_gpio);
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100430 if (ret < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +0100431 dev_err(vb_gpio->gpiob.parent, "could not add gpio b");
Laxman Dewangan45338c32016-02-22 17:43:28 +0530432 return ret;
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100433 }
434
435 platform_set_drvdata(pdev, vb_gpio);
436
437 return ret;
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100438}
439
440static struct platform_driver vprbrd_gpio_driver = {
441 .driver.name = "viperboard-gpio",
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100442 .probe = vprbrd_gpio_probe,
Lars Poeschel9d5b72d2012-11-05 15:48:24 +0100443};
444
445static int __init vprbrd_gpio_init(void)
446{
447 switch (gpioa_freq) {
448 case 1000000:
449 gpioa_clk = VPRBRD_GPIOA_CLK_1MHZ;
450 break;
451 case 100000:
452 gpioa_clk = VPRBRD_GPIOA_CLK_100KHZ;
453 break;
454 case 10000:
455 gpioa_clk = VPRBRD_GPIOA_CLK_10KHZ;
456 break;
457 case 1000:
458 gpioa_clk = VPRBRD_GPIOA_CLK_1KHZ;
459 break;
460 case 100:
461 gpioa_clk = VPRBRD_GPIOA_CLK_100HZ;
462 break;
463 case 10:
464 gpioa_clk = VPRBRD_GPIOA_CLK_10HZ;
465 break;
466 default:
467 pr_warn("invalid gpioa_freq (%d)\n", gpioa_freq);
468 gpioa_clk = VPRBRD_GPIOA_CLK_1KHZ;
469 }
470
471 return platform_driver_register(&vprbrd_gpio_driver);
472}
473subsys_initcall(vprbrd_gpio_init);
474
475static void __exit vprbrd_gpio_exit(void)
476{
477 platform_driver_unregister(&vprbrd_gpio_driver);
478}
479module_exit(vprbrd_gpio_exit);
480
481MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
482MODULE_DESCRIPTION("GPIO driver for Nano River Techs Viperboard");
483MODULE_LICENSE("GPL");
484MODULE_ALIAS("platform:viperboard-gpio");