2 * leds-max8997.c - LED class driver for MAX8997 LEDs.
4 * Copyright (C) 2011 Samsung Electronics
5 * Donggeun Kim <dg77.kim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/leds.h>
18 #include <linux/mfd/max8997.h>
19 #include <linux/mfd/max8997-private.h>
20 #include <linux/platform_device.h>
22 #define MAX8997_LED_FLASH_SHIFT 3
23 #define MAX8997_LED_FLASH_CUR_MASK 0xf8
24 #define MAX8997_LED_MOVIE_SHIFT 4
25 #define MAX8997_LED_MOVIE_CUR_MASK 0xf0
27 #define MAX8997_LED_FLASH_MAX_BRIGHTNESS 0x1f
28 #define MAX8997_LED_MOVIE_MAX_BRIGHTNESS 0xf
29 #define MAX8997_LED_NONE_MAX_BRIGHTNESS 0
31 #define MAX8997_LED0_FLASH_MASK 0x1
32 #define MAX8997_LED0_FLASH_PIN_MASK 0x5
33 #define MAX8997_LED0_MOVIE_MASK 0x8
34 #define MAX8997_LED0_MOVIE_PIN_MASK 0x28
36 #define MAX8997_LED1_FLASH_MASK 0x2
37 #define MAX8997_LED1_FLASH_PIN_MASK 0x6
38 #define MAX8997_LED1_MOVIE_MASK 0x10
39 #define MAX8997_LED1_MOVIE_PIN_MASK 0x30
41 #define MAX8997_LED_BOOST_ENABLE_MASK (1 << 6)
44 struct max8997_dev
*iodev
;
45 struct led_classdev cdev
;
48 enum max8997_led_mode led_mode
;
52 static void max8997_led_clear_mode(struct max8997_led
*led
,
53 enum max8997_led_mode mode
)
55 struct i2c_client
*client
= led
->iodev
->i2c
;
60 case MAX8997_FLASH_MODE
:
62 MAX8997_LED1_FLASH_MASK
: MAX8997_LED0_FLASH_MASK
;
64 case MAX8997_MOVIE_MODE
:
66 MAX8997_LED1_MOVIE_MASK
: MAX8997_LED0_MOVIE_MASK
;
68 case MAX8997_FLASH_PIN_CONTROL_MODE
:
70 MAX8997_LED1_FLASH_PIN_MASK
: MAX8997_LED0_FLASH_PIN_MASK
;
72 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
74 MAX8997_LED1_MOVIE_PIN_MASK
: MAX8997_LED0_MOVIE_PIN_MASK
;
81 ret
= max8997_update_reg(client
,
82 MAX8997_REG_LEN_CNTL
, val
, mask
);
84 dev_err(led
->iodev
->dev
,
85 "failed to update register(%d)\n", ret
);
89 static void max8997_led_set_mode(struct max8997_led
*led
,
90 enum max8997_led_mode mode
)
93 struct i2c_client
*client
= led
->iodev
->i2c
;
96 /* First, clear the previous mode */
97 max8997_led_clear_mode(led
, led
->led_mode
);
100 case MAX8997_FLASH_MODE
:
102 MAX8997_LED1_FLASH_MASK
: MAX8997_LED0_FLASH_MASK
;
103 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
105 case MAX8997_MOVIE_MODE
:
107 MAX8997_LED1_MOVIE_MASK
: MAX8997_LED0_MOVIE_MASK
;
108 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
110 case MAX8997_FLASH_PIN_CONTROL_MODE
:
112 MAX8997_LED1_FLASH_PIN_MASK
: MAX8997_LED0_FLASH_PIN_MASK
;
113 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
115 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
117 MAX8997_LED1_MOVIE_PIN_MASK
: MAX8997_LED0_MOVIE_PIN_MASK
;
118 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
121 led
->cdev
.max_brightness
= MAX8997_LED_NONE_MAX_BRIGHTNESS
;
126 ret
= max8997_update_reg(client
,
127 MAX8997_REG_LEN_CNTL
, mask
, mask
);
129 dev_err(led
->iodev
->dev
,
130 "failed to update register(%d)\n", ret
);
133 led
->led_mode
= mode
;
136 static void max8997_led_enable(struct max8997_led
*led
, bool enable
)
139 struct i2c_client
*client
= led
->iodev
->i2c
;
140 u8 val
= 0, mask
= MAX8997_LED_BOOST_ENABLE_MASK
;
142 if (led
->enabled
== enable
)
145 val
= enable
? MAX8997_LED_BOOST_ENABLE_MASK
: 0;
147 ret
= max8997_update_reg(client
, MAX8997_REG_BOOST_CNTL
, val
, mask
);
149 dev_err(led
->iodev
->dev
,
150 "failed to update register(%d)\n", ret
);
152 led
->enabled
= enable
;
155 static void max8997_led_set_current(struct max8997_led
*led
,
156 enum led_brightness value
)
159 struct i2c_client
*client
= led
->iodev
->i2c
;
160 u8 val
= 0, mask
= 0, reg
= 0;
162 switch (led
->led_mode
) {
163 case MAX8997_FLASH_MODE
:
164 case MAX8997_FLASH_PIN_CONTROL_MODE
:
165 val
= value
<< MAX8997_LED_FLASH_SHIFT
;
166 mask
= MAX8997_LED_FLASH_CUR_MASK
;
167 reg
= led
->id
? MAX8997_REG_FLASH2_CUR
: MAX8997_REG_FLASH1_CUR
;
169 case MAX8997_MOVIE_MODE
:
170 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
171 val
= value
<< MAX8997_LED_MOVIE_SHIFT
;
172 mask
= MAX8997_LED_MOVIE_CUR_MASK
;
173 reg
= MAX8997_REG_MOVIE_CUR
;
180 ret
= max8997_update_reg(client
, reg
, val
, mask
);
182 dev_err(led
->iodev
->dev
,
183 "failed to update register(%d)\n", ret
);
187 static void max8997_led_brightness_set(struct led_classdev
*led_cdev
,
188 enum led_brightness value
)
190 struct max8997_led
*led
=
191 container_of(led_cdev
, struct max8997_led
, cdev
);
194 max8997_led_set_current(led
, value
);
195 max8997_led_enable(led
, true);
197 max8997_led_set_current(led
, value
);
198 max8997_led_enable(led
, false);
202 static ssize_t
max8997_led_show_mode(struct device
*dev
,
203 struct device_attribute
*attr
, char *buf
)
205 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
206 struct max8997_led
*led
=
207 container_of(led_cdev
, struct max8997_led
, cdev
);
210 mutex_lock(&led
->mutex
);
212 switch (led
->led_mode
) {
213 case MAX8997_FLASH_MODE
:
214 ret
+= sprintf(buf
, "FLASH\n");
216 case MAX8997_MOVIE_MODE
:
217 ret
+= sprintf(buf
, "MOVIE\n");
219 case MAX8997_FLASH_PIN_CONTROL_MODE
:
220 ret
+= sprintf(buf
, "FLASH_PIN_CONTROL\n");
222 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
223 ret
+= sprintf(buf
, "MOVIE_PIN_CONTROL\n");
226 ret
+= sprintf(buf
, "NONE\n");
230 mutex_unlock(&led
->mutex
);
235 static ssize_t
max8997_led_store_mode(struct device
*dev
,
236 struct device_attribute
*attr
,
237 const char *buf
, size_t size
)
239 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
240 struct max8997_led
*led
=
241 container_of(led_cdev
, struct max8997_led
, cdev
);
242 enum max8997_led_mode mode
;
244 mutex_lock(&led
->mutex
);
246 if (!strncmp(buf
, "FLASH_PIN_CONTROL", 17))
247 mode
= MAX8997_FLASH_PIN_CONTROL_MODE
;
248 else if (!strncmp(buf
, "MOVIE_PIN_CONTROL", 17))
249 mode
= MAX8997_MOVIE_PIN_CONTROL_MODE
;
250 else if (!strncmp(buf
, "FLASH", 5))
251 mode
= MAX8997_FLASH_MODE
;
252 else if (!strncmp(buf
, "MOVIE", 5))
253 mode
= MAX8997_MOVIE_MODE
;
257 max8997_led_set_mode(led
, mode
);
259 mutex_unlock(&led
->mutex
);
264 static DEVICE_ATTR(mode
, 0644, max8997_led_show_mode
, max8997_led_store_mode
);
266 static int __devinit
max8997_led_probe(struct platform_device
*pdev
)
268 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
269 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
270 struct max8997_led
*led
;
275 dev_err(&pdev
->dev
, "no platform data\n");
279 led
= kzalloc(sizeof(*led
), GFP_KERNEL
);
286 snprintf(name
, sizeof(name
), "max8997-led%d", pdev
->id
);
288 led
->cdev
.name
= name
;
289 led
->cdev
.brightness_set
= max8997_led_brightness_set
;
290 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
291 led
->cdev
.brightness
= 0;
294 /* initialize mode and brightness according to platform_data */
295 if (pdata
->led_pdata
) {
296 u8 mode
= 0, brightness
= 0;
298 mode
= pdata
->led_pdata
->mode
[led
->id
];
299 brightness
= pdata
->led_pdata
->brightness
[led
->id
];
301 max8997_led_set_mode(led
, pdata
->led_pdata
->mode
[led
->id
]);
303 if (brightness
> led
->cdev
.max_brightness
)
304 brightness
= led
->cdev
.max_brightness
;
305 max8997_led_set_current(led
, brightness
);
306 led
->cdev
.brightness
= brightness
;
308 max8997_led_set_mode(led
, MAX8997_NONE
);
309 max8997_led_set_current(led
, 0);
312 mutex_init(&led
->mutex
);
314 platform_set_drvdata(pdev
, led
);
316 ret
= led_classdev_register(&pdev
->dev
, &led
->cdev
);
320 ret
= device_create_file(led
->cdev
.dev
, &dev_attr_mode
);
323 "failed to create file: %d\n", ret
);
330 led_classdev_unregister(&led
->cdev
);
337 static int __devexit
max8997_led_remove(struct platform_device
*pdev
)
339 struct max8997_led
*led
= platform_get_drvdata(pdev
);
341 device_remove_file(led
->cdev
.dev
, &dev_attr_mode
);
342 led_classdev_unregister(&led
->cdev
);
348 static struct platform_driver max8997_led_driver
= {
350 .name
= "max8997-led",
351 .owner
= THIS_MODULE
,
353 .probe
= max8997_led_probe
,
354 .remove
= __devexit_p(max8997_led_remove
),
357 static int __init
max8997_led_init(void)
359 return platform_driver_register(&max8997_led_driver
);
361 module_init(max8997_led_init
);
363 static void __exit
max8997_led_exit(void)
365 platform_driver_unregister(&max8997_led_driver
);
367 module_exit(max8997_led_exit
);
369 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
370 MODULE_DESCRIPTION("MAX8997 LED driver");
371 MODULE_LICENSE("GPL");
372 MODULE_ALIAS("platform:max8997-led");