blob: 8179cef0730f1175a09f986cda970d12bd338b82 [file] [log] [blame]
Mike Rapoport923ae482008-10-13 09:03:44 +01001/*
2 * Backlight driver for Dialog Semiconductor DA9030/DA9034
3 *
4 * Copyright (C) 2008 Compulab, Ltd.
Jingoo Han685746d2012-12-17 16:00:15 -08005 * Mike Rapoport <mike@compulab.co.il>
Mike Rapoport923ae482008-10-13 09:03:44 +01006 *
7 * Copyright (C) 2006-2008 Marvell International Ltd.
Jingoo Han685746d2012-12-17 16:00:15 -08008 * Eric Miao <eric.miao@marvell.com>
Mike Rapoport923ae482008-10-13 09:03:44 +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/kernel.h>
16#include <linux/init.h>
17#include <linux/platform_device.h>
18#include <linux/fb.h>
19#include <linux/backlight.h>
20#include <linux/mfd/da903x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Paul Gortmaker355b2002011-07-03 16:17:28 -040022#include <linux/module.h>
Mike Rapoport923ae482008-10-13 09:03:44 +010023
24#define DA9030_WLED_CONTROL 0x25
25#define DA9030_WLED_CP_EN (1 << 6)
26#define DA9030_WLED_TRIM(x) ((x) & 0x7)
27
28#define DA9034_WLED_CONTROL1 0x3C
29#define DA9034_WLED_CONTROL2 0x3D
Haojian Zhuangb3a85492009-11-05 10:27:13 -050030#define DA9034_WLED_ISET(x) ((x) & 0x1f)
Mike Rapoport923ae482008-10-13 09:03:44 +010031
32#define DA9034_WLED_BOOST_EN (1 << 5)
33
34#define DA9030_MAX_BRIGHTNESS 7
35#define DA9034_MAX_BRIGHTNESS 0x7f
36
37struct da903x_backlight_data {
38 struct device *da903x_dev;
39 int id;
40 int current_brightness;
41};
42
43static int da903x_backlight_set(struct backlight_device *bl, int brightness)
44{
45 struct da903x_backlight_data *data = bl_get_data(bl);
46 struct device *dev = data->da903x_dev;
47 uint8_t val;
48 int ret = 0;
49
50 switch (data->id) {
51 case DA9034_ID_WLED:
52 ret = da903x_update(dev, DA9034_WLED_CONTROL1,
53 brightness, 0x7f);
54 if (ret)
55 return ret;
56
57 if (data->current_brightness && brightness == 0)
58 ret = da903x_clr_bits(dev,
59 DA9034_WLED_CONTROL2,
60 DA9034_WLED_BOOST_EN);
61
62 if (data->current_brightness == 0 && brightness)
63 ret = da903x_set_bits(dev,
64 DA9034_WLED_CONTROL2,
65 DA9034_WLED_BOOST_EN);
66 break;
67 case DA9030_ID_WLED:
68 val = DA9030_WLED_TRIM(brightness);
69 val |= brightness ? DA9030_WLED_CP_EN : 0;
70 ret = da903x_write(dev, DA9030_WLED_CONTROL, val);
71 break;
72 }
73
74 if (ret)
75 return ret;
76
77 data->current_brightness = brightness;
78 return 0;
79}
80
81static int da903x_backlight_update_status(struct backlight_device *bl)
82{
83 int brightness = bl->props.brightness;
84
85 if (bl->props.power != FB_BLANK_UNBLANK)
86 brightness = 0;
87
88 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
89 brightness = 0;
90
91 return da903x_backlight_set(bl, brightness);
92}
93
94static int da903x_backlight_get_brightness(struct backlight_device *bl)
95{
96 struct da903x_backlight_data *data = bl_get_data(bl);
97 return data->current_brightness;
98}
99
Emese Revfy9905a432009-12-14 00:58:57 +0100100static const struct backlight_ops da903x_backlight_ops = {
Mike Rapoport923ae482008-10-13 09:03:44 +0100101 .update_status = da903x_backlight_update_status,
102 .get_brightness = da903x_backlight_get_brightness,
103};
104
105static int da903x_backlight_probe(struct platform_device *pdev)
106{
Haojian Zhuangb3a85492009-11-05 10:27:13 -0500107 struct da9034_backlight_pdata *pdata = pdev->dev.platform_data;
Mike Rapoport923ae482008-10-13 09:03:44 +0100108 struct da903x_backlight_data *data;
109 struct backlight_device *bl;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500110 struct backlight_properties props;
Mike Rapoport923ae482008-10-13 09:03:44 +0100111 int max_brightness;
112
Julia Lawallce969222012-03-23 15:02:00 -0700113 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Mike Rapoport923ae482008-10-13 09:03:44 +0100114 if (data == NULL)
115 return -ENOMEM;
116
117 switch (pdev->id) {
118 case DA9030_ID_WLED:
119 max_brightness = DA9030_MAX_BRIGHTNESS;
120 break;
121 case DA9034_ID_WLED:
122 max_brightness = DA9034_MAX_BRIGHTNESS;
123 break;
124 default:
125 dev_err(&pdev->dev, "invalid backlight device ID(%d)\n",
126 pdev->id);
Mike Rapoport923ae482008-10-13 09:03:44 +0100127 return -EINVAL;
128 }
129
130 data->id = pdev->id;
131 data->da903x_dev = pdev->dev.parent;
132 data->current_brightness = 0;
133
Haojian Zhuangb3a85492009-11-05 10:27:13 -0500134 /* adjust the WLED output current */
135 if (pdata)
136 da903x_write(data->da903x_dev, DA9034_WLED_CONTROL2,
137 DA9034_WLED_ISET(pdata->output_current));
138
Corentin Charyf5f4fd42012-05-29 15:07:20 -0700139 memset(&props, 0, sizeof(props));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700140 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500141 props.max_brightness = max_brightness;
142 bl = backlight_device_register(pdev->name, data->da903x_dev, data,
143 &da903x_backlight_ops, &props);
Mike Rapoport923ae482008-10-13 09:03:44 +0100144 if (IS_ERR(bl)) {
145 dev_err(&pdev->dev, "failed to register backlight\n");
Mike Rapoport923ae482008-10-13 09:03:44 +0100146 return PTR_ERR(bl);
147 }
148
Mike Rapoport923ae482008-10-13 09:03:44 +0100149 bl->props.brightness = max_brightness;
150
151 platform_set_drvdata(pdev, bl);
152 backlight_update_status(bl);
153 return 0;
154}
155
156static int da903x_backlight_remove(struct platform_device *pdev)
157{
158 struct backlight_device *bl = platform_get_drvdata(pdev);
Mike Rapoport923ae482008-10-13 09:03:44 +0100159
160 backlight_device_unregister(bl);
Mike Rapoport923ae482008-10-13 09:03:44 +0100161 return 0;
162}
163
164#ifdef CONFIG_PM
Mike Rapoport43ddc002009-07-21 16:57:47 +0300165static int da903x_backlight_suspend(struct device *dev)
Mike Rapoport923ae482008-10-13 09:03:44 +0100166{
Jingoo Hanf0f13cd2012-12-17 16:00:10 -0800167 struct backlight_device *bl = dev_get_drvdata(dev);
168
Mike Rapoport923ae482008-10-13 09:03:44 +0100169 return da903x_backlight_set(bl, 0);
170}
171
Mike Rapoport43ddc002009-07-21 16:57:47 +0300172static int da903x_backlight_resume(struct device *dev)
Mike Rapoport923ae482008-10-13 09:03:44 +0100173{
Jingoo Hanf0f13cd2012-12-17 16:00:10 -0800174 struct backlight_device *bl = dev_get_drvdata(dev);
Mike Rapoport923ae482008-10-13 09:03:44 +0100175
176 backlight_update_status(bl);
177 return 0;
178}
Mike Rapoport43ddc002009-07-21 16:57:47 +0300179
Alexey Dobriyan47145212009-12-14 18:00:08 -0800180static const struct dev_pm_ops da903x_backlight_pm_ops = {
Mike Rapoport43ddc002009-07-21 16:57:47 +0300181 .suspend = da903x_backlight_suspend,
182 .resume = da903x_backlight_resume,
183};
Mike Rapoport923ae482008-10-13 09:03:44 +0100184#endif
185
186static struct platform_driver da903x_backlight_driver = {
187 .driver = {
188 .name = "da903x-backlight",
189 .owner = THIS_MODULE,
Mike Rapoport43ddc002009-07-21 16:57:47 +0300190#ifdef CONFIG_PM
191 .pm = &da903x_backlight_pm_ops,
192#endif
Mike Rapoport923ae482008-10-13 09:03:44 +0100193 },
194 .probe = da903x_backlight_probe,
195 .remove = da903x_backlight_remove,
Mike Rapoport923ae482008-10-13 09:03:44 +0100196};
197
Axel Lin81178e02012-01-10 15:09:11 -0800198module_platform_driver(da903x_backlight_driver);
Mike Rapoport923ae482008-10-13 09:03:44 +0100199
200MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034");
Jingoo Han685746d2012-12-17 16:00:15 -0800201MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
202MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
Mike Rapoport923ae482008-10-13 09:03:44 +0100203MODULE_LICENSE("GPL");
204MODULE_ALIAS("platform:da903x-backlight");