1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2025 Linaro Ltd. 4 */ 5 6 #include <linux/auxiliary_bus.h> 7 #include <linux/cleanup.h> 8 #include <linux/device.h> 9 #include <linux/err.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/gpio/driver.h> 12 #include <linux/module.h> 13 #include <linux/string_choices.h> 14 #include <linux/types.h> 15 16 #include "gpiolib-shared.h" 17 18 struct gpio_shared_proxy_data { 19 struct gpio_chip gc; 20 struct gpio_shared_desc *shared_desc; 21 struct device *dev; 22 bool voted_high; 23 }; 24 25 static int 26 gpio_shared_proxy_set_unlocked(struct gpio_shared_proxy_data *proxy, 27 int (*set_func)(struct gpio_desc *desc, int value), 28 int value) 29 { 30 struct gpio_shared_desc *shared_desc = proxy->shared_desc; 31 struct gpio_desc *desc = shared_desc->desc; 32 int ret = 0; 33 34 gpio_shared_lockdep_assert(shared_desc); 35 36 if (value) { 37 /* User wants to set value to high. */ 38 if (proxy->voted_high) 39 /* Already voted for high, nothing to do. */ 40 goto out; 41 42 /* Haven't voted for high yet. */ 43 if (!shared_desc->highcnt) { 44 /* 45 * Current value is low, need to actually set value 46 * to high. 47 */ 48 ret = set_func(desc, 1); 49 if (ret) 50 goto out; 51 } 52 53 shared_desc->highcnt++; 54 proxy->voted_high = true; 55 56 goto out; 57 } 58 59 /* Desired value is low. */ 60 if (!proxy->voted_high) 61 /* We didn't vote for high, nothing to do. */ 62 goto out; 63 64 /* We previously voted for high. */ 65 if (shared_desc->highcnt == 1) { 66 /* This is the last remaining vote for high, set value to low. */ 67 ret = set_func(desc, 0); 68 if (ret) 69 goto out; 70 } 71 72 shared_desc->highcnt--; 73 proxy->voted_high = false; 74 75 out: 76 if (shared_desc->highcnt) 77 dev_dbg(proxy->dev, 78 "Voted for value '%s', effective value is 'high', number of votes for 'high': %u\n", 79 str_high_low(value), shared_desc->highcnt); 80 else 81 dev_dbg(proxy->dev, "Voted for value 'low', effective value is 'low'\n"); 82 83 return ret; 84 } 85 86 static int gpio_shared_proxy_request(struct gpio_chip *gc, unsigned int offset) 87 { 88 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 89 struct gpio_shared_desc *shared_desc = proxy->shared_desc; 90 91 guard(gpio_shared_desc_lock)(shared_desc); 92 93 proxy->shared_desc->usecnt++; 94 95 dev_dbg(proxy->dev, "Shared GPIO requested, number of users: %u\n", 96 proxy->shared_desc->usecnt); 97 98 return 0; 99 } 100 101 static void gpio_shared_proxy_free(struct gpio_chip *gc, unsigned int offset) 102 { 103 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 104 struct gpio_shared_desc *shared_desc = proxy->shared_desc; 105 int ret; 106 107 guard(gpio_shared_desc_lock)(shared_desc); 108 109 if (proxy->voted_high) { 110 ret = gpio_shared_proxy_set_unlocked(proxy, 111 shared_desc->can_sleep ? gpiod_set_value_cansleep : gpiod_set_value, 0); 112 if (ret) 113 dev_err(proxy->dev, 114 "Failed to unset the shared GPIO value on release: %d\n", ret); 115 } 116 117 proxy->shared_desc->usecnt--; 118 119 dev_dbg(proxy->dev, "Shared GPIO freed, number of users: %u\n", 120 proxy->shared_desc->usecnt); 121 } 122 123 static int gpio_shared_proxy_set_config(struct gpio_chip *gc, 124 unsigned int offset, unsigned long cfg) 125 { 126 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 127 struct gpio_shared_desc *shared_desc = proxy->shared_desc; 128 struct gpio_desc *desc = shared_desc->desc; 129 int ret; 130 131 guard(gpio_shared_desc_lock)(shared_desc); 132 133 if (shared_desc->usecnt > 1) { 134 if (shared_desc->cfg != cfg) { 135 dev_dbg(proxy->dev, 136 "Shared GPIO's configuration already set, accepting changes but users may conflict!!\n"); 137 } else { 138 dev_dbg(proxy->dev, "Equal config requested, nothing to do\n"); 139 return 0; 140 } 141 } 142 143 ret = gpiod_set_config(desc, cfg); 144 if (ret && ret != -ENOTSUPP) 145 return ret; 146 147 shared_desc->cfg = cfg; 148 return 0; 149 } 150 151 static int gpio_shared_proxy_direction_input(struct gpio_chip *gc, 152 unsigned int offset) 153 { 154 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 155 struct gpio_shared_desc *shared_desc = proxy->shared_desc; 156 struct gpio_desc *desc = shared_desc->desc; 157 int dir; 158 159 guard(gpio_shared_desc_lock)(shared_desc); 160 161 if (shared_desc->usecnt == 1) { 162 dev_dbg(proxy->dev, 163 "Only one user of this shared GPIO, allowing to set direction to input\n"); 164 165 return gpiod_direction_input(desc); 166 } 167 168 dir = gpiod_get_direction(desc); 169 if (dir < 0) 170 return dir; 171 172 if (dir == GPIO_LINE_DIRECTION_OUT) { 173 dev_dbg(proxy->dev, 174 "Shared GPIO's direction already set to output, refusing to change\n"); 175 return -EPERM; 176 } 177 178 return 0; 179 } 180 181 static int gpio_shared_proxy_direction_output(struct gpio_chip *gc, 182 unsigned int offset, int value) 183 { 184 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 185 struct gpio_shared_desc *shared_desc = proxy->shared_desc; 186 struct gpio_desc *desc = shared_desc->desc; 187 int ret, dir; 188 189 guard(gpio_shared_desc_lock)(shared_desc); 190 191 if (shared_desc->usecnt == 1) { 192 dev_dbg(proxy->dev, 193 "Only one user of this shared GPIO, allowing to set direction to output with value '%s'\n", 194 str_high_low(value)); 195 196 ret = gpiod_direction_output(desc, value); 197 if (ret) 198 return ret; 199 200 if (value) { 201 proxy->voted_high = true; 202 shared_desc->highcnt = 1; 203 } else { 204 proxy->voted_high = false; 205 shared_desc->highcnt = 0; 206 } 207 208 return 0; 209 } 210 211 dir = gpiod_get_direction(desc); 212 if (dir < 0) 213 return dir; 214 215 if (dir == GPIO_LINE_DIRECTION_IN) { 216 dev_dbg(proxy->dev, 217 "Shared GPIO's direction already set to input, refusing to change\n"); 218 return -EPERM; 219 } 220 221 return gpio_shared_proxy_set_unlocked(proxy, gpiod_direction_output, value); 222 } 223 224 static int gpio_shared_proxy_get(struct gpio_chip *gc, unsigned int offset) 225 { 226 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 227 228 return gpiod_get_value(proxy->shared_desc->desc); 229 } 230 231 static int gpio_shared_proxy_get_cansleep(struct gpio_chip *gc, 232 unsigned int offset) 233 { 234 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 235 236 return gpiod_get_value_cansleep(proxy->shared_desc->desc); 237 } 238 239 static int gpio_shared_proxy_do_set(struct gpio_shared_proxy_data *proxy, 240 int (*set_func)(struct gpio_desc *desc, int value), 241 int value) 242 { 243 guard(gpio_shared_desc_lock)(proxy->shared_desc); 244 245 return gpio_shared_proxy_set_unlocked(proxy, set_func, value); 246 } 247 248 static int gpio_shared_proxy_set(struct gpio_chip *gc, unsigned int offset, 249 int value) 250 { 251 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 252 253 return gpio_shared_proxy_do_set(proxy, gpiod_set_value, value); 254 } 255 256 static int gpio_shared_proxy_set_cansleep(struct gpio_chip *gc, 257 unsigned int offset, int value) 258 { 259 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 260 261 return gpio_shared_proxy_do_set(proxy, gpiod_set_value_cansleep, value); 262 } 263 264 static int gpio_shared_proxy_get_direction(struct gpio_chip *gc, 265 unsigned int offset) 266 { 267 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 268 269 return gpiod_get_direction(proxy->shared_desc->desc); 270 } 271 272 static int gpio_shared_proxy_to_irq(struct gpio_chip *gc, unsigned int offset) 273 { 274 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); 275 276 return gpiod_to_irq(proxy->shared_desc->desc); 277 } 278 279 static int gpio_shared_proxy_probe(struct auxiliary_device *adev, 280 const struct auxiliary_device_id *id) 281 { 282 struct gpio_shared_proxy_data *proxy; 283 struct gpio_shared_desc *shared_desc; 284 struct device *dev = &adev->dev; 285 struct gpio_chip *gc; 286 287 shared_desc = devm_gpiod_shared_get(dev); 288 if (IS_ERR(shared_desc)) 289 return PTR_ERR(shared_desc); 290 291 proxy = devm_kzalloc(dev, sizeof(*proxy), GFP_KERNEL); 292 if (!proxy) 293 return -ENOMEM; 294 295 proxy->shared_desc = shared_desc; 296 proxy->dev = dev; 297 298 gc = &proxy->gc; 299 gc->base = -1; 300 gc->ngpio = 1; 301 gc->label = dev_name(dev); 302 gc->parent = dev; 303 gc->owner = THIS_MODULE; 304 gc->can_sleep = shared_desc->can_sleep; 305 306 gc->request = gpio_shared_proxy_request; 307 gc->free = gpio_shared_proxy_free; 308 gc->set_config = gpio_shared_proxy_set_config; 309 gc->direction_input = gpio_shared_proxy_direction_input; 310 gc->direction_output = gpio_shared_proxy_direction_output; 311 if (gc->can_sleep) { 312 gc->set = gpio_shared_proxy_set_cansleep; 313 gc->get = gpio_shared_proxy_get_cansleep; 314 } else { 315 gc->set = gpio_shared_proxy_set; 316 gc->get = gpio_shared_proxy_get; 317 } 318 gc->get_direction = gpio_shared_proxy_get_direction; 319 gc->to_irq = gpio_shared_proxy_to_irq; 320 321 return devm_gpiochip_add_data(dev, &proxy->gc, proxy); 322 } 323 324 static const struct auxiliary_device_id gpio_shared_proxy_id_table[] = { 325 { .name = "gpiolib_shared.proxy" }, 326 {}, 327 }; 328 MODULE_DEVICE_TABLE(auxiliary, gpio_shared_proxy_id_table); 329 330 static struct auxiliary_driver gpio_shared_proxy_driver = { 331 .driver = { 332 .name = "gpio-shared-proxy", 333 .suppress_bind_attrs = true, 334 }, 335 .probe = gpio_shared_proxy_probe, 336 .id_table = gpio_shared_proxy_id_table, 337 }; 338 module_auxiliary_driver(gpio_shared_proxy_driver); 339 340 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>"); 341 MODULE_DESCRIPTION("Shared GPIO mux driver."); 342 MODULE_LICENSE("GPL"); 343