xref: /linux/drivers/leds/leds-lp8501.c (revision 4d310b96f2db602830c40f82a75ede799b243cce)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI LP8501 9 channel LED Driver
4  *
5  * Copyright (C) 2013 Texas Instruments
6  *
7  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/firmware.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/of.h>
18 #include <linux/platform_data/leds-lp55xx.h>
19 #include <linux/slab.h>
20 
21 #include "leds-lp55xx-common.h"
22 
23 #define LP8501_PROGRAM_LENGTH		32
24 #define LP8501_MAX_LEDS			9
25 
26 /* Registers */
27 #define LP8501_REG_ENABLE		0x00
28 #define LP8501_ENABLE			BIT(6)
29 #define LP8501_EXEC_M			0x3F
30 #define LP8501_EXEC_ENG1_M		0x30
31 #define LP8501_EXEC_ENG2_M		0x0C
32 #define LP8501_EXEC_ENG3_M		0x03
33 #define LP8501_RUN_ENG1			0x20
34 #define LP8501_RUN_ENG2			0x08
35 #define LP8501_RUN_ENG3			0x02
36 
37 #define LP8501_REG_OP_MODE		0x01
38 #define LP8501_MODE_ENG1_M		0x30
39 #define LP8501_MODE_ENG2_M		0x0C
40 #define LP8501_MODE_ENG3_M		0x03
41 #define LP8501_LOAD_ENG1		0x10
42 #define LP8501_LOAD_ENG2		0x04
43 #define LP8501_LOAD_ENG3		0x01
44 
45 #define LP8501_REG_PWR_CONFIG		0x05
46 #define LP8501_PWR_CONFIG_M		0x03
47 
48 #define LP8501_REG_LED_PWM_BASE		0x16
49 
50 #define LP8501_REG_LED_CURRENT_BASE	0x26
51 
52 #define LP8501_REG_CONFIG		0x36
53 #define LP8501_PWM_PSAVE		BIT(7)
54 #define LP8501_AUTO_INC			BIT(6)
55 #define LP8501_PWR_SAVE			BIT(5)
56 #define LP8501_CP_MODE_MASK		0x18
57 #define LP8501_CP_MODE_SHIFT		3
58 #define LP8501_INT_CLK			BIT(0)
59 #define LP8501_DEFAULT_CFG (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE)
60 
61 #define LP8501_REG_STATUS		0x3A
62 #define LP8501_ENGINE_BUSY		BIT(4)
63 
64 #define LP8501_REG_RESET		0x3D
65 #define LP8501_RESET			0xFF
66 
67 #define LP8501_REG_PROG_PAGE_SEL	0x4F
68 #define LP8501_PAGE_ENG1		0
69 #define LP8501_PAGE_ENG2		1
70 #define LP8501_PAGE_ENG3		2
71 
72 #define LP8501_REG_PROG_MEM		0x50
73 
74 #define LP8501_ENG1_IS_LOADING(mode)	\
75 	((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
76 #define LP8501_ENG2_IS_LOADING(mode)	\
77 	((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
78 #define LP8501_ENG3_IS_LOADING(mode)	\
79 	((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
80 
81 static inline void lp8501_wait_opmode_done(void)
82 {
83 	usleep_range(1000, 2000);
84 }
85 
86 static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current)
87 {
88 	led->led_current = led_current;
89 	lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr,
90 		led_current);
91 }
92 
93 static int lp8501_post_init_device(struct lp55xx_chip *chip)
94 {
95 	int ret;
96 	u8 val = LP8501_DEFAULT_CFG;
97 
98 	ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
99 	if (ret)
100 		return ret;
101 
102 	/* Chip startup time is 500 us, 1 - 2 ms gives some margin */
103 	usleep_range(1000, 2000);
104 
105 	if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT)
106 		val |= LP8501_INT_CLK;
107 
108 	val |= (chip->pdata->charge_pump_mode << LP8501_CP_MODE_SHIFT) & LP8501_CP_MODE_MASK;
109 
110 	ret = lp55xx_write(chip, LP8501_REG_CONFIG, val);
111 	if (ret)
112 		return ret;
113 
114 	/* Power selection for each output */
115 	return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG,
116 				LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
117 }
118 
119 static void lp8501_load_engine(struct lp55xx_chip *chip)
120 {
121 	enum lp55xx_engine_index idx = chip->engine_idx;
122 
123 	static const u8 page_sel[] = {
124 		[LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
125 		[LP55XX_ENGINE_2] = LP8501_PAGE_ENG2,
126 		[LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
127 	};
128 
129 	lp55xx_load_engine(chip);
130 
131 	lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
132 }
133 
134 static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
135 {
136 	int i;
137 
138 	for (i = 0; i < LP8501_MAX_LEDS; i++)
139 		lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0);
140 }
141 
142 static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
143 {
144 	int ret;
145 	u8 mode;
146 	u8 exec;
147 
148 	/* stop engine */
149 	if (!start) {
150 		lp55xx_stop_all_engine(chip);
151 		lp8501_turn_off_channels(chip);
152 		return;
153 	}
154 
155 	/*
156 	 * To run the engine,
157 	 * operation mode and enable register should updated at the same time
158 	 */
159 
160 	ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
161 	if (ret)
162 		return;
163 
164 	ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
165 	if (ret)
166 		return;
167 
168 	/* change operation mode to RUN only when each engine is loading */
169 	if (LP8501_ENG1_IS_LOADING(mode)) {
170 		mode = (mode & ~LP8501_MODE_ENG1_M) | LP8501_RUN_ENG1;
171 		exec = (exec & ~LP8501_EXEC_ENG1_M) | LP8501_RUN_ENG1;
172 	}
173 
174 	if (LP8501_ENG2_IS_LOADING(mode)) {
175 		mode = (mode & ~LP8501_MODE_ENG2_M) | LP8501_RUN_ENG2;
176 		exec = (exec & ~LP8501_EXEC_ENG2_M) | LP8501_RUN_ENG2;
177 	}
178 
179 	if (LP8501_ENG3_IS_LOADING(mode)) {
180 		mode = (mode & ~LP8501_MODE_ENG3_M) | LP8501_RUN_ENG3;
181 		exec = (exec & ~LP8501_EXEC_ENG3_M) | LP8501_RUN_ENG3;
182 	}
183 
184 	lp55xx_write(chip, LP8501_REG_OP_MODE, mode);
185 	lp8501_wait_opmode_done();
186 
187 	lp55xx_update_bits(chip, LP8501_REG_ENABLE, LP8501_EXEC_M, exec);
188 }
189 
190 static int lp8501_update_program_memory(struct lp55xx_chip *chip,
191 					const u8 *data, size_t size)
192 {
193 	u8 pattern[LP8501_PROGRAM_LENGTH] = {0};
194 	unsigned cmd;
195 	char c[3];
196 	int update_size;
197 	int nrchars;
198 	int offset = 0;
199 	int ret;
200 	int i;
201 
202 	/* clear program memory before updating */
203 	for (i = 0; i < LP8501_PROGRAM_LENGTH; i++)
204 		lp55xx_write(chip, LP8501_REG_PROG_MEM + i, 0);
205 
206 	i = 0;
207 	while ((offset < size - 1) && (i < LP8501_PROGRAM_LENGTH)) {
208 		/* separate sscanfs because length is working only for %s */
209 		ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
210 		if (ret != 1)
211 			goto err;
212 
213 		ret = sscanf(c, "%2x", &cmd);
214 		if (ret != 1)
215 			goto err;
216 
217 		pattern[i] = (u8)cmd;
218 		offset += nrchars;
219 		i++;
220 	}
221 
222 	/* Each instruction is 16bit long. Check that length is even */
223 	if (i % 2)
224 		goto err;
225 
226 	update_size = i;
227 	for (i = 0; i < update_size; i++)
228 		lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
229 
230 	return 0;
231 
232 err:
233 	dev_err(&chip->cl->dev, "wrong pattern format\n");
234 	return -EINVAL;
235 }
236 
237 static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
238 {
239 	const struct firmware *fw = chip->fw;
240 
241 	if (fw->size > LP8501_PROGRAM_LENGTH) {
242 		dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
243 			fw->size);
244 		return;
245 	}
246 
247 	/*
248 	 * Program memory sequence
249 	 *  1) set engine mode to "LOAD"
250 	 *  2) write firmware data into program memory
251 	 */
252 
253 	lp8501_load_engine(chip);
254 	lp8501_update_program_memory(chip, fw->data, fw->size);
255 }
256 
257 static int lp8501_led_brightness(struct lp55xx_led *led)
258 {
259 	struct lp55xx_chip *chip = led->chip;
260 	int ret;
261 
262 	mutex_lock(&chip->lock);
263 	ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
264 		     led->brightness);
265 	mutex_unlock(&chip->lock);
266 
267 	return ret;
268 }
269 
270 /* Chip specific configurations */
271 static struct lp55xx_device_config lp8501_cfg = {
272 	.reg_op_mode = {
273 		.addr = LP8501_REG_OP_MODE,
274 	},
275 	.engine_busy = {
276 		.addr = LP8501_REG_STATUS,
277 		.mask = LP8501_ENGINE_BUSY,
278 	},
279 	.reset = {
280 		.addr = LP8501_REG_RESET,
281 		.val  = LP8501_RESET,
282 	},
283 	.enable = {
284 		.addr = LP8501_REG_ENABLE,
285 		.val  = LP8501_ENABLE,
286 	},
287 	.max_channel  = LP8501_MAX_LEDS,
288 	.post_init_device   = lp8501_post_init_device,
289 	.brightness_fn      = lp8501_led_brightness,
290 	.set_led_current    = lp8501_set_led_current,
291 	.firmware_cb        = lp8501_firmware_loaded,
292 	.run_engine         = lp8501_run_engine,
293 };
294 
295 static const struct i2c_device_id lp8501_id[] = {
296 	{ "lp8501",  .driver_data = (kernel_ulong_t)&lp8501_cfg, },
297 	{ }
298 };
299 MODULE_DEVICE_TABLE(i2c, lp8501_id);
300 
301 static const struct of_device_id of_lp8501_leds_match[] = {
302 	{ .compatible = "ti,lp8501", .data = &lp8501_cfg, },
303 	{},
304 };
305 
306 MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
307 
308 static struct i2c_driver lp8501_driver = {
309 	.driver = {
310 		.name	= "lp8501",
311 		.of_match_table = of_lp8501_leds_match,
312 	},
313 	.probe		= lp55xx_probe,
314 	.remove		= lp55xx_remove,
315 	.id_table	= lp8501_id,
316 };
317 
318 module_i2c_driver(lp8501_driver);
319 
320 MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver");
321 MODULE_AUTHOR("Milo Kim");
322 MODULE_LICENSE("GPL");
323