1 /*
2  * Copyright (c) 2013 Google Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #ifndef __GPIO_I2C__
24 #define __GPIO_I2C__
25 
26 #include <stdint.h>
27 #include <dev/i2c.h>
28 
29 typedef struct gpio_i2c_info {
30     const int sda;
31     const int scl;
32     uint32_t  hcd;  /* 1/2 I2C clock delay in cpu cycles */
33     uint32_t  qcd;  /* 1/4 I2C clock delay in cpu cycles */
34 } gpio_i2c_info_t;
35 
36 #define DEFINE_GPIO_I2C(_name,                 \
37                         _sda_gpio_id,          \
38                         _scl_gpio_id,          \
39                         _clk_ticks)            \
40 static const gpio_i2c_info_t _name = {         \
41     .sda            = _sda_gpio_id,            \
42     .scl            = _scl_gpio_id,            \
43     .hcd            = ((_clk_ticks + 1) >> 1), \
44     .qcd            = ((_clk_ticks + 3) >> 2), \
45 }
46 
47 void gpio_i2c_add_bus(uint32_t bus_id, const gpio_i2c_info_t *info);
48 
49 void gpio_i2c_init_early(void);
50 void gpio_i2c_init(void);
51 status_t gpio_i2c_transmit(int, uint8_t, const void *, size_t);
52 status_t gpio_i2c_receive(int, uint8_t, void *, size_t);
53 status_t gpio_i2c_write_reg_bytes(int, uint8_t, uint8_t, const uint8_t *, size_t);
54 status_t gpio_i2c_read_reg_bytes(int, uint8_t, uint8_t, uint8_t *, size_t);
55 
56 #endif  // __GPIO_I2C__
57