blob: 2d34009d00e6de766fdc3b34868569ccdd3ebaa4 [file] [log] [blame]
Nate Casef46e9202008-07-16 22:49:55 +01001/*
2 * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
3 *
4 * Author: Nate Case <ncase@xes-inc.com>
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * LED driver for various PCA955x I2C LED drivers
11 *
12 * Supported devices:
13 *
14 * Device Description 7-bit slave address
15 * ------ ----------- -------------------
16 * PCA9550 2-bit driver 0x60 .. 0x61
17 * PCA9551 8-bit driver 0x60 .. 0x67
18 * PCA9552 16-bit driver 0x60 .. 0x67
19 * PCA9553/01 4-bit driver 0x62
20 * PCA9553/02 4-bit driver 0x63
21 *
22 * Philips PCA955x LED driver chips follow a register map as shown below:
23 *
24 * Control Register Description
25 * ---------------- -----------
26 * 0x0 Input register 0
27 * ..
28 * NUM_INPUT_REGS - 1 Last Input register X
29 *
30 * NUM_INPUT_REGS Frequency prescaler 0
31 * NUM_INPUT_REGS + 1 PWM register 0
32 * NUM_INPUT_REGS + 2 Frequency prescaler 1
33 * NUM_INPUT_REGS + 3 PWM register 1
34 *
35 * NUM_INPUT_REGS + 4 LED selector 0
36 * NUM_INPUT_REGS + 4
37 * + NUM_LED_REGS - 1 Last LED selector
38 *
39 * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many
40 * bits the chip supports.
41 */
42
Tin Huynh44b3e312016-12-02 11:39:13 +070043#include <linux/acpi.h>
Nate Casef46e9202008-07-16 22:49:55 +010044#include <linux/ctype.h>
Cédric Le Goatered1f4b92017-08-08 15:42:37 +020045#include <linux/delay.h>
Nate Casef46e9202008-07-16 22:49:55 +010046#include <linux/err.h>
Cédric Le Goatered1f4b92017-08-08 15:42:37 +020047#include <linux/gpio.h>
Nate Casef46e9202008-07-16 22:49:55 +010048#include <linux/i2c.h>
Cédric Le Goatered1f4b92017-08-08 15:42:37 +020049#include <linux/leds.h>
50#include <linux/module.h>
51#include <linux/of_device.h>
52#include <linux/of.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Cédric Le Goatered1f4b92017-08-08 15:42:37 +020054#include <linux/string.h>
Nate Casef46e9202008-07-16 22:49:55 +010055
56/* LED select registers determine the source that drives LED outputs */
57#define PCA955X_LS_LED_ON 0x0 /* Output LOW */
58#define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */
59#define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */
60#define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */
61
62enum pca955x_type {
63 pca9550,
64 pca9551,
65 pca9552,
66 pca9553,
67};
68
69struct pca955x_chipdef {
70 int bits;
71 u8 slv_addr; /* 7-bit slave address mask */
72 int slv_addr_shift; /* Number of bits to ignore */
73};
74
75static struct pca955x_chipdef pca955x_chipdefs[] = {
76 [pca9550] = {
77 .bits = 2,
78 .slv_addr = /* 110000x */ 0x60,
79 .slv_addr_shift = 1,
80 },
81 [pca9551] = {
82 .bits = 8,
83 .slv_addr = /* 1100xxx */ 0x60,
84 .slv_addr_shift = 3,
85 },
86 [pca9552] = {
87 .bits = 16,
88 .slv_addr = /* 1100xxx */ 0x60,
89 .slv_addr_shift = 3,
90 },
91 [pca9553] = {
92 .bits = 4,
93 .slv_addr = /* 110001x */ 0x62,
94 .slv_addr_shift = 1,
95 },
96};
97
98static const struct i2c_device_id pca955x_id[] = {
99 { "pca9550", pca9550 },
100 { "pca9551", pca9551 },
101 { "pca9552", pca9552 },
102 { "pca9553", pca9553 },
103 { }
104};
105MODULE_DEVICE_TABLE(i2c, pca955x_id);
106
Tin Huynh44b3e312016-12-02 11:39:13 +0700107static const struct acpi_device_id pca955x_acpi_ids[] = {
108 { "PCA9550", pca9550 },
109 { "PCA9551", pca9551 },
110 { "PCA9552", pca9552 },
111 { "PCA9553", pca9553 },
112 { }
113};
114MODULE_DEVICE_TABLE(acpi, pca955x_acpi_ids);
115
Alexander Steine7e11d82012-05-29 15:07:30 -0700116struct pca955x {
117 struct mutex lock;
118 struct pca955x_led *leds;
Nate Casef46e9202008-07-16 22:49:55 +0100119 struct pca955x_chipdef *chipdef;
120 struct i2c_client *client;
Alexander Steine7e11d82012-05-29 15:07:30 -0700121};
122
123struct pca955x_led {
124 struct pca955x *pca955x;
Nate Casef46e9202008-07-16 22:49:55 +0100125 struct led_classdev led_cdev;
126 int led_num; /* 0 .. 15 potentially */
127 char name[32];
Cédric Le Goatered1f4b92017-08-08 15:42:37 +0200128 const char *default_trigger;
129};
130
131struct pca955x_platform_data {
132 struct pca955x_led *leds;
133 int num_leds;
Nate Casef46e9202008-07-16 22:49:55 +0100134};
135
136/* 8 bits per input register */
137static inline int pca95xx_num_input_regs(int bits)
138{
139 return (bits + 7) / 8;
140}
141
142/* 4 bits per LED selector register */
143static inline int pca95xx_num_led_regs(int bits)
144{
145 return (bits + 3) / 4;
146}
147
148/*
149 * Return an LED selector register value based on an existing one, with
150 * the appropriate 2-bit state value set for the given LED number (0-3).
151 */
152static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
153{
154 return (oldval & (~(0x3 << (led_num << 1)))) |
155 ((state & 0x3) << (led_num << 1));
156}
157
158/*
159 * Write to frequency prescaler register, used to program the
160 * period of the PWM output. period = (PSCx + 1) / 38
161 */
162static void pca955x_write_psc(struct i2c_client *client, int n, u8 val)
163{
Alexander Steine7e11d82012-05-29 15:07:30 -0700164 struct pca955x *pca955x = i2c_get_clientdata(client);
Nate Casef46e9202008-07-16 22:49:55 +0100165
166 i2c_smbus_write_byte_data(client,
167 pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n,
168 val);
169}
170
171/*
172 * Write to PWM register, which determines the duty cycle of the
173 * output. LED is OFF when the count is less than the value of this
174 * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
175 *
176 * Duty cycle is (256 - PWMx) / 256
177 */
178static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
179{
Alexander Steine7e11d82012-05-29 15:07:30 -0700180 struct pca955x *pca955x = i2c_get_clientdata(client);
Nate Casef46e9202008-07-16 22:49:55 +0100181
182 i2c_smbus_write_byte_data(client,
183 pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n,
184 val);
185}
186
187/*
188 * Write to LED selector register, which determines the source that
189 * drives the LED output.
190 */
191static void pca955x_write_ls(struct i2c_client *client, int n, u8 val)
192{
Alexander Steine7e11d82012-05-29 15:07:30 -0700193 struct pca955x *pca955x = i2c_get_clientdata(client);
Nate Casef46e9202008-07-16 22:49:55 +0100194
195 i2c_smbus_write_byte_data(client,
196 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n,
197 val);
198}
199
200/*
201 * Read the LED selector register, which determines the source that
202 * drives the LED output.
203 */
204static u8 pca955x_read_ls(struct i2c_client *client, int n)
205{
Alexander Steine7e11d82012-05-29 15:07:30 -0700206 struct pca955x *pca955x = i2c_get_clientdata(client);
Nate Casef46e9202008-07-16 22:49:55 +0100207
208 return (u8) i2c_smbus_read_byte_data(client,
209 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n);
210}
211
Andrew Lunnc3482b82015-08-20 12:32:35 +0200212static int pca955x_led_set(struct led_classdev *led_cdev,
213 enum led_brightness value)
Nate Casef46e9202008-07-16 22:49:55 +0100214{
Alexander Steine7e11d82012-05-29 15:07:30 -0700215 struct pca955x_led *pca955x_led;
216 struct pca955x *pca955x;
Nate Casef46e9202008-07-16 22:49:55 +0100217 u8 ls;
218 int chip_ls; /* which LSx to use (0-3 potentially) */
219 int ls_led; /* which set of bits within LSx to use (0-3) */
220
Andrew Lunnc3482b82015-08-20 12:32:35 +0200221 pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
Alexander Steine7e11d82012-05-29 15:07:30 -0700222 pca955x = pca955x_led->pca955x;
223
224 chip_ls = pca955x_led->led_num / 4;
225 ls_led = pca955x_led->led_num % 4;
226
227 mutex_lock(&pca955x->lock);
Nate Casef46e9202008-07-16 22:49:55 +0100228
229 ls = pca955x_read_ls(pca955x->client, chip_ls);
230
Andrew Lunnc3482b82015-08-20 12:32:35 +0200231 switch (value) {
Nate Casef46e9202008-07-16 22:49:55 +0100232 case LED_FULL:
233 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
234 break;
235 case LED_OFF:
236 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
237 break;
238 case LED_HALF:
239 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
240 break;
241 default:
242 /*
243 * Use PWM1 for all other values. This has the unwanted
244 * side effect of making all LEDs on the chip share the
245 * same brightness level if set to a value other than
246 * OFF, HALF, or FULL. But, this is probably better than
247 * just turning off for all other values.
248 */
Alexander Steine7e11d82012-05-29 15:07:30 -0700249 pca955x_write_pwm(pca955x->client, 1,
Andrew Lunnc3482b82015-08-20 12:32:35 +0200250 255 - value);
Nate Casef46e9202008-07-16 22:49:55 +0100251 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
252 break;
253 }
254
255 pca955x_write_ls(pca955x->client, chip_ls, ls);
Alexander Steine7e11d82012-05-29 15:07:30 -0700256
257 mutex_unlock(&pca955x->lock);
Nate Casef46e9202008-07-16 22:49:55 +0100258
Andrew Lunnc3482b82015-08-20 12:32:35 +0200259 return 0;
Nate Casef46e9202008-07-16 22:49:55 +0100260}
261
Cédric Le Goatered1f4b92017-08-08 15:42:37 +0200262#if IS_ENABLED(CONFIG_OF)
263static struct pca955x_platform_data *
264pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip)
265{
266 struct device_node *np = client->dev.of_node;
267 struct device_node *child;
268 struct pca955x_platform_data *pdata;
269 int count;
270
271 count = of_get_child_count(np);
272 if (!count || count > chip->bits)
273 return ERR_PTR(-ENODEV);
274
275 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
276 if (!pdata)
277 return ERR_PTR(-ENOMEM);
278
279 pdata->leds = devm_kzalloc(&client->dev,
280 sizeof(struct pca955x_led) * chip->bits,
281 GFP_KERNEL);
282 if (!pdata->leds)
283 return ERR_PTR(-ENOMEM);
284
285 for_each_child_of_node(np, child) {
286 const char *name;
287 u32 reg;
288 int res;
289
290 res = of_property_read_u32(child, "reg", &reg);
291 if ((res != 0) || (reg >= chip->bits))
292 continue;
293
294 if (of_property_read_string(child, "label", &name))
295 name = child->name;
296
297 snprintf(pdata->leds[reg].name, sizeof(pdata->leds[reg].name),
298 "%s", name);
299
300 of_property_read_string(child, "linux,default-trigger",
301 &pdata->leds[reg].default_trigger);
302 }
303
304 pdata->num_leds = chip->bits;
305
306 return pdata;
307}
308
309static const struct of_device_id of_pca955x_match[] = {
310 { .compatible = "nxp,pca9550", .data = (void *)pca9550 },
311 { .compatible = "nxp,pca9551", .data = (void *)pca9551 },
312 { .compatible = "nxp,pca9552", .data = (void *)pca9552 },
313 { .compatible = "nxp,pca9553", .data = (void *)pca9553 },
314 {},
315};
316
317MODULE_DEVICE_TABLE(of, of_pca955x_match);
318#else
319static struct pca955x_platform_data *
320pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip)
321{
322 return ERR_PTR(-ENODEV);
323}
324#endif
325
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500326static int pca955x_probe(struct i2c_client *client,
Nate Casef46e9202008-07-16 22:49:55 +0100327 const struct i2c_device_id *id)
328{
Alexander Steine7e11d82012-05-29 15:07:30 -0700329 struct pca955x *pca955x;
330 struct pca955x_led *pca955x_led;
Nate Casef46e9202008-07-16 22:49:55 +0100331 struct pca955x_chipdef *chip;
332 struct i2c_adapter *adapter;
Sven Wegener95bf14b2008-10-03 15:23:48 -0700333 int i, err;
Cédric Le Goatered1f4b92017-08-08 15:42:37 +0200334 struct pca955x_platform_data *pdata;
Nate Casef46e9202008-07-16 22:49:55 +0100335
Tin Huynh44b3e312016-12-02 11:39:13 +0700336 if (id) {
337 chip = &pca955x_chipdefs[id->driver_data];
338 } else {
339 const struct acpi_device_id *acpi_id;
340
341 acpi_id = acpi_match_device(pca955x_acpi_ids, &client->dev);
342 if (!acpi_id)
343 return -ENODEV;
344 chip = &pca955x_chipdefs[acpi_id->driver_data];
345 }
Nate Casef46e9202008-07-16 22:49:55 +0100346 adapter = to_i2c_adapter(client->dev.parent);
Jingoo Han87aae1e2013-07-30 01:07:35 -0700347 pdata = dev_get_platdata(&client->dev);
Cédric Le Goatered1f4b92017-08-08 15:42:37 +0200348 if (!pdata) {
349 pdata = pca955x_pdata_of_init(client, chip);
350 if (IS_ERR(pdata))
351 return PTR_ERR(pdata);
352 }
Nate Casef46e9202008-07-16 22:49:55 +0100353
354 /* Make sure the slave address / chip type combo given is possible */
355 if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
356 chip->slv_addr) {
357 dev_err(&client->dev, "invalid slave address %02x\n",
358 client->addr);
359 return -ENODEV;
360 }
361
Sachin Kamatb40b0c12012-11-25 12:21:47 +0530362 dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
Nate Casef46e9202008-07-16 22:49:55 +0100363 "slave address 0x%02x\n",
Tin Huynh44b3e312016-12-02 11:39:13 +0700364 client->name, chip->bits, client->addr);
Nate Casef46e9202008-07-16 22:49:55 +0100365
Tin Huynhaace34c2017-05-22 16:19:20 +0700366 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Nate Casef46e9202008-07-16 22:49:55 +0100367 return -EIO;
368
Cédric Le Goatered1f4b92017-08-08 15:42:37 +0200369 if (pdata->num_leds != chip->bits) {
370 dev_err(&client->dev,
371 "board info claims %d LEDs on a %d-bit chip\n",
372 pdata->num_leds, chip->bits);
373 return -ENODEV;
Nate Casef46e9202008-07-16 22:49:55 +0100374 }
375
Bryan Wu73759f62012-07-04 12:09:05 +0800376 pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
Sven Wegener95bf14b2008-10-03 15:23:48 -0700377 if (!pca955x)
378 return -ENOMEM;
Nate Casef46e9202008-07-16 22:49:55 +0100379
Bryan Wu73759f62012-07-04 12:09:05 +0800380 pca955x->leds = devm_kzalloc(&client->dev,
381 sizeof(*pca955x_led) * chip->bits, GFP_KERNEL);
382 if (!pca955x->leds)
383 return -ENOMEM;
Alexander Steine7e11d82012-05-29 15:07:30 -0700384
Sven Wegener95bf14b2008-10-03 15:23:48 -0700385 i2c_set_clientdata(client, pca955x);
386
Alexander Steine7e11d82012-05-29 15:07:30 -0700387 mutex_init(&pca955x->lock);
388 pca955x->client = client;
389 pca955x->chipdef = chip;
390
Sven Wegener95bf14b2008-10-03 15:23:48 -0700391 for (i = 0; i < chip->bits; i++) {
Alexander Steine7e11d82012-05-29 15:07:30 -0700392 pca955x_led = &pca955x->leds[i];
393 pca955x_led->led_num = i;
394 pca955x_led->pca955x = pca955x;
Sven Wegener95bf14b2008-10-03 15:23:48 -0700395
Nate Casef46e9202008-07-16 22:49:55 +0100396 /* Platform data can specify LED names and default triggers */
397 if (pdata) {
398 if (pdata->leds[i].name)
Alexander Steine7e11d82012-05-29 15:07:30 -0700399 snprintf(pca955x_led->name,
400 sizeof(pca955x_led->name), "pca955x:%s",
401 pdata->leds[i].name);
Nate Casef46e9202008-07-16 22:49:55 +0100402 if (pdata->leds[i].default_trigger)
Alexander Steine7e11d82012-05-29 15:07:30 -0700403 pca955x_led->led_cdev.default_trigger =
Nate Casef46e9202008-07-16 22:49:55 +0100404 pdata->leds[i].default_trigger;
405 } else {
Alexander Steine7e11d82012-05-29 15:07:30 -0700406 snprintf(pca955x_led->name, sizeof(pca955x_led->name),
Sven Wegener95bf14b2008-10-03 15:23:48 -0700407 "pca955x:%d", i);
Nate Casef46e9202008-07-16 22:49:55 +0100408 }
Nate Casef46e9202008-07-16 22:49:55 +0100409
Alexander Steine7e11d82012-05-29 15:07:30 -0700410 pca955x_led->led_cdev.name = pca955x_led->name;
Andrew Lunnc3482b82015-08-20 12:32:35 +0200411 pca955x_led->led_cdev.brightness_set_blocking = pca955x_led_set;
Nate Casef46e9202008-07-16 22:49:55 +0100412
Alexander Steine7e11d82012-05-29 15:07:30 -0700413 err = led_classdev_register(&client->dev,
414 &pca955x_led->led_cdev);
Sven Wegener95bf14b2008-10-03 15:23:48 -0700415 if (err < 0)
416 goto exit;
Nate Casef46e9202008-07-16 22:49:55 +0100417 }
418
419 /* Turn off LEDs */
420 for (i = 0; i < pca95xx_num_led_regs(chip->bits); i++)
421 pca955x_write_ls(client, i, 0x55);
422
423 /* PWM0 is used for half brightness or 50% duty cycle */
424 pca955x_write_pwm(client, 0, 255-LED_HALF);
425
426 /* PWM1 is used for variable brightness, default to OFF */
427 pca955x_write_pwm(client, 1, 0);
428
429 /* Set to fast frequency so we do not see flashing */
430 pca955x_write_psc(client, 0, 0);
431 pca955x_write_psc(client, 1, 0);
432
433 return 0;
Sven Wegener95bf14b2008-10-03 15:23:48 -0700434
Nate Casef46e9202008-07-16 22:49:55 +0100435exit:
Andrew Lunnc3482b82015-08-20 12:32:35 +0200436 while (i--)
Alexander Steine7e11d82012-05-29 15:07:30 -0700437 led_classdev_unregister(&pca955x->leds[i].led_cdev);
Sven Wegener95bf14b2008-10-03 15:23:48 -0700438
Nate Casef46e9202008-07-16 22:49:55 +0100439 return err;
440}
441
Bill Pemberton678e8a62012-11-19 13:26:00 -0500442static int pca955x_remove(struct i2c_client *client)
Nate Casef46e9202008-07-16 22:49:55 +0100443{
Alexander Steine7e11d82012-05-29 15:07:30 -0700444 struct pca955x *pca955x = i2c_get_clientdata(client);
Nate Casef46e9202008-07-16 22:49:55 +0100445 int i;
446
Andrew Lunnc3482b82015-08-20 12:32:35 +0200447 for (i = 0; i < pca955x->chipdef->bits; i++)
Alexander Steine7e11d82012-05-29 15:07:30 -0700448 led_classdev_unregister(&pca955x->leds[i].led_cdev);
Nate Casef46e9202008-07-16 22:49:55 +0100449
Nate Casef46e9202008-07-16 22:49:55 +0100450 return 0;
451}
452
453static struct i2c_driver pca955x_driver = {
454 .driver = {
455 .name = "leds-pca955x",
Tin Huynh44b3e312016-12-02 11:39:13 +0700456 .acpi_match_table = ACPI_PTR(pca955x_acpi_ids),
Cédric Le Goatered1f4b92017-08-08 15:42:37 +0200457 .of_match_table = of_match_ptr(of_pca955x_match),
Nate Casef46e9202008-07-16 22:49:55 +0100458 },
459 .probe = pca955x_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500460 .remove = pca955x_remove,
Nate Casef46e9202008-07-16 22:49:55 +0100461 .id_table = pca955x_id,
462};
463
Axel Lin09a0d182012-01-10 15:09:27 -0800464module_i2c_driver(pca955x_driver);
Nate Casef46e9202008-07-16 22:49:55 +0100465
466MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
467MODULE_DESCRIPTION("PCA955x LED driver");
468MODULE_LICENSE("GPL v2");