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