2 * Driver for the Diolan DLN-2 USB-GPIO adapter
4 * Copyright (c) 2014 Intel Corporation
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.
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>
23 #define DLN2_GPIO_ID 0x01
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)
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
48 #define DLN2_GPIO_MAX_PINS 32
50 struct dln2_irq_work {
51 struct work_struct work;
52 struct dln2_gpio *dln2;
58 struct platform_device *pdev;
59 struct gpio_chip gpio;
62 * Cache pin direction to save us one transfer, since the hardware has
63 * separate commands to read the in and out values.
65 DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS);
67 DECLARE_BITMAP(irqs_masked, DLN2_GPIO_MAX_PINS);
68 DECLARE_BITMAP(irqs_enabled, DLN2_GPIO_MAX_PINS);
69 DECLARE_BITMAP(irqs_pending, DLN2_GPIO_MAX_PINS);
70 struct dln2_irq_work *irq_work;
73 struct dln2_gpio_pin {
77 struct dln2_gpio_pin_val {
82 static int dln2_gpio_get_pin_count(struct platform_device *pdev)
86 int len = sizeof(count);
88 ret = dln2_transfer_rx(pdev, DLN2_GPIO_GET_PIN_COUNT, &count, &len);
91 if (len < sizeof(count))
94 return le16_to_cpu(count);
97 static int dln2_gpio_pin_cmd(struct dln2_gpio *dln2, int cmd, unsigned pin)
99 struct dln2_gpio_pin req = {
100 .pin = cpu_to_le16(pin),
103 return dln2_transfer_tx(dln2->pdev, cmd, &req, sizeof(req));
106 static int dln2_gpio_pin_val(struct dln2_gpio *dln2, int cmd, unsigned int pin)
109 struct dln2_gpio_pin req = {
110 .pin = cpu_to_le16(pin),
112 struct dln2_gpio_pin_val rsp;
113 int len = sizeof(rsp);
115 ret = dln2_transfer(dln2->pdev, cmd, &req, sizeof(req), &rsp, &len);
118 if (len < sizeof(rsp) || req.pin != rsp.pin)
124 static int dln2_gpio_pin_get_in_val(struct dln2_gpio *dln2, unsigned int pin)
128 ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_VAL, pin);
134 static int dln2_gpio_pin_get_out_val(struct dln2_gpio *dln2, unsigned int pin)
138 ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_OUT_VAL, pin);
144 static void dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2,
145 unsigned int pin, int value)
147 struct dln2_gpio_pin_val req = {
148 .pin = cpu_to_le16(pin),
152 dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req,
156 #define DLN2_GPIO_DIRECTION_IN 0
157 #define DLN2_GPIO_DIRECTION_OUT 1
159 static int dln2_gpio_request(struct gpio_chip *chip, unsigned offset)
161 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
162 struct dln2_gpio_pin req = {
163 .pin = cpu_to_le16(offset),
165 struct dln2_gpio_pin_val rsp;
166 int len = sizeof(rsp);
169 ret = dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_ENABLE, offset);
173 /* cache the pin direction */
174 ret = dln2_transfer(dln2->pdev, DLN2_GPIO_PIN_GET_DIRECTION,
175 &req, sizeof(req), &rsp, &len);
178 if (len < sizeof(rsp) || req.pin != rsp.pin) {
184 case DLN2_GPIO_DIRECTION_IN:
185 clear_bit(offset, dln2->output_enabled);
187 case DLN2_GPIO_DIRECTION_OUT:
188 set_bit(offset, dln2->output_enabled);
196 dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
200 static void dln2_gpio_free(struct gpio_chip *chip, unsigned offset)
202 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
204 dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
207 static int dln2_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
209 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
211 if (test_bit(offset, dln2->output_enabled))
212 return GPIOF_DIR_OUT;
217 static int dln2_gpio_get(struct gpio_chip *chip, unsigned int offset)
219 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
222 dir = dln2_gpio_get_direction(chip, offset);
226 if (dir == GPIOF_DIR_IN)
227 return dln2_gpio_pin_get_in_val(dln2, offset);
229 return dln2_gpio_pin_get_out_val(dln2, offset);
232 static void dln2_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
234 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
236 dln2_gpio_pin_set_out_val(dln2, offset, value);
239 static int dln2_gpio_set_direction(struct gpio_chip *chip, unsigned offset,
242 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
243 struct dln2_gpio_pin_val req = {
244 .pin = cpu_to_le16(offset),
249 ret = dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_DIRECTION,
254 if (dir == DLN2_GPIO_DIRECTION_OUT)
255 set_bit(offset, dln2->output_enabled);
257 clear_bit(offset, dln2->output_enabled);
262 static int dln2_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
264 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_IN);
267 static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
270 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT);
273 static int dln2_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
276 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
277 __le32 duration = cpu_to_le32(debounce);
279 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_SET_DEBOUNCE,
280 &duration, sizeof(duration));
283 static int dln2_gpio_set_event_cfg(struct dln2_gpio *dln2, unsigned pin,
284 unsigned type, unsigned period)
291 .pin = cpu_to_le16(pin),
293 .period = cpu_to_le16(period),
296 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_EVENT_CFG,
300 static void dln2_irq_work(struct work_struct *w)
302 struct dln2_irq_work *iw = container_of(w, struct dln2_irq_work, work);
303 struct dln2_gpio *dln2 = iw->dln2;
304 u8 type = iw->type & DLN2_GPIO_EVENT_MASK;
306 if (test_bit(iw->pin, dln2->irqs_enabled))
307 dln2_gpio_set_event_cfg(dln2, iw->pin, type, 0);
309 dln2_gpio_set_event_cfg(dln2, iw->pin, DLN2_GPIO_EVENT_NONE, 0);
312 static void dln2_irq_enable(struct irq_data *irqd)
314 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
315 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
316 int pin = irqd_to_hwirq(irqd);
318 set_bit(pin, dln2->irqs_enabled);
319 schedule_work(&dln2->irq_work[pin].work);
322 static void dln2_irq_disable(struct irq_data *irqd)
324 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
325 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
326 int pin = irqd_to_hwirq(irqd);
328 clear_bit(pin, dln2->irqs_enabled);
329 schedule_work(&dln2->irq_work[pin].work);
332 static void dln2_irq_mask(struct irq_data *irqd)
334 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
335 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
336 int pin = irqd_to_hwirq(irqd);
338 set_bit(pin, dln2->irqs_masked);
341 static void dln2_irq_unmask(struct irq_data *irqd)
343 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
344 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
345 struct device *dev = dln2->gpio.dev;
346 int pin = irqd_to_hwirq(irqd);
348 if (test_and_clear_bit(pin, dln2->irqs_pending)) {
351 irq = irq_find_mapping(dln2->gpio.irqdomain, pin);
353 dev_err(dev, "pin %d not mapped to IRQ\n", pin);
357 generic_handle_irq(irq);
361 static int dln2_irq_set_type(struct irq_data *irqd, unsigned type)
363 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
364 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
365 int pin = irqd_to_hwirq(irqd);
368 case IRQ_TYPE_LEVEL_HIGH:
369 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_LVL_HIGH;
371 case IRQ_TYPE_LEVEL_LOW:
372 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_LVL_LOW;
374 case IRQ_TYPE_EDGE_BOTH:
375 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE;
377 case IRQ_TYPE_EDGE_RISING:
378 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE_RISING;
380 case IRQ_TYPE_EDGE_FALLING:
381 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE_FALLING;
390 static struct irq_chip dln2_gpio_irqchip = {
392 .irq_enable = dln2_irq_enable,
393 .irq_disable = dln2_irq_disable,
394 .irq_mask = dln2_irq_mask,
395 .irq_unmask = dln2_irq_unmask,
396 .irq_set_type = dln2_irq_set_type,
399 static void dln2_gpio_event(struct platform_device *pdev, u16 echo,
400 const void *data, int len)
408 } __packed *event = data;
409 struct dln2_gpio *dln2 = platform_get_drvdata(pdev);
411 if (len < sizeof(*event)) {
412 dev_err(dln2->gpio.dev, "short event message\n");
416 pin = le16_to_cpu(event->pin);
417 if (pin >= dln2->gpio.ngpio) {
418 dev_err(dln2->gpio.dev, "out of bounds pin %d\n", pin);
422 irq = irq_find_mapping(dln2->gpio.irqdomain, pin);
424 dev_err(dln2->gpio.dev, "pin %d not mapped to IRQ\n", pin);
428 if (!test_bit(pin, dln2->irqs_enabled))
430 if (test_bit(pin, dln2->irqs_masked)) {
431 set_bit(pin, dln2->irqs_pending);
435 switch (dln2->irq_work[pin].type) {
436 case DLN2_GPIO_EVENT_CHANGE_RISING:
438 generic_handle_irq(irq);
440 case DLN2_GPIO_EVENT_CHANGE_FALLING:
442 generic_handle_irq(irq);
445 generic_handle_irq(irq);
449 static int dln2_gpio_probe(struct platform_device *pdev)
451 struct dln2_gpio *dln2;
452 struct device *dev = &pdev->dev;
456 pins = dln2_gpio_get_pin_count(pdev);
458 dev_err(dev, "failed to get pin count: %d\n", pins);
461 if (pins > DLN2_GPIO_MAX_PINS) {
462 pins = DLN2_GPIO_MAX_PINS;
463 dev_warn(dev, "clamping pins to %d\n", DLN2_GPIO_MAX_PINS);
466 dln2 = devm_kzalloc(&pdev->dev, sizeof(*dln2), GFP_KERNEL);
470 dln2->irq_work = devm_kcalloc(&pdev->dev, pins,
471 sizeof(struct dln2_irq_work), GFP_KERNEL);
474 for (i = 0; i < pins; i++) {
475 INIT_WORK(&dln2->irq_work[i].work, dln2_irq_work);
476 dln2->irq_work[i].pin = i;
477 dln2->irq_work[i].dln2 = dln2;
482 dln2->gpio.label = "dln2";
483 dln2->gpio.dev = dev;
484 dln2->gpio.owner = THIS_MODULE;
485 dln2->gpio.base = -1;
486 dln2->gpio.ngpio = pins;
487 dln2->gpio.exported = true;
488 dln2->gpio.can_sleep = true;
489 dln2->gpio.irq_not_threaded = true;
490 dln2->gpio.set = dln2_gpio_set;
491 dln2->gpio.get = dln2_gpio_get;
492 dln2->gpio.request = dln2_gpio_request;
493 dln2->gpio.free = dln2_gpio_free;
494 dln2->gpio.get_direction = dln2_gpio_get_direction;
495 dln2->gpio.direction_input = dln2_gpio_direction_input;
496 dln2->gpio.direction_output = dln2_gpio_direction_output;
497 dln2->gpio.set_debounce = dln2_gpio_set_debounce;
499 platform_set_drvdata(pdev, dln2);
501 ret = gpiochip_add(&dln2->gpio);
503 dev_err(dev, "failed to add gpio chip: %d\n", ret);
507 ret = gpiochip_irqchip_add(&dln2->gpio, &dln2_gpio_irqchip, 0,
508 handle_simple_irq, IRQ_TYPE_NONE);
510 dev_err(dev, "failed to add irq chip: %d\n", ret);
511 goto out_gpiochip_remove;
514 ret = dln2_register_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV,
517 dev_err(dev, "failed to register event cb: %d\n", ret);
518 goto out_gpiochip_remove;
524 gpiochip_remove(&dln2->gpio);
529 static int dln2_gpio_remove(struct platform_device *pdev)
531 struct dln2_gpio *dln2 = platform_get_drvdata(pdev);
534 dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV);
535 for (i = 0; i < dln2->gpio.ngpio; i++)
536 flush_work(&dln2->irq_work[i].work);
537 gpiochip_remove(&dln2->gpio);
542 static struct platform_driver dln2_gpio_driver = {
543 .driver.name = "dln2-gpio",
544 .probe = dln2_gpio_probe,
545 .remove = dln2_gpio_remove,
548 module_platform_driver(dln2_gpio_driver);
550 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
551 MODULE_DESCRIPTION("Driver for the Diolan DLN2 GPIO interface");
552 MODULE_LICENSE("GPL v2");
553 MODULE_ALIAS("platform:dln2-gpio");