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