i2c-context.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * generic I2C slave access interface
  3. *
  4. * Copyright 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <sys/ioctl.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <linux/i2c.h>
  18. #include <linux/i2c-dev.h>
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include <fcntl.h>
  22. #include <assert.h>
  23. #include "miner.h"
  24. #include "i2c-context.h"
  25. static bool i2c_slave_write(struct i2c_ctx *ctx, uint8_t reg, uint8_t val)
  26. {
  27. union i2c_smbus_data data;
  28. data.byte = val;
  29. struct i2c_smbus_ioctl_data args;
  30. args.read_write = I2C_SMBUS_WRITE;
  31. args.command = reg;
  32. args.size = I2C_SMBUS_BYTE_DATA;
  33. args.data = &data;
  34. if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) {
  35. applog(LOG_INFO, "i2c 0x%02x: failed to write to fdesc %d: %s",
  36. ctx->addr, ctx->file, strerror(errno));
  37. return false;
  38. }
  39. applog(LOG_DEBUG, "I2C-W(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, val);
  40. return true;
  41. }
  42. static bool i2c_slave_read(struct i2c_ctx *ctx, uint8_t reg, uint8_t *val)
  43. {
  44. union i2c_smbus_data data;
  45. struct i2c_smbus_ioctl_data args;
  46. args.read_write = I2C_SMBUS_READ;
  47. args.command = reg;
  48. args.size = I2C_SMBUS_BYTE_DATA;
  49. args.data = &data;
  50. if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) {
  51. applog(LOG_INFO, "i2c 0x%02x: failed to read from fdesc %d: %s",
  52. ctx->addr, ctx->file, strerror(errno));
  53. return false;
  54. }
  55. *val = data.byte;
  56. applog(LOG_DEBUG, "I2C-R(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, *val);
  57. return true;
  58. }
  59. static void i2c_slave_exit(struct i2c_ctx *ctx)
  60. {
  61. if (ctx->file == -1)
  62. return;
  63. close(ctx->file);
  64. free(ctx);
  65. }
  66. extern struct i2c_ctx *i2c_slave_open(char *i2c_bus, uint8_t slave_addr)
  67. {
  68. int file = open(i2c_bus, O_RDWR);
  69. if (file < 0) {
  70. applog(LOG_INFO, "Failed to open i2c-1: %s", strerror(errno));
  71. return NULL;
  72. }
  73. if (ioctl(file, I2C_SLAVE, slave_addr) < 0) {
  74. close(file);
  75. return NULL;
  76. }
  77. struct i2c_ctx *ctx = malloc(sizeof(*ctx));
  78. assert(ctx != NULL);
  79. ctx->addr = slave_addr;
  80. ctx->file = file;
  81. ctx->exit = i2c_slave_exit;
  82. ctx->read = i2c_slave_read;
  83. ctx->write = i2c_slave_write;
  84. return ctx;
  85. }