gpio-pca9570.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for PCA9570 I2C GPO expander
  4. *
  5. * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
  6. *
  7. * Based on gpio-tpic2810.c
  8. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  9. * Andrew F. Davis <afd@ti.com>
  10. */
  11. #include <linux/gpio/driver.h>
  12. #include <linux/i2c.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/property.h>
  16. /**
  17. * struct pca9570 - GPIO driver data
  18. * @chip: GPIO controller chip
  19. * @lock: Protects write sequences
  20. * @out: Buffer for device register
  21. */
  22. struct pca9570 {
  23. struct gpio_chip chip;
  24. struct mutex lock;
  25. u8 out;
  26. };
  27. static int pca9570_read(struct pca9570 *gpio, u8 *value)
  28. {
  29. struct i2c_client *client = to_i2c_client(gpio->chip.parent);
  30. int ret;
  31. ret = i2c_smbus_read_byte(client);
  32. if (ret < 0)
  33. return ret;
  34. *value = ret;
  35. return 0;
  36. }
  37. static int pca9570_write(struct pca9570 *gpio, u8 value)
  38. {
  39. struct i2c_client *client = to_i2c_client(gpio->chip.parent);
  40. return i2c_smbus_write_byte(client, value);
  41. }
  42. static int pca9570_get_direction(struct gpio_chip *chip,
  43. unsigned offset)
  44. {
  45. /* This device always output */
  46. return GPIO_LINE_DIRECTION_OUT;
  47. }
  48. static int pca9570_get(struct gpio_chip *chip, unsigned offset)
  49. {
  50. struct pca9570 *gpio = gpiochip_get_data(chip);
  51. u8 buffer;
  52. int ret;
  53. ret = pca9570_read(gpio, &buffer);
  54. if (ret)
  55. return ret;
  56. return !!(buffer & BIT(offset));
  57. }
  58. static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
  59. {
  60. struct pca9570 *gpio = gpiochip_get_data(chip);
  61. u8 buffer;
  62. int ret;
  63. mutex_lock(&gpio->lock);
  64. buffer = gpio->out;
  65. if (value)
  66. buffer |= BIT(offset);
  67. else
  68. buffer &= ~BIT(offset);
  69. ret = pca9570_write(gpio, buffer);
  70. if (ret)
  71. goto out;
  72. gpio->out = buffer;
  73. out:
  74. mutex_unlock(&gpio->lock);
  75. }
  76. static int pca9570_probe(struct i2c_client *client)
  77. {
  78. struct pca9570 *gpio;
  79. gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
  80. if (!gpio)
  81. return -ENOMEM;
  82. gpio->chip.label = client->name;
  83. gpio->chip.parent = &client->dev;
  84. gpio->chip.owner = THIS_MODULE;
  85. gpio->chip.get_direction = pca9570_get_direction;
  86. gpio->chip.get = pca9570_get;
  87. gpio->chip.set = pca9570_set;
  88. gpio->chip.base = -1;
  89. gpio->chip.ngpio = (uintptr_t)device_get_match_data(&client->dev);
  90. gpio->chip.can_sleep = true;
  91. mutex_init(&gpio->lock);
  92. /* Read the current output level */
  93. pca9570_read(gpio, &gpio->out);
  94. i2c_set_clientdata(client, gpio);
  95. return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
  96. }
  97. static const struct i2c_device_id pca9570_id_table[] = {
  98. { "pca9570", 4 },
  99. { /* sentinel */ }
  100. };
  101. MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
  102. static const struct of_device_id pca9570_of_match_table[] = {
  103. { .compatible = "nxp,pca9570", .data = (void *)4 },
  104. { /* sentinel */ }
  105. };
  106. MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
  107. static struct i2c_driver pca9570_driver = {
  108. .driver = {
  109. .name = "pca9570",
  110. .of_match_table = pca9570_of_match_table,
  111. },
  112. .probe_new = pca9570_probe,
  113. .id_table = pca9570_id_table,
  114. };
  115. module_i2c_driver(pca9570_driver);
  116. MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
  117. MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
  118. MODULE_LICENSE("GPL v2");