leds-lp55xx-common.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
  21. {
  22. return container_of(cdev, struct lp55xx_led, cdev);
  23. }
  24. static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
  25. {
  26. return cdev_to_lp55xx_led(dev_get_drvdata(dev));
  27. }
  28. static void lp55xx_reset_device(struct lp55xx_chip *chip)
  29. {
  30. struct lp55xx_device_config *cfg = chip->cfg;
  31. u8 addr = cfg->reset.addr;
  32. u8 val = cfg->reset.val;
  33. /* no error checking here because no ACK from the device after reset */
  34. lp55xx_write(chip, addr, val);
  35. }
  36. static int lp55xx_detect_device(struct lp55xx_chip *chip)
  37. {
  38. struct lp55xx_device_config *cfg = chip->cfg;
  39. u8 addr = cfg->enable.addr;
  40. u8 val = cfg->enable.val;
  41. int ret;
  42. ret = lp55xx_write(chip, addr, val);
  43. if (ret)
  44. return ret;
  45. usleep_range(1000, 2000);
  46. ret = lp55xx_read(chip, addr, &val);
  47. if (ret)
  48. return ret;
  49. if (val != cfg->enable.val)
  50. return -ENODEV;
  51. return 0;
  52. }
  53. static int lp55xx_post_init_device(struct lp55xx_chip *chip)
  54. {
  55. struct lp55xx_device_config *cfg = chip->cfg;
  56. if (!cfg->post_init_device)
  57. return 0;
  58. return cfg->post_init_device(chip);
  59. }
  60. static ssize_t lp55xx_show_current(struct device *dev,
  61. struct device_attribute *attr,
  62. char *buf)
  63. {
  64. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  65. return sprintf(buf, "%d\n", led->led_current);
  66. }
  67. static ssize_t lp55xx_store_current(struct device *dev,
  68. struct device_attribute *attr,
  69. const char *buf, size_t len)
  70. {
  71. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  72. struct lp55xx_chip *chip = led->chip;
  73. unsigned long curr;
  74. if (kstrtoul(buf, 0, &curr))
  75. return -EINVAL;
  76. if (curr > led->max_current)
  77. return -EINVAL;
  78. if (!chip->cfg->set_led_current)
  79. return len;
  80. mutex_lock(&chip->lock);
  81. chip->cfg->set_led_current(led, (u8)curr);
  82. mutex_unlock(&chip->lock);
  83. return len;
  84. }
  85. static ssize_t lp55xx_show_max_current(struct device *dev,
  86. struct device_attribute *attr,
  87. char *buf)
  88. {
  89. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  90. return sprintf(buf, "%d\n", led->max_current);
  91. }
  92. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
  93. lp55xx_store_current);
  94. static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
  95. static struct attribute *lp55xx_led_attributes[] = {
  96. &dev_attr_led_current.attr,
  97. &dev_attr_max_current.attr,
  98. NULL,
  99. };
  100. static struct attribute_group lp55xx_led_attr_group = {
  101. .attrs = lp55xx_led_attributes
  102. };
  103. static void lp55xx_set_brightness(struct led_classdev *cdev,
  104. enum led_brightness brightness)
  105. {
  106. struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
  107. led->brightness = (u8)brightness;
  108. schedule_work(&led->brightness_work);
  109. }
  110. static int lp55xx_init_led(struct lp55xx_led *led,
  111. struct lp55xx_chip *chip, int chan)
  112. {
  113. struct lp55xx_platform_data *pdata = chip->pdata;
  114. struct lp55xx_device_config *cfg = chip->cfg;
  115. struct device *dev = &chip->cl->dev;
  116. char name[32];
  117. int ret;
  118. int max_channel = cfg->max_channel;
  119. if (chan >= max_channel) {
  120. dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
  121. return -EINVAL;
  122. }
  123. if (pdata->led_config[chan].led_current == 0)
  124. return 0;
  125. led->led_current = pdata->led_config[chan].led_current;
  126. led->max_current = pdata->led_config[chan].max_current;
  127. led->chan_nr = pdata->led_config[chan].chan_nr;
  128. if (led->chan_nr >= max_channel) {
  129. dev_err(dev, "Use channel numbers between 0 and %d\n",
  130. max_channel - 1);
  131. return -EINVAL;
  132. }
  133. led->cdev.brightness_set = lp55xx_set_brightness;
  134. if (pdata->led_config[chan].name) {
  135. led->cdev.name = pdata->led_config[chan].name;
  136. } else {
  137. snprintf(name, sizeof(name), "%s:channel%d",
  138. pdata->label ? : chip->cl->name, chan);
  139. led->cdev.name = name;
  140. }
  141. /*
  142. * register led class device for each channel and
  143. * add device attributes
  144. */
  145. ret = led_classdev_register(dev, &led->cdev);
  146. if (ret) {
  147. dev_err(dev, "led register err: %d\n", ret);
  148. return ret;
  149. }
  150. ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
  151. if (ret) {
  152. dev_err(dev, "led sysfs err: %d\n", ret);
  153. led_classdev_unregister(&led->cdev);
  154. return ret;
  155. }
  156. return 0;
  157. }
  158. int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
  159. {
  160. return i2c_smbus_write_byte_data(chip->cl, reg, val);
  161. }
  162. EXPORT_SYMBOL_GPL(lp55xx_write);
  163. int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
  164. {
  165. s32 ret;
  166. ret = i2c_smbus_read_byte_data(chip->cl, reg);
  167. if (ret < 0)
  168. return ret;
  169. *val = ret;
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(lp55xx_read);
  173. int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
  174. {
  175. int ret;
  176. u8 tmp;
  177. ret = lp55xx_read(chip, reg, &tmp);
  178. if (ret)
  179. return ret;
  180. tmp &= ~mask;
  181. tmp |= val & mask;
  182. return lp55xx_write(chip, reg, tmp);
  183. }
  184. EXPORT_SYMBOL_GPL(lp55xx_update_bits);
  185. int lp55xx_init_device(struct lp55xx_chip *chip)
  186. {
  187. struct lp55xx_platform_data *pdata;
  188. struct lp55xx_device_config *cfg;
  189. struct device *dev = &chip->cl->dev;
  190. int ret = 0;
  191. WARN_ON(!chip);
  192. pdata = chip->pdata;
  193. cfg = chip->cfg;
  194. if (!pdata || !cfg)
  195. return -EINVAL;
  196. if (pdata->setup_resources) {
  197. ret = pdata->setup_resources();
  198. if (ret < 0) {
  199. dev_err(dev, "setup resoure err: %d\n", ret);
  200. goto err;
  201. }
  202. }
  203. if (pdata->enable) {
  204. pdata->enable(0);
  205. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  206. pdata->enable(1);
  207. usleep_range(1000, 2000); /* 500us abs min. */
  208. }
  209. lp55xx_reset_device(chip);
  210. /*
  211. * Exact value is not available. 10 - 20ms
  212. * appears to be enough for reset.
  213. */
  214. usleep_range(10000, 20000);
  215. ret = lp55xx_detect_device(chip);
  216. if (ret) {
  217. dev_err(dev, "device detection err: %d\n", ret);
  218. goto err;
  219. }
  220. /* chip specific initialization */
  221. ret = lp55xx_post_init_device(chip);
  222. if (ret) {
  223. dev_err(dev, "post init device err: %d\n", ret);
  224. goto err_post_init;
  225. }
  226. return 0;
  227. err_post_init:
  228. lp55xx_deinit_device(chip);
  229. err:
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(lp55xx_init_device);
  233. void lp55xx_deinit_device(struct lp55xx_chip *chip)
  234. {
  235. struct lp55xx_platform_data *pdata = chip->pdata;
  236. if (pdata->enable)
  237. pdata->enable(0);
  238. if (pdata->release_resources)
  239. pdata->release_resources();
  240. }
  241. EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
  242. int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
  243. {
  244. struct lp55xx_platform_data *pdata = chip->pdata;
  245. struct lp55xx_device_config *cfg = chip->cfg;
  246. int num_channels = pdata->num_channels;
  247. struct lp55xx_led *each;
  248. u8 led_current;
  249. int ret;
  250. int i;
  251. if (!cfg->brightness_work_fn) {
  252. dev_err(&chip->cl->dev, "empty brightness configuration\n");
  253. return -EINVAL;
  254. }
  255. for (i = 0; i < num_channels; i++) {
  256. /* do not initialize channels that are not connected */
  257. if (pdata->led_config[i].led_current == 0)
  258. continue;
  259. led_current = pdata->led_config[i].led_current;
  260. each = led + i;
  261. ret = lp55xx_init_led(each, chip, i);
  262. if (ret)
  263. goto err_init_led;
  264. INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
  265. chip->num_leds++;
  266. each->chip = chip;
  267. /* setting led current at each channel */
  268. if (cfg->set_led_current)
  269. cfg->set_led_current(each, led_current);
  270. }
  271. return 0;
  272. err_init_led:
  273. for (i = 0; i < chip->num_leds; i++) {
  274. each = led + i;
  275. led_classdev_unregister(&each->cdev);
  276. flush_work(&each->brightness_work);
  277. }
  278. return ret;
  279. }
  280. EXPORT_SYMBOL_GPL(lp55xx_register_leds);
  281. void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
  282. {
  283. int i;
  284. struct lp55xx_led *each;
  285. struct kobject *kobj;
  286. for (i = 0; i < chip->num_leds; i++) {
  287. each = led + i;
  288. kobj = &led->cdev.dev->kobj;
  289. sysfs_remove_group(kobj, &lp55xx_led_attr_group);
  290. led_classdev_unregister(&each->cdev);
  291. flush_work(&each->brightness_work);
  292. }
  293. }
  294. EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
  295. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  296. MODULE_DESCRIPTION("LP55xx Common Driver");
  297. MODULE_LICENSE("GPL");