1 /*
2 * Copyright (c) 2023 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT intel_sedi_i2c
8
9 #include <stdint.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/kernel.h>
12 #include <sedi_driver_i2c.h>
13 #include <zephyr/pm/device.h>
14
15 #define I2C_SEDI_TIMEOUT_MS (1000)
16
17 struct i2c_sedi_context {
18 DEVICE_MMIO_RAM;
19 int sedi_device;
20 struct k_sem *sem;
21 struct k_mutex *mutex;
22 int err;
23 uint16_t addr_10bit;
24 };
25
26 struct i2c_sedi_config {
27 DEVICE_MMIO_ROM;
28 sedi_i2c_event_cb_t cb_sedi;
29 void (*irq_config)(const struct device *dev);
30 };
31
i2c_sedi_api_configure(const struct device * dev,uint32_t dev_config)32 static int i2c_sedi_api_configure(const struct device *dev, uint32_t dev_config)
33 {
34 int ret;
35 int speed = I2C_SPEED_GET(dev_config);
36 int sedi_speed;
37 struct i2c_sedi_context *const context = dev->data;
38
39 context->addr_10bit = (dev_config & I2C_ADDR_10_BITS) ? SEDI_I2C_ADDRESS_10BIT : 0;
40
41 if (speed == I2C_SPEED_STANDARD) {
42 sedi_speed = SEDI_I2C_BUS_SPEED_STANDARD;
43 } else if (speed == I2C_SPEED_FAST) {
44 sedi_speed = SEDI_I2C_BUS_SPEED_FAST;
45 } else if (speed == I2C_SPEED_FAST_PLUS) {
46 sedi_speed = SEDI_I2C_BUS_SPEED_FAST_PLUS;
47 } else if (speed == I2C_SPEED_HIGH) {
48 sedi_speed = SEDI_I2C_BUS_SPEED_HIGH;
49 } else {
50 return -EINVAL;
51 }
52
53 k_mutex_lock(context->mutex, K_FOREVER);
54 ret = sedi_i2c_control(context->sedi_device, SEDI_I2C_BUS_SPEED, sedi_speed);
55 k_mutex_unlock(context->mutex);
56
57 if (ret != 0) {
58 return -EIO;
59 } else {
60 return 0;
61 }
62 }
63
i2c_sedi_api_full_io(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)64 static int i2c_sedi_api_full_io(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
65 uint16_t addr)
66 {
67 int ret = 0;
68 struct i2c_sedi_context *context = dev->data;
69
70 if (!num_msgs) {
71 return 0;
72 }
73 __ASSERT_NO_MSG(msgs);
74
75 k_mutex_lock(context->mutex, K_FOREVER);
76 pm_device_busy_set(dev);
77
78 for (int i = 0; i < num_msgs; i++) {
79 if ((msgs[i].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE) {
80 ret = sedi_i2c_master_write_async(
81 context->sedi_device, addr | context->addr_10bit, msgs[i].buf,
82 msgs[i].len, (msgs[i].flags & I2C_MSG_STOP) == 0x0);
83 } else {
84 ret = sedi_i2c_master_read_async(
85 context->sedi_device, addr | context->addr_10bit, msgs[i].buf,
86 msgs[i].len, (msgs[i].flags & I2C_MSG_STOP) == 0x0);
87 }
88 if (ret != 0) {
89 ret = -EIO;
90 break;
91 }
92
93 ret = k_sem_take(context->sem, K_MSEC(I2C_SEDI_TIMEOUT_MS));
94 if (ret != 0) {
95 break;
96 }
97
98 if (context->err != 0) {
99 ret = -EIO;
100 break;
101 }
102 }
103
104 if (ret != 0) {
105 /* Abort current transfer */
106 sedi_i2c_control(context->sedi_device, SEDI_I2C_ABORT_TRANSFER, 0);
107 ret = -EIO;
108 }
109 pm_device_busy_clear(dev);
110 k_mutex_unlock(context->mutex);
111
112 return ret;
113 }
114
115 static DEVICE_API(i2c, i2c_sedi_apis) = {
116 .configure = i2c_sedi_api_configure,
117 .transfer = i2c_sedi_api_full_io,
118 #ifdef CONFIG_I2C_RTIO
119 .iodev_submit = i2c_iodev_submit_fallback,
120 #endif
121 };
122
123 #ifdef CONFIG_PM_DEVICE
124
i2c_sedi_suspend_device(const struct device * dev)125 static int i2c_sedi_suspend_device(const struct device *dev)
126 {
127 struct i2c_sedi_context *const context = dev->data;
128 int ret;
129
130 if (pm_device_is_busy(dev)) {
131 return -EBUSY;
132 }
133
134 ret = sedi_i2c_set_power(context->sedi_device, SEDI_POWER_SUSPEND);
135
136 if (ret != 0) {
137 return -EIO;
138 }
139
140 return 0;
141 }
142
i2c_sedi_resume_device_from_suspend(const struct device * dev)143 static int i2c_sedi_resume_device_from_suspend(const struct device *dev)
144 {
145 struct i2c_sedi_context *const context = dev->data;
146 int ret;
147
148 ret = sedi_i2c_set_power(context->sedi_device, SEDI_POWER_FULL);
149 if (ret != 0) {
150 return -EIO;
151 }
152
153 return 0;
154 }
155
i2c_sedi_pm_action(const struct device * dev,enum pm_device_action action)156 static int i2c_sedi_pm_action(const struct device *dev, enum pm_device_action action)
157 {
158 int ret = 0;
159
160 switch (action) {
161 case PM_DEVICE_ACTION_SUSPEND:
162 ret = i2c_sedi_suspend_device(dev);
163 break;
164 case PM_DEVICE_ACTION_RESUME:
165 ret = i2c_sedi_resume_device_from_suspend(dev);
166 break;
167
168 default:
169 ret = -ENOTSUP;
170 }
171
172 return ret;
173 }
174
175 #endif /* CONFIG_PM_DEVICE */
176
i2c_sedi_init(const struct device * dev)177 static int i2c_sedi_init(const struct device *dev)
178 {
179 int ret;
180 const struct i2c_sedi_config *const config = dev->config;
181 struct i2c_sedi_context *const context = dev->data;
182
183 DEVICE_MMIO_MAP(dev, K_MEM_CACHE_NONE);
184
185 ret = sedi_i2c_init(context->sedi_device, config->cb_sedi, DEVICE_MMIO_GET(dev));
186 if (ret != 0) {
187 return -EIO;
188 }
189
190 ret = sedi_i2c_set_power(context->sedi_device, SEDI_POWER_FULL);
191 if (ret != 0) {
192 return -EIO;
193 }
194
195 config->irq_config(dev);
196
197 return 0;
198 }
199
i2c_sedi_isr(const struct device * dev)200 static void i2c_sedi_isr(const struct device *dev)
201 {
202 struct i2c_sedi_context *const context = dev->data;
203
204 sedi_i2c_isr_handler(context->sedi_device);
205 }
206
207 #define I2C_SEDI_IRQ_FLAGS_SENSE0(n) 0
208 #define I2C_SEDI_IRQ_FLAGS_SENSE1(n) DT_INST_IRQ(n, sense)
209 #define I2C_SEDI_IRQ_FLAGS(n) _CONCAT(I2C_SEDI_IRQ_FLAGS_SENSE, DT_INST_IRQ_HAS_CELL(n, sense))(n)
210
211 #define I2C_DEVICE_INIT_SEDI(n) \
212 static K_SEM_DEFINE(i2c_sedi_sem_##n, 0, 1); \
213 static K_MUTEX_DEFINE(i2c_sedi_mutex_##n); \
214 static struct i2c_sedi_context i2c_sedi_data_##n = { \
215 .sedi_device = DT_INST_PROP(n, peripheral_id), \
216 .sem = &i2c_sedi_sem_##n, \
217 .mutex = &i2c_sedi_mutex_##n, \
218 }; \
219 static void i2c_sedi_callback_##n(const uint32_t event) \
220 { \
221 if (event == SEDI_I2C_EVENT_TRANSFER_DONE) { \
222 i2c_sedi_data_##n.err = 0; \
223 } else { \
224 i2c_sedi_data_##n.err = 1; \
225 } \
226 k_sem_give(i2c_sedi_data_##n.sem); \
227 }; \
228 static void i2c_sedi_irq_config_##n(const struct device *dev) \
229 { \
230 ARG_UNUSED(dev); \
231 IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), i2c_sedi_isr, \
232 DEVICE_DT_INST_GET(n), I2C_SEDI_IRQ_FLAGS(n)); \
233 irq_enable(DT_INST_IRQN(n)); \
234 }; \
235 static const struct i2c_sedi_config i2c_sedi_config_##n = { \
236 DEVICE_MMIO_ROM_INIT(DT_DRV_INST(n)), \
237 .cb_sedi = &i2c_sedi_callback_##n, \
238 .irq_config = &i2c_sedi_irq_config_##n, \
239 }; \
240 PM_DEVICE_DT_DEFINE(DT_NODELABEL(i2c##n), i2c_sedi_pm_action); \
241 I2C_DEVICE_DT_INST_DEFINE(n, i2c_sedi_init, PM_DEVICE_DT_GET(DT_NODELABEL(i2c##n)), \
242 &i2c_sedi_data_##n, &i2c_sedi_config_##n, PRE_KERNEL_2, \
243 CONFIG_I2C_INIT_PRIORITY, &i2c_sedi_apis);
244
245 DT_INST_FOREACH_STATUS_OKAY(I2C_DEVICE_INIT_SEDI)
246