leds-lp55xx-common.c 13 KB

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