gpio-gw-pld.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Gateworks I2C PLD GPIO expander
  4. //
  5. // Copyright (C) 2019 Linus Walleij <linus.walleij@linaro.org>
  6. //
  7. // Based on code and know-how from the OpenWrt driver:
  8. // Copyright (C) 2009 Gateworks Corporation
  9. // Authors: Chris Lang, Imre Kaloz
  10. #include <linux/bits.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/i2c.h>
  15. #include <linux/module.h>
  16. /**
  17. * struct gw_pld - State container for Gateworks PLD
  18. * @chip: GPIO chip instance
  19. * @client: I2C client
  20. * @out: shadow register for the output bute
  21. */
  22. struct gw_pld {
  23. struct gpio_chip chip;
  24. struct i2c_client *client;
  25. u8 out;
  26. };
  27. /*
  28. * The Gateworks I2C PLD chip only expose one read and one write register.
  29. * Writing a "one" bit (to match the reset state) lets that pin be used as an
  30. * input. It is an open-drain model.
  31. */
  32. static int gw_pld_input8(struct gpio_chip *gc, unsigned offset)
  33. {
  34. struct gw_pld *gw = gpiochip_get_data(gc);
  35. gw->out |= BIT(offset);
  36. return i2c_smbus_write_byte(gw->client, gw->out);
  37. }
  38. static int gw_pld_get8(struct gpio_chip *gc, unsigned offset)
  39. {
  40. struct gw_pld *gw = gpiochip_get_data(gc);
  41. s32 val;
  42. val = i2c_smbus_read_byte(gw->client);
  43. return (val < 0) ? 0 : !!(val & BIT(offset));
  44. }
  45. static int gw_pld_output8(struct gpio_chip *gc, unsigned offset, int value)
  46. {
  47. struct gw_pld *gw = gpiochip_get_data(gc);
  48. if (value)
  49. gw->out |= BIT(offset);
  50. else
  51. gw->out &= ~BIT(offset);
  52. return i2c_smbus_write_byte(gw->client, gw->out);
  53. }
  54. static void gw_pld_set8(struct gpio_chip *gc, unsigned offset, int value)
  55. {
  56. gw_pld_output8(gc, offset, value);
  57. }
  58. static int gw_pld_probe(struct i2c_client *client,
  59. const struct i2c_device_id *id)
  60. {
  61. struct device *dev = &client->dev;
  62. struct device_node *np = dev->of_node;
  63. struct gw_pld *gw;
  64. int ret;
  65. gw = devm_kzalloc(dev, sizeof(*gw), GFP_KERNEL);
  66. if (!gw)
  67. return -ENOMEM;
  68. gw->chip.base = -1;
  69. gw->chip.can_sleep = true;
  70. gw->chip.parent = dev;
  71. gw->chip.of_node = np;
  72. gw->chip.owner = THIS_MODULE;
  73. gw->chip.label = dev_name(dev);
  74. gw->chip.ngpio = 8;
  75. gw->chip.direction_input = gw_pld_input8;
  76. gw->chip.get = gw_pld_get8;
  77. gw->chip.direction_output = gw_pld_output8;
  78. gw->chip.set = gw_pld_set8;
  79. gw->client = client;
  80. /*
  81. * The Gateworks I2C PLD chip does not properly send the acknowledge
  82. * bit at all times, but we can still use the standard i2c_smbus
  83. * functions by simply ignoring this bit.
  84. */
  85. client->flags |= I2C_M_IGNORE_NAK;
  86. gw->out = 0xFF;
  87. i2c_set_clientdata(client, gw);
  88. ret = devm_gpiochip_add_data(dev, &gw->chip, gw);
  89. if (ret)
  90. return ret;
  91. dev_info(dev, "registered Gateworks PLD GPIO device\n");
  92. return 0;
  93. }
  94. static const struct i2c_device_id gw_pld_id[] = {
  95. { "gw-pld", },
  96. { }
  97. };
  98. MODULE_DEVICE_TABLE(i2c, gw_pld_id);
  99. static const struct of_device_id gw_pld_dt_ids[] = {
  100. { .compatible = "gateworks,pld-gpio", },
  101. { },
  102. };
  103. MODULE_DEVICE_TABLE(of, gw_pld_dt_ids);
  104. static struct i2c_driver gw_pld_driver = {
  105. .driver = {
  106. .name = "gw_pld",
  107. .of_match_table = gw_pld_dt_ids,
  108. },
  109. .probe = gw_pld_probe,
  110. .id_table = gw_pld_id,
  111. };
  112. module_i2c_driver(gw_pld_driver);
  113. MODULE_LICENSE("GPL");
  114. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");