Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 #include <dt-bindings/leds/rt4831-backlight.h>
0004 #include <linux/backlight.h>
0005 #include <linux/bitops.h>
0006 #include <linux/kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/property.h>
0010 #include <linux/regmap.h>
0011 
0012 #define RT4831_REG_BLCFG    0x02
0013 #define RT4831_REG_BLDIML   0x04
0014 #define RT4831_REG_ENABLE   0x08
0015 #define RT4831_REG_BLOPT2   0x11
0016 
0017 #define RT4831_BLMAX_BRIGHTNESS 2048
0018 
0019 #define RT4831_BLOVP_MASK   GENMASK(7, 5)
0020 #define RT4831_BLOVP_SHIFT  5
0021 #define RT4831_BLPWMEN_MASK BIT(0)
0022 #define RT4831_BLEN_MASK    BIT(4)
0023 #define RT4831_BLCH_MASK    GENMASK(3, 0)
0024 #define RT4831_BLDIML_MASK  GENMASK(2, 0)
0025 #define RT4831_BLDIMH_MASK  GENMASK(10, 3)
0026 #define RT4831_BLDIMH_SHIFT 3
0027 #define RT4831_BLOCP_MASK   GENMASK(1, 0)
0028 
0029 #define RT4831_BLOCP_MINUA  900000
0030 #define RT4831_BLOCP_MAXUA  1800000
0031 #define RT4831_BLOCP_STEPUA 300000
0032 
0033 struct rt4831_priv {
0034     struct device *dev;
0035     struct regmap *regmap;
0036     struct backlight_device *bl;
0037 };
0038 
0039 static int rt4831_bl_update_status(struct backlight_device *bl_dev)
0040 {
0041     struct rt4831_priv *priv = bl_get_data(bl_dev);
0042     int brightness = backlight_get_brightness(bl_dev);
0043     unsigned int enable = brightness ? RT4831_BLEN_MASK : 0;
0044     u8 v[2];
0045     int ret;
0046 
0047     if (brightness) {
0048         v[0] = (brightness - 1) & RT4831_BLDIML_MASK;
0049         v[1] = ((brightness - 1) & RT4831_BLDIMH_MASK) >> RT4831_BLDIMH_SHIFT;
0050 
0051         ret = regmap_raw_write(priv->regmap, RT4831_REG_BLDIML, v, sizeof(v));
0052         if (ret)
0053             return ret;
0054     }
0055 
0056     return regmap_update_bits(priv->regmap, RT4831_REG_ENABLE, RT4831_BLEN_MASK, enable);
0057 
0058 }
0059 
0060 static int rt4831_bl_get_brightness(struct backlight_device *bl_dev)
0061 {
0062     struct rt4831_priv *priv = bl_get_data(bl_dev);
0063     unsigned int val;
0064     u8 v[2];
0065     int ret;
0066 
0067     ret = regmap_read(priv->regmap, RT4831_REG_ENABLE, &val);
0068     if (ret)
0069         return ret;
0070 
0071     if (!(val & RT4831_BLEN_MASK))
0072         return 0;
0073 
0074     ret = regmap_raw_read(priv->regmap, RT4831_REG_BLDIML, v, sizeof(v));
0075     if (ret)
0076         return ret;
0077 
0078     ret = (v[1] << RT4831_BLDIMH_SHIFT) + (v[0] & RT4831_BLDIML_MASK) + 1;
0079 
0080     return ret;
0081 }
0082 
0083 static const struct backlight_ops rt4831_bl_ops = {
0084     .options = BL_CORE_SUSPENDRESUME,
0085     .update_status = rt4831_bl_update_status,
0086     .get_brightness = rt4831_bl_get_brightness,
0087 };
0088 
0089 static int rt4831_parse_backlight_properties(struct rt4831_priv *priv,
0090                          struct backlight_properties *bl_props)
0091 {
0092     struct device *dev = priv->dev;
0093     u8 propval;
0094     u32 brightness, ocp_uA;
0095     unsigned int val = 0;
0096     int ret;
0097 
0098     /* common properties */
0099     ret = device_property_read_u32(dev, "max-brightness", &brightness);
0100     if (ret)
0101         brightness = RT4831_BLMAX_BRIGHTNESS;
0102 
0103     bl_props->max_brightness = min_t(u32, brightness, RT4831_BLMAX_BRIGHTNESS);
0104 
0105     ret = device_property_read_u32(dev, "default-brightness", &brightness);
0106     if (ret)
0107         brightness = bl_props->max_brightness;
0108 
0109     bl_props->brightness = min_t(u32, brightness, bl_props->max_brightness);
0110 
0111     /* vendor properties */
0112     if (device_property_read_bool(dev, "richtek,pwm-enable"))
0113         val = RT4831_BLPWMEN_MASK;
0114 
0115     ret = regmap_update_bits(priv->regmap, RT4831_REG_BLCFG, RT4831_BLPWMEN_MASK, val);
0116     if (ret)
0117         return ret;
0118 
0119     ret = device_property_read_u8(dev, "richtek,bled-ovp-sel", &propval);
0120     if (ret)
0121         propval = RT4831_BLOVPLVL_21V;
0122 
0123     propval = min_t(u8, propval, RT4831_BLOVPLVL_29V);
0124     ret = regmap_update_bits(priv->regmap, RT4831_REG_BLCFG, RT4831_BLOVP_MASK,
0125                  propval << RT4831_BLOVP_SHIFT);
0126     if (ret)
0127         return ret;
0128 
0129     /*
0130      * This OCP level is used to protect and limit the inductor current.
0131      * If inductor peak current reach the level, low-side MOSFET will be
0132      * turned off. Meanwhile, the output channel current may be limited.
0133      * To match the configured channel current, the inductor chosen must
0134      * be higher than the OCP level.
0135      *
0136      * Not like the OVP level, the default 21V can be used in the most
0137      * application. But if the chosen OCP level is smaller than needed,
0138      * it will also affect the backlight channel output current to be
0139      * smaller than the register setting.
0140      */
0141     ret = device_property_read_u32(dev, "richtek,bled-ocp-microamp",
0142                        &ocp_uA);
0143     if (!ret) {
0144         ocp_uA = clamp_val(ocp_uA, RT4831_BLOCP_MINUA,
0145                    RT4831_BLOCP_MAXUA);
0146         val = DIV_ROUND_UP(ocp_uA - RT4831_BLOCP_MINUA,
0147                    RT4831_BLOCP_STEPUA);
0148         ret = regmap_update_bits(priv->regmap, RT4831_REG_BLOPT2,
0149                      RT4831_BLOCP_MASK, val);
0150         if (ret)
0151             return ret;
0152     }
0153 
0154     ret = device_property_read_u8(dev, "richtek,channel-use", &propval);
0155     if (ret) {
0156         dev_err(dev, "richtek,channel-use DT property missing\n");
0157         return ret;
0158     }
0159 
0160     if (!(propval & RT4831_BLCH_MASK)) {
0161         dev_err(dev, "No channel specified\n");
0162         return -EINVAL;
0163     }
0164 
0165     return regmap_update_bits(priv->regmap, RT4831_REG_ENABLE, RT4831_BLCH_MASK, propval);
0166 }
0167 
0168 static int rt4831_bl_probe(struct platform_device *pdev)
0169 {
0170     struct rt4831_priv *priv;
0171     struct backlight_properties bl_props = { .type = BACKLIGHT_RAW,
0172                          .scale = BACKLIGHT_SCALE_LINEAR };
0173     int ret;
0174 
0175     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0176     if (!priv)
0177         return -ENOMEM;
0178 
0179     priv->dev = &pdev->dev;
0180 
0181     priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
0182     if (!priv->regmap) {
0183         dev_err(&pdev->dev, "Failed to init regmap\n");
0184         return -ENODEV;
0185     }
0186 
0187     ret = rt4831_parse_backlight_properties(priv, &bl_props);
0188     if (ret) {
0189         dev_err(&pdev->dev, "Failed to parse backlight properties\n");
0190         return ret;
0191     }
0192 
0193     priv->bl = devm_backlight_device_register(&pdev->dev, pdev->name, &pdev->dev, priv,
0194                           &rt4831_bl_ops, &bl_props);
0195     if (IS_ERR(priv->bl)) {
0196         dev_err(&pdev->dev, "Failed to register backlight\n");
0197         return PTR_ERR(priv->bl);
0198     }
0199 
0200     backlight_update_status(priv->bl);
0201     platform_set_drvdata(pdev, priv);
0202 
0203     return 0;
0204 }
0205 
0206 static int rt4831_bl_remove(struct platform_device *pdev)
0207 {
0208     struct rt4831_priv *priv = platform_get_drvdata(pdev);
0209     struct backlight_device *bl_dev = priv->bl;
0210 
0211     bl_dev->props.brightness = 0;
0212     backlight_update_status(priv->bl);
0213 
0214     return 0;
0215 }
0216 
0217 static const struct of_device_id __maybe_unused rt4831_bl_of_match[] = {
0218     { .compatible = "richtek,rt4831-backlight", },
0219     {}
0220 };
0221 MODULE_DEVICE_TABLE(of, rt4831_bl_of_match);
0222 
0223 static struct platform_driver rt4831_bl_driver = {
0224     .driver = {
0225         .name = "rt4831-backlight",
0226         .of_match_table = rt4831_bl_of_match,
0227     },
0228     .probe = rt4831_bl_probe,
0229     .remove = rt4831_bl_remove,
0230 };
0231 module_platform_driver(rt4831_bl_driver);
0232 
0233 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
0234 MODULE_LICENSE("GPL v2");