apple_bl.c 5.7 KB

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