David Hendricks | f4c3508 | 2012-12-27 14:15:51 -0800 | [diff] [blame] | 1 | /* |
David Hendricks | cfb7360 | 2013-04-05 13:42:39 -0700 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
David Hendricks | f4c3508 | 2012-12-27 14:15:51 -0800 | [diff] [blame] | 3 | * |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 4 | * Copyright (C) 2014 Google, Inc. |
David Hendricks | f4c3508 | 2012-12-27 14:15:51 -0800 | [diff] [blame] | 5 | * |
David Hendricks | cfb7360 | 2013-04-05 13:42:39 -0700 | [diff] [blame] | 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; version 2 of the License. |
David Hendricks | f4c3508 | 2012-12-27 14:15:51 -0800 | [diff] [blame] | 9 | * |
| 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 Hendricks | f4c3508 | 2012-12-27 14:15:51 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
David Hendricks | cfb7360 | 2013-04-05 13:42:39 -0700 | [diff] [blame] | 16 | #ifndef _DEVICE_I2C_H_ |
| 17 | #define _DEVICE_I2C_H_ |
David Hendricks | f4c3508 | 2012-12-27 14:15:51 -0800 | [diff] [blame] | 18 | |
Gabe Black | a5dc091 | 2013-06-30 03:47:33 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Gabe Black | a5dc091 | 2013-06-30 03:47:33 -0700 | [diff] [blame] | 21 | |
Duncan Laurie | 1010b4a | 2016-05-09 20:10:47 -0700 | [diff] [blame] | 22 | enum 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 | |
| 30 | enum i2c_address_mode { |
| 31 | I2C_MODE_7_BIT, |
| 32 | I2C_MODE_10_BIT |
| 33 | }; |
| 34 | |
Lee Leahy | 6625ecc | 2017-03-07 15:11:07 -0800 | [diff] [blame^] | 35 | struct i2c_seg { |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 36 | int read; |
| 37 | uint8_t chip; |
| 38 | uint8_t *buf; |
| 39 | int len; |
| 40 | }; |
| 41 | |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 42 | int platform_i2c_transfer(unsigned int bus, struct i2c_seg *segments, |
| 43 | int count); |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 44 | |
| 45 | #define SOFTWARE_I2C_MAX_BUS 10 /* increase as necessary */ |
| 46 | |
| 47 | struct software_i2c_ops { |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 48 | void (*set_sda)(unsigned int bus, int high); |
| 49 | void (*set_scl)(unsigned int bus, int high); |
| 50 | int (*get_sda)(unsigned int bus); |
| 51 | int (*get_scl)(unsigned int bus); |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | extern struct software_i2c_ops *software_i2c[]; |
| 55 | |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 56 | int software_i2c_transfer(unsigned int bus, struct i2c_seg *segments, |
| 57 | int count); |
| 58 | void software_i2c_wedge_ack(unsigned int bus, u8 chip); |
| 59 | void software_i2c_wedge_read(unsigned int bus, u8 chip, u8 reg, int bit_count); |
| 60 | void software_i2c_wedge_write(unsigned int bus, u8 chip, u8 reg, int bit_count); |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 61 | |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 62 | int i2c_read_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t *data, |
Yidi Lin | d33ebd1 | 2016-03-15 14:38:44 +0800 | [diff] [blame] | 63 | uint8_t mask, uint8_t shift); |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 64 | int i2c_write_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t data, |
Yidi Lin | d33ebd1 | 2016-03-15 14:38:44 +0800 | [diff] [blame] | 65 | uint8_t mask, uint8_t shift); |
| 66 | |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 67 | /* |
| 68 | * software_i2c is supposed to be a debug feature. It's usually not compiled in, |
| 69 | * but when it is it can be dynamically enabled at runtime for certain busses. |
| 70 | * Need this ugly stub to arbitrate since I2C device drivers hardcode |
| 71 | * 'i2c_transfer()' as their entry point. |
| 72 | */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 73 | static inline int i2c_transfer(unsigned int bus, struct i2c_seg *segments, |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 74 | int count) |
| 75 | { |
| 76 | if (CONFIG_SOFTWARE_I2C) |
| 77 | if (bus < SOFTWARE_I2C_MAX_BUS && software_i2c[bus]) |
| 78 | return software_i2c_transfer(bus, segments, count); |
| 79 | |
| 80 | return platform_i2c_transfer(bus, segments, count); |
| 81 | } |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Read a raw chunk of data in one segment and one frame. |
| 85 | * |
| 86 | * [start][slave addr][r][data][stop] |
| 87 | */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 88 | static inline int i2c_read_raw(unsigned int bus, uint8_t chip, uint8_t *data, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 89 | int len) |
| 90 | { |
Lee Leahy | 6625ecc | 2017-03-07 15:11:07 -0800 | [diff] [blame^] | 91 | struct i2c_seg seg = { |
| 92 | .read = 1, .chip = chip, .buf = data, .len = len |
| 93 | }; |
| 94 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 95 | return i2c_transfer(bus, &seg, 1); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Write a raw chunk of data in one segment and one frame. |
| 100 | * |
| 101 | * [start][slave addr][w][data][stop] |
| 102 | */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 103 | static inline int i2c_write_raw(unsigned int bus, uint8_t chip, uint8_t *data, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 104 | int len) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 105 | { |
Lee Leahy | 6625ecc | 2017-03-07 15:11:07 -0800 | [diff] [blame^] | 106 | struct i2c_seg seg = { |
| 107 | .read = 0, .chip = chip, .buf = data, .len = len |
| 108 | }; |
| 109 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 110 | return i2c_transfer(bus, &seg, 1); |
| 111 | } |
| 112 | |
| 113 | /** |
Jitao Shi | 5ffb688 | 2016-02-01 17:33:49 +0800 | [diff] [blame] | 114 | * Read multi-bytes with two segments in one frame |
| 115 | * |
| 116 | * [start][slave addr][w][register addr][start][slave addr][r][data...][stop] |
| 117 | */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 118 | static inline int i2c_read_bytes(unsigned int bus, uint8_t chip, uint8_t reg, |
Jitao Shi | 5ffb688 | 2016-02-01 17:33:49 +0800 | [diff] [blame] | 119 | uint8_t *data, int len) |
| 120 | { |
| 121 | struct i2c_seg seg[2]; |
| 122 | |
| 123 | seg[0].read = 0; |
| 124 | seg[0].chip = chip; |
| 125 | seg[0].buf = ® |
| 126 | seg[0].len = 1; |
| 127 | seg[1].read = 1; |
| 128 | seg[1].chip = chip; |
| 129 | seg[1].buf = data; |
| 130 | seg[1].len = len; |
| 131 | |
| 132 | return i2c_transfer(bus, seg, ARRAY_SIZE(seg)); |
| 133 | } |
| 134 | |
| 135 | /** |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 136 | * Read a byte with two segments in one frame |
| 137 | * |
| 138 | * [start][slave addr][w][register addr][start][slave addr][r][data][stop] |
| 139 | */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 140 | static inline int i2c_readb(unsigned int bus, uint8_t chip, uint8_t reg, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 141 | uint8_t *data) |
| 142 | { |
| 143 | struct i2c_seg seg[2]; |
| 144 | |
| 145 | seg[0].read = 0; |
| 146 | seg[0].chip = chip; |
| 147 | seg[0].buf = ® |
| 148 | seg[0].len = 1; |
| 149 | seg[1].read = 1; |
| 150 | seg[1].chip = chip; |
| 151 | seg[1].buf = data; |
| 152 | seg[1].len = 1; |
| 153 | |
| 154 | return i2c_transfer(bus, seg, ARRAY_SIZE(seg)); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Write a byte with one segment in one frame. |
| 159 | * |
| 160 | * [start][slave addr][w][register addr][data][stop] |
| 161 | */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 162 | static inline int i2c_writeb(unsigned int bus, uint8_t chip, uint8_t reg, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 163 | uint8_t data) |
| 164 | { |
| 165 | struct i2c_seg seg; |
| 166 | uint8_t buf[] = {reg, data}; |
| 167 | |
| 168 | seg.read = 0; |
| 169 | seg.chip = chip; |
| 170 | seg.buf = buf; |
| 171 | seg.len = ARRAY_SIZE(buf); |
| 172 | |
| 173 | return i2c_transfer(bus, &seg, 1); |
| 174 | } |
David Hendricks | f4c3508 | 2012-12-27 14:15:51 -0800 | [diff] [blame] | 175 | |
Duncan Laurie | ec00968 | 2016-06-07 15:38:14 -0700 | [diff] [blame] | 176 | /* I2C bus operation for ramstage drivers */ |
| 177 | struct device; |
| 178 | struct i2c_bus_operations { |
| 179 | /* |
| 180 | * This is an SOC specific method that can be provided to translate the |
| 181 | * 'struct device' for an I2C controller into a unique I2C bus number. |
| 182 | * Returns -1 if the bus number for this device cannot be determined. |
| 183 | */ |
| 184 | int (*dev_to_bus)(struct device *dev); |
| 185 | }; |
| 186 | |
| 187 | /* Return I2C bus number for provided device, -1 if not found */ |
| 188 | int i2c_dev_find_bus(struct device *dev); |
| 189 | |
| 190 | /* Variants of I2C helper functions that take a device instead of bus number */ |
| 191 | int i2c_dev_transfer(struct device *dev, struct i2c_seg *segments, int count); |
| 192 | int i2c_dev_readb(struct device *dev, uint8_t reg, uint8_t *data); |
| 193 | int i2c_dev_writeb(struct device *dev, uint8_t reg, uint8_t data); |
| 194 | int i2c_dev_read_bytes(struct device *dev, uint8_t reg, uint8_t *data, int len); |
| 195 | int i2c_dev_read_raw(struct device *dev, uint8_t *data, int len); |
| 196 | int i2c_dev_write_raw(struct device *dev, uint8_t *data, int len); |
| 197 | |
David Hendricks | cfb7360 | 2013-04-05 13:42:39 -0700 | [diff] [blame] | 198 | #endif /* _DEVICE_I2C_H_ */ |