gpio-tps65910.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, 0, 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. if (!gpio_base)
  60. return;
  61. tps65910->gpio.owner = THIS_MODULE;
  62. tps65910->gpio.label = tps65910->i2c_client->name;
  63. tps65910->gpio.dev = tps65910->dev;
  64. tps65910->gpio.base = gpio_base;
  65. switch(tps65910_chip_id(tps65910)) {
  66. case TPS65910:
  67. tps65910->gpio.ngpio = 6;
  68. break;
  69. case TPS65911:
  70. tps65910->gpio.ngpio = 9;
  71. break;
  72. default:
  73. return;
  74. }
  75. tps65910->gpio.can_sleep = 1;
  76. tps65910->gpio.direction_input = tps65910_gpio_input;
  77. tps65910->gpio.direction_output = tps65910_gpio_output;
  78. tps65910->gpio.set = tps65910_gpio_set;
  79. tps65910->gpio.get = tps65910_gpio_get;
  80. ret = gpiochip_add(&tps65910->gpio);
  81. if (ret)
  82. dev_warn(tps65910->dev, "GPIO registration failed: %d\n", ret);
  83. }