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_set_mode(struct max8997_led
*led
,
53 enum max8997_led_mode mode
)
56 struct i2c_client
*client
= led
->iodev
->i2c
;
60 case MAX8997_FLASH_MODE
:
61 mask
= MAX8997_LED1_FLASH_MASK
| MAX8997_LED0_FLASH_MASK
;
63 MAX8997_LED1_FLASH_MASK
: MAX8997_LED0_FLASH_MASK
;
64 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
66 case MAX8997_MOVIE_MODE
:
67 mask
= MAX8997_LED1_MOVIE_MASK
| MAX8997_LED0_MOVIE_MASK
;
69 MAX8997_LED1_MOVIE_MASK
: MAX8997_LED0_MOVIE_MASK
;
70 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
72 case MAX8997_FLASH_PIN_CONTROL_MODE
:
73 mask
= MAX8997_LED1_FLASH_PIN_MASK
|
74 MAX8997_LED0_FLASH_PIN_MASK
;
76 MAX8997_LED1_FLASH_PIN_MASK
: MAX8997_LED0_FLASH_PIN_MASK
;
77 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
79 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
80 mask
= MAX8997_LED1_MOVIE_PIN_MASK
|
81 MAX8997_LED0_MOVIE_PIN_MASK
;
83 MAX8997_LED1_MOVIE_PIN_MASK
: MAX8997_LED0_MOVIE_PIN_MASK
;
84 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
87 led
->cdev
.max_brightness
= MAX8997_LED_NONE_MAX_BRIGHTNESS
;
92 ret
= max8997_update_reg(client
, MAX8997_REG_LEN_CNTL
, val
,
95 dev_err(led
->iodev
->dev
,
96 "failed to update register(%d)\n", ret
);
102 static void max8997_led_enable(struct max8997_led
*led
, bool enable
)
105 struct i2c_client
*client
= led
->iodev
->i2c
;
106 u8 val
= 0, mask
= MAX8997_LED_BOOST_ENABLE_MASK
;
108 if (led
->enabled
== enable
)
111 val
= enable
? MAX8997_LED_BOOST_ENABLE_MASK
: 0;
113 ret
= max8997_update_reg(client
, MAX8997_REG_BOOST_CNTL
, val
, mask
);
115 dev_err(led
->iodev
->dev
,
116 "failed to update register(%d)\n", ret
);
118 led
->enabled
= enable
;
121 static void max8997_led_set_current(struct max8997_led
*led
,
122 enum led_brightness value
)
125 struct i2c_client
*client
= led
->iodev
->i2c
;
126 u8 val
= 0, mask
= 0, reg
= 0;
128 switch (led
->led_mode
) {
129 case MAX8997_FLASH_MODE
:
130 case MAX8997_FLASH_PIN_CONTROL_MODE
:
131 val
= value
<< MAX8997_LED_FLASH_SHIFT
;
132 mask
= MAX8997_LED_FLASH_CUR_MASK
;
133 reg
= led
->id
? MAX8997_REG_FLASH2_CUR
: MAX8997_REG_FLASH1_CUR
;
135 case MAX8997_MOVIE_MODE
:
136 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
137 val
= value
<< MAX8997_LED_MOVIE_SHIFT
;
138 mask
= MAX8997_LED_MOVIE_CUR_MASK
;
139 reg
= MAX8997_REG_MOVIE_CUR
;
146 ret
= max8997_update_reg(client
, reg
, val
, mask
);
148 dev_err(led
->iodev
->dev
,
149 "failed to update register(%d)\n", ret
);
153 static void max8997_led_brightness_set(struct led_classdev
*led_cdev
,
154 enum led_brightness value
)
156 struct max8997_led
*led
=
157 container_of(led_cdev
, struct max8997_led
, cdev
);
160 max8997_led_set_current(led
, value
);
161 max8997_led_enable(led
, true);
163 max8997_led_set_current(led
, value
);
164 max8997_led_enable(led
, false);
168 static ssize_t
max8997_led_show_mode(struct device
*dev
,
169 struct device_attribute
*attr
, char *buf
)
171 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
172 struct max8997_led
*led
=
173 container_of(led_cdev
, struct max8997_led
, cdev
);
176 mutex_lock(&led
->mutex
);
178 switch (led
->led_mode
) {
179 case MAX8997_FLASH_MODE
:
180 ret
+= sprintf(buf
, "FLASH\n");
182 case MAX8997_MOVIE_MODE
:
183 ret
+= sprintf(buf
, "MOVIE\n");
185 case MAX8997_FLASH_PIN_CONTROL_MODE
:
186 ret
+= sprintf(buf
, "FLASH_PIN_CONTROL\n");
188 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
189 ret
+= sprintf(buf
, "MOVIE_PIN_CONTROL\n");
192 ret
+= sprintf(buf
, "NONE\n");
196 mutex_unlock(&led
->mutex
);
201 static ssize_t
max8997_led_store_mode(struct device
*dev
,
202 struct device_attribute
*attr
,
203 const char *buf
, size_t size
)
205 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
206 struct max8997_led
*led
=
207 container_of(led_cdev
, struct max8997_led
, cdev
);
208 enum max8997_led_mode mode
;
210 mutex_lock(&led
->mutex
);
212 if (!strncmp(buf
, "FLASH_PIN_CONTROL", 17))
213 mode
= MAX8997_FLASH_PIN_CONTROL_MODE
;
214 else if (!strncmp(buf
, "MOVIE_PIN_CONTROL", 17))
215 mode
= MAX8997_MOVIE_PIN_CONTROL_MODE
;
216 else if (!strncmp(buf
, "FLASH", 5))
217 mode
= MAX8997_FLASH_MODE
;
218 else if (!strncmp(buf
, "MOVIE", 5))
219 mode
= MAX8997_MOVIE_MODE
;
223 max8997_led_set_mode(led
, mode
);
225 mutex_unlock(&led
->mutex
);
230 static DEVICE_ATTR(mode
, 0644, max8997_led_show_mode
, max8997_led_store_mode
);
232 static struct attribute
*max8997_attrs
[] = {
236 ATTRIBUTE_GROUPS(max8997
);
238 static int max8997_led_probe(struct platform_device
*pdev
)
240 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
241 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
242 struct max8997_led
*led
;
247 dev_err(&pdev
->dev
, "no platform data\n");
251 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
256 snprintf(name
, sizeof(name
), "max8997-led%d", pdev
->id
);
258 led
->cdev
.name
= name
;
259 led
->cdev
.brightness_set
= max8997_led_brightness_set
;
260 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
261 led
->cdev
.brightness
= 0;
262 led
->cdev
.groups
= max8997_groups
;
265 /* initialize mode and brightness according to platform_data */
266 if (pdata
->led_pdata
) {
267 u8 mode
= 0, brightness
= 0;
269 mode
= pdata
->led_pdata
->mode
[led
->id
];
270 brightness
= pdata
->led_pdata
->brightness
[led
->id
];
272 max8997_led_set_mode(led
, pdata
->led_pdata
->mode
[led
->id
]);
274 if (brightness
> led
->cdev
.max_brightness
)
275 brightness
= led
->cdev
.max_brightness
;
276 max8997_led_set_current(led
, brightness
);
277 led
->cdev
.brightness
= brightness
;
279 max8997_led_set_mode(led
, MAX8997_NONE
);
280 max8997_led_set_current(led
, 0);
283 mutex_init(&led
->mutex
);
285 platform_set_drvdata(pdev
, led
);
287 ret
= led_classdev_register(&pdev
->dev
, &led
->cdev
);
294 static int max8997_led_remove(struct platform_device
*pdev
)
296 struct max8997_led
*led
= platform_get_drvdata(pdev
);
298 led_classdev_unregister(&led
->cdev
);
303 static struct platform_driver max8997_led_driver
= {
305 .name
= "max8997-led",
307 .probe
= max8997_led_probe
,
308 .remove
= max8997_led_remove
,
311 module_platform_driver(max8997_led_driver
);
313 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
314 MODULE_DESCRIPTION("MAX8997 LED driver");
315 MODULE_LICENSE("GPL");
316 MODULE_ALIAS("platform:max8997-led");