Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
c93d08fa MWK |
2 | /* |
3 | * LP55XX Common Driver Header | |
4 | * | |
5 | * Copyright (C) 2012 Texas Instruments | |
6 | * | |
7 | * Author: Milo(Woogyom) Kim <milo.kim@ti.com> | |
8 | * | |
c93d08fa MWK |
9 | * Derived from leds-lp5521.c, leds-lp5523.c |
10 | */ | |
11 | ||
12 | #ifndef _LEDS_LP55XX_COMMON_H | |
13 | #define _LEDS_LP55XX_COMMON_H | |
14 | ||
92a81562 DM |
15 | #include <linux/led-class-multicolor.h> |
16 | ||
b9d55087 CM |
17 | #define LP55xx_BYTES_PER_PAGE 32 /* bytes */ |
18 | ||
10c06d17 MWK |
19 | enum lp55xx_engine_index { |
20 | LP55XX_ENGINE_INVALID, | |
21 | LP55XX_ENGINE_1, | |
22 | LP55XX_ENGINE_2, | |
23 | LP55XX_ENGINE_3, | |
6841a91d MK |
24 | LP55XX_ENGINE_MAX = LP55XX_ENGINE_3, |
25 | }; | |
26 | ||
27 | enum lp55xx_engine_mode { | |
28 | LP55XX_ENGINE_DISABLED, | |
29 | LP55XX_ENGINE_LOAD, | |
30 | LP55XX_ENGINE_RUN, | |
10c06d17 MWK |
31 | }; |
32 | ||
36030978 MK |
33 | #define LP55XX_DEV_ATTR_RW(name, show, store) \ |
34 | DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store) | |
35 | #define LP55XX_DEV_ATTR_RO(name, show) \ | |
36 | DEVICE_ATTR(name, S_IRUGO, show, NULL) | |
37 | #define LP55XX_DEV_ATTR_WO(name, store) \ | |
38 | DEVICE_ATTR(name, S_IWUSR, NULL, store) | |
39 | ||
082a4d3f | 40 | #define LP55XX_DEV_ATTR_ENGINE_MODE(nr) \ |
36030978 | 41 | static ssize_t show_engine##nr##_mode(struct device *dev, \ |
082a4d3f CM |
42 | struct device_attribute *attr, \ |
43 | char *buf) \ | |
36030978 | 44 | { \ |
082a4d3f CM |
45 | return lp55xx_show_engine_mode(dev, attr, buf, nr); \ |
46 | } \ | |
36030978 | 47 | static ssize_t store_engine##nr##_mode(struct device *dev, \ |
082a4d3f CM |
48 | struct device_attribute *attr, \ |
49 | const char *buf, size_t len) \ | |
36030978 | 50 | { \ |
082a4d3f CM |
51 | return lp55xx_store_engine_mode(dev, attr, buf, len, nr); \ |
52 | } \ | |
53 | static LP55XX_DEV_ATTR_RW(engine##nr##_mode, show_engine##nr##_mode, \ | |
54 | store_engine##nr##_mode) | |
36030978 | 55 | |
8913c2c1 | 56 | #define LP55XX_DEV_ATTR_ENGINE_LEDS(nr) \ |
36030978 | 57 | static ssize_t show_engine##nr##_leds(struct device *dev, \ |
8913c2c1 CM |
58 | struct device_attribute *attr, \ |
59 | char *buf) \ | |
36030978 | 60 | { \ |
8913c2c1 CM |
61 | return lp55xx_show_engine_leds(dev, attr, buf, nr); \ |
62 | } \ | |
63 | static ssize_t store_engine##nr##_leds(struct device *dev, \ | |
64 | struct device_attribute *attr, \ | |
65 | const char *buf, size_t len) \ | |
66 | { \ | |
67 | return lp55xx_store_engine_leds(dev, attr, buf, len, nr); \ | |
68 | } \ | |
69 | static LP55XX_DEV_ATTR_RW(engine##nr##_leds, show_engine##nr##_leds, \ | |
70 | store_engine##nr##_leds) | |
36030978 | 71 | |
082a4d3f | 72 | #define LP55XX_DEV_ATTR_ENGINE_LOAD(nr) \ |
36030978 | 73 | static ssize_t store_engine##nr##_load(struct device *dev, \ |
082a4d3f CM |
74 | struct device_attribute *attr, \ |
75 | const char *buf, size_t len) \ | |
36030978 | 76 | { \ |
082a4d3f CM |
77 | return lp55xx_store_engine_load(dev, attr, buf, len, nr); \ |
78 | } \ | |
79 | static LP55XX_DEV_ATTR_WO(engine##nr##_load, store_engine##nr##_load) | |
36030978 | 80 | |
5a15b2ab CM |
81 | #define LP55XX_DEV_ATTR_MASTER_FADER(nr) \ |
82 | static ssize_t show_master_fader##nr(struct device *dev, \ | |
83 | struct device_attribute *attr, \ | |
84 | char *buf) \ | |
85 | { \ | |
86 | return lp55xx_show_master_fader(dev, attr, buf, nr); \ | |
87 | } \ | |
88 | static ssize_t store_master_fader##nr(struct device *dev, \ | |
89 | struct device_attribute *attr, \ | |
90 | const char *buf, size_t len) \ | |
91 | { \ | |
92 | return lp55xx_store_master_fader(dev, attr, buf, len, nr); \ | |
93 | } \ | |
94 | static LP55XX_DEV_ATTR_RW(master_fader##nr, show_master_fader##nr, \ | |
95 | store_master_fader##nr) | |
96 | ||
c93d08fa MWK |
97 | struct lp55xx_led; |
98 | struct lp55xx_chip; | |
99 | ||
48068d5d MWK |
100 | /* |
101 | * struct lp55xx_reg | |
102 | * @addr : Register address | |
a9b202b9 | 103 | * @val : Register value (can also used as mask or shift) |
48068d5d MWK |
104 | */ |
105 | struct lp55xx_reg { | |
106 | u8 addr; | |
a9b202b9 CM |
107 | union { |
108 | u8 val; | |
109 | u8 mask; | |
110 | u8 shift; | |
111 | }; | |
48068d5d MWK |
112 | }; |
113 | ||
114 | /* | |
115 | * struct lp55xx_device_config | |
a9b202b9 CM |
116 | * @reg_op_mode : Chip specific OP MODE reg addr |
117 | * @engine_busy : Chip specific engine busy | |
118 | * (if not supported 153 us sleep) | |
48068d5d | 119 | * @reset : Chip specific reset command |
e3a700d8 | 120 | * @enable : Chip specific enable command |
31379a57 | 121 | * @prog_mem_base : Chip specific base reg address for chip SMEM programming |
c63580b2 | 122 | * @reg_led_pwm_base : Chip specific base reg address for LED PWM conf |
01e0290d | 123 | * @reg_led_current_base : Chip specific base reg address for LED current conf |
5a15b2ab CM |
124 | * @reg_master_fader_base : Chip specific base reg address for master fader base |
125 | * @reg_led_ctrl_base : Chip specific base reg address for LED ctrl base | |
409a9dc5 CM |
126 | * @pages_per_engine : Assigned pages for each engine |
127 | * (if not set chip doesn't support pages) | |
0e202346 | 128 | * @max_channel : Maximum number of channels |
ffbdccdb | 129 | * @post_init_device : Chip specific initialization code |
95b2af63 | 130 | * @brightness_fn : Brightness function |
92a81562 | 131 | * @multicolor_brightness_fn : Multicolor brightness function |
9e9b3db1 | 132 | * @set_led_current : LED current set function |
10c06d17 MWK |
133 | * @firmware_cb : Call function when the firmware is loaded |
134 | * @run_engine : Run internal engine for pattern | |
240085e2 | 135 | * @dev_attr_group : Device specific attributes |
48068d5d MWK |
136 | */ |
137 | struct lp55xx_device_config { | |
a9b202b9 | 138 | const struct lp55xx_reg reg_op_mode; /* addr, shift */ |
42a9eaac | 139 | const struct lp55xx_reg reg_exec; /* addr, shift */ |
a9b202b9 | 140 | const struct lp55xx_reg engine_busy; /* addr, mask */ |
48068d5d | 141 | const struct lp55xx_reg reset; |
e3a700d8 | 142 | const struct lp55xx_reg enable; |
31379a57 | 143 | const struct lp55xx_reg prog_mem_base; |
c63580b2 | 144 | const struct lp55xx_reg reg_led_pwm_base; |
01e0290d | 145 | const struct lp55xx_reg reg_led_current_base; |
5a15b2ab CM |
146 | const struct lp55xx_reg reg_master_fader_base; |
147 | const struct lp55xx_reg reg_led_ctrl_base; | |
409a9dc5 | 148 | const int pages_per_engine; |
0e202346 | 149 | const int max_channel; |
ffbdccdb MWK |
150 | |
151 | /* define if the device has specific initialization process */ | |
152 | int (*post_init_device) (struct lp55xx_chip *chip); | |
9e9b3db1 | 153 | |
92a81562 | 154 | /* set LED brightness */ |
95b2af63 | 155 | int (*brightness_fn)(struct lp55xx_led *led); |
9e9b3db1 | 156 | |
92a81562 DM |
157 | /* set multicolor LED brightness */ |
158 | int (*multicolor_brightness_fn)(struct lp55xx_led *led); | |
159 | ||
9e9b3db1 MWK |
160 | /* current setting function */ |
161 | void (*set_led_current) (struct lp55xx_led *led, u8 led_current); | |
10c06d17 MWK |
162 | |
163 | /* access program memory when the firmware is loaded */ | |
164 | void (*firmware_cb)(struct lp55xx_chip *chip); | |
165 | ||
166 | /* used for running firmware LED patterns */ | |
167 | void (*run_engine) (struct lp55xx_chip *chip, bool start); | |
240085e2 MWK |
168 | |
169 | /* additional device specific attributes */ | |
170 | const struct attribute_group *dev_attr_group; | |
48068d5d MWK |
171 | }; |
172 | ||
6841a91d MK |
173 | /* |
174 | * struct lp55xx_engine | |
175 | * @mode : Engine mode | |
176 | * @led_mux : Mux bits for LED selection. Only used in LP5523 | |
177 | */ | |
178 | struct lp55xx_engine { | |
179 | enum lp55xx_engine_mode mode; | |
180 | u16 led_mux; | |
181 | }; | |
182 | ||
c93d08fa MWK |
183 | /* |
184 | * struct lp55xx_chip | |
185 | * @cl : I2C communication for access registers | |
186 | * @pdata : Platform specific data | |
187 | * @lock : Lock for user-space interface | |
188 | * @num_leds : Number of registered LEDs | |
48068d5d | 189 | * @cfg : Device specific configuration data |
10c06d17 | 190 | * @engine_idx : Selected engine number |
6841a91d | 191 | * @engines : Engine structure for the device attribute R/W interface |
10c06d17 | 192 | * @fw : Firmware data for running a LED pattern |
c93d08fa MWK |
193 | */ |
194 | struct lp55xx_chip { | |
195 | struct i2c_client *cl; | |
196 | struct lp55xx_platform_data *pdata; | |
197 | struct mutex lock; /* lock for user-space interface */ | |
198 | int num_leds; | |
db30c289 | 199 | const struct lp55xx_device_config *cfg; |
10c06d17 | 200 | enum lp55xx_engine_index engine_idx; |
6841a91d | 201 | struct lp55xx_engine engines[LP55XX_ENGINE_MAX]; |
10c06d17 | 202 | const struct firmware *fw; |
c93d08fa MWK |
203 | }; |
204 | ||
205 | /* | |
206 | * struct lp55xx_led | |
207 | * @chan_nr : Channel number | |
208 | * @cdev : LED class device | |
92a81562 DM |
209 | * @mc_cdev : Multi color class device |
210 | * @color_components: Multi color LED map information | |
c93d08fa MWK |
211 | * @led_current : Current setting at each led channel |
212 | * @max_current : Maximun current at each led channel | |
c93d08fa MWK |
213 | * @brightness : Brightness value |
214 | * @chip : The lp55xx chip data | |
215 | */ | |
216 | struct lp55xx_led { | |
217 | int chan_nr; | |
218 | struct led_classdev cdev; | |
92a81562 | 219 | struct led_classdev_mc mc_cdev; |
c93d08fa MWK |
220 | u8 led_current; |
221 | u8 max_current; | |
c93d08fa MWK |
222 | u8 brightness; |
223 | struct lp55xx_chip *chip; | |
224 | }; | |
225 | ||
226 | /* register access */ | |
227 | extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val); | |
228 | extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val); | |
229 | extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, | |
230 | u8 mask, u8 val); | |
231 | ||
53b41922 KM |
232 | /* external clock detection */ |
233 | extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip); | |
234 | ||
a9b202b9 CM |
235 | /* common chip functions */ |
236 | extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip); | |
4d310b96 | 237 | extern void lp55xx_load_engine(struct lp55xx_chip *chip); |
42a9eaac | 238 | extern int lp55xx_run_engine_common(struct lp55xx_chip *chip); |
31379a57 CM |
239 | extern int lp55xx_update_program_memory(struct lp55xx_chip *chip, |
240 | const u8 *data, size_t size); | |
a3df1906 | 241 | extern void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip); |
c63580b2 | 242 | extern int lp55xx_led_brightness(struct lp55xx_led *led); |
794826b2 | 243 | extern int lp55xx_multicolor_brightness(struct lp55xx_led *led); |
01e0290d | 244 | extern void lp55xx_set_led_current(struct lp55xx_led *led, u8 led_current); |
e35bc5d8 | 245 | extern void lp55xx_turn_off_channels(struct lp55xx_chip *chip); |
43e91e5e | 246 | extern void lp55xx_stop_engine(struct lp55xx_chip *chip); |
a9b202b9 | 247 | |
db30c289 CM |
248 | /* common probe/remove function */ |
249 | extern int lp55xx_probe(struct i2c_client *client); | |
250 | extern void lp55xx_remove(struct i2c_client *client); | |
7542a04b | 251 | |
082a4d3f CM |
252 | /* common sysfs function */ |
253 | extern ssize_t lp55xx_show_engine_mode(struct device *dev, | |
254 | struct device_attribute *attr, | |
255 | char *buf, int nr); | |
256 | extern ssize_t lp55xx_store_engine_mode(struct device *dev, | |
257 | struct device_attribute *attr, | |
258 | const char *buf, size_t len, int nr); | |
259 | extern ssize_t lp55xx_store_engine_load(struct device *dev, | |
260 | struct device_attribute *attr, | |
261 | const char *buf, size_t len, int nr); | |
8913c2c1 CM |
262 | extern ssize_t lp55xx_show_engine_leds(struct device *dev, |
263 | struct device_attribute *attr, | |
264 | char *buf, int nr); | |
265 | extern ssize_t lp55xx_store_engine_leds(struct device *dev, | |
266 | struct device_attribute *attr, | |
267 | const char *buf, size_t len, int nr); | |
5a15b2ab CM |
268 | extern ssize_t lp55xx_show_master_fader(struct device *dev, |
269 | struct device_attribute *attr, | |
270 | char *buf, int nr); | |
271 | extern ssize_t lp55xx_store_master_fader(struct device *dev, | |
272 | struct device_attribute *attr, | |
273 | const char *buf, size_t len, int nr); | |
274 | extern ssize_t lp55xx_show_master_fader_leds(struct device *dev, | |
275 | struct device_attribute *attr, | |
276 | char *buf); | |
277 | extern ssize_t lp55xx_store_master_fader_leds(struct device *dev, | |
278 | struct device_attribute *attr, | |
279 | const char *buf, size_t len); | |
082a4d3f | 280 | |
c93d08fa | 281 | #endif /* _LEDS_LP55XX_COMMON_H */ |