leds-lp55xx-common.c 14 KB

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