blob: 2b5fb00438a2d3e043ef26a13da3605eabe153bf [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mike Rapoport9e845612008-10-13 09:06:10 +01002/*
3 * LEDs driver for Dialog Semiconductor DA9030/DA9034
4 *
5 * Copyright (C) 2008 Compulab, Ltd.
Sachin Kamat141bfee2012-11-25 11:16:28 +05306 * Mike Rapoport <mike@compulab.co.il>
Mike Rapoport9e845612008-10-13 09:06:10 +01007 *
8 * Copyright (C) 2006-2008 Marvell International Ltd.
Sachin Kamat141bfee2012-11-25 11:16:28 +05309 * Eric Miao <eric.miao@marvell.com>
Mike Rapoport9e845612008-10-13 09:06:10 +010010 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
Mike Rapoport9e845612008-10-13 09:06:10 +010014#include <linux/platform_device.h>
15#include <linux/leds.h>
Mike Rapoport9e845612008-10-13 09:06:10 +010016#include <linux/mfd/da903x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mike Rapoport9e845612008-10-13 09:06:10 +010018
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 */
25
26#define DA9034_LED1_CONTROL 0x35
27#define DA9034_LED2_CONTROL 0x36
28#define DA9034_VIBRA 0x40
29
30struct da903x_led {
31 struct led_classdev cdev;
Mike Rapoport9e845612008-10-13 09:06:10 +010032 struct device *master;
Mike Rapoport9e845612008-10-13 09:06:10 +010033 int id;
34 int flags;
35};
36
37#define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1)
38#define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1)
39
Andrew Lunn803bb382015-08-20 12:43:49 +020040static int da903x_led_set(struct led_classdev *led_cdev,
41 enum led_brightness value)
Mike Rapoport9e845612008-10-13 09:06:10 +010042{
Andrew Lunn803bb382015-08-20 12:43:49 +020043 struct da903x_led *led =
44 container_of(led_cdev, struct da903x_led, cdev);
Mike Rapoport9e845612008-10-13 09:06:10 +010045 uint8_t val;
Andrew Lunn803bb382015-08-20 12:43:49 +020046 int offset, ret = -EINVAL;
Mike Rapoport9e845612008-10-13 09:06:10 +010047
48 switch (led->id) {
49 case DA9030_ID_LED_1:
50 case DA9030_ID_LED_2:
51 case DA9030_ID_LED_3:
52 case DA9030_ID_LED_4:
53 case DA9030_ID_LED_PC:
54 offset = DA9030_LED_OFFSET(led->id);
55 val = led->flags & ~0x87;
Andrew Lunn803bb382015-08-20 12:43:49 +020056 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,
59 val);
Mike Rapoport9e845612008-10-13 09:06:10 +010060 break;
61 case DA9030_ID_VIBRA:
62 val = led->flags & ~0x80;
Andrew Lunn803bb382015-08-20 12:43:49 +020063 val |= value ? 0x80 : 0; /* EN bit */
64 ret = da903x_write(led->master, DA9030_MISC_CONTROL_A, val);
Mike Rapoport9e845612008-10-13 09:06:10 +010065 break;
66 case DA9034_ID_LED_1:
67 case DA9034_ID_LED_2:
68 offset = DA9034_LED_OFFSET(led->id);
Andrew Lunn803bb382015-08-20 12:43:49 +020069 val = (value * 0x5f / LED_FULL) & 0x7f;
Mike Rapoport9e845612008-10-13 09:06:10 +010070 val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0;
Andrew Lunn803bb382015-08-20 12:43:49 +020071 ret = da903x_write(led->master, DA9034_LED1_CONTROL + offset,
72 val);
Mike Rapoport9e845612008-10-13 09:06:10 +010073 break;
74 case DA9034_ID_VIBRA:
Andrew Lunn803bb382015-08-20 12:43:49 +020075 val = value & 0xfe;
76 ret = da903x_write(led->master, DA9034_VIBRA, val);
Mike Rapoport9e845612008-10-13 09:06:10 +010077 break;
78 }
Mike Rapoport9e845612008-10-13 09:06:10 +010079
Andrew Lunn803bb382015-08-20 12:43:49 +020080 return ret;
Mike Rapoport9e845612008-10-13 09:06:10 +010081}
82
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050083static int da903x_led_probe(struct platform_device *pdev)
Mike Rapoport9e845612008-10-13 09:06:10 +010084{
Jingoo Han87aae1e2013-07-30 01:07:35 -070085 struct led_info *pdata = dev_get_platdata(&pdev->dev);
Mike Rapoport9e845612008-10-13 09:06:10 +010086 struct da903x_led *led;
87 int id, ret;
88
89 if (pdata == NULL)
90 return 0;
91
92 id = pdev->id;
93
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);
97 return -EINVAL;
98 }
99
Bryan Wubad1c892012-07-03 13:07:53 +0800100 led = devm_kzalloc(&pdev->dev, sizeof(struct da903x_led), GFP_KERNEL);
Xiubo Li0c9a03b2014-05-07 20:25:52 -0700101 if (!led)
Mike Rapoport9e845612008-10-13 09:06:10 +0100102 return -ENOMEM;
Mike Rapoport9e845612008-10-13 09:06:10 +0100103
104 led->cdev.name = pdata->name;
105 led->cdev.default_trigger = pdata->default_trigger;
Andrew Lunn803bb382015-08-20 12:43:49 +0200106 led->cdev.brightness_set_blocking = da903x_led_set;
Mike Rapoport9e845612008-10-13 09:06:10 +0100107 led->cdev.brightness = LED_OFF;
108
109 led->id = id;
110 led->flags = pdata->flags;
111 led->master = pdev->dev.parent;
Mike Rapoport9e845612008-10-13 09:06:10 +0100112
Johan Hovold6f4aa352020-06-01 15:39:46 +0200113 ret = led_classdev_register(led->master, &led->cdev);
Mike Rapoport9e845612008-10-13 09:06:10 +0100114 if (ret) {
115 dev_err(&pdev->dev, "failed to register LED %d\n", id);
Bryan Wubad1c892012-07-03 13:07:53 +0800116 return ret;
Mike Rapoport9e845612008-10-13 09:06:10 +0100117 }
118
Johan Hovold6f4aa352020-06-01 15:39:46 +0200119 platform_set_drvdata(pdev, led);
120
121 return 0;
122}
123
124static int da903x_led_remove(struct platform_device *pdev)
125{
126 struct da903x_led *led = platform_get_drvdata(pdev);
127
128 led_classdev_unregister(&led->cdev);
129
Mike Rapoport9e845612008-10-13 09:06:10 +0100130 return 0;
131}
132
133static struct platform_driver da903x_led_driver = {
134 .driver = {
135 .name = "da903x-led",
Mike Rapoport9e845612008-10-13 09:06:10 +0100136 },
137 .probe = da903x_led_probe,
Johan Hovold6f4aa352020-06-01 15:39:46 +0200138 .remove = da903x_led_remove,
Mike Rapoport9e845612008-10-13 09:06:10 +0100139};
140
Axel Lin892a8842012-01-10 15:09:24 -0800141module_platform_driver(da903x_led_driver);
Mike Rapoport9e845612008-10-13 09:06:10 +0100142
143MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034");
Sachin Kamat141bfee2012-11-25 11:16:28 +0530144MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
145MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
Mike Rapoport9e845612008-10-13 09:06:10 +0100146MODULE_LICENSE("GPL");
147MODULE_ALIAS("platform:da903x-led");