1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/delay.h>
27#include <linux/string.h>
28#include <linux/ctype.h>
29#include <linux/leds.h>
30#include <linux/err.h>
31#include <linux/i2c.h>
32#include <linux/property.h>
33#include <linux/slab.h>
34#include <linux/of.h>
35#include <linux/platform_data/leds-pca963x.h>
36
37
38#define PCA963X_LED_OFF 0x0
39#define PCA963X_LED_ON 0x1
40#define PCA963X_LED_PWM 0x2
41#define PCA963X_LED_GRP_PWM 0x3
42
43#define PCA963X_MODE2_DMBLNK 0x20
44
45#define PCA963X_MODE1 0x00
46#define PCA963X_MODE2 0x01
47#define PCA963X_PWM_BASE 0x02
48
49enum pca963x_type {
50 pca9633,
51 pca9634,
52 pca9635,
53};
54
55struct pca963x_chipdef {
56 u8 grppwm;
57 u8 grpfreq;
58 u8 ledout_base;
59 int n_leds;
60 unsigned int scaling;
61};
62
63static struct pca963x_chipdef pca963x_chipdefs[] = {
64 [pca9633] = {
65 .grppwm = 0x6,
66 .grpfreq = 0x7,
67 .ledout_base = 0x8,
68 .n_leds = 4,
69 },
70 [pca9634] = {
71 .grppwm = 0xa,
72 .grpfreq = 0xb,
73 .ledout_base = 0xc,
74 .n_leds = 8,
75 },
76 [pca9635] = {
77 .grppwm = 0x12,
78 .grpfreq = 0x13,
79 .ledout_base = 0x14,
80 .n_leds = 16,
81 },
82};
83
84
85#define PCA963X_BLINK_PERIOD_MIN 42
86#define PCA963X_BLINK_PERIOD_MAX 10667
87
88static const struct i2c_device_id pca963x_id[] = {
89 { "pca9632", pca9633 },
90 { "pca9633", pca9633 },
91 { "pca9634", pca9634 },
92 { "pca9635", pca9635 },
93 { }
94};
95MODULE_DEVICE_TABLE(i2c, pca963x_id);
96
97struct pca963x_led;
98
99struct pca963x {
100 struct pca963x_chipdef *chipdef;
101 struct mutex mutex;
102 struct i2c_client *client;
103 struct pca963x_led *leds;
104 unsigned long leds_on;
105};
106
107struct pca963x_led {
108 struct pca963x *chip;
109 struct led_classdev led_cdev;
110 int led_num;
111 char name[32];
112 u8 gdc;
113 u8 gfrq;
114};
115
116static int pca963x_brightness(struct pca963x_led *pca963x,
117 enum led_brightness brightness)
118{
119 u8 ledout_addr = pca963x->chip->chipdef->ledout_base
120 + (pca963x->led_num / 4);
121 u8 ledout;
122 int shift = 2 * (pca963x->led_num % 4);
123 u8 mask = 0x3 << shift;
124 int ret;
125
126 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
127 switch (brightness) {
128 case LED_FULL:
129 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
130 ledout_addr,
131 (ledout & ~mask) | (PCA963X_LED_ON << shift));
132 break;
133 case LED_OFF:
134 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
135 ledout_addr, ledout & ~mask);
136 break;
137 default:
138 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
139 PCA963X_PWM_BASE + pca963x->led_num,
140 brightness);
141 if (ret < 0)
142 return ret;
143 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
144 ledout_addr,
145 (ledout & ~mask) | (PCA963X_LED_PWM << shift));
146 break;
147 }
148
149 return ret;
150}
151
152static void pca963x_blink(struct pca963x_led *pca963x)
153{
154 u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
155 (pca963x->led_num / 4);
156 u8 ledout;
157 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
158 PCA963X_MODE2);
159 int shift = 2 * (pca963x->led_num % 4);
160 u8 mask = 0x3 << shift;
161
162 i2c_smbus_write_byte_data(pca963x->chip->client,
163 pca963x->chip->chipdef->grppwm, pca963x->gdc);
164
165 i2c_smbus_write_byte_data(pca963x->chip->client,
166 pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
167
168 if (!(mode2 & PCA963X_MODE2_DMBLNK))
169 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
170 mode2 | PCA963X_MODE2_DMBLNK);
171
172 mutex_lock(&pca963x->chip->mutex);
173 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
174 if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift))
175 i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
176 (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift));
177 mutex_unlock(&pca963x->chip->mutex);
178}
179
180static int pca963x_power_state(struct pca963x_led *pca963x)
181{
182 unsigned long *leds_on = &pca963x->chip->leds_on;
183 unsigned long cached_leds = pca963x->chip->leds_on;
184
185 if (pca963x->led_cdev.brightness)
186 set_bit(pca963x->led_num, leds_on);
187 else
188 clear_bit(pca963x->led_num, leds_on);
189
190 if (!(*leds_on) != !cached_leds)
191 return i2c_smbus_write_byte_data(pca963x->chip->client,
192 PCA963X_MODE1, *leds_on ? 0 : BIT(4));
193
194 return 0;
195}
196
197static int pca963x_led_set(struct led_classdev *led_cdev,
198 enum led_brightness value)
199{
200 struct pca963x_led *pca963x;
201 int ret;
202
203 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
204
205 mutex_lock(&pca963x->chip->mutex);
206
207 ret = pca963x_brightness(pca963x, value);
208 if (ret < 0)
209 goto unlock;
210 ret = pca963x_power_state(pca963x);
211
212unlock:
213 mutex_unlock(&pca963x->chip->mutex);
214 return ret;
215}
216
217static unsigned int pca963x_period_scale(struct pca963x_led *pca963x,
218 unsigned int val)
219{
220 unsigned int scaling = pca963x->chip->chipdef->scaling;
221
222 return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
223}
224
225static int pca963x_blink_set(struct led_classdev *led_cdev,
226 unsigned long *delay_on, unsigned long *delay_off)
227{
228 struct pca963x_led *pca963x;
229 unsigned long time_on, time_off, period;
230 u8 gdc, gfrq;
231
232 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
233
234 time_on = *delay_on;
235 time_off = *delay_off;
236
237
238 if (!time_on && !time_off) {
239 time_on = 500;
240 time_off = 500;
241 }
242
243 period = pca963x_period_scale(pca963x, time_on + time_off);
244
245
246 if ((period < PCA963X_BLINK_PERIOD_MIN) ||
247 (period > PCA963X_BLINK_PERIOD_MAX)) {
248 time_on = 500;
249 time_off = 500;
250 period = pca963x_period_scale(pca963x, 1000);
251 }
252
253
254
255
256
257
258 gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
259
260
261
262
263
264
265 gfrq = (period * 24 / 1000) - 1;
266
267 pca963x->gdc = gdc;
268 pca963x->gfrq = gfrq;
269
270 pca963x_blink(pca963x);
271
272 *delay_on = time_on;
273 *delay_off = time_off;
274
275 return 0;
276}
277
278static struct pca963x_platform_data *
279pca963x_get_pdata(struct i2c_client *client, struct pca963x_chipdef *chip)
280{
281 struct pca963x_platform_data *pdata;
282 struct led_info *pca963x_leds;
283 struct fwnode_handle *child;
284 int count;
285
286 count = device_get_child_node_count(&client->dev);
287 if (!count || count > chip->n_leds)
288 return ERR_PTR(-ENODEV);
289
290 pca963x_leds = devm_kcalloc(&client->dev,
291 chip->n_leds, sizeof(struct led_info), GFP_KERNEL);
292 if (!pca963x_leds)
293 return ERR_PTR(-ENOMEM);
294
295 device_for_each_child_node(&client->dev, child) {
296 struct led_info led = {};
297 u32 reg;
298 int res;
299
300 res = fwnode_property_read_u32(child, "reg", ®);
301 if ((res != 0) || (reg >= chip->n_leds))
302 continue;
303
304 res = fwnode_property_read_string(child, "label", &led.name);
305 if ((res != 0) && is_of_node(child))
306 led.name = to_of_node(child)->name;
307
308 fwnode_property_read_string(child, "linux,default-trigger",
309 &led.default_trigger);
310
311 pca963x_leds[reg] = led;
312 }
313 pdata = devm_kzalloc(&client->dev,
314 sizeof(struct pca963x_platform_data), GFP_KERNEL);
315 if (!pdata)
316 return ERR_PTR(-ENOMEM);
317
318 pdata->leds.leds = pca963x_leds;
319 pdata->leds.num_leds = chip->n_leds;
320
321
322 if (device_property_read_bool(&client->dev, "nxp,totem-pole"))
323 pdata->outdrv = PCA963X_TOTEM_POLE;
324 else
325 pdata->outdrv = PCA963X_OPEN_DRAIN;
326
327
328 if (device_property_read_bool(&client->dev, "nxp,hw-blink"))
329 pdata->blink_type = PCA963X_HW_BLINK;
330 else
331 pdata->blink_type = PCA963X_SW_BLINK;
332
333 if (device_property_read_u32(&client->dev, "nxp,period-scale",
334 &chip->scaling))
335 chip->scaling = 1000;
336
337
338 if (device_property_read_bool(&client->dev, "nxp,inverted-out"))
339 pdata->dir = PCA963X_INVERTED;
340 else
341 pdata->dir = PCA963X_NORMAL;
342
343 return pdata;
344}
345
346static const struct of_device_id of_pca963x_match[] = {
347 { .compatible = "nxp,pca9632", },
348 { .compatible = "nxp,pca9633", },
349 { .compatible = "nxp,pca9634", },
350 { .compatible = "nxp,pca9635", },
351 {},
352};
353MODULE_DEVICE_TABLE(of, of_pca963x_match);
354
355static int pca963x_probe(struct i2c_client *client,
356 const struct i2c_device_id *id)
357{
358 struct pca963x *pca963x_chip;
359 struct pca963x_led *pca963x;
360 struct pca963x_platform_data *pdata;
361 struct pca963x_chipdef *chip;
362 int i, err;
363
364 chip = &pca963x_chipdefs[id->driver_data];
365 pdata = dev_get_platdata(&client->dev);
366
367 if (!pdata) {
368 pdata = pca963x_get_pdata(client, chip);
369 if (IS_ERR(pdata)) {
370 dev_warn(&client->dev, "could not parse configuration\n");
371 pdata = NULL;
372 }
373 }
374
375 if (pdata && (pdata->leds.num_leds < 1 ||
376 pdata->leds.num_leds > chip->n_leds)) {
377 dev_err(&client->dev, "board info must claim 1-%d LEDs",
378 chip->n_leds);
379 return -EINVAL;
380 }
381
382 pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
383 GFP_KERNEL);
384 if (!pca963x_chip)
385 return -ENOMEM;
386 pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x),
387 GFP_KERNEL);
388 if (!pca963x)
389 return -ENOMEM;
390
391 i2c_set_clientdata(client, pca963x_chip);
392
393 mutex_init(&pca963x_chip->mutex);
394 pca963x_chip->chipdef = chip;
395 pca963x_chip->client = client;
396 pca963x_chip->leds = pca963x;
397
398
399 for (i = 0; i < chip->n_leds / 4; i++)
400 i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
401
402 for (i = 0; i < chip->n_leds; i++) {
403 pca963x[i].led_num = i;
404 pca963x[i].chip = pca963x_chip;
405
406
407 if (pdata && i < pdata->leds.num_leds) {
408 if (pdata->leds.leds[i].name)
409 snprintf(pca963x[i].name,
410 sizeof(pca963x[i].name), "pca963x:%s",
411 pdata->leds.leds[i].name);
412 if (pdata->leds.leds[i].default_trigger)
413 pca963x[i].led_cdev.default_trigger =
414 pdata->leds.leds[i].default_trigger;
415 }
416 if (!pdata || i >= pdata->leds.num_leds ||
417 !pdata->leds.leds[i].name)
418 snprintf(pca963x[i].name, sizeof(pca963x[i].name),
419 "pca963x:%d:%.2x:%d", client->adapter->nr,
420 client->addr, i);
421
422 pca963x[i].led_cdev.name = pca963x[i].name;
423 pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
424
425 if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
426 pca963x[i].led_cdev.blink_set = pca963x_blink_set;
427
428 err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
429 if (err < 0)
430 goto exit;
431 }
432
433
434 i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
435
436 if (pdata) {
437 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
438 PCA963X_MODE2);
439
440 if (pdata->outdrv == PCA963X_OPEN_DRAIN)
441 mode2 |= 0x01;
442 else
443 mode2 |= 0x05;
444
445 if (pdata->dir == PCA963X_INVERTED)
446 mode2 |= 0x10;
447 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
448 mode2);
449 }
450
451 return 0;
452
453exit:
454 while (i--)
455 led_classdev_unregister(&pca963x[i].led_cdev);
456
457 return err;
458}
459
460static int pca963x_remove(struct i2c_client *client)
461{
462 struct pca963x *pca963x = i2c_get_clientdata(client);
463 int i;
464
465 for (i = 0; i < pca963x->chipdef->n_leds; i++)
466 led_classdev_unregister(&pca963x->leds[i].led_cdev);
467
468 return 0;
469}
470
471static struct i2c_driver pca963x_driver = {
472 .driver = {
473 .name = "leds-pca963x",
474 .of_match_table = of_pca963x_match,
475 },
476 .probe = pca963x_probe,
477 .remove = pca963x_remove,
478 .id_table = pca963x_id,
479};
480
481module_i2c_driver(pca963x_driver);
482
483MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
484MODULE_DESCRIPTION("PCA963X LED driver");
485MODULE_LICENSE("GPL v2");
486