ddbridge-i2c.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * ddbridge-i2c.c: Digital Devices bridge i2c driver
  3. *
  4. * Copyright (C) 2010-2017 Digital Devices GmbH
  5. * Ralph Metzler <rjkm@metzlerbros.de>
  6. * Marcus Metzler <mocm@metzlerbros.de>
  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
  10. * version 2 only, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #ifndef __DDBRIDGE_I2C_H__
  19. #define __DDBRIDGE_I2C_H__
  20. #include <linux/i2c.h>
  21. #include "ddbridge.h"
  22. /******************************************************************************/
  23. void ddb_i2c_release(struct ddb *dev);
  24. int ddb_i2c_init(struct ddb *dev);
  25. /******************************************************************************/
  26. static int __maybe_unused i2c_io(struct i2c_adapter *adapter, u8 adr,
  27. u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen)
  28. {
  29. struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0,
  30. .buf = wbuf, .len = wlen },
  31. { .addr = adr, .flags = I2C_M_RD,
  32. .buf = rbuf, .len = rlen } };
  33. return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
  34. }
  35. static int __maybe_unused i2c_write(struct i2c_adapter *adap, u8 adr,
  36. u8 *data, int len)
  37. {
  38. struct i2c_msg msg = { .addr = adr, .flags = 0,
  39. .buf = data, .len = len };
  40. return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1;
  41. }
  42. static int __maybe_unused i2c_read(struct i2c_adapter *adapter, u8 adr, u8 *val)
  43. {
  44. struct i2c_msg msgs[1] = { { .addr = adr, .flags = I2C_M_RD,
  45. .buf = val, .len = 1 } };
  46. return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1;
  47. }
  48. static int __maybe_unused i2c_read_regs(struct i2c_adapter *adapter,
  49. u8 adr, u8 reg, u8 *val, u8 len)
  50. {
  51. struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0,
  52. .buf = &reg, .len = 1 },
  53. { .addr = adr, .flags = I2C_M_RD,
  54. .buf = val, .len = len } };
  55. return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
  56. }
  57. static int __maybe_unused i2c_read_regs16(struct i2c_adapter *adapter,
  58. u8 adr, u16 reg, u8 *val, u8 len)
  59. {
  60. u8 msg[2] = { reg >> 8, reg & 0xff };
  61. struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0,
  62. .buf = msg, .len = 2 },
  63. { .addr = adr, .flags = I2C_M_RD,
  64. .buf = val, .len = len } };
  65. return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
  66. }
  67. static int __maybe_unused i2c_write_reg16(struct i2c_adapter *adap,
  68. u8 adr, u16 reg, u8 val)
  69. {
  70. u8 msg[3] = { reg >> 8, reg & 0xff, val };
  71. return i2c_write(adap, adr, msg, 3);
  72. }
  73. static int __maybe_unused i2c_write_reg(struct i2c_adapter *adap,
  74. u8 adr, u8 reg, u8 val)
  75. {
  76. u8 msg[2] = { reg, val };
  77. return i2c_write(adap, adr, msg, 2);
  78. }
  79. static int __maybe_unused i2c_read_reg16(struct i2c_adapter *adapter,
  80. u8 adr, u16 reg, u8 *val)
  81. {
  82. return i2c_read_regs16(adapter, adr, reg, val, 1);
  83. }
  84. static int __maybe_unused i2c_read_reg(struct i2c_adapter *adapter,
  85. u8 adr, u8 reg, u8 *val)
  86. {
  87. return i2c_read_regs(adapter, adr, reg, val, 1);
  88. }
  89. #endif /* __DDBRIDGE_I2C_H__ */