1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Intel La Jolla Cove Adapter USB-GPIO driver
4 *
5 * Copyright (c) 2023, Intel Corporation.
6 */
7
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>
21
22 /* GPIO commands */
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
29
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)
38
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)
41
42 /* Intentional overlap with PULLUP / PULLDOWN */
43 #define LJCA_GPIO_CONF_SET BIT(3)
44 #define LJCA_GPIO_CONF_CLR BIT(4)
45
46 #define LJCA_GPIO_BUF_SIZE 60u
47
48 struct ljca_gpio_op {
49 u8 index;
50 u8 value;
51 } __packed;
52
53 struct ljca_gpio_packet {
54 u8 num;
55 struct ljca_gpio_op item[] __counted_by(num);
56 } __packed;
57
58 struct ljca_gpio_dev {
59 struct ljca_client *ljca;
60 struct gpio_chip gc;
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);
66 u8 *connect_mode;
67 /* protect irq bus */
68 struct mutex irq_lock;
69 struct work_struct work;
70 /* protect package transfer to hardware */
71 struct mutex trans_lock;
72
73 u8 obuf[LJCA_GPIO_BUF_SIZE];
74 u8 ibuf[LJCA_GPIO_BUF_SIZE];
75 };
76
ljca_gpio_config(struct ljca_gpio_dev * ljca_gpio,u8 gpio_id,u8 config)77 static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
78 u8 config)
79 {
80 struct ljca_gpio_packet *packet =
81 (struct ljca_gpio_packet *)ljca_gpio->obuf;
82 int ret;
83
84 mutex_lock(&ljca_gpio->trans_lock);
85 packet->num = 1;
86 packet->item[0].index = gpio_id;
87 packet->item[0].value = config | ljca_gpio->connect_mode[gpio_id];
88
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);
92
93 return ret < 0 ? ret : 0;
94 }
95
ljca_gpio_read(struct ljca_gpio_dev * ljca_gpio,u8 gpio_id)96 static int ljca_gpio_read(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id)
97 {
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;
102 int ret;
103
104 mutex_lock(&ljca_gpio->trans_lock);
105 packet->num = 1;
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);
110
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;
116 }
117 mutex_unlock(&ljca_gpio->trans_lock);
118
119 return ret < 0 ? ret : ack_packet->item[0].value > 0;
120 }
121
ljca_gpio_write(struct ljca_gpio_dev * ljca_gpio,u8 gpio_id,int value)122 static int ljca_gpio_write(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id, int value)
123 {
124 struct ljca_gpio_packet *packet =
125 (struct ljca_gpio_packet *)ljca_gpio->obuf;
126 int ret;
127
128 mutex_lock(&ljca_gpio->trans_lock);
129 packet->num = 1;
130 packet->item[0].index = gpio_id;
131 packet->item[0].value = value & 1;
132
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);
136
137 return ret < 0 ? ret : 0;
138 }
139
ljca_gpio_get_value(struct gpio_chip * chip,unsigned int offset)140 static int ljca_gpio_get_value(struct gpio_chip *chip, unsigned int offset)
141 {
142 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
143
144 return ljca_gpio_read(ljca_gpio, offset);
145 }
146
ljca_gpio_set_value(struct gpio_chip * chip,unsigned int offset,int val)147 static int ljca_gpio_set_value(struct gpio_chip *chip, unsigned int offset,
148 int val)
149 {
150 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
151 int ret;
152
153 ret = ljca_gpio_write(ljca_gpio, offset, val);
154 if (ret)
155 dev_err(chip->parent,
156 "set value failed offset: %u val: %d ret: %d\n",
157 offset, val, ret);
158
159 return ret;
160 }
161
ljca_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)162 static int ljca_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
163 {
164 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
165 u8 config = LJCA_GPIO_CONF_INPUT | LJCA_GPIO_CONF_CLR;
166 int ret;
167
168 ret = ljca_gpio_config(ljca_gpio, offset, config);
169 if (ret)
170 return ret;
171
172 clear_bit(offset, ljca_gpio->output_enabled);
173
174 return 0;
175 }
176
ljca_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int val)177 static int ljca_gpio_direction_output(struct gpio_chip *chip,
178 unsigned int offset, int val)
179 {
180 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
181 u8 config = LJCA_GPIO_CONF_OUTPUT | LJCA_GPIO_CONF_CLR;
182 int ret;
183
184 ret = ljca_gpio_config(ljca_gpio, offset, config);
185 if (ret)
186 return ret;
187
188 ret = ljca_gpio_set_value(chip, offset, val);
189 if (ret)
190 return ret;
191
192 set_bit(offset, ljca_gpio->output_enabled);
193
194 return 0;
195 }
196
ljca_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)197 static int ljca_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
198 {
199 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
200
201 if (test_bit(offset, ljca_gpio->output_enabled))
202 return GPIO_LINE_DIRECTION_OUT;
203
204 return GPIO_LINE_DIRECTION_IN;
205 }
206
ljca_gpio_set_config(struct gpio_chip * chip,unsigned int offset,unsigned long config)207 static int ljca_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
208 unsigned long config)
209 {
210 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
211
212 ljca_gpio->connect_mode[offset] = 0;
213 switch (pinconf_to_config_param(config)) {
214 case PIN_CONFIG_BIAS_PULL_UP:
215 ljca_gpio->connect_mode[offset] |= LJCA_GPIO_CONF_PULLUP;
216 break;
217 case PIN_CONFIG_BIAS_PULL_DOWN:
218 ljca_gpio->connect_mode[offset] |= LJCA_GPIO_CONF_PULLDOWN;
219 break;
220 case PIN_CONFIG_DRIVE_PUSH_PULL:
221 case PIN_CONFIG_PERSIST_STATE:
222 break;
223 default:
224 return -ENOTSUPP;
225 }
226
227 return 0;
228 }
229
ljca_gpio_init_valid_mask(struct gpio_chip * chip,unsigned long * valid_mask,unsigned int ngpios)230 static int ljca_gpio_init_valid_mask(struct gpio_chip *chip,
231 unsigned long *valid_mask,
232 unsigned int ngpios)
233 {
234 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(chip);
235
236 WARN_ON_ONCE(ngpios != ljca_gpio->gpio_info->num);
237 bitmap_copy(valid_mask, ljca_gpio->gpio_info->valid_pin_map, ngpios);
238
239 return 0;
240 }
241
ljca_gpio_irq_init_valid_mask(struct gpio_chip * chip,unsigned long * valid_mask,unsigned int ngpios)242 static void ljca_gpio_irq_init_valid_mask(struct gpio_chip *chip,
243 unsigned long *valid_mask,
244 unsigned int ngpios)
245 {
246 ljca_gpio_init_valid_mask(chip, valid_mask, ngpios);
247 }
248
ljca_enable_irq(struct ljca_gpio_dev * ljca_gpio,int gpio_id,bool enable)249 static int ljca_enable_irq(struct ljca_gpio_dev *ljca_gpio, int gpio_id,
250 bool enable)
251 {
252 struct ljca_gpio_packet *packet =
253 (struct ljca_gpio_packet *)ljca_gpio->obuf;
254 int ret;
255
256 mutex_lock(&ljca_gpio->trans_lock);
257 packet->num = 1;
258 packet->item[0].index = gpio_id;
259 packet->item[0].value = 0;
260
261 ret = ljca_transfer(ljca_gpio->ljca,
262 enable ? LJCA_GPIO_INT_UNMASK : LJCA_GPIO_INT_MASK,
263 (u8 *)packet, struct_size(packet, item, packet->num),
264 NULL, 0);
265 mutex_unlock(&ljca_gpio->trans_lock);
266
267 return ret < 0 ? ret : 0;
268 }
269
ljca_gpio_async(struct work_struct * work)270 static void ljca_gpio_async(struct work_struct *work)
271 {
272 struct ljca_gpio_dev *ljca_gpio =
273 container_of(work, struct ljca_gpio_dev, work);
274 int gpio_id, unmasked;
275
276 for_each_set_bit(gpio_id, ljca_gpio->reenable_irqs, ljca_gpio->gc.ngpio) {
277 clear_bit(gpio_id, ljca_gpio->reenable_irqs);
278 unmasked = test_bit(gpio_id, ljca_gpio->unmasked_irqs);
279 if (unmasked)
280 ljca_enable_irq(ljca_gpio, gpio_id, true);
281 }
282 }
283
ljca_gpio_event_cb(void * context,u8 cmd,const void * evt_data,int len)284 static void ljca_gpio_event_cb(void *context, u8 cmd, const void *evt_data,
285 int len)
286 {
287 const struct ljca_gpio_packet *packet = evt_data;
288 struct ljca_gpio_dev *ljca_gpio = context;
289 int i, irq;
290
291 if (cmd != LJCA_GPIO_INT_EVENT)
292 return;
293
294 for (i = 0; i < packet->num; i++) {
295 irq = irq_find_mapping(ljca_gpio->gc.irq.domain,
296 packet->item[i].index);
297 if (!irq) {
298 dev_err(ljca_gpio->gc.parent,
299 "gpio_id %u does not mapped to IRQ yet\n",
300 packet->item[i].index);
301 return;
302 }
303
304 generic_handle_domain_irq(ljca_gpio->gc.irq.domain, irq);
305 set_bit(packet->item[i].index, ljca_gpio->reenable_irqs);
306 }
307
308 schedule_work(&ljca_gpio->work);
309 }
310
ljca_irq_unmask(struct irq_data * irqd)311 static void ljca_irq_unmask(struct irq_data *irqd)
312 {
313 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
314 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
315 int gpio_id = irqd_to_hwirq(irqd);
316
317 gpiochip_enable_irq(gc, gpio_id);
318 set_bit(gpio_id, ljca_gpio->unmasked_irqs);
319 }
320
ljca_irq_mask(struct irq_data * irqd)321 static void ljca_irq_mask(struct irq_data *irqd)
322 {
323 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
324 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
325 int gpio_id = irqd_to_hwirq(irqd);
326
327 clear_bit(gpio_id, ljca_gpio->unmasked_irqs);
328 gpiochip_disable_irq(gc, gpio_id);
329 }
330
ljca_irq_set_type(struct irq_data * irqd,unsigned int type)331 static int ljca_irq_set_type(struct irq_data *irqd, unsigned int type)
332 {
333 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
334 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
335 int gpio_id = irqd_to_hwirq(irqd);
336
337 ljca_gpio->connect_mode[gpio_id] = LJCA_GPIO_CONF_INTERRUPT;
338 switch (type) {
339 case IRQ_TYPE_LEVEL_HIGH:
340 ljca_gpio->connect_mode[gpio_id] |=
341 (LJCA_GPIO_CONF_LEVEL | LJCA_GPIO_CONF_PULLUP);
342 break;
343 case IRQ_TYPE_LEVEL_LOW:
344 ljca_gpio->connect_mode[gpio_id] |=
345 (LJCA_GPIO_CONF_LEVEL | LJCA_GPIO_CONF_PULLDOWN);
346 break;
347 case IRQ_TYPE_EDGE_BOTH:
348 break;
349 case IRQ_TYPE_EDGE_RISING:
350 ljca_gpio->connect_mode[gpio_id] |=
351 (LJCA_GPIO_CONF_EDGE | LJCA_GPIO_CONF_PULLUP);
352 break;
353 case IRQ_TYPE_EDGE_FALLING:
354 ljca_gpio->connect_mode[gpio_id] |=
355 (LJCA_GPIO_CONF_EDGE | LJCA_GPIO_CONF_PULLDOWN);
356 break;
357 default:
358 return -EINVAL;
359 }
360
361 return 0;
362 }
363
ljca_irq_bus_lock(struct irq_data * irqd)364 static void ljca_irq_bus_lock(struct irq_data *irqd)
365 {
366 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
367 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
368
369 mutex_lock(&ljca_gpio->irq_lock);
370 }
371
ljca_irq_bus_unlock(struct irq_data * irqd)372 static void ljca_irq_bus_unlock(struct irq_data *irqd)
373 {
374 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
375 struct ljca_gpio_dev *ljca_gpio = gpiochip_get_data(gc);
376 int gpio_id = irqd_to_hwirq(irqd);
377 int enabled, unmasked;
378
379 enabled = test_bit(gpio_id, ljca_gpio->enabled_irqs);
380 unmasked = test_bit(gpio_id, ljca_gpio->unmasked_irqs);
381
382 if (enabled != unmasked) {
383 if (unmasked) {
384 ljca_gpio_config(ljca_gpio, gpio_id, 0);
385 ljca_enable_irq(ljca_gpio, gpio_id, true);
386 set_bit(gpio_id, ljca_gpio->enabled_irqs);
387 } else {
388 ljca_enable_irq(ljca_gpio, gpio_id, false);
389 clear_bit(gpio_id, ljca_gpio->enabled_irqs);
390 }
391 }
392
393 mutex_unlock(&ljca_gpio->irq_lock);
394 }
395
396 static const struct irq_chip ljca_gpio_irqchip = {
397 .name = "ljca-irq",
398 .irq_mask = ljca_irq_mask,
399 .irq_unmask = ljca_irq_unmask,
400 .irq_set_type = ljca_irq_set_type,
401 .irq_bus_lock = ljca_irq_bus_lock,
402 .irq_bus_sync_unlock = ljca_irq_bus_unlock,
403 .flags = IRQCHIP_IMMUTABLE,
404 GPIOCHIP_IRQ_RESOURCE_HELPERS,
405 };
406
ljca_gpio_probe(struct auxiliary_device * auxdev,const struct auxiliary_device_id * aux_dev_id)407 static int ljca_gpio_probe(struct auxiliary_device *auxdev,
408 const struct auxiliary_device_id *aux_dev_id)
409 {
410 struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
411 struct ljca_gpio_dev *ljca_gpio;
412 struct gpio_irq_chip *girq;
413 int ret;
414
415 ljca_gpio = devm_kzalloc(&auxdev->dev, sizeof(*ljca_gpio), GFP_KERNEL);
416 if (!ljca_gpio)
417 return -ENOMEM;
418
419 ljca_gpio->ljca = ljca;
420 ljca_gpio->gpio_info = dev_get_platdata(&auxdev->dev);
421 ljca_gpio->connect_mode = devm_kcalloc(&auxdev->dev,
422 ljca_gpio->gpio_info->num,
423 sizeof(*ljca_gpio->connect_mode),
424 GFP_KERNEL);
425 if (!ljca_gpio->connect_mode)
426 return -ENOMEM;
427
428 ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->irq_lock);
429 if (ret)
430 return ret;
431
432 ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->trans_lock);
433 if (ret)
434 return ret;
435
436 ljca_gpio->gc.direction_input = ljca_gpio_direction_input;
437 ljca_gpio->gc.direction_output = ljca_gpio_direction_output;
438 ljca_gpio->gc.get_direction = ljca_gpio_get_direction;
439 ljca_gpio->gc.get = ljca_gpio_get_value;
440 ljca_gpio->gc.set_rv = ljca_gpio_set_value;
441 ljca_gpio->gc.set_config = ljca_gpio_set_config;
442 ljca_gpio->gc.init_valid_mask = ljca_gpio_init_valid_mask;
443 ljca_gpio->gc.can_sleep = true;
444 ljca_gpio->gc.parent = &auxdev->dev;
445
446 ljca_gpio->gc.base = -1;
447 ljca_gpio->gc.ngpio = ljca_gpio->gpio_info->num;
448 ljca_gpio->gc.label = ACPI_COMPANION(&auxdev->dev) ?
449 acpi_dev_name(ACPI_COMPANION(&auxdev->dev)) :
450 dev_name(&auxdev->dev);
451 ljca_gpio->gc.owner = THIS_MODULE;
452
453 auxiliary_set_drvdata(auxdev, ljca_gpio);
454 ljca_register_event_cb(ljca, ljca_gpio_event_cb, ljca_gpio);
455
456 girq = &ljca_gpio->gc.irq;
457 gpio_irq_chip_set_chip(girq, &ljca_gpio_irqchip);
458 girq->parent_handler = NULL;
459 girq->num_parents = 0;
460 girq->parents = NULL;
461 girq->default_type = IRQ_TYPE_NONE;
462 girq->handler = handle_simple_irq;
463 girq->init_valid_mask = ljca_gpio_irq_init_valid_mask;
464
465 INIT_WORK(&ljca_gpio->work, ljca_gpio_async);
466 ret = gpiochip_add_data(&ljca_gpio->gc, ljca_gpio);
467 if (ret)
468 ljca_unregister_event_cb(ljca);
469
470 return ret;
471 }
472
ljca_gpio_remove(struct auxiliary_device * auxdev)473 static void ljca_gpio_remove(struct auxiliary_device *auxdev)
474 {
475 struct ljca_gpio_dev *ljca_gpio = auxiliary_get_drvdata(auxdev);
476
477 gpiochip_remove(&ljca_gpio->gc);
478 ljca_unregister_event_cb(ljca_gpio->ljca);
479 cancel_work_sync(&ljca_gpio->work);
480 }
481
482 static const struct auxiliary_device_id ljca_gpio_id_table[] = {
483 { "usb_ljca.ljca-gpio", 0 },
484 { /* sentinel */ },
485 };
486 MODULE_DEVICE_TABLE(auxiliary, ljca_gpio_id_table);
487
488 static struct auxiliary_driver ljca_gpio_driver = {
489 .probe = ljca_gpio_probe,
490 .remove = ljca_gpio_remove,
491 .id_table = ljca_gpio_id_table,
492 };
493 module_auxiliary_driver(ljca_gpio_driver);
494
495 MODULE_AUTHOR("Wentong Wu <wentong.wu@intel.com>");
496 MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>");
497 MODULE_AUTHOR("Lixu Zhang <lixu.zhang@intel.com>");
498 MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB-GPIO driver");
499 MODULE_LICENSE("GPL");
500 MODULE_IMPORT_NS("LJCA");
501