blob: d8a793c7167eb676db764ba0dc9c29316c9ac540 [file] [log] [blame]
David Hendricksf4c35082012-12-27 14:15:51 -08001/*
David Hendrickscfb73602013-04-05 13:42:39 -07002 * This file is part of the coreboot project.
David Hendricksf4c35082012-12-27 14:15:51 -08003 *
Gabe Blackcdb61a62014-04-07 18:45:14 -07004 * Copyright (C) 2014 Google, Inc.
David Hendricksf4c35082012-12-27 14:15:51 -08005 *
David Hendrickscfb73602013-04-05 13:42:39 -07006 * 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; version 2 of the License.
David Hendricksf4c35082012-12-27 14:15:51 -08009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
David Hendricksf4c35082012-12-27 14:15:51 -080014 */
15
David Hendrickscfb73602013-04-05 13:42:39 -070016#ifndef _DEVICE_I2C_H_
17#define _DEVICE_I2C_H_
David Hendricksf4c35082012-12-27 14:15:51 -080018
Gabe Blacka5dc0912013-06-30 03:47:33 -070019#include <stdint.h>
Gabe Blackcdb61a62014-04-07 18:45:14 -070020#include <stdlib.h>
Gabe Blacka5dc0912013-06-30 03:47:33 -070021
Duncan Laurie1010b4a2016-05-09 20:10:47 -070022enum i2c_speed {
23 I2C_SPEED_STANDARD = 100000,
24 I2C_SPEED_FAST = 400000,
25 I2C_SPEED_FAST_PLUS = 1000000,
26 I2C_SPEED_HIGH = 3400000,
27 I2C_SPEED_FAST_ULTRA = 5000000,
28};
29
30enum i2c_address_mode {
31 I2C_MODE_7_BIT,
32 I2C_MODE_10_BIT
33};
34
Gabe Blackcdb61a62014-04-07 18:45:14 -070035struct i2c_seg
36{
37 int read;
38 uint8_t chip;
39 uint8_t *buf;
40 int len;
41};
42
Julius Werner37d7ac82014-05-05 18:03:46 -070043int platform_i2c_transfer(unsigned bus, struct i2c_seg *segments, int count);
44
45#define SOFTWARE_I2C_MAX_BUS 10 /* increase as necessary */
46
47struct software_i2c_ops {
48 void (*set_sda)(unsigned bus, int high);
49 void (*set_scl)(unsigned bus, int high);
50 int (*get_sda)(unsigned bus);
51 int (*get_scl)(unsigned bus);
52};
53
54extern struct software_i2c_ops *software_i2c[];
55
56int software_i2c_transfer(unsigned bus, struct i2c_seg *segments, int count);
57void software_i2c_wedge_ack(unsigned bus, u8 chip);
58void software_i2c_wedge_read(unsigned bus, u8 chip, u8 reg, int bit_count);
59void software_i2c_wedge_write(unsigned bus, u8 chip, u8 reg, int bit_count);
60
Yidi Lind33ebd12016-03-15 14:38:44 +080061int i2c_read_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t *data,
62 uint8_t mask, uint8_t shift);
63int i2c_write_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t data,
64 uint8_t mask, uint8_t shift);
65
Julius Werner37d7ac82014-05-05 18:03:46 -070066/*
67 * software_i2c is supposed to be a debug feature. It's usually not compiled in,
68 * but when it is it can be dynamically enabled at runtime for certain busses.
69 * Need this ugly stub to arbitrate since I2C device drivers hardcode
70 * 'i2c_transfer()' as their entry point.
71 */
72static inline int i2c_transfer(unsigned bus, struct i2c_seg *segments,
73 int count)
74{
75 if (CONFIG_SOFTWARE_I2C)
76 if (bus < SOFTWARE_I2C_MAX_BUS && software_i2c[bus])
77 return software_i2c_transfer(bus, segments, count);
78
79 return platform_i2c_transfer(bus, segments, count);
80}
Gabe Blackcdb61a62014-04-07 18:45:14 -070081
82/*
83 * Read a raw chunk of data in one segment and one frame.
84 *
85 * [start][slave addr][r][data][stop]
86 */
87static inline int i2c_read_raw(unsigned bus, uint8_t chip, uint8_t *data,
88 int len)
89{
90 struct i2c_seg seg =
91 { .read = 1, .chip = chip, .buf = data, .len = len };
92 return i2c_transfer(bus, &seg, 1);
93}
94
95/*
96 * Write a raw chunk of data in one segment and one frame.
97 *
98 * [start][slave addr][w][data][stop]
99 */
100static inline int i2c_write_raw(unsigned bus, uint8_t chip, uint8_t *data,
101 int len)
102{
103 struct i2c_seg seg =
104 { .read = 0, .chip = chip, .buf = data, .len = len };
105 return i2c_transfer(bus, &seg, 1);
106}
107
108/**
Jitao Shi5ffb6882016-02-01 17:33:49 +0800109 * Read multi-bytes with two segments in one frame
110 *
111 * [start][slave addr][w][register addr][start][slave addr][r][data...][stop]
112 */
113static inline int i2c_read_bytes(unsigned bus, uint8_t chip, uint8_t reg,
114 uint8_t *data, int len)
115{
116 struct i2c_seg seg[2];
117
118 seg[0].read = 0;
119 seg[0].chip = chip;
120 seg[0].buf = &reg;
121 seg[0].len = 1;
122 seg[1].read = 1;
123 seg[1].chip = chip;
124 seg[1].buf = data;
125 seg[1].len = len;
126
127 return i2c_transfer(bus, seg, ARRAY_SIZE(seg));
128}
129
130/**
Gabe Blackcdb61a62014-04-07 18:45:14 -0700131 * Read a byte with two segments in one frame
132 *
133 * [start][slave addr][w][register addr][start][slave addr][r][data][stop]
134 */
135static inline int i2c_readb(unsigned bus, uint8_t chip, uint8_t reg,
136 uint8_t *data)
137{
138 struct i2c_seg seg[2];
139
140 seg[0].read = 0;
141 seg[0].chip = chip;
142 seg[0].buf = &reg;
143 seg[0].len = 1;
144 seg[1].read = 1;
145 seg[1].chip = chip;
146 seg[1].buf = data;
147 seg[1].len = 1;
148
149 return i2c_transfer(bus, seg, ARRAY_SIZE(seg));
150}
151
152/**
153 * Write a byte with one segment in one frame.
154 *
155 * [start][slave addr][w][register addr][data][stop]
156 */
157static inline int i2c_writeb(unsigned bus, uint8_t chip, uint8_t reg,
158 uint8_t data)
159{
160 struct i2c_seg seg;
161 uint8_t buf[] = {reg, data};
162
163 seg.read = 0;
164 seg.chip = chip;
165 seg.buf = buf;
166 seg.len = ARRAY_SIZE(buf);
167
168 return i2c_transfer(bus, &seg, 1);
169}
David Hendricksf4c35082012-12-27 14:15:51 -0800170
Duncan Laurieec009682016-06-07 15:38:14 -0700171/* I2C bus operation for ramstage drivers */
172struct device;
173struct i2c_bus_operations {
174 /*
175 * This is an SOC specific method that can be provided to translate the
176 * 'struct device' for an I2C controller into a unique I2C bus number.
177 * Returns -1 if the bus number for this device cannot be determined.
178 */
179 int (*dev_to_bus)(struct device *dev);
180};
181
182/* Return I2C bus number for provided device, -1 if not found */
183int i2c_dev_find_bus(struct device *dev);
184
185/* Variants of I2C helper functions that take a device instead of bus number */
186int i2c_dev_transfer(struct device *dev, struct i2c_seg *segments, int count);
187int i2c_dev_readb(struct device *dev, uint8_t reg, uint8_t *data);
188int i2c_dev_writeb(struct device *dev, uint8_t reg, uint8_t data);
189int i2c_dev_read_bytes(struct device *dev, uint8_t reg, uint8_t *data, int len);
190int i2c_dev_read_raw(struct device *dev, uint8_t *data, int len);
191int i2c_dev_write_raw(struct device *dev, uint8_t *data, int len);
192
David Hendrickscfb73602013-04-05 13:42:39 -0700193#endif /* _DEVICE_I2C_H_ */