i2c_exec.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* $OpenBSD: i2c_exec.c,v 1.3 2015/03/14 03:38:47 jsg Exp $ */
  2. /* $NetBSD: i2c_exec.c,v 1.3 2003/10/29 00:34:58 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 2003 Wasabi Systems, Inc.
  5. * All rights reserved.
  6. *
  7. * Written by Jason R. Thorpe for Wasabi Systems, Inc.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed for the NetBSD Project by
  20. * Wasabi Systems, Inc.
  21. * 4. The name of Wasabi Systems, Inc. may not be used to endorse
  22. * or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  27. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
  29. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/device.h>
  40. #include <sys/event.h>
  41. #define _I2C_PRIVATE
  42. #include <dev/i2c/i2cvar.h>
  43. /*
  44. * iic_exec:
  45. *
  46. * Simplified I2C client interface engine.
  47. *
  48. * This and the SMBus routines are the preferred interface for
  49. * client access to I2C/SMBus, since many automated controllers
  50. * do not provide access to the low-level primitives of the I2C
  51. * bus protocol.
  52. */
  53. int
  54. iic_exec(i2c_tag_t tag, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
  55. size_t cmdlen, void *vbuf, size_t buflen, int flags)
  56. {
  57. const uint8_t *cmd = vcmd;
  58. uint8_t *buf = vbuf;
  59. int error;
  60. size_t len;
  61. /*
  62. * Defer to the controller if it provides an exec function. Use
  63. * it if it does.
  64. */
  65. if (tag->ic_exec != NULL)
  66. return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd,
  67. cmdlen, buf, buflen, flags));
  68. if ((len = cmdlen) != 0) {
  69. if ((error = iic_initiate_xfer(tag, addr, flags)) != 0)
  70. goto bad;
  71. while (len--) {
  72. if ((error = iic_write_byte(tag, *cmd++, flags)) != 0)
  73. goto bad;
  74. }
  75. }
  76. if (I2C_OP_READ_P(op))
  77. flags |= I2C_F_READ;
  78. len = buflen;
  79. while (len--) {
  80. if (len == 0 && I2C_OP_STOP_P(op))
  81. flags |= I2C_F_STOP;
  82. if (I2C_OP_READ_P(op)) {
  83. /* Send REPEATED START. */
  84. if ((len + 1) == buflen &&
  85. (error = iic_initiate_xfer(tag, addr, flags)) != 0)
  86. goto bad;
  87. /* NACK on last byte. */
  88. if (len == 0)
  89. flags |= I2C_F_LAST;
  90. if ((error = iic_read_byte(tag, buf++, flags)) != 0)
  91. goto bad;
  92. } else {
  93. /* Maybe send START. */
  94. if ((len + 1) == buflen && cmdlen == 0 &&
  95. (error = iic_initiate_xfer(tag, addr, flags)) != 0)
  96. goto bad;
  97. if ((error = iic_write_byte(tag, *buf++, flags)) != 0)
  98. goto bad;
  99. }
  100. }
  101. return (0);
  102. bad:
  103. iic_send_stop(tag, flags);
  104. return (error);
  105. }
  106. /*
  107. * iic_smbus_write_byte:
  108. *
  109. * Perform an SMBus "write byte" operation.
  110. */
  111. int
  112. iic_smbus_write_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
  113. uint8_t val, int flags)
  114. {
  115. return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr,
  116. &cmd, sizeof cmd, &val, sizeof val, flags));
  117. }
  118. /*
  119. * iic_smbus_read_byte:
  120. *
  121. * Perform an SMBus "read byte" operation.
  122. */
  123. int
  124. iic_smbus_read_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
  125. uint8_t *valp, int flags)
  126. {
  127. return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr,
  128. &cmd, sizeof cmd, valp, sizeof (*valp), flags));
  129. }
  130. /*
  131. * iic_smbus_receive_byte:
  132. *
  133. * Perform an SMBus "receive byte" operation.
  134. */
  135. int
  136. iic_smbus_receive_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t *valp,
  137. int flags)
  138. {
  139. return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr,
  140. NULL, 0, valp, sizeof (*valp), flags));
  141. }