gpio-tpic2810.c 4.0 KB

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