gpio-tpic2810.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  4. * Andrew F. Davis <[email protected]>
  5. */
  6. #include <linux/gpio/driver.h>
  7. #include <linux/i2c.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #define TPIC2810_WS_COMMAND 0x44
  11. /**
  12. * struct tpic2810 - GPIO driver data
  13. * @chip: GPIO controller chip
  14. * @client: I2C device pointer
  15. * @buffer: Buffer for device register
  16. * @lock: Protects write sequences
  17. */
  18. struct tpic2810 {
  19. struct gpio_chip chip;
  20. struct i2c_client *client;
  21. u8 buffer;
  22. struct mutex lock;
  23. };
  24. static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value);
  25. static int tpic2810_get_direction(struct gpio_chip *chip,
  26. unsigned offset)
  27. {
  28. /* This device always output */
  29. return GPIO_LINE_DIRECTION_OUT;
  30. }
  31. static int tpic2810_direction_input(struct gpio_chip *chip,
  32. unsigned offset)
  33. {
  34. /* This device is output only */
  35. return -EINVAL;
  36. }
  37. static int tpic2810_direction_output(struct gpio_chip *chip,
  38. unsigned offset, int value)
  39. {
  40. /* This device always output */
  41. tpic2810_set(chip, offset, value);
  42. return 0;
  43. }
  44. static void tpic2810_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
  45. {
  46. struct tpic2810 *gpio = gpiochip_get_data(chip);
  47. u8 buffer;
  48. int err;
  49. mutex_lock(&gpio->lock);
  50. buffer = gpio->buffer & ~mask;
  51. buffer |= (mask & bits);
  52. err = i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
  53. buffer);
  54. if (!err)
  55. gpio->buffer = buffer;
  56. mutex_unlock(&gpio->lock);
  57. }
  58. static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value)
  59. {
  60. tpic2810_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
  61. }
  62. static void tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask,
  63. unsigned long *bits)
  64. {
  65. tpic2810_set_mask_bits(chip, *mask, *bits);
  66. }
  67. static const struct gpio_chip template_chip = {
  68. .label = "tpic2810",
  69. .owner = THIS_MODULE,
  70. .get_direction = tpic2810_get_direction,
  71. .direction_input = tpic2810_direction_input,
  72. .direction_output = tpic2810_direction_output,
  73. .set = tpic2810_set,
  74. .set_multiple = tpic2810_set_multiple,
  75. .base = -1,
  76. .ngpio = 8,
  77. .can_sleep = true,
  78. };
  79. static const struct of_device_id tpic2810_of_match_table[] = {
  80. { .compatible = "ti,tpic2810" },
  81. { /* sentinel */ }
  82. };
  83. MODULE_DEVICE_TABLE(of, tpic2810_of_match_table);
  84. static int tpic2810_probe(struct i2c_client *client,
  85. const struct i2c_device_id *id)
  86. {
  87. struct tpic2810 *gpio;
  88. int ret;
  89. gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
  90. if (!gpio)
  91. return -ENOMEM;
  92. i2c_set_clientdata(client, gpio);
  93. gpio->chip = template_chip;
  94. gpio->chip.parent = &client->dev;
  95. gpio->client = client;
  96. mutex_init(&gpio->lock);
  97. ret = gpiochip_add_data(&gpio->chip, gpio);
  98. if (ret < 0) {
  99. dev_err(&client->dev, "Unable to register gpiochip\n");
  100. return ret;
  101. }
  102. return 0;
  103. }
  104. static void tpic2810_remove(struct i2c_client *client)
  105. {
  106. struct tpic2810 *gpio = i2c_get_clientdata(client);
  107. gpiochip_remove(&gpio->chip);
  108. }
  109. static const struct i2c_device_id tpic2810_id_table[] = {
  110. { "tpic2810", },
  111. { /* sentinel */ }
  112. };
  113. MODULE_DEVICE_TABLE(i2c, tpic2810_id_table);
  114. static struct i2c_driver tpic2810_driver = {
  115. .driver = {
  116. .name = "tpic2810",
  117. .of_match_table = tpic2810_of_match_table,
  118. },
  119. .probe = tpic2810_probe,
  120. .remove = tpic2810_remove,
  121. .id_table = tpic2810_id_table,
  122. };
  123. module_i2c_driver(tpic2810_driver);
  124. MODULE_AUTHOR("Andrew F. Davis <[email protected]>");
  125. MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
  126. MODULE_LICENSE("GPL v2");