leds-lp55xx-common.c 14 KB

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