xref: /linux/drivers/leds/leds-lp8501.c (revision 31379a57cf2f155eb147ace86547b7143592945a)
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_PAGES_PER_ENGINE		1
25 #define LP8501_MAX_LEDS			9
26 
27 /* Registers */
28 #define LP8501_REG_ENABLE		0x00
29 #define LP8501_ENABLE			BIT(6)
30 #define LP8501_EXEC_M			0x3F
31 #define LP8501_EXEC_ENG1_M		0x30
32 #define LP8501_EXEC_ENG2_M		0x0C
33 #define LP8501_EXEC_ENG3_M		0x03
34 #define LP8501_RUN_ENG1			0x20
35 #define LP8501_RUN_ENG2			0x08
36 #define LP8501_RUN_ENG3			0x02
37 
38 #define LP8501_REG_OP_MODE		0x01
39 #define LP8501_MODE_ENG1_M		0x30
40 #define LP8501_MODE_ENG2_M		0x0C
41 #define LP8501_MODE_ENG3_M		0x03
42 #define LP8501_LOAD_ENG1		0x10
43 #define LP8501_LOAD_ENG2		0x04
44 #define LP8501_LOAD_ENG3		0x01
45 
46 #define LP8501_REG_PWR_CONFIG		0x05
47 #define LP8501_PWR_CONFIG_M		0x03
48 
49 #define LP8501_REG_LED_PWM_BASE		0x16
50 
51 #define LP8501_REG_LED_CURRENT_BASE	0x26
52 
53 #define LP8501_REG_CONFIG		0x36
54 #define LP8501_PWM_PSAVE		BIT(7)
55 #define LP8501_AUTO_INC			BIT(6)
56 #define LP8501_PWR_SAVE			BIT(5)
57 #define LP8501_CP_MODE_MASK		0x18
58 #define LP8501_CP_MODE_SHIFT		3
59 #define LP8501_INT_CLK			BIT(0)
60 #define LP8501_DEFAULT_CFG (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE)
61 
62 #define LP8501_REG_STATUS		0x3A
63 #define LP8501_ENGINE_BUSY		BIT(4)
64 
65 #define LP8501_REG_RESET		0x3D
66 #define LP8501_RESET			0xFF
67 
68 #define LP8501_REG_PROG_PAGE_SEL	0x4F
69 #define LP8501_PAGE_ENG1		0
70 #define LP8501_PAGE_ENG2		1
71 #define LP8501_PAGE_ENG3		2
72 
73 #define LP8501_REG_PROG_MEM		0x50
74 
75 #define LP8501_ENG1_IS_LOADING(mode)	\
76 	((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
77 #define LP8501_ENG2_IS_LOADING(mode)	\
78 	((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
79 #define LP8501_ENG3_IS_LOADING(mode)	\
80 	((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
81 
82 static inline void lp8501_wait_opmode_done(void)
83 {
84 	usleep_range(1000, 2000);
85 }
86 
87 static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current)
88 {
89 	led->led_current = led_current;
90 	lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr,
91 		led_current);
92 }
93 
94 static int lp8501_post_init_device(struct lp55xx_chip *chip)
95 {
96 	int ret;
97 	u8 val = LP8501_DEFAULT_CFG;
98 
99 	ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
100 	if (ret)
101 		return ret;
102 
103 	/* Chip startup time is 500 us, 1 - 2 ms gives some margin */
104 	usleep_range(1000, 2000);
105 
106 	if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT)
107 		val |= LP8501_INT_CLK;
108 
109 	val |= (chip->pdata->charge_pump_mode << LP8501_CP_MODE_SHIFT) & LP8501_CP_MODE_MASK;
110 
111 	ret = lp55xx_write(chip, LP8501_REG_CONFIG, val);
112 	if (ret)
113 		return ret;
114 
115 	/* Power selection for each output */
116 	return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG,
117 				LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
118 }
119 
120 static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
121 {
122 	int i;
123 
124 	for (i = 0; i < LP8501_MAX_LEDS; i++)
125 		lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0);
126 }
127 
128 static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
129 {
130 	/* stop engine */
131 	if (!start) {
132 		lp55xx_stop_all_engine(chip);
133 		lp8501_turn_off_channels(chip);
134 		return;
135 	}
136 
137 	lp55xx_run_engine_common(chip);
138 }
139 
140 static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
141 {
142 	const struct firmware *fw = chip->fw;
143 
144 	if (fw->size > LP8501_PROGRAM_LENGTH) {
145 		dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
146 			fw->size);
147 		return;
148 	}
149 
150 	/*
151 	 * Program memory sequence
152 	 *  1) set engine mode to "LOAD"
153 	 *  2) write firmware data into program memory
154 	 */
155 
156 	lp55xx_load_engine(chip);
157 	lp55xx_update_program_memory(chip, fw->data, fw->size);
158 }
159 
160 static int lp8501_led_brightness(struct lp55xx_led *led)
161 {
162 	struct lp55xx_chip *chip = led->chip;
163 	int ret;
164 
165 	mutex_lock(&chip->lock);
166 	ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
167 		     led->brightness);
168 	mutex_unlock(&chip->lock);
169 
170 	return ret;
171 }
172 
173 /* Chip specific configurations */
174 static struct lp55xx_device_config lp8501_cfg = {
175 	.reg_op_mode = {
176 		.addr = LP8501_REG_OP_MODE,
177 	},
178 	.reg_exec = {
179 		.addr = LP8501_REG_ENABLE,
180 	},
181 	.engine_busy = {
182 		.addr = LP8501_REG_STATUS,
183 		.mask = LP8501_ENGINE_BUSY,
184 	},
185 	.reset = {
186 		.addr = LP8501_REG_RESET,
187 		.val  = LP8501_RESET,
188 	},
189 	.enable = {
190 		.addr = LP8501_REG_ENABLE,
191 		.val  = LP8501_ENABLE,
192 	},
193 	.prog_mem_base = {
194 		.addr = LP8501_REG_PROG_MEM,
195 	},
196 	.pages_per_engine   = LP8501_PAGES_PER_ENGINE,
197 	.max_channel  = LP8501_MAX_LEDS,
198 	.post_init_device   = lp8501_post_init_device,
199 	.brightness_fn      = lp8501_led_brightness,
200 	.set_led_current    = lp8501_set_led_current,
201 	.firmware_cb        = lp8501_firmware_loaded,
202 	.run_engine         = lp8501_run_engine,
203 };
204 
205 static const struct i2c_device_id lp8501_id[] = {
206 	{ "lp8501",  .driver_data = (kernel_ulong_t)&lp8501_cfg, },
207 	{ }
208 };
209 MODULE_DEVICE_TABLE(i2c, lp8501_id);
210 
211 static const struct of_device_id of_lp8501_leds_match[] = {
212 	{ .compatible = "ti,lp8501", .data = &lp8501_cfg, },
213 	{},
214 };
215 
216 MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
217 
218 static struct i2c_driver lp8501_driver = {
219 	.driver = {
220 		.name	= "lp8501",
221 		.of_match_table = of_lp8501_leds_match,
222 	},
223 	.probe		= lp55xx_probe,
224 	.remove		= lp55xx_remove,
225 	.id_table	= lp8501_id,
226 };
227 
228 module_i2c_driver(lp8501_driver);
229 
230 MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver");
231 MODULE_AUTHOR("Milo Kim");
232 MODULE_LICENSE("GPL");
233