gpio-mockup.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * GPIO Testing Device Driver
  4. *
  5. * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com>
  6. * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com>
  7. * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/cleanup.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/irq_sim.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/property.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/string_helpers.h>
  25. #include <linux/uaccess.h>
  26. #define GPIO_MOCKUP_MAX_GC 10
  27. /*
  28. * We're storing two values per chip: the GPIO base and the number
  29. * of GPIO lines.
  30. */
  31. #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
  32. /* Maximum of four properties + the sentinel. */
  33. #define GPIO_MOCKUP_MAX_PROP 5
  34. /*
  35. * struct gpio_pin_status - structure describing a GPIO status
  36. * @dir: Configures direction of gpio as "in" or "out"
  37. * @value: Configures status of the gpio as 0(low) or 1(high)
  38. * @pull: Configures the current pull of the GPIO as 0 (pull-down) or
  39. * 1 (pull-up)
  40. * @requested: Request status of this GPIO
  41. */
  42. struct gpio_mockup_line_status {
  43. int dir;
  44. int value;
  45. int pull;
  46. bool requested;
  47. };
  48. struct gpio_mockup_chip {
  49. struct gpio_chip gc;
  50. struct gpio_mockup_line_status *lines;
  51. struct irq_domain *irq_sim_domain;
  52. struct dentry *dbg_dir;
  53. struct mutex lock;
  54. };
  55. struct gpio_mockup_dbgfs_private {
  56. struct gpio_mockup_chip *chip;
  57. unsigned int offset;
  58. };
  59. static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
  60. static int gpio_mockup_num_ranges;
  61. module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
  62. static bool gpio_mockup_named_lines;
  63. module_param_named(gpio_mockup_named_lines,
  64. gpio_mockup_named_lines, bool, 0400);
  65. static struct dentry *gpio_mockup_dbg_dir;
  66. static int gpio_mockup_range_base(unsigned int index)
  67. {
  68. return gpio_mockup_ranges[index * 2];
  69. }
  70. static int gpio_mockup_range_ngpio(unsigned int index)
  71. {
  72. return gpio_mockup_ranges[index * 2 + 1];
  73. }
  74. static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
  75. unsigned int offset)
  76. {
  77. return chip->lines[offset].value;
  78. }
  79. static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
  80. {
  81. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  82. int val;
  83. scoped_guard(mutex, &chip->lock)
  84. val = __gpio_mockup_get(chip, offset);
  85. return val;
  86. }
  87. static int gpio_mockup_get_multiple(struct gpio_chip *gc,
  88. unsigned long *mask, unsigned long *bits)
  89. {
  90. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  91. unsigned int bit, val;
  92. scoped_guard(mutex, &chip->lock) {
  93. for_each_set_bit(bit, mask, gc->ngpio) {
  94. val = __gpio_mockup_get(chip, bit);
  95. __assign_bit(bit, bits, val);
  96. }
  97. }
  98. return 0;
  99. }
  100. static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
  101. unsigned int offset, int value)
  102. {
  103. chip->lines[offset].value = !!value;
  104. }
  105. static void gpio_mockup_set(struct gpio_chip *gc,
  106. unsigned int offset, int value)
  107. {
  108. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  109. guard(mutex)(&chip->lock);
  110. __gpio_mockup_set(chip, offset, value);
  111. }
  112. static void gpio_mockup_set_multiple(struct gpio_chip *gc,
  113. unsigned long *mask, unsigned long *bits)
  114. {
  115. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  116. unsigned int bit;
  117. guard(mutex)(&chip->lock);
  118. for_each_set_bit(bit, mask, gc->ngpio)
  119. __gpio_mockup_set(chip, bit, test_bit(bit, bits));
  120. }
  121. static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
  122. unsigned int offset, int value)
  123. {
  124. struct gpio_mockup_line_status *line = &chip->lines[offset];
  125. int curr, irq, irq_type, ret = 0;
  126. guard(mutex)(&chip->lock);
  127. if (line->requested && line->dir == GPIO_LINE_DIRECTION_IN) {
  128. curr = __gpio_mockup_get(chip, offset);
  129. if (curr == value)
  130. goto out;
  131. irq = irq_find_mapping(chip->irq_sim_domain, offset);
  132. if (!irq)
  133. /*
  134. * This is fine - it just means, nobody is listening
  135. * for interrupts on this line, otherwise
  136. * irq_create_mapping() would have been called from
  137. * the to_irq() callback.
  138. */
  139. goto set_value;
  140. irq_type = irq_get_trigger_type(irq);
  141. if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
  142. (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
  143. ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
  144. true);
  145. if (ret)
  146. goto out;
  147. }
  148. }
  149. set_value:
  150. /* Change the value unless we're actively driving the line. */
  151. if (!line->requested || line->dir == GPIO_LINE_DIRECTION_IN)
  152. __gpio_mockup_set(chip, offset, value);
  153. out:
  154. chip->lines[offset].pull = value;
  155. return ret;
  156. }
  157. static int gpio_mockup_set_config(struct gpio_chip *gc,
  158. unsigned int offset, unsigned long config)
  159. {
  160. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  161. switch (pinconf_to_config_param(config)) {
  162. case PIN_CONFIG_BIAS_PULL_UP:
  163. return gpio_mockup_apply_pull(chip, offset, 1);
  164. case PIN_CONFIG_BIAS_PULL_DOWN:
  165. return gpio_mockup_apply_pull(chip, offset, 0);
  166. default:
  167. break;
  168. }
  169. return -ENOTSUPP;
  170. }
  171. static int gpio_mockup_dirout(struct gpio_chip *gc,
  172. unsigned int offset, int value)
  173. {
  174. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  175. scoped_guard(mutex, &chip->lock) {
  176. chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
  177. __gpio_mockup_set(chip, offset, value);
  178. }
  179. return 0;
  180. }
  181. static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
  182. {
  183. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  184. scoped_guard(mutex, &chip->lock)
  185. chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
  186. return 0;
  187. }
  188. static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
  189. {
  190. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  191. int direction;
  192. scoped_guard(mutex, &chip->lock)
  193. direction = chip->lines[offset].dir;
  194. return direction;
  195. }
  196. static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
  197. {
  198. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  199. return irq_create_mapping(chip->irq_sim_domain, offset);
  200. }
  201. static int gpio_mockup_request(struct gpio_chip *gc, unsigned int offset)
  202. {
  203. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  204. scoped_guard(mutex, &chip->lock)
  205. chip->lines[offset].requested = true;
  206. return 0;
  207. }
  208. static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
  209. {
  210. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  211. guard(mutex)(&chip->lock);
  212. chip->lines[offset].requested = false;
  213. __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
  214. }
  215. static ssize_t gpio_mockup_debugfs_read(struct file *file,
  216. char __user *usr_buf,
  217. size_t size, loff_t *ppos)
  218. {
  219. struct gpio_mockup_dbgfs_private *priv;
  220. struct gpio_mockup_chip *chip;
  221. struct seq_file *sfile;
  222. struct gpio_chip *gc;
  223. int val, cnt;
  224. char buf[3];
  225. if (*ppos != 0)
  226. return 0;
  227. sfile = file->private_data;
  228. priv = sfile->private;
  229. chip = priv->chip;
  230. gc = &chip->gc;
  231. val = gpio_mockup_get(gc, priv->offset);
  232. cnt = snprintf(buf, sizeof(buf), "%d\n", val);
  233. return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
  234. }
  235. static ssize_t gpio_mockup_debugfs_write(struct file *file,
  236. const char __user *usr_buf,
  237. size_t size, loff_t *ppos)
  238. {
  239. struct gpio_mockup_dbgfs_private *priv;
  240. int rv, val;
  241. struct seq_file *sfile;
  242. if (*ppos != 0)
  243. return -EINVAL;
  244. rv = kstrtoint_from_user(usr_buf, size, 0, &val);
  245. if (rv)
  246. return rv;
  247. if (val != 0 && val != 1)
  248. return -EINVAL;
  249. sfile = file->private_data;
  250. priv = sfile->private;
  251. rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
  252. if (rv)
  253. return rv;
  254. return size;
  255. }
  256. static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
  257. {
  258. return single_open(file, NULL, inode->i_private);
  259. }
  260. /*
  261. * Each mockup chip is represented by a directory named after the chip's device
  262. * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
  263. * a file using the line's offset as the name under the chip's directory.
  264. *
  265. * Reading from the line's file yields the current *value*, writing to the
  266. * line's file changes the current *pull*. Default pull for mockup lines is
  267. * down.
  268. *
  269. * Examples:
  270. * - when a line pulled down is requested in output mode and driven high, its
  271. * value will return to 0 once it's released
  272. * - when the line is requested in output mode and driven high, writing 0 to
  273. * the corresponding debugfs file will change the pull to down but the
  274. * reported value will still be 1 until the line is released
  275. * - line requested in input mode always reports the same value as its pull
  276. * configuration
  277. * - when the line is requested in input mode and monitored for events, writing
  278. * the same value to the debugfs file will be a noop, while writing the
  279. * opposite value will generate a dummy interrupt with an appropriate edge
  280. */
  281. static const struct file_operations gpio_mockup_debugfs_ops = {
  282. .owner = THIS_MODULE,
  283. .open = gpio_mockup_debugfs_open,
  284. .read = gpio_mockup_debugfs_read,
  285. .write = gpio_mockup_debugfs_write,
  286. .release = single_release,
  287. };
  288. static void gpio_mockup_debugfs_setup(struct device *dev,
  289. struct gpio_mockup_chip *chip)
  290. {
  291. struct gpio_mockup_dbgfs_private *priv;
  292. struct gpio_chip *gc;
  293. const char *devname;
  294. char *name;
  295. int i;
  296. gc = &chip->gc;
  297. /*
  298. * There can only be a single GPIO device per platform device in
  299. * gpio-mockup so using device_find_any_child() is OK.
  300. */
  301. struct device *child __free(put_device) = device_find_any_child(dev);
  302. if (!child)
  303. return;
  304. devname = dev_name(child);
  305. chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
  306. for (i = 0; i < gc->ngpio; i++) {
  307. name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
  308. if (!name)
  309. return;
  310. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  311. if (!priv)
  312. return;
  313. priv->chip = chip;
  314. priv->offset = i;
  315. debugfs_create_file(name, 0600, chip->dbg_dir, priv,
  316. &gpio_mockup_debugfs_ops);
  317. }
  318. }
  319. static void gpio_mockup_debugfs_cleanup(void *data)
  320. {
  321. struct gpio_mockup_chip *chip = data;
  322. debugfs_remove_recursive(chip->dbg_dir);
  323. }
  324. static void gpio_mockup_dispose_mappings(void *data)
  325. {
  326. struct gpio_mockup_chip *chip = data;
  327. struct gpio_chip *gc = &chip->gc;
  328. int i, irq;
  329. for (i = 0; i < gc->ngpio; i++) {
  330. irq = irq_find_mapping(chip->irq_sim_domain, i);
  331. if (irq)
  332. irq_dispose_mapping(irq);
  333. }
  334. }
  335. static int gpio_mockup_probe(struct platform_device *pdev)
  336. {
  337. struct gpio_mockup_chip *chip;
  338. struct gpio_chip *gc;
  339. struct device *dev;
  340. const char *name;
  341. int rv, base, i;
  342. u16 ngpio;
  343. dev = &pdev->dev;
  344. rv = device_property_read_u32(dev, "gpio-base", &base);
  345. if (rv)
  346. base = -1;
  347. rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
  348. if (rv)
  349. return rv;
  350. rv = device_property_read_string(dev, "chip-label", &name);
  351. if (rv)
  352. name = dev_name(dev);
  353. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  354. if (!chip)
  355. return -ENOMEM;
  356. mutex_init(&chip->lock);
  357. gc = &chip->gc;
  358. gc->base = base;
  359. gc->ngpio = ngpio;
  360. gc->label = name;
  361. gc->owner = THIS_MODULE;
  362. gc->parent = dev;
  363. gc->get = gpio_mockup_get;
  364. gc->set = gpio_mockup_set;
  365. gc->get_multiple = gpio_mockup_get_multiple;
  366. gc->set_multiple = gpio_mockup_set_multiple;
  367. gc->direction_output = gpio_mockup_dirout;
  368. gc->direction_input = gpio_mockup_dirin;
  369. gc->get_direction = gpio_mockup_get_direction;
  370. gc->set_config = gpio_mockup_set_config;
  371. gc->to_irq = gpio_mockup_to_irq;
  372. gc->request = gpio_mockup_request;
  373. gc->free = gpio_mockup_free;
  374. chip->lines = devm_kcalloc(dev, gc->ngpio,
  375. sizeof(*chip->lines), GFP_KERNEL);
  376. if (!chip->lines)
  377. return -ENOMEM;
  378. for (i = 0; i < gc->ngpio; i++)
  379. chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
  380. chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
  381. gc->ngpio);
  382. if (IS_ERR(chip->irq_sim_domain))
  383. return PTR_ERR(chip->irq_sim_domain);
  384. rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
  385. if (rv)
  386. return rv;
  387. rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
  388. if (rv)
  389. return rv;
  390. gpio_mockup_debugfs_setup(dev, chip);
  391. return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip);
  392. }
  393. static const struct of_device_id gpio_mockup_of_match[] = {
  394. { .compatible = "gpio-mockup", },
  395. {},
  396. };
  397. MODULE_DEVICE_TABLE(of, gpio_mockup_of_match);
  398. static struct platform_driver gpio_mockup_driver = {
  399. .driver = {
  400. .name = "gpio-mockup",
  401. .of_match_table = gpio_mockup_of_match,
  402. },
  403. .probe = gpio_mockup_probe,
  404. };
  405. static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
  406. static void gpio_mockup_unregister_pdevs(void)
  407. {
  408. struct platform_device *pdev;
  409. struct fwnode_handle *fwnode;
  410. int i;
  411. for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
  412. pdev = gpio_mockup_pdevs[i];
  413. if (!pdev)
  414. continue;
  415. fwnode = dev_fwnode(&pdev->dev);
  416. platform_device_unregister(pdev);
  417. fwnode_remove_software_node(fwnode);
  418. }
  419. }
  420. static int __init gpio_mockup_register_chip(int idx)
  421. {
  422. struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
  423. struct platform_device_info pdevinfo;
  424. struct platform_device *pdev;
  425. struct fwnode_handle *fwnode;
  426. char **line_names = NULL;
  427. char chip_label[32];
  428. int prop = 0, base;
  429. u16 ngpio;
  430. memset(properties, 0, sizeof(properties));
  431. memset(&pdevinfo, 0, sizeof(pdevinfo));
  432. snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
  433. properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
  434. base = gpio_mockup_range_base(idx);
  435. if (base >= 0)
  436. properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
  437. ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
  438. : gpio_mockup_range_ngpio(idx) - base;
  439. properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
  440. if (gpio_mockup_named_lines) {
  441. line_names = kasprintf_strarray(GFP_KERNEL, chip_label, ngpio);
  442. if (!line_names)
  443. return -ENOMEM;
  444. properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
  445. "gpio-line-names", line_names, ngpio);
  446. }
  447. fwnode = fwnode_create_software_node(properties, NULL);
  448. if (IS_ERR(fwnode)) {
  449. kfree_strarray(line_names, ngpio);
  450. return PTR_ERR(fwnode);
  451. }
  452. pdevinfo.name = "gpio-mockup";
  453. pdevinfo.id = idx;
  454. pdevinfo.fwnode = fwnode;
  455. pdev = platform_device_register_full(&pdevinfo);
  456. kfree_strarray(line_names, ngpio);
  457. if (IS_ERR(pdev)) {
  458. fwnode_remove_software_node(fwnode);
  459. pr_err("error registering device");
  460. return PTR_ERR(pdev);
  461. }
  462. gpio_mockup_pdevs[idx] = pdev;
  463. return 0;
  464. }
  465. static int __init gpio_mockup_init(void)
  466. {
  467. int i, num_chips, err;
  468. if ((gpio_mockup_num_ranges % 2) ||
  469. (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
  470. return -EINVAL;
  471. /* Each chip is described by two values. */
  472. num_chips = gpio_mockup_num_ranges / 2;
  473. /*
  474. * The second value in the <base GPIO - number of GPIOS> pair must
  475. * always be greater than 0.
  476. */
  477. for (i = 0; i < num_chips; i++) {
  478. if (gpio_mockup_range_ngpio(i) < 0)
  479. return -EINVAL;
  480. }
  481. gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
  482. err = platform_driver_register(&gpio_mockup_driver);
  483. if (err) {
  484. pr_err("error registering platform driver\n");
  485. debugfs_remove_recursive(gpio_mockup_dbg_dir);
  486. return err;
  487. }
  488. for (i = 0; i < num_chips; i++) {
  489. err = gpio_mockup_register_chip(i);
  490. if (err) {
  491. platform_driver_unregister(&gpio_mockup_driver);
  492. gpio_mockup_unregister_pdevs();
  493. debugfs_remove_recursive(gpio_mockup_dbg_dir);
  494. return err;
  495. }
  496. }
  497. return 0;
  498. }
  499. static void __exit gpio_mockup_exit(void)
  500. {
  501. gpio_mockup_unregister_pdevs();
  502. debugfs_remove_recursive(gpio_mockup_dbg_dir);
  503. platform_driver_unregister(&gpio_mockup_driver);
  504. }
  505. module_init(gpio_mockup_init);
  506. module_exit(gpio_mockup_exit);
  507. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  508. MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
  509. MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
  510. MODULE_DESCRIPTION("GPIO Testing driver");
  511. MODULE_LICENSE("GPL v2");