gpio-tpic2810.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 GPIO_LINE_DIRECTION_OUT;
  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_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
  53. {
  54. struct tpic2810 *gpio = gpiochip_get_data(chip);
  55. u8 buffer;
  56. int err;
  57. mutex_lock(&gpio->lock);
  58. buffer = gpio->buffer & ~mask;
  59. buffer |= (mask & bits);
  60. err = i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
  61. buffer);
  62. if (!err)
  63. gpio->buffer = buffer;
  64. mutex_unlock(&gpio->lock);
  65. }
  66. static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value)
  67. {
  68. tpic2810_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
  69. }
  70. static void tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask,
  71. unsigned long *bits)
  72. {
  73. tpic2810_set_mask_bits(chip, *mask, *bits);
  74. }
  75. static const struct gpio_chip template_chip = {
  76. .label = "tpic2810",
  77. .owner = THIS_MODULE,
  78. .get_direction = tpic2810_get_direction,
  79. .direction_input = tpic2810_direction_input,
  80. .direction_output = tpic2810_direction_output,
  81. .set = tpic2810_set,
  82. .set_multiple = tpic2810_set_multiple,
  83. .base = -1,
  84. .ngpio = 8,
  85. .can_sleep = true,
  86. };
  87. static const struct of_device_id tpic2810_of_match_table[] = {
  88. { .compatible = "ti,tpic2810" },
  89. { /* sentinel */ }
  90. };
  91. MODULE_DEVICE_TABLE(of, tpic2810_of_match_table);
  92. static int tpic2810_probe(struct i2c_client *client,
  93. const struct i2c_device_id *id)
  94. {
  95. struct tpic2810 *gpio;
  96. int ret;
  97. gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
  98. if (!gpio)
  99. return -ENOMEM;
  100. i2c_set_clientdata(client, gpio);
  101. gpio->chip = template_chip;
  102. gpio->chip.parent = &client->dev;
  103. gpio->client = client;
  104. mutex_init(&gpio->lock);
  105. ret = gpiochip_add_data(&gpio->chip, gpio);
  106. if (ret < 0) {
  107. dev_err(&client->dev, "Unable to register gpiochip\n");
  108. return ret;
  109. }
  110. return 0;
  111. }
  112. static int tpic2810_remove(struct i2c_client *client)
  113. {
  114. struct tpic2810 *gpio = i2c_get_clientdata(client);
  115. gpiochip_remove(&gpio->chip);
  116. return 0;
  117. }
  118. static const struct i2c_device_id tpic2810_id_table[] = {
  119. { "tpic2810", },
  120. { /* sentinel */ }
  121. };
  122. MODULE_DEVICE_TABLE(i2c, tpic2810_id_table);
  123. static struct i2c_driver tpic2810_driver = {
  124. .driver = {
  125. .name = "tpic2810",
  126. .of_match_table = tpic2810_of_match_table,
  127. },
  128. .probe = tpic2810_probe,
  129. .remove = tpic2810_remove,
  130. .id_table = tpic2810_id_table,
  131. };
  132. module_i2c_driver(tpic2810_driver);
  133. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  134. MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
  135. MODULE_LICENSE("GPL v2");