gpio-kempld.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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) (BIT((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 |= KEMPLD_GPIO_MASK(bit);
  45. else
  46. status &= ~KEMPLD_GPIO_MASK(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 & KEMPLD_GPIO_MASK(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), offset);
  63. }
  64. static void kempld_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  65. {
  66. struct kempld_gpio_data *gpio
  67. = container_of(chip, struct kempld_gpio_data, chip);
  68. struct kempld_device_data *pld = gpio->pld;
  69. kempld_get_mutex(pld);
  70. kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
  71. kempld_release_mutex(pld);
  72. }
  73. static int kempld_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  74. {
  75. struct kempld_gpio_data *gpio
  76. = container_of(chip, struct kempld_gpio_data, chip);
  77. struct kempld_device_data *pld = gpio->pld;
  78. kempld_get_mutex(pld);
  79. kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 0);
  80. kempld_release_mutex(pld);
  81. return 0;
  82. }
  83. static int kempld_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  84. int value)
  85. {
  86. struct kempld_gpio_data *gpio
  87. = container_of(chip, struct kempld_gpio_data, chip);
  88. struct kempld_device_data *pld = gpio->pld;
  89. kempld_get_mutex(pld);
  90. kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
  91. kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 1);
  92. kempld_release_mutex(pld);
  93. return 0;
  94. }
  95. static int kempld_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  96. {
  97. struct kempld_gpio_data *gpio
  98. = container_of(chip, struct kempld_gpio_data, chip);
  99. struct kempld_device_data *pld = gpio->pld;
  100. return !kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset), offset);
  101. }
  102. static int kempld_gpio_pincount(struct kempld_device_data *pld)
  103. {
  104. u16 evt, evt_back;
  105. kempld_get_mutex(pld);
  106. /* Backup event register as it might be already initialized */
  107. evt_back = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE);
  108. /* Clear event register */
  109. kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, 0x0000);
  110. /* Read back event register */
  111. evt = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE);
  112. /* Restore event register */
  113. kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, evt_back);
  114. kempld_release_mutex(pld);
  115. return evt ? __ffs(evt) : 16;
  116. }
  117. static int kempld_gpio_probe(struct platform_device *pdev)
  118. {
  119. struct device *dev = &pdev->dev;
  120. struct kempld_device_data *pld = dev_get_drvdata(dev->parent);
  121. struct kempld_platform_data *pdata = dev_get_platdata(pld->dev);
  122. struct kempld_gpio_data *gpio;
  123. struct gpio_chip *chip;
  124. int ret;
  125. if (pld->info.spec_major < 2) {
  126. dev_err(dev,
  127. "Driver only supports GPIO devices compatible to PLD spec. rev. 2.0 or higher\n");
  128. return -ENODEV;
  129. }
  130. gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
  131. if (!gpio)
  132. return -ENOMEM;
  133. gpio->pld = pld;
  134. platform_set_drvdata(pdev, gpio);
  135. chip = &gpio->chip;
  136. chip->label = "gpio-kempld";
  137. chip->owner = THIS_MODULE;
  138. chip->dev = dev;
  139. chip->can_sleep = true;
  140. if (pdata && pdata->gpio_base)
  141. chip->base = pdata->gpio_base;
  142. else
  143. chip->base = -1;
  144. chip->direction_input = kempld_gpio_direction_input;
  145. chip->direction_output = kempld_gpio_direction_output;
  146. chip->get_direction = kempld_gpio_get_direction;
  147. chip->get = kempld_gpio_get;
  148. chip->set = kempld_gpio_set;
  149. chip->ngpio = kempld_gpio_pincount(pld);
  150. if (chip->ngpio == 0) {
  151. dev_err(dev, "No GPIO pins detected\n");
  152. return -ENODEV;
  153. }
  154. ret = gpiochip_add(chip);
  155. if (ret) {
  156. dev_err(dev, "Could not register GPIO chip\n");
  157. return ret;
  158. }
  159. dev_info(dev, "GPIO functionality initialized with %d pins\n",
  160. chip->ngpio);
  161. return 0;
  162. }
  163. static int kempld_gpio_remove(struct platform_device *pdev)
  164. {
  165. struct kempld_gpio_data *gpio = platform_get_drvdata(pdev);
  166. gpiochip_remove(&gpio->chip);
  167. return 0;
  168. }
  169. static struct platform_driver kempld_gpio_driver = {
  170. .driver = {
  171. .name = "kempld-gpio",
  172. },
  173. .probe = kempld_gpio_probe,
  174. .remove = kempld_gpio_remove,
  175. };
  176. module_platform_driver(kempld_gpio_driver);
  177. MODULE_DESCRIPTION("KEM PLD GPIO Driver");
  178. MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
  179. MODULE_LICENSE("GPL");
  180. MODULE_ALIAS("platform:kempld-gpio");