pca9698.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <pca9698.h>
  29. /*
  30. * The pca9698 registers
  31. */
  32. #define PCA9698_REG_INPUT 0x00
  33. #define PCA9698_REG_OUTPUT 0x08
  34. #define PCA9698_REG_POLARITY 0x10
  35. #define PCA9698_REG_CONFIG 0x18
  36. #define PCA9698_BUFFER_SIZE 5
  37. static int pca9698_read40(u8 chip, u8 offset, u8 *buffer)
  38. {
  39. u8 command = offset | 0x80; /* autoincrement */
  40. return i2c_read(chip, command, 1, buffer, PCA9698_BUFFER_SIZE);
  41. }
  42. static int pca9698_write40(u8 chip, u8 offset, u8 *buffer)
  43. {
  44. u8 command = offset | 0x80; /* autoincrement */
  45. return i2c_write(chip, command, 1, buffer, PCA9698_BUFFER_SIZE);
  46. }
  47. static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
  48. {
  49. unsigned byte = gpio / 8;
  50. unsigned bit = gpio % 8;
  51. if (value)
  52. buffer[byte] |= (1 << bit);
  53. else
  54. buffer[byte] &= ~(1 << bit);
  55. }
  56. int pca9698_direction_input(u8 chip, unsigned offset)
  57. {
  58. u8 data[PCA9698_BUFFER_SIZE];
  59. int res;
  60. res = pca9698_read40(chip, PCA9698_REG_CONFIG, data);
  61. if (res)
  62. return res;
  63. pca9698_set_bit(offset, data, 1);
  64. return pca9698_write40(chip, PCA9698_REG_CONFIG, data);
  65. }
  66. int pca9698_direction_output(u8 chip, unsigned offset)
  67. {
  68. u8 data[PCA9698_BUFFER_SIZE];
  69. int res;
  70. res = pca9698_read40(chip, PCA9698_REG_CONFIG, data);
  71. if (res)
  72. return res;
  73. pca9698_set_bit(offset, data, 0);
  74. return pca9698_write40(chip, PCA9698_REG_CONFIG, data);
  75. }
  76. int pca9698_get_input(u8 chip, unsigned offset)
  77. {
  78. unsigned config_byte = offset / 8;
  79. unsigned config_bit = offset % 8;
  80. unsigned value;
  81. u8 data[PCA9698_BUFFER_SIZE];
  82. int res;
  83. res = pca9698_read40(chip, PCA9698_REG_INPUT, data);
  84. if (res)
  85. return -1;
  86. value = data[config_byte] & (1 << config_bit);
  87. return !!value;
  88. }
  89. int pca9698_set_output(u8 chip, unsigned offset, int value)
  90. {
  91. u8 data[PCA9698_BUFFER_SIZE];
  92. int res;
  93. res = pca9698_read40(chip, PCA9698_REG_OUTPUT, data);
  94. if (res)
  95. return res;
  96. memset(data, sizeof(data), 0);
  97. pca9698_set_bit(offset, data, value);
  98. return pca9698_write40(chip, PCA9698_REG_OUTPUT, data);
  99. }