1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * I2C multiplexer using GPIO API 4 * 5 * Peter Korsgaard <peter.korsgaard@barco.com> 6 */ 7 8 #include <linux/bits.h> 9 #include <linux/delay.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/gpio/driver.h> 12 #include <linux/i2c.h> 13 #include <linux/i2c-mux.h> 14 #include <linux/module.h> 15 #include <linux/overflow.h> 16 #include <linux/platform_data/i2c-mux-gpio.h> 17 #include <linux/platform_device.h> 18 #include <linux/slab.h> 19 20 struct gpiomux { 21 struct i2c_mux_gpio_platform_data data; 22 int ngpios; 23 struct gpio_desc **gpios; 24 }; 25 26 static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned int val) 27 { 28 DECLARE_BITMAP(values, BITS_PER_TYPE(val)); 29 30 values[0] = val; 31 32 gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values); 33 } 34 35 static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan) 36 { 37 struct gpiomux *mux = i2c_mux_priv(muxc); 38 39 i2c_mux_gpio_set(mux, chan); 40 41 if (mux->data.settle_time) 42 fsleep(mux->data.settle_time); 43 44 return 0; 45 } 46 47 static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan) 48 { 49 struct gpiomux *mux = i2c_mux_priv(muxc); 50 51 i2c_mux_gpio_set(mux, mux->data.idle); 52 53 return 0; 54 } 55 56 static int i2c_mux_gpio_probe_fw(struct gpiomux *mux, 57 struct platform_device *pdev) 58 { 59 struct device *dev = &pdev->dev; 60 struct fwnode_handle *fwnode = dev_fwnode(dev); 61 struct device_node *np = dev->of_node; 62 struct device_node *adapter_np; 63 struct i2c_adapter *adapter = NULL; 64 struct fwnode_handle *child; 65 unsigned int *values; 66 int rc, i = 0; 67 68 if (is_of_node(fwnode)) { 69 if (!np) 70 return -ENODEV; 71 72 adapter_np = of_parse_phandle(np, "i2c-parent", 0); 73 if (!adapter_np) { 74 dev_err(&pdev->dev, "Cannot parse i2c-parent\n"); 75 return -ENODEV; 76 } 77 adapter = of_find_i2c_adapter_by_node(adapter_np); 78 of_node_put(adapter_np); 79 80 } else if (is_acpi_node(fwnode)) { 81 /* 82 * In ACPI land the mux should be a direct child of the i2c 83 * bus it muxes. 84 */ 85 acpi_handle dev_handle = ACPI_HANDLE(dev->parent); 86 87 adapter = i2c_acpi_find_adapter_by_handle(dev_handle); 88 } 89 90 if (!adapter) 91 return -EPROBE_DEFER; 92 93 mux->data.parent = i2c_adapter_id(adapter); 94 put_device(&adapter->dev); 95 96 mux->data.n_values = device_get_child_node_count(dev); 97 values = devm_kcalloc(dev, 98 mux->data.n_values, sizeof(*mux->data.values), 99 GFP_KERNEL); 100 if (!values) { 101 dev_err(dev, "Cannot allocate values array"); 102 return -ENOMEM; 103 } 104 105 device_for_each_child_node(dev, child) { 106 if (is_of_node(child)) { 107 fwnode_property_read_u32(child, "reg", values + i); 108 } else if (is_acpi_node(child)) { 109 rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i); 110 if (rc) { 111 fwnode_handle_put(child); 112 return dev_err_probe(dev, rc, "Cannot get address\n"); 113 } 114 } 115 116 i++; 117 } 118 mux->data.values = values; 119 120 if (device_property_read_u32(dev, "idle-state", &mux->data.idle)) 121 mux->data.idle = I2C_MUX_GPIO_NO_IDLE; 122 123 device_property_read_u32(dev, "settle-time-us", &mux->data.settle_time); 124 125 return 0; 126 } 127 128 static int i2c_mux_gpio_probe(struct platform_device *pdev) 129 { 130 struct i2c_mux_core *muxc; 131 struct gpiomux *mux; 132 struct i2c_adapter *parent; 133 struct i2c_adapter *root; 134 unsigned int initial_state; 135 int i, ngpios, ret; 136 137 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); 138 if (!mux) 139 return -ENOMEM; 140 141 if (!dev_get_platdata(&pdev->dev)) { 142 ret = i2c_mux_gpio_probe_fw(mux, pdev); 143 if (ret < 0) 144 return ret; 145 } else { 146 memcpy(&mux->data, dev_get_platdata(&pdev->dev), 147 sizeof(mux->data)); 148 } 149 150 ngpios = gpiod_count(&pdev->dev, "mux"); 151 if (ngpios <= 0) { 152 dev_err(&pdev->dev, "no valid gpios provided\n"); 153 return ngpios ?: -EINVAL; 154 } 155 mux->ngpios = ngpios; 156 157 parent = i2c_get_adapter(mux->data.parent); 158 if (!parent) 159 return -EPROBE_DEFER; 160 161 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 162 array_size(ngpios, sizeof(*mux->gpios)), 0, 163 i2c_mux_gpio_select, NULL); 164 if (!muxc) { 165 ret = -ENOMEM; 166 goto alloc_failed; 167 } 168 mux->gpios = muxc->priv; 169 muxc->priv = mux; 170 171 platform_set_drvdata(pdev, muxc); 172 173 root = i2c_root_adapter(&parent->dev); 174 175 muxc->mux_locked = true; 176 177 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { 178 initial_state = mux->data.idle; 179 muxc->deselect = i2c_mux_gpio_deselect; 180 } else { 181 initial_state = mux->data.values[0]; 182 } 183 184 for (i = 0; i < ngpios; i++) { 185 struct gpio_device *gdev; 186 struct device *dev; 187 struct gpio_desc *gpiod; 188 enum gpiod_flags flag; 189 190 if (initial_state & BIT(i)) 191 flag = GPIOD_OUT_HIGH; 192 else 193 flag = GPIOD_OUT_LOW; 194 gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag); 195 if (IS_ERR(gpiod)) { 196 ret = PTR_ERR(gpiod); 197 goto alloc_failed; 198 } 199 200 mux->gpios[i] = gpiod; 201 202 if (!muxc->mux_locked) 203 continue; 204 205 gdev = gpiod_to_gpio_device(gpiod); 206 dev = gpio_device_to_device(gdev); 207 muxc->mux_locked = i2c_root_adapter(dev) == root; 208 } 209 210 if (muxc->mux_locked) 211 dev_info(&pdev->dev, "mux-locked i2c mux\n"); 212 213 for (i = 0; i < mux->data.n_values; i++) { 214 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0; 215 216 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i]); 217 if (ret) 218 goto add_adapter_failed; 219 } 220 221 dev_info(&pdev->dev, "%d port mux on %s adapter\n", 222 mux->data.n_values, parent->name); 223 224 return 0; 225 226 add_adapter_failed: 227 i2c_mux_del_adapters(muxc); 228 alloc_failed: 229 i2c_put_adapter(parent); 230 231 return ret; 232 } 233 234 static void i2c_mux_gpio_remove(struct platform_device *pdev) 235 { 236 struct i2c_mux_core *muxc = platform_get_drvdata(pdev); 237 238 i2c_mux_del_adapters(muxc); 239 i2c_put_adapter(muxc->parent); 240 } 241 242 static const struct of_device_id i2c_mux_gpio_of_match[] = { 243 { .compatible = "i2c-mux-gpio", }, 244 {}, 245 }; 246 MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match); 247 248 static struct platform_driver i2c_mux_gpio_driver = { 249 .probe = i2c_mux_gpio_probe, 250 .remove_new = i2c_mux_gpio_remove, 251 .driver = { 252 .name = "i2c-mux-gpio", 253 .of_match_table = i2c_mux_gpio_of_match, 254 }, 255 }; 256 257 module_platform_driver(i2c_mux_gpio_driver); 258 259 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver"); 260 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>"); 261 MODULE_LICENSE("GPL"); 262 MODULE_ALIAS("platform:i2c-mux-gpio"); 263