cr_bllcd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) Intel Corp. 2007.
  4. * All Rights Reserved.
  5. *
  6. * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
  7. * develop this driver.
  8. *
  9. * This file is part of the Carillo Ranch video subsystem driver.
  10. *
  11. * Authors:
  12. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  13. * Alan Hourihane <alanh-at-tungstengraphics-dot-com>
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mutex.h>
  21. #include <linux/fb.h>
  22. #include <linux/backlight.h>
  23. #include <linux/lcd.h>
  24. #include <linux/pci.h>
  25. #include <linux/slab.h>
  26. /* The LVDS- and panel power controls sits on the
  27. * GPIO port of the ISA bridge.
  28. */
  29. #define CRVML_DEVICE_LPC 0x27B8
  30. #define CRVML_REG_GPIOBAR 0x48
  31. #define CRVML_REG_GPIOEN 0x4C
  32. #define CRVML_GPIOEN_BIT (1 << 4)
  33. #define CRVML_PANEL_PORT 0x38
  34. #define CRVML_LVDS_ON 0x00000001
  35. #define CRVML_PANEL_ON 0x00000002
  36. #define CRVML_BACKLIGHT_OFF 0x00000004
  37. /* The PLL Clock register sits on Host bridge */
  38. #define CRVML_DEVICE_MCH 0x5001
  39. #define CRVML_REG_MCHBAR 0x44
  40. #define CRVML_REG_MCHEN 0x54
  41. #define CRVML_MCHEN_BIT (1 << 28)
  42. #define CRVML_MCHMAP_SIZE 4096
  43. #define CRVML_REG_CLOCK 0xc3c
  44. #define CRVML_CLOCK_SHIFT 8
  45. #define CRVML_CLOCK_MASK 0x00000f00
  46. static struct pci_dev *lpc_dev;
  47. static u32 gpio_bar;
  48. struct cr_panel {
  49. struct backlight_device *cr_backlight_device;
  50. struct lcd_device *cr_lcd_device;
  51. };
  52. static int cr_backlight_set_intensity(struct backlight_device *bd)
  53. {
  54. u32 addr = gpio_bar + CRVML_PANEL_PORT;
  55. u32 cur = inl(addr);
  56. if (backlight_get_brightness(bd) == 0) {
  57. /* OFF */
  58. cur |= CRVML_BACKLIGHT_OFF;
  59. outl(cur, addr);
  60. } else {
  61. /* FULL ON */
  62. cur &= ~CRVML_BACKLIGHT_OFF;
  63. outl(cur, addr);
  64. }
  65. return 0;
  66. }
  67. static int cr_backlight_get_intensity(struct backlight_device *bd)
  68. {
  69. u32 addr = gpio_bar + CRVML_PANEL_PORT;
  70. u32 cur = inl(addr);
  71. u8 intensity;
  72. if (cur & CRVML_BACKLIGHT_OFF)
  73. intensity = 0;
  74. else
  75. intensity = 1;
  76. return intensity;
  77. }
  78. static const struct backlight_ops cr_backlight_ops = {
  79. .get_brightness = cr_backlight_get_intensity,
  80. .update_status = cr_backlight_set_intensity,
  81. };
  82. static void cr_panel_on(void)
  83. {
  84. u32 addr = gpio_bar + CRVML_PANEL_PORT;
  85. u32 cur = inl(addr);
  86. if (!(cur & CRVML_PANEL_ON)) {
  87. /* Make sure LVDS controller is down. */
  88. if (cur & 0x00000001) {
  89. cur &= ~CRVML_LVDS_ON;
  90. outl(cur, addr);
  91. }
  92. /* Power up Panel */
  93. schedule_timeout(HZ / 10);
  94. cur |= CRVML_PANEL_ON;
  95. outl(cur, addr);
  96. }
  97. /* Power up LVDS controller */
  98. if (!(cur & CRVML_LVDS_ON)) {
  99. schedule_timeout(HZ / 10);
  100. outl(cur | CRVML_LVDS_ON, addr);
  101. }
  102. }
  103. static void cr_panel_off(void)
  104. {
  105. u32 addr = gpio_bar + CRVML_PANEL_PORT;
  106. u32 cur = inl(addr);
  107. /* Power down LVDS controller first to avoid high currents */
  108. if (cur & CRVML_LVDS_ON) {
  109. cur &= ~CRVML_LVDS_ON;
  110. outl(cur, addr);
  111. }
  112. if (cur & CRVML_PANEL_ON) {
  113. schedule_timeout(HZ / 10);
  114. outl(cur & ~CRVML_PANEL_ON, addr);
  115. }
  116. }
  117. static int cr_lcd_set_power(struct lcd_device *ld, int power)
  118. {
  119. if (power == FB_BLANK_UNBLANK)
  120. cr_panel_on();
  121. if (power == FB_BLANK_POWERDOWN)
  122. cr_panel_off();
  123. return 0;
  124. }
  125. static struct lcd_ops cr_lcd_ops = {
  126. .set_power = cr_lcd_set_power,
  127. };
  128. static int cr_backlight_probe(struct platform_device *pdev)
  129. {
  130. struct backlight_properties props;
  131. struct backlight_device *bdp;
  132. struct lcd_device *ldp;
  133. struct cr_panel *crp;
  134. u8 dev_en;
  135. lpc_dev = pci_get_device(PCI_VENDOR_ID_INTEL,
  136. CRVML_DEVICE_LPC, NULL);
  137. if (!lpc_dev) {
  138. pr_err("INTEL CARILLO RANCH LPC not found.\n");
  139. return -ENODEV;
  140. }
  141. pci_read_config_byte(lpc_dev, CRVML_REG_GPIOEN, &dev_en);
  142. if (!(dev_en & CRVML_GPIOEN_BIT)) {
  143. pr_err("Carillo Ranch GPIO device was not enabled.\n");
  144. pci_dev_put(lpc_dev);
  145. return -ENODEV;
  146. }
  147. memset(&props, 0, sizeof(struct backlight_properties));
  148. props.type = BACKLIGHT_RAW;
  149. bdp = devm_backlight_device_register(&pdev->dev, "cr-backlight",
  150. &pdev->dev, NULL, &cr_backlight_ops,
  151. &props);
  152. if (IS_ERR(bdp)) {
  153. pci_dev_put(lpc_dev);
  154. return PTR_ERR(bdp);
  155. }
  156. ldp = devm_lcd_device_register(&pdev->dev, "cr-lcd", &pdev->dev, NULL,
  157. &cr_lcd_ops);
  158. if (IS_ERR(ldp)) {
  159. pci_dev_put(lpc_dev);
  160. return PTR_ERR(ldp);
  161. }
  162. pci_read_config_dword(lpc_dev, CRVML_REG_GPIOBAR,
  163. &gpio_bar);
  164. gpio_bar &= ~0x3F;
  165. crp = devm_kzalloc(&pdev->dev, sizeof(*crp), GFP_KERNEL);
  166. if (!crp) {
  167. pci_dev_put(lpc_dev);
  168. return -ENOMEM;
  169. }
  170. crp->cr_backlight_device = bdp;
  171. crp->cr_lcd_device = ldp;
  172. crp->cr_backlight_device->props.power = FB_BLANK_UNBLANK;
  173. crp->cr_backlight_device->props.brightness = 0;
  174. cr_backlight_set_intensity(crp->cr_backlight_device);
  175. cr_lcd_set_power(crp->cr_lcd_device, FB_BLANK_UNBLANK);
  176. platform_set_drvdata(pdev, crp);
  177. return 0;
  178. }
  179. static int cr_backlight_remove(struct platform_device *pdev)
  180. {
  181. struct cr_panel *crp = platform_get_drvdata(pdev);
  182. crp->cr_backlight_device->props.power = FB_BLANK_POWERDOWN;
  183. crp->cr_backlight_device->props.brightness = 0;
  184. crp->cr_backlight_device->props.max_brightness = 0;
  185. cr_backlight_set_intensity(crp->cr_backlight_device);
  186. cr_lcd_set_power(crp->cr_lcd_device, FB_BLANK_POWERDOWN);
  187. pci_dev_put(lpc_dev);
  188. return 0;
  189. }
  190. static struct platform_driver cr_backlight_driver = {
  191. .probe = cr_backlight_probe,
  192. .remove = cr_backlight_remove,
  193. .driver = {
  194. .name = "cr_backlight",
  195. },
  196. };
  197. static struct platform_device *crp;
  198. static int __init cr_backlight_init(void)
  199. {
  200. int ret = platform_driver_register(&cr_backlight_driver);
  201. if (ret)
  202. return ret;
  203. crp = platform_device_register_simple("cr_backlight", -1, NULL, 0);
  204. if (IS_ERR(crp)) {
  205. platform_driver_unregister(&cr_backlight_driver);
  206. return PTR_ERR(crp);
  207. }
  208. pr_info("Carillo Ranch Backlight Driver Initialized.\n");
  209. return 0;
  210. }
  211. static void __exit cr_backlight_exit(void)
  212. {
  213. platform_device_unregister(crp);
  214. platform_driver_unregister(&cr_backlight_driver);
  215. }
  216. module_init(cr_backlight_init);
  217. module_exit(cr_backlight_exit);
  218. MODULE_AUTHOR("Tungsten Graphics Inc.");
  219. MODULE_DESCRIPTION("Carillo Ranch Backlight Driver");
  220. MODULE_LICENSE("GPL");