leds-lp55xx-common.h 5.98 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-only */
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * LP55XX Common Driver Header
 *
 * Copyright (C) 2012 Texas Instruments
 *
 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
 *
 * Derived from leds-lp5521.c, leds-lp5523.c
 */

#ifndef _LEDS_LP55XX_COMMON_H
#define _LEDS_LP55XX_COMMON_H

15 16
#include <linux/led-class-multicolor.h>

17 18 19 20 21
enum lp55xx_engine_index {
	LP55XX_ENGINE_INVALID,
	LP55XX_ENGINE_1,
	LP55XX_ENGINE_2,
	LP55XX_ENGINE_3,
22 23 24 25 26 27 28
	LP55XX_ENGINE_MAX = LP55XX_ENGINE_3,
};

enum lp55xx_engine_mode {
	LP55XX_ENGINE_DISABLED,
	LP55XX_ENGINE_LOAD,
	LP55XX_ENGINE_RUN,
29 30
};

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#define LP55XX_DEV_ATTR_RW(name, show, store)	\
	DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store)
#define LP55XX_DEV_ATTR_RO(name, show)		\
	DEVICE_ATTR(name, S_IRUGO, show, NULL)
#define LP55XX_DEV_ATTR_WO(name, store)		\
	DEVICE_ATTR(name, S_IWUSR, NULL, store)

#define show_mode(nr)							\
static ssize_t show_engine##nr##_mode(struct device *dev,		\
				    struct device_attribute *attr,	\
				    char *buf)				\
{									\
	return show_engine_mode(dev, attr, buf, nr);			\
}

#define store_mode(nr)							\
static ssize_t store_engine##nr##_mode(struct device *dev,		\
				     struct device_attribute *attr,	\
				     const char *buf, size_t len)	\
{									\
	return store_engine_mode(dev, attr, buf, len, nr);		\
}

#define show_leds(nr)							\
static ssize_t show_engine##nr##_leds(struct device *dev,		\
			    struct device_attribute *attr,		\
			    char *buf)					\
{									\
	return show_engine_leds(dev, attr, buf, nr);			\
}

#define store_leds(nr)						\
static ssize_t store_engine##nr##_leds(struct device *dev,	\
			     struct device_attribute *attr,	\
			     const char *buf, size_t len)	\
{								\
	return store_engine_leds(dev, attr, buf, len, nr);	\
}

#define store_load(nr)							\
static ssize_t store_engine##nr##_load(struct device *dev,		\
				     struct device_attribute *attr,	\
				     const char *buf, size_t len)	\
{									\
	return store_engine_load(dev, attr, buf, len, nr);		\
}

78 79 80
struct lp55xx_led;
struct lp55xx_chip;

81 82 83 84 85 86 87 88 89 90 91 92 93
/*
 * struct lp55xx_reg
 * @addr : Register address
 * @val  : Register value
 */
struct lp55xx_reg {
	u8 addr;
	u8 val;
};

/*
 * struct lp55xx_device_config
 * @reset              : Chip specific reset command
94
 * @enable             : Chip specific enable command
95
 * @max_channel        : Maximum number of channels
96
 * @post_init_device   : Chip specific initialization code
97
 * @brightness_fn      : Brightness function
98
 * @multicolor_brightness_fn : Multicolor brightness function
99
 * @set_led_current    : LED current set function
100 101
 * @firmware_cb        : Call function when the firmware is loaded
 * @run_engine         : Run internal engine for pattern
102
 * @dev_attr_group     : Device specific attributes
103 104 105
 */
struct lp55xx_device_config {
	const struct lp55xx_reg reset;
106
	const struct lp55xx_reg enable;
107
	const int max_channel;
108 109 110

	/* define if the device has specific initialization process */
	int (*post_init_device) (struct lp55xx_chip *chip);
111

112
	/* set LED brightness */
113
	int (*brightness_fn)(struct lp55xx_led *led);
114

115 116 117
	/* set multicolor LED brightness */
	int (*multicolor_brightness_fn)(struct lp55xx_led *led);

118 119
	/* current setting function */
	void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
120 121 122 123 124 125

	/* access program memory when the firmware is loaded */
	void (*firmware_cb)(struct lp55xx_chip *chip);

	/* used for running firmware LED patterns */
	void (*run_engine) (struct lp55xx_chip *chip, bool start);
126 127 128

	/* additional device specific attributes */
	const struct attribute_group *dev_attr_group;
129 130
};

131 132 133 134 135 136 137 138 139 140
/*
 * struct lp55xx_engine
 * @mode       : Engine mode
 * @led_mux    : Mux bits for LED selection. Only used in LP5523
 */
struct lp55xx_engine {
	enum lp55xx_engine_mode mode;
	u16 led_mux;
};

141 142 143 144 145 146
/*
 * struct lp55xx_chip
 * @cl         : I2C communication for access registers
 * @pdata      : Platform specific data
 * @lock       : Lock for user-space interface
 * @num_leds   : Number of registered LEDs
147
 * @cfg        : Device specific configuration data
148
 * @engine_idx : Selected engine number
149
 * @engines    : Engine structure for the device attribute R/W interface
150
 * @fw         : Firmware data for running a LED pattern
151 152 153
 */
struct lp55xx_chip {
	struct i2c_client *cl;
154
	struct clk *clk;
155 156 157
	struct lp55xx_platform_data *pdata;
	struct mutex lock;	/* lock for user-space interface */
	int num_leds;
158
	struct lp55xx_device_config *cfg;
159
	enum lp55xx_engine_index engine_idx;
160
	struct lp55xx_engine engines[LP55XX_ENGINE_MAX];
161
	const struct firmware *fw;
162 163 164 165 166 167
};

/*
 * struct lp55xx_led
 * @chan_nr         : Channel number
 * @cdev            : LED class device
168 169
 * @mc_cdev         : Multi color class device
 * @color_components: Multi color LED map information
170 171 172 173 174 175 176 177
 * @led_current     : Current setting at each led channel
 * @max_current     : Maximun current at each led channel
 * @brightness      : Brightness value
 * @chip            : The lp55xx chip data
 */
struct lp55xx_led {
	int chan_nr;
	struct led_classdev cdev;
178
	struct led_classdev_mc mc_cdev;
179 180 181 182 183 184 185 186 187 188 189 190
	u8 led_current;
	u8 max_current;
	u8 brightness;
	struct lp55xx_chip *chip;
};

/* register access */
extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val);
extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val);
extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
			u8 mask, u8 val);

191 192 193
/* external clock detection */
extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);

194
/* common device init/deinit functions */
195
extern int lp55xx_init_device(struct lp55xx_chip *chip);
196
extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
197

198 199 200
/* common LED class device functions */
extern int lp55xx_register_leds(struct lp55xx_led *led,
				struct lp55xx_chip *chip);
201 202 203

/* common device attributes functions */
extern int lp55xx_register_sysfs(struct lp55xx_chip *chip);
204
extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip);
205

206
/* common device tree population function */
207
extern struct lp55xx_platform_data
208 209
*lp55xx_of_populate_pdata(struct device *dev, struct device_node *np,
			  struct lp55xx_chip *chip);
210

211
#endif /* _LEDS_LP55XX_COMMON_H */