gpio-mockup.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/irq_work.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/uaccess.h>
  24. #include "gpiolib.h"
  25. #define GPIO_MOCKUP_NAME "gpio-mockup"
  26. #define GPIO_MOCKUP_MAX_GC 10
  27. enum {
  28. DIR_IN = 0,
  29. DIR_OUT,
  30. };
  31. /*
  32. * struct gpio_pin_status - structure describing a GPIO status
  33. * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=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. bool value;
  39. };
  40. struct gpio_mockup_irq_context {
  41. struct irq_work work;
  42. int irq;
  43. };
  44. struct gpio_mockup_chip {
  45. struct gpio_chip gc;
  46. struct gpio_mockup_line_status *lines;
  47. struct gpio_mockup_irq_context irq_ctx;
  48. struct dentry *dbg_dir;
  49. };
  50. struct gpio_mockup_dbgfs_private {
  51. struct gpio_mockup_chip *chip;
  52. struct gpio_desc *desc;
  53. int offset;
  54. };
  55. static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];
  56. static int gpio_mockup_params_nr;
  57. module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
  58. static bool gpio_mockup_named_lines;
  59. module_param_named(gpio_mockup_named_lines,
  60. gpio_mockup_named_lines, bool, 0400);
  61. static const char gpio_mockup_name_start = 'A';
  62. static struct dentry *gpio_mockup_dbg_dir;
  63. static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
  64. {
  65. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  66. return chip->lines[offset].value;
  67. }
  68. static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset,
  69. int value)
  70. {
  71. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  72. chip->lines[offset].value = !!value;
  73. }
  74. static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset,
  75. int value)
  76. {
  77. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  78. gpio_mockup_set(gc, offset, value);
  79. chip->lines[offset].dir = DIR_OUT;
  80. return 0;
  81. }
  82. static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
  83. {
  84. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  85. chip->lines[offset].dir = DIR_IN;
  86. return 0;
  87. }
  88. static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
  89. {
  90. struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
  91. return chip->lines[offset].dir;
  92. }
  93. static int gpio_mockup_name_lines(struct device *dev,
  94. struct gpio_mockup_chip *chip)
  95. {
  96. struct gpio_chip *gc = &chip->gc;
  97. char **names;
  98. int i;
  99. names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, GFP_KERNEL);
  100. if (!names)
  101. return -ENOMEM;
  102. for (i = 0; i < gc->ngpio; i++) {
  103. names[i] = devm_kasprintf(dev, GFP_KERNEL,
  104. "%s-%d", gc->label, i);
  105. if (!names[i])
  106. return -ENOMEM;
  107. }
  108. gc->names = (const char *const *)names;
  109. return 0;
  110. }
  111. static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset)
  112. {
  113. return chip->irq_base + offset;
  114. }
  115. /*
  116. * While we should generally support irqmask and irqunmask, this driver is
  117. * for testing purposes only so we don't care.
  118. */
  119. static void gpio_mockup_irqmask(struct irq_data *d) { }
  120. static void gpio_mockup_irqunmask(struct irq_data *d) { }
  121. static struct irq_chip gpio_mockup_irqchip = {
  122. .name = GPIO_MOCKUP_NAME,
  123. .irq_mask = gpio_mockup_irqmask,
  124. .irq_unmask = gpio_mockup_irqunmask,
  125. };
  126. static void gpio_mockup_handle_irq(struct irq_work *work)
  127. {
  128. struct gpio_mockup_irq_context *irq_ctx;
  129. irq_ctx = container_of(work, struct gpio_mockup_irq_context, work);
  130. handle_simple_irq(irq_to_desc(irq_ctx->irq));
  131. }
  132. static int gpio_mockup_irqchip_setup(struct device *dev,
  133. struct gpio_mockup_chip *chip)
  134. {
  135. struct gpio_chip *gc = &chip->gc;
  136. int irq_base, i;
  137. irq_base = devm_irq_alloc_descs(dev, -1, 0, gc->ngpio, 0);
  138. if (irq_base < 0)
  139. return irq_base;
  140. gc->irq_base = irq_base;
  141. gc->irqchip = &gpio_mockup_irqchip;
  142. for (i = 0; i < gc->ngpio; i++) {
  143. irq_set_chip(irq_base + i, gc->irqchip);
  144. irq_set_handler(irq_base + i, &handle_simple_irq);
  145. irq_modify_status(irq_base + i,
  146. IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
  147. }
  148. init_irq_work(&chip->irq_ctx.work, gpio_mockup_handle_irq);
  149. return 0;
  150. }
  151. static ssize_t gpio_mockup_event_write(struct file *file,
  152. const char __user *usr_buf,
  153. size_t size, loff_t *ppos)
  154. {
  155. struct gpio_mockup_dbgfs_private *priv;
  156. struct gpio_mockup_chip *chip;
  157. struct seq_file *sfile;
  158. struct gpio_desc *desc;
  159. struct gpio_chip *gc;
  160. int val;
  161. char buf;
  162. sfile = file->private_data;
  163. priv = sfile->private;
  164. desc = priv->desc;
  165. chip = priv->chip;
  166. gc = &chip->gc;
  167. if (copy_from_user(&buf, usr_buf, 1))
  168. return -EFAULT;
  169. if (buf == '0')
  170. val = 0;
  171. else if (buf == '1')
  172. val = 1;
  173. else
  174. return -EINVAL;
  175. gpiod_set_value_cansleep(desc, val);
  176. priv->chip->irq_ctx.irq = gc->irq_base + priv->offset;
  177. irq_work_queue(&priv->chip->irq_ctx.work);
  178. return size;
  179. }
  180. static int gpio_mockup_event_open(struct inode *inode, struct file *file)
  181. {
  182. return single_open(file, NULL, inode->i_private);
  183. }
  184. static const struct file_operations gpio_mockup_event_ops = {
  185. .owner = THIS_MODULE,
  186. .open = gpio_mockup_event_open,
  187. .write = gpio_mockup_event_write,
  188. .llseek = no_llseek,
  189. };
  190. static void gpio_mockup_debugfs_setup(struct device *dev,
  191. struct gpio_mockup_chip *chip)
  192. {
  193. struct gpio_mockup_dbgfs_private *priv;
  194. struct dentry *evfile;
  195. struct gpio_chip *gc;
  196. char *name;
  197. int i;
  198. gc = &chip->gc;
  199. chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir);
  200. if (!chip->dbg_dir)
  201. goto err;
  202. for (i = 0; i < gc->ngpio; i++) {
  203. name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
  204. if (!name)
  205. goto err;
  206. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  207. if (!priv)
  208. goto err;
  209. priv->chip = chip;
  210. priv->offset = i;
  211. priv->desc = &gc->gpiodev->descs[i];
  212. evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
  213. &gpio_mockup_event_ops);
  214. if (!evfile)
  215. goto err;
  216. }
  217. return;
  218. err:
  219. dev_err(dev, "error creating debugfs directory\n");
  220. }
  221. static int gpio_mockup_add(struct device *dev,
  222. struct gpio_mockup_chip *chip,
  223. const char *name, int base, int ngpio)
  224. {
  225. struct gpio_chip *gc = &chip->gc;
  226. int ret;
  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->direction_output = gpio_mockup_dirout;
  235. gc->direction_input = gpio_mockup_dirin;
  236. gc->get_direction = gpio_mockup_get_direction;
  237. gc->to_irq = gpio_mockup_to_irq;
  238. chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio,
  239. GFP_KERNEL);
  240. if (!chip->lines)
  241. return -ENOMEM;
  242. if (gpio_mockup_named_lines) {
  243. ret = gpio_mockup_name_lines(dev, chip);
  244. if (ret)
  245. return ret;
  246. }
  247. ret = gpio_mockup_irqchip_setup(dev, chip);
  248. if (ret)
  249. return ret;
  250. ret = devm_gpiochip_add_data(dev, &chip->gc, chip);
  251. if (ret)
  252. return ret;
  253. if (gpio_mockup_dbg_dir)
  254. gpio_mockup_debugfs_setup(dev, chip);
  255. return 0;
  256. }
  257. static int gpio_mockup_probe(struct platform_device *pdev)
  258. {
  259. struct gpio_mockup_chip *chips;
  260. struct device *dev = &pdev->dev;
  261. int ret, i, base, ngpio;
  262. char *chip_name;
  263. if (gpio_mockup_params_nr < 2)
  264. return -EINVAL;
  265. chips = devm_kzalloc(dev,
  266. sizeof(*chips) * (gpio_mockup_params_nr >> 1),
  267. GFP_KERNEL);
  268. if (!chips)
  269. return -ENOMEM;
  270. platform_set_drvdata(pdev, chips);
  271. for (i = 0; i < gpio_mockup_params_nr >> 1; i++) {
  272. base = gpio_mockup_ranges[i * 2];
  273. if (base == -1)
  274. ngpio = gpio_mockup_ranges[i * 2 + 1];
  275. else
  276. ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
  277. if (ngpio >= 0) {
  278. chip_name = devm_kasprintf(dev, GFP_KERNEL,
  279. "%s-%c", GPIO_MOCKUP_NAME,
  280. gpio_mockup_name_start + i);
  281. if (!chip_name)
  282. return -ENOMEM;
  283. ret = gpio_mockup_add(dev, &chips[i],
  284. chip_name, base, ngpio);
  285. } else {
  286. ret = -1;
  287. }
  288. if (ret) {
  289. dev_err(dev, "gpio<%d..%d> add failed\n",
  290. base, base < 0 ? ngpio : base + ngpio);
  291. return ret;
  292. }
  293. dev_info(dev, "gpio<%d..%d> add successful!",
  294. base, base + ngpio);
  295. }
  296. return 0;
  297. }
  298. static struct platform_driver gpio_mockup_driver = {
  299. .driver = {
  300. .name = GPIO_MOCKUP_NAME,
  301. },
  302. .probe = gpio_mockup_probe,
  303. };
  304. static struct platform_device *pdev;
  305. static int __init mock_device_init(void)
  306. {
  307. int err;
  308. gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
  309. if (!gpio_mockup_dbg_dir)
  310. pr_err("%s: error creating debugfs directory\n",
  311. GPIO_MOCKUP_NAME);
  312. pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1);
  313. if (!pdev)
  314. return -ENOMEM;
  315. err = platform_device_add(pdev);
  316. if (err) {
  317. platform_device_put(pdev);
  318. return err;
  319. }
  320. err = platform_driver_register(&gpio_mockup_driver);
  321. if (err) {
  322. platform_device_unregister(pdev);
  323. return err;
  324. }
  325. return 0;
  326. }
  327. static void __exit mock_device_exit(void)
  328. {
  329. debugfs_remove_recursive(gpio_mockup_dbg_dir);
  330. platform_driver_unregister(&gpio_mockup_driver);
  331. platform_device_unregister(pdev);
  332. }
  333. module_init(mock_device_init);
  334. module_exit(mock_device_exit);
  335. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  336. MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
  337. MODULE_DESCRIPTION("GPIO Testing driver");
  338. MODULE_LICENSE("GPL v2");