Linux Audio

Check our new training course

Loading...
v6.13.7
  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/ctype.h>
 13#include <linux/delay.h>
 
 
 
 14#include <linux/idr.h>
 15#include <linux/kernel.h>
 16#include <linux/mod_devicetable.h>
 17#include <linux/module.h>
 18#include <linux/mutex.h>
 19#include <linux/overflow.h>
 20#include <linux/platform_device.h>
 21#include <linux/property.h>
 22#include <linux/slab.h>
 23#include <linux/spinlock.h>
 24#include <linux/string.h>
 25
 26#include <linux/gpio/consumer.h>
 27#include <linux/gpio/driver.h>
 28#include <linux/gpio/machine.h>
 29
 30#define AGGREGATOR_MAX_GPIOS 512
 31
 32/*
 33 * GPIO Aggregator sysfs interface
 34 */
 35
 36struct gpio_aggregator {
 37	struct gpiod_lookup_table *lookups;
 38	struct platform_device *pdev;
 39	char args[];
 40};
 41
 42static DEFINE_MUTEX(gpio_aggregator_lock);	/* protects idr */
 43static DEFINE_IDR(gpio_aggregator_idr);
 44
 45static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
 46			 int hwnum, unsigned int *n)
 47{
 48	struct gpiod_lookup_table *lookups;
 49
 50	lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
 51			   GFP_KERNEL);
 52	if (!lookups)
 53		return -ENOMEM;
 54
 55	lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
 56
 57	(*n)++;
 58	memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
 59
 60	aggr->lookups = lookups;
 61	return 0;
 62}
 63
 64static int aggr_parse(struct gpio_aggregator *aggr)
 65{
 66	char *args = skip_spaces(aggr->args);
 67	char *name, *offsets, *p;
 
 68	unsigned int i, n = 0;
 69	int error = 0;
 70
 71	unsigned long *bitmap __free(bitmap) =
 72			bitmap_alloc(AGGREGATOR_MAX_GPIOS, GFP_KERNEL);
 73	if (!bitmap)
 74		return -ENOMEM;
 75
 76	args = next_arg(args, &name, &p);
 77	while (*args) {
 78		args = next_arg(args, &offsets, &p);
 79
 80		p = get_options(offsets, 0, &error);
 81		if (error == 0 || *p) {
 82			/* Named GPIO line */
 83			error = aggr_add_gpio(aggr, name, U16_MAX, &n);
 84			if (error)
 85				return error;
 86
 87			name = offsets;
 88			continue;
 89		}
 90
 91		/* GPIO chip + offset(s) */
 92		error = bitmap_parselist(offsets, bitmap, AGGREGATOR_MAX_GPIOS);
 93		if (error) {
 94			pr_err("Cannot parse %s: %d\n", offsets, error);
 95			return error;
 96		}
 97
 98		for_each_set_bit(i, bitmap, AGGREGATOR_MAX_GPIOS) {
 99			error = aggr_add_gpio(aggr, name, i, &n);
100			if (error)
101				return error;
102		}
103
104		args = next_arg(args, &name, &p);
105	}
106
107	if (!n) {
108		pr_err("No GPIOs specified\n");
109		return -EINVAL;
110	}
111
112	return 0;
 
 
113}
114
115static ssize_t new_device_store(struct device_driver *driver, const char *buf,
116				size_t count)
117{
118	struct gpio_aggregator *aggr;
119	struct platform_device *pdev;
120	int res, id;
121
122	if (!try_module_get(THIS_MODULE))
123		return -ENOENT;
124
125	/* kernfs guarantees string termination, so count + 1 is safe */
126	aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
127	if (!aggr) {
128		res = -ENOMEM;
129		goto put_module;
130	}
131
132	memcpy(aggr->args, buf, count + 1);
133
134	aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
135				GFP_KERNEL);
136	if (!aggr->lookups) {
137		res = -ENOMEM;
138		goto free_ga;
139	}
140
141	mutex_lock(&gpio_aggregator_lock);
142	id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
143	mutex_unlock(&gpio_aggregator_lock);
144
145	if (id < 0) {
146		res = id;
147		goto free_table;
148	}
149
150	aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
151	if (!aggr->lookups->dev_id) {
152		res = -ENOMEM;
153		goto remove_idr;
154	}
155
156	res = aggr_parse(aggr);
157	if (res)
158		goto free_dev_id;
159
160	gpiod_add_lookup_table(aggr->lookups);
161
162	pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
163	if (IS_ERR(pdev)) {
164		res = PTR_ERR(pdev);
165		goto remove_table;
166	}
167
168	aggr->pdev = pdev;
169	module_put(THIS_MODULE);
170	return count;
171
172remove_table:
173	gpiod_remove_lookup_table(aggr->lookups);
174free_dev_id:
175	kfree(aggr->lookups->dev_id);
176remove_idr:
177	mutex_lock(&gpio_aggregator_lock);
178	idr_remove(&gpio_aggregator_idr, id);
179	mutex_unlock(&gpio_aggregator_lock);
180free_table:
181	kfree(aggr->lookups);
182free_ga:
183	kfree(aggr);
184put_module:
185	module_put(THIS_MODULE);
186	return res;
187}
188
189static DRIVER_ATTR_WO(new_device);
190
191static void gpio_aggregator_free(struct gpio_aggregator *aggr)
192{
193	platform_device_unregister(aggr->pdev);
194	gpiod_remove_lookup_table(aggr->lookups);
195	kfree(aggr->lookups->dev_id);
196	kfree(aggr->lookups);
197	kfree(aggr);
198}
199
200static ssize_t delete_device_store(struct device_driver *driver,
201				   const char *buf, size_t count)
202{
203	struct gpio_aggregator *aggr;
204	unsigned int id;
205	int error;
206
207	if (!str_has_prefix(buf, DRV_NAME "."))
208		return -EINVAL;
209
210	error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
211	if (error)
212		return error;
213
214	if (!try_module_get(THIS_MODULE))
215		return -ENOENT;
216
217	mutex_lock(&gpio_aggregator_lock);
218	aggr = idr_remove(&gpio_aggregator_idr, id);
219	mutex_unlock(&gpio_aggregator_lock);
220	if (!aggr) {
221		module_put(THIS_MODULE);
222		return -ENOENT;
223	}
224
225	gpio_aggregator_free(aggr);
226	module_put(THIS_MODULE);
227	return count;
228}
229static DRIVER_ATTR_WO(delete_device);
230
231static struct attribute *gpio_aggregator_attrs[] = {
232	&driver_attr_new_device.attr,
233	&driver_attr_delete_device.attr,
234	NULL
235};
236ATTRIBUTE_GROUPS(gpio_aggregator);
237
238static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
239{
240	gpio_aggregator_free(p);
241	return 0;
242}
243
244static void __exit gpio_aggregator_remove_all(void)
245{
246	mutex_lock(&gpio_aggregator_lock);
247	idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
248	idr_destroy(&gpio_aggregator_idr);
249	mutex_unlock(&gpio_aggregator_lock);
250}
251
252
253/*
254 *  GPIO Forwarder
255 */
256
257struct gpiochip_fwd_timing {
258	u32 ramp_up_us;
259	u32 ramp_down_us;
260};
261
262struct gpiochip_fwd {
263	struct gpio_chip chip;
264	struct gpio_desc **descs;
265	union {
266		struct mutex mlock;	/* protects tmp[] if can_sleep */
267		spinlock_t slock;	/* protects tmp[] if !can_sleep */
268	};
269	struct gpiochip_fwd_timing *delay_timings;
270	unsigned long tmp[];		/* values and descs for multiple ops */
271};
272
273#define fwd_tmp_values(fwd)	&(fwd)->tmp[0]
274#define fwd_tmp_descs(fwd)	(void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
275
276#define fwd_tmp_size(ngpios)	(BITS_TO_LONGS((ngpios)) + (ngpios))
277
278static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
279{
280	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
281
282	return gpiod_get_direction(fwd->descs[offset]);
283}
284
285static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
286{
287	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
288
289	return gpiod_direction_input(fwd->descs[offset]);
290}
291
292static int gpio_fwd_direction_output(struct gpio_chip *chip,
293				     unsigned int offset, int value)
294{
295	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
296
297	return gpiod_direction_output(fwd->descs[offset], value);
298}
299
300static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
301{
302	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
303
304	return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset])
305			       : gpiod_get_value(fwd->descs[offset]);
306}
307
308static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
309				 unsigned long *bits)
310{
311	struct gpio_desc **descs = fwd_tmp_descs(fwd);
312	unsigned long *values = fwd_tmp_values(fwd);
313	unsigned int i, j = 0;
314	int error;
315
 
 
 
 
316	bitmap_clear(values, 0, fwd->chip.ngpio);
317	for_each_set_bit(i, mask, fwd->chip.ngpio)
318		descs[j++] = fwd->descs[i];
319
320	if (fwd->chip.can_sleep)
321		error = gpiod_get_array_value_cansleep(j, descs, NULL, values);
322	else
323		error = gpiod_get_array_value(j, descs, NULL, values);
324	if (error)
325		return error;
326
327	j = 0;
328	for_each_set_bit(i, mask, fwd->chip.ngpio)
329		__assign_bit(i, bits, test_bit(j++, values));
330
331	return 0;
332}
333
334static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
335					unsigned long *mask, unsigned long *bits)
336{
337	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
338	unsigned long flags;
339	int error;
340
341	if (chip->can_sleep) {
342		mutex_lock(&fwd->mlock);
343		error = gpio_fwd_get_multiple(fwd, mask, bits);
344		mutex_unlock(&fwd->mlock);
345	} else {
346		spin_lock_irqsave(&fwd->slock, flags);
347		error = gpio_fwd_get_multiple(fwd, mask, bits);
348		spin_unlock_irqrestore(&fwd->slock, flags);
349	}
350
351	return error;
352}
353
354static void gpio_fwd_delay(struct gpio_chip *chip, unsigned int offset, int value)
355{
356	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
357	const struct gpiochip_fwd_timing *delay_timings;
358	bool is_active_low = gpiod_is_active_low(fwd->descs[offset]);
359	u32 delay_us;
360
361	delay_timings = &fwd->delay_timings[offset];
362	if ((!is_active_low && value) || (is_active_low && !value))
363		delay_us = delay_timings->ramp_up_us;
364	else
365		delay_us = delay_timings->ramp_down_us;
366	if (!delay_us)
367		return;
368
369	if (chip->can_sleep)
370		fsleep(delay_us);
371	else
372		udelay(delay_us);
373}
374
375static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
376{
377	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
378
379	if (chip->can_sleep)
380		gpiod_set_value_cansleep(fwd->descs[offset], value);
381	else
382		gpiod_set_value(fwd->descs[offset], value);
383
384	if (fwd->delay_timings)
385		gpio_fwd_delay(chip, offset, value);
386}
387
388static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
389				  unsigned long *bits)
390{
391	struct gpio_desc **descs = fwd_tmp_descs(fwd);
392	unsigned long *values = fwd_tmp_values(fwd);
393	unsigned int i, j = 0;
394
 
 
 
 
395	for_each_set_bit(i, mask, fwd->chip.ngpio) {
396		__assign_bit(j, values, test_bit(i, bits));
397		descs[j++] = fwd->descs[i];
398	}
399
400	if (fwd->chip.can_sleep)
401		gpiod_set_array_value_cansleep(j, descs, NULL, values);
402	else
403		gpiod_set_array_value(j, descs, NULL, values);
404}
405
406static void 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
412	if (chip->can_sleep) {
413		mutex_lock(&fwd->mlock);
414		gpio_fwd_set_multiple(fwd, mask, bits);
415		mutex_unlock(&fwd->mlock);
416	} else {
417		spin_lock_irqsave(&fwd->slock, flags);
418		gpio_fwd_set_multiple(fwd, mask, bits);
419		spin_unlock_irqrestore(&fwd->slock, flags);
420	}
421}
422
423static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
424			       unsigned long config)
425{
426	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
427
428	return gpiod_set_config(fwd->descs[offset], config);
429}
430
431static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
432{
433	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
434
435	return gpiod_to_irq(fwd->descs[offset]);
436}
437
438/*
439 * The GPIO delay provides a way to configure platform specific delays
440 * for the GPIO ramp-up or ramp-down delays. This can serve the following
441 * purposes:
442 *   - Open-drain output using an RC filter
443 */
444#define FWD_FEATURE_DELAY		BIT(0)
445
446#ifdef CONFIG_OF_GPIO
447static int gpiochip_fwd_delay_of_xlate(struct gpio_chip *chip,
448				       const struct of_phandle_args *gpiospec,
449				       u32 *flags)
450{
451	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
452	struct gpiochip_fwd_timing *timings;
453	u32 line;
454
455	if (gpiospec->args_count != chip->of_gpio_n_cells)
456		return -EINVAL;
457
458	line = gpiospec->args[0];
459	if (line >= chip->ngpio)
460		return -EINVAL;
461
462	timings = &fwd->delay_timings[line];
463	timings->ramp_up_us = gpiospec->args[1];
464	timings->ramp_down_us = gpiospec->args[2];
465
466	return line;
467}
468
469static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *chip,
470					 struct gpiochip_fwd *fwd)
471{
472	fwd->delay_timings = devm_kcalloc(dev, chip->ngpio,
473					  sizeof(*fwd->delay_timings),
474					  GFP_KERNEL);
475	if (!fwd->delay_timings)
476		return -ENOMEM;
477
478	chip->of_xlate = gpiochip_fwd_delay_of_xlate;
479	chip->of_gpio_n_cells = 3;
480
481	return 0;
482}
483#else
484static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *chip,
485					 struct gpiochip_fwd *fwd)
486{
487	return 0;
488}
489#endif	/* !CONFIG_OF_GPIO */
490
491/**
492 * gpiochip_fwd_create() - Create a new GPIO forwarder
493 * @dev: Parent device pointer
494 * @ngpios: Number of GPIOs in the forwarder.
495 * @descs: Array containing the GPIO descriptors to forward to.
496 *         This array must contain @ngpios entries, and must not be deallocated
497 *         before the forwarder has been destroyed again.
498 * @features: Bitwise ORed features as defined with FWD_FEATURE_*.
499 *
500 * This function creates a new gpiochip, which forwards all GPIO operations to
501 * the passed GPIO descriptors.
502 *
503 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
504 *         code on failure.
505 */
506static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
507						unsigned int ngpios,
508						struct gpio_desc *descs[],
509						unsigned long features)
510{
511	const char *label = dev_name(dev);
512	struct gpiochip_fwd *fwd;
513	struct gpio_chip *chip;
514	unsigned int i;
515	int error;
516
517	fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
518			   GFP_KERNEL);
519	if (!fwd)
520		return ERR_PTR(-ENOMEM);
521
522	chip = &fwd->chip;
523
524	/*
525	 * If any of the GPIO lines are sleeping, then the entire forwarder
526	 * will be sleeping.
527	 * If any of the chips support .set_config(), then the forwarder will
528	 * support setting configs.
529	 */
530	for (i = 0; i < ngpios; i++) {
531		struct gpio_chip *parent = gpiod_to_chip(descs[i]);
532
533		dev_dbg(dev, "%u => gpio %d irq %d\n", i,
534			desc_to_gpio(descs[i]), gpiod_to_irq(descs[i]));
535
536		if (gpiod_cansleep(descs[i]))
537			chip->can_sleep = true;
538		if (parent && parent->set_config)
539			chip->set_config = gpio_fwd_set_config;
540	}
541
542	chip->label = label;
543	chip->parent = dev;
544	chip->owner = THIS_MODULE;
545	chip->get_direction = gpio_fwd_get_direction;
546	chip->direction_input = gpio_fwd_direction_input;
547	chip->direction_output = gpio_fwd_direction_output;
548	chip->get = gpio_fwd_get;
549	chip->get_multiple = gpio_fwd_get_multiple_locked;
550	chip->set = gpio_fwd_set;
551	chip->set_multiple = gpio_fwd_set_multiple_locked;
552	chip->to_irq = gpio_fwd_to_irq;
553	chip->base = -1;
554	chip->ngpio = ngpios;
555	fwd->descs = descs;
556
557	if (chip->can_sleep)
558		mutex_init(&fwd->mlock);
559	else
560		spin_lock_init(&fwd->slock);
561
562	if (features & FWD_FEATURE_DELAY) {
563		error = gpiochip_fwd_setup_delay_line(dev, chip, fwd);
564		if (error)
565			return ERR_PTR(error);
566	}
567
568	error = devm_gpiochip_add_data(dev, chip, fwd);
569	if (error)
570		return ERR_PTR(error);
571
572	return fwd;
573}
574
575
576/*
577 *  GPIO Aggregator platform device
578 */
579
580static int gpio_aggregator_probe(struct platform_device *pdev)
581{
582	struct device *dev = &pdev->dev;
583	struct gpio_desc **descs;
584	struct gpiochip_fwd *fwd;
585	unsigned long features;
586	int i, n;
587
588	n = gpiod_count(dev, NULL);
589	if (n < 0)
590		return n;
591
592	descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
593	if (!descs)
594		return -ENOMEM;
595
596	for (i = 0; i < n; i++) {
597		descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
598		if (IS_ERR(descs[i]))
599			return PTR_ERR(descs[i]);
600	}
601
602	features = (uintptr_t)device_get_match_data(dev);
603	fwd = gpiochip_fwd_create(dev, n, descs, features);
604	if (IS_ERR(fwd))
605		return PTR_ERR(fwd);
606
607	platform_set_drvdata(pdev, fwd);
608	return 0;
609}
610
 
611static const struct of_device_id gpio_aggregator_dt_ids[] = {
612	{
613		.compatible = "gpio-delay",
614		.data = (void *)FWD_FEATURE_DELAY,
615	},
616	/*
617	 * Add GPIO-operated devices controlled from userspace below,
618	 * or use "driver_override" in sysfs.
619	 */
620	{}
621};
622MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
 
623
624static struct platform_driver gpio_aggregator_driver = {
625	.probe = gpio_aggregator_probe,
626	.driver = {
627		.name = DRV_NAME,
628		.groups = gpio_aggregator_groups,
629		.of_match_table = gpio_aggregator_dt_ids,
630	},
631};
632
633static int __init gpio_aggregator_init(void)
634{
635	return platform_driver_register(&gpio_aggregator_driver);
636}
637module_init(gpio_aggregator_init);
638
639static void __exit gpio_aggregator_exit(void)
640{
641	gpio_aggregator_remove_all();
642	platform_driver_unregister(&gpio_aggregator_driver);
643}
644module_exit(gpio_aggregator_exit);
645
646MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
647MODULE_DESCRIPTION("GPIO Aggregator");
648MODULE_LICENSE("GPL v2");
v5.14.15
  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/ctype.h>
 13#include <linux/gpio.h>
 14#include <linux/gpio/consumer.h>
 15#include <linux/gpio/driver.h>
 16#include <linux/gpio/machine.h>
 17#include <linux/idr.h>
 18#include <linux/kernel.h>
 
 19#include <linux/module.h>
 20#include <linux/mutex.h>
 21#include <linux/overflow.h>
 22#include <linux/platform_device.h>
 
 
 23#include <linux/spinlock.h>
 24#include <linux/string.h>
 25
 
 
 
 
 
 26
 27/*
 28 * GPIO Aggregator sysfs interface
 29 */
 30
 31struct gpio_aggregator {
 32	struct gpiod_lookup_table *lookups;
 33	struct platform_device *pdev;
 34	char args[];
 35};
 36
 37static DEFINE_MUTEX(gpio_aggregator_lock);	/* protects idr */
 38static DEFINE_IDR(gpio_aggregator_idr);
 39
 40static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
 41			 int hwnum, unsigned int *n)
 42{
 43	struct gpiod_lookup_table *lookups;
 44
 45	lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
 46			   GFP_KERNEL);
 47	if (!lookups)
 48		return -ENOMEM;
 49
 50	lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
 51
 52	(*n)++;
 53	memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
 54
 55	aggr->lookups = lookups;
 56	return 0;
 57}
 58
 59static int aggr_parse(struct gpio_aggregator *aggr)
 60{
 61	char *args = skip_spaces(aggr->args);
 62	char *name, *offsets, *p;
 63	unsigned long *bitmap;
 64	unsigned int i, n = 0;
 65	int error = 0;
 66
 67	bitmap = bitmap_alloc(ARCH_NR_GPIOS, GFP_KERNEL);
 
 68	if (!bitmap)
 69		return -ENOMEM;
 70
 71	args = next_arg(args, &name, &p);
 72	while (*args) {
 73		args = next_arg(args, &offsets, &p);
 74
 75		p = get_options(offsets, 0, &error);
 76		if (error == 0 || *p) {
 77			/* Named GPIO line */
 78			error = aggr_add_gpio(aggr, name, U16_MAX, &n);
 79			if (error)
 80				goto free_bitmap;
 81
 82			name = offsets;
 83			continue;
 84		}
 85
 86		/* GPIO chip + offset(s) */
 87		error = bitmap_parselist(offsets, bitmap, ARCH_NR_GPIOS);
 88		if (error) {
 89			pr_err("Cannot parse %s: %d\n", offsets, error);
 90			goto free_bitmap;
 91		}
 92
 93		for_each_set_bit(i, bitmap, ARCH_NR_GPIOS) {
 94			error = aggr_add_gpio(aggr, name, i, &n);
 95			if (error)
 96				goto free_bitmap;
 97		}
 98
 99		args = next_arg(args, &name, &p);
100	}
101
102	if (!n) {
103		pr_err("No GPIOs specified\n");
104		error = -EINVAL;
105	}
106
107free_bitmap:
108	bitmap_free(bitmap);
109	return error;
110}
111
112static ssize_t new_device_store(struct device_driver *driver, const char *buf,
113				size_t count)
114{
115	struct gpio_aggregator *aggr;
116	struct platform_device *pdev;
117	int res, id;
118
 
 
 
119	/* kernfs guarantees string termination, so count + 1 is safe */
120	aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
121	if (!aggr)
122		return -ENOMEM;
 
 
123
124	memcpy(aggr->args, buf, count + 1);
125
126	aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
127				GFP_KERNEL);
128	if (!aggr->lookups) {
129		res = -ENOMEM;
130		goto free_ga;
131	}
132
133	mutex_lock(&gpio_aggregator_lock);
134	id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
135	mutex_unlock(&gpio_aggregator_lock);
136
137	if (id < 0) {
138		res = id;
139		goto free_table;
140	}
141
142	aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
143	if (!aggr->lookups->dev_id) {
144		res = -ENOMEM;
145		goto remove_idr;
146	}
147
148	res = aggr_parse(aggr);
149	if (res)
150		goto free_dev_id;
151
152	gpiod_add_lookup_table(aggr->lookups);
153
154	pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
155	if (IS_ERR(pdev)) {
156		res = PTR_ERR(pdev);
157		goto remove_table;
158	}
159
160	aggr->pdev = pdev;
 
161	return count;
162
163remove_table:
164	gpiod_remove_lookup_table(aggr->lookups);
165free_dev_id:
166	kfree(aggr->lookups->dev_id);
167remove_idr:
168	mutex_lock(&gpio_aggregator_lock);
169	idr_remove(&gpio_aggregator_idr, id);
170	mutex_unlock(&gpio_aggregator_lock);
171free_table:
172	kfree(aggr->lookups);
173free_ga:
174	kfree(aggr);
 
 
175	return res;
176}
177
178static DRIVER_ATTR_WO(new_device);
179
180static void gpio_aggregator_free(struct gpio_aggregator *aggr)
181{
182	platform_device_unregister(aggr->pdev);
183	gpiod_remove_lookup_table(aggr->lookups);
184	kfree(aggr->lookups->dev_id);
185	kfree(aggr->lookups);
186	kfree(aggr);
187}
188
189static ssize_t delete_device_store(struct device_driver *driver,
190				   const char *buf, size_t count)
191{
192	struct gpio_aggregator *aggr;
193	unsigned int id;
194	int error;
195
196	if (!str_has_prefix(buf, DRV_NAME "."))
197		return -EINVAL;
198
199	error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
200	if (error)
201		return error;
202
 
 
 
203	mutex_lock(&gpio_aggregator_lock);
204	aggr = idr_remove(&gpio_aggregator_idr, id);
205	mutex_unlock(&gpio_aggregator_lock);
206	if (!aggr)
 
207		return -ENOENT;
 
208
209	gpio_aggregator_free(aggr);
 
210	return count;
211}
212static DRIVER_ATTR_WO(delete_device);
213
214static struct attribute *gpio_aggregator_attrs[] = {
215	&driver_attr_new_device.attr,
216	&driver_attr_delete_device.attr,
217	NULL
218};
219ATTRIBUTE_GROUPS(gpio_aggregator);
220
221static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
222{
223	gpio_aggregator_free(p);
224	return 0;
225}
226
227static void __exit gpio_aggregator_remove_all(void)
228{
229	mutex_lock(&gpio_aggregator_lock);
230	idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
231	idr_destroy(&gpio_aggregator_idr);
232	mutex_unlock(&gpio_aggregator_lock);
233}
234
235
236/*
237 *  GPIO Forwarder
238 */
239
 
 
 
 
 
240struct gpiochip_fwd {
241	struct gpio_chip chip;
242	struct gpio_desc **descs;
243	union {
244		struct mutex mlock;	/* protects tmp[] if can_sleep */
245		spinlock_t slock;	/* protects tmp[] if !can_sleep */
246	};
 
247	unsigned long tmp[];		/* values and descs for multiple ops */
248};
249
 
 
 
 
 
250static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
251{
252	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
253
254	return gpiod_get_direction(fwd->descs[offset]);
255}
256
257static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
258{
259	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
260
261	return gpiod_direction_input(fwd->descs[offset]);
262}
263
264static int gpio_fwd_direction_output(struct gpio_chip *chip,
265				     unsigned int offset, int value)
266{
267	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
268
269	return gpiod_direction_output(fwd->descs[offset], value);
270}
271
272static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
273{
274	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
275
276	return gpiod_get_value(fwd->descs[offset]);
 
277}
278
279static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
280				 unsigned long *bits)
281{
282	struct gpio_desc **descs;
283	unsigned long *values;
284	unsigned int i, j = 0;
285	int error;
286
287	/* Both values bitmap and desc pointers are stored in tmp[] */
288	values = &fwd->tmp[0];
289	descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
290
291	bitmap_clear(values, 0, fwd->chip.ngpio);
292	for_each_set_bit(i, mask, fwd->chip.ngpio)
293		descs[j++] = fwd->descs[i];
294
295	error = gpiod_get_array_value(j, descs, NULL, values);
 
 
 
296	if (error)
297		return error;
298
299	j = 0;
300	for_each_set_bit(i, mask, fwd->chip.ngpio)
301		__assign_bit(i, bits, test_bit(j++, values));
302
303	return 0;
304}
305
306static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
307					unsigned long *mask, unsigned long *bits)
308{
309	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
310	unsigned long flags;
311	int error;
312
313	if (chip->can_sleep) {
314		mutex_lock(&fwd->mlock);
315		error = gpio_fwd_get_multiple(fwd, mask, bits);
316		mutex_unlock(&fwd->mlock);
317	} else {
318		spin_lock_irqsave(&fwd->slock, flags);
319		error = gpio_fwd_get_multiple(fwd, mask, bits);
320		spin_unlock_irqrestore(&fwd->slock, flags);
321	}
322
323	return error;
324}
325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
327{
328	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
329
330	gpiod_set_value(fwd->descs[offset], value);
 
 
 
 
 
 
331}
332
333static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
334				  unsigned long *bits)
335{
336	struct gpio_desc **descs;
337	unsigned long *values;
338	unsigned int i, j = 0;
339
340	/* Both values bitmap and desc pointers are stored in tmp[] */
341	values = &fwd->tmp[0];
342	descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
343
344	for_each_set_bit(i, mask, fwd->chip.ngpio) {
345		__assign_bit(j, values, test_bit(i, bits));
346		descs[j++] = fwd->descs[i];
347	}
348
349	gpiod_set_array_value(j, descs, NULL, values);
 
 
 
350}
351
352static void gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
353					 unsigned long *mask, unsigned long *bits)
354{
355	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
356	unsigned long flags;
357
358	if (chip->can_sleep) {
359		mutex_lock(&fwd->mlock);
360		gpio_fwd_set_multiple(fwd, mask, bits);
361		mutex_unlock(&fwd->mlock);
362	} else {
363		spin_lock_irqsave(&fwd->slock, flags);
364		gpio_fwd_set_multiple(fwd, mask, bits);
365		spin_unlock_irqrestore(&fwd->slock, flags);
366	}
367}
368
369static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
370			       unsigned long config)
371{
372	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
373
374	return gpiod_set_config(fwd->descs[offset], config);
375}
376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377/**
378 * gpiochip_fwd_create() - Create a new GPIO forwarder
379 * @dev: Parent device pointer
380 * @ngpios: Number of GPIOs in the forwarder.
381 * @descs: Array containing the GPIO descriptors to forward to.
382 *         This array must contain @ngpios entries, and must not be deallocated
383 *         before the forwarder has been destroyed again.
 
384 *
385 * This function creates a new gpiochip, which forwards all GPIO operations to
386 * the passed GPIO descriptors.
387 *
388 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
389 *         code on failure.
390 */
391static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
392						unsigned int ngpios,
393						struct gpio_desc *descs[])
 
394{
395	const char *label = dev_name(dev);
396	struct gpiochip_fwd *fwd;
397	struct gpio_chip *chip;
398	unsigned int i;
399	int error;
400
401	fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
402			   BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
403	if (!fwd)
404		return ERR_PTR(-ENOMEM);
405
406	chip = &fwd->chip;
407
408	/*
409	 * If any of the GPIO lines are sleeping, then the entire forwarder
410	 * will be sleeping.
411	 * If any of the chips support .set_config(), then the forwarder will
412	 * support setting configs.
413	 */
414	for (i = 0; i < ngpios; i++) {
415		struct gpio_chip *parent = gpiod_to_chip(descs[i]);
416
417		dev_dbg(dev, "%u => gpio-%d\n", i, desc_to_gpio(descs[i]));
 
418
419		if (gpiod_cansleep(descs[i]))
420			chip->can_sleep = true;
421		if (parent && parent->set_config)
422			chip->set_config = gpio_fwd_set_config;
423	}
424
425	chip->label = label;
426	chip->parent = dev;
427	chip->owner = THIS_MODULE;
428	chip->get_direction = gpio_fwd_get_direction;
429	chip->direction_input = gpio_fwd_direction_input;
430	chip->direction_output = gpio_fwd_direction_output;
431	chip->get = gpio_fwd_get;
432	chip->get_multiple = gpio_fwd_get_multiple_locked;
433	chip->set = gpio_fwd_set;
434	chip->set_multiple = gpio_fwd_set_multiple_locked;
 
435	chip->base = -1;
436	chip->ngpio = ngpios;
437	fwd->descs = descs;
438
439	if (chip->can_sleep)
440		mutex_init(&fwd->mlock);
441	else
442		spin_lock_init(&fwd->slock);
443
 
 
 
 
 
 
444	error = devm_gpiochip_add_data(dev, chip, fwd);
445	if (error)
446		return ERR_PTR(error);
447
448	return fwd;
449}
450
451
452/*
453 *  GPIO Aggregator platform device
454 */
455
456static int gpio_aggregator_probe(struct platform_device *pdev)
457{
458	struct device *dev = &pdev->dev;
459	struct gpio_desc **descs;
460	struct gpiochip_fwd *fwd;
 
461	int i, n;
462
463	n = gpiod_count(dev, NULL);
464	if (n < 0)
465		return n;
466
467	descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
468	if (!descs)
469		return -ENOMEM;
470
471	for (i = 0; i < n; i++) {
472		descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
473		if (IS_ERR(descs[i]))
474			return PTR_ERR(descs[i]);
475	}
476
477	fwd = gpiochip_fwd_create(dev, n, descs);
 
478	if (IS_ERR(fwd))
479		return PTR_ERR(fwd);
480
481	platform_set_drvdata(pdev, fwd);
482	return 0;
483}
484
485#ifdef CONFIG_OF
486static const struct of_device_id gpio_aggregator_dt_ids[] = {
 
 
 
 
487	/*
488	 * Add GPIO-operated devices controlled from userspace below,
489	 * or use "driver_override" in sysfs
490	 */
491	{}
492};
493MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
494#endif
495
496static struct platform_driver gpio_aggregator_driver = {
497	.probe = gpio_aggregator_probe,
498	.driver = {
499		.name = DRV_NAME,
500		.groups = gpio_aggregator_groups,
501		.of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
502	},
503};
504
505static int __init gpio_aggregator_init(void)
506{
507	return platform_driver_register(&gpio_aggregator_driver);
508}
509module_init(gpio_aggregator_init);
510
511static void __exit gpio_aggregator_exit(void)
512{
513	gpio_aggregator_remove_all();
514	platform_driver_unregister(&gpio_aggregator_driver);
515}
516module_exit(gpio_aggregator_exit);
517
518MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
519MODULE_DESCRIPTION("GPIO Aggregator");
520MODULE_LICENSE("GPL v2");