Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
mctp_i2c_gpio_controller.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 */
7
13
14#ifndef ZEPHYR_INCLUDE_PMCI_MCTP_MCTP_I2C_GPIO_CONTROLLER_H_
15#define ZEPHYR_INCLUDE_PMCI_MCTP_MCTP_I2C_GPIO_CONTROLLER_H_
16
17#include <stdint.h>
18#include <zephyr/kernel.h>
19#include <zephyr/device.h>
20#include <zephyr/drivers/i2c.h>
21#include <zephyr/drivers/gpio.h>
23#include <zephyr/rtio/rtio.h>
25
26#include <libmctp.h>
27#include <stdatomic.h>
28
30
32
33struct mctp_i2c_gpio_controller_cb {
34 struct mpsc_node q;
35 struct gpio_callback callback;
36 struct mctp_binding_i2c_gpio_controller *binding;
37 uint8_t index;
38};
39
41
47 struct mctp_binding binding;
48 const struct device *i2c;
49
50 uint8_t rx_buf_len;
51 uint8_t rx_buf[MCTP_I2C_GPIO_MAX_PKT_SIZE];
52 uint8_t tx_buf[MCTP_I2C_GPIO_MAX_PKT_SIZE];
53
54 struct mctp_pktbuf *rx_pkt;
55
56 /* Avoid needing a thread or work queue task by using RTIO */
57 struct k_sem *tx_lock;
58 struct rtio *r_tx;
59
60 /* Mapping of endpoints to i2c targets with gpios */
61 size_t num_endpoints;
62 const uint8_t *endpoint_ids;
63 const struct rtio_iodev **endpoint_iodevs;
64 const struct gpio_dt_spec *endpoint_gpios;
65 struct mctp_i2c_gpio_controller_cb *endpoint_gpio_cbs;
66
67 /* Ongoing request from a particular gpio */
68 struct k_spinlock rx_lock;
69 struct rtio *r_rx;
70 struct mpsc rx_q;
71 struct mctp_i2c_gpio_controller_cb *inflight_rx;
72
73 uint8_t tx_storage[MCTP_PKTBUF_SIZE(MCTP_I2C_GPIO_MAX_PKT_SIZE)] PKTBUF_STORAGE_ALIGN;
74
76};
77
79int mctp_i2c_gpio_controller_start(struct mctp_binding *binding);
80int mctp_i2c_gpio_controller_tx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
81
82#define MCTP_I2C_GPIO_CONTROLLER_IODEV_NAME(_idx, _name) _name##_idx
83
84#define MCTP_I2C_GPIO_CONTROLLER_IODEV_DEFINE(_node_id, addrs, _idx, _name) \
85 I2C_IODEV_DEFINE(MCTP_I2C_GPIO_CONTROLLER_IODEV_NAME(_idx, _name), \
86 DT_PHANDLE(_node_id, i2c), \
87 DT_PROP_BY_IDX(_node_id, addrs, _idx));
88
89
90#define MCTP_I2C_GPIO_CONTROLLER_DEFINE_GPIOS(_node_id, _name) \
91 const struct gpio_dt_spec _name##_endpoint_gpios[] = { \
92 DT_FOREACH_PROP_ELEM_SEP(_node_id, endpoint_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,)) \
93 }
94
95#define MCTP_I2C_GPIO_CONTROLLER_DEFINE_IDS(_node_id, _name) \
96 const uint8_t _name##_endpoint_ids[] = DT_PROP(_node_id, endpoint_ids);
97
98#define MCTP_I2C_GPIO_CONTROLLER_DEFINE_GPIO_CBS(_node_id, _name) \
99 struct mctp_i2c_gpio_controller_cb \
100 _name##_endpoint_gpio_cbs[DT_PROP_LEN(_node_id, endpoint_ids)]
101
102
103#define MCTP_I2C_GPIO_CONTROLLER_DEFINE_IODEVS(_node_id, _name) \
104 DT_FOREACH_PROP_ELEM_VARGS(_node_id, endpoint_addrs, \
105 MCTP_I2C_GPIO_CONTROLLER_IODEV_DEFINE, _name) \
106 const struct rtio_iodev *_name##_endpoint_iodevs[] = { \
107 LISTIFY(DT_PROP_LEN(_node_id, endpoint_ids), &MCTP_I2C_GPIO_CONTROLLER_IODEV_NAME, \
108 (,), _name) \
109 }
110
112
135#define MCTP_I2C_GPIO_CONTROLLER_DT_DEFINE(_name, _node_id) \
136 RTIO_DEFINE(_name##_rtio_tx, 5, 5); \
137 RTIO_DEFINE(_name##_rtio_rx, 5, 5); \
138 MCTP_I2C_GPIO_CONTROLLER_DEFINE_IODEVS(_node_id, _name); \
139 MCTP_I2C_GPIO_CONTROLLER_DEFINE_GPIOS(_node_id, _name); \
140 MCTP_I2C_GPIO_CONTROLLER_DEFINE_IDS(_node_id, _name); \
141 MCTP_I2C_GPIO_CONTROLLER_DEFINE_GPIO_CBS(_node_id, _name); \
142 K_SEM_DEFINE(_name##_tx_lock, 1, 1); \
143 struct mctp_binding_i2c_gpio_controller _name = { \
144 .binding = { \
145 .name = STRINGIFY(_name), .version = 1, \
146 .start = mctp_i2c_gpio_controller_start, \
147 .tx = mctp_i2c_gpio_controller_tx, \
148 .pkt_size = MCTP_I2C_GPIO_MAX_PKT_SIZE, \
149 .tx_storage = _name.tx_storage, \
150 }, \
151 .i2c = DEVICE_DT_GET(DT_PHANDLE(_node_id, i2c)), \
152 .num_endpoints = DT_PROP_LEN(_node_id, endpoint_ids), \
153 .endpoint_ids = _name##_endpoint_ids, \
154 .endpoint_gpios = _name##_endpoint_gpios, \
155 .endpoint_gpio_cbs = _name##_endpoint_gpio_cbs, \
156 .endpoint_iodevs = _name##_endpoint_iodevs, \
157 .r_tx = &_name##_rtio_tx, \
158 .r_rx = &_name##_rtio_rx, \
159 .tx_lock = &_name##_tx_lock, \
160 };
161
162#endif /* ZEPHYR_INCLUDE_PMCI_MCTP_MCTP_I2C_GPIO_CONTROLLER_H_ */
Main header file for GPIO driver API.
Main header file for I2C (Inter-Integrated Circuit) driver API.
Public kernel APIs.
Internal definitions shared by the MCTP I2C GPIO controller and target bindings.
A wait-free intrusive multi producer single consumer (MPSC) queue using a singly linked list.
Real-Time IO device API for moving bytes with low effort.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
Container for GPIO pin information specified in devicetree.
Definition gpio.h:296
Semaphore structure.
Definition kernel.h:3798
Kernel Spin Lock.
Definition spinlock.h:45
An MCTP binding for Zephyr's I2C interface using GPIO.
Definition mctp_i2c_gpio_controller.h:45
MPSC Queue.
Definition mpsc_lockfree.h:86
An IO device with a function table for submitting requests.
Definition iodev.h:48
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:70