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 const struct i2c_driver_api i2c_sedi_apis = {.configure = i2c_sedi_api_configure,
116 .transfer = i2c_sedi_api_full_io};
117
118 #ifdef CONFIG_PM_DEVICE
119
i2c_sedi_suspend_device(const struct device * dev)120 static int i2c_sedi_suspend_device(const struct device *dev)
121 {
122 struct i2c_sedi_context *const context = dev->data;
123 int ret;
124
125 if (pm_device_is_busy(dev)) {
126 return -EBUSY;
127 }
128
129 ret = sedi_i2c_set_power(context->sedi_device, SEDI_POWER_SUSPEND);
130
131 if (ret != 0) {
132 return -EIO;
133 }
134
135 return 0;
136 }
137
i2c_sedi_resume_device_from_suspend(const struct device * dev)138 static int i2c_sedi_resume_device_from_suspend(const struct device *dev)
139 {
140 struct i2c_sedi_context *const context = dev->data;
141 int ret;
142
143 ret = sedi_i2c_set_power(context->sedi_device, SEDI_POWER_FULL);
144 if (ret != 0) {
145 return -EIO;
146 }
147
148 return 0;
149 }
150
i2c_sedi_pm_action(const struct device * dev,enum pm_device_action action)151 static int i2c_sedi_pm_action(const struct device *dev, enum pm_device_action action)
152 {
153 int ret = 0;
154
155 switch (action) {
156 case PM_DEVICE_ACTION_SUSPEND:
157 ret = i2c_sedi_suspend_device(dev);
158 break;
159 case PM_DEVICE_ACTION_RESUME:
160 ret = i2c_sedi_resume_device_from_suspend(dev);
161 break;
162
163 default:
164 ret = -ENOTSUP;
165 }
166
167 return ret;
168 }
169
170 #endif /* CONFIG_PM_DEVICE */
171
i2c_sedi_init(const struct device * dev)172 static int i2c_sedi_init(const struct device *dev)
173 {
174 int ret;
175 const struct i2c_sedi_config *const config = dev->config;
176 struct i2c_sedi_context *const context = dev->data;
177
178 DEVICE_MMIO_MAP(dev, K_MEM_CACHE_NONE);
179
180 ret = sedi_i2c_init(context->sedi_device, config->cb_sedi, DEVICE_MMIO_GET(dev));
181 if (ret != 0) {
182 return -EIO;
183 }
184
185 ret = sedi_i2c_set_power(context->sedi_device, SEDI_POWER_FULL);
186 if (ret != 0) {
187 return -EIO;
188 }
189
190 config->irq_config(dev);
191
192 return 0;
193 }
194
i2c_sedi_isr(const struct device * dev)195 static void i2c_sedi_isr(const struct device *dev)
196 {
197 struct i2c_sedi_context *const context = dev->data;
198
199 sedi_i2c_isr_handler(context->sedi_device);
200 }
201
202 #define I2C_SEDI_IRQ_FLAGS_SENSE0(n) 0
203 #define I2C_SEDI_IRQ_FLAGS_SENSE1(n) DT_INST_IRQ(n, sense)
204 #define I2C_SEDI_IRQ_FLAGS(n) _CONCAT(I2C_SEDI_IRQ_FLAGS_SENSE, DT_INST_IRQ_HAS_CELL(n, sense))(n)
205
206 #define I2C_DEVICE_INIT_SEDI(n) \
207 static K_SEM_DEFINE(i2c_sedi_sem_##n, 0, 1); \
208 static K_MUTEX_DEFINE(i2c_sedi_mutex_##n); \
209 static struct i2c_sedi_context i2c_sedi_data_##n = { \
210 .sedi_device = DT_INST_PROP(n, peripheral_id), \
211 .sem = &i2c_sedi_sem_##n, \
212 .mutex = &i2c_sedi_mutex_##n, \
213 }; \
214 static void i2c_sedi_callback_##n(const uint32_t event) \
215 { \
216 if (event == SEDI_I2C_EVENT_TRANSFER_DONE) { \
217 i2c_sedi_data_##n.err = 0; \
218 } else { \
219 i2c_sedi_data_##n.err = 1; \
220 } \
221 k_sem_give(i2c_sedi_data_##n.sem); \
222 }; \
223 static void i2c_sedi_irq_config_##n(const struct device *dev) \
224 { \
225 ARG_UNUSED(dev); \
226 IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), i2c_sedi_isr, \
227 DEVICE_DT_INST_GET(n), I2C_SEDI_IRQ_FLAGS(n)); \
228 irq_enable(DT_INST_IRQN(n)); \
229 }; \
230 static const struct i2c_sedi_config i2c_sedi_config_##n = { \
231 DEVICE_MMIO_ROM_INIT(DT_DRV_INST(n)), \
232 .cb_sedi = &i2c_sedi_callback_##n, \
233 .irq_config = &i2c_sedi_irq_config_##n, \
234 }; \
235 PM_DEVICE_DT_DEFINE(DT_NODELABEL(i2c##n), i2c_sedi_pm_action); \
236 I2C_DEVICE_DT_INST_DEFINE(n, i2c_sedi_init, PM_DEVICE_DT_GET(DT_NODELABEL(i2c##n)), \
237 &i2c_sedi_data_##n, &i2c_sedi_config_##n, PRE_KERNEL_2, \
238 CONFIG_I2C_INIT_PRIORITY, &i2c_sedi_apis);
239
240 DT_INST_FOREACH_STATUS_OKAY(I2C_DEVICE_INIT_SEDI)
241