blob: 15f8f2a3b68ce0a67659216b545dbeb18145cb19 [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"
Angel Ponsdb232952021-05-02 18:56:45 +020030#include "programmer.h"
Shiyu Sundc2c83b2020-03-19 16:59:52 +110031
Angel Pons75b45362021-04-16 12:16:00 +020032/* Null characters are placeholders for bus number digits */
33#define I2C_DEV_PREFIX "/dev/i2c-\0\0\0"
Shiyu Sundc2c83b2020-03-19 16:59:52 +110034#define I2C_MAX_BUS 255
35
36int i2c_close(int fd)
37{
38 return fd == -1 ? 0 : close(fd);
39}
40
Peter Marheine0969e432021-03-31 11:28:11 +110041int i2c_open_path(const char *path, uint16_t addr, int force)
42{
43 int fd = open(path, O_RDWR);
44 if (fd < 0) {
45 msg_perr("Unable to open I2C device %s: %s.\n", path, strerror(errno));
46 return fd;
47 }
48
49 int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE;
50 int ret = ioctl(fd, request, addr);
51 if (ret < 0) {
52 msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
53 i2c_close(fd);
Angel Pons2bbc0702021-05-02 19:01:13 +020054 return ret;
Peter Marheine0969e432021-03-31 11:28:11 +110055 }
56
57 return fd;
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
Angel Ponsdb232952021-05-02 18:56:45 +020080static int get_bus_number(char *bus_str)
81{
82 char *bus_suffix;
83 errno = 0;
84 int bus = (int)strtol(bus_str, &bus_suffix, 10);
85 if (errno != 0 || bus_str == bus_suffix) {
86 msg_perr("%s: Could not convert 'bus'.\n", __func__);
87 return -1;
88 }
89
90 if (strlen(bus_suffix) > 0) {
91 msg_perr("%s: Garbage following 'bus' value.\n", __func__);
92 return -1;
93 }
94
95 msg_pinfo("Using I2C bus %d.\n", bus);
96 return bus;
97}
98
99int i2c_open_from_programmer_params(uint16_t addr, int force)
100{
101 int fd = -1;
102
103 char *bus_str = extract_programmer_param("bus");
104 char *device_path = extract_programmer_param("devpath");
105
106 if (device_path != NULL && bus_str != NULL) {
107 msg_perr("%s: only one of bus and devpath may be specified\n", __func__);
108 goto out;
109 }
110 if (device_path == NULL && bus_str == NULL) {
111 msg_perr("%s: one of bus and devpath must be specified\n", __func__);
112 goto out;
113 }
114
115 if (device_path != NULL)
116 fd = i2c_open_path(device_path, addr, force);
117 else
118 fd = i2c_open(get_bus_number(bus_str), addr, force);
119
120out:
121 free(bus_str);
122 free(device_path);
123 return fd;
124}
125
Shiyu Sundc2c83b2020-03-19 16:59:52 +1100126int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf)
127{
128 if (buf->len == 0)
129 return 0;
130
131 int ret = ioctl(fd, I2C_SLAVE, addr);
132 if (ret < 0) {
133 msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
134 return ret;
135 }
136
137 return read(fd, buf->buf, buf->len);
138}
139
140int i2c_write(int fd, uint16_t addr, const i2c_buffer_t *buf)
141{
142 if (buf->len == 0)
143 return 0;
144
145 int ret = ioctl(fd, I2C_SLAVE, addr);
146 if (ret < 0) {
147 msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
148 return ret;
149 }
150
151 return write(fd, buf->buf, buf->len);
152}