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 int max8997_led_probe(struct platform_device
*pdev
)
234 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
235 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
236 struct max8997_led
*led
;
241 dev_err(&pdev
->dev
, "no platform data\n");
245 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
250 snprintf(name
, sizeof(name
), "max8997-led%d", pdev
->id
);
252 led
->cdev
.name
= name
;
253 led
->cdev
.brightness_set
= max8997_led_brightness_set
;
254 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
255 led
->cdev
.brightness
= 0;
258 /* initialize mode and brightness according to platform_data */
259 if (pdata
->led_pdata
) {
260 u8 mode
= 0, brightness
= 0;
262 mode
= pdata
->led_pdata
->mode
[led
->id
];
263 brightness
= pdata
->led_pdata
->brightness
[led
->id
];
265 max8997_led_set_mode(led
, pdata
->led_pdata
->mode
[led
->id
]);
267 if (brightness
> led
->cdev
.max_brightness
)
268 brightness
= led
->cdev
.max_brightness
;
269 max8997_led_set_current(led
, brightness
);
270 led
->cdev
.brightness
= brightness
;
272 max8997_led_set_mode(led
, MAX8997_NONE
);
273 max8997_led_set_current(led
, 0);
276 mutex_init(&led
->mutex
);
278 platform_set_drvdata(pdev
, led
);
280 ret
= led_classdev_register(&pdev
->dev
, &led
->cdev
);
284 ret
= device_create_file(led
->cdev
.dev
, &dev_attr_mode
);
287 "failed to create file: %d\n", ret
);
288 led_classdev_unregister(&led
->cdev
);
295 static int max8997_led_remove(struct platform_device
*pdev
)
297 struct max8997_led
*led
= platform_get_drvdata(pdev
);
299 device_remove_file(led
->cdev
.dev
, &dev_attr_mode
);
300 led_classdev_unregister(&led
->cdev
);
305 static struct platform_driver max8997_led_driver
= {
307 .name
= "max8997-led",
308 .owner
= THIS_MODULE
,
310 .probe
= max8997_led_probe
,
311 .remove
= max8997_led_remove
,
314 module_platform_driver(max8997_led_driver
);
316 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
317 MODULE_DESCRIPTION("MAX8997 LED driver");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS("platform:max8997-led");