blob: 774450527ab3639dde04cee206b2d65cb95a7baf [file] [log] [blame]
Shiyu Sundc2c83b2020-03-19 16:59:52 +11001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2020 The Chromium OS Authors
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
Evgeny Zinovievd870a352020-12-05 03:33:44 +030020#include <fcntl.h>
Shiyu Sundc2c83b2020-03-19 16:59:52 +110021#include <sys/ioctl.h>
22#include <unistd.h>
23
24#include <stdlib.h>
25#include <linux/i2c.h>
26#include <linux/i2c-dev.h>
27
28#include "flash.h"
29#include "i2c_helper.h"
30
Angel Pons75b45362021-04-16 12:16:00 +020031/* Null characters are placeholders for bus number digits */
32#define I2C_DEV_PREFIX "/dev/i2c-\0\0\0"
Shiyu Sundc2c83b2020-03-19 16:59:52 +110033#define I2C_MAX_BUS 255
34
35int i2c_close(int fd)
36{
37 return fd == -1 ? 0 : close(fd);
38}
39
Peter Marheine0969e432021-03-31 11:28:11 +110040int i2c_open_path(const char *path, uint16_t addr, int force)
41{
42 int fd = open(path, O_RDWR);
43 if (fd < 0) {
44 msg_perr("Unable to open I2C device %s: %s.\n", path, strerror(errno));
45 return fd;
46 }
47
48 int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE;
49 int ret = ioctl(fd, request, addr);
50 if (ret < 0) {
51 msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
52 i2c_close(fd);
53 return ret;
54 }
55
56 return fd;
57}
58
59
Shiyu Sundc2c83b2020-03-19 16:59:52 +110060int i2c_open(int bus, uint16_t addr, int force)
61{
Angel Pons75b45362021-04-16 12:16:00 +020062 char dev[sizeof(I2C_DEV_PREFIX)] = {0};
63
Shiyu Sundc2c83b2020-03-19 16:59:52 +110064 int ret = -1;
Shiyu Sundc2c83b2020-03-19 16:59:52 +110065
66 if (bus < 0 || bus > I2C_MAX_BUS) {
67 msg_perr("Invalid I2C bus %d.\n", bus);
68 return ret;
69 }
70
Angel Pons75b45362021-04-16 12:16:00 +020071 ret = snprintf(dev, sizeof(dev), "%s%d", I2C_DEV_PREFIX, bus);
Shiyu Sundc2c83b2020-03-19 16:59:52 +110072 if (ret < 0) {
73 msg_perr("Unable to join bus number to device name: %s.\n", strerror(errno));
Angel Pons75b45362021-04-16 12:16:00 +020074 return ret;
Shiyu Sundc2c83b2020-03-19 16:59:52 +110075 }
76
Peter Marheine0969e432021-03-31 11:28:11 +110077 return i2c_open_path(dev, addr, force);
Shiyu Sundc2c83b2020-03-19 16:59:52 +110078}
79
80int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf)
81{
82 if (buf->len == 0)
83 return 0;
84
85 int ret = ioctl(fd, I2C_SLAVE, addr);
86 if (ret < 0) {
87 msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
88 return ret;
89 }
90
91 return read(fd, buf->buf, buf->len);
92}
93
94int i2c_write(int fd, uint16_t addr, const i2c_buffer_t *buf)
95{
96 if (buf->len == 0)
97 return 0;
98
99 int ret = ioctl(fd, I2C_SLAVE, addr);
100 if (ret < 0) {
101 msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
102 return ret;
103 }
104
105 return write(fd, buf->buf, buf->len);
106}