1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Diolan DLN-2 USB-GPIO adapter
5 * Copyright (c) 2014 Intel Corporation
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/types.h>
12 #include <linux/irqdomain.h>
13 #include <linux/irq.h>
14 #include <linux/irqchip/chained_irq.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/platform_device.h>
17 #include <linux/mfd/dln2.h>
19 #define DLN2_GPIO_ID 0x01
21 #define DLN2_GPIO_GET_PIN_COUNT DLN2_CMD(0x01, DLN2_GPIO_ID)
22 #define DLN2_GPIO_SET_DEBOUNCE DLN2_CMD(0x04, DLN2_GPIO_ID)
23 #define DLN2_GPIO_GET_DEBOUNCE DLN2_CMD(0x05, DLN2_GPIO_ID)
24 #define DLN2_GPIO_PORT_GET_VAL DLN2_CMD(0x06, DLN2_GPIO_ID)
25 #define DLN2_GPIO_PIN_GET_VAL DLN2_CMD(0x0B, DLN2_GPIO_ID)
26 #define DLN2_GPIO_PIN_SET_OUT_VAL DLN2_CMD(0x0C, DLN2_GPIO_ID)
27 #define DLN2_GPIO_PIN_GET_OUT_VAL DLN2_CMD(0x0D, DLN2_GPIO_ID)
28 #define DLN2_GPIO_CONDITION_MET_EV DLN2_CMD(0x0F, DLN2_GPIO_ID)
29 #define DLN2_GPIO_PIN_ENABLE DLN2_CMD(0x10, DLN2_GPIO_ID)
30 #define DLN2_GPIO_PIN_DISABLE DLN2_CMD(0x11, DLN2_GPIO_ID)
31 #define DLN2_GPIO_PIN_SET_DIRECTION DLN2_CMD(0x13, DLN2_GPIO_ID)
32 #define DLN2_GPIO_PIN_GET_DIRECTION DLN2_CMD(0x14, DLN2_GPIO_ID)
33 #define DLN2_GPIO_PIN_SET_EVENT_CFG DLN2_CMD(0x1E, DLN2_GPIO_ID)
34 #define DLN2_GPIO_PIN_GET_EVENT_CFG DLN2_CMD(0x1F, DLN2_GPIO_ID)
36 #define DLN2_GPIO_EVENT_NONE 0
37 #define DLN2_GPIO_EVENT_CHANGE 1
38 #define DLN2_GPIO_EVENT_LVL_HIGH 2
39 #define DLN2_GPIO_EVENT_LVL_LOW 3
40 #define DLN2_GPIO_EVENT_CHANGE_RISING 0x11
41 #define DLN2_GPIO_EVENT_CHANGE_FALLING 0x21
42 #define DLN2_GPIO_EVENT_MASK 0x0F
44 #define DLN2_GPIO_MAX_PINS 32
47 struct platform_device *pdev;
48 struct gpio_chip gpio;
51 * Cache pin direction to save us one transfer, since the hardware has
52 * separate commands to read the in and out values.
54 DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS);
56 /* active IRQs - not synced to hardware */
57 DECLARE_BITMAP(unmasked_irqs, DLN2_GPIO_MAX_PINS);
58 /* active IRQS - synced to hardware */
59 DECLARE_BITMAP(enabled_irqs, DLN2_GPIO_MAX_PINS);
60 int irq_type[DLN2_GPIO_MAX_PINS];
61 struct mutex irq_lock;
64 struct dln2_gpio_pin {
68 struct dln2_gpio_pin_val {
73 static int dln2_gpio_get_pin_count(struct platform_device *pdev)
77 int len = sizeof(count);
79 ret = dln2_transfer_rx(pdev, DLN2_GPIO_GET_PIN_COUNT, &count, &len);
82 if (len < sizeof(count))
85 return le16_to_cpu(count);
88 static int dln2_gpio_pin_cmd(struct dln2_gpio *dln2, int cmd, unsigned pin)
90 struct dln2_gpio_pin req = {
91 .pin = cpu_to_le16(pin),
94 return dln2_transfer_tx(dln2->pdev, cmd, &req, sizeof(req));
97 static int dln2_gpio_pin_val(struct dln2_gpio *dln2, int cmd, unsigned int pin)
100 struct dln2_gpio_pin req = {
101 .pin = cpu_to_le16(pin),
103 struct dln2_gpio_pin_val rsp;
104 int len = sizeof(rsp);
106 ret = dln2_transfer(dln2->pdev, cmd, &req, sizeof(req), &rsp, &len);
109 if (len < sizeof(rsp) || req.pin != rsp.pin)
115 static int dln2_gpio_pin_get_in_val(struct dln2_gpio *dln2, unsigned int pin)
119 ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_VAL, pin);
125 static int dln2_gpio_pin_get_out_val(struct dln2_gpio *dln2, unsigned int pin)
129 ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_OUT_VAL, pin);
135 static int dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2,
136 unsigned int pin, int value)
138 struct dln2_gpio_pin_val req = {
139 .pin = cpu_to_le16(pin),
143 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req,
147 #define DLN2_GPIO_DIRECTION_IN 0
148 #define DLN2_GPIO_DIRECTION_OUT 1
150 static int dln2_gpio_request(struct gpio_chip *chip, unsigned offset)
152 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
153 struct dln2_gpio_pin req = {
154 .pin = cpu_to_le16(offset),
156 struct dln2_gpio_pin_val rsp;
157 int len = sizeof(rsp);
160 ret = dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_ENABLE, offset);
164 /* cache the pin direction */
165 ret = dln2_transfer(dln2->pdev, DLN2_GPIO_PIN_GET_DIRECTION,
166 &req, sizeof(req), &rsp, &len);
169 if (len < sizeof(rsp) || req.pin != rsp.pin) {
175 case DLN2_GPIO_DIRECTION_IN:
176 clear_bit(offset, dln2->output_enabled);
178 case DLN2_GPIO_DIRECTION_OUT:
179 set_bit(offset, dln2->output_enabled);
187 dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
191 static void dln2_gpio_free(struct gpio_chip *chip, unsigned offset)
193 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
195 dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset);
198 static int dln2_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
200 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
202 if (test_bit(offset, dln2->output_enabled))
203 return GPIO_LINE_DIRECTION_OUT;
205 return GPIO_LINE_DIRECTION_IN;
208 static int dln2_gpio_get(struct gpio_chip *chip, unsigned int offset)
210 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
213 dir = dln2_gpio_get_direction(chip, offset);
217 if (dir == GPIO_LINE_DIRECTION_IN)
218 return dln2_gpio_pin_get_in_val(dln2, offset);
220 return dln2_gpio_pin_get_out_val(dln2, offset);
223 static void dln2_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
225 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
227 dln2_gpio_pin_set_out_val(dln2, offset, value);
230 static int dln2_gpio_set_direction(struct gpio_chip *chip, unsigned offset,
233 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
234 struct dln2_gpio_pin_val req = {
235 .pin = cpu_to_le16(offset),
240 ret = dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_DIRECTION,
245 if (dir == DLN2_GPIO_DIRECTION_OUT)
246 set_bit(offset, dln2->output_enabled);
248 clear_bit(offset, dln2->output_enabled);
253 static int dln2_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
255 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_IN);
258 static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
261 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
264 ret = dln2_gpio_pin_set_out_val(dln2, offset, value);
268 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT);
271 static int dln2_gpio_set_config(struct gpio_chip *chip, unsigned offset,
272 unsigned long config)
274 struct dln2_gpio *dln2 = gpiochip_get_data(chip);
277 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
280 duration = cpu_to_le32(pinconf_to_config_argument(config));
281 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_SET_DEBOUNCE,
282 &duration, sizeof(duration));
285 static int dln2_gpio_set_event_cfg(struct dln2_gpio *dln2, unsigned pin,
286 unsigned type, unsigned period)
293 .pin = cpu_to_le16(pin),
295 .period = cpu_to_le16(period),
298 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_EVENT_CFG,
302 static void dln2_irq_unmask(struct irq_data *irqd)
304 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
305 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
306 int pin = irqd_to_hwirq(irqd);
308 set_bit(pin, dln2->unmasked_irqs);
311 static void dln2_irq_mask(struct irq_data *irqd)
313 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
314 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
315 int pin = irqd_to_hwirq(irqd);
317 clear_bit(pin, dln2->unmasked_irqs);
320 static int dln2_irq_set_type(struct irq_data *irqd, unsigned type)
322 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
323 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
324 int pin = irqd_to_hwirq(irqd);
327 case IRQ_TYPE_LEVEL_HIGH:
328 dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_HIGH;
330 case IRQ_TYPE_LEVEL_LOW:
331 dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_LOW;
333 case IRQ_TYPE_EDGE_BOTH:
334 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE;
336 case IRQ_TYPE_EDGE_RISING:
337 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_RISING;
339 case IRQ_TYPE_EDGE_FALLING:
340 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_FALLING;
349 static void dln2_irq_bus_lock(struct irq_data *irqd)
351 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
352 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
354 mutex_lock(&dln2->irq_lock);
357 static void dln2_irq_bus_unlock(struct irq_data *irqd)
359 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
360 struct dln2_gpio *dln2 = gpiochip_get_data(gc);
361 int pin = irqd_to_hwirq(irqd);
362 int enabled, unmasked;
366 enabled = test_bit(pin, dln2->enabled_irqs);
367 unmasked = test_bit(pin, dln2->unmasked_irqs);
369 if (enabled != unmasked) {
371 type = dln2->irq_type[pin] & DLN2_GPIO_EVENT_MASK;
372 set_bit(pin, dln2->enabled_irqs);
374 type = DLN2_GPIO_EVENT_NONE;
375 clear_bit(pin, dln2->enabled_irqs);
378 ret = dln2_gpio_set_event_cfg(dln2, pin, type, 0);
380 dev_err(dln2->gpio.parent, "failed to set event\n");
383 mutex_unlock(&dln2->irq_lock);
386 static struct irq_chip dln2_gpio_irqchip = {
388 .irq_mask = dln2_irq_mask,
389 .irq_unmask = dln2_irq_unmask,
390 .irq_set_type = dln2_irq_set_type,
391 .irq_bus_lock = dln2_irq_bus_lock,
392 .irq_bus_sync_unlock = dln2_irq_bus_unlock,
395 static void dln2_gpio_event(struct platform_device *pdev, u16 echo,
396 const void *data, int len)
405 } __packed *event = data;
406 struct dln2_gpio *dln2 = platform_get_drvdata(pdev);
408 if (len < sizeof(*event)) {
409 dev_err(dln2->gpio.parent, "short event message\n");
413 pin = le16_to_cpu(event->pin);
414 if (pin >= dln2->gpio.ngpio) {
415 dev_err(dln2->gpio.parent, "out of bounds pin %d\n", pin);
419 switch (dln2->irq_type[pin]) {
420 case DLN2_GPIO_EVENT_CHANGE_RISING:
424 case DLN2_GPIO_EVENT_CHANGE_FALLING:
430 ret = generic_handle_domain_irq(dln2->gpio.irq.domain, pin);
432 dev_err(dln2->gpio.parent, "pin %d not mapped to IRQ\n", pin);
435 static int dln2_gpio_probe(struct platform_device *pdev)
437 struct dln2_gpio *dln2;
438 struct device *dev = &pdev->dev;
439 struct gpio_irq_chip *girq;
443 pins = dln2_gpio_get_pin_count(pdev);
445 dev_err(dev, "failed to get pin count: %d\n", pins);
448 if (pins > DLN2_GPIO_MAX_PINS) {
449 pins = DLN2_GPIO_MAX_PINS;
450 dev_warn(dev, "clamping pins to %d\n", DLN2_GPIO_MAX_PINS);
453 dln2 = devm_kzalloc(&pdev->dev, sizeof(*dln2), GFP_KERNEL);
457 mutex_init(&dln2->irq_lock);
461 dln2->gpio.label = "dln2";
462 dln2->gpio.parent = dev;
463 dln2->gpio.owner = THIS_MODULE;
464 dln2->gpio.base = -1;
465 dln2->gpio.ngpio = pins;
466 dln2->gpio.can_sleep = true;
467 dln2->gpio.set = dln2_gpio_set;
468 dln2->gpio.get = dln2_gpio_get;
469 dln2->gpio.request = dln2_gpio_request;
470 dln2->gpio.free = dln2_gpio_free;
471 dln2->gpio.get_direction = dln2_gpio_get_direction;
472 dln2->gpio.direction_input = dln2_gpio_direction_input;
473 dln2->gpio.direction_output = dln2_gpio_direction_output;
474 dln2->gpio.set_config = dln2_gpio_set_config;
476 girq = &dln2->gpio.irq;
477 girq->chip = &dln2_gpio_irqchip;
478 /* The event comes from the outside so no parent handler */
479 girq->parent_handler = NULL;
480 girq->num_parents = 0;
481 girq->parents = NULL;
482 girq->default_type = IRQ_TYPE_NONE;
483 girq->handler = handle_simple_irq;
485 platform_set_drvdata(pdev, dln2);
487 ret = devm_gpiochip_add_data(dev, &dln2->gpio, dln2);
489 dev_err(dev, "failed to add gpio chip: %d\n", ret);
493 ret = dln2_register_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV,
496 dev_err(dev, "failed to register event cb: %d\n", ret);
503 static int dln2_gpio_remove(struct platform_device *pdev)
505 dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV);
510 static struct platform_driver dln2_gpio_driver = {
511 .driver.name = "dln2-gpio",
512 .probe = dln2_gpio_probe,
513 .remove = dln2_gpio_remove,
516 module_platform_driver(dln2_gpio_driver);
518 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
519 MODULE_DESCRIPTION("Driver for the Diolan DLN2 GPIO interface");
520 MODULE_LICENSE("GPL v2");
521 MODULE_ALIAS("platform:dln2-gpio");