1 // SPDX-License-Identifier: GPL-2.0-only
3 * leds-lp3944.c - driver for National Semiconductor LP3944 Funlight Chip
5 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
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
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)
63 struct lp3944_led_data {
65 enum lp3944_type type;
66 struct led_classdev ldev;
67 struct i2c_client *client;
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)
80 tmp = i2c_smbus_read_byte_data(client, reg);
89 static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
91 return i2c_smbus_write_byte_data(client, reg, value);
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)
107 if (dim == LP3944_DIM0)
108 psc_reg = LP3944_REG_PSC0;
109 else if (dim == LP3944_DIM1)
110 psc_reg = LP3944_REG_PSC1;
114 /* Convert period to Prescaler value */
115 if (period > LP3944_PERIOD_MAX)
118 psc_value = (period * 255) / LP3944_PERIOD_MAX;
120 err = lp3944_reg_write(client, psc_reg, psc_value);
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,
139 if (dim == LP3944_DIM0)
140 pwm_reg = LP3944_REG_PWM0;
141 else if (dim == LP3944_DIM1)
142 pwm_reg = LP3944_REG_PWM1;
146 /* Convert duty cycle to PWM value */
147 if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
150 pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
152 err = lp3944_reg_write(client, pwm_reg, pwm_value);
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);
174 dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
175 __func__, led->ldev.name, status);
182 reg = LP3944_REG_LS0;
189 reg = LP3944_REG_LS1;
195 if (status > LP3944_LED_STATUS_DIM1)
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)
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);
217 err = lp3944_reg_write(led->client, reg, val);
218 mutex_unlock(&data->lock);
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);
232 /* units are in ms */
233 if (*delay_on + *delay_off > LP3944_PERIOD_MAX)
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
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);
267 err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle);
271 dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n",
274 lp3944_led_set(led, LP3944_LED_STATUS_DIM0);
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)
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;
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);
316 dev_err(&client->dev,
317 "couldn't register LED %s\n",
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);
329 dev_err(&client->dev,
330 "%s couldn't set STATUS %d\n",
331 led->ldev.name, pled->status);
336 case LP3944_LED_TYPE_NONE:
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);
354 case LP3944_LED_TYPE_NONE:
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;
369 if (lp3944_pdata == NULL) {
370 dev_err(&client->dev, "no platform data\n");
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");
381 data = devm_kzalloc(&client->dev, sizeof(struct lp3944_data),
386 data->client = client;
387 i2c_set_clientdata(client, data);
389 mutex_init(&data->lock);
391 err = lp3944_configure(client, data, lp3944_pdata);
395 dev_info(&client->dev, "lp3944 enabled\n");
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);
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);
412 case LP3944_LED_TYPE_NONE:
418 /* lp3944 i2c driver struct */
419 static const struct i2c_device_id lp3944_id[] = {
424 MODULE_DEVICE_TABLE(i2c, lp3944_id);
426 static struct i2c_driver lp3944_driver = {
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");