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) | |
3dfedb9d | 15 | * LED driver for the PCA9634/5 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> | |
75cb2e1d | 35 | #include <linux/slab.h> |
81d22878 | 36 | #include <linux/of.h> |
56a1740c | 37 | #include <linux/platform_data/leds-pca963x.h> |
75cb2e1d PM |
38 | |
39 | /* LED select registers determine the source that drives LED outputs */ | |
56a1740c RRD |
40 | #define PCA963X_LED_OFF 0x0 /* LED driver off */ |
41 | #define PCA963X_LED_ON 0x1 /* LED driver on */ | |
42 | #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */ | |
43 | #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */ | |
75cb2e1d | 44 | |
56a1740c | 45 | #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */ |
8465b018 | 46 | |
56a1740c RRD |
47 | #define PCA963X_MODE1 0x00 |
48 | #define PCA963X_MODE2 0x01 | |
49 | #define PCA963X_PWM_BASE 0x02 | |
af67384f | 50 | |
56a1740c | 51 | enum pca963x_type { |
af67384f RRD |
52 | pca9633, |
53 | pca9634, | |
3dfedb9d | 54 | pca9635, |
af67384f RRD |
55 | }; |
56 | ||
56a1740c | 57 | struct pca963x_chipdef { |
af67384f RRD |
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 RRD |
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 | }, | |
3dfedb9d PM |
77 | [pca9635] = { |
78 | .grppwm = 0x12, | |
79 | .grpfreq = 0x13, | |
80 | .ledout_base = 0x14, | |
81 | .n_leds = 16, | |
82 | }, | |
af67384f | 83 | }; |
75cb2e1d | 84 | |
8465b018 | 85 | /* Total blink period in milliseconds */ |
56a1740c RRD |
86 | #define PCA963X_BLINK_PERIOD_MIN 42 |
87 | #define PCA963X_BLINK_PERIOD_MAX 10667 | |
8465b018 | 88 | |
56a1740c | 89 | static const struct i2c_device_id pca963x_id[] = { |
af67384f RRD |
90 | { "pca9632", pca9633 }, |
91 | { "pca9633", pca9633 }, | |
92 | { "pca9634", pca9634 }, | |
3dfedb9d | 93 | { "pca9635", pca9635 }, |
75cb2e1d PM |
94 | { } |
95 | }; | |
56a1740c | 96 | MODULE_DEVICE_TABLE(i2c, pca963x_id); |
75cb2e1d | 97 | |
56a1740c | 98 | struct pca963x_led; |
a7d0e988 | 99 | |
56a1740c RRD |
100 | struct pca963x { |
101 | struct pca963x_chipdef *chipdef; | |
a7d0e988 RRD |
102 | struct mutex mutex; |
103 | struct i2c_client *client; | |
56a1740c | 104 | struct pca963x_led *leds; |
a7d0e988 RRD |
105 | }; |
106 | ||
56a1740c RRD |
107 | struct pca963x_led { |
108 | struct pca963x *chip; | |
75cb2e1d | 109 | struct led_classdev led_cdev; |
3dfedb9d | 110 | int led_num; /* 0 .. 15 potentially */ |
75cb2e1d | 111 | char name[32]; |
8465b018 MG |
112 | u8 gdc; |
113 | u8 gfrq; | |
75cb2e1d PM |
114 | }; |
115 | ||
5029a2e3 AL |
116 | static int pca963x_brightness(struct pca963x_led *pca963x, |
117 | enum led_brightness brightness) | |
75cb2e1d | 118 | { |
56a1740c RRD |
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 | 123 | u8 mask = 0x3 << shift; |
5029a2e3 | 124 | int ret; |
75cb2e1d | 125 | |
56a1740c RRD |
126 | mutex_lock(&pca963x->chip->mutex); |
127 | ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); | |
5029a2e3 | 128 | switch (brightness) { |
75cb2e1d | 129 | case LED_FULL: |
5029a2e3 AL |
130 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
131 | ledout_addr, | |
56a1740c | 132 | (ledout & ~mask) | (PCA963X_LED_ON << shift)); |
75cb2e1d PM |
133 | break; |
134 | case LED_OFF: | |
5029a2e3 AL |
135 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
136 | ledout_addr, ledout & ~mask); | |
75cb2e1d PM |
137 | break; |
138 | default: | |
5029a2e3 | 139 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
56a1740c | 140 | PCA963X_PWM_BASE + pca963x->led_num, |
5029a2e3 AL |
141 | brightness); |
142 | if (ret < 0) | |
143 | goto unlock; | |
144 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, | |
145 | ledout_addr, | |
56a1740c | 146 | (ledout & ~mask) | (PCA963X_LED_PWM << shift)); |
75cb2e1d PM |
147 | break; |
148 | } | |
5029a2e3 | 149 | unlock: |
56a1740c | 150 | mutex_unlock(&pca963x->chip->mutex); |
5029a2e3 | 151 | return ret; |
75cb2e1d PM |
152 | } |
153 | ||
5029a2e3 | 154 | static void pca963x_blink(struct pca963x_led *pca963x) |
8465b018 | 155 | { |
56a1740c RRD |
156 | u8 ledout_addr = pca963x->chip->chipdef->ledout_base + |
157 | (pca963x->led_num / 4); | |
a7d0e988 | 158 | u8 ledout; |
56a1740c RRD |
159 | u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, |
160 | PCA963X_MODE2); | |
161 | int shift = 2 * (pca963x->led_num % 4); | |
8465b018 MG |
162 | u8 mask = 0x3 << shift; |
163 | ||
56a1740c RRD |
164 | i2c_smbus_write_byte_data(pca963x->chip->client, |
165 | pca963x->chip->chipdef->grppwm, pca963x->gdc); | |
8465b018 | 166 | |
56a1740c RRD |
167 | i2c_smbus_write_byte_data(pca963x->chip->client, |
168 | pca963x->chip->chipdef->grpfreq, pca963x->gfrq); | |
8465b018 | 169 | |
56a1740c RRD |
170 | if (!(mode2 & PCA963X_MODE2_DMBLNK)) |
171 | i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, | |
172 | mode2 | PCA963X_MODE2_DMBLNK); | |
8465b018 | 173 | |
56a1740c RRD |
174 | mutex_lock(&pca963x->chip->mutex); |
175 | ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); | |
176 | if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) | |
177 | i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, | |
178 | (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift)); | |
179 | mutex_unlock(&pca963x->chip->mutex); | |
8465b018 MG |
180 | } |
181 | ||
5029a2e3 | 182 | static int pca963x_led_set(struct led_classdev *led_cdev, |
75cb2e1d PM |
183 | enum led_brightness value) |
184 | { | |
56a1740c | 185 | struct pca963x_led *pca963x; |
75cb2e1d | 186 | |
56a1740c | 187 | pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
75cb2e1d | 188 | |
5029a2e3 | 189 | return pca963x_brightness(pca963x, value); |
75cb2e1d PM |
190 | } |
191 | ||
56a1740c | 192 | static int pca963x_blink_set(struct led_classdev *led_cdev, |
8465b018 MG |
193 | unsigned long *delay_on, unsigned long *delay_off) |
194 | { | |
56a1740c | 195 | struct pca963x_led *pca963x; |
8465b018 MG |
196 | unsigned long time_on, time_off, period; |
197 | u8 gdc, gfrq; | |
198 | ||
56a1740c | 199 | pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
8465b018 MG |
200 | |
201 | time_on = *delay_on; | |
202 | time_off = *delay_off; | |
203 | ||
204 | /* If both zero, pick reasonable defaults of 500ms each */ | |
205 | if (!time_on && !time_off) { | |
206 | time_on = 500; | |
207 | time_off = 500; | |
208 | } | |
209 | ||
210 | period = time_on + time_off; | |
211 | ||
212 | /* If period not supported by hardware, default to someting sane. */ | |
56a1740c RRD |
213 | if ((period < PCA963X_BLINK_PERIOD_MIN) || |
214 | (period > PCA963X_BLINK_PERIOD_MAX)) { | |
8465b018 MG |
215 | time_on = 500; |
216 | time_off = 500; | |
217 | period = time_on + time_off; | |
218 | } | |
219 | ||
220 | /* | |
221 | * From manual: duty cycle = (GDC / 256) -> | |
222 | * (time_on / period) = (GDC / 256) -> | |
223 | * GDC = ((time_on * 256) / period) | |
224 | */ | |
225 | gdc = (time_on * 256) / period; | |
226 | ||
227 | /* | |
228 | * From manual: period = ((GFRQ + 1) / 24) in seconds. | |
229 | * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) -> | |
230 | * GFRQ = ((period * 24 / 1000) - 1) | |
231 | */ | |
232 | gfrq = (period * 24 / 1000) - 1; | |
233 | ||
56a1740c RRD |
234 | pca963x->gdc = gdc; |
235 | pca963x->gfrq = gfrq; | |
8465b018 | 236 | |
5029a2e3 | 237 | pca963x_blink(pca963x); |
8465b018 MG |
238 | |
239 | *delay_on = time_on; | |
240 | *delay_off = time_off; | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
81d22878 | 245 | #if IS_ENABLED(CONFIG_OF) |
56a1740c RRD |
246 | static struct pca963x_platform_data * |
247 | pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) | |
81d22878 TL |
248 | { |
249 | struct device_node *np = client->dev.of_node, *child; | |
56a1740c RRD |
250 | struct pca963x_platform_data *pdata; |
251 | struct led_info *pca963x_leds; | |
81d22878 TL |
252 | int count; |
253 | ||
254 | count = of_get_child_count(np); | |
af67384f | 255 | if (!count || count > chip->n_leds) |
81d22878 TL |
256 | return ERR_PTR(-ENODEV); |
257 | ||
56a1740c | 258 | pca963x_leds = devm_kzalloc(&client->dev, |
af67384f | 259 | sizeof(struct led_info) * chip->n_leds, GFP_KERNEL); |
56a1740c | 260 | if (!pca963x_leds) |
81d22878 TL |
261 | return ERR_PTR(-ENOMEM); |
262 | ||
263 | for_each_child_of_node(np, child) { | |
a44b0f5e | 264 | struct led_info led = {}; |
81d22878 TL |
265 | u32 reg; |
266 | int res; | |
267 | ||
8a6acd64 RRD |
268 | res = of_property_read_u32(child, "reg", ®); |
269 | if ((res != 0) || (reg >= chip->n_leds)) | |
270 | continue; | |
81d22878 TL |
271 | led.name = |
272 | of_get_property(child, "label", NULL) ? : child->name; | |
273 | led.default_trigger = | |
274 | of_get_property(child, "linux,default-trigger", NULL); | |
56a1740c | 275 | pca963x_leds[reg] = led; |
81d22878 TL |
276 | } |
277 | pdata = devm_kzalloc(&client->dev, | |
56a1740c | 278 | sizeof(struct pca963x_platform_data), GFP_KERNEL); |
81d22878 TL |
279 | if (!pdata) |
280 | return ERR_PTR(-ENOMEM); | |
281 | ||
56a1740c | 282 | pdata->leds.leds = pca963x_leds; |
8a6acd64 | 283 | pdata->leds.num_leds = chip->n_leds; |
81d22878 TL |
284 | |
285 | /* default to open-drain unless totem pole (push-pull) is specified */ | |
286 | if (of_property_read_bool(np, "nxp,totem-pole")) | |
56a1740c | 287 | pdata->outdrv = PCA963X_TOTEM_POLE; |
81d22878 | 288 | else |
56a1740c | 289 | pdata->outdrv = PCA963X_OPEN_DRAIN; |
81d22878 | 290 | |
8465b018 MG |
291 | /* default to software blinking unless hardware blinking is specified */ |
292 | if (of_property_read_bool(np, "nxp,hw-blink")) | |
56a1740c | 293 | pdata->blink_type = PCA963X_HW_BLINK; |
8465b018 | 294 | else |
56a1740c | 295 | pdata->blink_type = PCA963X_SW_BLINK; |
8465b018 | 296 | |
81d22878 TL |
297 | return pdata; |
298 | } | |
299 | ||
56a1740c | 300 | static const struct of_device_id of_pca963x_match[] = { |
af67384f RRD |
301 | { .compatible = "nxp,pca9632", }, |
302 | { .compatible = "nxp,pca9633", }, | |
303 | { .compatible = "nxp,pca9634", }, | |
3dfedb9d | 304 | { .compatible = "nxp,pca9635", }, |
81d22878 TL |
305 | {}, |
306 | }; | |
4d59ed85 | 307 | MODULE_DEVICE_TABLE(of, of_pca963x_match); |
81d22878 | 308 | #else |
56a1740c RRD |
309 | static struct pca963x_platform_data * |
310 | pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) | |
81d22878 TL |
311 | { |
312 | return ERR_PTR(-ENODEV); | |
313 | } | |
314 | #endif | |
315 | ||
56a1740c | 316 | static int pca963x_probe(struct i2c_client *client, |
75cb2e1d PM |
317 | const struct i2c_device_id *id) |
318 | { | |
56a1740c RRD |
319 | struct pca963x *pca963x_chip; |
320 | struct pca963x_led *pca963x; | |
321 | struct pca963x_platform_data *pdata; | |
322 | struct pca963x_chipdef *chip; | |
75cb2e1d PM |
323 | int i, err; |
324 | ||
56a1740c | 325 | chip = &pca963x_chipdefs[id->driver_data]; |
87aae1ea | 326 | pdata = dev_get_platdata(&client->dev); |
75cb2e1d | 327 | |
81d22878 | 328 | if (!pdata) { |
56a1740c | 329 | pdata = pca963x_dt_init(client, chip); |
81d22878 TL |
330 | if (IS_ERR(pdata)) { |
331 | dev_warn(&client->dev, "could not parse configuration\n"); | |
332 | pdata = NULL; | |
333 | } | |
334 | } | |
335 | ||
af67384f RRD |
336 | if (pdata && (pdata->leds.num_leds < 1 || |
337 | pdata->leds.num_leds > chip->n_leds)) { | |
338 | dev_err(&client->dev, "board info must claim 1-%d LEDs", | |
339 | chip->n_leds); | |
340 | return -EINVAL; | |
75cb2e1d PM |
341 | } |
342 | ||
56a1740c | 343 | pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip), |
a7d0e988 | 344 | GFP_KERNEL); |
56a1740c | 345 | if (!pca963x_chip) |
a7d0e988 | 346 | return -ENOMEM; |
56a1740c | 347 | pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x), |
af67384f | 348 | GFP_KERNEL); |
56a1740c | 349 | if (!pca963x) |
75cb2e1d PM |
350 | return -ENOMEM; |
351 | ||
56a1740c | 352 | i2c_set_clientdata(client, pca963x_chip); |
a7d0e988 | 353 | |
56a1740c RRD |
354 | mutex_init(&pca963x_chip->mutex); |
355 | pca963x_chip->chipdef = chip; | |
356 | pca963x_chip->client = client; | |
357 | pca963x_chip->leds = pca963x; | |
a7d0e988 RRD |
358 | |
359 | /* Turn off LEDs by default*/ | |
3dfedb9d PM |
360 | for (i = 0; i < chip->n_leds / 4; i++) |
361 | i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00); | |
75cb2e1d | 362 | |
af67384f | 363 | for (i = 0; i < chip->n_leds; i++) { |
56a1740c RRD |
364 | pca963x[i].led_num = i; |
365 | pca963x[i].chip = pca963x_chip; | |
75cb2e1d PM |
366 | |
367 | /* Platform data can specify LED names and default triggers */ | |
2f73c392 PM |
368 | if (pdata && i < pdata->leds.num_leds) { |
369 | if (pdata->leds.leds[i].name) | |
56a1740c RRD |
370 | snprintf(pca963x[i].name, |
371 | sizeof(pca963x[i].name), "pca963x:%s", | |
2f73c392 PM |
372 | pdata->leds.leds[i].name); |
373 | if (pdata->leds.leds[i].default_trigger) | |
56a1740c | 374 | pca963x[i].led_cdev.default_trigger = |
2f73c392 | 375 | pdata->leds.leds[i].default_trigger; |
75cb2e1d | 376 | } |
a5cd98b7 RRD |
377 | if (!pdata || i >= pdata->leds.num_leds || |
378 | !pdata->leds.leds[i].name) | |
56a1740c RRD |
379 | snprintf(pca963x[i].name, sizeof(pca963x[i].name), |
380 | "pca963x:%d:%.2x:%d", client->adapter->nr, | |
a5cd98b7 | 381 | client->addr, i); |
75cb2e1d | 382 | |
56a1740c | 383 | pca963x[i].led_cdev.name = pca963x[i].name; |
5029a2e3 | 384 | pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set; |
75cb2e1d | 385 | |
56a1740c RRD |
386 | if (pdata && pdata->blink_type == PCA963X_HW_BLINK) |
387 | pca963x[i].led_cdev.blink_set = pca963x_blink_set; | |
8465b018 | 388 | |
56a1740c | 389 | err = led_classdev_register(&client->dev, &pca963x[i].led_cdev); |
75cb2e1d PM |
390 | if (err < 0) |
391 | goto exit; | |
392 | } | |
393 | ||
394 | /* Disable LED all-call address and set normal mode */ | |
56a1740c | 395 | i2c_smbus_write_byte_data(client, PCA963X_MODE1, 0x00); |
75cb2e1d | 396 | |
0c62f42d PM |
397 | if (pdata) { |
398 | /* Configure output: open-drain or totem pole (push-pull) */ | |
399 | if (pdata->outdrv == PCA963X_OPEN_DRAIN) | |
400 | i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01); | |
401 | else | |
402 | i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x05); | |
403 | } | |
2f73c392 | 404 | |
75cb2e1d PM |
405 | return 0; |
406 | ||
407 | exit: | |
5029a2e3 | 408 | while (i--) |
56a1740c | 409 | led_classdev_unregister(&pca963x[i].led_cdev); |
75cb2e1d | 410 | |
75cb2e1d PM |
411 | return err; |
412 | } | |
413 | ||
56a1740c | 414 | static int pca963x_remove(struct i2c_client *client) |
75cb2e1d | 415 | { |
56a1740c | 416 | struct pca963x *pca963x = i2c_get_clientdata(client); |
75cb2e1d PM |
417 | int i; |
418 | ||
5029a2e3 | 419 | for (i = 0; i < pca963x->chipdef->n_leds; i++) |
56a1740c | 420 | led_classdev_unregister(&pca963x->leds[i].led_cdev); |
75cb2e1d | 421 | |
75cb2e1d PM |
422 | return 0; |
423 | } | |
424 | ||
56a1740c | 425 | static struct i2c_driver pca963x_driver = { |
75cb2e1d | 426 | .driver = { |
56a1740c | 427 | .name = "leds-pca963x", |
56a1740c | 428 | .of_match_table = of_match_ptr(of_pca963x_match), |
75cb2e1d | 429 | }, |
56a1740c RRD |
430 | .probe = pca963x_probe, |
431 | .remove = pca963x_remove, | |
432 | .id_table = pca963x_id, | |
75cb2e1d PM |
433 | }; |
434 | ||
56a1740c | 435 | module_i2c_driver(pca963x_driver); |
75cb2e1d PM |
436 | |
437 | MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); | |
56a1740c | 438 | MODULE_DESCRIPTION("PCA963X LED driver"); |
75cb2e1d | 439 | MODULE_LICENSE("GPL v2"); |