1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2c93d08faSMilo(Woogyom) Kim /* 3c93d08faSMilo(Woogyom) Kim * LP55XX Common Driver Header 4c93d08faSMilo(Woogyom) Kim * 5c93d08faSMilo(Woogyom) Kim * Copyright (C) 2012 Texas Instruments 6c93d08faSMilo(Woogyom) Kim * 7c93d08faSMilo(Woogyom) Kim * Author: Milo(Woogyom) Kim <milo.kim@ti.com> 8c93d08faSMilo(Woogyom) Kim * 9c93d08faSMilo(Woogyom) Kim * Derived from leds-lp5521.c, leds-lp5523.c 10c93d08faSMilo(Woogyom) Kim */ 11c93d08faSMilo(Woogyom) Kim 12c93d08faSMilo(Woogyom) Kim #ifndef _LEDS_LP55XX_COMMON_H 13c93d08faSMilo(Woogyom) Kim #define _LEDS_LP55XX_COMMON_H 14c93d08faSMilo(Woogyom) Kim 15*92a81562SDan Murphy #include <linux/led-class-multicolor.h> 16*92a81562SDan Murphy 1710c06d17SMilo(Woogyom) Kim enum lp55xx_engine_index { 1810c06d17SMilo(Woogyom) Kim LP55XX_ENGINE_INVALID, 1910c06d17SMilo(Woogyom) Kim LP55XX_ENGINE_1, 2010c06d17SMilo(Woogyom) Kim LP55XX_ENGINE_2, 2110c06d17SMilo(Woogyom) Kim LP55XX_ENGINE_3, 226841a91dSMilo Kim LP55XX_ENGINE_MAX = LP55XX_ENGINE_3, 236841a91dSMilo Kim }; 246841a91dSMilo Kim 256841a91dSMilo Kim enum lp55xx_engine_mode { 266841a91dSMilo Kim LP55XX_ENGINE_DISABLED, 276841a91dSMilo Kim LP55XX_ENGINE_LOAD, 286841a91dSMilo Kim LP55XX_ENGINE_RUN, 2910c06d17SMilo(Woogyom) Kim }; 3010c06d17SMilo(Woogyom) Kim 3136030978SMilo Kim #define LP55XX_DEV_ATTR_RW(name, show, store) \ 3236030978SMilo Kim DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store) 3336030978SMilo Kim #define LP55XX_DEV_ATTR_RO(name, show) \ 3436030978SMilo Kim DEVICE_ATTR(name, S_IRUGO, show, NULL) 3536030978SMilo Kim #define LP55XX_DEV_ATTR_WO(name, store) \ 3636030978SMilo Kim DEVICE_ATTR(name, S_IWUSR, NULL, store) 3736030978SMilo Kim 3836030978SMilo Kim #define show_mode(nr) \ 3936030978SMilo Kim static ssize_t show_engine##nr##_mode(struct device *dev, \ 4036030978SMilo Kim struct device_attribute *attr, \ 4136030978SMilo Kim char *buf) \ 4236030978SMilo Kim { \ 4336030978SMilo Kim return show_engine_mode(dev, attr, buf, nr); \ 4436030978SMilo Kim } 4536030978SMilo Kim 4636030978SMilo Kim #define store_mode(nr) \ 4736030978SMilo Kim static ssize_t store_engine##nr##_mode(struct device *dev, \ 4836030978SMilo Kim struct device_attribute *attr, \ 4936030978SMilo Kim const char *buf, size_t len) \ 5036030978SMilo Kim { \ 5136030978SMilo Kim return store_engine_mode(dev, attr, buf, len, nr); \ 5236030978SMilo Kim } 5336030978SMilo Kim 5436030978SMilo Kim #define show_leds(nr) \ 5536030978SMilo Kim static ssize_t show_engine##nr##_leds(struct device *dev, \ 5636030978SMilo Kim struct device_attribute *attr, \ 5736030978SMilo Kim char *buf) \ 5836030978SMilo Kim { \ 5936030978SMilo Kim return show_engine_leds(dev, attr, buf, nr); \ 6036030978SMilo Kim } 6136030978SMilo Kim 6236030978SMilo Kim #define store_leds(nr) \ 6336030978SMilo Kim static ssize_t store_engine##nr##_leds(struct device *dev, \ 6436030978SMilo Kim struct device_attribute *attr, \ 6536030978SMilo Kim const char *buf, size_t len) \ 6636030978SMilo Kim { \ 6736030978SMilo Kim return store_engine_leds(dev, attr, buf, len, nr); \ 6836030978SMilo Kim } 6936030978SMilo Kim 7036030978SMilo Kim #define store_load(nr) \ 7136030978SMilo Kim static ssize_t store_engine##nr##_load(struct device *dev, \ 7236030978SMilo Kim struct device_attribute *attr, \ 7336030978SMilo Kim const char *buf, size_t len) \ 7436030978SMilo Kim { \ 7536030978SMilo Kim return store_engine_load(dev, attr, buf, len, nr); \ 7636030978SMilo Kim } 7736030978SMilo Kim 78c93d08faSMilo(Woogyom) Kim struct lp55xx_led; 79c93d08faSMilo(Woogyom) Kim struct lp55xx_chip; 80c93d08faSMilo(Woogyom) Kim 81c93d08faSMilo(Woogyom) Kim /* 8248068d5dSMilo(Woogyom) Kim * struct lp55xx_reg 8348068d5dSMilo(Woogyom) Kim * @addr : Register address 8448068d5dSMilo(Woogyom) Kim * @val : Register value 8548068d5dSMilo(Woogyom) Kim */ 8648068d5dSMilo(Woogyom) Kim struct lp55xx_reg { 8748068d5dSMilo(Woogyom) Kim u8 addr; 8848068d5dSMilo(Woogyom) Kim u8 val; 8948068d5dSMilo(Woogyom) Kim }; 9048068d5dSMilo(Woogyom) Kim 9148068d5dSMilo(Woogyom) Kim /* 9248068d5dSMilo(Woogyom) Kim * struct lp55xx_device_config 9348068d5dSMilo(Woogyom) Kim * @reset : Chip specific reset command 94e3a700d8SMilo(Woogyom) Kim * @enable : Chip specific enable command 950e202346SMilo(Woogyom) Kim * @max_channel : Maximum number of channels 96ffbdccdbSMilo(Woogyom) Kim * @post_init_device : Chip specific initialization code 9795b2af63SAndrew Lunn * @brightness_fn : Brightness function 98*92a81562SDan Murphy * @multicolor_brightness_fn : Multicolor brightness function 999e9b3db1SMilo(Woogyom) Kim * @set_led_current : LED current set function 10010c06d17SMilo(Woogyom) Kim * @firmware_cb : Call function when the firmware is loaded 10110c06d17SMilo(Woogyom) Kim * @run_engine : Run internal engine for pattern 102240085e2SMilo(Woogyom) Kim * @dev_attr_group : Device specific attributes 10348068d5dSMilo(Woogyom) Kim */ 10448068d5dSMilo(Woogyom) Kim struct lp55xx_device_config { 10548068d5dSMilo(Woogyom) Kim const struct lp55xx_reg reset; 106e3a700d8SMilo(Woogyom) Kim const struct lp55xx_reg enable; 1070e202346SMilo(Woogyom) Kim const int max_channel; 108ffbdccdbSMilo(Woogyom) Kim 109ffbdccdbSMilo(Woogyom) Kim /* define if the device has specific initialization process */ 110ffbdccdbSMilo(Woogyom) Kim int (*post_init_device) (struct lp55xx_chip *chip); 1119e9b3db1SMilo(Woogyom) Kim 112*92a81562SDan Murphy /* set LED brightness */ 11395b2af63SAndrew Lunn int (*brightness_fn)(struct lp55xx_led *led); 1149e9b3db1SMilo(Woogyom) Kim 115*92a81562SDan Murphy /* set multicolor LED brightness */ 116*92a81562SDan Murphy int (*multicolor_brightness_fn)(struct lp55xx_led *led); 117*92a81562SDan Murphy 1189e9b3db1SMilo(Woogyom) Kim /* current setting function */ 1199e9b3db1SMilo(Woogyom) Kim void (*set_led_current) (struct lp55xx_led *led, u8 led_current); 12010c06d17SMilo(Woogyom) Kim 12110c06d17SMilo(Woogyom) Kim /* access program memory when the firmware is loaded */ 12210c06d17SMilo(Woogyom) Kim void (*firmware_cb)(struct lp55xx_chip *chip); 12310c06d17SMilo(Woogyom) Kim 12410c06d17SMilo(Woogyom) Kim /* used for running firmware LED patterns */ 12510c06d17SMilo(Woogyom) Kim void (*run_engine) (struct lp55xx_chip *chip, bool start); 126240085e2SMilo(Woogyom) Kim 127240085e2SMilo(Woogyom) Kim /* additional device specific attributes */ 128240085e2SMilo(Woogyom) Kim const struct attribute_group *dev_attr_group; 12948068d5dSMilo(Woogyom) Kim }; 13048068d5dSMilo(Woogyom) Kim 13148068d5dSMilo(Woogyom) Kim /* 1326841a91dSMilo Kim * struct lp55xx_engine 1336841a91dSMilo Kim * @mode : Engine mode 1346841a91dSMilo Kim * @led_mux : Mux bits for LED selection. Only used in LP5523 1356841a91dSMilo Kim */ 1366841a91dSMilo Kim struct lp55xx_engine { 1376841a91dSMilo Kim enum lp55xx_engine_mode mode; 1386841a91dSMilo Kim u16 led_mux; 1396841a91dSMilo Kim }; 1406841a91dSMilo Kim 1416841a91dSMilo Kim /* 142c93d08faSMilo(Woogyom) Kim * struct lp55xx_chip 143c93d08faSMilo(Woogyom) Kim * @cl : I2C communication for access registers 144c93d08faSMilo(Woogyom) Kim * @pdata : Platform specific data 145c93d08faSMilo(Woogyom) Kim * @lock : Lock for user-space interface 146c93d08faSMilo(Woogyom) Kim * @num_leds : Number of registered LEDs 14748068d5dSMilo(Woogyom) Kim * @cfg : Device specific configuration data 14810c06d17SMilo(Woogyom) Kim * @engine_idx : Selected engine number 1496841a91dSMilo Kim * @engines : Engine structure for the device attribute R/W interface 15010c06d17SMilo(Woogyom) Kim * @fw : Firmware data for running a LED pattern 151c93d08faSMilo(Woogyom) Kim */ 152c93d08faSMilo(Woogyom) Kim struct lp55xx_chip { 153c93d08faSMilo(Woogyom) Kim struct i2c_client *cl; 15453b41922SKim, Milo struct clk *clk; 155c93d08faSMilo(Woogyom) Kim struct lp55xx_platform_data *pdata; 156c93d08faSMilo(Woogyom) Kim struct mutex lock; /* lock for user-space interface */ 157c93d08faSMilo(Woogyom) Kim int num_leds; 15848068d5dSMilo(Woogyom) Kim struct lp55xx_device_config *cfg; 15910c06d17SMilo(Woogyom) Kim enum lp55xx_engine_index engine_idx; 1606841a91dSMilo Kim struct lp55xx_engine engines[LP55XX_ENGINE_MAX]; 16110c06d17SMilo(Woogyom) Kim const struct firmware *fw; 162c93d08faSMilo(Woogyom) Kim }; 163c93d08faSMilo(Woogyom) Kim 164c93d08faSMilo(Woogyom) Kim /* 165c93d08faSMilo(Woogyom) Kim * struct lp55xx_led 166c93d08faSMilo(Woogyom) Kim * @chan_nr : Channel number 167c93d08faSMilo(Woogyom) Kim * @cdev : LED class device 168*92a81562SDan Murphy * @mc_cdev : Multi color class device 169*92a81562SDan Murphy * @color_components: Multi color LED map information 170c93d08faSMilo(Woogyom) Kim * @led_current : Current setting at each led channel 171c93d08faSMilo(Woogyom) Kim * @max_current : Maximun current at each led channel 172c93d08faSMilo(Woogyom) Kim * @brightness : Brightness value 173c93d08faSMilo(Woogyom) Kim * @chip : The lp55xx chip data 174c93d08faSMilo(Woogyom) Kim */ 175c93d08faSMilo(Woogyom) Kim struct lp55xx_led { 176c93d08faSMilo(Woogyom) Kim int chan_nr; 177c93d08faSMilo(Woogyom) Kim struct led_classdev cdev; 178*92a81562SDan Murphy struct led_classdev_mc mc_cdev; 179c93d08faSMilo(Woogyom) Kim u8 led_current; 180c93d08faSMilo(Woogyom) Kim u8 max_current; 181c93d08faSMilo(Woogyom) Kim u8 brightness; 182c93d08faSMilo(Woogyom) Kim struct lp55xx_chip *chip; 183c93d08faSMilo(Woogyom) Kim }; 184c93d08faSMilo(Woogyom) Kim 185c93d08faSMilo(Woogyom) Kim /* register access */ 186c93d08faSMilo(Woogyom) Kim extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val); 187c93d08faSMilo(Woogyom) Kim extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val); 188c93d08faSMilo(Woogyom) Kim extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, 189c93d08faSMilo(Woogyom) Kim u8 mask, u8 val); 190c93d08faSMilo(Woogyom) Kim 19153b41922SKim, Milo /* external clock detection */ 19253b41922SKim, Milo extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip); 19353b41922SKim, Milo 1946ce61762SMilo(Woogyom) Kim /* common device init/deinit functions */ 195a85908ddSMilo(Woogyom) Kim extern int lp55xx_init_device(struct lp55xx_chip *chip); 1966ce61762SMilo(Woogyom) Kim extern void lp55xx_deinit_device(struct lp55xx_chip *chip); 197a85908ddSMilo(Woogyom) Kim 1989e9b3db1SMilo(Woogyom) Kim /* common LED class device functions */ 1999e9b3db1SMilo(Woogyom) Kim extern int lp55xx_register_leds(struct lp55xx_led *led, 2009e9b3db1SMilo(Woogyom) Kim struct lp55xx_chip *chip); 201b3b6f811SMilo(Woogyom) Kim 202b3b6f811SMilo(Woogyom) Kim /* common device attributes functions */ 203b3b6f811SMilo(Woogyom) Kim extern int lp55xx_register_sysfs(struct lp55xx_chip *chip); 204ba6fa846SMilo(Woogyom) Kim extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip); 205b3b6f811SMilo(Woogyom) Kim 2067542a04bSLinus Walleij /* common device tree population function */ 207ed133352SMilo Kim extern struct lp55xx_platform_data 208*92a81562SDan Murphy *lp55xx_of_populate_pdata(struct device *dev, struct device_node *np, 209*92a81562SDan Murphy struct lp55xx_chip *chip); 2107542a04bSLinus Walleij 211c93d08faSMilo(Woogyom) Kim #endif /* _LEDS_LP55XX_COMMON_H */ 212