viafb: use proper pci config API
[linux/fpc-iii.git] / drivers / video / backlight / da903x_bl.c
blob686e4a789238832f80fda14b2e4af8218f095640
1 /*
2 * Backlight driver for Dialog Semiconductor DA9030/DA9034
4 * Copyright (C) 2008 Compulab, Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
7 * Copyright (C) 2006-2008 Marvell International Ltd.
8 * Eric Miao <eric.miao@marvell.com>
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.
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>
22 #define DA9030_WLED_CONTROL 0x25
23 #define DA9030_WLED_CP_EN (1 << 6)
24 #define DA9030_WLED_TRIM(x) ((x) & 0x7)
26 #define DA9034_WLED_CONTROL1 0x3C
27 #define DA9034_WLED_CONTROL2 0x3D
28 #define DA9034_WLED_ISET(x) ((x) & 0x1f)
30 #define DA9034_WLED_BOOST_EN (1 << 5)
32 #define DA9030_MAX_BRIGHTNESS 7
33 #define DA9034_MAX_BRIGHTNESS 0x7f
35 struct da903x_backlight_data {
36 struct device *da903x_dev;
37 int id;
38 int current_brightness;
41 static int da903x_backlight_set(struct backlight_device *bl, int brightness)
43 struct da903x_backlight_data *data = bl_get_data(bl);
44 struct device *dev = data->da903x_dev;
45 uint8_t val;
46 int ret = 0;
48 switch (data->id) {
49 case DA9034_ID_WLED:
50 ret = da903x_update(dev, DA9034_WLED_CONTROL1,
51 brightness, 0x7f);
52 if (ret)
53 return ret;
55 if (data->current_brightness && brightness == 0)
56 ret = da903x_clr_bits(dev,
57 DA9034_WLED_CONTROL2,
58 DA9034_WLED_BOOST_EN);
60 if (data->current_brightness == 0 && brightness)
61 ret = da903x_set_bits(dev,
62 DA9034_WLED_CONTROL2,
63 DA9034_WLED_BOOST_EN);
64 break;
65 case DA9030_ID_WLED:
66 val = DA9030_WLED_TRIM(brightness);
67 val |= brightness ? DA9030_WLED_CP_EN : 0;
68 ret = da903x_write(dev, DA9030_WLED_CONTROL, val);
69 break;
72 if (ret)
73 return ret;
75 data->current_brightness = brightness;
76 return 0;
79 static int da903x_backlight_update_status(struct backlight_device *bl)
81 int brightness = bl->props.brightness;
83 if (bl->props.power != FB_BLANK_UNBLANK)
84 brightness = 0;
86 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
87 brightness = 0;
89 return da903x_backlight_set(bl, brightness);
92 static int da903x_backlight_get_brightness(struct backlight_device *bl)
94 struct da903x_backlight_data *data = bl_get_data(bl);
95 return data->current_brightness;
98 static const struct backlight_ops da903x_backlight_ops = {
99 .update_status = da903x_backlight_update_status,
100 .get_brightness = da903x_backlight_get_brightness,
103 static int da903x_backlight_probe(struct platform_device *pdev)
105 struct da9034_backlight_pdata *pdata = pdev->dev.platform_data;
106 struct da903x_backlight_data *data;
107 struct backlight_device *bl;
108 struct backlight_properties props;
109 int max_brightness;
111 data = kzalloc(sizeof(*data), GFP_KERNEL);
112 if (data == NULL)
113 return -ENOMEM;
115 switch (pdev->id) {
116 case DA9030_ID_WLED:
117 max_brightness = DA9030_MAX_BRIGHTNESS;
118 break;
119 case DA9034_ID_WLED:
120 max_brightness = DA9034_MAX_BRIGHTNESS;
121 break;
122 default:
123 dev_err(&pdev->dev, "invalid backlight device ID(%d)\n",
124 pdev->id);
125 kfree(data);
126 return -EINVAL;
129 data->id = pdev->id;
130 data->da903x_dev = pdev->dev.parent;
131 data->current_brightness = 0;
133 /* adjust the WLED output current */
134 if (pdata)
135 da903x_write(data->da903x_dev, DA9034_WLED_CONTROL2,
136 DA9034_WLED_ISET(pdata->output_current));
138 props.max_brightness = max_brightness;
139 bl = backlight_device_register(pdev->name, data->da903x_dev, data,
140 &da903x_backlight_ops, &props);
141 if (IS_ERR(bl)) {
142 dev_err(&pdev->dev, "failed to register backlight\n");
143 kfree(data);
144 return PTR_ERR(bl);
147 bl->props.brightness = max_brightness;
149 platform_set_drvdata(pdev, bl);
150 backlight_update_status(bl);
151 return 0;
154 static int da903x_backlight_remove(struct platform_device *pdev)
156 struct backlight_device *bl = platform_get_drvdata(pdev);
157 struct da903x_backlight_data *data = bl_get_data(bl);
159 backlight_device_unregister(bl);
160 kfree(data);
161 return 0;
164 #ifdef CONFIG_PM
165 static int da903x_backlight_suspend(struct device *dev)
167 struct platform_device *pdev = to_platform_device(dev);
168 struct backlight_device *bl = platform_get_drvdata(pdev);
169 return da903x_backlight_set(bl, 0);
172 static int da903x_backlight_resume(struct device *dev)
174 struct platform_device *pdev = to_platform_device(dev);
175 struct backlight_device *bl = platform_get_drvdata(pdev);
177 backlight_update_status(bl);
178 return 0;
181 static const struct dev_pm_ops da903x_backlight_pm_ops = {
182 .suspend = da903x_backlight_suspend,
183 .resume = da903x_backlight_resume,
185 #endif
187 static struct platform_driver da903x_backlight_driver = {
188 .driver = {
189 .name = "da903x-backlight",
190 .owner = THIS_MODULE,
191 #ifdef CONFIG_PM
192 .pm = &da903x_backlight_pm_ops,
193 #endif
195 .probe = da903x_backlight_probe,
196 .remove = da903x_backlight_remove,
199 static int __init da903x_backlight_init(void)
201 return platform_driver_register(&da903x_backlight_driver);
203 module_init(da903x_backlight_init);
205 static void __exit da903x_backlight_exit(void)
207 platform_driver_unregister(&da903x_backlight_driver);
209 module_exit(da903x_backlight_exit);
211 MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034");
212 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
213 "Mike Rapoport <mike@compulab.co.il>");
214 MODULE_LICENSE("GPL");
215 MODULE_ALIAS("platform:da903x-backlight");