1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI LP8788 MFD - backlight driver
5 * Copyright 2012 Texas Instruments
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 props.type = BACKLIGHT_PLATFORM;
195 props.max_brightness = MAX_BRIGHTNESS;
197 /* Initial brightness */
199 init_brt = min_t(int, pdata->initial_brightness,
200 props.max_brightness);
204 props.brightness = init_brt;
206 /* Backlight device name */
207 if (!pdata || !pdata->name)
208 name = DEFAULT_BL_NAME;
212 bl_dev = backlight_device_register(name, bl->lp->dev, bl,
213 &lp8788_bl_ops, &props);
215 return PTR_ERR(bl_dev);
222 static void lp8788_backlight_unregister(struct lp8788_bl *bl)
224 struct backlight_device *bl_dev = bl->bl_dev;
226 backlight_device_unregister(bl_dev);
229 static ssize_t lp8788_get_bl_ctl_mode(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct lp8788_bl *bl = dev_get_drvdata(dev);
233 enum lp8788_bl_ctrl_mode mode = bl->mode;
236 if (is_brightness_ctrl_by_pwm(mode))
237 strmode = "PWM based";
238 else if (is_brightness_ctrl_by_register(mode))
239 strmode = "Register based";
241 strmode = "Invalid mode";
243 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
246 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp8788_get_bl_ctl_mode, NULL);
248 static struct attribute *lp8788_attributes[] = {
249 &dev_attr_bl_ctl_mode.attr,
253 static const struct attribute_group lp8788_attr_group = {
254 .attrs = lp8788_attributes,
257 static int lp8788_backlight_probe(struct platform_device *pdev)
259 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
260 struct lp8788_bl *bl;
263 bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL);
269 bl->pdata = lp->pdata->bl_pdata;
271 platform_set_drvdata(pdev, bl);
273 ret = lp8788_backlight_configure(bl);
275 dev_err(lp->dev, "backlight config err: %d\n", ret);
279 ret = lp8788_backlight_register(bl);
281 dev_err(lp->dev, "register backlight err: %d\n", ret);
285 ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
287 dev_err(lp->dev, "register sysfs err: %d\n", ret);
291 backlight_update_status(bl->bl_dev);
296 lp8788_backlight_unregister(bl);
301 static int lp8788_backlight_remove(struct platform_device *pdev)
303 struct lp8788_bl *bl = platform_get_drvdata(pdev);
304 struct backlight_device *bl_dev = bl->bl_dev;
306 bl_dev->props.brightness = 0;
307 backlight_update_status(bl_dev);
308 sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
309 lp8788_backlight_unregister(bl);
314 static struct platform_driver lp8788_bl_driver = {
315 .probe = lp8788_backlight_probe,
316 .remove = lp8788_backlight_remove,
318 .name = LP8788_DEV_BACKLIGHT,
321 module_platform_driver(lp8788_bl_driver);
323 MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver");
324 MODULE_AUTHOR("Milo Kim");
325 MODULE_LICENSE("GPL");
326 MODULE_ALIAS("platform:lp8788-backlight");