1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Backlight driver for Marvell Semiconductor 88PM8606
4  *
5  * Copyright (C) 2009 Marvell International Ltd.
6  *	Haojian Zhuang <haojian.zhuang@marvell.com>
7  */
8 
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/fb.h>
15 #include <linux/i2c.h>
16 #include <linux/backlight.h>
17 #include <linux/mfd/88pm860x.h>
18 #include <linux/module.h>
19 
20 #define MAX_BRIGHTNESS		(0xFF)
21 #define MIN_BRIGHTNESS		(0)
22 
23 #define CURRENT_BITMASK		(0x1F << 1)
24 
25 struct pm860x_backlight_data {
26 	struct pm860x_chip *chip;
27 	struct i2c_client *i2c;
28 	int	current_brightness;
29 	int	port;
30 	int	pwm;
31 	int	iset;
32 	int	reg_duty_cycle;
33 	int	reg_always_on;
34 	int	reg_current;
35 };
36 
37 static int backlight_power_set(struct pm860x_chip *chip, int port,
38 		int on)
39 {
40 	int ret = -EINVAL;
41 
42 	switch (port) {
43 	case 0:
44 		ret = on ? pm8606_osc_enable(chip, WLED1_DUTY) :
45 			pm8606_osc_disable(chip, WLED1_DUTY);
46 		break;
47 	case 1:
48 		ret = on ? pm8606_osc_enable(chip, WLED2_DUTY) :
49 			pm8606_osc_disable(chip, WLED2_DUTY);
50 		break;
51 	case 2:
52 		ret = on ? pm8606_osc_enable(chip, WLED3_DUTY) :
53 			pm8606_osc_disable(chip, WLED3_DUTY);
54 		break;
55 	}
56 	return ret;
57 }
58 
59 static int pm860x_backlight_set(struct backlight_device *bl, int brightness)
60 {
61 	struct pm860x_backlight_data *data = bl_get_data(bl);
62 	struct pm860x_chip *chip = data->chip;
63 	unsigned char value;
64 	int ret;
65 
66 	if (brightness > MAX_BRIGHTNESS)
67 		value = MAX_BRIGHTNESS;
68 	else
69 		value = brightness;
70 
71 	if (brightness)
72 		backlight_power_set(chip, data->port, 1);
73 
74 	ret = pm860x_reg_write(data->i2c, data->reg_duty_cycle, value);
75 	if (ret < 0)
76 		goto out;
77 
78 	if ((data->current_brightness == 0) && brightness) {
79 		if (data->iset) {
80 			ret = pm860x_set_bits(data->i2c, data->reg_current,
81 					      CURRENT_BITMASK, data->iset);
82 			if (ret < 0)
83 				goto out;
84 		}
85 		if (data->pwm) {
86 			ret = pm860x_set_bits(data->i2c, PM8606_PWM,
87 					      PM8606_PWM_FREQ_MASK, data->pwm);
88 			if (ret < 0)
89 				goto out;
90 		}
91 		if (brightness == MAX_BRIGHTNESS) {
92 			/* set WLED_ON bit as 100% */
93 			ret = pm860x_set_bits(data->i2c, data->reg_always_on,
94 					      PM8606_WLED_ON, PM8606_WLED_ON);
95 		}
96 	} else {
97 		if (brightness == MAX_BRIGHTNESS) {
98 			/* set WLED_ON bit as 100% */
99 			ret = pm860x_set_bits(data->i2c, data->reg_always_on,
100 					      PM8606_WLED_ON, PM8606_WLED_ON);
101 		} else {
102 			/* clear WLED_ON bit since it's not 100% */
103 			ret = pm860x_set_bits(data->i2c, data->reg_always_on,
104 					      PM8606_WLED_ON, 0);
105 		}
106 	}
107 	if (ret < 0)
108 		goto out;
109 
110 	if (brightness == 0)
111 		backlight_power_set(chip, data->port, 0);
112 
113 	dev_dbg(chip->dev, "set brightness %d\n", value);
114 	data->current_brightness = value;
115 	return 0;
116 out:
117 	dev_dbg(chip->dev, "set brightness %d failure with return value: %d\n",
118 		value, ret);
119 	return ret;
120 }
121 
122 static int pm860x_backlight_update_status(struct backlight_device *bl)
123 {
124 	int brightness = bl->props.brightness;
125 
126 	if (bl->props.power != FB_BLANK_UNBLANK)
127 		brightness = 0;
128 
129 	if (bl->props.fb_blank != FB_BLANK_UNBLANK)
130 		brightness = 0;
131 
132 	if (bl->props.state & BL_CORE_SUSPENDED)
133 		brightness = 0;
134 
135 	return pm860x_backlight_set(bl, brightness);
136 }
137 
138 static int pm860x_backlight_get_brightness(struct backlight_device *bl)
139 {
140 	struct pm860x_backlight_data *data = bl_get_data(bl);
141 	struct pm860x_chip *chip = data->chip;
142 	int ret;
143 
144 	ret = pm860x_reg_read(data->i2c, data->reg_duty_cycle);
145 	if (ret < 0)
146 		goto out;
147 	data->current_brightness = ret;
148 	dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
149 	return data->current_brightness;
150 out:
151 	return -EINVAL;
152 }
153 
154 static const struct backlight_ops pm860x_backlight_ops = {
155 	.options	= BL_CORE_SUSPENDRESUME,
156 	.update_status	= pm860x_backlight_update_status,
157 	.get_brightness	= pm860x_backlight_get_brightness,
158 };
159 
160 #ifdef CONFIG_OF
161 static int pm860x_backlight_dt_init(struct platform_device *pdev,
162 				    struct pm860x_backlight_data *data,
163 				    char *name)
164 {
165 	struct device_node *nproot, *np;
166 	int iset = 0;
167 
168 	nproot = of_get_child_by_name(pdev->dev.parent->of_node, "backlights");
169 	if (!nproot) {
170 		dev_err(&pdev->dev, "failed to find backlights node\n");
171 		return -ENODEV;
172 	}
173 	for_each_child_of_node(nproot, np) {
174 		if (of_node_name_eq(np, name)) {
175 			of_property_read_u32(np, "marvell,88pm860x-iset",
176 					     &iset);
177 			data->iset = PM8606_WLED_CURRENT(iset);
178 			of_property_read_u32(np, "marvell,88pm860x-pwm",
179 					     &data->pwm);
180 			of_node_put(np);
181 			break;
182 		}
183 	}
184 	of_node_put(nproot);
185 	return 0;
186 }
187 #else
188 #define pm860x_backlight_dt_init(x, y, z)	(-1)
189 #endif
190 
191 static int pm860x_backlight_probe(struct platform_device *pdev)
192 {
193 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
194 	struct pm860x_backlight_pdata *pdata = dev_get_platdata(&pdev->dev);
195 	struct pm860x_backlight_data *data;
196 	struct backlight_device *bl;
197 	struct resource *res;
198 	struct backlight_properties props;
199 	char name[MFD_NAME_SIZE];
200 	int ret = 0;
201 
202 	data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data),
203 			    GFP_KERNEL);
204 	if (data == NULL)
205 		return -ENOMEM;
206 	res = platform_get_resource_byname(pdev, IORESOURCE_REG, "duty cycle");
207 	if (!res) {
208 		dev_err(&pdev->dev, "No REG resource for duty cycle\n");
209 		return -ENXIO;
210 	}
211 	data->reg_duty_cycle = res->start;
212 	res = platform_get_resource_byname(pdev, IORESOURCE_REG, "always on");
213 	if (!res) {
214 		dev_err(&pdev->dev, "No REG resource for always on\n");
215 		return -ENXIO;
216 	}
217 	data->reg_always_on = res->start;
218 	res = platform_get_resource_byname(pdev, IORESOURCE_REG, "current");
219 	if (!res) {
220 		dev_err(&pdev->dev, "No REG resource for current\n");
221 		return -ENXIO;
222 	}
223 	data->reg_current = res->start;
224 
225 	memset(name, 0, MFD_NAME_SIZE);
226 	sprintf(name, "backlight-%d", pdev->id);
227 	data->port = pdev->id;
228 	data->chip = chip;
229 	data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
230 	data->current_brightness = MAX_BRIGHTNESS;
231 	if (pm860x_backlight_dt_init(pdev, data, name)) {
232 		if (pdata) {
233 			data->pwm = pdata->pwm;
234 			data->iset = pdata->iset;
235 		}
236 	}
237 
238 	memset(&props, 0, sizeof(struct backlight_properties));
239 	props.type = BACKLIGHT_RAW;
240 	props.max_brightness = MAX_BRIGHTNESS;
241 	bl = devm_backlight_device_register(&pdev->dev, name, &pdev->dev, data,
242 					&pm860x_backlight_ops, &props);
243 	if (IS_ERR(bl)) {
244 		dev_err(&pdev->dev, "failed to register backlight\n");
245 		return PTR_ERR(bl);
246 	}
247 	bl->props.brightness = MAX_BRIGHTNESS;
248 
249 	platform_set_drvdata(pdev, bl);
250 
251 	/* read current backlight */
252 	ret = pm860x_backlight_get_brightness(bl);
253 	if (ret < 0)
254 		return ret;
255 
256 	backlight_update_status(bl);
257 	return 0;
258 }
259 
260 static struct platform_driver pm860x_backlight_driver = {
261 	.driver		= {
262 		.name	= "88pm860x-backlight",
263 	},
264 	.probe		= pm860x_backlight_probe,
265 };
266 
267 module_platform_driver(pm860x_backlight_driver);
268 
269 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
270 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
271 MODULE_LICENSE("GPL");
272 MODULE_ALIAS("platform:88pm860x-backlight");
273