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/driver.h>
19 #include <linux/platform_device.h>
20 #include <linux/mfd/dln2.h>
22 #define DLN2_GPIO_ID 0x01
24 #define DLN2_GPIO_GET_PIN_COUNT DLN2_CMD(0x01, DLN2_GPIO_ID)
25 #define DLN2_GPIO_SET_DEBOUNCE DLN2_CMD(0x04, DLN2_GPIO_ID)
26 #define DLN2_GPIO_GET_DEBOUNCE DLN2_CMD(0x05, DLN2_GPIO_ID)
27 #define DLN2_GPIO_PORT_GET_VAL DLN2_CMD(0x06, DLN2_GPIO_ID)
28 #define DLN2_GPIO_PIN_GET_VAL DLN2_CMD(0x0B, DLN2_GPIO_ID)
29 #define DLN2_GPIO_PIN_SET_OUT_VAL DLN2_CMD(0x0C, DLN2_GPIO_ID)
30 #define DLN2_GPIO_PIN_GET_OUT_VAL DLN2_CMD(0x0D, DLN2_GPIO_ID)
31 #define DLN2_GPIO_CONDITION_MET_EV DLN2_CMD(0x0F, DLN2_GPIO_ID)
32 #define DLN2_GPIO_PIN_ENABLE DLN2_CMD(0x10, DLN2_GPIO_ID)
33 #define DLN2_GPIO_PIN_DISABLE DLN2_CMD(0x11, DLN2_GPIO_ID)
34 #define DLN2_GPIO_PIN_SET_DIRECTION DLN2_CMD(0x13, DLN2_GPIO_ID)
35 #define DLN2_GPIO_PIN_GET_DIRECTION DLN2_CMD(0x14, DLN2_GPIO_ID)
36 #define DLN2_GPIO_PIN_SET_EVENT_CFG DLN2_CMD(0x1E, DLN2_GPIO_ID)
37 #define DLN2_GPIO_PIN_GET_EVENT_CFG DLN2_CMD(0x1F, DLN2_GPIO_ID)
39 #define DLN2_GPIO_EVENT_NONE 0
40 #define DLN2_GPIO_EVENT_CHANGE 1
41 #define DLN2_GPIO_EVENT_LVL_HIGH 2
42 #define DLN2_GPIO_EVENT_LVL_LOW 3
43 #define DLN2_GPIO_EVENT_CHANGE_RISING 0x11
44 #define DLN2_GPIO_EVENT_CHANGE_FALLING 0x21
45 #define DLN2_GPIO_EVENT_MASK 0x0F
47 #define DLN2_GPIO_MAX_PINS 32
50 struct platform_device *pdev;
51 struct gpio_chip gpio;
54 * Cache pin direction to save us one transfer, since the hardware has
55 * separate commands to read the in and out values.
57 DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS);
59 /* active IRQs - not synced to hardware */
60 DECLARE_BITMAP(unmasked_irqs, DLN2_GPIO_MAX_PINS);
61 /* active IRQS - synced to hardware */
62 DECLARE_BITMAP(enabled_irqs, DLN2_GPIO_MAX_PINS);
63 int irq_type[DLN2_GPIO_MAX_PINS];
64 struct mutex irq_lock;
67 struct dln2_gpio_pin {
71 struct dln2_gpio_pin_val {
76 static int dln2_gpio_get_pin_count(struct platform_device *pdev)
80 int len = sizeof(count);
82 ret = dln2_transfer_rx(pdev, DLN2_GPIO_GET_PIN_COUNT, &count, &len);
85 if (len < sizeof(count))
88 return le16_to_cpu(count);
91 static int dln2_gpio_pin_cmd(struct dln2_gpio *dln2, int cmd, unsigned pin)
93 struct dln2_gpio_pin req = {
94 .pin = cpu_to_le16(pin),
97 return dln2_transfer_tx(dln2->pdev, cmd, &req, sizeof(req));
100 static int dln2_gpio_pin_val(struct dln2_gpio *dln2, int cmd, unsigned int pin)
103 struct dln2_gpio_pin req = {
104 .pin = cpu_to_le16(pin),
106 struct dln2_gpio_pin_val rsp;
107 int len = sizeof(rsp);
109 ret = dln2_transfer(dln2->pdev, cmd, &req, sizeof(req), &rsp, &len);
112 if (len < sizeof(rsp) || req.pin != rsp.pin)
118 static int dln2_gpio_pin_get_in_val(struct dln2_gpio *dln2, unsigned int pin)
122 ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_VAL, pin);
128 static int dln2_gpio_pin_get_out_val(struct dln2_gpio *dln2, unsigned int pin)
132 ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_OUT_VAL, pin);
138 static int dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2,
139 unsigned int pin, int value)
141 struct dln2_gpio_pin_val req = {
142 .pin = cpu_to_le16(pin),
146 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req,
150 #define DLN2_GPIO_DIRECTION_IN 0
151 #define DLN2_GPIO_DIRECTION_OUT 1
153 static int dln2_gpio_request(struct gpio_chip *chip, unsigned offset)
155 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
156 struct dln2_gpio_pin req = {
157 .pin = cpu_to_le16(offset),
159 struct dln2_gpio_pin_val rsp;
160 int len = sizeof(rsp);
163 ret = dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_ENABLE, offset);
167 /* cache the pin direction */
168 ret = dln2_transfer(dln2->pdev, DLN2_GPIO_PIN_GET_DIRECTION,
169 &req, sizeof(req), &rsp, &len);
172 if (len < sizeof(rsp) || req.pin != rsp.pin) {
178 case DLN2_GPIO_DIRECTION_IN:
179 clear_bit(offset, dln2->output_enabled);
181 case DLN2_GPIO_DIRECTION_OUT:
182 set_bit(offset, dln2->output_enabled);
190 dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
194 static void dln2_gpio_free(struct gpio_chip *chip, unsigned offset)
196 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
198 dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
201 static int dln2_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
203 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
205 if (test_bit(offset, dln2->output_enabled))
211 static int dln2_gpio_get(struct gpio_chip *chip, unsigned int offset)
213 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
216 dir = dln2_gpio_get_direction(chip, offset);
221 return dln2_gpio_pin_get_in_val(dln2, offset);
223 return dln2_gpio_pin_get_out_val(dln2, offset);
226 static void dln2_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
228 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
230 dln2_gpio_pin_set_out_val(dln2, offset, value);
233 static int dln2_gpio_set_direction(struct gpio_chip *chip, unsigned offset,
236 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
237 struct dln2_gpio_pin_val req = {
238 .pin = cpu_to_le16(offset),
243 ret = dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_DIRECTION,
248 if (dir == DLN2_GPIO_DIRECTION_OUT)
249 set_bit(offset, dln2->output_enabled);
251 clear_bit(offset, dln2->output_enabled);
256 static int dln2_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
258 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_IN);
261 static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
264 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
267 ret = dln2_gpio_pin_set_out_val(dln2, offset, value);
271 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT);
274 static int dln2_gpio_set_config(struct gpio_chip *chip, unsigned offset,
275 unsigned long config)
277 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
280 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
283 duration = cpu_to_le32(pinconf_to_config_argument(config));
284 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_SET_DEBOUNCE,
285 &duration, sizeof(duration));
288 static int dln2_gpio_set_event_cfg(struct dln2_gpio *dln2, unsigned pin,
289 unsigned type, unsigned period)
296 .pin = cpu_to_le16(pin),
298 .period = cpu_to_le16(period),
301 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_EVENT_CFG,
305 static void dln2_irq_unmask(struct irq_data *irqd)
307 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
308 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
309 int pin = irqd_to_hwirq(irqd);
311 set_bit(pin, dln2->unmasked_irqs);
314 static void dln2_irq_mask(struct irq_data *irqd)
316 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
317 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
318 int pin = irqd_to_hwirq(irqd);
320 clear_bit(pin, dln2->unmasked_irqs);
323 static int dln2_irq_set_type(struct irq_data *irqd, unsigned type)
325 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
326 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
327 int pin = irqd_to_hwirq(irqd);
330 case IRQ_TYPE_LEVEL_HIGH:
331 dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_HIGH;
333 case IRQ_TYPE_LEVEL_LOW:
334 dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_LOW;
336 case IRQ_TYPE_EDGE_BOTH:
337 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE;
339 case IRQ_TYPE_EDGE_RISING:
340 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_RISING;
342 case IRQ_TYPE_EDGE_FALLING:
343 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_FALLING;
352 static void dln2_irq_bus_lock(struct irq_data *irqd)
354 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
355 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
357 mutex_lock(&dln2->irq_lock);
360 static void dln2_irq_bus_unlock(struct irq_data *irqd)
362 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
363 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
364 int pin = irqd_to_hwirq(irqd);
365 int enabled, unmasked;
369 enabled = test_bit(pin, dln2->enabled_irqs);
370 unmasked = test_bit(pin, dln2->unmasked_irqs);
372 if (enabled != unmasked) {
374 type = dln2->irq_type[pin] & DLN2_GPIO_EVENT_MASK;
375 set_bit(pin, dln2->enabled_irqs);
377 type = DLN2_GPIO_EVENT_NONE;
378 clear_bit(pin, dln2->enabled_irqs);
381 ret = dln2_gpio_set_event_cfg(dln2, pin, type, 0);
383 dev_err(dln2->gpio.parent, "failed to set event\n");
386 mutex_unlock(&dln2->irq_lock);
389 static struct irq_chip dln2_gpio_irqchip = {
391 .irq_mask = dln2_irq_mask,
392 .irq_unmask = dln2_irq_unmask,
393 .irq_set_type = dln2_irq_set_type,
394 .irq_bus_lock = dln2_irq_bus_lock,
395 .irq_bus_sync_unlock = dln2_irq_bus_unlock,
398 static void dln2_gpio_event(struct platform_device *pdev, u16 echo,
399 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.parent, "short event message\n");
416 pin = le16_to_cpu(event->pin);
417 if (pin >= dln2->gpio.ngpio) {
418 dev_err(dln2->gpio.parent, "out of bounds pin %d\n", pin);
422 irq = irq_find_mapping(dln2->gpio.irq.domain, pin);
424 dev_err(dln2->gpio.parent, "pin %d not mapped to IRQ\n", pin);
428 switch (dln2->irq_type[pin]) {
429 case DLN2_GPIO_EVENT_CHANGE_RISING:
431 generic_handle_irq(irq);
433 case DLN2_GPIO_EVENT_CHANGE_FALLING:
435 generic_handle_irq(irq);
438 generic_handle_irq(irq);
442 static int dln2_gpio_probe(struct platform_device *pdev)
444 struct dln2_gpio *dln2;
445 struct device *dev = &pdev->dev;
449 pins = dln2_gpio_get_pin_count(pdev);
451 dev_err(dev, "failed to get pin count: %d\n", pins);
454 if (pins > DLN2_GPIO_MAX_PINS) {
455 pins = DLN2_GPIO_MAX_PINS;
456 dev_warn(dev, "clamping pins to %d\n", DLN2_GPIO_MAX_PINS);
459 dln2 = devm_kzalloc(&pdev->dev, sizeof(*dln2), GFP_KERNEL);
463 mutex_init(&dln2->irq_lock);
467 dln2->gpio.label = "dln2";
468 dln2->gpio.parent = dev;
469 dln2->gpio.owner = THIS_MODULE;
470 dln2->gpio.base = -1;
471 dln2->gpio.ngpio = pins;
472 dln2->gpio.can_sleep = true;
473 dln2->gpio.set = dln2_gpio_set;
474 dln2->gpio.get = dln2_gpio_get;
475 dln2->gpio.request = dln2_gpio_request;
476 dln2->gpio.free = dln2_gpio_free;
477 dln2->gpio.get_direction = dln2_gpio_get_direction;
478 dln2->gpio.direction_input = dln2_gpio_direction_input;
479 dln2->gpio.direction_output = dln2_gpio_direction_output;
480 dln2->gpio.set_config = dln2_gpio_set_config;
482 platform_set_drvdata(pdev, dln2);
484 ret = devm_gpiochip_add_data(dev, &dln2->gpio, dln2);
486 dev_err(dev, "failed to add gpio chip: %d\n", ret);
490 ret = gpiochip_irqchip_add(&dln2->gpio, &dln2_gpio_irqchip, 0,
491 handle_simple_irq, IRQ_TYPE_NONE);
493 dev_err(dev, "failed to add irq chip: %d\n", ret);
497 ret = dln2_register_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV,
500 dev_err(dev, "failed to register event cb: %d\n", ret);
507 static int dln2_gpio_remove(struct platform_device *pdev)
509 dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV);
514 static struct platform_driver dln2_gpio_driver = {
515 .driver.name = "dln2-gpio",
516 .probe = dln2_gpio_probe,
517 .remove = dln2_gpio_remove,
520 module_platform_driver(dln2_gpio_driver);
523 MODULE_DESCRIPTION("Driver for the Diolan DLN2 GPIO interface");
524 MODULE_LICENSE("GPL v2");
525 MODULE_ALIAS("platform:dln2-gpio");