gpio-aggregator.c 13 KB

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