gpio-aggregator.c 15 KB

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