platform_lcd.c 3.7 KB

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