1 /*
2  * Copyright (c) 2023 Ayush Singh <ayushdevel1325@gmail.com>
3  * Copyright (c) 2021 Jason Kridner, BeagleBoard.org Foundation
4  * Copyright (c) 2020 Innoseis BV
5  *
6  * Based on i2c_tca9656a.c
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #define DT_DRV_COMPAT gpio_i2c_switch
12 
13 #define GPIO_I2C_TOGGLE_DELAY_US 1
14 #define GPIO_I2C_LOCK_TIMEOUT_US   (GPIO_I2C_TOGGLE_DELAY_US * 2 + 100)
15 
16 #include <zephyr/kernel.h>
17 #include <zephyr/device.h>
18 #include <zephyr/devicetree.h>
19 #include <zephyr/drivers/i2c.h>
20 #include <zephyr/drivers/gpio.h>
21 #include <zephyr/logging/log.h>
22 
23 LOG_MODULE_REGISTER(gpio_i2c_switch, CONFIG_I2C_LOG_LEVEL);
24 
25 struct gpio_i2c_switch_config {
26 	const struct device *bus;
27 	const struct gpio_dt_spec gpio;
28 };
29 
30 struct gpio_i2c_switch_data {
31 	struct k_mutex lock;
32 };
33 
gpio_i2c_switch_configure(const struct device * dev,uint32_t dev_config)34 static int gpio_i2c_switch_configure(const struct device *dev, uint32_t dev_config)
35 {
36 	const struct gpio_i2c_switch_config *config = dev->config;
37 
38 	return i2c_configure(config->bus, dev_config);
39 }
40 
gpio_i2c_switch_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)41 static int gpio_i2c_switch_transfer(const struct device *dev, struct i2c_msg *msgs,
42 				    uint8_t num_msgs, uint16_t addr)
43 {
44 	int res;
45 	struct gpio_i2c_switch_data *data = dev->data;
46 	const struct gpio_i2c_switch_config *config = dev->config;
47 
48 	res = k_mutex_lock(&data->lock, K_USEC(GPIO_I2C_LOCK_TIMEOUT_US));
49 	if (res != 0) {
50 		return res;
51 	}
52 
53 	/* enable switch */
54 	gpio_pin_set_dt(&config->gpio, 1);
55 	k_busy_wait(GPIO_I2C_TOGGLE_DELAY_US);
56 
57 	res = i2c_transfer(config->bus, msgs, num_msgs, addr);
58 
59 	/* disable switch */
60 	gpio_pin_set_dt(&config->gpio, 0);
61 	k_busy_wait(GPIO_I2C_TOGGLE_DELAY_US);
62 	k_mutex_unlock(&data->lock);
63 
64 	return res;
65 }
66 
67 static DEVICE_API(i2c, gpio_i2c_switch_api_funcs) = {
68 	.configure = gpio_i2c_switch_configure,
69 	.transfer = gpio_i2c_switch_transfer,
70 #ifdef CONFIG_I2C_RTIO
71 	.iodev_submit = i2c_iodev_submit_fallback,
72 #endif
73 };
74 
gpio_i2c_switch_init(const struct device * dev)75 static int gpio_i2c_switch_init(const struct device *dev)
76 {
77 	const struct gpio_i2c_switch_config *config = dev->config;
78 	struct gpio_i2c_switch_data *data = dev->data;
79 
80 	k_mutex_init(&data->lock);
81 
82 	return gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_INACTIVE);
83 }
84 
85 #define DEFINE_GPIO_I2C_SWITCH(inst)                                                               \
86                                                                                                    \
87 	static struct gpio_i2c_switch_data gpio_i2c_switch_dev_data_##inst;                        \
88                                                                                                    \
89 	static const struct gpio_i2c_switch_config gpio_i2c_switch_dev_cfg_##inst = {              \
90 		.bus = DEVICE_DT_GET(DT_PHANDLE(DT_DRV_INST(inst), controller)),                   \
91 		.gpio = GPIO_DT_SPEC_GET(DT_DRV_INST(inst), gpios),                                \
92 	};                                                                                         \
93                                                                                                    \
94 	I2C_DEVICE_DT_INST_DEFINE(inst, gpio_i2c_switch_init, device_pm_control_nop,               \
95 			      &gpio_i2c_switch_dev_data_##inst, &gpio_i2c_switch_dev_cfg_##inst,   \
96 			      POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, &gpio_i2c_switch_api_funcs);
97 
98 DT_INST_FOREACH_STATUS_OKAY(DEFINE_GPIO_I2C_SWITCH)
99