xref: /openbmc/linux/drivers/leds/leds-lp3944.c (revision aaf9128a)
1 /*
2  * leds-lp3944.c - driver for National Semiconductor LP3944 Funlight Chip
3  *
4  * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
5  *
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.
9  *
10  */
11 
12 /*
13  * I2C driver for National Semiconductor LP3944 Funlight Chip
14  * http://www.national.com/pf/LP/LP3944.html
15  *
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.
19  *
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
24  *
25  * LP3944 can be found on Motorola A910 smartphone, where it drives the rgb
26  * leds, the camera flash light and the displays backlights.
27  */
28 
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>
35 
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) */
39 
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) */
46 
47 /* These registers are not used to control leds in LP3944, they can store
48  * arbitrary values which the chip will ignore.
49  */
50 #define LP3944_REG_REGISTER8  0x08
51 #define LP3944_REG_REGISTER9  0x09
52 
53 #define LP3944_DIM0 0
54 #define LP3944_DIM1 1
55 
56 /* period in ms */
57 #define LP3944_PERIOD_MIN 0
58 #define LP3944_PERIOD_MAX 1600
59 
60 /* duty cycle is a percentage */
61 #define LP3944_DUTY_CYCLE_MIN 0
62 #define LP3944_DUTY_CYCLE_MAX 100
63 
64 #define ldev_to_led(c)       container_of(c, struct lp3944_led_data, ldev)
65 
66 /* Saved data */
67 struct lp3944_led_data {
68 	u8 id;
69 	enum lp3944_type type;
70 	struct led_classdev ldev;
71 	struct i2c_client *client;
72 };
73 
74 struct lp3944_data {
75 	struct mutex lock;
76 	struct i2c_client *client;
77 	struct lp3944_led_data leds[LP3944_LEDS_MAX];
78 };
79 
80 static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value)
81 {
82 	int tmp;
83 
84 	tmp = i2c_smbus_read_byte_data(client, reg);
85 	if (tmp < 0)
86 		return tmp;
87 
88 	*value = tmp;
89 
90 	return 0;
91 }
92 
93 static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
94 {
95 	return i2c_smbus_write_byte_data(client, reg, value);
96 }
97 
98 /**
99  * Set the period for DIM status
100  *
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.
104  */
105 static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period)
106 {
107 	u8 psc_reg;
108 	u8 psc_value;
109 	int err;
110 
111 	if (dim == LP3944_DIM0)
112 		psc_reg = LP3944_REG_PSC0;
113 	else if (dim == LP3944_DIM1)
114 		psc_reg = LP3944_REG_PSC1;
115 	else
116 		return -EINVAL;
117 
118 	/* Convert period to Prescaler value */
119 	if (period > LP3944_PERIOD_MAX)
120 		return -EINVAL;
121 
122 	psc_value = (period * 255) / LP3944_PERIOD_MAX;
123 
124 	err = lp3944_reg_write(client, psc_reg, psc_value);
125 
126 	return err;
127 }
128 
129 /**
130  * Set the duty cycle for DIM status
131  *
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
135  */
136 static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim,
137 				    u8 duty_cycle)
138 {
139 	u8 pwm_reg;
140 	u8 pwm_value;
141 	int err;
142 
143 	if (dim == LP3944_DIM0)
144 		pwm_reg = LP3944_REG_PWM0;
145 	else if (dim == LP3944_DIM1)
146 		pwm_reg = LP3944_REG_PWM1;
147 	else
148 		return -EINVAL;
149 
150 	/* Convert duty cycle to PWM value */
151 	if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
152 		return -EINVAL;
153 
154 	pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
155 
156 	err = lp3944_reg_write(client, pwm_reg, pwm_value);
157 
158 	return err;
159 }
160 
161 /**
162  * Set the led status
163  *
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
169  */
170 static int lp3944_led_set(struct lp3944_led_data *led, u8 status)
171 {
172 	struct lp3944_data *data = i2c_get_clientdata(led->client);
173 	u8 id = led->id;
174 	u8 reg;
175 	u8 val = 0;
176 	int err;
177 
178 	dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
179 		__func__, led->ldev.name, status);
180 
181 	switch (id) {
182 	case LP3944_LED0:
183 	case LP3944_LED1:
184 	case LP3944_LED2:
185 	case LP3944_LED3:
186 		reg = LP3944_REG_LS0;
187 		break;
188 	case LP3944_LED4:
189 	case LP3944_LED5:
190 	case LP3944_LED6:
191 	case LP3944_LED7:
192 		id -= LP3944_LED4;
193 		reg = LP3944_REG_LS1;
194 		break;
195 	default:
196 		return -EINVAL;
197 	}
198 
199 	if (status > LP3944_LED_STATUS_DIM1)
200 		return -EINVAL;
201 
202 	/*
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.
207 	 */
208 	if (led->type == LP3944_LED_TYPE_LED_INVERTED && status < 2)
209 		status = 1 - status;
210 
211 	mutex_lock(&data->lock);
212 	lp3944_reg_read(led->client, reg, &val);
213 
214 	val &= ~(LP3944_LED_STATUS_MASK << (id << 1));
215 	val |= (status << (id << 1));
216 
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);
219 
220 	/* set led status */
221 	err = lp3944_reg_write(led->client, reg, val);
222 	mutex_unlock(&data->lock);
223 
224 	return err;
225 }
226 
227 static int lp3944_led_set_blink(struct led_classdev *led_cdev,
228 				unsigned long *delay_on,
229 				unsigned long *delay_off)
230 {
231 	struct lp3944_led_data *led = ldev_to_led(led_cdev);
232 	u16 period;
233 	u8 duty_cycle;
234 	int err;
235 
236 	/* units are in ms */
237 	if (*delay_on + *delay_off > LP3944_PERIOD_MAX)
238 		return -EINVAL;
239 
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
243 		 * slowly (1Hz).
244 		 */
245 		*delay_on = 500;
246 		*delay_off = 500;
247 	}
248 
249 	period = (*delay_on) + (*delay_off);
250 
251 	/* duty_cycle is the percentage of period during which the led is ON */
252 	duty_cycle = 100 * (*delay_on) / period;
253 
254 	/* invert duty cycle for inverted leds, this has the same effect of
255 	 * swapping delay_on and delay_off
256 	 */
257 	if (led->type == LP3944_LED_TYPE_LED_INVERTED)
258 		duty_cycle = 100 - duty_cycle;
259 
260 	/* NOTE: using always the first DIM mode, this means that all leds
261 	 * will have the same blinking pattern.
262 	 *
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.
266 	 */
267 	err = lp3944_dim_set_period(led->client, LP3944_DIM0, period);
268 	if (err)
269 		return err;
270 
271 	err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle);
272 	if (err)
273 		return err;
274 
275 	dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n",
276 		__func__);
277 
278 	lp3944_led_set(led, LP3944_LED_STATUS_DIM0);
279 
280 	return 0;
281 }
282 
283 static int lp3944_led_set_brightness(struct led_classdev *led_cdev,
284 				      enum led_brightness brightness)
285 {
286 	struct lp3944_led_data *led = ldev_to_led(led_cdev);
287 
288 	dev_dbg(&led->client->dev, "%s: %s, %d\n",
289 		__func__, led_cdev->name, brightness);
290 
291 	return lp3944_led_set(led, !!brightness);
292 }
293 
294 static int lp3944_configure(struct i2c_client *client,
295 			    struct lp3944_data *data,
296 			    struct lp3944_platform_data *pdata)
297 {
298 	int i, err = 0;
299 
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;
304 		led->id = i;
305 
306 		switch (pled->type) {
307 
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;
317 
318 			err = led_classdev_register(&client->dev, &led->ldev);
319 			if (err < 0) {
320 				dev_err(&client->dev,
321 					"couldn't register LED %s\n",
322 					led->ldev.name);
323 				goto exit;
324 			}
325 
326 			/* to expose the default value to userspace */
327 			led->ldev.brightness =
328 					(enum led_brightness) pled->status;
329 
330 			/* Set the default led status */
331 			err = lp3944_led_set(led, pled->status);
332 			if (err < 0) {
333 				dev_err(&client->dev,
334 					"%s couldn't set STATUS %d\n",
335 					led->ldev.name, pled->status);
336 				goto exit;
337 			}
338 			break;
339 
340 		case LP3944_LED_TYPE_NONE:
341 		default:
342 			break;
343 
344 		}
345 	}
346 	return 0;
347 
348 exit:
349 	if (i > 0)
350 		for (i = i - 1; i >= 0; i--)
351 			switch (pdata->leds[i].type) {
352 
353 			case LP3944_LED_TYPE_LED:
354 			case LP3944_LED_TYPE_LED_INVERTED:
355 				led_classdev_unregister(&data->leds[i].ldev);
356 				break;
357 
358 			case LP3944_LED_TYPE_NONE:
359 			default:
360 				break;
361 			}
362 
363 	return err;
364 }
365 
366 static int lp3944_probe(struct i2c_client *client,
367 				  const struct i2c_device_id *id)
368 {
369 	struct lp3944_platform_data *lp3944_pdata =
370 			dev_get_platdata(&client->dev);
371 	struct lp3944_data *data;
372 	int err;
373 
374 	if (lp3944_pdata == NULL) {
375 		dev_err(&client->dev, "no platform data\n");
376 		return -EINVAL;
377 	}
378 
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");
383 		return -ENODEV;
384 	}
385 
386 	data = devm_kzalloc(&client->dev, sizeof(struct lp3944_data),
387 			GFP_KERNEL);
388 	if (!data)
389 		return -ENOMEM;
390 
391 	data->client = client;
392 	i2c_set_clientdata(client, data);
393 
394 	mutex_init(&data->lock);
395 
396 	err = lp3944_configure(client, data, lp3944_pdata);
397 	if (err < 0)
398 		return err;
399 
400 	dev_info(&client->dev, "lp3944 enabled\n");
401 	return 0;
402 }
403 
404 static int lp3944_remove(struct i2c_client *client)
405 {
406 	struct lp3944_platform_data *pdata = dev_get_platdata(&client->dev);
407 	struct lp3944_data *data = i2c_get_clientdata(client);
408 	int i;
409 
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);
415 			break;
416 
417 		case LP3944_LED_TYPE_NONE:
418 		default:
419 			break;
420 		}
421 
422 	return 0;
423 }
424 
425 /* lp3944 i2c driver struct */
426 static const struct i2c_device_id lp3944_id[] = {
427 	{"lp3944", 0},
428 	{}
429 };
430 
431 MODULE_DEVICE_TABLE(i2c, lp3944_id);
432 
433 static struct i2c_driver lp3944_driver = {
434 	.driver   = {
435 		   .name = "lp3944",
436 	},
437 	.probe    = lp3944_probe,
438 	.remove   = lp3944_remove,
439 	.id_table = lp3944_id,
440 };
441 
442 module_i2c_driver(lp3944_driver);
443 
444 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
445 MODULE_DESCRIPTION("LP3944 Fun Light Chip");
446 MODULE_LICENSE("GPL");
447