blob: bf8bb8fc007c63bb31e97d298ca826fb4974c39e [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>
Riku Voipioe14fa822008-05-31 14:43:41 +010021
Jan Weitzel3dbf6222011-05-24 17:13:26 -070022/* m = num_leds*/
23#define PCA9532_REG_INPUT(i) ((i) >> 3)
24#define PCA9532_REG_OFFSET(m) ((m) >> 4)
25#define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
26#define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
27#define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
28#define LED_NUM(led) (led & 0x3)
Markus Moll2a378852020-09-22 21:28:35 +020029#define LED_SHIFT(led) (LED_NUM(led) * 2)
30#define LED_MASK(led) (0x3 << LED_SHIFT(led))
Riku Voipioe14fa822008-05-31 14:43:41 +010031
32#define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
33
Jan Weitzel3dbf6222011-05-24 17:13:26 -070034struct pca9532_chip_info {
35 u8 num_leds;
36};
37
Riku Voipioe14fa822008-05-31 14:43:41 +010038struct pca9532_data {
39 struct i2c_client *client;
40 struct pca9532_led leds[16];
41 struct mutex update_lock;
Richard Purdie85c52042009-09-07 14:35:04 +010042 struct input_dev *idev;
Antonio Ospite07172d22009-06-19 13:53:07 +020043 struct work_struct work;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -070044#ifdef CONFIG_LEDS_PCA9532_GPIO
45 struct gpio_chip gpio;
46#endif
Jan Weitzel3dbf6222011-05-24 17:13:26 -070047 const struct pca9532_chip_info *chip_info;
Riku Voipioe14fa822008-05-31 14:43:41 +010048 u8 pwm[2];
49 u8 psc[2];
50};
51
Uwe Kleine-König1e1e6672022-11-18 23:40:18 +010052static int pca9532_probe(struct i2c_client *client);
Uwe Kleine-Königed5c2f52022-08-15 10:02:30 +020053static void pca9532_remove(struct i2c_client *client);
Riku Voipioe14fa822008-05-31 14:43:41 +010054
Jan Weitzel3dbf6222011-05-24 17:13:26 -070055enum {
56 pca9530,
57 pca9531,
58 pca9532,
59 pca9533,
60};
61
Riku Voipioe14fa822008-05-31 14:43:41 +010062static const struct i2c_device_id pca9532_id[] = {
Jan Weitzel3dbf6222011-05-24 17:13:26 -070063 { "pca9530", pca9530 },
64 { "pca9531", pca9531 },
65 { "pca9532", pca9532 },
66 { "pca9533", pca9533 },
Riku Voipioe14fa822008-05-31 14:43:41 +010067 { }
68};
69
70MODULE_DEVICE_TABLE(i2c, pca9532_id);
71
Jan Weitzel3dbf6222011-05-24 17:13:26 -070072static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
73 [pca9530] = {
74 .num_leds = 2,
75 },
76 [pca9531] = {
77 .num_leds = 8,
78 },
79 [pca9532] = {
80 .num_leds = 16,
81 },
82 [pca9533] = {
83 .num_leds = 4,
84 },
85};
86
Phil Reidfa4191a2016-06-14 15:36:17 +080087#ifdef CONFIG_OF
88static const struct of_device_id of_pca9532_leds_match[] = {
89 { .compatible = "nxp,pca9530", .data = (void *)pca9530 },
90 { .compatible = "nxp,pca9531", .data = (void *)pca9531 },
91 { .compatible = "nxp,pca9532", .data = (void *)pca9532 },
92 { .compatible = "nxp,pca9533", .data = (void *)pca9533 },
93 {},
94};
95
96MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
97#endif
98
Riku Voipioe14fa822008-05-31 14:43:41 +010099static struct i2c_driver pca9532_driver = {
100 .driver = {
Wolfram Sang30cb35b2011-07-08 15:39:46 -0700101 .name = "leds-pca953x",
Phil Reidfa4191a2016-06-14 15:36:17 +0800102 .of_match_table = of_match_ptr(of_pca9532_leds_match),
Riku Voipioe14fa822008-05-31 14:43:41 +0100103 },
Uwe Kleine-Königd9ff8a82023-05-17 20:05:59 +0200104 .probe = pca9532_probe,
Riku Voipioe14fa822008-05-31 14:43:41 +0100105 .remove = pca9532_remove,
106 .id_table = pca9532_id,
107};
108
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300109/* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
Riku Voipioe14fa822008-05-31 14:43:41 +0100110 * the clever Thecus people are using one pwm to drive the beeper. So,
111 * as a compromise we average one pwm to the values requested by all
112 * leds that are not ON/OFF.
113 * */
Riku Voipio934cd3f2008-12-03 08:21:36 +0000114static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
Riku Voipioe14fa822008-05-31 14:43:41 +0100115 enum led_brightness value)
116{
117 int a = 0, b = 0, i = 0;
118 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700119 for (i = 0; i < data->chip_info->num_leds; i++) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100120 if (data->leds[i].type == PCA9532_TYPE_LED &&
121 data->leds[i].state == PCA9532_PWM0+pwm) {
122 a++;
123 b += data->leds[i].ldev.brightness;
124 }
125 }
126 if (a == 0) {
127 dev_err(&client->dev,
128 "fear of division by zero %d/%d, wanted %d\n",
129 b, a, value);
130 return -EINVAL;
131 }
132 b = b/a;
133 if (b > 0xFF)
134 return -EINVAL;
Riku Voipioe14fa822008-05-31 14:43:41 +0100135 data->pwm[pwm] = b;
Antonio Ospite07172d22009-06-19 13:53:07 +0200136 data->psc[pwm] = blink;
137 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000138}
139
140static int pca9532_setpwm(struct i2c_client *client, int pwm)
141{
Antonio Ospite07172d22009-06-19 13:53:07 +0200142 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700143 u8 maxleds = data->chip_info->num_leds;
144
Antonio Ospite07172d22009-06-19 13:53:07 +0200145 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700146 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
Riku Voipioe14fa822008-05-31 14:43:41 +0100147 data->pwm[pwm]);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700148 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
Riku Voipioe14fa822008-05-31 14:43:41 +0100149 data->psc[pwm]);
150 mutex_unlock(&data->update_lock);
151 return 0;
152}
153
154/* Set LED routing */
155static void pca9532_setled(struct pca9532_led *led)
156{
157 struct i2c_client *client = led->client;
158 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700159 u8 maxleds = data->chip_info->num_leds;
Riku Voipioe14fa822008-05-31 14:43:41 +0100160 char reg;
161
162 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700163 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
Riku Voipioe14fa822008-05-31 14:43:41 +0100164 /* zero led bits */
Markus Moll2a378852020-09-22 21:28:35 +0200165 reg = reg & ~LED_MASK(led->id);
Riku Voipioe14fa822008-05-31 14:43:41 +0100166 /* set the new value */
Markus Moll2a378852020-09-22 21:28:35 +0200167 reg = reg | (led->state << LED_SHIFT(led->id));
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700168 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
Riku Voipioe14fa822008-05-31 14:43:41 +0100169 mutex_unlock(&data->update_lock);
170}
171
Andrew Lunn00a88a12015-08-20 12:08:10 +0200172static int pca9532_set_brightness(struct led_classdev *led_cdev,
Riku Voipioe14fa822008-05-31 14:43:41 +0100173 enum led_brightness value)
174{
175 int err = 0;
176 struct pca9532_led *led = ldev_to_led(led_cdev);
177
178 if (value == LED_OFF)
179 led->state = PCA9532_OFF;
180 else if (value == LED_FULL)
181 led->state = PCA9532_ON;
182 else {
183 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
Antonio Ospite07172d22009-06-19 13:53:07 +0200184 err = pca9532_calcpwm(led->client, 0, 0, value);
Riku Voipioe14fa822008-05-31 14:43:41 +0100185 if (err)
Andrew Lunn00a88a12015-08-20 12:08:10 +0200186 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100187 }
Andrew Lunn00a88a12015-08-20 12:08:10 +0200188 if (led->state == PCA9532_PWM0)
189 pca9532_setpwm(led->client, 0);
190 pca9532_setled(led);
191 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100192}
193
194static int pca9532_set_blink(struct led_classdev *led_cdev,
195 unsigned long *delay_on, unsigned long *delay_off)
196{
197 struct pca9532_led *led = ldev_to_led(led_cdev);
198 struct i2c_client *client = led->client;
199 int psc;
Antonio Ospite07172d22009-06-19 13:53:07 +0200200 int err = 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100201
202 if (*delay_on == 0 && *delay_off == 0) {
Jingoo Han962363c2013-01-21 21:58:07 -0800203 /* led subsystem ask us for a blink rate */
Richard Purdie85c52042009-09-07 14:35:04 +0100204 *delay_on = 1000;
Riku Voipioe14fa822008-05-31 14:43:41 +0100205 *delay_off = 1000;
206 }
207 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
208 return -EINVAL;
209
210 /* Thecus specific: only use PSC/PWM 0 */
211 psc = (*delay_on * 152-1)/1000;
Antonio Ospite07172d22009-06-19 13:53:07 +0200212 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
213 if (err)
214 return err;
Andrew Lunn00a88a12015-08-20 12:08:10 +0200215 if (led->state == PCA9532_PWM0)
216 pca9532_setpwm(led->client, 0);
217 pca9532_setled(led);
218
Antonio Ospite07172d22009-06-19 13:53:07 +0200219 return 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100220}
221
Sven Wegener0d733572008-11-17 14:33:41 +0000222static int pca9532_event(struct input_dev *dev, unsigned int type,
223 unsigned int code, int value)
Riku Voipioe14fa822008-05-31 14:43:41 +0100224{
225 struct pca9532_data *data = input_get_drvdata(dev);
226
Riku Voipio7fbc3a92009-03-03 22:13:06 +0200227 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
Riku Voipioe14fa822008-05-31 14:43:41 +0100228 return -1;
229
230 /* XXX: allow different kind of beeps with psc/pwm modifications */
231 if (value > 1 && value < 32767)
232 data->pwm[1] = 127;
233 else
234 data->pwm[1] = 0;
235
Antonio Ospite07172d22009-06-19 13:53:07 +0200236 schedule_work(&data->work);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000237
Antonio Ospite07172d22009-06-19 13:53:07 +0200238 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000239}
240
241static void pca9532_input_work(struct work_struct *work)
242{
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700243 struct pca9532_data *data =
244 container_of(work, struct pca9532_data, work);
245 u8 maxleds = data->chip_info->num_leds;
246
Riku Voipioe14fa822008-05-31 14:43:41 +0100247 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700248 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
Riku Voipioe14fa822008-05-31 14:43:41 +0100249 data->pwm[1]);
250 mutex_unlock(&data->update_lock);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000251}
Riku Voipioe14fa822008-05-31 14:43:41 +0100252
Felix Brack28c5fe92017-04-13 09:51:38 +0200253static enum pca9532_state pca9532_getled(struct pca9532_led *led)
254{
255 struct i2c_client *client = led->client;
256 struct pca9532_data *data = i2c_get_clientdata(client);
257 u8 maxleds = data->chip_info->num_leds;
258 char reg;
259 enum pca9532_state ret;
260
261 mutex_lock(&data->update_lock);
262 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
Markus Moll2a378852020-09-22 21:28:35 +0200263 ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
Felix Brack28c5fe92017-04-13 09:51:38 +0200264 mutex_unlock(&data->update_lock);
265 return ret;
266}
267
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700268#ifdef CONFIG_LEDS_PCA9532_GPIO
269static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
270{
Linus Walleijdced1462015-12-08 16:39:20 +0100271 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700272 struct pca9532_led *led = &data->leds[offset];
273
274 if (led->type == PCA9532_TYPE_GPIO)
275 return 0;
276
277 return -EBUSY;
278}
279
280static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
281{
Linus Walleijdced1462015-12-08 16:39:20 +0100282 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700283 struct pca9532_led *led = &data->leds[offset];
284
285 if (val)
286 led->state = PCA9532_ON;
287 else
288 led->state = PCA9532_OFF;
289
290 pca9532_setled(led);
291}
292
293static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
294{
Linus Walleijdced1462015-12-08 16:39:20 +0100295 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700296 unsigned char reg;
297
298 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
299
300 return !!(reg & (1 << (offset % 8)));
301}
302
303static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
304{
305 /* To use as input ensure pin is not driven */
306 pca9532_gpio_set_value(gc, offset, 0);
307
308 return 0;
309}
310
311static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
312{
313 pca9532_gpio_set_value(gc, offset, val);
314
315 return 0;
316}
317#endif /* CONFIG_LEDS_PCA9532_GPIO */
318
Uwe Kleine-Königc55f75c2021-10-22 12:17:07 +0200319static void pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
Axel Lin125c7132011-01-12 16:59:15 -0800320{
321 int i = n_devs;
322
Axel Lin125c7132011-01-12 16:59:15 -0800323 while (--i >= 0) {
324 switch (data->leds[i].type) {
325 case PCA9532_TYPE_NONE:
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700326 case PCA9532_TYPE_GPIO:
Axel Lin125c7132011-01-12 16:59:15 -0800327 break;
328 case PCA9532_TYPE_LED:
329 led_classdev_unregister(&data->leds[i].ldev);
Axel Lin125c7132011-01-12 16:59:15 -0800330 break;
331 case PCA9532_TYPE_N2100_BEEP:
332 if (data->idev != NULL) {
Axel Lin125c7132011-01-12 16:59:15 -0800333 cancel_work_sync(&data->work);
334 data->idev = NULL;
335 }
336 break;
337 }
338 }
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700339
340#ifdef CONFIG_LEDS_PCA9532_GPIO
Linus Walleij58383c782015-11-04 09:56:26 +0100341 if (data->gpio.parent)
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200342 gpiochip_remove(&data->gpio);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700343#endif
Axel Lin125c7132011-01-12 16:59:15 -0800344}
345
Riku Voipioe14fa822008-05-31 14:43:41 +0100346static int pca9532_configure(struct i2c_client *client,
347 struct pca9532_data *data, struct pca9532_platform_data *pdata)
348{
349 int i, err = 0;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700350 int gpios = 0;
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700351 u8 maxleds = data->chip_info->num_leds;
Riku Voipioe14fa822008-05-31 14:43:41 +0100352
353 for (i = 0; i < 2; i++) {
354 data->pwm[i] = pdata->pwm[i];
355 data->psc[i] = pdata->psc[i];
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700356 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
Riku Voipioe14fa822008-05-31 14:43:41 +0100357 data->pwm[i]);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700358 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
Riku Voipioe14fa822008-05-31 14:43:41 +0100359 data->psc[i]);
360 }
361
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700362 for (i = 0; i < data->chip_info->num_leds; i++) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100363 struct pca9532_led *led = &data->leds[i];
364 struct pca9532_led *pled = &pdata->leds[i];
365 led->client = client;
366 led->id = i;
367 led->type = pled->type;
368 switch (led->type) {
369 case PCA9532_TYPE_NONE:
370 break;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700371 case PCA9532_TYPE_GPIO:
372 gpios++;
373 break;
Riku Voipioe14fa822008-05-31 14:43:41 +0100374 case PCA9532_TYPE_LED:
Felix Brack28c5fe92017-04-13 09:51:38 +0200375 if (pled->state == PCA9532_KEEP)
376 led->state = pca9532_getled(led);
377 else
378 led->state = pled->state;
Richard Purdie85c52042009-09-07 14:35:04 +0100379 led->name = pled->name;
Riku Voipioe14fa822008-05-31 14:43:41 +0100380 led->ldev.name = led->name;
Felix Brack90a55372016-10-26 16:08:02 +0200381 led->ldev.default_trigger = pled->default_trigger;
Riku Voipioe14fa822008-05-31 14:43:41 +0100382 led->ldev.brightness = LED_OFF;
Andrew Lunn00a88a12015-08-20 12:08:10 +0200383 led->ldev.brightness_set_blocking =
384 pca9532_set_brightness;
Riku Voipioe14fa822008-05-31 14:43:41 +0100385 led->ldev.blink_set = pca9532_set_blink;
Sven Wegenerf785d022008-12-03 08:12:53 +0000386 err = led_classdev_register(&client->dev, &led->ldev);
387 if (err < 0) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100388 dev_err(&client->dev,
389 "couldn't register LED %s\n",
390 led->name);
391 goto exit;
392 }
393 pca9532_setled(led);
394 break;
395 case PCA9532_TYPE_N2100_BEEP:
396 BUG_ON(data->idev);
397 led->state = PCA9532_PWM1;
398 pca9532_setled(led);
Axel Lin8614fb42012-12-25 02:16:54 -0800399 data->idev = devm_input_allocate_device(&client->dev);
Riku Voipioe14fa822008-05-31 14:43:41 +0100400 if (data->idev == NULL) {
401 err = -ENOMEM;
402 goto exit;
403 }
404 data->idev->name = pled->name;
405 data->idev->phys = "i2c/pca9532";
406 data->idev->id.bustype = BUS_HOST;
Richard Purdie85c52042009-09-07 14:35:04 +0100407 data->idev->id.vendor = 0x001f;
Riku Voipioe14fa822008-05-31 14:43:41 +0100408 data->idev->id.product = 0x0001;
409 data->idev->id.version = 0x0100;
410 data->idev->evbit[0] = BIT_MASK(EV_SND);
411 data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
412 BIT_MASK(SND_TONE);
413 data->idev->event = pca9532_event;
414 input_set_drvdata(data->idev, data);
Antonio Ospite07172d22009-06-19 13:53:07 +0200415 INIT_WORK(&data->work, pca9532_input_work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100416 err = input_register_device(data->idev);
417 if (err) {
Antonio Ospite07172d22009-06-19 13:53:07 +0200418 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100419 data->idev = NULL;
420 goto exit;
421 }
422 break;
423 }
424 }
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700425
426#ifdef CONFIG_LEDS_PCA9532_GPIO
427 if (gpios) {
428 data->gpio.label = "gpio-pca9532";
429 data->gpio.direction_input = pca9532_gpio_direction_input;
430 data->gpio.direction_output = pca9532_gpio_direction_output;
431 data->gpio.set = pca9532_gpio_set_value;
432 data->gpio.get = pca9532_gpio_get_value;
433 data->gpio.request = pca9532_gpio_request_pin;
434 data->gpio.can_sleep = 1;
435 data->gpio.base = pdata->gpio_base;
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700436 data->gpio.ngpio = data->chip_info->num_leds;
Linus Walleij58383c782015-11-04 09:56:26 +0100437 data->gpio.parent = &client->dev;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700438 data->gpio.owner = THIS_MODULE;
439
Linus Walleijdced1462015-12-08 16:39:20 +0100440 err = gpiochip_add_data(&data->gpio, data);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700441 if (err) {
442 /* Use data->gpio.dev as a flag for freeing gpiochip */
Linus Walleij58383c782015-11-04 09:56:26 +0100443 data->gpio.parent = NULL;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700444 dev_warn(&client->dev, "could not add gpiochip\n");
445 } else {
446 dev_info(&client->dev, "gpios %i...%i\n",
447 data->gpio.base, data->gpio.base +
448 data->gpio.ngpio - 1);
449 }
450 }
451#endif
452
Riku Voipioe14fa822008-05-31 14:43:41 +0100453 return 0;
454
455exit:
Axel Lin125c7132011-01-12 16:59:15 -0800456 pca9532_destroy_devices(data, i);
Riku Voipioe14fa822008-05-31 14:43:41 +0100457 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100458}
459
Phil Reidfa4191a2016-06-14 15:36:17 +0800460static struct pca9532_platform_data *
461pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
462{
463 struct pca9532_platform_data *pdata;
464 struct device_node *child;
Phil Reidfa4191a2016-06-14 15:36:17 +0800465 int devid, maxleds;
466 int i = 0;
Felix Brack28c5fe92017-04-13 09:51:38 +0200467 const char *state;
Phil Reidfa4191a2016-06-14 15:36:17 +0800468
Stephen Boyd66c41132019-10-04 14:43:25 -0700469 devid = (int)(uintptr_t)of_device_get_match_data(dev);
Phil Reidfa4191a2016-06-14 15:36:17 +0800470 maxleds = pca9532_chip_info_tbl[devid].num_leds;
471
472 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
473 if (!pdata)
474 return ERR_PTR(-ENOMEM);
475
Stefan Riedmueller23a70042021-04-14 13:51:24 +0200476 pdata->gpio_base = -1;
477
Markus Moll7ac53382020-09-22 21:31:15 +0200478 of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
479 ARRAY_SIZE(pdata->pwm));
480 of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
481 ARRAY_SIZE(pdata->psc));
482
Marek Behún99a013c2020-09-18 00:32:56 +0200483 for_each_available_child_of_node(np, child) {
Phil Reidfa4191a2016-06-14 15:36:17 +0800484 if (of_property_read_string(child, "label",
485 &pdata->leds[i].name))
486 pdata->leds[i].name = child->name;
487 of_property_read_u32(child, "type", &pdata->leds[i].type);
488 of_property_read_string(child, "linux,default-trigger",
489 &pdata->leds[i].default_trigger);
Felix Brack28c5fe92017-04-13 09:51:38 +0200490 if (!of_property_read_string(child, "default-state", &state)) {
491 if (!strcmp(state, "on"))
492 pdata->leds[i].state = PCA9532_ON;
493 else if (!strcmp(state, "keep"))
494 pdata->leds[i].state = PCA9532_KEEP;
495 }
Phil Reidfa4191a2016-06-14 15:36:17 +0800496 if (++i >= maxleds) {
497 of_node_put(child);
498 break;
499 }
500 }
501
502 return pdata;
503}
504
Uwe Kleine-König1e1e6672022-11-18 23:40:18 +0100505static int pca9532_probe(struct i2c_client *client)
Riku Voipioe14fa822008-05-31 14:43:41 +0100506{
Uwe Kleine-König1e1e6672022-11-18 23:40:18 +0100507 const struct i2c_device_id *id = i2c_client_get_device_id(client);
Phil Reidfa4191a2016-06-14 15:36:17 +0800508 int devid;
Riku Voipioe14fa822008-05-31 14:43:41 +0100509 struct pca9532_data *data = i2c_get_clientdata(client);
Jingoo Han87aae1e2013-07-30 01:07:35 -0700510 struct pca9532_platform_data *pca9532_pdata =
511 dev_get_platdata(&client->dev);
Marek Behún8853c95e92020-09-18 00:32:54 +0200512 struct device_node *np = dev_of_node(&client->dev);
Sven Wegenerf785d022008-12-03 08:12:53 +0000513
Phil Reidfa4191a2016-06-14 15:36:17 +0800514 if (!pca9532_pdata) {
515 if (np) {
516 pca9532_pdata =
517 pca9532_of_populate_pdata(&client->dev, np);
518 if (IS_ERR(pca9532_pdata))
519 return PTR_ERR(pca9532_pdata);
520 } else {
521 dev_err(&client->dev, "no platform data\n");
522 return -EINVAL;
523 }
Stephen Boyd66c41132019-10-04 14:43:25 -0700524 devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
Phil Reidfa4191a2016-06-14 15:36:17 +0800525 } else {
526 devid = id->driver_data;
527 }
Riku Voipioe14fa822008-05-31 14:43:41 +0100528
529 if (!i2c_check_functionality(client->adapter,
530 I2C_FUNC_SMBUS_BYTE_DATA))
531 return -EIO;
532
Bryan Wu0f4630c2012-07-04 11:59:19 +0800533 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Riku Voipioe14fa822008-05-31 14:43:41 +0100534 if (!data)
535 return -ENOMEM;
536
Phil Reidfa4191a2016-06-14 15:36:17 +0800537 data->chip_info = &pca9532_chip_info_tbl[devid];
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700538
Riku Voipioe14fa822008-05-31 14:43:41 +0100539 dev_info(&client->dev, "setting platform data\n");
540 i2c_set_clientdata(client, data);
541 data->client = client;
542 mutex_init(&data->update_lock);
543
Bryan Wu0f4630c2012-07-04 11:59:19 +0800544 return pca9532_configure(client, data, pca9532_pdata);
Riku Voipioe14fa822008-05-31 14:43:41 +0100545}
546
Uwe Kleine-Königed5c2f52022-08-15 10:02:30 +0200547static void pca9532_remove(struct i2c_client *client)
Riku Voipioe14fa822008-05-31 14:43:41 +0100548{
549 struct pca9532_data *data = i2c_get_clientdata(client);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700550
Uwe Kleine-Königc55f75c2021-10-22 12:17:07 +0200551 pca9532_destroy_devices(data, data->chip_info->num_leds);
Riku Voipioe14fa822008-05-31 14:43:41 +0100552}
553
Axel Lin09a0d182012-01-10 15:09:27 -0800554module_i2c_driver(pca9532_driver);
Riku Voipioe14fa822008-05-31 14:43:41 +0100555
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200556MODULE_AUTHOR("Riku Voipio");
Riku Voipioe14fa822008-05-31 14:43:41 +0100557MODULE_LICENSE("GPL");
558MODULE_DESCRIPTION("PCA 9532 LED dimmer");