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

pwm-backlight: switch to gpiod interface

Switch to the new gpiod interface, which allows to handle GPIO
properties such as active low transparently and removes a whole bunch of
code.

There are still a couple of users of this driver that rely on passing
the enable GPIO number through platform data, so a fallback mechanism
using a GPIO number is still available to avoid breaking them. It will
be removed once current users have switched to the GPIO lookup tables
provided by the gpiod interface.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>

authored by

Alexandre Courbot and committed by
Thierry Reding
257462db a4406f16

+30 -44
+29 -40
drivers/video/backlight/pwm_bl.c
··· 10 10 * published by the Free Software Foundation. 11 11 */ 12 12 13 + #include <linux/gpio/consumer.h> 13 14 #include <linux/gpio.h> 14 - #include <linux/of_gpio.h> 15 15 #include <linux/module.h> 16 16 #include <linux/kernel.h> 17 17 #include <linux/init.h> ··· 32 32 unsigned int *levels; 33 33 bool enabled; 34 34 struct regulator *power_supply; 35 - int enable_gpio; 36 - unsigned long enable_gpio_flags; 35 + struct gpio_desc *enable_gpio; 37 36 unsigned int scale; 38 37 int (*notify)(struct device *, 39 38 int brightness); ··· 53 54 if (err < 0) 54 55 dev_err(pb->dev, "failed to enable power supply\n"); 55 56 56 - if (gpio_is_valid(pb->enable_gpio)) { 57 - if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW) 58 - gpio_set_value(pb->enable_gpio, 0); 59 - else 60 - gpio_set_value(pb->enable_gpio, 1); 61 - } 57 + if (pb->enable_gpio) 58 + gpiod_set_value(pb->enable_gpio, 1); 62 59 63 60 pwm_enable(pb->pwm); 64 61 pb->enabled = true; ··· 68 73 pwm_config(pb->pwm, 0, pb->period); 69 74 pwm_disable(pb->pwm); 70 75 71 - if (gpio_is_valid(pb->enable_gpio)) { 72 - if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW) 73 - gpio_set_value(pb->enable_gpio, 1); 74 - else 75 - gpio_set_value(pb->enable_gpio, 0); 76 - } 76 + if (pb->enable_gpio) 77 + gpiod_set_value(pb->enable_gpio, 0); 77 78 78 79 regulator_disable(pb->power_supply); 79 80 pb->enabled = false; ··· 139 148 struct platform_pwm_backlight_data *data) 140 149 { 141 150 struct device_node *node = dev->of_node; 142 - enum of_gpio_flags flags; 143 151 struct property *prop; 144 152 int length; 145 153 u32 value; ··· 178 188 data->dft_brightness = value; 179 189 data->max_brightness--; 180 190 } 181 - 182 - data->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0, 183 - &flags); 184 - if (data->enable_gpio == -EPROBE_DEFER) 185 - return -EPROBE_DEFER; 186 - 187 - if (gpio_is_valid(data->enable_gpio) && (flags & OF_GPIO_ACTIVE_LOW)) 188 - data->enable_gpio_flags |= PWM_BACKLIGHT_GPIO_ACTIVE_LOW; 189 191 190 192 return 0; 191 193 } ··· 238 256 } else 239 257 pb->scale = data->max_brightness; 240 258 241 - pb->enable_gpio = data->enable_gpio; 242 - pb->enable_gpio_flags = data->enable_gpio_flags; 243 259 pb->notify = data->notify; 244 260 pb->notify_after = data->notify_after; 245 261 pb->check_fb = data->check_fb; ··· 245 265 pb->dev = &pdev->dev; 246 266 pb->enabled = false; 247 267 248 - if (gpio_is_valid(pb->enable_gpio)) { 249 - unsigned long flags; 250 - 251 - if (pb->enable_gpio_flags & PWM_BACKLIGHT_GPIO_ACTIVE_LOW) 252 - flags = GPIOF_OUT_INIT_HIGH; 268 + pb->enable_gpio = devm_gpiod_get(&pdev->dev, "enable"); 269 + if (IS_ERR(pb->enable_gpio)) { 270 + ret = PTR_ERR(pb->enable_gpio); 271 + if (ret == -ENOENT) 272 + pb->enable_gpio = NULL; 253 273 else 254 - flags = GPIOF_OUT_INIT_LOW; 274 + goto err_alloc; 275 + } 255 276 256 - ret = gpio_request_one(pb->enable_gpio, flags, "enable"); 277 + /* 278 + * Compatibility fallback for drivers still using the integer GPIO 279 + * platform data. Must go away soon. 280 + */ 281 + if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) { 282 + ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio, 283 + GPIOF_OUT_INIT_HIGH, "enable"); 257 284 if (ret < 0) { 258 285 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n", 259 - pb->enable_gpio, ret); 286 + data->enable_gpio, ret); 260 287 goto err_alloc; 261 288 } 289 + 290 + pb->enable_gpio = gpio_to_desc(data->enable_gpio); 262 291 } 292 + 293 + if (pb->enable_gpio) 294 + gpiod_direction_output(pb->enable_gpio, 1); 263 295 264 296 pb->power_supply = devm_regulator_get(&pdev->dev, "power"); 265 297 if (IS_ERR(pb->power_supply)) { 266 298 ret = PTR_ERR(pb->power_supply); 267 - goto err_gpio; 299 + goto err_alloc; 268 300 } 269 301 270 302 pb->pwm = devm_pwm_get(&pdev->dev, NULL); ··· 287 295 if (IS_ERR(pb->pwm)) { 288 296 dev_err(&pdev->dev, "unable to request legacy PWM\n"); 289 297 ret = PTR_ERR(pb->pwm); 290 - goto err_gpio; 298 + goto err_alloc; 291 299 } 292 300 } 293 301 ··· 312 320 if (IS_ERR(bl)) { 313 321 dev_err(&pdev->dev, "failed to register backlight\n"); 314 322 ret = PTR_ERR(bl); 315 - goto err_gpio; 323 + goto err_alloc; 316 324 } 317 325 318 326 if (data->dft_brightness > data->max_brightness) { ··· 328 336 platform_set_drvdata(pdev, bl); 329 337 return 0; 330 338 331 - err_gpio: 332 - if (gpio_is_valid(pb->enable_gpio)) 333 - gpio_free(pb->enable_gpio); 334 339 err_alloc: 335 340 if (data->exit) 336 341 data->exit(&pdev->dev);
+1 -4
include/linux/pwm_backlight.h
··· 6 6 7 7 #include <linux/backlight.h> 8 8 9 - /* TODO: convert to gpiod_*() API once it has been merged */ 10 - #define PWM_BACKLIGHT_GPIO_ACTIVE_LOW (1 << 0) 11 - 12 9 struct platform_pwm_backlight_data { 13 10 int pwm_id; 14 11 unsigned int max_brightness; ··· 13 16 unsigned int lth_brightness; 14 17 unsigned int pwm_period_ns; 15 18 unsigned int *levels; 19 + /* TODO remove once all users are switched to gpiod_* API */ 16 20 int enable_gpio; 17 - unsigned long enable_gpio_flags; 18 21 int (*init)(struct device *dev); 19 22 int (*notify)(struct device *dev, int brightness); 20 23 void (*notify_after)(struct device *dev, int brightness);