Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * TI LP8788 MFD - backlight driver
0004  *
0005  * Copyright 2012 Texas Instruments
0006  *
0007  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
0008  */
0009 
0010 #include <linux/backlight.h>
0011 #include <linux/err.h>
0012 #include <linux/mfd/lp8788.h>
0013 #include <linux/module.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/pwm.h>
0016 #include <linux/slab.h>
0017 
0018 /* Register address */
0019 #define LP8788_BL_CONFIG        0x96
0020 #define LP8788_BL_EN            BIT(0)
0021 #define LP8788_BL_PWM_INPUT_EN      BIT(5)
0022 #define LP8788_BL_FULLSCALE_SHIFT   2
0023 #define LP8788_BL_DIM_MODE_SHIFT    1
0024 #define LP8788_BL_PWM_POLARITY_SHIFT    6
0025 
0026 #define LP8788_BL_BRIGHTNESS        0x97
0027 
0028 #define LP8788_BL_RAMP          0x98
0029 #define LP8788_BL_RAMP_RISE_SHIFT   4
0030 
0031 #define MAX_BRIGHTNESS          127
0032 #define DEFAULT_BL_NAME         "lcd-backlight"
0033 
0034 struct lp8788_bl_config {
0035     enum lp8788_bl_ctrl_mode bl_mode;
0036     enum lp8788_bl_dim_mode dim_mode;
0037     enum lp8788_bl_full_scale_current full_scale;
0038     enum lp8788_bl_ramp_step rise_time;
0039     enum lp8788_bl_ramp_step fall_time;
0040     enum pwm_polarity pwm_pol;
0041 };
0042 
0043 struct lp8788_bl {
0044     struct lp8788 *lp;
0045     struct backlight_device *bl_dev;
0046     struct lp8788_backlight_platform_data *pdata;
0047     enum lp8788_bl_ctrl_mode mode;
0048     struct pwm_device *pwm;
0049 };
0050 
0051 static struct lp8788_bl_config default_bl_config = {
0052     .bl_mode    = LP8788_BL_REGISTER_ONLY,
0053     .dim_mode   = LP8788_DIM_EXPONENTIAL,
0054     .full_scale = LP8788_FULLSCALE_1900uA,
0055     .rise_time  = LP8788_RAMP_8192us,
0056     .fall_time  = LP8788_RAMP_8192us,
0057     .pwm_pol    = PWM_POLARITY_NORMAL,
0058 };
0059 
0060 static inline bool is_brightness_ctrl_by_pwm(enum lp8788_bl_ctrl_mode mode)
0061 {
0062     return mode == LP8788_BL_COMB_PWM_BASED;
0063 }
0064 
0065 static inline bool is_brightness_ctrl_by_register(enum lp8788_bl_ctrl_mode mode)
0066 {
0067     return mode == LP8788_BL_REGISTER_ONLY ||
0068         mode == LP8788_BL_COMB_REGISTER_BASED;
0069 }
0070 
0071 static int lp8788_backlight_configure(struct lp8788_bl *bl)
0072 {
0073     struct lp8788_backlight_platform_data *pdata = bl->pdata;
0074     struct lp8788_bl_config *cfg = &default_bl_config;
0075     int ret;
0076     u8 val;
0077 
0078     /*
0079      * Update chip configuration if platform data exists,
0080      * otherwise use the default settings.
0081      */
0082     if (pdata) {
0083         cfg->bl_mode    = pdata->bl_mode;
0084         cfg->dim_mode   = pdata->dim_mode;
0085         cfg->full_scale = pdata->full_scale;
0086         cfg->rise_time  = pdata->rise_time;
0087         cfg->fall_time  = pdata->fall_time;
0088         cfg->pwm_pol    = pdata->pwm_pol;
0089     }
0090 
0091     /* Brightness ramp up/down */
0092     val = (cfg->rise_time << LP8788_BL_RAMP_RISE_SHIFT) | cfg->fall_time;
0093     ret = lp8788_write_byte(bl->lp, LP8788_BL_RAMP, val);
0094     if (ret)
0095         return ret;
0096 
0097     /* Fullscale current setting */
0098     val = (cfg->full_scale << LP8788_BL_FULLSCALE_SHIFT) |
0099         (cfg->dim_mode << LP8788_BL_DIM_MODE_SHIFT);
0100 
0101     /* Brightness control mode */
0102     switch (cfg->bl_mode) {
0103     case LP8788_BL_REGISTER_ONLY:
0104         val |= LP8788_BL_EN;
0105         break;
0106     case LP8788_BL_COMB_PWM_BASED:
0107     case LP8788_BL_COMB_REGISTER_BASED:
0108         val |= LP8788_BL_EN | LP8788_BL_PWM_INPUT_EN |
0109             (cfg->pwm_pol << LP8788_BL_PWM_POLARITY_SHIFT);
0110         break;
0111     default:
0112         dev_err(bl->lp->dev, "invalid mode: %d\n", cfg->bl_mode);
0113         return -EINVAL;
0114     }
0115 
0116     bl->mode = cfg->bl_mode;
0117 
0118     return lp8788_write_byte(bl->lp, LP8788_BL_CONFIG, val);
0119 }
0120 
0121 static void lp8788_pwm_ctrl(struct lp8788_bl *bl, int br, int max_br)
0122 {
0123     unsigned int period;
0124     unsigned int duty;
0125     struct device *dev;
0126     struct pwm_device *pwm;
0127 
0128     if (!bl->pdata)
0129         return;
0130 
0131     period = bl->pdata->period_ns;
0132     duty = br * period / max_br;
0133     dev = bl->lp->dev;
0134 
0135     /* request PWM device with the consumer name */
0136     if (!bl->pwm) {
0137         pwm = devm_pwm_get(dev, LP8788_DEV_BACKLIGHT);
0138         if (IS_ERR(pwm)) {
0139             dev_err(dev, "can not get PWM device\n");
0140             return;
0141         }
0142 
0143         bl->pwm = pwm;
0144 
0145         /*
0146          * FIXME: pwm_apply_args() should be removed when switching to
0147          * the atomic PWM API.
0148          */
0149         pwm_apply_args(pwm);
0150     }
0151 
0152     pwm_config(bl->pwm, duty, period);
0153     if (duty)
0154         pwm_enable(bl->pwm);
0155     else
0156         pwm_disable(bl->pwm);
0157 }
0158 
0159 static int lp8788_bl_update_status(struct backlight_device *bl_dev)
0160 {
0161     struct lp8788_bl *bl = bl_get_data(bl_dev);
0162     enum lp8788_bl_ctrl_mode mode = bl->mode;
0163 
0164     if (bl_dev->props.state & BL_CORE_SUSPENDED)
0165         bl_dev->props.brightness = 0;
0166 
0167     if (is_brightness_ctrl_by_pwm(mode)) {
0168         int brt = bl_dev->props.brightness;
0169         int max = bl_dev->props.max_brightness;
0170 
0171         lp8788_pwm_ctrl(bl, brt, max);
0172     } else if (is_brightness_ctrl_by_register(mode)) {
0173         u8 brt = bl_dev->props.brightness;
0174 
0175         lp8788_write_byte(bl->lp, LP8788_BL_BRIGHTNESS, brt);
0176     }
0177 
0178     return 0;
0179 }
0180 
0181 static const struct backlight_ops lp8788_bl_ops = {
0182     .options = BL_CORE_SUSPENDRESUME,
0183     .update_status = lp8788_bl_update_status,
0184 };
0185 
0186 static int lp8788_backlight_register(struct lp8788_bl *bl)
0187 {
0188     struct backlight_device *bl_dev;
0189     struct backlight_properties props;
0190     struct lp8788_backlight_platform_data *pdata = bl->pdata;
0191     int init_brt;
0192     char *name;
0193 
0194     props.type = BACKLIGHT_PLATFORM;
0195     props.max_brightness = MAX_BRIGHTNESS;
0196 
0197     /* Initial brightness */
0198     if (pdata)
0199         init_brt = min_t(int, pdata->initial_brightness,
0200                 props.max_brightness);
0201     else
0202         init_brt = 0;
0203 
0204     props.brightness = init_brt;
0205 
0206     /* Backlight device name */
0207     if (!pdata || !pdata->name)
0208         name = DEFAULT_BL_NAME;
0209     else
0210         name = pdata->name;
0211 
0212     bl_dev = backlight_device_register(name, bl->lp->dev, bl,
0213                        &lp8788_bl_ops, &props);
0214     if (IS_ERR(bl_dev))
0215         return PTR_ERR(bl_dev);
0216 
0217     bl->bl_dev = bl_dev;
0218 
0219     return 0;
0220 }
0221 
0222 static void lp8788_backlight_unregister(struct lp8788_bl *bl)
0223 {
0224     struct backlight_device *bl_dev = bl->bl_dev;
0225 
0226     backlight_device_unregister(bl_dev);
0227 }
0228 
0229 static ssize_t lp8788_get_bl_ctl_mode(struct device *dev,
0230                      struct device_attribute *attr, char *buf)
0231 {
0232     struct lp8788_bl *bl = dev_get_drvdata(dev);
0233     enum lp8788_bl_ctrl_mode mode = bl->mode;
0234     char *strmode;
0235 
0236     if (is_brightness_ctrl_by_pwm(mode))
0237         strmode = "PWM based";
0238     else if (is_brightness_ctrl_by_register(mode))
0239         strmode = "Register based";
0240     else
0241         strmode = "Invalid mode";
0242 
0243     return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
0244 }
0245 
0246 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp8788_get_bl_ctl_mode, NULL);
0247 
0248 static struct attribute *lp8788_attributes[] = {
0249     &dev_attr_bl_ctl_mode.attr,
0250     NULL,
0251 };
0252 
0253 static const struct attribute_group lp8788_attr_group = {
0254     .attrs = lp8788_attributes,
0255 };
0256 
0257 static int lp8788_backlight_probe(struct platform_device *pdev)
0258 {
0259     struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
0260     struct lp8788_bl *bl;
0261     int ret;
0262 
0263     bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL);
0264     if (!bl)
0265         return -ENOMEM;
0266 
0267     bl->lp = lp;
0268     if (lp->pdata)
0269         bl->pdata = lp->pdata->bl_pdata;
0270 
0271     platform_set_drvdata(pdev, bl);
0272 
0273     ret = lp8788_backlight_configure(bl);
0274     if (ret) {
0275         dev_err(lp->dev, "backlight config err: %d\n", ret);
0276         goto err_dev;
0277     }
0278 
0279     ret = lp8788_backlight_register(bl);
0280     if (ret) {
0281         dev_err(lp->dev, "register backlight err: %d\n", ret);
0282         goto err_dev;
0283     }
0284 
0285     ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
0286     if (ret) {
0287         dev_err(lp->dev, "register sysfs err: %d\n", ret);
0288         goto err_sysfs;
0289     }
0290 
0291     backlight_update_status(bl->bl_dev);
0292 
0293     return 0;
0294 
0295 err_sysfs:
0296     lp8788_backlight_unregister(bl);
0297 err_dev:
0298     return ret;
0299 }
0300 
0301 static int lp8788_backlight_remove(struct platform_device *pdev)
0302 {
0303     struct lp8788_bl *bl = platform_get_drvdata(pdev);
0304     struct backlight_device *bl_dev = bl->bl_dev;
0305 
0306     bl_dev->props.brightness = 0;
0307     backlight_update_status(bl_dev);
0308     sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
0309     lp8788_backlight_unregister(bl);
0310 
0311     return 0;
0312 }
0313 
0314 static struct platform_driver lp8788_bl_driver = {
0315     .probe = lp8788_backlight_probe,
0316     .remove = lp8788_backlight_remove,
0317     .driver = {
0318         .name = LP8788_DEV_BACKLIGHT,
0319     },
0320 };
0321 module_platform_driver(lp8788_bl_driver);
0322 
0323 MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver");
0324 MODULE_AUTHOR("Milo Kim");
0325 MODULE_LICENSE("GPL");
0326 MODULE_ALIAS("platform:lp8788-backlight");