da9052_bl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Backlight Driver for Dialog DA9052 PMICs
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <linux/backlight.h>
  15. #include <linux/delay.h>
  16. #include <linux/fb.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mfd/da9052/da9052.h>
  20. #include <linux/mfd/da9052/reg.h>
  21. #define DA9052_MAX_BRIGHTNESS 0xFF
  22. enum {
  23. DA9052_WLEDS_OFF,
  24. DA9052_WLEDS_ON,
  25. };
  26. enum {
  27. DA9052_TYPE_WLED1,
  28. DA9052_TYPE_WLED2,
  29. DA9052_TYPE_WLED3,
  30. };
  31. static const unsigned char wled_bank[] = {
  32. DA9052_LED1_CONF_REG,
  33. DA9052_LED2_CONF_REG,
  34. DA9052_LED3_CONF_REG,
  35. };
  36. struct da9052_bl {
  37. struct da9052 *da9052;
  38. uint brightness;
  39. uint state;
  40. uint led_reg;
  41. };
  42. static int da9052_adjust_wled_brightness(struct da9052_bl *wleds)
  43. {
  44. unsigned char boost_en;
  45. unsigned char i_sink;
  46. int ret;
  47. boost_en = 0x3F;
  48. i_sink = 0xFF;
  49. if (wleds->state == DA9052_WLEDS_OFF) {
  50. boost_en = 0x00;
  51. i_sink = 0x00;
  52. }
  53. ret = da9052_reg_write(wleds->da9052, DA9052_BOOST_REG, boost_en);
  54. if (ret < 0)
  55. return ret;
  56. ret = da9052_reg_write(wleds->da9052, DA9052_LED_CONT_REG, i_sink);
  57. if (ret < 0)
  58. return ret;
  59. ret = da9052_reg_write(wleds->da9052, wled_bank[wleds->led_reg], 0x0);
  60. if (ret < 0)
  61. return ret;
  62. usleep_range(10000, 11000);
  63. if (wleds->brightness) {
  64. ret = da9052_reg_write(wleds->da9052, wled_bank[wleds->led_reg],
  65. wleds->brightness);
  66. if (ret < 0)
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. static int da9052_backlight_update_status(struct backlight_device *bl)
  72. {
  73. int brightness = bl->props.brightness;
  74. struct da9052_bl *wleds = bl_get_data(bl);
  75. wleds->brightness = brightness;
  76. wleds->state = DA9052_WLEDS_ON;
  77. return da9052_adjust_wled_brightness(wleds);
  78. }
  79. static int da9052_backlight_get_brightness(struct backlight_device *bl)
  80. {
  81. struct da9052_bl *wleds = bl_get_data(bl);
  82. return wleds->brightness;
  83. }
  84. static const struct backlight_ops da9052_backlight_ops = {
  85. .update_status = da9052_backlight_update_status,
  86. .get_brightness = da9052_backlight_get_brightness,
  87. };
  88. static int da9052_backlight_probe(struct platform_device *pdev)
  89. {
  90. struct backlight_device *bl;
  91. struct backlight_properties props;
  92. struct da9052_bl *wleds;
  93. wleds = devm_kzalloc(&pdev->dev, sizeof(struct da9052_bl), GFP_KERNEL);
  94. if (!wleds)
  95. return -ENOMEM;
  96. wleds->da9052 = dev_get_drvdata(pdev->dev.parent);
  97. wleds->brightness = 0;
  98. wleds->led_reg = platform_get_device_id(pdev)->driver_data;
  99. wleds->state = DA9052_WLEDS_OFF;
  100. props.type = BACKLIGHT_RAW;
  101. props.max_brightness = DA9052_MAX_BRIGHTNESS;
  102. bl = devm_backlight_device_register(&pdev->dev, pdev->name,
  103. wleds->da9052->dev, wleds,
  104. &da9052_backlight_ops, &props);
  105. if (IS_ERR(bl)) {
  106. dev_err(&pdev->dev, "Failed to register backlight\n");
  107. return PTR_ERR(bl);
  108. }
  109. bl->props.max_brightness = DA9052_MAX_BRIGHTNESS;
  110. bl->props.brightness = 0;
  111. platform_set_drvdata(pdev, bl);
  112. return da9052_adjust_wled_brightness(wleds);
  113. }
  114. static int da9052_backlight_remove(struct platform_device *pdev)
  115. {
  116. struct backlight_device *bl = platform_get_drvdata(pdev);
  117. struct da9052_bl *wleds = bl_get_data(bl);
  118. wleds->brightness = 0;
  119. wleds->state = DA9052_WLEDS_OFF;
  120. da9052_adjust_wled_brightness(wleds);
  121. return 0;
  122. }
  123. static const struct platform_device_id da9052_wled_ids[] = {
  124. {
  125. .name = "da9052-wled1",
  126. .driver_data = DA9052_TYPE_WLED1,
  127. },
  128. {
  129. .name = "da9052-wled2",
  130. .driver_data = DA9052_TYPE_WLED2,
  131. },
  132. {
  133. .name = "da9052-wled3",
  134. .driver_data = DA9052_TYPE_WLED3,
  135. },
  136. { },
  137. };
  138. static struct platform_driver da9052_wled_driver = {
  139. .probe = da9052_backlight_probe,
  140. .remove = da9052_backlight_remove,
  141. .id_table = da9052_wled_ids,
  142. .driver = {
  143. .name = "da9052-wled",
  144. },
  145. };
  146. module_platform_driver(da9052_wled_driver);
  147. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  148. MODULE_DESCRIPTION("Backlight driver for DA9052 PMIC");
  149. MODULE_LICENSE("GPL");
  150. MODULE_ALIAS("platform:da9052-backlight");