pcf50633-backlight.c 3.9 KB

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