Skip to content

Commit db30c28

Browse files
Ansuellag-linaro
authored andcommitted
leds: leds-lp55xx: Generalize probe/remove functions
Now that stop_all_engine is generalized, probe and remove function are the same across every lp55xx based LED driver and can be generalized. To permit to use a common probe, make use of the OF match_data and i2c driver_data value to store the device_config struct specific for the LED. Also drop the now unused exported symbol in lp55xx-common and make them static. Update any lp55xx based LED driver to use the new generic probe/remove. Suggested-by: Lee Jones <lee@kernel.org> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com> Link: https://lore.kernel.org/r/20240626160027.19703-5-ansuelsmth@gmail.com Signed-off-by: Lee Jones <lee@kernel.org>
1 parent a9b202b commit db30c28

6 files changed

Lines changed: 127 additions & 362 deletions

File tree

drivers/leds/leds-lp5521.c

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -514,87 +514,14 @@ static struct lp55xx_device_config lp5521_cfg = {
514514
.dev_attr_group = &lp5521_group,
515515
};
516516

517-
static int lp5521_probe(struct i2c_client *client)
518-
{
519-
const struct i2c_device_id *id = i2c_client_get_device_id(client);
520-
int ret;
521-
struct lp55xx_chip *chip;
522-
struct lp55xx_led *led;
523-
struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
524-
struct device_node *np = dev_of_node(&client->dev);
525-
526-
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
527-
if (!chip)
528-
return -ENOMEM;
529-
530-
chip->cfg = &lp5521_cfg;
531-
532-
if (!pdata) {
533-
if (np) {
534-
pdata = lp55xx_of_populate_pdata(&client->dev, np,
535-
chip);
536-
if (IS_ERR(pdata))
537-
return PTR_ERR(pdata);
538-
} else {
539-
dev_err(&client->dev, "no platform data\n");
540-
return -EINVAL;
541-
}
542-
}
543-
544-
led = devm_kcalloc(&client->dev,
545-
pdata->num_channels, sizeof(*led), GFP_KERNEL);
546-
if (!led)
547-
return -ENOMEM;
548-
549-
chip->cl = client;
550-
chip->pdata = pdata;
551-
552-
mutex_init(&chip->lock);
553-
554-
i2c_set_clientdata(client, led);
555-
556-
ret = lp55xx_init_device(chip);
557-
if (ret)
558-
goto err_init;
559-
560-
dev_info(&client->dev, "%s programmable led chip found\n", id->name);
561-
562-
ret = lp55xx_register_leds(led, chip);
563-
if (ret)
564-
goto err_out;
565-
566-
ret = lp55xx_register_sysfs(chip);
567-
if (ret) {
568-
dev_err(&client->dev, "registering sysfs failed\n");
569-
goto err_out;
570-
}
571-
572-
return 0;
573-
574-
err_out:
575-
lp55xx_deinit_device(chip);
576-
err_init:
577-
return ret;
578-
}
579-
580-
static void lp5521_remove(struct i2c_client *client)
581-
{
582-
struct lp55xx_led *led = i2c_get_clientdata(client);
583-
struct lp55xx_chip *chip = led->chip;
584-
585-
lp55xx_stop_all_engine(chip);
586-
lp55xx_unregister_sysfs(chip);
587-
lp55xx_deinit_device(chip);
588-
}
589-
590517
static const struct i2c_device_id lp5521_id[] = {
591-
{ "lp5521" }, /* Three channel chip */
518+
{ "lp5521", .driver_data = (kernel_ulong_t)&lp5521_cfg, }, /* Three channel chip */
592519
{ }
593520
};
594521
MODULE_DEVICE_TABLE(i2c, lp5521_id);
595522

596523
static const struct of_device_id of_lp5521_leds_match[] = {
597-
{ .compatible = "national,lp5521", },
524+
{ .compatible = "national,lp5521", .data = &lp5521_cfg, },
598525
{},
599526
};
600527

@@ -605,8 +532,8 @@ static struct i2c_driver lp5521_driver = {
605532
.name = "lp5521",
606533
.of_match_table = of_lp5521_leds_match,
607534
},
608-
.probe = lp5521_probe,
609-
.remove = lp5521_remove,
535+
.probe = lp55xx_probe,
536+
.remove = lp55xx_remove,
610537
.id_table = lp5521_id,
611538
};
612539

drivers/leds/leds-lp5523.c

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -895,90 +895,17 @@ static struct lp55xx_device_config lp5523_cfg = {
895895
.dev_attr_group = &lp5523_group,
896896
};
897897

898-
static int lp5523_probe(struct i2c_client *client)
899-
{
900-
const struct i2c_device_id *id = i2c_client_get_device_id(client);
901-
int ret;
902-
struct lp55xx_chip *chip;
903-
struct lp55xx_led *led;
904-
struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
905-
struct device_node *np = dev_of_node(&client->dev);
906-
907-
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
908-
if (!chip)
909-
return -ENOMEM;
910-
911-
chip->cfg = &lp5523_cfg;
912-
913-
if (!pdata) {
914-
if (np) {
915-
pdata = lp55xx_of_populate_pdata(&client->dev, np,
916-
chip);
917-
if (IS_ERR(pdata))
918-
return PTR_ERR(pdata);
919-
} else {
920-
dev_err(&client->dev, "no platform data\n");
921-
return -EINVAL;
922-
}
923-
}
924-
925-
led = devm_kcalloc(&client->dev,
926-
pdata->num_channels, sizeof(*led), GFP_KERNEL);
927-
if (!led)
928-
return -ENOMEM;
929-
930-
chip->cl = client;
931-
chip->pdata = pdata;
932-
933-
mutex_init(&chip->lock);
934-
935-
i2c_set_clientdata(client, led);
936-
937-
ret = lp55xx_init_device(chip);
938-
if (ret)
939-
goto err_init;
940-
941-
dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
942-
943-
ret = lp55xx_register_leds(led, chip);
944-
if (ret)
945-
goto err_out;
946-
947-
ret = lp55xx_register_sysfs(chip);
948-
if (ret) {
949-
dev_err(&client->dev, "registering sysfs failed\n");
950-
goto err_out;
951-
}
952-
953-
return 0;
954-
955-
err_out:
956-
lp55xx_deinit_device(chip);
957-
err_init:
958-
return ret;
959-
}
960-
961-
static void lp5523_remove(struct i2c_client *client)
962-
{
963-
struct lp55xx_led *led = i2c_get_clientdata(client);
964-
struct lp55xx_chip *chip = led->chip;
965-
966-
lp55xx_stop_all_engine(chip);
967-
lp55xx_unregister_sysfs(chip);
968-
lp55xx_deinit_device(chip);
969-
}
970-
971898
static const struct i2c_device_id lp5523_id[] = {
972-
{ "lp5523", LP5523 },
973-
{ "lp55231", LP55231 },
899+
{ "lp5523", .driver_data = (kernel_ulong_t)&lp5523_cfg, },
900+
{ "lp55231", .driver_data = (kernel_ulong_t)&lp5523_cfg, },
974901
{ }
975902
};
976903

977904
MODULE_DEVICE_TABLE(i2c, lp5523_id);
978905

979906
static const struct of_device_id of_lp5523_leds_match[] = {
980-
{ .compatible = "national,lp5523", },
981-
{ .compatible = "ti,lp55231", },
907+
{ .compatible = "national,lp5523", .data = &lp5523_cfg, },
908+
{ .compatible = "ti,lp55231", .data = &lp5523_cfg, },
982909
{},
983910
};
984911

@@ -989,8 +916,8 @@ static struct i2c_driver lp5523_driver = {
989916
.name = "lp5523x",
990917
.of_match_table = of_lp5523_leds_match,
991918
},
992-
.probe = lp5523_probe,
993-
.remove = lp5523_remove,
919+
.probe = lp55xx_probe,
920+
.remove = lp55xx_remove,
994921
.id_table = lp5523_id,
995922
};
996923

drivers/leds/leds-lp5562.c

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -508,86 +508,14 @@ static struct lp55xx_device_config lp5562_cfg = {
508508
.dev_attr_group = &lp5562_group,
509509
};
510510

511-
static int lp5562_probe(struct i2c_client *client)
512-
{
513-
int ret;
514-
struct lp55xx_chip *chip;
515-
struct lp55xx_led *led;
516-
struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
517-
struct device_node *np = dev_of_node(&client->dev);
518-
519-
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
520-
if (!chip)
521-
return -ENOMEM;
522-
523-
chip->cfg = &lp5562_cfg;
524-
525-
if (!pdata) {
526-
if (np) {
527-
pdata = lp55xx_of_populate_pdata(&client->dev, np,
528-
chip);
529-
if (IS_ERR(pdata))
530-
return PTR_ERR(pdata);
531-
} else {
532-
dev_err(&client->dev, "no platform data\n");
533-
return -EINVAL;
534-
}
535-
}
536-
537-
538-
led = devm_kcalloc(&client->dev,
539-
pdata->num_channels, sizeof(*led), GFP_KERNEL);
540-
if (!led)
541-
return -ENOMEM;
542-
543-
chip->cl = client;
544-
chip->pdata = pdata;
545-
546-
mutex_init(&chip->lock);
547-
548-
i2c_set_clientdata(client, led);
549-
550-
ret = lp55xx_init_device(chip);
551-
if (ret)
552-
goto err_init;
553-
554-
ret = lp55xx_register_leds(led, chip);
555-
if (ret)
556-
goto err_out;
557-
558-
ret = lp55xx_register_sysfs(chip);
559-
if (ret) {
560-
dev_err(&client->dev, "registering sysfs failed\n");
561-
goto err_out;
562-
}
563-
564-
return 0;
565-
566-
err_out:
567-
lp55xx_deinit_device(chip);
568-
err_init:
569-
return ret;
570-
}
571-
572-
static void lp5562_remove(struct i2c_client *client)
573-
{
574-
struct lp55xx_led *led = i2c_get_clientdata(client);
575-
struct lp55xx_chip *chip = led->chip;
576-
577-
lp55xx_stop_all_engine(chip);
578-
579-
lp55xx_unregister_sysfs(chip);
580-
lp55xx_deinit_device(chip);
581-
}
582-
583511
static const struct i2c_device_id lp5562_id[] = {
584-
{ "lp5562" },
512+
{ "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
585513
{ }
586514
};
587515
MODULE_DEVICE_TABLE(i2c, lp5562_id);
588516

589517
static const struct of_device_id of_lp5562_leds_match[] = {
590-
{ .compatible = "ti,lp5562", },
518+
{ .compatible = "ti,lp5562", .data = &lp5562_cfg, },
591519
{},
592520
};
593521

@@ -598,8 +526,8 @@ static struct i2c_driver lp5562_driver = {
598526
.name = "lp5562",
599527
.of_match_table = of_lp5562_leds_match,
600528
},
601-
.probe = lp5562_probe,
602-
.remove = lp5562_remove,
529+
.probe = lp55xx_probe,
530+
.remove = lp55xx_remove,
603531
.id_table = lp5562_id,
604532
};
605533

0 commit comments

Comments
 (0)