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/leds.h>
17 #include <linux/mfd/max8997.h>
18 #include <linux/mfd/max8997-private.h>
19 #include <linux/platform_device.h>
21 #define MAX8997_LED_FLASH_SHIFT 3
22 #define MAX8997_LED_FLASH_CUR_MASK 0xf8
23 #define MAX8997_LED_MOVIE_SHIFT 4
24 #define MAX8997_LED_MOVIE_CUR_MASK 0xf0
26 #define MAX8997_LED_FLASH_MAX_BRIGHTNESS 0x1f
27 #define MAX8997_LED_MOVIE_MAX_BRIGHTNESS 0xf
28 #define MAX8997_LED_NONE_MAX_BRIGHTNESS 0
30 #define MAX8997_LED0_FLASH_MASK 0x1
31 #define MAX8997_LED0_FLASH_PIN_MASK 0x5
32 #define MAX8997_LED0_MOVIE_MASK 0x8
33 #define MAX8997_LED0_MOVIE_PIN_MASK 0x28
35 #define MAX8997_LED1_FLASH_MASK 0x2
36 #define MAX8997_LED1_FLASH_PIN_MASK 0x6
37 #define MAX8997_LED1_MOVIE_MASK 0x10
38 #define MAX8997_LED1_MOVIE_PIN_MASK 0x30
40 #define MAX8997_LED_BOOST_ENABLE_MASK (1 << 6)
43 struct max8997_dev
*iodev
;
44 struct led_classdev cdev
;
47 enum max8997_led_mode led_mode
;
51 static void max8997_led_set_mode(struct max8997_led
*led
,
52 enum max8997_led_mode mode
)
55 struct i2c_client
*client
= led
->iodev
->i2c
;
59 case MAX8997_FLASH_MODE
:
60 mask
= MAX8997_LED1_FLASH_MASK
| MAX8997_LED0_FLASH_MASK
;
62 MAX8997_LED1_FLASH_MASK
: MAX8997_LED0_FLASH_MASK
;
63 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
65 case MAX8997_MOVIE_MODE
:
66 mask
= MAX8997_LED1_MOVIE_MASK
| MAX8997_LED0_MOVIE_MASK
;
68 MAX8997_LED1_MOVIE_MASK
: MAX8997_LED0_MOVIE_MASK
;
69 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
71 case MAX8997_FLASH_PIN_CONTROL_MODE
:
72 mask
= MAX8997_LED1_FLASH_PIN_MASK
|
73 MAX8997_LED0_FLASH_PIN_MASK
;
75 MAX8997_LED1_FLASH_PIN_MASK
: MAX8997_LED0_FLASH_PIN_MASK
;
76 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
78 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
79 mask
= MAX8997_LED1_MOVIE_PIN_MASK
|
80 MAX8997_LED0_MOVIE_PIN_MASK
;
82 MAX8997_LED1_MOVIE_PIN_MASK
: MAX8997_LED0_MOVIE_PIN_MASK
;
83 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
86 led
->cdev
.max_brightness
= MAX8997_LED_NONE_MAX_BRIGHTNESS
;
91 ret
= max8997_update_reg(client
, MAX8997_REG_LEN_CNTL
, val
,
94 dev_err(led
->iodev
->dev
,
95 "failed to update register(%d)\n", ret
);
101 static void max8997_led_enable(struct max8997_led
*led
, bool enable
)
104 struct i2c_client
*client
= led
->iodev
->i2c
;
105 u8 val
= 0, mask
= MAX8997_LED_BOOST_ENABLE_MASK
;
107 if (led
->enabled
== enable
)
110 val
= enable
? MAX8997_LED_BOOST_ENABLE_MASK
: 0;
112 ret
= max8997_update_reg(client
, MAX8997_REG_BOOST_CNTL
, val
, mask
);
114 dev_err(led
->iodev
->dev
,
115 "failed to update register(%d)\n", ret
);
117 led
->enabled
= enable
;
120 static void max8997_led_set_current(struct max8997_led
*led
,
121 enum led_brightness value
)
124 struct i2c_client
*client
= led
->iodev
->i2c
;
125 u8 val
= 0, mask
= 0, reg
= 0;
127 switch (led
->led_mode
) {
128 case MAX8997_FLASH_MODE
:
129 case MAX8997_FLASH_PIN_CONTROL_MODE
:
130 val
= value
<< MAX8997_LED_FLASH_SHIFT
;
131 mask
= MAX8997_LED_FLASH_CUR_MASK
;
132 reg
= led
->id
? MAX8997_REG_FLASH2_CUR
: MAX8997_REG_FLASH1_CUR
;
134 case MAX8997_MOVIE_MODE
:
135 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
136 val
= value
<< MAX8997_LED_MOVIE_SHIFT
;
137 mask
= MAX8997_LED_MOVIE_CUR_MASK
;
138 reg
= MAX8997_REG_MOVIE_CUR
;
145 ret
= max8997_update_reg(client
, reg
, val
, mask
);
147 dev_err(led
->iodev
->dev
,
148 "failed to update register(%d)\n", ret
);
152 static void max8997_led_brightness_set(struct led_classdev
*led_cdev
,
153 enum led_brightness value
)
155 struct max8997_led
*led
=
156 container_of(led_cdev
, struct max8997_led
, cdev
);
159 max8997_led_set_current(led
, value
);
160 max8997_led_enable(led
, true);
162 max8997_led_set_current(led
, value
);
163 max8997_led_enable(led
, false);
167 static ssize_t
max8997_led_show_mode(struct device
*dev
,
168 struct device_attribute
*attr
, char *buf
)
170 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
171 struct max8997_led
*led
=
172 container_of(led_cdev
, struct max8997_led
, cdev
);
175 mutex_lock(&led
->mutex
);
177 switch (led
->led_mode
) {
178 case MAX8997_FLASH_MODE
:
179 ret
+= sprintf(buf
, "FLASH\n");
181 case MAX8997_MOVIE_MODE
:
182 ret
+= sprintf(buf
, "MOVIE\n");
184 case MAX8997_FLASH_PIN_CONTROL_MODE
:
185 ret
+= sprintf(buf
, "FLASH_PIN_CONTROL\n");
187 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
188 ret
+= sprintf(buf
, "MOVIE_PIN_CONTROL\n");
191 ret
+= sprintf(buf
, "NONE\n");
195 mutex_unlock(&led
->mutex
);
200 static ssize_t
max8997_led_store_mode(struct device
*dev
,
201 struct device_attribute
*attr
,
202 const char *buf
, size_t size
)
204 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
205 struct max8997_led
*led
=
206 container_of(led_cdev
, struct max8997_led
, cdev
);
207 enum max8997_led_mode mode
;
209 mutex_lock(&led
->mutex
);
211 if (!strncmp(buf
, "FLASH_PIN_CONTROL", 17))
212 mode
= MAX8997_FLASH_PIN_CONTROL_MODE
;
213 else if (!strncmp(buf
, "MOVIE_PIN_CONTROL", 17))
214 mode
= MAX8997_MOVIE_PIN_CONTROL_MODE
;
215 else if (!strncmp(buf
, "FLASH", 5))
216 mode
= MAX8997_FLASH_MODE
;
217 else if (!strncmp(buf
, "MOVIE", 5))
218 mode
= MAX8997_MOVIE_MODE
;
222 max8997_led_set_mode(led
, mode
);
224 mutex_unlock(&led
->mutex
);
229 static DEVICE_ATTR(mode
, 0644, max8997_led_show_mode
, max8997_led_store_mode
);
231 static struct attribute
*max8997_attrs
[] = {
235 ATTRIBUTE_GROUPS(max8997
);
237 static int max8997_led_probe(struct platform_device
*pdev
)
239 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
240 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
241 struct max8997_led
*led
;
246 dev_err(&pdev
->dev
, "no platform data\n");
250 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
255 snprintf(name
, sizeof(name
), "max8997-led%d", pdev
->id
);
257 led
->cdev
.name
= name
;
258 led
->cdev
.brightness_set
= max8997_led_brightness_set
;
259 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
260 led
->cdev
.brightness
= 0;
261 led
->cdev
.groups
= max8997_groups
;
264 /* initialize mode and brightness according to platform_data */
265 if (pdata
->led_pdata
) {
266 u8 mode
= 0, brightness
= 0;
268 mode
= pdata
->led_pdata
->mode
[led
->id
];
269 brightness
= pdata
->led_pdata
->brightness
[led
->id
];
271 max8997_led_set_mode(led
, mode
);
273 if (brightness
> led
->cdev
.max_brightness
)
274 brightness
= led
->cdev
.max_brightness
;
275 max8997_led_set_current(led
, brightness
);
276 led
->cdev
.brightness
= brightness
;
278 max8997_led_set_mode(led
, MAX8997_NONE
);
279 max8997_led_set_current(led
, 0);
282 mutex_init(&led
->mutex
);
284 ret
= devm_led_classdev_register(&pdev
->dev
, &led
->cdev
);
291 static struct platform_driver max8997_led_driver
= {
293 .name
= "max8997-led",
295 .probe
= max8997_led_probe
,
298 module_platform_driver(max8997_led_driver
);
300 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
301 MODULE_DESCRIPTION("MAX8997 LED driver");
302 MODULE_LICENSE("GPL");
303 MODULE_ALIAS("platform:max8997-led");