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
36
37#define PCA963X_LED_OFF 0x0
38#define PCA963X_LED_ON 0x1
39#define PCA963X_LED_PWM 0x2
40#define PCA963X_LED_GRP_PWM 0x3
41
42#define PCA963X_MODE2_OUTDRV 0x04
43#define PCA963X_MODE2_INVRT 0x10
44#define PCA963X_MODE2_DMBLNK 0x20
45
46#define PCA963X_MODE1 0x00
47#define PCA963X_MODE2 0x01
48#define PCA963X_PWM_BASE 0x02
49
50enum pca963x_type {
51 pca9633,
52 pca9634,
53 pca9635,
54};
55
56struct pca963x_chipdef {
57 u8 grppwm;
58 u8 grpfreq;
59 u8 ledout_base;
60 int n_leds;
61 unsigned int scaling;
62};
63
64static struct pca963x_chipdef pca963x_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 [pca9635] = {
78 .grppwm = 0x12,
79 .grpfreq = 0x13,
80 .ledout_base = 0x14,
81 .n_leds = 16,
82 },
83};
84
85
86#define PCA963X_BLINK_PERIOD_MIN 42
87#define PCA963X_BLINK_PERIOD_MAX 10667
88
89static const struct i2c_device_id pca963x_id[] = {
90 { "pca9632", pca9633 },
91 { "pca9633", pca9633 },
92 { "pca9634", pca9634 },
93 { "pca9635", pca9635 },
94 { }
95};
96MODULE_DEVICE_TABLE(i2c, pca963x_id);
97
98struct pca963x;
99
100struct pca963x_led {
101 struct pca963x *chip;
102 struct led_classdev led_cdev;
103 int led_num;
104 u8 gdc;
105 u8 gfrq;
106};
107
108struct pca963x {
109 struct pca963x_chipdef *chipdef;
110 struct mutex mutex;
111 struct i2c_client *client;
112 unsigned long leds_on;
113 struct pca963x_led leds[];
114};
115
116static int pca963x_brightness(struct pca963x_led *led,
117 enum led_brightness brightness)
118{
119 struct i2c_client *client = led->chip->client;
120 struct pca963x_chipdef *chipdef = led->chip->chipdef;
121 u8 ledout_addr, ledout, mask, val;
122 int shift;
123 int ret;
124
125 ledout_addr = chipdef->ledout_base + (led->led_num / 4);
126 shift = 2 * (led->led_num % 4);
127 mask = 0x3 << shift;
128 ledout = i2c_smbus_read_byte_data(client, ledout_addr);
129
130 switch (brightness) {
131 case LED_FULL:
132 val = (ledout & ~mask) | (PCA963X_LED_ON << shift);
133 ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
134 break;
135 case LED_OFF:
136 val = ledout & ~mask;
137 ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
138 break;
139 default:
140 ret = i2c_smbus_write_byte_data(client,
141 PCA963X_PWM_BASE +
142 led->led_num,
143 brightness);
144 if (ret < 0)
145 return ret;
146
147 val = (ledout & ~mask) | (PCA963X_LED_PWM << shift);
148 ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
149 break;
150 }
151
152 return ret;
153}
154
155static void pca963x_blink(struct pca963x_led *led)
156{
157 struct i2c_client *client = led->chip->client;
158 struct pca963x_chipdef *chipdef = led->chip->chipdef;
159 u8 ledout_addr, ledout, mask, val, mode2;
160 int shift;
161
162 ledout_addr = chipdef->ledout_base + (led->led_num / 4);
163 shift = 2 * (led->led_num % 4);
164 mask = 0x3 << shift;
165 mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
166
167 i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc);
168
169 i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq);
170
171 if (!(mode2 & PCA963X_MODE2_DMBLNK))
172 i2c_smbus_write_byte_data(client, PCA963X_MODE2,
173 mode2 | PCA963X_MODE2_DMBLNK);
174
175 mutex_lock(&led->chip->mutex);
176
177 ledout = i2c_smbus_read_byte_data(client, ledout_addr);
178 if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) {
179 val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
180 i2c_smbus_write_byte_data(client, ledout_addr, val);
181 }
182
183 mutex_unlock(&led->chip->mutex);
184}
185
186static int pca963x_power_state(struct pca963x_led *led)
187{
188 struct i2c_client *client = led->chip->client;
189 unsigned long *leds_on = &led->chip->leds_on;
190 unsigned long cached_leds = *leds_on;
191
192 if (led->led_cdev.brightness)
193 set_bit(led->led_num, leds_on);
194 else
195 clear_bit(led->led_num, leds_on);
196
197 if (!(*leds_on) != !cached_leds)
198 return i2c_smbus_write_byte_data(client, PCA963X_MODE1,
199 *leds_on ? 0 : BIT(4));
200
201 return 0;
202}
203
204static int pca963x_led_set(struct led_classdev *led_cdev,
205 enum led_brightness value)
206{
207 struct pca963x_led *led;
208 int ret;
209
210 led = container_of(led_cdev, struct pca963x_led, led_cdev);
211
212 mutex_lock(&led->chip->mutex);
213
214 ret = pca963x_brightness(led, value);
215 if (ret < 0)
216 goto unlock;
217 ret = pca963x_power_state(led);
218
219unlock:
220 mutex_unlock(&led->chip->mutex);
221 return ret;
222}
223
224static unsigned int pca963x_period_scale(struct pca963x_led *led,
225 unsigned int val)
226{
227 unsigned int scaling = led->chip->chipdef->scaling;
228
229 return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
230}
231
232static int pca963x_blink_set(struct led_classdev *led_cdev,
233 unsigned long *delay_on, unsigned long *delay_off)
234{
235 unsigned long time_on, time_off, period;
236 struct pca963x_led *led;
237 u8 gdc, gfrq;
238
239 led = container_of(led_cdev, struct pca963x_led, led_cdev);
240
241 time_on = *delay_on;
242 time_off = *delay_off;
243
244
245 if (!time_on && !time_off) {
246 time_on = 500;
247 time_off = 500;
248 }
249
250 period = pca963x_period_scale(led, time_on + time_off);
251
252
253 if ((period < PCA963X_BLINK_PERIOD_MIN) ||
254 (period > PCA963X_BLINK_PERIOD_MAX)) {
255 time_on = 500;
256 time_off = 500;
257 period = pca963x_period_scale(led, 1000);
258 }
259
260
261
262
263
264
265 gdc = (pca963x_period_scale(led, time_on) * 256) / period;
266
267
268
269
270
271
272 gfrq = (period * 24 / 1000) - 1;
273
274 led->gdc = gdc;
275 led->gfrq = gfrq;
276
277 pca963x_blink(led);
278
279 *delay_on = time_on;
280 *delay_off = time_off;
281
282 return 0;
283}
284
285static int pca963x_register_leds(struct i2c_client *client,
286 struct pca963x *chip)
287{
288 struct pca963x_chipdef *chipdef = chip->chipdef;
289 struct pca963x_led *led = chip->leds;
290 struct device *dev = &client->dev;
291 struct fwnode_handle *child;
292 bool hw_blink;
293 s32 mode2;
294 u32 reg;
295 int ret;
296
297 if (device_property_read_u32(dev, "nxp,period-scale",
298 &chipdef->scaling))
299 chipdef->scaling = 1000;
300
301 hw_blink = device_property_read_bool(dev, "nxp,hw-blink");
302
303 mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
304 if (mode2 < 0)
305 return mode2;
306
307
308 if (device_property_read_bool(dev, "nxp,totem-pole"))
309 mode2 |= PCA963X_MODE2_OUTDRV;
310 else
311 mode2 &= ~PCA963X_MODE2_OUTDRV;
312
313
314 if (device_property_read_bool(dev, "nxp,inverted-out"))
315 mode2 |= PCA963X_MODE2_INVRT;
316 else
317 mode2 &= ~PCA963X_MODE2_INVRT;
318
319 ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2);
320 if (ret < 0)
321 return ret;
322
323 device_for_each_child_node(dev, child) {
324 struct led_init_data init_data = {};
325 char default_label[32];
326
327 ret = fwnode_property_read_u32(child, "reg", ®);
328 if (ret || reg >= chipdef->n_leds) {
329 dev_err(dev, "Invalid 'reg' property for node %pfw\n",
330 child);
331 ret = -EINVAL;
332 goto err;
333 }
334
335 led->led_num = reg;
336 led->chip = chip;
337 led->led_cdev.brightness_set_blocking = pca963x_led_set;
338 if (hw_blink)
339 led->led_cdev.blink_set = pca963x_blink_set;
340
341 init_data.fwnode = child;
342
343 init_data.devicename = "pca963x";
344 snprintf(default_label, sizeof(default_label), "%d:%.2x:%u",
345 client->adapter->nr, client->addr, reg);
346 init_data.default_label = default_label;
347
348 ret = devm_led_classdev_register_ext(dev, &led->led_cdev,
349 &init_data);
350 if (ret) {
351 dev_err(dev, "Failed to register LED for node %pfw\n",
352 child);
353 goto err;
354 }
355
356 ++led;
357 }
358
359 return 0;
360err:
361 fwnode_handle_put(child);
362 return ret;
363}
364
365static const struct of_device_id of_pca963x_match[] = {
366 { .compatible = "nxp,pca9632", },
367 { .compatible = "nxp,pca9633", },
368 { .compatible = "nxp,pca9634", },
369 { .compatible = "nxp,pca9635", },
370 {},
371};
372MODULE_DEVICE_TABLE(of, of_pca963x_match);
373
374static int pca963x_probe(struct i2c_client *client,
375 const struct i2c_device_id *id)
376{
377 struct device *dev = &client->dev;
378 struct pca963x_chipdef *chipdef;
379 struct pca963x *chip;
380 int i, count;
381
382 chipdef = &pca963x_chipdefs[id->driver_data];
383
384 count = device_get_child_node_count(dev);
385 if (!count || count > chipdef->n_leds) {
386 dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n",
387 dev_fwnode(dev), chipdef->n_leds);
388 return -EINVAL;
389 }
390
391 chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
392 if (!chip)
393 return -ENOMEM;
394
395 i2c_set_clientdata(client, chip);
396
397 mutex_init(&chip->mutex);
398 chip->chipdef = chipdef;
399 chip->client = client;
400
401
402 for (i = 0; i < chipdef->n_leds / 4; i++)
403 i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00);
404
405
406 i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
407
408 return pca963x_register_leds(client, chip);
409}
410
411static struct i2c_driver pca963x_driver = {
412 .driver = {
413 .name = "leds-pca963x",
414 .of_match_table = of_pca963x_match,
415 },
416 .probe = pca963x_probe,
417 .id_table = pca963x_id,
418};
419
420module_i2c_driver(pca963x_driver);
421
422MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
423MODULE_DESCRIPTION("PCA963X LED driver");
424MODULE_LICENSE("GPL v2");
425