apple_bl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/backlight.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <linux/pci.h>
  25. #include <linux/acpi.h>
  26. #include <linux/atomic.h>
  27. static struct backlight_device *apple_backlight_device;
  28. struct hw_data {
  29. /* I/O resource to allocate. */
  30. unsigned long iostart;
  31. unsigned long iolen;
  32. /* Backlight operations structure. */
  33. const struct backlight_ops backlight_ops;
  34. void (*set_brightness)(int);
  35. };
  36. static const struct hw_data *hw_data;
  37. #define DRIVER "apple_backlight: "
  38. /* Module parameters. */
  39. static int debug;
  40. module_param_named(debug, debug, int, 0644);
  41. MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
  42. /*
  43. * Implementation for machines with Intel chipset.
  44. */
  45. static void intel_chipset_set_brightness(int intensity)
  46. {
  47. outb(0x04 | (intensity << 4), 0xb3);
  48. outb(0xbf, 0xb2);
  49. }
  50. static int intel_chipset_send_intensity(struct backlight_device *bd)
  51. {
  52. int intensity = bd->props.brightness;
  53. if (debug)
  54. printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
  55. 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. printk(KERN_DEBUG DRIVER "read brightness of %d\n",
  67. intensity);
  68. return intensity;
  69. }
  70. static const struct hw_data intel_chipset_data = {
  71. .iostart = 0xb2,
  72. .iolen = 2,
  73. .backlight_ops = {
  74. .options = BL_CORE_SUSPENDRESUME,
  75. .get_brightness = intel_chipset_get_intensity,
  76. .update_status = intel_chipset_send_intensity,
  77. },
  78. .set_brightness = intel_chipset_set_brightness,
  79. };
  80. /*
  81. * Implementation for machines with Nvidia chipset.
  82. */
  83. static void nvidia_chipset_set_brightness(int intensity)
  84. {
  85. outb(0x04 | (intensity << 4), 0x52f);
  86. outb(0xbf, 0x52e);
  87. }
  88. static int nvidia_chipset_send_intensity(struct backlight_device *bd)
  89. {
  90. int intensity = bd->props.brightness;
  91. if (debug)
  92. printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
  93. intensity);
  94. nvidia_chipset_set_brightness(intensity);
  95. return 0;
  96. }
  97. static int nvidia_chipset_get_intensity(struct backlight_device *bd)
  98. {
  99. int intensity;
  100. outb(0x03, 0x52f);
  101. outb(0xbf, 0x52e);
  102. intensity = inb(0x52f) >> 4;
  103. if (debug)
  104. printk(KERN_DEBUG DRIVER "read brightness of %d\n",
  105. intensity);
  106. return intensity;
  107. }
  108. static const struct hw_data nvidia_chipset_data = {
  109. .iostart = 0x52e,
  110. .iolen = 2,
  111. .backlight_ops = {
  112. .options = BL_CORE_SUSPENDRESUME,
  113. .get_brightness = nvidia_chipset_get_intensity,
  114. .update_status = nvidia_chipset_send_intensity
  115. },
  116. .set_brightness = nvidia_chipset_set_brightness,
  117. };
  118. static int __devinit apple_bl_add(struct acpi_device *dev)
  119. {
  120. struct backlight_properties props;
  121. struct pci_dev *host;
  122. int intensity;
  123. host = pci_get_bus_and_slot(0, 0);
  124. if (!host) {
  125. printk(KERN_ERR DRIVER "unable to find PCI host\n");
  126. return -ENODEV;
  127. }
  128. if (host->vendor == PCI_VENDOR_ID_INTEL)
  129. hw_data = &intel_chipset_data;
  130. else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
  131. hw_data = &nvidia_chipset_data;
  132. pci_dev_put(host);
  133. if (!hw_data) {
  134. printk(KERN_ERR DRIVER "unknown hardware\n");
  135. return -ENODEV;
  136. }
  137. /* Check that the hardware responds - this may not work under EFI */
  138. intensity = hw_data->backlight_ops.get_brightness(NULL);
  139. if (!intensity) {
  140. hw_data->set_brightness(1);
  141. if (!hw_data->backlight_ops.get_brightness(NULL))
  142. return -ENODEV;
  143. hw_data->set_brightness(0);
  144. }
  145. if (!request_region(hw_data->iostart, hw_data->iolen,
  146. "Apple backlight"))
  147. return -ENXIO;
  148. memset(&props, 0, sizeof(struct backlight_properties));
  149. props.type = BACKLIGHT_PLATFORM;
  150. props.max_brightness = 15;
  151. apple_backlight_device = backlight_device_register("apple_backlight",
  152. NULL, NULL, &hw_data->backlight_ops, &props);
  153. if (IS_ERR(apple_backlight_device)) {
  154. release_region(hw_data->iostart, hw_data->iolen);
  155. return PTR_ERR(apple_backlight_device);
  156. }
  157. apple_backlight_device->props.brightness =
  158. hw_data->backlight_ops.get_brightness(apple_backlight_device);
  159. backlight_update_status(apple_backlight_device);
  160. return 0;
  161. }
  162. static int __devexit apple_bl_remove(struct acpi_device *dev, int type)
  163. {
  164. backlight_device_unregister(apple_backlight_device);
  165. release_region(hw_data->iostart, hw_data->iolen);
  166. hw_data = NULL;
  167. return 0;
  168. }
  169. static const struct acpi_device_id apple_bl_ids[] = {
  170. {"APP0002", 0},
  171. {"", 0},
  172. };
  173. static struct acpi_driver apple_bl_driver = {
  174. .name = "Apple backlight",
  175. .ids = apple_bl_ids,
  176. .ops = {
  177. .add = apple_bl_add,
  178. .remove = apple_bl_remove,
  179. },
  180. };
  181. static atomic_t apple_bl_registered = ATOMIC_INIT(0);
  182. int apple_bl_register(void)
  183. {
  184. if (atomic_xchg(&apple_bl_registered, 1) == 0)
  185. return acpi_bus_register_driver(&apple_bl_driver);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL_GPL(apple_bl_register);
  189. void apple_bl_unregister(void)
  190. {
  191. if (atomic_xchg(&apple_bl_registered, 0) == 1)
  192. acpi_bus_unregister_driver(&apple_bl_driver);
  193. }
  194. EXPORT_SYMBOL_GPL(apple_bl_unregister);
  195. static int __init apple_bl_init(void)
  196. {
  197. return apple_bl_register();
  198. }
  199. static void __exit apple_bl_exit(void)
  200. {
  201. apple_bl_unregister();
  202. }
  203. module_init(apple_bl_init);
  204. module_exit(apple_bl_exit);
  205. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  206. MODULE_DESCRIPTION("Apple Backlight Driver");
  207. MODULE_LICENSE("GPL");
  208. MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
  209. MODULE_ALIAS("mbp_nvidia_bl");