1 // SPDX-License-Identifier: GPL-2.0-only
3 * leds-max8997.c - LED class driver for MAX8997 LEDs.
5 * Copyright (C) 2011 Samsung Electronics
6 * Donggeun Kim <dg77.kim@samsung.com>
9 #include <linux/module.h>
10 #include <linux/err.h>
11 #include <linux/slab.h>
12 #include <linux/leds.h>
13 #include <linux/mfd/max8997.h>
14 #include <linux/mfd/max8997-private.h>
15 #include <linux/platform_device.h>
17 #define MAX8997_LED_FLASH_SHIFT 3
18 #define MAX8997_LED_FLASH_CUR_MASK 0xf8
19 #define MAX8997_LED_MOVIE_SHIFT 4
20 #define MAX8997_LED_MOVIE_CUR_MASK 0xf0
22 #define MAX8997_LED_FLASH_MAX_BRIGHTNESS 0x1f
23 #define MAX8997_LED_MOVIE_MAX_BRIGHTNESS 0xf
24 #define MAX8997_LED_NONE_MAX_BRIGHTNESS 0
26 #define MAX8997_LED0_FLASH_MASK 0x1
27 #define MAX8997_LED0_FLASH_PIN_MASK 0x5
28 #define MAX8997_LED0_MOVIE_MASK 0x8
29 #define MAX8997_LED0_MOVIE_PIN_MASK 0x28
31 #define MAX8997_LED1_FLASH_MASK 0x2
32 #define MAX8997_LED1_FLASH_PIN_MASK 0x6
33 #define MAX8997_LED1_MOVIE_MASK 0x10
34 #define MAX8997_LED1_MOVIE_PIN_MASK 0x30
36 #define MAX8997_LED_BOOST_ENABLE_MASK (1 << 6)
39 struct max8997_dev
*iodev
;
40 struct led_classdev cdev
;
43 enum max8997_led_mode led_mode
;
47 static void max8997_led_set_mode(struct max8997_led
*led
,
48 enum max8997_led_mode mode
)
51 struct i2c_client
*client
= led
->iodev
->i2c
;
55 case MAX8997_FLASH_MODE
:
56 mask
= MAX8997_LED1_FLASH_MASK
| MAX8997_LED0_FLASH_MASK
;
58 MAX8997_LED1_FLASH_MASK
: MAX8997_LED0_FLASH_MASK
;
59 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
61 case MAX8997_MOVIE_MODE
:
62 mask
= MAX8997_LED1_MOVIE_MASK
| MAX8997_LED0_MOVIE_MASK
;
64 MAX8997_LED1_MOVIE_MASK
: MAX8997_LED0_MOVIE_MASK
;
65 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
67 case MAX8997_FLASH_PIN_CONTROL_MODE
:
68 mask
= MAX8997_LED1_FLASH_PIN_MASK
|
69 MAX8997_LED0_FLASH_PIN_MASK
;
71 MAX8997_LED1_FLASH_PIN_MASK
: MAX8997_LED0_FLASH_PIN_MASK
;
72 led
->cdev
.max_brightness
= MAX8997_LED_FLASH_MAX_BRIGHTNESS
;
74 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
75 mask
= MAX8997_LED1_MOVIE_PIN_MASK
|
76 MAX8997_LED0_MOVIE_PIN_MASK
;
78 MAX8997_LED1_MOVIE_PIN_MASK
: MAX8997_LED0_MOVIE_PIN_MASK
;
79 led
->cdev
.max_brightness
= MAX8997_LED_MOVIE_MAX_BRIGHTNESS
;
82 led
->cdev
.max_brightness
= MAX8997_LED_NONE_MAX_BRIGHTNESS
;
87 ret
= max8997_update_reg(client
, MAX8997_REG_LEN_CNTL
, val
,
90 dev_err(led
->iodev
->dev
,
91 "failed to update register(%d)\n", ret
);
97 static void max8997_led_enable(struct max8997_led
*led
, bool enable
)
100 struct i2c_client
*client
= led
->iodev
->i2c
;
101 u8 val
= 0, mask
= MAX8997_LED_BOOST_ENABLE_MASK
;
103 if (led
->enabled
== enable
)
106 val
= enable
? MAX8997_LED_BOOST_ENABLE_MASK
: 0;
108 ret
= max8997_update_reg(client
, MAX8997_REG_BOOST_CNTL
, val
, mask
);
110 dev_err(led
->iodev
->dev
,
111 "failed to update register(%d)\n", ret
);
113 led
->enabled
= enable
;
116 static void max8997_led_set_current(struct max8997_led
*led
,
117 enum led_brightness value
)
120 struct i2c_client
*client
= led
->iodev
->i2c
;
121 u8 val
= 0, mask
= 0, reg
= 0;
123 switch (led
->led_mode
) {
124 case MAX8997_FLASH_MODE
:
125 case MAX8997_FLASH_PIN_CONTROL_MODE
:
126 val
= value
<< MAX8997_LED_FLASH_SHIFT
;
127 mask
= MAX8997_LED_FLASH_CUR_MASK
;
128 reg
= led
->id
? MAX8997_REG_FLASH2_CUR
: MAX8997_REG_FLASH1_CUR
;
130 case MAX8997_MOVIE_MODE
:
131 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
132 val
= value
<< MAX8997_LED_MOVIE_SHIFT
;
133 mask
= MAX8997_LED_MOVIE_CUR_MASK
;
134 reg
= MAX8997_REG_MOVIE_CUR
;
141 ret
= max8997_update_reg(client
, reg
, val
, mask
);
143 dev_err(led
->iodev
->dev
,
144 "failed to update register(%d)\n", ret
);
148 static void max8997_led_brightness_set(struct led_classdev
*led_cdev
,
149 enum led_brightness value
)
151 struct max8997_led
*led
=
152 container_of(led_cdev
, struct max8997_led
, cdev
);
155 max8997_led_set_current(led
, value
);
156 max8997_led_enable(led
, true);
158 max8997_led_set_current(led
, value
);
159 max8997_led_enable(led
, false);
163 static ssize_t
mode_show(struct device
*dev
,
164 struct device_attribute
*attr
, char *buf
)
166 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
167 struct max8997_led
*led
=
168 container_of(led_cdev
, struct max8997_led
, cdev
);
171 mutex_lock(&led
->mutex
);
173 switch (led
->led_mode
) {
174 case MAX8997_FLASH_MODE
:
175 ret
+= sprintf(buf
, "FLASH\n");
177 case MAX8997_MOVIE_MODE
:
178 ret
+= sprintf(buf
, "MOVIE\n");
180 case MAX8997_FLASH_PIN_CONTROL_MODE
:
181 ret
+= sprintf(buf
, "FLASH_PIN_CONTROL\n");
183 case MAX8997_MOVIE_PIN_CONTROL_MODE
:
184 ret
+= sprintf(buf
, "MOVIE_PIN_CONTROL\n");
187 ret
+= sprintf(buf
, "NONE\n");
191 mutex_unlock(&led
->mutex
);
196 static ssize_t
mode_store(struct device
*dev
,
197 struct device_attribute
*attr
,
198 const char *buf
, size_t size
)
200 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
201 struct max8997_led
*led
=
202 container_of(led_cdev
, struct max8997_led
, cdev
);
203 enum max8997_led_mode mode
;
205 mutex_lock(&led
->mutex
);
207 if (!strncmp(buf
, "FLASH_PIN_CONTROL", 17))
208 mode
= MAX8997_FLASH_PIN_CONTROL_MODE
;
209 else if (!strncmp(buf
, "MOVIE_PIN_CONTROL", 17))
210 mode
= MAX8997_MOVIE_PIN_CONTROL_MODE
;
211 else if (!strncmp(buf
, "FLASH", 5))
212 mode
= MAX8997_FLASH_MODE
;
213 else if (!strncmp(buf
, "MOVIE", 5))
214 mode
= MAX8997_MOVIE_MODE
;
218 max8997_led_set_mode(led
, mode
);
220 mutex_unlock(&led
->mutex
);
225 static DEVICE_ATTR_RW(mode
);
227 static struct attribute
*max8997_attrs
[] = {
231 ATTRIBUTE_GROUPS(max8997
);
233 static int max8997_led_probe(struct platform_device
*pdev
)
235 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
236 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
237 struct max8997_led
*led
;
241 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
246 snprintf(name
, sizeof(name
), "max8997-led%d", pdev
->id
);
248 led
->cdev
.name
= name
;
249 led
->cdev
.brightness_set
= max8997_led_brightness_set
;
250 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
251 led
->cdev
.brightness
= 0;
252 led
->cdev
.groups
= max8997_groups
;
255 /* initialize mode and brightness according to platform_data */
256 if (pdata
&& pdata
->led_pdata
) {
257 u8 mode
= 0, brightness
= 0;
259 mode
= pdata
->led_pdata
->mode
[led
->id
];
260 brightness
= pdata
->led_pdata
->brightness
[led
->id
];
262 max8997_led_set_mode(led
, mode
);
264 if (brightness
> led
->cdev
.max_brightness
)
265 brightness
= led
->cdev
.max_brightness
;
266 max8997_led_set_current(led
, brightness
);
267 led
->cdev
.brightness
= brightness
;
269 max8997_led_set_mode(led
, MAX8997_NONE
);
270 max8997_led_set_current(led
, 0);
273 mutex_init(&led
->mutex
);
275 ret
= devm_led_classdev_register(&pdev
->dev
, &led
->cdev
);
282 static struct platform_driver max8997_led_driver
= {
284 .name
= "max8997-led",
286 .probe
= max8997_led_probe
,
289 module_platform_driver(max8997_led_driver
);
291 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
292 MODULE_DESCRIPTION("MAX8997 LED driver");
293 MODULE_LICENSE("GPL");
294 MODULE_ALIAS("platform:max8997-led");