2 * leds-lp3944.c - driver for National Semiconductor LP3944 Funlight Chip
4 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 * I2C driver for National Semiconductor LP3944 Funlight Chip
14 * http://www.national.com/pf/LP/LP3944.html
16 * This helper chip can drive up to 8 leds, with two programmable DIM modes;
17 * it could even be used as a gpio expander but this driver assumes it is used
18 * as a led controller.
20 * The DIM modes are used to set _blink_ patterns for leds, the pattern is
21 * specified supplying two parameters:
22 * - period: from 0s to 1.6s
23 * - duty cycle: percentage of the period the led is on, from 0 to 100
25 * LP3944 can be found on Motorola A910 smartphone, where it drives the rgb
26 * leds, the camera flash light and the displays backlights.
29 #include <linux/module.h>
30 #include <linux/i2c.h>
31 #include <linux/slab.h>
32 #include <linux/leds.h>
33 #include <linux/mutex.h>
34 #include <linux/leds-lp3944.h>
36 /* Read Only Registers */
37 #define LP3944_REG_INPUT1 0x00 /* LEDs 0-7 InputRegister (Read Only) */
38 #define LP3944_REG_REGISTER1 0x01 /* None (Read Only) */
40 #define LP3944_REG_PSC0 0x02 /* Frequency Prescaler 0 (R/W) */
41 #define LP3944_REG_PWM0 0x03 /* PWM Register 0 (R/W) */
42 #define LP3944_REG_PSC1 0x04 /* Frequency Prescaler 1 (R/W) */
43 #define LP3944_REG_PWM1 0x05 /* PWM Register 1 (R/W) */
44 #define LP3944_REG_LS0 0x06 /* LEDs 0-3 Selector (R/W) */
45 #define LP3944_REG_LS1 0x07 /* LEDs 4-7 Selector (R/W) */
47 /* These registers are not used to control leds in LP3944, they can store
48 * arbitrary values which the chip will ignore.
50 #define LP3944_REG_REGISTER8 0x08
51 #define LP3944_REG_REGISTER9 0x09
57 #define LP3944_PERIOD_MIN 0
58 #define LP3944_PERIOD_MAX 1600
60 /* duty cycle is a percentage */
61 #define LP3944_DUTY_CYCLE_MIN 0
62 #define LP3944_DUTY_CYCLE_MAX 100
64 #define ldev_to_led(c) container_of(c, struct lp3944_led_data, ldev)
67 struct lp3944_led_data {
69 enum lp3944_type type;
70 struct led_classdev ldev;
71 struct i2c_client *client;
76 struct i2c_client *client;
77 struct lp3944_led_data leds[LP3944_LEDS_MAX];
80 static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value)
84 tmp = i2c_smbus_read_byte_data(client, reg);
93 static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
95 return i2c_smbus_write_byte_data(client, reg, value);
99 * Set the period for DIM status
101 * @client: the i2c client
102 * @dim: either LP3944_DIM0 or LP3944_DIM1
103 * @period: period of a blink, that is a on/off cycle, expressed in ms.
105 static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period)
111 if (dim == LP3944_DIM0)
112 psc_reg = LP3944_REG_PSC0;
113 else if (dim == LP3944_DIM1)
114 psc_reg = LP3944_REG_PSC1;
118 /* Convert period to Prescaler value */
119 if (period > LP3944_PERIOD_MAX)
122 psc_value = (period * 255) / LP3944_PERIOD_MAX;
124 err = lp3944_reg_write(client, psc_reg, psc_value);
130 * Set the duty cycle for DIM status
132 * @client: the i2c client
133 * @dim: either LP3944_DIM0 or LP3944_DIM1
134 * @duty_cycle: percentage of a period during which a led is ON
136 static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim,
143 if (dim == LP3944_DIM0)
144 pwm_reg = LP3944_REG_PWM0;
145 else if (dim == LP3944_DIM1)
146 pwm_reg = LP3944_REG_PWM1;
150 /* Convert duty cycle to PWM value */
151 if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
154 pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
156 err = lp3944_reg_write(client, pwm_reg, pwm_value);
164 * @led: a lp3944_led_data structure
165 * @status: one of LP3944_LED_STATUS_OFF
166 * LP3944_LED_STATUS_ON
167 * LP3944_LED_STATUS_DIM0
168 * LP3944_LED_STATUS_DIM1
170 static int lp3944_led_set(struct lp3944_led_data *led, u8 status)
172 struct lp3944_data *data = i2c_get_clientdata(led->client);
178 dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
179 __func__, led->ldev.name, status);
186 reg = LP3944_REG_LS0;
193 reg = LP3944_REG_LS1;
199 if (status > LP3944_LED_STATUS_DIM1)
203 * Invert status only when it's < 2 (i.e. 0 or 1) which means it's
204 * controlling the on/off state directly.
205 * When, instead, status is >= 2 don't invert it because it would mean
206 * to mess with the hardware blinking mode.
208 if (led->type == LP3944_LED_TYPE_LED_INVERTED && status < 2)
211 mutex_lock(&data->lock);
212 lp3944_reg_read(led->client, reg, &val);
214 val &= ~(LP3944_LED_STATUS_MASK << (id << 1));
215 val |= (status << (id << 1));
217 dev_dbg(&led->client->dev, "%s: %s, reg:%d id:%d status:%d val:%#x\n",
218 __func__, led->ldev.name, reg, id, status, val);
221 err = lp3944_reg_write(led->client, reg, val);
222 mutex_unlock(&data->lock);
227 static int lp3944_led_set_blink(struct led_classdev *led_cdev,
228 unsigned long *delay_on,
229 unsigned long *delay_off)
231 struct lp3944_led_data *led = ldev_to_led(led_cdev);
236 /* units are in ms */
237 if (*delay_on + *delay_off > LP3944_PERIOD_MAX)
240 if (*delay_on == 0 && *delay_off == 0) {
241 /* Special case: the leds subsystem requires a default user
242 * friendly blink pattern for the LED. Let's blink the led
249 period = (*delay_on) + (*delay_off);
251 /* duty_cycle is the percentage of period during which the led is ON */
252 duty_cycle = 100 * (*delay_on) / period;
254 /* invert duty cycle for inverted leds, this has the same effect of
255 * swapping delay_on and delay_off
257 if (led->type == LP3944_LED_TYPE_LED_INVERTED)
258 duty_cycle = 100 - duty_cycle;
260 /* NOTE: using always the first DIM mode, this means that all leds
261 * will have the same blinking pattern.
263 * We could find a way later to have two leds blinking in hardware
264 * with different patterns at the same time, falling back to software
265 * control for the other ones.
267 err = lp3944_dim_set_period(led->client, LP3944_DIM0, period);
271 err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle);
275 dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n",
278 lp3944_led_set(led, LP3944_LED_STATUS_DIM0);
283 static int lp3944_led_set_brightness(struct led_classdev *led_cdev,
284 enum led_brightness brightness)
286 struct lp3944_led_data *led = ldev_to_led(led_cdev);
288 dev_dbg(&led->client->dev, "%s: %s, %d\n",
289 __func__, led_cdev->name, brightness);
291 return lp3944_led_set(led, !!brightness);
294 static int lp3944_configure(struct i2c_client *client,
295 struct lp3944_data *data,
296 struct lp3944_platform_data *pdata)
300 for (i = 0; i < pdata->leds_size; i++) {
301 struct lp3944_led *pled = &pdata->leds[i];
302 struct lp3944_led_data *led = &data->leds[i];
303 led->client = client;
306 switch (pled->type) {
308 case LP3944_LED_TYPE_LED:
309 case LP3944_LED_TYPE_LED_INVERTED:
310 led->type = pled->type;
311 led->ldev.name = pled->name;
312 led->ldev.max_brightness = 1;
313 led->ldev.brightness_set_blocking =
314 lp3944_led_set_brightness;
315 led->ldev.blink_set = lp3944_led_set_blink;
316 led->ldev.flags = LED_CORE_SUSPENDRESUME;
318 err = led_classdev_register(&client->dev, &led->ldev);
320 dev_err(&client->dev,
321 "couldn't register LED %s\n",
326 /* to expose the default value to userspace */
327 led->ldev.brightness =
328 (enum led_brightness) pled->status;
330 /* Set the default led status */
331 err = lp3944_led_set(led, pled->status);
333 dev_err(&client->dev,
334 "%s couldn't set STATUS %d\n",
335 led->ldev.name, pled->status);
340 case LP3944_LED_TYPE_NONE:
350 for (i = i - 1; i >= 0; i--)
351 switch (pdata->leds[i].type) {
353 case LP3944_LED_TYPE_LED:
354 case LP3944_LED_TYPE_LED_INVERTED:
355 led_classdev_unregister(&data->leds[i].ldev);
358 case LP3944_LED_TYPE_NONE:
366 static int lp3944_probe(struct i2c_client *client,
367 const struct i2c_device_id *id)
369 struct lp3944_platform_data *lp3944_pdata =
370 dev_get_platdata(&client->dev);
371 struct lp3944_data *data;
374 if (lp3944_pdata == NULL) {
375 dev_err(&client->dev, "no platform data\n");
379 /* Let's see whether this adapter can support what we need. */
380 if (!i2c_check_functionality(client->adapter,
381 I2C_FUNC_SMBUS_BYTE_DATA)) {
382 dev_err(&client->dev, "insufficient functionality!\n");
386 data = devm_kzalloc(&client->dev, sizeof(struct lp3944_data),
391 data->client = client;
392 i2c_set_clientdata(client, data);
394 mutex_init(&data->lock);
396 err = lp3944_configure(client, data, lp3944_pdata);
400 dev_info(&client->dev, "lp3944 enabled\n");
404 static int lp3944_remove(struct i2c_client *client)
406 struct lp3944_platform_data *pdata = dev_get_platdata(&client->dev);
407 struct lp3944_data *data = i2c_get_clientdata(client);
410 for (i = 0; i < pdata->leds_size; i++)
411 switch (data->leds[i].type) {
412 case LP3944_LED_TYPE_LED:
413 case LP3944_LED_TYPE_LED_INVERTED:
414 led_classdev_unregister(&data->leds[i].ldev);
417 case LP3944_LED_TYPE_NONE:
425 /* lp3944 i2c driver struct */
426 static const struct i2c_device_id lp3944_id[] = {
431 MODULE_DEVICE_TABLE(i2c, lp3944_id);
433 static struct i2c_driver lp3944_driver = {
437 .probe = lp3944_probe,
438 .remove = lp3944_remove,
439 .id_table = lp3944_id,
442 module_i2c_driver(lp3944_driver);
444 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
445 MODULE_DESCRIPTION("LP3944 Fun Light Chip");
446 MODULE_LICENSE("GPL");