xref: /linux/drivers/leds/leds-lp8501.c (revision a3df1906fb9aa9ff45149e0a3c6434b2cef4f6e7)
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 int lp8501_led_brightness(struct lp55xx_led *led)
141 {
142 	struct lp55xx_chip *chip = led->chip;
143 	int ret;
144 
145 	mutex_lock(&chip->lock);
146 	ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
147 		     led->brightness);
148 	mutex_unlock(&chip->lock);
149 
150 	return ret;
151 }
152 
153 /* Chip specific configurations */
154 static struct lp55xx_device_config lp8501_cfg = {
155 	.reg_op_mode = {
156 		.addr = LP8501_REG_OP_MODE,
157 	},
158 	.reg_exec = {
159 		.addr = LP8501_REG_ENABLE,
160 	},
161 	.engine_busy = {
162 		.addr = LP8501_REG_STATUS,
163 		.mask = LP8501_ENGINE_BUSY,
164 	},
165 	.reset = {
166 		.addr = LP8501_REG_RESET,
167 		.val  = LP8501_RESET,
168 	},
169 	.enable = {
170 		.addr = LP8501_REG_ENABLE,
171 		.val  = LP8501_ENABLE,
172 	},
173 	.prog_mem_base = {
174 		.addr = LP8501_REG_PROG_MEM,
175 	},
176 	.pages_per_engine   = LP8501_PAGES_PER_ENGINE,
177 	.max_channel  = LP8501_MAX_LEDS,
178 	.post_init_device   = lp8501_post_init_device,
179 	.brightness_fn      = lp8501_led_brightness,
180 	.set_led_current    = lp8501_set_led_current,
181 	.firmware_cb        = lp55xx_firmware_loaded_cb,
182 	.run_engine         = lp8501_run_engine,
183 };
184 
185 static const struct i2c_device_id lp8501_id[] = {
186 	{ "lp8501",  .driver_data = (kernel_ulong_t)&lp8501_cfg, },
187 	{ }
188 };
189 MODULE_DEVICE_TABLE(i2c, lp8501_id);
190 
191 static const struct of_device_id of_lp8501_leds_match[] = {
192 	{ .compatible = "ti,lp8501", .data = &lp8501_cfg, },
193 	{},
194 };
195 
196 MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
197 
198 static struct i2c_driver lp8501_driver = {
199 	.driver = {
200 		.name	= "lp8501",
201 		.of_match_table = of_lp8501_leds_match,
202 	},
203 	.probe		= lp55xx_probe,
204 	.remove		= lp55xx_remove,
205 	.id_table	= lp8501_id,
206 };
207 
208 module_i2c_driver(lp8501_driver);
209 
210 MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver");
211 MODULE_AUTHOR("Milo Kim");
212 MODULE_LICENSE("GPL");
213