| 1 | /* $NetBSD: i2c.h,v 1.8 2015/03/05 17:29:18 riastradh Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 2015 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Taylor R. Campbell. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef _LINUX_I2C_H_ |
| 33 | #define _LINUX_I2C_H_ |
| 34 | |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/device_if.h> |
| 37 | #include <sys/queue.h> /* XXX include order botch: i2cvar.h needs */ |
| 38 | #include <sys/systm.h> |
| 39 | |
| 40 | #include <dev/i2c/i2cvar.h> |
| 41 | |
| 42 | #include <linux/pm.h> |
| 43 | |
| 44 | struct i2c_adapter; |
| 45 | struct i2c_algorithm; |
| 46 | struct i2c_msg; |
| 47 | |
| 48 | #define I2C_NAME_SIZE 20 |
| 49 | |
| 50 | /* |
| 51 | * I2C_M_*: i2c_msg flags |
| 52 | */ |
| 53 | #define I2C_M_RD 0x01 /* xfer is read, not write */ |
| 54 | #define I2C_M_NOSTART 0x02 /* don't initiate xfer */ |
| 55 | #define I2C_M_TEN 0x04 /* 10-bit chip address */ |
| 56 | |
| 57 | /* |
| 58 | * I2C_CLASS_*: i2c_adapter classes |
| 59 | */ |
| 60 | #define I2C_CLASS_DDC 0x01 |
| 61 | |
| 62 | /* |
| 63 | * I2C_FUNC_*: i2c_adapter functionality bits |
| 64 | */ |
| 65 | #define I2C_FUNC_I2C 0x01 |
| 66 | #define I2C_FUNC_NOSTART 0x02 |
| 67 | #define I2C_FUNC_SMBUS_EMUL 0x04 |
| 68 | #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x08 |
| 69 | #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x10 |
| 70 | #define I2C_FUNC_10BIT_ADDR 0x20 |
| 71 | |
| 72 | /* |
| 73 | * struct i2c_msg: A single i2c message request on a particular |
| 74 | * address. Read if I2C_M_RD is set, write otherwise. |
| 75 | */ |
| 76 | struct i2c_msg { |
| 77 | i2c_addr_t addr; |
| 78 | uint16_t flags; /* I2C_M_* */ |
| 79 | uint16_t len; |
| 80 | uint8_t *buf; |
| 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * struct i2c_adapter: An i2c bus controller. |
| 85 | */ |
| 86 | struct i2c_adapter { |
| 87 | char name[I2C_NAME_SIZE]; |
| 88 | const struct i2c_algorithm *algo; |
| 89 | void *algo_data; |
| 90 | int retries; |
| 91 | struct module *owner; |
| 92 | unsigned int class; /* I2C_CLASS_* */ |
| 93 | struct { |
| 94 | device_t parent; |
| 95 | } dev; |
| 96 | void *i2ca_adapdata; |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * struct i2c_algorithm: A procedure for transferring an i2c message on |
| 101 | * an i2c bus, along with a set of flags describing its functionality. |
| 102 | */ |
| 103 | struct i2c_algorithm { |
| 104 | int (*master_xfer)(struct i2c_adapter *, struct i2c_msg *, |
| 105 | int); |
| 106 | uint32_t (*functionality)(struct i2c_adapter *); |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * struct i2c_board_info: Parameters to find an i2c bus and a slave on |
| 111 | * it. type is the name of an i2c driver; addr is the slave address; |
| 112 | * platform_data is an extra parameter to pass to the i2c driver. |
| 113 | */ |
| 114 | struct i2c_board_info { |
| 115 | char type[I2C_NAME_SIZE]; |
| 116 | uint16_t addr; |
| 117 | uint16_t flags; |
| 118 | void *platform_data; |
| 119 | }; |
| 120 | |
| 121 | #define I2C_BOARD_INFO(board_type, board_addr) \ |
| 122 | .type = (board_type), \ |
| 123 | .addr = (board_addr) |
| 124 | |
| 125 | /* |
| 126 | * struct i2c_client: An i2c slave device at a particular address on a |
| 127 | * particular bus. |
| 128 | */ |
| 129 | struct i2c_client { |
| 130 | struct i2c_adapter *adapter; |
| 131 | uint16_t addr; |
| 132 | uint16_t flags; |
| 133 | }; |
| 134 | |
| 135 | /* |
| 136 | * struct i2c_device_id: Device id naming a class of i2c slave devices |
| 137 | * and parameters to the driver for the devices. |
| 138 | */ |
| 139 | struct i2c_device_id { |
| 140 | char name[I2C_NAME_SIZE]; |
| 141 | unsigned long driver_data; |
| 142 | }; |
| 143 | |
| 144 | /* |
| 145 | * struct i2c_driver: A driver for a class of i2c slave devices. We |
| 146 | * don't actually use this. |
| 147 | */ |
| 148 | struct i2c_driver { |
| 149 | int (*probe)(struct i2c_client *, const struct i2c_device_id *); |
| 150 | int (*remove)(struct i2c_client *); |
| 151 | struct { |
| 152 | char name[I2C_NAME_SIZE]; |
| 153 | const struct dev_pm_ops pm; |
| 154 | } driver; |
| 155 | }; |
| 156 | |
| 157 | /* |
| 158 | * Adapter management. We don't register these in a global database |
| 159 | * like Linux, so these are just stubs. |
| 160 | */ |
| 161 | static inline int |
| 162 | i2c_add_adapter(struct i2c_adapter *adapter __unused) |
| 163 | { |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static inline void |
| 169 | i2c_del_adapter(struct i2c_adapter *adapter __unused) |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | static inline void * |
| 174 | i2c_get_adapdata(const struct i2c_adapter *adapter) |
| 175 | { |
| 176 | |
| 177 | return adapter->i2ca_adapdata; |
| 178 | } |
| 179 | |
| 180 | static inline void |
| 181 | i2c_set_adapdata(struct i2c_adapter *adapter, void *data) |
| 182 | { |
| 183 | |
| 184 | adapter->i2ca_adapdata = data; |
| 185 | } |
| 186 | |
| 187 | /* XXX Make the nm output a little more greppable... */ |
| 188 | #define i2c_master_recv linux_i2c_master_recv |
| 189 | #define i2c_master_send linux_i2c_master_send |
| 190 | #define i2c_new_device linux_i2c_new_device |
| 191 | #define i2c_transfer linux_i2c_transfer |
| 192 | #define i2c_unregister_device linux_i2c_unregister_device |
| 193 | |
| 194 | int i2c_master_send(const struct i2c_client *, const char *, int); |
| 195 | int i2c_master_recv(const struct i2c_client *, char *, int); |
| 196 | struct i2c_client * |
| 197 | i2c_new_device(struct i2c_adapter *, const struct i2c_board_info *); |
| 198 | int i2c_transfer(struct i2c_adapter *, struct i2c_msg *, int); |
| 199 | void i2c_unregister_device(struct i2c_client *); |
| 200 | |
| 201 | #endif /* _LINUX_I2C_H_ */ |
| 202 | |