1 // SPDX-License-Identifier: GPL-2.0-only
5 * TPS65217 backlight driver
7 * Copyright (C) 2012 Matthias Kaehlcke
8 * Author: Matthias Kaehlcke <matthias@kaehlcke.net>
11 #include <linux/kernel.h>
12 #include <linux/backlight.h>
13 #include <linux/err.h>
15 #include <linux/mfd/tps65217.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
23 struct backlight_device
*bl
;
27 static int tps65217_bl_enable(struct tps65217_bl
*tps65217_bl
)
31 rc
= tps65217_set_bits(tps65217_bl
->tps
, TPS65217_REG_WLEDCTRL1
,
32 TPS65217_WLEDCTRL1_ISINK_ENABLE
,
33 TPS65217_WLEDCTRL1_ISINK_ENABLE
, TPS65217_PROTECT_NONE
);
35 dev_err(tps65217_bl
->dev
,
36 "failed to enable backlight: %d\n", rc
);
40 tps65217_bl
->is_enabled
= true;
42 dev_dbg(tps65217_bl
->dev
, "backlight enabled\n");
47 static int tps65217_bl_disable(struct tps65217_bl
*tps65217_bl
)
51 rc
= tps65217_clear_bits(tps65217_bl
->tps
,
52 TPS65217_REG_WLEDCTRL1
,
53 TPS65217_WLEDCTRL1_ISINK_ENABLE
,
54 TPS65217_PROTECT_NONE
);
56 dev_err(tps65217_bl
->dev
,
57 "failed to disable backlight: %d\n", rc
);
61 tps65217_bl
->is_enabled
= false;
63 dev_dbg(tps65217_bl
->dev
, "backlight disabled\n");
68 static int tps65217_bl_update_status(struct backlight_device
*bl
)
70 struct tps65217_bl
*tps65217_bl
= bl_get_data(bl
);
72 int brightness
= backlight_get_brightness(bl
);
75 rc
= tps65217_reg_write(tps65217_bl
->tps
,
76 TPS65217_REG_WLEDCTRL2
,
78 TPS65217_PROTECT_NONE
);
80 dev_err(tps65217_bl
->dev
,
81 "failed to set brightness level: %d\n", rc
);
85 dev_dbg(tps65217_bl
->dev
, "brightness set to %d\n", brightness
);
87 if (!tps65217_bl
->is_enabled
)
88 rc
= tps65217_bl_enable(tps65217_bl
);
90 rc
= tps65217_bl_disable(tps65217_bl
);
96 static const struct backlight_ops tps65217_bl_ops
= {
97 .options
= BL_CORE_SUSPENDRESUME
,
98 .update_status
= tps65217_bl_update_status
,
101 static int tps65217_bl_hw_init(struct tps65217_bl
*tps65217_bl
,
102 struct tps65217_bl_pdata
*pdata
)
106 rc
= tps65217_bl_disable(tps65217_bl
);
110 switch (pdata
->isel
) {
111 case TPS65217_BL_ISET1
:
112 /* select ISET_1 current level */
113 rc
= tps65217_clear_bits(tps65217_bl
->tps
,
114 TPS65217_REG_WLEDCTRL1
,
115 TPS65217_WLEDCTRL1_ISEL
,
116 TPS65217_PROTECT_NONE
);
118 dev_err(tps65217_bl
->dev
,
119 "failed to select ISET1 current level: %d)\n",
124 dev_dbg(tps65217_bl
->dev
, "selected ISET1 current level\n");
128 case TPS65217_BL_ISET2
:
129 /* select ISET2 current level */
130 rc
= tps65217_set_bits(tps65217_bl
->tps
, TPS65217_REG_WLEDCTRL1
,
131 TPS65217_WLEDCTRL1_ISEL
,
132 TPS65217_WLEDCTRL1_ISEL
, TPS65217_PROTECT_NONE
);
134 dev_err(tps65217_bl
->dev
,
135 "failed to select ISET2 current level: %d\n",
140 dev_dbg(tps65217_bl
->dev
, "selected ISET2 current level\n");
145 dev_err(tps65217_bl
->dev
,
146 "invalid value for current level: %d\n", pdata
->isel
);
150 /* set PWM frequency */
151 rc
= tps65217_set_bits(tps65217_bl
->tps
,
152 TPS65217_REG_WLEDCTRL1
,
153 TPS65217_WLEDCTRL1_FDIM_MASK
,
155 TPS65217_PROTECT_NONE
);
157 dev_err(tps65217_bl
->dev
,
158 "failed to select PWM dimming frequency: %d\n",
167 static struct tps65217_bl_pdata
*
168 tps65217_bl_parse_dt(struct platform_device
*pdev
)
170 struct tps65217
*tps
= dev_get_drvdata(pdev
->dev
.parent
);
171 struct device_node
*node
;
172 struct tps65217_bl_pdata
*pdata
, *err
;
175 node
= of_get_child_by_name(tps
->dev
->of_node
, "backlight");
177 return ERR_PTR(-ENODEV
);
179 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
181 err
= ERR_PTR(-ENOMEM
);
185 pdata
->isel
= TPS65217_BL_ISET1
;
186 if (!of_property_read_u32(node
, "isel", &val
)) {
187 if (val
< TPS65217_BL_ISET1
||
188 val
> TPS65217_BL_ISET2
) {
190 "invalid 'isel' value in the device tree\n");
191 err
= ERR_PTR(-EINVAL
);
198 pdata
->fdim
= TPS65217_BL_FDIM_200HZ
;
199 if (!of_property_read_u32(node
, "fdim", &val
)) {
202 pdata
->fdim
= TPS65217_BL_FDIM_100HZ
;
206 pdata
->fdim
= TPS65217_BL_FDIM_200HZ
;
210 pdata
->fdim
= TPS65217_BL_FDIM_500HZ
;
214 pdata
->fdim
= TPS65217_BL_FDIM_1000HZ
;
219 "invalid 'fdim' value in the device tree\n");
220 err
= ERR_PTR(-EINVAL
);
225 if (!of_property_read_u32(node
, "default-brightness", &val
)) {
228 "invalid 'default-brightness' value in the device tree\n");
229 err
= ERR_PTR(-EINVAL
);
233 pdata
->dft_brightness
= val
;
246 static struct tps65217_bl_pdata
*
247 tps65217_bl_parse_dt(struct platform_device
*pdev
)
253 static int tps65217_bl_probe(struct platform_device
*pdev
)
256 struct tps65217
*tps
= dev_get_drvdata(pdev
->dev
.parent
);
257 struct tps65217_bl
*tps65217_bl
;
258 struct tps65217_bl_pdata
*pdata
;
259 struct backlight_properties bl_props
;
261 pdata
= tps65217_bl_parse_dt(pdev
);
263 return PTR_ERR(pdata
);
265 tps65217_bl
= devm_kzalloc(&pdev
->dev
, sizeof(*tps65217_bl
),
267 if (tps65217_bl
== NULL
)
270 tps65217_bl
->tps
= tps
;
271 tps65217_bl
->dev
= &pdev
->dev
;
272 tps65217_bl
->is_enabled
= false;
274 rc
= tps65217_bl_hw_init(tps65217_bl
, pdata
);
278 memset(&bl_props
, 0, sizeof(struct backlight_properties
));
279 bl_props
.type
= BACKLIGHT_RAW
;
280 bl_props
.max_brightness
= 100;
282 tps65217_bl
->bl
= devm_backlight_device_register(&pdev
->dev
, pdev
->name
,
283 tps65217_bl
->dev
, tps65217_bl
,
284 &tps65217_bl_ops
, &bl_props
);
285 if (IS_ERR(tps65217_bl
->bl
)) {
286 dev_err(tps65217_bl
->dev
,
287 "registration of backlight device failed: %d\n", rc
);
288 return PTR_ERR(tps65217_bl
->bl
);
291 tps65217_bl
->bl
->props
.brightness
= pdata
->dft_brightness
;
292 backlight_update_status(tps65217_bl
->bl
);
293 platform_set_drvdata(pdev
, tps65217_bl
);
299 static const struct of_device_id tps65217_bl_of_match
[] = {
300 { .compatible
= "ti,tps65217-bl", },
303 MODULE_DEVICE_TABLE(of
, tps65217_bl_of_match
);
306 static struct platform_driver tps65217_bl_driver
= {
307 .probe
= tps65217_bl_probe
,
309 .name
= "tps65217-bl",
310 .of_match_table
= of_match_ptr(tps65217_bl_of_match
),
314 module_platform_driver(tps65217_bl_driver
);
316 MODULE_DESCRIPTION("TPS65217 Backlight driver");
317 MODULE_LICENSE("GPL v2");
318 MODULE_AUTHOR("Matthias Kaehlcke <matthias@kaehlcke.net>");