1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI LP8788 MFD - backlight driver
5 * Copyright 2012 Texas Instruments
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
10 #include <linux/backlight.h>
11 #include <linux/err.h>
12 #include <linux/mfd/lp8788.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pwm.h>
16 #include <linux/slab.h>
18 /* Register address */
19 #define LP8788_BL_CONFIG 0x96
20 #define LP8788_BL_EN BIT(0)
21 #define LP8788_BL_PWM_INPUT_EN BIT(5)
22 #define LP8788_BL_FULLSCALE_SHIFT 2
23 #define LP8788_BL_DIM_MODE_SHIFT 1
24 #define LP8788_BL_PWM_POLARITY_SHIFT 6
26 #define LP8788_BL_BRIGHTNESS 0x97
28 #define LP8788_BL_RAMP 0x98
29 #define LP8788_BL_RAMP_RISE_SHIFT 4
31 #define MAX_BRIGHTNESS 127
32 #define DEFAULT_BL_NAME "lcd-backlight"
34 struct lp8788_bl_config {
35 enum lp8788_bl_ctrl_mode bl_mode;
36 enum lp8788_bl_dim_mode dim_mode;
37 enum lp8788_bl_full_scale_current full_scale;
38 enum lp8788_bl_ramp_step rise_time;
39 enum lp8788_bl_ramp_step fall_time;
40 enum pwm_polarity pwm_pol;
45 struct backlight_device *bl_dev;
46 struct lp8788_backlight_platform_data *pdata;
47 enum lp8788_bl_ctrl_mode mode;
48 struct pwm_device *pwm;
51 static struct lp8788_bl_config default_bl_config = {
52 .bl_mode = LP8788_BL_REGISTER_ONLY,
53 .dim_mode = LP8788_DIM_EXPONENTIAL,
54 .full_scale = LP8788_FULLSCALE_1900uA,
55 .rise_time = LP8788_RAMP_8192us,
56 .fall_time = LP8788_RAMP_8192us,
57 .pwm_pol = PWM_POLARITY_NORMAL,
60 static inline bool is_brightness_ctrl_by_pwm(enum lp8788_bl_ctrl_mode mode)
62 return mode == LP8788_BL_COMB_PWM_BASED;
65 static inline bool is_brightness_ctrl_by_register(enum lp8788_bl_ctrl_mode mode)
67 return mode == LP8788_BL_REGISTER_ONLY ||
68 mode == LP8788_BL_COMB_REGISTER_BASED;
71 static int lp8788_backlight_configure(struct lp8788_bl *bl)
73 struct lp8788_backlight_platform_data *pdata = bl->pdata;
74 struct lp8788_bl_config *cfg = &default_bl_config;
79 * Update chip configuration if platform data exists,
80 * otherwise use the default settings.
83 cfg->bl_mode = pdata->bl_mode;
84 cfg->dim_mode = pdata->dim_mode;
85 cfg->full_scale = pdata->full_scale;
86 cfg->rise_time = pdata->rise_time;
87 cfg->fall_time = pdata->fall_time;
88 cfg->pwm_pol = pdata->pwm_pol;
91 /* Brightness ramp up/down */
92 val = (cfg->rise_time << LP8788_BL_RAMP_RISE_SHIFT) | cfg->fall_time;
93 ret = lp8788_write_byte(bl->lp, LP8788_BL_RAMP, val);
97 /* Fullscale current setting */
98 val = (cfg->full_scale << LP8788_BL_FULLSCALE_SHIFT) |
99 (cfg->dim_mode << LP8788_BL_DIM_MODE_SHIFT);
101 /* Brightness control mode */
102 switch (cfg->bl_mode) {
103 case LP8788_BL_REGISTER_ONLY:
106 case LP8788_BL_COMB_PWM_BASED:
107 case LP8788_BL_COMB_REGISTER_BASED:
108 val |= LP8788_BL_EN | LP8788_BL_PWM_INPUT_EN |
109 (cfg->pwm_pol << LP8788_BL_PWM_POLARITY_SHIFT);
112 dev_err(bl->lp->dev, "invalid mode: %d\n", cfg->bl_mode);
116 bl->mode = cfg->bl_mode;
118 return lp8788_write_byte(bl->lp, LP8788_BL_CONFIG, val);
121 static void lp8788_pwm_ctrl(struct lp8788_bl *bl, int br, int max_br)
126 struct pwm_device *pwm;
131 period = bl->pdata->period_ns;
132 duty = br * period / max_br;
135 /* request PWM device with the consumer name */
137 pwm = devm_pwm_get(dev, LP8788_DEV_BACKLIGHT);
139 dev_err(dev, "can not get PWM device\n");
146 * FIXME: pwm_apply_args() should be removed when switching to
147 * the atomic PWM API.
152 pwm_config(bl->pwm, duty, period);
156 pwm_disable(bl->pwm);
159 static int lp8788_bl_update_status(struct backlight_device *bl_dev)
161 struct lp8788_bl *bl = bl_get_data(bl_dev);
162 enum lp8788_bl_ctrl_mode mode = bl->mode;
164 if (bl_dev->props.state & BL_CORE_SUSPENDED)
165 bl_dev->props.brightness = 0;
167 if (is_brightness_ctrl_by_pwm(mode)) {
168 int brt = bl_dev->props.brightness;
169 int max = bl_dev->props.max_brightness;
171 lp8788_pwm_ctrl(bl, brt, max);
172 } else if (is_brightness_ctrl_by_register(mode)) {
173 u8 brt = bl_dev->props.brightness;
175 lp8788_write_byte(bl->lp, LP8788_BL_BRIGHTNESS, brt);
181 static const struct backlight_ops lp8788_bl_ops = {
182 .options = BL_CORE_SUSPENDRESUME,
183 .update_status = lp8788_bl_update_status,
186 static int lp8788_backlight_register(struct lp8788_bl *bl)
188 struct backlight_device *bl_dev;
189 struct backlight_properties props;
190 struct lp8788_backlight_platform_data *pdata = bl->pdata;
194 memset(&props, 0, sizeof(struct backlight_properties));
195 props.type = BACKLIGHT_PLATFORM;
196 props.max_brightness = MAX_BRIGHTNESS;
198 /* Initial brightness */
200 init_brt = min_t(int, pdata->initial_brightness,
201 props.max_brightness);
205 props.brightness = init_brt;
207 /* Backlight device name */
208 if (!pdata || !pdata->name)
209 name = DEFAULT_BL_NAME;
213 bl_dev = backlight_device_register(name, bl->lp->dev, bl,
214 &lp8788_bl_ops, &props);
216 return PTR_ERR(bl_dev);
223 static void lp8788_backlight_unregister(struct lp8788_bl *bl)
225 struct backlight_device *bl_dev = bl->bl_dev;
227 backlight_device_unregister(bl_dev);
230 static ssize_t lp8788_get_bl_ctl_mode(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 struct lp8788_bl *bl = dev_get_drvdata(dev);
234 enum lp8788_bl_ctrl_mode mode = bl->mode;
237 if (is_brightness_ctrl_by_pwm(mode))
238 strmode = "PWM based";
239 else if (is_brightness_ctrl_by_register(mode))
240 strmode = "Register based";
242 strmode = "Invalid mode";
244 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
247 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp8788_get_bl_ctl_mode, NULL);
249 static struct attribute *lp8788_attributes[] = {
250 &dev_attr_bl_ctl_mode.attr,
254 static const struct attribute_group lp8788_attr_group = {
255 .attrs = lp8788_attributes,
258 static int lp8788_backlight_probe(struct platform_device *pdev)
260 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
261 struct lp8788_bl *bl;
264 bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL);
270 bl->pdata = lp->pdata->bl_pdata;
272 platform_set_drvdata(pdev, bl);
274 ret = lp8788_backlight_configure(bl);
276 dev_err(lp->dev, "backlight config err: %d\n", ret);
280 ret = lp8788_backlight_register(bl);
282 dev_err(lp->dev, "register backlight err: %d\n", ret);
286 ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
288 dev_err(lp->dev, "register sysfs err: %d\n", ret);
292 backlight_update_status(bl->bl_dev);
297 lp8788_backlight_unregister(bl);
302 static int lp8788_backlight_remove(struct platform_device *pdev)
304 struct lp8788_bl *bl = platform_get_drvdata(pdev);
305 struct backlight_device *bl_dev = bl->bl_dev;
307 bl_dev->props.brightness = 0;
308 backlight_update_status(bl_dev);
309 sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
310 lp8788_backlight_unregister(bl);
315 static struct platform_driver lp8788_bl_driver = {
316 .probe = lp8788_backlight_probe,
317 .remove = lp8788_backlight_remove,
319 .name = LP8788_DEV_BACKLIGHT,
322 module_platform_driver(lp8788_bl_driver);
324 MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver");
325 MODULE_AUTHOR("Milo Kim");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:lp8788-backlight");