apple-gmux.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Gmux driver for Apple laptops
  3. *
  4. * Copyright (C) Canonical Ltd. <seth.forshee@canonical.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/backlight.h>
  15. #include <linux/acpi.h>
  16. #include <linux/pnp.h>
  17. #include <linux/apple_bl.h>
  18. #include <linux/slab.h>
  19. #include <acpi/video.h>
  20. #include <asm/io.h>
  21. struct apple_gmux_data {
  22. unsigned long iostart;
  23. unsigned long iolen;
  24. struct backlight_device *bdev;
  25. };
  26. /*
  27. * gmux port offsets. Many of these are not yet used, but may be in the
  28. * future, and it's useful to have them documented here anyhow.
  29. */
  30. #define GMUX_PORT_VERSION_MAJOR 0x04
  31. #define GMUX_PORT_VERSION_MINOR 0x05
  32. #define GMUX_PORT_VERSION_RELEASE 0x06
  33. #define GMUX_PORT_SWITCH_DISPLAY 0x10
  34. #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
  35. #define GMUX_PORT_INTERRUPT_ENABLE 0x14
  36. #define GMUX_PORT_INTERRUPT_STATUS 0x16
  37. #define GMUX_PORT_SWITCH_DDC 0x28
  38. #define GMUX_PORT_SWITCH_EXTERNAL 0x40
  39. #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
  40. #define GMUX_PORT_DISCRETE_POWER 0x50
  41. #define GMUX_PORT_MAX_BRIGHTNESS 0x70
  42. #define GMUX_PORT_BRIGHTNESS 0x74
  43. #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
  44. #define GMUX_INTERRUPT_ENABLE 0xff
  45. #define GMUX_INTERRUPT_DISABLE 0x00
  46. #define GMUX_INTERRUPT_STATUS_ACTIVE 0
  47. #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
  48. #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
  49. #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
  50. #define GMUX_BRIGHTNESS_MASK 0x00ffffff
  51. #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
  52. static inline u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
  53. {
  54. return inb(gmux_data->iostart + port);
  55. }
  56. static inline void gmux_write8(struct apple_gmux_data *gmux_data, int port,
  57. u8 val)
  58. {
  59. outb(val, gmux_data->iostart + port);
  60. }
  61. static inline u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
  62. {
  63. return inl(gmux_data->iostart + port);
  64. }
  65. static int gmux_get_brightness(struct backlight_device *bd)
  66. {
  67. struct apple_gmux_data *gmux_data = bl_get_data(bd);
  68. return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
  69. GMUX_BRIGHTNESS_MASK;
  70. }
  71. static int gmux_update_status(struct backlight_device *bd)
  72. {
  73. struct apple_gmux_data *gmux_data = bl_get_data(bd);
  74. u32 brightness = bd->props.brightness;
  75. if (bd->props.state & BL_CORE_SUSPENDED)
  76. return 0;
  77. /*
  78. * Older gmux versions require writing out lower bytes first then
  79. * setting the upper byte to 0 to flush the values. Newer versions
  80. * accept a single u32 write, but the old method also works, so we
  81. * just use the old method for all gmux versions.
  82. */
  83. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
  84. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS + 1, brightness >> 8);
  85. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS + 2, brightness >> 16);
  86. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS + 3, 0);
  87. return 0;
  88. }
  89. static const struct backlight_ops gmux_bl_ops = {
  90. .options = BL_CORE_SUSPENDRESUME,
  91. .get_brightness = gmux_get_brightness,
  92. .update_status = gmux_update_status,
  93. };
  94. static int __devinit gmux_probe(struct pnp_dev *pnp,
  95. const struct pnp_device_id *id)
  96. {
  97. struct apple_gmux_data *gmux_data;
  98. struct resource *res;
  99. struct backlight_properties props;
  100. struct backlight_device *bdev;
  101. u8 ver_major, ver_minor, ver_release;
  102. int ret = -ENXIO;
  103. gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
  104. if (!gmux_data)
  105. return -ENOMEM;
  106. pnp_set_drvdata(pnp, gmux_data);
  107. res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
  108. if (!res) {
  109. pr_err("Failed to find gmux I/O resource\n");
  110. goto err_free;
  111. }
  112. gmux_data->iostart = res->start;
  113. gmux_data->iolen = res->end - res->start;
  114. if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
  115. pr_err("gmux I/O region too small (%lu < %u)\n",
  116. gmux_data->iolen, GMUX_MIN_IO_LEN);
  117. goto err_free;
  118. }
  119. if (!request_region(gmux_data->iostart, gmux_data->iolen,
  120. "Apple gmux")) {
  121. pr_err("gmux I/O already in use\n");
  122. goto err_free;
  123. }
  124. /*
  125. * On some machines the gmux is in ACPI even thought the machine
  126. * doesn't really have a gmux. Check for invalid version information
  127. * to detect this.
  128. */
  129. ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
  130. ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
  131. ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
  132. if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
  133. pr_info("gmux device not present\n");
  134. ret = -ENODEV;
  135. goto err_release;
  136. }
  137. pr_info("Found gmux version %d.%d.%d\n", ver_major, ver_minor,
  138. ver_release);
  139. memset(&props, 0, sizeof(props));
  140. props.type = BACKLIGHT_PLATFORM;
  141. props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
  142. /*
  143. * Currently it's assumed that the maximum brightness is less than
  144. * 2^24 for compatibility with old gmux versions. Cap the max
  145. * brightness at this value, but print a warning if the hardware
  146. * reports something higher so that it can be fixed.
  147. */
  148. if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
  149. props.max_brightness = GMUX_MAX_BRIGHTNESS;
  150. bdev = backlight_device_register("gmux_backlight", &pnp->dev,
  151. gmux_data, &gmux_bl_ops, &props);
  152. if (IS_ERR(bdev)) {
  153. ret = PTR_ERR(bdev);
  154. goto err_release;
  155. }
  156. gmux_data->bdev = bdev;
  157. bdev->props.brightness = gmux_get_brightness(bdev);
  158. backlight_update_status(bdev);
  159. /*
  160. * The backlight situation on Macs is complicated. If the gmux is
  161. * present it's the best choice, because it always works for
  162. * backlight control and supports more levels than other options.
  163. * Disable the other backlight choices.
  164. */
  165. acpi_video_unregister();
  166. apple_bl_unregister();
  167. return 0;
  168. err_release:
  169. release_region(gmux_data->iostart, gmux_data->iolen);
  170. err_free:
  171. kfree(gmux_data);
  172. return ret;
  173. }
  174. static void __devexit gmux_remove(struct pnp_dev *pnp)
  175. {
  176. struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
  177. backlight_device_unregister(gmux_data->bdev);
  178. release_region(gmux_data->iostart, gmux_data->iolen);
  179. kfree(gmux_data);
  180. acpi_video_register();
  181. apple_bl_register();
  182. }
  183. static const struct pnp_device_id gmux_device_ids[] = {
  184. {"APP000B", 0},
  185. {"", 0}
  186. };
  187. static struct pnp_driver gmux_pnp_driver = {
  188. .name = "apple-gmux",
  189. .probe = gmux_probe,
  190. .remove = __devexit_p(gmux_remove),
  191. .id_table = gmux_device_ids,
  192. };
  193. static int __init apple_gmux_init(void)
  194. {
  195. return pnp_register_driver(&gmux_pnp_driver);
  196. }
  197. static void __exit apple_gmux_exit(void)
  198. {
  199. pnp_unregister_driver(&gmux_pnp_driver);
  200. }
  201. module_init(apple_gmux_init);
  202. module_exit(apple_gmux_exit);
  203. MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
  204. MODULE_DESCRIPTION("Apple Gmux Driver");
  205. MODULE_LICENSE("GPL");
  206. MODULE_DEVICE_TABLE(pnp, gmux_device_ids);