Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 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> |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 13 | #include <linux/i2c-mux-gpio.h> |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/gpio.h> |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 18 | #include <linux/of_gpio.h> |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 19 | |
| 20 | struct gpiomux { |
| 21 | struct i2c_adapter *parent; |
| 22 | struct i2c_adapter **adap; /* child busses */ |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 23 | struct i2c_mux_gpio_platform_data data; |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 24 | unsigned gpio_base; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 25 | }; |
| 26 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 27 | static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val) |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 28 | { |
| 29 | int i; |
| 30 | |
| 31 | for (i = 0; i < mux->data.n_gpios; i++) |
Ionut Nicu | 250ad59 | 2013-10-11 14:17:10 +0200 | [diff] [blame] | 32 | gpio_set_value_cansleep(mux->gpio_base + mux->data.gpios[i], |
| 33 | val & (1 << i)); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 36 | static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan) |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 37 | { |
| 38 | struct gpiomux *mux = data; |
| 39 | |
Ionut Nicu | 8c0ec25 | 2013-10-11 12:09:57 +0200 | [diff] [blame] | 40 | i2c_mux_gpio_set(mux, chan); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 45 | static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan) |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 46 | { |
| 47 | struct gpiomux *mux = data; |
| 48 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 49 | i2c_mux_gpio_set(mux, mux->data.idle); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 54 | static int match_gpio_chip_by_label(struct gpio_chip *chip, |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 55 | void *data) |
| 56 | { |
| 57 | return !strcmp(chip->label, data); |
| 58 | } |
| 59 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 60 | #ifdef CONFIG_OF |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 61 | static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 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; |
Ionut Nicu | 492ed77 | 2013-10-08 15:51:53 +0200 | [diff] [blame] | 68 | int i = 0, ret; |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 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); |
Ioan Nicu | 623e4ec | 2015-03-30 15:03:38 +0200 | [diff] [blame] | 79 | if (!adapter) |
Ionut Nicu | 9937b8d | 2013-10-09 11:50:45 +0200 | [diff] [blame] | 80 | return -EPROBE_DEFER; |
Ioan Nicu | 623e4ec | 2015-03-30 15:03:38 +0200 | [diff] [blame] | 81 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 82 | mux->data.parent = i2c_adapter_id(adapter); |
| 83 | put_device(&adapter->dev); |
| 84 | |
| 85 | mux->data.n_values = of_get_child_count(np); |
| 86 | |
| 87 | values = devm_kzalloc(&pdev->dev, |
| 88 | sizeof(*mux->data.values) * mux->data.n_values, |
| 89 | GFP_KERNEL); |
| 90 | if (!values) { |
| 91 | dev_err(&pdev->dev, "Cannot allocate values array"); |
| 92 | return -ENOMEM; |
| 93 | } |
| 94 | |
| 95 | for_each_child_of_node(np, child) { |
| 96 | of_property_read_u32(child, "reg", values + i); |
| 97 | i++; |
| 98 | } |
| 99 | mux->data.values = values; |
| 100 | |
| 101 | if (of_property_read_u32(np, "idle-state", &mux->data.idle)) |
| 102 | mux->data.idle = I2C_MUX_GPIO_NO_IDLE; |
| 103 | |
| 104 | mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios"); |
| 105 | if (mux->data.n_gpios < 0) { |
| 106 | dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n"); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | |
| 110 | gpios = devm_kzalloc(&pdev->dev, |
| 111 | sizeof(*mux->data.gpios) * mux->data.n_gpios, GFP_KERNEL); |
| 112 | if (!gpios) { |
| 113 | dev_err(&pdev->dev, "Cannot allocate gpios array"); |
| 114 | return -ENOMEM; |
| 115 | } |
| 116 | |
Ionut Nicu | 492ed77 | 2013-10-08 15:51:53 +0200 | [diff] [blame] | 117 | for (i = 0; i < mux->data.n_gpios; i++) { |
| 118 | ret = of_get_named_gpio(np, "mux-gpios", i); |
| 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | gpios[i] = ret; |
| 122 | } |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 123 | |
| 124 | mux->data.gpios = gpios; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | #else |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 129 | static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 130 | struct platform_device *pdev) |
| 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | #endif |
| 135 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 136 | static int i2c_mux_gpio_probe(struct platform_device *pdev) |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 137 | { |
| 138 | struct gpiomux *mux; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 139 | struct i2c_adapter *parent; |
| 140 | int (*deselect) (struct i2c_adapter *, void *, u32); |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 141 | unsigned initial_state, gpio_base; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 142 | int i, ret; |
| 143 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 144 | mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); |
| 145 | if (!mux) { |
| 146 | dev_err(&pdev->dev, "Cannot allocate gpiomux structure"); |
| 147 | return -ENOMEM; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 150 | platform_set_drvdata(pdev, mux); |
| 151 | |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 152 | if (!dev_get_platdata(&pdev->dev)) { |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 153 | ret = i2c_mux_gpio_probe_dt(mux, pdev); |
| 154 | if (ret < 0) |
| 155 | return ret; |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 156 | } else { |
| 157 | memcpy(&mux->data, dev_get_platdata(&pdev->dev), |
| 158 | sizeof(mux->data)); |
| 159 | } |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 160 | |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 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 | */ |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 165 | if (mux->data.gpio_chip) { |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 166 | struct gpio_chip *gpio; |
| 167 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 168 | gpio = gpiochip_find(mux->data.gpio_chip, |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 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 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 178 | parent = i2c_get_adapter(mux->data.parent); |
Ioan Nicu | 623e4ec | 2015-03-30 15:03:38 +0200 | [diff] [blame] | 179 | if (!parent) |
Ionut Nicu | 9937b8d | 2013-10-09 11:50:45 +0200 | [diff] [blame] | 180 | return -EPROBE_DEFER; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 181 | |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 182 | mux->parent = parent; |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 183 | mux->gpio_base = gpio_base; |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 184 | |
Maxime Ripard | 2614a85 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 185 | mux->adap = devm_kzalloc(&pdev->dev, |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 186 | sizeof(*mux->adap) * mux->data.n_values, |
Maxime Ripard | 2614a85 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 187 | GFP_KERNEL); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 188 | if (!mux->adap) { |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 189 | dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure"); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 190 | ret = -ENOMEM; |
Maxime Ripard | 2614a85 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 191 | goto alloc_failed; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 194 | if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { |
| 195 | initial_state = mux->data.idle; |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 196 | deselect = i2c_mux_gpio_deselect; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 197 | } else { |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 198 | initial_state = mux->data.values[0]; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 199 | deselect = NULL; |
| 200 | } |
| 201 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 202 | for (i = 0; i < mux->data.n_gpios; i++) { |
| 203 | ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio"); |
Jean Delvare | 7fafae6 | 2013-03-06 22:35:53 +0000 | [diff] [blame] | 204 | if (ret) { |
| 205 | dev_err(&pdev->dev, "Failed to request GPIO %d\n", |
| 206 | mux->data.gpios[i]); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 207 | goto err_request_gpio; |
Jean Delvare | 7fafae6 | 2013-03-06 22:35:53 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | ret = gpio_direction_output(gpio_base + mux->data.gpios[i], |
| 211 | initial_state & (1 << i)); |
| 212 | if (ret) { |
| 213 | dev_err(&pdev->dev, |
| 214 | "Failed to set direction of GPIO %d to output\n", |
| 215 | mux->data.gpios[i]); |
| 216 | i++; /* gpio_request above succeeded, so must free */ |
| 217 | goto err_request_gpio; |
| 218 | } |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 221 | for (i = 0; i < mux->data.n_values; i++) { |
| 222 | u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0; |
| 223 | unsigned int class = mux->data.classes ? mux->data.classes[i] : 0; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 224 | |
Jean Delvare | eee543e | 2012-10-05 22:23:51 +0200 | [diff] [blame] | 225 | mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr, |
Ionut Nicu | 8c0ec25 | 2013-10-11 12:09:57 +0200 | [diff] [blame] | 226 | mux->data.values[i], class, |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 227 | i2c_mux_gpio_select, deselect); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 228 | if (!mux->adap[i]) { |
| 229 | ret = -ENODEV; |
| 230 | dev_err(&pdev->dev, "Failed to add adapter %d\n", i); |
| 231 | goto add_adapter_failed; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | dev_info(&pdev->dev, "%d port mux on %s adapter\n", |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 236 | mux->data.n_values, parent->name); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | |
| 240 | add_adapter_failed: |
| 241 | for (; i > 0; i--) |
| 242 | i2c_del_mux_adapter(mux->adap[i - 1]); |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 243 | i = mux->data.n_gpios; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 244 | err_request_gpio: |
| 245 | for (; i > 0; i--) |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 246 | gpio_free(gpio_base + mux->data.gpios[i - 1]); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 247 | alloc_failed: |
| 248 | i2c_put_adapter(parent); |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 253 | static int i2c_mux_gpio_remove(struct platform_device *pdev) |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 254 | { |
| 255 | struct gpiomux *mux = platform_get_drvdata(pdev); |
| 256 | int i; |
| 257 | |
| 258 | for (i = 0; i < mux->data.n_values; i++) |
| 259 | i2c_del_mux_adapter(mux->adap[i]); |
| 260 | |
| 261 | for (i = 0; i < mux->data.n_gpios; i++) |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame] | 262 | gpio_free(mux->gpio_base + mux->data.gpios[i]); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 263 | |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 264 | i2c_put_adapter(mux->parent); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 269 | static const struct of_device_id i2c_mux_gpio_of_match[] = { |
Maxime Ripard | 9b7a0c4 | 2012-10-25 18:23:53 +0200 | [diff] [blame] | 270 | { .compatible = "i2c-mux-gpio", }, |
| 271 | {}, |
| 272 | }; |
| 273 | MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match); |
| 274 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 275 | static struct platform_driver i2c_mux_gpio_driver = { |
| 276 | .probe = i2c_mux_gpio_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 277 | .remove = i2c_mux_gpio_remove, |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 278 | .driver = { |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 279 | .name = "i2c-mux-gpio", |
Sachin Kamat | 4e90532 | 2013-09-30 09:04:25 +0530 | [diff] [blame] | 280 | .of_match_table = i2c_mux_gpio_of_match, |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 281 | }, |
| 282 | }; |
| 283 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 284 | module_platform_driver(i2c_mux_gpio_driver); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 285 | |
| 286 | MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver"); |
| 287 | MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>"); |
| 288 | MODULE_LICENSE("GPL"); |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 289 | MODULE_ALIAS("platform:i2c-mux-gpio"); |