leds-lp55xx-common.c 12 KB

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