Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

regulator: gpio: Convert to use descriptors

This converts the GPIO regulator driver to use decriptors only.

We have to let go of the array gpio handling: the fetched descriptors
are handled individually anyway, and the array retrieveal function
does not make it possible to retrieve each GPIO descriptor with
unique flags. Instead get them one by one.

We request the "enable" GPIO separately as before, and make sure
that this line is requested as nonexclusive since enable lines can
be shared and the regulator core expects this.

Most users of the GPIO regulator are using device tree.

There are two boards in the kernel using the gpio regulator from a
non-devicetree path: PXA hx4700 and magician. Make sure to switch
these over to use descriptors as well.

Cc: Philipp Zabel <p.zabel@pengutronix.de> # Magician
Cc: Petr Cvek <petr.cvek@tul.cz> # Magician
Cc: Robert Jarzmik <robert.jarzmik@free.fr> # PXA
Cc: Paul Parsons <lost.distance@yahoo.com> # hx4700
Cc: Kevin Hilman <khilman@baylibre.com> # Meson
Cc: Neil Armstrong <narmstrong@baylibre.com> # Meson
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Linus Walleij and committed by
Mark Brown
d6cd33ad 4f297062

+95 -113
+16 -7
arch/arm/mach-pxa/hx4700.c
··· 19 19 #include <linux/platform_device.h> 20 20 #include <linux/delay.h> 21 21 #include <linux/fb.h> 22 + #include <linux/gpio/machine.h> 22 23 #include <linux/gpio.h> 23 24 #include <linux/gpio_keys.h> 24 25 #include <linux/input.h> ··· 703 702 .consumer_supplies = bq24022_consumers, 704 703 }; 705 704 706 - static struct gpio bq24022_gpios[] = { 707 - { GPIO96_HX4700_BQ24022_ISET2, GPIOF_OUT_INIT_LOW, "bq24022_iset2" }, 708 - }; 705 + static enum gpiod_flags bq24022_gpiod_gflags[] = { GPIOD_OUT_LOW }; 709 706 710 707 static struct gpio_regulator_state bq24022_states[] = { 711 708 { .value = 100000, .gpios = (0 << 0) }, ··· 713 714 static struct gpio_regulator_config bq24022_info = { 714 715 .supply_name = "bq24022", 715 716 716 - .enable_gpio = GPIO72_HX4700_BQ24022_nCHARGE_EN, 717 - .enable_high = 0, 718 717 .enabled_at_boot = 0, 719 718 720 - .gpios = bq24022_gpios, 721 - .nr_gpios = ARRAY_SIZE(bq24022_gpios), 719 + .gflags = bq24022_gpiod_gflags, 720 + .ngpios = ARRAY_SIZE(bq24022_gpiod_gflags), 722 721 723 722 .states = bq24022_states, 724 723 .nr_states = ARRAY_SIZE(bq24022_states), ··· 730 733 .id = -1, 731 734 .dev = { 732 735 .platform_data = &bq24022_info, 736 + }, 737 + }; 738 + 739 + static struct gpiod_lookup_table bq24022_gpiod_table = { 740 + .dev_id = "gpio-regulator", 741 + .table = { 742 + GPIO_LOOKUP("gpio-pxa", GPIO96_HX4700_BQ24022_ISET2, 743 + NULL, GPIO_ACTIVE_HIGH), 744 + GPIO_LOOKUP("gpio-pxa", GPIO72_HX4700_BQ24022_nCHARGE_EN, 745 + "enable", GPIO_ACTIVE_LOW), 746 + { }, 733 747 }, 734 748 }; 735 749 ··· 886 878 pxa_set_btuart_info(NULL); 887 879 pxa_set_stuart_info(NULL); 888 880 881 + gpiod_add_lookup_table(&bq24022_gpiod_table); 889 882 platform_add_devices(devices, ARRAY_SIZE(devices)); 890 883 pwm_add_table(hx4700_pwm_lookup, ARRAY_SIZE(hx4700_pwm_lookup)); 891 884
+16 -7
arch/arm/mach-pxa/magician.c
··· 645 645 .consumer_supplies = bq24022_consumers, 646 646 }; 647 647 648 - static struct gpio bq24022_gpios[] = { 649 - { EGPIO_MAGICIAN_BQ24022_ISET2, GPIOF_OUT_INIT_LOW, "bq24022_iset2" }, 650 - }; 648 + 649 + static enum gpiod_flags bq24022_gpiod_gflags[] = { GPIOD_OUT_LOW }; 651 650 652 651 static struct gpio_regulator_state bq24022_states[] = { 653 652 { .value = 100000, .gpios = (0 << 0) }, ··· 656 657 static struct gpio_regulator_config bq24022_info = { 657 658 .supply_name = "bq24022", 658 659 659 - .enable_gpio = GPIO30_MAGICIAN_BQ24022_nCHARGE_EN, 660 - .enable_high = 0, 661 660 .enabled_at_boot = 1, 662 661 663 - .gpios = bq24022_gpios, 664 - .nr_gpios = ARRAY_SIZE(bq24022_gpios), 662 + .gflags = bq24022_gpiod_gflags, 663 + .ngpios = ARRAY_SIZE(bq24022_gpiod_gflags), 665 664 666 665 .states = bq24022_states, 667 666 .nr_states = ARRAY_SIZE(bq24022_states), ··· 673 676 .id = -1, 674 677 .dev = { 675 678 .platform_data = &bq24022_info, 679 + }, 680 + }; 681 + 682 + static struct gpiod_lookup_table bq24022_gpiod_table = { 683 + .dev_id = "gpio-regulator", 684 + .table = { 685 + GPIO_LOOKUP("gpio-pxa", EGPIO_MAGICIAN_BQ24022_ISET2, 686 + NULL, GPIO_ACTIVE_HIGH), 687 + GPIO_LOOKUP("gpio-pxa", GPIO30_MAGICIAN_BQ24022_nCHARGE_EN, 688 + "enable", GPIO_ACTIVE_LOW), 689 + { }, 676 690 }, 677 691 }; 678 692 ··· 1035 1027 regulator_register_always_on(0, "power", pwm_backlight_supply, 1036 1028 ARRAY_SIZE(pwm_backlight_supply), 5000000); 1037 1029 1030 + gpiod_add_lookup_table(&bq24022_gpiod_table); 1038 1031 platform_add_devices(ARRAY_AND_SIZE(devices)); 1039 1032 } 1040 1033
+56 -94
drivers/regulator/gpio-regulator.c
··· 30 30 #include <linux/regulator/machine.h> 31 31 #include <linux/regulator/of_regulator.h> 32 32 #include <linux/regulator/gpio-regulator.h> 33 - #include <linux/gpio.h> 33 + #include <linux/gpio/consumer.h> 34 34 #include <linux/slab.h> 35 35 #include <linux/of.h> 36 - #include <linux/of_gpio.h> 37 36 38 37 struct gpio_regulator_data { 39 38 struct regulator_desc desc; 40 39 struct regulator_dev *dev; 41 40 42 - struct gpio *gpios; 41 + struct gpio_desc **gpiods; 43 42 int nr_gpios; 44 43 45 44 struct gpio_regulator_state *states; ··· 81 82 82 83 for (ptr = 0; ptr < data->nr_gpios; ptr++) { 83 84 state = (target & (1 << ptr)) >> ptr; 84 - gpio_set_value_cansleep(data->gpios[ptr].gpio, state); 85 + gpiod_set_value_cansleep(data->gpiods[ptr], state); 85 86 } 86 87 data->state = target; 87 88 ··· 118 119 119 120 for (ptr = 0; ptr < data->nr_gpios; ptr++) { 120 121 state = (target & (1 << ptr)) >> ptr; 121 - gpio_set_value_cansleep(data->gpios[ptr].gpio, state); 122 + gpiod_set_value_cansleep(data->gpiods[ptr], state); 122 123 } 123 124 data->state = target; 124 125 ··· 137 138 { 138 139 struct gpio_regulator_config *config; 139 140 const char *regtype; 140 - int proplen, gpio, i; 141 + int proplen, i; 142 + int ngpios; 141 143 int ret; 142 144 143 145 config = devm_kzalloc(dev, ··· 153 153 154 154 config->supply_name = config->init_data->constraints.name; 155 155 156 - if (of_property_read_bool(np, "enable-active-high")) 157 - config->enable_high = true; 158 - 159 156 if (of_property_read_bool(np, "enable-at-boot")) 160 157 config->enabled_at_boot = true; 161 158 162 159 of_property_read_u32(np, "startup-delay-us", &config->startup_delay); 163 160 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) 161 + /* Fetch GPIO init levels */ 162 + ngpios = gpiod_count(dev, NULL); 163 + if (ngpios > 0) { 164 + config->gflags = devm_kzalloc(dev, 165 + sizeof(enum gpiod_flags) 166 + * ngpios, 167 + GFP_KERNEL); 168 + if (!config->gflags) 179 169 return ERR_PTR(-ENOMEM); 180 170 181 - proplen = of_property_count_u32_elems(np, "gpios-states"); 182 - /* optional property */ 183 - if (proplen < 0) 184 - proplen = 0; 171 + for (i = 0; i < ngpios; i++) { 172 + u32 val; 185 173 186 - if (proplen > 0 && proplen != config->nr_gpios) { 187 - dev_warn(dev, "gpios <-> gpios-states mismatch\n"); 188 - proplen = 0; 189 - } 174 + ret = of_property_read_u32_index(np, "gpios-states", i, 175 + &val); 190 176 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 - } 177 + /* Default to high per specification */ 178 + if (ret) 179 + config->gflags[i] = GPIOD_OUT_HIGH; 180 + else 181 + config->gflags[i] = 182 + val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 207 183 } 208 184 } 185 + config->ngpios = ngpios; 209 186 210 187 /* Fetch states. */ 211 188 proplen = of_property_count_u32_elems(np, "states"); ··· 232 255 struct device_node *np = pdev->dev.of_node; 233 256 struct gpio_regulator_data *drvdata; 234 257 struct regulator_config cfg = { }; 235 - int ptr, ret, state; 258 + enum gpiod_flags gflags; 259 + int ptr, ret, state, i; 236 260 237 261 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data), 238 262 GFP_KERNEL); ··· 253 275 return -ENOMEM; 254 276 } 255 277 256 - if (config->nr_gpios != 0) { 257 - drvdata->gpios = kmemdup(config->gpios, 258 - config->nr_gpios * sizeof(struct gpio), 259 - GFP_KERNEL); 260 - if (drvdata->gpios == NULL) { 261 - dev_err(&pdev->dev, "Failed to allocate gpio data\n"); 262 - ret = -ENOMEM; 263 - goto err_name; 264 - } 265 - 266 - drvdata->nr_gpios = config->nr_gpios; 267 - ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios); 268 - if (ret) { 269 - if (ret != -EPROBE_DEFER) 270 - dev_err(&pdev->dev, 271 - "Could not obtain regulator setting GPIOs: %d\n", 272 - ret); 273 - goto err_memgpio; 274 - } 278 + drvdata->gpiods = devm_kzalloc(&pdev->dev, sizeof(struct gpio_desc *), 279 + GFP_KERNEL); 280 + if (!drvdata->gpiods) 281 + return -ENOMEM; 282 + for (i = 0; i < config->ngpios; i++) { 283 + drvdata->gpiods[i] = devm_gpiod_get_index(&pdev->dev, 284 + NULL, 285 + i, 286 + config->gflags[i]); 287 + if (IS_ERR(drvdata->gpiods[i])) 288 + return PTR_ERR(drvdata->gpiods[i]); 289 + /* This is good to know */ 290 + gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name); 275 291 } 292 + drvdata->nr_gpios = config->ngpios; 276 293 277 294 drvdata->states = kmemdup(config->states, 278 295 config->nr_states * ··· 276 303 if (drvdata->states == NULL) { 277 304 dev_err(&pdev->dev, "Failed to allocate state data\n"); 278 305 ret = -ENOMEM; 279 - goto err_stategpio; 306 + goto err_name; 280 307 } 281 308 drvdata->nr_states = config->nr_states; 282 309 ··· 303 330 /* build initial state from gpio init data. */ 304 331 state = 0; 305 332 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) { 306 - if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH) 333 + if (config->gflags[ptr] == GPIOD_OUT_HIGH) 307 334 state |= (1 << ptr); 308 335 } 309 336 drvdata->state = state; ··· 313 340 cfg.driver_data = drvdata; 314 341 cfg.of_node = np; 315 342 316 - if (gpio_is_valid(config->enable_gpio)) { 317 - cfg.ena_gpio = config->enable_gpio; 318 - cfg.ena_gpio_initialized = true; 319 - } 320 - cfg.ena_gpio_invert = !config->enable_high; 321 - if (config->enabled_at_boot) { 322 - if (config->enable_high) 323 - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; 324 - else 325 - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; 326 - } else { 327 - if (config->enable_high) 328 - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; 329 - else 330 - cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; 343 + /* 344 + * The signal will be inverted by the GPIO core if flagged so in the 345 + * decriptor. 346 + */ 347 + if (config->enabled_at_boot) 348 + gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE; 349 + else 350 + gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE; 351 + 352 + cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, "enable", gflags); 353 + if (IS_ERR(cfg.ena_gpiod)) { 354 + ret = PTR_ERR(cfg.ena_gpiod); 355 + goto err_memstate; 331 356 } 332 357 333 358 drvdata->dev = regulator_register(&drvdata->desc, &cfg); ··· 341 370 342 371 err_memstate: 343 372 kfree(drvdata->states); 344 - err_stategpio: 345 - gpio_free_array(drvdata->gpios, drvdata->nr_gpios); 346 - err_memgpio: 347 - kfree(drvdata->gpios); 348 373 err_name: 349 374 kfree(drvdata->desc.name); 350 375 return ret; ··· 351 384 struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev); 352 385 353 386 regulator_unregister(drvdata->dev); 354 - 355 - gpio_free_array(drvdata->gpios, drvdata->nr_gpios); 356 - 357 387 kfree(drvdata->states); 358 - kfree(drvdata->gpios); 359 - 360 388 kfree(drvdata->desc.name); 361 389 362 390 return 0;
+7 -5
include/linux/regulator/gpio-regulator.h
··· 21 21 #ifndef __REGULATOR_GPIO_H 22 22 #define __REGULATOR_GPIO_H 23 23 24 + #include <linux/gpio/consumer.h> 25 + 24 26 struct regulator_init_data; 25 27 26 28 enum regulator_type; ··· 55 53 * This is used to keep the regulator at 56 54 * the default state 57 55 * @startup_delay: Start-up time in microseconds 58 - * @gpios: Array containing the gpios needed to control 59 - * the setting of the regulator 60 - * @nr_gpios: Number of gpios 56 + * @gflags: Array of GPIO configuration flags for initial 57 + * states 58 + * @ngpios: Number of GPIOs and configurations available 61 59 * @states: Array of gpio_regulator_state entries describing 62 60 * the gpio state for specific voltages 63 61 * @nr_states: Number of states available ··· 76 74 unsigned enabled_at_boot:1; 77 75 unsigned startup_delay; 78 76 79 - struct gpio *gpios; 80 - int nr_gpios; 77 + enum gpiod_flags *gflags; 78 + int ngpios; 81 79 82 80 struct gpio_regulator_state *states; 83 81 int nr_states;