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>
15 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/backlight.h>
19 #include <linux/mfd/88pm860x.h>
21 #define MAX_BRIGHTNESS (0xFF)
22 #define MIN_BRIGHTNESS (0)
24 #define CURRENT_BITMASK (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_BITMASK
, 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 const 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 pm860x_backlight_probe(struct platform_device
*pdev
)
166 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
167 struct pm860x_backlight_pdata
*pdata
= NULL
;
168 struct pm860x_backlight_data
*data
;
169 struct backlight_device
*bl
;
170 struct resource
*res
;
171 struct backlight_properties props
;
173 char name
[MFD_NAME_SIZE
];
176 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
178 dev_err(&pdev
->dev
, "No I/O resource!\n");
182 pdata
= pdev
->dev
.platform_data
;
184 dev_err(&pdev
->dev
, "platform data isn't assigned to "
189 data
= kzalloc(sizeof(struct pm860x_backlight_data
), GFP_KERNEL
);
192 strncpy(name
, res
->name
, MFD_NAME_SIZE
);
194 data
->i2c
= (chip
->id
== CHIP_PM8606
) ? chip
->client \
196 data
->current_brightness
= MAX_BRIGHTNESS
;
197 data
->pwm
= pdata
->pwm
;
198 data
->iset
= pdata
->iset
;
199 data
->port
= pdata
->flags
;
200 if (data
->port
< 0) {
201 dev_err(&pdev
->dev
, "wrong platform data is assigned");
206 memset(&props
, 0, sizeof(struct backlight_properties
));
207 props
.type
= BACKLIGHT_RAW
;
208 props
.max_brightness
= MAX_BRIGHTNESS
;
209 bl
= backlight_device_register(name
, &pdev
->dev
, data
,
210 &pm860x_backlight_ops
, &props
);
212 dev_err(&pdev
->dev
, "failed to register backlight\n");
216 bl
->props
.brightness
= MAX_BRIGHTNESS
;
218 platform_set_drvdata(pdev
, bl
);
220 /* Enable reference VSYS */
221 ret
= pm860x_reg_read(data
->i2c
, PM8606_VSYS
);
224 if ((ret
& PM8606_VSYS_EN
) == 0) {
225 value
= ret
| PM8606_VSYS_EN
;
226 ret
= pm860x_reg_write(data
->i2c
, PM8606_VSYS
, value
);
230 /* Enable reference OSC */
231 ret
= pm860x_reg_read(data
->i2c
, PM8606_MISC
);
234 if ((ret
& PM8606_MISC_OSC_EN
) == 0) {
235 value
= ret
| PM8606_MISC_OSC_EN
;
236 ret
= pm860x_reg_write(data
->i2c
, PM8606_MISC
, value
);
240 /* read current backlight */
241 ret
= pm860x_backlight_get_brightness(bl
);
245 backlight_update_status(bl
);
248 backlight_device_unregister(bl
);
253 static int pm860x_backlight_remove(struct platform_device
*pdev
)
255 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
256 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
258 backlight_device_unregister(bl
);
263 static struct platform_driver pm860x_backlight_driver
= {
265 .name
= "88pm860x-backlight",
266 .owner
= THIS_MODULE
,
268 .probe
= pm860x_backlight_probe
,
269 .remove
= pm860x_backlight_remove
,
272 static int __init
pm860x_backlight_init(void)
274 return platform_driver_register(&pm860x_backlight_driver
);
276 module_init(pm860x_backlight_init
);
278 static void __exit
pm860x_backlight_exit(void)
280 platform_driver_unregister(&pm860x_backlight_driver
);
282 module_exit(pm860x_backlight_exit
);
284 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
285 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
286 MODULE_LICENSE("GPL");
287 MODULE_ALIAS("platform:88pm860x-backlight");