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>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/backlight.h>
20 #include <linux/mfd/88pm860x.h>
21 #include <linux/module.h>
23 #define MAX_BRIGHTNESS (0xFF)
24 #define MIN_BRIGHTNESS (0)
26 #define CURRENT_BITMASK (0x1F << 1)
28 struct pm860x_backlight_data
{
29 struct pm860x_chip
*chip
;
30 struct i2c_client
*i2c
;
31 int current_brightness
;
40 static int backlight_power_set(struct pm860x_chip
*chip
, int port
,
47 ret
= on
? pm8606_osc_enable(chip
, WLED1_DUTY
) :
48 pm8606_osc_disable(chip
, WLED1_DUTY
);
51 ret
= on
? pm8606_osc_enable(chip
, WLED2_DUTY
) :
52 pm8606_osc_disable(chip
, WLED2_DUTY
);
55 ret
= on
? pm8606_osc_enable(chip
, WLED3_DUTY
) :
56 pm8606_osc_disable(chip
, WLED3_DUTY
);
62 static int pm860x_backlight_set(struct backlight_device
*bl
, int brightness
)
64 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
65 struct pm860x_chip
*chip
= data
->chip
;
69 if (brightness
> MAX_BRIGHTNESS
)
70 value
= MAX_BRIGHTNESS
;
75 backlight_power_set(chip
, data
->port
, 1);
77 ret
= pm860x_reg_write(data
->i2c
, data
->reg_duty_cycle
, value
);
81 if ((data
->current_brightness
== 0) && brightness
) {
83 ret
= pm860x_set_bits(data
->i2c
, data
->reg_current
,
84 CURRENT_BITMASK
, data
->iset
);
89 ret
= pm860x_set_bits(data
->i2c
, PM8606_PWM
,
90 PM8606_PWM_FREQ_MASK
, data
->pwm
);
94 if (brightness
== MAX_BRIGHTNESS
) {
95 /* set WLED_ON bit as 100% */
96 ret
= pm860x_set_bits(data
->i2c
, data
->reg_always_on
,
97 PM8606_WLED_ON
, PM8606_WLED_ON
);
100 if (brightness
== MAX_BRIGHTNESS
) {
101 /* set WLED_ON bit as 100% */
102 ret
= pm860x_set_bits(data
->i2c
, data
->reg_always_on
,
103 PM8606_WLED_ON
, PM8606_WLED_ON
);
105 /* clear WLED_ON bit since it's not 100% */
106 ret
= pm860x_set_bits(data
->i2c
, data
->reg_always_on
,
114 backlight_power_set(chip
, data
->port
, 0);
116 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
117 data
->current_brightness
= value
;
120 dev_dbg(chip
->dev
, "set brightness %d failure with return value: %d\n",
125 static int pm860x_backlight_update_status(struct backlight_device
*bl
)
127 int brightness
= bl
->props
.brightness
;
129 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
132 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
135 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
138 return pm860x_backlight_set(bl
, brightness
);
141 static int pm860x_backlight_get_brightness(struct backlight_device
*bl
)
143 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
144 struct pm860x_chip
*chip
= data
->chip
;
147 ret
= pm860x_reg_read(data
->i2c
, data
->reg_duty_cycle
);
150 data
->current_brightness
= ret
;
151 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
152 return data
->current_brightness
;
157 static const struct backlight_ops pm860x_backlight_ops
= {
158 .options
= BL_CORE_SUSPENDRESUME
,
159 .update_status
= pm860x_backlight_update_status
,
160 .get_brightness
= pm860x_backlight_get_brightness
,
164 static int pm860x_backlight_dt_init(struct platform_device
*pdev
,
165 struct pm860x_backlight_data
*data
,
168 struct device_node
*nproot
, *np
;
171 nproot
= of_node_get(pdev
->dev
.parent
->of_node
);
174 nproot
= of_find_node_by_name(nproot
, "backlights");
176 dev_err(&pdev
->dev
, "failed to find backlights node\n");
179 for_each_child_of_node(nproot
, np
) {
180 if (!of_node_cmp(np
->name
, name
)) {
181 of_property_read_u32(np
, "marvell,88pm860x-iset",
183 data
->iset
= PM8606_WLED_CURRENT(iset
);
184 of_property_read_u32(np
, "marvell,88pm860x-pwm",
193 #define pm860x_backlight_dt_init(x, y, z) (-1)
196 static int pm860x_backlight_probe(struct platform_device
*pdev
)
198 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
199 struct pm860x_backlight_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
200 struct pm860x_backlight_data
*data
;
201 struct backlight_device
*bl
;
202 struct resource
*res
;
203 struct backlight_properties props
;
204 char name
[MFD_NAME_SIZE
];
207 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct pm860x_backlight_data
),
211 res
= platform_get_resource_byname(pdev
, IORESOURCE_REG
, "duty cycle");
213 dev_err(&pdev
->dev
, "No REG resource for duty cycle\n");
216 data
->reg_duty_cycle
= res
->start
;
217 res
= platform_get_resource_byname(pdev
, IORESOURCE_REG
, "always on");
219 dev_err(&pdev
->dev
, "No REG resorce for always on\n");
222 data
->reg_always_on
= res
->start
;
223 res
= platform_get_resource_byname(pdev
, IORESOURCE_REG
, "current");
225 dev_err(&pdev
->dev
, "No REG resource for current\n");
228 data
->reg_current
= res
->start
;
230 memset(name
, 0, MFD_NAME_SIZE
);
231 sprintf(name
, "backlight-%d", pdev
->id
);
232 data
->port
= pdev
->id
;
234 data
->i2c
= (chip
->id
== CHIP_PM8606
) ? chip
->client
: chip
->companion
;
235 data
->current_brightness
= MAX_BRIGHTNESS
;
236 if (pm860x_backlight_dt_init(pdev
, data
, name
)) {
238 data
->pwm
= pdata
->pwm
;
239 data
->iset
= pdata
->iset
;
243 memset(&props
, 0, sizeof(struct backlight_properties
));
244 props
.type
= BACKLIGHT_RAW
;
245 props
.max_brightness
= MAX_BRIGHTNESS
;
246 bl
= devm_backlight_device_register(&pdev
->dev
, name
, &pdev
->dev
, data
,
247 &pm860x_backlight_ops
, &props
);
249 dev_err(&pdev
->dev
, "failed to register backlight\n");
252 bl
->props
.brightness
= MAX_BRIGHTNESS
;
254 platform_set_drvdata(pdev
, bl
);
256 /* read current backlight */
257 ret
= pm860x_backlight_get_brightness(bl
);
261 backlight_update_status(bl
);
265 static struct platform_driver pm860x_backlight_driver
= {
267 .name
= "88pm860x-backlight",
268 .owner
= THIS_MODULE
,
270 .probe
= pm860x_backlight_probe
,
273 module_platform_driver(pm860x_backlight_driver
);
275 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
276 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
277 MODULE_LICENSE("GPL");
278 MODULE_ALIAS("platform:88pm860x-backlight");