| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * gpio-regulator.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 12 | 13 | * Copyright (c) 2009 Nokia Corporation |
|---|
| 13 | 14 | * Roger Quadros <ext-roger.quadros@nokia.com> |
|---|
| 14 | 15 | * |
|---|
| 15 | | - * This program is free software; you can redistribute it and/or |
|---|
| 16 | | - * modify it under the terms of the GNU General Public License as |
|---|
| 17 | | - * published by the Free Software Foundation; either version 2 of the |
|---|
| 18 | | - * License, or (at your option) any later version. |
|---|
| 19 | | - * |
|---|
| 20 | 16 | * This is useful for systems with mixed controllable and |
|---|
| 21 | 17 | * non-controllable regulators, as well as for allowing testing on |
|---|
| 22 | 18 | * systems with no controllable regulators. |
|---|
| .. | .. |
|---|
| 30 | 26 | #include <linux/regulator/machine.h> |
|---|
| 31 | 27 | #include <linux/regulator/of_regulator.h> |
|---|
| 32 | 28 | #include <linux/regulator/gpio-regulator.h> |
|---|
| 33 | | -#include <linux/gpio.h> |
|---|
| 29 | +#include <linux/gpio/consumer.h> |
|---|
| 34 | 30 | #include <linux/slab.h> |
|---|
| 35 | 31 | #include <linux/of.h> |
|---|
| 36 | | -#include <linux/of_gpio.h> |
|---|
| 37 | 32 | |
|---|
| 38 | 33 | struct gpio_regulator_data { |
|---|
| 39 | 34 | struct regulator_desc desc; |
|---|
| 40 | | - struct regulator_dev *dev; |
|---|
| 41 | 35 | |
|---|
| 42 | | - struct gpio *gpios; |
|---|
| 36 | + struct gpio_desc **gpiods; |
|---|
| 43 | 37 | int nr_gpios; |
|---|
| 44 | 38 | |
|---|
| 45 | 39 | struct gpio_regulator_state *states; |
|---|
| .. | .. |
|---|
| 82 | 76 | |
|---|
| 83 | 77 | for (ptr = 0; ptr < data->nr_gpios; ptr++) { |
|---|
| 84 | 78 | state = (target & (1 << ptr)) >> ptr; |
|---|
| 85 | | - gpio_set_value_cansleep(data->gpios[ptr].gpio, state); |
|---|
| 79 | + gpiod_set_value_cansleep(data->gpiods[ptr], state); |
|---|
| 86 | 80 | } |
|---|
| 87 | 81 | data->state = target; |
|---|
| 88 | 82 | |
|---|
| .. | .. |
|---|
| 119 | 113 | |
|---|
| 120 | 114 | for (ptr = 0; ptr < data->nr_gpios; ptr++) { |
|---|
| 121 | 115 | state = (target & (1 << ptr)) >> ptr; |
|---|
| 122 | | - gpio_set_value_cansleep(data->gpios[ptr].gpio, state); |
|---|
| 116 | + gpiod_set_value_cansleep(data->gpiods[ptr], state); |
|---|
| 123 | 117 | } |
|---|
| 124 | 118 | data->state = target; |
|---|
| 125 | 119 | |
|---|
| 126 | 120 | return 0; |
|---|
| 127 | 121 | } |
|---|
| 128 | 122 | |
|---|
| 129 | | -static struct regulator_ops gpio_regulator_voltage_ops = { |
|---|
| 123 | +static const struct regulator_ops gpio_regulator_voltage_ops = { |
|---|
| 130 | 124 | .get_voltage = gpio_regulator_get_value, |
|---|
| 131 | 125 | .set_voltage = gpio_regulator_set_voltage, |
|---|
| 132 | 126 | .list_voltage = gpio_regulator_list_voltage, |
|---|
| .. | .. |
|---|
| 138 | 132 | { |
|---|
| 139 | 133 | struct gpio_regulator_config *config; |
|---|
| 140 | 134 | const char *regtype; |
|---|
| 141 | | - int proplen, gpio, i; |
|---|
| 135 | + int proplen, i; |
|---|
| 136 | + int ngpios; |
|---|
| 142 | 137 | int ret; |
|---|
| 143 | 138 | |
|---|
| 144 | 139 | config = devm_kzalloc(dev, |
|---|
| .. | .. |
|---|
| 153 | 148 | |
|---|
| 154 | 149 | config->supply_name = config->init_data->constraints.name; |
|---|
| 155 | 150 | |
|---|
| 156 | | - if (of_property_read_bool(np, "enable-active-high")) |
|---|
| 157 | | - config->enable_high = true; |
|---|
| 151 | + if (config->init_data->constraints.boot_on) |
|---|
| 152 | + config->enabled_at_boot = true; |
|---|
| 158 | 153 | |
|---|
| 154 | + /* |
|---|
| 155 | + * Do not use: undocumented device tree property. |
|---|
| 156 | + * This is kept around solely for device tree ABI stability. |
|---|
| 157 | + */ |
|---|
| 159 | 158 | if (of_property_read_bool(np, "enable-at-boot")) |
|---|
| 160 | 159 | config->enabled_at_boot = true; |
|---|
| 161 | 160 | |
|---|
| 162 | 161 | of_property_read_u32(np, "startup-delay-us", &config->startup_delay); |
|---|
| 163 | 162 | |
|---|
| 164 | | - config->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0); |
|---|
| 165 | | - if (config->enable_gpio < 0 && config->enable_gpio != -ENOENT) |
|---|
| 166 | | - return ERR_PTR(config->enable_gpio); |
|---|
| 167 | | - |
|---|
| 168 | | - /* Fetch GPIOs. - optional property*/ |
|---|
| 169 | | - ret = of_gpio_count(np); |
|---|
| 170 | | - if ((ret < 0) && (ret != -ENOENT)) |
|---|
| 171 | | - return ERR_PTR(ret); |
|---|
| 172 | | - |
|---|
| 173 | | - if (ret > 0) { |
|---|
| 174 | | - config->nr_gpios = ret; |
|---|
| 175 | | - config->gpios = devm_kcalloc(dev, |
|---|
| 176 | | - config->nr_gpios, sizeof(struct gpio), |
|---|
| 177 | | - GFP_KERNEL); |
|---|
| 178 | | - if (!config->gpios) |
|---|
| 163 | + /* Fetch GPIO init levels */ |
|---|
| 164 | + ngpios = gpiod_count(dev, NULL); |
|---|
| 165 | + if (ngpios > 0) { |
|---|
| 166 | + config->gflags = devm_kzalloc(dev, |
|---|
| 167 | + sizeof(enum gpiod_flags) |
|---|
| 168 | + * ngpios, |
|---|
| 169 | + GFP_KERNEL); |
|---|
| 170 | + if (!config->gflags) |
|---|
| 179 | 171 | return ERR_PTR(-ENOMEM); |
|---|
| 180 | 172 | |
|---|
| 181 | | - proplen = of_property_count_u32_elems(np, "gpios-states"); |
|---|
| 182 | | - /* optional property */ |
|---|
| 183 | | - if (proplen < 0) |
|---|
| 184 | | - proplen = 0; |
|---|
| 173 | + for (i = 0; i < ngpios; i++) { |
|---|
| 174 | + u32 val; |
|---|
| 185 | 175 | |
|---|
| 186 | | - if (proplen > 0 && proplen != config->nr_gpios) { |
|---|
| 187 | | - dev_warn(dev, "gpios <-> gpios-states mismatch\n"); |
|---|
| 188 | | - proplen = 0; |
|---|
| 189 | | - } |
|---|
| 176 | + ret = of_property_read_u32_index(np, "gpios-states", i, |
|---|
| 177 | + &val); |
|---|
| 190 | 178 | |
|---|
| 191 | | - for (i = 0; i < config->nr_gpios; i++) { |
|---|
| 192 | | - gpio = of_get_named_gpio(np, "gpios", i); |
|---|
| 193 | | - if (gpio < 0) { |
|---|
| 194 | | - if (gpio != -ENOENT) |
|---|
| 195 | | - return ERR_PTR(gpio); |
|---|
| 196 | | - break; |
|---|
| 197 | | - } |
|---|
| 198 | | - config->gpios[i].gpio = gpio; |
|---|
| 199 | | - config->gpios[i].label = config->supply_name; |
|---|
| 200 | | - if (proplen > 0) { |
|---|
| 201 | | - of_property_read_u32_index(np, "gpios-states", |
|---|
| 202 | | - i, &ret); |
|---|
| 203 | | - if (ret) |
|---|
| 204 | | - config->gpios[i].flags = |
|---|
| 205 | | - GPIOF_OUT_INIT_HIGH; |
|---|
| 206 | | - } |
|---|
| 179 | + /* Default to high per specification */ |
|---|
| 180 | + if (ret) |
|---|
| 181 | + config->gflags[i] = GPIOD_OUT_HIGH; |
|---|
| 182 | + else |
|---|
| 183 | + config->gflags[i] = |
|---|
| 184 | + val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
|---|
| 207 | 185 | } |
|---|
| 208 | 186 | } |
|---|
| 187 | + config->ngpios = ngpios; |
|---|
| 209 | 188 | |
|---|
| 210 | 189 | /* Fetch states. */ |
|---|
| 211 | 190 | proplen = of_property_count_u32_elems(np, "states"); |
|---|
| .. | .. |
|---|
| 241 | 220 | regtype); |
|---|
| 242 | 221 | } |
|---|
| 243 | 222 | |
|---|
| 244 | | - if (of_find_property(np, "vin-supply", NULL)) |
|---|
| 245 | | - config->input_supply = "vin"; |
|---|
| 246 | | - |
|---|
| 247 | 223 | return config; |
|---|
| 248 | 224 | } |
|---|
| 249 | 225 | |
|---|
| 250 | | -static struct regulator_ops gpio_regulator_current_ops = { |
|---|
| 226 | +static const struct regulator_ops gpio_regulator_current_ops = { |
|---|
| 251 | 227 | .get_current_limit = gpio_regulator_get_value, |
|---|
| 252 | 228 | .set_current_limit = gpio_regulator_set_current_limit, |
|---|
| 253 | 229 | }; |
|---|
| 254 | 230 | |
|---|
| 255 | 231 | static int gpio_regulator_probe(struct platform_device *pdev) |
|---|
| 256 | 232 | { |
|---|
| 257 | | - struct gpio_regulator_config *config = dev_get_platdata(&pdev->dev); |
|---|
| 258 | | - struct device_node *np = pdev->dev.of_node; |
|---|
| 233 | + struct device *dev = &pdev->dev; |
|---|
| 234 | + struct gpio_regulator_config *config = dev_get_platdata(dev); |
|---|
| 235 | + struct device_node *np = dev->of_node; |
|---|
| 259 | 236 | struct gpio_regulator_data *drvdata; |
|---|
| 260 | 237 | struct regulator_config cfg = { }; |
|---|
| 261 | | - int ptr, ret, state; |
|---|
| 238 | + struct regulator_dev *rdev; |
|---|
| 239 | + enum gpiod_flags gflags; |
|---|
| 240 | + int ptr, ret, state, i; |
|---|
| 262 | 241 | |
|---|
| 263 | | - drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data), |
|---|
| 242 | + drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data), |
|---|
| 264 | 243 | GFP_KERNEL); |
|---|
| 265 | 244 | if (drvdata == NULL) |
|---|
| 266 | 245 | return -ENOMEM; |
|---|
| 267 | 246 | |
|---|
| 268 | 247 | if (np) { |
|---|
| 269 | | - config = of_get_gpio_regulator_config(&pdev->dev, np, |
|---|
| 248 | + config = of_get_gpio_regulator_config(dev, np, |
|---|
| 270 | 249 | &drvdata->desc); |
|---|
| 271 | 250 | if (IS_ERR(config)) |
|---|
| 272 | 251 | return PTR_ERR(config); |
|---|
| 273 | 252 | } |
|---|
| 274 | 253 | |
|---|
| 275 | | - drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL); |
|---|
| 254 | + drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL); |
|---|
| 276 | 255 | if (drvdata->desc.name == NULL) { |
|---|
| 277 | | - dev_err(&pdev->dev, "Failed to allocate supply name\n"); |
|---|
| 256 | + dev_err(dev, "Failed to allocate supply name\n"); |
|---|
| 278 | 257 | return -ENOMEM; |
|---|
| 279 | 258 | } |
|---|
| 280 | 259 | |
|---|
| 281 | | - if (config->input_supply) { |
|---|
| 282 | | - drvdata->desc.supply_name = devm_kstrdup(&pdev->dev, |
|---|
| 283 | | - config->input_supply, |
|---|
| 284 | | - GFP_KERNEL); |
|---|
| 285 | | - if (!drvdata->desc.supply_name) { |
|---|
| 286 | | - dev_err(&pdev->dev, |
|---|
| 287 | | - "Failed to allocate input supply\n"); |
|---|
| 288 | | - ret = -ENOMEM; |
|---|
| 289 | | - goto err_name; |
|---|
| 290 | | - } |
|---|
| 260 | + drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *), |
|---|
| 261 | + GFP_KERNEL); |
|---|
| 262 | + if (!drvdata->gpiods) |
|---|
| 263 | + return -ENOMEM; |
|---|
| 264 | + for (i = 0; i < config->ngpios; i++) { |
|---|
| 265 | + drvdata->gpiods[i] = devm_gpiod_get_index(dev, |
|---|
| 266 | + NULL, |
|---|
| 267 | + i, |
|---|
| 268 | + config->gflags[i]); |
|---|
| 269 | + if (IS_ERR(drvdata->gpiods[i])) |
|---|
| 270 | + return PTR_ERR(drvdata->gpiods[i]); |
|---|
| 271 | + /* This is good to know */ |
|---|
| 272 | + gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name); |
|---|
| 291 | 273 | } |
|---|
| 274 | + drvdata->nr_gpios = config->ngpios; |
|---|
| 292 | 275 | |
|---|
| 293 | | - if (config->nr_gpios != 0) { |
|---|
| 294 | | - drvdata->gpios = kmemdup(config->gpios, |
|---|
| 295 | | - config->nr_gpios * sizeof(struct gpio), |
|---|
| 296 | | - GFP_KERNEL); |
|---|
| 297 | | - if (drvdata->gpios == NULL) { |
|---|
| 298 | | - dev_err(&pdev->dev, "Failed to allocate gpio data\n"); |
|---|
| 299 | | - ret = -ENOMEM; |
|---|
| 300 | | - goto err_name; |
|---|
| 301 | | - } |
|---|
| 302 | | - |
|---|
| 303 | | - drvdata->nr_gpios = config->nr_gpios; |
|---|
| 304 | | - ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios); |
|---|
| 305 | | - if (ret) { |
|---|
| 306 | | - if (ret != -EPROBE_DEFER) |
|---|
| 307 | | - dev_err(&pdev->dev, |
|---|
| 308 | | - "Could not obtain regulator setting GPIOs: %d\n", |
|---|
| 309 | | - ret); |
|---|
| 310 | | - goto err_memgpio; |
|---|
| 311 | | - } |
|---|
| 312 | | - } |
|---|
| 313 | | - |
|---|
| 314 | | - drvdata->states = kmemdup(config->states, |
|---|
| 315 | | - config->nr_states * |
|---|
| 316 | | - sizeof(struct gpio_regulator_state), |
|---|
| 317 | | - GFP_KERNEL); |
|---|
| 276 | + drvdata->states = devm_kmemdup(dev, |
|---|
| 277 | + config->states, |
|---|
| 278 | + config->nr_states * |
|---|
| 279 | + sizeof(struct gpio_regulator_state), |
|---|
| 280 | + GFP_KERNEL); |
|---|
| 318 | 281 | if (drvdata->states == NULL) { |
|---|
| 319 | | - dev_err(&pdev->dev, "Failed to allocate state data\n"); |
|---|
| 320 | | - ret = -ENOMEM; |
|---|
| 321 | | - goto err_stategpio; |
|---|
| 282 | + dev_err(dev, "Failed to allocate state data\n"); |
|---|
| 283 | + return -ENOMEM; |
|---|
| 322 | 284 | } |
|---|
| 323 | 285 | drvdata->nr_states = config->nr_states; |
|---|
| 324 | 286 | |
|---|
| .. | .. |
|---|
| 337 | 299 | drvdata->desc.ops = &gpio_regulator_current_ops; |
|---|
| 338 | 300 | break; |
|---|
| 339 | 301 | default: |
|---|
| 340 | | - dev_err(&pdev->dev, "No regulator type set\n"); |
|---|
| 341 | | - ret = -EINVAL; |
|---|
| 342 | | - goto err_memstate; |
|---|
| 302 | + dev_err(dev, "No regulator type set\n"); |
|---|
| 303 | + return -EINVAL; |
|---|
| 343 | 304 | } |
|---|
| 344 | 305 | |
|---|
| 345 | 306 | /* build initial state from gpio init data. */ |
|---|
| 346 | 307 | state = 0; |
|---|
| 347 | 308 | for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) { |
|---|
| 348 | | - if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH) |
|---|
| 309 | + if (config->gflags[ptr] == GPIOD_OUT_HIGH) |
|---|
| 349 | 310 | state |= (1 << ptr); |
|---|
| 350 | 311 | } |
|---|
| 351 | 312 | drvdata->state = state; |
|---|
| 352 | 313 | |
|---|
| 353 | | - cfg.dev = &pdev->dev; |
|---|
| 314 | + cfg.dev = dev; |
|---|
| 354 | 315 | cfg.init_data = config->init_data; |
|---|
| 355 | 316 | cfg.driver_data = drvdata; |
|---|
| 356 | 317 | cfg.of_node = np; |
|---|
| 357 | 318 | |
|---|
| 358 | | - if (gpio_is_valid(config->enable_gpio)) { |
|---|
| 359 | | - cfg.ena_gpio = config->enable_gpio; |
|---|
| 360 | | - cfg.ena_gpio_initialized = true; |
|---|
| 361 | | - } |
|---|
| 362 | | - cfg.ena_gpio_invert = !config->enable_high; |
|---|
| 363 | | - if (config->enabled_at_boot) { |
|---|
| 364 | | - if (config->enable_high) |
|---|
| 365 | | - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; |
|---|
| 366 | | - else |
|---|
| 367 | | - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; |
|---|
| 368 | | - } else { |
|---|
| 369 | | - if (config->enable_high) |
|---|
| 370 | | - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; |
|---|
| 371 | | - else |
|---|
| 372 | | - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; |
|---|
| 373 | | - } |
|---|
| 319 | + /* |
|---|
| 320 | + * The signal will be inverted by the GPIO core if flagged so in the |
|---|
| 321 | + * descriptor. |
|---|
| 322 | + */ |
|---|
| 323 | + if (config->enabled_at_boot) |
|---|
| 324 | + gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE; |
|---|
| 325 | + else |
|---|
| 326 | + gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE; |
|---|
| 374 | 327 | |
|---|
| 375 | | - drvdata->dev = regulator_register(&drvdata->desc, &cfg); |
|---|
| 376 | | - if (IS_ERR(drvdata->dev)) { |
|---|
| 377 | | - ret = PTR_ERR(drvdata->dev); |
|---|
| 378 | | - dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret); |
|---|
| 379 | | - goto err_memstate; |
|---|
| 328 | + cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags); |
|---|
| 329 | + if (IS_ERR(cfg.ena_gpiod)) |
|---|
| 330 | + return PTR_ERR(cfg.ena_gpiod); |
|---|
| 331 | + |
|---|
| 332 | + rdev = devm_regulator_register(dev, &drvdata->desc, &cfg); |
|---|
| 333 | + if (IS_ERR(rdev)) { |
|---|
| 334 | + ret = PTR_ERR(rdev); |
|---|
| 335 | + dev_err(dev, "Failed to register regulator: %d\n", ret); |
|---|
| 336 | + return ret; |
|---|
| 380 | 337 | } |
|---|
| 381 | 338 | |
|---|
| 382 | 339 | platform_set_drvdata(pdev, drvdata); |
|---|
| 383 | | - |
|---|
| 384 | | - return 0; |
|---|
| 385 | | - |
|---|
| 386 | | -err_memstate: |
|---|
| 387 | | - kfree(drvdata->states); |
|---|
| 388 | | -err_stategpio: |
|---|
| 389 | | - gpio_free_array(drvdata->gpios, drvdata->nr_gpios); |
|---|
| 390 | | -err_memgpio: |
|---|
| 391 | | - kfree(drvdata->gpios); |
|---|
| 392 | | -err_name: |
|---|
| 393 | | - kfree(drvdata->desc.name); |
|---|
| 394 | | - return ret; |
|---|
| 395 | | -} |
|---|
| 396 | | - |
|---|
| 397 | | -static int gpio_regulator_remove(struct platform_device *pdev) |
|---|
| 398 | | -{ |
|---|
| 399 | | - struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev); |
|---|
| 400 | | - |
|---|
| 401 | | - regulator_unregister(drvdata->dev); |
|---|
| 402 | | - |
|---|
| 403 | | - gpio_free_array(drvdata->gpios, drvdata->nr_gpios); |
|---|
| 404 | | - |
|---|
| 405 | | - kfree(drvdata->states); |
|---|
| 406 | | - kfree(drvdata->gpios); |
|---|
| 407 | | - |
|---|
| 408 | | - kfree(drvdata->desc.name); |
|---|
| 409 | 340 | |
|---|
| 410 | 341 | return 0; |
|---|
| 411 | 342 | } |
|---|
| .. | .. |
|---|
| 420 | 351 | |
|---|
| 421 | 352 | static struct platform_driver gpio_regulator_driver = { |
|---|
| 422 | 353 | .probe = gpio_regulator_probe, |
|---|
| 423 | | - .remove = gpio_regulator_remove, |
|---|
| 424 | 354 | .driver = { |
|---|
| 425 | 355 | .name = "gpio-regulator", |
|---|
| 426 | 356 | .of_match_table = of_match_ptr(regulator_gpio_of_match), |
|---|