pcf50633-backlight.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * PCF50633 backlight device driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/backlight.h>
  20. #include <linux/fb.h>
  21. #include <linux/mfd/pcf50633/core.h>
  22. #include <linux/mfd/pcf50633/backlight.h>
  23. struct pcf50633_bl {
  24. struct pcf50633 *pcf;
  25. struct backlight_device *bl;
  26. unsigned int brightness;
  27. unsigned int brightness_limit;
  28. };
  29. /*
  30. * pcf50633_bl_set_brightness_limit
  31. *
  32. * Update the brightness limit for the pc50633 backlight. The actual brightness
  33. * will not go above the limit. This is useful to limit power drain for example
  34. * on low battery.
  35. *
  36. * @dev: Pointer to a pcf50633 device
  37. * @limit: The brightness limit. Valid values are 0-63
  38. */
  39. int pcf50633_bl_set_brightness_limit(struct pcf50633 *pcf, unsigned int limit)
  40. {
  41. struct pcf50633_bl *pcf_bl = platform_get_drvdata(pcf->bl_pdev);
  42. if (!pcf_bl)
  43. return -ENODEV;
  44. pcf_bl->brightness_limit = limit & 0x3f;
  45. backlight_update_status(pcf_bl->bl);
  46. return 0;
  47. }
  48. static int pcf50633_bl_update_status(struct backlight_device *bl)
  49. {
  50. struct pcf50633_bl *pcf_bl = bl_get_data(bl);
  51. unsigned int new_brightness;
  52. if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK) ||
  53. bl->props.power != FB_BLANK_UNBLANK)
  54. new_brightness = 0;
  55. else if (bl->props.brightness < pcf_bl->brightness_limit)
  56. new_brightness = bl->props.brightness;
  57. else
  58. new_brightness = pcf_bl->brightness_limit;
  59. if (pcf_bl->brightness == new_brightness)
  60. return 0;
  61. if (new_brightness) {
  62. pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDOUT,
  63. new_brightness);
  64. if (!pcf_bl->brightness)
  65. pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDENA, 1);
  66. } else {
  67. pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDENA, 0);
  68. }
  69. pcf_bl->brightness = new_brightness;
  70. return 0;
  71. }
  72. static int pcf50633_bl_get_brightness(struct backlight_device *bl)
  73. {
  74. struct pcf50633_bl *pcf_bl = bl_get_data(bl);
  75. return pcf_bl->brightness;
  76. }
  77. static const struct backlight_ops pcf50633_bl_ops = {
  78. .get_brightness = pcf50633_bl_get_brightness,
  79. .update_status = pcf50633_bl_update_status,
  80. .options = BL_CORE_SUSPENDRESUME,
  81. };
  82. static int pcf50633_bl_probe(struct platform_device *pdev)
  83. {
  84. struct pcf50633_bl *pcf_bl;
  85. struct device *parent = pdev->dev.parent;
  86. struct pcf50633_platform_data *pcf50633_data = dev_get_platdata(parent);
  87. struct pcf50633_bl_platform_data *pdata = pcf50633_data->backlight_data;
  88. struct backlight_properties bl_props;
  89. pcf_bl = devm_kzalloc(&pdev->dev, sizeof(*pcf_bl), GFP_KERNEL);
  90. if (!pcf_bl)
  91. return -ENOMEM;
  92. memset(&bl_props, 0, sizeof(bl_props));
  93. bl_props.type = BACKLIGHT_RAW;
  94. bl_props.max_brightness = 0x3f;
  95. bl_props.power = FB_BLANK_UNBLANK;
  96. if (pdata) {
  97. bl_props.brightness = pdata->default_brightness;
  98. pcf_bl->brightness_limit = pdata->default_brightness_limit;
  99. } else {
  100. bl_props.brightness = 0x3f;
  101. pcf_bl->brightness_limit = 0x3f;
  102. }
  103. pcf_bl->pcf = dev_to_pcf50633(pdev->dev.parent);
  104. pcf_bl->bl = devm_backlight_device_register(&pdev->dev, pdev->name,
  105. &pdev->dev, pcf_bl,
  106. &pcf50633_bl_ops, &bl_props);
  107. if (IS_ERR(pcf_bl->bl))
  108. return PTR_ERR(pcf_bl->bl);
  109. platform_set_drvdata(pdev, pcf_bl);
  110. pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDDIM, pdata->ramp_time);
  111. /*
  112. * Should be different from bl_props.brightness, so we do not exit
  113. * update_status early the first time it's called
  114. */
  115. pcf_bl->brightness = pcf_bl->bl->props.brightness + 1;
  116. backlight_update_status(pcf_bl->bl);
  117. return 0;
  118. }
  119. static struct platform_driver pcf50633_bl_driver = {
  120. .probe = pcf50633_bl_probe,
  121. .driver = {
  122. .name = "pcf50633-backlight",
  123. },
  124. };
  125. module_platform_driver(pcf50633_bl_driver);
  126. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  127. MODULE_DESCRIPTION("PCF50633 backlight driver");
  128. MODULE_LICENSE("GPL");
  129. MODULE_ALIAS("platform:pcf50633-backlight");