leds-lp55xx-common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. if (!fw) {
  171. dev_err(dev, "firmware request failed\n");
  172. goto out;
  173. }
  174. /* handling firmware data is chip dependent */
  175. mutex_lock(&chip->lock);
  176. chip->fw = fw;
  177. if (chip->cfg->firmware_cb)
  178. chip->cfg->firmware_cb(chip);
  179. mutex_unlock(&chip->lock);
  180. out:
  181. /* firmware should be released for other channel use */
  182. release_firmware(chip->fw);
  183. }
  184. static int lp55xx_request_firmware(struct lp55xx_chip *chip)
  185. {
  186. const char *name = chip->cl->name;
  187. struct device *dev = &chip->cl->dev;
  188. return request_firmware_nowait(THIS_MODULE, true, name, dev,
  189. GFP_KERNEL, chip, lp55xx_firmware_loaded);
  190. }
  191. static ssize_t lp55xx_show_engine_select(struct device *dev,
  192. struct device_attribute *attr,
  193. char *buf)
  194. {
  195. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  196. struct lp55xx_chip *chip = led->chip;
  197. return sprintf(buf, "%d\n", chip->engine_idx);
  198. }
  199. static ssize_t lp55xx_store_engine_select(struct device *dev,
  200. struct device_attribute *attr,
  201. const char *buf, size_t len)
  202. {
  203. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  204. struct lp55xx_chip *chip = led->chip;
  205. unsigned long val;
  206. int ret;
  207. if (kstrtoul(buf, 0, &val))
  208. return -EINVAL;
  209. /* select the engine to be run */
  210. switch (val) {
  211. case LP55XX_ENGINE_1:
  212. case LP55XX_ENGINE_2:
  213. case LP55XX_ENGINE_3:
  214. mutex_lock(&chip->lock);
  215. chip->engine_idx = val;
  216. ret = lp55xx_request_firmware(chip);
  217. mutex_unlock(&chip->lock);
  218. break;
  219. default:
  220. dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
  221. return -EINVAL;
  222. }
  223. if (ret) {
  224. dev_err(dev, "request firmware err: %d\n", ret);
  225. return ret;
  226. }
  227. return len;
  228. }
  229. static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
  230. {
  231. if (chip->cfg->run_engine)
  232. chip->cfg->run_engine(chip, start);
  233. }
  234. static ssize_t lp55xx_store_engine_run(struct device *dev,
  235. struct device_attribute *attr,
  236. const char *buf, size_t len)
  237. {
  238. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  239. struct lp55xx_chip *chip = led->chip;
  240. unsigned long val;
  241. if (kstrtoul(buf, 0, &val))
  242. return -EINVAL;
  243. /* run or stop the selected engine */
  244. if (val <= 0) {
  245. lp55xx_run_engine(chip, false);
  246. return len;
  247. }
  248. mutex_lock(&chip->lock);
  249. lp55xx_run_engine(chip, true);
  250. mutex_unlock(&chip->lock);
  251. return len;
  252. }
  253. static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR,
  254. lp55xx_show_engine_select, lp55xx_store_engine_select);
  255. static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run);
  256. static struct attribute *lp55xx_engine_attributes[] = {
  257. &dev_attr_select_engine.attr,
  258. &dev_attr_run_engine.attr,
  259. NULL,
  260. };
  261. static const struct attribute_group lp55xx_engine_attr_group = {
  262. .attrs = lp55xx_engine_attributes,
  263. };
  264. int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
  265. {
  266. return i2c_smbus_write_byte_data(chip->cl, reg, val);
  267. }
  268. EXPORT_SYMBOL_GPL(lp55xx_write);
  269. int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
  270. {
  271. s32 ret;
  272. ret = i2c_smbus_read_byte_data(chip->cl, reg);
  273. if (ret < 0)
  274. return ret;
  275. *val = ret;
  276. return 0;
  277. }
  278. EXPORT_SYMBOL_GPL(lp55xx_read);
  279. int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
  280. {
  281. int ret;
  282. u8 tmp;
  283. ret = lp55xx_read(chip, reg, &tmp);
  284. if (ret)
  285. return ret;
  286. tmp &= ~mask;
  287. tmp |= val & mask;
  288. return lp55xx_write(chip, reg, tmp);
  289. }
  290. EXPORT_SYMBOL_GPL(lp55xx_update_bits);
  291. bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
  292. {
  293. struct clk *clk;
  294. int err;
  295. clk = devm_clk_get(&chip->cl->dev, "32k_clk");
  296. if (IS_ERR(clk))
  297. goto use_internal_clk;
  298. err = clk_prepare_enable(clk);
  299. if (err)
  300. goto use_internal_clk;
  301. if (clk_get_rate(clk) != LP55XX_CLK_32K) {
  302. clk_disable_unprepare(clk);
  303. goto use_internal_clk;
  304. }
  305. dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
  306. chip->clk = clk;
  307. return true;
  308. use_internal_clk:
  309. dev_info(&chip->cl->dev, "internal clock used\n");
  310. return false;
  311. }
  312. EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
  313. int lp55xx_init_device(struct lp55xx_chip *chip)
  314. {
  315. struct lp55xx_platform_data *pdata;
  316. struct lp55xx_device_config *cfg;
  317. struct device *dev = &chip->cl->dev;
  318. int ret = 0;
  319. WARN_ON(!chip);
  320. pdata = chip->pdata;
  321. cfg = chip->cfg;
  322. if (!pdata || !cfg)
  323. return -EINVAL;
  324. if (gpio_is_valid(pdata->enable_gpio)) {
  325. ret = devm_gpio_request_one(dev, pdata->enable_gpio,
  326. GPIOF_DIR_OUT, "lp5523_enable");
  327. if (ret < 0) {
  328. dev_err(dev, "could not acquire enable gpio (err=%d)\n",
  329. ret);
  330. goto err;
  331. }
  332. gpio_set_value(pdata->enable_gpio, 0);
  333. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  334. gpio_set_value(pdata->enable_gpio, 1);
  335. usleep_range(1000, 2000); /* 500us abs min. */
  336. }
  337. lp55xx_reset_device(chip);
  338. /*
  339. * Exact value is not available. 10 - 20ms
  340. * appears to be enough for reset.
  341. */
  342. usleep_range(10000, 20000);
  343. ret = lp55xx_detect_device(chip);
  344. if (ret) {
  345. dev_err(dev, "device detection err: %d\n", ret);
  346. goto err;
  347. }
  348. /* chip specific initialization */
  349. ret = lp55xx_post_init_device(chip);
  350. if (ret) {
  351. dev_err(dev, "post init device err: %d\n", ret);
  352. goto err_post_init;
  353. }
  354. return 0;
  355. err_post_init:
  356. lp55xx_deinit_device(chip);
  357. err:
  358. return ret;
  359. }
  360. EXPORT_SYMBOL_GPL(lp55xx_init_device);
  361. void lp55xx_deinit_device(struct lp55xx_chip *chip)
  362. {
  363. struct lp55xx_platform_data *pdata = chip->pdata;
  364. if (chip->clk)
  365. clk_disable_unprepare(chip->clk);
  366. if (gpio_is_valid(pdata->enable_gpio))
  367. gpio_set_value(pdata->enable_gpio, 0);
  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. pdata->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0);
  472. /* LP8501 specific */
  473. of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
  474. dev->platform_data = pdata;
  475. return 0;
  476. }
  477. EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
  478. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  479. MODULE_DESCRIPTION("LP55xx Common Driver");
  480. MODULE_LICENSE("GPL");