gpio-aggregator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // GPIO Aggregator
  4. //
  5. // Copyright (C) 2019-2020 Glider bv
  6. #define DRV_NAME "gpio-aggregator"
  7. #define pr_fmt(fmt) DRV_NAME ": " fmt
  8. #include <linux/bitmap.h>
  9. #include <linux/bitops.h>
  10. #include <linux/ctype.h>
  11. #include <linux/gpio.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/gpio/machine.h>
  15. #include <linux/idr.h>
  16. #include <linux/kernel.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/spinlock.h>
  22. #include <linux/string.h>
  23. /*
  24. * GPIO Aggregator sysfs interface
  25. */
  26. struct gpio_aggregator {
  27. struct gpiod_lookup_table *lookups;
  28. struct platform_device *pdev;
  29. char args[];
  30. };
  31. static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */
  32. static DEFINE_IDR(gpio_aggregator_idr);
  33. static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
  34. int hwnum, unsigned int *n)
  35. {
  36. struct gpiod_lookup_table *lookups;
  37. lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
  38. GFP_KERNEL);
  39. if (!lookups)
  40. return -ENOMEM;
  41. lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
  42. (*n)++;
  43. memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
  44. aggr->lookups = lookups;
  45. return 0;
  46. }
  47. static int aggr_parse(struct gpio_aggregator *aggr)
  48. {
  49. char *args = skip_spaces(aggr->args);
  50. char *name, *offsets, *p;
  51. unsigned long *bitmap;
  52. unsigned int i, n = 0;
  53. int error = 0;
  54. bitmap = bitmap_alloc(ARCH_NR_GPIOS, GFP_KERNEL);
  55. if (!bitmap)
  56. return -ENOMEM;
  57. args = next_arg(args, &name, &p);
  58. while (*args) {
  59. args = next_arg(args, &offsets, &p);
  60. p = get_options(offsets, 0, &error);
  61. if (error == 0 || *p) {
  62. /* Named GPIO line */
  63. error = aggr_add_gpio(aggr, name, U16_MAX, &n);
  64. if (error)
  65. goto free_bitmap;
  66. name = offsets;
  67. continue;
  68. }
  69. /* GPIO chip + offset(s) */
  70. error = bitmap_parselist(offsets, bitmap, ARCH_NR_GPIOS);
  71. if (error) {
  72. pr_err("Cannot parse %s: %d\n", offsets, error);
  73. goto free_bitmap;
  74. }
  75. for_each_set_bit(i, bitmap, ARCH_NR_GPIOS) {
  76. error = aggr_add_gpio(aggr, name, i, &n);
  77. if (error)
  78. goto free_bitmap;
  79. }
  80. args = next_arg(args, &name, &p);
  81. }
  82. if (!n) {
  83. pr_err("No GPIOs specified\n");
  84. error = -EINVAL;
  85. }
  86. free_bitmap:
  87. bitmap_free(bitmap);
  88. return error;
  89. }
  90. static ssize_t new_device_store(struct device_driver *driver, const char *buf,
  91. size_t count)
  92. {
  93. struct gpio_aggregator *aggr;
  94. struct platform_device *pdev;
  95. int res, id;
  96. /* kernfs guarantees string termination, so count + 1 is safe */
  97. aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
  98. if (!aggr)
  99. return -ENOMEM;
  100. memcpy(aggr->args, buf, count + 1);
  101. aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
  102. GFP_KERNEL);
  103. if (!aggr->lookups) {
  104. res = -ENOMEM;
  105. goto free_ga;
  106. }
  107. mutex_lock(&gpio_aggregator_lock);
  108. id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
  109. mutex_unlock(&gpio_aggregator_lock);
  110. if (id < 0) {
  111. res = id;
  112. goto free_table;
  113. }
  114. aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
  115. if (!aggr->lookups->dev_id) {
  116. res = -ENOMEM;
  117. goto remove_idr;
  118. }
  119. res = aggr_parse(aggr);
  120. if (res)
  121. goto free_dev_id;
  122. gpiod_add_lookup_table(aggr->lookups);
  123. pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
  124. if (IS_ERR(pdev)) {
  125. res = PTR_ERR(pdev);
  126. goto remove_table;
  127. }
  128. aggr->pdev = pdev;
  129. return count;
  130. remove_table:
  131. gpiod_remove_lookup_table(aggr->lookups);
  132. free_dev_id:
  133. kfree(aggr->lookups->dev_id);
  134. remove_idr:
  135. mutex_lock(&gpio_aggregator_lock);
  136. idr_remove(&gpio_aggregator_idr, id);
  137. mutex_unlock(&gpio_aggregator_lock);
  138. free_table:
  139. kfree(aggr->lookups);
  140. free_ga:
  141. kfree(aggr);
  142. return res;
  143. }
  144. static DRIVER_ATTR_WO(new_device);
  145. static void gpio_aggregator_free(struct gpio_aggregator *aggr)
  146. {
  147. platform_device_unregister(aggr->pdev);
  148. gpiod_remove_lookup_table(aggr->lookups);
  149. kfree(aggr->lookups->dev_id);
  150. kfree(aggr->lookups);
  151. kfree(aggr);
  152. }
  153. static ssize_t delete_device_store(struct device_driver *driver,
  154. const char *buf, size_t count)
  155. {
  156. struct gpio_aggregator *aggr;
  157. unsigned int id;
  158. int error;
  159. if (!str_has_prefix(buf, DRV_NAME "."))
  160. return -EINVAL;
  161. error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
  162. if (error)
  163. return error;
  164. mutex_lock(&gpio_aggregator_lock);
  165. aggr = idr_remove(&gpio_aggregator_idr, id);
  166. mutex_unlock(&gpio_aggregator_lock);
  167. if (!aggr)
  168. return -ENOENT;
  169. gpio_aggregator_free(aggr);
  170. return count;
  171. }
  172. static DRIVER_ATTR_WO(delete_device);
  173. static struct attribute *gpio_aggregator_attrs[] = {
  174. &driver_attr_new_device.attr,
  175. &driver_attr_delete_device.attr,
  176. NULL
  177. };
  178. ATTRIBUTE_GROUPS(gpio_aggregator);
  179. static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
  180. {
  181. gpio_aggregator_free(p);
  182. return 0;
  183. }
  184. static void __exit gpio_aggregator_remove_all(void)
  185. {
  186. mutex_lock(&gpio_aggregator_lock);
  187. idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
  188. idr_destroy(&gpio_aggregator_idr);
  189. mutex_unlock(&gpio_aggregator_lock);
  190. }
  191. /*
  192. * GPIO Forwarder
  193. */
  194. struct gpiochip_fwd {
  195. struct gpio_chip chip;
  196. struct gpio_desc **descs;
  197. union {
  198. struct mutex mlock; /* protects tmp[] if can_sleep */
  199. spinlock_t slock; /* protects tmp[] if !can_sleep */
  200. };
  201. unsigned long tmp[]; /* values and descs for multiple ops */
  202. };
  203. #define fwd_tmp_values(fwd) &(fwd)->tmp[0]
  204. #define fwd_tmp_descs(fwd) (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
  205. #define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
  206. static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
  207. {
  208. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  209. return gpiod_get_direction(fwd->descs[offset]);
  210. }
  211. static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
  212. {
  213. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  214. return gpiod_direction_input(fwd->descs[offset]);
  215. }
  216. static int gpio_fwd_direction_output(struct gpio_chip *chip,
  217. unsigned int offset, int value)
  218. {
  219. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  220. return gpiod_direction_output(fwd->descs[offset], value);
  221. }
  222. static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
  223. {
  224. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  225. return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset])
  226. : gpiod_get_value(fwd->descs[offset]);
  227. }
  228. static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
  229. unsigned long *bits)
  230. {
  231. struct gpio_desc **descs = fwd_tmp_descs(fwd);
  232. unsigned long *values = fwd_tmp_values(fwd);
  233. unsigned int i, j = 0;
  234. int error;
  235. bitmap_clear(values, 0, fwd->chip.ngpio);
  236. for_each_set_bit(i, mask, fwd->chip.ngpio)
  237. descs[j++] = fwd->descs[i];
  238. if (fwd->chip.can_sleep)
  239. error = gpiod_get_array_value_cansleep(j, descs, NULL, values);
  240. else
  241. error = gpiod_get_array_value(j, descs, NULL, values);
  242. if (error)
  243. return error;
  244. j = 0;
  245. for_each_set_bit(i, mask, fwd->chip.ngpio)
  246. __assign_bit(i, bits, test_bit(j++, values));
  247. return 0;
  248. }
  249. static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
  250. unsigned long *mask, unsigned long *bits)
  251. {
  252. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  253. unsigned long flags;
  254. int error;
  255. if (chip->can_sleep) {
  256. mutex_lock(&fwd->mlock);
  257. error = gpio_fwd_get_multiple(fwd, mask, bits);
  258. mutex_unlock(&fwd->mlock);
  259. } else {
  260. spin_lock_irqsave(&fwd->slock, flags);
  261. error = gpio_fwd_get_multiple(fwd, mask, bits);
  262. spin_unlock_irqrestore(&fwd->slock, flags);
  263. }
  264. return error;
  265. }
  266. static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
  267. {
  268. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  269. if (chip->can_sleep)
  270. gpiod_set_value_cansleep(fwd->descs[offset], value);
  271. else
  272. gpiod_set_value(fwd->descs[offset], value);
  273. }
  274. static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
  275. unsigned long *bits)
  276. {
  277. struct gpio_desc **descs = fwd_tmp_descs(fwd);
  278. unsigned long *values = fwd_tmp_values(fwd);
  279. unsigned int i, j = 0;
  280. for_each_set_bit(i, mask, fwd->chip.ngpio) {
  281. __assign_bit(j, values, test_bit(i, bits));
  282. descs[j++] = fwd->descs[i];
  283. }
  284. if (fwd->chip.can_sleep)
  285. gpiod_set_array_value_cansleep(j, descs, NULL, values);
  286. else
  287. gpiod_set_array_value(j, descs, NULL, values);
  288. }
  289. static void gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
  290. unsigned long *mask, unsigned long *bits)
  291. {
  292. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  293. unsigned long flags;
  294. if (chip->can_sleep) {
  295. mutex_lock(&fwd->mlock);
  296. gpio_fwd_set_multiple(fwd, mask, bits);
  297. mutex_unlock(&fwd->mlock);
  298. } else {
  299. spin_lock_irqsave(&fwd->slock, flags);
  300. gpio_fwd_set_multiple(fwd, mask, bits);
  301. spin_unlock_irqrestore(&fwd->slock, flags);
  302. }
  303. }
  304. static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
  305. unsigned long config)
  306. {
  307. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  308. return gpiod_set_config(fwd->descs[offset], config);
  309. }
  310. static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
  311. {
  312. struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
  313. return gpiod_to_irq(fwd->descs[offset]);
  314. }
  315. /**
  316. * gpiochip_fwd_create() - Create a new GPIO forwarder
  317. * @dev: Parent device pointer
  318. * @ngpios: Number of GPIOs in the forwarder.
  319. * @descs: Array containing the GPIO descriptors to forward to.
  320. * This array must contain @ngpios entries, and must not be deallocated
  321. * before the forwarder has been destroyed again.
  322. *
  323. * This function creates a new gpiochip, which forwards all GPIO operations to
  324. * the passed GPIO descriptors.
  325. *
  326. * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
  327. * code on failure.
  328. */
  329. static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
  330. unsigned int ngpios,
  331. struct gpio_desc *descs[])
  332. {
  333. const char *label = dev_name(dev);
  334. struct gpiochip_fwd *fwd;
  335. struct gpio_chip *chip;
  336. unsigned int i;
  337. int error;
  338. fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
  339. GFP_KERNEL);
  340. if (!fwd)
  341. return ERR_PTR(-ENOMEM);
  342. chip = &fwd->chip;
  343. /*
  344. * If any of the GPIO lines are sleeping, then the entire forwarder
  345. * will be sleeping.
  346. * If any of the chips support .set_config(), then the forwarder will
  347. * support setting configs.
  348. */
  349. for (i = 0; i < ngpios; i++) {
  350. struct gpio_chip *parent = gpiod_to_chip(descs[i]);
  351. dev_dbg(dev, "%u => gpio %d irq %d\n", i,
  352. desc_to_gpio(descs[i]), gpiod_to_irq(descs[i]));
  353. if (gpiod_cansleep(descs[i]))
  354. chip->can_sleep = true;
  355. if (parent && parent->set_config)
  356. chip->set_config = gpio_fwd_set_config;
  357. }
  358. chip->label = label;
  359. chip->parent = dev;
  360. chip->owner = THIS_MODULE;
  361. chip->get_direction = gpio_fwd_get_direction;
  362. chip->direction_input = gpio_fwd_direction_input;
  363. chip->direction_output = gpio_fwd_direction_output;
  364. chip->get = gpio_fwd_get;
  365. chip->get_multiple = gpio_fwd_get_multiple_locked;
  366. chip->set = gpio_fwd_set;
  367. chip->set_multiple = gpio_fwd_set_multiple_locked;
  368. chip->to_irq = gpio_fwd_to_irq;
  369. chip->base = -1;
  370. chip->ngpio = ngpios;
  371. fwd->descs = descs;
  372. if (chip->can_sleep)
  373. mutex_init(&fwd->mlock);
  374. else
  375. spin_lock_init(&fwd->slock);
  376. error = devm_gpiochip_add_data(dev, chip, fwd);
  377. if (error)
  378. return ERR_PTR(error);
  379. return fwd;
  380. }
  381. /*
  382. * GPIO Aggregator platform device
  383. */
  384. static int gpio_aggregator_probe(struct platform_device *pdev)
  385. {
  386. struct device *dev = &pdev->dev;
  387. struct gpio_desc **descs;
  388. struct gpiochip_fwd *fwd;
  389. int i, n;
  390. n = gpiod_count(dev, NULL);
  391. if (n < 0)
  392. return n;
  393. descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
  394. if (!descs)
  395. return -ENOMEM;
  396. for (i = 0; i < n; i++) {
  397. descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
  398. if (IS_ERR(descs[i]))
  399. return PTR_ERR(descs[i]);
  400. }
  401. fwd = gpiochip_fwd_create(dev, n, descs);
  402. if (IS_ERR(fwd))
  403. return PTR_ERR(fwd);
  404. platform_set_drvdata(pdev, fwd);
  405. return 0;
  406. }
  407. #ifdef CONFIG_OF
  408. static const struct of_device_id gpio_aggregator_dt_ids[] = {
  409. /*
  410. * Add GPIO-operated devices controlled from userspace below,
  411. * or use "driver_override" in sysfs
  412. */
  413. {}
  414. };
  415. MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
  416. #endif
  417. static struct platform_driver gpio_aggregator_driver = {
  418. .probe = gpio_aggregator_probe,
  419. .driver = {
  420. .name = DRV_NAME,
  421. .groups = gpio_aggregator_groups,
  422. .of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
  423. },
  424. };
  425. static int __init gpio_aggregator_init(void)
  426. {
  427. return platform_driver_register(&gpio_aggregator_driver);
  428. }
  429. module_init(gpio_aggregator_init);
  430. static void __exit gpio_aggregator_exit(void)
  431. {
  432. gpio_aggregator_remove_all();
  433. platform_driver_unregister(&gpio_aggregator_driver);
  434. }
  435. module_exit(gpio_aggregator_exit);
  436. MODULE_AUTHOR("Geert Uytterhoeven <[email protected]>");
  437. MODULE_DESCRIPTION("GPIO Aggregator");
  438. MODULE_LICENSE("GPL v2");