1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel La Jolla Cove Adapter USB-GPIO driver
5 * Copyright (c) 2023, Intel Corporation.
8 #include <linux/acpi.h>
9 #include <linux/auxiliary_bus.h>
10 #include <linux/bitfield.h>
11 #include <linux/bitops.h>
12 #include <linux/dev_printk.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/kref.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/usb/ljca.h>
23 #define LJCA_GPIO_CONFIG 1
24 #define LJCA_GPIO_READ 2
25 #define LJCA_GPIO_WRITE 3
26 #define LJCA_GPIO_INT_EVENT 4
27 #define LJCA_GPIO_INT_MASK 5
28 #define LJCA_GPIO_INT_UNMASK 6
30 #define LJCA_GPIO_CONF_DISABLE BIT(0)
31 #define LJCA_GPIO_CONF_INPUT BIT(1)
32 #define LJCA_GPIO_CONF_OUTPUT BIT(2)
33 #define LJCA_GPIO_CONF_PULLUP BIT(3)
34 #define LJCA_GPIO_CONF_PULLDOWN BIT(4)
35 #define LJCA_GPIO_CONF_DEFAULT BIT(5)
36 #define LJCA_GPIO_CONF_INTERRUPT BIT(6)
37 #define LJCA_GPIO_INT_TYPE BIT(7)
39 #define LJCA_GPIO_CONF_EDGE FIELD_PREP(LJCA_GPIO_INT_TYPE, 1)
40 #define LJCA_GPIO_CONF_LEVEL FIELD_PREP(LJCA_GPIO_INT_TYPE, 0)
42 /* Intentional overlap with PULLUP / PULLDOWN */
43 #define LJCA_GPIO_CONF_SET BIT(3)
44 #define LJCA_GPIO_CONF_CLR BIT(4)
46 #define LJCA_GPIO_BUF_SIZE 60u
53 struct ljca_gpio_packet {
55 struct ljca_gpio_op item[] __counted_by(num);
58 struct ljca_gpio_dev {
59 struct ljca_client *ljca;
61 struct ljca_gpio_info *gpio_info;
62 DECLARE_BITMAP(unmasked_irqs, LJCA_MAX_GPIO_NUM);
63 DECLARE_BITMAP(enabled_irqs, LJCA_MAX_GPIO_NUM);
64 DECLARE_BITMAP(reenable_irqs, LJCA_MAX_GPIO_NUM);
65 DECLARE_BITMAP(output_enabled, LJCA_MAX_GPIO_NUM);
68 struct mutex irq_lock;
69 struct work_struct work;
70 /* protect package transfer to hardware */
71 struct mutex trans_lock;
73 u8 obuf[LJCA_GPIO_BUF_SIZE];
74 u8 ibuf[LJCA_GPIO_BUF_SIZE];
77 static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
80 struct ljca_gpio_packet *packet =
81 (struct ljca_gpio_packet *)ljca_gpio->obuf;
84 mutex_lock(&ljca_gpio->trans_lock);
85 packet->item[0].index = gpio_id;
86 packet->item[0].value = config | ljca_gpio->connect_mode[gpio_id];
89 ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_CONFIG, (u8 *)packet,
90 struct_size(packet, item, packet->num), NULL, 0);
91 mutex_unlock(&ljca_gpio->trans_lock);
93 return ret < 0 ? ret : 0;
96 static int ljca_gpio_read(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id)
98 struct ljca_gpio_packet *ack_packet =
99 (struct ljca_gpio_packet *)ljca_gpio->ibuf;
100 struct ljca_gpio_packet *packet =
101 (struct ljca_gpio_packet *)ljca_gpio->obuf;
104 mutex_lock(&ljca_gpio->trans_lock);
106 packet->item[0].index = gpio_id;
107 ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_READ, (u8 *)packet,
108 struct_size(packet, item, packet->num),
109 ljca_gpio->ibuf, LJCA_GPIO_BUF_SIZE);
111 if (ret <= 0 || ack_packet->num != packet->num) {
112 dev_err(&ljca_gpio->ljca->auxdev.dev,
113 "read package error, gpio_id: %u num: %u ret: %d\n",
114 gpio_id, ack_packet->num, ret);
115 ret = ret < 0 ? ret : -EIO;
117 mutex_unlock(&ljca_gpio->trans_lock);
119 return ret < 0 ? ret : ack_packet->item[0].value > 0;
122 static int ljca_gpio_write(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id, int value)
124 struct ljca_gpio_packet *packet =
125 (struct ljca_gpio_packet *)ljca_gpio->obuf;
128 mutex_lock(&ljca_gpio->trans_lock);
130 packet->item[0].index = gpio_id;
131 packet->item[0].value = value & 1;
133 ret = ljca_transfer(ljca_gpio->ljca, LJCA_GPIO_WRITE, (u8 *)packet,
134 struct_size(packet, item, packet->num), NULL, 0);
135 mutex_unlock(&ljca_gpio->trans_lock);
137 return ret < 0 ? ret : 0;
140 static int ljca_gpio_get_value(struct gpio_chip *chip, unsigned int offset)
142 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
144 return ljca_gpio_read(ljca_gpio, offset);
147 static void ljca_gpio_set_value(struct gpio_chip *chip, unsigned int offset,
150 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
153 ret = ljca_gpio_write(ljca_gpio, offset, val);
155 dev_err(chip->parent,
156 "set value failed offset: %u val: %d ret: %d\n",
160 static int ljca_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
162 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
163 u8 config = LJCA_GPIO_CONF_INPUT | LJCA_GPIO_CONF_CLR;
166 ret = ljca_gpio_config(ljca_gpio, offset, config);
170 clear_bit(offset, ljca_gpio->output_enabled);
175 static int ljca_gpio_direction_output(struct gpio_chip *chip,
176 unsigned int offset, int val)
178 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
179 u8 config = LJCA_GPIO_CONF_OUTPUT | LJCA_GPIO_CONF_CLR;
182 ret = ljca_gpio_config(ljca_gpio, offset, config);
186 ljca_gpio_set_value(chip, offset, val);
187 set_bit(offset, ljca_gpio->output_enabled);
192 static int ljca_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
194 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
196 if (test_bit(offset, ljca_gpio->output_enabled))
197 return GPIO_LINE_DIRECTION_OUT;
199 return GPIO_LINE_DIRECTION_IN;
202 static int ljca_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
203 unsigned long config)
205 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
207 ljca_gpio->connect_mode[offset] = 0;
208 switch (pinconf_to_config_param(config)) {
209 case PIN_CONFIG_BIAS_PULL_UP:
210 ljca_gpio->connect_mode[offset] |= LJCA_GPIO_CONF_PULLUP;
212 case PIN_CONFIG_BIAS_PULL_DOWN:
213 ljca_gpio->connect_mode[offset] |= LJCA_GPIO_CONF_PULLDOWN;
215 case PIN_CONFIG_DRIVE_PUSH_PULL:
216 case PIN_CONFIG_PERSIST_STATE:
225 static int ljca_gpio_init_valid_mask(struct gpio_chip *chip,
226 unsigned long *valid_mask,
229 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
231 WARN_ON_ONCE(ngpios != ljca_gpio->gpio_info->num);
232 bitmap_copy(valid_mask, ljca_gpio->gpio_info->valid_pin_map, ngpios);
237 static void ljca_gpio_irq_init_valid_mask(struct gpio_chip *chip,
238 unsigned long *valid_mask,
241 ljca_gpio_init_valid_mask(chip, valid_mask, ngpios);
244 static int ljca_enable_irq(struct ljca_gpio_dev *ljca_gpio, int gpio_id,
247 struct ljca_gpio_packet *packet =
248 (struct ljca_gpio_packet *)ljca_gpio->obuf;
251 mutex_lock(&ljca_gpio->trans_lock);
253 packet->item[0].index = gpio_id;
254 packet->item[0].value = 0;
256 ret = ljca_transfer(ljca_gpio->ljca,
257 enable ? LJCA_GPIO_INT_UNMASK : LJCA_GPIO_INT_MASK,
258 (u8 *)packet, struct_size(packet, item, packet->num),
260 mutex_unlock(&ljca_gpio->trans_lock);
262 return ret < 0 ? ret : 0;
265 static void ljca_gpio_async(struct work_struct *work)
267 struct ljca_gpio_dev *ljca_gpio =
268 container_of(work, struct ljca_gpio_dev, work);
269 int gpio_id, unmasked;
271 for_each_set_bit(gpio_id, ljca_gpio->reenable_irqs, ljca_gpio->gc.ngpio) {
272 clear_bit(gpio_id, ljca_gpio->reenable_irqs);
273 unmasked = test_bit(gpio_id, ljca_gpio->unmasked_irqs);
275 ljca_enable_irq(ljca_gpio, gpio_id, true);
279 static void ljca_gpio_event_cb(void *context, u8 cmd, const void *evt_data,
282 const struct ljca_gpio_packet *packet = evt_data;
283 struct ljca_gpio_dev *ljca_gpio = context;
286 if (cmd != LJCA_GPIO_INT_EVENT)
289 for (i = 0; i < packet->num; i++) {
290 irq = irq_find_mapping(ljca_gpio->gc.irq.domain,
291 packet->item[i].index);
293 dev_err(ljca_gpio->gc.parent,
294 "gpio_id %u does not mapped to IRQ yet\n",
295 packet->item[i].index);
299 generic_handle_domain_irq(ljca_gpio->gc.irq.domain, irq);
300 set_bit(packet->item[i].index, ljca_gpio->reenable_irqs);
303 schedule_work(&ljca_gpio->work);
306 static void ljca_irq_unmask(struct irq_data *irqd)
308 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
309 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
310 int gpio_id = irqd_to_hwirq(irqd);
312 gpiochip_enable_irq(gc, gpio_id);
313 set_bit(gpio_id, ljca_gpio->unmasked_irqs);
316 static void ljca_irq_mask(struct irq_data *irqd)
318 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
319 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
320 int gpio_id = irqd_to_hwirq(irqd);
322 clear_bit(gpio_id, ljca_gpio->unmasked_irqs);
323 gpiochip_disable_irq(gc, gpio_id);
326 static int ljca_irq_set_type(struct irq_data *irqd, unsigned int type)
328 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
329 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
330 int gpio_id = irqd_to_hwirq(irqd);
332 ljca_gpio->connect_mode[gpio_id] = LJCA_GPIO_CONF_INTERRUPT;
334 case IRQ_TYPE_LEVEL_HIGH:
335 ljca_gpio->connect_mode[gpio_id] |=
336 (LJCA_GPIO_CONF_LEVEL | LJCA_GPIO_CONF_PULLUP);
338 case IRQ_TYPE_LEVEL_LOW:
339 ljca_gpio->connect_mode[gpio_id] |=
340 (LJCA_GPIO_CONF_LEVEL | LJCA_GPIO_CONF_PULLDOWN);
342 case IRQ_TYPE_EDGE_BOTH:
344 case IRQ_TYPE_EDGE_RISING:
345 ljca_gpio->connect_mode[gpio_id] |=
346 (LJCA_GPIO_CONF_EDGE | LJCA_GPIO_CONF_PULLUP);
348 case IRQ_TYPE_EDGE_FALLING:
349 ljca_gpio->connect_mode[gpio_id] |=
350 (LJCA_GPIO_CONF_EDGE | LJCA_GPIO_CONF_PULLDOWN);
359 static void ljca_irq_bus_lock(struct irq_data *irqd)
361 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
362 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
364 mutex_lock(&ljca_gpio->irq_lock);
367 static void ljca_irq_bus_unlock(struct irq_data *irqd)
369 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
370 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
371 int gpio_id = irqd_to_hwirq(irqd);
372 int enabled, unmasked;
374 enabled = test_bit(gpio_id, ljca_gpio->enabled_irqs);
375 unmasked = test_bit(gpio_id, ljca_gpio->unmasked_irqs);
377 if (enabled != unmasked) {
379 ljca_gpio_config(ljca_gpio, gpio_id, 0);
380 ljca_enable_irq(ljca_gpio, gpio_id, true);
381 set_bit(gpio_id, ljca_gpio->enabled_irqs);
383 ljca_enable_irq(ljca_gpio, gpio_id, false);
384 clear_bit(gpio_id, ljca_gpio->enabled_irqs);
388 mutex_unlock(&ljca_gpio->irq_lock);
391 static const struct irq_chip ljca_gpio_irqchip = {
393 .irq_mask = ljca_irq_mask,
394 .irq_unmask = ljca_irq_unmask,
395 .irq_set_type = ljca_irq_set_type,
396 .irq_bus_lock = ljca_irq_bus_lock,
397 .irq_bus_sync_unlock = ljca_irq_bus_unlock,
398 .flags = IRQCHIP_IMMUTABLE,
399 GPIOCHIP_IRQ_RESOURCE_HELPERS,
402 static int ljca_gpio_probe(struct auxiliary_device *auxdev,
403 const struct auxiliary_device_id *aux_dev_id)
405 struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
406 struct ljca_gpio_dev *ljca_gpio;
407 struct gpio_irq_chip *girq;
410 ljca_gpio = devm_kzalloc(&auxdev->dev, sizeof(*ljca_gpio), GFP_KERNEL);
414 ljca_gpio->ljca = ljca;
415 ljca_gpio->gpio_info = dev_get_platdata(&auxdev->dev);
416 ljca_gpio->connect_mode = devm_kcalloc(&auxdev->dev,
417 ljca_gpio->gpio_info->num,
418 sizeof(*ljca_gpio->connect_mode),
420 if (!ljca_gpio->connect_mode)
423 mutex_init(&ljca_gpio->irq_lock);
424 mutex_init(&ljca_gpio->trans_lock);
425 ljca_gpio->gc.direction_input = ljca_gpio_direction_input;
426 ljca_gpio->gc.direction_output = ljca_gpio_direction_output;
427 ljca_gpio->gc.get_direction = ljca_gpio_get_direction;
428 ljca_gpio->gc.get = ljca_gpio_get_value;
429 ljca_gpio->gc.set = ljca_gpio_set_value;
430 ljca_gpio->gc.set_config = ljca_gpio_set_config;
431 ljca_gpio->gc.init_valid_mask = ljca_gpio_init_valid_mask;
432 ljca_gpio->gc.can_sleep = true;
433 ljca_gpio->gc.parent = &auxdev->dev;
435 ljca_gpio->gc.base = -1;
436 ljca_gpio->gc.ngpio = ljca_gpio->gpio_info->num;
437 ljca_gpio->gc.label = ACPI_COMPANION(&auxdev->dev) ?
438 acpi_dev_name(ACPI_COMPANION(&auxdev->dev)) :
439 dev_name(&auxdev->dev);
440 ljca_gpio->gc.owner = THIS_MODULE;
442 auxiliary_set_drvdata(auxdev, ljca_gpio);
443 ljca_register_event_cb(ljca, ljca_gpio_event_cb, ljca_gpio);
445 girq = &ljca_gpio->gc.irq;
446 gpio_irq_chip_set_chip(girq, &ljca_gpio_irqchip);
447 girq->parent_handler = NULL;
448 girq->num_parents = 0;
449 girq->parents = NULL;
450 girq->default_type = IRQ_TYPE_NONE;
451 girq->handler = handle_simple_irq;
452 girq->init_valid_mask = ljca_gpio_irq_init_valid_mask;
454 INIT_WORK(&ljca_gpio->work, ljca_gpio_async);
455 ret = gpiochip_add_data(&ljca_gpio->gc, ljca_gpio);
457 ljca_unregister_event_cb(ljca);
458 mutex_destroy(&ljca_gpio->irq_lock);
459 mutex_destroy(&ljca_gpio->trans_lock);
465 static void ljca_gpio_remove(struct auxiliary_device *auxdev)
467 struct ljca_gpio_dev *ljca_gpio = auxiliary_get_drvdata(auxdev);
469 gpiochip_remove(&ljca_gpio->gc);
470 ljca_unregister_event_cb(ljca_gpio->ljca);
471 cancel_work_sync(&ljca_gpio->work);
472 mutex_destroy(&ljca_gpio->irq_lock);
473 mutex_destroy(&ljca_gpio->trans_lock);
476 static const struct auxiliary_device_id ljca_gpio_id_table[] = {
477 { "usb_ljca.ljca-gpio", 0 },
480 MODULE_DEVICE_TABLE(auxiliary, ljca_gpio_id_table);
482 static struct auxiliary_driver ljca_gpio_driver = {
483 .probe = ljca_gpio_probe,
484 .remove = ljca_gpio_remove,
485 .id_table = ljca_gpio_id_table,
487 module_auxiliary_driver(ljca_gpio_driver);
492 MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB-GPIO driver");
493 MODULE_LICENSE("GPL");
494 MODULE_IMPORT_NS(LJCA);