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