blob: 7fea18b0c15d115178c874163232fa6c2e3706ce [file] [log] [blame]
Riku Voipioe14fa822008-05-31 14:43:41 +01001/*
2 * pca9532.c - 16-bit Led dimmer
3 *
Jan Weitzel3dbf6222011-05-24 17:13:26 -07004 * Copyright (C) 2011 Jan Weitzel
Riku Voipiob26e0ed2009-03-03 21:37:17 +02005 * Copyright (C) 2008 Riku Voipio
Riku Voipioe14fa822008-05-31 14:43:41 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
Jan Weitzel3dbf6222011-05-24 17:13:26 -070011 * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
Riku Voipioe14fa822008-05-31 14:43:41 +010012 *
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010018#include <linux/leds.h>
19#include <linux/input.h>
20#include <linux/mutex.h>
Riku Voipio934cd3f2008-12-03 08:21:36 +000021#include <linux/workqueue.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010022#include <linux/leds-pca9532.h>
Joachim Eastwood3c1ab502011-05-24 17:13:23 -070023#include <linux/gpio.h>
Phil Reidfa4191a2016-06-14 15:36:17 +080024#include <linux/of.h>
25#include <linux/of_device.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010026
Jan Weitzel3dbf6222011-05-24 17:13:26 -070027/* m = num_leds*/
28#define PCA9532_REG_INPUT(i) ((i) >> 3)
29#define PCA9532_REG_OFFSET(m) ((m) >> 4)
30#define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
31#define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
32#define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
33#define LED_NUM(led) (led & 0x3)
Riku Voipioe14fa822008-05-31 14:43:41 +010034
35#define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
36
Jan Weitzel3dbf6222011-05-24 17:13:26 -070037struct pca9532_chip_info {
38 u8 num_leds;
39};
40
Riku Voipioe14fa822008-05-31 14:43:41 +010041struct pca9532_data {
42 struct i2c_client *client;
43 struct pca9532_led leds[16];
44 struct mutex update_lock;
Richard Purdie85c52042009-09-07 14:35:04 +010045 struct input_dev *idev;
Antonio Ospite07172d22009-06-19 13:53:07 +020046 struct work_struct work;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -070047#ifdef CONFIG_LEDS_PCA9532_GPIO
48 struct gpio_chip gpio;
49#endif
Jan Weitzel3dbf6222011-05-24 17:13:26 -070050 const struct pca9532_chip_info *chip_info;
Riku Voipioe14fa822008-05-31 14:43:41 +010051 u8 pwm[2];
52 u8 psc[2];
53};
54
55static int pca9532_probe(struct i2c_client *client,
56 const struct i2c_device_id *id);
57static int pca9532_remove(struct i2c_client *client);
58
Jan Weitzel3dbf6222011-05-24 17:13:26 -070059enum {
60 pca9530,
61 pca9531,
62 pca9532,
63 pca9533,
64};
65
Riku Voipioe14fa822008-05-31 14:43:41 +010066static const struct i2c_device_id pca9532_id[] = {
Jan Weitzel3dbf6222011-05-24 17:13:26 -070067 { "pca9530", pca9530 },
68 { "pca9531", pca9531 },
69 { "pca9532", pca9532 },
70 { "pca9533", pca9533 },
Riku Voipioe14fa822008-05-31 14:43:41 +010071 { }
72};
73
74MODULE_DEVICE_TABLE(i2c, pca9532_id);
75
Jan Weitzel3dbf6222011-05-24 17:13:26 -070076static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
77 [pca9530] = {
78 .num_leds = 2,
79 },
80 [pca9531] = {
81 .num_leds = 8,
82 },
83 [pca9532] = {
84 .num_leds = 16,
85 },
86 [pca9533] = {
87 .num_leds = 4,
88 },
89};
90
Phil Reidfa4191a2016-06-14 15:36:17 +080091#ifdef CONFIG_OF
92static const struct of_device_id of_pca9532_leds_match[] = {
93 { .compatible = "nxp,pca9530", .data = (void *)pca9530 },
94 { .compatible = "nxp,pca9531", .data = (void *)pca9531 },
95 { .compatible = "nxp,pca9532", .data = (void *)pca9532 },
96 { .compatible = "nxp,pca9533", .data = (void *)pca9533 },
97 {},
98};
99
100MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
101#endif
102
Riku Voipioe14fa822008-05-31 14:43:41 +0100103static struct i2c_driver pca9532_driver = {
104 .driver = {
Wolfram Sang30cb35b2011-07-08 15:39:46 -0700105 .name = "leds-pca953x",
Phil Reidfa4191a2016-06-14 15:36:17 +0800106 .of_match_table = of_match_ptr(of_pca9532_leds_match),
Riku Voipioe14fa822008-05-31 14:43:41 +0100107 },
Richard Purdie85c52042009-09-07 14:35:04 +0100108 .probe = pca9532_probe,
Riku Voipioe14fa822008-05-31 14:43:41 +0100109 .remove = pca9532_remove,
110 .id_table = pca9532_id,
111};
112
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300113/* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
Riku Voipioe14fa822008-05-31 14:43:41 +0100114 * the clever Thecus people are using one pwm to drive the beeper. So,
115 * as a compromise we average one pwm to the values requested by all
116 * leds that are not ON/OFF.
117 * */
Riku Voipio934cd3f2008-12-03 08:21:36 +0000118static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
Riku Voipioe14fa822008-05-31 14:43:41 +0100119 enum led_brightness value)
120{
121 int a = 0, b = 0, i = 0;
122 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700123 for (i = 0; i < data->chip_info->num_leds; i++) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100124 if (data->leds[i].type == PCA9532_TYPE_LED &&
125 data->leds[i].state == PCA9532_PWM0+pwm) {
126 a++;
127 b += data->leds[i].ldev.brightness;
128 }
129 }
130 if (a == 0) {
131 dev_err(&client->dev,
132 "fear of division by zero %d/%d, wanted %d\n",
133 b, a, value);
134 return -EINVAL;
135 }
136 b = b/a;
137 if (b > 0xFF)
138 return -EINVAL;
Riku Voipioe14fa822008-05-31 14:43:41 +0100139 data->pwm[pwm] = b;
Antonio Ospite07172d22009-06-19 13:53:07 +0200140 data->psc[pwm] = blink;
141 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000142}
143
144static int pca9532_setpwm(struct i2c_client *client, int pwm)
145{
Antonio Ospite07172d22009-06-19 13:53:07 +0200146 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700147 u8 maxleds = data->chip_info->num_leds;
148
Antonio Ospite07172d22009-06-19 13:53:07 +0200149 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700150 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
Riku Voipioe14fa822008-05-31 14:43:41 +0100151 data->pwm[pwm]);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700152 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
Riku Voipioe14fa822008-05-31 14:43:41 +0100153 data->psc[pwm]);
154 mutex_unlock(&data->update_lock);
155 return 0;
156}
157
158/* Set LED routing */
159static void pca9532_setled(struct pca9532_led *led)
160{
161 struct i2c_client *client = led->client;
162 struct pca9532_data *data = i2c_get_clientdata(client);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700163 u8 maxleds = data->chip_info->num_leds;
Riku Voipioe14fa822008-05-31 14:43:41 +0100164 char reg;
165
166 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700167 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
Riku Voipioe14fa822008-05-31 14:43:41 +0100168 /* zero led bits */
169 reg = reg & ~(0x3<<LED_NUM(led->id)*2);
170 /* set the new value */
171 reg = reg | (led->state << LED_NUM(led->id)*2);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700172 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
Riku Voipioe14fa822008-05-31 14:43:41 +0100173 mutex_unlock(&data->update_lock);
174}
175
Andrew Lunn00a88a12015-08-20 12:08:10 +0200176static int pca9532_set_brightness(struct led_classdev *led_cdev,
Riku Voipioe14fa822008-05-31 14:43:41 +0100177 enum led_brightness value)
178{
179 int err = 0;
180 struct pca9532_led *led = ldev_to_led(led_cdev);
181
182 if (value == LED_OFF)
183 led->state = PCA9532_OFF;
184 else if (value == LED_FULL)
185 led->state = PCA9532_ON;
186 else {
187 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
Antonio Ospite07172d22009-06-19 13:53:07 +0200188 err = pca9532_calcpwm(led->client, 0, 0, value);
Riku Voipioe14fa822008-05-31 14:43:41 +0100189 if (err)
Andrew Lunn00a88a12015-08-20 12:08:10 +0200190 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100191 }
Andrew Lunn00a88a12015-08-20 12:08:10 +0200192 if (led->state == PCA9532_PWM0)
193 pca9532_setpwm(led->client, 0);
194 pca9532_setled(led);
195 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100196}
197
198static int pca9532_set_blink(struct led_classdev *led_cdev,
199 unsigned long *delay_on, unsigned long *delay_off)
200{
201 struct pca9532_led *led = ldev_to_led(led_cdev);
202 struct i2c_client *client = led->client;
203 int psc;
Antonio Ospite07172d22009-06-19 13:53:07 +0200204 int err = 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100205
206 if (*delay_on == 0 && *delay_off == 0) {
Jingoo Han962363c2013-01-21 21:58:07 -0800207 /* led subsystem ask us for a blink rate */
Richard Purdie85c52042009-09-07 14:35:04 +0100208 *delay_on = 1000;
Riku Voipioe14fa822008-05-31 14:43:41 +0100209 *delay_off = 1000;
210 }
211 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
212 return -EINVAL;
213
214 /* Thecus specific: only use PSC/PWM 0 */
215 psc = (*delay_on * 152-1)/1000;
Antonio Ospite07172d22009-06-19 13:53:07 +0200216 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
217 if (err)
218 return err;
Andrew Lunn00a88a12015-08-20 12:08:10 +0200219 if (led->state == PCA9532_PWM0)
220 pca9532_setpwm(led->client, 0);
221 pca9532_setled(led);
222
Antonio Ospite07172d22009-06-19 13:53:07 +0200223 return 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100224}
225
Sven Wegener0d733572008-11-17 14:33:41 +0000226static int pca9532_event(struct input_dev *dev, unsigned int type,
227 unsigned int code, int value)
Riku Voipioe14fa822008-05-31 14:43:41 +0100228{
229 struct pca9532_data *data = input_get_drvdata(dev);
230
Riku Voipio7fbc3a92009-03-03 22:13:06 +0200231 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
Riku Voipioe14fa822008-05-31 14:43:41 +0100232 return -1;
233
234 /* XXX: allow different kind of beeps with psc/pwm modifications */
235 if (value > 1 && value < 32767)
236 data->pwm[1] = 127;
237 else
238 data->pwm[1] = 0;
239
Antonio Ospite07172d22009-06-19 13:53:07 +0200240 schedule_work(&data->work);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000241
Antonio Ospite07172d22009-06-19 13:53:07 +0200242 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000243}
244
245static void pca9532_input_work(struct work_struct *work)
246{
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700247 struct pca9532_data *data =
248 container_of(work, struct pca9532_data, work);
249 u8 maxleds = data->chip_info->num_leds;
250
Riku Voipioe14fa822008-05-31 14:43:41 +0100251 mutex_lock(&data->update_lock);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700252 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
Riku Voipioe14fa822008-05-31 14:43:41 +0100253 data->pwm[1]);
254 mutex_unlock(&data->update_lock);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000255}
Riku Voipioe14fa822008-05-31 14:43:41 +0100256
Felix Brack28c5fe92017-04-13 09:51:38 +0200257static enum pca9532_state pca9532_getled(struct pca9532_led *led)
258{
259 struct i2c_client *client = led->client;
260 struct pca9532_data *data = i2c_get_clientdata(client);
261 u8 maxleds = data->chip_info->num_leds;
262 char reg;
263 enum pca9532_state ret;
264
265 mutex_lock(&data->update_lock);
266 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
267 ret = reg >> LED_NUM(led->id)/2;
268 mutex_unlock(&data->update_lock);
269 return ret;
270}
271
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700272#ifdef CONFIG_LEDS_PCA9532_GPIO
273static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
274{
Linus Walleijdced1462015-12-08 16:39:20 +0100275 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700276 struct pca9532_led *led = &data->leds[offset];
277
278 if (led->type == PCA9532_TYPE_GPIO)
279 return 0;
280
281 return -EBUSY;
282}
283
284static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
285{
Linus Walleijdced1462015-12-08 16:39:20 +0100286 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700287 struct pca9532_led *led = &data->leds[offset];
288
289 if (val)
290 led->state = PCA9532_ON;
291 else
292 led->state = PCA9532_OFF;
293
294 pca9532_setled(led);
295}
296
297static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
298{
Linus Walleijdced1462015-12-08 16:39:20 +0100299 struct pca9532_data *data = gpiochip_get_data(gc);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700300 unsigned char reg;
301
302 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
303
304 return !!(reg & (1 << (offset % 8)));
305}
306
307static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
308{
309 /* To use as input ensure pin is not driven */
310 pca9532_gpio_set_value(gc, offset, 0);
311
312 return 0;
313}
314
315static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
316{
317 pca9532_gpio_set_value(gc, offset, val);
318
319 return 0;
320}
321#endif /* CONFIG_LEDS_PCA9532_GPIO */
322
323static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
Axel Lin125c7132011-01-12 16:59:15 -0800324{
325 int i = n_devs;
326
327 if (!data)
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700328 return -EINVAL;
Axel Lin125c7132011-01-12 16:59:15 -0800329
330 while (--i >= 0) {
331 switch (data->leds[i].type) {
332 case PCA9532_TYPE_NONE:
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700333 case PCA9532_TYPE_GPIO:
Axel Lin125c7132011-01-12 16:59:15 -0800334 break;
335 case PCA9532_TYPE_LED:
336 led_classdev_unregister(&data->leds[i].ldev);
Axel Lin125c7132011-01-12 16:59:15 -0800337 break;
338 case PCA9532_TYPE_N2100_BEEP:
339 if (data->idev != NULL) {
Axel Lin125c7132011-01-12 16:59:15 -0800340 cancel_work_sync(&data->work);
341 data->idev = NULL;
342 }
343 break;
344 }
345 }
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700346
347#ifdef CONFIG_LEDS_PCA9532_GPIO
Linus Walleij58383c782015-11-04 09:56:26 +0100348 if (data->gpio.parent)
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200349 gpiochip_remove(&data->gpio);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700350#endif
351
352 return 0;
Axel Lin125c7132011-01-12 16:59:15 -0800353}
354
Riku Voipioe14fa822008-05-31 14:43:41 +0100355static int pca9532_configure(struct i2c_client *client,
356 struct pca9532_data *data, struct pca9532_platform_data *pdata)
357{
358 int i, err = 0;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700359 int gpios = 0;
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700360 u8 maxleds = data->chip_info->num_leds;
Riku Voipioe14fa822008-05-31 14:43:41 +0100361
362 for (i = 0; i < 2; i++) {
363 data->pwm[i] = pdata->pwm[i];
364 data->psc[i] = pdata->psc[i];
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700365 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
Riku Voipioe14fa822008-05-31 14:43:41 +0100366 data->pwm[i]);
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700367 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
Riku Voipioe14fa822008-05-31 14:43:41 +0100368 data->psc[i]);
369 }
370
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700371 for (i = 0; i < data->chip_info->num_leds; i++) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100372 struct pca9532_led *led = &data->leds[i];
373 struct pca9532_led *pled = &pdata->leds[i];
374 led->client = client;
375 led->id = i;
376 led->type = pled->type;
377 switch (led->type) {
378 case PCA9532_TYPE_NONE:
379 break;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700380 case PCA9532_TYPE_GPIO:
381 gpios++;
382 break;
Riku Voipioe14fa822008-05-31 14:43:41 +0100383 case PCA9532_TYPE_LED:
Felix Brack28c5fe92017-04-13 09:51:38 +0200384 if (pled->state == PCA9532_KEEP)
385 led->state = pca9532_getled(led);
386 else
387 led->state = pled->state;
Richard Purdie85c52042009-09-07 14:35:04 +0100388 led->name = pled->name;
Riku Voipioe14fa822008-05-31 14:43:41 +0100389 led->ldev.name = led->name;
Felix Brack90a55372016-10-26 16:08:02 +0200390 led->ldev.default_trigger = pled->default_trigger;
Riku Voipioe14fa822008-05-31 14:43:41 +0100391 led->ldev.brightness = LED_OFF;
Andrew Lunn00a88a12015-08-20 12:08:10 +0200392 led->ldev.brightness_set_blocking =
393 pca9532_set_brightness;
Riku Voipioe14fa822008-05-31 14:43:41 +0100394 led->ldev.blink_set = pca9532_set_blink;
Sven Wegenerf785d022008-12-03 08:12:53 +0000395 err = led_classdev_register(&client->dev, &led->ldev);
396 if (err < 0) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100397 dev_err(&client->dev,
398 "couldn't register LED %s\n",
399 led->name);
400 goto exit;
401 }
402 pca9532_setled(led);
403 break;
404 case PCA9532_TYPE_N2100_BEEP:
405 BUG_ON(data->idev);
406 led->state = PCA9532_PWM1;
407 pca9532_setled(led);
Axel Lin8614fb42012-12-25 02:16:54 -0800408 data->idev = devm_input_allocate_device(&client->dev);
Riku Voipioe14fa822008-05-31 14:43:41 +0100409 if (data->idev == NULL) {
410 err = -ENOMEM;
411 goto exit;
412 }
413 data->idev->name = pled->name;
414 data->idev->phys = "i2c/pca9532";
415 data->idev->id.bustype = BUS_HOST;
Richard Purdie85c52042009-09-07 14:35:04 +0100416 data->idev->id.vendor = 0x001f;
Riku Voipioe14fa822008-05-31 14:43:41 +0100417 data->idev->id.product = 0x0001;
418 data->idev->id.version = 0x0100;
419 data->idev->evbit[0] = BIT_MASK(EV_SND);
420 data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
421 BIT_MASK(SND_TONE);
422 data->idev->event = pca9532_event;
423 input_set_drvdata(data->idev, data);
Antonio Ospite07172d22009-06-19 13:53:07 +0200424 INIT_WORK(&data->work, pca9532_input_work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100425 err = input_register_device(data->idev);
426 if (err) {
Antonio Ospite07172d22009-06-19 13:53:07 +0200427 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100428 data->idev = NULL;
429 goto exit;
430 }
431 break;
432 }
433 }
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700434
435#ifdef CONFIG_LEDS_PCA9532_GPIO
436 if (gpios) {
437 data->gpio.label = "gpio-pca9532";
438 data->gpio.direction_input = pca9532_gpio_direction_input;
439 data->gpio.direction_output = pca9532_gpio_direction_output;
440 data->gpio.set = pca9532_gpio_set_value;
441 data->gpio.get = pca9532_gpio_get_value;
442 data->gpio.request = pca9532_gpio_request_pin;
443 data->gpio.can_sleep = 1;
444 data->gpio.base = pdata->gpio_base;
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700445 data->gpio.ngpio = data->chip_info->num_leds;
Linus Walleij58383c782015-11-04 09:56:26 +0100446 data->gpio.parent = &client->dev;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700447 data->gpio.owner = THIS_MODULE;
448
Linus Walleijdced1462015-12-08 16:39:20 +0100449 err = gpiochip_add_data(&data->gpio, data);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700450 if (err) {
451 /* Use data->gpio.dev as a flag for freeing gpiochip */
Linus Walleij58383c782015-11-04 09:56:26 +0100452 data->gpio.parent = NULL;
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700453 dev_warn(&client->dev, "could not add gpiochip\n");
454 } else {
455 dev_info(&client->dev, "gpios %i...%i\n",
456 data->gpio.base, data->gpio.base +
457 data->gpio.ngpio - 1);
458 }
459 }
460#endif
461
Riku Voipioe14fa822008-05-31 14:43:41 +0100462 return 0;
463
464exit:
Axel Lin125c7132011-01-12 16:59:15 -0800465 pca9532_destroy_devices(data, i);
Riku Voipioe14fa822008-05-31 14:43:41 +0100466 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100467}
468
Phil Reidfa4191a2016-06-14 15:36:17 +0800469static struct pca9532_platform_data *
470pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
471{
472 struct pca9532_platform_data *pdata;
473 struct device_node *child;
474 const struct of_device_id *match;
475 int devid, maxleds;
476 int i = 0;
Felix Brack28c5fe92017-04-13 09:51:38 +0200477 const char *state;
Phil Reidfa4191a2016-06-14 15:36:17 +0800478
479 match = of_match_device(of_pca9532_leds_match, dev);
480 if (!match)
481 return ERR_PTR(-ENODEV);
482
483 devid = (int)(uintptr_t)match->data;
484 maxleds = pca9532_chip_info_tbl[devid].num_leds;
485
486 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
487 if (!pdata)
488 return ERR_PTR(-ENOMEM);
489
490 for_each_child_of_node(np, child) {
491 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);
Phil Reidfa4191a2016-06-14 15:36:17 +0800519 struct device_node *np = client->dev.of_node;
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 }
531 devid = (int)(uintptr_t)of_match_device(
532 of_pca9532_leds_match, &client->dev)->data;
533 } else {
534 devid = id->driver_data;
535 }
Riku Voipioe14fa822008-05-31 14:43:41 +0100536
537 if (!i2c_check_functionality(client->adapter,
538 I2C_FUNC_SMBUS_BYTE_DATA))
539 return -EIO;
540
Bryan Wu0f4630c2012-07-04 11:59:19 +0800541 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Riku Voipioe14fa822008-05-31 14:43:41 +0100542 if (!data)
543 return -ENOMEM;
544
Phil Reidfa4191a2016-06-14 15:36:17 +0800545 data->chip_info = &pca9532_chip_info_tbl[devid];
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700546
Riku Voipioe14fa822008-05-31 14:43:41 +0100547 dev_info(&client->dev, "setting platform data\n");
548 i2c_set_clientdata(client, data);
549 data->client = client;
550 mutex_init(&data->update_lock);
551
Bryan Wu0f4630c2012-07-04 11:59:19 +0800552 return pca9532_configure(client, data, pca9532_pdata);
Riku Voipioe14fa822008-05-31 14:43:41 +0100553}
554
555static int pca9532_remove(struct i2c_client *client)
556{
557 struct pca9532_data *data = i2c_get_clientdata(client);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700558 int err;
559
Jan Weitzel3dbf6222011-05-24 17:13:26 -0700560 err = pca9532_destroy_devices(data, data->chip_info->num_leds);
Joachim Eastwood3c1ab502011-05-24 17:13:23 -0700561 if (err)
562 return err;
563
Riku Voipioe14fa822008-05-31 14:43:41 +0100564 return 0;
565}
566
Axel Lin09a0d182012-01-10 15:09:27 -0800567module_i2c_driver(pca9532_driver);
Riku Voipioe14fa822008-05-31 14:43:41 +0100568
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200569MODULE_AUTHOR("Riku Voipio");
Riku Voipioe14fa822008-05-31 14:43:41 +0100570MODULE_LICENSE("GPL");
571MODULE_DESCRIPTION("PCA 9532 LED dimmer");