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 const struct i2c_driver_api gpio_i2c_switch_api_funcs = {
68 	.configure = gpio_i2c_switch_configure,
69 	.transfer = gpio_i2c_switch_transfer,
70 };
71 
gpio_i2c_switch_init(const struct device * dev)72 static int gpio_i2c_switch_init(const struct device *dev)
73 {
74 	const struct gpio_i2c_switch_config *config = dev->config;
75 	struct gpio_i2c_switch_data *data = dev->data;
76 
77 	k_mutex_init(&data->lock);
78 
79 	return gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_INACTIVE);
80 }
81 
82 #define DEFINE_GPIO_I2C_SWITCH(inst)                                                               \
83                                                                                                    \
84 	static struct gpio_i2c_switch_data gpio_i2c_switch_dev_data_##inst;                        \
85                                                                                                    \
86 	static const struct gpio_i2c_switch_config gpio_i2c_switch_dev_cfg_##inst = {              \
87 		.bus = DEVICE_DT_GET(DT_PHANDLE(DT_DRV_INST(inst), controller)),                   \
88 		.gpio = GPIO_DT_SPEC_GET(DT_DRV_INST(inst), gpios),                                \
89 	};                                                                                         \
90                                                                                                    \
91 	DEVICE_DT_INST_DEFINE(inst, gpio_i2c_switch_init, device_pm_control_nop,                   \
92 			      &gpio_i2c_switch_dev_data_##inst, &gpio_i2c_switch_dev_cfg_##inst,   \
93 			      POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, &gpio_i2c_switch_api_funcs);
94 
95 DT_INST_FOREACH_STATUS_OKAY(DEFINE_GPIO_I2C_SWITCH)
96