leds-lp55xx-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * LP5521/LP5523/LP55231/LP5562 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/firmware.h>
  16. #include <linux/i2c.h>
  17. #include <linux/leds.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_data/leds-lp55xx.h>
  20. #include "leds-lp55xx-common.h"
  21. static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
  22. {
  23. return container_of(cdev, struct lp55xx_led, cdev);
  24. }
  25. static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
  26. {
  27. return cdev_to_lp55xx_led(dev_get_drvdata(dev));
  28. }
  29. static void lp55xx_reset_device(struct lp55xx_chip *chip)
  30. {
  31. struct lp55xx_device_config *cfg = chip->cfg;
  32. u8 addr = cfg->reset.addr;
  33. u8 val = cfg->reset.val;
  34. /* no error checking here because no ACK from the device after reset */
  35. lp55xx_write(chip, addr, val);
  36. }
  37. static int lp55xx_detect_device(struct lp55xx_chip *chip)
  38. {
  39. struct lp55xx_device_config *cfg = chip->cfg;
  40. u8 addr = cfg->enable.addr;
  41. u8 val = cfg->enable.val;
  42. int ret;
  43. ret = lp55xx_write(chip, addr, val);
  44. if (ret)
  45. return ret;
  46. usleep_range(1000, 2000);
  47. ret = lp55xx_read(chip, addr, &val);
  48. if (ret)
  49. return ret;
  50. if (val != cfg->enable.val)
  51. return -ENODEV;
  52. return 0;
  53. }
  54. static int lp55xx_post_init_device(struct lp55xx_chip *chip)
  55. {
  56. struct lp55xx_device_config *cfg = chip->cfg;
  57. if (!cfg->post_init_device)
  58. return 0;
  59. return cfg->post_init_device(chip);
  60. }
  61. static ssize_t lp55xx_show_current(struct device *dev,
  62. struct device_attribute *attr,
  63. char *buf)
  64. {
  65. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  66. return sprintf(buf, "%d\n", led->led_current);
  67. }
  68. static ssize_t lp55xx_store_current(struct device *dev,
  69. struct device_attribute *attr,
  70. const char *buf, size_t len)
  71. {
  72. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  73. struct lp55xx_chip *chip = led->chip;
  74. unsigned long curr;
  75. if (kstrtoul(buf, 0, &curr))
  76. return -EINVAL;
  77. if (curr > led->max_current)
  78. return -EINVAL;
  79. if (!chip->cfg->set_led_current)
  80. return len;
  81. mutex_lock(&chip->lock);
  82. chip->cfg->set_led_current(led, (u8)curr);
  83. mutex_unlock(&chip->lock);
  84. return len;
  85. }
  86. static ssize_t lp55xx_show_max_current(struct device *dev,
  87. struct device_attribute *attr,
  88. char *buf)
  89. {
  90. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  91. return sprintf(buf, "%d\n", led->max_current);
  92. }
  93. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
  94. lp55xx_store_current);
  95. static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
  96. static struct attribute *lp55xx_led_attributes[] = {
  97. &dev_attr_led_current.attr,
  98. &dev_attr_max_current.attr,
  99. NULL,
  100. };
  101. static struct attribute_group lp55xx_led_attr_group = {
  102. .attrs = lp55xx_led_attributes
  103. };
  104. static void lp55xx_set_brightness(struct led_classdev *cdev,
  105. enum led_brightness brightness)
  106. {
  107. struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
  108. led->brightness = (u8)brightness;
  109. schedule_work(&led->brightness_work);
  110. }
  111. static int lp55xx_init_led(struct lp55xx_led *led,
  112. struct lp55xx_chip *chip, int chan)
  113. {
  114. struct lp55xx_platform_data *pdata = chip->pdata;
  115. struct lp55xx_device_config *cfg = chip->cfg;
  116. struct device *dev = &chip->cl->dev;
  117. char name[32];
  118. int ret;
  119. int max_channel = cfg->max_channel;
  120. if (chan >= max_channel) {
  121. dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
  122. return -EINVAL;
  123. }
  124. if (pdata->led_config[chan].led_current == 0)
  125. return 0;
  126. led->led_current = pdata->led_config[chan].led_current;
  127. led->max_current = pdata->led_config[chan].max_current;
  128. led->chan_nr = pdata->led_config[chan].chan_nr;
  129. if (led->chan_nr >= max_channel) {
  130. dev_err(dev, "Use channel numbers between 0 and %d\n",
  131. max_channel - 1);
  132. return -EINVAL;
  133. }
  134. led->cdev.brightness_set = lp55xx_set_brightness;
  135. if (pdata->led_config[chan].name) {
  136. led->cdev.name = pdata->led_config[chan].name;
  137. } else {
  138. snprintf(name, sizeof(name), "%s:channel%d",
  139. pdata->label ? : chip->cl->name, chan);
  140. led->cdev.name = name;
  141. }
  142. /*
  143. * register led class device for each channel and
  144. * add device attributes
  145. */
  146. ret = led_classdev_register(dev, &led->cdev);
  147. if (ret) {
  148. dev_err(dev, "led register err: %d\n", ret);
  149. return ret;
  150. }
  151. ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
  152. if (ret) {
  153. dev_err(dev, "led sysfs err: %d\n", ret);
  154. led_classdev_unregister(&led->cdev);
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
  160. {
  161. struct lp55xx_chip *chip = context;
  162. struct device *dev = &chip->cl->dev;
  163. if (!fw) {
  164. dev_err(dev, "firmware request failed\n");
  165. goto out;
  166. }
  167. /* handling firmware data is chip dependent */
  168. mutex_lock(&chip->lock);
  169. chip->fw = fw;
  170. if (chip->cfg->firmware_cb)
  171. chip->cfg->firmware_cb(chip);
  172. mutex_unlock(&chip->lock);
  173. out:
  174. /* firmware should be released for other channel use */
  175. release_firmware(chip->fw);
  176. }
  177. static int lp55xx_request_firmware(struct lp55xx_chip *chip)
  178. {
  179. const char *name = chip->cl->name;
  180. struct device *dev = &chip->cl->dev;
  181. return request_firmware_nowait(THIS_MODULE, true, name, dev,
  182. GFP_KERNEL, chip, lp55xx_firmware_loaded);
  183. }
  184. static ssize_t lp55xx_show_engine_select(struct device *dev,
  185. struct device_attribute *attr,
  186. char *buf)
  187. {
  188. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  189. struct lp55xx_chip *chip = led->chip;
  190. return sprintf(buf, "%d\n", chip->engine_idx);
  191. }
  192. static ssize_t lp55xx_store_engine_select(struct device *dev,
  193. struct device_attribute *attr,
  194. const char *buf, size_t len)
  195. {
  196. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  197. struct lp55xx_chip *chip = led->chip;
  198. unsigned long val;
  199. int ret;
  200. if (kstrtoul(buf, 0, &val))
  201. return -EINVAL;
  202. /* select the engine to be run */
  203. switch (val) {
  204. case LP55XX_ENGINE_1:
  205. case LP55XX_ENGINE_2:
  206. case LP55XX_ENGINE_3:
  207. mutex_lock(&chip->lock);
  208. chip->engine_idx = val;
  209. ret = lp55xx_request_firmware(chip);
  210. mutex_unlock(&chip->lock);
  211. break;
  212. default:
  213. dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
  214. return -EINVAL;
  215. }
  216. if (ret) {
  217. dev_err(dev, "request firmware err: %d\n", ret);
  218. return ret;
  219. }
  220. return len;
  221. }
  222. static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
  223. {
  224. if (chip->cfg->run_engine)
  225. chip->cfg->run_engine(chip, start);
  226. }
  227. static ssize_t lp55xx_store_engine_run(struct device *dev,
  228. struct device_attribute *attr,
  229. const char *buf, size_t len)
  230. {
  231. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  232. struct lp55xx_chip *chip = led->chip;
  233. unsigned long val;
  234. if (kstrtoul(buf, 0, &val))
  235. return -EINVAL;
  236. /* run or stop the selected engine */
  237. if (val <= 0) {
  238. lp55xx_run_engine(chip, false);
  239. return len;
  240. }
  241. mutex_lock(&chip->lock);
  242. lp55xx_run_engine(chip, true);
  243. mutex_unlock(&chip->lock);
  244. return len;
  245. }
  246. static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR,
  247. lp55xx_show_engine_select, lp55xx_store_engine_select);
  248. static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run);
  249. static struct attribute *lp55xx_engine_attributes[] = {
  250. &dev_attr_select_engine.attr,
  251. &dev_attr_run_engine.attr,
  252. NULL,
  253. };
  254. static const struct attribute_group lp55xx_engine_attr_group = {
  255. .attrs = lp55xx_engine_attributes,
  256. };
  257. int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
  258. {
  259. return i2c_smbus_write_byte_data(chip->cl, reg, val);
  260. }
  261. EXPORT_SYMBOL_GPL(lp55xx_write);
  262. int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
  263. {
  264. s32 ret;
  265. ret = i2c_smbus_read_byte_data(chip->cl, reg);
  266. if (ret < 0)
  267. return ret;
  268. *val = ret;
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(lp55xx_read);
  272. int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
  273. {
  274. int ret;
  275. u8 tmp;
  276. ret = lp55xx_read(chip, reg, &tmp);
  277. if (ret)
  278. return ret;
  279. tmp &= ~mask;
  280. tmp |= val & mask;
  281. return lp55xx_write(chip, reg, tmp);
  282. }
  283. EXPORT_SYMBOL_GPL(lp55xx_update_bits);
  284. int lp55xx_init_device(struct lp55xx_chip *chip)
  285. {
  286. struct lp55xx_platform_data *pdata;
  287. struct lp55xx_device_config *cfg;
  288. struct device *dev = &chip->cl->dev;
  289. int ret = 0;
  290. WARN_ON(!chip);
  291. pdata = chip->pdata;
  292. cfg = chip->cfg;
  293. if (!pdata || !cfg)
  294. return -EINVAL;
  295. if (pdata->setup_resources) {
  296. ret = pdata->setup_resources();
  297. if (ret < 0) {
  298. dev_err(dev, "setup resoure err: %d\n", ret);
  299. goto err;
  300. }
  301. }
  302. if (pdata->enable) {
  303. pdata->enable(0);
  304. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  305. pdata->enable(1);
  306. usleep_range(1000, 2000); /* 500us abs min. */
  307. }
  308. lp55xx_reset_device(chip);
  309. /*
  310. * Exact value is not available. 10 - 20ms
  311. * appears to be enough for reset.
  312. */
  313. usleep_range(10000, 20000);
  314. ret = lp55xx_detect_device(chip);
  315. if (ret) {
  316. dev_err(dev, "device detection err: %d\n", ret);
  317. goto err;
  318. }
  319. /* chip specific initialization */
  320. ret = lp55xx_post_init_device(chip);
  321. if (ret) {
  322. dev_err(dev, "post init device err: %d\n", ret);
  323. goto err_post_init;
  324. }
  325. return 0;
  326. err_post_init:
  327. lp55xx_deinit_device(chip);
  328. err:
  329. return ret;
  330. }
  331. EXPORT_SYMBOL_GPL(lp55xx_init_device);
  332. void lp55xx_deinit_device(struct lp55xx_chip *chip)
  333. {
  334. struct lp55xx_platform_data *pdata = chip->pdata;
  335. if (pdata->enable)
  336. pdata->enable(0);
  337. if (pdata->release_resources)
  338. pdata->release_resources();
  339. }
  340. EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
  341. int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
  342. {
  343. struct lp55xx_platform_data *pdata = chip->pdata;
  344. struct lp55xx_device_config *cfg = chip->cfg;
  345. int num_channels = pdata->num_channels;
  346. struct lp55xx_led *each;
  347. u8 led_current;
  348. int ret;
  349. int i;
  350. if (!cfg->brightness_work_fn) {
  351. dev_err(&chip->cl->dev, "empty brightness configuration\n");
  352. return -EINVAL;
  353. }
  354. for (i = 0; i < num_channels; i++) {
  355. /* do not initialize channels that are not connected */
  356. if (pdata->led_config[i].led_current == 0)
  357. continue;
  358. led_current = pdata->led_config[i].led_current;
  359. each = led + i;
  360. ret = lp55xx_init_led(each, chip, i);
  361. if (ret)
  362. goto err_init_led;
  363. INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
  364. chip->num_leds++;
  365. each->chip = chip;
  366. /* setting led current at each channel */
  367. if (cfg->set_led_current)
  368. cfg->set_led_current(each, led_current);
  369. }
  370. return 0;
  371. err_init_led:
  372. lp55xx_unregister_leds(led, chip);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL_GPL(lp55xx_register_leds);
  376. void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
  377. {
  378. int i;
  379. struct lp55xx_led *each;
  380. for (i = 0; i < chip->num_leds; i++) {
  381. each = led + i;
  382. led_classdev_unregister(&each->cdev);
  383. flush_work(&each->brightness_work);
  384. }
  385. }
  386. EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
  387. int lp55xx_register_sysfs(struct lp55xx_chip *chip)
  388. {
  389. struct device *dev = &chip->cl->dev;
  390. struct lp55xx_device_config *cfg = chip->cfg;
  391. int ret;
  392. if (!cfg->run_engine || !cfg->firmware_cb)
  393. goto dev_specific_attrs;
  394. ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
  395. if (ret)
  396. return ret;
  397. dev_specific_attrs:
  398. return cfg->dev_attr_group ?
  399. sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
  400. }
  401. EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
  402. void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
  403. {
  404. struct device *dev = &chip->cl->dev;
  405. struct lp55xx_device_config *cfg = chip->cfg;
  406. if (cfg->dev_attr_group)
  407. sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
  408. sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
  409. }
  410. EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
  411. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  412. MODULE_DESCRIPTION("LP55xx Common Driver");
  413. MODULE_LICENSE("GPL");