xref: /linux/drivers/video/backlight/da903x_bl.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Backlight driver for Dialog Semiconductor DA9030/DA9034
4  *
5  * Copyright (C) 2008 Compulab, Ltd.
6  *	Mike Rapoport <mike@compulab.co.il>
7  *
8  * Copyright (C) 2006-2008 Marvell International Ltd.
9  *	Eric Miao <eric.miao@marvell.com>
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/fb.h>
16 #include <linux/backlight.h>
17 #include <linux/mfd/da903x.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 
21 #define DA9030_WLED_CONTROL	0x25
22 #define DA9030_WLED_CP_EN	(1 << 6)
23 #define DA9030_WLED_TRIM(x)	((x) & 0x7)
24 
25 #define DA9034_WLED_CONTROL1	0x3C
26 #define DA9034_WLED_CONTROL2	0x3D
27 #define DA9034_WLED_ISET(x)	((x) & 0x1f)
28 
29 #define DA9034_WLED_BOOST_EN	(1 << 5)
30 
31 #define DA9030_MAX_BRIGHTNESS	7
32 #define DA9034_MAX_BRIGHTNESS	0x7f
33 
34 struct da903x_backlight_data {
35 	struct device *da903x_dev;
36 	int id;
37 	int current_brightness;
38 };
39 
40 static int da903x_backlight_set(struct backlight_device *bl, int brightness)
41 {
42 	struct da903x_backlight_data *data = bl_get_data(bl);
43 	struct device *dev = data->da903x_dev;
44 	uint8_t val;
45 	int ret = 0;
46 
47 	switch (data->id) {
48 	case DA9034_ID_WLED:
49 		ret = da903x_update(dev, DA9034_WLED_CONTROL1,
50 				brightness, 0x7f);
51 		if (ret)
52 			return ret;
53 
54 		if (data->current_brightness && brightness == 0)
55 			ret = da903x_clr_bits(dev,
56 					DA9034_WLED_CONTROL2,
57 					DA9034_WLED_BOOST_EN);
58 
59 		if (data->current_brightness == 0 && brightness)
60 			ret = da903x_set_bits(dev,
61 					DA9034_WLED_CONTROL2,
62 					DA9034_WLED_BOOST_EN);
63 		break;
64 	case DA9030_ID_WLED:
65 		val = DA9030_WLED_TRIM(brightness);
66 		val |= brightness ? DA9030_WLED_CP_EN : 0;
67 		ret = da903x_write(dev, DA9030_WLED_CONTROL, val);
68 		break;
69 	}
70 
71 	if (ret)
72 		return ret;
73 
74 	data->current_brightness = brightness;
75 	return 0;
76 }
77 
78 static int da903x_backlight_update_status(struct backlight_device *bl)
79 {
80 	int brightness = bl->props.brightness;
81 
82 	if (bl->props.power != FB_BLANK_UNBLANK)
83 		brightness = 0;
84 
85 	if (bl->props.fb_blank != FB_BLANK_UNBLANK)
86 		brightness = 0;
87 
88 	if (bl->props.state & BL_CORE_SUSPENDED)
89 		brightness = 0;
90 
91 	return da903x_backlight_set(bl, brightness);
92 }
93 
94 static int da903x_backlight_get_brightness(struct backlight_device *bl)
95 {
96 	struct da903x_backlight_data *data = bl_get_data(bl);
97 
98 	return data->current_brightness;
99 }
100 
101 static const struct backlight_ops da903x_backlight_ops = {
102 	.options	= BL_CORE_SUSPENDRESUME,
103 	.update_status	= da903x_backlight_update_status,
104 	.get_brightness	= da903x_backlight_get_brightness,
105 };
106 
107 static int da903x_backlight_probe(struct platform_device *pdev)
108 {
109 	struct da9034_backlight_pdata *pdata = dev_get_platdata(&pdev->dev);
110 	struct da903x_backlight_data *data;
111 	struct backlight_device *bl;
112 	struct backlight_properties props;
113 	int max_brightness;
114 
115 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
116 	if (data == NULL)
117 		return -ENOMEM;
118 
119 	switch (pdev->id) {
120 	case DA9030_ID_WLED:
121 		max_brightness = DA9030_MAX_BRIGHTNESS;
122 		break;
123 	case DA9034_ID_WLED:
124 		max_brightness = DA9034_MAX_BRIGHTNESS;
125 		break;
126 	default:
127 		dev_err(&pdev->dev, "invalid backlight device ID(%d)\n",
128 				pdev->id);
129 		return -EINVAL;
130 	}
131 
132 	data->id = pdev->id;
133 	data->da903x_dev = pdev->dev.parent;
134 	data->current_brightness = 0;
135 
136 	/* adjust the WLED output current */
137 	if (pdata)
138 		da903x_write(data->da903x_dev, DA9034_WLED_CONTROL2,
139 				DA9034_WLED_ISET(pdata->output_current));
140 
141 	memset(&props, 0, sizeof(props));
142 	props.type = BACKLIGHT_RAW;
143 	props.max_brightness = max_brightness;
144 	bl = devm_backlight_device_register(&pdev->dev, pdev->name,
145 					data->da903x_dev, data,
146 					&da903x_backlight_ops, &props);
147 	if (IS_ERR(bl)) {
148 		dev_err(&pdev->dev, "failed to register backlight\n");
149 		return PTR_ERR(bl);
150 	}
151 
152 	bl->props.brightness = max_brightness;
153 
154 	platform_set_drvdata(pdev, bl);
155 	backlight_update_status(bl);
156 	return 0;
157 }
158 
159 static struct platform_driver da903x_backlight_driver = {
160 	.driver		= {
161 		.name	= "da903x-backlight",
162 	},
163 	.probe		= da903x_backlight_probe,
164 };
165 
166 module_platform_driver(da903x_backlight_driver);
167 
168 MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034");
169 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
170 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
171 MODULE_LICENSE("GPL");
172 MODULE_ALIAS("platform:da903x-backlight");
173