fix race condition in cor_announce_send_stop
[cor.git] / drivers / leds / leds-pca963x.c
blob4afc317901a89178631766076e179209b983ad60
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2011 bct electronic GmbH
4 * Copyright 2013 Qtechnology/AS
6 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
7 * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
9 * Based on leds-pca955x.c
11 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
12 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
14 * Note that hardware blinking violates the leds infrastructure driver
15 * interface since the hardware only supports blinking all LEDs with the
16 * same delay_on/delay_off rates. That is, only the LEDs that are set to
17 * blink will actually blink but all LEDs that are set to blink will blink
18 * in identical fashion. The delay_on/delay_off values of the last LED
19 * that is set to blink will be used for all of the blinking LEDs.
20 * Hardware blinking is disabled by default but can be enabled by setting
21 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
22 * or by adding the 'nxp,hw-blink' property to the DTS.
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>
37 /* LED select registers determine the source that drives LED outputs */
38 #define PCA963X_LED_OFF 0x0 /* LED driver off */
39 #define PCA963X_LED_ON 0x1 /* LED driver on */
40 #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
41 #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
43 #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
45 #define PCA963X_MODE1 0x00
46 #define PCA963X_MODE2 0x01
47 #define PCA963X_PWM_BASE 0x02
49 enum pca963x_type {
50 pca9633,
51 pca9634,
52 pca9635,
55 struct pca963x_chipdef {
56 u8 grppwm;
57 u8 grpfreq;
58 u8 ledout_base;
59 int n_leds;
60 unsigned int scaling;
63 static struct pca963x_chipdef pca963x_chipdefs[] = {
64 [pca9633] = {
65 .grppwm = 0x6,
66 .grpfreq = 0x7,
67 .ledout_base = 0x8,
68 .n_leds = 4,
70 [pca9634] = {
71 .grppwm = 0xa,
72 .grpfreq = 0xb,
73 .ledout_base = 0xc,
74 .n_leds = 8,
76 [pca9635] = {
77 .grppwm = 0x12,
78 .grpfreq = 0x13,
79 .ledout_base = 0x14,
80 .n_leds = 16,
84 /* Total blink period in milliseconds */
85 #define PCA963X_BLINK_PERIOD_MIN 42
86 #define PCA963X_BLINK_PERIOD_MAX 10667
88 static const struct i2c_device_id pca963x_id[] = {
89 { "pca9632", pca9633 },
90 { "pca9633", pca9633 },
91 { "pca9634", pca9634 },
92 { "pca9635", pca9635 },
93 { }
95 MODULE_DEVICE_TABLE(i2c, pca963x_id);
97 struct pca963x_led;
99 struct 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;
107 struct pca963x_led {
108 struct pca963x *chip;
109 struct led_classdev led_cdev;
110 int led_num; /* 0 .. 15 potentially */
111 char name[32];
112 u8 gdc;
113 u8 gfrq;
116 static int pca963x_brightness(struct pca963x_led *pca963x,
117 enum led_brightness brightness)
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;
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;
149 return ret;
152 static void pca963x_blink(struct pca963x_led *pca963x)
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;
162 i2c_smbus_write_byte_data(pca963x->chip->client,
163 pca963x->chip->chipdef->grppwm, pca963x->gdc);
165 i2c_smbus_write_byte_data(pca963x->chip->client,
166 pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
168 if (!(mode2 & PCA963X_MODE2_DMBLNK))
169 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
170 mode2 | PCA963X_MODE2_DMBLNK);
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);
180 static int pca963x_power_state(struct pca963x_led *pca963x)
182 unsigned long *leds_on = &pca963x->chip->leds_on;
183 unsigned long cached_leds = pca963x->chip->leds_on;
185 if (pca963x->led_cdev.brightness)
186 set_bit(pca963x->led_num, leds_on);
187 else
188 clear_bit(pca963x->led_num, leds_on);
190 if (!(*leds_on) != !cached_leds)
191 return i2c_smbus_write_byte_data(pca963x->chip->client,
192 PCA963X_MODE1, *leds_on ? 0 : BIT(4));
194 return 0;
197 static int pca963x_led_set(struct led_classdev *led_cdev,
198 enum led_brightness value)
200 struct pca963x_led *pca963x;
201 int ret;
203 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
205 mutex_lock(&pca963x->chip->mutex);
207 ret = pca963x_brightness(pca963x, value);
208 if (ret < 0)
209 goto unlock;
210 ret = pca963x_power_state(pca963x);
212 unlock:
213 mutex_unlock(&pca963x->chip->mutex);
214 return ret;
217 static unsigned int pca963x_period_scale(struct pca963x_led *pca963x,
218 unsigned int val)
220 unsigned int scaling = pca963x->chip->chipdef->scaling;
222 return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
225 static int pca963x_blink_set(struct led_classdev *led_cdev,
226 unsigned long *delay_on, unsigned long *delay_off)
228 struct pca963x_led *pca963x;
229 unsigned long time_on, time_off, period;
230 u8 gdc, gfrq;
232 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
234 time_on = *delay_on;
235 time_off = *delay_off;
237 /* If both zero, pick reasonable defaults of 500ms each */
238 if (!time_on && !time_off) {
239 time_on = 500;
240 time_off = 500;
243 period = pca963x_period_scale(pca963x, time_on + time_off);
245 /* If period not supported by hardware, default to someting sane. */
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);
254 * From manual: duty cycle = (GDC / 256) ->
255 * (time_on / period) = (GDC / 256) ->
256 * GDC = ((time_on * 256) / period)
258 gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
261 * From manual: period = ((GFRQ + 1) / 24) in seconds.
262 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
263 * GFRQ = ((period * 24 / 1000) - 1)
265 gfrq = (period * 24 / 1000) - 1;
267 pca963x->gdc = gdc;
268 pca963x->gfrq = gfrq;
270 pca963x_blink(pca963x);
272 *delay_on = time_on;
273 *delay_off = time_off;
275 return 0;
278 static struct pca963x_platform_data *
279 pca963x_get_pdata(struct i2c_client *client, struct pca963x_chipdef *chip)
281 struct pca963x_platform_data *pdata;
282 struct led_info *pca963x_leds;
283 struct fwnode_handle *child;
284 int count;
286 count = device_get_child_node_count(&client->dev);
287 if (!count || count > chip->n_leds)
288 return ERR_PTR(-ENODEV);
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);
295 device_for_each_child_node(&client->dev, child) {
296 struct led_info led = {};
297 u32 reg;
298 int res;
300 res = fwnode_property_read_u32(child, "reg", &reg);
301 if ((res != 0) || (reg >= chip->n_leds))
302 continue;
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;
308 fwnode_property_read_string(child, "linux,default-trigger",
309 &led.default_trigger);
311 pca963x_leds[reg] = led;
313 pdata = devm_kzalloc(&client->dev,
314 sizeof(struct pca963x_platform_data), GFP_KERNEL);
315 if (!pdata)
316 return ERR_PTR(-ENOMEM);
318 pdata->leds.leds = pca963x_leds;
319 pdata->leds.num_leds = chip->n_leds;
321 /* default to open-drain unless totem pole (push-pull) is specified */
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;
327 /* default to software blinking unless hardware blinking is specified */
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;
333 if (device_property_read_u32(&client->dev, "nxp,period-scale",
334 &chip->scaling))
335 chip->scaling = 1000;
337 /* default to non-inverted output, unless inverted is specified */
338 if (device_property_read_bool(&client->dev, "nxp,inverted-out"))
339 pdata->dir = PCA963X_INVERTED;
340 else
341 pdata->dir = PCA963X_NORMAL;
343 return pdata;
346 static const struct of_device_id of_pca963x_match[] = {
347 { .compatible = "nxp,pca9632", },
348 { .compatible = "nxp,pca9633", },
349 { .compatible = "nxp,pca9634", },
350 { .compatible = "nxp,pca9635", },
353 MODULE_DEVICE_TABLE(of, of_pca963x_match);
355 static int pca963x_probe(struct i2c_client *client,
356 const struct i2c_device_id *id)
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;
364 chip = &pca963x_chipdefs[id->driver_data];
365 pdata = dev_get_platdata(&client->dev);
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;
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;
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;
391 i2c_set_clientdata(client, pca963x_chip);
393 mutex_init(&pca963x_chip->mutex);
394 pca963x_chip->chipdef = chip;
395 pca963x_chip->client = client;
396 pca963x_chip->leds = pca963x;
398 /* Turn off LEDs by default*/
399 for (i = 0; i < chip->n_leds / 4; i++)
400 i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
402 for (i = 0; i < chip->n_leds; i++) {
403 pca963x[i].led_num = i;
404 pca963x[i].chip = pca963x_chip;
406 /* Platform data can specify LED names and default triggers */
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;
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);
422 pca963x[i].led_cdev.name = pca963x[i].name;
423 pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
425 if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
426 pca963x[i].led_cdev.blink_set = pca963x_blink_set;
428 err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
429 if (err < 0)
430 goto exit;
433 /* Disable LED all-call address, and power down initially */
434 i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
436 if (pdata) {
437 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
438 PCA963X_MODE2);
439 /* Configure output: open-drain or totem pole (push-pull) */
440 if (pdata->outdrv == PCA963X_OPEN_DRAIN)
441 mode2 |= 0x01;
442 else
443 mode2 |= 0x05;
444 /* Configure direction: normal or inverted */
445 if (pdata->dir == PCA963X_INVERTED)
446 mode2 |= 0x10;
447 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
448 mode2);
451 return 0;
453 exit:
454 while (i--)
455 led_classdev_unregister(&pca963x[i].led_cdev);
457 return err;
460 static int pca963x_remove(struct i2c_client *client)
462 struct pca963x *pca963x = i2c_get_clientdata(client);
463 int i;
465 for (i = 0; i < pca963x->chipdef->n_leds; i++)
466 led_classdev_unregister(&pca963x->leds[i].led_cdev);
468 return 0;
471 static struct i2c_driver pca963x_driver = {
472 .driver = {
473 .name = "leds-pca963x",
474 .of_match_table = of_pca963x_match,
476 .probe = pca963x_probe,
477 .remove = pca963x_remove,
478 .id_table = pca963x_id,
481 module_i2c_driver(pca963x_driver);
483 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
484 MODULE_DESCRIPTION("PCA963X LED driver");
485 MODULE_LICENSE("GPL v2");