drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / leds / leds-lp3944.c
blobccfeee49ea78c44a5500a79c7fb540d6ee55f663
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * leds-lp3944.c - driver for National Semiconductor LP3944 Funlight Chip
5 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
6 */
8 /*
9 * I2C driver for National Semiconductor LP3944 Funlight Chip
10 * http://www.national.com/pf/LP/LP3944.html
12 * This helper chip can drive up to 8 leds, with two programmable DIM modes;
13 * it could even be used as a gpio expander but this driver assumes it is used
14 * as a led controller.
16 * The DIM modes are used to set _blink_ patterns for leds, the pattern is
17 * specified supplying two parameters:
18 * - period: from 0s to 1.6s
19 * - duty cycle: percentage of the period the led is on, from 0 to 100
21 * LP3944 can be found on Motorola A910 smartphone, where it drives the rgb
22 * leds, the camera flash light and the displays backlights.
25 #include <linux/module.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/leds.h>
29 #include <linux/mutex.h>
30 #include <linux/leds-lp3944.h>
32 /* Read Only Registers */
33 #define LP3944_REG_INPUT1 0x00 /* LEDs 0-7 InputRegister (Read Only) */
34 #define LP3944_REG_REGISTER1 0x01 /* None (Read Only) */
36 #define LP3944_REG_PSC0 0x02 /* Frequency Prescaler 0 (R/W) */
37 #define LP3944_REG_PWM0 0x03 /* PWM Register 0 (R/W) */
38 #define LP3944_REG_PSC1 0x04 /* Frequency Prescaler 1 (R/W) */
39 #define LP3944_REG_PWM1 0x05 /* PWM Register 1 (R/W) */
40 #define LP3944_REG_LS0 0x06 /* LEDs 0-3 Selector (R/W) */
41 #define LP3944_REG_LS1 0x07 /* LEDs 4-7 Selector (R/W) */
43 /* These registers are not used to control leds in LP3944, they can store
44 * arbitrary values which the chip will ignore.
46 #define LP3944_REG_REGISTER8 0x08
47 #define LP3944_REG_REGISTER9 0x09
49 #define LP3944_DIM0 0
50 #define LP3944_DIM1 1
52 /* period in ms */
53 #define LP3944_PERIOD_MIN 0
54 #define LP3944_PERIOD_MAX 1600
56 /* duty cycle is a percentage */
57 #define LP3944_DUTY_CYCLE_MIN 0
58 #define LP3944_DUTY_CYCLE_MAX 100
60 #define ldev_to_led(c) container_of(c, struct lp3944_led_data, ldev)
62 /* Saved data */
63 struct lp3944_led_data {
64 u8 id;
65 enum lp3944_type type;
66 struct led_classdev ldev;
67 struct i2c_client *client;
70 struct lp3944_data {
71 struct mutex lock;
72 struct i2c_client *client;
73 struct lp3944_led_data leds[LP3944_LEDS_MAX];
76 static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value)
78 int tmp;
80 tmp = i2c_smbus_read_byte_data(client, reg);
81 if (tmp < 0)
82 return tmp;
84 *value = tmp;
86 return 0;
89 static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
91 return i2c_smbus_write_byte_data(client, reg, value);
94 /**
95 * lp3944_dim_set_period() - Set the period for DIM status
97 * @client: the i2c client
98 * @dim: either LP3944_DIM0 or LP3944_DIM1
99 * @period: period of a blink, that is a on/off cycle, expressed in ms.
101 static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period)
103 u8 psc_reg;
104 u8 psc_value;
105 int err;
107 if (dim == LP3944_DIM0)
108 psc_reg = LP3944_REG_PSC0;
109 else if (dim == LP3944_DIM1)
110 psc_reg = LP3944_REG_PSC1;
111 else
112 return -EINVAL;
114 /* Convert period to Prescaler value */
115 if (period > LP3944_PERIOD_MAX)
116 return -EINVAL;
118 psc_value = (period * 255) / LP3944_PERIOD_MAX;
120 err = lp3944_reg_write(client, psc_reg, psc_value);
122 return err;
126 * lp3944_dim_set_dutycycle - Set the duty cycle for DIM status
128 * @client: the i2c client
129 * @dim: either LP3944_DIM0 or LP3944_DIM1
130 * @duty_cycle: percentage of a period during which a led is ON
132 static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim,
133 u8 duty_cycle)
135 u8 pwm_reg;
136 u8 pwm_value;
137 int err;
139 if (dim == LP3944_DIM0)
140 pwm_reg = LP3944_REG_PWM0;
141 else if (dim == LP3944_DIM1)
142 pwm_reg = LP3944_REG_PWM1;
143 else
144 return -EINVAL;
146 /* Convert duty cycle to PWM value */
147 if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
148 return -EINVAL;
150 pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
152 err = lp3944_reg_write(client, pwm_reg, pwm_value);
154 return err;
158 * lp3944_led_set() - Set the led status
160 * @led: a lp3944_led_data structure
161 * @status: one of LP3944_LED_STATUS_OFF
162 * LP3944_LED_STATUS_ON
163 * LP3944_LED_STATUS_DIM0
164 * LP3944_LED_STATUS_DIM1
166 static int lp3944_led_set(struct lp3944_led_data *led, u8 status)
168 struct lp3944_data *data = i2c_get_clientdata(led->client);
169 u8 id = led->id;
170 u8 reg;
171 u8 val = 0;
172 int err;
174 dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
175 __func__, led->ldev.name, status);
177 switch (id) {
178 case LP3944_LED0:
179 case LP3944_LED1:
180 case LP3944_LED2:
181 case LP3944_LED3:
182 reg = LP3944_REG_LS0;
183 break;
184 case LP3944_LED4:
185 case LP3944_LED5:
186 case LP3944_LED6:
187 case LP3944_LED7:
188 id -= LP3944_LED4;
189 reg = LP3944_REG_LS1;
190 break;
191 default:
192 return -EINVAL;
195 if (status > LP3944_LED_STATUS_DIM1)
196 return -EINVAL;
199 * Invert status only when it's < 2 (i.e. 0 or 1) which means it's
200 * controlling the on/off state directly.
201 * When, instead, status is >= 2 don't invert it because it would mean
202 * to mess with the hardware blinking mode.
204 if (led->type == LP3944_LED_TYPE_LED_INVERTED && status < 2)
205 status = 1 - status;
207 mutex_lock(&data->lock);
208 lp3944_reg_read(led->client, reg, &val);
210 val &= ~(LP3944_LED_STATUS_MASK << (id << 1));
211 val |= (status << (id << 1));
213 dev_dbg(&led->client->dev, "%s: %s, reg:%d id:%d status:%d val:%#x\n",
214 __func__, led->ldev.name, reg, id, status, val);
216 /* set led status */
217 err = lp3944_reg_write(led->client, reg, val);
218 mutex_unlock(&data->lock);
220 return err;
223 static int lp3944_led_set_blink(struct led_classdev *led_cdev,
224 unsigned long *delay_on,
225 unsigned long *delay_off)
227 struct lp3944_led_data *led = ldev_to_led(led_cdev);
228 u16 period;
229 u8 duty_cycle;
230 int err;
232 /* units are in ms */
233 if (*delay_on + *delay_off > LP3944_PERIOD_MAX)
234 return -EINVAL;
236 if (*delay_on == 0 && *delay_off == 0) {
237 /* Special case: the leds subsystem requires a default user
238 * friendly blink pattern for the LED. Let's blink the led
239 * slowly (1Hz).
241 *delay_on = 500;
242 *delay_off = 500;
245 period = (*delay_on) + (*delay_off);
247 /* duty_cycle is the percentage of period during which the led is ON */
248 duty_cycle = 100 * (*delay_on) / period;
250 /* invert duty cycle for inverted leds, this has the same effect of
251 * swapping delay_on and delay_off
253 if (led->type == LP3944_LED_TYPE_LED_INVERTED)
254 duty_cycle = 100 - duty_cycle;
256 /* NOTE: using always the first DIM mode, this means that all leds
257 * will have the same blinking pattern.
259 * We could find a way later to have two leds blinking in hardware
260 * with different patterns at the same time, falling back to software
261 * control for the other ones.
263 err = lp3944_dim_set_period(led->client, LP3944_DIM0, period);
264 if (err)
265 return err;
267 err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle);
268 if (err)
269 return err;
271 dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n",
272 __func__);
274 lp3944_led_set(led, LP3944_LED_STATUS_DIM0);
276 return 0;
279 static int lp3944_led_set_brightness(struct led_classdev *led_cdev,
280 enum led_brightness brightness)
282 struct lp3944_led_data *led = ldev_to_led(led_cdev);
284 dev_dbg(&led->client->dev, "%s: %s, %d\n",
285 __func__, led_cdev->name, brightness);
287 return lp3944_led_set(led, !!brightness);
290 static int lp3944_configure(struct i2c_client *client,
291 struct lp3944_data *data,
292 struct lp3944_platform_data *pdata)
294 int i, err = 0;
296 for (i = 0; i < pdata->leds_size; i++) {
297 struct lp3944_led *pled = &pdata->leds[i];
298 struct lp3944_led_data *led = &data->leds[i];
299 led->client = client;
300 led->id = i;
302 switch (pled->type) {
304 case LP3944_LED_TYPE_LED:
305 case LP3944_LED_TYPE_LED_INVERTED:
306 led->type = pled->type;
307 led->ldev.name = pled->name;
308 led->ldev.max_brightness = 1;
309 led->ldev.brightness_set_blocking =
310 lp3944_led_set_brightness;
311 led->ldev.blink_set = lp3944_led_set_blink;
312 led->ldev.flags = LED_CORE_SUSPENDRESUME;
314 err = led_classdev_register(&client->dev, &led->ldev);
315 if (err < 0) {
316 dev_err(&client->dev,
317 "couldn't register LED %s\n",
318 led->ldev.name);
319 goto exit;
322 /* to expose the default value to userspace */
323 led->ldev.brightness =
324 (enum led_brightness) pled->status;
326 /* Set the default led status */
327 err = lp3944_led_set(led, pled->status);
328 if (err < 0) {
329 dev_err(&client->dev,
330 "%s couldn't set STATUS %d\n",
331 led->ldev.name, pled->status);
332 goto exit;
334 break;
336 case LP3944_LED_TYPE_NONE:
337 default:
338 break;
342 return 0;
344 exit:
345 if (i > 0)
346 for (i = i - 1; i >= 0; i--)
347 switch (pdata->leds[i].type) {
349 case LP3944_LED_TYPE_LED:
350 case LP3944_LED_TYPE_LED_INVERTED:
351 led_classdev_unregister(&data->leds[i].ldev);
352 break;
354 case LP3944_LED_TYPE_NONE:
355 default:
356 break;
359 return err;
362 static int lp3944_probe(struct i2c_client *client)
364 struct lp3944_platform_data *lp3944_pdata =
365 dev_get_platdata(&client->dev);
366 struct lp3944_data *data;
367 int err;
369 if (lp3944_pdata == NULL) {
370 dev_err(&client->dev, "no platform data\n");
371 return -EINVAL;
374 /* Let's see whether this adapter can support what we need. */
375 if (!i2c_check_functionality(client->adapter,
376 I2C_FUNC_SMBUS_BYTE_DATA)) {
377 dev_err(&client->dev, "insufficient functionality!\n");
378 return -ENODEV;
381 data = devm_kzalloc(&client->dev, sizeof(struct lp3944_data),
382 GFP_KERNEL);
383 if (!data)
384 return -ENOMEM;
386 data->client = client;
387 i2c_set_clientdata(client, data);
389 mutex_init(&data->lock);
391 err = lp3944_configure(client, data, lp3944_pdata);
392 if (err < 0)
393 return err;
395 dev_info(&client->dev, "lp3944 enabled\n");
396 return 0;
399 static void lp3944_remove(struct i2c_client *client)
401 struct lp3944_platform_data *pdata = dev_get_platdata(&client->dev);
402 struct lp3944_data *data = i2c_get_clientdata(client);
403 int i;
405 for (i = 0; i < pdata->leds_size; i++)
406 switch (data->leds[i].type) {
407 case LP3944_LED_TYPE_LED:
408 case LP3944_LED_TYPE_LED_INVERTED:
409 led_classdev_unregister(&data->leds[i].ldev);
410 break;
412 case LP3944_LED_TYPE_NONE:
413 default:
414 break;
418 /* lp3944 i2c driver struct */
419 static const struct i2c_device_id lp3944_id[] = {
420 { "lp3944" },
424 MODULE_DEVICE_TABLE(i2c, lp3944_id);
426 static struct i2c_driver lp3944_driver = {
427 .driver = {
428 .name = "lp3944",
430 .probe = lp3944_probe,
431 .remove = lp3944_remove,
432 .id_table = lp3944_id,
435 module_i2c_driver(lp3944_driver);
437 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
438 MODULE_DESCRIPTION("LP3944 Fun Light Chip");
439 MODULE_LICENSE("GPL");