gpio-kempld.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Kontron PLD GPIO driver
  3. *
  4. * Copyright (c) 2010-2013 Kontron Europe GmbH
  5. * Author: Michael Brunner <michael.brunner@kontron.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/bitops.h>
  20. #include <linux/errno.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/gpio.h>
  23. #include <linux/mfd/kempld.h>
  24. #define KEMPLD_GPIO_MAX_NUM 16
  25. #define KEMPLD_GPIO_MASK(x) (1 << ((x) % 8))
  26. #define KEMPLD_GPIO_DIR_NUM(x) (0x40 + (x) / 8)
  27. #define KEMPLD_GPIO_LVL_NUM(x) (0x42 + (x) / 8)
  28. #define KEMPLD_GPIO_EVT_LVL_EDGE 0x46
  29. #define KEMPLD_GPIO_IEN 0x4A
  30. struct kempld_gpio_data {
  31. struct gpio_chip chip;
  32. struct kempld_device_data *pld;
  33. };
  34. /*
  35. * Set or clear GPIO bit
  36. * kempld_get_mutex must be called prior to calling this function.
  37. */
  38. static void kempld_gpio_bitop(struct kempld_device_data *pld,
  39. u8 reg, u8 bit, u8 val)
  40. {
  41. u8 status;
  42. status = kempld_read8(pld, reg);
  43. if (val)
  44. status |= (1 << bit);
  45. else
  46. status &= ~(1 << bit);
  47. kempld_write8(pld, reg, status);
  48. }
  49. static int kempld_gpio_get_bit(struct kempld_device_data *pld, u8 reg, u8 bit)
  50. {
  51. u8 status;
  52. kempld_get_mutex(pld);
  53. status = kempld_read8(pld, reg);
  54. kempld_release_mutex(pld);
  55. return !!(status & (1 << bit));
  56. }
  57. static int kempld_gpio_get(struct gpio_chip *chip, unsigned offset)
  58. {
  59. struct kempld_gpio_data *gpio
  60. = container_of(chip, struct kempld_gpio_data, chip);
  61. struct kempld_device_data *pld = gpio->pld;
  62. return kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL_NUM(offset),
  63. KEMPLD_GPIO_MASK(offset));
  64. }
  65. static void kempld_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  66. {
  67. struct kempld_gpio_data *gpio
  68. = container_of(chip, struct kempld_gpio_data, chip);
  69. struct kempld_device_data *pld = gpio->pld;
  70. kempld_get_mutex(pld);
  71. kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset),
  72. KEMPLD_GPIO_MASK(offset), value);
  73. kempld_release_mutex(pld);
  74. }
  75. static int kempld_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  76. {
  77. struct kempld_gpio_data *gpio
  78. = container_of(chip, struct kempld_gpio_data, chip);
  79. struct kempld_device_data *pld = gpio->pld;
  80. kempld_get_mutex(pld);
  81. kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset),
  82. KEMPLD_GPIO_MASK(offset), 0);
  83. kempld_release_mutex(pld);
  84. return 0;
  85. }
  86. static int kempld_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  87. int value)
  88. {
  89. struct kempld_gpio_data *gpio
  90. = container_of(chip, struct kempld_gpio_data, chip);
  91. struct kempld_device_data *pld = gpio->pld;
  92. kempld_get_mutex(pld);
  93. kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset),
  94. KEMPLD_GPIO_MASK(offset), value);
  95. kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset),
  96. KEMPLD_GPIO_MASK(offset), 1);
  97. kempld_release_mutex(pld);
  98. return 0;
  99. }
  100. static int kempld_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  101. {
  102. struct kempld_gpio_data *gpio
  103. = container_of(chip, struct kempld_gpio_data, chip);
  104. struct kempld_device_data *pld = gpio->pld;
  105. return kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset),
  106. KEMPLD_GPIO_MASK(offset));
  107. }
  108. static int kempld_gpio_pincount(struct kempld_device_data *pld)
  109. {
  110. u16 evt, evt_back;
  111. kempld_get_mutex(pld);
  112. /* Backup event register as it might be already initialized */
  113. evt_back = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE);
  114. /* Clear event register */
  115. kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, 0x0000);
  116. /* Read back event register */
  117. evt = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE);
  118. /* Restore event register */
  119. kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, evt_back);
  120. kempld_release_mutex(pld);
  121. return evt ? __ffs(evt) : 16;
  122. }
  123. static int kempld_gpio_probe(struct platform_device *pdev)
  124. {
  125. struct device *dev = &pdev->dev;
  126. struct kempld_device_data *pld = dev_get_drvdata(dev->parent);
  127. struct kempld_platform_data *pdata = dev_get_platdata(pld->dev);
  128. struct kempld_gpio_data *gpio;
  129. struct gpio_chip *chip;
  130. int ret;
  131. if (pld->info.spec_major < 2) {
  132. dev_err(dev,
  133. "Driver only supports GPIO devices compatible to PLD spec. rev. 2.0 or higher\n");
  134. return -ENODEV;
  135. }
  136. gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
  137. if (gpio == NULL)
  138. return -ENOMEM;
  139. gpio->pld = pld;
  140. platform_set_drvdata(pdev, gpio);
  141. chip = &gpio->chip;
  142. chip->label = "gpio-kempld";
  143. chip->owner = THIS_MODULE;
  144. chip->dev = dev;
  145. chip->can_sleep = 1;
  146. if (pdata && pdata->gpio_base)
  147. chip->base = pdata->gpio_base;
  148. else
  149. chip->base = -1;
  150. chip->direction_input = kempld_gpio_direction_input;
  151. chip->direction_output = kempld_gpio_direction_output;
  152. chip->get_direction = kempld_gpio_get_direction;
  153. chip->get = kempld_gpio_get;
  154. chip->set = kempld_gpio_set;
  155. chip->ngpio = kempld_gpio_pincount(pld);
  156. if (chip->ngpio == 0) {
  157. dev_err(dev, "No GPIO pins detected\n");
  158. return -ENODEV;
  159. }
  160. ret = gpiochip_add(chip);
  161. if (ret) {
  162. dev_err(dev, "Could not register GPIO chip\n");
  163. return ret;
  164. }
  165. dev_info(dev, "GPIO functionality initialized with %d pins\n",
  166. chip->ngpio);
  167. return 0;
  168. }
  169. static int kempld_gpio_remove(struct platform_device *pdev)
  170. {
  171. struct kempld_gpio_data *gpio = platform_get_drvdata(pdev);
  172. return gpiochip_remove(&gpio->chip);
  173. }
  174. static struct platform_driver kempld_gpio_driver = {
  175. .driver = {
  176. .name = "gpio-kempld",
  177. .owner = THIS_MODULE,
  178. },
  179. .probe = kempld_gpio_probe,
  180. .remove = kempld_gpio_remove,
  181. };
  182. module_platform_driver(kempld_gpio_driver);
  183. MODULE_DESCRIPTION("KEM PLD GPIO Driver");
  184. MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
  185. MODULE_LICENSE("GPL");
  186. MODULE_ALIAS("platform:gpio-kempld");