leds-lp55xx-common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * LP5521/LP5523/LP55231 Common Driver
  3. *
  4. * Copyright 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 modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Derived from leds-lp5521.c, leds-lp5523.c
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/leds.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_data/leds-lp55xx.h>
  19. #include "leds-lp55xx-common.h"
  20. static void lp55xx_reset_device(struct lp55xx_chip *chip)
  21. {
  22. struct lp55xx_device_config *cfg = chip->cfg;
  23. u8 addr = cfg->reset.addr;
  24. u8 val = cfg->reset.val;
  25. /* no error checking here because no ACK from the device after reset */
  26. lp55xx_write(chip, addr, val);
  27. }
  28. static int lp55xx_detect_device(struct lp55xx_chip *chip)
  29. {
  30. struct lp55xx_device_config *cfg = chip->cfg;
  31. u8 addr = cfg->enable.addr;
  32. u8 val = cfg->enable.val;
  33. int ret;
  34. ret = lp55xx_write(chip, addr, val);
  35. if (ret)
  36. return ret;
  37. usleep_range(1000, 2000);
  38. ret = lp55xx_read(chip, addr, &val);
  39. if (ret)
  40. return ret;
  41. if (val != cfg->enable.val)
  42. return -ENODEV;
  43. return 0;
  44. }
  45. static int lp55xx_post_init_device(struct lp55xx_chip *chip)
  46. {
  47. struct lp55xx_device_config *cfg = chip->cfg;
  48. if (!cfg->post_init_device)
  49. return 0;
  50. return cfg->post_init_device(chip);
  51. }
  52. static struct attribute *lp55xx_led_attributes[] = {
  53. NULL,
  54. };
  55. static struct attribute_group lp55xx_led_attr_group = {
  56. .attrs = lp55xx_led_attributes
  57. };
  58. static void lp55xx_set_brightness(struct led_classdev *cdev,
  59. enum led_brightness brightness)
  60. {
  61. }
  62. static int lp55xx_init_led(struct lp55xx_led *led,
  63. struct lp55xx_chip *chip, int chan)
  64. {
  65. struct lp55xx_platform_data *pdata = chip->pdata;
  66. struct lp55xx_device_config *cfg = chip->cfg;
  67. struct device *dev = &chip->cl->dev;
  68. char name[32];
  69. int ret;
  70. int max_channel = cfg->max_channel;
  71. if (chan >= max_channel) {
  72. dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
  73. return -EINVAL;
  74. }
  75. if (pdata->led_config[chan].led_current == 0)
  76. return 0;
  77. led->led_current = pdata->led_config[chan].led_current;
  78. led->max_current = pdata->led_config[chan].max_current;
  79. led->chan_nr = pdata->led_config[chan].chan_nr;
  80. if (led->chan_nr >= max_channel) {
  81. dev_err(dev, "Use channel numbers between 0 and %d\n",
  82. max_channel - 1);
  83. return -EINVAL;
  84. }
  85. led->cdev.brightness_set = lp55xx_set_brightness;
  86. if (pdata->led_config[chan].name) {
  87. led->cdev.name = pdata->led_config[chan].name;
  88. } else {
  89. snprintf(name, sizeof(name), "%s:channel%d",
  90. pdata->label ? : chip->cl->name, chan);
  91. led->cdev.name = name;
  92. }
  93. /*
  94. * register led class device for each channel and
  95. * add device attributes
  96. */
  97. ret = led_classdev_register(dev, &led->cdev);
  98. if (ret) {
  99. dev_err(dev, "led register err: %d\n", ret);
  100. return ret;
  101. }
  102. ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
  103. if (ret) {
  104. dev_err(dev, "led sysfs err: %d\n", ret);
  105. led_classdev_unregister(&led->cdev);
  106. return ret;
  107. }
  108. return 0;
  109. }
  110. int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
  111. {
  112. return i2c_smbus_write_byte_data(chip->cl, reg, val);
  113. }
  114. EXPORT_SYMBOL_GPL(lp55xx_write);
  115. int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
  116. {
  117. s32 ret;
  118. ret = i2c_smbus_read_byte_data(chip->cl, reg);
  119. if (ret < 0)
  120. return ret;
  121. *val = ret;
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(lp55xx_read);
  125. int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
  126. {
  127. int ret;
  128. u8 tmp;
  129. ret = lp55xx_read(chip, reg, &tmp);
  130. if (ret)
  131. return ret;
  132. tmp &= ~mask;
  133. tmp |= val & mask;
  134. return lp55xx_write(chip, reg, tmp);
  135. }
  136. EXPORT_SYMBOL_GPL(lp55xx_update_bits);
  137. int lp55xx_init_device(struct lp55xx_chip *chip)
  138. {
  139. struct lp55xx_platform_data *pdata;
  140. struct lp55xx_device_config *cfg;
  141. struct device *dev = &chip->cl->dev;
  142. int ret = 0;
  143. WARN_ON(!chip);
  144. pdata = chip->pdata;
  145. cfg = chip->cfg;
  146. if (!pdata || !cfg)
  147. return -EINVAL;
  148. if (pdata->setup_resources) {
  149. ret = pdata->setup_resources();
  150. if (ret < 0) {
  151. dev_err(dev, "setup resoure err: %d\n", ret);
  152. goto err;
  153. }
  154. }
  155. if (pdata->enable) {
  156. pdata->enable(0);
  157. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  158. pdata->enable(1);
  159. usleep_range(1000, 2000); /* 500us abs min. */
  160. }
  161. lp55xx_reset_device(chip);
  162. /*
  163. * Exact value is not available. 10 - 20ms
  164. * appears to be enough for reset.
  165. */
  166. usleep_range(10000, 20000);
  167. ret = lp55xx_detect_device(chip);
  168. if (ret) {
  169. dev_err(dev, "device detection err: %d\n", ret);
  170. goto err;
  171. }
  172. /* chip specific initialization */
  173. ret = lp55xx_post_init_device(chip);
  174. if (ret) {
  175. dev_err(dev, "post init device err: %d\n", ret);
  176. goto err_post_init;
  177. }
  178. return 0;
  179. err_post_init:
  180. lp55xx_deinit_device(chip);
  181. err:
  182. return ret;
  183. }
  184. EXPORT_SYMBOL_GPL(lp55xx_init_device);
  185. void lp55xx_deinit_device(struct lp55xx_chip *chip)
  186. {
  187. struct lp55xx_platform_data *pdata = chip->pdata;
  188. if (pdata->enable)
  189. pdata->enable(0);
  190. if (pdata->release_resources)
  191. pdata->release_resources();
  192. }
  193. EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
  194. int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
  195. {
  196. struct lp55xx_platform_data *pdata = chip->pdata;
  197. struct lp55xx_device_config *cfg = chip->cfg;
  198. int num_channels = pdata->num_channels;
  199. struct lp55xx_led *each;
  200. u8 led_current;
  201. int ret;
  202. int i;
  203. if (!cfg->brightness_work_fn) {
  204. dev_err(&chip->cl->dev, "empty brightness configuration\n");
  205. return -EINVAL;
  206. }
  207. for (i = 0; i < num_channels; i++) {
  208. /* do not initialize channels that are not connected */
  209. if (pdata->led_config[i].led_current == 0)
  210. continue;
  211. led_current = pdata->led_config[i].led_current;
  212. each = led + i;
  213. ret = lp55xx_init_led(each, chip, i);
  214. if (ret)
  215. goto err_init_led;
  216. INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
  217. chip->num_leds++;
  218. each->chip = chip;
  219. /* setting led current at each channel */
  220. if (cfg->set_led_current)
  221. cfg->set_led_current(each, led_current);
  222. }
  223. return 0;
  224. err_init_led:
  225. for (i = 0; i < chip->num_leds; i++) {
  226. each = led + i;
  227. led_classdev_unregister(&each->cdev);
  228. flush_work(&each->brightness_work);
  229. }
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(lp55xx_register_leds);
  233. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  234. MODULE_DESCRIPTION("LP55xx Common Driver");
  235. MODULE_LICENSE("GPL");