leds-lp55xx-common.c 14 KB

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