blob: c263a21db8298d057ca1cf5b9b28196d0af8e864 [file] [log] [blame]
Mike Rapoport9e845612008-10-13 09:06:10 +01001/*
2 * LEDs driver for Dialog Semiconductor DA9030/DA9034
3 *
4 * Copyright (C) 2008 Compulab, Ltd.
Sachin Kamat141bfee2012-11-25 11:16:28 +05305 * Mike Rapoport <mike@compulab.co.il>
Mike Rapoport9e845612008-10-13 09:06:10 +01006 *
7 * Copyright (C) 2006-2008 Marvell International Ltd.
Sachin Kamat141bfee2012-11-25 11:16:28 +05308 * Eric Miao <eric.miao@marvell.com>
Mike Rapoport9e845612008-10-13 09:06:10 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/leds.h>
Eric Miaob9b54aa2008-10-27 17:48:50 +080020#include <linux/workqueue.h>
Mike Rapoport9e845612008-10-13 09:06:10 +010021#include <linux/mfd/da903x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mike Rapoport9e845612008-10-13 09:06:10 +010023
24#define DA9030_LED1_CONTROL 0x20
25#define DA9030_LED2_CONTROL 0x21
26#define DA9030_LED3_CONTROL 0x22
27#define DA9030_LED4_CONTROL 0x23
28#define DA9030_LEDPC_CONTROL 0x24
29#define DA9030_MISC_CONTROL_A 0x26 /* Vibrator Control */
30
31#define DA9034_LED1_CONTROL 0x35
32#define DA9034_LED2_CONTROL 0x36
33#define DA9034_VIBRA 0x40
34
35struct da903x_led {
36 struct led_classdev cdev;
37 struct work_struct work;
38 struct device *master;
39 enum led_brightness new_brightness;
40 int id;
41 int flags;
42};
43
44#define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1)
45#define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1)
46
47static void da903x_led_work(struct work_struct *work)
48{
49 struct da903x_led *led = container_of(work, struct da903x_led, work);
50 uint8_t val;
51 int offset;
52
53 switch (led->id) {
54 case DA9030_ID_LED_1:
55 case DA9030_ID_LED_2:
56 case DA9030_ID_LED_3:
57 case DA9030_ID_LED_4:
58 case DA9030_ID_LED_PC:
59 offset = DA9030_LED_OFFSET(led->id);
60 val = led->flags & ~0x87;
61 val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */
Jonathan Camerona0601c82008-10-30 11:05:00 +000062 val |= (0x7 - (led->new_brightness >> 5)) & 0x7; /* PWM<2:0> */
Mike Rapoport9e845612008-10-13 09:06:10 +010063 da903x_write(led->master, DA9030_LED1_CONTROL + offset, val);
64 break;
65 case DA9030_ID_VIBRA:
66 val = led->flags & ~0x80;
67 val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */
68 da903x_write(led->master, DA9030_MISC_CONTROL_A, val);
69 break;
70 case DA9034_ID_LED_1:
71 case DA9034_ID_LED_2:
72 offset = DA9034_LED_OFFSET(led->id);
73 val = (led->new_brightness * 0x5f / LED_FULL) & 0x7f;
74 val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0;
75 da903x_write(led->master, DA9034_LED1_CONTROL + offset, val);
76 break;
77 case DA9034_ID_VIBRA:
78 val = led->new_brightness & 0xfe;
79 da903x_write(led->master, DA9034_VIBRA, val);
80 break;
81 }
82}
83
84static void da903x_led_set(struct led_classdev *led_cdev,
85 enum led_brightness value)
86{
87 struct da903x_led *led;
Sachin Kamat141bfee2012-11-25 11:16:28 +053088
Mike Rapoport9e845612008-10-13 09:06:10 +010089 led = container_of(led_cdev, struct da903x_led, cdev);
90 led->new_brightness = value;
91 schedule_work(&led->work);
92}
93
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050094static int da903x_led_probe(struct platform_device *pdev)
Mike Rapoport9e845612008-10-13 09:06:10 +010095{
96 struct led_info *pdata = pdev->dev.platform_data;
97 struct da903x_led *led;
98 int id, ret;
99
100 if (pdata == NULL)
101 return 0;
102
103 id = pdev->id;
104
105 if (!((id >= DA9030_ID_LED_1 && id <= DA9030_ID_VIBRA) ||
106 (id >= DA9034_ID_LED_1 && id <= DA9034_ID_VIBRA))) {
107 dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", id);
108 return -EINVAL;
109 }
110
Bryan Wubad1c892012-07-03 13:07:53 +0800111 led = devm_kzalloc(&pdev->dev, sizeof(struct da903x_led), GFP_KERNEL);
Mike Rapoport9e845612008-10-13 09:06:10 +0100112 if (led == NULL) {
113 dev_err(&pdev->dev, "failed to alloc memory for LED%d\n", id);
114 return -ENOMEM;
115 }
116
117 led->cdev.name = pdata->name;
118 led->cdev.default_trigger = pdata->default_trigger;
119 led->cdev.brightness_set = da903x_led_set;
120 led->cdev.brightness = LED_OFF;
121
122 led->id = id;
123 led->flags = pdata->flags;
124 led->master = pdev->dev.parent;
125 led->new_brightness = LED_OFF;
126
127 INIT_WORK(&led->work, da903x_led_work);
128
129 ret = led_classdev_register(led->master, &led->cdev);
130 if (ret) {
131 dev_err(&pdev->dev, "failed to register LED %d\n", id);
Bryan Wubad1c892012-07-03 13:07:53 +0800132 return ret;
Mike Rapoport9e845612008-10-13 09:06:10 +0100133 }
134
135 platform_set_drvdata(pdev, led);
136 return 0;
Mike Rapoport9e845612008-10-13 09:06:10 +0100137}
138
Bill Pemberton678e8a62012-11-19 13:26:00 -0500139static int da903x_led_remove(struct platform_device *pdev)
Mike Rapoport9e845612008-10-13 09:06:10 +0100140{
141 struct da903x_led *led = platform_get_drvdata(pdev);
142
143 led_classdev_unregister(&led->cdev);
Mike Rapoport9e845612008-10-13 09:06:10 +0100144 return 0;
145}
146
147static struct platform_driver da903x_led_driver = {
148 .driver = {
149 .name = "da903x-led",
150 .owner = THIS_MODULE,
151 },
152 .probe = da903x_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500153 .remove = da903x_led_remove,
Mike Rapoport9e845612008-10-13 09:06:10 +0100154};
155
Axel Lin892a8842012-01-10 15:09:24 -0800156module_platform_driver(da903x_led_driver);
Mike Rapoport9e845612008-10-13 09:06:10 +0100157
158MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034");
Sachin Kamat141bfee2012-11-25 11:16:28 +0530159MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
160MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
Mike Rapoport9e845612008-10-13 09:06:10 +0100161MODULE_LICENSE("GPL");
162MODULE_ALIAS("platform:da903x-led");