gpio-menz127.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = round_up(debounce, MEN_Z127_DB_MIN_US);
  52. else
  53. debounce = round_down(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 single_ended_mode mode)
  76. {
  77. struct men_z127_gpio *priv = gpiochip_get_data(gc);
  78. u32 od_en;
  79. if (mode != LINE_MODE_OPEN_DRAIN &&
  80. mode != LINE_MODE_PUSH_PULL)
  81. return -ENOTSUPP;
  82. spin_lock(&gc->bgpio_lock);
  83. od_en = readl(priv->reg_base + MEN_Z127_ODER);
  84. if (mode == LINE_MODE_OPEN_DRAIN)
  85. od_en |= BIT(offset);
  86. else
  87. /* Implicitly LINE_MODE_PUSH_PULL */
  88. od_en &= ~BIT(offset);
  89. writel(od_en, priv->reg_base + MEN_Z127_ODER);
  90. spin_unlock(&gc->bgpio_lock);
  91. return 0;
  92. }
  93. static int men_z127_probe(struct mcb_device *mdev,
  94. const struct mcb_device_id *id)
  95. {
  96. struct men_z127_gpio *men_z127_gpio;
  97. struct device *dev = &mdev->dev;
  98. int ret;
  99. men_z127_gpio = devm_kzalloc(dev, sizeof(struct men_z127_gpio),
  100. GFP_KERNEL);
  101. if (!men_z127_gpio)
  102. return -ENOMEM;
  103. men_z127_gpio->mem = mcb_request_mem(mdev, dev_name(dev));
  104. if (IS_ERR(men_z127_gpio->mem)) {
  105. dev_err(dev, "failed to request device memory");
  106. return PTR_ERR(men_z127_gpio->mem);
  107. }
  108. men_z127_gpio->reg_base = ioremap(men_z127_gpio->mem->start,
  109. resource_size(men_z127_gpio->mem));
  110. if (men_z127_gpio->reg_base == NULL) {
  111. ret = -ENXIO;
  112. goto err_release;
  113. }
  114. mcb_set_drvdata(mdev, men_z127_gpio);
  115. ret = bgpio_init(&men_z127_gpio->gc, &mdev->dev, 4,
  116. men_z127_gpio->reg_base + MEN_Z127_PSR,
  117. men_z127_gpio->reg_base + MEN_Z127_CTRL,
  118. NULL,
  119. men_z127_gpio->reg_base + MEN_Z127_GPIODR,
  120. NULL, 0);
  121. if (ret)
  122. goto err_unmap;
  123. men_z127_gpio->gc.set_debounce = men_z127_debounce;
  124. men_z127_gpio->gc.set_single_ended = men_z127_set_single_ended;
  125. ret = gpiochip_add_data(&men_z127_gpio->gc, men_z127_gpio);
  126. if (ret) {
  127. dev_err(dev, "failed to register MEN 16Z127 GPIO controller");
  128. goto err_unmap;
  129. }
  130. dev_info(dev, "MEN 16Z127 GPIO driver registered");
  131. return 0;
  132. err_unmap:
  133. iounmap(men_z127_gpio->reg_base);
  134. err_release:
  135. mcb_release_mem(men_z127_gpio->mem);
  136. return ret;
  137. }
  138. static void men_z127_remove(struct mcb_device *mdev)
  139. {
  140. struct men_z127_gpio *men_z127_gpio = mcb_get_drvdata(mdev);
  141. gpiochip_remove(&men_z127_gpio->gc);
  142. iounmap(men_z127_gpio->reg_base);
  143. mcb_release_mem(men_z127_gpio->mem);
  144. }
  145. static const struct mcb_device_id men_z127_ids[] = {
  146. { .device = 0x7f },
  147. { }
  148. };
  149. MODULE_DEVICE_TABLE(mcb, men_z127_ids);
  150. static struct mcb_driver men_z127_driver = {
  151. .driver = {
  152. .name = "z127-gpio",
  153. },
  154. .probe = men_z127_probe,
  155. .remove = men_z127_remove,
  156. .id_table = men_z127_ids,
  157. };
  158. module_mcb_driver(men_z127_driver);
  159. MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
  160. MODULE_DESCRIPTION("MEN 16z127 GPIO Controller");
  161. MODULE_LICENSE("GPL v2");
  162. MODULE_ALIAS("mcb:16z127");