apple-gmux.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 <linux/delay.h>
  20. #include <acpi/video.h>
  21. #include <asm/io.h>
  22. struct apple_gmux_data {
  23. unsigned long iostart;
  24. unsigned long iolen;
  25. bool indexed;
  26. struct mutex index_lock;
  27. struct backlight_device *bdev;
  28. };
  29. /*
  30. * gmux port offsets. Many of these are not yet used, but may be in the
  31. * future, and it's useful to have them documented here anyhow.
  32. */
  33. #define GMUX_PORT_VERSION_MAJOR 0x04
  34. #define GMUX_PORT_VERSION_MINOR 0x05
  35. #define GMUX_PORT_VERSION_RELEASE 0x06
  36. #define GMUX_PORT_SWITCH_DISPLAY 0x10
  37. #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
  38. #define GMUX_PORT_INTERRUPT_ENABLE 0x14
  39. #define GMUX_PORT_INTERRUPT_STATUS 0x16
  40. #define GMUX_PORT_SWITCH_DDC 0x28
  41. #define GMUX_PORT_SWITCH_EXTERNAL 0x40
  42. #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
  43. #define GMUX_PORT_DISCRETE_POWER 0x50
  44. #define GMUX_PORT_MAX_BRIGHTNESS 0x70
  45. #define GMUX_PORT_BRIGHTNESS 0x74
  46. #define GMUX_PORT_VALUE 0xc2
  47. #define GMUX_PORT_READ 0xd0
  48. #define GMUX_PORT_WRITE 0xd4
  49. #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
  50. #define GMUX_INTERRUPT_ENABLE 0xff
  51. #define GMUX_INTERRUPT_DISABLE 0x00
  52. #define GMUX_INTERRUPT_STATUS_ACTIVE 0
  53. #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
  54. #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
  55. #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
  56. #define GMUX_BRIGHTNESS_MASK 0x00ffffff
  57. #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
  58. static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
  59. {
  60. return inb(gmux_data->iostart + port);
  61. }
  62. static void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port,
  63. u8 val)
  64. {
  65. outb(val, gmux_data->iostart + port);
  66. }
  67. static u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port)
  68. {
  69. return inl(gmux_data->iostart + port);
  70. }
  71. static void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port,
  72. u32 val)
  73. {
  74. int i;
  75. u8 tmpval;
  76. for (i = 0; i < 4; i++) {
  77. tmpval = (val >> (i * 8)) & 0xff;
  78. outb(tmpval, port + i);
  79. }
  80. }
  81. static int gmux_index_wait_ready(struct apple_gmux_data *gmux_data)
  82. {
  83. int i = 200;
  84. u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  85. while (i && (gwr & 0x01)) {
  86. inb(gmux_data->iostart + GMUX_PORT_READ);
  87. gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  88. udelay(100);
  89. i--;
  90. }
  91. return !!i;
  92. }
  93. static int gmux_index_wait_complete(struct apple_gmux_data *gmux_data)
  94. {
  95. int i = 200;
  96. u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  97. while (i && !(gwr & 0x01)) {
  98. gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  99. udelay(100);
  100. i--;
  101. }
  102. if (gwr & 0x01)
  103. inb(gmux_data->iostart + GMUX_PORT_READ);
  104. return !!i;
  105. }
  106. static u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port)
  107. {
  108. u8 val;
  109. mutex_lock(&gmux_data->index_lock);
  110. outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
  111. gmux_index_wait_ready(gmux_data);
  112. val = inb(gmux_data->iostart + GMUX_PORT_VALUE);
  113. mutex_unlock(&gmux_data->index_lock);
  114. return val;
  115. }
  116. static void gmux_index_write8(struct apple_gmux_data *gmux_data, int port,
  117. u8 val)
  118. {
  119. mutex_lock(&gmux_data->index_lock);
  120. outb(val, gmux_data->iostart + GMUX_PORT_VALUE);
  121. gmux_index_wait_ready(gmux_data);
  122. outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
  123. gmux_index_wait_complete(gmux_data);
  124. mutex_unlock(&gmux_data->index_lock);
  125. }
  126. static u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port)
  127. {
  128. u32 val;
  129. mutex_lock(&gmux_data->index_lock);
  130. outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
  131. gmux_index_wait_ready(gmux_data);
  132. val = inl(gmux_data->iostart + GMUX_PORT_VALUE);
  133. mutex_unlock(&gmux_data->index_lock);
  134. return val;
  135. }
  136. static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port,
  137. u32 val)
  138. {
  139. int i;
  140. u8 tmpval;
  141. mutex_lock(&gmux_data->index_lock);
  142. for (i = 0; i < 4; i++) {
  143. tmpval = (val >> (i * 8)) & 0xff;
  144. outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i);
  145. }
  146. gmux_index_wait_ready(gmux_data);
  147. outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
  148. gmux_index_wait_complete(gmux_data);
  149. mutex_unlock(&gmux_data->index_lock);
  150. }
  151. static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
  152. {
  153. if (gmux_data->indexed)
  154. return gmux_index_read8(gmux_data, port);
  155. else
  156. return gmux_pio_read8(gmux_data, port);
  157. }
  158. static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val)
  159. {
  160. if (gmux_data->indexed)
  161. gmux_index_write8(gmux_data, port, val);
  162. else
  163. gmux_pio_write8(gmux_data, port, val);
  164. }
  165. static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
  166. {
  167. if (gmux_data->indexed)
  168. return gmux_index_read32(gmux_data, port);
  169. else
  170. return gmux_pio_read32(gmux_data, port);
  171. }
  172. static void gmux_write32(struct apple_gmux_data *gmux_data, int port,
  173. u32 val)
  174. {
  175. if (gmux_data->indexed)
  176. gmux_index_write32(gmux_data, port, val);
  177. else
  178. gmux_pio_write32(gmux_data, port, val);
  179. }
  180. static bool gmux_is_indexed(struct apple_gmux_data *gmux_data)
  181. {
  182. u16 val;
  183. outb(0xaa, gmux_data->iostart + 0xcc);
  184. outb(0x55, gmux_data->iostart + 0xcd);
  185. outb(0x00, gmux_data->iostart + 0xce);
  186. val = inb(gmux_data->iostart + 0xcc) |
  187. (inb(gmux_data->iostart + 0xcd) << 8);
  188. if (val == 0x55aa)
  189. return true;
  190. return false;
  191. }
  192. static int gmux_get_brightness(struct backlight_device *bd)
  193. {
  194. struct apple_gmux_data *gmux_data = bl_get_data(bd);
  195. return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
  196. GMUX_BRIGHTNESS_MASK;
  197. }
  198. static int gmux_update_status(struct backlight_device *bd)
  199. {
  200. struct apple_gmux_data *gmux_data = bl_get_data(bd);
  201. u32 brightness = bd->props.brightness;
  202. if (bd->props.state & BL_CORE_SUSPENDED)
  203. return 0;
  204. gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
  205. return 0;
  206. }
  207. static const struct backlight_ops gmux_bl_ops = {
  208. .options = BL_CORE_SUSPENDRESUME,
  209. .get_brightness = gmux_get_brightness,
  210. .update_status = gmux_update_status,
  211. };
  212. static int __devinit gmux_probe(struct pnp_dev *pnp,
  213. const struct pnp_device_id *id)
  214. {
  215. struct apple_gmux_data *gmux_data;
  216. struct resource *res;
  217. struct backlight_properties props;
  218. struct backlight_device *bdev;
  219. u8 ver_major, ver_minor, ver_release;
  220. int ret = -ENXIO;
  221. gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
  222. if (!gmux_data)
  223. return -ENOMEM;
  224. pnp_set_drvdata(pnp, gmux_data);
  225. res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
  226. if (!res) {
  227. pr_err("Failed to find gmux I/O resource\n");
  228. goto err_free;
  229. }
  230. gmux_data->iostart = res->start;
  231. gmux_data->iolen = res->end - res->start;
  232. if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
  233. pr_err("gmux I/O region too small (%lu < %u)\n",
  234. gmux_data->iolen, GMUX_MIN_IO_LEN);
  235. goto err_free;
  236. }
  237. if (!request_region(gmux_data->iostart, gmux_data->iolen,
  238. "Apple gmux")) {
  239. pr_err("gmux I/O already in use\n");
  240. goto err_free;
  241. }
  242. /*
  243. * Invalid version information may indicate either that the gmux
  244. * device isn't present or that it's a new one that uses indexed
  245. * io
  246. */
  247. ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
  248. ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
  249. ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
  250. if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
  251. if (gmux_is_indexed(gmux_data)) {
  252. mutex_init(&gmux_data->index_lock);
  253. gmux_data->indexed = true;
  254. } else {
  255. pr_info("gmux device not present\n");
  256. ret = -ENODEV;
  257. goto err_release;
  258. }
  259. pr_info("Found indexed gmux\n");
  260. } else {
  261. pr_info("Found gmux version %d.%d.%d\n", ver_major, ver_minor,
  262. ver_release);
  263. }
  264. memset(&props, 0, sizeof(props));
  265. props.type = BACKLIGHT_PLATFORM;
  266. props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
  267. /*
  268. * Currently it's assumed that the maximum brightness is less than
  269. * 2^24 for compatibility with old gmux versions. Cap the max
  270. * brightness at this value, but print a warning if the hardware
  271. * reports something higher so that it can be fixed.
  272. */
  273. if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
  274. props.max_brightness = GMUX_MAX_BRIGHTNESS;
  275. bdev = backlight_device_register("gmux_backlight", &pnp->dev,
  276. gmux_data, &gmux_bl_ops, &props);
  277. if (IS_ERR(bdev)) {
  278. ret = PTR_ERR(bdev);
  279. goto err_release;
  280. }
  281. gmux_data->bdev = bdev;
  282. bdev->props.brightness = gmux_get_brightness(bdev);
  283. backlight_update_status(bdev);
  284. /*
  285. * The backlight situation on Macs is complicated. If the gmux is
  286. * present it's the best choice, because it always works for
  287. * backlight control and supports more levels than other options.
  288. * Disable the other backlight choices.
  289. */
  290. acpi_video_dmi_promote_vendor();
  291. #ifdef CONFIG_ACPI_VIDEO
  292. acpi_video_unregister();
  293. #endif
  294. apple_bl_unregister();
  295. return 0;
  296. err_release:
  297. release_region(gmux_data->iostart, gmux_data->iolen);
  298. err_free:
  299. kfree(gmux_data);
  300. return ret;
  301. }
  302. static void __devexit gmux_remove(struct pnp_dev *pnp)
  303. {
  304. struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
  305. backlight_device_unregister(gmux_data->bdev);
  306. release_region(gmux_data->iostart, gmux_data->iolen);
  307. kfree(gmux_data);
  308. acpi_video_dmi_demote_vendor();
  309. #ifdef CONFIG_ACPI_VIDEO
  310. acpi_video_register();
  311. #endif
  312. apple_bl_register();
  313. }
  314. static const struct pnp_device_id gmux_device_ids[] = {
  315. {"APP000B", 0},
  316. {"", 0}
  317. };
  318. static struct pnp_driver gmux_pnp_driver = {
  319. .name = "apple-gmux",
  320. .probe = gmux_probe,
  321. .remove = __devexit_p(gmux_remove),
  322. .id_table = gmux_device_ids,
  323. };
  324. static int __init apple_gmux_init(void)
  325. {
  326. return pnp_register_driver(&gmux_pnp_driver);
  327. }
  328. static void __exit apple_gmux_exit(void)
  329. {
  330. pnp_unregister_driver(&gmux_pnp_driver);
  331. }
  332. module_init(apple_gmux_init);
  333. module_exit(apple_gmux_exit);
  334. MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
  335. MODULE_DESCRIPTION("Apple Gmux Driver");
  336. MODULE_LICENSE("GPL");
  337. MODULE_DEVICE_TABLE(pnp, gmux_device_ids);