| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * I2C multiplexer using GPIO API |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 10 | 7 | |
|---|
| 11 | 8 | #include <linux/i2c.h> |
|---|
| .. | .. |
|---|
| 14 | 11 | #include <linux/platform_device.h> |
|---|
| 15 | 12 | #include <linux/module.h> |
|---|
| 16 | 13 | #include <linux/slab.h> |
|---|
| 17 | | -#include <linux/gpio.h> |
|---|
| 14 | +#include <linux/bits.h> |
|---|
| 15 | +#include <linux/gpio/consumer.h> |
|---|
| 16 | +/* FIXME: stop poking around inside gpiolib */ |
|---|
| 18 | 17 | #include "../../gpio/gpiolib.h" |
|---|
| 19 | | -#include <linux/of_gpio.h> |
|---|
| 20 | 18 | |
|---|
| 21 | 19 | struct gpiomux { |
|---|
| 22 | 20 | struct i2c_mux_gpio_platform_data data; |
|---|
| 23 | | - unsigned gpio_base; |
|---|
| 21 | + int ngpios; |
|---|
| 24 | 22 | struct gpio_desc **gpios; |
|---|
| 25 | | - int *values; |
|---|
| 26 | 23 | }; |
|---|
| 27 | 24 | |
|---|
| 28 | 25 | static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val) |
|---|
| 29 | 26 | { |
|---|
| 30 | | - int i; |
|---|
| 27 | + DECLARE_BITMAP(values, BITS_PER_TYPE(val)); |
|---|
| 31 | 28 | |
|---|
| 32 | | - for (i = 0; i < mux->data.n_gpios; i++) |
|---|
| 33 | | - mux->values[i] = (val >> i) & 1; |
|---|
| 29 | + values[0] = val; |
|---|
| 34 | 30 | |
|---|
| 35 | | - gpiod_set_array_value_cansleep(mux->data.n_gpios, |
|---|
| 36 | | - mux->gpios, mux->values); |
|---|
| 31 | + gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values); |
|---|
| 37 | 32 | } |
|---|
| 38 | 33 | |
|---|
| 39 | 34 | static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan) |
|---|
| .. | .. |
|---|
| 54 | 49 | return 0; |
|---|
| 55 | 50 | } |
|---|
| 56 | 51 | |
|---|
| 57 | | -static int match_gpio_chip_by_label(struct gpio_chip *chip, |
|---|
| 58 | | - void *data) |
|---|
| 59 | | -{ |
|---|
| 60 | | - return !strcmp(chip->label, data); |
|---|
| 61 | | -} |
|---|
| 62 | | - |
|---|
| 63 | 52 | #ifdef CONFIG_OF |
|---|
| 64 | 53 | static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, |
|---|
| 65 | 54 | struct platform_device *pdev) |
|---|
| .. | .. |
|---|
| 67 | 56 | struct device_node *np = pdev->dev.of_node; |
|---|
| 68 | 57 | struct device_node *adapter_np, *child; |
|---|
| 69 | 58 | struct i2c_adapter *adapter; |
|---|
| 70 | | - unsigned *values, *gpios; |
|---|
| 71 | | - int i = 0, ret; |
|---|
| 59 | + unsigned *values; |
|---|
| 60 | + int i = 0; |
|---|
| 72 | 61 | |
|---|
| 73 | 62 | if (!np) |
|---|
| 74 | 63 | return -ENODEV; |
|---|
| .. | .. |
|---|
| 105 | 94 | if (of_property_read_u32(np, "idle-state", &mux->data.idle)) |
|---|
| 106 | 95 | mux->data.idle = I2C_MUX_GPIO_NO_IDLE; |
|---|
| 107 | 96 | |
|---|
| 108 | | - mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios"); |
|---|
| 109 | | - if (mux->data.n_gpios < 0) { |
|---|
| 110 | | - dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n"); |
|---|
| 111 | | - return -EINVAL; |
|---|
| 112 | | - } |
|---|
| 113 | | - |
|---|
| 114 | | - gpios = devm_kcalloc(&pdev->dev, |
|---|
| 115 | | - mux->data.n_gpios, sizeof(*mux->data.gpios), |
|---|
| 116 | | - GFP_KERNEL); |
|---|
| 117 | | - if (!gpios) { |
|---|
| 118 | | - dev_err(&pdev->dev, "Cannot allocate gpios array"); |
|---|
| 119 | | - return -ENOMEM; |
|---|
| 120 | | - } |
|---|
| 121 | | - |
|---|
| 122 | | - for (i = 0; i < mux->data.n_gpios; i++) { |
|---|
| 123 | | - ret = of_get_named_gpio(np, "mux-gpios", i); |
|---|
| 124 | | - if (ret < 0) |
|---|
| 125 | | - return ret; |
|---|
| 126 | | - gpios[i] = ret; |
|---|
| 127 | | - } |
|---|
| 128 | | - |
|---|
| 129 | | - mux->data.gpios = gpios; |
|---|
| 130 | | - |
|---|
| 131 | 97 | return 0; |
|---|
| 132 | 98 | } |
|---|
| 133 | 99 | #else |
|---|
| .. | .. |
|---|
| 144 | 110 | struct gpiomux *mux; |
|---|
| 145 | 111 | struct i2c_adapter *parent; |
|---|
| 146 | 112 | struct i2c_adapter *root; |
|---|
| 147 | | - unsigned initial_state, gpio_base; |
|---|
| 148 | | - int i, ret; |
|---|
| 113 | + unsigned initial_state; |
|---|
| 114 | + int i, ngpios, ret; |
|---|
| 149 | 115 | |
|---|
| 150 | 116 | mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); |
|---|
| 151 | 117 | if (!mux) |
|---|
| .. | .. |
|---|
| 160 | 126 | sizeof(mux->data)); |
|---|
| 161 | 127 | } |
|---|
| 162 | 128 | |
|---|
| 163 | | - /* |
|---|
| 164 | | - * If a GPIO chip name is provided, the GPIO pin numbers provided are |
|---|
| 165 | | - * relative to its base GPIO number. Otherwise they are absolute. |
|---|
| 166 | | - */ |
|---|
| 167 | | - if (mux->data.gpio_chip) { |
|---|
| 168 | | - struct gpio_chip *gpio; |
|---|
| 169 | | - |
|---|
| 170 | | - gpio = gpiochip_find(mux->data.gpio_chip, |
|---|
| 171 | | - match_gpio_chip_by_label); |
|---|
| 172 | | - if (!gpio) |
|---|
| 173 | | - return -EPROBE_DEFER; |
|---|
| 174 | | - |
|---|
| 175 | | - gpio_base = gpio->base; |
|---|
| 176 | | - } else { |
|---|
| 177 | | - gpio_base = 0; |
|---|
| 129 | + ngpios = gpiod_count(&pdev->dev, "mux"); |
|---|
| 130 | + if (ngpios <= 0) { |
|---|
| 131 | + dev_err(&pdev->dev, "no valid gpios provided\n"); |
|---|
| 132 | + return ngpios ?: -EINVAL; |
|---|
| 178 | 133 | } |
|---|
| 134 | + mux->ngpios = ngpios; |
|---|
| 179 | 135 | |
|---|
| 180 | 136 | parent = i2c_get_adapter(mux->data.parent); |
|---|
| 181 | 137 | if (!parent) |
|---|
| 182 | 138 | return -EPROBE_DEFER; |
|---|
| 183 | 139 | |
|---|
| 184 | 140 | muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, |
|---|
| 185 | | - mux->data.n_gpios * sizeof(*mux->gpios) + |
|---|
| 186 | | - mux->data.n_gpios * sizeof(*mux->values), 0, |
|---|
| 141 | + ngpios * sizeof(*mux->gpios), 0, |
|---|
| 187 | 142 | i2c_mux_gpio_select, NULL); |
|---|
| 188 | 143 | if (!muxc) { |
|---|
| 189 | 144 | ret = -ENOMEM; |
|---|
| 190 | 145 | goto alloc_failed; |
|---|
| 191 | 146 | } |
|---|
| 192 | 147 | mux->gpios = muxc->priv; |
|---|
| 193 | | - mux->values = (int *)(mux->gpios + mux->data.n_gpios); |
|---|
| 194 | 148 | muxc->priv = mux; |
|---|
| 195 | 149 | |
|---|
| 196 | 150 | platform_set_drvdata(pdev, muxc); |
|---|
| .. | .. |
|---|
| 198 | 152 | root = i2c_root_adapter(&parent->dev); |
|---|
| 199 | 153 | |
|---|
| 200 | 154 | muxc->mux_locked = true; |
|---|
| 201 | | - mux->gpio_base = gpio_base; |
|---|
| 202 | 155 | |
|---|
| 203 | 156 | if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { |
|---|
| 204 | 157 | initial_state = mux->data.idle; |
|---|
| .. | .. |
|---|
| 207 | 160 | initial_state = mux->data.values[0]; |
|---|
| 208 | 161 | } |
|---|
| 209 | 162 | |
|---|
| 210 | | - for (i = 0; i < mux->data.n_gpios; i++) { |
|---|
| 163 | + for (i = 0; i < ngpios; i++) { |
|---|
| 211 | 164 | struct device *gpio_dev; |
|---|
| 212 | | - struct gpio_desc *gpio_desc; |
|---|
| 165 | + struct gpio_desc *gpiod; |
|---|
| 166 | + enum gpiod_flags flag; |
|---|
| 213 | 167 | |
|---|
| 214 | | - ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio"); |
|---|
| 215 | | - if (ret) { |
|---|
| 216 | | - dev_err(&pdev->dev, "Failed to request GPIO %d\n", |
|---|
| 217 | | - mux->data.gpios[i]); |
|---|
| 218 | | - goto err_request_gpio; |
|---|
| 168 | + if (initial_state & BIT(i)) |
|---|
| 169 | + flag = GPIOD_OUT_HIGH; |
|---|
| 170 | + else |
|---|
| 171 | + flag = GPIOD_OUT_LOW; |
|---|
| 172 | + gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag); |
|---|
| 173 | + if (IS_ERR(gpiod)) { |
|---|
| 174 | + ret = PTR_ERR(gpiod); |
|---|
| 175 | + goto alloc_failed; |
|---|
| 219 | 176 | } |
|---|
| 220 | 177 | |
|---|
| 221 | | - ret = gpio_direction_output(gpio_base + mux->data.gpios[i], |
|---|
| 222 | | - initial_state & (1 << i)); |
|---|
| 223 | | - if (ret) { |
|---|
| 224 | | - dev_err(&pdev->dev, |
|---|
| 225 | | - "Failed to set direction of GPIO %d to output\n", |
|---|
| 226 | | - mux->data.gpios[i]); |
|---|
| 227 | | - i++; /* gpio_request above succeeded, so must free */ |
|---|
| 228 | | - goto err_request_gpio; |
|---|
| 229 | | - } |
|---|
| 230 | | - |
|---|
| 231 | | - gpio_desc = gpio_to_desc(gpio_base + mux->data.gpios[i]); |
|---|
| 232 | | - mux->gpios[i] = gpio_desc; |
|---|
| 178 | + mux->gpios[i] = gpiod; |
|---|
| 233 | 179 | |
|---|
| 234 | 180 | if (!muxc->mux_locked) |
|---|
| 235 | 181 | continue; |
|---|
| 236 | 182 | |
|---|
| 237 | | - gpio_dev = &gpio_desc->gdev->dev; |
|---|
| 183 | + /* FIXME: find a proper way to access the GPIO device */ |
|---|
| 184 | + gpio_dev = &gpiod->gdev->dev; |
|---|
| 238 | 185 | muxc->mux_locked = i2c_root_adapter(gpio_dev) == root; |
|---|
| 239 | 186 | } |
|---|
| 240 | 187 | |
|---|
| .. | .. |
|---|
| 257 | 204 | |
|---|
| 258 | 205 | add_adapter_failed: |
|---|
| 259 | 206 | i2c_mux_del_adapters(muxc); |
|---|
| 260 | | - i = mux->data.n_gpios; |
|---|
| 261 | | -err_request_gpio: |
|---|
| 262 | | - for (; i > 0; i--) |
|---|
| 263 | | - gpio_free(gpio_base + mux->data.gpios[i - 1]); |
|---|
| 264 | 207 | alloc_failed: |
|---|
| 265 | 208 | i2c_put_adapter(parent); |
|---|
| 266 | 209 | |
|---|
| .. | .. |
|---|
| 270 | 213 | static int i2c_mux_gpio_remove(struct platform_device *pdev) |
|---|
| 271 | 214 | { |
|---|
| 272 | 215 | struct i2c_mux_core *muxc = platform_get_drvdata(pdev); |
|---|
| 273 | | - struct gpiomux *mux = i2c_mux_priv(muxc); |
|---|
| 274 | | - int i; |
|---|
| 275 | 216 | |
|---|
| 276 | 217 | i2c_mux_del_adapters(muxc); |
|---|
| 277 | | - |
|---|
| 278 | | - for (i = 0; i < mux->data.n_gpios; i++) |
|---|
| 279 | | - gpio_free(mux->gpio_base + mux->data.gpios[i]); |
|---|
| 280 | | - |
|---|
| 281 | 218 | i2c_put_adapter(muxc->parent); |
|---|
| 282 | 219 | |
|---|
| 283 | 220 | return 0; |
|---|