Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * LP55XX Common Driver Header
0004  *
0005  * Copyright (C) 2012 Texas Instruments
0006  *
0007  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
0008  *
0009  * Derived from leds-lp5521.c, leds-lp5523.c
0010  */
0011 
0012 #ifndef _LEDS_LP55XX_COMMON_H
0013 #define _LEDS_LP55XX_COMMON_H
0014 
0015 #include <linux/led-class-multicolor.h>
0016 
0017 enum lp55xx_engine_index {
0018     LP55XX_ENGINE_INVALID,
0019     LP55XX_ENGINE_1,
0020     LP55XX_ENGINE_2,
0021     LP55XX_ENGINE_3,
0022     LP55XX_ENGINE_MAX = LP55XX_ENGINE_3,
0023 };
0024 
0025 enum lp55xx_engine_mode {
0026     LP55XX_ENGINE_DISABLED,
0027     LP55XX_ENGINE_LOAD,
0028     LP55XX_ENGINE_RUN,
0029 };
0030 
0031 #define LP55XX_DEV_ATTR_RW(name, show, store)   \
0032     DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store)
0033 #define LP55XX_DEV_ATTR_RO(name, show)      \
0034     DEVICE_ATTR(name, S_IRUGO, show, NULL)
0035 #define LP55XX_DEV_ATTR_WO(name, store)     \
0036     DEVICE_ATTR(name, S_IWUSR, NULL, store)
0037 
0038 #define show_mode(nr)                           \
0039 static ssize_t show_engine##nr##_mode(struct device *dev,       \
0040                     struct device_attribute *attr,  \
0041                     char *buf)              \
0042 {                                   \
0043     return show_engine_mode(dev, attr, buf, nr);            \
0044 }
0045 
0046 #define store_mode(nr)                          \
0047 static ssize_t store_engine##nr##_mode(struct device *dev,      \
0048                      struct device_attribute *attr, \
0049                      const char *buf, size_t len)   \
0050 {                                   \
0051     return store_engine_mode(dev, attr, buf, len, nr);      \
0052 }
0053 
0054 #define show_leds(nr)                           \
0055 static ssize_t show_engine##nr##_leds(struct device *dev,       \
0056                 struct device_attribute *attr,      \
0057                 char *buf)                  \
0058 {                                   \
0059     return show_engine_leds(dev, attr, buf, nr);            \
0060 }
0061 
0062 #define store_leds(nr)                      \
0063 static ssize_t store_engine##nr##_leds(struct device *dev,  \
0064                  struct device_attribute *attr, \
0065                  const char *buf, size_t len)   \
0066 {                               \
0067     return store_engine_leds(dev, attr, buf, len, nr);  \
0068 }
0069 
0070 #define store_load(nr)                          \
0071 static ssize_t store_engine##nr##_load(struct device *dev,      \
0072                      struct device_attribute *attr, \
0073                      const char *buf, size_t len)   \
0074 {                                   \
0075     return store_engine_load(dev, attr, buf, len, nr);      \
0076 }
0077 
0078 struct lp55xx_led;
0079 struct lp55xx_chip;
0080 
0081 /*
0082  * struct lp55xx_reg
0083  * @addr : Register address
0084  * @val  : Register value
0085  */
0086 struct lp55xx_reg {
0087     u8 addr;
0088     u8 val;
0089 };
0090 
0091 /*
0092  * struct lp55xx_device_config
0093  * @reset              : Chip specific reset command
0094  * @enable             : Chip specific enable command
0095  * @max_channel        : Maximum number of channels
0096  * @post_init_device   : Chip specific initialization code
0097  * @brightness_fn      : Brightness function
0098  * @multicolor_brightness_fn : Multicolor brightness function
0099  * @set_led_current    : LED current set function
0100  * @firmware_cb        : Call function when the firmware is loaded
0101  * @run_engine         : Run internal engine for pattern
0102  * @dev_attr_group     : Device specific attributes
0103  */
0104 struct lp55xx_device_config {
0105     const struct lp55xx_reg reset;
0106     const struct lp55xx_reg enable;
0107     const int max_channel;
0108 
0109     /* define if the device has specific initialization process */
0110     int (*post_init_device) (struct lp55xx_chip *chip);
0111 
0112     /* set LED brightness */
0113     int (*brightness_fn)(struct lp55xx_led *led);
0114 
0115     /* set multicolor LED brightness */
0116     int (*multicolor_brightness_fn)(struct lp55xx_led *led);
0117 
0118     /* current setting function */
0119     void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
0120 
0121     /* access program memory when the firmware is loaded */
0122     void (*firmware_cb)(struct lp55xx_chip *chip);
0123 
0124     /* used for running firmware LED patterns */
0125     void (*run_engine) (struct lp55xx_chip *chip, bool start);
0126 
0127     /* additional device specific attributes */
0128     const struct attribute_group *dev_attr_group;
0129 };
0130 
0131 /*
0132  * struct lp55xx_engine
0133  * @mode       : Engine mode
0134  * @led_mux    : Mux bits for LED selection. Only used in LP5523
0135  */
0136 struct lp55xx_engine {
0137     enum lp55xx_engine_mode mode;
0138     u16 led_mux;
0139 };
0140 
0141 /*
0142  * struct lp55xx_chip
0143  * @cl         : I2C communication for access registers
0144  * @pdata      : Platform specific data
0145  * @lock       : Lock for user-space interface
0146  * @num_leds   : Number of registered LEDs
0147  * @cfg        : Device specific configuration data
0148  * @engine_idx : Selected engine number
0149  * @engines    : Engine structure for the device attribute R/W interface
0150  * @fw         : Firmware data for running a LED pattern
0151  */
0152 struct lp55xx_chip {
0153     struct i2c_client *cl;
0154     struct clk *clk;
0155     struct lp55xx_platform_data *pdata;
0156     struct mutex lock;  /* lock for user-space interface */
0157     int num_leds;
0158     struct lp55xx_device_config *cfg;
0159     enum lp55xx_engine_index engine_idx;
0160     struct lp55xx_engine engines[LP55XX_ENGINE_MAX];
0161     const struct firmware *fw;
0162 };
0163 
0164 /*
0165  * struct lp55xx_led
0166  * @chan_nr         : Channel number
0167  * @cdev            : LED class device
0168  * @mc_cdev         : Multi color class device
0169  * @color_components: Multi color LED map information
0170  * @led_current     : Current setting at each led channel
0171  * @max_current     : Maximun current at each led channel
0172  * @brightness      : Brightness value
0173  * @chip            : The lp55xx chip data
0174  */
0175 struct lp55xx_led {
0176     int chan_nr;
0177     struct led_classdev cdev;
0178     struct led_classdev_mc mc_cdev;
0179     u8 led_current;
0180     u8 max_current;
0181     u8 brightness;
0182     struct lp55xx_chip *chip;
0183 };
0184 
0185 /* register access */
0186 extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val);
0187 extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val);
0188 extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
0189             u8 mask, u8 val);
0190 
0191 /* external clock detection */
0192 extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
0193 
0194 /* common device init/deinit functions */
0195 extern int lp55xx_init_device(struct lp55xx_chip *chip);
0196 extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
0197 
0198 /* common LED class device functions */
0199 extern int lp55xx_register_leds(struct lp55xx_led *led,
0200                 struct lp55xx_chip *chip);
0201 
0202 /* common device attributes functions */
0203 extern int lp55xx_register_sysfs(struct lp55xx_chip *chip);
0204 extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip);
0205 
0206 /* common device tree population function */
0207 extern struct lp55xx_platform_data
0208 *lp55xx_of_populate_pdata(struct device *dev, struct device_node *np,
0209               struct lp55xx_chip *chip);
0210 
0211 #endif /* _LEDS_LP55XX_COMMON_H */