1 /* 2 * I2C multiplexer using GPIO API 3 * 4 * Peter Korsgaard <peter.korsgaard@barco.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 11 #include <linux/i2c.h> 12 #include <linux/i2c-mux.h> 13 #include <linux/platform_data/i2c-mux-gpio.h> 14 #include <linux/platform_device.h> 15 #include <linux/module.h> 16 #include <linux/slab.h> 17 #include <linux/gpio.h> 18 #include "../../gpio/gpiolib.h" 19 #include <linux/of_gpio.h> 20 21 struct gpiomux { 22 struct i2c_mux_gpio_platform_data data; 23 unsigned gpio_base; 24 struct gpio_desc **gpios; 25 }; 26 27 static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val) 28 { 29 DECLARE_BITMAP(values, BITS_PER_TYPE(val)); 30 31 values[0] = val; 32 33 gpiod_set_array_value_cansleep(mux->data.n_gpios, mux->gpios, NULL, 34 values); 35 } 36 37 static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan) 38 { 39 struct gpiomux *mux = i2c_mux_priv(muxc); 40 41 i2c_mux_gpio_set(mux, chan); 42 43 return 0; 44 } 45 46 static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan) 47 { 48 struct gpiomux *mux = i2c_mux_priv(muxc); 49 50 i2c_mux_gpio_set(mux, mux->data.idle); 51 52 return 0; 53 } 54 55 static int match_gpio_chip_by_label(struct gpio_chip *chip, 56 void *data) 57 { 58 return !strcmp(chip->label, data); 59 } 60 61 #ifdef CONFIG_OF 62 static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, 63 struct platform_device *pdev) 64 { 65 struct device_node *np = pdev->dev.of_node; 66 struct device_node *adapter_np, *child; 67 struct i2c_adapter *adapter; 68 unsigned *values, *gpios; 69 int i = 0, ret; 70 71 if (!np) 72 return -ENODEV; 73 74 adapter_np = of_parse_phandle(np, "i2c-parent", 0); 75 if (!adapter_np) { 76 dev_err(&pdev->dev, "Cannot parse i2c-parent\n"); 77 return -ENODEV; 78 } 79 adapter = of_find_i2c_adapter_by_node(adapter_np); 80 of_node_put(adapter_np); 81 if (!adapter) 82 return -EPROBE_DEFER; 83 84 mux->data.parent = i2c_adapter_id(adapter); 85 put_device(&adapter->dev); 86 87 mux->data.n_values = of_get_child_count(np); 88 89 values = devm_kcalloc(&pdev->dev, 90 mux->data.n_values, sizeof(*mux->data.values), 91 GFP_KERNEL); 92 if (!values) { 93 dev_err(&pdev->dev, "Cannot allocate values array"); 94 return -ENOMEM; 95 } 96 97 for_each_child_of_node(np, child) { 98 of_property_read_u32(child, "reg", values + i); 99 i++; 100 } 101 mux->data.values = values; 102 103 if (of_property_read_u32(np, "idle-state", &mux->data.idle)) 104 mux->data.idle = I2C_MUX_GPIO_NO_IDLE; 105 106 mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios"); 107 if (mux->data.n_gpios < 0) { 108 dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n"); 109 return -EINVAL; 110 } 111 112 gpios = devm_kcalloc(&pdev->dev, 113 mux->data.n_gpios, sizeof(*mux->data.gpios), 114 GFP_KERNEL); 115 if (!gpios) { 116 dev_err(&pdev->dev, "Cannot allocate gpios array"); 117 return -ENOMEM; 118 } 119 120 for (i = 0; i < mux->data.n_gpios; i++) { 121 ret = of_get_named_gpio(np, "mux-gpios", i); 122 if (ret < 0) 123 return ret; 124 gpios[i] = ret; 125 } 126 127 mux->data.gpios = gpios; 128 129 return 0; 130 } 131 #else 132 static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, 133 struct platform_device *pdev) 134 { 135 return 0; 136 } 137 #endif 138 139 static int i2c_mux_gpio_probe(struct platform_device *pdev) 140 { 141 struct i2c_mux_core *muxc; 142 struct gpiomux *mux; 143 struct i2c_adapter *parent; 144 struct i2c_adapter *root; 145 unsigned initial_state, gpio_base; 146 int i, ret; 147 148 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); 149 if (!mux) 150 return -ENOMEM; 151 152 if (!dev_get_platdata(&pdev->dev)) { 153 ret = i2c_mux_gpio_probe_dt(mux, pdev); 154 if (ret < 0) 155 return ret; 156 } else { 157 memcpy(&mux->data, dev_get_platdata(&pdev->dev), 158 sizeof(mux->data)); 159 } 160 161 /* 162 * If a GPIO chip name is provided, the GPIO pin numbers provided are 163 * relative to its base GPIO number. Otherwise they are absolute. 164 */ 165 if (mux->data.gpio_chip) { 166 struct gpio_chip *gpio; 167 168 gpio = gpiochip_find(mux->data.gpio_chip, 169 match_gpio_chip_by_label); 170 if (!gpio) 171 return -EPROBE_DEFER; 172 173 gpio_base = gpio->base; 174 } else { 175 gpio_base = 0; 176 } 177 178 parent = i2c_get_adapter(mux->data.parent); 179 if (!parent) 180 return -EPROBE_DEFER; 181 182 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 183 mux->data.n_gpios * sizeof(*mux->gpios), 0, 184 i2c_mux_gpio_select, NULL); 185 if (!muxc) { 186 ret = -ENOMEM; 187 goto alloc_failed; 188 } 189 mux->gpios = muxc->priv; 190 muxc->priv = mux; 191 192 platform_set_drvdata(pdev, muxc); 193 194 root = i2c_root_adapter(&parent->dev); 195 196 muxc->mux_locked = true; 197 mux->gpio_base = gpio_base; 198 199 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { 200 initial_state = mux->data.idle; 201 muxc->deselect = i2c_mux_gpio_deselect; 202 } else { 203 initial_state = mux->data.values[0]; 204 } 205 206 for (i = 0; i < mux->data.n_gpios; i++) { 207 struct device *gpio_dev; 208 struct gpio_desc *gpio_desc; 209 210 ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio"); 211 if (ret) { 212 dev_err(&pdev->dev, "Failed to request GPIO %d\n", 213 mux->data.gpios[i]); 214 goto err_request_gpio; 215 } 216 217 ret = gpio_direction_output(gpio_base + mux->data.gpios[i], 218 initial_state & (1 << i)); 219 if (ret) { 220 dev_err(&pdev->dev, 221 "Failed to set direction of GPIO %d to output\n", 222 mux->data.gpios[i]); 223 i++; /* gpio_request above succeeded, so must free */ 224 goto err_request_gpio; 225 } 226 227 gpio_desc = gpio_to_desc(gpio_base + mux->data.gpios[i]); 228 mux->gpios[i] = gpio_desc; 229 230 if (!muxc->mux_locked) 231 continue; 232 233 gpio_dev = &gpio_desc->gdev->dev; 234 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root; 235 } 236 237 if (muxc->mux_locked) 238 dev_info(&pdev->dev, "mux-locked i2c mux\n"); 239 240 for (i = 0; i < mux->data.n_values; i++) { 241 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0; 242 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0; 243 244 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class); 245 if (ret) 246 goto add_adapter_failed; 247 } 248 249 dev_info(&pdev->dev, "%d port mux on %s adapter\n", 250 mux->data.n_values, parent->name); 251 252 return 0; 253 254 add_adapter_failed: 255 i2c_mux_del_adapters(muxc); 256 i = mux->data.n_gpios; 257 err_request_gpio: 258 for (; i > 0; i--) 259 gpio_free(gpio_base + mux->data.gpios[i - 1]); 260 alloc_failed: 261 i2c_put_adapter(parent); 262 263 return ret; 264 } 265 266 static int i2c_mux_gpio_remove(struct platform_device *pdev) 267 { 268 struct i2c_mux_core *muxc = platform_get_drvdata(pdev); 269 struct gpiomux *mux = i2c_mux_priv(muxc); 270 int i; 271 272 i2c_mux_del_adapters(muxc); 273 274 for (i = 0; i < mux->data.n_gpios; i++) 275 gpio_free(mux->gpio_base + mux->data.gpios[i]); 276 277 i2c_put_adapter(muxc->parent); 278 279 return 0; 280 } 281 282 static const struct of_device_id i2c_mux_gpio_of_match[] = { 283 { .compatible = "i2c-mux-gpio", }, 284 {}, 285 }; 286 MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match); 287 288 static struct platform_driver i2c_mux_gpio_driver = { 289 .probe = i2c_mux_gpio_probe, 290 .remove = i2c_mux_gpio_remove, 291 .driver = { 292 .name = "i2c-mux-gpio", 293 .of_match_table = i2c_mux_gpio_of_match, 294 }, 295 }; 296 297 module_platform_driver(i2c_mux_gpio_driver); 298 299 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver"); 300 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>"); 301 MODULE_LICENSE("GPL"); 302 MODULE_ALIAS("platform:i2c-mux-gpio"); 303