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/leds.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
20 #define PM8941_WLED_REG_VAL_BASE 0x40
21 #define PM8941_WLED_REG_VAL_MAX 0xFFF
23 #define PM8941_WLED_REG_MOD_EN 0x46
24 #define PM8941_WLED_REG_MOD_EN_BIT BIT(7)
25 #define PM8941_WLED_REG_MOD_EN_MASK BIT(7)
27 #define PM8941_WLED_REG_SYNC 0x47
28 #define PM8941_WLED_REG_SYNC_MASK 0x07
29 #define PM8941_WLED_REG_SYNC_LED1 BIT(0)
30 #define PM8941_WLED_REG_SYNC_LED2 BIT(1)
31 #define PM8941_WLED_REG_SYNC_LED3 BIT(2)
32 #define PM8941_WLED_REG_SYNC_ALL 0x07
33 #define PM8941_WLED_REG_SYNC_CLEAR 0x00
35 #define PM8941_WLED_REG_FREQ 0x4c
36 #define PM8941_WLED_REG_FREQ_MASK 0x0f
38 #define PM8941_WLED_REG_OVP 0x4d
39 #define PM8941_WLED_REG_OVP_MASK 0x03
41 #define PM8941_WLED_REG_BOOST 0x4e
42 #define PM8941_WLED_REG_BOOST_MASK 0x07
44 #define PM8941_WLED_REG_SINK 0x4f
45 #define PM8941_WLED_REG_SINK_MASK 0xe0
46 #define PM8941_WLED_REG_SINK_SHFT 0x05
48 /* Per-'string' registers below */
49 #define PM8941_WLED_REG_STR_OFFSET 0x10
51 #define PM8941_WLED_REG_STR_MOD_EN_BASE 0x60
52 #define PM8941_WLED_REG_STR_MOD_MASK BIT(7)
53 #define PM8941_WLED_REG_STR_MOD_EN BIT(7)
55 #define PM8941_WLED_REG_STR_SCALE_BASE 0x62
56 #define PM8941_WLED_REG_STR_SCALE_MASK 0x1f
58 #define PM8941_WLED_REG_STR_MOD_SRC_BASE 0x63
59 #define PM8941_WLED_REG_STR_MOD_SRC_MASK 0x01
60 #define PM8941_WLED_REG_STR_MOD_SRC_INT 0x00
61 #define PM8941_WLED_REG_STR_MOD_SRC_EXT 0x01
63 #define PM8941_WLED_REG_STR_CABC_BASE 0x66
64 #define PM8941_WLED_REG_STR_CABC_MASK BIT(7)
65 #define PM8941_WLED_REG_STR_CABC_EN BIT(7)
67 struct pm8941_wled_config
{
79 struct regmap
*regmap
;
82 struct led_classdev cdev
;
84 struct pm8941_wled_config cfg
;
87 static int pm8941_wled_set(struct led_classdev
*cdev
,
88 enum led_brightness value
)
90 struct pm8941_wled
*wled
;
96 wled
= container_of(cdev
, struct pm8941_wled
, cdev
);
99 ctrl
= PM8941_WLED_REG_MOD_EN_BIT
;
101 val
= value
* PM8941_WLED_REG_VAL_MAX
/ LED_FULL
;
103 rc
= regmap_update_bits(wled
->regmap
,
104 wled
->addr
+ PM8941_WLED_REG_MOD_EN
,
105 PM8941_WLED_REG_MOD_EN_MASK
, ctrl
);
109 for (i
= 0; i
< wled
->cfg
.num_strings
; ++i
) {
110 u8 v
[2] = { val
& 0xff, (val
>> 8) & 0xf };
112 rc
= regmap_bulk_write(wled
->regmap
,
113 wled
->addr
+ PM8941_WLED_REG_VAL_BASE
+ 2 * i
,
119 rc
= regmap_update_bits(wled
->regmap
,
120 wled
->addr
+ PM8941_WLED_REG_SYNC
,
121 PM8941_WLED_REG_SYNC_MASK
, PM8941_WLED_REG_SYNC_ALL
);
125 rc
= regmap_update_bits(wled
->regmap
,
126 wled
->addr
+ PM8941_WLED_REG_SYNC
,
127 PM8941_WLED_REG_SYNC_MASK
, PM8941_WLED_REG_SYNC_CLEAR
);
131 static void pm8941_wled_set_brightness(struct led_classdev
*cdev
,
132 enum led_brightness value
)
134 if (pm8941_wled_set(cdev
, value
)) {
135 dev_err(cdev
->dev
, "Unable to set brightness\n");
138 cdev
->brightness
= value
;
141 static int pm8941_wled_setup(struct pm8941_wled
*wled
)
146 rc
= regmap_update_bits(wled
->regmap
,
147 wled
->addr
+ PM8941_WLED_REG_OVP
,
148 PM8941_WLED_REG_OVP_MASK
, wled
->cfg
.ovp
);
152 rc
= regmap_update_bits(wled
->regmap
,
153 wled
->addr
+ PM8941_WLED_REG_BOOST
,
154 PM8941_WLED_REG_BOOST_MASK
, wled
->cfg
.i_boost_limit
);
158 rc
= regmap_update_bits(wled
->regmap
,
159 wled
->addr
+ PM8941_WLED_REG_FREQ
,
160 PM8941_WLED_REG_FREQ_MASK
, wled
->cfg
.switch_freq
);
164 if (wled
->cfg
.cs_out_en
) {
165 u8 all
= (BIT(wled
->cfg
.num_strings
) - 1)
166 << PM8941_WLED_REG_SINK_SHFT
;
168 rc
= regmap_update_bits(wled
->regmap
,
169 wled
->addr
+ PM8941_WLED_REG_SINK
,
170 PM8941_WLED_REG_SINK_MASK
, all
);
175 for (i
= 0; i
< wled
->cfg
.num_strings
; ++i
) {
176 u16 addr
= wled
->addr
+ PM8941_WLED_REG_STR_OFFSET
* i
;
178 rc
= regmap_update_bits(wled
->regmap
,
179 addr
+ PM8941_WLED_REG_STR_MOD_EN_BASE
,
180 PM8941_WLED_REG_STR_MOD_MASK
,
181 PM8941_WLED_REG_STR_MOD_EN
);
185 if (wled
->cfg
.ext_gen
) {
186 rc
= regmap_update_bits(wled
->regmap
,
187 addr
+ PM8941_WLED_REG_STR_MOD_SRC_BASE
,
188 PM8941_WLED_REG_STR_MOD_SRC_MASK
,
189 PM8941_WLED_REG_STR_MOD_SRC_EXT
);
194 rc
= regmap_update_bits(wled
->regmap
,
195 addr
+ PM8941_WLED_REG_STR_SCALE_BASE
,
196 PM8941_WLED_REG_STR_SCALE_MASK
,
201 rc
= regmap_update_bits(wled
->regmap
,
202 addr
+ PM8941_WLED_REG_STR_CABC_BASE
,
203 PM8941_WLED_REG_STR_CABC_MASK
,
205 PM8941_WLED_REG_STR_CABC_EN
: 0);
213 static const struct pm8941_wled_config pm8941_wled_config_defaults
= {
224 struct pm8941_wled_var_cfg
{
230 static const u32 pm8941_wled_i_boost_limit_values
[] = {
231 105, 385, 525, 805, 980, 1260, 1400, 1680,
234 static const struct pm8941_wled_var_cfg pm8941_wled_i_boost_limit_cfg
= {
235 .values
= pm8941_wled_i_boost_limit_values
,
236 .size
= ARRAY_SIZE(pm8941_wled_i_boost_limit_values
),
239 static const u32 pm8941_wled_ovp_values
[] = {
243 static const struct pm8941_wled_var_cfg pm8941_wled_ovp_cfg
= {
244 .values
= pm8941_wled_ovp_values
,
245 .size
= ARRAY_SIZE(pm8941_wled_ovp_values
),
248 static u32
pm8941_wled_num_strings_values_fn(u32 idx
)
253 static const struct pm8941_wled_var_cfg pm8941_wled_num_strings_cfg
= {
254 .fn
= pm8941_wled_num_strings_values_fn
,
258 static u32
pm8941_wled_switch_freq_values_fn(u32 idx
)
260 return 19200 / (2 * (1 + idx
));
263 static const struct pm8941_wled_var_cfg pm8941_wled_switch_freq_cfg
= {
264 .fn
= pm8941_wled_switch_freq_values_fn
,
268 static const struct pm8941_wled_var_cfg pm8941_wled_i_limit_cfg
= {
272 static u32
pm8941_wled_values(const struct pm8941_wled_var_cfg
*cfg
, u32 idx
)
274 if (idx
>= cfg
->size
)
279 return cfg
->values
[idx
];
283 static int pm8941_wled_configure(struct pm8941_wled
*wled
, struct device
*dev
)
285 struct pm8941_wled_config
*cfg
= &wled
->cfg
;
295 const struct pm8941_wled_var_cfg
*cfg
;
298 "qcom,current-boost-limit",
300 .cfg
= &pm8941_wled_i_boost_limit_cfg
,
303 "qcom,current-limit",
305 .cfg
= &pm8941_wled_i_limit_cfg
,
310 .cfg
= &pm8941_wled_ovp_cfg
,
313 "qcom,switching-freq",
315 .cfg
= &pm8941_wled_switch_freq_cfg
,
320 .cfg
= &pm8941_wled_num_strings_cfg
,
327 { "qcom,cs-out", &cfg
->cs_out_en
, },
328 { "qcom,ext-gen", &cfg
->ext_gen
, },
329 { "qcom,cabc", &cfg
->cabc_en
, },
332 rc
= of_property_read_u32(dev
->of_node
, "reg", &val
);
333 if (rc
|| val
> 0xffff) {
334 dev_err(dev
, "invalid IO resources\n");
335 return rc
? rc
: -EINVAL
;
339 rc
= of_property_read_string(dev
->of_node
, "label", &wled
->cdev
.name
);
341 wled
->cdev
.name
= dev
->of_node
->name
;
343 wled
->cdev
.default_trigger
= of_get_property(dev
->of_node
,
344 "linux,default-trigger", NULL
);
346 *cfg
= pm8941_wled_config_defaults
;
347 for (i
= 0; i
< ARRAY_SIZE(u32_opts
); ++i
) {
348 rc
= of_property_read_u32(dev
->of_node
, u32_opts
[i
].name
, &val
);
352 dev_err(dev
, "error reading '%s'\n", u32_opts
[i
].name
);
357 for (j
= 0; c
!= val
; j
++) {
358 c
= pm8941_wled_values(u32_opts
[i
].cfg
, j
);
360 dev_err(dev
, "invalid value for '%s'\n",
366 dev_dbg(dev
, "'%s' = %u\n", u32_opts
[i
].name
, c
);
367 *u32_opts
[i
].val_ptr
= j
;
370 for (i
= 0; i
< ARRAY_SIZE(bool_opts
); ++i
) {
371 if (of_property_read_bool(dev
->of_node
, bool_opts
[i
].name
))
372 *bool_opts
[i
].val_ptr
= true;
375 cfg
->num_strings
= cfg
->num_strings
+ 1;
380 static int pm8941_wled_probe(struct platform_device
*pdev
)
382 struct pm8941_wled
*wled
;
383 struct regmap
*regmap
;
386 regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
388 dev_err(&pdev
->dev
, "Unable to get regmap\n");
392 wled
= devm_kzalloc(&pdev
->dev
, sizeof(*wled
), GFP_KERNEL
);
396 wled
->regmap
= regmap
;
398 rc
= pm8941_wled_configure(wled
, &pdev
->dev
);
402 rc
= pm8941_wled_setup(wled
);
406 wled
->cdev
.brightness_set
= pm8941_wled_set_brightness
;
408 rc
= devm_led_classdev_register(&pdev
->dev
, &wled
->cdev
);
412 platform_set_drvdata(pdev
, wled
);
417 static const struct of_device_id pm8941_wled_match_table
[] = {
418 { .compatible
= "qcom,pm8941-wled" },
421 MODULE_DEVICE_TABLE(of
, pm8941_wled_match_table
);
423 static struct platform_driver pm8941_wled_driver
= {
424 .probe
= pm8941_wled_probe
,
426 .name
= "pm8941-wled",
427 .of_match_table
= pm8941_wled_match_table
,
431 module_platform_driver(pm8941_wled_driver
);
433 MODULE_DESCRIPTION("pm8941 wled driver");
434 MODULE_LICENSE("GPL v2");
435 MODULE_ALIAS("platform:pm8941-wled");