blob: 37f183cd1b9b8ae5dc859ed6c3fc76cd7a81ebeb [file] [log] [blame]
Rodrigo Caballero5903f232016-02-06 15:19:43 -06001/**
2 * @file
3 *
4 * @brief Public APIs for the I2C drivers.
5 */
Dan Kalowsky8c85f012015-06-30 09:44:34 -07006
7/*
8 * Copyright (c) 2015 Intel Corporation
9 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050010 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
Dan Kalowsky8c85f012015-06-30 09:44:34 -070013 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050014 * http://www.apache.org/licenses/LICENSE-2.0
Dan Kalowsky8c85f012015-06-30 09:44:34 -070015 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050016 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Dan Kalowsky8c85f012015-06-30 09:44:34 -070021 */
22#ifndef __DRIVERS_I2C_H
23#define __DRIVERS_I2C_H
24
Anas Nashif75482aa2015-10-26 06:18:44 -040025/**
26 * @brief I2C Interface
27 * @defgroup i2c_interface I2C Interface
28 * @ingroup io_interfaces
29 * @{
30 */
31
Dan Kalowsky8c85f012015-06-30 09:44:34 -070032#ifdef __cplusplus
33extern "C" {
34#endif
35
36#include <stdint.h>
37#include <device.h>
38
Daniel Leungf6646e22016-02-17 11:31:11 -080039/*
40 * The following #defines are used to configure the I2C controller.
Rodrigo Caballero5903f232016-02-06 15:19:43 -060041 */
Daniel Leungf6646e22016-02-17 11:31:11 -080042
43/** I2C Standard Speed */
Dan Kalowsky9c215672015-08-24 11:30:06 -070044#define I2C_SPEED_STANDARD (0x1)
Daniel Leungf6646e22016-02-17 11:31:11 -080045
46/** I2C Fast Speed */
Dan Kalowsky9c215672015-08-24 11:30:06 -070047#define I2C_SPEED_FAST (0x2)
Daniel Leungf6646e22016-02-17 11:31:11 -080048
49/** I2C Fast Plus Speed */
Dan Kalowsky9c215672015-08-24 11:30:06 -070050#define I2C_SPEED_FAST_PLUS (0x3)
Daniel Leungf6646e22016-02-17 11:31:11 -080051
52/** I2C High Speed */
Dan Kalowsky9c215672015-08-24 11:30:06 -070053#define I2C_SPEED_HIGH (0x4)
Daniel Leungf6646e22016-02-17 11:31:11 -080054
55/** I2C Ultra Fast Speed */
Dan Kalowsky9c215672015-08-24 11:30:06 -070056#define I2C_SPEED_ULTRA (0x5)
Dan Kalowsky8c85f012015-06-30 09:44:34 -070057
Daniel Leungf6646e22016-02-17 11:31:11 -080058/** For internal use. */
Dan Kalowsky9c215672015-08-24 11:30:06 -070059#define I2C_SPEED_MASK (0x7 << 1) /* 3 bits */
Daniel Leungf6646e22016-02-17 11:31:11 -080060
61/** Use 10-bit addressing. */
62#define I2C_ADDR_10_BITS (1 << 0)
63
64/** Controller to act as Master. */
Dan Kalowsky9c215672015-08-24 11:30:06 -070065#define I2C_MODE_MASTER (1 << 4)
Daniel Leungf6646e22016-02-17 11:31:11 -080066
67/** Controller to act as Slave. */
Dan Kalowsky9c215672015-08-24 11:30:06 -070068#define I2C_MODE_SLAVE_READ (1 << 5)
Dan Kalowsky8c85f012015-06-30 09:44:34 -070069
Daniel Leungf6646e22016-02-17 11:31:11 -080070
71/*
72 * I2C_MSG_* are I2C Message flags.
Rodrigo Caballero5903f232016-02-06 15:19:43 -060073 */
Daniel Leungf6646e22016-02-17 11:31:11 -080074
75/** Write message to I2C bus. */
Daniel Leungdff5f6a2016-01-11 15:35:39 -080076#define I2C_MSG_WRITE (0 << 0)
Daniel Leungf6646e22016-02-17 11:31:11 -080077
78/** Read message from I2C bus. */
Daniel Leungdff5f6a2016-01-11 15:35:39 -080079#define I2C_MSG_READ (1 << 0)
Daniel Leungf6646e22016-02-17 11:31:11 -080080
81/** For internal use. */
Daniel Leungdff5f6a2016-01-11 15:35:39 -080082#define I2C_MSG_RW_MASK (1 << 0)
83
Daniel Leungf6646e22016-02-17 11:31:11 -080084/** Send STOP after this message. */
Daniel Leungdff5f6a2016-01-11 15:35:39 -080085#define I2C_MSG_STOP (1 << 1)
Rodrigo Caballero5903f232016-02-06 15:19:43 -060086
Daniel Leungf6646e22016-02-17 11:31:11 -080087/** RESTART I2C transaction for this message. */
Daniel Leungdff5f6a2016-01-11 15:35:39 -080088#define I2C_MSG_RESTART (1 << 2)
89
Daniel Leungf6646e22016-02-17 11:31:11 -080090/**
91 * @brief One I2C Message.
92 *
93 * This defines one I2C message to transact on the I2C bus.
94 */
Daniel Leungdff5f6a2016-01-11 15:35:39 -080095struct i2c_msg {
Daniel Leungf6646e22016-02-17 11:31:11 -080096 /** Data buffer in bytes */
Daniel Leungdff5f6a2016-01-11 15:35:39 -080097 uint8_t *buf;
98
Daniel Leungf6646e22016-02-17 11:31:11 -080099 /** Length of buffer in bytes */
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800100 uint32_t len;
101
Daniel Leungf6646e22016-02-17 11:31:11 -0800102 /** Flags for this message */
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800103 uint8_t flags;
Daniel Leungf6646e22016-02-17 11:31:11 -0800104
Dan Kalowsky47436842016-01-28 12:53:34 -0800105 uint8_t stride[3];
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800106};
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700107
Dan Kalowsky9c215672015-08-24 11:30:06 -0700108union dev_config {
109 uint32_t raw;
Inaky Perez-Gonzalezecc4c762016-06-16 14:52:44 -0700110 struct __bits {
Dan Kalowsky9c215672015-08-24 11:30:06 -0700111 uint32_t use_10_bit_addr : 1;
112 uint32_t speed : 3;
113 uint32_t is_master_device : 1;
114 uint32_t is_slave_read : 1;
115 uint32_t reserved : 26;
116 } bits;
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700117};
118
Daniel Leungf6646e22016-02-17 11:31:11 -0800119/**
120 * @cond INTERNAL_HIDDEN
121 *
122 * These are for internal use only, so skip these in
123 * public documentation.
124 */
Dan Kalowsky9c215672015-08-24 11:30:06 -0700125typedef int (*i2c_api_configure_t)(struct device *dev,
126 uint32_t dev_config);
Daniel Leungac0beb72015-10-07 15:08:17 -0700127typedef int (*i2c_api_full_io_t)(struct device *dev,
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800128 struct i2c_msg *msgs,
129 uint8_t num_msgs,
130 uint16_t addr);
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700131
132struct i2c_driver_api {
Dan Kalowsky9c215672015-08-24 11:30:06 -0700133 i2c_api_configure_t configure;
Daniel Leungac0beb72015-10-07 15:08:17 -0700134 i2c_api_full_io_t transfer;
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700135};
Daniel Leungf6646e22016-02-17 11:31:11 -0800136/**
137 * @endcond
138 */
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700139
Anas Nashif20764a22015-07-01 16:47:13 -0400140/**
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600141 * @brief Configure operation of a host controller.
Tomasz Bursztykace31c522015-12-08 12:16:41 +0100142 *
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600143 * @param dev Pointer to the device structure for the driver instance.
Dan Kalowsky9c215672015-08-24 11:30:06 -0700144 * @param dev_config Bit-packed 32-bit value to the device runtime configuration
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600145 * for the I2C controller.
Dan Kalowskye728cad2015-10-01 14:16:47 -0700146 *
Andre Guedes024cfe72016-03-09 14:01:20 -0300147 * @retval 0 If successful.
Andre Guedes4851ee72016-03-10 11:47:06 -0300148 * @retval Negative errno code if failure.
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700149 */
Dan Kalowsky9c215672015-08-24 11:30:06 -0700150static inline int i2c_configure(struct device *dev, uint32_t dev_config)
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700151{
Marcus Shawcrofte7a24452016-10-22 10:02:48 +0100152 const struct i2c_driver_api *api = dev->driver_api;
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700153
Dan Kalowsky9c215672015-08-24 11:30:06 -0700154 return api->configure(dev, dev_config);
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700155}
156
Anas Nashif20764a22015-07-01 16:47:13 -0400157/**
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600158 * @brief Write a set amount of data to an I2C device.
Tomasz Bursztykad3d14842015-10-01 14:16:47 -0700159 *
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600160 * This routine writes a set amount of data synchronously.
Tomasz Bursztykace31c522015-12-08 12:16:41 +0100161 *
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600162 * @param dev Pointer to the device structure for the driver instance.
163 * @param buf Memory pool from which the data is transferred.
164 * @param len Size of the memory pool available for reading.
165 * @param addr Address to the target I2C device for writing.
Dan Kalowskye728cad2015-10-01 14:16:47 -0700166 *
Andre Guedes024cfe72016-03-09 14:01:20 -0300167 * @retval 0 If successful.
Andre Guedes4851ee72016-03-10 11:47:06 -0300168 * @retval Negative errno code if failure.
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700169 */
Dan Kalowsky9c215672015-08-24 11:30:06 -0700170static inline int i2c_write(struct device *dev, uint8_t *buf,
Tomasz Bursztykace31c522015-12-08 12:16:41 +0100171 uint32_t len, uint16_t addr)
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700172{
Marcus Shawcrofte7a24452016-10-22 10:02:48 +0100173 const struct i2c_driver_api *api = dev->driver_api;
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800174 struct i2c_msg msg;
175
176 msg.buf = buf;
177 msg.len = len;
Andre Guedese0d353b2016-01-13 20:35:10 -0200178 msg.flags = I2C_MSG_WRITE | I2C_MSG_STOP;
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700179
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800180 return api->transfer(dev, &msg, 1, addr);
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700181}
182
Anas Nashif20764a22015-07-01 16:47:13 -0400183/**
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600184 * @brief Read a set amount of data from an I2C device.
Tomasz Bursztykace31c522015-12-08 12:16:41 +0100185 *
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600186 * This routine reads a set amount of data synchronously.
Tomasz Bursztykace31c522015-12-08 12:16:41 +0100187 *
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600188 * @param dev Pointer to the device structure for the driver instance.
189 * @param buf Memory pool that stores the retrieved data.
190 * @param len Size of the memory pool available for writing.
191 * @param addr Address of the I2C device being read.
Dan Kalowskye728cad2015-10-01 14:16:47 -0700192 *
Andre Guedes024cfe72016-03-09 14:01:20 -0300193 * @retval 0 If successful.
Andre Guedes4851ee72016-03-10 11:47:06 -0300194 * @retval Negative errno code if failure.
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700195 */
Dan Kalowsky9c215672015-08-24 11:30:06 -0700196static inline int i2c_read(struct device *dev, uint8_t *buf,
Tomasz Bursztykace31c522015-12-08 12:16:41 +0100197 uint32_t len, uint16_t addr)
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700198{
Marcus Shawcrofte7a24452016-10-22 10:02:48 +0100199 const struct i2c_driver_api *api = dev->driver_api;
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800200 struct i2c_msg msg;
201
202 msg.buf = buf;
203 msg.len = len;
Andre Guedese0d353b2016-01-13 20:35:10 -0200204 msg.flags = I2C_MSG_READ | I2C_MSG_STOP;
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700205
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800206 return api->transfer(dev, &msg, 1, addr);
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700207}
208
Anas Nashif20764a22015-07-01 16:47:13 -0400209/**
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600210 * @brief Perform data transfer to another I2C device.
Daniel Leungac0beb72015-10-07 15:08:17 -0700211 *
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600212 * This routine provides a generic interface to perform data transfer
213 * to another I2C device synchronously. Use i2c_read()/i2c_write()
214 * for simple read or write.
Daniel Leungac0beb72015-10-07 15:08:17 -0700215 *
Rodrigo Caballero5903f232016-02-06 15:19:43 -0600216 * @param dev Pointer to the device structure for the driver instance.
217 * @param msgs Array of messages to transfer.
218 * @param num_msgs Number of messages to transfer.
219 * @param addr Address of the I2C target device.
Daniel Leungac0beb72015-10-07 15:08:17 -0700220 *
Andre Guedes024cfe72016-03-09 14:01:20 -0300221 * @retval 0 If successful.
Andre Guedes4851ee72016-03-10 11:47:06 -0300222 * @retval Negative errno code if failure.
Daniel Leungac0beb72015-10-07 15:08:17 -0700223 */
224static inline int i2c_transfer(struct device *dev,
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800225 struct i2c_msg *msgs, uint8_t num_msgs,
226 uint16_t addr)
Daniel Leungac0beb72015-10-07 15:08:17 -0700227{
Marcus Shawcrofte7a24452016-10-22 10:02:48 +0100228 const struct i2c_driver_api *api = dev->driver_api;
Daniel Leungac0beb72015-10-07 15:08:17 -0700229
Daniel Leungdff5f6a2016-01-11 15:35:39 -0800230 return api->transfer(dev, msgs, num_msgs, addr);
Daniel Leungac0beb72015-10-07 15:08:17 -0700231}
232
233/**
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300234 * @brief Read multiple bytes from an internal address of an I2C device.
235 *
236 * This routine reads multiple bytes from an internal address of an
237 * I2C device synchronously.
238 *
239 * @param dev Pointer to the device structure for the driver instance.
240 * @param dev_addr Address of the I2C device for reading.
241 * @param start_addr Internal address from which the data is being read.
242 * @param buf Memory pool that stores the retrieved data.
243 * @param num_bytes Number of bytes being read.
244 *
245 * @retval 0 If successful.
246 * @retval Negative errno code if failure.
247 */
248static inline int i2c_burst_read(struct device *dev, uint16_t dev_addr,
249 uint8_t start_addr, uint8_t *buf,
250 uint8_t num_bytes)
251{
Marcus Shawcrofte7a24452016-10-22 10:02:48 +0100252 const struct i2c_driver_api *api = dev->driver_api;
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300253 struct i2c_msg msg[2];
254
255 msg[0].buf = &start_addr;
256 msg[0].len = 1;
Maureen Helm92de81c2016-04-29 16:17:03 -0500257 msg[0].flags = I2C_MSG_WRITE;
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300258
259 msg[1].buf = buf;
260 msg[1].len = num_bytes;
Maureen Helm92de81c2016-04-29 16:17:03 -0500261 msg[1].flags = I2C_MSG_RESTART | I2C_MSG_READ | I2C_MSG_STOP;
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300262
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300263 return api->transfer(dev, msg, 2, dev_addr);
264}
265
266/**
267 * @brief Write multiple bytes to an internal address of an I2C device.
268 *
269 * This routine writes multiple bytes to an internal address of an
270 * I2C device synchronously.
271 *
272 * @param dev Pointer to the device structure for the driver instance.
273 * @param dev_addr Address of the I2C device for writing.
274 * @param start_addr Internal address to which the data is being written.
275 * @param buf Memory pool from which the data is transferred.
276 * @param num_bytes Number of bytes being written.
277 *
278 * @retval 0 If successful.
279 * @retval Negative errno code if failure.
280 */
281static inline int i2c_burst_write(struct device *dev, uint16_t dev_addr,
282 uint8_t start_addr, uint8_t *buf,
283 uint8_t num_bytes)
284{
Marcus Shawcrofte7a24452016-10-22 10:02:48 +0100285 const struct i2c_driver_api *api = dev->driver_api;
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300286 struct i2c_msg msg[2];
287
288 msg[0].buf = &start_addr;
289 msg[0].len = 1;
290 msg[0].flags = I2C_MSG_WRITE;
291
292 msg[1].buf = buf;
293 msg[1].len = num_bytes;
294 msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
295
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300296 return api->transfer(dev, msg, 2, dev_addr);
297}
298
299/**
300 * @brief Read internal register of an I2C device.
301 *
302 * This routine reads the value of an 8-bit internal register of an I2C
303 * device synchronously.
304 *
305 * @param dev Pointer to the device structure for the driver instance.
306 * @param dev_addr Address of the I2C device for reading.
307 * @param reg_addr Address of the internal register being read.
308 * @param value Memory pool that stores the retrieved register value.
309 *
310 * @retval 0 If successful.
311 * @retval Negative errno code if failure.
312 */
313static inline int i2c_reg_read_byte(struct device *dev, uint16_t dev_addr,
314 uint8_t reg_addr, uint8_t *value)
315{
316 return i2c_burst_read(dev, dev_addr, reg_addr, value, 1);
317}
318
319/**
320 * @brief Write internal register of an I2C device.
321 *
322 * This routine writes a value to an 8-bit internal register of an I2C
323 * device synchronously.
324 *
325 * @param dev Pointer to the device structure for the driver instance.
326 * @param dev_addr Address of the I2C device for writing.
327 * @param reg_addr Address of the internal register being written.
328 * @param value Value to be written to internal register.
329 *
330 * @retval 0 If successful.
331 * @retval Negative errno code if failure.
332 */
333static inline int i2c_reg_write_byte(struct device *dev, uint16_t dev_addr,
334 uint8_t reg_addr, uint8_t value)
335{
336 uint8_t tx_buf[2] = {reg_addr, value};
337
338 return i2c_write(dev, tx_buf, 2, dev_addr);
339}
340
341/**
342 * @brief Update internal register of an I2C device.
343 *
344 * This routine updates the value of a set of bits from an 8-bit internal
345 * register of an I2C device synchronously.
346 *
347 * @param dev Pointer to the device structure for the driver instance.
348 * @param dev_addr Address of the I2C device for updating.
349 * @param reg_addr Address of the internal register being updated.
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300350 * @param mask Bitmask for updating internal register.
Bogdan Davidoaia9496ec62016-04-25 18:58:41 +0300351 * @param value Value for updating internal register.
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300352 *
353 * @retval 0 If successful.
354 * @retval Negative errno code if failure.
355 */
356static inline int i2c_reg_update_byte(struct device *dev, uint8_t dev_addr,
Bogdan Davidoaia9496ec62016-04-25 18:58:41 +0300357 uint8_t reg_addr, uint8_t mask,
358 uint8_t value)
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300359{
360 uint8_t old_value, new_value;
361 int rc;
362
Bogdan Davidoaia6f6e65b2016-04-25 16:33:06 +0300363 rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value);
Bogdan Davidoaiaef26bf72016-04-21 11:45:36 +0300364 if (rc != 0) {
365 return rc;
366 }
367
368 new_value = (old_value & ~mask) | (value & mask);
369 if (new_value == old_value) {
370 return 0;
371 }
372
373 return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value);
374}
375
Vlad Dogaru8fed55f2016-04-21 17:22:27 +0300376struct i2c_client_config {
377 char *i2c_master;
378 uint16_t i2c_addr;
379};
380
381#define I2C_DECLARE_CLIENT_CONFIG struct i2c_client_config i2c_client
382
383#define I2C_CLIENT(_master, _addr) \
384 .i2c_client = { \
385 .i2c_master = (_master), \
386 .i2c_addr = (_addr), \
387 }
388
389#define I2C_GET_MASTER(_conf) ((_conf)->i2c_client.i2c_master)
390#define I2C_GET_ADDR(_conf) ((_conf)->i2c_client.i2c_addr)
391
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700392#ifdef __cplusplus
393}
394#endif
395
Anas Nashif75482aa2015-10-26 06:18:44 -0400396/**
397 * @}
398 */
399
400
Dan Kalowsky8c85f012015-06-30 09:44:34 -0700401#endif /* __DRIVERS_I2C_H */