1 // SPDX-License-Identifier: GPL-2.0-only
3 * LEDs driver for Dialog Semiconductor DA9030/DA9034
5 * Copyright (C) 2008 Compulab, Ltd.
6 * Mike Rapoport <mike@compulab.co.il>
8 * Copyright (C) 2006-2008 Marvell International Ltd.
9 * Eric Miao <eric.miao@marvell.com>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/leds.h>
16 #include <linux/mfd/da903x.h>
17 #include <linux/slab.h>
19 #define DA9030_LED1_CONTROL 0x20
20 #define DA9030_LED2_CONTROL 0x21
21 #define DA9030_LED3_CONTROL 0x22
22 #define DA9030_LED4_CONTROL 0x23
23 #define DA9030_LEDPC_CONTROL 0x24
24 #define DA9030_MISC_CONTROL_A 0x26 /* Vibrator Control */
26 #define DA9034_LED1_CONTROL 0x35
27 #define DA9034_LED2_CONTROL 0x36
28 #define DA9034_VIBRA 0x40
31 struct led_classdev cdev
;
32 struct device
*master
;
37 #define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1)
38 #define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1)
40 static int da903x_led_set(struct led_classdev
*led_cdev
,
41 enum led_brightness value
)
43 struct da903x_led
*led
=
44 container_of(led_cdev
, struct da903x_led
, cdev
);
46 int offset
, ret
= -EINVAL
;
53 case DA9030_ID_LED_PC
:
54 offset
= DA9030_LED_OFFSET(led
->id
);
55 val
= led
->flags
& ~0x87;
56 val
|= value
? 0x80 : 0; /* EN bit */
57 val
|= (0x7 - (value
>> 5)) & 0x7; /* PWM<2:0> */
58 ret
= da903x_write(led
->master
, DA9030_LED1_CONTROL
+ offset
,
62 val
= led
->flags
& ~0x80;
63 val
|= value
? 0x80 : 0; /* EN bit */
64 ret
= da903x_write(led
->master
, DA9030_MISC_CONTROL_A
, val
);
68 offset
= DA9034_LED_OFFSET(led
->id
);
69 val
= (value
* 0x5f / LED_FULL
) & 0x7f;
70 val
|= (led
->flags
& DA9034_LED_RAMP
) ? 0x80 : 0;
71 ret
= da903x_write(led
->master
, DA9034_LED1_CONTROL
+ offset
,
76 ret
= da903x_write(led
->master
, DA9034_VIBRA
, val
);
83 static int da903x_led_probe(struct platform_device
*pdev
)
85 struct led_info
*pdata
= dev_get_platdata(&pdev
->dev
);
86 struct da903x_led
*led
;
94 if (!((id
>= DA9030_ID_LED_1
&& id
<= DA9030_ID_VIBRA
) ||
95 (id
>= DA9034_ID_LED_1
&& id
<= DA9034_ID_VIBRA
))) {
96 dev_err(&pdev
->dev
, "invalid LED ID (%d) specified\n", id
);
100 led
= devm_kzalloc(&pdev
->dev
, sizeof(struct da903x_led
), GFP_KERNEL
);
104 led
->cdev
.name
= pdata
->name
;
105 led
->cdev
.default_trigger
= pdata
->default_trigger
;
106 led
->cdev
.brightness_set_blocking
= da903x_led_set
;
107 led
->cdev
.brightness
= LED_OFF
;
110 led
->flags
= pdata
->flags
;
111 led
->master
= pdev
->dev
.parent
;
113 ret
= led_classdev_register(led
->master
, &led
->cdev
);
115 dev_err(&pdev
->dev
, "failed to register LED %d\n", id
);
119 platform_set_drvdata(pdev
, led
);
124 static int da903x_led_remove(struct platform_device
*pdev
)
126 struct da903x_led
*led
= platform_get_drvdata(pdev
);
128 led_classdev_unregister(&led
->cdev
);
133 static struct platform_driver da903x_led_driver
= {
135 .name
= "da903x-led",
137 .probe
= da903x_led_probe
,
138 .remove
= da903x_led_remove
,
141 module_platform_driver(da903x_led_driver
);
143 MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034");
144 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
145 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
146 MODULE_LICENSE("GPL");
147 MODULE_ALIAS("platform:da903x-led");