platform_lcd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* drivers/video/backlight/platform_lcd.c
  3. *
  4. * Copyright 2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * Generic platform-device LCD power control interface.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/fb.h>
  12. #include <linux/backlight.h>
  13. #include <linux/lcd.h>
  14. #include <linux/of.h>
  15. #include <linux/slab.h>
  16. #include <video/platform_lcd.h>
  17. struct platform_lcd {
  18. struct device *us;
  19. struct lcd_device *lcd;
  20. struct plat_lcd_data *pdata;
  21. unsigned int power;
  22. unsigned int suspended:1;
  23. };
  24. static inline struct platform_lcd *to_our_lcd(struct lcd_device *lcd)
  25. {
  26. return lcd_get_data(lcd);
  27. }
  28. static int platform_lcd_get_power(struct lcd_device *lcd)
  29. {
  30. struct platform_lcd *plcd = to_our_lcd(lcd);
  31. return plcd->power;
  32. }
  33. static int platform_lcd_set_power(struct lcd_device *lcd, int power)
  34. {
  35. struct platform_lcd *plcd = to_our_lcd(lcd);
  36. int lcd_power = 1;
  37. if (power == FB_BLANK_POWERDOWN || plcd->suspended)
  38. lcd_power = 0;
  39. plcd->pdata->set_power(plcd->pdata, lcd_power);
  40. plcd->power = power;
  41. return 0;
  42. }
  43. static int platform_lcd_match(struct lcd_device *lcd, struct fb_info *info)
  44. {
  45. struct platform_lcd *plcd = to_our_lcd(lcd);
  46. struct plat_lcd_data *pdata = plcd->pdata;
  47. if (pdata->match_fb)
  48. return pdata->match_fb(pdata, info);
  49. return plcd->us->parent == info->device;
  50. }
  51. static struct lcd_ops platform_lcd_ops = {
  52. .get_power = platform_lcd_get_power,
  53. .set_power = platform_lcd_set_power,
  54. .check_fb = platform_lcd_match,
  55. };
  56. static int platform_lcd_probe(struct platform_device *pdev)
  57. {
  58. struct plat_lcd_data *pdata;
  59. struct platform_lcd *plcd;
  60. struct device *dev = &pdev->dev;
  61. int err;
  62. pdata = dev_get_platdata(&pdev->dev);
  63. if (!pdata) {
  64. dev_err(dev, "no platform data supplied\n");
  65. return -EINVAL;
  66. }
  67. if (pdata->probe) {
  68. err = pdata->probe(pdata);
  69. if (err)
  70. return err;
  71. }
  72. plcd = devm_kzalloc(&pdev->dev, sizeof(struct platform_lcd),
  73. GFP_KERNEL);
  74. if (!plcd)
  75. return -ENOMEM;
  76. plcd->us = dev;
  77. plcd->pdata = pdata;
  78. plcd->lcd = devm_lcd_device_register(&pdev->dev, dev_name(dev), dev,
  79. plcd, &platform_lcd_ops);
  80. if (IS_ERR(plcd->lcd)) {
  81. dev_err(dev, "cannot register lcd device\n");
  82. return PTR_ERR(plcd->lcd);
  83. }
  84. platform_set_drvdata(pdev, plcd);
  85. platform_lcd_set_power(plcd->lcd, FB_BLANK_NORMAL);
  86. return 0;
  87. }
  88. #ifdef CONFIG_PM_SLEEP
  89. static int platform_lcd_suspend(struct device *dev)
  90. {
  91. struct platform_lcd *plcd = dev_get_drvdata(dev);
  92. plcd->suspended = 1;
  93. platform_lcd_set_power(plcd->lcd, plcd->power);
  94. return 0;
  95. }
  96. static int platform_lcd_resume(struct device *dev)
  97. {
  98. struct platform_lcd *plcd = dev_get_drvdata(dev);
  99. plcd->suspended = 0;
  100. platform_lcd_set_power(plcd->lcd, plcd->power);
  101. return 0;
  102. }
  103. #endif
  104. static SIMPLE_DEV_PM_OPS(platform_lcd_pm_ops, platform_lcd_suspend,
  105. platform_lcd_resume);
  106. #ifdef CONFIG_OF
  107. static const struct of_device_id platform_lcd_of_match[] = {
  108. { .compatible = "platform-lcd" },
  109. {},
  110. };
  111. MODULE_DEVICE_TABLE(of, platform_lcd_of_match);
  112. #endif
  113. static struct platform_driver platform_lcd_driver = {
  114. .driver = {
  115. .name = "platform-lcd",
  116. .pm = &platform_lcd_pm_ops,
  117. .of_match_table = of_match_ptr(platform_lcd_of_match),
  118. },
  119. .probe = platform_lcd_probe,
  120. };
  121. module_platform_driver(platform_lcd_driver);
  122. MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
  123. MODULE_LICENSE("GPL v2");
  124. MODULE_ALIAS("platform:platform-lcd");