1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // GPIO Aggregator
4 //
5 // Copyright (C) 2019-2020 Glider bv
6
7 #define DRV_NAME "gpio-aggregator"
8 #define pr_fmt(fmt) DRV_NAME ": " fmt
9
10 #include <linux/bitmap.h>
11 #include <linux/bitops.h>
12 #include <linux/configfs.h>
13 #include <linux/ctype.h>
14 #include <linux/delay.h>
15 #include <linux/export.h>
16 #include <linux/idr.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/lockdep.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/overflow.h>
24 #include <linux/platform_device.h>
25 #include <linux/property.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/string.h>
29
30 #include <linux/gpio/consumer.h>
31 #include <linux/gpio/driver.h>
32 #include <linux/gpio/forwarder.h>
33 #include <linux/gpio/machine.h>
34
35 #define AGGREGATOR_MAX_GPIOS 512
36 #define AGGREGATOR_LEGACY_PREFIX "_sysfs"
37
38 /*
39 * GPIO Aggregator sysfs interface
40 */
41
42 struct gpio_aggregator {
43 struct platform_device *pdev;
44 struct config_group group;
45 struct gpiod_lookup_table *lookups;
46 struct mutex lock;
47 int id;
48
49 /* List of gpio_aggregator_line. Always added in order */
50 struct list_head list_head;
51
52 /* used by legacy sysfs interface only */
53 bool init_via_sysfs;
54 char args[];
55 };
56
57 struct gpio_aggregator_line {
58 struct config_group group;
59 struct gpio_aggregator *parent;
60 struct list_head entry;
61
62 /* Line index within the aggregator device */
63 unsigned int idx;
64
65 /* Custom name for the virtual line */
66 const char *name;
67 /* GPIO chip label or line name */
68 const char *key;
69 /* Can be negative to indicate lookup by line name */
70 int offset;
71
72 enum gpio_lookup_flags flags;
73 };
74
75 struct gpio_aggregator_pdev_meta {
76 bool init_via_sysfs;
77 };
78
79 static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */
80 static DEFINE_IDR(gpio_aggregator_idr);
81
gpio_aggregator_alloc(struct gpio_aggregator ** aggr,size_t arg_size)82 static int gpio_aggregator_alloc(struct gpio_aggregator **aggr, size_t arg_size)
83 {
84 int ret;
85
86 struct gpio_aggregator *new __free(kfree) = kzalloc(
87 sizeof(*new) + arg_size, GFP_KERNEL);
88 if (!new)
89 return -ENOMEM;
90
91 scoped_guard(mutex, &gpio_aggregator_lock)
92 ret = idr_alloc(&gpio_aggregator_idr, new, 0, 0, GFP_KERNEL);
93
94 if (ret < 0)
95 return ret;
96
97 new->id = ret;
98 INIT_LIST_HEAD(&new->list_head);
99 mutex_init(&new->lock);
100 *aggr = no_free_ptr(new);
101 return 0;
102 }
103
gpio_aggregator_free(struct gpio_aggregator * aggr)104 static void gpio_aggregator_free(struct gpio_aggregator *aggr)
105 {
106 scoped_guard(mutex, &gpio_aggregator_lock)
107 idr_remove(&gpio_aggregator_idr, aggr->id);
108
109 mutex_destroy(&aggr->lock);
110 kfree(aggr);
111 }
112
gpio_aggregator_add_gpio(struct gpio_aggregator * aggr,const char * key,int hwnum,unsigned int * n)113 static int gpio_aggregator_add_gpio(struct gpio_aggregator *aggr,
114 const char *key, int hwnum, unsigned int *n)
115 {
116 struct gpiod_lookup_table *lookups;
117
118 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
119 GFP_KERNEL);
120 if (!lookups)
121 return -ENOMEM;
122
123 lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
124
125 (*n)++;
126 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
127
128 aggr->lookups = lookups;
129 return 0;
130 }
131
gpio_aggregator_is_active(struct gpio_aggregator * aggr)132 static bool gpio_aggregator_is_active(struct gpio_aggregator *aggr)
133 {
134 lockdep_assert_held(&aggr->lock);
135
136 return aggr->pdev && platform_get_drvdata(aggr->pdev);
137 }
138
139 /* Only aggregators created via legacy sysfs can be "activating". */
gpio_aggregator_is_activating(struct gpio_aggregator * aggr)140 static bool gpio_aggregator_is_activating(struct gpio_aggregator *aggr)
141 {
142 lockdep_assert_held(&aggr->lock);
143
144 return aggr->pdev && !platform_get_drvdata(aggr->pdev);
145 }
146
gpio_aggregator_count_lines(struct gpio_aggregator * aggr)147 static size_t gpio_aggregator_count_lines(struct gpio_aggregator *aggr)
148 {
149 lockdep_assert_held(&aggr->lock);
150
151 return list_count_nodes(&aggr->list_head);
152 }
153
154 static struct gpio_aggregator_line *
gpio_aggregator_line_alloc(struct gpio_aggregator * parent,unsigned int idx,char * key,int offset)155 gpio_aggregator_line_alloc(struct gpio_aggregator *parent, unsigned int idx,
156 char *key, int offset)
157 {
158 struct gpio_aggregator_line *line;
159
160 line = kzalloc_obj(*line);
161 if (!line)
162 return ERR_PTR(-ENOMEM);
163
164 if (key) {
165 line->key = kstrdup(key, GFP_KERNEL);
166 if (!line->key) {
167 kfree(line);
168 return ERR_PTR(-ENOMEM);
169 }
170 }
171
172 line->flags = GPIO_LOOKUP_FLAGS_DEFAULT;
173 line->parent = parent;
174 line->idx = idx;
175 line->offset = offset;
176 INIT_LIST_HEAD(&line->entry);
177
178 return line;
179 }
180
gpio_aggregator_line_add(struct gpio_aggregator * aggr,struct gpio_aggregator_line * line)181 static void gpio_aggregator_line_add(struct gpio_aggregator *aggr,
182 struct gpio_aggregator_line *line)
183 {
184 struct gpio_aggregator_line *tmp;
185
186 lockdep_assert_held(&aggr->lock);
187
188 list_for_each_entry(tmp, &aggr->list_head, entry) {
189 if (tmp->idx > line->idx) {
190 list_add_tail(&line->entry, &tmp->entry);
191 return;
192 }
193 }
194 list_add_tail(&line->entry, &aggr->list_head);
195 }
196
gpio_aggregator_line_del(struct gpio_aggregator * aggr,struct gpio_aggregator_line * line)197 static void gpio_aggregator_line_del(struct gpio_aggregator *aggr,
198 struct gpio_aggregator_line *line)
199 {
200 lockdep_assert_held(&aggr->lock);
201
202 list_del(&line->entry);
203 }
204
gpio_aggregator_free_lines(struct gpio_aggregator * aggr)205 static void gpio_aggregator_free_lines(struct gpio_aggregator *aggr)
206 {
207 struct gpio_aggregator_line *line, *tmp;
208
209 list_for_each_entry_safe(line, tmp, &aggr->list_head, entry) {
210 configfs_unregister_group(&line->group);
211 /*
212 * Normally, we acquire aggr->lock within the configfs
213 * callback. However, in the legacy sysfs interface case,
214 * calling configfs_(un)register_group while holding
215 * aggr->lock could cause a deadlock. Fortunately, this is
216 * unnecessary because the new_device/delete_device path
217 * and the module unload path are mutually exclusive,
218 * thanks to an explicit try_module_get. That's why this
219 * minimal scoped_guard suffices.
220 */
221 scoped_guard(mutex, &aggr->lock)
222 gpio_aggregator_line_del(aggr, line);
223 kfree(line->key);
224 kfree(line->name);
225 kfree(line);
226 }
227 }
228
229
230 /*
231 * GPIO Forwarder
232 */
233
234 struct gpiochip_fwd_timing {
235 u32 ramp_up_us;
236 u32 ramp_down_us;
237 };
238
239 struct gpiochip_fwd {
240 struct gpio_chip chip;
241 struct gpio_desc **descs;
242 union {
243 struct mutex mlock; /* protects tmp[] if can_sleep */
244 spinlock_t slock; /* protects tmp[] if !can_sleep */
245 };
246 struct gpiochip_fwd_timing *delay_timings;
247 void *data;
248 unsigned long *valid_mask;
249 unsigned long tmp[]; /* values and descs for multiple ops */
250 };
251
252 #define fwd_tmp_values(fwd) (&(fwd)->tmp[0])
253 #define fwd_tmp_descs(fwd) ((void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)])
254
255 #define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
256
gpio_fwd_request(struct gpio_chip * chip,unsigned int offset)257 static int gpio_fwd_request(struct gpio_chip *chip, unsigned int offset)
258 {
259 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
260
261 return test_bit(offset, fwd->valid_mask) ? 0 : -ENODEV;
262 }
263
gpio_fwd_get_direction(struct gpio_chip * chip,unsigned int offset)264 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
265 {
266 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
267
268 /*
269 * get_direction() is called during gpiochip registration, return
270 * -ENODEV if there is no GPIO desc for the line.
271 */
272 if (!test_bit(offset, fwd->valid_mask))
273 return -ENODEV;
274
275 return gpiod_get_direction(fwd->descs[offset]);
276 }
277
gpio_fwd_direction_input(struct gpio_chip * chip,unsigned int offset)278 static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
279 {
280 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
281
282 return gpiod_direction_input(fwd->descs[offset]);
283 }
284
gpio_fwd_direction_output(struct gpio_chip * chip,unsigned int offset,int value)285 static int gpio_fwd_direction_output(struct gpio_chip *chip,
286 unsigned int offset, int value)
287 {
288 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
289
290 return gpiod_direction_output(fwd->descs[offset], value);
291 }
292
gpio_fwd_get(struct gpio_chip * chip,unsigned int offset)293 static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
294 {
295 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
296
297 return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset])
298 : gpiod_get_value(fwd->descs[offset]);
299 }
300
gpio_fwd_get_multiple(struct gpiochip_fwd * fwd,unsigned long * mask,unsigned long * bits)301 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
302 unsigned long *bits)
303 {
304 struct gpio_desc **descs = fwd_tmp_descs(fwd);
305 unsigned long *values = fwd_tmp_values(fwd);
306 unsigned int i, j = 0;
307 int error;
308
309 bitmap_clear(values, 0, fwd->chip.ngpio);
310 for_each_set_bit(i, mask, fwd->chip.ngpio)
311 descs[j++] = fwd->descs[i];
312
313 if (fwd->chip.can_sleep)
314 error = gpiod_get_array_value_cansleep(j, descs, NULL, values);
315 else
316 error = gpiod_get_array_value(j, descs, NULL, values);
317 if (error)
318 return error;
319
320 j = 0;
321 for_each_set_bit(i, mask, fwd->chip.ngpio)
322 __assign_bit(i, bits, test_bit(j++, values));
323
324 return 0;
325 }
326
gpio_fwd_get_multiple_locked(struct gpio_chip * chip,unsigned long * mask,unsigned long * bits)327 static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
328 unsigned long *mask, unsigned long *bits)
329 {
330 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
331 unsigned long flags;
332 int error;
333
334 if (chip->can_sleep) {
335 mutex_lock(&fwd->mlock);
336 error = gpio_fwd_get_multiple(fwd, mask, bits);
337 mutex_unlock(&fwd->mlock);
338 } else {
339 spin_lock_irqsave(&fwd->slock, flags);
340 error = gpio_fwd_get_multiple(fwd, mask, bits);
341 spin_unlock_irqrestore(&fwd->slock, flags);
342 }
343
344 return error;
345 }
346
gpio_fwd_delay(struct gpio_chip * chip,unsigned int offset,int value)347 static void gpio_fwd_delay(struct gpio_chip *chip, unsigned int offset, int value)
348 {
349 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
350 const struct gpiochip_fwd_timing *delay_timings;
351 bool is_active_low = gpiod_is_active_low(fwd->descs[offset]);
352 u32 delay_us;
353
354 delay_timings = &fwd->delay_timings[offset];
355 if ((!is_active_low && value) || (is_active_low && !value))
356 delay_us = delay_timings->ramp_up_us;
357 else
358 delay_us = delay_timings->ramp_down_us;
359 if (!delay_us)
360 return;
361
362 if (chip->can_sleep)
363 fsleep(delay_us);
364 else
365 udelay(delay_us);
366 }
367
gpio_fwd_set(struct gpio_chip * chip,unsigned int offset,int value)368 static int gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
369 {
370 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
371 int ret;
372
373 if (chip->can_sleep)
374 ret = gpiod_set_value_cansleep(fwd->descs[offset], value);
375 else
376 ret = gpiod_set_value(fwd->descs[offset], value);
377 if (ret)
378 return ret;
379
380 if (fwd->delay_timings)
381 gpio_fwd_delay(chip, offset, value);
382
383 return ret;
384 }
385
gpio_fwd_set_multiple(struct gpiochip_fwd * fwd,unsigned long * mask,unsigned long * bits)386 static int gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
387 unsigned long *bits)
388 {
389 struct gpio_desc **descs = fwd_tmp_descs(fwd);
390 unsigned long *values = fwd_tmp_values(fwd);
391 unsigned int i, j = 0, ret;
392
393 for_each_set_bit(i, mask, fwd->chip.ngpio) {
394 __assign_bit(j, values, test_bit(i, bits));
395 descs[j++] = fwd->descs[i];
396 }
397
398 if (fwd->chip.can_sleep)
399 ret = gpiod_set_array_value_cansleep(j, descs, NULL, values);
400 else
401 ret = gpiod_set_array_value(j, descs, NULL, values);
402
403 return ret;
404 }
405
gpio_fwd_set_multiple_locked(struct gpio_chip * chip,unsigned long * mask,unsigned long * bits)406 static int gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
407 unsigned long *mask, unsigned long *bits)
408 {
409 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
410 unsigned long flags;
411 int ret;
412
413 if (chip->can_sleep) {
414 mutex_lock(&fwd->mlock);
415 ret = gpio_fwd_set_multiple(fwd, mask, bits);
416 mutex_unlock(&fwd->mlock);
417 } else {
418 spin_lock_irqsave(&fwd->slock, flags);
419 ret = gpio_fwd_set_multiple(fwd, mask, bits);
420 spin_unlock_irqrestore(&fwd->slock, flags);
421 }
422
423 return ret;
424 }
425
gpio_fwd_set_config(struct gpio_chip * chip,unsigned int offset,unsigned long config)426 static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
427 unsigned long config)
428 {
429 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
430
431 return gpiod_set_config(fwd->descs[offset], config);
432 }
433
gpio_fwd_to_irq(struct gpio_chip * chip,unsigned int offset)434 static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
435 {
436 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
437
438 return gpiod_to_irq(fwd->descs[offset]);
439 }
440
441 /*
442 * The GPIO delay provides a way to configure platform specific delays
443 * for the GPIO ramp-up or ramp-down delays. This can serve the following
444 * purposes:
445 * - Open-drain output using an RC filter
446 */
447 #define FWD_FEATURE_DELAY BIT(0)
448
449 #ifdef CONFIG_OF_GPIO
gpiochip_fwd_delay_of_xlate(struct gpio_chip * chip,const struct of_phandle_args * gpiospec,u32 * flags)450 static int gpiochip_fwd_delay_of_xlate(struct gpio_chip *chip,
451 const struct of_phandle_args *gpiospec,
452 u32 *flags)
453 {
454 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
455 struct gpiochip_fwd_timing *timings;
456 u32 line;
457
458 if (gpiospec->args_count != chip->of_gpio_n_cells)
459 return -EINVAL;
460
461 line = gpiospec->args[0];
462 if (line >= chip->ngpio)
463 return -EINVAL;
464
465 timings = &fwd->delay_timings[line];
466 timings->ramp_up_us = gpiospec->args[1];
467 timings->ramp_down_us = gpiospec->args[2];
468
469 return line;
470 }
471
gpiochip_fwd_setup_delay_line(struct gpiochip_fwd * fwd)472 static int gpiochip_fwd_setup_delay_line(struct gpiochip_fwd *fwd)
473 {
474 struct gpio_chip *chip = &fwd->chip;
475
476 fwd->delay_timings = devm_kcalloc(chip->parent, chip->ngpio,
477 sizeof(*fwd->delay_timings),
478 GFP_KERNEL);
479 if (!fwd->delay_timings)
480 return -ENOMEM;
481
482 chip->of_xlate = gpiochip_fwd_delay_of_xlate;
483 chip->of_gpio_n_cells = 3;
484
485 return 0;
486 }
487 #else
gpiochip_fwd_setup_delay_line(struct gpiochip_fwd * fwd)488 static int gpiochip_fwd_setup_delay_line(struct gpiochip_fwd *fwd)
489 {
490 return 0;
491 }
492 #endif /* !CONFIG_OF_GPIO */
493
494 /**
495 * gpiochip_fwd_get_gpiochip - Get the GPIO chip for the GPIO forwarder
496 * @fwd: GPIO forwarder
497 *
498 * Returns: The GPIO chip for the GPIO forwarder
499 */
gpiochip_fwd_get_gpiochip(struct gpiochip_fwd * fwd)500 struct gpio_chip *gpiochip_fwd_get_gpiochip(struct gpiochip_fwd *fwd)
501 {
502 return &fwd->chip;
503 }
504 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_get_gpiochip, "GPIO_FORWARDER");
505
506 /**
507 * gpiochip_fwd_get_data - Get driver-private data for the GPIO forwarder
508 * @fwd: GPIO forwarder
509 *
510 * Returns: The driver-private data for the GPIO forwarder
511 */
gpiochip_fwd_get_data(struct gpiochip_fwd * fwd)512 void *gpiochip_fwd_get_data(struct gpiochip_fwd *fwd)
513 {
514 return fwd->data;
515 }
516 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_get_data, "GPIO_FORWARDER");
517
518 /**
519 * gpiochip_fwd_gpio_request - Request a line of the GPIO forwarder
520 * @fwd: GPIO forwarder
521 * @offset: the offset of the line to request
522 *
523 * Returns: 0 on success, or negative errno on failure.
524 */
gpiochip_fwd_gpio_request(struct gpiochip_fwd * fwd,unsigned int offset)525 int gpiochip_fwd_gpio_request(struct gpiochip_fwd *fwd, unsigned int offset)
526 {
527 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
528
529 return gpio_fwd_request(gc, offset);
530 }
531 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_request, "GPIO_FORWARDER");
532
533 /**
534 * gpiochip_fwd_gpio_get_direction - Return the current direction of a GPIO forwarder line
535 * @fwd: GPIO forwarder
536 * @offset: the offset of the line
537 *
538 * Returns: 0 for output, 1 for input, or an error code in case of error.
539 */
gpiochip_fwd_gpio_get_direction(struct gpiochip_fwd * fwd,unsigned int offset)540 int gpiochip_fwd_gpio_get_direction(struct gpiochip_fwd *fwd, unsigned int offset)
541 {
542 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
543
544 return gpio_fwd_get_direction(gc, offset);
545 }
546 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_get_direction, "GPIO_FORWARDER");
547
548 /**
549 * gpiochip_fwd_gpio_direction_output - Set a GPIO forwarder line direction to
550 * output
551 * @fwd: GPIO forwarder
552 * @offset: the offset of the line
553 * @value: value to set
554 *
555 * Returns: 0 on success, or negative errno on failure.
556 */
gpiochip_fwd_gpio_direction_output(struct gpiochip_fwd * fwd,unsigned int offset,int value)557 int gpiochip_fwd_gpio_direction_output(struct gpiochip_fwd *fwd, unsigned int offset,
558 int value)
559 {
560 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
561
562 return gpio_fwd_direction_output(gc, offset, value);
563 }
564 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_direction_output, "GPIO_FORWARDER");
565
566 /**
567 * gpiochip_fwd_gpio_direction_input - Set a GPIO forwarder line direction to input
568 * @fwd: GPIO forwarder
569 * @offset: the offset of the line
570 *
571 * Returns: 0 on success, or negative errno on failure.
572 */
gpiochip_fwd_gpio_direction_input(struct gpiochip_fwd * fwd,unsigned int offset)573 int gpiochip_fwd_gpio_direction_input(struct gpiochip_fwd *fwd, unsigned int offset)
574 {
575 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
576
577 return gpio_fwd_direction_input(gc, offset);
578 }
579 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_direction_input, "GPIO_FORWARDER");
580
581 /**
582 * gpiochip_fwd_gpio_get - Return a GPIO forwarder line's value
583 * @fwd: GPIO forwarder
584 * @offset: the offset of the line
585 *
586 * Returns: The GPIO's logical value, i.e. taking the ACTIVE_LOW status into
587 * account, or negative errno on failure.
588 */
gpiochip_fwd_gpio_get(struct gpiochip_fwd * fwd,unsigned int offset)589 int gpiochip_fwd_gpio_get(struct gpiochip_fwd *fwd, unsigned int offset)
590 {
591 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
592
593 return gpio_fwd_get(gc, offset);
594 }
595 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_get, "GPIO_FORWARDER");
596
597 /**
598 * gpiochip_fwd_gpio_get_multiple - Get values for multiple GPIO forwarder lines
599 * @fwd: GPIO forwarder
600 * @mask: bit mask array; one bit per line; BITS_PER_LONG bits per word defines
601 * which lines are to be read
602 * @bits: bit value array; one bit per line; BITS_PER_LONG bits per word will
603 * contains the read values for the lines specified by mask
604 *
605 * Returns: 0 on success, or negative errno on failure.
606 */
gpiochip_fwd_gpio_get_multiple(struct gpiochip_fwd * fwd,unsigned long * mask,unsigned long * bits)607 int gpiochip_fwd_gpio_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
608 unsigned long *bits)
609 {
610 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
611
612 return gpio_fwd_get_multiple_locked(gc, mask, bits);
613 }
614 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_get_multiple, "GPIO_FORWARDER");
615
616 /**
617 * gpiochip_fwd_gpio_set - Assign value to a GPIO forwarder line.
618 * @fwd: GPIO forwarder
619 * @offset: the offset of the line
620 * @value: value to set
621 *
622 * Returns: 0 on success, or negative errno on failure.
623 */
gpiochip_fwd_gpio_set(struct gpiochip_fwd * fwd,unsigned int offset,int value)624 int gpiochip_fwd_gpio_set(struct gpiochip_fwd *fwd, unsigned int offset, int value)
625 {
626 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
627
628 return gpio_fwd_set(gc, offset, value);
629 }
630 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_set, "GPIO_FORWARDER");
631
632 /**
633 * gpiochip_fwd_gpio_set_multiple - Assign values to multiple GPIO forwarder lines
634 * @fwd: GPIO forwarder
635 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
636 * defines which outputs are to be changed
637 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
638 * defines the values the outputs specified by mask are to be set to
639 *
640 * Returns: 0 on success, or negative errno on failure.
641 */
gpiochip_fwd_gpio_set_multiple(struct gpiochip_fwd * fwd,unsigned long * mask,unsigned long * bits)642 int gpiochip_fwd_gpio_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
643 unsigned long *bits)
644 {
645 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
646
647 return gpio_fwd_set_multiple_locked(gc, mask, bits);
648 }
649 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_set_multiple, "GPIO_FORWARDER");
650
651 /**
652 * gpiochip_fwd_gpio_set_config - Set @config for a GPIO forwarder line
653 * @fwd: GPIO forwarder
654 * @offset: the offset of the line
655 * @config: Same packed config format as generic pinconf
656 *
657 * Returns: 0 on success, %-ENOTSUPP if the controller doesn't support setting
658 * the configuration.
659 */
gpiochip_fwd_gpio_set_config(struct gpiochip_fwd * fwd,unsigned int offset,unsigned long config)660 int gpiochip_fwd_gpio_set_config(struct gpiochip_fwd *fwd, unsigned int offset,
661 unsigned long config)
662 {
663 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
664
665 return gpio_fwd_set_config(gc, offset, config);
666 }
667 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_set_config, "GPIO_FORWARDER");
668
669 /**
670 * gpiochip_fwd_gpio_to_irq - Return the IRQ corresponding to a GPIO forwarder line
671 * @fwd: GPIO forwarder
672 * @offset: the offset of the line
673 *
674 * Returns: The Linux IRQ corresponding to the passed line, or an error code in
675 * case of error.
676 */
gpiochip_fwd_gpio_to_irq(struct gpiochip_fwd * fwd,unsigned int offset)677 int gpiochip_fwd_gpio_to_irq(struct gpiochip_fwd *fwd, unsigned int offset)
678 {
679 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd);
680
681 return gpio_fwd_to_irq(gc, offset);
682 }
683 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_to_irq, "GPIO_FORWARDER");
684
685 /**
686 * devm_gpiochip_fwd_alloc - Allocate and initialize a new GPIO forwarder
687 * @dev: Parent device pointer
688 * @ngpios: Number of GPIOs in the forwarder
689 *
690 * Returns: An opaque object pointer, or an ERR_PTR()-encoded negative error
691 * code on failure.
692 */
devm_gpiochip_fwd_alloc(struct device * dev,unsigned int ngpios)693 struct gpiochip_fwd *devm_gpiochip_fwd_alloc(struct device *dev,
694 unsigned int ngpios)
695 {
696 struct gpiochip_fwd *fwd;
697 struct gpio_chip *chip;
698
699 fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)), GFP_KERNEL);
700 if (!fwd)
701 return ERR_PTR(-ENOMEM);
702
703 fwd->descs = devm_kcalloc(dev, ngpios, sizeof(*fwd->descs), GFP_KERNEL);
704 if (!fwd->descs)
705 return ERR_PTR(-ENOMEM);
706
707 fwd->valid_mask = devm_bitmap_zalloc(dev, ngpios, GFP_KERNEL);
708 if (!fwd->valid_mask)
709 return ERR_PTR(-ENOMEM);
710
711 chip = &fwd->chip;
712
713 chip->label = dev_name(dev);
714 chip->parent = dev;
715 chip->owner = THIS_MODULE;
716 chip->request = gpio_fwd_request;
717 chip->get_direction = gpio_fwd_get_direction;
718 chip->direction_input = gpio_fwd_direction_input;
719 chip->direction_output = gpio_fwd_direction_output;
720 chip->get = gpio_fwd_get;
721 chip->get_multiple = gpio_fwd_get_multiple_locked;
722 chip->set = gpio_fwd_set;
723 chip->set_multiple = gpio_fwd_set_multiple_locked;
724 chip->set_config = gpio_fwd_set_config;
725 chip->to_irq = gpio_fwd_to_irq;
726 chip->base = -1;
727 chip->ngpio = ngpios;
728
729 return fwd;
730 }
731 EXPORT_SYMBOL_NS_GPL(devm_gpiochip_fwd_alloc, "GPIO_FORWARDER");
732
733 /**
734 * gpiochip_fwd_desc_add - Add a GPIO desc in the forwarder
735 * @fwd: GPIO forwarder
736 * @desc: GPIO descriptor to register
737 * @offset: offset for the GPIO in the forwarder
738 *
739 * Returns: 0 on success, or negative errno on failure.
740 */
gpiochip_fwd_desc_add(struct gpiochip_fwd * fwd,struct gpio_desc * desc,unsigned int offset)741 int gpiochip_fwd_desc_add(struct gpiochip_fwd *fwd, struct gpio_desc *desc,
742 unsigned int offset)
743 {
744 struct gpio_chip *chip = &fwd->chip;
745
746 if (offset >= chip->ngpio)
747 return -EINVAL;
748
749 if (test_and_set_bit(offset, fwd->valid_mask))
750 return -EEXIST;
751
752 /*
753 * If any of the GPIO lines are sleeping, then the entire forwarder
754 * will be sleeping.
755 */
756 if (gpiod_cansleep(desc))
757 chip->can_sleep = true;
758
759 fwd->descs[offset] = desc;
760
761 dev_dbg(chip->parent, "%u => gpio %d irq %d\n", offset,
762 desc_to_gpio(desc), gpiod_to_irq(desc));
763
764 return 0;
765 }
766 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_desc_add, "GPIO_FORWARDER");
767
768 /**
769 * gpiochip_fwd_desc_free - Remove a GPIO desc from the forwarder
770 * @fwd: GPIO forwarder
771 * @offset: offset of GPIO desc to remove
772 */
gpiochip_fwd_desc_free(struct gpiochip_fwd * fwd,unsigned int offset)773 void gpiochip_fwd_desc_free(struct gpiochip_fwd *fwd, unsigned int offset)
774 {
775 if (test_and_clear_bit(offset, fwd->valid_mask))
776 gpiod_put(fwd->descs[offset]);
777 }
778 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_desc_free, "GPIO_FORWARDER");
779
780 /**
781 * gpiochip_fwd_register - Register a GPIO forwarder
782 * @fwd: GPIO forwarder
783 * @data: driver-private data associated with this forwarder
784 *
785 * Returns: 0 on success, or negative errno on failure.
786 */
gpiochip_fwd_register(struct gpiochip_fwd * fwd,void * data)787 int gpiochip_fwd_register(struct gpiochip_fwd *fwd, void *data)
788 {
789 struct gpio_chip *chip = &fwd->chip;
790
791 /*
792 * Some gpio_desc were not registered. They will be registered at runtime
793 * but we have to suppose they can sleep.
794 */
795 if (!bitmap_full(fwd->valid_mask, chip->ngpio))
796 chip->can_sleep = true;
797
798 if (chip->can_sleep)
799 mutex_init(&fwd->mlock);
800 else
801 spin_lock_init(&fwd->slock);
802
803 fwd->data = data;
804
805 return devm_gpiochip_add_data(chip->parent, chip, fwd);
806 }
807 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_register, "GPIO_FORWARDER");
808
809 /**
810 * gpiochip_fwd_create() - Create a new GPIO forwarder
811 * @dev: Parent device pointer
812 * @ngpios: Number of GPIOs in the forwarder.
813 * @descs: Array containing the GPIO descriptors to forward to.
814 * This array must contain @ngpios entries, and can be deallocated
815 * as the forwarder has its own array.
816 * @features: Bitwise ORed features as defined with FWD_FEATURE_*.
817 *
818 * This function creates a new gpiochip, which forwards all GPIO operations to
819 * the passed GPIO descriptors.
820 *
821 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
822 * code on failure.
823 */
gpiochip_fwd_create(struct device * dev,unsigned int ngpios,struct gpio_desc * descs[],unsigned long features)824 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
825 unsigned int ngpios,
826 struct gpio_desc *descs[],
827 unsigned long features)
828 {
829 struct gpiochip_fwd *fwd;
830 unsigned int i;
831 int error;
832
833 fwd = devm_gpiochip_fwd_alloc(dev, ngpios);
834 if (IS_ERR(fwd))
835 return fwd;
836
837 for (i = 0; i < ngpios; i++) {
838 error = gpiochip_fwd_desc_add(fwd, descs[i], i);
839 if (error)
840 return ERR_PTR(error);
841 }
842
843 if (features & FWD_FEATURE_DELAY) {
844 error = gpiochip_fwd_setup_delay_line(fwd);
845 if (error)
846 return ERR_PTR(error);
847 }
848
849 error = gpiochip_fwd_register(fwd, NULL);
850 if (error)
851 return ERR_PTR(error);
852
853 return fwd;
854 }
855
856 /*
857 * Configfs interface
858 */
859
860 static struct gpio_aggregator *
to_gpio_aggregator(struct config_item * item)861 to_gpio_aggregator(struct config_item *item)
862 {
863 struct config_group *group = to_config_group(item);
864
865 return container_of(group, struct gpio_aggregator, group);
866 }
867
868 static struct gpio_aggregator_line *
to_gpio_aggregator_line(struct config_item * item)869 to_gpio_aggregator_line(struct config_item *item)
870 {
871 struct config_group *group = to_config_group(item);
872
873 return container_of(group, struct gpio_aggregator_line, group);
874 }
875
876 static struct fwnode_handle *
gpio_aggregator_make_device_sw_node(struct gpio_aggregator * aggr)877 gpio_aggregator_make_device_sw_node(struct gpio_aggregator *aggr)
878 {
879 struct property_entry properties[2];
880 struct gpio_aggregator_line *line;
881 size_t num_lines;
882 int n = 0;
883
884 memset(properties, 0, sizeof(properties));
885
886 num_lines = gpio_aggregator_count_lines(aggr);
887 if (num_lines == 0)
888 return NULL;
889
890 const char **line_names __free(kfree) = kcalloc(
891 num_lines, sizeof(*line_names), GFP_KERNEL);
892 if (!line_names)
893 return ERR_PTR(-ENOMEM);
894
895 /* The list is always sorted as new elements are inserted in order. */
896 list_for_each_entry(line, &aggr->list_head, entry)
897 line_names[n++] = line->name ?: "";
898
899 properties[0] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
900 "gpio-line-names",
901 line_names, num_lines);
902
903 return fwnode_create_software_node(properties, NULL);
904 }
905
gpio_aggregator_activate(struct gpio_aggregator * aggr)906 static int gpio_aggregator_activate(struct gpio_aggregator *aggr)
907 {
908 struct platform_device_info pdevinfo;
909 struct gpio_aggregator_line *line;
910 struct platform_device *pdev;
911 struct fwnode_handle *swnode;
912 unsigned int n = 0;
913 int ret = 0;
914
915 if (gpio_aggregator_count_lines(aggr) == 0)
916 return -EINVAL;
917
918 aggr->lookups = kzalloc_flex(*aggr->lookups, table, 1);
919 if (!aggr->lookups)
920 return -ENOMEM;
921
922 swnode = gpio_aggregator_make_device_sw_node(aggr);
923 if (IS_ERR(swnode)) {
924 ret = PTR_ERR(swnode);
925 goto err_remove_lookups;
926 }
927
928 memset(&pdevinfo, 0, sizeof(pdevinfo));
929 pdevinfo.name = DRV_NAME;
930 pdevinfo.id = aggr->id;
931 pdevinfo.fwnode = swnode;
932
933 /* The list is always sorted as new elements are inserted in order. */
934 list_for_each_entry(line, &aggr->list_head, entry) {
935 /*
936 * - Either GPIO chip label or line name must be configured
937 * (i.e. line->key must be non-NULL)
938 * - Line directories must be named with sequential numeric
939 * suffixes starting from 0. (i.e. ./line0, ./line1, ...)
940 */
941 if (!line->key || line->idx != n) {
942 ret = -EINVAL;
943 goto err_remove_swnode;
944 }
945
946 if (line->offset < 0)
947 ret = gpio_aggregator_add_gpio(aggr, line->key,
948 U16_MAX, &n);
949 else
950 ret = gpio_aggregator_add_gpio(aggr, line->key,
951 line->offset, &n);
952 if (ret)
953 goto err_remove_swnode;
954 }
955
956 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, aggr->id);
957 if (!aggr->lookups->dev_id) {
958 ret = -ENOMEM;
959 goto err_remove_swnode;
960 }
961
962 gpiod_add_lookup_table(aggr->lookups);
963
964 pdev = platform_device_register_full(&pdevinfo);
965 if (IS_ERR(pdev)) {
966 ret = PTR_ERR(pdev);
967 goto err_remove_lookup_table;
968 }
969
970 wait_for_device_probe();
971
972 scoped_guard(device, &pdev->dev) {
973 if (!device_is_bound(&pdev->dev)) {
974 ret = -ENXIO;
975 goto err_unregister_pdev;
976 }
977 }
978
979 aggr->pdev = pdev;
980 return 0;
981
982 err_unregister_pdev:
983 platform_device_unregister(pdev);
984 err_remove_lookup_table:
985 gpiod_remove_lookup_table(aggr->lookups);
986 kfree(aggr->lookups->dev_id);
987 err_remove_swnode:
988 fwnode_remove_software_node(swnode);
989 err_remove_lookups:
990 kfree(aggr->lookups);
991
992 return ret;
993 }
994
gpio_aggregator_deactivate(struct gpio_aggregator * aggr)995 static void gpio_aggregator_deactivate(struct gpio_aggregator *aggr)
996 {
997 struct fwnode_handle *swnode;
998
999 swnode = dev_fwnode(&aggr->pdev->dev);
1000 platform_device_unregister(aggr->pdev);
1001 aggr->pdev = NULL;
1002 gpiod_remove_lookup_table(aggr->lookups);
1003 kfree(aggr->lookups->dev_id);
1004 kfree(aggr->lookups);
1005 fwnode_remove_software_node(swnode);
1006 }
1007
gpio_aggregator_lockup_configfs(struct gpio_aggregator * aggr,bool lock)1008 static void gpio_aggregator_lockup_configfs(struct gpio_aggregator *aggr,
1009 bool lock)
1010 {
1011 struct configfs_subsystem *subsys = aggr->group.cg_subsys;
1012 struct gpio_aggregator_line *line;
1013
1014 /*
1015 * The device only needs to depend on leaf lines. This is
1016 * sufficient to lock up all the configfs entries that the
1017 * instantiated, alive device depends on.
1018 */
1019 list_for_each_entry(line, &aggr->list_head, entry) {
1020 if (lock)
1021 configfs_depend_item_unlocked(
1022 subsys, &line->group.cg_item);
1023 else
1024 configfs_undepend_item_unlocked(
1025 &line->group.cg_item);
1026 }
1027 }
1028
1029 static ssize_t
gpio_aggregator_line_key_show(struct config_item * item,char * page)1030 gpio_aggregator_line_key_show(struct config_item *item, char *page)
1031 {
1032 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item);
1033 struct gpio_aggregator *aggr = line->parent;
1034
1035 guard(mutex)(&aggr->lock);
1036
1037 return sysfs_emit(page, "%s\n", line->key ?: "");
1038 }
1039
1040 static ssize_t
gpio_aggregator_line_key_store(struct config_item * item,const char * page,size_t count)1041 gpio_aggregator_line_key_store(struct config_item *item, const char *page,
1042 size_t count)
1043 {
1044 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item);
1045 struct gpio_aggregator *aggr = line->parent;
1046
1047 char *key __free(kfree) = kstrndup(skip_spaces(page), count,
1048 GFP_KERNEL);
1049 if (!key)
1050 return -ENOMEM;
1051
1052 strim(key);
1053
1054 guard(mutex)(&aggr->lock);
1055
1056 if (gpio_aggregator_is_activating(aggr) ||
1057 gpio_aggregator_is_active(aggr))
1058 return -EBUSY;
1059
1060 kfree(line->key);
1061 line->key = no_free_ptr(key);
1062
1063 return count;
1064 }
1065 CONFIGFS_ATTR(gpio_aggregator_line_, key);
1066
1067 static ssize_t
gpio_aggregator_line_name_show(struct config_item * item,char * page)1068 gpio_aggregator_line_name_show(struct config_item *item, char *page)
1069 {
1070 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item);
1071 struct gpio_aggregator *aggr = line->parent;
1072
1073 guard(mutex)(&aggr->lock);
1074
1075 return sysfs_emit(page, "%s\n", line->name ?: "");
1076 }
1077
1078 static ssize_t
gpio_aggregator_line_name_store(struct config_item * item,const char * page,size_t count)1079 gpio_aggregator_line_name_store(struct config_item *item, const char *page,
1080 size_t count)
1081 {
1082 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item);
1083 struct gpio_aggregator *aggr = line->parent;
1084
1085 char *name __free(kfree) = kstrndup(skip_spaces(page), count,
1086 GFP_KERNEL);
1087 if (!name)
1088 return -ENOMEM;
1089
1090 strim(name);
1091
1092 guard(mutex)(&aggr->lock);
1093
1094 if (gpio_aggregator_is_activating(aggr) ||
1095 gpio_aggregator_is_active(aggr))
1096 return -EBUSY;
1097
1098 kfree(line->name);
1099 line->name = no_free_ptr(name);
1100
1101 return count;
1102 }
1103 CONFIGFS_ATTR(gpio_aggregator_line_, name);
1104
1105 static ssize_t
gpio_aggregator_line_offset_show(struct config_item * item,char * page)1106 gpio_aggregator_line_offset_show(struct config_item *item, char *page)
1107 {
1108 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item);
1109 struct gpio_aggregator *aggr = line->parent;
1110
1111 guard(mutex)(&aggr->lock);
1112
1113 return sysfs_emit(page, "%d\n", line->offset);
1114 }
1115
1116 static ssize_t
gpio_aggregator_line_offset_store(struct config_item * item,const char * page,size_t count)1117 gpio_aggregator_line_offset_store(struct config_item *item, const char *page,
1118 size_t count)
1119 {
1120 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item);
1121 struct gpio_aggregator *aggr = line->parent;
1122 int offset, ret;
1123
1124 ret = kstrtoint(page, 0, &offset);
1125 if (ret)
1126 return ret;
1127
1128 /*
1129 * When offset == -1, 'key' represents a line name to lookup.
1130 * When 0 <= offset < 65535, 'key' represents the label of the chip with
1131 * the 'offset' value representing the line within that chip.
1132 *
1133 * GPIOLIB uses the U16_MAX value to indicate lookup by line name so
1134 * the greatest offset we can accept is (U16_MAX - 1).
1135 */
1136 if (offset > (U16_MAX - 1) || offset < -1)
1137 return -EINVAL;
1138
1139 guard(mutex)(&aggr->lock);
1140
1141 if (gpio_aggregator_is_activating(aggr) ||
1142 gpio_aggregator_is_active(aggr))
1143 return -EBUSY;
1144
1145 line->offset = offset;
1146
1147 return count;
1148 }
1149 CONFIGFS_ATTR(gpio_aggregator_line_, offset);
1150
1151 static struct configfs_attribute *gpio_aggregator_line_attrs[] = {
1152 &gpio_aggregator_line_attr_key,
1153 &gpio_aggregator_line_attr_name,
1154 &gpio_aggregator_line_attr_offset,
1155 NULL
1156 };
1157
1158 static ssize_t
gpio_aggregator_device_dev_name_show(struct config_item * item,char * page)1159 gpio_aggregator_device_dev_name_show(struct config_item *item, char *page)
1160 {
1161 struct gpio_aggregator *aggr = to_gpio_aggregator(item);
1162 struct platform_device *pdev;
1163
1164 guard(mutex)(&aggr->lock);
1165
1166 pdev = aggr->pdev;
1167 if (pdev)
1168 return sysfs_emit(page, "%s\n", dev_name(&pdev->dev));
1169
1170 return sysfs_emit(page, "%s.%d\n", DRV_NAME, aggr->id);
1171 }
1172 CONFIGFS_ATTR_RO(gpio_aggregator_device_, dev_name);
1173
1174 static ssize_t
gpio_aggregator_device_live_show(struct config_item * item,char * page)1175 gpio_aggregator_device_live_show(struct config_item *item, char *page)
1176 {
1177 struct gpio_aggregator *aggr = to_gpio_aggregator(item);
1178
1179 guard(mutex)(&aggr->lock);
1180
1181 return sysfs_emit(page, "%c\n",
1182 gpio_aggregator_is_active(aggr) ? '1' : '0');
1183 }
1184
1185 static ssize_t
gpio_aggregator_device_live_store(struct config_item * item,const char * page,size_t count)1186 gpio_aggregator_device_live_store(struct config_item *item, const char *page,
1187 size_t count)
1188 {
1189 struct gpio_aggregator *aggr = to_gpio_aggregator(item);
1190 int ret = 0;
1191 bool live;
1192
1193 ret = kstrtobool(page, &live);
1194 if (ret)
1195 return ret;
1196
1197 if (!try_module_get(THIS_MODULE))
1198 return -ENOENT;
1199
1200 if (live && !aggr->init_via_sysfs)
1201 gpio_aggregator_lockup_configfs(aggr, true);
1202
1203 scoped_guard(mutex, &aggr->lock) {
1204 if (gpio_aggregator_is_activating(aggr) ||
1205 (live == gpio_aggregator_is_active(aggr)))
1206 ret = -EPERM;
1207 else if (live)
1208 ret = gpio_aggregator_activate(aggr);
1209 else
1210 gpio_aggregator_deactivate(aggr);
1211 }
1212
1213 /*
1214 * Undepend is required only if device disablement (live == 0)
1215 * succeeds or if device enablement (live == 1) fails.
1216 */
1217 if (live == !!ret && !aggr->init_via_sysfs)
1218 gpio_aggregator_lockup_configfs(aggr, false);
1219
1220 module_put(THIS_MODULE);
1221
1222 return ret ?: count;
1223 }
1224 CONFIGFS_ATTR(gpio_aggregator_device_, live);
1225
1226 static struct configfs_attribute *gpio_aggregator_device_attrs[] = {
1227 &gpio_aggregator_device_attr_dev_name,
1228 &gpio_aggregator_device_attr_live,
1229 NULL
1230 };
1231
1232 static void
gpio_aggregator_line_release(struct config_item * item)1233 gpio_aggregator_line_release(struct config_item *item)
1234 {
1235 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item);
1236 struct gpio_aggregator *aggr = line->parent;
1237
1238 guard(mutex)(&aggr->lock);
1239
1240 gpio_aggregator_line_del(aggr, line);
1241 kfree(line->key);
1242 kfree(line->name);
1243 kfree(line);
1244 }
1245
1246 static const struct configfs_item_operations gpio_aggregator_line_item_ops = {
1247 .release = gpio_aggregator_line_release,
1248 };
1249
1250 static const struct config_item_type gpio_aggregator_line_type = {
1251 .ct_item_ops = &gpio_aggregator_line_item_ops,
1252 .ct_attrs = gpio_aggregator_line_attrs,
1253 .ct_owner = THIS_MODULE,
1254 };
1255
gpio_aggregator_device_release(struct config_item * item)1256 static void gpio_aggregator_device_release(struct config_item *item)
1257 {
1258 struct gpio_aggregator *aggr = to_gpio_aggregator(item);
1259
1260 /*
1261 * At this point, aggr is neither active nor activating,
1262 * so calling gpio_aggregator_deactivate() is always unnecessary.
1263 */
1264 gpio_aggregator_free(aggr);
1265 }
1266
1267 static const struct configfs_item_operations gpio_aggregator_device_item_ops = {
1268 .release = gpio_aggregator_device_release,
1269 };
1270
1271 static struct config_group *
gpio_aggregator_device_make_group(struct config_group * group,const char * name)1272 gpio_aggregator_device_make_group(struct config_group *group, const char *name)
1273 {
1274 struct gpio_aggregator *aggr = to_gpio_aggregator(&group->cg_item);
1275 struct gpio_aggregator_line *line;
1276 unsigned int idx;
1277 int ret, nchar;
1278
1279 ret = sscanf(name, "line%u%n", &idx, &nchar);
1280 if (ret != 1 || nchar != strlen(name))
1281 return ERR_PTR(-EINVAL);
1282
1283 if (aggr->init_via_sysfs)
1284 /*
1285 * Aggregators created via legacy sysfs interface are exposed as
1286 * default groups, which means rmdir(2) is prohibited for them.
1287 * For simplicity, and to avoid confusion, we also prohibit
1288 * mkdir(2).
1289 */
1290 return ERR_PTR(-EPERM);
1291
1292 guard(mutex)(&aggr->lock);
1293
1294 if (gpio_aggregator_is_active(aggr))
1295 return ERR_PTR(-EBUSY);
1296
1297 list_for_each_entry(line, &aggr->list_head, entry)
1298 if (line->idx == idx)
1299 return ERR_PTR(-EINVAL);
1300
1301 line = gpio_aggregator_line_alloc(aggr, idx, NULL, -1);
1302 if (IS_ERR(line))
1303 return ERR_CAST(line);
1304
1305 config_group_init_type_name(&line->group, name, &gpio_aggregator_line_type);
1306
1307 gpio_aggregator_line_add(aggr, line);
1308
1309 return &line->group;
1310 }
1311
1312 static const struct configfs_group_operations gpio_aggregator_device_group_ops = {
1313 .make_group = gpio_aggregator_device_make_group,
1314 };
1315
1316 static const struct config_item_type gpio_aggregator_device_type = {
1317 .ct_group_ops = &gpio_aggregator_device_group_ops,
1318 .ct_item_ops = &gpio_aggregator_device_item_ops,
1319 .ct_attrs = gpio_aggregator_device_attrs,
1320 .ct_owner = THIS_MODULE,
1321 };
1322
1323 static struct config_group *
gpio_aggregator_make_group(struct config_group * group,const char * name)1324 gpio_aggregator_make_group(struct config_group *group, const char *name)
1325 {
1326 struct gpio_aggregator *aggr;
1327 int ret;
1328
1329 /*
1330 * "_sysfs" prefix is reserved for auto-generated config group
1331 * for devices create via legacy sysfs interface.
1332 */
1333 if (strncmp(name, AGGREGATOR_LEGACY_PREFIX,
1334 sizeof(AGGREGATOR_LEGACY_PREFIX) - 1) == 0)
1335 return ERR_PTR(-EINVAL);
1336
1337 /* arg space is unneeded */
1338 ret = gpio_aggregator_alloc(&aggr, 0);
1339 if (ret)
1340 return ERR_PTR(ret);
1341
1342 config_group_init_type_name(&aggr->group, name, &gpio_aggregator_device_type);
1343
1344 return &aggr->group;
1345 }
1346
1347 static const struct configfs_group_operations gpio_aggregator_group_ops = {
1348 .make_group = gpio_aggregator_make_group,
1349 };
1350
1351 static const struct config_item_type gpio_aggregator_type = {
1352 .ct_group_ops = &gpio_aggregator_group_ops,
1353 .ct_owner = THIS_MODULE,
1354 };
1355
1356 static struct configfs_subsystem gpio_aggregator_subsys = {
1357 .su_group = {
1358 .cg_item = {
1359 .ci_namebuf = DRV_NAME,
1360 .ci_type = &gpio_aggregator_type,
1361 },
1362 },
1363 };
1364
1365 /*
1366 * Sysfs interface
1367 */
gpio_aggregator_parse(struct gpio_aggregator * aggr)1368 static int gpio_aggregator_parse(struct gpio_aggregator *aggr)
1369 {
1370 char *args = skip_spaces(aggr->args);
1371 struct gpio_aggregator_line *line;
1372 char name[CONFIGFS_ITEM_NAME_LEN];
1373 char *key, *offsets, *p;
1374 unsigned int i, n = 0;
1375 int error = 0;
1376
1377 unsigned long *bitmap __free(bitmap) =
1378 bitmap_alloc(AGGREGATOR_MAX_GPIOS, GFP_KERNEL);
1379 if (!bitmap)
1380 return -ENOMEM;
1381
1382 args = next_arg(args, &key, &p);
1383 while (*args) {
1384 args = next_arg(args, &offsets, &p);
1385
1386 p = get_options(offsets, 0, &error);
1387 if (error == 0 || *p) {
1388 /* Named GPIO line */
1389 scnprintf(name, sizeof(name), "line%u", n);
1390 line = gpio_aggregator_line_alloc(aggr, n, key, -1);
1391 if (IS_ERR(line)) {
1392 error = PTR_ERR(line);
1393 goto err;
1394 }
1395 config_group_init_type_name(&line->group, name,
1396 &gpio_aggregator_line_type);
1397 error = configfs_register_group(&aggr->group,
1398 &line->group);
1399 if (error)
1400 goto err;
1401 scoped_guard(mutex, &aggr->lock)
1402 gpio_aggregator_line_add(aggr, line);
1403
1404 error = gpio_aggregator_add_gpio(aggr, key, U16_MAX, &n);
1405 if (error)
1406 goto err;
1407
1408 key = offsets;
1409 continue;
1410 }
1411
1412 /* GPIO chip + offset(s) */
1413 error = bitmap_parselist(offsets, bitmap, AGGREGATOR_MAX_GPIOS);
1414 if (error) {
1415 pr_err("Cannot parse %s: %d\n", offsets, error);
1416 goto err;
1417 }
1418
1419 for_each_set_bit(i, bitmap, AGGREGATOR_MAX_GPIOS) {
1420 scnprintf(name, sizeof(name), "line%u", n);
1421 line = gpio_aggregator_line_alloc(aggr, n, key, i);
1422 if (IS_ERR(line)) {
1423 error = PTR_ERR(line);
1424 goto err;
1425 }
1426 config_group_init_type_name(&line->group, name,
1427 &gpio_aggregator_line_type);
1428 error = configfs_register_group(&aggr->group,
1429 &line->group);
1430 if (error)
1431 goto err;
1432 scoped_guard(mutex, &aggr->lock)
1433 gpio_aggregator_line_add(aggr, line);
1434
1435 error = gpio_aggregator_add_gpio(aggr, key, i, &n);
1436 if (error)
1437 goto err;
1438 }
1439
1440 args = next_arg(args, &key, &p);
1441 }
1442
1443 if (!n) {
1444 pr_err("No GPIOs specified\n");
1445 error = -EINVAL;
1446 goto err;
1447 }
1448
1449 return 0;
1450
1451 err:
1452 gpio_aggregator_free_lines(aggr);
1453 return error;
1454 }
1455
gpio_aggregator_new_device_store(struct device_driver * driver,const char * buf,size_t count)1456 static ssize_t gpio_aggregator_new_device_store(struct device_driver *driver,
1457 const char *buf, size_t count)
1458 {
1459 struct gpio_aggregator_pdev_meta meta = { .init_via_sysfs = true };
1460 char name[CONFIGFS_ITEM_NAME_LEN];
1461 struct gpio_aggregator *aggr;
1462 struct platform_device *pdev;
1463 int res;
1464
1465 if (!try_module_get(THIS_MODULE))
1466 return -ENOENT;
1467
1468 /* kernfs guarantees string termination, so count + 1 is safe */
1469 res = gpio_aggregator_alloc(&aggr, count + 1);
1470 if (res)
1471 goto put_module;
1472
1473 memcpy(aggr->args, buf, count + 1);
1474
1475 aggr->init_via_sysfs = true;
1476 aggr->lookups = kzalloc_flex(*aggr->lookups, table, 1);
1477 if (!aggr->lookups) {
1478 res = -ENOMEM;
1479 goto free_ga;
1480 }
1481
1482 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, aggr->id);
1483 if (!aggr->lookups->dev_id) {
1484 res = -ENOMEM;
1485 goto free_table;
1486 }
1487
1488 scnprintf(name, sizeof(name), "%s.%d", AGGREGATOR_LEGACY_PREFIX, aggr->id);
1489 config_group_init_type_name(&aggr->group, name, &gpio_aggregator_device_type);
1490
1491 /* Expose to configfs */
1492 res = configfs_register_group(&gpio_aggregator_subsys.su_group,
1493 &aggr->group);
1494 if (res)
1495 goto free_dev_id;
1496
1497 res = gpio_aggregator_parse(aggr);
1498 if (res)
1499 goto unregister_group;
1500
1501 gpiod_add_lookup_table(aggr->lookups);
1502
1503 pdev = platform_device_register_data(NULL, DRV_NAME, aggr->id, &meta, sizeof(meta));
1504 if (IS_ERR(pdev)) {
1505 res = PTR_ERR(pdev);
1506 goto remove_table;
1507 }
1508
1509 aggr->pdev = pdev;
1510 module_put(THIS_MODULE);
1511 return count;
1512
1513 remove_table:
1514 gpiod_remove_lookup_table(aggr->lookups);
1515 unregister_group:
1516 configfs_unregister_group(&aggr->group);
1517 free_dev_id:
1518 kfree(aggr->lookups->dev_id);
1519 free_table:
1520 kfree(aggr->lookups);
1521 free_ga:
1522 gpio_aggregator_free(aggr);
1523 put_module:
1524 module_put(THIS_MODULE);
1525 return res;
1526 }
1527
1528 static struct driver_attribute driver_attr_gpio_aggregator_new_device =
1529 __ATTR(new_device, 0200, NULL, gpio_aggregator_new_device_store);
1530
gpio_aggregator_destroy(struct gpio_aggregator * aggr)1531 static void gpio_aggregator_destroy(struct gpio_aggregator *aggr)
1532 {
1533 scoped_guard(mutex, &aggr->lock) {
1534 if (gpio_aggregator_is_activating(aggr) ||
1535 gpio_aggregator_is_active(aggr))
1536 gpio_aggregator_deactivate(aggr);
1537 }
1538 gpio_aggregator_free_lines(aggr);
1539 configfs_unregister_group(&aggr->group);
1540 kfree(aggr);
1541 }
1542
gpio_aggregator_delete_device_store(struct device_driver * driver,const char * buf,size_t count)1543 static ssize_t gpio_aggregator_delete_device_store(struct device_driver *driver,
1544 const char *buf, size_t count)
1545 {
1546 struct gpio_aggregator *aggr;
1547 unsigned int id;
1548 int error;
1549
1550 if (!str_has_prefix(buf, DRV_NAME "."))
1551 return -EINVAL;
1552
1553 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
1554 if (error)
1555 return error;
1556
1557 if (!try_module_get(THIS_MODULE))
1558 return -ENOENT;
1559
1560 mutex_lock(&gpio_aggregator_lock);
1561 aggr = idr_find(&gpio_aggregator_idr, id);
1562 /*
1563 * For simplicity, devices created via configfs cannot be deleted
1564 * via sysfs.
1565 */
1566 if (aggr && aggr->init_via_sysfs)
1567 idr_remove(&gpio_aggregator_idr, id);
1568 else {
1569 mutex_unlock(&gpio_aggregator_lock);
1570 module_put(THIS_MODULE);
1571 return -ENOENT;
1572 }
1573 mutex_unlock(&gpio_aggregator_lock);
1574
1575 gpio_aggregator_destroy(aggr);
1576 module_put(THIS_MODULE);
1577 return count;
1578 }
1579
1580 static struct driver_attribute driver_attr_gpio_aggregator_delete_device =
1581 __ATTR(delete_device, 0200, NULL, gpio_aggregator_delete_device_store);
1582
1583 static struct attribute *gpio_aggregator_attrs[] = {
1584 &driver_attr_gpio_aggregator_new_device.attr,
1585 &driver_attr_gpio_aggregator_delete_device.attr,
1586 NULL
1587 };
1588 ATTRIBUTE_GROUPS(gpio_aggregator);
1589
1590 /*
1591 * GPIO Aggregator platform device
1592 */
1593
gpio_aggregator_probe(struct platform_device * pdev)1594 static int gpio_aggregator_probe(struct platform_device *pdev)
1595 {
1596 struct gpio_aggregator_pdev_meta *meta;
1597 struct device *dev = &pdev->dev;
1598 bool init_via_sysfs = false;
1599 struct gpio_desc **descs;
1600 struct gpiochip_fwd *fwd;
1601 unsigned long features;
1602 int i, n;
1603
1604 n = gpiod_count(dev, NULL);
1605 if (n < 0)
1606 return n;
1607
1608 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
1609 if (!descs)
1610 return -ENOMEM;
1611
1612 meta = dev_get_platdata(&pdev->dev);
1613 if (meta && meta->init_via_sysfs)
1614 init_via_sysfs = true;
1615
1616 for (i = 0; i < n; i++) {
1617 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
1618 if (IS_ERR(descs[i])) {
1619 /*
1620 * Deferred probing is not suitable when the aggregator
1621 * is created via configfs. They should just retry later
1622 * whenever they like. For device creation via sysfs,
1623 * error is propagated without overriding for backward
1624 * compatibility. .prevent_deferred_probe is kept unset
1625 * for other cases.
1626 */
1627 if (!init_via_sysfs && !dev_of_node(dev) &&
1628 descs[i] == ERR_PTR(-EPROBE_DEFER)) {
1629 pr_warn("Deferred probe canceled for creation via configfs.\n");
1630 return -ENODEV;
1631 }
1632 return PTR_ERR(descs[i]);
1633 }
1634 }
1635
1636 features = (uintptr_t)device_get_match_data(dev);
1637 fwd = gpiochip_fwd_create(dev, n, descs, features);
1638 if (IS_ERR(fwd))
1639 return PTR_ERR(fwd);
1640
1641 platform_set_drvdata(pdev, fwd);
1642 devm_kfree(dev, descs);
1643 return 0;
1644 }
1645
1646 static const struct of_device_id gpio_aggregator_dt_ids[] = {
1647 {
1648 .compatible = "gpio-delay",
1649 .data = (void *)FWD_FEATURE_DELAY,
1650 },
1651 /*
1652 * Add GPIO-operated devices controlled from userspace below,
1653 * or use "driver_override" in sysfs.
1654 */
1655 {}
1656 };
1657 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
1658
1659 static struct platform_driver gpio_aggregator_driver = {
1660 .probe = gpio_aggregator_probe,
1661 .driver = {
1662 .name = DRV_NAME,
1663 .groups = gpio_aggregator_groups,
1664 .of_match_table = gpio_aggregator_dt_ids,
1665 },
1666 };
1667
gpio_aggregator_idr_remove(int id,void * p,void * data)1668 static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
1669 {
1670 /*
1671 * There should be no aggregator created via configfs, as their
1672 * presence would prevent module unloading.
1673 */
1674 gpio_aggregator_destroy(p);
1675 return 0;
1676 }
1677
gpio_aggregator_remove_all(void)1678 static void __exit gpio_aggregator_remove_all(void)
1679 {
1680 /*
1681 * Configfs callbacks acquire gpio_aggregator_lock when accessing
1682 * gpio_aggregator_idr, so to prevent lock inversion deadlock, we
1683 * cannot protect idr_for_each invocation here with
1684 * gpio_aggregator_lock, as gpio_aggregator_idr_remove() accesses
1685 * configfs groups. Fortunately, the new_device/delete_device path
1686 * and the module unload path are mutually exclusive, thanks to an
1687 * explicit try_module_get inside of those driver attr handlers.
1688 * Also, when we reach here, no configfs entries present or being
1689 * created. Therefore, no need to protect with gpio_aggregator_lock
1690 * below.
1691 */
1692 idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
1693 idr_destroy(&gpio_aggregator_idr);
1694 }
1695
gpio_aggregator_init(void)1696 static int __init gpio_aggregator_init(void)
1697 {
1698 int ret = 0;
1699
1700 config_group_init(&gpio_aggregator_subsys.su_group);
1701 mutex_init(&gpio_aggregator_subsys.su_mutex);
1702 ret = configfs_register_subsystem(&gpio_aggregator_subsys);
1703 if (ret) {
1704 pr_err("Failed to register the '%s' configfs subsystem: %d\n",
1705 gpio_aggregator_subsys.su_group.cg_item.ci_namebuf, ret);
1706 mutex_destroy(&gpio_aggregator_subsys.su_mutex);
1707 return ret;
1708 }
1709
1710 /*
1711 * CAVEAT: This must occur after configfs registration. Otherwise,
1712 * a race condition could arise: driver attribute groups might be
1713 * exposed and accessed by users before configfs registration
1714 * completes. new_device_store() does not expect a partially
1715 * initialized configfs state.
1716 */
1717 ret = platform_driver_register(&gpio_aggregator_driver);
1718 if (ret) {
1719 pr_err("Failed to register the platform driver: %d\n", ret);
1720 mutex_destroy(&gpio_aggregator_subsys.su_mutex);
1721 configfs_unregister_subsystem(&gpio_aggregator_subsys);
1722 }
1723
1724 return ret;
1725 }
1726 module_init(gpio_aggregator_init);
1727
gpio_aggregator_exit(void)1728 static void __exit gpio_aggregator_exit(void)
1729 {
1730 gpio_aggregator_remove_all();
1731 platform_driver_unregister(&gpio_aggregator_driver);
1732 configfs_unregister_subsystem(&gpio_aggregator_subsys);
1733 }
1734 module_exit(gpio_aggregator_exit);
1735
1736 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
1737 MODULE_DESCRIPTION("GPIO Aggregator");
1738 MODULE_LICENSE("GPL v2");
1739