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