]>
Commit | Line | Data |
---|---|---|
75cb2e1d PM |
1 | /* |
2 | * Copyright 2011 bct electronic GmbH | |
af67384f | 3 | * Copyright 2013 Qtechnology/AS |
75cb2e1d PM |
4 | * |
5 | * Author: Peter Meerwald <p.meerwald@bct-electronic.com> | |
af67384f | 6 | * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com> |
75cb2e1d PM |
7 | * |
8 | * Based on leds-pca955x.c | |
9 | * | |
10 | * This file is subject to the terms and conditions of version 2 of | |
11 | * the GNU General Public License. See the file COPYING in the main | |
12 | * directory of this archive for more details. | |
13 | * | |
14 | * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62) | |
af67384f | 15 | * LED driver for the PCA9634 I2C LED driver (7-bit slave address set by hw.) |
75cb2e1d | 16 | * |
8465b018 MG |
17 | * Note that hardware blinking violates the leds infrastructure driver |
18 | * interface since the hardware only supports blinking all LEDs with the | |
19 | * same delay_on/delay_off rates. That is, only the LEDs that are set to | |
20 | * blink will actually blink but all LEDs that are set to blink will blink | |
21 | * in identical fashion. The delay_on/delay_off values of the last LED | |
22 | * that is set to blink will be used for all of the blinking LEDs. | |
23 | * Hardware blinking is disabled by default but can be enabled by setting | |
24 | * the 'blink_type' member in the platform_data struct to 'PCA9633_HW_BLINK' | |
25 | * or by adding the 'nxp,hw-blink' property to the DTS. | |
75cb2e1d PM |
26 | */ |
27 | ||
28 | #include <linux/module.h> | |
29 | #include <linux/delay.h> | |
30 | #include <linux/string.h> | |
31 | #include <linux/ctype.h> | |
32 | #include <linux/leds.h> | |
33 | #include <linux/err.h> | |
34 | #include <linux/i2c.h> | |
35 | #include <linux/workqueue.h> | |
36 | #include <linux/slab.h> | |
81d22878 | 37 | #include <linux/of.h> |
2f73c392 | 38 | #include <linux/platform_data/leds-pca9633.h> |
75cb2e1d PM |
39 | |
40 | /* LED select registers determine the source that drives LED outputs */ | |
41 | #define PCA9633_LED_OFF 0x0 /* LED driver off */ | |
42 | #define PCA9633_LED_ON 0x1 /* LED driver on */ | |
43 | #define PCA9633_LED_PWM 0x2 /* Controlled through PWM */ | |
44 | #define PCA9633_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */ | |
45 | ||
8465b018 MG |
46 | #define PCA9633_MODE2_DMBLNK 0x20 /* Enable blinking */ |
47 | ||
75cb2e1d PM |
48 | #define PCA9633_MODE1 0x00 |
49 | #define PCA9633_MODE2 0x01 | |
50 | #define PCA9633_PWM_BASE 0x02 | |
af67384f RRD |
51 | |
52 | enum pca9633_type { | |
53 | pca9633, | |
54 | pca9634, | |
55 | }; | |
56 | ||
57 | struct pca9633_chipdef { | |
58 | u8 grppwm; | |
59 | u8 grpfreq; | |
60 | u8 ledout_base; | |
61 | int n_leds; | |
62 | }; | |
63 | ||
64 | static struct pca9633_chipdef pca9633_chipdefs[] = { | |
65 | [pca9633] = { | |
66 | .grppwm = 0x6, | |
67 | .grpfreq = 0x7, | |
68 | .ledout_base = 0x8, | |
69 | .n_leds = 4, | |
70 | }, | |
71 | [pca9634] = { | |
72 | .grppwm = 0xa, | |
73 | .grpfreq = 0xb, | |
74 | .ledout_base = 0xc, | |
75 | .n_leds = 8, | |
76 | }, | |
77 | }; | |
75cb2e1d | 78 | |
8465b018 MG |
79 | /* Total blink period in milliseconds */ |
80 | #define PCA9632_BLINK_PERIOD_MIN 42 | |
81 | #define PCA9632_BLINK_PERIOD_MAX 10667 | |
82 | ||
75cb2e1d | 83 | static const struct i2c_device_id pca9633_id[] = { |
af67384f RRD |
84 | { "pca9632", pca9633 }, |
85 | { "pca9633", pca9633 }, | |
86 | { "pca9634", pca9634 }, | |
75cb2e1d PM |
87 | { } |
88 | }; | |
89 | MODULE_DEVICE_TABLE(i2c, pca9633_id); | |
90 | ||
8465b018 MG |
91 | enum pca9633_cmd { |
92 | BRIGHTNESS_SET, | |
93 | BLINK_SET, | |
94 | }; | |
95 | ||
75cb2e1d PM |
96 | struct pca9633_led { |
97 | struct i2c_client *client; | |
af67384f | 98 | struct pca9633_chipdef *chipdef; |
75cb2e1d PM |
99 | struct work_struct work; |
100 | enum led_brightness brightness; | |
101 | struct led_classdev led_cdev; | |
af67384f | 102 | int led_num; /* 0 .. 7 potentially */ |
8465b018 | 103 | enum pca9633_cmd cmd; |
75cb2e1d | 104 | char name[32]; |
8465b018 MG |
105 | u8 gdc; |
106 | u8 gfrq; | |
75cb2e1d PM |
107 | }; |
108 | ||
8465b018 | 109 | static void pca9633_brightness_work(struct pca9633_led *pca9633) |
75cb2e1d | 110 | { |
af67384f RRD |
111 | u8 ledout_addr = pca9633->chipdef->ledout_base + (pca9633->led_num / 4); |
112 | u8 ledout; | |
113 | int shift = 2 * (pca9633->led_num % 4); | |
75cb2e1d PM |
114 | u8 mask = 0x3 << shift; |
115 | ||
af67384f | 116 | ledout = i2c_smbus_read_byte_data(pca9633->client, ledout_addr); |
75cb2e1d PM |
117 | switch (pca9633->brightness) { |
118 | case LED_FULL: | |
af67384f | 119 | i2c_smbus_write_byte_data(pca9633->client, ledout_addr, |
75cb2e1d PM |
120 | (ledout & ~mask) | (PCA9633_LED_ON << shift)); |
121 | break; | |
122 | case LED_OFF: | |
af67384f | 123 | i2c_smbus_write_byte_data(pca9633->client, ledout_addr, |
75cb2e1d PM |
124 | ledout & ~mask); |
125 | break; | |
126 | default: | |
127 | i2c_smbus_write_byte_data(pca9633->client, | |
128 | PCA9633_PWM_BASE + pca9633->led_num, | |
129 | pca9633->brightness); | |
af67384f | 130 | i2c_smbus_write_byte_data(pca9633->client, ledout_addr, |
75cb2e1d PM |
131 | (ledout & ~mask) | (PCA9633_LED_PWM << shift)); |
132 | break; | |
133 | } | |
134 | } | |
135 | ||
8465b018 MG |
136 | static void pca9633_blink_work(struct pca9633_led *pca9633) |
137 | { | |
af67384f RRD |
138 | u8 ledout_addr = pca9633->chipdef->ledout_base + (pca9633->led_num / 4); |
139 | u8 ledout = i2c_smbus_read_byte_data(pca9633->client, ledout_addr); | |
8465b018 | 140 | u8 mode2 = i2c_smbus_read_byte_data(pca9633->client, PCA9633_MODE2); |
af67384f | 141 | int shift = 2 * (pca9633->led_num % 4); |
8465b018 MG |
142 | u8 mask = 0x3 << shift; |
143 | ||
af67384f | 144 | i2c_smbus_write_byte_data(pca9633->client, pca9633->chipdef->grppwm, |
8465b018 MG |
145 | pca9633->gdc); |
146 | ||
af67384f | 147 | i2c_smbus_write_byte_data(pca9633->client, pca9633->chipdef->grpfreq, |
8465b018 MG |
148 | pca9633->gfrq); |
149 | ||
150 | if (!(mode2 & PCA9633_MODE2_DMBLNK)) | |
151 | i2c_smbus_write_byte_data(pca9633->client, PCA9633_MODE2, | |
152 | mode2 | PCA9633_MODE2_DMBLNK); | |
153 | ||
154 | if ((ledout & mask) != (PCA9633_LED_GRP_PWM << shift)) | |
af67384f | 155 | i2c_smbus_write_byte_data(pca9633->client, ledout_addr, |
8465b018 MG |
156 | (ledout & ~mask) | (PCA9633_LED_GRP_PWM << shift)); |
157 | } | |
158 | ||
159 | static void pca9633_work(struct work_struct *work) | |
160 | { | |
161 | struct pca9633_led *pca9633 = container_of(work, | |
162 | struct pca9633_led, work); | |
163 | ||
164 | switch (pca9633->cmd) { | |
165 | case BRIGHTNESS_SET: | |
166 | pca9633_brightness_work(pca9633); | |
167 | break; | |
168 | case BLINK_SET: | |
169 | pca9633_blink_work(pca9633); | |
170 | break; | |
171 | } | |
172 | } | |
173 | ||
75cb2e1d PM |
174 | static void pca9633_led_set(struct led_classdev *led_cdev, |
175 | enum led_brightness value) | |
176 | { | |
177 | struct pca9633_led *pca9633; | |
178 | ||
179 | pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev); | |
180 | ||
8465b018 | 181 | pca9633->cmd = BRIGHTNESS_SET; |
75cb2e1d PM |
182 | pca9633->brightness = value; |
183 | ||
184 | /* | |
185 | * Must use workqueue for the actual I/O since I2C operations | |
186 | * can sleep. | |
187 | */ | |
188 | schedule_work(&pca9633->work); | |
189 | } | |
190 | ||
8465b018 MG |
191 | static int pca9633_blink_set(struct led_classdev *led_cdev, |
192 | unsigned long *delay_on, unsigned long *delay_off) | |
193 | { | |
194 | struct pca9633_led *pca9633; | |
195 | unsigned long time_on, time_off, period; | |
196 | u8 gdc, gfrq; | |
197 | ||
198 | pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev); | |
199 | ||
200 | time_on = *delay_on; | |
201 | time_off = *delay_off; | |
202 | ||
203 | /* If both zero, pick reasonable defaults of 500ms each */ | |
204 | if (!time_on && !time_off) { | |
205 | time_on = 500; | |
206 | time_off = 500; | |
207 | } | |
208 | ||
209 | period = time_on + time_off; | |
210 | ||
211 | /* If period not supported by hardware, default to someting sane. */ | |
212 | if ((period < PCA9632_BLINK_PERIOD_MIN) || | |
213 | (period > PCA9632_BLINK_PERIOD_MAX)) { | |
214 | time_on = 500; | |
215 | time_off = 500; | |
216 | period = time_on + time_off; | |
217 | } | |
218 | ||
219 | /* | |
220 | * From manual: duty cycle = (GDC / 256) -> | |
221 | * (time_on / period) = (GDC / 256) -> | |
222 | * GDC = ((time_on * 256) / period) | |
223 | */ | |
224 | gdc = (time_on * 256) / period; | |
225 | ||
226 | /* | |
227 | * From manual: period = ((GFRQ + 1) / 24) in seconds. | |
228 | * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) -> | |
229 | * GFRQ = ((period * 24 / 1000) - 1) | |
230 | */ | |
231 | gfrq = (period * 24 / 1000) - 1; | |
232 | ||
233 | pca9633->cmd = BLINK_SET; | |
234 | pca9633->gdc = gdc; | |
235 | pca9633->gfrq = gfrq; | |
236 | ||
237 | /* | |
238 | * Must use workqueue for the actual I/O since I2C operations | |
239 | * can sleep. | |
240 | */ | |
241 | schedule_work(&pca9633->work); | |
242 | ||
243 | *delay_on = time_on; | |
244 | *delay_off = time_off; | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
81d22878 TL |
249 | #if IS_ENABLED(CONFIG_OF) |
250 | static struct pca9633_platform_data * | |
af67384f | 251 | pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip) |
81d22878 TL |
252 | { |
253 | struct device_node *np = client->dev.of_node, *child; | |
254 | struct pca9633_platform_data *pdata; | |
255 | struct led_info *pca9633_leds; | |
256 | int count; | |
257 | ||
258 | count = of_get_child_count(np); | |
af67384f | 259 | if (!count || count > chip->n_leds) |
81d22878 TL |
260 | return ERR_PTR(-ENODEV); |
261 | ||
262 | pca9633_leds = devm_kzalloc(&client->dev, | |
af67384f | 263 | sizeof(struct led_info) * chip->n_leds, GFP_KERNEL); |
81d22878 TL |
264 | if (!pca9633_leds) |
265 | return ERR_PTR(-ENOMEM); | |
266 | ||
267 | for_each_child_of_node(np, child) { | |
268 | struct led_info led; | |
269 | u32 reg; | |
270 | int res; | |
271 | ||
272 | led.name = | |
273 | of_get_property(child, "label", NULL) ? : child->name; | |
274 | led.default_trigger = | |
275 | of_get_property(child, "linux,default-trigger", NULL); | |
276 | res = of_property_read_u32(child, "reg", ®); | |
277 | if (res != 0) | |
278 | continue; | |
279 | pca9633_leds[reg] = led; | |
280 | } | |
281 | pdata = devm_kzalloc(&client->dev, | |
282 | sizeof(struct pca9633_platform_data), GFP_KERNEL); | |
283 | if (!pdata) | |
284 | return ERR_PTR(-ENOMEM); | |
285 | ||
286 | pdata->leds.leds = pca9633_leds; | |
287 | pdata->leds.num_leds = count; | |
288 | ||
289 | /* default to open-drain unless totem pole (push-pull) is specified */ | |
290 | if (of_property_read_bool(np, "nxp,totem-pole")) | |
291 | pdata->outdrv = PCA9633_TOTEM_POLE; | |
292 | else | |
293 | pdata->outdrv = PCA9633_OPEN_DRAIN; | |
294 | ||
8465b018 MG |
295 | /* default to software blinking unless hardware blinking is specified */ |
296 | if (of_property_read_bool(np, "nxp,hw-blink")) | |
297 | pdata->blink_type = PCA9633_HW_BLINK; | |
298 | else | |
299 | pdata->blink_type = PCA9633_SW_BLINK; | |
300 | ||
81d22878 TL |
301 | return pdata; |
302 | } | |
303 | ||
304 | static const struct of_device_id of_pca9633_match[] = { | |
af67384f RRD |
305 | { .compatible = "nxp,pca9632", }, |
306 | { .compatible = "nxp,pca9633", }, | |
307 | { .compatible = "nxp,pca9634", }, | |
81d22878 TL |
308 | {}, |
309 | }; | |
310 | #else | |
311 | static struct pca9633_platform_data * | |
af67384f | 312 | pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip) |
81d22878 TL |
313 | { |
314 | return ERR_PTR(-ENODEV); | |
315 | } | |
316 | #endif | |
317 | ||
98ea1ea2 | 318 | static int pca9633_probe(struct i2c_client *client, |
75cb2e1d PM |
319 | const struct i2c_device_id *id) |
320 | { | |
321 | struct pca9633_led *pca9633; | |
2f73c392 | 322 | struct pca9633_platform_data *pdata; |
af67384f | 323 | struct pca9633_chipdef *chip; |
75cb2e1d PM |
324 | int i, err; |
325 | ||
af67384f | 326 | chip = &pca9633_chipdefs[id->driver_data]; |
87aae1ea | 327 | pdata = dev_get_platdata(&client->dev); |
75cb2e1d | 328 | |
81d22878 | 329 | if (!pdata) { |
af67384f | 330 | pdata = pca9633_dt_init(client, chip); |
81d22878 TL |
331 | if (IS_ERR(pdata)) { |
332 | dev_warn(&client->dev, "could not parse configuration\n"); | |
333 | pdata = NULL; | |
334 | } | |
335 | } | |
336 | ||
af67384f RRD |
337 | if (pdata && (pdata->leds.num_leds < 1 || |
338 | pdata->leds.num_leds > chip->n_leds)) { | |
339 | dev_err(&client->dev, "board info must claim 1-%d LEDs", | |
340 | chip->n_leds); | |
341 | return -EINVAL; | |
75cb2e1d PM |
342 | } |
343 | ||
af67384f RRD |
344 | pca9633 = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca9633), |
345 | GFP_KERNEL); | |
75cb2e1d PM |
346 | if (!pca9633) |
347 | return -ENOMEM; | |
348 | ||
349 | i2c_set_clientdata(client, pca9633); | |
350 | ||
af67384f | 351 | for (i = 0; i < chip->n_leds; i++) { |
75cb2e1d PM |
352 | pca9633[i].client = client; |
353 | pca9633[i].led_num = i; | |
af67384f | 354 | pca9633[i].chipdef = chip; |
75cb2e1d PM |
355 | |
356 | /* Platform data can specify LED names and default triggers */ | |
2f73c392 PM |
357 | if (pdata && i < pdata->leds.num_leds) { |
358 | if (pdata->leds.leds[i].name) | |
75cb2e1d PM |
359 | snprintf(pca9633[i].name, |
360 | sizeof(pca9633[i].name), "pca9633:%s", | |
2f73c392 PM |
361 | pdata->leds.leds[i].name); |
362 | if (pdata->leds.leds[i].default_trigger) | |
75cb2e1d | 363 | pca9633[i].led_cdev.default_trigger = |
2f73c392 | 364 | pdata->leds.leds[i].default_trigger; |
75cb2e1d | 365 | } |
a5cd98b7 RRD |
366 | if (!pdata || i >= pdata->leds.num_leds || |
367 | !pdata->leds.leds[i].name) | |
368 | snprintf(pca9633[i].name, sizeof(pca9633[i].name), | |
369 | "pca9633:%d:%.2x:%d", client->adapter->nr, | |
370 | client->addr, i); | |
75cb2e1d PM |
371 | |
372 | pca9633[i].led_cdev.name = pca9633[i].name; | |
373 | pca9633[i].led_cdev.brightness_set = pca9633_led_set; | |
374 | ||
8465b018 MG |
375 | if (pdata && pdata->blink_type == PCA9633_HW_BLINK) |
376 | pca9633[i].led_cdev.blink_set = pca9633_blink_set; | |
377 | ||
378 | INIT_WORK(&pca9633[i].work, pca9633_work); | |
75cb2e1d PM |
379 | |
380 | err = led_classdev_register(&client->dev, &pca9633[i].led_cdev); | |
381 | if (err < 0) | |
382 | goto exit; | |
383 | } | |
384 | ||
385 | /* Disable LED all-call address and set normal mode */ | |
386 | i2c_smbus_write_byte_data(client, PCA9633_MODE1, 0x00); | |
387 | ||
2f73c392 PM |
388 | /* Configure output: open-drain or totem pole (push-pull) */ |
389 | if (pdata && pdata->outdrv == PCA9633_OPEN_DRAIN) | |
390 | i2c_smbus_write_byte_data(client, PCA9633_MODE2, 0x01); | |
391 | ||
75cb2e1d | 392 | /* Turn off LEDs */ |
af67384f RRD |
393 | i2c_smbus_write_byte_data(client, chip->ledout_base, 0x00); |
394 | if (chip->n_leds > 4) | |
395 | i2c_smbus_write_byte_data(client, chip->ledout_base + 1, 0x00); | |
75cb2e1d PM |
396 | |
397 | return 0; | |
398 | ||
399 | exit: | |
400 | while (i--) { | |
401 | led_classdev_unregister(&pca9633[i].led_cdev); | |
402 | cancel_work_sync(&pca9633[i].work); | |
403 | } | |
404 | ||
75cb2e1d PM |
405 | return err; |
406 | } | |
407 | ||
678e8a6b | 408 | static int pca9633_remove(struct i2c_client *client) |
75cb2e1d PM |
409 | { |
410 | struct pca9633_led *pca9633 = i2c_get_clientdata(client); | |
411 | int i; | |
412 | ||
af67384f RRD |
413 | for (i = 0; i < pca9633->chipdef->n_leds; i++) |
414 | if (pca9633[i].client != NULL) { | |
415 | led_classdev_unregister(&pca9633[i].led_cdev); | |
416 | cancel_work_sync(&pca9633[i].work); | |
417 | } | |
75cb2e1d | 418 | |
75cb2e1d PM |
419 | return 0; |
420 | } | |
421 | ||
422 | static struct i2c_driver pca9633_driver = { | |
423 | .driver = { | |
424 | .name = "leds-pca9633", | |
425 | .owner = THIS_MODULE, | |
81d22878 | 426 | .of_match_table = of_match_ptr(of_pca9633_match), |
75cb2e1d PM |
427 | }, |
428 | .probe = pca9633_probe, | |
df07cf81 | 429 | .remove = pca9633_remove, |
75cb2e1d PM |
430 | .id_table = pca9633_id, |
431 | }; | |
432 | ||
433 | module_i2c_driver(pca9633_driver); | |
434 | ||
435 | MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); | |
436 | MODULE_DESCRIPTION("PCA9633 LED driver"); | |
437 | MODULE_LICENSE("GPL v2"); |