leds-lp55xx-common.c 14 KB

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