gpio-menz127.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * MEN 16Z127 GPIO driver
  3. *
  4. * Copyright (C) 2016 MEN Mikroelektronik GmbH (www.men.de)
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; version 2 of the License.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/io.h>
  13. #include <linux/err.h>
  14. #include <linux/mcb.h>
  15. #include <linux/bitops.h>
  16. #include <linux/gpio/driver.h>
  17. #define MEN_Z127_CTRL 0x00
  18. #define MEN_Z127_PSR 0x04
  19. #define MEN_Z127_IRQR 0x08
  20. #define MEN_Z127_GPIODR 0x0c
  21. #define MEN_Z127_IER1 0x10
  22. #define MEN_Z127_IER2 0x14
  23. #define MEN_Z127_DBER 0x18
  24. #define MEN_Z127_ODER 0x1C
  25. #define GPIO_TO_DBCNT_REG(gpio) ((gpio * 4) + 0x80)
  26. #define MEN_Z127_DB_MIN_US 50
  27. /* 16 bit compare register. Each bit represents 50us */
  28. #define MEN_Z127_DB_MAX_US (0xffff * MEN_Z127_DB_MIN_US)
  29. #define MEN_Z127_DB_IN_RANGE(db) ((db >= MEN_Z127_DB_MIN_US) && \
  30. (db <= MEN_Z127_DB_MAX_US))
  31. struct men_z127_gpio {
  32. struct gpio_chip gc;
  33. void __iomem *reg_base;
  34. struct resource *mem;
  35. };
  36. static int men_z127_debounce(struct gpio_chip *gc, unsigned gpio,
  37. unsigned debounce)
  38. {
  39. struct men_z127_gpio *priv = gpiochip_get_data(gc);
  40. struct device *dev = gc->parent;
  41. unsigned int rnd;
  42. u32 db_en, db_cnt;
  43. if (!MEN_Z127_DB_IN_RANGE(debounce)) {
  44. dev_err(dev, "debounce value %u out of range", debounce);
  45. return -EINVAL;
  46. }
  47. if (debounce > 0) {
  48. /* round up or down depending on MSB-1 */
  49. rnd = fls(debounce) - 1;
  50. if (rnd && (debounce & BIT(rnd - 1)))
  51. debounce = roundup(debounce, MEN_Z127_DB_MIN_US);
  52. else
  53. debounce = rounddown(debounce, MEN_Z127_DB_MIN_US);
  54. if (debounce > MEN_Z127_DB_MAX_US)
  55. debounce = MEN_Z127_DB_MAX_US;
  56. /* 50us per register unit */
  57. debounce /= 50;
  58. }
  59. spin_lock(&gc->bgpio_lock);
  60. db_en = readl(priv->reg_base + MEN_Z127_DBER);
  61. if (debounce == 0) {
  62. db_en &= ~BIT(gpio);
  63. db_cnt = 0;
  64. } else {
  65. db_en |= BIT(gpio);
  66. db_cnt = debounce;
  67. }
  68. writel(db_en, priv->reg_base + MEN_Z127_DBER);
  69. writel(db_cnt, priv->reg_base + GPIO_TO_DBCNT_REG(gpio));
  70. spin_unlock(&gc->bgpio_lock);
  71. return 0;
  72. }
  73. static int men_z127_set_single_ended(struct gpio_chip *gc,
  74. unsigned offset,
  75. enum pin_config_param param)
  76. {
  77. struct men_z127_gpio *priv = gpiochip_get_data(gc);
  78. u32 od_en;
  79. spin_lock(&gc->bgpio_lock);
  80. od_en = readl(priv->reg_base + MEN_Z127_ODER);
  81. if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN)
  82. od_en |= BIT(offset);
  83. else
  84. /* Implicitly PIN_CONFIG_DRIVE_PUSH_PULL */
  85. od_en &= ~BIT(offset);
  86. writel(od_en, priv->reg_base + MEN_Z127_ODER);
  87. spin_unlock(&gc->bgpio_lock);
  88. return 0;
  89. }
  90. static int men_z127_set_config(struct gpio_chip *gc, unsigned offset,
  91. unsigned long config)
  92. {
  93. enum pin_config_param param = pinconf_to_config_param(config);
  94. switch (param) {
  95. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  96. case PIN_CONFIG_DRIVE_PUSH_PULL:
  97. return men_z127_set_single_ended(gc, offset, param);
  98. case PIN_CONFIG_INPUT_DEBOUNCE:
  99. return men_z127_debounce(gc, offset,
  100. pinconf_to_config_argument(config));
  101. default:
  102. break;
  103. }
  104. return -ENOTSUPP;
  105. }
  106. static int men_z127_probe(struct mcb_device *mdev,
  107. const struct mcb_device_id *id)
  108. {
  109. struct men_z127_gpio *men_z127_gpio;
  110. struct device *dev = &mdev->dev;
  111. int ret;
  112. men_z127_gpio = devm_kzalloc(dev, sizeof(struct men_z127_gpio),
  113. GFP_KERNEL);
  114. if (!men_z127_gpio)
  115. return -ENOMEM;
  116. men_z127_gpio->mem = mcb_request_mem(mdev, dev_name(dev));
  117. if (IS_ERR(men_z127_gpio->mem)) {
  118. dev_err(dev, "failed to request device memory");
  119. return PTR_ERR(men_z127_gpio->mem);
  120. }
  121. men_z127_gpio->reg_base = ioremap(men_z127_gpio->mem->start,
  122. resource_size(men_z127_gpio->mem));
  123. if (men_z127_gpio->reg_base == NULL) {
  124. ret = -ENXIO;
  125. goto err_release;
  126. }
  127. mcb_set_drvdata(mdev, men_z127_gpio);
  128. ret = bgpio_init(&men_z127_gpio->gc, &mdev->dev, 4,
  129. men_z127_gpio->reg_base + MEN_Z127_PSR,
  130. men_z127_gpio->reg_base + MEN_Z127_CTRL,
  131. NULL,
  132. men_z127_gpio->reg_base + MEN_Z127_GPIODR,
  133. NULL, 0);
  134. if (ret)
  135. goto err_unmap;
  136. men_z127_gpio->gc.set_config = men_z127_set_config;
  137. ret = gpiochip_add_data(&men_z127_gpio->gc, men_z127_gpio);
  138. if (ret) {
  139. dev_err(dev, "failed to register MEN 16Z127 GPIO controller");
  140. goto err_unmap;
  141. }
  142. dev_info(dev, "MEN 16Z127 GPIO driver registered");
  143. return 0;
  144. err_unmap:
  145. iounmap(men_z127_gpio->reg_base);
  146. err_release:
  147. mcb_release_mem(men_z127_gpio->mem);
  148. return ret;
  149. }
  150. static void men_z127_remove(struct mcb_device *mdev)
  151. {
  152. struct men_z127_gpio *men_z127_gpio = mcb_get_drvdata(mdev);
  153. gpiochip_remove(&men_z127_gpio->gc);
  154. iounmap(men_z127_gpio->reg_base);
  155. mcb_release_mem(men_z127_gpio->mem);
  156. }
  157. static const struct mcb_device_id men_z127_ids[] = {
  158. { .device = 0x7f },
  159. { }
  160. };
  161. MODULE_DEVICE_TABLE(mcb, men_z127_ids);
  162. static struct mcb_driver men_z127_driver = {
  163. .driver = {
  164. .name = "z127-gpio",
  165. },
  166. .probe = men_z127_probe,
  167. .remove = men_z127_remove,
  168. .id_table = men_z127_ids,
  169. };
  170. module_mcb_driver(men_z127_driver);
  171. MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
  172. MODULE_DESCRIPTION("MEN 16z127 GPIO Controller");
  173. MODULE_LICENSE("GPL v2");
  174. MODULE_ALIAS("mcb:16z127");