2 * Backlight driver for Marvell Semiconductor 88PM8606
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
16 #include <linux/i2c.h>
17 #include <linux/backlight.h>
18 #include <linux/mfd/88pm860x.h>
19 #include <linux/slab.h>
21 #define MAX_BRIGHTNESS (0xFF)
22 #define MIN_BRIGHTNESS (0)
24 #define CURRENT_MASK (0x1F << 1)
26 struct pm860x_backlight_data
{
27 struct pm860x_chip
*chip
;
28 struct i2c_client
*i2c
;
29 int current_brightness
;
35 static inline int wled_a(int port
)
39 ret
= ((port
- PM8606_BACKLIGHT1
) << 1) + 2;
43 static inline int wled_b(int port
)
47 ret
= ((port
- PM8606_BACKLIGHT1
) << 1) + 3;
51 /* WLED2 & WLED3 share the same IDC */
52 static inline int wled_idc(int port
)
57 case PM8606_BACKLIGHT1
:
58 case PM8606_BACKLIGHT2
:
59 ret
= ((port
- PM8606_BACKLIGHT1
) << 1) + 3;
61 case PM8606_BACKLIGHT3
:
63 ret
= ((port
- PM8606_BACKLIGHT2
) << 1) + 3;
69 static int pm860x_backlight_set(struct backlight_device
*bl
, int brightness
)
71 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
72 struct pm860x_chip
*chip
= data
->chip
;
76 if (brightness
> MAX_BRIGHTNESS
)
77 value
= MAX_BRIGHTNESS
;
81 ret
= pm860x_reg_write(data
->i2c
, wled_a(data
->port
), value
);
85 if ((data
->current_brightness
== 0) && brightness
) {
87 ret
= pm860x_set_bits(data
->i2c
, wled_idc(data
->port
),
88 CURRENT_MASK
, data
->iset
);
93 ret
= pm860x_set_bits(data
->i2c
, PM8606_PWM
,
94 PM8606_PWM_FREQ_MASK
, data
->pwm
);
98 if (brightness
== MAX_BRIGHTNESS
) {
99 /* set WLED_ON bit as 100% */
100 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
101 PM8606_WLED_ON
, PM8606_WLED_ON
);
104 if (brightness
== MAX_BRIGHTNESS
) {
105 /* set WLED_ON bit as 100% */
106 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
107 PM8606_WLED_ON
, PM8606_WLED_ON
);
109 /* clear WLED_ON bit since it's not 100% */
110 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
117 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
118 data
->current_brightness
= value
;
121 dev_dbg(chip
->dev
, "set brightness %d failure with return "
122 "value:%d\n", value
, ret
);
126 static int pm860x_backlight_update_status(struct backlight_device
*bl
)
128 int brightness
= bl
->props
.brightness
;
130 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
133 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
136 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
139 return pm860x_backlight_set(bl
, brightness
);
142 static int pm860x_backlight_get_brightness(struct backlight_device
*bl
)
144 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
145 struct pm860x_chip
*chip
= data
->chip
;
148 ret
= pm860x_reg_read(data
->i2c
, wled_a(data
->port
));
151 data
->current_brightness
= ret
;
152 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
153 return data
->current_brightness
;
158 static struct backlight_ops pm860x_backlight_ops
= {
159 .options
= BL_CORE_SUSPENDRESUME
,
160 .update_status
= pm860x_backlight_update_status
,
161 .get_brightness
= pm860x_backlight_get_brightness
,
164 static int __check_device(struct pm860x_backlight_pdata
*pdata
, char *name
)
166 struct pm860x_backlight_pdata
*p
= pdata
;
170 if ((p
->id
!= PM8606_ID_BACKLIGHT
) || (p
->flags
< 0))
173 if (!strncmp(name
, pm860x_backlight_name
[p
->flags
],
183 static int pm860x_backlight_probe(struct platform_device
*pdev
)
185 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
186 struct pm860x_platform_data
*pm860x_pdata
;
187 struct pm860x_backlight_pdata
*pdata
= NULL
;
188 struct pm860x_backlight_data
*data
;
189 struct backlight_device
*bl
;
190 struct resource
*res
;
191 struct backlight_properties props
;
193 char name
[MFD_NAME_SIZE
];
196 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
198 dev_err(&pdev
->dev
, "No I/O resource!\n");
202 if (pdev
->dev
.parent
->platform_data
) {
203 pm860x_pdata
= pdev
->dev
.parent
->platform_data
;
204 pdata
= pm860x_pdata
->backlight
;
207 dev_err(&pdev
->dev
, "platform data isn't assigned to "
212 data
= kzalloc(sizeof(struct pm860x_backlight_data
), GFP_KERNEL
);
215 strncpy(name
, res
->name
, MFD_NAME_SIZE
);
217 data
->i2c
= (chip
->id
== CHIP_PM8606
) ? chip
->client \
219 data
->current_brightness
= MAX_BRIGHTNESS
;
220 data
->pwm
= pdata
->pwm
;
221 data
->iset
= pdata
->iset
;
222 data
->port
= __check_device(pdata
, name
);
223 if (data
->port
< 0) {
224 dev_err(&pdev
->dev
, "wrong platform data is assigned");
229 memset(&props
, 0, sizeof(struct backlight_properties
));
230 props
.max_brightness
= MAX_BRIGHTNESS
;
231 bl
= backlight_device_register(name
, &pdev
->dev
, data
,
232 &pm860x_backlight_ops
, &props
);
234 dev_err(&pdev
->dev
, "failed to register backlight\n");
238 bl
->props
.brightness
= MAX_BRIGHTNESS
;
240 platform_set_drvdata(pdev
, bl
);
242 /* Enable reference VSYS */
243 ret
= pm860x_reg_read(data
->i2c
, PM8606_VSYS
);
246 if ((ret
& PM8606_VSYS_EN
) == 0) {
247 value
= ret
| PM8606_VSYS_EN
;
248 ret
= pm860x_reg_write(data
->i2c
, PM8606_VSYS
, value
);
252 /* Enable reference OSC */
253 ret
= pm860x_reg_read(data
->i2c
, PM8606_MISC
);
256 if ((ret
& PM8606_MISC_OSC_EN
) == 0) {
257 value
= ret
| PM8606_MISC_OSC_EN
;
258 ret
= pm860x_reg_write(data
->i2c
, PM8606_MISC
, value
);
262 /* read current backlight */
263 ret
= pm860x_backlight_get_brightness(bl
);
267 backlight_update_status(bl
);
274 static int pm860x_backlight_remove(struct platform_device
*pdev
)
276 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
277 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
279 backlight_device_unregister(bl
);
284 static struct platform_driver pm860x_backlight_driver
= {
286 .name
= "88pm860x-backlight",
287 .owner
= THIS_MODULE
,
289 .probe
= pm860x_backlight_probe
,
290 .remove
= pm860x_backlight_remove
,
293 static int __init
pm860x_backlight_init(void)
295 return platform_driver_register(&pm860x_backlight_driver
);
297 module_init(pm860x_backlight_init
);
299 static void __exit
pm860x_backlight_exit(void)
301 platform_driver_unregister(&pm860x_backlight_driver
);
303 module_exit(pm860x_backlight_exit
);
305 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
306 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
307 MODULE_LICENSE("GPL");
308 MODULE_ALIAS("platform:88pm860x-backlight");