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

leds: pca963x: register LEDs immediately after parsing, get rid of platdata

Register LEDs immediately after parsing their properties.
This allows us to get rid of platdata, and since no one in tree uses
header linux/platform_data/leds-pca963x.h, remove it.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
Cc: Peter Meerwald <p.meerwald@bct-electronic.com>
Cc: Ricardo Ribalda <ribalda@kernel.org>
Cc: Zahari Petkov <zahari@balena.io>
Signed-off-by: Pavel Machek <pavel@ucw.cz>

authored by

Marek Behún and committed by
Pavel Machek
85fc8efe 7e2dc43d

+75 -156
+75 -121
drivers/leds/leds-pca963x.c
··· 32 32 #include <linux/property.h> 33 33 #include <linux/slab.h> 34 34 #include <linux/of.h> 35 - #include <linux/platform_data/leds-pca963x.h> 36 35 37 36 /* LED select registers determine the source that drives LED outputs */ 38 37 #define PCA963X_LED_OFF 0x0 /* LED driver off */ ··· 101 102 struct pca963x *chip; 102 103 struct led_classdev led_cdev; 103 104 int led_num; /* 0 .. 15 potentially */ 104 - char name[32]; 105 105 u8 gdc; 106 106 u8 gfrq; 107 107 }; ··· 282 284 return 0; 283 285 } 284 286 285 - static struct pca963x_platform_data * 286 - pca963x_get_pdata(struct device *dev, struct pca963x_chipdef *chipdef) 287 + static int pca963x_register_leds(struct i2c_client *client, 288 + struct pca963x *chip) 287 289 { 288 - struct pca963x_platform_data *pdata; 289 - struct led_info *pca963x_leds; 290 + struct pca963x_chipdef *chipdef = chip->chipdef; 291 + struct pca963x_led *led = chip->leds; 292 + struct device *dev = &client->dev; 290 293 struct fwnode_handle *child; 291 - int count; 292 - 293 - count = device_get_child_node_count(dev); 294 - if (!count || count > chipdef->n_leds) 295 - return ERR_PTR(-ENODEV); 296 - 297 - pca963x_leds = devm_kcalloc(dev, chipdef->n_leds, 298 - sizeof(struct led_info), GFP_KERNEL); 299 - if (!pca963x_leds) 300 - return ERR_PTR(-ENOMEM); 301 - 302 - device_for_each_child_node(dev, child) { 303 - struct led_info led = {}; 304 - u32 reg; 305 - int res; 306 - 307 - res = fwnode_property_read_u32(child, "reg", &reg); 308 - if ((res != 0) || (reg >= chipdef->n_leds)) 309 - continue; 310 - 311 - res = fwnode_property_read_string(child, "label", &led.name); 312 - if ((res != 0) && is_of_node(child)) 313 - led.name = to_of_node(child)->name; 314 - 315 - fwnode_property_read_string(child, "linux,default-trigger", 316 - &led.default_trigger); 317 - 318 - pca963x_leds[reg] = led; 319 - } 320 - pdata = devm_kzalloc(dev, sizeof(struct pca963x_platform_data), 321 - GFP_KERNEL); 322 - if (!pdata) 323 - return ERR_PTR(-ENOMEM); 324 - 325 - pdata->leds.leds = pca963x_leds; 326 - pdata->leds.num_leds = chipdef->n_leds; 327 - 328 - /* default to open-drain unless totem pole (push-pull) is specified */ 329 - if (device_property_read_bool(dev, "nxp,totem-pole")) 330 - pdata->outdrv = PCA963X_TOTEM_POLE; 331 - else 332 - pdata->outdrv = PCA963X_OPEN_DRAIN; 333 - 334 - /* default to software blinking unless hardware blinking is specified */ 335 - if (device_property_read_bool(dev, "nxp,hw-blink")) 336 - pdata->blink_type = PCA963X_HW_BLINK; 337 - else 338 - pdata->blink_type = PCA963X_SW_BLINK; 294 + const char *name; 295 + char label[64]; 296 + bool hw_blink; 297 + s32 mode2; 298 + u32 reg; 299 + int ret; 339 300 340 301 if (device_property_read_u32(dev, "nxp,period-scale", 341 302 &chipdef->scaling)) 342 303 chipdef->scaling = 1000; 343 304 305 + hw_blink = device_property_read_bool(dev, "nxp,hw-blink"); 306 + 307 + mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2); 308 + if (mode2 < 0) 309 + return mode2; 310 + 311 + /* default to open-drain unless totem pole (push-pull) is specified */ 312 + if (device_property_read_bool(dev, "nxp,totem-pole")) 313 + mode2 |= PCA963X_MODE2_OUTDRV; 314 + else 315 + mode2 &= ~PCA963X_MODE2_OUTDRV; 316 + 344 317 /* default to non-inverted output, unless inverted is specified */ 345 318 if (device_property_read_bool(dev, "nxp,inverted-out")) 346 - pdata->dir = PCA963X_INVERTED; 319 + mode2 |= PCA963X_MODE2_INVRT; 347 320 else 348 - pdata->dir = PCA963X_NORMAL; 321 + mode2 &= ~PCA963X_MODE2_INVRT; 349 322 350 - return pdata; 323 + ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2); 324 + if (ret < 0) 325 + return ret; 326 + 327 + device_for_each_child_node(dev, child) { 328 + ret = fwnode_property_read_u32(child, "reg", &reg); 329 + if (ret || reg >= chipdef->n_leds) { 330 + dev_err(dev, "Invalid 'reg' property for node %pfw\n", 331 + child); 332 + ret = -EINVAL; 333 + goto err; 334 + } 335 + 336 + ret = fwnode_property_read_string(child, "label", &name); 337 + if (!fwnode_property_read_string(child, "label", &name)) 338 + snprintf(label, sizeof(label), "pca963x:%s", name); 339 + else 340 + snprintf(label, sizeof(label), "pca963x::"); 341 + 342 + fwnode_property_read_string(child, "linux,default-trigger", 343 + &led->led_cdev.default_trigger); 344 + 345 + led->led_num = reg; 346 + led->chip = chip; 347 + led->led_cdev.name = label; 348 + led->led_cdev.brightness_set_blocking = pca963x_led_set; 349 + if (hw_blink) 350 + led->led_cdev.blink_set = pca963x_blink_set; 351 + 352 + ret = devm_led_classdev_register(dev, &led->led_cdev); 353 + if (ret) { 354 + dev_err(dev, "Failed to register LED for node %pfw\n", 355 + child); 356 + goto err; 357 + } 358 + 359 + ++led; 360 + } 361 + 362 + return 0; 363 + err: 364 + fwnode_handle_put(child); 365 + return ret; 351 366 } 352 367 353 368 static const struct of_device_id of_pca963x_match[] = { ··· 377 366 { 378 367 struct device *dev = &client->dev; 379 368 struct pca963x_chipdef *chipdef; 380 - struct pca963x_platform_data *pdata; 381 369 struct pca963x *chip; 382 - int i, err; 370 + int i, count; 383 371 384 372 chipdef = &pca963x_chipdefs[id->driver_data]; 385 - pdata = dev_get_platdata(dev); 386 373 387 - if (!pdata) { 388 - pdata = pca963x_get_pdata(dev, chipdef); 389 - if (IS_ERR(pdata)) { 390 - dev_warn(dev, "could not parse configuration\n"); 391 - pdata = NULL; 392 - } 393 - } 394 - 395 - if (pdata && (pdata->leds.num_leds < 1 || 396 - pdata->leds.num_leds > chipdef->n_leds)) { 397 - dev_err(dev, "board info must claim 1-%d LEDs", 398 - chipdef->n_leds); 374 + count = device_get_child_node_count(dev); 375 + if (!count || count > chipdef->n_leds) { 376 + dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n", 377 + dev_fwnode(dev), chipdef->n_leds); 399 378 return -EINVAL; 400 379 } 401 380 402 - chip = devm_kzalloc(dev, struct_size(chip, leds, chipdef->n_leds), 403 - GFP_KERNEL); 381 + chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL); 404 382 if (!chip) 405 383 return -ENOMEM; 406 384 ··· 403 403 for (i = 0; i < chipdef->n_leds / 4; i++) 404 404 i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00); 405 405 406 - for (i = 0; i < chipdef->n_leds; i++) { 407 - struct pca963x_led *led = &chip->leds[i]; 408 - 409 - led->led_num = i; 410 - led->chip = chip; 411 - 412 - /* Platform data can specify LED names and default triggers */ 413 - if (pdata && i < pdata->leds.num_leds) { 414 - if (pdata->leds.leds[i].name) 415 - snprintf(led->name, 416 - sizeof(led->name), "pca963x:%s", 417 - pdata->leds.leds[i].name); 418 - if (pdata->leds.leds[i].default_trigger) 419 - led->led_cdev.default_trigger = 420 - pdata->leds.leds[i].default_trigger; 421 - } 422 - if (!pdata || i >= pdata->leds.num_leds || 423 - !pdata->leds.leds[i].name) 424 - snprintf(led->name, sizeof(led->name), 425 - "pca963x:%d:%.2x:%d", client->adapter->nr, 426 - client->addr, i); 427 - 428 - led->led_cdev.name = led->name; 429 - led->led_cdev.brightness_set_blocking = pca963x_led_set; 430 - 431 - if (pdata && pdata->blink_type == PCA963X_HW_BLINK) 432 - led->led_cdev.blink_set = pca963x_blink_set; 433 - 434 - err = devm_led_classdev_register(dev, &led->led_cdev); 435 - if (err < 0) 436 - return err; 437 - } 438 - 439 406 /* Disable LED all-call address, and power down initially */ 440 407 i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4)); 441 408 442 - if (pdata) { 443 - u8 mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2); 444 - /* Configure output: open-drain or totem pole (push-pull) */ 445 - if (pdata->outdrv == PCA963X_OPEN_DRAIN) 446 - mode2 &= ~PCA963X_MODE2_OUTDRV; 447 - else 448 - mode2 |= PCA963X_MODE2_OUTDRV; 449 - /* Configure direction: normal or inverted */ 450 - if (pdata->dir == PCA963X_INVERTED) 451 - mode2 |= PCA963X_MODE2_INVRT; 452 - i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2); 453 - } 454 - 455 - return 0; 409 + return pca963x_register_leds(client, chip); 456 410 } 457 411 458 412 static struct i2c_driver pca963x_driver = {
-35
include/linux/platform_data/leds-pca963x.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * PCA963X LED chip driver. 4 - * 5 - * Copyright 2012 bct electronic GmbH 6 - * Copyright 2013 Qtechnology A/S 7 - */ 8 - 9 - #ifndef __LINUX_PCA963X_H 10 - #define __LINUX_PCA963X_H 11 - #include <linux/leds.h> 12 - 13 - enum pca963x_outdrv { 14 - PCA963X_OPEN_DRAIN, 15 - PCA963X_TOTEM_POLE, /* aka push-pull */ 16 - }; 17 - 18 - enum pca963x_blink_type { 19 - PCA963X_SW_BLINK, 20 - PCA963X_HW_BLINK, 21 - }; 22 - 23 - enum pca963x_direction { 24 - PCA963X_NORMAL, 25 - PCA963X_INVERTED, 26 - }; 27 - 28 - struct pca963x_platform_data { 29 - struct led_platform_data leds; 30 - enum pca963x_outdrv outdrv; 31 - enum pca963x_blink_type blink_type; 32 - enum pca963x_direction dir; 33 - }; 34 - 35 - #endif /* __LINUX_PCA963X_H*/