gpio-mockup.c 15 KB

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