xref: /linux/drivers/video/backlight/max8925_bl.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Backlight driver for Maxim MAX8925
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/platform_device.h>
12 #include <linux/fb.h>
13 #include <linux/i2c.h>
14 #include <linux/backlight.h>
15 #include <linux/mfd/max8925.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 
19 #define MAX_BRIGHTNESS		(0xff)
20 #define MIN_BRIGHTNESS		(0)
21 
22 #define LWX_FREQ(x)		(((x - 601) / 100) & 0x7)
23 
24 struct max8925_backlight_data {
25 	struct max8925_chip	*chip;
26 
27 	int	current_brightness;
28 	int	reg_mode_cntl;
29 	int	reg_cntl;
30 };
31 
32 static int max8925_backlight_set(struct backlight_device *bl, int brightness)
33 {
34 	struct max8925_backlight_data *data = bl_get_data(bl);
35 	struct max8925_chip *chip = data->chip;
36 	unsigned char value;
37 	int ret;
38 
39 	if (brightness > MAX_BRIGHTNESS)
40 		value = MAX_BRIGHTNESS;
41 	else
42 		value = brightness;
43 
44 	ret = max8925_reg_write(chip->i2c, data->reg_cntl, value);
45 	if (ret < 0)
46 		goto out;
47 
48 	if (!data->current_brightness && brightness)
49 		/* enable WLED output */
50 		ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 1);
51 	else if (!brightness)
52 		/* disable WLED output */
53 		ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 0);
54 	if (ret < 0)
55 		goto out;
56 	dev_dbg(chip->dev, "set brightness %d\n", value);
57 	data->current_brightness = value;
58 	return 0;
59 out:
60 	dev_dbg(chip->dev, "set brightness %d failure with return value:%d\n",
61 		value, ret);
62 	return ret;
63 }
64 
65 static int max8925_backlight_update_status(struct backlight_device *bl)
66 {
67 	return max8925_backlight_set(bl, backlight_get_brightness(bl));
68 }
69 
70 static int max8925_backlight_get_brightness(struct backlight_device *bl)
71 {
72 	struct max8925_backlight_data *data = bl_get_data(bl);
73 	struct max8925_chip *chip = data->chip;
74 	int ret;
75 
76 	ret = max8925_reg_read(chip->i2c, data->reg_cntl);
77 	if (ret < 0)
78 		return -EINVAL;
79 	data->current_brightness = ret;
80 	dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
81 	return ret;
82 }
83 
84 static const struct backlight_ops max8925_backlight_ops = {
85 	.options	= BL_CORE_SUSPENDRESUME,
86 	.update_status	= max8925_backlight_update_status,
87 	.get_brightness	= max8925_backlight_get_brightness,
88 };
89 
90 static void max8925_backlight_dt_init(struct platform_device *pdev)
91 {
92 	struct device_node *nproot = pdev->dev.parent->of_node, *np;
93 	struct max8925_backlight_pdata *pdata;
94 	u32 val;
95 
96 	if (!nproot || !IS_ENABLED(CONFIG_OF))
97 		return;
98 
99 	pdata = devm_kzalloc(&pdev->dev,
100 			     sizeof(struct max8925_backlight_pdata),
101 			     GFP_KERNEL);
102 	if (!pdata)
103 		return;
104 
105 	np = of_get_child_by_name(nproot, "backlight");
106 	if (!np) {
107 		dev_err(&pdev->dev, "failed to find backlight node\n");
108 		return;
109 	}
110 
111 	if (!of_property_read_u32(np, "maxim,max8925-dual-string", &val))
112 		pdata->dual_string = val;
113 
114 	of_node_put(np);
115 
116 	pdev->dev.platform_data = pdata;
117 }
118 
119 static int max8925_backlight_probe(struct platform_device *pdev)
120 {
121 	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
122 	struct max8925_backlight_pdata *pdata;
123 	struct max8925_backlight_data *data;
124 	struct backlight_device *bl;
125 	struct backlight_properties props;
126 	struct resource *res;
127 	unsigned char value;
128 	int ret = 0;
129 
130 	data = devm_kzalloc(&pdev->dev, sizeof(struct max8925_backlight_data),
131 			    GFP_KERNEL);
132 	if (data == NULL)
133 		return -ENOMEM;
134 
135 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
136 	if (!res) {
137 		dev_err(&pdev->dev, "No REG resource for mode control!\n");
138 		return -ENXIO;
139 	}
140 	data->reg_mode_cntl = res->start;
141 	res = platform_get_resource(pdev, IORESOURCE_REG, 1);
142 	if (!res) {
143 		dev_err(&pdev->dev, "No REG resource for control!\n");
144 		return -ENXIO;
145 	}
146 	data->reg_cntl = res->start;
147 
148 	data->chip = chip;
149 	data->current_brightness = 0;
150 
151 	memset(&props, 0, sizeof(struct backlight_properties));
152 	props.type = BACKLIGHT_RAW;
153 	props.max_brightness = MAX_BRIGHTNESS;
154 	bl = devm_backlight_device_register(&pdev->dev, "max8925-backlight",
155 					&pdev->dev, data,
156 					&max8925_backlight_ops, &props);
157 	if (IS_ERR(bl)) {
158 		dev_err(&pdev->dev, "failed to register backlight\n");
159 		return PTR_ERR(bl);
160 	}
161 	bl->props.brightness = MAX_BRIGHTNESS;
162 
163 	platform_set_drvdata(pdev, bl);
164 
165 	value = 0;
166 	if (!pdev->dev.platform_data)
167 		max8925_backlight_dt_init(pdev);
168 
169 	pdata = pdev->dev.platform_data;
170 	if (pdata) {
171 		if (pdata->lxw_scl)
172 			value |= (1 << 7);
173 		if (pdata->lxw_freq)
174 			value |= (LWX_FREQ(pdata->lxw_freq) << 4);
175 		if (pdata->dual_string)
176 			value |= (1 << 1);
177 	}
178 	ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 0xfe, value);
179 	if (ret < 0)
180 		return ret;
181 	backlight_update_status(bl);
182 	return 0;
183 }
184 
185 static struct platform_driver max8925_backlight_driver = {
186 	.driver		= {
187 		.name	= "max8925-backlight",
188 	},
189 	.probe		= max8925_backlight_probe,
190 };
191 
192 module_platform_driver(max8925_backlight_driver);
193 
194 MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
195 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
196 MODULE_LICENSE("GPL");
197 MODULE_ALIAS("platform:max8925-backlight");
198