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/slab.h>
17 /* Register address */
18 #define LP8788_BL_CONFIG 0x96
19 #define LP8788_BL_EN BIT(0)
20 #define LP8788_BL_PWM_INPUT_EN BIT(5)
21 #define LP8788_BL_FULLSCALE_SHIFT 2
22 #define LP8788_BL_DIM_MODE_SHIFT 1
23 #define LP8788_BL_PWM_POLARITY_SHIFT 6
25 #define LP8788_BL_BRIGHTNESS 0x97
27 #define LP8788_BL_RAMP 0x98
28 #define LP8788_BL_RAMP_RISE_SHIFT 4
30 #define MAX_BRIGHTNESS 127
31 #define DEFAULT_BL_NAME "lcd-backlight"
35 struct backlight_device
*bl_dev
;
38 static int lp8788_backlight_configure(struct lp8788_bl
*bl
)
43 /* Brightness ramp up/down */
44 val
= (LP8788_RAMP_8192us
<< LP8788_BL_RAMP_RISE_SHIFT
) | LP8788_RAMP_8192us
;
45 ret
= lp8788_write_byte(bl
->lp
, LP8788_BL_RAMP
, val
);
49 /* Fullscale current setting */
50 val
= (LP8788_FULLSCALE_1900uA
<< LP8788_BL_FULLSCALE_SHIFT
) |
51 (LP8788_DIM_EXPONENTIAL
<< LP8788_BL_DIM_MODE_SHIFT
);
53 /* Brightness control mode */
56 return lp8788_write_byte(bl
->lp
, LP8788_BL_CONFIG
, val
);
59 static int lp8788_bl_update_status(struct backlight_device
*bl_dev
)
61 struct lp8788_bl
*bl
= bl_get_data(bl_dev
);
63 if (bl_dev
->props
.state
& BL_CORE_SUSPENDED
)
64 bl_dev
->props
.brightness
= 0;
66 lp8788_write_byte(bl
->lp
, LP8788_BL_BRIGHTNESS
, bl_dev
->props
.brightness
);
71 static const struct backlight_ops lp8788_bl_ops
= {
72 .options
= BL_CORE_SUSPENDRESUME
,
73 .update_status
= lp8788_bl_update_status
,
76 static int lp8788_backlight_register(struct lp8788_bl
*bl
)
78 struct backlight_device
*bl_dev
;
79 struct backlight_properties props
;
81 memset(&props
, 0, sizeof(struct backlight_properties
));
82 props
.type
= BACKLIGHT_PLATFORM
;
83 props
.max_brightness
= MAX_BRIGHTNESS
;
85 /* Initial brightness */
88 /* Backlight device name */
89 bl_dev
= backlight_device_register(DEFAULT_BL_NAME
, bl
->lp
->dev
, bl
,
90 &lp8788_bl_ops
, &props
);
92 return PTR_ERR(bl_dev
);
99 static void lp8788_backlight_unregister(struct lp8788_bl
*bl
)
101 struct backlight_device
*bl_dev
= bl
->bl_dev
;
103 backlight_device_unregister(bl_dev
);
106 static ssize_t
lp8788_get_bl_ctl_mode(struct device
*dev
,
107 struct device_attribute
*attr
, char *buf
)
109 const char *strmode
= "Register based";
111 return scnprintf(buf
, PAGE_SIZE
, "%s\n", strmode
);
114 static DEVICE_ATTR(bl_ctl_mode
, S_IRUGO
, lp8788_get_bl_ctl_mode
, NULL
);
116 static struct attribute
*lp8788_attributes
[] = {
117 &dev_attr_bl_ctl_mode
.attr
,
121 static const struct attribute_group lp8788_attr_group
= {
122 .attrs
= lp8788_attributes
,
125 static int lp8788_backlight_probe(struct platform_device
*pdev
)
127 struct lp8788
*lp
= dev_get_drvdata(pdev
->dev
.parent
);
128 struct lp8788_bl
*bl
;
131 bl
= devm_kzalloc(lp
->dev
, sizeof(struct lp8788_bl
), GFP_KERNEL
);
137 platform_set_drvdata(pdev
, bl
);
139 ret
= lp8788_backlight_configure(bl
);
141 dev_err(lp
->dev
, "backlight config err: %d\n", ret
);
145 ret
= lp8788_backlight_register(bl
);
147 dev_err(lp
->dev
, "register backlight err: %d\n", ret
);
151 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &lp8788_attr_group
);
153 dev_err(lp
->dev
, "register sysfs err: %d\n", ret
);
157 backlight_update_status(bl
->bl_dev
);
162 lp8788_backlight_unregister(bl
);
167 static void lp8788_backlight_remove(struct platform_device
*pdev
)
169 struct lp8788_bl
*bl
= platform_get_drvdata(pdev
);
170 struct backlight_device
*bl_dev
= bl
->bl_dev
;
172 bl_dev
->props
.brightness
= 0;
173 backlight_update_status(bl_dev
);
174 sysfs_remove_group(&pdev
->dev
.kobj
, &lp8788_attr_group
);
175 lp8788_backlight_unregister(bl
);
178 static struct platform_driver lp8788_bl_driver
= {
179 .probe
= lp8788_backlight_probe
,
180 .remove
= lp8788_backlight_remove
,
182 .name
= LP8788_DEV_BACKLIGHT
,
185 module_platform_driver(lp8788_bl_driver
);
187 MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver");
188 MODULE_AUTHOR("Milo Kim");
189 MODULE_LICENSE("GPL");
190 MODULE_ALIAS("platform:lp8788-backlight");