gpio-mockup.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * GPIO Testing Device Driver
  3. *
  4. * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com>
  5. * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>
  6. * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/irq_sim.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/uaccess.h>
  25. #include "gpiolib.h"
  26. #define GPIO_MOCKUP_NAME "gpio-mockup"
  27. #define GPIO_MOCKUP_MAX_GC 10
  28. /*
  29. * We're storing two values per chip: the GPIO base and the number
  30. * of GPIO lines.
  31. */
  32. #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
  33. #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
  34. enum {
  35. GPIO_MOCKUP_DIR_OUT = 0,
  36. GPIO_MOCKUP_DIR_IN = 1,
  37. };
  38. /*
  39. * struct gpio_pin_status - structure describing a GPIO status
  40. * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
  41. * @value: Configures status of the gpio as 0(low) or 1(high)
  42. */
  43. struct gpio_mockup_line_status {
  44. int dir;
  45. int value;
  46. };
  47. struct gpio_mockup_chip {
  48. struct gpio_chip gc;
  49. struct gpio_mockup_line_status *lines;
  50. struct irq_sim irqsim;
  51. struct dentry *dbg_dir;
  52. };
  53. struct gpio_mockup_dbgfs_private {
  54. struct gpio_mockup_chip *chip;
  55. struct gpio_desc *desc;
  56. int offset;
  57. };
  58. struct gpio_mockup_platform_data {
  59. int base;
  60. int ngpio;
  61. int index;
  62. bool named_lines;
  63. };
  64. static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
  65. static int gpio_mockup_num_ranges;
  66. module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
  67. static bool gpio_mockup_named_lines;
  68. module_param_named(gpio_mockup_named_lines,
  69. gpio_mockup_named_lines, bool, 0400);
  70. static struct dentry *gpio_mockup_dbg_dir;
  71. static int gpio_mockup_range_base(unsigned int index)
  72. {
  73. return gpio_mockup_ranges[index * 2];
  74. }
  75. static int gpio_mockup_range_ngpio(unsigned int index)
  76. {
  77. return gpio_mockup_ranges[index * 2 + 1];
  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. return chip->lines[offset].value;
  83. }
  84. static void gpio_mockup_set(struct gpio_chip *gc,
  85. unsigned int offset, int value)
  86. {
  87. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  88. chip->lines[offset].value = !!value;
  89. }
  90. static void gpio_mockup_set_multiple(struct gpio_chip *gc,
  91. unsigned long *mask, unsigned long *bits)
  92. {
  93. unsigned int bit;
  94. for_each_set_bit(bit, mask, gc->ngpio)
  95. gpio_mockup_set(gc, bit, test_bit(bit, bits));
  96. }
  97. static int gpio_mockup_dirout(struct gpio_chip *gc,
  98. unsigned int offset, int value)
  99. {
  100. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  101. gpio_mockup_set(gc, offset, value);
  102. chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
  103. return 0;
  104. }
  105. static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
  106. {
  107. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  108. chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
  109. return 0;
  110. }
  111. static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
  112. {
  113. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  114. return chip->lines[offset].dir;
  115. }
  116. static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
  117. {
  118. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  119. return irq_sim_irqnum(&chip->irqsim, offset);
  120. }
  121. static ssize_t gpio_mockup_event_write(struct file *file,
  122. const char __user *usr_buf,
  123. size_t size, loff_t *ppos)
  124. {
  125. struct gpio_mockup_dbgfs_private *priv;
  126. struct gpio_mockup_chip *chip;
  127. struct seq_file *sfile;
  128. struct gpio_desc *desc;
  129. int rv, val;
  130. rv = kstrtoint_from_user(usr_buf, size, 0, &val);
  131. if (rv)
  132. return rv;
  133. if (val != 0 && val != 1)
  134. return -EINVAL;
  135. sfile = file->private_data;
  136. priv = sfile->private;
  137. desc = priv->desc;
  138. chip = priv->chip;
  139. gpiod_set_value_cansleep(desc, val);
  140. irq_sim_fire(&chip->irqsim, priv->offset);
  141. return size;
  142. }
  143. static int gpio_mockup_event_open(struct inode *inode, struct file *file)
  144. {
  145. return single_open(file, NULL, inode->i_private);
  146. }
  147. static const struct file_operations gpio_mockup_event_ops = {
  148. .owner = THIS_MODULE,
  149. .open = gpio_mockup_event_open,
  150. .write = gpio_mockup_event_write,
  151. .llseek = no_llseek,
  152. };
  153. static void gpio_mockup_debugfs_setup(struct device *dev,
  154. struct gpio_mockup_chip *chip)
  155. {
  156. struct gpio_mockup_dbgfs_private *priv;
  157. struct dentry *evfile, *link;
  158. struct gpio_chip *gc;
  159. const char *devname;
  160. char *name;
  161. int i;
  162. gc = &chip->gc;
  163. devname = dev_name(&gc->gpiodev->dev);
  164. chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
  165. if (IS_ERR_OR_NULL(chip->dbg_dir))
  166. goto err;
  167. link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname);
  168. if (IS_ERR_OR_NULL(link))
  169. goto err;
  170. for (i = 0; i < gc->ngpio; i++) {
  171. name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
  172. if (!name)
  173. goto err;
  174. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  175. if (!priv)
  176. goto err;
  177. priv->chip = chip;
  178. priv->offset = i;
  179. priv->desc = &gc->gpiodev->descs[i];
  180. evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
  181. &gpio_mockup_event_ops);
  182. if (IS_ERR_OR_NULL(evfile))
  183. goto err;
  184. }
  185. return;
  186. err:
  187. dev_err(dev, "error creating debugfs event files\n");
  188. }
  189. static int gpio_mockup_name_lines(struct device *dev,
  190. struct gpio_mockup_chip *chip)
  191. {
  192. struct gpio_chip *gc = &chip->gc;
  193. char **names;
  194. int i;
  195. names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
  196. if (!names)
  197. return -ENOMEM;
  198. for (i = 0; i < gc->ngpio; i++) {
  199. names[i] = devm_kasprintf(dev, GFP_KERNEL,
  200. "%s-%d", gc->label, i);
  201. if (!names[i])
  202. return -ENOMEM;
  203. }
  204. gc->names = (const char *const *)names;
  205. return 0;
  206. }
  207. static int gpio_mockup_probe(struct platform_device *pdev)
  208. {
  209. struct gpio_mockup_platform_data *pdata;
  210. struct gpio_mockup_chip *chip;
  211. struct gpio_chip *gc;
  212. int rv, base, ngpio;
  213. struct device *dev;
  214. char *name;
  215. dev = &pdev->dev;
  216. pdata = dev_get_platdata(dev);
  217. base = pdata->base;
  218. ngpio = pdata->ngpio;
  219. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  220. if (!chip)
  221. return -ENOMEM;
  222. name = devm_kasprintf(dev, GFP_KERNEL, "%s-%c",
  223. pdev->name, pdata->index);
  224. if (!name)
  225. return -ENOMEM;
  226. gc = &chip->gc;
  227. gc->base = base;
  228. gc->ngpio = ngpio;
  229. gc->label = name;
  230. gc->owner = THIS_MODULE;
  231. gc->parent = dev;
  232. gc->get = gpio_mockup_get;
  233. gc->set = gpio_mockup_set;
  234. gc->set_multiple = gpio_mockup_set_multiple;
  235. gc->direction_output = gpio_mockup_dirout;
  236. gc->direction_input = gpio_mockup_dirin;
  237. gc->get_direction = gpio_mockup_get_direction;
  238. gc->to_irq = gpio_mockup_to_irq;
  239. chip->lines = devm_kcalloc(dev, gc->ngpio,
  240. sizeof(*chip->lines), GFP_KERNEL);
  241. if (!chip->lines)
  242. return -ENOMEM;
  243. if (pdata->named_lines) {
  244. rv = gpio_mockup_name_lines(dev, chip);
  245. if (rv)
  246. return rv;
  247. }
  248. rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
  249. if (rv < 0)
  250. return rv;
  251. rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
  252. if (rv)
  253. return rv;
  254. if (gpio_mockup_dbg_dir)
  255. gpio_mockup_debugfs_setup(dev, chip);
  256. return 0;
  257. }
  258. static struct platform_driver gpio_mockup_driver = {
  259. .driver = {
  260. .name = GPIO_MOCKUP_NAME,
  261. },
  262. .probe = gpio_mockup_probe,
  263. };
  264. static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
  265. static void gpio_mockup_unregister_pdevs(void)
  266. {
  267. struct platform_device *pdev;
  268. int i;
  269. for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
  270. pdev = gpio_mockup_pdevs[i];
  271. if (pdev)
  272. platform_device_unregister(pdev);
  273. }
  274. }
  275. static int __init gpio_mockup_init(void)
  276. {
  277. int i, num_chips, err = 0, index = 'A';
  278. struct gpio_mockup_platform_data pdata;
  279. struct platform_device *pdev;
  280. if ((gpio_mockup_num_ranges < 2) ||
  281. (gpio_mockup_num_ranges % 2) ||
  282. (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
  283. return -EINVAL;
  284. /* Each chip is described by two values. */
  285. num_chips = gpio_mockup_num_ranges / 2;
  286. /*
  287. * The second value in the <base GPIO - number of GPIOS> pair must
  288. * always be greater than 0.
  289. */
  290. for (i = 0; i < num_chips; i++) {
  291. if (gpio_mockup_range_ngpio(i) < 0)
  292. return -EINVAL;
  293. }
  294. gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
  295. if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
  296. gpio_mockup_err("error creating debugfs directory\n");
  297. err = platform_driver_register(&gpio_mockup_driver);
  298. if (err) {
  299. gpio_mockup_err("error registering platform driver\n");
  300. return err;
  301. }
  302. for (i = 0; i < num_chips; i++) {
  303. pdata.index = index++;
  304. pdata.base = gpio_mockup_range_base(i);
  305. pdata.ngpio = pdata.base < 0
  306. ? gpio_mockup_range_ngpio(i)
  307. : gpio_mockup_range_ngpio(i) - pdata.base;
  308. pdata.named_lines = gpio_mockup_named_lines;
  309. pdev = platform_device_register_resndata(NULL,
  310. GPIO_MOCKUP_NAME,
  311. i, NULL, 0, &pdata,
  312. sizeof(pdata));
  313. if (IS_ERR(pdev)) {
  314. gpio_mockup_err("error registering device");
  315. platform_driver_unregister(&gpio_mockup_driver);
  316. gpio_mockup_unregister_pdevs();
  317. return PTR_ERR(pdev);
  318. }
  319. gpio_mockup_pdevs[i] = pdev;
  320. }
  321. return 0;
  322. }
  323. static void __exit gpio_mockup_exit(void)
  324. {
  325. debugfs_remove_recursive(gpio_mockup_dbg_dir);
  326. platform_driver_unregister(&gpio_mockup_driver);
  327. gpio_mockup_unregister_pdevs();
  328. }
  329. module_init(gpio_mockup_init);
  330. module_exit(gpio_mockup_exit);
  331. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  332. MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
  333. MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
  334. MODULE_DESCRIPTION("GPIO Testing driver");
  335. MODULE_LICENSE("GPL v2");