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> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/gpio.h> |
| 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++) |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 32 | gpio_set_value(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 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 40 | i2c_mux_gpio_set(mux, mux->data.values[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 | |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 54 | static int __devinit match_gpio_chip_by_label(struct gpio_chip *chip, |
| 55 | void *data) |
| 56 | { |
| 57 | return !strcmp(chip->label, data); |
| 58 | } |
| 59 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 60 | static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev) |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 61 | { |
| 62 | struct gpiomux *mux; |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 63 | struct i2c_mux_gpio_platform_data *pdata; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 64 | struct i2c_adapter *parent; |
| 65 | int (*deselect) (struct i2c_adapter *, void *, u32); |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 66 | unsigned initial_state, gpio_base; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 67 | int i, ret; |
| 68 | |
| 69 | pdata = pdev->dev.platform_data; |
| 70 | if (!pdata) { |
| 71 | dev_err(&pdev->dev, "Missing platform data\n"); |
| 72 | return -ENODEV; |
| 73 | } |
| 74 | |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 75 | /* |
| 76 | * If a GPIO chip name is provided, the GPIO pin numbers provided are |
| 77 | * relative to its base GPIO number. Otherwise they are absolute. |
| 78 | */ |
| 79 | if (pdata->gpio_chip) { |
| 80 | struct gpio_chip *gpio; |
| 81 | |
| 82 | gpio = gpiochip_find(pdata->gpio_chip, |
| 83 | match_gpio_chip_by_label); |
| 84 | if (!gpio) |
| 85 | return -EPROBE_DEFER; |
| 86 | |
| 87 | gpio_base = gpio->base; |
| 88 | } else { |
| 89 | gpio_base = 0; |
| 90 | } |
| 91 | |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 92 | parent = i2c_get_adapter(pdata->parent); |
| 93 | if (!parent) { |
| 94 | dev_err(&pdev->dev, "Parent adapter (%d) not found\n", |
| 95 | pdata->parent); |
| 96 | return -ENODEV; |
| 97 | } |
| 98 | |
Maxime Ripard | 2614a85 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 99 | mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 100 | if (!mux) { |
| 101 | ret = -ENOMEM; |
| 102 | goto alloc_failed; |
| 103 | } |
| 104 | |
| 105 | mux->parent = parent; |
| 106 | mux->data = *pdata; |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 107 | mux->gpio_base = gpio_base; |
Maxime Ripard | 2614a85 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 108 | mux->adap = devm_kzalloc(&pdev->dev, |
| 109 | sizeof(*mux->adap) * pdata->n_values, |
| 110 | GFP_KERNEL); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 111 | if (!mux->adap) { |
| 112 | ret = -ENOMEM; |
Maxime Ripard | 2614a85 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 113 | goto alloc_failed; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 116 | if (pdata->idle != I2C_MUX_GPIO_NO_IDLE) { |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 117 | initial_state = pdata->idle; |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 118 | deselect = i2c_mux_gpio_deselect; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 119 | } else { |
| 120 | initial_state = pdata->values[0]; |
| 121 | deselect = NULL; |
| 122 | } |
| 123 | |
| 124 | for (i = 0; i < pdata->n_gpios; i++) { |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 125 | ret = gpio_request(gpio_base + pdata->gpios[i], "i2c-mux-gpio"); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 126 | if (ret) |
| 127 | goto err_request_gpio; |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 128 | gpio_direction_output(gpio_base + pdata->gpios[i], |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 129 | initial_state & (1 << i)); |
| 130 | } |
| 131 | |
| 132 | for (i = 0; i < pdata->n_values; i++) { |
| 133 | u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0; |
Jean Delvare | eee543e | 2012-10-05 22:23:51 +0200 | [diff] [blame] | 134 | unsigned int class = pdata->classes ? pdata->classes[i] : 0; |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 135 | |
Jean Delvare | eee543e | 2012-10-05 22:23:51 +0200 | [diff] [blame] | 136 | mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr, |
| 137 | i, class, |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 138 | i2c_mux_gpio_select, deselect); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 139 | if (!mux->adap[i]) { |
| 140 | ret = -ENODEV; |
| 141 | dev_err(&pdev->dev, "Failed to add adapter %d\n", i); |
| 142 | goto add_adapter_failed; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | dev_info(&pdev->dev, "%d port mux on %s adapter\n", |
| 147 | pdata->n_values, parent->name); |
| 148 | |
| 149 | platform_set_drvdata(pdev, mux); |
| 150 | |
| 151 | return 0; |
| 152 | |
| 153 | add_adapter_failed: |
| 154 | for (; i > 0; i--) |
| 155 | i2c_del_mux_adapter(mux->adap[i - 1]); |
| 156 | i = pdata->n_gpios; |
| 157 | err_request_gpio: |
| 158 | for (; i > 0; i--) |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 159 | gpio_free(gpio_base + pdata->gpios[i - 1]); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 160 | alloc_failed: |
| 161 | i2c_put_adapter(parent); |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 166 | static int __devexit i2c_mux_gpio_remove(struct platform_device *pdev) |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 167 | { |
| 168 | struct gpiomux *mux = platform_get_drvdata(pdev); |
| 169 | int i; |
| 170 | |
| 171 | for (i = 0; i < mux->data.n_values; i++) |
| 172 | i2c_del_mux_adapter(mux->adap[i]); |
| 173 | |
| 174 | for (i = 0; i < mux->data.n_gpios; i++) |
Jean Delvare | e7ee514 | 2012-10-05 22:23:54 +0200 | [diff] [blame^] | 175 | gpio_free(mux->gpio_base + mux->data.gpios[i]); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 176 | |
| 177 | platform_set_drvdata(pdev, NULL); |
| 178 | i2c_put_adapter(mux->parent); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 183 | static struct platform_driver i2c_mux_gpio_driver = { |
| 184 | .probe = i2c_mux_gpio_probe, |
| 185 | .remove = __devexit_p(i2c_mux_gpio_remove), |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 186 | .driver = { |
| 187 | .owner = THIS_MODULE, |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 188 | .name = "i2c-mux-gpio", |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 189 | }, |
| 190 | }; |
| 191 | |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 192 | module_platform_driver(i2c_mux_gpio_driver); |
Peter Korsgaard | 92ed1a7 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 193 | |
| 194 | MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver"); |
| 195 | MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>"); |
| 196 | MODULE_LICENSE("GPL"); |
Jean Delvare | e7065e2 | 2012-04-28 15:32:06 +0200 | [diff] [blame] | 197 | MODULE_ALIAS("platform:i2c-mux-gpio"); |