leds-lp55xx-common.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. enum lp55xx_engine_index {
  15. LP55XX_ENGINE_INVALID,
  16. LP55XX_ENGINE_1,
  17. LP55XX_ENGINE_2,
  18. LP55XX_ENGINE_3,
  19. LP55XX_ENGINE_MAX = LP55XX_ENGINE_3,
  20. };
  21. enum lp55xx_engine_mode {
  22. LP55XX_ENGINE_DISABLED,
  23. LP55XX_ENGINE_LOAD,
  24. LP55XX_ENGINE_RUN,
  25. };
  26. #define LP55XX_DEV_ATTR_RW(name, show, store) \
  27. DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store)
  28. #define LP55XX_DEV_ATTR_RO(name, show) \
  29. DEVICE_ATTR(name, S_IRUGO, show, NULL)
  30. #define LP55XX_DEV_ATTR_WO(name, store) \
  31. DEVICE_ATTR(name, S_IWUSR, NULL, store)
  32. #define show_mode(nr) \
  33. static ssize_t show_engine##nr##_mode(struct device *dev, \
  34. struct device_attribute *attr, \
  35. char *buf) \
  36. { \
  37. return show_engine_mode(dev, attr, buf, nr); \
  38. }
  39. #define store_mode(nr) \
  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 store_engine_mode(dev, attr, buf, len, nr); \
  45. }
  46. #define show_leds(nr) \
  47. static ssize_t show_engine##nr##_leds(struct device *dev, \
  48. struct device_attribute *attr, \
  49. char *buf) \
  50. { \
  51. return show_engine_leds(dev, attr, buf, nr); \
  52. }
  53. #define store_leds(nr) \
  54. static ssize_t store_engine##nr##_leds(struct device *dev, \
  55. struct device_attribute *attr, \
  56. const char *buf, size_t len) \
  57. { \
  58. return store_engine_leds(dev, attr, buf, len, nr); \
  59. }
  60. #define store_load(nr) \
  61. static ssize_t store_engine##nr##_load(struct device *dev, \
  62. struct device_attribute *attr, \
  63. const char *buf, size_t len) \
  64. { \
  65. return store_engine_load(dev, attr, buf, len, nr); \
  66. }
  67. struct lp55xx_led;
  68. struct lp55xx_chip;
  69. /*
  70. * struct lp55xx_reg
  71. * @addr : Register address
  72. * @val : Register value
  73. */
  74. struct lp55xx_reg {
  75. u8 addr;
  76. u8 val;
  77. };
  78. /*
  79. * struct lp55xx_device_config
  80. * @reset : Chip specific reset command
  81. * @enable : Chip specific enable command
  82. * @max_channel : Maximum number of channels
  83. * @post_init_device : Chip specific initialization code
  84. * @brightness_fn : Brightness function
  85. * @multicolor_brightness_fn : Multicolor brightness function
  86. * @set_led_current : LED current set function
  87. * @firmware_cb : Call function when the firmware is loaded
  88. * @run_engine : Run internal engine for pattern
  89. * @dev_attr_group : Device specific attributes
  90. */
  91. struct lp55xx_device_config {
  92. const struct lp55xx_reg reset;
  93. const struct lp55xx_reg enable;
  94. const int max_channel;
  95. /* define if the device has specific initialization process */
  96. int (*post_init_device) (struct lp55xx_chip *chip);
  97. /* set LED brightness */
  98. int (*brightness_fn)(struct lp55xx_led *led);
  99. /* set multicolor LED brightness */
  100. int (*multicolor_brightness_fn)(struct lp55xx_led *led);
  101. /* current setting function */
  102. void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
  103. /* access program memory when the firmware is loaded */
  104. void (*firmware_cb)(struct lp55xx_chip *chip);
  105. /* used for running firmware LED patterns */
  106. void (*run_engine) (struct lp55xx_chip *chip, bool start);
  107. /* additional device specific attributes */
  108. const struct attribute_group *dev_attr_group;
  109. };
  110. /*
  111. * struct lp55xx_engine
  112. * @mode : Engine mode
  113. * @led_mux : Mux bits for LED selection. Only used in LP5523
  114. */
  115. struct lp55xx_engine {
  116. enum lp55xx_engine_mode mode;
  117. u16 led_mux;
  118. };
  119. /*
  120. * struct lp55xx_chip
  121. * @cl : I2C communication for access registers
  122. * @pdata : Platform specific data
  123. * @lock : Lock for user-space interface
  124. * @num_leds : Number of registered LEDs
  125. * @cfg : Device specific configuration data
  126. * @engine_idx : Selected engine number
  127. * @engines : Engine structure for the device attribute R/W interface
  128. * @fw : Firmware data for running a LED pattern
  129. */
  130. struct lp55xx_chip {
  131. struct i2c_client *cl;
  132. struct clk *clk;
  133. struct lp55xx_platform_data *pdata;
  134. struct mutex lock; /* lock for user-space interface */
  135. int num_leds;
  136. struct lp55xx_device_config *cfg;
  137. enum lp55xx_engine_index engine_idx;
  138. struct lp55xx_engine engines[LP55XX_ENGINE_MAX];
  139. const struct firmware *fw;
  140. };
  141. /*
  142. * struct lp55xx_led
  143. * @chan_nr : Channel number
  144. * @cdev : LED class device
  145. * @mc_cdev : Multi color class device
  146. * @color_components: Multi color LED map information
  147. * @led_current : Current setting at each led channel
  148. * @max_current : Maximun current at each led channel
  149. * @brightness : Brightness value
  150. * @chip : The lp55xx chip data
  151. */
  152. struct lp55xx_led {
  153. int chan_nr;
  154. struct led_classdev cdev;
  155. struct led_classdev_mc mc_cdev;
  156. u8 led_current;
  157. u8 max_current;
  158. u8 brightness;
  159. struct lp55xx_chip *chip;
  160. };
  161. /* register access */
  162. extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val);
  163. extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val);
  164. extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
  165. u8 mask, u8 val);
  166. /* external clock detection */
  167. extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
  168. /* common device init/deinit functions */
  169. extern int lp55xx_init_device(struct lp55xx_chip *chip);
  170. extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
  171. /* common LED class device functions */
  172. extern int lp55xx_register_leds(struct lp55xx_led *led,
  173. struct lp55xx_chip *chip);
  174. /* common device attributes functions */
  175. extern int lp55xx_register_sysfs(struct lp55xx_chip *chip);
  176. extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip);
  177. /* common device tree population function */
  178. extern struct lp55xx_platform_data
  179. *lp55xx_of_populate_pdata(struct device *dev, struct device_node *np,
  180. struct lp55xx_chip *chip);
  181. #endif /* _LEDS_LP55XX_COMMON_H */