1 // SPDX-License-Identifier: GPL-2.0
2 // TI LM36274 LED chip family driver
3 // Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
4
5 #include <linux/bitops.h>
6 #include <linux/device.h>
7 #include <linux/err.h>
8 #include <linux/leds.h>
9 #include <linux/leds-ti-lmu-common.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13
14 #include <linux/mfd/ti-lmu.h>
15 #include <linux/mfd/ti-lmu-register.h>
16
17 #include <uapi/linux/uleds.h>
18
19 #define LM36274_MAX_STRINGS 4
20 #define LM36274_BL_EN BIT(4)
21
22 /**
23 * struct lm36274
24 * @pdev: platform device
25 * @led_dev: led class device
26 * @lmu_data: Register and setting values for common code
27 * @regmap: Devices register map
28 * @dev: Pointer to the devices device struct
29 * @led_sources - The LED strings supported in this array
30 * @num_leds - Number of LED strings are supported in this array
31 */
32 struct lm36274 {
33 struct platform_device *pdev;
34 struct led_classdev led_dev;
35 struct ti_lmu_bank lmu_data;
36 struct regmap *regmap;
37 struct device *dev;
38
39 u32 led_sources[LM36274_MAX_STRINGS];
40 int num_leds;
41 };
42
lm36274_brightness_set(struct led_classdev * led_cdev,enum led_brightness brt_val)43 static int lm36274_brightness_set(struct led_classdev *led_cdev,
44 enum led_brightness brt_val)
45 {
46 struct lm36274 *chip = container_of(led_cdev, struct lm36274, led_dev);
47
48 return ti_lmu_common_set_brightness(&chip->lmu_data, brt_val);
49 }
50
lm36274_init(struct lm36274 * chip)51 static int lm36274_init(struct lm36274 *chip)
52 {
53 int enable_val = 0;
54 int i;
55
56 for (i = 0; i < chip->num_leds; i++)
57 enable_val |= (1 << chip->led_sources[i]);
58
59 if (!enable_val) {
60 dev_err(chip->dev, "No LEDs were enabled\n");
61 return -EINVAL;
62 }
63
64 enable_val |= LM36274_BL_EN;
65
66 return regmap_write(chip->regmap, LM36274_REG_BL_EN, enable_val);
67 }
68
lm36274_parse_dt(struct lm36274 * chip)69 static int lm36274_parse_dt(struct lm36274 *chip)
70 {
71 struct fwnode_handle *child = NULL;
72 char label[LED_MAX_NAME_SIZE];
73 struct device *dev = &chip->pdev->dev;
74 const char *name;
75 int child_cnt;
76 int ret = -EINVAL;
77
78 /* There should only be 1 node */
79 child_cnt = device_get_child_node_count(dev);
80 if (child_cnt != 1)
81 return -EINVAL;
82
83 device_for_each_child_node(dev, child) {
84 ret = fwnode_property_read_string(child, "label", &name);
85 if (ret)
86 snprintf(label, sizeof(label), "%s::",
87 chip->pdev->name);
88 else
89 snprintf(label, sizeof(label), "%s:%s",
90 chip->pdev->name, name);
91
92 chip->num_leds = fwnode_property_count_u32(child, "led-sources");
93 if (chip->num_leds <= 0)
94 return -ENODEV;
95
96 ret = fwnode_property_read_u32_array(child, "led-sources",
97 chip->led_sources,
98 chip->num_leds);
99 if (ret) {
100 dev_err(dev, "led-sources property missing\n");
101 return ret;
102 }
103
104 fwnode_property_read_string(child, "linux,default-trigger",
105 &chip->led_dev.default_trigger);
106
107 }
108
109 chip->lmu_data.regmap = chip->regmap;
110 chip->lmu_data.max_brightness = MAX_BRIGHTNESS_11BIT;
111 chip->lmu_data.msb_brightness_reg = LM36274_REG_BRT_MSB;
112 chip->lmu_data.lsb_brightness_reg = LM36274_REG_BRT_LSB;
113
114 chip->led_dev.name = label;
115 chip->led_dev.max_brightness = MAX_BRIGHTNESS_11BIT;
116 chip->led_dev.brightness_set_blocking = lm36274_brightness_set;
117
118 return 0;
119 }
120
lm36274_probe(struct platform_device * pdev)121 static int lm36274_probe(struct platform_device *pdev)
122 {
123 struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent);
124 struct lm36274 *chip;
125 int ret;
126
127 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
128 if (!chip)
129 return -ENOMEM;
130
131 chip->pdev = pdev;
132 chip->dev = lmu->dev;
133 chip->regmap = lmu->regmap;
134 platform_set_drvdata(pdev, chip);
135
136 ret = lm36274_parse_dt(chip);
137 if (ret) {
138 dev_err(chip->dev, "Failed to parse DT node\n");
139 return ret;
140 }
141
142 ret = lm36274_init(chip);
143 if (ret) {
144 dev_err(chip->dev, "Failed to init the device\n");
145 return ret;
146 }
147
148 return led_classdev_register(chip->dev, &chip->led_dev);
149 }
150
lm36274_remove(struct platform_device * pdev)151 static int lm36274_remove(struct platform_device *pdev)
152 {
153 struct lm36274 *chip = platform_get_drvdata(pdev);
154
155 led_classdev_unregister(&chip->led_dev);
156
157 return 0;
158 }
159
160 static const struct of_device_id of_lm36274_leds_match[] = {
161 { .compatible = "ti,lm36274-backlight", },
162 {},
163 };
164 MODULE_DEVICE_TABLE(of, of_lm36274_leds_match);
165
166 static struct platform_driver lm36274_driver = {
167 .probe = lm36274_probe,
168 .remove = lm36274_remove,
169 .driver = {
170 .name = "lm36274-leds",
171 },
172 };
173 module_platform_driver(lm36274_driver)
174
175 MODULE_DESCRIPTION("Texas Instruments LM36274 LED driver");
176 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
177 MODULE_LICENSE("GPL v2");
178