mbp_nvidia_bl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Backlight Driver for Nvidia 8600 in Macbook Pro
  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/platform_device.h>
  22. #include <linux/backlight.h>
  23. #include <linux/err.h>
  24. #include <linux/dmi.h>
  25. #include <linux/io.h>
  26. static struct backlight_device *mbp_backlight_device;
  27. /* Structure to be passed to the DMI_MATCH function. */
  28. struct dmi_match_data {
  29. /* I/O resource to allocate. */
  30. unsigned long iostart;
  31. unsigned long iolen;
  32. /* Backlight operations structure. */
  33. struct backlight_ops backlight_ops;
  34. };
  35. /* Module parameters. */
  36. static int debug;
  37. module_param_named(debug, debug, int, 0644);
  38. MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
  39. /*
  40. * Implementation for MacBooks with Intel chipset.
  41. */
  42. static int intel_chipset_send_intensity(struct backlight_device *bd)
  43. {
  44. int intensity = bd->props.brightness;
  45. if (debug)
  46. printk(KERN_DEBUG "mbp_nvidia_bl: setting brightness to %d\n",
  47. intensity);
  48. outb(0x04 | (intensity << 4), 0xb3);
  49. outb(0xbf, 0xb2);
  50. return 0;
  51. }
  52. static int intel_chipset_get_intensity(struct backlight_device *bd)
  53. {
  54. int intensity;
  55. outb(0x03, 0xb3);
  56. outb(0xbf, 0xb2);
  57. intensity = inb(0xb3) >> 4;
  58. if (debug)
  59. printk(KERN_DEBUG "mbp_nvidia_bl: read brightness of %d\n",
  60. intensity);
  61. return intensity;
  62. }
  63. static const struct dmi_match_data intel_chipset_data = {
  64. .iostart = 0xb2,
  65. .iolen = 2,
  66. .backlight_ops = {
  67. .options = BL_CORE_SUSPENDRESUME,
  68. .get_brightness = intel_chipset_get_intensity,
  69. .update_status = intel_chipset_send_intensity,
  70. }
  71. };
  72. /*
  73. * Implementation for MacBooks with Nvidia chipset.
  74. */
  75. static int nvidia_chipset_send_intensity(struct backlight_device *bd)
  76. {
  77. int intensity = bd->props.brightness;
  78. if (debug)
  79. printk(KERN_DEBUG "mbp_nvidia_bl: setting brightness to %d\n",
  80. intensity);
  81. outb(0x04 | (intensity << 4), 0x52f);
  82. outb(0xbf, 0x52e);
  83. return 0;
  84. }
  85. static int nvidia_chipset_get_intensity(struct backlight_device *bd)
  86. {
  87. int intensity;
  88. outb(0x03, 0x52f);
  89. outb(0xbf, 0x52e);
  90. intensity = inb(0x52f) >> 4;
  91. if (debug)
  92. printk(KERN_DEBUG "mbp_nvidia_bl: read brightness of %d\n",
  93. intensity);
  94. return intensity;
  95. }
  96. static const struct dmi_match_data nvidia_chipset_data = {
  97. .iostart = 0x52e,
  98. .iolen = 2,
  99. .backlight_ops = {
  100. .options = BL_CORE_SUSPENDRESUME,
  101. .get_brightness = nvidia_chipset_get_intensity,
  102. .update_status = nvidia_chipset_send_intensity
  103. }
  104. };
  105. /*
  106. * DMI matching.
  107. */
  108. static /* const */ struct dmi_match_data *driver_data;
  109. static int mbp_dmi_match(const struct dmi_system_id *id)
  110. {
  111. driver_data = id->driver_data;
  112. printk(KERN_INFO "mbp_nvidia_bl: %s detected\n", id->ident);
  113. return 1;
  114. }
  115. static const struct dmi_system_id __initdata mbp_device_table[] = {
  116. {
  117. .callback = mbp_dmi_match,
  118. .ident = "MacBookPro 3,1",
  119. .matches = {
  120. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  121. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,1"),
  122. },
  123. .driver_data = (void *)&intel_chipset_data,
  124. },
  125. {
  126. .callback = mbp_dmi_match,
  127. .ident = "MacBookPro 3,2",
  128. .matches = {
  129. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  130. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,2"),
  131. },
  132. .driver_data = (void *)&intel_chipset_data,
  133. },
  134. {
  135. .callback = mbp_dmi_match,
  136. .ident = "MacBookPro 4,1",
  137. .matches = {
  138. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  139. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4,1"),
  140. },
  141. .driver_data = (void *)&intel_chipset_data,
  142. },
  143. {
  144. .callback = mbp_dmi_match,
  145. .ident = "MacBook 5,1",
  146. .matches = {
  147. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  148. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5,1"),
  149. },
  150. .driver_data = (void *)&nvidia_chipset_data,
  151. },
  152. {
  153. .callback = mbp_dmi_match,
  154. .ident = "MacBookAir 2,1",
  155. .matches = {
  156. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  157. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2,1"),
  158. },
  159. .driver_data = (void *)&nvidia_chipset_data,
  160. },
  161. {
  162. .callback = mbp_dmi_match,
  163. .ident = "MacBookPro 5,1",
  164. .matches = {
  165. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  166. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,1"),
  167. },
  168. .driver_data = (void *)&nvidia_chipset_data,
  169. },
  170. { }
  171. };
  172. static int __init mbp_init(void)
  173. {
  174. if (!dmi_check_system(mbp_device_table))
  175. return -ENODEV;
  176. if (!request_region(driver_data->iostart, driver_data->iolen,
  177. "Macbook Pro backlight"))
  178. return -ENXIO;
  179. mbp_backlight_device = backlight_device_register("mbp_backlight",
  180. NULL, NULL, &driver_data->backlight_ops);
  181. if (IS_ERR(mbp_backlight_device)) {
  182. release_region(driver_data->iostart, driver_data->iolen);
  183. return PTR_ERR(mbp_backlight_device);
  184. }
  185. mbp_backlight_device->props.max_brightness = 15;
  186. mbp_backlight_device->props.brightness =
  187. driver_data->backlight_ops.get_brightness(mbp_backlight_device);
  188. backlight_update_status(mbp_backlight_device);
  189. return 0;
  190. }
  191. static void __exit mbp_exit(void)
  192. {
  193. backlight_device_unregister(mbp_backlight_device);
  194. release_region(driver_data->iostart, driver_data->iolen);
  195. }
  196. module_init(mbp_init);
  197. module_exit(mbp_exit);
  198. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  199. MODULE_DESCRIPTION("Nvidia-based Macbook Pro Backlight Driver");
  200. MODULE_LICENSE("GPL");
  201. MODULE_DEVICE_TABLE(dmi, mbp_device_table);