gpio-gw-pld.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. {
  60. struct device *dev = &client->dev;
  61. struct gw_pld *gw;
  62. int ret;
  63. gw = devm_kzalloc(dev, sizeof(*gw), GFP_KERNEL);
  64. if (!gw)
  65. return -ENOMEM;
  66. gw->chip.base = -1;
  67. gw->chip.can_sleep = true;
  68. gw->chip.parent = dev;
  69. gw->chip.owner = THIS_MODULE;
  70. gw->chip.label = dev_name(dev);
  71. gw->chip.ngpio = 8;
  72. gw->chip.direction_input = gw_pld_input8;
  73. gw->chip.get = gw_pld_get8;
  74. gw->chip.direction_output = gw_pld_output8;
  75. gw->chip.set = gw_pld_set8;
  76. gw->client = client;
  77. /*
  78. * The Gateworks I2C PLD chip does not properly send the acknowledge
  79. * bit at all times, but we can still use the standard i2c_smbus
  80. * functions by simply ignoring this bit.
  81. */
  82. client->flags |= I2C_M_IGNORE_NAK;
  83. gw->out = 0xFF;
  84. i2c_set_clientdata(client, gw);
  85. ret = devm_gpiochip_add_data(dev, &gw->chip, gw);
  86. if (ret)
  87. return ret;
  88. dev_info(dev, "registered Gateworks PLD GPIO device\n");
  89. return 0;
  90. }
  91. static const struct i2c_device_id gw_pld_id[] = {
  92. { "gw-pld", },
  93. { }
  94. };
  95. MODULE_DEVICE_TABLE(i2c, gw_pld_id);
  96. static const struct of_device_id gw_pld_dt_ids[] = {
  97. { .compatible = "gateworks,pld-gpio", },
  98. { },
  99. };
  100. MODULE_DEVICE_TABLE(of, gw_pld_dt_ids);
  101. static struct i2c_driver gw_pld_driver = {
  102. .driver = {
  103. .name = "gw_pld",
  104. .of_match_table = gw_pld_dt_ids,
  105. },
  106. .probe = gw_pld_probe,
  107. .id_table = gw_pld_id,
  108. };
  109. module_i2c_driver(gw_pld_driver);
  110. MODULE_DESCRIPTION("Gateworks I2C PLD GPIO expander");
  111. MODULE_LICENSE("GPL");
  112. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");