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 | ||
a7d0e988 RRD |
96 | struct pca9633_led; |
97 | ||
98 | struct pca9633 { | |
af67384f | 99 | struct pca9633_chipdef *chipdef; |
a7d0e988 RRD |
100 | struct mutex mutex; |
101 | struct i2c_client *client; | |
102 | struct pca9633_led *leds; | |
103 | }; | |
104 | ||
105 | struct pca9633_led { | |
106 | struct pca9633 *chip; | |
75cb2e1d PM |
107 | struct work_struct work; |
108 | enum led_brightness brightness; | |
109 | struct led_classdev led_cdev; | |
af67384f | 110 | int led_num; /* 0 .. 7 potentially */ |
8465b018 | 111 | enum pca9633_cmd cmd; |
75cb2e1d | 112 | char name[32]; |
8465b018 MG |
113 | u8 gdc; |
114 | u8 gfrq; | |
75cb2e1d PM |
115 | }; |
116 | ||
8465b018 | 117 | static void pca9633_brightness_work(struct pca9633_led *pca9633) |
75cb2e1d | 118 | { |
a7d0e988 RRD |
119 | u8 ledout_addr = pca9633->chip->chipdef->ledout_base |
120 | + (pca9633->led_num / 4); | |
af67384f RRD |
121 | u8 ledout; |
122 | int shift = 2 * (pca9633->led_num % 4); | |
75cb2e1d PM |
123 | u8 mask = 0x3 << shift; |
124 | ||
a7d0e988 RRD |
125 | mutex_lock(&pca9633->chip->mutex); |
126 | ledout = i2c_smbus_read_byte_data(pca9633->chip->client, ledout_addr); | |
75cb2e1d PM |
127 | switch (pca9633->brightness) { |
128 | case LED_FULL: | |
a7d0e988 | 129 | i2c_smbus_write_byte_data(pca9633->chip->client, ledout_addr, |
75cb2e1d PM |
130 | (ledout & ~mask) | (PCA9633_LED_ON << shift)); |
131 | break; | |
132 | case LED_OFF: | |
a7d0e988 | 133 | i2c_smbus_write_byte_data(pca9633->chip->client, ledout_addr, |
75cb2e1d PM |
134 | ledout & ~mask); |
135 | break; | |
136 | default: | |
a7d0e988 | 137 | i2c_smbus_write_byte_data(pca9633->chip->client, |
75cb2e1d PM |
138 | PCA9633_PWM_BASE + pca9633->led_num, |
139 | pca9633->brightness); | |
a7d0e988 | 140 | i2c_smbus_write_byte_data(pca9633->chip->client, ledout_addr, |
75cb2e1d PM |
141 | (ledout & ~mask) | (PCA9633_LED_PWM << shift)); |
142 | break; | |
143 | } | |
a7d0e988 | 144 | mutex_unlock(&pca9633->chip->mutex); |
75cb2e1d PM |
145 | } |
146 | ||
8465b018 MG |
147 | static void pca9633_blink_work(struct pca9633_led *pca9633) |
148 | { | |
a7d0e988 RRD |
149 | u8 ledout_addr = pca9633->chip->chipdef->ledout_base + |
150 | (pca9633->led_num / 4); | |
151 | u8 ledout; | |
152 | u8 mode2 = i2c_smbus_read_byte_data(pca9633->chip->client, | |
153 | PCA9633_MODE2); | |
af67384f | 154 | int shift = 2 * (pca9633->led_num % 4); |
8465b018 MG |
155 | u8 mask = 0x3 << shift; |
156 | ||
a7d0e988 RRD |
157 | i2c_smbus_write_byte_data(pca9633->chip->client, |
158 | pca9633->chip->chipdef->grppwm, pca9633->gdc); | |
8465b018 | 159 | |
a7d0e988 RRD |
160 | i2c_smbus_write_byte_data(pca9633->chip->client, |
161 | pca9633->chip->chipdef->grpfreq, pca9633->gfrq); | |
8465b018 MG |
162 | |
163 | if (!(mode2 & PCA9633_MODE2_DMBLNK)) | |
a7d0e988 | 164 | i2c_smbus_write_byte_data(pca9633->chip->client, PCA9633_MODE2, |
8465b018 MG |
165 | mode2 | PCA9633_MODE2_DMBLNK); |
166 | ||
a7d0e988 RRD |
167 | mutex_lock(&pca9633->chip->mutex); |
168 | ledout = i2c_smbus_read_byte_data(pca9633->chip->client, ledout_addr); | |
8465b018 | 169 | if ((ledout & mask) != (PCA9633_LED_GRP_PWM << shift)) |
a7d0e988 | 170 | i2c_smbus_write_byte_data(pca9633->chip->client, ledout_addr, |
8465b018 | 171 | (ledout & ~mask) | (PCA9633_LED_GRP_PWM << shift)); |
a7d0e988 | 172 | mutex_unlock(&pca9633->chip->mutex); |
8465b018 MG |
173 | } |
174 | ||
175 | static void pca9633_work(struct work_struct *work) | |
176 | { | |
177 | struct pca9633_led *pca9633 = container_of(work, | |
178 | struct pca9633_led, work); | |
179 | ||
180 | switch (pca9633->cmd) { | |
181 | case BRIGHTNESS_SET: | |
182 | pca9633_brightness_work(pca9633); | |
183 | break; | |
184 | case BLINK_SET: | |
185 | pca9633_blink_work(pca9633); | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
75cb2e1d PM |
190 | static void pca9633_led_set(struct led_classdev *led_cdev, |
191 | enum led_brightness value) | |
192 | { | |
193 | struct pca9633_led *pca9633; | |
194 | ||
195 | pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev); | |
196 | ||
8465b018 | 197 | pca9633->cmd = BRIGHTNESS_SET; |
75cb2e1d PM |
198 | pca9633->brightness = value; |
199 | ||
200 | /* | |
201 | * Must use workqueue for the actual I/O since I2C operations | |
202 | * can sleep. | |
203 | */ | |
204 | schedule_work(&pca9633->work); | |
205 | } | |
206 | ||
8465b018 MG |
207 | static int pca9633_blink_set(struct led_classdev *led_cdev, |
208 | unsigned long *delay_on, unsigned long *delay_off) | |
209 | { | |
210 | struct pca9633_led *pca9633; | |
211 | unsigned long time_on, time_off, period; | |
212 | u8 gdc, gfrq; | |
213 | ||
214 | pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev); | |
215 | ||
216 | time_on = *delay_on; | |
217 | time_off = *delay_off; | |
218 | ||
219 | /* If both zero, pick reasonable defaults of 500ms each */ | |
220 | if (!time_on && !time_off) { | |
221 | time_on = 500; | |
222 | time_off = 500; | |
223 | } | |
224 | ||
225 | period = time_on + time_off; | |
226 | ||
227 | /* If period not supported by hardware, default to someting sane. */ | |
228 | if ((period < PCA9632_BLINK_PERIOD_MIN) || | |
229 | (period > PCA9632_BLINK_PERIOD_MAX)) { | |
230 | time_on = 500; | |
231 | time_off = 500; | |
232 | period = time_on + time_off; | |
233 | } | |
234 | ||
235 | /* | |
236 | * From manual: duty cycle = (GDC / 256) -> | |
237 | * (time_on / period) = (GDC / 256) -> | |
238 | * GDC = ((time_on * 256) / period) | |
239 | */ | |
240 | gdc = (time_on * 256) / period; | |
241 | ||
242 | /* | |
243 | * From manual: period = ((GFRQ + 1) / 24) in seconds. | |
244 | * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) -> | |
245 | * GFRQ = ((period * 24 / 1000) - 1) | |
246 | */ | |
247 | gfrq = (period * 24 / 1000) - 1; | |
248 | ||
249 | pca9633->cmd = BLINK_SET; | |
250 | pca9633->gdc = gdc; | |
251 | pca9633->gfrq = gfrq; | |
252 | ||
253 | /* | |
254 | * Must use workqueue for the actual I/O since I2C operations | |
255 | * can sleep. | |
256 | */ | |
257 | schedule_work(&pca9633->work); | |
258 | ||
259 | *delay_on = time_on; | |
260 | *delay_off = time_off; | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
81d22878 TL |
265 | #if IS_ENABLED(CONFIG_OF) |
266 | static struct pca9633_platform_data * | |
af67384f | 267 | pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip) |
81d22878 TL |
268 | { |
269 | struct device_node *np = client->dev.of_node, *child; | |
270 | struct pca9633_platform_data *pdata; | |
271 | struct led_info *pca9633_leds; | |
272 | int count; | |
273 | ||
274 | count = of_get_child_count(np); | |
af67384f | 275 | if (!count || count > chip->n_leds) |
81d22878 TL |
276 | return ERR_PTR(-ENODEV); |
277 | ||
278 | pca9633_leds = devm_kzalloc(&client->dev, | |
af67384f | 279 | sizeof(struct led_info) * chip->n_leds, GFP_KERNEL); |
81d22878 TL |
280 | if (!pca9633_leds) |
281 | return ERR_PTR(-ENOMEM); | |
282 | ||
283 | for_each_child_of_node(np, child) { | |
284 | struct led_info led; | |
285 | u32 reg; | |
286 | int res; | |
287 | ||
288 | led.name = | |
289 | of_get_property(child, "label", NULL) ? : child->name; | |
290 | led.default_trigger = | |
291 | of_get_property(child, "linux,default-trigger", NULL); | |
292 | res = of_property_read_u32(child, "reg", ®); | |
293 | if (res != 0) | |
294 | continue; | |
295 | pca9633_leds[reg] = led; | |
296 | } | |
297 | pdata = devm_kzalloc(&client->dev, | |
298 | sizeof(struct pca9633_platform_data), GFP_KERNEL); | |
299 | if (!pdata) | |
300 | return ERR_PTR(-ENOMEM); | |
301 | ||
302 | pdata->leds.leds = pca9633_leds; | |
303 | pdata->leds.num_leds = count; | |
304 | ||
305 | /* default to open-drain unless totem pole (push-pull) is specified */ | |
306 | if (of_property_read_bool(np, "nxp,totem-pole")) | |
307 | pdata->outdrv = PCA9633_TOTEM_POLE; | |
308 | else | |
309 | pdata->outdrv = PCA9633_OPEN_DRAIN; | |
310 | ||
8465b018 MG |
311 | /* default to software blinking unless hardware blinking is specified */ |
312 | if (of_property_read_bool(np, "nxp,hw-blink")) | |
313 | pdata->blink_type = PCA9633_HW_BLINK; | |
314 | else | |
315 | pdata->blink_type = PCA9633_SW_BLINK; | |
316 | ||
81d22878 TL |
317 | return pdata; |
318 | } | |
319 | ||
320 | static const struct of_device_id of_pca9633_match[] = { | |
af67384f RRD |
321 | { .compatible = "nxp,pca9632", }, |
322 | { .compatible = "nxp,pca9633", }, | |
323 | { .compatible = "nxp,pca9634", }, | |
81d22878 TL |
324 | {}, |
325 | }; | |
326 | #else | |
327 | static struct pca9633_platform_data * | |
af67384f | 328 | pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip) |
81d22878 TL |
329 | { |
330 | return ERR_PTR(-ENODEV); | |
331 | } | |
332 | #endif | |
333 | ||
98ea1ea2 | 334 | static int pca9633_probe(struct i2c_client *client, |
75cb2e1d PM |
335 | const struct i2c_device_id *id) |
336 | { | |
a7d0e988 | 337 | struct pca9633 *pca9633_chip; |
75cb2e1d | 338 | struct pca9633_led *pca9633; |
2f73c392 | 339 | struct pca9633_platform_data *pdata; |
af67384f | 340 | struct pca9633_chipdef *chip; |
75cb2e1d PM |
341 | int i, err; |
342 | ||
af67384f | 343 | chip = &pca9633_chipdefs[id->driver_data]; |
87aae1ea | 344 | pdata = dev_get_platdata(&client->dev); |
75cb2e1d | 345 | |
81d22878 | 346 | if (!pdata) { |
af67384f | 347 | pdata = pca9633_dt_init(client, chip); |
81d22878 TL |
348 | if (IS_ERR(pdata)) { |
349 | dev_warn(&client->dev, "could not parse configuration\n"); | |
350 | pdata = NULL; | |
351 | } | |
352 | } | |
353 | ||
af67384f RRD |
354 | if (pdata && (pdata->leds.num_leds < 1 || |
355 | pdata->leds.num_leds > chip->n_leds)) { | |
356 | dev_err(&client->dev, "board info must claim 1-%d LEDs", | |
357 | chip->n_leds); | |
358 | return -EINVAL; | |
75cb2e1d PM |
359 | } |
360 | ||
a7d0e988 RRD |
361 | pca9633_chip = devm_kzalloc(&client->dev, sizeof(*pca9633_chip), |
362 | GFP_KERNEL); | |
363 | if (!pca9633_chip) | |
364 | return -ENOMEM; | |
af67384f RRD |
365 | pca9633 = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca9633), |
366 | GFP_KERNEL); | |
75cb2e1d PM |
367 | if (!pca9633) |
368 | return -ENOMEM; | |
369 | ||
a7d0e988 RRD |
370 | i2c_set_clientdata(client, pca9633_chip); |
371 | ||
372 | mutex_init(&pca9633_chip->mutex); | |
373 | pca9633_chip->chipdef = chip; | |
374 | pca9633_chip->client = client; | |
375 | pca9633_chip->leds = pca9633; | |
376 | ||
377 | /* Turn off LEDs by default*/ | |
378 | i2c_smbus_write_byte_data(client, chip->ledout_base, 0x00); | |
379 | if (chip->n_leds > 4) | |
380 | i2c_smbus_write_byte_data(client, chip->ledout_base + 1, 0x00); | |
75cb2e1d | 381 | |
af67384f | 382 | for (i = 0; i < chip->n_leds; i++) { |
75cb2e1d | 383 | pca9633[i].led_num = i; |
a7d0e988 | 384 | pca9633[i].chip = pca9633_chip; |
75cb2e1d PM |
385 | |
386 | /* Platform data can specify LED names and default triggers */ | |
2f73c392 PM |
387 | if (pdata && i < pdata->leds.num_leds) { |
388 | if (pdata->leds.leds[i].name) | |
75cb2e1d PM |
389 | snprintf(pca9633[i].name, |
390 | sizeof(pca9633[i].name), "pca9633:%s", | |
2f73c392 PM |
391 | pdata->leds.leds[i].name); |
392 | if (pdata->leds.leds[i].default_trigger) | |
75cb2e1d | 393 | pca9633[i].led_cdev.default_trigger = |
2f73c392 | 394 | pdata->leds.leds[i].default_trigger; |
75cb2e1d | 395 | } |
a5cd98b7 RRD |
396 | if (!pdata || i >= pdata->leds.num_leds || |
397 | !pdata->leds.leds[i].name) | |
398 | snprintf(pca9633[i].name, sizeof(pca9633[i].name), | |
399 | "pca9633:%d:%.2x:%d", client->adapter->nr, | |
400 | client->addr, i); | |
75cb2e1d PM |
401 | |
402 | pca9633[i].led_cdev.name = pca9633[i].name; | |
403 | pca9633[i].led_cdev.brightness_set = pca9633_led_set; | |
404 | ||
8465b018 MG |
405 | if (pdata && pdata->blink_type == PCA9633_HW_BLINK) |
406 | pca9633[i].led_cdev.blink_set = pca9633_blink_set; | |
407 | ||
408 | INIT_WORK(&pca9633[i].work, pca9633_work); | |
75cb2e1d PM |
409 | |
410 | err = led_classdev_register(&client->dev, &pca9633[i].led_cdev); | |
411 | if (err < 0) | |
412 | goto exit; | |
413 | } | |
414 | ||
415 | /* Disable LED all-call address and set normal mode */ | |
416 | i2c_smbus_write_byte_data(client, PCA9633_MODE1, 0x00); | |
417 | ||
2f73c392 PM |
418 | /* Configure output: open-drain or totem pole (push-pull) */ |
419 | if (pdata && pdata->outdrv == PCA9633_OPEN_DRAIN) | |
420 | i2c_smbus_write_byte_data(client, PCA9633_MODE2, 0x01); | |
421 | ||
75cb2e1d PM |
422 | return 0; |
423 | ||
424 | exit: | |
425 | while (i--) { | |
426 | led_classdev_unregister(&pca9633[i].led_cdev); | |
427 | cancel_work_sync(&pca9633[i].work); | |
428 | } | |
429 | ||
75cb2e1d PM |
430 | return err; |
431 | } | |
432 | ||
678e8a6b | 433 | static int pca9633_remove(struct i2c_client *client) |
75cb2e1d | 434 | { |
a7d0e988 | 435 | struct pca9633 *pca9633 = i2c_get_clientdata(client); |
75cb2e1d PM |
436 | int i; |
437 | ||
a7d0e988 RRD |
438 | for (i = 0; i < pca9633->chipdef->n_leds; i++) { |
439 | led_classdev_unregister(&pca9633->leds[i].led_cdev); | |
440 | cancel_work_sync(&pca9633->leds[i].work); | |
441 | } | |
75cb2e1d | 442 | |
75cb2e1d PM |
443 | return 0; |
444 | } | |
445 | ||
446 | static struct i2c_driver pca9633_driver = { | |
447 | .driver = { | |
448 | .name = "leds-pca9633", | |
449 | .owner = THIS_MODULE, | |
81d22878 | 450 | .of_match_table = of_match_ptr(of_pca9633_match), |
75cb2e1d PM |
451 | }, |
452 | .probe = pca9633_probe, | |
df07cf81 | 453 | .remove = pca9633_remove, |
75cb2e1d PM |
454 | .id_table = pca9633_id, |
455 | }; | |
456 | ||
457 | module_i2c_driver(pca9633_driver); | |
458 | ||
459 | MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); | |
460 | MODULE_DESCRIPTION("PCA9633 LED driver"); | |
461 | MODULE_LICENSE("GPL v2"); |