1 /* Copyright (c) 2015, Sony Mobile Communications, AB.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/backlight.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
21 #define PM8941_WLED_DEFAULT_BRIGHTNESS 2048
23 #define PM8941_WLED_REG_VAL_BASE 0x40
24 #define PM8941_WLED_REG_VAL_MAX 0xFFF
26 #define PM8941_WLED_REG_MOD_EN 0x46
27 #define PM8941_WLED_REG_MOD_EN_BIT BIT(7)
28 #define PM8941_WLED_REG_MOD_EN_MASK BIT(7)
30 #define PM8941_WLED_REG_SYNC 0x47
31 #define PM8941_WLED_REG_SYNC_MASK 0x07
32 #define PM8941_WLED_REG_SYNC_LED1 BIT(0)
33 #define PM8941_WLED_REG_SYNC_LED2 BIT(1)
34 #define PM8941_WLED_REG_SYNC_LED3 BIT(2)
35 #define PM8941_WLED_REG_SYNC_ALL 0x07
36 #define PM8941_WLED_REG_SYNC_CLEAR 0x00
38 #define PM8941_WLED_REG_FREQ 0x4c
39 #define PM8941_WLED_REG_FREQ_MASK 0x0f
41 #define PM8941_WLED_REG_OVP 0x4d
42 #define PM8941_WLED_REG_OVP_MASK 0x03
44 #define PM8941_WLED_REG_BOOST 0x4e
45 #define PM8941_WLED_REG_BOOST_MASK 0x07
47 #define PM8941_WLED_REG_SINK 0x4f
48 #define PM8941_WLED_REG_SINK_MASK 0xe0
49 #define PM8941_WLED_REG_SINK_SHFT 0x05
51 /* Per-'string' registers below */
52 #define PM8941_WLED_REG_STR_OFFSET 0x10
54 #define PM8941_WLED_REG_STR_MOD_EN_BASE 0x60
55 #define PM8941_WLED_REG_STR_MOD_MASK BIT(7)
56 #define PM8941_WLED_REG_STR_MOD_EN BIT(7)
58 #define PM8941_WLED_REG_STR_SCALE_BASE 0x62
59 #define PM8941_WLED_REG_STR_SCALE_MASK 0x1f
61 #define PM8941_WLED_REG_STR_MOD_SRC_BASE 0x63
62 #define PM8941_WLED_REG_STR_MOD_SRC_MASK 0x01
63 #define PM8941_WLED_REG_STR_MOD_SRC_INT 0x00
64 #define PM8941_WLED_REG_STR_MOD_SRC_EXT 0x01
66 #define PM8941_WLED_REG_STR_CABC_BASE 0x66
67 #define PM8941_WLED_REG_STR_CABC_MASK BIT(7)
68 #define PM8941_WLED_REG_STR_CABC_EN BIT(7)
70 struct pm8941_wled_config
{
83 struct regmap
*regmap
;
86 struct pm8941_wled_config cfg
;
89 static int pm8941_wled_update_status(struct backlight_device
*bl
)
91 struct pm8941_wled
*wled
= bl_get_data(bl
);
92 u16 val
= bl
->props
.brightness
;
97 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
98 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
99 bl
->props
.state
& BL_CORE_FBBLANK
)
103 ctrl
= PM8941_WLED_REG_MOD_EN_BIT
;
105 rc
= regmap_update_bits(wled
->regmap
,
106 wled
->addr
+ PM8941_WLED_REG_MOD_EN
,
107 PM8941_WLED_REG_MOD_EN_MASK
, ctrl
);
111 for (i
= 0; i
< wled
->cfg
.num_strings
; ++i
) {
112 u8 v
[2] = { val
& 0xff, (val
>> 8) & 0xf };
114 rc
= regmap_bulk_write(wled
->regmap
,
115 wled
->addr
+ PM8941_WLED_REG_VAL_BASE
+ 2 * i
,
121 rc
= regmap_update_bits(wled
->regmap
,
122 wled
->addr
+ PM8941_WLED_REG_SYNC
,
123 PM8941_WLED_REG_SYNC_MASK
, PM8941_WLED_REG_SYNC_ALL
);
127 rc
= regmap_update_bits(wled
->regmap
,
128 wled
->addr
+ PM8941_WLED_REG_SYNC
,
129 PM8941_WLED_REG_SYNC_MASK
, PM8941_WLED_REG_SYNC_CLEAR
);
133 static int pm8941_wled_setup(struct pm8941_wled
*wled
)
138 rc
= regmap_update_bits(wled
->regmap
,
139 wled
->addr
+ PM8941_WLED_REG_OVP
,
140 PM8941_WLED_REG_OVP_MASK
, wled
->cfg
.ovp
);
144 rc
= regmap_update_bits(wled
->regmap
,
145 wled
->addr
+ PM8941_WLED_REG_BOOST
,
146 PM8941_WLED_REG_BOOST_MASK
, wled
->cfg
.i_boost_limit
);
150 rc
= regmap_update_bits(wled
->regmap
,
151 wled
->addr
+ PM8941_WLED_REG_FREQ
,
152 PM8941_WLED_REG_FREQ_MASK
, wled
->cfg
.switch_freq
);
156 if (wled
->cfg
.cs_out_en
) {
157 u8 all
= (BIT(wled
->cfg
.num_strings
) - 1)
158 << PM8941_WLED_REG_SINK_SHFT
;
160 rc
= regmap_update_bits(wled
->regmap
,
161 wled
->addr
+ PM8941_WLED_REG_SINK
,
162 PM8941_WLED_REG_SINK_MASK
, all
);
167 for (i
= 0; i
< wled
->cfg
.num_strings
; ++i
) {
168 u16 addr
= wled
->addr
+ PM8941_WLED_REG_STR_OFFSET
* i
;
170 rc
= regmap_update_bits(wled
->regmap
,
171 addr
+ PM8941_WLED_REG_STR_MOD_EN_BASE
,
172 PM8941_WLED_REG_STR_MOD_MASK
,
173 PM8941_WLED_REG_STR_MOD_EN
);
177 if (wled
->cfg
.ext_gen
) {
178 rc
= regmap_update_bits(wled
->regmap
,
179 addr
+ PM8941_WLED_REG_STR_MOD_SRC_BASE
,
180 PM8941_WLED_REG_STR_MOD_SRC_MASK
,
181 PM8941_WLED_REG_STR_MOD_SRC_EXT
);
186 rc
= regmap_update_bits(wled
->regmap
,
187 addr
+ PM8941_WLED_REG_STR_SCALE_BASE
,
188 PM8941_WLED_REG_STR_SCALE_MASK
,
193 rc
= regmap_update_bits(wled
->regmap
,
194 addr
+ PM8941_WLED_REG_STR_CABC_BASE
,
195 PM8941_WLED_REG_STR_CABC_MASK
,
197 PM8941_WLED_REG_STR_CABC_EN
: 0);
205 static const struct pm8941_wled_config pm8941_wled_config_defaults
= {
216 struct pm8941_wled_var_cfg
{
222 static const u32 pm8941_wled_i_boost_limit_values
[] = {
223 105, 385, 525, 805, 980, 1260, 1400, 1680,
226 static const struct pm8941_wled_var_cfg pm8941_wled_i_boost_limit_cfg
= {
227 .values
= pm8941_wled_i_boost_limit_values
,
228 .size
= ARRAY_SIZE(pm8941_wled_i_boost_limit_values
),
231 static const u32 pm8941_wled_ovp_values
[] = {
235 static const struct pm8941_wled_var_cfg pm8941_wled_ovp_cfg
= {
236 .values
= pm8941_wled_ovp_values
,
237 .size
= ARRAY_SIZE(pm8941_wled_ovp_values
),
240 static u32
pm8941_wled_num_strings_values_fn(u32 idx
)
245 static const struct pm8941_wled_var_cfg pm8941_wled_num_strings_cfg
= {
246 .fn
= pm8941_wled_num_strings_values_fn
,
250 static u32
pm8941_wled_switch_freq_values_fn(u32 idx
)
252 return 19200 / (2 * (1 + idx
));
255 static const struct pm8941_wled_var_cfg pm8941_wled_switch_freq_cfg
= {
256 .fn
= pm8941_wled_switch_freq_values_fn
,
260 static const struct pm8941_wled_var_cfg pm8941_wled_i_limit_cfg
= {
264 static u32
pm8941_wled_values(const struct pm8941_wled_var_cfg
*cfg
, u32 idx
)
266 if (idx
>= cfg
->size
)
271 return cfg
->values
[idx
];
275 static int pm8941_wled_configure(struct pm8941_wled
*wled
, struct device
*dev
)
277 struct pm8941_wled_config
*cfg
= &wled
->cfg
;
287 const struct pm8941_wled_var_cfg
*cfg
;
290 "qcom,current-boost-limit",
292 .cfg
= &pm8941_wled_i_boost_limit_cfg
,
295 "qcom,current-limit",
297 .cfg
= &pm8941_wled_i_limit_cfg
,
302 .cfg
= &pm8941_wled_ovp_cfg
,
305 "qcom,switching-freq",
307 .cfg
= &pm8941_wled_switch_freq_cfg
,
312 .cfg
= &pm8941_wled_num_strings_cfg
,
319 { "qcom,cs-out", &cfg
->cs_out_en
, },
320 { "qcom,ext-gen", &cfg
->ext_gen
, },
321 { "qcom,cabc", &cfg
->cabc_en
, },
324 rc
= of_property_read_u32(dev
->of_node
, "reg", &val
);
325 if (rc
|| val
> 0xffff) {
326 dev_err(dev
, "invalid IO resources\n");
327 return rc
? rc
: -EINVAL
;
331 rc
= of_property_read_string(dev
->of_node
, "label", &wled
->name
);
333 wled
->name
= dev
->of_node
->name
;
335 *cfg
= pm8941_wled_config_defaults
;
336 for (i
= 0; i
< ARRAY_SIZE(u32_opts
); ++i
) {
337 rc
= of_property_read_u32(dev
->of_node
, u32_opts
[i
].name
, &val
);
341 dev_err(dev
, "error reading '%s'\n", u32_opts
[i
].name
);
346 for (j
= 0; c
!= val
; j
++) {
347 c
= pm8941_wled_values(u32_opts
[i
].cfg
, j
);
349 dev_err(dev
, "invalid value for '%s'\n",
355 dev_dbg(dev
, "'%s' = %u\n", u32_opts
[i
].name
, c
);
356 *u32_opts
[i
].val_ptr
= j
;
359 for (i
= 0; i
< ARRAY_SIZE(bool_opts
); ++i
) {
360 if (of_property_read_bool(dev
->of_node
, bool_opts
[i
].name
))
361 *bool_opts
[i
].val_ptr
= true;
364 cfg
->num_strings
= cfg
->num_strings
+ 1;
369 static const struct backlight_ops pm8941_wled_ops
= {
370 .update_status
= pm8941_wled_update_status
,
373 static int pm8941_wled_probe(struct platform_device
*pdev
)
375 struct backlight_properties props
;
376 struct backlight_device
*bl
;
377 struct pm8941_wled
*wled
;
378 struct regmap
*regmap
;
382 regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
384 dev_err(&pdev
->dev
, "Unable to get regmap\n");
388 wled
= devm_kzalloc(&pdev
->dev
, sizeof(*wled
), GFP_KERNEL
);
392 wled
->regmap
= regmap
;
394 rc
= pm8941_wled_configure(wled
, &pdev
->dev
);
398 rc
= pm8941_wled_setup(wled
);
402 val
= PM8941_WLED_DEFAULT_BRIGHTNESS
;
403 of_property_read_u32(pdev
->dev
.of_node
, "default-brightness", &val
);
405 memset(&props
, 0, sizeof(struct backlight_properties
));
406 props
.type
= BACKLIGHT_RAW
;
407 props
.brightness
= val
;
408 props
.max_brightness
= PM8941_WLED_REG_VAL_MAX
;
409 bl
= devm_backlight_device_register(&pdev
->dev
, wled
->name
,
411 &pm8941_wled_ops
, &props
);
412 return PTR_ERR_OR_ZERO(bl
);
415 static const struct of_device_id pm8941_wled_match_table
[] = {
416 { .compatible
= "qcom,pm8941-wled" },
419 MODULE_DEVICE_TABLE(of
, pm8941_wled_match_table
);
421 static struct platform_driver pm8941_wled_driver
= {
422 .probe
= pm8941_wled_probe
,
424 .name
= "pm8941-wled",
425 .of_match_table
= pm8941_wled_match_table
,
429 module_platform_driver(pm8941_wled_driver
);
431 MODULE_DESCRIPTION("pm8941 wled driver");
432 MODULE_LICENSE("GPL v2");