platform_lcd.c 3.8 KB

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