1 /*
2  * Copyright (c) 2016 Freescale Semiconductor, Inc.
3  * Copyright 2019-2023, NXP
4  * Copyright (c) 2022 Vestas Wind Systems A/S
5  * Copyright (c) 2024 Intel Corporation
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #define DT_DRV_COMPAT nxp_lpi2c
11 
12 #include <errno.h>
13 #include <zephyr/drivers/i2c.h>
14 #include <zephyr/drivers/i2c/rtio.h>
15 #include <zephyr/drivers/clock_control.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/irq.h>
18 #include <fsl_lpi2c.h>
19 
20 #include <zephyr/drivers/pinctrl.h>
21 
22 #ifdef CONFIG_I2C_MCUX_LPI2C_BUS_RECOVERY
23 #include "i2c_bitbang.h"
24 #include <zephyr/drivers/gpio.h>
25 #endif /* CONFIG_I2C_MCUX_LPI2C_BUS_RECOVERY */
26 
27 #include <zephyr/logging/log.h>
28 LOG_MODULE_REGISTER(mcux_lpi2c);
29 
30 #include "i2c-priv.h"
31 /* Wait for the duration of 12 bits to detect a NAK after a bus
32  * address scan.  (10 appears sufficient, 20% safety factor.)
33  */
34 #define SCAN_DELAY_US(baudrate) (12 * USEC_PER_SEC / baudrate)
35 
36 /* Required by DEVICE_MMIO_NAMED_* macros */
37 #define DEV_CFG(_dev) \
38 	((const struct mcux_lpi2c_config *)(_dev)->config)
39 #define DEV_DATA(_dev) ((struct mcux_lpi2c_data *)(_dev)->data)
40 
41 struct mcux_lpi2c_config {
42 	DEVICE_MMIO_NAMED_ROM(reg_base);
43 	const struct device *clock_dev;
44 	clock_control_subsys_t clock_subsys;
45 	void (*irq_config_func)(const struct device *dev);
46 	uint32_t bitrate;
47 	uint32_t bus_idle_timeout_ns;
48 	const struct pinctrl_dev_config *pincfg;
49 #ifdef CONFIG_I2C_MCUX_LPI2C_BUS_RECOVERY
50 	struct gpio_dt_spec scl;
51 	struct gpio_dt_spec sda;
52 #endif /* CONFIG_I2C_MCUX_LPI2C_BUS_RECOVERY */
53 };
54 
55 struct mcux_lpi2c_data {
56 	DEVICE_MMIO_NAMED_RAM(reg_base);
57 	lpi2c_master_handle_t handle;
58 	struct i2c_rtio *ctx;
59 	lpi2c_master_transfer_t transfer;
60 #ifdef CONFIG_I2C_TARGET
61 	lpi2c_slave_handle_t target_handle;
62 	struct i2c_target_config *target_cfg;
63 	bool target_attached;
64 	bool first_tx;
65 	bool read_active;
66 	bool send_ack;
67 #endif
68 };
69 
mcux_lpi2c_configure(const struct device * dev,uint32_t dev_config_raw)70 static int mcux_lpi2c_configure(const struct device *dev,
71 				uint32_t dev_config_raw)
72 {
73 	struct i2c_rtio *const ctx = ((struct mcux_lpi2c_data *)
74 		dev->data)->ctx;
75 
76 	return i2c_rtio_configure(ctx, dev_config_raw);
77 }
78 
79 
mcux_lpi2c_do_configure(const struct device * dev,uint32_t dev_config_raw)80 static int  mcux_lpi2c_do_configure(const struct device *dev, uint32_t dev_config_raw)
81 {
82 	const struct mcux_lpi2c_config *config = dev->config;
83 	LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
84 	uint32_t clock_freq;
85 	uint32_t baudrate;
86 
87 	if (!(I2C_MODE_CONTROLLER & dev_config_raw)) {
88 		return -EINVAL;
89 	}
90 
91 	if (I2C_ADDR_10_BITS & dev_config_raw) {
92 		return -EINVAL;
93 	}
94 
95 	switch (I2C_SPEED_GET(dev_config_raw)) {
96 	case I2C_SPEED_STANDARD:
97 		baudrate = KHZ(100);
98 		break;
99 	case I2C_SPEED_FAST:
100 		baudrate = KHZ(400);
101 		break;
102 	case I2C_SPEED_FAST_PLUS:
103 		baudrate = MHZ(1);
104 		break;
105 	default:
106 		return -EINVAL;
107 	}
108 
109 	if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
110 				   &clock_freq)) {
111 		return -EINVAL;
112 	}
113 
114 	LPI2C_MasterSetBaudRate(base, clock_freq, baudrate);
115 
116 	return 0;
117 }
118 
119 
mcux_lpi2c_convert_flags(int msg_flags)120 static uint32_t mcux_lpi2c_convert_flags(int msg_flags)
121 {
122 	uint32_t flags = 0U;
123 
124 	if (!(msg_flags & I2C_MSG_STOP)) {
125 		flags |= kLPI2C_TransferNoStopFlag;
126 	}
127 
128 	if (msg_flags & I2C_MSG_RESTART) {
129 		flags |= kLPI2C_TransferRepeatedStartFlag;
130 	}
131 
132 	return flags;
133 }
134 
mcux_lpi2c_msg_start(const struct device * dev,uint8_t flags,uint8_t * buf,size_t buf_len,uint16_t i2c_addr)135 static bool mcux_lpi2c_msg_start(const struct device *dev, uint8_t flags,
136 				 uint8_t *buf, size_t buf_len, uint16_t i2c_addr)
137 {
138 	struct mcux_lpi2c_data *data = dev->data;
139 	struct i2c_rtio *ctx = data->ctx;
140 	LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
141 	lpi2c_master_transfer_t *transfer = &data->transfer;
142 	status_t status;
143 
144 	if (I2C_MSG_ADDR_10_BITS & flags) {
145 		return i2c_rtio_complete(ctx, -ENOTSUP);
146 	}
147 
148 	/* Initialize the transfer descriptor */
149 	transfer->flags = mcux_lpi2c_convert_flags(flags);
150 
151 	/* Prevent the controller to send a start condition between
152 	 * messages, except if explicitly requested.
153 	 */
154 	if (ctx->txn_curr != ctx->txn_head && !(flags & I2C_MSG_RESTART)) {
155 		transfer->flags |= kLPI2C_TransferNoStartFlag;
156 	}
157 
158 	transfer->slaveAddress = i2c_addr;
159 	transfer->direction = (flags & I2C_MSG_READ)
160 		? kLPI2C_Read : kLPI2C_Write;
161 	transfer->subaddress = 0;
162 	transfer->subaddressSize = 0;
163 	transfer->data = buf;
164 	transfer->dataSize = buf_len;
165 
166 	/* Start the transfer */
167 	status = LPI2C_MasterTransferNonBlocking(base,
168 			&data->handle, transfer);
169 
170 	/* Return an error if the transfer didn't start successfully
171 	 * e.g., if the bus was busy
172 	 */
173 	if (status != kStatus_Success) {
174 		LPI2C_MasterTransferAbort(base, &data->handle);
175 		return i2c_rtio_complete(ctx, -EIO);
176 	}
177 
178 	return false;
179 }
180 
181 static void mcux_lpi2c_complete(const struct device *dev, int status);
182 
mcux_lpi2c_start(const struct device * dev)183 static bool mcux_lpi2c_start(const struct device *dev)
184 {
185 	struct mcux_lpi2c_data *data = dev->data;
186 	struct i2c_rtio *ctx = data->ctx;
187 	struct rtio_sqe *sqe = &ctx->txn_curr->sqe;
188 	struct i2c_dt_spec *dt_spec = sqe->iodev->data;
189 
190 	int res = 0;
191 
192 	switch (sqe->op) {
193 	case RTIO_OP_RX:
194 		return mcux_lpi2c_msg_start(dev, I2C_MSG_READ | sqe->iodev_flags,
195 					    sqe->rx.buf, sqe->rx.buf_len, dt_spec->addr);
196 	case RTIO_OP_TINY_TX:
197 		return mcux_lpi2c_msg_start(dev, I2C_MSG_WRITE | sqe->iodev_flags,
198 					    (uint8_t *)sqe->tiny_tx.buf, sqe->tiny_tx.buf_len,
199 					    dt_spec->addr);
200 	case RTIO_OP_TX:
201 		return mcux_lpi2c_msg_start(dev, I2C_MSG_WRITE | sqe->iodev_flags,
202 					    (uint8_t *)sqe->tx.buf, sqe->tx.buf_len,
203 					    dt_spec->addr);
204 	case RTIO_OP_I2C_CONFIGURE:
205 		res = mcux_lpi2c_do_configure(dev, sqe->i2c_config);
206 		return i2c_rtio_complete(data->ctx, res);
207 	default:
208 		LOG_ERR("Invalid op code %d for submission %p\n", sqe->op, (void *)sqe);
209 		return i2c_rtio_complete(data->ctx, -EINVAL);
210 	}
211 }
212 
mcux_lpi2c_complete(const struct device * dev,status_t status)213 static void mcux_lpi2c_complete(const struct device *dev, status_t status)
214 {
215 	const struct mcux_lpi2c_config *config = dev->config;
216 	struct mcux_lpi2c_data *data = dev->data;
217 	LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
218 	struct i2c_rtio *const ctx = data->ctx;
219 
220 	int ret = 0;
221 
222 	/* Return an error if the transfer didn't complete
223 	 * successfully. e.g., nak, timeout, lost arbitration
224 	 */
225 	if (status != kStatus_Success) {
226 		LPI2C_MasterTransferAbort(base, &data->handle);
227 		ret = -EIO;
228 		goto out;
229 	}
230 
231 	if (data->transfer.dataSize == 0) {
232 		k_busy_wait(SCAN_DELAY_US(config->bitrate));
233 		if (0 != (base->MSR & LPI2C_MSR_NDF_MASK)) {
234 			LPI2C_MasterTransferAbort(base, &data->handle);
235 			ret = -EIO;
236 			goto out;
237 		}
238 	}
239 
240 out:
241 	if (i2c_rtio_complete(ctx, ret)) {
242 		mcux_lpi2c_start(dev);
243 	}
244 }
245 
mcux_lpi2c_submit(const struct device * dev,struct rtio_iodev_sqe * iodev_sqe)246 static void mcux_lpi2c_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
247 {
248 	struct mcux_lpi2c_data *data = dev->data;
249 	struct i2c_rtio *const ctx = data->ctx;
250 
251 	if (i2c_rtio_submit(ctx, iodev_sqe)) {
252 		mcux_lpi2c_start(dev);
253 	}
254 }
255 
256 
mcux_lpi2c_master_transfer_callback(LPI2C_Type * base,lpi2c_master_handle_t * handle,status_t status,void * userData)257 static void mcux_lpi2c_master_transfer_callback(LPI2C_Type *base,
258 						lpi2c_master_handle_t *handle,
259 						status_t status, void *userData)
260 {
261 	ARG_UNUSED(handle);
262 	ARG_UNUSED(base);
263 
264 	const struct device *dev = userData;
265 
266 	mcux_lpi2c_complete(dev, status);
267 }
268 
mcux_lpi2c_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)269 static int mcux_lpi2c_transfer(const struct device *dev, struct i2c_msg *msgs,
270 				   uint8_t num_msgs, uint16_t addr)
271 {
272 	struct i2c_rtio *const ctx = ((struct mcux_lpi2c_data *)
273 		dev->data)->ctx;
274 
275 	return i2c_rtio_transfer(ctx, msgs, num_msgs, addr);
276 }
277 
mcux_lpi2c_isr(const struct device * dev)278 static void mcux_lpi2c_isr(const struct device *dev)
279 {
280 	struct mcux_lpi2c_data *data = dev->data;
281 	LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
282 
283 	LPI2C_MasterTransferHandleIRQ(base, &data->handle);
284 }
285 
mcux_lpi2c_init(const struct device * dev)286 static int mcux_lpi2c_init(const struct device *dev)
287 {
288 	const struct mcux_lpi2c_config *config = dev->config;
289 	struct mcux_lpi2c_data *data = dev->data;
290 	LPI2C_Type *base;
291 	uint32_t clock_freq, bitrate_cfg;
292 	lpi2c_master_config_t master_config;
293 	int error;
294 
295 	DEVICE_MMIO_NAMED_MAP(dev, reg_base, K_MEM_CACHE_NONE | K_MEM_DIRECT_MAP);
296 
297 	base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
298 
299 	if (!device_is_ready(config->clock_dev)) {
300 		LOG_ERR("clock control device not ready");
301 		return -ENODEV;
302 	}
303 
304 	error = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
305 	if (error) {
306 		return error;
307 	}
308 
309 	if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
310 				   &clock_freq)) {
311 		return -EINVAL;
312 	}
313 
314 	LPI2C_MasterGetDefaultConfig(&master_config);
315 	master_config.busIdleTimeout_ns = config->bus_idle_timeout_ns;
316 	LPI2C_MasterInit(base, &master_config, clock_freq);
317 	LPI2C_MasterTransferCreateHandle(base, &data->handle,
318 					 mcux_lpi2c_master_transfer_callback,
319 					 (void *)dev);
320 
321 	bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
322 
323 	error = mcux_lpi2c_do_configure(dev, I2C_MODE_CONTROLLER | bitrate_cfg);
324 	if (error) {
325 		return error;
326 	}
327 
328 	config->irq_config_func(dev);
329 
330 	i2c_rtio_init(data->ctx, dev);
331 
332 	return 0;
333 }
334 
335 static DEVICE_API(i2c, mcux_lpi2c_driver_api) = {
336 	.configure = mcux_lpi2c_configure,
337 	.transfer = mcux_lpi2c_transfer,
338 	.iodev_submit = mcux_lpi2c_submit,
339 };
340 
341 #if CONFIG_I2C_MCUX_LPI2C_BUS_RECOVERY
342 #define I2C_MCUX_LPI2C_SCL_INIT(n) .scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),
343 #define I2C_MCUX_LPI2C_SDA_INIT(n) .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),
344 #else
345 #define I2C_MCUX_LPI2C_SCL_INIT(n)
346 #define I2C_MCUX_LPI2C_SDA_INIT(n)
347 #endif /* CONFIG_I2C_MCUX_LPI2C_BUS_RECOVERY */
348 
349 #define I2C_MCUX_LPI2C_INIT(n)						\
350 	PINCTRL_DT_INST_DEFINE(n);					\
351 									\
352 	static void mcux_lpi2c_config_func_##n(const struct device *dev); \
353 									\
354 	static const struct mcux_lpi2c_config mcux_lpi2c_config_##n = {	\
355 		DEVICE_MMIO_NAMED_ROM_INIT(reg_base, DT_DRV_INST(n)),	\
356 		.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)),	\
357 		.clock_subsys =						\
358 			(clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),\
359 		.irq_config_func = mcux_lpi2c_config_func_##n,		\
360 		.bitrate = DT_INST_PROP(n, clock_frequency),		\
361 		.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),		\
362 		I2C_MCUX_LPI2C_SCL_INIT(n)				\
363 		I2C_MCUX_LPI2C_SDA_INIT(n)				\
364 		.bus_idle_timeout_ns =					\
365 			UTIL_AND(DT_INST_NODE_HAS_PROP(n, bus_idle_timeout),\
366 				 DT_INST_PROP(n, bus_idle_timeout)),	\
367 	};								\
368 									\
369 	I2C_RTIO_DEFINE(_i2c##n##_lpi2c_rtio,				\
370 		DT_INST_PROP_OR(n, sq_size, CONFIG_I2C_RTIO_SQ_SIZE),	\
371 		DT_INST_PROP_OR(n, cq_size, CONFIG_I2C_RTIO_CQ_SIZE));	\
372 									\
373 	static struct mcux_lpi2c_data mcux_lpi2c_data_##n = {		\
374 		.ctx = &CONCAT(_i2c, n, _lpi2c_rtio),			\
375 	};								\
376 									\
377 	I2C_DEVICE_DT_INST_DEFINE(n, mcux_lpi2c_init, NULL,		\
378 				&mcux_lpi2c_data_##n,			\
379 				&mcux_lpi2c_config_##n, POST_KERNEL,	\
380 				CONFIG_I2C_INIT_PRIORITY,		\
381 				&mcux_lpi2c_driver_api);		\
382 									\
383 	static void mcux_lpi2c_config_func_##n(const struct device *dev)\
384 	{								\
385 		IRQ_CONNECT(DT_INST_IRQN(n),				\
386 				DT_INST_IRQ(n, priority),		\
387 				mcux_lpi2c_isr,				\
388 				DEVICE_DT_INST_GET(n), 0);		\
389 									\
390 		irq_enable(DT_INST_IRQN(n));				\
391 	}
392 
393 DT_INST_FOREACH_STATUS_OKAY(I2C_MCUX_LPI2C_INIT)
394