apple_bl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Backlight Driver for Intel-based Apples
  3. *
  4. * Copyright (c) Red Hat <mjg@redhat.com>
  5. * Based on code from Pommed:
  6. * Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
  7. * Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
  8. * Copyright (C) 2007 Julien BLACHE <jb@jblache.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This driver triggers SMIs which cause the firmware to change the
  15. * backlight brightness. This is icky in many ways, but it's impractical to
  16. * get at the firmware code in order to figure out what it's actually doing.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/backlight.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/pci.h>
  26. #include <linux/acpi.h>
  27. #include <linux/atomic.h>
  28. #include <linux/apple_bl.h>
  29. static struct backlight_device *apple_backlight_device;
  30. struct hw_data {
  31. /* I/O resource to allocate. */
  32. unsigned long iostart;
  33. unsigned long iolen;
  34. /* Backlight operations structure. */
  35. const struct backlight_ops backlight_ops;
  36. void (*set_brightness)(int);
  37. };
  38. static const struct hw_data *hw_data;
  39. /* Module parameters. */
  40. static int debug;
  41. module_param_named(debug, debug, int, 0644);
  42. MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
  43. /*
  44. * Implementation for machines with Intel chipset.
  45. */
  46. static void intel_chipset_set_brightness(int intensity)
  47. {
  48. outb(0x04 | (intensity << 4), 0xb3);
  49. outb(0xbf, 0xb2);
  50. }
  51. static int intel_chipset_send_intensity(struct backlight_device *bd)
  52. {
  53. int intensity = bd->props.brightness;
  54. if (debug)
  55. pr_debug("setting brightness to %d\n", intensity);
  56. intel_chipset_set_brightness(intensity);
  57. return 0;
  58. }
  59. static int intel_chipset_get_intensity(struct backlight_device *bd)
  60. {
  61. int intensity;
  62. outb(0x03, 0xb3);
  63. outb(0xbf, 0xb2);
  64. intensity = inb(0xb3) >> 4;
  65. if (debug)
  66. pr_debug("read brightness of %d\n", intensity);
  67. return intensity;
  68. }
  69. static const struct hw_data intel_chipset_data = {
  70. .iostart = 0xb2,
  71. .iolen = 2,
  72. .backlight_ops = {
  73. .options = BL_CORE_SUSPENDRESUME,
  74. .get_brightness = intel_chipset_get_intensity,
  75. .update_status = intel_chipset_send_intensity,
  76. },
  77. .set_brightness = intel_chipset_set_brightness,
  78. };
  79. /*
  80. * Implementation for machines with Nvidia chipset.
  81. */
  82. static void nvidia_chipset_set_brightness(int intensity)
  83. {
  84. outb(0x04 | (intensity << 4), 0x52f);
  85. outb(0xbf, 0x52e);
  86. }
  87. static int nvidia_chipset_send_intensity(struct backlight_device *bd)
  88. {
  89. int intensity = bd->props.brightness;
  90. if (debug)
  91. pr_debug("setting brightness to %d\n", intensity);
  92. nvidia_chipset_set_brightness(intensity);
  93. return 0;
  94. }
  95. static int nvidia_chipset_get_intensity(struct backlight_device *bd)
  96. {
  97. int intensity;
  98. outb(0x03, 0x52f);
  99. outb(0xbf, 0x52e);
  100. intensity = inb(0x52f) >> 4;
  101. if (debug)
  102. pr_debug("read brightness of %d\n", intensity);
  103. return intensity;
  104. }
  105. static const struct hw_data nvidia_chipset_data = {
  106. .iostart = 0x52e,
  107. .iolen = 2,
  108. .backlight_ops = {
  109. .options = BL_CORE_SUSPENDRESUME,
  110. .get_brightness = nvidia_chipset_get_intensity,
  111. .update_status = nvidia_chipset_send_intensity
  112. },
  113. .set_brightness = nvidia_chipset_set_brightness,
  114. };
  115. static int apple_bl_add(struct acpi_device *dev)
  116. {
  117. struct backlight_properties props;
  118. struct pci_dev *host;
  119. int intensity;
  120. host = pci_get_bus_and_slot(0, 0);
  121. if (!host) {
  122. pr_err("unable to find PCI host\n");
  123. return -ENODEV;
  124. }
  125. if (host->vendor == PCI_VENDOR_ID_INTEL)
  126. hw_data = &intel_chipset_data;
  127. else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
  128. hw_data = &nvidia_chipset_data;
  129. pci_dev_put(host);
  130. if (!hw_data) {
  131. pr_err("unknown hardware\n");
  132. return -ENODEV;
  133. }
  134. /* Check that the hardware responds - this may not work under EFI */
  135. intensity = hw_data->backlight_ops.get_brightness(NULL);
  136. if (!intensity) {
  137. hw_data->set_brightness(1);
  138. if (!hw_data->backlight_ops.get_brightness(NULL))
  139. return -ENODEV;
  140. hw_data->set_brightness(0);
  141. }
  142. if (!request_region(hw_data->iostart, hw_data->iolen,
  143. "Apple backlight"))
  144. return -ENXIO;
  145. memset(&props, 0, sizeof(struct backlight_properties));
  146. props.type = BACKLIGHT_PLATFORM;
  147. props.max_brightness = 15;
  148. apple_backlight_device = backlight_device_register("apple_backlight",
  149. NULL, NULL, &hw_data->backlight_ops, &props);
  150. if (IS_ERR(apple_backlight_device)) {
  151. release_region(hw_data->iostart, hw_data->iolen);
  152. return PTR_ERR(apple_backlight_device);
  153. }
  154. apple_backlight_device->props.brightness =
  155. hw_data->backlight_ops.get_brightness(apple_backlight_device);
  156. backlight_update_status(apple_backlight_device);
  157. return 0;
  158. }
  159. static int apple_bl_remove(struct acpi_device *dev)
  160. {
  161. backlight_device_unregister(apple_backlight_device);
  162. release_region(hw_data->iostart, hw_data->iolen);
  163. hw_data = NULL;
  164. return 0;
  165. }
  166. static const struct acpi_device_id apple_bl_ids[] = {
  167. {"APP0002", 0},
  168. {"", 0},
  169. };
  170. static struct acpi_driver apple_bl_driver = {
  171. .name = "Apple backlight",
  172. .ids = apple_bl_ids,
  173. .ops = {
  174. .add = apple_bl_add,
  175. .remove = apple_bl_remove,
  176. },
  177. };
  178. static atomic_t apple_bl_registered = ATOMIC_INIT(0);
  179. int apple_bl_register(void)
  180. {
  181. if (atomic_xchg(&apple_bl_registered, 1) == 0)
  182. return acpi_bus_register_driver(&apple_bl_driver);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(apple_bl_register);
  186. void apple_bl_unregister(void)
  187. {
  188. if (atomic_xchg(&apple_bl_registered, 0) == 1)
  189. acpi_bus_unregister_driver(&apple_bl_driver);
  190. }
  191. EXPORT_SYMBOL_GPL(apple_bl_unregister);
  192. static int __init apple_bl_init(void)
  193. {
  194. return apple_bl_register();
  195. }
  196. static void __exit apple_bl_exit(void)
  197. {
  198. apple_bl_unregister();
  199. }
  200. module_init(apple_bl_init);
  201. module_exit(apple_bl_exit);
  202. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  203. MODULE_DESCRIPTION("Apple Backlight Driver");
  204. MODULE_LICENSE("GPL");
  205. MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
  206. MODULE_ALIAS("mbp_nvidia_bl");