Get rid of 'remove_new' relic from platform driver struct
[linux.git] / drivers / video / backlight / lp8788_bl.c
blobf61a64905a02dc722158f800465f36cc66ef2d87
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TI LP8788 MFD - backlight driver
5 * Copyright 2012 Texas Instruments
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 */
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"
33 struct lp8788_bl {
34 struct lp8788 *lp;
35 struct backlight_device *bl_dev;
38 static int lp8788_backlight_configure(struct lp8788_bl *bl)
40 int ret;
41 u8 val;
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);
46 if (ret)
47 return ret;
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 */
54 val |= LP8788_BL_EN;
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);
68 return 0;
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 */
86 props.brightness = 0;
88 /* Backlight device name */
89 bl_dev = backlight_device_register(DEFAULT_BL_NAME, bl->lp->dev, bl,
90 &lp8788_bl_ops, &props);
91 if (IS_ERR(bl_dev))
92 return PTR_ERR(bl_dev);
94 bl->bl_dev = bl_dev;
96 return 0;
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,
118 NULL,
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;
129 int ret;
131 bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL);
132 if (!bl)
133 return -ENOMEM;
135 bl->lp = lp;
137 platform_set_drvdata(pdev, bl);
139 ret = lp8788_backlight_configure(bl);
140 if (ret) {
141 dev_err(lp->dev, "backlight config err: %d\n", ret);
142 goto err_dev;
145 ret = lp8788_backlight_register(bl);
146 if (ret) {
147 dev_err(lp->dev, "register backlight err: %d\n", ret);
148 goto err_dev;
151 ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
152 if (ret) {
153 dev_err(lp->dev, "register sysfs err: %d\n", ret);
154 goto err_sysfs;
157 backlight_update_status(bl->bl_dev);
159 return 0;
161 err_sysfs:
162 lp8788_backlight_unregister(bl);
163 err_dev:
164 return ret;
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,
181 .driver = {
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");