gpio-mockup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/platform_device.h>
  17. #define GPIO_NAME "gpio-mockup"
  18. #define MAX_GC 10
  19. enum direction {
  20. OUT,
  21. IN
  22. };
  23. /*
  24. * struct gpio_pin_status - structure describing a GPIO status
  25. * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
  26. * @value: Configures status of the gpio as 0(low) or 1(high)
  27. */
  28. struct gpio_pin_status {
  29. enum direction dir;
  30. bool value;
  31. };
  32. struct mockup_gpio_controller {
  33. struct gpio_chip gc;
  34. struct gpio_pin_status *stats;
  35. };
  36. static int gpio_mockup_ranges[MAX_GC << 1];
  37. static int gpio_mockup_params_nr;
  38. module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
  39. const char pins_name_start = 'A';
  40. static int mockup_gpio_get(struct gpio_chip *gc, unsigned int offset)
  41. {
  42. struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
  43. return cntr->stats[offset].value;
  44. }
  45. static void mockup_gpio_set(struct gpio_chip *gc, unsigned int offset,
  46. int value)
  47. {
  48. struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
  49. cntr->stats[offset].value = !!value;
  50. }
  51. static int mockup_gpio_dirout(struct gpio_chip *gc, unsigned int offset,
  52. int value)
  53. {
  54. struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
  55. mockup_gpio_set(gc, offset, value);
  56. cntr->stats[offset].dir = OUT;
  57. return 0;
  58. }
  59. static int mockup_gpio_dirin(struct gpio_chip *gc, unsigned int offset)
  60. {
  61. struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
  62. cntr->stats[offset].dir = IN;
  63. return 0;
  64. }
  65. static int mockup_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  66. {
  67. struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
  68. return cntr->stats[offset].dir;
  69. }
  70. static int mockup_gpio_add(struct device *dev,
  71. struct mockup_gpio_controller *cntr,
  72. const char *name, int base, int ngpio)
  73. {
  74. int ret;
  75. cntr->gc.base = base;
  76. cntr->gc.ngpio = ngpio;
  77. cntr->gc.label = name;
  78. cntr->gc.owner = THIS_MODULE;
  79. cntr->gc.parent = dev;
  80. cntr->gc.get = mockup_gpio_get;
  81. cntr->gc.set = mockup_gpio_set;
  82. cntr->gc.direction_output = mockup_gpio_dirout;
  83. cntr->gc.direction_input = mockup_gpio_dirin;
  84. cntr->gc.get_direction = mockup_gpio_get_direction;
  85. cntr->stats = devm_kzalloc(dev, sizeof(*cntr->stats) * cntr->gc.ngpio,
  86. GFP_KERNEL);
  87. if (!cntr->stats) {
  88. ret = -ENOMEM;
  89. goto err;
  90. }
  91. ret = devm_gpiochip_add_data(dev, &cntr->gc, cntr);
  92. if (ret)
  93. goto err;
  94. dev_info(dev, "gpio<%d..%d> add successful!", base, base + ngpio);
  95. return 0;
  96. err:
  97. dev_err(dev, "gpio<%d..%d> add failed!", base, base + ngpio);
  98. return ret;
  99. }
  100. static int mockup_gpio_probe(struct platform_device *pdev)
  101. {
  102. struct device *dev = &pdev->dev;
  103. struct mockup_gpio_controller *cntr;
  104. int ret;
  105. int i;
  106. int base;
  107. int ngpio;
  108. char *chip_name;
  109. if (gpio_mockup_params_nr < 2)
  110. return -EINVAL;
  111. cntr = devm_kzalloc(dev, sizeof(*cntr) * (gpio_mockup_params_nr >> 1),
  112. GFP_KERNEL);
  113. if (!cntr)
  114. return -ENOMEM;
  115. platform_set_drvdata(pdev, cntr);
  116. for (i = 0; i < gpio_mockup_params_nr >> 1; i++) {
  117. base = gpio_mockup_ranges[i * 2];
  118. if (base == -1)
  119. ngpio = gpio_mockup_ranges[i * 2 + 1];
  120. else
  121. ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
  122. if (ngpio >= 0) {
  123. chip_name = devm_kasprintf(dev, GFP_KERNEL,
  124. "%s-%c", GPIO_NAME,
  125. pins_name_start + i);
  126. if (!chip_name)
  127. return -ENOMEM;
  128. ret = mockup_gpio_add(dev, &cntr[i],
  129. chip_name, base, ngpio);
  130. } else {
  131. ret = -1;
  132. }
  133. if (ret) {
  134. if (base < 0)
  135. dev_err(dev, "gpio<%d..%d> add failed\n",
  136. base, ngpio);
  137. else
  138. dev_err(dev, "gpio<%d..%d> add failed\n",
  139. base, base + ngpio);
  140. return ret;
  141. }
  142. }
  143. return 0;
  144. }
  145. static struct platform_driver mockup_gpio_driver = {
  146. .driver = {
  147. .name = GPIO_NAME,
  148. },
  149. .probe = mockup_gpio_probe,
  150. };
  151. static struct platform_device *pdev;
  152. static int __init mock_device_init(void)
  153. {
  154. int err;
  155. pdev = platform_device_alloc(GPIO_NAME, -1);
  156. if (!pdev)
  157. return -ENOMEM;
  158. err = platform_device_add(pdev);
  159. if (err) {
  160. platform_device_put(pdev);
  161. return err;
  162. }
  163. err = platform_driver_register(&mockup_gpio_driver);
  164. if (err) {
  165. platform_device_unregister(pdev);
  166. return err;
  167. }
  168. return 0;
  169. }
  170. static void __exit mock_device_exit(void)
  171. {
  172. platform_driver_unregister(&mockup_gpio_driver);
  173. platform_device_unregister(pdev);
  174. }
  175. module_init(mock_device_init);
  176. module_exit(mock_device_exit);
  177. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  178. MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
  179. MODULE_DESCRIPTION("GPIO Testing driver");
  180. MODULE_LICENSE("GPL v2");