ep93xx_bl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Driver for the Cirrus EP93xx lcd backlight
  3. *
  4. * Copyright (c) 2010 H Hartley Sweeten <hsweeten@visionengravers.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This driver controls the pulse width modulated brightness control output,
  11. * BRIGHT, on the Cirrus EP9307, EP9312, and EP9315 processors.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/io.h>
  16. #include <linux/fb.h>
  17. #include <linux/backlight.h>
  18. #define EP93XX_MAX_COUNT 255
  19. #define EP93XX_MAX_BRIGHT 255
  20. #define EP93XX_DEF_BRIGHT 128
  21. struct ep93xxbl {
  22. void __iomem *mmio;
  23. int brightness;
  24. };
  25. static int ep93xxbl_set(struct backlight_device *bl, int brightness)
  26. {
  27. struct ep93xxbl *ep93xxbl = bl_get_data(bl);
  28. writel((brightness << 8) | EP93XX_MAX_COUNT, ep93xxbl->mmio);
  29. ep93xxbl->brightness = brightness;
  30. return 0;
  31. }
  32. static int ep93xxbl_update_status(struct backlight_device *bl)
  33. {
  34. int brightness = bl->props.brightness;
  35. if (bl->props.power != FB_BLANK_UNBLANK ||
  36. bl->props.fb_blank != FB_BLANK_UNBLANK)
  37. brightness = 0;
  38. return ep93xxbl_set(bl, brightness);
  39. }
  40. static int ep93xxbl_get_brightness(struct backlight_device *bl)
  41. {
  42. struct ep93xxbl *ep93xxbl = bl_get_data(bl);
  43. return ep93xxbl->brightness;
  44. }
  45. static const struct backlight_ops ep93xxbl_ops = {
  46. .update_status = ep93xxbl_update_status,
  47. .get_brightness = ep93xxbl_get_brightness,
  48. };
  49. static int ep93xxbl_probe(struct platform_device *dev)
  50. {
  51. struct ep93xxbl *ep93xxbl;
  52. struct backlight_device *bl;
  53. struct backlight_properties props;
  54. struct resource *res;
  55. ep93xxbl = devm_kzalloc(&dev->dev, sizeof(*ep93xxbl), GFP_KERNEL);
  56. if (!ep93xxbl)
  57. return -ENOMEM;
  58. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  59. if (!res)
  60. return -ENXIO;
  61. /*
  62. * FIXME - We don't do a request_mem_region here because we are
  63. * sharing the register space with the framebuffer driver (see
  64. * drivers/video/ep93xx-fb.c) and doing so will cause the second
  65. * loaded driver to return -EBUSY.
  66. *
  67. * NOTE: No locking is required; the framebuffer does not touch
  68. * this register.
  69. */
  70. ep93xxbl->mmio = devm_ioremap(&dev->dev, res->start,
  71. resource_size(res));
  72. if (!ep93xxbl->mmio)
  73. return -ENXIO;
  74. memset(&props, 0, sizeof(struct backlight_properties));
  75. props.type = BACKLIGHT_RAW;
  76. props.max_brightness = EP93XX_MAX_BRIGHT;
  77. bl = devm_backlight_device_register(&dev->dev, dev->name, &dev->dev,
  78. ep93xxbl, &ep93xxbl_ops, &props);
  79. if (IS_ERR(bl))
  80. return PTR_ERR(bl);
  81. bl->props.brightness = EP93XX_DEF_BRIGHT;
  82. platform_set_drvdata(dev, bl);
  83. ep93xxbl_update_status(bl);
  84. return 0;
  85. }
  86. #ifdef CONFIG_PM_SLEEP
  87. static int ep93xxbl_suspend(struct device *dev)
  88. {
  89. struct backlight_device *bl = dev_get_drvdata(dev);
  90. return ep93xxbl_set(bl, 0);
  91. }
  92. static int ep93xxbl_resume(struct device *dev)
  93. {
  94. struct backlight_device *bl = dev_get_drvdata(dev);
  95. backlight_update_status(bl);
  96. return 0;
  97. }
  98. #endif
  99. static SIMPLE_DEV_PM_OPS(ep93xxbl_pm_ops, ep93xxbl_suspend, ep93xxbl_resume);
  100. static struct platform_driver ep93xxbl_driver = {
  101. .driver = {
  102. .name = "ep93xx-bl",
  103. .pm = &ep93xxbl_pm_ops,
  104. },
  105. .probe = ep93xxbl_probe,
  106. };
  107. module_platform_driver(ep93xxbl_driver);
  108. MODULE_DESCRIPTION("EP93xx Backlight Driver");
  109. MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
  110. MODULE_LICENSE("GPL");
  111. MODULE_ALIAS("platform:ep93xx-bl");