apple_bl.c 5.8 KB

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