/ i2c-context.c
i2c-context.c
1 /* 2 * generic I2C slave access interface 3 * 4 * Copyright 2014-2016 Mikeqin <Fengling.Qin@gmail.com> 5 * Copyright 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com> 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 3 of the License, or (at your option) 10 * any later version. See COPYING for more details. 11 */ 12 13 #include <sys/ioctl.h> 14 #include <errno.h> 15 #include <string.h> 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <unistd.h> 19 #include <linux/i2c.h> 20 #include <linux/i2c-dev.h> 21 22 #include <stdint.h> 23 #include <stdbool.h> 24 #include <fcntl.h> 25 #include <assert.h> 26 27 #include "miner.h" 28 #include "i2c-context.h" 29 30 static bool i2c_slave_write(struct i2c_ctx *ctx, uint8_t reg, uint8_t val) 31 { 32 union i2c_smbus_data data; 33 data.byte = val; 34 35 struct i2c_smbus_ioctl_data args; 36 37 args.read_write = I2C_SMBUS_WRITE; 38 args.command = reg; 39 args.size = I2C_SMBUS_BYTE_DATA; 40 args.data = &data; 41 42 if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) { 43 applog(LOG_INFO, "i2c 0x%02x: failed to write to fdesc %d: %s", 44 ctx->addr, ctx->file, strerror(errno)); 45 return false; 46 } 47 applog(LOG_DEBUG, "I2C-W(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, val); 48 return true; 49 } 50 51 static bool i2c_slave_read(struct i2c_ctx *ctx, uint8_t reg, uint8_t *val) 52 { 53 union i2c_smbus_data data; 54 struct i2c_smbus_ioctl_data args; 55 56 args.read_write = I2C_SMBUS_READ; 57 args.command = reg; 58 args.size = I2C_SMBUS_BYTE_DATA; 59 args.data = &data; 60 61 if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) { 62 applog(LOG_INFO, "i2c 0x%02x: failed to read from fdesc %d: %s", 63 ctx->addr, ctx->file, strerror(errno)); 64 return false; 65 } 66 *val = data.byte; 67 applog(LOG_DEBUG, "I2C-R(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, *val); 68 return true; 69 } 70 71 static bool i2c_slave_write_raw(struct i2c_ctx *ctx, uint8_t *buf, uint32_t len) 72 { 73 /* SMBus cann't support write bytes > 32, use plain i2c write */ 74 if (len != write(ctx->file, buf, len)) { 75 applog(LOG_INFO, "i2c 0x%02x: failed to write raw to fdesc %d: %s", 76 ctx->addr, ctx->file, strerror(errno)); 77 return false; 78 } 79 applog(LOG_DEBUG, "I2C-W-RAW(0x%02x)", ctx->addr); 80 return true; 81 } 82 83 static bool i2c_slave_read_raw(struct i2c_ctx *ctx, uint8_t *buf, uint32_t len) 84 { 85 /* SMBus cann't support read bytes > 32, use plain i2c read */ 86 if (len != read(ctx->file, buf, len)) { 87 applog(LOG_INFO, "i2c 0x%02x: failed to read raw from fdesc %d: %s", 88 ctx->addr, ctx->file, strerror(errno)); 89 return false; 90 } 91 applog(LOG_DEBUG, "I2C-R-RAW(0x%02x)", ctx->addr); 92 return true; 93 } 94 95 static void i2c_slave_exit(struct i2c_ctx *ctx) 96 { 97 if (ctx->file == -1) 98 return; 99 close(ctx->file); 100 free(ctx); 101 } 102 103 extern struct i2c_ctx *i2c_slave_open(char *i2c_bus, uint8_t slave_addr) 104 { 105 int file = open(i2c_bus, O_RDWR); 106 if (file < 0) { 107 applog(LOG_INFO, "Failed to open %s: %s", i2c_bus, strerror(errno)); 108 return NULL; 109 } 110 111 if (ioctl(file, I2C_SLAVE, slave_addr) < 0) { 112 close(file); 113 return NULL; 114 } 115 struct i2c_ctx *ctx = malloc(sizeof(*ctx)); 116 assert(ctx != NULL); 117 118 ctx->addr = slave_addr; 119 ctx->file = file; 120 ctx->exit = i2c_slave_exit; 121 ctx->read = i2c_slave_read; 122 ctx->write = i2c_slave_write; 123 ctx->read_raw = i2c_slave_read_raw; 124 ctx->write_raw = i2c_slave_write_raw; 125 return ctx; 126 } 127