^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Bitbanging I2C bus driver using the GPIO API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007 Atmel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c-algo-bit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/platform_data/i2c-gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct i2c_gpio_private_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct gpio_desc *sda;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct gpio_desc *scl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct i2c_adapter adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct i2c_algo_bit_data bit_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct i2c_gpio_platform_data pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct dentry *debug_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* these must be protected by bus lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct completion scl_irq_completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u64 scl_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Toggle SDA by changing the output value of the pin. This is only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * valid for pins configured as open drain (i.e. setting the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * high effectively turns off the output driver.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static void i2c_gpio_setsda_val(void *data, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) gpiod_set_value_cansleep(priv->sda, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Toggle SCL by changing the output value of the pin. This is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * for pins that are configured as open drain and for output-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * pins. The latter case will break the i2c protocol, but it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * often work in practice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void i2c_gpio_setscl_val(void *data, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) gpiod_set_value_cansleep(priv->scl, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int i2c_gpio_getsda(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return gpiod_get_value_cansleep(priv->sda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int i2c_gpio_getscl(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return gpiod_get_value_cansleep(priv->scl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static struct dentry *i2c_gpio_debug_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define setsda(bd, val) ((bd)->setsda((bd)->data, val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define setscl(bd, val) ((bd)->setscl((bd)->data, val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define getsda(bd) ((bd)->getsda((bd)->data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define getscl(bd) ((bd)->getscl((bd)->data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define WIRE_ATTRIBUTE(wire) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int fops_##wire##_get(void *data, u64 *val) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct i2c_gpio_private_data *priv = data; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *val = get##wire(&priv->bit_data); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int fops_##wire##_set(void *data, u64 val) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct i2c_gpio_private_data *priv = data; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) set##wire(&priv->bit_data, val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) WIRE_ATTRIBUTE(scl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) WIRE_ATTRIBUTE(sda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u32 pattern, u8 pattern_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct i2c_algo_bit_data *bit_data = &priv->bit_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* START condition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) setsda(bit_data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) udelay(bit_data->udelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* Send pattern, request ACK, don't send STOP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) for (i = pattern_size - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) setscl(bit_data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) udelay(bit_data->udelay / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) setsda(bit_data, (pattern >> i) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) udelay((bit_data->udelay + 1) / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) setscl(bit_data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) udelay(bit_data->udelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int fops_incomplete_addr_phase_set(void *data, u64 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) u32 pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (addr > 0x7f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pattern = (addr << 2) | 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) i2c_gpio_incomplete_transfer(priv, pattern, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int fops_incomplete_write_byte_set(void *data, u64 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) u32 pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (addr > 0x7f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) pattern = (addr << 2) | 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pattern = (pattern << 9) | 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) i2c_gpio_incomplete_transfer(priv, pattern, 18);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) irqreturn_t handler(int, void*))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int ret, irq = gpiod_to_irq(priv->scl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ret = gpiod_direction_input(priv->scl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) reinit_completion(&priv->scl_irq_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) "i2c_gpio_fault_injector_scl_irq", priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) goto output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) wait_for_completion_interruptible(&priv->scl_irq_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) free_irq(irq, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) output:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ret = gpiod_direction_output(priv->scl, 1) ?: ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static irqreturn_t lose_arbitration_irq(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct i2c_gpio_private_data *priv = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) setsda(&priv->bit_data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) udelay(priv->scl_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) setsda(&priv->bit_data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) complete(&priv->scl_irq_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int fops_lose_arbitration_set(void *data, u64 duration)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (duration > 100 * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) priv->scl_irq_data = duration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * Interrupt on falling SCL. This ensures that the master under test has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * really started the transfer. Interrupt on falling SDA did only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * exercise 'bus busy' detection on some HW but not 'arbitration lost'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * Note that the interrupt latency may cause the first bits to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * transmitted correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return i2c_gpio_fi_act_on_scl_irq(priv, lose_arbitration_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static irqreturn_t inject_panic_irq(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct i2c_gpio_private_data *priv = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) udelay(priv->scl_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) panic("I2C fault injector induced panic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int fops_inject_panic_set(void *data, u64 duration)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct i2c_gpio_private_data *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (duration > 100 * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) priv->scl_irq_data = duration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Interrupt on falling SCL. This ensures that the master under test has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * really started the transfer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return i2c_gpio_fi_act_on_scl_irq(priv, inject_panic_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * If there will be a debugfs-dir per i2c adapter somewhen, put the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * 'fault-injector' dir there. Until then, we have a global dir with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * all adapters as subdirs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (!i2c_gpio_debug_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!i2c_gpio_debug_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (!priv->debug_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) init_completion(&priv->scl_irq_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) priv, &fops_incomplete_addr_phase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) priv, &fops_incomplete_write_byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (priv->bit_data.getscl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) priv, &fops_inject_panic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) priv, &fops_lose_arbitration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) debugfs_remove_recursive(priv->debug_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static void of_i2c_gpio_get_props(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct i2c_gpio_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) pdata->timeout = msecs_to_jiffies(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) pdata->sda_is_open_drain =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) of_property_read_bool(np, "i2c-gpio,sda-open-drain");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) pdata->scl_is_open_drain =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) of_property_read_bool(np, "i2c-gpio,scl-open-drain");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) pdata->scl_is_output_only =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) of_property_read_bool(np, "i2c-gpio,scl-output-only");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unsigned int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) enum gpiod_flags gflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct gpio_desc *retdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) retdesc = devm_gpiod_get(dev, con_id, gflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (!IS_ERR(retdesc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) dev_dbg(dev, "got GPIO from name %s\n", con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return retdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (!IS_ERR(retdesc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) dev_dbg(dev, "got GPIO from index %u\n", index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return retdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) ret = PTR_ERR(retdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* FIXME: hack in the old code, is this really necessary? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (ret == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) retdesc = ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* This happens if the GPIO driver is not yet probed, let's defer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) retdesc = ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (PTR_ERR(retdesc) != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) dev_err(dev, "error trying to get descriptor: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return retdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static int i2c_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct i2c_gpio_private_data *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct i2c_gpio_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) struct i2c_algo_bit_data *bit_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct i2c_adapter *adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) enum gpiod_flags gflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) adap = &priv->adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) bit_data = &priv->bit_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) pdata = &priv->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) of_i2c_gpio_get_props(np, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * If all platform data settings are zero it is OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * to not provide any platform data from the board.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (dev_get_platdata(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * First get the GPIO pins; if it fails, we'll defer the probe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * If the SCL/SDA lines are marked "open drain" by platform data or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * device tree then this means that something outside of our control is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * marking these lines to be handled as open drain, and we should just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * handle them as we handle any other output. Else we enforce open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * drain as this is required for an I2C bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (pdata->sda_is_open_drain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) gflags = GPIOD_OUT_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (IS_ERR(priv->sda))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return PTR_ERR(priv->sda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (pdata->scl_is_open_drain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) gflags = GPIOD_OUT_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (IS_ERR(priv->scl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return PTR_ERR(priv->scl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) bit_data->can_do_atomic = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) bit_data->setsda = i2c_gpio_setsda_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) bit_data->setscl = i2c_gpio_setscl_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!pdata->scl_is_output_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) bit_data->getscl = i2c_gpio_getscl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) bit_data->getsda = i2c_gpio_getsda;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (pdata->udelay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) bit_data->udelay = pdata->udelay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) else if (pdata->scl_is_output_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) bit_data->udelay = 50; /* 10 kHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) bit_data->udelay = 5; /* 100 kHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (pdata->timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) bit_data->timeout = pdata->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) bit_data->timeout = HZ / 10; /* 100 ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) bit_data->data = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) adap->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) adap->algo_data = bit_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) adap->dev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) adap->dev.of_node = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) adap->nr = pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ret = i2c_bit_add_numbered_bus(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) platform_set_drvdata(pdev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * FIXME: using global GPIO numbers is not helpful. If/when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * get accessors to get the actual name of the GPIO line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * from the descriptor, then provide that instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) pdata->scl_is_output_only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ? ", no clock stretching" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) i2c_gpio_fault_injector_init(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static int i2c_gpio_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct i2c_gpio_private_data *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct i2c_adapter *adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) i2c_gpio_fault_injector_exit(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) priv = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) adap = &priv->adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) i2c_del_adapter(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) #if defined(CONFIG_OF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static const struct of_device_id i2c_gpio_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) { .compatible = "i2c-gpio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static struct platform_driver i2c_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) .name = "i2c-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) .of_match_table = of_match_ptr(i2c_gpio_dt_ids),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) .probe = i2c_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) .remove = i2c_gpio_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static int __init i2c_gpio_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) ret = platform_driver_register(&i2c_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) subsys_initcall(i2c_gpio_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static void __exit i2c_gpio_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) platform_driver_unregister(&i2c_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) module_exit(i2c_gpio_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) MODULE_ALIAS("platform:i2c-gpio");