Skip to content

Commit 940cca1

Browse files
elkablopavelmachek
authored andcommitted
leds: ns2: convert to fwnode API
Convert from OF api to fwnode API, so that it is possible to bind this driver without device-tree. The fwnode API does not expose a function to read a specific element of an array. We therefore change the types of the ns2_led_modval structure so that we can read the whole modval array with one fwnode call. Signed-off-by: Marek Behún <[email protected]> Tested-by: Simon Guinot <[email protected]> Signed-off-by: Pavel Machek <[email protected]>
1 parent 108f466 commit 940cca1

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

drivers/leds/leds-ns2.c

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ enum ns2_led_modes {
2424
NS_V2_LED_SATA,
2525
};
2626

27+
/*
28+
* If the size of this structure or types of its members is changed,
29+
* the filling of array modval in function ns2_led_register must be changed
30+
* accordingly.
31+
*/
2732
struct ns2_led_modval {
28-
enum ns2_led_modes mode;
29-
int cmd_level;
30-
int slow_level;
31-
};
33+
u32 mode;
34+
u32 cmd_level;
35+
u32 slow_level;
36+
} __packed;
3237

3338
/*
3439
* The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
@@ -167,27 +172,28 @@ static struct attribute *ns2_led_attrs[] = {
167172
};
168173
ATTRIBUTE_GROUPS(ns2_led);
169174

170-
static int ns2_led_register(struct device *dev, struct device_node *np,
175+
static int ns2_led_register(struct device *dev, struct fwnode_handle *node,
171176
struct ns2_led *led)
172177
{
173178
struct led_init_data init_data = {};
174179
struct ns2_led_modval *modval;
175180
enum ns2_led_modes mode;
176-
int nmodes, ret, i;
181+
int nmodes, ret;
177182

178-
led->cmd = devm_gpiod_get_from_of_node(dev, np, "cmd-gpio", 0,
179-
GPIOD_ASIS, np->name);
183+
led->cmd = devm_fwnode_gpiod_get_index(dev, node, "cmd", 0, GPIOD_ASIS,
184+
fwnode_get_name(node));
180185
if (IS_ERR(led->cmd))
181186
return PTR_ERR(led->cmd);
182187

183-
led->slow = devm_gpiod_get_from_of_node(dev, np, "slow-gpio", 0,
184-
GPIOD_ASIS, np->name);
188+
led->slow = devm_fwnode_gpiod_get_index(dev, node, "slow", 0,
189+
GPIOD_ASIS,
190+
fwnode_get_name(node));
185191
if (IS_ERR(led->slow))
186192
return PTR_ERR(led->slow);
187193

188-
ret = of_property_count_u32_elems(np, "modes-map");
194+
ret = fwnode_property_count_u32(node, "modes-map");
189195
if (ret < 0 || ret % 3) {
190-
dev_err(dev, "Missing or malformed modes-map for %pOF\n", np);
196+
dev_err(dev, "Missing or malformed modes-map for %pfw\n", node);
191197
return -EINVAL;
192198
}
193199

@@ -196,16 +202,8 @@ static int ns2_led_register(struct device *dev, struct device_node *np,
196202
if (!modval)
197203
return -ENOMEM;
198204

199-
for (i = 0; i < nmodes; i++) {
200-
u32 val;
201-
202-
of_property_read_u32_index(np, "modes-map", 3 * i, &val);
203-
modval[i].mode = val;
204-
of_property_read_u32_index(np, "modes-map", 3 * i + 1, &val);
205-
modval[i].cmd_level = val;
206-
of_property_read_u32_index(np, "modes-map", 3 * i + 2, &val);
207-
modval[i].slow_level = val;
208-
}
205+
fwnode_property_read_u32_array(node, "modes-map", (void *)modval,
206+
nmodes * 3);
209207

210208
rwlock_init(&led->rw_lock);
211209

@@ -228,11 +226,11 @@ static int ns2_led_register(struct device *dev, struct device_node *np,
228226
led->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
229227
led->cdev.brightness = (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
230228

231-
init_data.fwnode = of_fwnode_handle(np);
229+
init_data.fwnode = node;
232230

233231
ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
234232
if (ret)
235-
dev_err(dev, "Failed to register LED for node %pOF\n", np);
233+
dev_err(dev, "Failed to register LED for node %pfw\n", node);
236234

237235
return ret;
238236
}
@@ -246,24 +244,23 @@ MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
246244
static int ns2_led_probe(struct platform_device *pdev)
247245
{
248246
struct device *dev = &pdev->dev;
249-
struct device_node *np, *child;
247+
struct fwnode_handle *child;
250248
struct ns2_led *leds;
251249
int count;
252250
int ret;
253251

254-
np = dev_of_node(dev);
255-
count = of_get_available_child_count(np);
252+
count = device_get_child_node_count(dev);
256253
if (!count)
257254
return -ENODEV;
258255

259256
leds = devm_kzalloc(dev, array_size(sizeof(*leds), count), GFP_KERNEL);
260257
if (!leds)
261258
return -ENOMEM;
262259

263-
for_each_available_child_of_node(np, child) {
260+
device_for_each_child_node(dev, child) {
264261
ret = ns2_led_register(dev, child, leds++);
265262
if (ret) {
266-
of_node_put(child);
263+
fwnode_handle_put(child);
267264
return ret;
268265
}
269266
}

0 commit comments

Comments
 (0)