gpio-moxtet.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Turris Mox Moxtet GPIO expander
  4. *
  5. * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/moxtet.h>
  10. #include <linux/module.h>
  11. #define MOXTET_GPIO_NGPIOS 12
  12. #define MOXTET_GPIO_INPUTS 4
  13. struct moxtet_gpio_desc {
  14. u16 in_mask;
  15. u16 out_mask;
  16. };
  17. static const struct moxtet_gpio_desc descs[] = {
  18. [TURRIS_MOX_MODULE_SFP] = {
  19. .in_mask = GENMASK(2, 0),
  20. .out_mask = GENMASK(5, 4),
  21. },
  22. };
  23. struct moxtet_gpio_chip {
  24. struct device *dev;
  25. struct gpio_chip gpio_chip;
  26. const struct moxtet_gpio_desc *desc;
  27. };
  28. static int moxtet_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
  29. {
  30. struct moxtet_gpio_chip *chip = gpiochip_get_data(gc);
  31. int ret;
  32. if (chip->desc->in_mask & BIT(offset)) {
  33. ret = moxtet_device_read(chip->dev);
  34. } else if (chip->desc->out_mask & BIT(offset)) {
  35. ret = moxtet_device_written(chip->dev);
  36. if (ret >= 0)
  37. ret <<= MOXTET_GPIO_INPUTS;
  38. } else {
  39. return -EINVAL;
  40. }
  41. if (ret < 0)
  42. return ret;
  43. return !!(ret & BIT(offset));
  44. }
  45. static void moxtet_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
  46. int val)
  47. {
  48. struct moxtet_gpio_chip *chip = gpiochip_get_data(gc);
  49. int state;
  50. state = moxtet_device_written(chip->dev);
  51. if (state < 0)
  52. return;
  53. offset -= MOXTET_GPIO_INPUTS;
  54. if (val)
  55. state |= BIT(offset);
  56. else
  57. state &= ~BIT(offset);
  58. moxtet_device_write(chip->dev, state);
  59. }
  60. static int moxtet_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  61. {
  62. struct moxtet_gpio_chip *chip = gpiochip_get_data(gc);
  63. /* All lines are hard wired to be either input or output, not both. */
  64. if (chip->desc->in_mask & BIT(offset))
  65. return GPIO_LINE_DIRECTION_IN;
  66. else if (chip->desc->out_mask & BIT(offset))
  67. return GPIO_LINE_DIRECTION_OUT;
  68. else
  69. return -EINVAL;
  70. }
  71. static int moxtet_gpio_direction_input(struct gpio_chip *gc,
  72. unsigned int offset)
  73. {
  74. struct moxtet_gpio_chip *chip = gpiochip_get_data(gc);
  75. if (chip->desc->in_mask & BIT(offset))
  76. return 0;
  77. else if (chip->desc->out_mask & BIT(offset))
  78. return -ENOTSUPP;
  79. else
  80. return -EINVAL;
  81. }
  82. static int moxtet_gpio_direction_output(struct gpio_chip *gc,
  83. unsigned int offset, int val)
  84. {
  85. struct moxtet_gpio_chip *chip = gpiochip_get_data(gc);
  86. if (chip->desc->out_mask & BIT(offset))
  87. moxtet_gpio_set_value(gc, offset, val);
  88. else if (chip->desc->in_mask & BIT(offset))
  89. return -ENOTSUPP;
  90. else
  91. return -EINVAL;
  92. return 0;
  93. }
  94. static int moxtet_gpio_probe(struct device *dev)
  95. {
  96. struct moxtet_gpio_chip *chip;
  97. struct device_node *nc = dev->of_node;
  98. int id;
  99. id = to_moxtet_device(dev)->id;
  100. if (id >= ARRAY_SIZE(descs)) {
  101. dev_err(dev, "%pOF Moxtet device id 0x%x is not supported by gpio-moxtet driver\n",
  102. nc, id);
  103. return -ENOTSUPP;
  104. }
  105. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  106. if (!chip)
  107. return -ENOMEM;
  108. chip->dev = dev;
  109. chip->gpio_chip.parent = dev;
  110. chip->desc = &descs[id];
  111. dev_set_drvdata(dev, chip);
  112. chip->gpio_chip.label = dev_name(dev);
  113. chip->gpio_chip.get_direction = moxtet_gpio_get_direction;
  114. chip->gpio_chip.direction_input = moxtet_gpio_direction_input;
  115. chip->gpio_chip.direction_output = moxtet_gpio_direction_output;
  116. chip->gpio_chip.get = moxtet_gpio_get_value;
  117. chip->gpio_chip.set = moxtet_gpio_set_value;
  118. chip->gpio_chip.base = -1;
  119. chip->gpio_chip.ngpio = MOXTET_GPIO_NGPIOS;
  120. chip->gpio_chip.can_sleep = true;
  121. chip->gpio_chip.owner = THIS_MODULE;
  122. return devm_gpiochip_add_data(dev, &chip->gpio_chip, chip);
  123. }
  124. static const struct of_device_id moxtet_gpio_dt_ids[] = {
  125. { .compatible = "cznic,moxtet-gpio", },
  126. {},
  127. };
  128. MODULE_DEVICE_TABLE(of, moxtet_gpio_dt_ids);
  129. static const enum turris_mox_module_id moxtet_gpio_module_table[] = {
  130. TURRIS_MOX_MODULE_SFP,
  131. 0,
  132. };
  133. static struct moxtet_driver moxtet_gpio_driver = {
  134. .driver = {
  135. .name = "moxtet-gpio",
  136. .of_match_table = moxtet_gpio_dt_ids,
  137. .probe = moxtet_gpio_probe,
  138. },
  139. .id_table = moxtet_gpio_module_table,
  140. };
  141. module_moxtet_driver(moxtet_gpio_driver);
  142. MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
  143. MODULE_DESCRIPTION("Turris Mox Moxtet GPIO expander");
  144. MODULE_LICENSE("GPL v2");