platform_lcd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = pdev->dev.platform_data;
  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. dev_err(dev, "no memory for state\n");
  80. return -ENOMEM;
  81. }
  82. plcd->us = dev;
  83. plcd->pdata = pdata;
  84. plcd->lcd = lcd_device_register(dev_name(dev), dev,
  85. plcd, &platform_lcd_ops);
  86. if (IS_ERR(plcd->lcd)) {
  87. dev_err(dev, "cannot register lcd device\n");
  88. err = PTR_ERR(plcd->lcd);
  89. goto err;
  90. }
  91. platform_set_drvdata(pdev, plcd);
  92. platform_lcd_set_power(plcd->lcd, FB_BLANK_NORMAL);
  93. return 0;
  94. err:
  95. return err;
  96. }
  97. static int platform_lcd_remove(struct platform_device *pdev)
  98. {
  99. struct platform_lcd *plcd = platform_get_drvdata(pdev);
  100. lcd_device_unregister(plcd->lcd);
  101. return 0;
  102. }
  103. #ifdef CONFIG_PM_SLEEP
  104. static int platform_lcd_suspend(struct device *dev)
  105. {
  106. struct platform_lcd *plcd = dev_get_drvdata(dev);
  107. plcd->suspended = 1;
  108. platform_lcd_set_power(plcd->lcd, plcd->power);
  109. return 0;
  110. }
  111. static int platform_lcd_resume(struct device *dev)
  112. {
  113. struct platform_lcd *plcd = dev_get_drvdata(dev);
  114. plcd->suspended = 0;
  115. platform_lcd_set_power(plcd->lcd, plcd->power);
  116. return 0;
  117. }
  118. #endif
  119. static SIMPLE_DEV_PM_OPS(platform_lcd_pm_ops, platform_lcd_suspend,
  120. platform_lcd_resume);
  121. #ifdef CONFIG_OF
  122. static const struct of_device_id platform_lcd_of_match[] = {
  123. { .compatible = "platform-lcd" },
  124. {},
  125. };
  126. MODULE_DEVICE_TABLE(of, platform_lcd_of_match);
  127. #endif
  128. static struct platform_driver platform_lcd_driver = {
  129. .driver = {
  130. .name = "platform-lcd",
  131. .owner = THIS_MODULE,
  132. .pm = &platform_lcd_pm_ops,
  133. .of_match_table = of_match_ptr(platform_lcd_of_match),
  134. },
  135. .probe = platform_lcd_probe,
  136. .remove = platform_lcd_remove,
  137. };
  138. module_platform_driver(platform_lcd_driver);
  139. MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
  140. MODULE_LICENSE("GPL v2");
  141. MODULE_ALIAS("platform:platform-lcd");