gpio-tps65910.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * TI TPS6591x GPIO driver
  3. *
  4. * Copyright 2010 Texas Instruments Inc.
  5. *
  6. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  7. * Author: Jorge Eduardo Candelaria jedu@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/gpio.h>
  19. #include <linux/i2c.h>
  20. #include <linux/mfd/tps65910.h>
  21. static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
  22. {
  23. struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio);
  24. uint8_t val;
  25. tps65910->read(tps65910, TPS65910_GPIO0 + offset, 1, &val);
  26. if (val & GPIO_STS_MASK)
  27. return 1;
  28. return 0;
  29. }
  30. static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
  31. int value)
  32. {
  33. struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio);
  34. if (value)
  35. tps65910_set_bits(tps65910, TPS65910_GPIO0 + offset,
  36. GPIO_SET_MASK);
  37. else
  38. tps65910_clear_bits(tps65910, TPS65910_GPIO0 + offset,
  39. GPIO_SET_MASK);
  40. }
  41. static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
  42. int value)
  43. {
  44. struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio);
  45. /* Set the initial value */
  46. tps65910_gpio_set(gc, offset, value);
  47. return tps65910_set_bits(tps65910, TPS65910_GPIO0 + offset,
  48. GPIO_CFG_MASK);
  49. }
  50. static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
  51. {
  52. struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio);
  53. return tps65910_clear_bits(tps65910, TPS65910_GPIO0 + offset,
  54. GPIO_CFG_MASK);
  55. }
  56. void tps65910_gpio_init(struct tps65910 *tps65910, int gpio_base)
  57. {
  58. int ret;
  59. struct tps65910_board *board_data;
  60. if (!gpio_base)
  61. return;
  62. tps65910->gpio.owner = THIS_MODULE;
  63. tps65910->gpio.label = tps65910->i2c_client->name;
  64. tps65910->gpio.dev = tps65910->dev;
  65. tps65910->gpio.base = gpio_base;
  66. switch(tps65910_chip_id(tps65910)) {
  67. case TPS65910:
  68. tps65910->gpio.ngpio = TPS65910_NUM_GPIO;
  69. break;
  70. case TPS65911:
  71. tps65910->gpio.ngpio = TPS65911_NUM_GPIO;
  72. break;
  73. default:
  74. return;
  75. }
  76. tps65910->gpio.can_sleep = 1;
  77. tps65910->gpio.direction_input = tps65910_gpio_input;
  78. tps65910->gpio.direction_output = tps65910_gpio_output;
  79. tps65910->gpio.set = tps65910_gpio_set;
  80. tps65910->gpio.get = tps65910_gpio_get;
  81. /* Configure sleep control for gpios */
  82. board_data = dev_get_platdata(tps65910->dev);
  83. if (board_data) {
  84. int i;
  85. for (i = 0; i < tps65910->gpio.ngpio; ++i) {
  86. if (board_data->en_gpio_sleep[i]) {
  87. ret = tps65910_set_bits(tps65910,
  88. TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
  89. if (ret < 0)
  90. dev_warn(tps65910->dev,
  91. "GPIO Sleep setting failed\n");
  92. }
  93. }
  94. }
  95. ret = gpiochip_add(&tps65910->gpio);
  96. if (ret)
  97. dev_warn(tps65910->dev, "GPIO registration failed: %d\n", ret);
  98. }