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

regulator: max8998: Convert to GPIO descriptors

This rewrites the max8998 regulator driver to fetch the dvs
regulators as descriptors. This will likely mostly come from
the device tree since there are no in-tree users of the platform
data, but supplying GPIO descriptor tables from board files is
also possible if needed.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://msgid.link/r/20240220-descriptors-regulators-v1-5-097f608694be@linaro.org
Acked-by: Lee Jones <lee@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Linus Walleij and committed by
Mark Brown
f25828a1 84618d5e

+51 -99
+51 -93
drivers/regulator/max8998.c
··· 10 10 #include <linux/init.h> 11 11 #include <linux/i2c.h> 12 12 #include <linux/err.h> 13 - #include <linux/gpio.h> 13 + #include <linux/bits.h> 14 + #include <linux/gpio/consumer.h> 14 15 #include <linux/slab.h> 15 16 #include <linux/interrupt.h> 16 17 #include <linux/mutex.h> 17 18 #include <linux/of.h> 18 - #include <linux/of_gpio.h> 19 19 #include <linux/platform_device.h> 20 20 #include <linux/regulator/driver.h> 21 21 #include <linux/regulator/of_regulator.h> ··· 31 31 unsigned int buck1_idx; /* index to last changed voltage */ 32 32 /* value in a set */ 33 33 unsigned int buck2_idx; 34 + struct gpio_desc *buck1_gpio1; 35 + struct gpio_desc *buck1_gpio2; 36 + struct gpio_desc *buck2_gpio; 34 37 }; 35 38 36 39 static const unsigned int charger_current_table[] = { ··· 230 227 return ret; 231 228 } 232 229 233 - static inline void buck1_gpio_set(int gpio1, int gpio2, int v) 230 + static inline void buck1_gpio_set(struct gpio_desc *gpio1, struct gpio_desc *gpio2, int v) 234 231 { 235 - gpio_set_value(gpio1, v & 0x1); 236 - gpio_set_value(gpio2, (v >> 1) & 0x1); 232 + gpiod_set_value(gpio1, v & 0x1); 233 + gpiod_set_value(gpio2, (v >> 1) & 0x1); 237 234 } 238 235 239 - static inline void buck2_gpio_set(int gpio, int v) 236 + static inline void buck2_gpio_set(struct gpio_desc *gpio, int v) 240 237 { 241 - gpio_set_value(gpio, v & 0x1); 238 + gpiod_set_value(gpio, v & 0x1); 242 239 } 243 240 244 241 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev, ··· 263 260 selector, max8998->buck1_vol[0], max8998->buck1_vol[1], 264 261 max8998->buck1_vol[2], max8998->buck1_vol[3]); 265 262 266 - if (gpio_is_valid(pdata->buck1_set1) && 267 - gpio_is_valid(pdata->buck1_set2)) { 263 + if (max8998->buck1_gpio1 && max8998->buck1_gpio2) { 268 264 269 265 /* check if requested voltage */ 270 266 /* value is already defined */ 271 267 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) { 272 268 if (max8998->buck1_vol[j] == selector) { 273 269 max8998->buck1_idx = j; 274 - buck1_gpio_set(pdata->buck1_set1, 275 - pdata->buck1_set2, j); 270 + buck1_gpio_set(max8998->buck1_gpio1, 271 + max8998->buck1_gpio2, j); 276 272 goto buck1_exit; 277 273 } 278 274 } ··· 288 286 &shift, 289 287 &mask); 290 288 ret = max8998_write_reg(i2c, reg, selector); 291 - buck1_gpio_set(pdata->buck1_set1, 292 - pdata->buck1_set2, max8998->buck1_idx); 289 + buck1_gpio_set(max8998->buck1_gpio1, 290 + max8998->buck1_gpio2, max8998->buck1_idx); 293 291 buck1_last_val++; 294 292 buck1_exit: 295 293 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n", 296 - i2c->name, gpio_get_value(pdata->buck1_set1), 297 - gpio_get_value(pdata->buck1_set2)); 294 + i2c->name, gpiod_get_value(max8998->buck1_gpio1), 295 + gpiod_get_value(max8998->buck1_gpio2)); 298 296 break; 299 297 } else { 300 298 ret = max8998_write_reg(i2c, reg, selector); ··· 305 303 dev_dbg(max8998->dev, 306 304 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n", 307 305 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]); 308 - if (gpio_is_valid(pdata->buck2_set3)) { 309 - 306 + if (max8998->buck2_gpio) { 310 307 /* check if requested voltage */ 311 308 /* value is already defined */ 312 309 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) { 313 310 if (max8998->buck2_vol[j] == selector) { 314 311 max8998->buck2_idx = j; 315 - buck2_gpio_set(pdata->buck2_set3, j); 312 + buck2_gpio_set(max8998->buck2_gpio, j); 316 313 goto buck2_exit; 317 314 } 318 315 } ··· 323 322 &reg, &shift, &mask); 324 323 ret = max8998_write_reg(i2c, reg, selector); 325 324 max8998->buck2_vol[max8998->buck2_idx] = selector; 326 - buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx); 325 + buck2_gpio_set(max8998->buck2_gpio, max8998->buck2_idx); 327 326 buck2_exit: 328 327 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name, 329 - gpio_get_value(pdata->buck2_set3)); 328 + gpiod_get_value(max8998->buck2_gpio)); 330 329 } else { 331 330 ret = max8998_write_reg(i2c, reg, selector); 332 331 } ··· 540 539 charger_current_table, MAX8998_REG_CHGR1, 0x7), 541 540 }; 542 541 543 - static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev, 544 - struct max8998_platform_data *pdata, 545 - struct device_node *pmic_np) 546 - { 547 - int gpio; 548 - 549 - gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0); 550 - if (!gpio_is_valid(gpio)) { 551 - dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio); 552 - return -EINVAL; 553 - } 554 - pdata->buck1_set1 = gpio; 555 - 556 - gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1); 557 - if (!gpio_is_valid(gpio)) { 558 - dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio); 559 - return -EINVAL; 560 - } 561 - pdata->buck1_set2 = gpio; 562 - 563 - gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0); 564 - if (!gpio_is_valid(gpio)) { 565 - dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio); 566 - return -EINVAL; 567 - } 568 - pdata->buck2_set3 = gpio; 569 - 570 - return 0; 571 - } 572 - 573 542 static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev, 574 543 struct max8998_platform_data *pdata) 575 544 { ··· 584 613 585 614 of_node_put(reg_np); 586 615 of_node_put(regulators_np); 587 - 588 - ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np); 589 - if (ret) 590 - return -EINVAL; 591 616 592 617 pdata->buck_voltage_lock = of_property_read_bool(pmic_np, "max8998,pmic-buck-voltage-lock"); 593 618 ··· 632 665 struct regulator_dev *rdev; 633 666 struct max8998_data *max8998; 634 667 struct i2c_client *i2c; 668 + enum gpiod_flags flags; 635 669 int i, ret; 636 670 unsigned int v; 637 671 ··· 661 693 max8998->buck1_idx = pdata->buck1_default_idx; 662 694 max8998->buck2_idx = pdata->buck2_default_idx; 663 695 664 - /* NOTE: */ 665 - /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */ 666 - /* will be displayed */ 667 - 668 696 /* Check if MAX8998 voltage selection GPIOs are defined */ 669 - if (gpio_is_valid(pdata->buck1_set1) && 670 - gpio_is_valid(pdata->buck1_set2)) { 671 - /* Check if SET1 is not equal to 0 */ 672 - if (!pdata->buck1_set1) { 673 - dev_err(&pdev->dev, 674 - "MAX8998 SET1 GPIO defined as 0 !\n"); 675 - WARN_ON(!pdata->buck1_set1); 676 - return -EIO; 677 - } 678 - /* Check if SET2 is not equal to 0 */ 679 - if (!pdata->buck1_set2) { 680 - dev_err(&pdev->dev, 681 - "MAX8998 SET2 GPIO defined as 0 !\n"); 682 - WARN_ON(!pdata->buck1_set2); 683 - return -EIO; 684 - } 697 + flags = (max8998->buck1_idx & BIT(0)) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 698 + max8998->buck1_gpio1 = devm_gpiod_get_index_optional(iodev->dev, 699 + "max8998,pmic-buck1-dvs", 700 + 0, 701 + flags); 702 + if (IS_ERR(max8998->buck1_gpio1)) 703 + return dev_err_probe(&pdev->dev, PTR_ERR(max8998->buck1_gpio1), 704 + "could not get BUCK1 GPIO1\n"); 705 + gpiod_set_consumer_name(max8998->buck1_gpio1, "MAX8998 BUCK1_SET1"); 685 706 686 - gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1"); 687 - gpio_direction_output(pdata->buck1_set1, 688 - max8998->buck1_idx & 0x1); 707 + flags = (max8998->buck1_idx & BIT(1)) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 708 + max8998->buck1_gpio2 = devm_gpiod_get_index_optional(iodev->dev, 709 + "max8998,pmic-buck1-dvs", 710 + 1, 711 + flags); 712 + if (IS_ERR(max8998->buck1_gpio2)) 713 + return dev_err_probe(&pdev->dev, PTR_ERR(max8998->buck1_gpio2), 714 + "could not get BUCK1 GPIO2\n"); 715 + gpiod_set_consumer_name(max8998->buck1_gpio1, "MAX8998 BUCK1_SET2"); 689 716 717 + flags = (max8998->buck2_idx & BIT(0)) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 718 + max8998->buck2_gpio = devm_gpiod_get_index_optional(iodev->dev, 719 + "max8998,pmic-buck2-dvs", 720 + 0, 721 + flags); 722 + if (IS_ERR(max8998->buck2_gpio)) 723 + return dev_err_probe(&pdev->dev, PTR_ERR(max8998->buck2_gpio), 724 + "could not get BUCK2 GPIO\n"); 725 + gpiod_set_consumer_name(max8998->buck1_gpio1, "MAX8998 BUCK2_SET3"); 690 726 691 - gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2"); 692 - gpio_direction_output(pdata->buck1_set2, 693 - (max8998->buck1_idx >> 1) & 0x1); 694 - 727 + if (max8998->buck1_gpio1 && max8998->buck1_gpio2) { 695 728 /* Set predefined values for BUCK1 registers */ 696 729 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) { 697 730 int index = MAX8998_BUCK1 - MAX8998_LDO2; ··· 711 742 } 712 743 } 713 744 714 - if (gpio_is_valid(pdata->buck2_set3)) { 715 - /* Check if SET3 is not equal to 0 */ 716 - if (!pdata->buck2_set3) { 717 - dev_err(&pdev->dev, 718 - "MAX8998 SET3 GPIO defined as 0 !\n"); 719 - WARN_ON(!pdata->buck2_set3); 720 - return -EIO; 721 - } 722 - gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3"); 723 - gpio_direction_output(pdata->buck2_set3, 724 - max8998->buck2_idx & 0x1); 725 - 745 + if (max8998->buck2_gpio) { 726 746 /* Set predefined values for BUCK2 registers */ 727 747 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) { 728 748 int index = MAX8998_BUCK2 - MAX8998_LDO2;
-6
include/linux/mfd/max8998.h
··· 65 65 * be other than the preset values. 66 66 * @buck1_voltage: BUCK1 DVS mode 1 voltage registers 67 67 * @buck2_voltage: BUCK2 DVS mode 2 voltage registers 68 - * @buck1_set1: BUCK1 gpio pin 1 to set output voltage 69 - * @buck1_set2: BUCK1 gpio pin 2 to set output voltage 70 68 * @buck1_default_idx: Default for BUCK1 gpio pin 1, 2 71 - * @buck2_set3: BUCK2 gpio pin to set output voltage 72 69 * @buck2_default_idx: Default for BUCK2 gpio pin. 73 70 * @wakeup: Allow to wake up from suspend 74 71 * @rtc_delay: LP3974 RTC chip bug that requires delay after a register ··· 88 91 bool buck_voltage_lock; 89 92 int buck1_voltage[4]; 90 93 int buck2_voltage[2]; 91 - int buck1_set1; 92 - int buck1_set2; 93 94 int buck1_default_idx; 94 - int buck2_set3; 95 95 int buck2_default_idx; 96 96 bool wakeup; 97 97 bool rtc_delay;