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>
20 #include <linux/module.h>
22 #define MAX_BRIGHTNESS (0xFF)
23 #define MIN_BRIGHTNESS (0)
25 #define CURRENT_BITMASK (0x1F << 1)
27 struct pm860x_backlight_data
{
28 struct pm860x_chip
*chip
;
29 struct i2c_client
*i2c
;
30 int current_brightness
;
36 static inline int wled_a(int port
)
40 ret
= ((port
- PM8606_BACKLIGHT1
) << 1) + 2;
44 static inline int wled_b(int port
)
48 ret
= ((port
- PM8606_BACKLIGHT1
) << 1) + 3;
52 /* WLED2 & WLED3 share the same IDC */
53 static inline int wled_idc(int port
)
58 case PM8606_BACKLIGHT1
:
59 case PM8606_BACKLIGHT2
:
60 ret
= ((port
- PM8606_BACKLIGHT1
) << 1) + 3;
62 case PM8606_BACKLIGHT3
:
64 ret
= ((port
- PM8606_BACKLIGHT2
) << 1) + 3;
70 static int pm860x_backlight_set(struct backlight_device
*bl
, int brightness
)
72 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
73 struct pm860x_chip
*chip
= data
->chip
;
77 if (brightness
> MAX_BRIGHTNESS
)
78 value
= MAX_BRIGHTNESS
;
82 ret
= pm860x_reg_write(data
->i2c
, wled_a(data
->port
), value
);
86 if ((data
->current_brightness
== 0) && brightness
) {
88 ret
= pm860x_set_bits(data
->i2c
, wled_idc(data
->port
),
89 CURRENT_BITMASK
, data
->iset
);
94 ret
= pm860x_set_bits(data
->i2c
, PM8606_PWM
,
95 PM8606_PWM_FREQ_MASK
, data
->pwm
);
99 if (brightness
== MAX_BRIGHTNESS
) {
100 /* set WLED_ON bit as 100% */
101 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
102 PM8606_WLED_ON
, PM8606_WLED_ON
);
105 if (brightness
== MAX_BRIGHTNESS
) {
106 /* set WLED_ON bit as 100% */
107 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
108 PM8606_WLED_ON
, PM8606_WLED_ON
);
110 /* clear WLED_ON bit since it's not 100% */
111 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
118 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
119 data
->current_brightness
= value
;
122 dev_dbg(chip
->dev
, "set brightness %d failure with return "
123 "value:%d\n", value
, ret
);
127 static int pm860x_backlight_update_status(struct backlight_device
*bl
)
129 int brightness
= bl
->props
.brightness
;
131 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
134 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
137 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
140 return pm860x_backlight_set(bl
, brightness
);
143 static int pm860x_backlight_get_brightness(struct backlight_device
*bl
)
145 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
146 struct pm860x_chip
*chip
= data
->chip
;
149 ret
= pm860x_reg_read(data
->i2c
, wled_a(data
->port
));
152 data
->current_brightness
= ret
;
153 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
154 return data
->current_brightness
;
159 static const struct backlight_ops pm860x_backlight_ops
= {
160 .options
= BL_CORE_SUSPENDRESUME
,
161 .update_status
= pm860x_backlight_update_status
,
162 .get_brightness
= pm860x_backlight_get_brightness
,
165 static int pm860x_backlight_probe(struct platform_device
*pdev
)
167 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
168 struct pm860x_backlight_pdata
*pdata
= NULL
;
169 struct pm860x_backlight_data
*data
;
170 struct backlight_device
*bl
;
171 struct resource
*res
;
172 struct backlight_properties props
;
174 char name
[MFD_NAME_SIZE
];
177 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
179 dev_err(&pdev
->dev
, "No I/O resource!\n");
183 pdata
= pdev
->dev
.platform_data
;
185 dev_err(&pdev
->dev
, "platform data isn't assigned to "
190 data
= kzalloc(sizeof(struct pm860x_backlight_data
), GFP_KERNEL
);
193 strncpy(name
, res
->name
, MFD_NAME_SIZE
);
195 data
->i2c
= (chip
->id
== CHIP_PM8606
) ? chip
->client \
197 data
->current_brightness
= MAX_BRIGHTNESS
;
198 data
->pwm
= pdata
->pwm
;
199 data
->iset
= pdata
->iset
;
200 data
->port
= pdata
->flags
;
201 if (data
->port
< 0) {
202 dev_err(&pdev
->dev
, "wrong platform data is assigned");
207 memset(&props
, 0, sizeof(struct backlight_properties
));
208 props
.type
= BACKLIGHT_RAW
;
209 props
.max_brightness
= MAX_BRIGHTNESS
;
210 bl
= backlight_device_register(name
, &pdev
->dev
, data
,
211 &pm860x_backlight_ops
, &props
);
213 dev_err(&pdev
->dev
, "failed to register backlight\n");
217 bl
->props
.brightness
= MAX_BRIGHTNESS
;
219 platform_set_drvdata(pdev
, bl
);
221 /* Enable reference VSYS */
222 ret
= pm860x_reg_read(data
->i2c
, PM8606_VSYS
);
225 if ((ret
& PM8606_VSYS_EN
) == 0) {
226 value
= ret
| PM8606_VSYS_EN
;
227 ret
= pm860x_reg_write(data
->i2c
, PM8606_VSYS
, value
);
231 /* Enable reference OSC */
232 ret
= pm860x_reg_read(data
->i2c
, PM8606_MISC
);
235 if ((ret
& PM8606_MISC_OSC_EN
) == 0) {
236 value
= ret
| PM8606_MISC_OSC_EN
;
237 ret
= pm860x_reg_write(data
->i2c
, PM8606_MISC
, value
);
241 /* read current backlight */
242 ret
= pm860x_backlight_get_brightness(bl
);
246 backlight_update_status(bl
);
249 backlight_device_unregister(bl
);
254 static int pm860x_backlight_remove(struct platform_device
*pdev
)
256 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
257 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
259 backlight_device_unregister(bl
);
264 static struct platform_driver pm860x_backlight_driver
= {
266 .name
= "88pm860x-backlight",
267 .owner
= THIS_MODULE
,
269 .probe
= pm860x_backlight_probe
,
270 .remove
= pm860x_backlight_remove
,
273 static int __init
pm860x_backlight_init(void)
275 return platform_driver_register(&pm860x_backlight_driver
);
277 module_init(pm860x_backlight_init
);
279 static void __exit
pm860x_backlight_exit(void)
281 platform_driver_unregister(&pm860x_backlight_driver
);
283 module_exit(pm860x_backlight_exit
);
285 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
286 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
287 MODULE_LICENSE("GPL");
288 MODULE_ALIAS("platform:88pm860x-backlight");