gpio-exar.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * GPIO driver for Exar XR17V35X chip
  4. *
  5. * Copyright (C) 2015 Sudip Mukherjee <[email protected]>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/device.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/idr.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #define EXAR_OFFSET_MPIOLVL_LO 0x90
  18. #define EXAR_OFFSET_MPIOSEL_LO 0x93
  19. #define EXAR_OFFSET_MPIOLVL_HI 0x96
  20. #define EXAR_OFFSET_MPIOSEL_HI 0x99
  21. /*
  22. * The Device Configuration and UART Configuration Registers
  23. * for each UART channel take 1KB of memory address space.
  24. */
  25. #define EXAR_UART_CHANNEL_SIZE 0x400
  26. #define DRIVER_NAME "gpio_exar"
  27. static DEFINE_IDA(ida_index);
  28. struct exar_gpio_chip {
  29. struct gpio_chip gpio_chip;
  30. struct regmap *regmap;
  31. int index;
  32. char name[20];
  33. unsigned int first_pin;
  34. /*
  35. * The offset to the cascaded device's (if existing)
  36. * Device Configuration Registers.
  37. */
  38. unsigned int cascaded_offset;
  39. };
  40. static unsigned int
  41. exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
  42. {
  43. unsigned int pin = exar_gpio->first_pin + (offset % 16);
  44. unsigned int cascaded = offset / 16;
  45. unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
  46. return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
  47. }
  48. static unsigned int
  49. exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
  50. {
  51. unsigned int pin = exar_gpio->first_pin + (offset % 16);
  52. unsigned int cascaded = offset / 16;
  53. unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
  54. return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
  55. }
  56. static unsigned int
  57. exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset)
  58. {
  59. unsigned int pin = exar_gpio->first_pin + (offset % 16);
  60. return pin % 8;
  61. }
  62. static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
  63. {
  64. struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
  65. unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
  66. unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
  67. if (regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)))
  68. return GPIO_LINE_DIRECTION_IN;
  69. return GPIO_LINE_DIRECTION_OUT;
  70. }
  71. static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
  72. {
  73. struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
  74. unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
  75. unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
  76. return !!(regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)));
  77. }
  78. static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
  79. int value)
  80. {
  81. struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
  82. unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
  83. unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
  84. if (value)
  85. regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
  86. else
  87. regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
  88. }
  89. static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
  90. int value)
  91. {
  92. struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
  93. unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
  94. unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
  95. exar_set_value(chip, offset, value);
  96. regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
  97. return 0;
  98. }
  99. static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
  100. {
  101. struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
  102. unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
  103. unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
  104. regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
  105. return 0;
  106. }
  107. static void exar_devm_ida_free(void *data)
  108. {
  109. struct exar_gpio_chip *exar_gpio = data;
  110. ida_free(&ida_index, exar_gpio->index);
  111. }
  112. static const struct regmap_config exar_regmap_config = {
  113. .name = "exar-gpio",
  114. .reg_bits = 16,
  115. .val_bits = 8,
  116. };
  117. static int gpio_exar_probe(struct platform_device *pdev)
  118. {
  119. struct device *dev = &pdev->dev;
  120. struct pci_dev *pcidev = to_pci_dev(dev->parent);
  121. struct exar_gpio_chip *exar_gpio;
  122. u32 first_pin, ngpios;
  123. void __iomem *p;
  124. int index, ret;
  125. /*
  126. * The UART driver must have mapped region 0 prior to registering this
  127. * device - use it.
  128. */
  129. p = pcim_iomap_table(pcidev)[0];
  130. if (!p)
  131. return -ENOMEM;
  132. ret = device_property_read_u32(dev, "exar,first-pin", &first_pin);
  133. if (ret)
  134. return ret;
  135. ret = device_property_read_u32(dev, "ngpios", &ngpios);
  136. if (ret)
  137. return ret;
  138. exar_gpio = devm_kzalloc(dev, sizeof(*exar_gpio), GFP_KERNEL);
  139. if (!exar_gpio)
  140. return -ENOMEM;
  141. /*
  142. * If cascaded, secondary xr17v354 or xr17v358 have the same amount
  143. * of MPIOs as their primaries and the last 4 bits of the primary's
  144. * PCI Device ID is the number of its UART channels.
  145. */
  146. if (pcidev->device & GENMASK(15, 12)) {
  147. ngpios += ngpios;
  148. exar_gpio->cascaded_offset = (pcidev->device & GENMASK(3, 0)) *
  149. EXAR_UART_CHANNEL_SIZE;
  150. }
  151. /*
  152. * We don't need to check the return values of mmio regmap operations (unless
  153. * the regmap has a clock attached which is not the case here).
  154. */
  155. exar_gpio->regmap = devm_regmap_init_mmio(dev, p, &exar_regmap_config);
  156. if (IS_ERR(exar_gpio->regmap))
  157. return PTR_ERR(exar_gpio->regmap);
  158. index = ida_alloc(&ida_index, GFP_KERNEL);
  159. if (index < 0)
  160. return index;
  161. ret = devm_add_action_or_reset(dev, exar_devm_ida_free, exar_gpio);
  162. if (ret)
  163. return ret;
  164. sprintf(exar_gpio->name, "exar_gpio%d", index);
  165. exar_gpio->gpio_chip.label = exar_gpio->name;
  166. exar_gpio->gpio_chip.parent = dev;
  167. exar_gpio->gpio_chip.direction_output = exar_direction_output;
  168. exar_gpio->gpio_chip.direction_input = exar_direction_input;
  169. exar_gpio->gpio_chip.get_direction = exar_get_direction;
  170. exar_gpio->gpio_chip.get = exar_get_value;
  171. exar_gpio->gpio_chip.set = exar_set_value;
  172. exar_gpio->gpio_chip.base = -1;
  173. exar_gpio->gpio_chip.ngpio = ngpios;
  174. exar_gpio->index = index;
  175. exar_gpio->first_pin = first_pin;
  176. ret = devm_gpiochip_add_data(dev, &exar_gpio->gpio_chip, exar_gpio);
  177. if (ret)
  178. return ret;
  179. platform_set_drvdata(pdev, exar_gpio);
  180. return 0;
  181. }
  182. static struct platform_driver gpio_exar_driver = {
  183. .probe = gpio_exar_probe,
  184. .driver = {
  185. .name = DRIVER_NAME,
  186. },
  187. };
  188. module_platform_driver(gpio_exar_driver);
  189. MODULE_ALIAS("platform:" DRIVER_NAME);
  190. MODULE_DESCRIPTION("Exar GPIO driver");
  191. MODULE_AUTHOR("Sudip Mukherjee <[email protected]>");
  192. MODULE_LICENSE("GPL");