1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight driver for Marvell Semiconductor 88PM8606
5 * Copyright (C) 2009 Marvell International Ltd.
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/backlight.h>
17 #include <linux/mfd/88pm860x.h>
18 #include <linux/module.h>
20 #define MAX_BRIGHTNESS (0xFF)
21 #define MIN_BRIGHTNESS (0)
23 #define CURRENT_BITMASK (0x1F << 1)
25 struct pm860x_backlight_data
{
26 struct pm860x_chip
*chip
;
27 struct i2c_client
*i2c
;
28 int current_brightness
;
37 static int backlight_power_set(struct pm860x_chip
*chip
, int port
,
44 ret
= on
? pm8606_osc_enable(chip
, WLED1_DUTY
) :
45 pm8606_osc_disable(chip
, WLED1_DUTY
);
48 ret
= on
? pm8606_osc_enable(chip
, WLED2_DUTY
) :
49 pm8606_osc_disable(chip
, WLED2_DUTY
);
52 ret
= on
? pm8606_osc_enable(chip
, WLED3_DUTY
) :
53 pm8606_osc_disable(chip
, WLED3_DUTY
);
59 static int pm860x_backlight_set(struct backlight_device
*bl
, int brightness
)
61 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
62 struct pm860x_chip
*chip
= data
->chip
;
66 if (brightness
> MAX_BRIGHTNESS
)
67 value
= MAX_BRIGHTNESS
;
72 backlight_power_set(chip
, data
->port
, 1);
74 ret
= pm860x_reg_write(data
->i2c
, data
->reg_duty_cycle
, value
);
78 if ((data
->current_brightness
== 0) && brightness
) {
80 ret
= pm860x_set_bits(data
->i2c
, data
->reg_current
,
81 CURRENT_BITMASK
, data
->iset
);
86 ret
= pm860x_set_bits(data
->i2c
, PM8606_PWM
,
87 PM8606_PWM_FREQ_MASK
, data
->pwm
);
91 if (brightness
== MAX_BRIGHTNESS
) {
92 /* set WLED_ON bit as 100% */
93 ret
= pm860x_set_bits(data
->i2c
, data
->reg_always_on
,
94 PM8606_WLED_ON
, PM8606_WLED_ON
);
97 if (brightness
== MAX_BRIGHTNESS
) {
98 /* set WLED_ON bit as 100% */
99 ret
= pm860x_set_bits(data
->i2c
, data
->reg_always_on
,
100 PM8606_WLED_ON
, PM8606_WLED_ON
);
102 /* clear WLED_ON bit since it's not 100% */
103 ret
= pm860x_set_bits(data
->i2c
, data
->reg_always_on
,
111 backlight_power_set(chip
, data
->port
, 0);
113 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
114 data
->current_brightness
= value
;
117 dev_dbg(chip
->dev
, "set brightness %d failure with return value: %d\n",
122 static int pm860x_backlight_update_status(struct backlight_device
*bl
)
124 return pm860x_backlight_set(bl
, backlight_get_brightness(bl
));
127 static int pm860x_backlight_get_brightness(struct backlight_device
*bl
)
129 struct pm860x_backlight_data
*data
= bl_get_data(bl
);
130 struct pm860x_chip
*chip
= data
->chip
;
133 ret
= pm860x_reg_read(data
->i2c
, data
->reg_duty_cycle
);
136 data
->current_brightness
= ret
;
137 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
138 return data
->current_brightness
;
143 static const struct backlight_ops pm860x_backlight_ops
= {
144 .options
= BL_CORE_SUSPENDRESUME
,
145 .update_status
= pm860x_backlight_update_status
,
146 .get_brightness
= pm860x_backlight_get_brightness
,
150 static int pm860x_backlight_dt_init(struct platform_device
*pdev
,
151 struct pm860x_backlight_data
*data
,
154 struct device_node
*nproot
, *np
;
157 nproot
= of_get_child_by_name(pdev
->dev
.parent
->of_node
, "backlights");
159 dev_err(&pdev
->dev
, "failed to find backlights node\n");
162 for_each_child_of_node(nproot
, np
) {
163 if (of_node_name_eq(np
, name
)) {
164 of_property_read_u32(np
, "marvell,88pm860x-iset",
166 data
->iset
= PM8606_WLED_CURRENT(iset
);
167 of_property_read_u32(np
, "marvell,88pm860x-pwm",
177 #define pm860x_backlight_dt_init(x, y, z) (-1)
180 static int pm860x_backlight_probe(struct platform_device
*pdev
)
182 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
183 struct pm860x_backlight_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
184 struct pm860x_backlight_data
*data
;
185 struct backlight_device
*bl
;
186 struct resource
*res
;
187 struct backlight_properties props
;
188 char name
[MFD_NAME_SIZE
];
191 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct pm860x_backlight_data
),
195 res
= platform_get_resource_byname(pdev
, IORESOURCE_REG
, "duty cycle");
197 dev_err(&pdev
->dev
, "No REG resource for duty cycle\n");
200 data
->reg_duty_cycle
= res
->start
;
201 res
= platform_get_resource_byname(pdev
, IORESOURCE_REG
, "always on");
203 dev_err(&pdev
->dev
, "No REG resource for always on\n");
206 data
->reg_always_on
= res
->start
;
207 res
= platform_get_resource_byname(pdev
, IORESOURCE_REG
, "current");
209 dev_err(&pdev
->dev
, "No REG resource for current\n");
212 data
->reg_current
= res
->start
;
214 memset(name
, 0, MFD_NAME_SIZE
);
215 sprintf(name
, "backlight-%d", pdev
->id
);
216 data
->port
= pdev
->id
;
218 data
->i2c
= (chip
->id
== CHIP_PM8606
) ? chip
->client
: chip
->companion
;
219 data
->current_brightness
= MAX_BRIGHTNESS
;
220 if (pm860x_backlight_dt_init(pdev
, data
, name
)) {
222 data
->pwm
= pdata
->pwm
;
223 data
->iset
= pdata
->iset
;
227 memset(&props
, 0, sizeof(struct backlight_properties
));
228 props
.type
= BACKLIGHT_RAW
;
229 props
.max_brightness
= MAX_BRIGHTNESS
;
230 bl
= devm_backlight_device_register(&pdev
->dev
, name
, &pdev
->dev
, data
,
231 &pm860x_backlight_ops
, &props
);
233 dev_err(&pdev
->dev
, "failed to register backlight\n");
236 bl
->props
.brightness
= MAX_BRIGHTNESS
;
238 platform_set_drvdata(pdev
, bl
);
240 /* read current backlight */
241 ret
= pm860x_backlight_get_brightness(bl
);
245 backlight_update_status(bl
);
249 static struct platform_driver pm860x_backlight_driver
= {
251 .name
= "88pm860x-backlight",
253 .probe
= pm860x_backlight_probe
,
256 module_platform_driver(pm860x_backlight_driver
);
258 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
259 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
260 MODULE_LICENSE("GPL");
261 MODULE_ALIAS("platform:88pm860x-backlight");