platform_lcd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <video/platform_lcd.h>
  19. struct platform_lcd {
  20. struct device *us;
  21. struct lcd_device *lcd;
  22. struct plat_lcd_data *pdata;
  23. unsigned int power;
  24. unsigned int suspended : 1;
  25. };
  26. static inline struct platform_lcd *to_our_lcd(struct lcd_device *lcd)
  27. {
  28. return lcd_get_data(lcd);
  29. }
  30. static int platform_lcd_get_power(struct lcd_device *lcd)
  31. {
  32. struct platform_lcd *plcd = to_our_lcd(lcd);
  33. return plcd->power;
  34. }
  35. static int platform_lcd_set_power(struct lcd_device *lcd, int power)
  36. {
  37. struct platform_lcd *plcd = to_our_lcd(lcd);
  38. int lcd_power = 1;
  39. if (power == FB_BLANK_POWERDOWN || plcd->suspended)
  40. lcd_power = 0;
  41. plcd->pdata->set_power(plcd->pdata, lcd_power);
  42. plcd->power = power;
  43. return 0;
  44. }
  45. static int platform_lcd_match(struct lcd_device *lcd, struct fb_info *info)
  46. {
  47. struct platform_lcd *plcd = to_our_lcd(lcd);
  48. struct plat_lcd_data *pdata = plcd->pdata;
  49. if (pdata->match_fb)
  50. return pdata->match_fb(pdata, info);
  51. return plcd->us->parent == info->device;
  52. }
  53. static struct lcd_ops platform_lcd_ops = {
  54. .get_power = platform_lcd_get_power,
  55. .set_power = platform_lcd_set_power,
  56. .check_fb = platform_lcd_match,
  57. };
  58. static int __devinit platform_lcd_probe(struct platform_device *pdev)
  59. {
  60. struct plat_lcd_data *pdata;
  61. struct platform_lcd *plcd;
  62. struct device *dev = &pdev->dev;
  63. int err;
  64. pdata = pdev->dev.platform_data;
  65. if (!pdata) {
  66. dev_err(dev, "no platform data supplied\n");
  67. return -EINVAL;
  68. }
  69. plcd = kzalloc(sizeof(struct platform_lcd), GFP_KERNEL);
  70. if (!plcd) {
  71. dev_err(dev, "no memory for state\n");
  72. return -ENOMEM;
  73. }
  74. plcd->us = dev;
  75. plcd->pdata = pdata;
  76. plcd->lcd = lcd_device_register("platform-lcd", dev,
  77. plcd, &platform_lcd_ops);
  78. if (IS_ERR(plcd->lcd)) {
  79. dev_err(dev, "cannot register lcd device\n");
  80. err = PTR_ERR(plcd->lcd);
  81. goto err_mem;
  82. }
  83. platform_set_drvdata(pdev, plcd);
  84. return 0;
  85. err_mem:
  86. kfree(plcd);
  87. return err;
  88. }
  89. static int __devexit platform_lcd_remove(struct platform_device *pdev)
  90. {
  91. struct platform_lcd *plcd = platform_get_drvdata(pdev);
  92. lcd_device_unregister(plcd->lcd);
  93. kfree(plcd);
  94. return 0;
  95. }
  96. #ifdef CONFIG_PM
  97. static int platform_lcd_suspend(struct platform_device *pdev, pm_message_t st)
  98. {
  99. struct platform_lcd *plcd = platform_get_drvdata(pdev);
  100. plcd->suspended = 1;
  101. platform_lcd_set_power(plcd->lcd, plcd->power);
  102. return 0;
  103. }
  104. static int platform_lcd_resume(struct platform_device *pdev)
  105. {
  106. struct platform_lcd *plcd = platform_get_drvdata(pdev);
  107. plcd->suspended = 0;
  108. platform_lcd_set_power(plcd->lcd, plcd->power);
  109. return 0;
  110. }
  111. #else
  112. #define platform_lcd_suspend NULL
  113. #define platform_lcd_resume NULL
  114. #endif
  115. static struct platform_driver platform_lcd_driver = {
  116. .driver = {
  117. .name = "platform-lcd",
  118. .owner = THIS_MODULE,
  119. },
  120. .probe = platform_lcd_probe,
  121. .remove = __devexit_p(platform_lcd_remove),
  122. .suspend = platform_lcd_suspend,
  123. .resume = platform_lcd_resume,
  124. };
  125. static int __init platform_lcd_init(void)
  126. {
  127. return platform_driver_register(&platform_lcd_driver);
  128. }
  129. static void __exit platform_lcd_cleanup(void)
  130. {
  131. platform_driver_unregister(&platform_lcd_driver);
  132. }
  133. module_init(platform_lcd_init);
  134. module_exit(platform_lcd_cleanup);
  135. MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
  136. MODULE_LICENSE("GPL v2");
  137. MODULE_ALIAS("platform:platform-lcd");