via-velocity: remove private #define
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / leds / leds-lp3944.c
blob5946208ba26e586aa42ce4fb944f011fc9ff43bf
1 /*
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/leds.h>
32 #include <linux/mutex.h>
33 #include <linux/workqueue.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
53 #define LP3944_DIM0 0
54 #define LP3944_DIM1 1
56 /* period in ms */
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)
66 /* Saved data */
67 struct lp3944_led_data {
68 u8 id;
69 enum lp3944_type type;
70 enum lp3944_status status;
71 struct led_classdev ldev;
72 struct i2c_client *client;
73 struct work_struct work;
76 struct lp3944_data {
77 struct mutex lock;
78 struct i2c_client *client;
79 struct lp3944_led_data leds[LP3944_LEDS_MAX];
82 static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value)
84 int tmp;
86 tmp = i2c_smbus_read_byte_data(client, reg);
87 if (tmp < 0)
88 return -EINVAL;
90 *value = tmp;
92 return 0;
95 static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
97 return i2c_smbus_write_byte_data(client, reg, value);
101 * Set the period for DIM status
103 * @client: the i2c client
104 * @dim: either LP3944_DIM0 or LP3944_DIM1
105 * @period: period of a blink, that is a on/off cycle, expressed in ms.
107 static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period)
109 u8 psc_reg;
110 u8 psc_value;
111 int err;
113 if (dim == LP3944_DIM0)
114 psc_reg = LP3944_REG_PSC0;
115 else if (dim == LP3944_DIM1)
116 psc_reg = LP3944_REG_PSC1;
117 else
118 return -EINVAL;
120 /* Convert period to Prescaler value */
121 if (period > LP3944_PERIOD_MAX)
122 return -EINVAL;
124 psc_value = (period * 255) / LP3944_PERIOD_MAX;
126 err = lp3944_reg_write(client, psc_reg, psc_value);
128 return err;
132 * Set the duty cycle for DIM status
134 * @client: the i2c client
135 * @dim: either LP3944_DIM0 or LP3944_DIM1
136 * @duty_cycle: percentage of a period during which a led is ON
138 static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim,
139 u8 duty_cycle)
141 u8 pwm_reg;
142 u8 pwm_value;
143 int err;
145 if (dim == LP3944_DIM0)
146 pwm_reg = LP3944_REG_PWM0;
147 else if (dim == LP3944_DIM1)
148 pwm_reg = LP3944_REG_PWM1;
149 else
150 return -EINVAL;
152 /* Convert duty cycle to PWM value */
153 if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
154 return -EINVAL;
156 pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
158 err = lp3944_reg_write(client, pwm_reg, pwm_value);
160 return err;
164 * Set the led status
166 * @led: a lp3944_led_data structure
167 * @status: one of LP3944_LED_STATUS_OFF
168 * LP3944_LED_STATUS_ON
169 * LP3944_LED_STATUS_DIM0
170 * LP3944_LED_STATUS_DIM1
172 static int lp3944_led_set(struct lp3944_led_data *led, u8 status)
174 struct lp3944_data *data = i2c_get_clientdata(led->client);
175 u8 id = led->id;
176 u8 reg;
177 u8 val = 0;
178 int err;
180 dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
181 __func__, led->ldev.name, status);
183 switch (id) {
184 case LP3944_LED0:
185 case LP3944_LED1:
186 case LP3944_LED2:
187 case LP3944_LED3:
188 reg = LP3944_REG_LS0;
189 break;
190 case LP3944_LED4:
191 case LP3944_LED5:
192 case LP3944_LED6:
193 case LP3944_LED7:
194 id -= LP3944_LED4;
195 reg = LP3944_REG_LS1;
196 break;
197 default:
198 return -EINVAL;
201 if (status > LP3944_LED_STATUS_DIM1)
202 return -EINVAL;
204 /* invert only 0 and 1, leave unchanged the other values,
205 * remember we are abusing status to set blink patterns
207 if (led->type == LP3944_LED_TYPE_LED_INVERTED && status < 2)
208 status = 1 - status;
210 mutex_lock(&data->lock);
211 lp3944_reg_read(led->client, reg, &val);
213 val &= ~(LP3944_LED_STATUS_MASK << (id << 1));
214 val |= (status << (id << 1));
216 dev_dbg(&led->client->dev, "%s: %s, reg:%d id:%d status:%d val:%#x\n",
217 __func__, led->ldev.name, reg, id, status, val);
219 /* set led status */
220 err = lp3944_reg_write(led->client, reg, val);
221 mutex_unlock(&data->lock);
223 return err;
226 static int lp3944_led_set_blink(struct led_classdev *led_cdev,
227 unsigned long *delay_on,
228 unsigned long *delay_off)
230 struct lp3944_led_data *led = ldev_to_led(led_cdev);
231 u16 period;
232 u8 duty_cycle;
233 int err;
235 /* units are in ms */
236 if (*delay_on + *delay_off > LP3944_PERIOD_MAX)
237 return -EINVAL;
239 if (*delay_on == 0 && *delay_off == 0) {
240 /* Special case: the leds subsystem requires a default user
241 * friendly blink pattern for the LED. Let's blink the led
242 * slowly (1Hz).
244 *delay_on = 500;
245 *delay_off = 500;
248 period = (*delay_on) + (*delay_off);
250 /* duty_cycle is the percentage of period during which the led is ON */
251 duty_cycle = 100 * (*delay_on) / period;
253 /* invert duty cycle for inverted leds, this has the same effect of
254 * swapping delay_on and delay_off
256 if (led->type == LP3944_LED_TYPE_LED_INVERTED)
257 duty_cycle = 100 - duty_cycle;
259 /* NOTE: using always the first DIM mode, this means that all leds
260 * will have the same blinking pattern.
262 * We could find a way later to have two leds blinking in hardware
263 * with different patterns at the same time, falling back to software
264 * control for the other ones.
266 err = lp3944_dim_set_period(led->client, LP3944_DIM0, period);
267 if (err)
268 return err;
270 err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle);
271 if (err)
272 return err;
274 dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n",
275 __func__);
277 led->status = LP3944_LED_STATUS_DIM0;
278 schedule_work(&led->work);
280 return 0;
283 static void 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 led->status = brightness;
292 schedule_work(&led->work);
295 static void lp3944_led_work(struct work_struct *work)
297 struct lp3944_led_data *led;
299 led = container_of(work, struct lp3944_led_data, work);
300 lp3944_led_set(led, led->status);
303 static int lp3944_configure(struct i2c_client *client,
304 struct lp3944_data *data,
305 struct lp3944_platform_data *pdata)
307 int i, err = 0;
309 for (i = 0; i < pdata->leds_size; i++) {
310 struct lp3944_led *pled = &pdata->leds[i];
311 struct lp3944_led_data *led = &data->leds[i];
312 led->client = client;
313 led->id = i;
315 switch (pled->type) {
317 case LP3944_LED_TYPE_LED:
318 case LP3944_LED_TYPE_LED_INVERTED:
319 led->type = pled->type;
320 led->status = pled->status;
321 led->ldev.name = pled->name;
322 led->ldev.max_brightness = 1;
323 led->ldev.brightness_set = lp3944_led_set_brightness;
324 led->ldev.blink_set = lp3944_led_set_blink;
325 led->ldev.flags = LED_CORE_SUSPENDRESUME;
327 INIT_WORK(&led->work, lp3944_led_work);
328 err = led_classdev_register(&client->dev, &led->ldev);
329 if (err < 0) {
330 dev_err(&client->dev,
331 "couldn't register LED %s\n",
332 led->ldev.name);
333 goto exit;
336 /* to expose the default value to userspace */
337 led->ldev.brightness = led->status;
339 /* Set the default led status */
340 err = lp3944_led_set(led, led->status);
341 if (err < 0) {
342 dev_err(&client->dev,
343 "%s couldn't set STATUS %d\n",
344 led->ldev.name, led->status);
345 goto exit;
347 break;
349 case LP3944_LED_TYPE_NONE:
350 default:
351 break;
355 return 0;
357 exit:
358 if (i > 0)
359 for (i = i - 1; i >= 0; i--)
360 switch (pdata->leds[i].type) {
362 case LP3944_LED_TYPE_LED:
363 case LP3944_LED_TYPE_LED_INVERTED:
364 led_classdev_unregister(&data->leds[i].ldev);
365 cancel_work_sync(&data->leds[i].work);
366 break;
368 case LP3944_LED_TYPE_NONE:
369 default:
370 break;
373 return err;
376 static int __devinit lp3944_probe(struct i2c_client *client,
377 const struct i2c_device_id *id)
379 struct lp3944_platform_data *lp3944_pdata = client->dev.platform_data;
380 struct lp3944_data *data;
382 if (lp3944_pdata == NULL) {
383 dev_err(&client->dev, "no platform data\n");
384 return -EINVAL;
387 /* Let's see whether this adapter can support what we need. */
388 if (!i2c_check_functionality(client->adapter,
389 I2C_FUNC_SMBUS_BYTE_DATA)) {
390 dev_err(&client->dev, "insufficient functionality!\n");
391 return -ENODEV;
394 data = kzalloc(sizeof(struct lp3944_data), GFP_KERNEL);
395 if (!data)
396 return -ENOMEM;
398 data->client = client;
399 i2c_set_clientdata(client, data);
401 mutex_init(&data->lock);
403 dev_info(&client->dev, "lp3944 enabled\n");
405 lp3944_configure(client, data, lp3944_pdata);
406 return 0;
409 static int __devexit lp3944_remove(struct i2c_client *client)
411 struct lp3944_platform_data *pdata = client->dev.platform_data;
412 struct lp3944_data *data = i2c_get_clientdata(client);
413 int i;
415 for (i = 0; i < pdata->leds_size; i++)
416 switch (data->leds[i].type) {
417 case LP3944_LED_TYPE_LED:
418 case LP3944_LED_TYPE_LED_INVERTED:
419 led_classdev_unregister(&data->leds[i].ldev);
420 cancel_work_sync(&data->leds[i].work);
421 break;
423 case LP3944_LED_TYPE_NONE:
424 default:
425 break;
428 kfree(data);
429 i2c_set_clientdata(client, NULL);
431 return 0;
434 /* lp3944 i2c driver struct */
435 static const struct i2c_device_id lp3944_id[] = {
436 {"lp3944", 0},
440 MODULE_DEVICE_TABLE(i2c, lp3944_id);
442 static struct i2c_driver lp3944_driver = {
443 .driver = {
444 .name = "lp3944",
446 .probe = lp3944_probe,
447 .remove = __devexit_p(lp3944_remove),
448 .id_table = lp3944_id,
451 static int __init lp3944_module_init(void)
453 return i2c_add_driver(&lp3944_driver);
456 static void __exit lp3944_module_exit(void)
458 i2c_del_driver(&lp3944_driver);
461 module_init(lp3944_module_init);
462 module_exit(lp3944_module_exit);
464 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
465 MODULE_DESCRIPTION("LP3944 Fun Light Chip");
466 MODULE_LICENSE("GPL");