blob: 017794bb87ae460fa899053542c7e0993942559a [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Riku Voipioe14fa822008-05-31 14:43:41 +01002/*
3 * pca9532.c - 16-bit Led dimmer
4 *
Jan Weitzel3dbf6222011-05-24 17:13:26 -07005 * Copyright (C) 2011 Jan Weitzel
Riku Voipiob26e0ed2009-03-03 21:37:17 +02006 * Copyright (C) 2008 Riku Voipio
Riku Voipioe14fa822008-05-31 14:43:41 +01007 *
Jan Weitzel3dbf6222011-05-24 17:13:26 -07008 * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
Riku Voipioe14fa822008-05-31 14:43:41 +01009 */
10
11#include <linux/module.h>
12#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010014#include <linux/leds.h>
15#include <linux/input.h>
16#include <linux/mutex.h>
Riku Voipio934cd3f2008-12-03 08:21:36 +000017#include <linux/workqueue.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010018#include <linux/leds-pca9532.h>
Linus Walleij3881a72f2019-08-08 15:20:20 +020019#include <linux/gpio/driver.h>
Phil Reidfa4191a2016-06-14 15:36:17 +080020#include <linux/of.h>
21#include <linux/of_device.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010022
Jan Weitzel3dbf6222011-05-24 17:13:26 -070023/* m = num_leds*/
24#define PCA9532_REG_INPUT(i) ((i) >> 3)
25#define PCA9532_REG_OFFSET(m) ((m) >> 4)
26#define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
27#define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
28#define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
29#define LED_NUM(led) (led & 0x3)
Markus Moll2a378852020-09-22 21:28:35 +020030#define LED_SHIFT(led) (LED_NUM(led) * 2)
31#define LED_MASK(led) (0x3 << LED_SHIFT(led))
Riku Voipioe14fa822008-05-31 14:43:41 +010032
33#define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
34
Jan Weitzel3dbf6222011-05-24 17:13:26 -070035struct pca9532_chip_info {
36 u8 num_leds;
37};
38
Riku Voipioe14fa822008-05-31 14:43:41 +010039struct pca9532_data {
40 struct i2c_client *client;
41 struct pca9532_led leds[16];
42 struct mutex update_lock;
Richard Purdie85c52042009-09-07 14:35:04 +010043 struct input_dev *idev;
Antonio Ospite07172d22009-06-19 13:53:07 +020044 struct work_struct work;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -070045#ifdef CONFIG_LEDS_PCA9532_GPIO
46 struct gpio_chip gpio;
47#endif
Jan Weitzel3dbf6222011-05-24 17:13:26 -070048 const struct pca9532_chip_info *chip_info;
Riku Voipioe14fa822008-05-31 14:43:41 +010049 u8 pwm[2];
50 u8 psc[2];
51};
52
53static int pca9532_probe(struct i2c_client *client,
54 const struct i2c_device_id *id);
55static int pca9532_remove(struct i2c_client *client);
56
Jan Weitzel3dbf6222011-05-24 17:13:26 -070057enum {
58 pca9530,
59 pca9531,
60 pca9532,
61 pca9533,
62};
63
Riku Voipioe14fa822008-05-31 14:43:41 +010064static const struct i2c_device_id pca9532_id[] = {
Jan Weitzel3dbf6222011-05-24 17:13:26 -070065 { "pca9530", pca9530 },
66 { "pca9531", pca9531 },
67 { "pca9532", pca9532 },
68 { "pca9533", pca9533 },
Riku Voipioe14fa822008-05-31 14:43:41 +010069 { }
70};
71
72MODULE_DEVICE_TABLE(i2c, pca9532_id);
73
Jan Weitzel3dbf6222011-05-24 17:13:26 -070074static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
75 [pca9530] = {
76 .num_leds = 2,
77 },
78 [pca9531] = {
79 .num_leds = 8,
80 },
81 [pca9532] = {
82 .num_leds = 16,
83 },
84 [pca9533] = {
85 .num_leds = 4,
86 },
87};
88
Phil Reidfa4191a2016-06-14 15:36:17 +080089#ifdef CONFIG_OF
90static const struct of_device_id of_pca9532_leds_match[] = {
91 { .compatible = "nxp,pca9530", .data = (void *)pca9530 },
92 { .compatible = "nxp,pca9531", .data = (void *)pca9531 },
93 { .compatible = "nxp,pca9532", .data = (void *)pca9532 },
94 { .compatible = "nxp,pca9533", .data = (void *)pca9533 },
95 {},
96};
97
98MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
99#endif
100
Riku Voipioe14fa822008-05-31 14:43:41 +0100101static struct i2c_driver pca9532_driver = {
102 .driver = {
Wolfram Sang30cb35b2011-07-08 15:39:46 -0700103 .name = "leds-pca953x",
Phil Reidfa4191a2016-06-14 15:36:17 +0800104 .of_match_table = of_match_ptr(of_pca9532_leds_match),
Riku Voipioe14fa822008-05-31 14:43:41 +0100105 },
Richard Purdie85c52042009-09-07 14:35:04 +0100106 .probe = pca9532_probe,
Riku Voipioe14fa822008-05-31 14:43:41 +0100107 .remove = pca9532_remove,
108 .id_table = pca9532_id,
109};
110
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300111/* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
Riku Voipioe14fa822008-05-31 14:43:41 +0100112 * the clever Thecus people are using one pwm to drive the beeper. So,
113 * as a compromise we average one pwm to the values requested by all
114 * leds that are not ON/OFF.
115 * */
Riku Voipio934cd3f2008-12-03 08:21:36 +0000116static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
Riku Voipioe14fa822008-05-31 14:43:41 +0100117 enum led_brightness value)
118{
119 int a = 0, b = 0, i = 0;
120 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700121 for (i = 0; i < data->chip_info->num_leds; i++) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100122 if (data->leds[i].type == PCA9532_TYPE_LED &&
123 data->leds[i].state == PCA9532_PWM0+pwm) {
124 a++;
125 b += data->leds[i].ldev.brightness;
126 }
127 }
128 if (a == 0) {
129 dev_err(&client->dev,
130 "fear of division by zero %d/%d, wanted %d\n",
131 b, a, value);
132 return -EINVAL;
133 }
134 b = b/a;
135 if (b > 0xFF)
136 return -EINVAL;
Riku Voipioe14fa822008-05-31 14:43:41 +0100137 data->pwm[pwm] = b;
Antonio Ospite07172d22009-06-19 13:53:07 +0200138 data->psc[pwm] = blink;
139 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000140}
141
142static int pca9532_setpwm(struct i2c_client *client, int pwm)
143{
Antonio Ospite07172d22009-06-19 13:53:07 +0200144 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700145 u8 maxleds = data->chip_info->num_leds;
146
Antonio Ospite07172d22009-06-19 13:53:07 +0200147 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700148 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
Riku Voipioe14fa822008-05-31 14:43:41 +0100149 data->pwm[pwm]);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700150 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
Riku Voipioe14fa822008-05-31 14:43:41 +0100151 data->psc[pwm]);
152 mutex_unlock(&data->update_lock);
153 return 0;
154}
155
156/* Set LED routing */
157static void pca9532_setled(struct pca9532_led *led)
158{
159 struct i2c_client *client = led->client;
160 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700161 u8 maxleds = data->chip_info->num_leds;
Riku Voipioe14fa822008-05-31 14:43:41 +0100162 char reg;
163
164 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700165 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
Riku Voipioe14fa822008-05-31 14:43:41 +0100166 /* zero led bits */
Markus Moll2a378852020-09-22 21:28:35 +0200167 reg = reg & ~LED_MASK(led->id);
Riku Voipioe14fa822008-05-31 14:43:41 +0100168 /* set the new value */
Markus Moll2a378852020-09-22 21:28:35 +0200169 reg = reg | (led->state << LED_SHIFT(led->id));
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700170 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
Riku Voipioe14fa822008-05-31 14:43:41 +0100171 mutex_unlock(&data->update_lock);
172}
173
Andrew Lunn00a88a12015-08-20 12:08:10 +0200174static int pca9532_set_brightness(struct led_classdev *led_cdev,
Riku Voipioe14fa822008-05-31 14:43:41 +0100175 enum led_brightness value)
176{
177 int err = 0;
178 struct pca9532_led *led = ldev_to_led(led_cdev);
179
180 if (value == LED_OFF)
181 led->state = PCA9532_OFF;
182 else if (value == LED_FULL)
183 led->state = PCA9532_ON;
184 else {
185 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
Antonio Ospite07172d22009-06-19 13:53:07 +0200186 err = pca9532_calcpwm(led->client, 0, 0, value);
Riku Voipioe14fa822008-05-31 14:43:41 +0100187 if (err)
Andrew Lunn00a88a12015-08-20 12:08:10 +0200188 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100189 }
Andrew Lunn00a88a12015-08-20 12:08:10 +0200190 if (led->state == PCA9532_PWM0)
191 pca9532_setpwm(led->client, 0);
192 pca9532_setled(led);
193 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100194}
195
196static int pca9532_set_blink(struct led_classdev *led_cdev,
197 unsigned long *delay_on, unsigned long *delay_off)
198{
199 struct pca9532_led *led = ldev_to_led(led_cdev);
200 struct i2c_client *client = led->client;
201 int psc;
Antonio Ospite07172d22009-06-19 13:53:07 +0200202 int err = 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100203
204 if (*delay_on == 0 && *delay_off == 0) {
Jingoo Han962363c2013-01-21 21:58:07 -0800205 /* led subsystem ask us for a blink rate */
Richard Purdie85c52042009-09-07 14:35:04 +0100206 *delay_on = 1000;
Riku Voipioe14fa822008-05-31 14:43:41 +0100207 *delay_off = 1000;
208 }
209 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
210 return -EINVAL;
211
212 /* Thecus specific: only use PSC/PWM 0 */
213 psc = (*delay_on * 152-1)/1000;
Antonio Ospite07172d22009-06-19 13:53:07 +0200214 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
215 if (err)
216 return err;
Andrew Lunn00a88a12015-08-20 12:08:10 +0200217 if (led->state == PCA9532_PWM0)
218 pca9532_setpwm(led->client, 0);
219 pca9532_setled(led);
220
Antonio Ospite07172d22009-06-19 13:53:07 +0200221 return 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100222}
223
Sven Wegener0d733572008-11-17 14:33:41 +0000224static int pca9532_event(struct input_dev *dev, unsigned int type,
225 unsigned int code, int value)
Riku Voipioe14fa822008-05-31 14:43:41 +0100226{
227 struct pca9532_data *data = input_get_drvdata(dev);
228
Riku Voipio7fbc3a92009-03-03 22:13:06 +0200229 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
Riku Voipioe14fa822008-05-31 14:43:41 +0100230 return -1;
231
232 /* XXX: allow different kind of beeps with psc/pwm modifications */
233 if (value > 1 && value < 32767)
234 data->pwm[1] = 127;
235 else
236 data->pwm[1] = 0;
237
Antonio Ospite07172d22009-06-19 13:53:07 +0200238 schedule_work(&data->work);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000239
Antonio Ospite07172d22009-06-19 13:53:07 +0200240 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000241}
242
243static void pca9532_input_work(struct work_struct *work)
244{
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700245 struct pca9532_data *data =
246 container_of(work, struct pca9532_data, work);
247 u8 maxleds = data->chip_info->num_leds;
248
Riku Voipioe14fa822008-05-31 14:43:41 +0100249 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700250 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
Riku Voipioe14fa822008-05-31 14:43:41 +0100251 data->pwm[1]);
252 mutex_unlock(&data->update_lock);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000253}
Riku Voipioe14fa822008-05-31 14:43:41 +0100254
Felix Brack28c5fe92017-04-13 09:51:38 +0200255static enum pca9532_state pca9532_getled(struct pca9532_led *led)
256{
257 struct i2c_client *client = led->client;
258 struct pca9532_data *data = i2c_get_clientdata(client);
259 u8 maxleds = data->chip_info->num_leds;
260 char reg;
261 enum pca9532_state ret;
262
263 mutex_lock(&data->update_lock);
264 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
Markus Moll2a378852020-09-22 21:28:35 +0200265 ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
Felix Brack28c5fe92017-04-13 09:51:38 +0200266 mutex_unlock(&data->update_lock);
267 return ret;
268}
269
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700270#ifdef CONFIG_LEDS_PCA9532_GPIO
271static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
272{
Linus Walleijdced1462015-12-08 16:39:20 +0100273 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700274 struct pca9532_led *led = &data->leds[offset];
275
276 if (led->type == PCA9532_TYPE_GPIO)
277 return 0;
278
279 return -EBUSY;
280}
281
282static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
283{
Linus Walleijdced1462015-12-08 16:39:20 +0100284 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700285 struct pca9532_led *led = &data->leds[offset];
286
287 if (val)
288 led->state = PCA9532_ON;
289 else
290 led->state = PCA9532_OFF;
291
292 pca9532_setled(led);
293}
294
295static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
296{
Linus Walleijdced1462015-12-08 16:39:20 +0100297 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700298 unsigned char reg;
299
300 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
301
302 return !!(reg & (1 << (offset % 8)));
303}
304
305static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
306{
307 /* To use as input ensure pin is not driven */
308 pca9532_gpio_set_value(gc, offset, 0);
309
310 return 0;
311}
312
313static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
314{
315 pca9532_gpio_set_value(gc, offset, val);
316
317 return 0;
318}
319#endif /* CONFIG_LEDS_PCA9532_GPIO */
320
321static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
Axel Lin125c7132011-01-12 16:59:15 -0800322{
323 int i = n_devs;
324
325 if (!data)
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700326 return -EINVAL;
Axel Lin125c7132011-01-12 16:59:15 -0800327
328 while (--i >= 0) {
329 switch (data->leds[i].type) {
330 case PCA9532_TYPE_NONE:
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700331 case PCA9532_TYPE_GPIO:
Axel Lin125c7132011-01-12 16:59:15 -0800332 break;
333 case PCA9532_TYPE_LED:
334 led_classdev_unregister(&data->leds[i].ldev);
Axel Lin125c7132011-01-12 16:59:15 -0800335 break;
336 case PCA9532_TYPE_N2100_BEEP:
337 if (data->idev != NULL) {
Axel Lin125c7132011-01-12 16:59:15 -0800338 cancel_work_sync(&data->work);
339 data->idev = NULL;
340 }
341 break;
342 }
343 }
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700344
345#ifdef CONFIG_LEDS_PCA9532_GPIO
Linus Walleij58383c782015-11-04 09:56:26 +0100346 if (data->gpio.parent)
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200347 gpiochip_remove(&data->gpio);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700348#endif
349
350 return 0;
Axel Lin125c7132011-01-12 16:59:15 -0800351}
352
Riku Voipioe14fa822008-05-31 14:43:41 +0100353static int pca9532_configure(struct i2c_client *client,
354 struct pca9532_data *data, struct pca9532_platform_data *pdata)
355{
356 int i, err = 0;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700357 int gpios = 0;
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700358 u8 maxleds = data->chip_info->num_leds;
Riku Voipioe14fa822008-05-31 14:43:41 +0100359
360 for (i = 0; i < 2; i++) {
361 data->pwm[i] = pdata->pwm[i];
362 data->psc[i] = pdata->psc[i];
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700363 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
Riku Voipioe14fa822008-05-31 14:43:41 +0100364 data->pwm[i]);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700365 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
Riku Voipioe14fa822008-05-31 14:43:41 +0100366 data->psc[i]);
367 }
368
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700369 for (i = 0; i < data->chip_info->num_leds; i++) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100370 struct pca9532_led *led = &data->leds[i];
371 struct pca9532_led *pled = &pdata->leds[i];
372 led->client = client;
373 led->id = i;
374 led->type = pled->type;
375 switch (led->type) {
376 case PCA9532_TYPE_NONE:
377 break;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700378 case PCA9532_TYPE_GPIO:
379 gpios++;
380 break;
Riku Voipioe14fa822008-05-31 14:43:41 +0100381 case PCA9532_TYPE_LED:
Felix Brack28c5fe92017-04-13 09:51:38 +0200382 if (pled->state == PCA9532_KEEP)
383 led->state = pca9532_getled(led);
384 else
385 led->state = pled->state;
Richard Purdie85c52042009-09-07 14:35:04 +0100386 led->name = pled->name;
Riku Voipioe14fa822008-05-31 14:43:41 +0100387 led->ldev.name = led->name;
Felix Brack90a55372016-10-26 16:08:02 +0200388 led->ldev.default_trigger = pled->default_trigger;
Riku Voipioe14fa822008-05-31 14:43:41 +0100389 led->ldev.brightness = LED_OFF;
Andrew Lunn00a88a12015-08-20 12:08:10 +0200390 led->ldev.brightness_set_blocking =
391 pca9532_set_brightness;
Riku Voipioe14fa822008-05-31 14:43:41 +0100392 led->ldev.blink_set = pca9532_set_blink;
Sven Wegenerf785d022008-12-03 08:12:53 +0000393 err = led_classdev_register(&client->dev, &led->ldev);
394 if (err < 0) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100395 dev_err(&client->dev,
396 "couldn't register LED %s\n",
397 led->name);
398 goto exit;
399 }
400 pca9532_setled(led);
401 break;
402 case PCA9532_TYPE_N2100_BEEP:
403 BUG_ON(data->idev);
404 led->state = PCA9532_PWM1;
405 pca9532_setled(led);
Axel Lin8614fb42012-12-25 02:16:54 -0800406 data->idev = devm_input_allocate_device(&client->dev);
Riku Voipioe14fa822008-05-31 14:43:41 +0100407 if (data->idev == NULL) {
408 err = -ENOMEM;
409 goto exit;
410 }
411 data->idev->name = pled->name;
412 data->idev->phys = "i2c/pca9532";
413 data->idev->id.bustype = BUS_HOST;
Richard Purdie85c52042009-09-07 14:35:04 +0100414 data->idev->id.vendor = 0x001f;
Riku Voipioe14fa822008-05-31 14:43:41 +0100415 data->idev->id.product = 0x0001;
416 data->idev->id.version = 0x0100;
417 data->idev->evbit[0] = BIT_MASK(EV_SND);
418 data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
419 BIT_MASK(SND_TONE);
420 data->idev->event = pca9532_event;
421 input_set_drvdata(data->idev, data);
Antonio Ospite07172d22009-06-19 13:53:07 +0200422 INIT_WORK(&data->work, pca9532_input_work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100423 err = input_register_device(data->idev);
424 if (err) {
Antonio Ospite07172d22009-06-19 13:53:07 +0200425 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100426 data->idev = NULL;
427 goto exit;
428 }
429 break;
430 }
431 }
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700432
433#ifdef CONFIG_LEDS_PCA9532_GPIO
434 if (gpios) {
435 data->gpio.label = "gpio-pca9532";
436 data->gpio.direction_input = pca9532_gpio_direction_input;
437 data->gpio.direction_output = pca9532_gpio_direction_output;
438 data->gpio.set = pca9532_gpio_set_value;
439 data->gpio.get = pca9532_gpio_get_value;
440 data->gpio.request = pca9532_gpio_request_pin;
441 data->gpio.can_sleep = 1;
442 data->gpio.base = pdata->gpio_base;
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700443 data->gpio.ngpio = data->chip_info->num_leds;
Linus Walleij58383c782015-11-04 09:56:26 +0100444 data->gpio.parent = &client->dev;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700445 data->gpio.owner = THIS_MODULE;
446
Linus Walleijdced1462015-12-08 16:39:20 +0100447 err = gpiochip_add_data(&data->gpio, data);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700448 if (err) {
449 /* Use data->gpio.dev as a flag for freeing gpiochip */
Linus Walleij58383c782015-11-04 09:56:26 +0100450 data->gpio.parent = NULL;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700451 dev_warn(&client->dev, "could not add gpiochip\n");
452 } else {
453 dev_info(&client->dev, "gpios %i...%i\n",
454 data->gpio.base, data->gpio.base +
455 data->gpio.ngpio - 1);
456 }
457 }
458#endif
459
Riku Voipioe14fa822008-05-31 14:43:41 +0100460 return 0;
461
462exit:
Axel Lin125c7132011-01-12 16:59:15 -0800463 pca9532_destroy_devices(data, i);
Riku Voipioe14fa822008-05-31 14:43:41 +0100464 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100465}
466
Phil Reidfa4191a2016-06-14 15:36:17 +0800467static struct pca9532_platform_data *
468pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
469{
470 struct pca9532_platform_data *pdata;
471 struct device_node *child;
Phil Reidfa4191a2016-06-14 15:36:17 +0800472 int devid, maxleds;
473 int i = 0;
Felix Brack28c5fe92017-04-13 09:51:38 +0200474 const char *state;
Phil Reidfa4191a2016-06-14 15:36:17 +0800475
Stephen Boyd66c41132019-10-04 14:43:25 -0700476 devid = (int)(uintptr_t)of_device_get_match_data(dev);
Phil Reidfa4191a2016-06-14 15:36:17 +0800477 maxleds = pca9532_chip_info_tbl[devid].num_leds;
478
479 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
480 if (!pdata)
481 return ERR_PTR(-ENOMEM);
482
Stefan Riedmueller23a70042021-04-14 13:51:24 +0200483 pdata->gpio_base = -1;
484
Markus Moll7ac53382020-09-22 21:31:15 +0200485 of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
486 ARRAY_SIZE(pdata->pwm));
487 of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
488 ARRAY_SIZE(pdata->psc));
489
Marek BehĂșn99a013c2020-09-18 00:32:56 +0200490 for_each_available_child_of_node(np, child) {
Phil Reidfa4191a2016-06-14 15:36:17 +0800491 if (of_property_read_string(child, "label",
492 &pdata->leds[i].name))
493 pdata->leds[i].name = child->name;
494 of_property_read_u32(child, "type", &pdata->leds[i].type);
495 of_property_read_string(child, "linux,default-trigger",
496 &pdata->leds[i].default_trigger);
Felix Brack28c5fe92017-04-13 09:51:38 +0200497 if (!of_property_read_string(child, "default-state", &state)) {
498 if (!strcmp(state, "on"))
499 pdata->leds[i].state = PCA9532_ON;
500 else if (!strcmp(state, "keep"))
501 pdata->leds[i].state = PCA9532_KEEP;
502 }
Phil Reidfa4191a2016-06-14 15:36:17 +0800503 if (++i >= maxleds) {
504 of_node_put(child);
505 break;
506 }
507 }
508
509 return pdata;
510}
511
Riku Voipioe14fa822008-05-31 14:43:41 +0100512static int pca9532_probe(struct i2c_client *client,
513 const struct i2c_device_id *id)
514{
Phil Reidfa4191a2016-06-14 15:36:17 +0800515 int devid;
Riku Voipioe14fa822008-05-31 14:43:41 +0100516 struct pca9532_data *data = i2c_get_clientdata(client);
Jingoo Han87aae1e2013-07-30 01:07:35 -0700517 struct pca9532_platform_data *pca9532_pdata =
518 dev_get_platdata(&client->dev);
Marek BehĂșn8853c95e92020-09-18 00:32:54 +0200519 struct device_node *np = dev_of_node(&client->dev);
Sven Wegenerf785d022008-12-03 08:12:53 +0000520
Phil Reidfa4191a2016-06-14 15:36:17 +0800521 if (!pca9532_pdata) {
522 if (np) {
523 pca9532_pdata =
524 pca9532_of_populate_pdata(&client->dev, np);
525 if (IS_ERR(pca9532_pdata))
526 return PTR_ERR(pca9532_pdata);
527 } else {
528 dev_err(&client->dev, "no platform data\n");
529 return -EINVAL;
530 }
Stephen Boyd66c41132019-10-04 14:43:25 -0700531 devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
Phil Reidfa4191a2016-06-14 15:36:17 +0800532 } else {
533 devid = id->driver_data;
534 }
Riku Voipioe14fa822008-05-31 14:43:41 +0100535
536 if (!i2c_check_functionality(client->adapter,
537 I2C_FUNC_SMBUS_BYTE_DATA))
538 return -EIO;
539
Bryan Wu0f4630c2012-07-04 11:59:19 +0800540 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Riku Voipioe14fa822008-05-31 14:43:41 +0100541 if (!data)
542 return -ENOMEM;
543
Phil Reidfa4191a2016-06-14 15:36:17 +0800544 data->chip_info = &pca9532_chip_info_tbl[devid];
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700545
Riku Voipioe14fa822008-05-31 14:43:41 +0100546 dev_info(&client->dev, "setting platform data\n");
547 i2c_set_clientdata(client, data);
548 data->client = client;
549 mutex_init(&data->update_lock);
550
Bryan Wu0f4630c2012-07-04 11:59:19 +0800551 return pca9532_configure(client, data, pca9532_pdata);
Riku Voipioe14fa822008-05-31 14:43:41 +0100552}
553
554static int pca9532_remove(struct i2c_client *client)
555{
556 struct pca9532_data *data = i2c_get_clientdata(client);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700557
Liu Shixinc4241abcd2020-09-19 18:08:53 +0800558 return pca9532_destroy_devices(data, data->chip_info->num_leds);
Riku Voipioe14fa822008-05-31 14:43:41 +0100559}
560
Axel Lin09a0d182012-01-10 15:09:27 -0800561module_i2c_driver(pca9532_driver);
Riku Voipioe14fa822008-05-31 14:43:41 +0100562
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200563MODULE_AUTHOR("Riku Voipio");
Riku Voipioe14fa822008-05-31 14:43:41 +0100564MODULE_LICENSE("GPL");
565MODULE_DESCRIPTION("PCA 9532 LED dimmer");