]>
Commit | Line | Data |
---|---|---|
6732127f DB |
1 | /* |
2 | * Driver for the Diolan DLN-2 USB-GPIO adapter | |
3 | * | |
4 | * Copyright (c) 2014 Intel Corporation | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation, version 2. | |
9 | */ | |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/irqdomain.h> | |
16 | #include <linux/irq.h> | |
17 | #include <linux/irqchip/chained_irq.h> | |
18 | #include <linux/gpio.h> | |
19 | #include <linux/gpio/driver.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <linux/mfd/dln2.h> | |
22 | ||
23 | #define DLN2_GPIO_ID 0x01 | |
24 | ||
25 | #define DLN2_GPIO_GET_PIN_COUNT DLN2_CMD(0x01, DLN2_GPIO_ID) | |
26 | #define DLN2_GPIO_SET_DEBOUNCE DLN2_CMD(0x04, DLN2_GPIO_ID) | |
27 | #define DLN2_GPIO_GET_DEBOUNCE DLN2_CMD(0x05, DLN2_GPIO_ID) | |
28 | #define DLN2_GPIO_PORT_GET_VAL DLN2_CMD(0x06, DLN2_GPIO_ID) | |
29 | #define DLN2_GPIO_PIN_GET_VAL DLN2_CMD(0x0B, DLN2_GPIO_ID) | |
30 | #define DLN2_GPIO_PIN_SET_OUT_VAL DLN2_CMD(0x0C, DLN2_GPIO_ID) | |
31 | #define DLN2_GPIO_PIN_GET_OUT_VAL DLN2_CMD(0x0D, DLN2_GPIO_ID) | |
32 | #define DLN2_GPIO_CONDITION_MET_EV DLN2_CMD(0x0F, DLN2_GPIO_ID) | |
33 | #define DLN2_GPIO_PIN_ENABLE DLN2_CMD(0x10, DLN2_GPIO_ID) | |
34 | #define DLN2_GPIO_PIN_DISABLE DLN2_CMD(0x11, DLN2_GPIO_ID) | |
35 | #define DLN2_GPIO_PIN_SET_DIRECTION DLN2_CMD(0x13, DLN2_GPIO_ID) | |
36 | #define DLN2_GPIO_PIN_GET_DIRECTION DLN2_CMD(0x14, DLN2_GPIO_ID) | |
37 | #define DLN2_GPIO_PIN_SET_EVENT_CFG DLN2_CMD(0x1E, DLN2_GPIO_ID) | |
38 | #define DLN2_GPIO_PIN_GET_EVENT_CFG DLN2_CMD(0x1F, DLN2_GPIO_ID) | |
39 | ||
40 | #define DLN2_GPIO_EVENT_NONE 0 | |
41 | #define DLN2_GPIO_EVENT_CHANGE 1 | |
42 | #define DLN2_GPIO_EVENT_LVL_HIGH 2 | |
43 | #define DLN2_GPIO_EVENT_LVL_LOW 3 | |
44 | #define DLN2_GPIO_EVENT_CHANGE_RISING 0x11 | |
45 | #define DLN2_GPIO_EVENT_CHANGE_FALLING 0x21 | |
46 | #define DLN2_GPIO_EVENT_MASK 0x0F | |
47 | ||
48 | #define DLN2_GPIO_MAX_PINS 32 | |
49 | ||
50 | struct dln2_irq_work { | |
51 | struct work_struct work; | |
52 | struct dln2_gpio *dln2; | |
53 | int pin; | |
54 | int type; | |
55 | }; | |
56 | ||
57 | struct dln2_gpio { | |
58 | struct platform_device *pdev; | |
59 | struct gpio_chip gpio; | |
60 | ||
61 | /* | |
62 | * Cache pin direction to save us one transfer, since the hardware has | |
63 | * separate commands to read the in and out values. | |
64 | */ | |
65 | DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS); | |
66 | ||
0acb0e71 OP |
67 | /* active IRQs - not synced to hardware */ |
68 | DECLARE_BITMAP(unmasked_irqs, DLN2_GPIO_MAX_PINS); | |
6732127f DB |
69 | struct dln2_irq_work *irq_work; |
70 | }; | |
71 | ||
72 | struct dln2_gpio_pin { | |
73 | __le16 pin; | |
74 | }; | |
75 | ||
76 | struct dln2_gpio_pin_val { | |
77 | __le16 pin __packed; | |
78 | u8 value; | |
79 | }; | |
80 | ||
81 | static int dln2_gpio_get_pin_count(struct platform_device *pdev) | |
82 | { | |
83 | int ret; | |
84 | __le16 count; | |
85 | int len = sizeof(count); | |
86 | ||
87 | ret = dln2_transfer_rx(pdev, DLN2_GPIO_GET_PIN_COUNT, &count, &len); | |
88 | if (ret < 0) | |
89 | return ret; | |
90 | if (len < sizeof(count)) | |
91 | return -EPROTO; | |
92 | ||
93 | return le16_to_cpu(count); | |
94 | } | |
95 | ||
96 | static int dln2_gpio_pin_cmd(struct dln2_gpio *dln2, int cmd, unsigned pin) | |
97 | { | |
98 | struct dln2_gpio_pin req = { | |
99 | .pin = cpu_to_le16(pin), | |
100 | }; | |
101 | ||
102 | return dln2_transfer_tx(dln2->pdev, cmd, &req, sizeof(req)); | |
103 | } | |
104 | ||
105 | static int dln2_gpio_pin_val(struct dln2_gpio *dln2, int cmd, unsigned int pin) | |
106 | { | |
107 | int ret; | |
108 | struct dln2_gpio_pin req = { | |
109 | .pin = cpu_to_le16(pin), | |
110 | }; | |
111 | struct dln2_gpio_pin_val rsp; | |
112 | int len = sizeof(rsp); | |
113 | ||
114 | ret = dln2_transfer(dln2->pdev, cmd, &req, sizeof(req), &rsp, &len); | |
115 | if (ret < 0) | |
116 | return ret; | |
117 | if (len < sizeof(rsp) || req.pin != rsp.pin) | |
118 | return -EPROTO; | |
119 | ||
120 | return rsp.value; | |
121 | } | |
122 | ||
123 | static int dln2_gpio_pin_get_in_val(struct dln2_gpio *dln2, unsigned int pin) | |
124 | { | |
125 | int ret; | |
126 | ||
127 | ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_VAL, pin); | |
128 | if (ret < 0) | |
129 | return ret; | |
130 | return !!ret; | |
131 | } | |
132 | ||
133 | static int dln2_gpio_pin_get_out_val(struct dln2_gpio *dln2, unsigned int pin) | |
134 | { | |
135 | int ret; | |
136 | ||
137 | ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_OUT_VAL, pin); | |
138 | if (ret < 0) | |
139 | return ret; | |
140 | return !!ret; | |
141 | } | |
142 | ||
143 | static void dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2, | |
144 | unsigned int pin, int value) | |
145 | { | |
146 | struct dln2_gpio_pin_val req = { | |
147 | .pin = cpu_to_le16(pin), | |
148 | .value = value, | |
149 | }; | |
150 | ||
151 | dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req, | |
152 | sizeof(req)); | |
153 | } | |
154 | ||
155 | #define DLN2_GPIO_DIRECTION_IN 0 | |
156 | #define DLN2_GPIO_DIRECTION_OUT 1 | |
157 | ||
158 | static int dln2_gpio_request(struct gpio_chip *chip, unsigned offset) | |
159 | { | |
160 | struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio); | |
161 | struct dln2_gpio_pin req = { | |
162 | .pin = cpu_to_le16(offset), | |
163 | }; | |
164 | struct dln2_gpio_pin_val rsp; | |
165 | int len = sizeof(rsp); | |
166 | int ret; | |
167 | ||
168 | ret = dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_ENABLE, offset); | |
169 | if (ret < 0) | |
170 | return ret; | |
171 | ||
172 | /* cache the pin direction */ | |
173 | ret = dln2_transfer(dln2->pdev, DLN2_GPIO_PIN_GET_DIRECTION, | |
174 | &req, sizeof(req), &rsp, &len); | |
175 | if (ret < 0) | |
176 | return ret; | |
177 | if (len < sizeof(rsp) || req.pin != rsp.pin) { | |
178 | ret = -EPROTO; | |
179 | goto out_disable; | |
180 | } | |
181 | ||
182 | switch (rsp.value) { | |
183 | case DLN2_GPIO_DIRECTION_IN: | |
184 | clear_bit(offset, dln2->output_enabled); | |
185 | return 0; | |
186 | case DLN2_GPIO_DIRECTION_OUT: | |
187 | set_bit(offset, dln2->output_enabled); | |
188 | return 0; | |
189 | default: | |
190 | ret = -EPROTO; | |
191 | goto out_disable; | |
192 | } | |
193 | ||
194 | out_disable: | |
195 | dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset); | |
196 | return ret; | |
197 | } | |
198 | ||
199 | static void dln2_gpio_free(struct gpio_chip *chip, unsigned offset) | |
200 | { | |
201 | struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio); | |
202 | ||
203 | dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset); | |
204 | } | |
205 | ||
206 | static int dln2_gpio_get_direction(struct gpio_chip *chip, unsigned offset) | |
207 | { | |
208 | struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio); | |
209 | ||
210 | if (test_bit(offset, dln2->output_enabled)) | |
211 | return GPIOF_DIR_OUT; | |
212 | ||
213 | return GPIOF_DIR_IN; | |
214 | } | |
215 | ||
216 | static int dln2_gpio_get(struct gpio_chip *chip, unsigned int offset) | |
217 | { | |
218 | struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio); | |
219 | int dir; | |
220 | ||
221 | dir = dln2_gpio_get_direction(chip, offset); | |
222 | if (dir < 0) | |
223 | return dir; | |
224 | ||
225 | if (dir == GPIOF_DIR_IN) | |
226 | return dln2_gpio_pin_get_in_val(dln2, offset); | |
227 | ||
228 | return dln2_gpio_pin_get_out_val(dln2, offset); | |
229 | } | |
230 | ||
231 | static void dln2_gpio_set(struct gpio_chip *chip, unsigned offset, int value) | |
232 | { | |
233 | struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio); | |
234 | ||
235 | dln2_gpio_pin_set_out_val(dln2, offset, value); | |
236 | } | |
237 | ||
238 | static int dln2_gpio_set_direction(struct gpio_chip *chip, unsigned offset, | |
239 | unsigned dir) | |
240 | { | |
241 | struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio); | |
242 | struct dln2_gpio_pin_val req = { | |
243 | .pin = cpu_to_le16(offset), | |
244 | .value = dir, | |
245 | }; | |
246 | int ret; | |
247 | ||
248 | ret = dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_DIRECTION, | |
249 | &req, sizeof(req)); | |
250 | if (ret < 0) | |
251 | return ret; | |
252 | ||
253 | if (dir == DLN2_GPIO_DIRECTION_OUT) | |
254 | set_bit(offset, dln2->output_enabled); | |
255 | else | |
256 | clear_bit(offset, dln2->output_enabled); | |
257 | ||
258 | return ret; | |
259 | } | |
260 | ||
261 | static int dln2_gpio_direction_input(struct gpio_chip *chip, unsigned offset) | |
262 | { | |
263 | return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_IN); | |
264 | } | |
265 | ||
266 | static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset, | |
267 | int value) | |
268 | { | |
269 | return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT); | |
270 | } | |
271 | ||
272 | static int dln2_gpio_set_debounce(struct gpio_chip *chip, unsigned offset, | |
273 | unsigned debounce) | |
274 | { | |
275 | struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio); | |
276 | __le32 duration = cpu_to_le32(debounce); | |
277 | ||
278 | return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_SET_DEBOUNCE, | |
279 | &duration, sizeof(duration)); | |
280 | } | |
281 | ||
282 | static int dln2_gpio_set_event_cfg(struct dln2_gpio *dln2, unsigned pin, | |
283 | unsigned type, unsigned period) | |
284 | { | |
285 | struct { | |
286 | __le16 pin; | |
287 | u8 type; | |
288 | __le16 period; | |
289 | } __packed req = { | |
290 | .pin = cpu_to_le16(pin), | |
291 | .type = type, | |
292 | .period = cpu_to_le16(period), | |
293 | }; | |
294 | ||
295 | return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_EVENT_CFG, | |
296 | &req, sizeof(req)); | |
297 | } | |
298 | ||
299 | static void dln2_irq_work(struct work_struct *w) | |
300 | { | |
301 | struct dln2_irq_work *iw = container_of(w, struct dln2_irq_work, work); | |
302 | struct dln2_gpio *dln2 = iw->dln2; | |
303 | u8 type = iw->type & DLN2_GPIO_EVENT_MASK; | |
304 | ||
0acb0e71 | 305 | if (test_bit(iw->pin, dln2->unmasked_irqs)) |
6732127f DB |
306 | dln2_gpio_set_event_cfg(dln2, iw->pin, type, 0); |
307 | else | |
308 | dln2_gpio_set_event_cfg(dln2, iw->pin, DLN2_GPIO_EVENT_NONE, 0); | |
309 | } | |
310 | ||
0acb0e71 | 311 | static void dln2_irq_unmask(struct irq_data *irqd) |
6732127f DB |
312 | { |
313 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); | |
314 | struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio); | |
315 | int pin = irqd_to_hwirq(irqd); | |
316 | ||
0acb0e71 | 317 | set_bit(pin, dln2->unmasked_irqs); |
6732127f DB |
318 | schedule_work(&dln2->irq_work[pin].work); |
319 | } | |
320 | ||
321 | static void dln2_irq_mask(struct irq_data *irqd) | |
322 | { | |
323 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); | |
324 | struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio); | |
325 | int pin = irqd_to_hwirq(irqd); | |
326 | ||
0acb0e71 OP |
327 | clear_bit(pin, dln2->unmasked_irqs); |
328 | schedule_work(&dln2->irq_work[pin].work); | |
6732127f DB |
329 | } |
330 | ||
331 | static int dln2_irq_set_type(struct irq_data *irqd, unsigned type) | |
332 | { | |
333 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); | |
334 | struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio); | |
335 | int pin = irqd_to_hwirq(irqd); | |
336 | ||
337 | switch (type) { | |
338 | case IRQ_TYPE_LEVEL_HIGH: | |
339 | dln2->irq_work[pin].type = DLN2_GPIO_EVENT_LVL_HIGH; | |
340 | break; | |
341 | case IRQ_TYPE_LEVEL_LOW: | |
342 | dln2->irq_work[pin].type = DLN2_GPIO_EVENT_LVL_LOW; | |
343 | break; | |
344 | case IRQ_TYPE_EDGE_BOTH: | |
345 | dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE; | |
346 | break; | |
347 | case IRQ_TYPE_EDGE_RISING: | |
348 | dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE_RISING; | |
349 | break; | |
350 | case IRQ_TYPE_EDGE_FALLING: | |
351 | dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE_FALLING; | |
352 | break; | |
353 | default: | |
354 | return -EINVAL; | |
355 | } | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | static struct irq_chip dln2_gpio_irqchip = { | |
361 | .name = "dln2-irq", | |
6732127f DB |
362 | .irq_mask = dln2_irq_mask, |
363 | .irq_unmask = dln2_irq_unmask, | |
364 | .irq_set_type = dln2_irq_set_type, | |
365 | }; | |
366 | ||
367 | static void dln2_gpio_event(struct platform_device *pdev, u16 echo, | |
368 | const void *data, int len) | |
369 | { | |
370 | int pin, irq; | |
371 | const struct { | |
372 | __le16 count; | |
373 | __u8 type; | |
374 | __le16 pin; | |
375 | __u8 value; | |
376 | } __packed *event = data; | |
377 | struct dln2_gpio *dln2 = platform_get_drvdata(pdev); | |
378 | ||
379 | if (len < sizeof(*event)) { | |
380 | dev_err(dln2->gpio.dev, "short event message\n"); | |
381 | return; | |
382 | } | |
383 | ||
384 | pin = le16_to_cpu(event->pin); | |
385 | if (pin >= dln2->gpio.ngpio) { | |
386 | dev_err(dln2->gpio.dev, "out of bounds pin %d\n", pin); | |
387 | return; | |
388 | } | |
389 | ||
390 | irq = irq_find_mapping(dln2->gpio.irqdomain, pin); | |
391 | if (!irq) { | |
392 | dev_err(dln2->gpio.dev, "pin %d not mapped to IRQ\n", pin); | |
393 | return; | |
394 | } | |
395 | ||
6732127f DB |
396 | switch (dln2->irq_work[pin].type) { |
397 | case DLN2_GPIO_EVENT_CHANGE_RISING: | |
398 | if (event->value) | |
399 | generic_handle_irq(irq); | |
400 | break; | |
401 | case DLN2_GPIO_EVENT_CHANGE_FALLING: | |
402 | if (!event->value) | |
403 | generic_handle_irq(irq); | |
404 | break; | |
405 | default: | |
406 | generic_handle_irq(irq); | |
407 | } | |
408 | } | |
409 | ||
410 | static int dln2_gpio_probe(struct platform_device *pdev) | |
411 | { | |
412 | struct dln2_gpio *dln2; | |
413 | struct device *dev = &pdev->dev; | |
414 | int pins; | |
415 | int i, ret; | |
416 | ||
417 | pins = dln2_gpio_get_pin_count(pdev); | |
418 | if (pins < 0) { | |
419 | dev_err(dev, "failed to get pin count: %d\n", pins); | |
420 | return pins; | |
421 | } | |
422 | if (pins > DLN2_GPIO_MAX_PINS) { | |
423 | pins = DLN2_GPIO_MAX_PINS; | |
424 | dev_warn(dev, "clamping pins to %d\n", DLN2_GPIO_MAX_PINS); | |
425 | } | |
426 | ||
427 | dln2 = devm_kzalloc(&pdev->dev, sizeof(*dln2), GFP_KERNEL); | |
428 | if (!dln2) | |
429 | return -ENOMEM; | |
430 | ||
431 | dln2->irq_work = devm_kcalloc(&pdev->dev, pins, | |
432 | sizeof(struct dln2_irq_work), GFP_KERNEL); | |
433 | if (!dln2->irq_work) | |
434 | return -ENOMEM; | |
435 | for (i = 0; i < pins; i++) { | |
436 | INIT_WORK(&dln2->irq_work[i].work, dln2_irq_work); | |
437 | dln2->irq_work[i].pin = i; | |
438 | dln2->irq_work[i].dln2 = dln2; | |
439 | } | |
440 | ||
441 | dln2->pdev = pdev; | |
442 | ||
443 | dln2->gpio.label = "dln2"; | |
444 | dln2->gpio.dev = dev; | |
445 | dln2->gpio.owner = THIS_MODULE; | |
446 | dln2->gpio.base = -1; | |
447 | dln2->gpio.ngpio = pins; | |
448 | dln2->gpio.exported = true; | |
449 | dln2->gpio.can_sleep = true; | |
450 | dln2->gpio.irq_not_threaded = true; | |
451 | dln2->gpio.set = dln2_gpio_set; | |
452 | dln2->gpio.get = dln2_gpio_get; | |
453 | dln2->gpio.request = dln2_gpio_request; | |
454 | dln2->gpio.free = dln2_gpio_free; | |
455 | dln2->gpio.get_direction = dln2_gpio_get_direction; | |
456 | dln2->gpio.direction_input = dln2_gpio_direction_input; | |
457 | dln2->gpio.direction_output = dln2_gpio_direction_output; | |
458 | dln2->gpio.set_debounce = dln2_gpio_set_debounce; | |
459 | ||
460 | platform_set_drvdata(pdev, dln2); | |
461 | ||
462 | ret = gpiochip_add(&dln2->gpio); | |
463 | if (ret < 0) { | |
464 | dev_err(dev, "failed to add gpio chip: %d\n", ret); | |
465 | goto out; | |
466 | } | |
467 | ||
468 | ret = gpiochip_irqchip_add(&dln2->gpio, &dln2_gpio_irqchip, 0, | |
469 | handle_simple_irq, IRQ_TYPE_NONE); | |
470 | if (ret < 0) { | |
471 | dev_err(dev, "failed to add irq chip: %d\n", ret); | |
472 | goto out_gpiochip_remove; | |
473 | } | |
474 | ||
475 | ret = dln2_register_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV, | |
476 | dln2_gpio_event); | |
477 | if (ret) { | |
478 | dev_err(dev, "failed to register event cb: %d\n", ret); | |
479 | goto out_gpiochip_remove; | |
480 | } | |
481 | ||
482 | return 0; | |
483 | ||
484 | out_gpiochip_remove: | |
485 | gpiochip_remove(&dln2->gpio); | |
486 | out: | |
487 | return ret; | |
488 | } | |
489 | ||
490 | static int dln2_gpio_remove(struct platform_device *pdev) | |
491 | { | |
492 | struct dln2_gpio *dln2 = platform_get_drvdata(pdev); | |
493 | int i; | |
494 | ||
495 | dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV); | |
496 | for (i = 0; i < dln2->gpio.ngpio; i++) | |
497 | flush_work(&dln2->irq_work[i].work); | |
498 | gpiochip_remove(&dln2->gpio); | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
503 | static struct platform_driver dln2_gpio_driver = { | |
504 | .driver.name = "dln2-gpio", | |
505 | .probe = dln2_gpio_probe, | |
506 | .remove = dln2_gpio_remove, | |
507 | }; | |
508 | ||
509 | module_platform_driver(dln2_gpio_driver); | |
510 | ||
511 | MODULE_AUTHOR("Daniel Baluta <[email protected]"); | |
512 | MODULE_DESCRIPTION("Driver for the Diolan DLN2 GPIO interface"); | |
513 | MODULE_LICENSE("GPL v2"); | |
514 | MODULE_ALIAS("platform:dln2-gpio"); |