Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'backlight-next-6.1' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/backlight

Pull backlight update from Lee Jones:

- Add support for MediaTek MT6370 Backlight

* tag 'backlight-next-6.1' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/backlight:
video: backlight: mt6370: Add MediaTek MT6370 support
dt-bindings: backlight: Add MediaTek MT6370 backlight

+486
+121
Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: MediaTek MT6370 Backlight 8 + 9 + maintainers: 10 + - ChiaEn Wu <chiaen_wu@richtek.com> 11 + 12 + description: | 13 + This module is part of the MT6370 MFD device. 14 + The MT6370 Backlight WLED driver supports up to a 29V output voltage for 15 + 4 channels of 8 series WLEDs. Each channel supports up to 30mA of current 16 + capability with 2048 current steps (11 bits, only for MT6370/MT6371) or 17 + 16384 current steps (14 bits, only for MT6372) in exponential or linear 18 + mapping curves. 19 + 20 + allOf: 21 + - $ref: common.yaml# 22 + 23 + properties: 24 + compatible: 25 + enum: 26 + - mediatek,mt6370-backlight 27 + - mediatek,mt6372-backlight 28 + 29 + default-brightness: 30 + minimum: 0 31 + 32 + max-brightness: 33 + minimum: 0 34 + 35 + enable-gpios: 36 + description: External backlight 'enable' pin 37 + maxItems: 1 38 + 39 + mediatek,bled-pwm-enable: 40 + description: | 41 + Enable external PWM input for backlight dimming 42 + type: boolean 43 + 44 + mediatek,bled-pwm-hys-enable: 45 + description: | 46 + Enable the backlight input-hysteresis for PWM mode 47 + type: boolean 48 + 49 + mediatek,bled-pwm-hys-input-th-steps: 50 + $ref: /schemas/types.yaml#/definitions/uint8 51 + enum: [1, 4, 16, 64] 52 + description: | 53 + The selection of the upper and lower bounds threshold of backlight 54 + PWM resolution. If we choose selection 64, the variation of PWM 55 + resolution needs more than 64 steps. 56 + 57 + mediatek,bled-ovp-shutdown: 58 + description: | 59 + Enable the backlight shutdown when OVP level triggered 60 + type: boolean 61 + 62 + mediatek,bled-ovp-microvolt: 63 + enum: [17000000, 21000000, 25000000, 29000000] 64 + description: | 65 + Backlight OVP level selection. 66 + 67 + mediatek,bled-ocp-shutdown: 68 + description: | 69 + Enable the backlight shutdown when OCP level triggerred. 70 + type: boolean 71 + 72 + mediatek,bled-ocp-microamp: 73 + enum: [900000, 1200000, 1500000, 1800000] 74 + description: | 75 + Backlight OC level selection. 76 + 77 + mediatek,bled-exponential-mode-enable: 78 + description: | 79 + Enable the exponential mode of backlight brightness. If this property 80 + is not enabled, the default is to use linear mode. 81 + type: boolean 82 + 83 + mediatek,bled-channel-use: 84 + $ref: /schemas/types.yaml#/definitions/uint8 85 + description: | 86 + Backlight LED channel to be used. 87 + Each bit mapping to: 88 + - 0: CH4 89 + - 1: CH3 90 + - 2: CH2 91 + - 3: CH1 92 + minimum: 1 93 + maximum: 15 94 + 95 + if: 96 + properties: 97 + compatible: 98 + contains: 99 + const: mediatek,mt6372-backlight 100 + 101 + then: 102 + properties: 103 + default-brightness: 104 + maximum: 16384 105 + 106 + max-brightness: 107 + maximum: 16384 108 + 109 + else: 110 + properties: 111 + default-brightness: 112 + maximum: 2048 113 + 114 + max-brightness: 115 + maximum: 2048 116 + 117 + required: 118 + - compatible 119 + - mediatek,bled-channel-use 120 + 121 + additionalProperties: false
+13
drivers/video/backlight/Kconfig
··· 268 268 If you have a LCD backlight connected to the WLED output of MAX8925 269 269 WLED output, say Y here to enable this driver. 270 270 271 + config BACKLIGHT_MT6370 272 + tristate "MediaTek MT6370 Backlight Driver" 273 + depends on MFD_MT6370 274 + help 275 + This enables support for Mediatek MT6370 Backlight driver. 276 + It's commonly used to drive the display WLED. There are 4 channels 277 + inside, and each channel supports up to 30mA of current capability 278 + with 2048 current steps (only for MT6370/MT6371) or 16384 current 279 + steps (only for MT6372) in exponential or linear mapping curves. 280 + 281 + This driver can also be built as a module. If so, the module 282 + will be called "mt6370-backlight". 283 + 271 284 config BACKLIGHT_APPLE 272 285 tristate "Apple Backlight Driver" 273 286 depends on X86 && ACPI
+1
drivers/video/backlight/Makefile
··· 44 44 obj-$(CONFIG_BACKLIGHT_LP8788) += lp8788_bl.o 45 45 obj-$(CONFIG_BACKLIGHT_LV5207LP) += lv5207lp.o 46 46 obj-$(CONFIG_BACKLIGHT_MAX8925) += max8925_bl.o 47 + obj-$(CONFIG_BACKLIGHT_MT6370) += mt6370-backlight.o 47 48 obj-$(CONFIG_BACKLIGHT_OMAP1) += omap1_bl.o 48 49 obj-$(CONFIG_BACKLIGHT_PANDORA) += pandora_bl.o 49 50 obj-$(CONFIG_BACKLIGHT_PCF50633) += pcf50633-backlight.o
+351
drivers/video/backlight/mt6370-backlight.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Copyright (C) 2022 Richtek Technology Corp. 4 + * 5 + * Author: ChiaEn Wu <chiaen_wu@richtek.com> 6 + */ 7 + 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> 19 + 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 25 + 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 45 + 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 57 + 58 + enum { 59 + MT6370_VID_COMMON = 1, 60 + MT6370_VID_6372, 61 + }; 62 + 63 + struct mt6370_priv { 64 + u8 dim2_mask; 65 + u8 dim2_shift; 66 + int def_max_brightness; 67 + struct backlight_device *bl; 68 + struct device *dev; 69 + struct gpio_desc *enable_gpio; 70 + struct regmap *regmap; 71 + }; 72 + 73 + static int mt6370_bl_update_status(struct backlight_device *bl_dev) 74 + { 75 + struct mt6370_priv *priv = bl_get_data(bl_dev); 76 + int brightness = backlight_get_brightness(bl_dev); 77 + unsigned int enable_val; 78 + u8 brightness_val[2]; 79 + int ret; 80 + 81 + if (brightness) { 82 + brightness_val[0] = (brightness - 1) & priv->dim2_mask; 83 + brightness_val[1] = (brightness - 1) >> priv->dim2_shift; 84 + 85 + ret = regmap_raw_write(priv->regmap, MT6370_REG_BL_DIM2, 86 + brightness_val, sizeof(brightness_val)); 87 + if (ret) 88 + return ret; 89 + } 90 + 91 + gpiod_set_value(priv->enable_gpio, !!brightness); 92 + 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); 96 + } 97 + 98 + static int mt6370_bl_get_brightness(struct backlight_device *bl_dev) 99 + { 100 + struct mt6370_priv *priv = bl_get_data(bl_dev); 101 + unsigned int enable; 102 + u8 brightness_val[2]; 103 + int brightness, ret; 104 + 105 + ret = regmap_read(priv->regmap, MT6370_REG_BL_EN, &enable); 106 + if (ret) 107 + return ret; 108 + 109 + if (!(enable & MT6370_BL_EN_MASK)) 110 + return 0; 111 + 112 + ret = regmap_raw_read(priv->regmap, MT6370_REG_BL_DIM2, 113 + brightness_val, sizeof(brightness_val)); 114 + if (ret) 115 + return ret; 116 + 117 + brightness = brightness_val[1] << priv->dim2_shift; 118 + brightness += brightness_val[0] & priv->dim2_mask; 119 + 120 + return brightness + 1; 121 + } 122 + 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, 127 + }; 128 + 129 + static int mt6370_init_backlight_properties(struct mt6370_priv *priv, 130 + struct backlight_properties *props) 131 + { 132 + struct device *dev = priv->dev; 133 + u8 prop_val; 134 + u32 brightness, ovp_uV, ocp_uA; 135 + unsigned int mask, val; 136 + int ret; 137 + 138 + /* Vendor optional properties */ 139 + val = 0; 140 + if (device_property_read_bool(dev, "mediatek,bled-pwm-enable")) 141 + val |= MT6370_BL_PWM_EN_MASK; 142 + 143 + if (device_property_read_bool(dev, "mediatek,bled-pwm-hys-enable")) 144 + val |= MT6370_BL_PWM_HYS_EN_MASK; 145 + 146 + ret = device_property_read_u8(dev, 147 + "mediatek,bled-pwm-hys-input-th-steps", 148 + &prop_val); 149 + if (!ret) { 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 : 154 + prop_val <= 4 ? 1 : 155 + prop_val <= 16 ? 2 : 3; 156 + val |= prop_val; 157 + } 158 + 159 + ret = regmap_update_bits(priv->regmap, MT6370_REG_BL_PWM, 160 + val, val); 161 + if (ret) 162 + return ret; 163 + 164 + val = 0; 165 + if (device_property_read_bool(dev, "mediatek,bled-ovp-shutdown")) 166 + val |= MT6370_BL_OVP_EN_MASK; 167 + 168 + ret = device_property_read_u32(dev, "mediatek,bled-ovp-microvolt", 169 + &ovp_uV); 170 + if (!ret) { 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; 176 + } 177 + 178 + if (device_property_read_bool(dev, "mediatek,bled-ocp-shutdown")) 179 + val |= MT6370_BL_OC_EN_MASK; 180 + 181 + ret = device_property_read_u32(dev, "mediatek,bled-ocp-microamp", 182 + &ocp_uA); 183 + if (!ret) { 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; 189 + } 190 + 191 + ret = regmap_update_bits(priv->regmap, MT6370_REG_BL_BSTCTRL, 192 + val, val); 193 + if (ret) 194 + return ret; 195 + 196 + /* Common properties */ 197 + ret = device_property_read_u32(dev, "max-brightness", &brightness); 198 + if (ret) 199 + brightness = priv->def_max_brightness; 200 + 201 + props->max_brightness = min_t(u32, brightness, priv->def_max_brightness); 202 + 203 + ret = device_property_read_u32(dev, "default-brightness", &brightness); 204 + if (ret) 205 + brightness = props->max_brightness; 206 + 207 + props->brightness = min_t(u32, brightness, props->max_brightness); 208 + 209 + val = 0; 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; 213 + } else 214 + props->scale = BACKLIGHT_SCALE_LINEAR; 215 + 216 + ret = device_property_read_u8(dev, "mediatek,bled-channel-use", 217 + &prop_val); 218 + if (ret) { 219 + dev_err(dev, "mediatek,bled-channel-use DT property missing\n"); 220 + return ret; 221 + } 222 + 223 + if (!prop_val || prop_val > MT6370_BL_MAX_CH) { 224 + dev_err(dev, 225 + "No channel specified or over than upper bound (%d)\n", 226 + prop_val); 227 + return -EINVAL; 228 + } 229 + 230 + mask = MT6370_BL_EXT_EN_MASK | MT6370_BL_CH_MASK; 231 + val |= prop_val << MT6370_BL_CH_SHIFT; 232 + 233 + if (priv->enable_gpio) 234 + val |= MT6370_BL_EXT_EN_MASK; 235 + 236 + return regmap_update_bits(priv->regmap, MT6370_REG_BL_EN, mask, val); 237 + } 238 + 239 + static int mt6370_check_vendor_info(struct mt6370_priv *priv) 240 + { 241 + /* 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 246 + * should be used. 247 + */ 248 + unsigned int dev_info, hw_vid, of_vid; 249 + int ret; 250 + 251 + ret = regmap_read(priv->regmap, MT6370_REG_DEV_INFO, &dev_info); 252 + if (ret) 253 + return ret; 254 + 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"); 261 + 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; 266 + } else { 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; 270 + } 271 + 272 + return 0; 273 + } 274 + 275 + static int mt6370_bl_probe(struct platform_device *pdev) 276 + { 277 + struct backlight_properties props = { 278 + .type = BACKLIGHT_RAW, 279 + }; 280 + struct device *dev = &pdev->dev; 281 + struct mt6370_priv *priv; 282 + int ret; 283 + 284 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 285 + if (!priv) 286 + return -ENOMEM; 287 + 288 + priv->dev = dev; 289 + 290 + priv->regmap = dev_get_regmap(dev->parent, NULL); 291 + if (!priv->regmap) 292 + return dev_err_probe(dev, -ENODEV, "Failed to get regmap\n"); 293 + 294 + ret = mt6370_check_vendor_info(priv); 295 + if (ret) 296 + return dev_err_probe(dev, ret, "Failed to check vendor info\n"); 297 + 298 + priv->enable_gpio = devm_gpiod_get_optional(dev, "enable", 299 + GPIOD_OUT_HIGH); 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"); 303 + 304 + ret = mt6370_init_backlight_properties(priv, &props); 305 + if (ret) 306 + return dev_err_probe(dev, ret, 307 + "Failed to init backlight properties\n"); 308 + 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"); 314 + 315 + backlight_update_status(priv->bl); 316 + platform_set_drvdata(pdev, priv); 317 + 318 + return 0; 319 + } 320 + 321 + static int mt6370_bl_remove(struct platform_device *pdev) 322 + { 323 + struct mt6370_priv *priv = platform_get_drvdata(pdev); 324 + struct backlight_device *bl_dev = priv->bl; 325 + 326 + bl_dev->props.brightness = 0; 327 + backlight_update_status(priv->bl); 328 + 329 + return 0; 330 + } 331 + 332 + static const struct of_device_id mt6370_bl_of_match[] = { 333 + { .compatible = "mediatek,mt6370-backlight", .data = (void *)MT6370_VID_COMMON }, 334 + { .compatible = "mediatek,mt6372-backlight", .data = (void *)MT6370_VID_6372 }, 335 + {} 336 + }; 337 + MODULE_DEVICE_TABLE(of, mt6370_bl_of_match); 338 + 339 + static struct platform_driver mt6370_bl_driver = { 340 + .driver = { 341 + .name = "mt6370-backlight", 342 + .of_match_table = mt6370_bl_of_match, 343 + }, 344 + .probe = mt6370_bl_probe, 345 + .remove = mt6370_bl_remove, 346 + }; 347 + module_platform_driver(mt6370_bl_driver); 348 + 349 + MODULE_AUTHOR("ChiaEn Wu <chiaen_wu@richtek.com>"); 350 + MODULE_DESCRIPTION("MediaTek MT6370 Backlight Driver"); 351 + MODULE_LICENSE("GPL v2");