leds-lp8788.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * TI LP8788 MFD - keyled 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. */
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/leds.h>
  18. #include <linux/mutex.h>
  19. #include <linux/mfd/lp8788.h>
  20. #include <linux/mfd/lp8788-isink.h>
  21. #define MAX_BRIGHTNESS LP8788_ISINK_MAX_PWM
  22. #define DEFAULT_LED_NAME "keyboard-backlight"
  23. struct lp8788_led {
  24. struct lp8788 *lp;
  25. struct mutex lock;
  26. struct work_struct work;
  27. struct led_classdev led_dev;
  28. enum lp8788_isink_number isink_num;
  29. enum led_brightness brightness;
  30. int on;
  31. };
  32. struct lp8788_led_config {
  33. enum lp8788_isink_scale scale;
  34. enum lp8788_isink_number num;
  35. int iout;
  36. };
  37. static struct lp8788_led_config default_led_config = {
  38. .scale = LP8788_ISINK_SCALE_100mA,
  39. .num = LP8788_ISINK_3,
  40. .iout = 0,
  41. };
  42. static int lp8788_led_init_device(struct lp8788_led *led,
  43. struct lp8788_led_platform_data *pdata)
  44. {
  45. struct lp8788_led_config *cfg = &default_led_config;
  46. u8 addr, mask, val;
  47. int ret;
  48. if (pdata) {
  49. cfg->scale = pdata->scale;
  50. cfg->num = pdata->num;
  51. cfg->iout = pdata->iout_code;
  52. }
  53. led->isink_num = cfg->num;
  54. /* scale configuration */
  55. addr = LP8788_ISINK_CTRL;
  56. mask = 1 << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
  57. val = cfg->scale << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
  58. ret = lp8788_update_bits(led->lp, addr, mask, val);
  59. if (ret)
  60. return ret;
  61. /* current configuration */
  62. addr = lp8788_iout_addr[cfg->num];
  63. mask = lp8788_iout_mask[cfg->num];
  64. val = cfg->iout;
  65. return lp8788_update_bits(led->lp, addr, mask, val);
  66. }
  67. static void lp8788_led_enable(struct lp8788_led *led,
  68. enum lp8788_isink_number num, int on)
  69. {
  70. u8 mask = 1 << num;
  71. u8 val = on << num;
  72. if (lp8788_update_bits(led->lp, LP8788_ISINK_CTRL, mask, val))
  73. return;
  74. led->on = on;
  75. }
  76. static void lp8788_led_work(struct work_struct *work)
  77. {
  78. struct lp8788_led *led = container_of(work, struct lp8788_led, work);
  79. enum lp8788_isink_number num = led->isink_num;
  80. int enable;
  81. u8 val = led->brightness;
  82. mutex_lock(&led->lock);
  83. switch (num) {
  84. case LP8788_ISINK_1:
  85. case LP8788_ISINK_2:
  86. case LP8788_ISINK_3:
  87. lp8788_write_byte(led->lp, lp8788_pwm_addr[num], val);
  88. break;
  89. default:
  90. mutex_unlock(&led->lock);
  91. return;
  92. }
  93. enable = (val > 0) ? 1 : 0;
  94. if (enable != led->on)
  95. lp8788_led_enable(led, num, enable);
  96. mutex_unlock(&led->lock);
  97. }
  98. static void lp8788_brightness_set(struct led_classdev *led_cdev,
  99. enum led_brightness brt_val)
  100. {
  101. struct lp8788_led *led =
  102. container_of(led_cdev, struct lp8788_led, led_dev);
  103. led->brightness = brt_val;
  104. schedule_work(&led->work);
  105. }
  106. static int lp8788_led_probe(struct platform_device *pdev)
  107. {
  108. struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
  109. struct lp8788_led_platform_data *led_pdata;
  110. struct lp8788_led *led;
  111. struct device *dev = &pdev->dev;
  112. int ret;
  113. led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL);
  114. if (!led)
  115. return -ENOMEM;
  116. led->lp = lp;
  117. led->led_dev.max_brightness = MAX_BRIGHTNESS;
  118. led->led_dev.brightness_set = lp8788_brightness_set;
  119. led_pdata = lp->pdata ? lp->pdata->led_pdata : NULL;
  120. if (!led_pdata || !led_pdata->name)
  121. led->led_dev.name = DEFAULT_LED_NAME;
  122. else
  123. led->led_dev.name = led_pdata->name;
  124. mutex_init(&led->lock);
  125. INIT_WORK(&led->work, lp8788_led_work);
  126. platform_set_drvdata(pdev, led);
  127. ret = lp8788_led_init_device(led, led_pdata);
  128. if (ret) {
  129. dev_err(dev, "led init device err: %d\n", ret);
  130. return ret;
  131. }
  132. ret = led_classdev_register(dev, &led->led_dev);
  133. if (ret) {
  134. dev_err(dev, "led register err: %d\n", ret);
  135. return ret;
  136. }
  137. return 0;
  138. }
  139. static int lp8788_led_remove(struct platform_device *pdev)
  140. {
  141. struct lp8788_led *led = platform_get_drvdata(pdev);
  142. led_classdev_unregister(&led->led_dev);
  143. flush_work(&led->work);
  144. return 0;
  145. }
  146. static struct platform_driver lp8788_led_driver = {
  147. .probe = lp8788_led_probe,
  148. .remove = lp8788_led_remove,
  149. .driver = {
  150. .name = LP8788_DEV_KEYLED,
  151. },
  152. };
  153. module_platform_driver(lp8788_led_driver);
  154. MODULE_DESCRIPTION("Texas Instruments LP8788 Keyboard LED Driver");
  155. MODULE_AUTHOR("Milo Kim");
  156. MODULE_LICENSE("GPL");
  157. MODULE_ALIAS("platform:lp8788-keyled");