1 /*
2 * Copyright (c) 2022 Renesas Electronics Corporation and/or its affiliates
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT renesas_smartbond_i2c
8
9 #include <errno.h>
10
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/drivers/pinctrl.h>
13 #include <DA1469xAB.h>
14 #include <da1469x_pd.h>
15 #include <zephyr/pm/device.h>
16 #include <zephyr/pm/policy.h>
17 #include <zephyr/pm/device_runtime.h>
18
19 #include <zephyr/logging/log.h>
20 LOG_MODULE_REGISTER(i2c_smartbond);
21
22 #include "i2c-priv.h"
23
24 struct i2c_smartbond_cfg {
25 I2C_Type *regs;
26 int periph_clock_config;
27 const struct pinctrl_dev_config *pcfg;
28 uint32_t bitrate;
29 };
30
31 struct i2c_smartbond_data {
32 struct k_spinlock lock;
33 struct i2c_msg *msgs;
34 uint8_t num_msgs;
35 uint32_t transmit_cnt, receive_cnt;
36 i2c_callback_t cb;
37 void *userdata;
38 #ifdef CONFIG_I2C_CALLBACK
39 k_spinlock_key_t spinlock_key;
40 #endif
41 };
42
43 #if defined(CONFIG_PM_DEVICE)
i2c_smartbond_pm_prevent_system_sleep(void)44 static inline void i2c_smartbond_pm_prevent_system_sleep(void)
45 {
46 /*
47 * Prevent the SoC from etering the normal sleep state as PDC does not support
48 * waking up the application core following I2C events.
49 */
50 pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
51 }
52
i2c_smartbond_pm_allow_system_sleep(void)53 static inline void i2c_smartbond_pm_allow_system_sleep(void)
54 {
55 /*
56 * Allow the SoC to enter the nornmal sleep state once I2C transactions are done.
57 */
58 pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
59 }
60 #endif
61
i2c_smartbond_pm_policy_state_lock_get(const struct device * dev)62 static inline void i2c_smartbond_pm_policy_state_lock_get(const struct device *dev)
63 {
64 #ifdef CONFIG_PM_DEVICE
65 #ifdef CONFIG_PM_DEVICE_RUNTIME
66 pm_device_runtime_get(dev);
67 #else
68 ARG_UNUSED(dev);
69 i2c_smartbond_pm_prevent_system_sleep();
70 #endif
71 #endif
72 }
73
i2c_smartbond_pm_policy_state_lock_put(const struct device * dev)74 static inline void i2c_smartbond_pm_policy_state_lock_put(const struct device *dev)
75 {
76 #ifdef CONFIG_PM_DEVICE
77 #ifdef CONFIG_PM_DEVICE_RUNTIME
78 pm_device_runtime_put(dev);
79 #else
80 ARG_UNUSED(dev);
81 i2c_smartbond_pm_allow_system_sleep();
82 #endif
83 #endif
84 }
85
i2c_smartbond_is_idle(const struct device * dev)86 static inline bool i2c_smartbond_is_idle(const struct device *dev)
87 {
88 const struct i2c_smartbond_cfg *config = dev->config;
89 uint32_t mask = I2C_I2C_STATUS_REG_I2C_ACTIVITY_Msk |
90 I2C_I2C_STATUS_REG_RFNE_Msk |
91 I2C_I2C_STATUS_REG_TFE_Msk;
92
93 return ((config->regs->I2C_STATUS_REG & mask) == I2C_I2C_STATUS_REG_TFE_Msk);
94 }
95
i2c_smartbond_disable_when_inactive(const struct device * dev)96 static void i2c_smartbond_disable_when_inactive(const struct device *dev)
97 {
98 const struct i2c_smartbond_cfg *config = dev->config;
99
100 if ((config->regs->I2C_ENABLE_REG & I2C_I2C_ENABLE_REG_I2C_EN_Msk)) {
101 while (!i2c_smartbond_is_idle(dev)) {
102 };
103 config->regs->I2C_ENABLE_REG &= ~I2C_I2C_ENABLE_REG_I2C_EN_Msk;
104 }
105 }
106
i2c_smartbond_apply_configure(const struct device * dev,uint32_t dev_config)107 static int i2c_smartbond_apply_configure(const struct device *dev, uint32_t dev_config)
108 {
109 const struct i2c_smartbond_cfg *config = dev->config;
110 struct i2c_smartbond_data *data = dev->data;
111 uint32_t con_reg = 0x0UL;
112 k_spinlock_key_t key = k_spin_lock(&data->lock);
113
114 /* Configure Speed (SCL frequency) */
115 switch (I2C_SPEED_GET(dev_config)) {
116 case I2C_SPEED_STANDARD:
117 con_reg |= 1UL << I2C_I2C_CON_REG_I2C_SPEED_Pos;
118 break;
119 case I2C_SPEED_FAST:
120 con_reg |= 2UL << I2C_I2C_CON_REG_I2C_SPEED_Pos;
121 break;
122 /* TODO: Currently 1 MHz, add switching to 96 MHz PLL sys_clk to support 3.4 Mbit/s */
123 /*
124 * case I2C_SPEED_HIGH:
125 * con_reg |= 3UL << I2C_I2C_CON_REG_I2C_SPEED_Pos;
126 * break;
127 */
128 default:
129 LOG_ERR("Speed not supported");
130 return -ENOTSUP;
131 }
132
133 /* Configure Mode */
134 if ((dev_config & I2C_MODE_CONTROLLER) == I2C_MODE_CONTROLLER) {
135 con_reg |=
136 I2C_I2C_CON_REG_I2C_MASTER_MODE_Msk | I2C_I2C_CON_REG_I2C_SLAVE_DISABLE_Msk;
137 } else {
138 LOG_ERR("Only I2C Controller mode supported");
139 return -ENOTSUP;
140 }
141
142 /* Enable sending RESTART as master */
143 con_reg |= I2C_I2C_CON_REG_I2C_RESTART_EN_Msk;
144
145 i2c_smartbond_disable_when_inactive(dev);
146
147 /* Write control register*/
148 config->regs->I2C_CON_REG = con_reg;
149
150 /* Reset interrupt mask */
151 config->regs->I2C_INTR_MASK_REG = 0x0000U;
152
153 config->regs->I2C_ENABLE_REG |= I2C_I2C_ENABLE_REG_I2C_EN_Msk;
154
155 k_spin_unlock(&data->lock, key);
156
157 return 0;
158 }
159
i2c_smartbond_configure(const struct device * dev,uint32_t dev_config)160 static int i2c_smartbond_configure(const struct device *dev, uint32_t dev_config)
161 {
162 int ret = 0;
163
164 pm_device_runtime_get(dev);
165 ret = i2c_smartbond_apply_configure(dev, dev_config);
166 pm_device_runtime_put(dev);
167
168 return ret;
169 }
170
i2c_smartbond_get_config(const struct device * dev,uint32_t * dev_config)171 static int i2c_smartbond_get_config(const struct device *dev, uint32_t *dev_config)
172 {
173 const struct i2c_smartbond_cfg *config = dev->config;
174 struct i2c_smartbond_data *data = data = dev->data;
175 uint32_t reg;
176 k_spinlock_key_t key = k_spin_lock(&data->lock);
177
178 pm_device_runtime_get(dev);
179 /* Read the value of the control register */
180 reg = config->regs->I2C_CON_REG;
181 pm_device_runtime_put(dev);
182
183 k_spin_unlock(&data->lock, key);
184
185 *dev_config = 0UL;
186
187 /* Check if I2C is in controller or target mode */
188 if ((reg & I2C_I2C_CON_REG_I2C_MASTER_MODE_Msk) &&
189 (reg & I2C_I2C_CON_REG_I2C_SLAVE_DISABLE_Msk)) {
190 *dev_config |= I2C_MODE_CONTROLLER;
191 } else if (!(reg & I2C_I2C_CON_REG_I2C_MASTER_MODE_Msk) &&
192 !(reg & I2C_I2C_CON_REG_I2C_SLAVE_DISABLE_Msk)) {
193 *dev_config &= ~I2C_MODE_CONTROLLER;
194 } else {
195 return -EIO;
196 }
197
198 /* Get the operating speed */
199 switch ((reg & I2C_I2C_CON_REG_I2C_SPEED_Msk) >> I2C_I2C_CON_REG_I2C_SPEED_Pos) {
200 case 1UL:
201 *dev_config |= I2C_SPEED_SET(I2C_SPEED_STANDARD);
202 break;
203 case 2UL:
204 *dev_config |= I2C_SPEED_SET(I2C_SPEED_FAST);
205 break;
206 case 3UL:
207 *dev_config |= I2C_SPEED_SET(I2C_SPEED_HIGH);
208 break;
209 default:
210 return -ERANGE;
211 }
212
213 return 0;
214 }
215
i2c_smartbond_set_target_address(const struct i2c_smartbond_cfg * const config,struct i2c_smartbond_data * data,const struct i2c_msg * const msg,uint16_t addr)216 static inline void i2c_smartbond_set_target_address(const struct i2c_smartbond_cfg *const config,
217 struct i2c_smartbond_data *data,
218 const struct i2c_msg *const msg, uint16_t addr)
219 {
220 /* Disable I2C Controller */
221 config->regs->I2C_ENABLE_REG &= ~I2C_I2C_ENABLE_REG_I2C_EN_Msk;
222 /* Configure addressing mode*/
223 if (msg->flags & I2C_MSG_ADDR_10_BITS) {
224 config->regs->I2C_CON_REG |= I2C_I2C_CON_REG_I2C_10BITADDR_MASTER_Msk;
225 } else {
226 config->regs->I2C_CON_REG &= ~(I2C_I2C_CON_REG_I2C_10BITADDR_MASTER_Msk);
227 }
228
229 /* Change the Target Address */
230 config->regs->I2C_TAR_REG = ((config->regs->I2C_TAR_REG & ~I2C_I2C_TAR_REG_IC_TAR_Msk) |
231 (addr & I2C_I2C_TAR_REG_IC_TAR_Msk));
232 /* Enable again the I2C to use the new address */
233 config->regs->I2C_ENABLE_REG |= I2C_I2C_ENABLE_REG_I2C_EN_Msk;
234 }
235
i2c_smartbond_set_msg_flags(struct i2c_msg * msgs,uint8_t num_msgs)236 static inline int i2c_smartbond_set_msg_flags(struct i2c_msg *msgs, uint8_t num_msgs)
237 {
238 struct i2c_msg *current, *next;
239
240 current = msgs;
241 for (uint8_t i = 1; i <= num_msgs; i++) {
242 if (i < num_msgs) {
243 next = current + 1;
244 if ((current->flags & I2C_MSG_RW_MASK) != (next->flags & I2C_MSG_RW_MASK)) {
245 next->flags |= I2C_MSG_RESTART;
246 }
247 if (current->flags & I2C_MSG_STOP) {
248 return -EINVAL;
249 }
250 }
251 current++;
252 }
253
254 return 0;
255 }
256
i2c_smartbond_prep_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)257 static inline int i2c_smartbond_prep_transfer(const struct device *dev, struct i2c_msg *msgs,
258 uint8_t num_msgs, uint16_t addr)
259 {
260 const struct i2c_smartbond_cfg *config = dev->config;
261 struct i2c_smartbond_data *data = dev->data;
262 int ret = 0;
263
264 ret = i2c_smartbond_set_msg_flags(msgs, num_msgs);
265 if (ret != 0) {
266 return ret;
267 }
268
269 i2c_smartbond_set_target_address(config, data, msgs, addr);
270
271 data->msgs = msgs;
272 data->num_msgs = num_msgs;
273 data->transmit_cnt = 0;
274 data->receive_cnt = 0;
275
276 return 0;
277 }
278
i2c_smartbond_tx(const struct i2c_smartbond_cfg * const config,struct i2c_smartbond_data * data)279 static inline int i2c_smartbond_tx(const struct i2c_smartbond_cfg *const config,
280 struct i2c_smartbond_data *data)
281 {
282 const bool rw = ((data->msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ);
283 int ret = 0;
284
285 if (!data->msgs->buf || data->msgs->len == 0) {
286 return -EINVAL;
287 }
288
289 /* Transmits data or read commands with correct flags */
290 while ((data->transmit_cnt < data->msgs->len) &&
291 (config->regs->I2C_STATUS_REG & I2C_I2C_STATUS_REG_TFNF_Msk)) {
292 config->regs->I2C_DATA_CMD_REG =
293 (rw ? I2C_I2C_DATA_CMD_REG_I2C_CMD_Msk
294 : (data->msgs->buf[data->transmit_cnt] &
295 I2C_I2C_DATA_CMD_REG_I2C_DAT_Msk)) |
296 ((data->transmit_cnt == 0) && (data->msgs->flags & I2C_MSG_RESTART)
297 ? I2C_I2C_DATA_CMD_REG_I2C_RESTART_Msk
298 : 0) |
299 ((data->transmit_cnt == (data->msgs->len - 1)) &&
300 (data->msgs->flags & I2C_MSG_STOP)
301 ? I2C_I2C_DATA_CMD_REG_I2C_STOP_Msk
302 : 0);
303 data->transmit_cnt++;
304
305 /* Return IO error if any of the abort flags are set */
306 if (config->regs->I2C_TX_ABRT_SOURCE_REG & 0x1FFFF) {
307 ret = -EIO;
308 }
309 }
310
311 if (config->regs->I2C_TX_ABRT_SOURCE_REG & 0x1FFFF) {
312 ret = -EIO;
313 }
314
315 if (ret) {
316 (void)config->regs->I2C_CLR_TX_ABRT_REG;
317 }
318
319 return ret;
320 }
321
i2c_smartbond_rx(const struct i2c_smartbond_cfg * const config,struct i2c_smartbond_data * data)322 static inline int i2c_smartbond_rx(const struct i2c_smartbond_cfg *const config,
323 struct i2c_smartbond_data *data)
324 {
325 int ret = 0;
326
327 if (!data->msgs->buf || data->msgs->len == 0) {
328 return -EINVAL;
329 }
330
331 /* Reads the data register until fifo is empty */
332 while ((data->receive_cnt < data->transmit_cnt) &&
333 (config->regs->I2C_STATUS_REG & I2C2_I2C2_STATUS_REG_RFNE_Msk)) {
334 data->msgs->buf[data->receive_cnt] =
335 config->regs->I2C_DATA_CMD_REG & I2C_I2C_DATA_CMD_REG_I2C_DAT_Msk;
336 data->receive_cnt++;
337 }
338
339 return ret;
340 }
341
i2c_smartbond_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)342 static int i2c_smartbond_transfer(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
343 uint16_t addr)
344 {
345 const struct i2c_smartbond_cfg *config = dev->config;
346 struct i2c_smartbond_data *data = dev->data;
347 int ret = 0;
348 k_spinlock_key_t key = k_spin_lock(&data->lock);
349
350 i2c_smartbond_pm_policy_state_lock_get(dev);
351
352 ret = i2c_smartbond_prep_transfer(dev, msgs, num_msgs, addr);
353 if (ret != 0) {
354 goto finish;
355 }
356
357 for (; data->num_msgs > 0; data->num_msgs--, data->msgs++) {
358 data->transmit_cnt = 0;
359 data->receive_cnt = 0;
360 if ((data->msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
361 /* Repeating transmit and receives until all data has been read */
362 while (data->receive_cnt < data->msgs->len) {
363 /* Transmit read commands */
364 ret = i2c_smartbond_tx(config, data);
365 if (ret < 0) {
366 goto finish;
367 }
368 /* Read received data */
369 ret = i2c_smartbond_rx(config, data);
370 if (ret < 0) {
371 goto finish;
372 }
373 }
374 } else {
375 while (data->transmit_cnt < data->msgs->len) {
376 /* Transmit data */
377 ret = i2c_smartbond_tx(config, data);
378 if (ret < 0) {
379 goto finish;
380 }
381 }
382 }
383 }
384
385 finish:
386 while (!i2c_smartbond_is_idle(dev)) {
387 };
388 i2c_smartbond_pm_policy_state_lock_put(dev);
389 k_spin_unlock(&data->lock, key);
390
391 return ret;
392 }
393
394 #ifdef CONFIG_I2C_CALLBACK
395
396 #define TX_FIFO_DEPTH 32
397
i2c_smartbond_enable_msg_interrupts(const struct i2c_smartbond_cfg * const config,struct i2c_smartbond_data * data)398 static int i2c_smartbond_enable_msg_interrupts(const struct i2c_smartbond_cfg *const config,
399 struct i2c_smartbond_data *data)
400 {
401 if ((data->msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
402 uint32_t remaining = data->msgs->len - data->receive_cnt;
403 uint32_t tx_space = TX_FIFO_DEPTH - config->regs->I2C_TXFLR_REG;
404 uint32_t rx_tl = ((remaining < tx_space) ? remaining : tx_space) - 1;
405
406 config->regs->I2C_RX_TL_REG = rx_tl & I2C_I2C_RX_TL_REG_RX_TL_Msk;
407 config->regs->I2C_INTR_MASK_REG |= I2C_I2C_INTR_MASK_REG_M_RX_FULL_Msk;
408 } else {
409 config->regs->I2C_INTR_MASK_REG &= ~I2C_I2C_INTR_MASK_REG_M_RX_FULL_Msk;
410 }
411
412 config->regs->I2C_TX_TL_REG = 0UL;
413 config->regs->I2C_INTR_MASK_REG |= I2C_I2C_INTR_MASK_REG_M_TX_EMPTY_Msk;
414
415 return 0;
416 }
417
i2c_smartbond_transfer_cb(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr,i2c_callback_t cb,void * userdata)418 static int i2c_smartbond_transfer_cb(const struct device *dev, struct i2c_msg *msgs,
419 uint8_t num_msgs, uint16_t addr, i2c_callback_t cb,
420 void *userdata)
421 {
422 const struct i2c_smartbond_cfg *config = dev->config;
423 struct i2c_smartbond_data *data = dev->data;
424 int ret = 0;
425 k_spinlock_key_t key = k_spin_lock(&data->lock);
426
427 if (cb == NULL) {
428 return -EINVAL;
429 }
430
431 if (data->cb != NULL) {
432 return -EWOULDBLOCK;
433 }
434
435 data->spinlock_key = key;
436 data->cb = cb;
437 data->userdata = userdata;
438
439 i2c_smartbond_pm_policy_state_lock_get(dev);
440
441 ret = i2c_smartbond_prep_transfer(dev, msgs, num_msgs, addr);
442 if (ret != 0) {
443 i2c_smartbond_pm_policy_state_lock_put(dev);
444 k_spin_unlock(&data->lock, key);
445 return ret;
446 }
447
448 i2c_smartbond_enable_msg_interrupts(config, data);
449
450 LOG_INF("async transfer started");
451
452 return 0;
453 }
454
isr_tx(const struct i2c_smartbond_cfg * config,struct i2c_smartbond_data * data)455 static inline void isr_tx(const struct i2c_smartbond_cfg *config, struct i2c_smartbond_data *data)
456 {
457 const bool rw = ((data->msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ);
458
459 while ((data->transmit_cnt < data->msgs->len) &&
460 (config->regs->I2C_STATUS_REG & I2C_I2C_STATUS_REG_TFNF_Msk)) {
461 config->regs->I2C_DATA_CMD_REG =
462 (rw ? I2C_I2C_DATA_CMD_REG_I2C_CMD_Msk
463 : (data->msgs->buf[data->transmit_cnt] &
464 I2C_I2C_DATA_CMD_REG_I2C_DAT_Msk)) |
465 ((data->transmit_cnt == 0) && (data->msgs->flags & I2C_MSG_RESTART)
466 ? I2C_I2C_DATA_CMD_REG_I2C_RESTART_Msk
467 : 0) |
468 ((data->transmit_cnt == (data->msgs->len - 1)) &&
469 (data->msgs->flags & I2C_MSG_STOP)
470 ? I2C_I2C_DATA_CMD_REG_I2C_STOP_Msk
471 : 0);
472 data->transmit_cnt++;
473 }
474 }
475
isr_rx(const struct i2c_smartbond_cfg * config,struct i2c_smartbond_data * data)476 static inline void isr_rx(const struct i2c_smartbond_cfg *config, struct i2c_smartbond_data *data)
477 {
478 while ((data->receive_cnt < data->transmit_cnt) &&
479 (config->regs->I2C_STATUS_REG & I2C2_I2C2_STATUS_REG_RFNE_Msk)) {
480 data->msgs->buf[data->receive_cnt] =
481 config->regs->I2C_DATA_CMD_REG & I2C_I2C_DATA_CMD_REG_I2C_DAT_Msk;
482 data->receive_cnt++;
483 }
484 }
485
i2c_smartbond_async_msg_done(const struct device * dev)486 static inline void i2c_smartbond_async_msg_done(const struct device *dev)
487 {
488 const struct i2c_smartbond_cfg *config = dev->config;
489 struct i2c_smartbond_data *data = dev->data;
490
491 data->num_msgs--;
492 if (data->num_msgs > 0) {
493 data->msgs++;
494 data->transmit_cnt = 0;
495 data->receive_cnt = 0;
496 i2c_smartbond_enable_msg_interrupts(config, data);
497 } else {
498 i2c_callback_t cb = data->cb;
499
500 data->msgs = NULL;
501 data->cb = NULL;
502 LOG_INF("async transfer finished");
503 cb(dev, 0, data->userdata);
504 while (!i2c_smartbond_is_idle(dev)) {
505 };
506 i2c_smartbond_pm_policy_state_lock_put(dev);
507 k_spin_unlock(&data->lock, data->spinlock_key);
508 }
509 }
510
i2c_smartbond_isr(const struct device * dev)511 static void i2c_smartbond_isr(const struct device *dev)
512 {
513 const struct i2c_smartbond_cfg *config = dev->config;
514 struct i2c_smartbond_data *data = dev->data;
515 uint32_t flags = config->regs->I2C_INTR_STAT_REG;
516
517 if (flags & I2C_I2C_INTR_STAT_REG_R_TX_EMPTY_Msk) {
518 isr_tx(config, data);
519 if (data->transmit_cnt == data->msgs->len) {
520 config->regs->I2C_INTR_MASK_REG &= ~I2C_I2C_INTR_MASK_REG_M_TX_EMPTY_Msk;
521 if ((data->msgs->flags & I2C_MSG_RW_MASK) != I2C_MSG_READ) {
522 i2c_smartbond_async_msg_done(dev);
523 }
524 }
525 }
526
527 if (flags & I2C_I2C_INTR_STAT_REG_R_RX_FULL_Msk) {
528 isr_rx(config, data);
529 if (data->receive_cnt == data->msgs->len) {
530 config->regs->I2C_INTR_MASK_REG &= ~I2C_I2C_INTR_MASK_REG_M_RX_FULL_Msk;
531 i2c_smartbond_async_msg_done(dev);
532 } else {
533 uint32_t remaining = data->msgs->len - data->receive_cnt;
534 uint32_t tx_space = 32 - config->regs->I2C_TXFLR_REG;
535 uint32_t rx_tl = ((remaining < tx_space) ? remaining : tx_space) - 1;
536
537 config->regs->I2C_RX_TL_REG = rx_tl & I2C_I2C_RX_TL_REG_RX_TL_Msk;
538 config->regs->I2C_INTR_MASK_REG |= I2C_I2C_INTR_MASK_REG_M_RX_FULL_Msk;
539 }
540 }
541 }
542
543 #define I2C_SMARTBOND_CONFIGURE(id) \
544 IRQ_CONNECT(DT_INST_IRQN(id), DT_INST_IRQ(id, priority), i2c_smartbond_isr, \
545 DEVICE_DT_INST_GET(id), 0); \
546 irq_enable(DT_INST_IRQN(id));
547 #else
548 #define I2C_SMARTBOND_CONFIGURE(id)
549 #endif
550
551 static DEVICE_API(i2c, i2c_smartbond_driver_api) = {
552 .configure = i2c_smartbond_configure,
553 .get_config = i2c_smartbond_get_config,
554 .transfer = i2c_smartbond_transfer,
555 #ifdef CONFIG_I2C_CALLBACK
556 .transfer_cb = i2c_smartbond_transfer_cb,
557 #endif
558 #ifdef CONFIG_I2C_RTIO
559 .iodev_submit = i2c_iodev_submit_fallback,
560 #endif
561 };
562
i2c_smartbond_resume(const struct device * dev)563 static int i2c_smartbond_resume(const struct device *dev)
564 {
565 const struct i2c_smartbond_cfg *config = dev->config;
566 int err;
567
568 config->regs->I2C_ENABLE_REG &= ~I2C_I2C_ENABLE_REG_I2C_EN_Msk;
569
570 /* Reset I2C CLK_SEL */
571 CRG_COM->RESET_CLK_COM_REG = (config->periph_clock_config << 1);
572 /* Set I2C CLK ENABLE */
573 CRG_COM->SET_CLK_COM_REG = config->periph_clock_config;
574
575 err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
576 if (err < 0) {
577 LOG_ERR("Failed to configure I2C pins");
578 return err;
579 }
580
581 return i2c_smartbond_apply_configure(dev,
582 I2C_MODE_CONTROLLER | i2c_map_dt_bitrate(config->bitrate));
583 }
584
585 #if defined(CONFIG_PM_DEVICE)
i2c_smartbond_suspend(const struct device * dev)586 static int i2c_smartbond_suspend(const struct device *dev)
587 {
588 int ret;
589 const struct i2c_smartbond_cfg *config = dev->config;
590
591 /* Disable the I2C digital block */
592 config->regs->I2C_ENABLE_REG &= ~I2C_I2C_ENABLE_REG_I2C_EN_Msk;
593 /* Gate I2C clocking */
594 CRG_COM->RESET_CLK_COM_REG = config->periph_clock_config;
595
596 ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
597 if (ret < 0) {
598 LOG_WRN("Fialid to configure the I2C pins to inactive state");
599 }
600
601 return ret;
602 }
603
i2c_smartbond_pm_action(const struct device * dev,enum pm_device_action action)604 static int i2c_smartbond_pm_action(const struct device *dev,
605 enum pm_device_action action)
606 {
607 int ret = 0;
608
609 switch (action) {
610 case PM_DEVICE_ACTION_RESUME:
611 #ifdef CONFIG_PM_DEVICE_RUNTIME
612 i2c_smartbond_pm_prevent_system_sleep();
613 #endif
614 /*
615 * Although the GPIO driver should already be initialized, make sure PD_COM
616 * is up and running before accessing the I2C block.
617 */
618 da1469x_pd_acquire(MCU_PD_DOMAIN_COM);
619 ret = i2c_smartbond_resume(dev);
620 break;
621 case PM_DEVICE_ACTION_SUSPEND:
622 ret = i2c_smartbond_suspend(dev);
623 /*
624 * Once the I2C block is turned off its power domain can
625 * be released, as well.
626 */
627 da1469x_pd_release(MCU_PD_DOMAIN_COM);
628 #ifdef CONFIG_PM_DEVICE_RUNTIME
629 i2c_smartbond_pm_allow_system_sleep();
630 #endif
631 break;
632 default:
633 return -ENOTSUP;
634 }
635
636 return ret;
637 }
638 #endif
639
640
i2c_smartbond_init(const struct device * dev)641 static int i2c_smartbond_init(const struct device *dev)
642 {
643 int ret;
644
645 #ifdef CONFIG_PM_DEVICE_RUNTIME
646 /* Make sure device state is marked as suspended */
647 pm_device_init_suspended(dev);
648
649 ret = pm_device_runtime_enable(dev);
650 #else
651 da1469x_pd_acquire(MCU_PD_DOMAIN_COM);
652 ret = i2c_smartbond_resume(dev);
653 #endif
654
655 return ret;
656 }
657
658 #define I2C_SMARTBOND_DEVICE(id) \
659 PM_DEVICE_DT_INST_DEFINE(id, i2c_smartbond_pm_action); \
660 PINCTRL_DT_INST_DEFINE(id); \
661 static const struct i2c_smartbond_cfg i2c_smartbond_##id##_cfg = { \
662 .regs = (I2C_Type *)DT_INST_REG_ADDR(id), \
663 .periph_clock_config = DT_INST_PROP(id, periph_clock_config), \
664 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(id), \
665 .bitrate = DT_INST_PROP_OR(id, clock_frequency, 100000)}; \
666 static struct i2c_smartbond_data i2c_smartbond_##id##_data = {.msgs = NULL, .cb = NULL}; \
667 static int i2c_smartbond_##id##_init(const struct device *dev) \
668 { \
669 int ret = i2c_smartbond_init(dev); \
670 I2C_SMARTBOND_CONFIGURE(id); \
671 return ret; \
672 } \
673 I2C_DEVICE_DT_INST_DEFINE(id, i2c_smartbond_##id##_init, PM_DEVICE_DT_INST_GET(id), \
674 &i2c_smartbond_##id##_data, \
675 &i2c_smartbond_##id##_cfg, POST_KERNEL, \
676 CONFIG_I2C_INIT_PRIORITY, &i2c_smartbond_driver_api);
677
678 DT_INST_FOREACH_STATUS_OKAY(I2C_SMARTBOND_DEVICE)
679