1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 Richtek Technology Corp.
5 * Author: ChiaEn Wu <chiaen_wu@richtek.com>
8 #include <linux/backlight.h>
9 #include <linux/bitfield.h>
10 #include <linux/bits.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/kernel.h>
13 #include <linux/minmax.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
20 #define MT6370_REG_DEV_INFO 0x100
21 #define MT6370_REG_BL_EN 0x1A0
22 #define MT6370_REG_BL_BSTCTRL 0x1A1
23 #define MT6370_REG_BL_PWM 0x1A2
24 #define MT6370_REG_BL_DIM2 0x1A4
26 #define MT6370_VENID_MASK GENMASK(7, 4)
27 #define MT6370_BL_EXT_EN_MASK BIT(7)
28 #define MT6370_BL_EN_MASK BIT(6)
29 #define MT6370_BL_CODE_MASK BIT(0)
30 #define MT6370_BL_CH_MASK GENMASK(5, 2)
31 #define MT6370_BL_CH_SHIFT 2
32 #define MT6370_BL_DIM2_COMMON_MASK GENMASK(2, 0)
33 #define MT6370_BL_DIM2_COMMON_SHIFT 3
34 #define MT6370_BL_DIM2_6372_MASK GENMASK(5, 0)
35 #define MT6370_BL_DIM2_6372_SHIFT 6
36 #define MT6370_BL_PWM_EN_MASK BIT(7)
37 #define MT6370_BL_PWM_HYS_EN_MASK BIT(2)
38 #define MT6370_BL_PWM_HYS_SEL_MASK GENMASK(1, 0)
39 #define MT6370_BL_OVP_EN_MASK BIT(7)
40 #define MT6370_BL_OVP_SEL_MASK GENMASK(6, 5)
41 #define MT6370_BL_OVP_SEL_SHIFT 5
42 #define MT6370_BL_OC_EN_MASK BIT(3)
43 #define MT6370_BL_OC_SEL_MASK GENMASK(2, 1)
44 #define MT6370_BL_OC_SEL_SHIFT 1
46 #define MT6370_BL_PWM_HYS_TH_MIN_STEP 1
47 #define MT6370_BL_PWM_HYS_TH_MAX_STEP 64
48 #define MT6370_BL_OVP_MIN_UV 17000000
49 #define MT6370_BL_OVP_MAX_UV 29000000
50 #define MT6370_BL_OVP_STEP_UV 4000000
51 #define MT6370_BL_OCP_MIN_UA 900000
52 #define MT6370_BL_OCP_MAX_UA 1800000
53 #define MT6370_BL_OCP_STEP_UA 300000
54 #define MT6370_BL_MAX_COMMON_BRIGHTNESS 2048
55 #define MT6370_BL_MAX_6372_BRIGHTNESS 16384
56 #define MT6370_BL_MAX_CH 15
59 MT6370_VID_COMMON
= 1,
66 int def_max_brightness
;
67 struct backlight_device
*bl
;
69 struct gpio_desc
*enable_gpio
;
70 struct regmap
*regmap
;
73 static int mt6370_bl_update_status(struct backlight_device
*bl_dev
)
75 struct mt6370_priv
*priv
= bl_get_data(bl_dev
);
76 int brightness
= backlight_get_brightness(bl_dev
);
77 unsigned int enable_val
;
82 brightness_val
[0] = (brightness
- 1) & priv
->dim2_mask
;
83 brightness_val
[1] = (brightness
- 1) >> priv
->dim2_shift
;
85 ret
= regmap_raw_write(priv
->regmap
, MT6370_REG_BL_DIM2
,
86 brightness_val
, sizeof(brightness_val
));
91 gpiod_set_value(priv
->enable_gpio
, !!brightness
);
93 enable_val
= brightness
? MT6370_BL_EN_MASK
: 0;
94 return regmap_update_bits(priv
->regmap
, MT6370_REG_BL_EN
,
95 MT6370_BL_EN_MASK
, enable_val
);
98 static int mt6370_bl_get_brightness(struct backlight_device
*bl_dev
)
100 struct mt6370_priv
*priv
= bl_get_data(bl_dev
);
102 u8 brightness_val
[2];
105 ret
= regmap_read(priv
->regmap
, MT6370_REG_BL_EN
, &enable
);
109 if (!(enable
& MT6370_BL_EN_MASK
))
112 ret
= regmap_raw_read(priv
->regmap
, MT6370_REG_BL_DIM2
,
113 brightness_val
, sizeof(brightness_val
));
117 brightness
= brightness_val
[1] << priv
->dim2_shift
;
118 brightness
+= brightness_val
[0] & priv
->dim2_mask
;
120 return brightness
+ 1;
123 static const struct backlight_ops mt6370_bl_ops
= {
124 .options
= BL_CORE_SUSPENDRESUME
,
125 .update_status
= mt6370_bl_update_status
,
126 .get_brightness
= mt6370_bl_get_brightness
,
129 static int mt6370_init_backlight_properties(struct mt6370_priv
*priv
,
130 struct backlight_properties
*props
)
132 struct device
*dev
= priv
->dev
;
134 u32 brightness
, ovp_uV
, ocp_uA
;
135 unsigned int mask
, val
;
138 /* Vendor optional properties */
140 if (device_property_read_bool(dev
, "mediatek,bled-pwm-enable"))
141 val
|= MT6370_BL_PWM_EN_MASK
;
143 if (device_property_read_bool(dev
, "mediatek,bled-pwm-hys-enable"))
144 val
|= MT6370_BL_PWM_HYS_EN_MASK
;
146 ret
= device_property_read_u8(dev
,
147 "mediatek,bled-pwm-hys-input-th-steps",
150 prop_val
= clamp_val(prop_val
,
151 MT6370_BL_PWM_HYS_TH_MIN_STEP
,
152 MT6370_BL_PWM_HYS_TH_MAX_STEP
);
153 prop_val
= prop_val
<= 1 ? 0 :
155 prop_val
<= 16 ? 2 : 3;
159 ret
= regmap_update_bits(priv
->regmap
, MT6370_REG_BL_PWM
,
165 if (device_property_read_bool(dev
, "mediatek,bled-ovp-shutdown"))
166 val
|= MT6370_BL_OVP_EN_MASK
;
168 ret
= device_property_read_u32(dev
, "mediatek,bled-ovp-microvolt",
171 ovp_uV
= clamp_val(ovp_uV
, MT6370_BL_OVP_MIN_UV
,
172 MT6370_BL_OVP_MAX_UV
);
173 ovp_uV
= DIV_ROUND_UP(ovp_uV
- MT6370_BL_OVP_MIN_UV
,
174 MT6370_BL_OVP_STEP_UV
);
175 val
|= ovp_uV
<< MT6370_BL_OVP_SEL_SHIFT
;
178 if (device_property_read_bool(dev
, "mediatek,bled-ocp-shutdown"))
179 val
|= MT6370_BL_OC_EN_MASK
;
181 ret
= device_property_read_u32(dev
, "mediatek,bled-ocp-microamp",
184 ocp_uA
= clamp_val(ocp_uA
, MT6370_BL_OCP_MIN_UA
,
185 MT6370_BL_OCP_MAX_UA
);
186 ocp_uA
= DIV_ROUND_UP(ocp_uA
- MT6370_BL_OCP_MIN_UA
,
187 MT6370_BL_OCP_STEP_UA
);
188 val
|= ocp_uA
<< MT6370_BL_OC_SEL_SHIFT
;
191 ret
= regmap_update_bits(priv
->regmap
, MT6370_REG_BL_BSTCTRL
,
196 /* Common properties */
197 ret
= device_property_read_u32(dev
, "max-brightness", &brightness
);
199 brightness
= priv
->def_max_brightness
;
201 props
->max_brightness
= min_t(u32
, brightness
, priv
->def_max_brightness
);
203 ret
= device_property_read_u32(dev
, "default-brightness", &brightness
);
205 brightness
= props
->max_brightness
;
207 props
->brightness
= min_t(u32
, brightness
, props
->max_brightness
);
210 if (device_property_read_bool(dev
, "mediatek,bled-exponential-mode-enable")) {
211 val
|= MT6370_BL_CODE_MASK
;
212 props
->scale
= BACKLIGHT_SCALE_NON_LINEAR
;
214 props
->scale
= BACKLIGHT_SCALE_LINEAR
;
216 ret
= device_property_read_u8(dev
, "mediatek,bled-channel-use",
219 dev_err(dev
, "mediatek,bled-channel-use DT property missing\n");
223 if (!prop_val
|| prop_val
> MT6370_BL_MAX_CH
) {
225 "No channel specified or over than upper bound (%d)\n",
230 mask
= MT6370_BL_EXT_EN_MASK
| MT6370_BL_CH_MASK
;
231 val
|= prop_val
<< MT6370_BL_CH_SHIFT
;
233 if (priv
->enable_gpio
)
234 val
|= MT6370_BL_EXT_EN_MASK
;
236 return regmap_update_bits(priv
->regmap
, MT6370_REG_BL_EN
, mask
, val
);
239 static int mt6370_check_vendor_info(struct mt6370_priv
*priv
)
242 * Because MT6372 uses 14 bits to control the brightness,
243 * MT6370 and MT6371 use 11 bits. This function is used
244 * to check the vendor's ID and set the relative hardware
245 * mask, shift and default maximum brightness value that
248 unsigned int dev_info
, hw_vid
, of_vid
;
251 ret
= regmap_read(priv
->regmap
, MT6370_REG_DEV_INFO
, &dev_info
);
255 of_vid
= (uintptr_t)device_get_match_data(priv
->dev
);
256 hw_vid
= FIELD_GET(MT6370_VENID_MASK
, dev_info
);
257 hw_vid
= (hw_vid
== 0x9 || hw_vid
== 0xb) ? MT6370_VID_6372
: MT6370_VID_COMMON
;
258 if (hw_vid
!= of_vid
)
259 return dev_err_probe(priv
->dev
, -EINVAL
,
260 "Buggy DT, wrong compatible string\n");
262 if (hw_vid
== MT6370_VID_6372
) {
263 priv
->dim2_mask
= MT6370_BL_DIM2_6372_MASK
;
264 priv
->dim2_shift
= MT6370_BL_DIM2_6372_SHIFT
;
265 priv
->def_max_brightness
= MT6370_BL_MAX_6372_BRIGHTNESS
;
267 priv
->dim2_mask
= MT6370_BL_DIM2_COMMON_MASK
;
268 priv
->dim2_shift
= MT6370_BL_DIM2_COMMON_SHIFT
;
269 priv
->def_max_brightness
= MT6370_BL_MAX_COMMON_BRIGHTNESS
;
275 static int mt6370_bl_probe(struct platform_device
*pdev
)
277 struct backlight_properties props
= {
278 .type
= BACKLIGHT_RAW
,
280 struct device
*dev
= &pdev
->dev
;
281 struct mt6370_priv
*priv
;
284 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
290 priv
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
292 return dev_err_probe(dev
, -ENODEV
, "Failed to get regmap\n");
294 ret
= mt6370_check_vendor_info(priv
);
296 return dev_err_probe(dev
, ret
, "Failed to check vendor info\n");
298 priv
->enable_gpio
= devm_gpiod_get_optional(dev
, "enable",
300 if (IS_ERR(priv
->enable_gpio
))
301 return dev_err_probe(dev
, PTR_ERR(priv
->enable_gpio
),
302 "Failed to get 'enable' gpio\n");
304 ret
= mt6370_init_backlight_properties(priv
, &props
);
306 return dev_err_probe(dev
, ret
,
307 "Failed to init backlight properties\n");
309 priv
->bl
= devm_backlight_device_register(dev
, pdev
->name
, dev
, priv
,
310 &mt6370_bl_ops
, &props
);
311 if (IS_ERR(priv
->bl
))
312 return dev_err_probe(dev
, PTR_ERR(priv
->bl
),
313 "Failed to register backlight\n");
315 backlight_update_status(priv
->bl
);
316 platform_set_drvdata(pdev
, priv
);
321 static void mt6370_bl_remove(struct platform_device
*pdev
)
323 struct mt6370_priv
*priv
= platform_get_drvdata(pdev
);
324 struct backlight_device
*bl_dev
= priv
->bl
;
326 bl_dev
->props
.brightness
= 0;
327 backlight_update_status(priv
->bl
);
330 static const struct of_device_id mt6370_bl_of_match
[] = {
331 { .compatible
= "mediatek,mt6370-backlight", .data
= (void *)MT6370_VID_COMMON
},
332 { .compatible
= "mediatek,mt6372-backlight", .data
= (void *)MT6370_VID_6372
},
335 MODULE_DEVICE_TABLE(of
, mt6370_bl_of_match
);
337 static struct platform_driver mt6370_bl_driver
= {
339 .name
= "mt6370-backlight",
340 .of_match_table
= mt6370_bl_of_match
,
342 .probe
= mt6370_bl_probe
,
343 .remove
= mt6370_bl_remove
,
345 module_platform_driver(mt6370_bl_driver
);
347 MODULE_AUTHOR("ChiaEn Wu <chiaen_wu@richtek.com>");
348 MODULE_DESCRIPTION("MediaTek MT6370 Backlight Driver");
349 MODULE_LICENSE("GPL v2");