blob: 2f38c5b338308e2d1b5d40f99f3e718f1960fc24 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +09002/*
3 * LP55XX Common Driver Header
4 *
5 * Copyright (C) 2012 Texas Instruments
6 *
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 *
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +09009 * Derived from leds-lp5521.c, leds-lp5523.c
10 */
11
12#ifndef _LEDS_LP55XX_COMMON_H
13#define _LEDS_LP55XX_COMMON_H
14
Dan Murphy92a81562020-07-16 13:20:01 -050015#include <linux/led-class-multicolor.h>
16
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +090017enum lp55xx_engine_index {
18 LP55XX_ENGINE_INVALID,
19 LP55XX_ENGINE_1,
20 LP55XX_ENGINE_2,
21 LP55XX_ENGINE_3,
Milo Kim6841a912013-08-08 12:45:41 +090022 LP55XX_ENGINE_MAX = LP55XX_ENGINE_3,
23};
24
25enum lp55xx_engine_mode {
26 LP55XX_ENGINE_DISABLED,
27 LP55XX_ENGINE_LOAD,
28 LP55XX_ENGINE_RUN,
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +090029};
30
Milo Kim36030972013-08-08 12:46:43 +090031#define LP55XX_DEV_ATTR_RW(name, show, store) \
32 DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store)
33#define LP55XX_DEV_ATTR_RO(name, show) \
34 DEVICE_ATTR(name, S_IRUGO, show, NULL)
35#define LP55XX_DEV_ATTR_WO(name, store) \
36 DEVICE_ATTR(name, S_IWUSR, NULL, store)
37
38#define show_mode(nr) \
39static ssize_t show_engine##nr##_mode(struct device *dev, \
40 struct device_attribute *attr, \
41 char *buf) \
42{ \
43 return show_engine_mode(dev, attr, buf, nr); \
44}
45
46#define store_mode(nr) \
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 store_engine_mode(dev, attr, buf, len, nr); \
52}
53
54#define show_leds(nr) \
55static ssize_t show_engine##nr##_leds(struct device *dev, \
56 struct device_attribute *attr, \
57 char *buf) \
58{ \
59 return show_engine_leds(dev, attr, buf, nr); \
60}
61
62#define store_leds(nr) \
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 store_engine_leds(dev, attr, buf, len, nr); \
68}
69
70#define store_load(nr) \
71static ssize_t store_engine##nr##_load(struct device *dev, \
72 struct device_attribute *attr, \
73 const char *buf, size_t len) \
74{ \
75 return store_engine_load(dev, attr, buf, len, nr); \
76}
77
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +090078struct lp55xx_led;
79struct lp55xx_chip;
80
81/*
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090082 * struct lp55xx_reg
83 * @addr : Register address
84 * @val : Register value
85 */
86struct lp55xx_reg {
87 u8 addr;
88 u8 val;
89};
90
91/*
92 * struct lp55xx_device_config
93 * @reset : Chip specific reset command
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +090094 * @enable : Chip specific enable command
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +090095 * @max_channel : Maximum number of channels
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +090096 * @post_init_device : Chip specific initialization code
Andrew Lunn95b2af62015-08-20 12:22:57 +020097 * @brightness_fn : Brightness function
Dan Murphy92a81562020-07-16 13:20:01 -050098 * @multicolor_brightness_fn : Multicolor brightness function
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +090099 * @set_led_current : LED current set function
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900100 * @firmware_cb : Call function when the firmware is loaded
101 * @run_engine : Run internal engine for pattern
Milo(Woogyom) Kim240085e2013-02-05 19:20:01 +0900102 * @dev_attr_group : Device specific attributes
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900103 */
104struct lp55xx_device_config {
105 const struct lp55xx_reg reset;
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900106 const struct lp55xx_reg enable;
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900107 const int max_channel;
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900108
109 /* define if the device has specific initialization process */
110 int (*post_init_device) (struct lp55xx_chip *chip);
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900111
Dan Murphy92a81562020-07-16 13:20:01 -0500112 /* set LED brightness */
Andrew Lunn95b2af62015-08-20 12:22:57 +0200113 int (*brightness_fn)(struct lp55xx_led *led);
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900114
Dan Murphy92a81562020-07-16 13:20:01 -0500115 /* set multicolor LED brightness */
116 int (*multicolor_brightness_fn)(struct lp55xx_led *led);
117
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900118 /* current setting function */
119 void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900120
121 /* access program memory when the firmware is loaded */
122 void (*firmware_cb)(struct lp55xx_chip *chip);
123
124 /* used for running firmware LED patterns */
125 void (*run_engine) (struct lp55xx_chip *chip, bool start);
Milo(Woogyom) Kim240085e2013-02-05 19:20:01 +0900126
127 /* additional device specific attributes */
128 const struct attribute_group *dev_attr_group;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900129};
130
131/*
Milo Kim6841a912013-08-08 12:45:41 +0900132 * struct lp55xx_engine
133 * @mode : Engine mode
134 * @led_mux : Mux bits for LED selection. Only used in LP5523
135 */
136struct lp55xx_engine {
137 enum lp55xx_engine_mode mode;
138 u16 led_mux;
139};
140
141/*
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900142 * struct lp55xx_chip
143 * @cl : I2C communication for access registers
144 * @pdata : Platform specific data
145 * @lock : Lock for user-space interface
146 * @num_leds : Number of registered LEDs
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900147 * @cfg : Device specific configuration data
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900148 * @engine_idx : Selected engine number
Milo Kim6841a912013-08-08 12:45:41 +0900149 * @engines : Engine structure for the device attribute R/W interface
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900150 * @fw : Firmware data for running a LED pattern
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900151 */
152struct lp55xx_chip {
153 struct i2c_client *cl;
Kim, Milo53b41922013-03-20 17:37:00 -0700154 struct clk *clk;
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900155 struct lp55xx_platform_data *pdata;
156 struct mutex lock; /* lock for user-space interface */
157 int num_leds;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900158 struct lp55xx_device_config *cfg;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900159 enum lp55xx_engine_index engine_idx;
Milo Kim6841a912013-08-08 12:45:41 +0900160 struct lp55xx_engine engines[LP55XX_ENGINE_MAX];
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900161 const struct firmware *fw;
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900162};
163
164/*
165 * struct lp55xx_led
166 * @chan_nr : Channel number
167 * @cdev : LED class device
Dan Murphy92a81562020-07-16 13:20:01 -0500168 * @mc_cdev : Multi color class device
169 * @color_components: Multi color LED map information
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900170 * @led_current : Current setting at each led channel
171 * @max_current : Maximun current at each led channel
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900172 * @brightness : Brightness value
173 * @chip : The lp55xx chip data
174 */
175struct lp55xx_led {
176 int chan_nr;
177 struct led_classdev cdev;
Dan Murphy92a81562020-07-16 13:20:01 -0500178 struct led_classdev_mc mc_cdev;
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900179 u8 led_current;
180 u8 max_current;
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900181 u8 brightness;
182 struct lp55xx_chip *chip;
183};
184
185/* register access */
186extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val);
187extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val);
188extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
189 u8 mask, u8 val);
190
Kim, Milo53b41922013-03-20 17:37:00 -0700191/* external clock detection */
192extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
193
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900194/* common device init/deinit functions */
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900195extern int lp55xx_init_device(struct lp55xx_chip *chip);
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900196extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900197
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900198/* common LED class device functions */
199extern int lp55xx_register_leds(struct lp55xx_led *led,
200 struct lp55xx_chip *chip);
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900201
202/* common device attributes functions */
203extern int lp55xx_register_sysfs(struct lp55xx_chip *chip);
Milo(Woogyom) Kimba6fa842013-02-05 19:23:04 +0900204extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip);
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900205
Linus Walleij7542a04b2013-04-23 04:52:59 -0700206/* common device tree population function */
Milo Kimed1333522015-08-24 16:09:55 +0900207extern struct lp55xx_platform_data
Dan Murphy92a81562020-07-16 13:20:01 -0500208*lp55xx_of_populate_pdata(struct device *dev, struct device_node *np,
209 struct lp55xx_chip *chip);
Linus Walleij7542a04b2013-04-23 04:52:59 -0700210
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900211#endif /* _LEDS_LP55XX_COMMON_H */