rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* vim: set expandtab ts=4 sw=4 tw=100: */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 Google Inc. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files |
| 7 | * (the "Software"), to deal in the Software without restriction, |
| 8 | * including without limitation the rights to use, copy, modify, merge, |
| 9 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 10 | * and to permit persons to whom the Software is furnished to do so, |
| 11 | * subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be |
| 14 | * included in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <compiler.h> |
| 27 | #include <debug.h> |
| 28 | #include <printf.h> |
| 29 | #include <err.h> |
| 30 | |
| 31 | #include <dev/gpio.h> |
| 32 | #include <dev/gpio_i2c.h> |
| 33 | #include <kernel/mutex.h> |
| 34 | |
| 35 | #if (!(defined(GPIO_I2C_BUS_COUNT)) || (GPIO_I2C_BUS_COUNT <= 0)) |
| 36 | #error ERROR: Must define GPIO_I2C_BUS_COUNT |
| 37 | #endif |
| 38 | |
| 39 | typedef struct gpio_i2c_state { |
| 40 | mutex_t lock; |
| 41 | const gpio_i2c_info_t* info; |
| 42 | } gpio_i2c_state_t; |
| 43 | |
| 44 | static gpio_i2c_state_t gpio_i2c_states[GPIO_I2C_BUS_COUNT]; |
| 45 | |
| 46 | /****************************************************************************** |
| 47 | * |
| 48 | * Internal implementation. |
| 49 | * |
| 50 | ******************************************************************************/ |
| 51 | static inline void send_start(const gpio_i2c_info_t* i) |
| 52 | { |
| 53 | gpio_config(i->sda, GPIO_OUTPUT); |
| 54 | spin_cycles(i->qcd); |
| 55 | gpio_config(i->scl, GPIO_OUTPUT); |
| 56 | spin_cycles(i->hcd); |
| 57 | } |
| 58 | |
| 59 | static inline void send_stop(const gpio_i2c_info_t* i) |
| 60 | { |
| 61 | gpio_config(i->sda, GPIO_OUTPUT); |
| 62 | gpio_config(i->scl, GPIO_INPUT); |
| 63 | spin_cycles(i->qcd); |
| 64 | gpio_config(i->sda, GPIO_INPUT); |
| 65 | } |
| 66 | |
| 67 | static inline void send_restart(const gpio_i2c_info_t* i) |
| 68 | { |
| 69 | gpio_config(i->scl, GPIO_INPUT); |
| 70 | spin_cycles(i->qcd); |
| 71 | send_start(i); |
| 72 | } |
| 73 | |
| 74 | static inline void send_nack(const gpio_i2c_info_t* i) |
| 75 | { |
| 76 | spin_cycles(i->hcd); |
| 77 | gpio_config(i->scl, GPIO_INPUT); |
| 78 | spin_cycles(i->hcd); |
| 79 | gpio_config(i->scl, GPIO_OUTPUT); |
| 80 | gpio_config(i->sda, GPIO_INPUT); |
| 81 | } |
| 82 | |
| 83 | static inline void send_ack(const gpio_i2c_info_t* i) |
| 84 | { |
| 85 | gpio_config(i->sda, GPIO_OUTPUT); |
| 86 | send_nack(i); |
| 87 | } |
| 88 | |
| 89 | static inline bool send_byte(const gpio_i2c_info_t* i, uint32_t b) |
| 90 | { |
| 91 | bool ret; |
| 92 | |
| 93 | for (size_t j = 0; j < 8; ++j) { |
| 94 | if (b & 0x80) |
| 95 | gpio_config(i->sda, GPIO_INPUT); |
| 96 | else |
| 97 | gpio_config(i->sda, GPIO_OUTPUT); |
| 98 | b <<= 1; |
| 99 | /* setup time for data (the time between when data becomes stable and |
| 100 | * clock becomes a stable high) is spec'ed to be 250ns for 100KHz i2c |
| 101 | * and 100nsec for 400KHz i2c. If any micro running LK needs to spin |
| 102 | * here in order to hit that timing, they are welcome to add a spin |
| 103 | * right here. |
| 104 | */ |
| 105 | spin_cycles(i->hcd); |
| 106 | gpio_config(i->scl, GPIO_INPUT); |
| 107 | spin_cycles(i->hcd); |
| 108 | gpio_config(i->scl, GPIO_OUTPUT); |
| 109 | } |
| 110 | |
| 111 | gpio_config(i->sda, GPIO_INPUT); |
| 112 | spin_cycles(i->hcd); |
| 113 | gpio_config(i->scl, GPIO_INPUT); |
| 114 | spin_cycles(i->hcd); |
| 115 | ret = (0 == gpio_get(i->sda)); |
| 116 | gpio_config(i->scl, GPIO_OUTPUT); |
| 117 | spin_cycles(i->hcd); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static inline void recv_byte(const gpio_i2c_info_t* i, uint8_t* b) |
| 123 | { |
| 124 | uint32_t tmp = 0; |
| 125 | |
| 126 | for (size_t j = 0; j < 7; ++j) { |
| 127 | gpio_config(i->scl, GPIO_INPUT); |
| 128 | spin_cycles(i->hcd); |
| 129 | if (gpio_get(i->sda)) |
| 130 | tmp |= 1; |
| 131 | tmp <<= 1; |
| 132 | gpio_config(i->scl, GPIO_OUTPUT); |
| 133 | spin_cycles(i->hcd); |
| 134 | } |
| 135 | |
| 136 | gpio_config(i->scl, GPIO_INPUT); |
| 137 | spin_cycles(i->hcd); |
| 138 | if (gpio_get(i->sda)) |
| 139 | tmp |= 1; |
| 140 | gpio_config(i->scl, GPIO_OUTPUT); |
| 141 | |
| 142 | *b = (uint8_t)tmp; |
| 143 | } |
| 144 | |
| 145 | static status_t gpio_i2c_tx_common(gpio_i2c_state_t* s, |
| 146 | uint8_t address, |
| 147 | const uint8_t* reg, |
| 148 | const void* buf, |
| 149 | size_t cnt) |
| 150 | { |
| 151 | const gpio_i2c_info_t* i = s->info; |
| 152 | status_t ret = ERR_I2C_NACK; |
| 153 | |
| 154 | DEBUG_ASSERT(buf || !cnt); |
| 155 | |
| 156 | mutex_acquire(&s->lock); |
| 157 | send_start(i); |
| 158 | if (!send_byte(i, address << 1)) |
| 159 | goto finished; |
| 160 | |
| 161 | if ((NULL != reg) && !send_byte(i, *reg)) |
| 162 | goto finished; |
| 163 | |
| 164 | for (size_t j = 0; j < cnt; ++j) |
| 165 | if (!send_byte(i, ((const uint8_t*)buf)[j])) |
| 166 | goto finished; |
| 167 | |
| 168 | ret = NO_ERROR; |
| 169 | |
| 170 | finished: |
| 171 | send_stop(i); |
| 172 | mutex_release(&s->lock); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | static status_t gpio_i2c_rx_common(gpio_i2c_state_t* s, |
| 177 | uint8_t address, |
| 178 | const uint8_t* reg, |
| 179 | void* buf, |
| 180 | size_t cnt) |
| 181 | { |
| 182 | const gpio_i2c_info_t* i = s->info; |
| 183 | status_t ret = ERR_I2C_NACK; |
| 184 | |
| 185 | DEBUG_ASSERT(buf && cnt); |
| 186 | |
| 187 | address <<= 1; |
| 188 | |
| 189 | mutex_acquire(&s->lock); |
| 190 | send_start(i); |
| 191 | if (!send_byte(i, address | (!reg ? 0x1 : 0x0))) |
| 192 | goto finished; |
| 193 | |
| 194 | if (NULL != reg) { |
| 195 | if (!send_byte(i, *reg)) |
| 196 | goto finished; |
| 197 | |
| 198 | send_restart(i); |
| 199 | |
| 200 | if (!send_byte(i, address | 0x1)) |
| 201 | goto finished; |
| 202 | } |
| 203 | |
| 204 | recv_byte(i, buf++); |
| 205 | for (size_t j = 0; j < (cnt - 1); ++j) { |
| 206 | send_ack(i); |
| 207 | recv_byte(i, buf++); |
| 208 | } |
| 209 | send_nack(i); |
| 210 | ret = NO_ERROR; |
| 211 | |
| 212 | finished: |
| 213 | send_stop(i); |
| 214 | mutex_release(&s->lock); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | void gpio_i2c_add_bus(uint32_t bus_id, const gpio_i2c_info_t* info) |
| 219 | { |
| 220 | gpio_i2c_state_t* s = gpio_i2c_states + bus_id; |
| 221 | |
| 222 | DEBUG_ASSERT(info); |
| 223 | DEBUG_ASSERT(bus_id < GPIO_I2C_BUS_COUNT); |
| 224 | DEBUG_ASSERT(!s->info); |
| 225 | |
| 226 | gpio_config(info->scl, GPIO_INPUT); |
| 227 | gpio_config(info->sda, GPIO_INPUT); |
| 228 | gpio_set(info->scl, 0); |
| 229 | gpio_set(info->sda, 0); |
| 230 | |
| 231 | mutex_init(&s->lock); |
| 232 | s->info = info; |
| 233 | } |
| 234 | |
| 235 | /****************************************************************************** |
| 236 | * |
| 237 | * LK facing API |
| 238 | * |
| 239 | * *****************************************************************************/ |
| 240 | void gpio_i2c_init_early(void) { } |
| 241 | void gpio_i2c_init(void) { } |
| 242 | |
| 243 | status_t gpio_i2c_transmit(int bus, uint8_t address, const void* buf, size_t cnt) |
| 244 | { |
| 245 | gpio_i2c_state_t* s = gpio_i2c_states + bus; |
| 246 | if (((unsigned)bus >= countof(gpio_i2c_states)) || !s->info) |
| 247 | return ERR_NOT_FOUND; |
| 248 | |
| 249 | return gpio_i2c_tx_common(s, address, NULL, buf, cnt); |
| 250 | } |
| 251 | |
| 252 | status_t gpio_i2c_receive(int bus, uint8_t address, void* buf, size_t cnt) |
| 253 | { |
| 254 | gpio_i2c_state_t* s = gpio_i2c_states + bus; |
| 255 | if (((unsigned)bus >= countof(gpio_i2c_states)) || !s->info) |
| 256 | return ERR_NOT_FOUND; |
| 257 | |
| 258 | return gpio_i2c_rx_common(s, address, NULL, buf, cnt); |
| 259 | } |
| 260 | |
| 261 | status_t gpio_i2c_write_reg_bytes(int bus, uint8_t address, uint8_t reg, const uint8_t* buf, size_t cnt) |
| 262 | { |
| 263 | gpio_i2c_state_t* s = gpio_i2c_states + bus; |
| 264 | if (((unsigned)bus >= countof(gpio_i2c_states)) || !s->info) |
| 265 | return ERR_NOT_FOUND; |
| 266 | |
| 267 | return gpio_i2c_tx_common(s, address, ®, buf, cnt); |
| 268 | } |
| 269 | |
| 270 | status_t gpio_i2c_read_reg_bytes(int bus, uint8_t address, uint8_t reg, uint8_t* buf, size_t cnt) |
| 271 | { |
| 272 | gpio_i2c_state_t* s = gpio_i2c_states + bus; |
| 273 | if (((unsigned)bus >= countof(gpio_i2c_states)) || !s->info) { |
| 274 | return ERR_NOT_FOUND; |
| 275 | } |
| 276 | |
| 277 | return gpio_i2c_rx_common(s, address, ®, buf, cnt); |
| 278 | } |
| 279 | |
| 280 | void i2c_init_early(void) __WEAK_ALIAS("gpio_i2c_init_early"); |
| 281 | void i2c_init(void) __WEAK_ALIAS("gpio_i2c_init"); |
| 282 | status_t i2c_transmit(int, uint8_t, const void*, size_t) __WEAK_ALIAS("gpio_i2c_transmit"); |
| 283 | status_t i2c_receive(int, uint8_t, void*, size_t) __WEAK_ALIAS("gpio_i2c_receive"); |
| 284 | status_t i2c_write_reg_bytes(int, uint8_t, uint8_t, |
| 285 | const uint8_t*, size_t) __WEAK_ALIAS("gpio_i2c_write_reg_bytes"); |
| 286 | status_t i2c_read_reg_bytes(int, uint8_t, uint8_t, |
| 287 | uint8_t*, size_t) __WEAK_ALIAS("gpio_i2c_read_reg_bytes"); |