leds-lp55xx-common.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * LP55XX Common Driver Header
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. *
  6. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * Derived from leds-lp5521.c, leds-lp5523.c
  13. */
  14. #ifndef _LEDS_LP55XX_COMMON_H
  15. #define _LEDS_LP55XX_COMMON_H
  16. enum lp55xx_engine_index {
  17. LP55XX_ENGINE_INVALID,
  18. LP55XX_ENGINE_1,
  19. LP55XX_ENGINE_2,
  20. LP55XX_ENGINE_3,
  21. LP55XX_ENGINE_MAX = LP55XX_ENGINE_3,
  22. };
  23. enum lp55xx_engine_mode {
  24. LP55XX_ENGINE_DISABLED,
  25. LP55XX_ENGINE_LOAD,
  26. LP55XX_ENGINE_RUN,
  27. };
  28. #define LP55XX_DEV_ATTR_RW(name, show, store) \
  29. DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store)
  30. #define LP55XX_DEV_ATTR_RO(name, show) \
  31. DEVICE_ATTR(name, S_IRUGO, show, NULL)
  32. #define LP55XX_DEV_ATTR_WO(name, store) \
  33. DEVICE_ATTR(name, S_IWUSR, NULL, store)
  34. #define show_mode(nr) \
  35. static ssize_t show_engine##nr##_mode(struct device *dev, \
  36. struct device_attribute *attr, \
  37. char *buf) \
  38. { \
  39. return show_engine_mode(dev, attr, buf, nr); \
  40. }
  41. #define store_mode(nr) \
  42. static ssize_t store_engine##nr##_mode(struct device *dev, \
  43. struct device_attribute *attr, \
  44. const char *buf, size_t len) \
  45. { \
  46. return store_engine_mode(dev, attr, buf, len, nr); \
  47. }
  48. #define show_leds(nr) \
  49. static ssize_t show_engine##nr##_leds(struct device *dev, \
  50. struct device_attribute *attr, \
  51. char *buf) \
  52. { \
  53. return show_engine_leds(dev, attr, buf, nr); \
  54. }
  55. #define store_leds(nr) \
  56. static ssize_t store_engine##nr##_leds(struct device *dev, \
  57. struct device_attribute *attr, \
  58. const char *buf, size_t len) \
  59. { \
  60. return store_engine_leds(dev, attr, buf, len, nr); \
  61. }
  62. #define store_load(nr) \
  63. static ssize_t store_engine##nr##_load(struct device *dev, \
  64. struct device_attribute *attr, \
  65. const char *buf, size_t len) \
  66. { \
  67. return store_engine_load(dev, attr, buf, len, nr); \
  68. }
  69. struct lp55xx_led;
  70. struct lp55xx_chip;
  71. /*
  72. * struct lp55xx_reg
  73. * @addr : Register address
  74. * @val : Register value
  75. */
  76. struct lp55xx_reg {
  77. u8 addr;
  78. u8 val;
  79. };
  80. /*
  81. * struct lp55xx_device_config
  82. * @reset : Chip specific reset command
  83. * @enable : Chip specific enable command
  84. * @max_channel : Maximum number of channels
  85. * @post_init_device : Chip specific initialization code
  86. * @brightness_work_fn : Brightness work function
  87. * @set_led_current : LED current set function
  88. * @firmware_cb : Call function when the firmware is loaded
  89. * @run_engine : Run internal engine for pattern
  90. * @dev_attr_group : Device specific attributes
  91. */
  92. struct lp55xx_device_config {
  93. const struct lp55xx_reg reset;
  94. const struct lp55xx_reg enable;
  95. const int max_channel;
  96. /* define if the device has specific initialization process */
  97. int (*post_init_device) (struct lp55xx_chip *chip);
  98. /* access brightness register */
  99. void (*brightness_work_fn)(struct work_struct *work);
  100. /* current setting function */
  101. void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
  102. /* access program memory when the firmware is loaded */
  103. void (*firmware_cb)(struct lp55xx_chip *chip);
  104. /* used for running firmware LED patterns */
  105. void (*run_engine) (struct lp55xx_chip *chip, bool start);
  106. /* additional device specific attributes */
  107. const struct attribute_group *dev_attr_group;
  108. };
  109. /*
  110. * struct lp55xx_engine
  111. * @mode : Engine mode
  112. * @led_mux : Mux bits for LED selection. Only used in LP5523
  113. */
  114. struct lp55xx_engine {
  115. enum lp55xx_engine_mode mode;
  116. u16 led_mux;
  117. };
  118. /*
  119. * struct lp55xx_chip
  120. * @cl : I2C communication for access registers
  121. * @pdata : Platform specific data
  122. * @lock : Lock for user-space interface
  123. * @num_leds : Number of registered LEDs
  124. * @cfg : Device specific configuration data
  125. * @engine_idx : Selected engine number
  126. * @engines : Engine structure for the device attribute R/W interface
  127. * @fw : Firmware data for running a LED pattern
  128. */
  129. struct lp55xx_chip {
  130. struct i2c_client *cl;
  131. struct clk *clk;
  132. struct lp55xx_platform_data *pdata;
  133. struct mutex lock; /* lock for user-space interface */
  134. int num_leds;
  135. struct lp55xx_device_config *cfg;
  136. enum lp55xx_engine_index engine_idx;
  137. struct lp55xx_engine engines[LP55XX_ENGINE_MAX];
  138. const struct firmware *fw;
  139. };
  140. /*
  141. * struct lp55xx_led
  142. * @chan_nr : Channel number
  143. * @cdev : LED class device
  144. * @led_current : Current setting at each led channel
  145. * @max_current : Maximun current at each led channel
  146. * @brightness_work : Workqueue for brightness control
  147. * @brightness : Brightness value
  148. * @chip : The lp55xx chip data
  149. */
  150. struct lp55xx_led {
  151. int chan_nr;
  152. struct led_classdev cdev;
  153. u8 led_current;
  154. u8 max_current;
  155. struct work_struct brightness_work;
  156. u8 brightness;
  157. struct lp55xx_chip *chip;
  158. };
  159. /* register access */
  160. extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val);
  161. extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val);
  162. extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
  163. u8 mask, u8 val);
  164. /* external clock detection */
  165. extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
  166. /* common device init/deinit functions */
  167. extern int lp55xx_init_device(struct lp55xx_chip *chip);
  168. extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
  169. /* common LED class device functions */
  170. extern int lp55xx_register_leds(struct lp55xx_led *led,
  171. struct lp55xx_chip *chip);
  172. extern void lp55xx_unregister_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. #endif /* _LEDS_LP55XX_COMMON_H */