gpio-kempld.c 5.4 KB

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