hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/leds/leds-tca6507.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * leds-tca6507
34 *
....@@ -68,23 +69,6 @@
6869 * defaulted. Similarly the banks know if each time was explicit or a
6970 * default. Defaults are permitted to be changed freely - they are
7071 * not recognised when matching.
71
- *
72
- *
73
- * An led-tca6507 device must be provided with platform data or
74
- * configured via devicetree.
75
- *
76
- * The platform-data lists for each output: the name, default trigger,
77
- * and whether the signal is being used as a GPIO rather than an LED.
78
- * 'struct led_plaform_data' is used for this. If 'name' is NULL, the
79
- * output isn't used. If 'flags' is TCA6507_MAKE_GPIO, the output is
80
- * a GPO. The "struct led_platform_data" can be embedded in a "struct
81
- * tca6507_platform_data" which adds a 'gpio_base' for the GPIOs, and
82
- * a 'setup' callback which is called once the GPIOs are available.
83
- *
84
- * When configured via devicetree there is one child for each output.
85
- * The "reg" determines the output number and "compatible" determines
86
- * whether it is an LED or a GPIO. "linux,default-trigger" can set a
87
- * default trigger.
8872 */
8973
9074 #include <linux/module.h>
....@@ -92,10 +76,9 @@
9276 #include <linux/leds.h>
9377 #include <linux/err.h>
9478 #include <linux/i2c.h>
95
-#include <linux/gpio.h>
79
+#include <linux/gpio/driver.h>
80
+#include <linux/property.h>
9681 #include <linux/workqueue.h>
97
-#include <linux/leds-tca6507.h>
98
-#include <linux/of.h>
9982
10083 /* LED select registers determine the source that drives LED outputs */
10184 #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */
....@@ -106,6 +89,15 @@
10689 #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */
10790 #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */
10891 #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */
92
+
93
+struct tca6507_platform_data {
94
+ struct led_platform_data leds;
95
+#ifdef CONFIG_GPIOLIB
96
+ int gpio_base;
97
+#endif
98
+};
99
+
100
+#define TCA6507_MAKE_GPIO 1
109101
110102 enum {
111103 BANK0,
....@@ -188,7 +180,6 @@
188180 } leds[NUM_LEDS];
189181 #ifdef CONFIG_GPIOLIB
190182 struct gpio_chip gpio;
191
- const char *gpio_name[NUM_LEDS];
192183 int gpio_map[NUM_LEDS];
193184 #endif
194185 };
....@@ -627,7 +618,7 @@
627618 return 0;
628619 }
629620
630
-static int tca6507_probe_gpios(struct i2c_client *client,
621
+static int tca6507_probe_gpios(struct device *dev,
631622 struct tca6507_chip *tca,
632623 struct tca6507_platform_data *pdata)
633624 {
....@@ -638,7 +629,6 @@
638629 for (i = 0; i < NUM_LEDS; i++)
639630 if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
640631 /* Configure as a gpio */
641
- tca->gpio_name[gpios] = pdata->leds.leds[i].name;
642632 tca->gpio_map[gpios] = i;
643633 gpios++;
644634 }
....@@ -647,23 +637,20 @@
647637 return 0;
648638
649639 tca->gpio.label = "gpio-tca6507";
650
- tca->gpio.names = tca->gpio_name;
651640 tca->gpio.ngpio = gpios;
652641 tca->gpio.base = pdata->gpio_base;
653642 tca->gpio.owner = THIS_MODULE;
654643 tca->gpio.direction_output = tca6507_gpio_direction_output;
655644 tca->gpio.set = tca6507_gpio_set_value;
656
- tca->gpio.parent = &client->dev;
645
+ tca->gpio.parent = dev;
657646 #ifdef CONFIG_OF_GPIO
658
- tca->gpio.of_node = of_node_get(client->dev.of_node);
647
+ tca->gpio.of_node = of_node_get(dev_of_node(dev));
659648 #endif
660649 err = gpiochip_add_data(&tca->gpio, tca);
661650 if (err) {
662651 tca->gpio.ngpio = 0;
663652 return err;
664653 }
665
- if (pdata->setup)
666
- pdata->setup(tca->gpio.base, tca->gpio.ngpio);
667654 return 0;
668655 }
669656
....@@ -673,7 +660,7 @@
673660 gpiochip_remove(&tca->gpio);
674661 }
675662 #else /* CONFIG_GPIOLIB */
676
-static int tca6507_probe_gpios(struct i2c_client *client,
663
+static int tca6507_probe_gpios(struct device *dev,
677664 struct tca6507_chip *tca,
678665 struct tca6507_platform_data *pdata)
679666 {
....@@ -684,44 +671,50 @@
684671 }
685672 #endif /* CONFIG_GPIOLIB */
686673
687
-#ifdef CONFIG_OF
688674 static struct tca6507_platform_data *
689
-tca6507_led_dt_init(struct i2c_client *client)
675
+tca6507_led_dt_init(struct device *dev)
690676 {
691
- struct device_node *np = client->dev.of_node, *child;
692677 struct tca6507_platform_data *pdata;
678
+ struct fwnode_handle *child;
693679 struct led_info *tca_leds;
694680 int count;
695681
696
- count = of_get_child_count(np);
682
+ count = device_get_child_node_count(dev);
697683 if (!count || count > NUM_LEDS)
698684 return ERR_PTR(-ENODEV);
699685
700
- tca_leds = devm_kcalloc(&client->dev,
701
- NUM_LEDS, sizeof(struct led_info), GFP_KERNEL);
686
+ tca_leds = devm_kcalloc(dev, NUM_LEDS, sizeof(struct led_info),
687
+ GFP_KERNEL);
702688 if (!tca_leds)
703689 return ERR_PTR(-ENOMEM);
704690
705
- for_each_child_of_node(np, child) {
691
+ device_for_each_child_node(dev, child) {
706692 struct led_info led;
707693 u32 reg;
708694 int ret;
709695
710
- led.name =
711
- of_get_property(child, "label", NULL) ? : child->name;
712
- led.default_trigger =
713
- of_get_property(child, "linux,default-trigger", NULL);
696
+ if (fwnode_property_read_string(child, "label", &led.name))
697
+ led.name = fwnode_get_name(child);
698
+
699
+ fwnode_property_read_string(child, "linux,default-trigger",
700
+ &led.default_trigger);
701
+
714702 led.flags = 0;
715
- if (of_property_match_string(child, "compatible", "gpio") >= 0)
703
+ if (fwnode_property_match_string(child, "compatible",
704
+ "gpio") >= 0)
716705 led.flags |= TCA6507_MAKE_GPIO;
717
- ret = of_property_read_u32(child, "reg", &reg);
718
- if (ret != 0 || reg >= NUM_LEDS)
719
- continue;
706
+
707
+ ret = fwnode_property_read_u32(child, "reg", &reg);
708
+ if (ret || reg >= NUM_LEDS) {
709
+ fwnode_handle_put(child);
710
+ return ERR_PTR(ret ? : -EINVAL);
711
+ }
720712
721713 tca_leds[reg] = led;
722714 }
723
- pdata = devm_kzalloc(&client->dev,
724
- sizeof(struct tca6507_platform_data), GFP_KERNEL);
715
+
716
+ pdata = devm_kzalloc(dev, sizeof(struct tca6507_platform_data),
717
+ GFP_KERNEL);
725718 if (!pdata)
726719 return ERR_PTR(-ENOMEM);
727720
....@@ -730,48 +723,37 @@
730723 #ifdef CONFIG_GPIOLIB
731724 pdata->gpio_base = -1;
732725 #endif
726
+
733727 return pdata;
734728 }
735729
736
-static const struct of_device_id of_tca6507_leds_match[] = {
730
+static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
737731 { .compatible = "ti,tca6507", },
738732 {},
739733 };
740734 MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
741735
742
-#else
743
-static struct tca6507_platform_data *
744
-tca6507_led_dt_init(struct i2c_client *client)
745
-{
746
- return ERR_PTR(-ENODEV);
747
-}
748
-
749
-#endif
750
-
751736 static int tca6507_probe(struct i2c_client *client,
752737 const struct i2c_device_id *id)
753738 {
754
- struct tca6507_chip *tca;
739
+ struct device *dev = &client->dev;
755740 struct i2c_adapter *adapter;
741
+ struct tca6507_chip *tca;
756742 struct tca6507_platform_data *pdata;
757743 int err;
758744 int i = 0;
759745
760
- adapter = to_i2c_adapter(client->dev.parent);
761
- pdata = dev_get_platdata(&client->dev);
746
+ adapter = client->adapter;
762747
763748 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
764749 return -EIO;
765750
766
- if (!pdata || pdata->leds.num_leds != NUM_LEDS) {
767
- pdata = tca6507_led_dt_init(client);
768
- if (IS_ERR(pdata)) {
769
- dev_err(&client->dev, "Need %d entries in platform-data list\n",
770
- NUM_LEDS);
771
- return PTR_ERR(pdata);
772
- }
751
+ pdata = tca6507_led_dt_init(dev);
752
+ if (IS_ERR(pdata)) {
753
+ dev_err(dev, "Need %d entries in platform-data list\n", NUM_LEDS);
754
+ return PTR_ERR(pdata);
773755 }
774
- tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL);
756
+ tca = devm_kzalloc(dev, sizeof(*tca), GFP_KERNEL);
775757 if (!tca)
776758 return -ENOMEM;
777759
....@@ -792,13 +774,12 @@
792774 l->led_cdev.brightness_set = tca6507_brightness_set;
793775 l->led_cdev.blink_set = tca6507_blink_set;
794776 l->bank = -1;
795
- err = led_classdev_register(&client->dev,
796
- &l->led_cdev);
777
+ err = led_classdev_register(dev, &l->led_cdev);
797778 if (err < 0)
798779 goto exit;
799780 }
800781 }
801
- err = tca6507_probe_gpios(client, tca, pdata);
782
+ err = tca6507_probe_gpios(dev, tca, pdata);
802783 if (err)
803784 goto exit;
804785 /* set all registers to known state - zero */