1 /**
2 * @file
3 *
4 * @brief Public APIs for the I2C drivers.
5 */
6
7 /*
8 * Copyright (c) 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12 #ifndef ZEPHYR_INCLUDE_DRIVERS_I2C_H_
13 #define ZEPHYR_INCLUDE_DRIVERS_I2C_H_
14
15 /**
16 * @brief I2C Interface
17 * @defgroup i2c_interface I2C Interface
18 * @ingroup io_interfaces
19 * @{
20 */
21
22 #include <errno.h>
23
24 #include <zephyr/types.h>
25 #include <zephyr/device.h>
26 #include <zephyr/kernel.h>
27 #include <zephyr/sys/slist.h>
28 #include <zephyr/rtio/rtio.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*
35 * The following #defines are used to configure the I2C controller.
36 */
37
38 /** I2C Standard Speed: 100 kHz */
39 #define I2C_SPEED_STANDARD (0x1U)
40
41 /** I2C Fast Speed: 400 kHz */
42 #define I2C_SPEED_FAST (0x2U)
43
44 /** I2C Fast Plus Speed: 1 MHz */
45 #define I2C_SPEED_FAST_PLUS (0x3U)
46
47 /** I2C High Speed: 3.4 MHz */
48 #define I2C_SPEED_HIGH (0x4U)
49
50 /** I2C Ultra Fast Speed: 5 MHz */
51 #define I2C_SPEED_ULTRA (0x5U)
52
53 /** Device Tree specified speed */
54 #define I2C_SPEED_DT (0x7U)
55
56 #define I2C_SPEED_SHIFT (1U)
57 #define I2C_SPEED_SET(speed) (((speed) << I2C_SPEED_SHIFT) \
58 & I2C_SPEED_MASK)
59 #define I2C_SPEED_MASK (0x7U << I2C_SPEED_SHIFT) /* 3 bits */
60 #define I2C_SPEED_GET(cfg) (((cfg) & I2C_SPEED_MASK) \
61 >> I2C_SPEED_SHIFT)
62
63 /** Use 10-bit addressing. DEPRECATED - Use I2C_MSG_ADDR_10_BITS instead. */
64 #define I2C_ADDR_10_BITS BIT(0)
65
66 /** Peripheral to act as Controller. */
67 #define I2C_MODE_CONTROLLER BIT(4)
68
69 /**
70 * @brief Complete I2C DT information
71 *
72 * @param bus is the I2C bus
73 * @param addr is the target address
74 */
75 struct i2c_dt_spec {
76 const struct device *bus;
77 uint16_t addr;
78 };
79
80 /**
81 * @brief Structure initializer for i2c_dt_spec from devicetree (on I3C bus)
82 *
83 * This helper macro expands to a static initializer for a <tt>struct
84 * i2c_dt_spec</tt> by reading the relevant bus and address data from
85 * the devicetree.
86 *
87 * @param node_id Devicetree node identifier for the I2C device whose
88 * struct i2c_dt_spec to create an initializer for
89 */
90 #define I2C_DT_SPEC_GET_ON_I3C(node_id) \
91 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
92 .addr = DT_PROP_BY_IDX(node_id, reg, 0)
93
94 /**
95 * @brief Structure initializer for i2c_dt_spec from devicetree (on I2C bus)
96 *
97 * This helper macro expands to a static initializer for a <tt>struct
98 * i2c_dt_spec</tt> by reading the relevant bus and address data from
99 * the devicetree.
100 *
101 * @param node_id Devicetree node identifier for the I2C device whose
102 * struct i2c_dt_spec to create an initializer for
103 */
104 #define I2C_DT_SPEC_GET_ON_I2C(node_id) \
105 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
106 .addr = DT_REG_ADDR(node_id)
107
108 /**
109 * @brief Structure initializer for i2c_dt_spec from devicetree
110 *
111 * This helper macro expands to a static initializer for a <tt>struct
112 * i2c_dt_spec</tt> by reading the relevant bus and address data from
113 * the devicetree.
114 *
115 * @param node_id Devicetree node identifier for the I2C device whose
116 * struct i2c_dt_spec to create an initializer for
117 */
118 #define I2C_DT_SPEC_GET(node_id) \
119 { \
120 COND_CODE_1(DT_ON_BUS(node_id, i3c), \
121 (I2C_DT_SPEC_GET_ON_I3C(node_id)), \
122 (I2C_DT_SPEC_GET_ON_I2C(node_id))) \
123 }
124
125 /**
126 * @brief Structure initializer for i2c_dt_spec from devicetree instance
127 *
128 * This is equivalent to
129 * <tt>I2C_DT_SPEC_GET(DT_DRV_INST(inst))</tt>.
130 *
131 * @param inst Devicetree instance number
132 */
133 #define I2C_DT_SPEC_INST_GET(inst) \
134 I2C_DT_SPEC_GET(DT_DRV_INST(inst))
135
136
137 /*
138 * I2C_MSG_* are I2C Message flags.
139 */
140
141 /** Write message to I2C bus. */
142 #define I2C_MSG_WRITE (0U << 0U)
143
144 /** Read message from I2C bus. */
145 #define I2C_MSG_READ BIT(0)
146
147 /** @cond INTERNAL_HIDDEN */
148 #define I2C_MSG_RW_MASK BIT(0)
149 /** @endcond */
150
151 /** Send STOP after this message. */
152 #define I2C_MSG_STOP BIT(1)
153
154 /** RESTART I2C transaction for this message.
155 *
156 * @note Not all I2C drivers have or require explicit support for this
157 * feature. Some drivers require this be present on a read message
158 * that follows a write, or vice-versa. Some drivers will merge
159 * adjacent fragments into a single transaction using this flag; some
160 * will not. */
161 #define I2C_MSG_RESTART BIT(2)
162
163 /** Use 10-bit addressing for this message.
164 *
165 * @note Not all SoC I2C implementations support this feature. */
166 #define I2C_MSG_ADDR_10_BITS BIT(3)
167
168 /**
169 * @brief One I2C Message.
170 *
171 * This defines one I2C message to transact on the I2C bus.
172 *
173 * @note Some of the configurations supported by this API may not be
174 * supported by specific SoC I2C hardware implementations, in
175 * particular features related to bus transactions intended to read or
176 * write data from different buffers within a single transaction.
177 * Invocations of i2c_transfer() may not indicate an error when an
178 * unsupported configuration is encountered. In some cases drivers
179 * will generate separate transactions for each message fragment, with
180 * or without presence of @ref I2C_MSG_RESTART in #flags.
181 */
182 struct i2c_msg {
183 /** Data buffer in bytes */
184 uint8_t *buf;
185
186 /** Length of buffer in bytes */
187 uint32_t len;
188
189 /** Flags for this message */
190 uint8_t flags;
191 };
192
193 /**
194 * @brief I2C callback for asynchronous transfer requests
195 *
196 * @param dev I2C device which is notifying of transfer completion or error
197 * @param result Result code of the transfer request. 0 is success, -errno for failure.
198 * @param data Transfer requester supplied data which is passed along to the callback.
199 */
200 typedef void (*i2c_callback_t)(const struct device *dev, int result, void *data);
201
202 /**
203 * @cond INTERNAL_HIDDEN
204 *
205 * These are for internal use only, so skip these in
206 * public documentation.
207 */
208 struct i2c_target_config;
209
210 typedef int (*i2c_api_configure_t)(const struct device *dev,
211 uint32_t dev_config);
212 typedef int (*i2c_api_get_config_t)(const struct device *dev,
213 uint32_t *dev_config);
214 typedef int (*i2c_api_full_io_t)(const struct device *dev,
215 struct i2c_msg *msgs,
216 uint8_t num_msgs,
217 uint16_t addr);
218 typedef int (*i2c_api_target_register_t)(const struct device *dev,
219 struct i2c_target_config *cfg);
220 typedef int (*i2c_api_target_unregister_t)(const struct device *dev,
221 struct i2c_target_config *cfg);
222 #ifdef CONFIG_I2C_CALLBACK
223 typedef int (*i2c_api_transfer_cb_t)(const struct device *dev,
224 struct i2c_msg *msgs,
225 uint8_t num_msgs,
226 uint16_t addr,
227 i2c_callback_t cb,
228 void *userdata);
229 #endif /* CONFIG_I2C_CALLBACK */
230 #if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
231
232 /**
233 * @typedef i2c_api_iodev_submit
234 * @brief Callback API for submitting work to a I2C device with RTIO
235 */
236 typedef void (*i2c_api_iodev_submit)(const struct device *dev,
237 struct rtio_iodev_sqe *iodev_sqe);
238 #endif /* CONFIG_I2C_RTIO */
239
240 typedef int (*i2c_api_recover_bus_t)(const struct device *dev);
241
242 __subsystem struct i2c_driver_api {
243 i2c_api_configure_t configure;
244 i2c_api_get_config_t get_config;
245 i2c_api_full_io_t transfer;
246 i2c_api_target_register_t target_register;
247 i2c_api_target_unregister_t target_unregister;
248 #ifdef CONFIG_I2C_CALLBACK
249 i2c_api_transfer_cb_t transfer_cb;
250 #endif
251 #ifdef CONFIG_I2C_RTIO
252 i2c_api_iodev_submit iodev_submit;
253 #endif
254 i2c_api_recover_bus_t recover_bus;
255 };
256
257 typedef int (*i2c_target_api_register_t)(const struct device *dev);
258 typedef int (*i2c_target_api_unregister_t)(const struct device *dev);
259
260 struct i2c_target_driver_api {
261 i2c_target_api_register_t driver_register;
262 i2c_target_api_unregister_t driver_unregister;
263 };
264
265 /**
266 * @endcond
267 */
268
269 /** Target device responds to 10-bit addressing. */
270 #define I2C_TARGET_FLAGS_ADDR_10_BITS BIT(0)
271
272 /** @brief Function called when a write to the device is initiated.
273 *
274 * This function is invoked by the controller when the bus completes a
275 * start condition for a write operation to the address associated
276 * with a particular device.
277 *
278 * A success return shall cause the controller to ACK the next byte
279 * received. An error return shall cause the controller to NACK the
280 * next byte received.
281 *
282 * @param config the configuration structure associated with the
283 * device to which the operation is addressed.
284 *
285 * @return 0 if the write is accepted, or a negative error code.
286 */
287 typedef int (*i2c_target_write_requested_cb_t)(
288 struct i2c_target_config *config);
289
290 /** @brief Function called when a write to the device is continued.
291 *
292 * This function is invoked by the controller when it completes
293 * reception of a byte of data in an ongoing write operation to the
294 * device.
295 *
296 * A success return shall cause the controller to ACK the next byte
297 * received. An error return shall cause the controller to NACK the
298 * next byte received.
299 *
300 * @param config the configuration structure associated with the
301 * device to which the operation is addressed.
302 *
303 * @param val the byte received by the controller.
304 *
305 * @return 0 if more data can be accepted, or a negative error
306 * code.
307 */
308 typedef int (*i2c_target_write_received_cb_t)(
309 struct i2c_target_config *config, uint8_t val);
310
311 /** @brief Function called when a read from the device is initiated.
312 *
313 * This function is invoked by the controller when the bus completes a
314 * start condition for a read operation from the address associated
315 * with a particular device.
316 *
317 * The value returned in @p *val will be transmitted. A success
318 * return shall cause the controller to react to additional read
319 * operations. An error return shall cause the controller to ignore
320 * bus operations until a new start condition is received.
321 *
322 * @param config the configuration structure associated with the
323 * device to which the operation is addressed.
324 *
325 * @param val pointer to storage for the first byte of data to return
326 * for the read request.
327 *
328 * @return 0 if more data can be requested, or a negative error code.
329 */
330 typedef int (*i2c_target_read_requested_cb_t)(
331 struct i2c_target_config *config, uint8_t *val);
332
333 /** @brief Function called when a read from the device is continued.
334 *
335 * This function is invoked by the controller when the bus is ready to
336 * provide additional data for a read operation from the address
337 * associated with the device device.
338 *
339 * The value returned in @p *val will be transmitted. A success
340 * return shall cause the controller to react to additional read
341 * operations. An error return shall cause the controller to ignore
342 * bus operations until a new start condition is received.
343 *
344 * @param config the configuration structure associated with the
345 * device to which the operation is addressed.
346 *
347 * @param val pointer to storage for the next byte of data to return
348 * for the read request.
349 *
350 * @return 0 if data has been provided, or a negative error code.
351 */
352 typedef int (*i2c_target_read_processed_cb_t)(
353 struct i2c_target_config *config, uint8_t *val);
354
355 #ifdef CONFIG_I2C_TARGET_BUFFER_MODE
356 /** @brief Function called when a write to the device is completed.
357 *
358 * This function is invoked by the controller when it completes
359 * reception of data from the source buffer to the destination
360 * buffer in an ongoing write operation to the device.
361 *
362 * @param config the configuration structure associated with the
363 * device to which the operation is addressed.
364 *
365 * @param ptr pointer to the buffer that contains the data to be transferred.
366 *
367 * @param len the length of the data to be transferred.
368 */
369 typedef void (*i2c_target_buf_write_received_cb_t)(
370 struct i2c_target_config *config, uint8_t *ptr, uint32_t len);
371
372 /** @brief Function called when a read from the device is initiated.
373 *
374 * This function is invoked by the controller when the bus is ready to
375 * provide additional data by buffer for a read operation from the address
376 * associated with the device.
377 *
378 * The value returned in @p **ptr and @p *len will be transmitted. A success
379 * return shall cause the controller to react to additional read operations.
380 * An error return shall cause the controller to ignore bus operations until
381 * a new start condition is received.
382 *
383 * @param config the configuration structure associated with the
384 * device to which the operation is addressed.
385 *
386 * @param ptr pointer to storage for the address of data buffer to return
387 * for the read request.
388 *
389 * @param len pointer to storage for the length of the data to be transferred
390 * for the read request.
391 *
392 * @return 0 if data has been provided, or a negative error code.
393 */
394 typedef int (*i2c_target_buf_read_requested_cb_t)(
395 struct i2c_target_config *config, uint8_t **ptr, uint32_t *len);
396 #endif
397
398 /** @brief Function called when a stop condition is observed after a
399 * start condition addressed to a particular device.
400 *
401 * This function is invoked by the controller when the bus is ready to
402 * provide additional data for a read operation from the address
403 * associated with the device device. After the function returns the
404 * controller shall enter a state where it is ready to react to new
405 * start conditions.
406 *
407 * @param config the configuration structure associated with the
408 * device to which the operation is addressed.
409 *
410 * @return Ignored.
411 */
412 typedef int (*i2c_target_stop_cb_t)(struct i2c_target_config *config);
413
414 /** @brief Structure providing callbacks to be implemented for devices
415 * that supports the I2C target API.
416 *
417 * This structure may be shared by multiple devices that implement the
418 * same API at different addresses on the bus.
419 */
420 struct i2c_target_callbacks {
421 i2c_target_write_requested_cb_t write_requested;
422 i2c_target_read_requested_cb_t read_requested;
423 i2c_target_write_received_cb_t write_received;
424 i2c_target_read_processed_cb_t read_processed;
425 #ifdef CONFIG_I2C_TARGET_BUFFER_MODE
426 i2c_target_buf_write_received_cb_t buf_write_received;
427 i2c_target_buf_read_requested_cb_t buf_read_requested;
428 #endif
429 i2c_target_stop_cb_t stop;
430 };
431
432 /** @brief Structure describing a device that supports the I2C
433 * target API.
434 *
435 * Instances of this are passed to the i2c_target_register() and
436 * i2c_target_unregister() functions to indicate addition and removal
437 * of a target device, respective.
438 *
439 * Fields other than @c node must be initialized by the module that
440 * implements the device behavior prior to passing the object
441 * reference to i2c_target_register().
442 */
443 struct i2c_target_config {
444 /** Private, do not modify */
445 sys_snode_t node;
446
447 /** Flags for the target device defined by I2C_TARGET_FLAGS_* constants */
448 uint8_t flags;
449
450 /** Address for this target device */
451 uint16_t address;
452
453 /** Callback functions */
454 const struct i2c_target_callbacks *callbacks;
455 };
456
457 /**
458 * @brief Validate that I2C bus is ready.
459 *
460 * @param spec I2C specification from devicetree
461 *
462 * @retval true if the I2C bus is ready for use.
463 * @retval false if the I2C bus is not ready for use.
464 */
i2c_is_ready_dt(const struct i2c_dt_spec * spec)465 static inline bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
466 {
467 /* Validate bus is ready */
468 return device_is_ready(spec->bus);
469 }
470
471 /**
472 * @brief Check if the current message is a read operation
473 *
474 * @param msg The message to check
475 * @return true if the I2C message is sa read operation
476 * @return false if the I2C message is a write operation
477 */
i2c_is_read_op(struct i2c_msg * msg)478 static inline bool i2c_is_read_op(struct i2c_msg *msg)
479 {
480 return (msg->flags & I2C_MSG_READ) == I2C_MSG_READ;
481 }
482
483 /**
484 * @brief Dump out an I2C message
485 *
486 * Dumps out a list of I2C messages. For any that are writes (W), the data is
487 * displayed in hex. Setting dump_read will dump the data for read messages too,
488 * which only makes sense when called after the messages have been processed.
489 *
490 * It looks something like this (with name "testing"):
491 *
492 * @code
493 * D: I2C msg: testing, addr=56
494 * D: W len=01: 06
495 * D: W len=0e:
496 * D: contents:
497 * D: 00 01 02 03 04 05 06 07 |........
498 * D: 08 09 0a 0b 0c 0d |......
499 * D: W len=01: 0f
500 * D: R len=01: 6c
501 * @endcode
502 *
503 * @param dev Target for the messages being sent. Its name will be printed in the log.
504 * @param msgs Array of messages to dump.
505 * @param num_msgs Number of messages to dump.
506 * @param addr Address of the I2C target device.
507 * @param dump_read Dump data from I2C reads, otherwise only writes have data dumped.
508 */
509 void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
510 uint16_t addr, bool dump_read);
511
512 /**
513 * @brief Dump out an I2C message, before it is executed.
514 *
515 * This is equivalent to:
516 *
517 * i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);
518 *
519 * The read messages' data isn't dumped.
520 *
521 * @param dev Target for the messages being sent. Its name will be printed in the log.
522 * @param msgs Array of messages to dump.
523 * @param num_msgs Number of messages to dump.
524 * @param addr Address of the I2C target device.
525 */
i2c_dump_msgs(const struct device * dev,const struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)526 static inline void i2c_dump_msgs(const struct device *dev, const struct i2c_msg *msgs,
527 uint8_t num_msgs, uint16_t addr)
528 {
529 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);
530 }
531
532 #if defined(CONFIG_I2C_STATS) || defined(__DOXYGEN__)
533
534 #include <zephyr/stats/stats.h>
535
536 /** @cond INTERNAL_HIDDEN */
537
538 STATS_SECT_START(i2c)
539 STATS_SECT_ENTRY32(bytes_read)
540 STATS_SECT_ENTRY32(bytes_written)
541 STATS_SECT_ENTRY32(message_count)
542 STATS_SECT_ENTRY32(transfer_call_count)
543 STATS_SECT_END;
544
545 STATS_NAME_START(i2c)
546 STATS_NAME(i2c, bytes_read)
547 STATS_NAME(i2c, bytes_written)
548 STATS_NAME(i2c, message_count)
549 STATS_NAME(i2c, transfer_call_count)
550 STATS_NAME_END(i2c);
551
552 /** @endcond */
553
554
555 /**
556 * @brief I2C specific device state which allows for i2c device class specific additions
557 */
558 struct i2c_device_state {
559 struct device_state devstate;
560 struct stats_i2c stats;
561 };
562
563 /**
564 * @brief Updates the i2c stats for i2c transfers
565 *
566 * @param dev I2C device to update stats for
567 * @param msgs Array of struct i2c_msg
568 * @param num_msgs Number of i2c_msgs
569 */
i2c_xfer_stats(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs)570 static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
571 uint8_t num_msgs)
572 {
573 struct i2c_device_state *state =
574 CONTAINER_OF(dev->state, struct i2c_device_state, devstate);
575 uint32_t bytes_read = 0U;
576 uint32_t bytes_written = 0U;
577
578 STATS_INC(state->stats, transfer_call_count);
579 STATS_INCN(state->stats, message_count, num_msgs);
580 for (uint8_t i = 0U; i < num_msgs; i++) {
581 if (msgs[i].flags & I2C_MSG_READ) {
582 bytes_read += msgs[i].len;
583 } else {
584 bytes_written += msgs[i].len;
585 }
586 }
587 STATS_INCN(state->stats, bytes_read, bytes_read);
588 STATS_INCN(state->stats, bytes_written, bytes_written);
589 }
590
591 /** @cond INTERNAL_HIDDEN */
592
593 /**
594 * @brief Define a statically allocated and section assigned i2c device state
595 */
596 #define Z_I2C_DEVICE_STATE_DEFINE(dev_id) \
597 static struct i2c_device_state Z_DEVICE_STATE_NAME(dev_id) \
598 __attribute__((__section__(".z_devstate")))
599
600 /**
601 * @brief Define an i2c device init wrapper function
602 *
603 * This does device instance specific initialization of common data (such as stats)
604 * and calls the given init_fn
605 */
606 #define Z_I2C_INIT_FN(dev_id, init_fn) \
607 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
608 { \
609 struct i2c_device_state *state = \
610 CONTAINER_OF(dev->state, struct i2c_device_state, devstate); \
611 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 4, \
612 STATS_NAME_INIT_PARMS(i2c)); \
613 stats_register(dev->name, &(state->stats.s_hdr)); \
614 if (init_fn != NULL) { \
615 return init_fn(dev); \
616 } \
617 \
618 return 0; \
619 }
620
621 /** @endcond */
622
623 /**
624 * @brief Like DEVICE_DT_DEFINE() with I2C specifics.
625 *
626 * @details Defines a device which implements the I2C API. May
627 * generate a custom device_state container struct and init_fn
628 * wrapper when needed depending on I2C @kconfig{CONFIG_I2C_STATS}.
629 *
630 * @param node_id The devicetree node identifier.
631 *
632 * @param init_fn Name of the init function of the driver. Can be `NULL`.
633 *
634 * @param pm PM device resources reference (NULL if device does not use PM).
635 *
636 * @param data Pointer to the device's private data.
637 *
638 * @param config The address to the structure containing the
639 * configuration information for this instance of the driver.
640 *
641 * @param level The initialization level. See SYS_INIT() for
642 * details.
643 *
644 * @param prio Priority within the selected initialization level. See
645 * SYS_INIT() for details.
646 *
647 * @param api Provides an initial pointer to the API function struct
648 * used by the driver. Can be NULL.
649 */
650 #define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
651 prio, api, ...) \
652 Z_I2C_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
653 Z_I2C_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
654 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
655 DEVICE_DT_NAME(node_id), \
656 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
657 pm, data, config, level, prio, api, \
658 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
659 __VA_ARGS__)
660
661 #else /* CONFIG_I2C_STATS */
662
i2c_xfer_stats(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs)663 static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
664 uint8_t num_msgs)
665 {
666 ARG_UNUSED(dev);
667 ARG_UNUSED(msgs);
668 ARG_UNUSED(num_msgs);
669 }
670
671 #define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
672 prio, api, ...) \
673 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
674 prio, api, __VA_ARGS__)
675
676 #endif /* CONFIG_I2C_STATS */
677
678 /**
679 * @brief Like I2C_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible
680 *
681 * @param inst instance number. This is replaced by
682 * <tt>DT_DRV_COMPAT(inst)</tt> in the call to I2C_DEVICE_DT_DEFINE().
683 *
684 * @param ... other parameters as expected by I2C_DEVICE_DT_DEFINE().
685 */
686 #define I2C_DEVICE_DT_INST_DEFINE(inst, ...) \
687 I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
688
689
690 /**
691 * @brief Configure operation of a host controller.
692 *
693 * @param dev Pointer to the device structure for the driver instance.
694 * @param dev_config Bit-packed 32-bit value to the device runtime configuration
695 * for the I2C controller.
696 *
697 * @retval 0 If successful.
698 * @retval -EIO General input / output error, failed to configure device.
699 */
700 __syscall int i2c_configure(const struct device *dev, uint32_t dev_config);
701
z_impl_i2c_configure(const struct device * dev,uint32_t dev_config)702 static inline int z_impl_i2c_configure(const struct device *dev,
703 uint32_t dev_config)
704 {
705 const struct i2c_driver_api *api =
706 (const struct i2c_driver_api *)dev->api;
707
708 return api->configure(dev, dev_config);
709 }
710
711 /**
712 * @brief Get configuration of a host controller.
713 *
714 * This routine provides a way to get current configuration. It is allowed to
715 * call the function before i2c_configure, because some I2C ports can be
716 * configured during init process. However, if the I2C port is not configured,
717 * i2c_get_config returns an error.
718 *
719 * i2c_get_config can return cached config or probe hardware, but it has to be
720 * up to date with current configuration.
721 *
722 * @param dev Pointer to the device structure for the driver instance.
723 * @param dev_config Pointer to return bit-packed 32-bit value of
724 * the I2C controller configuration.
725 *
726 * @retval 0 If successful.
727 * @retval -EIO General input / output error.
728 * @retval -ERANGE Configured I2C frequency is invalid.
729 * @retval -ENOSYS If get config is not implemented
730 */
731 __syscall int i2c_get_config(const struct device *dev, uint32_t *dev_config);
732
z_impl_i2c_get_config(const struct device * dev,uint32_t * dev_config)733 static inline int z_impl_i2c_get_config(const struct device *dev, uint32_t *dev_config)
734 {
735 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
736
737 if (api->get_config == NULL) {
738 return -ENOSYS;
739 }
740
741 return api->get_config(dev, dev_config);
742 }
743
744 /**
745 * @brief Perform data transfer to another I2C device in controller mode.
746 *
747 * This routine provides a generic interface to perform data transfer
748 * to another I2C device synchronously. Use i2c_read()/i2c_write()
749 * for simple read or write.
750 *
751 * The array of message @a msgs must not be NULL. The number of
752 * message @a num_msgs may be zero,in which case no transfer occurs.
753 *
754 * @note Not all scatter/gather transactions can be supported by all
755 * drivers. As an example, a gather write (multiple consecutive
756 * `i2c_msg` buffers all configured for `I2C_MSG_WRITE`) may be packed
757 * into a single transaction by some drivers, but others may emit each
758 * fragment as a distinct write transaction, which will not produce
759 * the same behavior. See the documentation of `struct i2c_msg` for
760 * limitations on support for multi-message bus transactions.
761 *
762 * @param dev Pointer to the device structure for an I2C controller
763 * driver configured in controller mode.
764 * @param msgs Array of messages to transfer.
765 * @param num_msgs Number of messages to transfer.
766 * @param addr Address of the I2C target device.
767 *
768 * @retval 0 If successful.
769 * @retval -EIO General input / output error.
770 */
771 __syscall int i2c_transfer(const struct device *dev,
772 struct i2c_msg *msgs, uint8_t num_msgs,
773 uint16_t addr);
774
z_impl_i2c_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)775 static inline int z_impl_i2c_transfer(const struct device *dev,
776 struct i2c_msg *msgs, uint8_t num_msgs,
777 uint16_t addr)
778 {
779 const struct i2c_driver_api *api =
780 (const struct i2c_driver_api *)dev->api;
781
782 int res = api->transfer(dev, msgs, num_msgs, addr);
783
784 i2c_xfer_stats(dev, msgs, num_msgs);
785
786 if (IS_ENABLED(CONFIG_I2C_DUMP_MESSAGES)) {
787 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, true);
788 }
789
790 return res;
791 }
792
793 #if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
794
795 /**
796 * @brief Perform data transfer to another I2C device in controller mode.
797 *
798 * This routine provides a generic interface to perform data transfer
799 * to another I2C device asynchronously with a callback completion.
800 *
801 * @see i2c_transfer()
802 * @funcprops \isr_ok
803 *
804 * @param dev Pointer to the device structure for an I2C controller
805 * driver configured in controller mode.
806 * @param msgs Array of messages to transfer, must live until callback completes.
807 * @param num_msgs Number of messages to transfer.
808 * @param addr Address of the I2C target device.
809 * @param cb Function pointer for completion callback.
810 * @param userdata Userdata passed to callback.
811 *
812 * @retval 0 If successful.
813 * @retval -EIO General input / output error.
814 * @retval -ENOSYS If transfer async is not implemented
815 * @retval -EWOULDBLOCK If the device is temporarily busy doing another transfer
816 */
i2c_transfer_cb(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr,i2c_callback_t cb,void * userdata)817 static inline int i2c_transfer_cb(const struct device *dev,
818 struct i2c_msg *msgs,
819 uint8_t num_msgs,
820 uint16_t addr,
821 i2c_callback_t cb,
822 void *userdata)
823 {
824 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
825
826 if (api->transfer_cb == NULL) {
827 return -ENOSYS;
828 }
829
830 return api->transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
831 }
832
833 /**
834 * @brief Perform data transfer to another I2C device in master mode asynchronously.
835 *
836 * This is equivalent to:
837 *
838 * i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
839 *
840 * @param spec I2C specification from devicetree.
841 * @param msgs Array of messages to transfer.
842 * @param num_msgs Number of messages to transfer.
843 * @param cb Function pointer for completion callback.
844 * @param userdata Userdata passed to callback.
845 *
846 * @return a value from i2c_transfer_cb()
847 */
i2c_transfer_cb_dt(const struct i2c_dt_spec * spec,struct i2c_msg * msgs,uint8_t num_msgs,i2c_callback_t cb,void * userdata)848 static inline int i2c_transfer_cb_dt(const struct i2c_dt_spec *spec,
849 struct i2c_msg *msgs,
850 uint8_t num_msgs,
851 i2c_callback_t cb,
852 void *userdata)
853 {
854 return i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
855 }
856
857 /**
858 * @brief Write then read data from an I2C device asynchronously.
859 *
860 * This supports the common operation "this is what I want", "now give
861 * it to me" transaction pair through a combined write-then-read bus
862 * transaction but using i2c_transfer_cb. This helper function expects
863 * caller to pass a message pointer with 2 and only 2 size.
864 *
865 * @param dev Pointer to the device structure for an I2C controller
866 * driver configured in master mode.
867 * @param msgs Array of messages to transfer.
868 * @param num_msgs Number of messages to transfer.
869 * @param addr Address of the I2C device
870 * @param write_buf Pointer to the data to be written
871 * @param num_write Number of bytes to write
872 * @param read_buf Pointer to storage for read data
873 * @param num_read Number of bytes to read
874 * @param cb Function pointer for completion callback.
875 * @param userdata Userdata passed to callback.
876 *
877 * @retval 0 if successful
878 * @retval negative on error.
879 */
i2c_write_read_cb(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr,const void * write_buf,size_t num_write,void * read_buf,size_t num_read,i2c_callback_t cb,void * userdata)880 static inline int i2c_write_read_cb(const struct device *dev, struct i2c_msg *msgs,
881 uint8_t num_msgs, uint16_t addr, const void *write_buf,
882 size_t num_write, void *read_buf, size_t num_read,
883 i2c_callback_t cb, void *userdata)
884 {
885 if ((msgs == NULL) || (num_msgs != 2)) {
886 return -EINVAL;
887 }
888
889 msgs[0].buf = (uint8_t *)write_buf;
890 msgs[0].len = num_write;
891 msgs[0].flags = I2C_MSG_WRITE;
892
893 msgs[1].buf = (uint8_t *)read_buf;
894 msgs[1].len = num_read;
895 msgs[1].flags = I2C_MSG_RESTART | I2C_MSG_READ | I2C_MSG_STOP;
896
897 return i2c_transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
898 }
899
900 /**
901 * @brief Write then read data from an I2C device asynchronously.
902 *
903 * This is equivalent to:
904 *
905 * i2c_write_read_cb(spec->bus, msgs, num_msgs,
906 * spec->addr, write_buf,
907 * num_write, read_buf, num_read);
908 *
909 * @param spec I2C specification from devicetree.
910 * @param msgs Array of messages to transfer.
911 * @param num_msgs Number of messages to transfer.
912 * @param write_buf Pointer to the data to be written
913 * @param num_write Number of bytes to write
914 * @param read_buf Pointer to storage for read data
915 * @param num_read Number of bytes to read
916 * @param cb Function pointer for completion callback.
917 * @param userdata Userdata passed to callback.
918 *
919 * @return a value from i2c_write_read_cb()
920 */
i2c_write_read_cb_dt(const struct i2c_dt_spec * spec,struct i2c_msg * msgs,uint8_t num_msgs,const void * write_buf,size_t num_write,void * read_buf,size_t num_read,i2c_callback_t cb,void * userdata)921 static inline int i2c_write_read_cb_dt(const struct i2c_dt_spec *spec, struct i2c_msg *msgs,
922 uint8_t num_msgs, const void *write_buf, size_t num_write,
923 void *read_buf, size_t num_read, i2c_callback_t cb,
924 void *userdata)
925 {
926 return i2c_write_read_cb(spec->bus, msgs, num_msgs, spec->addr, write_buf, num_write,
927 read_buf, num_read, cb, userdata);
928 }
929
930 #if defined(CONFIG_POLL) || defined(__DOXYGEN__)
931
932 /** @cond INTERNAL_HIDDEN */
933 void z_i2c_transfer_signal_cb(const struct device *dev, int result, void *userdata);
934 /** @endcond */
935
936 /**
937 * @brief Perform data transfer to another I2C device in controller mode.
938 *
939 * This routine provides a generic interface to perform data transfer
940 * to another I2C device asynchronously with a k_poll_signal completion.
941 *
942 * @see i2c_transfer_cb()
943 * @funcprops \isr_ok
944 *
945 * @param dev Pointer to the device structure for an I2C controller
946 * driver configured in controller mode.
947 * @param msgs Array of messages to transfer, must live until callback completes.
948 * @param num_msgs Number of messages to transfer.
949 * @param addr Address of the I2C target device.
950 * @param sig Signal to notify of transfer completion.
951 *
952 * @retval 0 If successful.
953 * @retval -EIO General input / output error.
954 * @retval -ENOSYS If transfer async is not implemented
955 * @retval -EWOULDBLOCK If the device is temporarily busy doing another transfer
956 */
i2c_transfer_signal(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr,struct k_poll_signal * sig)957 static inline int i2c_transfer_signal(const struct device *dev,
958 struct i2c_msg *msgs,
959 uint8_t num_msgs,
960 uint16_t addr,
961 struct k_poll_signal *sig)
962 {
963 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
964
965 if (api->transfer_cb == NULL) {
966 return -ENOSYS;
967 }
968
969 return api->transfer_cb(dev, msgs, num_msgs, addr, z_i2c_transfer_signal_cb, sig);
970 }
971
972 #endif /* CONFIG_POLL */
973
974 #endif /* CONFIG_I2C_CALLBACK */
975
976
977 #if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
978
979 /**
980 * @brief Submit request(s) to an I2C device with RTIO
981 *
982 * @param iodev_sqe Prepared submissions queue entry connected to an iodev
983 * defined by I2C_DT_IODEV_DEFINE.
984 */
i2c_iodev_submit(struct rtio_iodev_sqe * iodev_sqe)985 static inline void i2c_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
986 {
987 const struct i2c_dt_spec *dt_spec = (const struct i2c_dt_spec *)iodev_sqe->sqe.iodev->data;
988 const struct device *dev = dt_spec->bus;
989 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
990
991 api->iodev_submit(dt_spec->bus, iodev_sqe);
992 }
993
994 extern const struct rtio_iodev_api i2c_iodev_api;
995
996 /**
997 * @brief Define an iodev for a given dt node on the bus
998 *
999 * These do not need to be shared globally but doing so
1000 * will save a small amount of memory.
1001 *
1002 * @param name Symbolic name of the iodev to define
1003 * @param node_id Devicetree node identifier
1004 */
1005 #define I2C_DT_IODEV_DEFINE(name, node_id) \
1006 const struct i2c_dt_spec _i2c_dt_spec_##name = \
1007 I2C_DT_SPEC_GET(node_id); \
1008 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
1009
1010 /**
1011 * @brief Copy the i2c_msgs into a set of RTIO requests
1012 *
1013 * @param r RTIO context
1014 * @param iodev RTIO IODev to target for the submissions
1015 * @param msgs Array of messages
1016 * @param num_msgs Number of i2c msgs in array
1017 *
1018 * @retval sqe Last submission in the queue added
1019 * @retval NULL Not enough memory in the context to copy the requests
1020 */
1021 struct rtio_sqe *i2c_rtio_copy(struct rtio *r,
1022 struct rtio_iodev *iodev,
1023 const struct i2c_msg *msgs,
1024 uint8_t num_msgs);
1025
1026 #endif /* CONFIG_I2C_RTIO */
1027
1028 /**
1029 * @brief Perform data transfer to another I2C device in controller mode.
1030 *
1031 * This is equivalent to:
1032 *
1033 * i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
1034 *
1035 * @param spec I2C specification from devicetree.
1036 * @param msgs Array of messages to transfer.
1037 * @param num_msgs Number of messages to transfer.
1038 *
1039 * @return a value from i2c_transfer()
1040 */
i2c_transfer_dt(const struct i2c_dt_spec * spec,struct i2c_msg * msgs,uint8_t num_msgs)1041 static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
1042 struct i2c_msg *msgs, uint8_t num_msgs)
1043 {
1044 return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
1045 }
1046
1047 /**
1048 * @brief Recover the I2C bus
1049 *
1050 * Attempt to recover the I2C bus.
1051 *
1052 * @param dev Pointer to the device structure for an I2C controller
1053 * driver configured in controller mode.
1054 * @retval 0 If successful
1055 * @retval -EBUSY If bus is not clear after recovery attempt.
1056 * @retval -EIO General input / output error.
1057 * @retval -ENOSYS If bus recovery is not implemented
1058 */
1059 __syscall int i2c_recover_bus(const struct device *dev);
1060
z_impl_i2c_recover_bus(const struct device * dev)1061 static inline int z_impl_i2c_recover_bus(const struct device *dev)
1062 {
1063 const struct i2c_driver_api *api =
1064 (const struct i2c_driver_api *)dev->api;
1065
1066 if (api->recover_bus == NULL) {
1067 return -ENOSYS;
1068 }
1069
1070 return api->recover_bus(dev);
1071 }
1072
1073 /**
1074 * @brief Registers the provided config as Target device of a controller.
1075 *
1076 * Enable I2C target mode for the 'dev' I2C bus driver using the provided
1077 * 'config' struct containing the functions and parameters to send bus
1078 * events. The I2C target will be registered at the address provided as 'address'
1079 * struct member. Addressing mode - 7 or 10 bit - depends on the 'flags'
1080 * struct member. Any I2C bus events related to the target mode will be passed
1081 * onto I2C target device driver via a set of callback functions provided in
1082 * the 'callbacks' struct member.
1083 *
1084 * Most of the existing hardware allows simultaneous support for controller
1085 * and target mode. This is however not guaranteed.
1086 *
1087 * @param dev Pointer to the device structure for an I2C controller
1088 * driver configured in target mode.
1089 * @param cfg Config struct with functions and parameters used by the I2C driver
1090 * to send bus events
1091 *
1092 * @retval 0 Is successful
1093 * @retval -EINVAL If parameters are invalid
1094 * @retval -EIO General input / output error.
1095 * @retval -ENOSYS If target mode is not implemented
1096 */
i2c_target_register(const struct device * dev,struct i2c_target_config * cfg)1097 static inline int i2c_target_register(const struct device *dev,
1098 struct i2c_target_config *cfg)
1099 {
1100 const struct i2c_driver_api *api =
1101 (const struct i2c_driver_api *)dev->api;
1102
1103 if (api->target_register == NULL) {
1104 return -ENOSYS;
1105 }
1106
1107 return api->target_register(dev, cfg);
1108 }
1109
1110 /**
1111 * @brief Unregisters the provided config as Target device
1112 *
1113 * This routine disables I2C target mode for the 'dev' I2C bus driver using
1114 * the provided 'config' struct containing the functions and parameters
1115 * to send bus events.
1116 *
1117 * @param dev Pointer to the device structure for an I2C controller
1118 * driver configured in target mode.
1119 * @param cfg Config struct with functions and parameters used by the I2C driver
1120 * to send bus events
1121 *
1122 * @retval 0 Is successful
1123 * @retval -EINVAL If parameters are invalid
1124 * @retval -ENOSYS If target mode is not implemented
1125 */
i2c_target_unregister(const struct device * dev,struct i2c_target_config * cfg)1126 static inline int i2c_target_unregister(const struct device *dev,
1127 struct i2c_target_config *cfg)
1128 {
1129 const struct i2c_driver_api *api =
1130 (const struct i2c_driver_api *)dev->api;
1131
1132 if (api->target_unregister == NULL) {
1133 return -ENOSYS;
1134 }
1135
1136 return api->target_unregister(dev, cfg);
1137 }
1138
1139 /**
1140 * @brief Instructs the I2C Target device to register itself to the I2C Controller
1141 *
1142 * This routine instructs the I2C Target device to register itself to the I2C
1143 * Controller via its parent controller's i2c_target_register() API.
1144 *
1145 * @param dev Pointer to the device structure for the I2C target
1146 * device (not itself an I2C controller).
1147 *
1148 * @retval 0 Is successful
1149 * @retval -EINVAL If parameters are invalid
1150 * @retval -EIO General input / output error.
1151 */
1152 __syscall int i2c_target_driver_register(const struct device *dev);
1153
z_impl_i2c_target_driver_register(const struct device * dev)1154 static inline int z_impl_i2c_target_driver_register(const struct device *dev)
1155 {
1156 const struct i2c_target_driver_api *api =
1157 (const struct i2c_target_driver_api *)dev->api;
1158
1159 return api->driver_register(dev);
1160 }
1161
1162 /**
1163 * @brief Instructs the I2C Target device to unregister itself from the I2C
1164 * Controller
1165 *
1166 * This routine instructs the I2C Target device to unregister itself from the I2C
1167 * Controller via its parent controller's i2c_target_register() API.
1168 *
1169 * @param dev Pointer to the device structure for the I2C target
1170 * device (not itself an I2C controller).
1171 *
1172 * @retval 0 Is successful
1173 * @retval -EINVAL If parameters are invalid
1174 */
1175 __syscall int i2c_target_driver_unregister(const struct device *dev);
1176
z_impl_i2c_target_driver_unregister(const struct device * dev)1177 static inline int z_impl_i2c_target_driver_unregister(const struct device *dev)
1178 {
1179 const struct i2c_target_driver_api *api =
1180 (const struct i2c_target_driver_api *)dev->api;
1181
1182 return api->driver_unregister(dev);
1183 }
1184
1185 /*
1186 * Derived i2c APIs -- all implemented in terms of i2c_transfer()
1187 */
1188
1189 /**
1190 * @brief Write a set amount of data to an I2C device.
1191 *
1192 * This routine writes a set amount of data synchronously.
1193 *
1194 * @param dev Pointer to the device structure for an I2C controller
1195 * driver configured in controller mode.
1196 * @param buf Memory pool from which the data is transferred.
1197 * @param num_bytes Number of bytes to write.
1198 * @param addr Address to the target I2C device for writing.
1199 *
1200 * @retval 0 If successful.
1201 * @retval -EIO General input / output error.
1202 */
i2c_write(const struct device * dev,const uint8_t * buf,uint32_t num_bytes,uint16_t addr)1203 static inline int i2c_write(const struct device *dev, const uint8_t *buf,
1204 uint32_t num_bytes, uint16_t addr)
1205 {
1206 struct i2c_msg msg;
1207
1208 msg.buf = (uint8_t *)buf;
1209 msg.len = num_bytes;
1210 msg.flags = I2C_MSG_WRITE | I2C_MSG_STOP;
1211
1212 return i2c_transfer(dev, &msg, 1, addr);
1213 }
1214
1215 /**
1216 * @brief Write a set amount of data to an I2C device.
1217 *
1218 * This is equivalent to:
1219 *
1220 * i2c_write(spec->bus, buf, num_bytes, spec->addr);
1221 *
1222 * @param spec I2C specification from devicetree.
1223 * @param buf Memory pool from which the data is transferred.
1224 * @param num_bytes Number of bytes to write.
1225 *
1226 * @return a value from i2c_write()
1227 */
i2c_write_dt(const struct i2c_dt_spec * spec,const uint8_t * buf,uint32_t num_bytes)1228 static inline int i2c_write_dt(const struct i2c_dt_spec *spec,
1229 const uint8_t *buf, uint32_t num_bytes)
1230 {
1231 return i2c_write(spec->bus, buf, num_bytes, spec->addr);
1232 }
1233
1234 /**
1235 * @brief Read a set amount of data from an I2C device.
1236 *
1237 * This routine reads a set amount of data synchronously.
1238 *
1239 * @param dev Pointer to the device structure for an I2C controller
1240 * driver configured in controller mode.
1241 * @param buf Memory pool that stores the retrieved data.
1242 * @param num_bytes Number of bytes to read.
1243 * @param addr Address of the I2C device being read.
1244 *
1245 * @retval 0 If successful.
1246 * @retval -EIO General input / output error.
1247 */
i2c_read(const struct device * dev,uint8_t * buf,uint32_t num_bytes,uint16_t addr)1248 static inline int i2c_read(const struct device *dev, uint8_t *buf,
1249 uint32_t num_bytes, uint16_t addr)
1250 {
1251 struct i2c_msg msg;
1252
1253 msg.buf = buf;
1254 msg.len = num_bytes;
1255 msg.flags = I2C_MSG_READ | I2C_MSG_STOP;
1256
1257 return i2c_transfer(dev, &msg, 1, addr);
1258 }
1259
1260 /**
1261 * @brief Read a set amount of data from an I2C device.
1262 *
1263 * This is equivalent to:
1264 *
1265 * i2c_read(spec->bus, buf, num_bytes, spec->addr);
1266 *
1267 * @param spec I2C specification from devicetree.
1268 * @param buf Memory pool that stores the retrieved data.
1269 * @param num_bytes Number of bytes to read.
1270 *
1271 * @return a value from i2c_read()
1272 */
i2c_read_dt(const struct i2c_dt_spec * spec,uint8_t * buf,uint32_t num_bytes)1273 static inline int i2c_read_dt(const struct i2c_dt_spec *spec,
1274 uint8_t *buf, uint32_t num_bytes)
1275 {
1276 return i2c_read(spec->bus, buf, num_bytes, spec->addr);
1277 }
1278
1279 /**
1280 * @brief Write then read data from an I2C device.
1281 *
1282 * This supports the common operation "this is what I want", "now give
1283 * it to me" transaction pair through a combined write-then-read bus
1284 * transaction.
1285 *
1286 * @param dev Pointer to the device structure for an I2C controller
1287 * driver configured in controller mode.
1288 * @param addr Address of the I2C device
1289 * @param write_buf Pointer to the data to be written
1290 * @param num_write Number of bytes to write
1291 * @param read_buf Pointer to storage for read data
1292 * @param num_read Number of bytes to read
1293 *
1294 * @retval 0 if successful
1295 * @retval negative on error.
1296 */
i2c_write_read(const struct device * dev,uint16_t addr,const void * write_buf,size_t num_write,void * read_buf,size_t num_read)1297 static inline int i2c_write_read(const struct device *dev, uint16_t addr,
1298 const void *write_buf, size_t num_write,
1299 void *read_buf, size_t num_read)
1300 {
1301 struct i2c_msg msg[2];
1302
1303 msg[0].buf = (uint8_t *)write_buf;
1304 msg[0].len = num_write;
1305 msg[0].flags = I2C_MSG_WRITE;
1306
1307 msg[1].buf = (uint8_t *)read_buf;
1308 msg[1].len = num_read;
1309 msg[1].flags = I2C_MSG_RESTART | I2C_MSG_READ | I2C_MSG_STOP;
1310
1311 return i2c_transfer(dev, msg, 2, addr);
1312 }
1313
1314 /**
1315 * @brief Write then read data from an I2C device.
1316 *
1317 * This is equivalent to:
1318 *
1319 * i2c_write_read(spec->bus, spec->addr,
1320 * write_buf, num_write,
1321 * read_buf, num_read);
1322 *
1323 * @param spec I2C specification from devicetree.
1324 * @param write_buf Pointer to the data to be written
1325 * @param num_write Number of bytes to write
1326 * @param read_buf Pointer to storage for read data
1327 * @param num_read Number of bytes to read
1328 *
1329 * @return a value from i2c_write_read()
1330 */
i2c_write_read_dt(const struct i2c_dt_spec * spec,const void * write_buf,size_t num_write,void * read_buf,size_t num_read)1331 static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec,
1332 const void *write_buf, size_t num_write,
1333 void *read_buf, size_t num_read)
1334 {
1335 return i2c_write_read(spec->bus, spec->addr,
1336 write_buf, num_write,
1337 read_buf, num_read);
1338 }
1339
1340 /**
1341 * @brief Read multiple bytes from an internal address of an I2C device.
1342 *
1343 * This routine reads multiple bytes from an internal address of an
1344 * I2C device synchronously.
1345 *
1346 * Instances of this may be replaced by i2c_write_read().
1347 *
1348 * @param dev Pointer to the device structure for an I2C controller
1349 * driver configured in controller mode.
1350 * @param dev_addr Address of the I2C device for reading.
1351 * @param start_addr Internal address from which the data is being read.
1352 * @param buf Memory pool that stores the retrieved data.
1353 * @param num_bytes Number of bytes being read.
1354 *
1355 * @retval 0 If successful.
1356 * @retval -EIO General input / output error.
1357 */
i2c_burst_read(const struct device * dev,uint16_t dev_addr,uint8_t start_addr,uint8_t * buf,uint32_t num_bytes)1358 static inline int i2c_burst_read(const struct device *dev,
1359 uint16_t dev_addr,
1360 uint8_t start_addr,
1361 uint8_t *buf,
1362 uint32_t num_bytes)
1363 {
1364 return i2c_write_read(dev, dev_addr,
1365 &start_addr, sizeof(start_addr),
1366 buf, num_bytes);
1367 }
1368
1369 /**
1370 * @brief Read multiple bytes from an internal address of an I2C device.
1371 *
1372 * This is equivalent to:
1373 *
1374 * i2c_burst_read(spec->bus, spec->addr, start_addr, buf, num_bytes);
1375 *
1376 * @param spec I2C specification from devicetree.
1377 * @param start_addr Internal address from which the data is being read.
1378 * @param buf Memory pool that stores the retrieved data.
1379 * @param num_bytes Number of bytes to read.
1380 *
1381 * @return a value from i2c_burst_read()
1382 */
i2c_burst_read_dt(const struct i2c_dt_spec * spec,uint8_t start_addr,uint8_t * buf,uint32_t num_bytes)1383 static inline int i2c_burst_read_dt(const struct i2c_dt_spec *spec,
1384 uint8_t start_addr,
1385 uint8_t *buf,
1386 uint32_t num_bytes)
1387 {
1388 return i2c_burst_read(spec->bus, spec->addr,
1389 start_addr, buf, num_bytes);
1390 }
1391
1392 /**
1393 * @brief Write multiple bytes to an internal address of an I2C device.
1394 *
1395 * This routine writes multiple bytes to an internal address of an
1396 * I2C device synchronously.
1397 *
1398 * @warning The combined write synthesized by this API may not be
1399 * supported on all I2C devices. Uses of this API may be made more
1400 * portable by replacing them with calls to i2c_write() passing a
1401 * buffer containing the combined address and data.
1402 *
1403 * @param dev Pointer to the device structure for an I2C controller
1404 * driver configured in controller mode.
1405 * @param dev_addr Address of the I2C device for writing.
1406 * @param start_addr Internal address to which the data is being written.
1407 * @param buf Memory pool from which the data is transferred.
1408 * @param num_bytes Number of bytes being written.
1409 *
1410 * @retval 0 If successful.
1411 * @retval -EIO General input / output error.
1412 */
i2c_burst_write(const struct device * dev,uint16_t dev_addr,uint8_t start_addr,const uint8_t * buf,uint32_t num_bytes)1413 static inline int i2c_burst_write(const struct device *dev,
1414 uint16_t dev_addr,
1415 uint8_t start_addr,
1416 const uint8_t *buf,
1417 uint32_t num_bytes)
1418 {
1419 struct i2c_msg msg[2];
1420
1421 msg[0].buf = &start_addr;
1422 msg[0].len = 1U;
1423 msg[0].flags = I2C_MSG_WRITE;
1424
1425 msg[1].buf = (uint8_t *)buf;
1426 msg[1].len = num_bytes;
1427 msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
1428
1429 return i2c_transfer(dev, msg, 2, dev_addr);
1430 }
1431
1432 /**
1433 * @brief Write multiple bytes to an internal address of an I2C device.
1434 *
1435 * This is equivalent to:
1436 *
1437 * i2c_burst_write(spec->bus, spec->addr, start_addr, buf, num_bytes);
1438 *
1439 * @param spec I2C specification from devicetree.
1440 * @param start_addr Internal address to which the data is being written.
1441 * @param buf Memory pool from which the data is transferred.
1442 * @param num_bytes Number of bytes being written.
1443 *
1444 * @return a value from i2c_burst_write()
1445 */
i2c_burst_write_dt(const struct i2c_dt_spec * spec,uint8_t start_addr,const uint8_t * buf,uint32_t num_bytes)1446 static inline int i2c_burst_write_dt(const struct i2c_dt_spec *spec,
1447 uint8_t start_addr,
1448 const uint8_t *buf,
1449 uint32_t num_bytes)
1450 {
1451 return i2c_burst_write(spec->bus, spec->addr,
1452 start_addr, buf, num_bytes);
1453 }
1454
1455 /**
1456 * @brief Read internal register of an I2C device.
1457 *
1458 * This routine reads the value of an 8-bit internal register of an I2C
1459 * device synchronously.
1460 *
1461 * @param dev Pointer to the device structure for an I2C controller
1462 * driver configured in controller mode.
1463 * @param dev_addr Address of the I2C device for reading.
1464 * @param reg_addr Address of the internal register being read.
1465 * @param value Memory pool that stores the retrieved register value.
1466 *
1467 * @retval 0 If successful.
1468 * @retval -EIO General input / output error.
1469 */
i2c_reg_read_byte(const struct device * dev,uint16_t dev_addr,uint8_t reg_addr,uint8_t * value)1470 static inline int i2c_reg_read_byte(const struct device *dev,
1471 uint16_t dev_addr,
1472 uint8_t reg_addr, uint8_t *value)
1473 {
1474 return i2c_write_read(dev, dev_addr,
1475 ®_addr, sizeof(reg_addr),
1476 value, sizeof(*value));
1477 }
1478
1479 /**
1480 * @brief Read internal register of an I2C device.
1481 *
1482 * This is equivalent to:
1483 *
1484 * i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
1485 *
1486 * @param spec I2C specification from devicetree.
1487 * @param reg_addr Address of the internal register being read.
1488 * @param value Memory pool that stores the retrieved register value.
1489 *
1490 * @return a value from i2c_reg_read_byte()
1491 */
i2c_reg_read_byte_dt(const struct i2c_dt_spec * spec,uint8_t reg_addr,uint8_t * value)1492 static inline int i2c_reg_read_byte_dt(const struct i2c_dt_spec *spec,
1493 uint8_t reg_addr, uint8_t *value)
1494 {
1495 return i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
1496 }
1497
1498 /**
1499 * @brief Write internal register of an I2C device.
1500 *
1501 * This routine writes a value to an 8-bit internal register of an I2C
1502 * device synchronously.
1503 *
1504 * @note This function internally combines the register and value into
1505 * a single bus transaction.
1506 *
1507 * @param dev Pointer to the device structure for an I2C controller
1508 * driver configured in controller mode.
1509 * @param dev_addr Address of the I2C device for writing.
1510 * @param reg_addr Address of the internal register being written.
1511 * @param value Value to be written to internal register.
1512 *
1513 * @retval 0 If successful.
1514 * @retval -EIO General input / output error.
1515 */
i2c_reg_write_byte(const struct device * dev,uint16_t dev_addr,uint8_t reg_addr,uint8_t value)1516 static inline int i2c_reg_write_byte(const struct device *dev,
1517 uint16_t dev_addr,
1518 uint8_t reg_addr, uint8_t value)
1519 {
1520 uint8_t tx_buf[2] = {reg_addr, value};
1521
1522 return i2c_write(dev, tx_buf, 2, dev_addr);
1523 }
1524
1525 /**
1526 * @brief Write internal register of an I2C device.
1527 *
1528 * This is equivalent to:
1529 *
1530 * i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
1531 *
1532 * @param spec I2C specification from devicetree.
1533 * @param reg_addr Address of the internal register being written.
1534 * @param value Value to be written to internal register.
1535 *
1536 * @return a value from i2c_reg_write_byte()
1537 */
i2c_reg_write_byte_dt(const struct i2c_dt_spec * spec,uint8_t reg_addr,uint8_t value)1538 static inline int i2c_reg_write_byte_dt(const struct i2c_dt_spec *spec,
1539 uint8_t reg_addr, uint8_t value)
1540 {
1541 return i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
1542 }
1543
1544 /**
1545 * @brief Update internal register of an I2C device.
1546 *
1547 * This routine updates the value of a set of bits from an 8-bit internal
1548 * register of an I2C device synchronously.
1549 *
1550 * @note If the calculated new register value matches the value that
1551 * was read this function will not generate a write operation.
1552 *
1553 * @param dev Pointer to the device structure for an I2C controller
1554 * driver configured in controller mode.
1555 * @param dev_addr Address of the I2C device for updating.
1556 * @param reg_addr Address of the internal register being updated.
1557 * @param mask Bitmask for updating internal register.
1558 * @param value Value for updating internal register.
1559 *
1560 * @retval 0 If successful.
1561 * @retval -EIO General input / output error.
1562 */
i2c_reg_update_byte(const struct device * dev,uint8_t dev_addr,uint8_t reg_addr,uint8_t mask,uint8_t value)1563 static inline int i2c_reg_update_byte(const struct device *dev,
1564 uint8_t dev_addr,
1565 uint8_t reg_addr, uint8_t mask,
1566 uint8_t value)
1567 {
1568 uint8_t old_value, new_value;
1569 int rc;
1570
1571 rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value);
1572 if (rc != 0) {
1573 return rc;
1574 }
1575
1576 new_value = (old_value & ~mask) | (value & mask);
1577 if (new_value == old_value) {
1578 return 0;
1579 }
1580
1581 return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value);
1582 }
1583
1584 /**
1585 * @brief Update internal register of an I2C device.
1586 *
1587 * This is equivalent to:
1588 *
1589 * i2c_reg_update_byte(spec->bus, spec->addr, reg_addr, mask, value);
1590 *
1591 * @param spec I2C specification from devicetree.
1592 * @param reg_addr Address of the internal register being updated.
1593 * @param mask Bitmask for updating internal register.
1594 * @param value Value for updating internal register.
1595 *
1596 * @return a value from i2c_reg_update_byte()
1597 */
i2c_reg_update_byte_dt(const struct i2c_dt_spec * spec,uint8_t reg_addr,uint8_t mask,uint8_t value)1598 static inline int i2c_reg_update_byte_dt(const struct i2c_dt_spec *spec,
1599 uint8_t reg_addr, uint8_t mask,
1600 uint8_t value)
1601 {
1602 return i2c_reg_update_byte(spec->bus, spec->addr,
1603 reg_addr, mask, value);
1604 }
1605
1606 #ifdef __cplusplus
1607 }
1608 #endif
1609
1610 /**
1611 * @}
1612 */
1613
1614 #include <syscalls/i2c.h>
1615
1616 #endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_H_ */
1617