gpio-pca9570.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #define SLG7XL45106_GPO_REG 0xDB
  17. /**
  18. * struct pca9570_chip_data - GPIO platformdata
  19. * @ngpio: no of gpios
  20. * @command: Command to be sent
  21. */
  22. struct pca9570_chip_data {
  23. u16 ngpio;
  24. u32 command;
  25. };
  26. /**
  27. * struct pca9570 - GPIO driver data
  28. * @chip: GPIO controller chip
  29. * @chip_data: GPIO controller platform data
  30. * @lock: Protects write sequences
  31. * @out: Buffer for device register
  32. */
  33. struct pca9570 {
  34. struct gpio_chip chip;
  35. const struct pca9570_chip_data *chip_data;
  36. struct mutex lock;
  37. u8 out;
  38. };
  39. static int pca9570_read(struct pca9570 *gpio, u8 *value)
  40. {
  41. struct i2c_client *client = to_i2c_client(gpio->chip.parent);
  42. int ret;
  43. if (gpio->chip_data->command != 0)
  44. ret = i2c_smbus_read_byte_data(client, gpio->chip_data->command);
  45. else
  46. ret = i2c_smbus_read_byte(client);
  47. if (ret < 0)
  48. return ret;
  49. *value = ret;
  50. return 0;
  51. }
  52. static int pca9570_write(struct pca9570 *gpio, u8 value)
  53. {
  54. struct i2c_client *client = to_i2c_client(gpio->chip.parent);
  55. if (gpio->chip_data->command != 0)
  56. return i2c_smbus_write_byte_data(client, gpio->chip_data->command, value);
  57. return i2c_smbus_write_byte(client, value);
  58. }
  59. static int pca9570_get_direction(struct gpio_chip *chip,
  60. unsigned offset)
  61. {
  62. /* This device always output */
  63. return GPIO_LINE_DIRECTION_OUT;
  64. }
  65. static int pca9570_get(struct gpio_chip *chip, unsigned offset)
  66. {
  67. struct pca9570 *gpio = gpiochip_get_data(chip);
  68. u8 buffer;
  69. int ret;
  70. ret = pca9570_read(gpio, &buffer);
  71. if (ret)
  72. return ret;
  73. return !!(buffer & BIT(offset));
  74. }
  75. static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
  76. {
  77. struct pca9570 *gpio = gpiochip_get_data(chip);
  78. u8 buffer;
  79. int ret;
  80. mutex_lock(&gpio->lock);
  81. buffer = gpio->out;
  82. if (value)
  83. buffer |= BIT(offset);
  84. else
  85. buffer &= ~BIT(offset);
  86. ret = pca9570_write(gpio, buffer);
  87. if (ret)
  88. goto out;
  89. gpio->out = buffer;
  90. out:
  91. mutex_unlock(&gpio->lock);
  92. }
  93. static int pca9570_probe(struct i2c_client *client)
  94. {
  95. struct pca9570 *gpio;
  96. gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
  97. if (!gpio)
  98. return -ENOMEM;
  99. gpio->chip.label = client->name;
  100. gpio->chip.parent = &client->dev;
  101. gpio->chip.owner = THIS_MODULE;
  102. gpio->chip.get_direction = pca9570_get_direction;
  103. gpio->chip.get = pca9570_get;
  104. gpio->chip.set = pca9570_set;
  105. gpio->chip.base = -1;
  106. gpio->chip_data = device_get_match_data(&client->dev);
  107. gpio->chip.ngpio = gpio->chip_data->ngpio;
  108. gpio->chip.can_sleep = true;
  109. mutex_init(&gpio->lock);
  110. /* Read the current output level */
  111. pca9570_read(gpio, &gpio->out);
  112. i2c_set_clientdata(client, gpio);
  113. return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
  114. }
  115. static const struct pca9570_chip_data pca9570_gpio = {
  116. .ngpio = 4,
  117. };
  118. static const struct pca9570_chip_data pca9571_gpio = {
  119. .ngpio = 8,
  120. };
  121. static const struct pca9570_chip_data slg7xl45106_gpio = {
  122. .ngpio = 8,
  123. .command = SLG7XL45106_GPO_REG,
  124. };
  125. static const struct i2c_device_id pca9570_id_table[] = {
  126. { "pca9570", (kernel_ulong_t)&pca9570_gpio},
  127. { "pca9571", (kernel_ulong_t)&pca9571_gpio },
  128. { "slg7xl45106", (kernel_ulong_t)&slg7xl45106_gpio },
  129. { /* sentinel */ }
  130. };
  131. MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
  132. static const struct of_device_id pca9570_of_match_table[] = {
  133. { .compatible = "dlg,slg7xl45106", .data = &slg7xl45106_gpio},
  134. { .compatible = "nxp,pca9570", .data = &pca9570_gpio },
  135. { .compatible = "nxp,pca9571", .data = &pca9571_gpio },
  136. { /* sentinel */ }
  137. };
  138. MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
  139. static struct i2c_driver pca9570_driver = {
  140. .driver = {
  141. .name = "pca9570",
  142. .of_match_table = pca9570_of_match_table,
  143. },
  144. .probe = pca9570_probe,
  145. .id_table = pca9570_id_table,
  146. };
  147. module_i2c_driver(pca9570_driver);
  148. MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
  149. MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
  150. MODULE_LICENSE("GPL v2");