1 /*
2  * Copyright (c) 2018, Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/i2c.h>
8 #include <zephyr/drivers/pinctrl.h>
9 #include <zephyr/dt-bindings/i2c/i2c.h>
10 #include <zephyr/logging/log.h>
11 #include <zephyr/pm/device_runtime.h>
12 
13 #include "i2c_nrfx_twim_common.h"
14 
15 LOG_MODULE_DECLARE(i2c_nrfx_twim);
16 
i2c_nrfx_twim_recover_bus(const struct device * dev)17 int i2c_nrfx_twim_recover_bus(const struct device *dev)
18 {
19 	const struct i2c_nrfx_twim_common_config *config = dev->config;
20 	enum pm_device_state state;
21 	uint32_t scl_pin;
22 	uint32_t sda_pin;
23 	nrfx_err_t err;
24 
25 	scl_pin = nrf_twim_scl_pin_get(config->twim.p_twim);
26 	sda_pin = nrf_twim_sda_pin_get(config->twim.p_twim);
27 
28 	/* disable peripheral if active (required to release SCL/SDA lines) */
29 	(void)pm_device_state_get(dev, &state);
30 	if (state == PM_DEVICE_STATE_ACTIVE) {
31 		nrfx_twim_disable(&config->twim);
32 	}
33 
34 	err = nrfx_twim_bus_recover(scl_pin, sda_pin);
35 
36 	/* restore peripheral if it was active before */
37 	if (state == PM_DEVICE_STATE_ACTIVE) {
38 		(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
39 		nrfx_twim_enable(&config->twim);
40 	}
41 
42 	return (err == NRFX_SUCCESS ? 0 : -EBUSY);
43 }
44 
i2c_nrfx_twim_configure(const struct device * dev,uint32_t i2c_config)45 int i2c_nrfx_twim_configure(const struct device *dev, uint32_t i2c_config)
46 {
47 	const struct i2c_nrfx_twim_common_config *config = dev->config;
48 
49 	if (I2C_ADDR_10_BITS & i2c_config) {
50 		return -EINVAL;
51 	}
52 
53 	switch (I2C_SPEED_GET(i2c_config)) {
54 	case I2C_SPEED_STANDARD:
55 		nrf_twim_frequency_set(config->twim.p_twim, NRF_TWIM_FREQ_100K);
56 		break;
57 	case I2C_SPEED_FAST:
58 		nrf_twim_frequency_set(config->twim.p_twim, NRF_TWIM_FREQ_400K);
59 		break;
60 #if NRF_TWIM_HAS_1000_KHZ_FREQ
61 	case I2C_SPEED_FAST_PLUS:
62 		nrf_twim_frequency_set(config->twim.p_twim, NRF_TWIM_FREQ_1000K);
63 		break;
64 #endif
65 	default:
66 		LOG_ERR("unsupported speed");
67 		return -EINVAL;
68 	}
69 
70 	return 0;
71 }
72 
i2c_nrfx_twim_msg_transfer(const struct device * dev,uint8_t flags,uint8_t * buf,size_t buf_len,uint16_t i2c_addr)73 int i2c_nrfx_twim_msg_transfer(const struct device *dev, uint8_t flags, uint8_t *buf,
74 			       size_t buf_len, uint16_t i2c_addr)
75 {
76 	const struct i2c_nrfx_twim_common_config *config = dev->config;
77 	nrfx_twim_xfer_desc_t cur_xfer = {
78 		.address = i2c_addr,
79 		.type = (flags & I2C_MSG_READ) ? NRFX_TWIM_XFER_RX : NRFX_TWIM_XFER_TX,
80 		.p_primary_buf = buf,
81 		.primary_length = buf_len,
82 	};
83 	nrfx_err_t res;
84 	int ret = 0;
85 
86 	if (buf_len > config->max_transfer_size) {
87 		LOG_ERR("Trying to transfer more than the maximum size "
88 			"for this device: %d > %d",
89 			buf_len, config->max_transfer_size);
90 		return -ENOSPC;
91 	}
92 
93 	res = nrfx_twim_xfer(&config->twim, &cur_xfer,
94 			     (flags & I2C_MSG_STOP) ? 0 : NRFX_TWIM_FLAG_TX_NO_STOP);
95 	if (res != NRFX_SUCCESS) {
96 		if (res == NRFX_ERROR_BUSY) {
97 			ret = -EBUSY;
98 		} else {
99 			ret = -EIO;
100 		}
101 	}
102 	return ret;
103 }
104 
twim_nrfx_pm_action(const struct device * dev,enum pm_device_action action)105 int twim_nrfx_pm_action(const struct device *dev, enum pm_device_action action)
106 {
107 	const struct i2c_nrfx_twim_common_config *config = dev->config;
108 
109 	switch (action) {
110 	case PM_DEVICE_ACTION_RESUME:
111 		(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
112 		nrfx_twim_enable(&config->twim);
113 		break;
114 	case PM_DEVICE_ACTION_SUSPEND:
115 		nrfx_twim_disable(&config->twim);
116 		(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
117 		break;
118 	default:
119 		return -ENOTSUP;
120 	}
121 
122 	return 0;
123 }
124 
i2c_nrfx_twim_common_init(const struct device * dev)125 int i2c_nrfx_twim_common_init(const struct device *dev)
126 {
127 	const struct i2c_nrfx_twim_common_config *config = dev->config;
128 
129 	config->irq_connect();
130 
131 	(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
132 
133 	if (nrfx_twim_init(&config->twim, &config->twim_config, config->event_handler,
134 			   (void *)dev) != NRFX_SUCCESS) {
135 		LOG_ERR("Failed to initialize device: %s", dev->name);
136 		return -EIO;
137 	}
138 
139 	return pm_device_driver_init(dev, twim_nrfx_pm_action);
140 }
141