]>
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 | |
56a1740c | 24 | * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK' |
8465b018 | 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> |
56a1740c | 38 | #include <linux/platform_data/leds-pca963x.h> |
75cb2e1d PM |
39 | |
40 | /* LED select registers determine the source that drives LED outputs */ | |
56a1740c RR |
41 | #define PCA963X_LED_OFF 0x0 /* LED driver off */ |
42 | #define PCA963X_LED_ON 0x1 /* LED driver on */ | |
43 | #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */ | |
44 | #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */ | |
75cb2e1d | 45 | |
56a1740c | 46 | #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */ |
8465b018 | 47 | |
56a1740c RR |
48 | #define PCA963X_MODE1 0x00 |
49 | #define PCA963X_MODE2 0x01 | |
50 | #define PCA963X_PWM_BASE 0x02 | |
af67384f | 51 | |
56a1740c | 52 | enum pca963x_type { |
af67384f RR |
53 | pca9633, |
54 | pca9634, | |
55 | }; | |
56 | ||
56a1740c | 57 | struct pca963x_chipdef { |
af67384f RR |
58 | u8 grppwm; |
59 | u8 grpfreq; | |
60 | u8 ledout_base; | |
61 | int n_leds; | |
62 | }; | |
63 | ||
56a1740c | 64 | static struct pca963x_chipdef pca963x_chipdefs[] = { |
af67384f RR |
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 | 79 | /* Total blink period in milliseconds */ |
56a1740c RR |
80 | #define PCA963X_BLINK_PERIOD_MIN 42 |
81 | #define PCA963X_BLINK_PERIOD_MAX 10667 | |
8465b018 | 82 | |
56a1740c | 83 | static const struct i2c_device_id pca963x_id[] = { |
af67384f RR |
84 | { "pca9632", pca9633 }, |
85 | { "pca9633", pca9633 }, | |
86 | { "pca9634", pca9634 }, | |
75cb2e1d PM |
87 | { } |
88 | }; | |
56a1740c | 89 | MODULE_DEVICE_TABLE(i2c, pca963x_id); |
75cb2e1d | 90 | |
56a1740c | 91 | enum pca963x_cmd { |
8465b018 MG |
92 | BRIGHTNESS_SET, |
93 | BLINK_SET, | |
94 | }; | |
95 | ||
56a1740c | 96 | struct pca963x_led; |
a7d0e988 | 97 | |
56a1740c RR |
98 | struct pca963x { |
99 | struct pca963x_chipdef *chipdef; | |
a7d0e988 RR |
100 | struct mutex mutex; |
101 | struct i2c_client *client; | |
56a1740c | 102 | struct pca963x_led *leds; |
a7d0e988 RR |
103 | }; |
104 | ||
56a1740c RR |
105 | struct pca963x_led { |
106 | struct pca963x *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 */ |
56a1740c | 111 | enum pca963x_cmd cmd; |
75cb2e1d | 112 | char name[32]; |
8465b018 MG |
113 | u8 gdc; |
114 | u8 gfrq; | |
75cb2e1d PM |
115 | }; |
116 | ||
56a1740c | 117 | static void pca963x_brightness_work(struct pca963x_led *pca963x) |
75cb2e1d | 118 | { |
56a1740c RR |
119 | u8 ledout_addr = pca963x->chip->chipdef->ledout_base |
120 | + (pca963x->led_num / 4); | |
af67384f | 121 | u8 ledout; |
56a1740c | 122 | int shift = 2 * (pca963x->led_num % 4); |
75cb2e1d PM |
123 | u8 mask = 0x3 << shift; |
124 | ||
56a1740c RR |
125 | mutex_lock(&pca963x->chip->mutex); |
126 | ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); | |
127 | switch (pca963x->brightness) { | |
75cb2e1d | 128 | case LED_FULL: |
56a1740c RR |
129 | i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, |
130 | (ledout & ~mask) | (PCA963X_LED_ON << shift)); | |
75cb2e1d PM |
131 | break; |
132 | case LED_OFF: | |
56a1740c | 133 | i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, |
75cb2e1d PM |
134 | ledout & ~mask); |
135 | break; | |
136 | default: | |
56a1740c RR |
137 | i2c_smbus_write_byte_data(pca963x->chip->client, |
138 | PCA963X_PWM_BASE + pca963x->led_num, | |
139 | pca963x->brightness); | |
140 | i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, | |
141 | (ledout & ~mask) | (PCA963X_LED_PWM << shift)); | |
75cb2e1d PM |
142 | break; |
143 | } | |
56a1740c | 144 | mutex_unlock(&pca963x->chip->mutex); |
75cb2e1d PM |
145 | } |
146 | ||
56a1740c | 147 | static void pca963x_blink_work(struct pca963x_led *pca963x) |
8465b018 | 148 | { |
56a1740c RR |
149 | u8 ledout_addr = pca963x->chip->chipdef->ledout_base + |
150 | (pca963x->led_num / 4); | |
a7d0e988 | 151 | u8 ledout; |
56a1740c RR |
152 | u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, |
153 | PCA963X_MODE2); | |
154 | int shift = 2 * (pca963x->led_num % 4); | |
8465b018 MG |
155 | u8 mask = 0x3 << shift; |
156 | ||
56a1740c RR |
157 | i2c_smbus_write_byte_data(pca963x->chip->client, |
158 | pca963x->chip->chipdef->grppwm, pca963x->gdc); | |
8465b018 | 159 | |
56a1740c RR |
160 | i2c_smbus_write_byte_data(pca963x->chip->client, |
161 | pca963x->chip->chipdef->grpfreq, pca963x->gfrq); | |
8465b018 | 162 | |
56a1740c RR |
163 | if (!(mode2 & PCA963X_MODE2_DMBLNK)) |
164 | i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, | |
165 | mode2 | PCA963X_MODE2_DMBLNK); | |
8465b018 | 166 | |
56a1740c RR |
167 | mutex_lock(&pca963x->chip->mutex); |
168 | ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); | |
169 | if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) | |
170 | i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, | |
171 | (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift)); | |
172 | mutex_unlock(&pca963x->chip->mutex); | |
8465b018 MG |
173 | } |
174 | ||
56a1740c | 175 | static void pca963x_work(struct work_struct *work) |
8465b018 | 176 | { |
56a1740c RR |
177 | struct pca963x_led *pca963x = container_of(work, |
178 | struct pca963x_led, work); | |
8465b018 | 179 | |
56a1740c | 180 | switch (pca963x->cmd) { |
8465b018 | 181 | case BRIGHTNESS_SET: |
56a1740c | 182 | pca963x_brightness_work(pca963x); |
8465b018 MG |
183 | break; |
184 | case BLINK_SET: | |
56a1740c | 185 | pca963x_blink_work(pca963x); |
8465b018 MG |
186 | break; |
187 | } | |
188 | } | |
189 | ||
56a1740c | 190 | static void pca963x_led_set(struct led_classdev *led_cdev, |
75cb2e1d PM |
191 | enum led_brightness value) |
192 | { | |
56a1740c | 193 | struct pca963x_led *pca963x; |
75cb2e1d | 194 | |
56a1740c | 195 | pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
75cb2e1d | 196 | |
56a1740c RR |
197 | pca963x->cmd = BRIGHTNESS_SET; |
198 | pca963x->brightness = value; | |
75cb2e1d PM |
199 | |
200 | /* | |
201 | * Must use workqueue for the actual I/O since I2C operations | |
202 | * can sleep. | |
203 | */ | |
56a1740c | 204 | schedule_work(&pca963x->work); |
75cb2e1d PM |
205 | } |
206 | ||
56a1740c | 207 | static int pca963x_blink_set(struct led_classdev *led_cdev, |
8465b018 MG |
208 | unsigned long *delay_on, unsigned long *delay_off) |
209 | { | |
56a1740c | 210 | struct pca963x_led *pca963x; |
8465b018 MG |
211 | unsigned long time_on, time_off, period; |
212 | u8 gdc, gfrq; | |
213 | ||
56a1740c | 214 | pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
8465b018 MG |
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. */ | |
56a1740c RR |
228 | if ((period < PCA963X_BLINK_PERIOD_MIN) || |
229 | (period > PCA963X_BLINK_PERIOD_MAX)) { | |
8465b018 MG |
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 | ||
56a1740c RR |
249 | pca963x->cmd = BLINK_SET; |
250 | pca963x->gdc = gdc; | |
251 | pca963x->gfrq = gfrq; | |
8465b018 MG |
252 | |
253 | /* | |
254 | * Must use workqueue for the actual I/O since I2C operations | |
255 | * can sleep. | |
256 | */ | |
56a1740c | 257 | schedule_work(&pca963x->work); |
8465b018 MG |
258 | |
259 | *delay_on = time_on; | |
260 | *delay_off = time_off; | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
81d22878 | 265 | #if IS_ENABLED(CONFIG_OF) |
56a1740c RR |
266 | static struct pca963x_platform_data * |
267 | pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) | |
81d22878 TL |
268 | { |
269 | struct device_node *np = client->dev.of_node, *child; | |
56a1740c RR |
270 | struct pca963x_platform_data *pdata; |
271 | struct led_info *pca963x_leds; | |
81d22878 TL |
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 | ||
56a1740c | 278 | pca963x_leds = devm_kzalloc(&client->dev, |
af67384f | 279 | sizeof(struct led_info) * chip->n_leds, GFP_KERNEL); |
56a1740c | 280 | if (!pca963x_leds) |
81d22878 TL |
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; | |
56a1740c | 295 | pca963x_leds[reg] = led; |
81d22878 TL |
296 | } |
297 | pdata = devm_kzalloc(&client->dev, | |
56a1740c | 298 | sizeof(struct pca963x_platform_data), GFP_KERNEL); |
81d22878 TL |
299 | if (!pdata) |
300 | return ERR_PTR(-ENOMEM); | |
301 | ||
56a1740c | 302 | pdata->leds.leds = pca963x_leds; |
81d22878 TL |
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")) | |
56a1740c | 307 | pdata->outdrv = PCA963X_TOTEM_POLE; |
81d22878 | 308 | else |
56a1740c | 309 | pdata->outdrv = PCA963X_OPEN_DRAIN; |
81d22878 | 310 | |
8465b018 MG |
311 | /* default to software blinking unless hardware blinking is specified */ |
312 | if (of_property_read_bool(np, "nxp,hw-blink")) | |
56a1740c | 313 | pdata->blink_type = PCA963X_HW_BLINK; |
8465b018 | 314 | else |
56a1740c | 315 | pdata->blink_type = PCA963X_SW_BLINK; |
8465b018 | 316 | |
81d22878 TL |
317 | return pdata; |
318 | } | |
319 | ||
56a1740c | 320 | static const struct of_device_id of_pca963x_match[] = { |
af67384f RR |
321 | { .compatible = "nxp,pca9632", }, |
322 | { .compatible = "nxp,pca9633", }, | |
323 | { .compatible = "nxp,pca9634", }, | |
81d22878 TL |
324 | {}, |
325 | }; | |
326 | #else | |
56a1740c RR |
327 | static struct pca963x_platform_data * |
328 | pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) | |
81d22878 TL |
329 | { |
330 | return ERR_PTR(-ENODEV); | |
331 | } | |
332 | #endif | |
333 | ||
56a1740c | 334 | static int pca963x_probe(struct i2c_client *client, |
75cb2e1d PM |
335 | const struct i2c_device_id *id) |
336 | { | |
56a1740c RR |
337 | struct pca963x *pca963x_chip; |
338 | struct pca963x_led *pca963x; | |
339 | struct pca963x_platform_data *pdata; | |
340 | struct pca963x_chipdef *chip; | |
75cb2e1d PM |
341 | int i, err; |
342 | ||
56a1740c | 343 | chip = &pca963x_chipdefs[id->driver_data]; |
87aae1ea | 344 | pdata = dev_get_platdata(&client->dev); |
75cb2e1d | 345 | |
81d22878 | 346 | if (!pdata) { |
56a1740c | 347 | pdata = pca963x_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 RR |
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 | ||
56a1740c | 361 | pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip), |
a7d0e988 | 362 | GFP_KERNEL); |
56a1740c | 363 | if (!pca963x_chip) |
a7d0e988 | 364 | return -ENOMEM; |
56a1740c | 365 | pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x), |
af67384f | 366 | GFP_KERNEL); |
56a1740c | 367 | if (!pca963x) |
75cb2e1d PM |
368 | return -ENOMEM; |
369 | ||
56a1740c | 370 | i2c_set_clientdata(client, pca963x_chip); |
a7d0e988 | 371 | |
56a1740c RR |
372 | mutex_init(&pca963x_chip->mutex); |
373 | pca963x_chip->chipdef = chip; | |
374 | pca963x_chip->client = client; | |
375 | pca963x_chip->leds = pca963x; | |
a7d0e988 RR |
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++) { |
56a1740c RR |
383 | pca963x[i].led_num = i; |
384 | pca963x[i].chip = pca963x_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) | |
56a1740c RR |
389 | snprintf(pca963x[i].name, |
390 | sizeof(pca963x[i].name), "pca963x:%s", | |
2f73c392 PM |
391 | pdata->leds.leds[i].name); |
392 | if (pdata->leds.leds[i].default_trigger) | |
56a1740c | 393 | pca963x[i].led_cdev.default_trigger = |
2f73c392 | 394 | pdata->leds.leds[i].default_trigger; |
75cb2e1d | 395 | } |
a5cd98b7 RR |
396 | if (!pdata || i >= pdata->leds.num_leds || |
397 | !pdata->leds.leds[i].name) | |
56a1740c RR |
398 | snprintf(pca963x[i].name, sizeof(pca963x[i].name), |
399 | "pca963x:%d:%.2x:%d", client->adapter->nr, | |
a5cd98b7 | 400 | client->addr, i); |
75cb2e1d | 401 | |
56a1740c RR |
402 | pca963x[i].led_cdev.name = pca963x[i].name; |
403 | pca963x[i].led_cdev.brightness_set = pca963x_led_set; | |
75cb2e1d | 404 | |
56a1740c RR |
405 | if (pdata && pdata->blink_type == PCA963X_HW_BLINK) |
406 | pca963x[i].led_cdev.blink_set = pca963x_blink_set; | |
8465b018 | 407 | |
56a1740c | 408 | INIT_WORK(&pca963x[i].work, pca963x_work); |
75cb2e1d | 409 | |
56a1740c | 410 | err = led_classdev_register(&client->dev, &pca963x[i].led_cdev); |
75cb2e1d PM |
411 | if (err < 0) |
412 | goto exit; | |
413 | } | |
414 | ||
415 | /* Disable LED all-call address and set normal mode */ | |
56a1740c | 416 | i2c_smbus_write_byte_data(client, PCA963X_MODE1, 0x00); |
75cb2e1d | 417 | |
2f73c392 | 418 | /* Configure output: open-drain or totem pole (push-pull) */ |
56a1740c RR |
419 | if (pdata && pdata->outdrv == PCA963X_OPEN_DRAIN) |
420 | i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01); | |
2f73c392 | 421 | |
75cb2e1d PM |
422 | return 0; |
423 | ||
424 | exit: | |
425 | while (i--) { | |
56a1740c RR |
426 | led_classdev_unregister(&pca963x[i].led_cdev); |
427 | cancel_work_sync(&pca963x[i].work); | |
75cb2e1d PM |
428 | } |
429 | ||
75cb2e1d PM |
430 | return err; |
431 | } | |
432 | ||
56a1740c | 433 | static int pca963x_remove(struct i2c_client *client) |
75cb2e1d | 434 | { |
56a1740c | 435 | struct pca963x *pca963x = i2c_get_clientdata(client); |
75cb2e1d PM |
436 | int i; |
437 | ||
56a1740c RR |
438 | for (i = 0; i < pca963x->chipdef->n_leds; i++) { |
439 | led_classdev_unregister(&pca963x->leds[i].led_cdev); | |
440 | cancel_work_sync(&pca963x->leds[i].work); | |
a7d0e988 | 441 | } |
75cb2e1d | 442 | |
75cb2e1d PM |
443 | return 0; |
444 | } | |
445 | ||
56a1740c | 446 | static struct i2c_driver pca963x_driver = { |
75cb2e1d | 447 | .driver = { |
56a1740c | 448 | .name = "leds-pca963x", |
75cb2e1d | 449 | .owner = THIS_MODULE, |
56a1740c | 450 | .of_match_table = of_match_ptr(of_pca963x_match), |
75cb2e1d | 451 | }, |
56a1740c RR |
452 | .probe = pca963x_probe, |
453 | .remove = pca963x_remove, | |
454 | .id_table = pca963x_id, | |
75cb2e1d PM |
455 | }; |
456 | ||
56a1740c | 457 | module_i2c_driver(pca963x_driver); |
75cb2e1d PM |
458 | |
459 | MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); | |
56a1740c | 460 | MODULE_DESCRIPTION("PCA963X LED driver"); |
75cb2e1d | 461 | MODULE_LICENSE("GPL v2"); |