pca9698.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * (C) Copyright 2011
  3. * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Driver for NXP's pca9698 40 bit I2C gpio expander
  25. */
  26. #include <common.h>
  27. #include <i2c.h>
  28. #include <asm/errno.h>
  29. #include <pca9698.h>
  30. /*
  31. * The pca9698 registers
  32. */
  33. #define PCA9698_REG_INPUT 0x00
  34. #define PCA9698_REG_OUTPUT 0x08
  35. #define PCA9698_REG_POLARITY 0x10
  36. #define PCA9698_REG_CONFIG 0x18
  37. #define PCA9698_BUFFER_SIZE 5
  38. #define PCA9698_GPIO_COUNT 40
  39. static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
  40. {
  41. u8 command = offset | 0x80; /* autoincrement */
  42. return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
  43. }
  44. static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
  45. {
  46. u8 command = offset | 0x80; /* autoincrement */
  47. return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
  48. }
  49. static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
  50. {
  51. unsigned byte = gpio / 8;
  52. unsigned bit = gpio % 8;
  53. if (value)
  54. buffer[byte] |= (1 << bit);
  55. else
  56. buffer[byte] &= ~(1 << bit);
  57. }
  58. int pca9698_request(unsigned gpio, const char *label)
  59. {
  60. if (gpio >= PCA9698_GPIO_COUNT)
  61. return -EINVAL;
  62. return 0;
  63. }
  64. void pca9698_free(unsigned gpio)
  65. {
  66. }
  67. int pca9698_direction_input(u8 addr, unsigned gpio)
  68. {
  69. u8 data[PCA9698_BUFFER_SIZE];
  70. int res;
  71. res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
  72. if (res)
  73. return res;
  74. pca9698_set_bit(gpio, data, 1);
  75. return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
  76. }
  77. int pca9698_direction_output(u8 addr, unsigned gpio, int value)
  78. {
  79. u8 data[PCA9698_BUFFER_SIZE];
  80. int res;
  81. res = pca9698_set_value(addr, gpio, value);
  82. if (res)
  83. return res;
  84. res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
  85. if (res)
  86. return res;
  87. pca9698_set_bit(gpio, data, 0);
  88. return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
  89. }
  90. int pca9698_get_value(u8 addr, unsigned gpio)
  91. {
  92. unsigned config_byte = gpio / 8;
  93. unsigned config_bit = gpio % 8;
  94. unsigned value;
  95. u8 data[PCA9698_BUFFER_SIZE];
  96. int res;
  97. res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
  98. if (res)
  99. return -1;
  100. value = data[config_byte] & (1 << config_bit);
  101. return !!value;
  102. }
  103. int pca9698_set_value(u8 addr, unsigned gpio, int value)
  104. {
  105. u8 data[PCA9698_BUFFER_SIZE];
  106. int res;
  107. res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
  108. if (res)
  109. return res;
  110. pca9698_set_bit(gpio, data, value);
  111. return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);
  112. }