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 backlight_power_set(struct pm860x_chip
*chip
, int port
,
76 case PM8606_BACKLIGHT1
:
77 ret
= on
? pm8606_osc_enable(chip
, WLED1_DUTY
) :
78 pm8606_osc_disable(chip
, WLED1_DUTY
);
80 case PM8606_BACKLIGHT2
:
81 ret
= on
? pm8606_osc_enable(chip
, WLED2_DUTY
) :
82 pm8606_osc_disable(chip
, WLED2_DUTY
);
84 case PM8606_BACKLIGHT3
:
85 ret
= on
? pm8606_osc_enable(chip
, WLED3_DUTY
) :
86 pm8606_osc_disable(chip
, WLED3_DUTY
);
92 static int pm860x_backlight_set(struct backlight_device
*bl
, int brightness
)
94 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
95 struct pm860x_chip
*chip
= data
->chip
;
99 if (brightness
> MAX_BRIGHTNESS
)
100 value
= MAX_BRIGHTNESS
;
105 backlight_power_set(chip
, data
->port
, 1);
107 ret
= pm860x_reg_write(data
->i2c
, wled_a(data
->port
), value
);
111 if ((data
->current_brightness
== 0) && brightness
) {
113 ret
= pm860x_set_bits(data
->i2c
, wled_idc(data
->port
),
114 CURRENT_BITMASK
, data
->iset
);
119 ret
= pm860x_set_bits(data
->i2c
, PM8606_PWM
,
120 PM8606_PWM_FREQ_MASK
, data
->pwm
);
124 if (brightness
== MAX_BRIGHTNESS
) {
125 /* set WLED_ON bit as 100% */
126 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
127 PM8606_WLED_ON
, PM8606_WLED_ON
);
130 if (brightness
== MAX_BRIGHTNESS
) {
131 /* set WLED_ON bit as 100% */
132 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
133 PM8606_WLED_ON
, PM8606_WLED_ON
);
135 /* clear WLED_ON bit since it's not 100% */
136 ret
= pm860x_set_bits(data
->i2c
, wled_b(data
->port
),
144 backlight_power_set(chip
, data
->port
, 0);
146 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
147 data
->current_brightness
= value
;
150 dev_dbg(chip
->dev
, "set brightness %d failure with return "
151 "value:%d\n", value
, ret
);
155 static int pm860x_backlight_update_status(struct backlight_device
*bl
)
157 int brightness
= bl
->props
.brightness
;
159 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
162 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
165 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
168 return pm860x_backlight_set(bl
, brightness
);
171 static int pm860x_backlight_get_brightness(struct backlight_device
*bl
)
173 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
174 struct pm860x_chip
*chip
= data
->chip
;
177 ret
= pm860x_reg_read(data
->i2c
, wled_a(data
->port
));
180 data
->current_brightness
= ret
;
181 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
182 return data
->current_brightness
;
187 static const struct backlight_ops pm860x_backlight_ops
= {
188 .options
= BL_CORE_SUSPENDRESUME
,
189 .update_status
= pm860x_backlight_update_status
,
190 .get_brightness
= pm860x_backlight_get_brightness
,
193 static int pm860x_backlight_probe(struct platform_device
*pdev
)
195 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
196 struct pm860x_backlight_pdata
*pdata
= NULL
;
197 struct pm860x_backlight_data
*data
;
198 struct backlight_device
*bl
;
199 struct resource
*res
;
200 struct backlight_properties props
;
201 char name
[MFD_NAME_SIZE
];
204 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
206 dev_err(&pdev
->dev
, "No I/O resource!\n");
210 pdata
= pdev
->dev
.platform_data
;
212 dev_err(&pdev
->dev
, "platform data isn't assigned to "
217 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct pm860x_backlight_data
),
221 strncpy(name
, res
->name
, MFD_NAME_SIZE
);
223 data
->i2c
= (chip
->id
== CHIP_PM8606
) ? chip
->client \
225 data
->current_brightness
= MAX_BRIGHTNESS
;
226 data
->pwm
= pdata
->pwm
;
227 data
->iset
= pdata
->iset
;
228 data
->port
= pdata
->flags
;
229 if (data
->port
< 0) {
230 dev_err(&pdev
->dev
, "wrong platform data is assigned");
234 memset(&props
, 0, sizeof(struct backlight_properties
));
235 props
.type
= BACKLIGHT_RAW
;
236 props
.max_brightness
= MAX_BRIGHTNESS
;
237 bl
= backlight_device_register(name
, &pdev
->dev
, data
,
238 &pm860x_backlight_ops
, &props
);
240 dev_err(&pdev
->dev
, "failed to register backlight\n");
243 bl
->props
.brightness
= MAX_BRIGHTNESS
;
245 platform_set_drvdata(pdev
, bl
);
247 /* read current backlight */
248 ret
= pm860x_backlight_get_brightness(bl
);
252 backlight_update_status(bl
);
255 backlight_device_unregister(bl
);
259 static int pm860x_backlight_remove(struct platform_device
*pdev
)
261 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
263 backlight_device_unregister(bl
);
267 static struct platform_driver pm860x_backlight_driver
= {
269 .name
= "88pm860x-backlight",
270 .owner
= THIS_MODULE
,
272 .probe
= pm860x_backlight_probe
,
273 .remove
= pm860x_backlight_remove
,
276 module_platform_driver(pm860x_backlight_driver
);
278 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
279 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
280 MODULE_LICENSE("GPL");
281 MODULE_ALIAS("platform:88pm860x-backlight");