gpio-mockup.c 15 KB

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