apple-gmux.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /*
  76. * Older gmux versions require writing out lower bytes first then
  77. * setting the upper byte to 0 to flush the values. Newer versions
  78. * accept a single u32 write, but the old method also works, so we
  79. * just use the old method for all gmux versions.
  80. */
  81. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
  82. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS + 1, brightness >> 8);
  83. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS + 2, brightness >> 16);
  84. gmux_write8(gmux_data, GMUX_PORT_BRIGHTNESS + 3, 0);
  85. return 0;
  86. }
  87. static const struct backlight_ops gmux_bl_ops = {
  88. .get_brightness = gmux_get_brightness,
  89. .update_status = gmux_update_status,
  90. };
  91. static int __devinit gmux_probe(struct pnp_dev *pnp,
  92. const struct pnp_device_id *id)
  93. {
  94. struct apple_gmux_data *gmux_data;
  95. struct resource *res;
  96. struct backlight_properties props;
  97. struct backlight_device *bdev;
  98. u8 ver_major, ver_minor, ver_release;
  99. int ret = -ENXIO;
  100. gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
  101. if (!gmux_data)
  102. return -ENOMEM;
  103. pnp_set_drvdata(pnp, gmux_data);
  104. res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
  105. if (!res) {
  106. pr_err("Failed to find gmux I/O resource\n");
  107. goto err_free;
  108. }
  109. gmux_data->iostart = res->start;
  110. gmux_data->iolen = res->end - res->start;
  111. if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
  112. pr_err("gmux I/O region too small (%lu < %u)\n",
  113. gmux_data->iolen, GMUX_MIN_IO_LEN);
  114. goto err_free;
  115. }
  116. if (!request_region(gmux_data->iostart, gmux_data->iolen,
  117. "Apple gmux")) {
  118. pr_err("gmux I/O already in use\n");
  119. goto err_free;
  120. }
  121. /*
  122. * On some machines the gmux is in ACPI even thought the machine
  123. * doesn't really have a gmux. Check for invalid version information
  124. * to detect this.
  125. */
  126. ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
  127. ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
  128. ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
  129. if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
  130. pr_info("gmux device not present\n");
  131. ret = -ENODEV;
  132. goto err_release;
  133. }
  134. pr_info("Found gmux version %d.%d.%d\n", ver_major, ver_minor,
  135. ver_release);
  136. memset(&props, 0, sizeof(props));
  137. props.type = BACKLIGHT_PLATFORM;
  138. props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
  139. /*
  140. * Currently it's assumed that the maximum brightness is less than
  141. * 2^24 for compatibility with old gmux versions. Cap the max
  142. * brightness at this value, but print a warning if the hardware
  143. * reports something higher so that it can be fixed.
  144. */
  145. if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
  146. props.max_brightness = GMUX_MAX_BRIGHTNESS;
  147. bdev = backlight_device_register("gmux_backlight", &pnp->dev,
  148. gmux_data, &gmux_bl_ops, &props);
  149. if (IS_ERR(bdev)) {
  150. ret = PTR_ERR(bdev);
  151. goto err_release;
  152. }
  153. gmux_data->bdev = bdev;
  154. bdev->props.brightness = gmux_get_brightness(bdev);
  155. backlight_update_status(bdev);
  156. /*
  157. * The backlight situation on Macs is complicated. If the gmux is
  158. * present it's the best choice, because it always works for
  159. * backlight control and supports more levels than other options.
  160. * Disable the other backlight choices.
  161. */
  162. acpi_video_unregister();
  163. apple_bl_unregister();
  164. return 0;
  165. err_release:
  166. release_region(gmux_data->iostart, gmux_data->iolen);
  167. err_free:
  168. kfree(gmux_data);
  169. return ret;
  170. }
  171. static void __devexit gmux_remove(struct pnp_dev *pnp)
  172. {
  173. struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
  174. backlight_device_unregister(gmux_data->bdev);
  175. release_region(gmux_data->iostart, gmux_data->iolen);
  176. kfree(gmux_data);
  177. acpi_video_register();
  178. apple_bl_register();
  179. }
  180. static const struct pnp_device_id gmux_device_ids[] = {
  181. {"APP000B", 0},
  182. {"", 0}
  183. };
  184. static struct pnp_driver gmux_pnp_driver = {
  185. .name = "apple-gmux",
  186. .probe = gmux_probe,
  187. .remove = __devexit_p(gmux_remove),
  188. .id_table = gmux_device_ids,
  189. };
  190. static int __init apple_gmux_init(void)
  191. {
  192. return pnp_register_driver(&gmux_pnp_driver);
  193. }
  194. static void __exit apple_gmux_exit(void)
  195. {
  196. pnp_unregister_driver(&gmux_pnp_driver);
  197. }
  198. module_init(apple_gmux_init);
  199. module_exit(apple_gmux_exit);
  200. MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
  201. MODULE_DESCRIPTION("Apple Gmux Driver");
  202. MODULE_LICENSE("GPL");
  203. MODULE_DEVICE_TABLE(pnp, gmux_device_ids);