tps65910-gpio.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * tps65910-gpio.c -- TI TPS6591x
  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, 1, &val);
  26. if (val & GPIO0_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,
  36. GPIO0_GPIO_SET_MASK);
  37. else
  38. tps65910_clear_bits(tps65910, TPS65910_GPIO0,
  39. GPIO0_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, GPIO0_GPIO_CFG_MASK);
  48. }
  49. static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
  50. {
  51. struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio);
  52. return tps65910_clear_bits(tps65910, TPS65910_GPIO0,
  53. GPIO0_GPIO_CFG_MASK);
  54. }
  55. void tps65910_gpio_init(struct tps65910 *tps65910, int gpio_base)
  56. {
  57. int ret;
  58. if (!gpio_base)
  59. return;
  60. tps65910->gpio.owner = THIS_MODULE;
  61. tps65910->gpio.label = tps65910->i2c_client->name;
  62. tps65910->gpio.dev = tps65910->dev;
  63. tps65910->gpio.base = gpio_base;
  64. tps65910->gpio.ngpio = 1;
  65. tps65910->gpio.can_sleep = 1;
  66. tps65910->gpio.direction_input = tps65910_gpio_input;
  67. tps65910->gpio.direction_output = tps65910_gpio_output;
  68. tps65910->gpio.set = tps65910_gpio_set;
  69. tps65910->gpio.get = tps65910_gpio_get;
  70. ret = gpiochip_add(&tps65910->gpio);
  71. if (ret)
  72. dev_warn(tps65910->dev, "GPIO registration failed: %d\n", ret);
  73. }