leds-lp55xx-common.h 9.5 KB

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