summaryrefslogtreecommitdiffstats
path: root/src/device/i2c_bus.c
blob: a65cdada895771dbfaed3327abc70d88d3c43e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* SPDX-License-Identifier: GPL-2.0-only */

#include <stdint.h>
#include <console/console.h>
#include <device/smbus.h>
#include <device/i2c_bus.h>
#include <commonlib/endian.h>

struct bus *i2c_link(struct device *const dev)
{
	if (!dev || !dev->bus)
		return NULL;

	struct bus *link = dev->bus;
	while (link) {
		struct device *const parent = link->dev;

		if (parent && parent->ops &&
		    (parent->ops->ops_i2c_bus || parent->ops->ops_smbus_bus))
			break;

		if (parent && parent->bus)
			link = parent->bus;
		else
			link = NULL;
	}

	if (!link) {
		printk(BIOS_ALERT, "%s Cannot find I2C or SMBus bus operations",
		       dev_path(dev));
	}

	return link;
}

int i2c_dev_readb(struct device *const dev)
{
	struct device *const busdev = i2c_busdev(dev);
	if (!busdev)
		return -1;

	if (busdev->ops->ops_i2c_bus) {
		uint8_t val;
		const struct i2c_msg msg = {
			.flags	= I2C_M_RD,
			.slave	= dev->path.i2c.device,
			.buf	= &val,
			.len	= sizeof(val),
		};

		const int ret = busdev->ops->ops_i2c_bus->
			transfer(busdev, &msg, 1);
		if (ret)
			return ret;
		else
			return val;
	} else if (busdev->ops->ops_smbus_bus->recv_byte) {
		return busdev->ops->ops_smbus_bus->recv_byte(dev);
	} else {
		printk(BIOS_ERR, "%s Missing ops_smbus_bus->recv_byte",
		       dev_path(busdev));
		return -1;
	}
}

int i2c_dev_writeb(struct device *const dev, uint8_t val)
{
	struct device *const busdev = i2c_busdev(dev);
	if (!busdev)
		return -1;

	if (busdev->ops->ops_i2c_bus) {
		const struct i2c_msg msg = {
			.flags	= 0,
			.slave	= dev->path.i2c.device,
			.buf	= &val,
			.len	= sizeof(val),
		};
		return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1);
	} else if (busdev->ops->ops_smbus_bus->send_byte) {
		return busdev->ops->ops_smbus_bus->send_byte(dev, val);
	} else {
		printk(BIOS_ERR, "%s Missing ops_smbus_bus->send_byte",
		       dev_path(busdev));
		return -1;
	}
}

int i2c_dev_readb_at(struct device *const dev, uint8_t off)
{
	struct device *const busdev = i2c_busdev(dev);
	if (!busdev)
		return -1;

	if (busdev->ops->ops_i2c_bus) {
		uint8_t val;
		const struct i2c_msg msg[] = {
			{
				.flags	= 0,
				.slave	= dev->path.i2c.device,
				.buf	= &off,
				.len	= sizeof(off),
			},
			{
				.flags	= I2C_M_RD,
				.slave	= dev->path.i2c.device,
				.buf	= &val,
				.len	= sizeof(val),
			},
		};

		const int ret = busdev->ops->ops_i2c_bus->
			transfer(busdev, msg, ARRAY_SIZE(msg));
		if (ret)
			return ret;
		else
			return val;
	} else if (busdev->ops->ops_smbus_bus->read_byte) {
		return busdev->ops->ops_smbus_bus->read_byte(dev, off);
	} else {
		printk(BIOS_ERR, "%s Missing ops_smbus_bus->read_byte",
		       dev_path(busdev));
		return -1;
	}
}

int i2c_dev_writeb_at(struct device *const dev,
			const uint8_t off, const uint8_t val)
{
	struct device *const busdev = i2c_busdev(dev);
	if (!busdev)
		return -1;

	if (busdev->ops->ops_i2c_bus) {
		uint8_t buf[] = { off, val };
		const struct i2c_msg msg = {
			.flags	= 0,
			.slave	= dev->path.i2c.device,
			.buf	= buf,
			.len	= sizeof(buf),
		};
		return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1);
	} else if (busdev->ops->ops_smbus_bus->write_byte) {
		return busdev->ops->ops_smbus_bus->write_byte(dev, off, val);
	} else {
		printk(BIOS_ERR, "%s Missing ops_smbus_bus->write_byte",
		       dev_path(busdev));
		return -1;
	}
}

int i2c_dev_read_at16(struct device *const dev,
		      uint8_t *const buf, const size_t len, uint16_t off)
{
	struct device *const busdev = i2c_busdev(dev);
	if (!busdev)
		return -1;

	if (busdev->ops->ops_i2c_bus) {
		const struct i2c_msg msg[] = {
			{
				.flags	= 0,
				.slave	= dev->path.i2c.device,
				.buf	= (uint8_t *)&off,
				.len	= sizeof(off),
			},
			{
				.flags	= I2C_M_RD,
				.slave	= dev->path.i2c.device,
				.buf	= buf,
				.len	= len,
			},
		};

		write_be16(&off, off);
		const int ret = busdev->ops->ops_i2c_bus->transfer(
					busdev, msg, ARRAY_SIZE(msg));
		if (ret)
			return ret;
		else
			return len;
	} else {
		printk(BIOS_ERR, "%s Missing ops_i2c_bus->transfer", dev_path(busdev));
		return -1;
	}
}