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 | #ifndef __GPIO_I2C__ |
| 25 | #define __GPIO_I2C__ |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | #include <dev/i2c.h> |
| 29 | |
| 30 | typedef struct gpio_i2c_info { |
| 31 | const int sda; |
| 32 | const int scl; |
| 33 | uint32_t hcd; /* 1/2 I2C clock delay in cpu cycles */ |
| 34 | uint32_t qcd; /* 1/4 I2C clock delay in cpu cycles */ |
| 35 | } gpio_i2c_info_t; |
| 36 | |
| 37 | #define DEFINE_GPIO_I2C(_name, \ |
| 38 | _sda_gpio_id, \ |
| 39 | _scl_gpio_id, \ |
| 40 | _clk_ticks) \ |
| 41 | static const gpio_i2c_info_t _name = { \ |
| 42 | .sda = _sda_gpio_id, \ |
| 43 | .scl = _scl_gpio_id, \ |
| 44 | .hcd = ((_clk_ticks + 1) >> 1), \ |
| 45 | .qcd = ((_clk_ticks + 3) >> 2), \ |
| 46 | } |
| 47 | |
| 48 | void gpio_i2c_add_bus(uint32_t bus_id, const gpio_i2c_info_t* info); |
| 49 | |
| 50 | void gpio_i2c_init_early(void); |
| 51 | void gpio_i2c_init(void); |
| 52 | status_t gpio_i2c_transmit(int, uint8_t, const void*, size_t); |
| 53 | status_t gpio_i2c_receive(int, uint8_t, void*, size_t); |
| 54 | status_t gpio_i2c_write_reg_bytes(int, uint8_t, uint8_t, const uint8_t*, size_t); |
| 55 | status_t gpio_i2c_read_reg_bytes(int, uint8_t, uint8_t, uint8_t*, size_t); |
| 56 | |
| 57 | #endif // __GPIO_I2C__ |