.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright 2014 Belkin Inc. |
---|
3 | 4 | * Copyright 2015 Andrew Lunn <andrew@lunn.ch> |
---|
4 | | - * |
---|
5 | | - * This program is free software; you can redistribute it and/or modify |
---|
6 | | - * it under the terms of the GNU General Public License as published by |
---|
7 | | - * the Free Software Foundation; version 2 of the License. |
---|
8 | 5 | */ |
---|
9 | 6 | |
---|
10 | 7 | #include <linux/i2c.h> |
---|
.. | .. |
---|
16 | 13 | #include <linux/slab.h> |
---|
17 | 14 | |
---|
18 | 15 | #define TLC591XX_MAX_LEDS 16 |
---|
| 16 | +#define TLC591XX_MAX_BRIGHTNESS 256 |
---|
19 | 17 | |
---|
20 | 18 | #define TLC591XX_REG_MODE1 0x00 |
---|
21 | 19 | #define MODE1_RESPON_ADDR_MASK 0xF0 |
---|
.. | .. |
---|
115 | 113 | struct tlc591xx_priv *priv = led->priv; |
---|
116 | 114 | int err; |
---|
117 | 115 | |
---|
118 | | - switch (brightness) { |
---|
| 116 | + switch ((int)brightness) { |
---|
119 | 117 | case 0: |
---|
120 | 118 | err = tlc591xx_set_ledout(priv, led, LEDOUT_OFF); |
---|
121 | 119 | break; |
---|
122 | | - case LED_FULL: |
---|
| 120 | + case TLC591XX_MAX_BRIGHTNESS: |
---|
123 | 121 | err = tlc591xx_set_ledout(priv, led, LEDOUT_ON); |
---|
124 | 122 | break; |
---|
125 | 123 | default: |
---|
.. | .. |
---|
128 | 126 | err = tlc591xx_set_pwm(priv, led, brightness); |
---|
129 | 127 | } |
---|
130 | 128 | |
---|
131 | | - return err; |
---|
132 | | -} |
---|
133 | | - |
---|
134 | | -static void |
---|
135 | | -tlc591xx_destroy_devices(struct tlc591xx_priv *priv, unsigned int j) |
---|
136 | | -{ |
---|
137 | | - int i = j; |
---|
138 | | - |
---|
139 | | - while (--i >= 0) { |
---|
140 | | - if (priv->leds[i].active) |
---|
141 | | - led_classdev_unregister(&priv->leds[i].ldev); |
---|
142 | | - } |
---|
143 | | -} |
---|
144 | | - |
---|
145 | | -static int |
---|
146 | | -tlc591xx_configure(struct device *dev, |
---|
147 | | - struct tlc591xx_priv *priv, |
---|
148 | | - const struct tlc591xx *tlc591xx) |
---|
149 | | -{ |
---|
150 | | - unsigned int i; |
---|
151 | | - int err = 0; |
---|
152 | | - |
---|
153 | | - tlc591xx_set_mode(priv->regmap, MODE2_DIM); |
---|
154 | | - for (i = 0; i < TLC591XX_MAX_LEDS; i++) { |
---|
155 | | - struct tlc591xx_led *led = &priv->leds[i]; |
---|
156 | | - |
---|
157 | | - if (!led->active) |
---|
158 | | - continue; |
---|
159 | | - |
---|
160 | | - led->priv = priv; |
---|
161 | | - led->led_no = i; |
---|
162 | | - led->ldev.brightness_set_blocking = tlc591xx_brightness_set; |
---|
163 | | - led->ldev.max_brightness = LED_FULL; |
---|
164 | | - err = led_classdev_register(dev, &led->ldev); |
---|
165 | | - if (err < 0) { |
---|
166 | | - dev_err(dev, "couldn't register LED %s\n", |
---|
167 | | - led->ldev.name); |
---|
168 | | - goto exit; |
---|
169 | | - } |
---|
170 | | - } |
---|
171 | | - |
---|
172 | | - return 0; |
---|
173 | | - |
---|
174 | | -exit: |
---|
175 | | - tlc591xx_destroy_devices(priv, i); |
---|
176 | 129 | return err; |
---|
177 | 130 | } |
---|
178 | 131 | |
---|
.. | .. |
---|
195 | 148 | tlc591xx_probe(struct i2c_client *client, |
---|
196 | 149 | const struct i2c_device_id *id) |
---|
197 | 150 | { |
---|
198 | | - struct device_node *np = client->dev.of_node, *child; |
---|
| 151 | + struct device_node *np, *child; |
---|
199 | 152 | struct device *dev = &client->dev; |
---|
200 | | - const struct of_device_id *match; |
---|
201 | 153 | const struct tlc591xx *tlc591xx; |
---|
202 | 154 | struct tlc591xx_priv *priv; |
---|
203 | 155 | int err, count, reg; |
---|
204 | 156 | |
---|
205 | | - match = of_match_device(of_tlc591xx_leds_match, dev); |
---|
206 | | - if (!match) |
---|
207 | | - return -ENODEV; |
---|
208 | | - |
---|
209 | | - tlc591xx = match->data; |
---|
| 157 | + np = dev_of_node(dev); |
---|
210 | 158 | if (!np) |
---|
211 | 159 | return -ENODEV; |
---|
212 | 160 | |
---|
213 | | - count = of_get_child_count(np); |
---|
| 161 | + tlc591xx = device_get_match_data(dev); |
---|
| 162 | + if (!tlc591xx) |
---|
| 163 | + return -ENODEV; |
---|
| 164 | + |
---|
| 165 | + count = of_get_available_child_count(np); |
---|
214 | 166 | if (!count || count > tlc591xx->max_leds) |
---|
215 | 167 | return -EINVAL; |
---|
216 | 168 | |
---|
.. | .. |
---|
228 | 180 | |
---|
229 | 181 | i2c_set_clientdata(client, priv); |
---|
230 | 182 | |
---|
231 | | - for_each_child_of_node(np, child) { |
---|
| 183 | + err = tlc591xx_set_mode(priv->regmap, MODE2_DIM); |
---|
| 184 | + if (err < 0) |
---|
| 185 | + return err; |
---|
| 186 | + |
---|
| 187 | + for_each_available_child_of_node(np, child) { |
---|
| 188 | + struct tlc591xx_led *led; |
---|
| 189 | + struct led_init_data init_data = {}; |
---|
| 190 | + |
---|
| 191 | + init_data.fwnode = of_fwnode_handle(child); |
---|
| 192 | + |
---|
232 | 193 | err = of_property_read_u32(child, "reg", ®); |
---|
233 | 194 | if (err) { |
---|
234 | 195 | of_node_put(child); |
---|
.. | .. |
---|
239 | 200 | of_node_put(child); |
---|
240 | 201 | return -EINVAL; |
---|
241 | 202 | } |
---|
242 | | - priv->leds[reg].active = true; |
---|
243 | | - priv->leds[reg].ldev.name = |
---|
244 | | - of_get_property(child, "label", NULL) ? : child->name; |
---|
245 | | - priv->leds[reg].ldev.default_trigger = |
---|
246 | | - of_get_property(child, "linux,default-trigger", NULL); |
---|
| 203 | + led = &priv->leds[reg]; |
---|
| 204 | + |
---|
| 205 | + led->active = true; |
---|
| 206 | + led->priv = priv; |
---|
| 207 | + led->led_no = reg; |
---|
| 208 | + led->ldev.brightness_set_blocking = tlc591xx_brightness_set; |
---|
| 209 | + led->ldev.max_brightness = TLC591XX_MAX_BRIGHTNESS; |
---|
| 210 | + err = devm_led_classdev_register_ext(dev, &led->ldev, |
---|
| 211 | + &init_data); |
---|
| 212 | + if (err < 0) { |
---|
| 213 | + of_node_put(child); |
---|
| 214 | + return dev_err_probe(dev, err, |
---|
| 215 | + "couldn't register LED %s\n", |
---|
| 216 | + led->ldev.name); |
---|
| 217 | + } |
---|
247 | 218 | } |
---|
248 | | - return tlc591xx_configure(dev, priv, tlc591xx); |
---|
249 | | -} |
---|
250 | | - |
---|
251 | | -static int |
---|
252 | | -tlc591xx_remove(struct i2c_client *client) |
---|
253 | | -{ |
---|
254 | | - struct tlc591xx_priv *priv = i2c_get_clientdata(client); |
---|
255 | | - |
---|
256 | | - tlc591xx_destroy_devices(priv, TLC591XX_MAX_LEDS); |
---|
257 | | - |
---|
258 | 219 | return 0; |
---|
259 | 220 | } |
---|
260 | 221 | |
---|
.. | .. |
---|
271 | 232 | .of_match_table = of_match_ptr(of_tlc591xx_leds_match), |
---|
272 | 233 | }, |
---|
273 | 234 | .probe = tlc591xx_probe, |
---|
274 | | - .remove = tlc591xx_remove, |
---|
275 | 235 | .id_table = tlc591xx_id, |
---|
276 | 236 | }; |
---|
277 | 237 | |
---|