platform_lcd.c 3.5 KB

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