apple-gmux.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Gmux driver for Apple laptops
  3. *
  4. * Copyright (C) Canonical Ltd. <seth.forshee@canonical.com>
  5. * Copyright (C) 2010-2012 Andreas Heider <andreas@meetr.de>
  6. * Copyright (C) 2015 Lukas Wunner <lukas@wunner.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/backlight.h>
  17. #include <linux/acpi.h>
  18. #include <linux/pnp.h>
  19. #include <linux/apple_bl.h>
  20. #include <linux/apple-gmux.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <linux/pci.h>
  24. #include <linux/vga_switcheroo.h>
  25. #include <linux/vgaarb.h>
  26. #include <acpi/video.h>
  27. #include <asm/io.h>
  28. /**
  29. * DOC: Overview
  30. *
  31. * gmux is a microcontroller built into the MacBook Pro to support dual GPUs:
  32. * A `Lattice XP2`_ on pre-retinas, a `Renesas R4F2113`_ on retinas.
  33. *
  34. * (The MacPro6,1 2013 also has a gmux, however it is unclear why since it has
  35. * dual GPUs but no built-in display.)
  36. *
  37. * gmux is connected to the LPC bus of the southbridge. Its I/O ports are
  38. * accessed differently depending on the microcontroller: Driver functions
  39. * to access a pre-retina gmux are infixed ``_pio_``, those for a retina gmux
  40. * are infixed ``_index_``.
  41. *
  42. * .. _Lattice XP2:
  43. * http://www.latticesemi.com/en/Products/FPGAandCPLD/LatticeXP2.aspx
  44. * .. _Renesas R4F2113:
  45. * http://www.renesas.com/products/mpumcu/h8s/h8s2100/h8s2113/index.jsp
  46. */
  47. struct apple_gmux_data {
  48. unsigned long iostart;
  49. unsigned long iolen;
  50. bool indexed;
  51. struct mutex index_lock;
  52. struct pci_dev *pdev;
  53. struct backlight_device *bdev;
  54. /* switcheroo data */
  55. acpi_handle dhandle;
  56. int gpe;
  57. enum vga_switcheroo_client_id switch_state_display;
  58. enum vga_switcheroo_client_id switch_state_ddc;
  59. enum vga_switcheroo_client_id switch_state_external;
  60. enum vga_switcheroo_state power_state;
  61. struct completion powerchange_done;
  62. };
  63. static struct apple_gmux_data *apple_gmux_data;
  64. /*
  65. * gmux port offsets. Many of these are not yet used, but may be in the
  66. * future, and it's useful to have them documented here anyhow.
  67. */
  68. #define GMUX_PORT_VERSION_MAJOR 0x04
  69. #define GMUX_PORT_VERSION_MINOR 0x05
  70. #define GMUX_PORT_VERSION_RELEASE 0x06
  71. #define GMUX_PORT_SWITCH_DISPLAY 0x10
  72. #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
  73. #define GMUX_PORT_INTERRUPT_ENABLE 0x14
  74. #define GMUX_PORT_INTERRUPT_STATUS 0x16
  75. #define GMUX_PORT_SWITCH_DDC 0x28
  76. #define GMUX_PORT_SWITCH_EXTERNAL 0x40
  77. #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
  78. #define GMUX_PORT_DISCRETE_POWER 0x50
  79. #define GMUX_PORT_MAX_BRIGHTNESS 0x70
  80. #define GMUX_PORT_BRIGHTNESS 0x74
  81. #define GMUX_PORT_VALUE 0xc2
  82. #define GMUX_PORT_READ 0xd0
  83. #define GMUX_PORT_WRITE 0xd4
  84. #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
  85. #define GMUX_INTERRUPT_ENABLE 0xff
  86. #define GMUX_INTERRUPT_DISABLE 0x00
  87. #define GMUX_INTERRUPT_STATUS_ACTIVE 0
  88. #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
  89. #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
  90. #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
  91. #define GMUX_BRIGHTNESS_MASK 0x00ffffff
  92. #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
  93. static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
  94. {
  95. return inb(gmux_data->iostart + port);
  96. }
  97. static void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port,
  98. u8 val)
  99. {
  100. outb(val, gmux_data->iostart + port);
  101. }
  102. static u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port)
  103. {
  104. return inl(gmux_data->iostart + port);
  105. }
  106. static void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port,
  107. u32 val)
  108. {
  109. int i;
  110. u8 tmpval;
  111. for (i = 0; i < 4; i++) {
  112. tmpval = (val >> (i * 8)) & 0xff;
  113. outb(tmpval, gmux_data->iostart + port + i);
  114. }
  115. }
  116. static int gmux_index_wait_ready(struct apple_gmux_data *gmux_data)
  117. {
  118. int i = 200;
  119. u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  120. while (i && (gwr & 0x01)) {
  121. inb(gmux_data->iostart + GMUX_PORT_READ);
  122. gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  123. udelay(100);
  124. i--;
  125. }
  126. return !!i;
  127. }
  128. static int gmux_index_wait_complete(struct apple_gmux_data *gmux_data)
  129. {
  130. int i = 200;
  131. u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  132. while (i && !(gwr & 0x01)) {
  133. gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
  134. udelay(100);
  135. i--;
  136. }
  137. if (gwr & 0x01)
  138. inb(gmux_data->iostart + GMUX_PORT_READ);
  139. return !!i;
  140. }
  141. static u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port)
  142. {
  143. u8 val;
  144. mutex_lock(&gmux_data->index_lock);
  145. gmux_index_wait_ready(gmux_data);
  146. outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
  147. gmux_index_wait_complete(gmux_data);
  148. val = inb(gmux_data->iostart + GMUX_PORT_VALUE);
  149. mutex_unlock(&gmux_data->index_lock);
  150. return val;
  151. }
  152. static void gmux_index_write8(struct apple_gmux_data *gmux_data, int port,
  153. u8 val)
  154. {
  155. mutex_lock(&gmux_data->index_lock);
  156. outb(val, gmux_data->iostart + GMUX_PORT_VALUE);
  157. gmux_index_wait_ready(gmux_data);
  158. outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
  159. gmux_index_wait_complete(gmux_data);
  160. mutex_unlock(&gmux_data->index_lock);
  161. }
  162. static u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port)
  163. {
  164. u32 val;
  165. mutex_lock(&gmux_data->index_lock);
  166. gmux_index_wait_ready(gmux_data);
  167. outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
  168. gmux_index_wait_complete(gmux_data);
  169. val = inl(gmux_data->iostart + GMUX_PORT_VALUE);
  170. mutex_unlock(&gmux_data->index_lock);
  171. return val;
  172. }
  173. static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port,
  174. u32 val)
  175. {
  176. int i;
  177. u8 tmpval;
  178. mutex_lock(&gmux_data->index_lock);
  179. for (i = 0; i < 4; i++) {
  180. tmpval = (val >> (i * 8)) & 0xff;
  181. outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i);
  182. }
  183. gmux_index_wait_ready(gmux_data);
  184. outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
  185. gmux_index_wait_complete(gmux_data);
  186. mutex_unlock(&gmux_data->index_lock);
  187. }
  188. static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
  189. {
  190. if (gmux_data->indexed)
  191. return gmux_index_read8(gmux_data, port);
  192. else
  193. return gmux_pio_read8(gmux_data, port);
  194. }
  195. static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val)
  196. {
  197. if (gmux_data->indexed)
  198. gmux_index_write8(gmux_data, port, val);
  199. else
  200. gmux_pio_write8(gmux_data, port, val);
  201. }
  202. static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
  203. {
  204. if (gmux_data->indexed)
  205. return gmux_index_read32(gmux_data, port);
  206. else
  207. return gmux_pio_read32(gmux_data, port);
  208. }
  209. static void gmux_write32(struct apple_gmux_data *gmux_data, int port,
  210. u32 val)
  211. {
  212. if (gmux_data->indexed)
  213. gmux_index_write32(gmux_data, port, val);
  214. else
  215. gmux_pio_write32(gmux_data, port, val);
  216. }
  217. static bool gmux_is_indexed(struct apple_gmux_data *gmux_data)
  218. {
  219. u16 val;
  220. outb(0xaa, gmux_data->iostart + 0xcc);
  221. outb(0x55, gmux_data->iostart + 0xcd);
  222. outb(0x00, gmux_data->iostart + 0xce);
  223. val = inb(gmux_data->iostart + 0xcc) |
  224. (inb(gmux_data->iostart + 0xcd) << 8);
  225. if (val == 0x55aa)
  226. return true;
  227. return false;
  228. }
  229. /**
  230. * DOC: Backlight control
  231. *
  232. * On single GPU MacBooks, the PWM signal for the backlight is generated by
  233. * the GPU. On dual GPU MacBook Pros by contrast, either GPU may be suspended
  234. * to conserve energy. Hence the PWM signal needs to be generated by a separate
  235. * backlight driver which is controlled by gmux. The earliest generation
  236. * MBP5 2008/09 uses a `TI LP8543`_ backlight driver. All newer models
  237. * use a `TI LP8545`_.
  238. *
  239. * .. _TI LP8543: http://www.ti.com/lit/ds/symlink/lp8543.pdf
  240. * .. _TI LP8545: http://www.ti.com/lit/ds/symlink/lp8545.pdf
  241. */
  242. static int gmux_get_brightness(struct backlight_device *bd)
  243. {
  244. struct apple_gmux_data *gmux_data = bl_get_data(bd);
  245. return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
  246. GMUX_BRIGHTNESS_MASK;
  247. }
  248. static int gmux_update_status(struct backlight_device *bd)
  249. {
  250. struct apple_gmux_data *gmux_data = bl_get_data(bd);
  251. u32 brightness = bd->props.brightness;
  252. if (bd->props.state & BL_CORE_SUSPENDED)
  253. return 0;
  254. gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
  255. return 0;
  256. }
  257. static const struct backlight_ops gmux_bl_ops = {
  258. .options = BL_CORE_SUSPENDRESUME,
  259. .get_brightness = gmux_get_brightness,
  260. .update_status = gmux_update_status,
  261. };
  262. /**
  263. * DOC: Graphics mux
  264. *
  265. * On pre-retinas, the LVDS outputs of both GPUs feed into gmux which muxes
  266. * either of them to the panel. One of the tricks gmux has up its sleeve is
  267. * to lengthen the blanking interval of its output during a switch to
  268. * synchronize it with the GPU switched to. This allows for a flicker-free
  269. * switch that is imperceptible by the user (`US 8,687,007 B2`_).
  270. *
  271. * On retinas, muxing is no longer done by gmux itself, but by a separate
  272. * chip which is controlled by gmux. The chip is triple sourced, it is
  273. * either an `NXP CBTL06142`_, `TI HD3SS212`_ or `Pericom PI3VDP12412`_.
  274. * The panel is driven with eDP instead of LVDS since the pixel clock
  275. * required for retina resolution exceeds LVDS' limits.
  276. *
  277. * Pre-retinas are able to switch the panel's DDC pins separately.
  278. * This is handled by a `TI SN74LV4066A`_ which is controlled by gmux.
  279. * The inactive GPU can thus probe the panel's EDID without switching over
  280. * the entire panel. Retinas lack this functionality as the chips used for
  281. * eDP muxing are incapable of switching the AUX channel separately (see
  282. * the linked data sheets, Pericom would be capable but this is unused).
  283. * However the retina panel has the NO_AUX_HANDSHAKE_LINK_TRAINING bit set
  284. * in its DPCD, allowing the inactive GPU to skip the AUX handshake and
  285. * set up the output with link parameters pre-calibrated by the active GPU.
  286. *
  287. * The external DP port is only fully switchable on the first two unibody
  288. * MacBook Pro generations, MBP5 2008/09 and MBP6 2010. This is done by an
  289. * `NXP CBTL06141`_ which is controlled by gmux. It's the predecessor of the
  290. * eDP mux on retinas, the difference being support for 2.7 versus 5.4 Gbit/s.
  291. *
  292. * The following MacBook Pro generations replaced the external DP port with a
  293. * combined DP/Thunderbolt port and lost the ability to switch it between GPUs,
  294. * connecting it either to the discrete GPU or the Thunderbolt controller.
  295. * Oddly enough, while the full port is no longer switchable, AUX and HPD
  296. * are still switchable by way of an `NXP CBTL03062`_ (on pre-retinas
  297. * MBP8 2011 and MBP9 2012) or two `TI TS3DS10224`_ (on retinas) under the
  298. * control of gmux. Since the integrated GPU is missing the main link,
  299. * external displays appear to it as phantoms which fail to link-train.
  300. *
  301. * gmux receives the HPD signal of all display connectors and sends an
  302. * interrupt on hotplug. On generations which cannot switch external ports,
  303. * the discrete GPU can then be woken to drive the newly connected display.
  304. * The ability to switch AUX on these generations could be used to improve
  305. * reliability of hotplug detection by having the integrated GPU poll the
  306. * ports while the discrete GPU is asleep, but currently we do not make use
  307. * of this feature.
  308. *
  309. * gmux' initial switch state on bootup is user configurable via the EFI
  310. * variable ``gpu-power-prefs-fa4ce28d-b62f-4c99-9cc3-6815686e30f9`` (5th byte,
  311. * 1 = IGD, 0 = DIS). Based on this setting, the EFI firmware tells gmux to
  312. * switch the panel and the external DP connector and allocates a framebuffer
  313. * for the selected GPU.
  314. *
  315. * .. _US 8,687,007 B2: http://pimg-fpiw.uspto.gov/fdd/07/870/086/0.pdf
  316. * .. _NXP CBTL06141: http://www.nxp.com/documents/data_sheet/CBTL06141.pdf
  317. * .. _NXP CBTL06142: http://www.nxp.com/documents/data_sheet/CBTL06141.pdf
  318. * .. _TI HD3SS212: http://www.ti.com/lit/ds/symlink/hd3ss212.pdf
  319. * .. _Pericom PI3VDP12412: https://www.pericom.com/assets/Datasheets/PI3VDP12412.pdf
  320. * .. _TI SN74LV4066A: http://www.ti.com/lit/ds/symlink/sn74lv4066a.pdf
  321. * .. _NXP CBTL03062: http://pdf.datasheetarchive.com/indexerfiles/Datasheets-SW16/DSASW00308511.pdf
  322. * .. _TI TS3DS10224: http://www.ti.com/lit/ds/symlink/ts3ds10224.pdf
  323. */
  324. static void gmux_read_switch_state(struct apple_gmux_data *gmux_data)
  325. {
  326. if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DDC) == 1)
  327. gmux_data->switch_state_ddc = VGA_SWITCHEROO_IGD;
  328. else
  329. gmux_data->switch_state_ddc = VGA_SWITCHEROO_DIS;
  330. if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2)
  331. gmux_data->switch_state_display = VGA_SWITCHEROO_IGD;
  332. else
  333. gmux_data->switch_state_display = VGA_SWITCHEROO_DIS;
  334. if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL) == 2)
  335. gmux_data->switch_state_external = VGA_SWITCHEROO_IGD;
  336. else
  337. gmux_data->switch_state_external = VGA_SWITCHEROO_DIS;
  338. }
  339. static void gmux_write_switch_state(struct apple_gmux_data *gmux_data)
  340. {
  341. if (gmux_data->switch_state_ddc == VGA_SWITCHEROO_IGD)
  342. gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 1);
  343. else
  344. gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 2);
  345. if (gmux_data->switch_state_display == VGA_SWITCHEROO_IGD)
  346. gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
  347. else
  348. gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
  349. if (gmux_data->switch_state_external == VGA_SWITCHEROO_IGD)
  350. gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
  351. else
  352. gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
  353. }
  354. static int gmux_switchto(enum vga_switcheroo_client_id id)
  355. {
  356. apple_gmux_data->switch_state_ddc = id;
  357. apple_gmux_data->switch_state_display = id;
  358. apple_gmux_data->switch_state_external = id;
  359. gmux_write_switch_state(apple_gmux_data);
  360. return 0;
  361. }
  362. static int gmux_switch_ddc(enum vga_switcheroo_client_id id)
  363. {
  364. enum vga_switcheroo_client_id old_ddc_owner =
  365. apple_gmux_data->switch_state_ddc;
  366. if (id == old_ddc_owner)
  367. return id;
  368. pr_debug("Switching DDC from %d to %d\n", old_ddc_owner, id);
  369. apple_gmux_data->switch_state_ddc = id;
  370. if (id == VGA_SWITCHEROO_IGD)
  371. gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 1);
  372. else
  373. gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 2);
  374. return old_ddc_owner;
  375. }
  376. /**
  377. * DOC: Power control
  378. *
  379. * gmux is able to cut power to the discrete GPU. It automatically takes care
  380. * of the correct sequence to tear down and bring up the power rails for
  381. * core voltage, VRAM and PCIe.
  382. */
  383. static int gmux_set_discrete_state(struct apple_gmux_data *gmux_data,
  384. enum vga_switcheroo_state state)
  385. {
  386. reinit_completion(&gmux_data->powerchange_done);
  387. if (state == VGA_SWITCHEROO_ON) {
  388. gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
  389. gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 3);
  390. pr_debug("Discrete card powered up\n");
  391. } else {
  392. gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
  393. gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 0);
  394. pr_debug("Discrete card powered down\n");
  395. }
  396. gmux_data->power_state = state;
  397. if (gmux_data->gpe >= 0 &&
  398. !wait_for_completion_interruptible_timeout(&gmux_data->powerchange_done,
  399. msecs_to_jiffies(200)))
  400. pr_warn("Timeout waiting for gmux switch to complete\n");
  401. return 0;
  402. }
  403. static int gmux_set_power_state(enum vga_switcheroo_client_id id,
  404. enum vga_switcheroo_state state)
  405. {
  406. if (id == VGA_SWITCHEROO_IGD)
  407. return 0;
  408. return gmux_set_discrete_state(apple_gmux_data, state);
  409. }
  410. static int gmux_get_client_id(struct pci_dev *pdev)
  411. {
  412. /*
  413. * Early Macbook Pros with switchable graphics use nvidia
  414. * integrated graphics. Hardcode that the 9400M is integrated.
  415. */
  416. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  417. return VGA_SWITCHEROO_IGD;
  418. else if (pdev->vendor == PCI_VENDOR_ID_NVIDIA &&
  419. pdev->device == 0x0863)
  420. return VGA_SWITCHEROO_IGD;
  421. else
  422. return VGA_SWITCHEROO_DIS;
  423. }
  424. static const struct vga_switcheroo_handler gmux_handler_indexed = {
  425. .switchto = gmux_switchto,
  426. .power_state = gmux_set_power_state,
  427. .get_client_id = gmux_get_client_id,
  428. };
  429. static const struct vga_switcheroo_handler gmux_handler_classic = {
  430. .switchto = gmux_switchto,
  431. .switch_ddc = gmux_switch_ddc,
  432. .power_state = gmux_set_power_state,
  433. .get_client_id = gmux_get_client_id,
  434. };
  435. /**
  436. * DOC: Interrupt
  437. *
  438. * gmux is also connected to a GPIO pin of the southbridge and thereby is able
  439. * to trigger an ACPI GPE. On the MBP5 2008/09 it's GPIO pin 22 of the Nvidia
  440. * MCP79, on all following generations it's GPIO pin 6 of the Intel PCH.
  441. * The GPE merely signals that an interrupt occurred, the actual type of event
  442. * is identified by reading a gmux register.
  443. */
  444. static inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data)
  445. {
  446. gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
  447. GMUX_INTERRUPT_DISABLE);
  448. }
  449. static inline void gmux_enable_interrupts(struct apple_gmux_data *gmux_data)
  450. {
  451. gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
  452. GMUX_INTERRUPT_ENABLE);
  453. }
  454. static inline u8 gmux_interrupt_get_status(struct apple_gmux_data *gmux_data)
  455. {
  456. return gmux_read8(gmux_data, GMUX_PORT_INTERRUPT_STATUS);
  457. }
  458. static void gmux_clear_interrupts(struct apple_gmux_data *gmux_data)
  459. {
  460. u8 status;
  461. /* to clear interrupts write back current status */
  462. status = gmux_interrupt_get_status(gmux_data);
  463. gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status);
  464. }
  465. static void gmux_notify_handler(acpi_handle device, u32 value, void *context)
  466. {
  467. u8 status;
  468. struct pnp_dev *pnp = (struct pnp_dev *)context;
  469. struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
  470. status = gmux_interrupt_get_status(gmux_data);
  471. gmux_disable_interrupts(gmux_data);
  472. pr_debug("Notify handler called: status %d\n", status);
  473. gmux_clear_interrupts(gmux_data);
  474. gmux_enable_interrupts(gmux_data);
  475. if (status & GMUX_INTERRUPT_STATUS_POWER)
  476. complete(&gmux_data->powerchange_done);
  477. }
  478. static int gmux_suspend(struct device *dev)
  479. {
  480. struct pnp_dev *pnp = to_pnp_dev(dev);
  481. struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
  482. gmux_disable_interrupts(gmux_data);
  483. return 0;
  484. }
  485. static int gmux_resume(struct device *dev)
  486. {
  487. struct pnp_dev *pnp = to_pnp_dev(dev);
  488. struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
  489. gmux_enable_interrupts(gmux_data);
  490. gmux_write_switch_state(gmux_data);
  491. if (gmux_data->power_state == VGA_SWITCHEROO_OFF)
  492. gmux_set_discrete_state(gmux_data, gmux_data->power_state);
  493. return 0;
  494. }
  495. static struct pci_dev *gmux_get_io_pdev(void)
  496. {
  497. struct pci_dev *pdev = NULL;
  498. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev))) {
  499. u16 cmd;
  500. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  501. if (!(cmd & PCI_COMMAND_IO))
  502. continue;
  503. return pdev;
  504. }
  505. return NULL;
  506. }
  507. static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  508. {
  509. struct apple_gmux_data *gmux_data;
  510. struct resource *res;
  511. struct backlight_properties props;
  512. struct backlight_device *bdev;
  513. u8 ver_major, ver_minor, ver_release;
  514. int ret = -ENXIO;
  515. acpi_status status;
  516. unsigned long long gpe;
  517. struct pci_dev *pdev = NULL;
  518. if (apple_gmux_data)
  519. return -EBUSY;
  520. gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
  521. if (!gmux_data)
  522. return -ENOMEM;
  523. pnp_set_drvdata(pnp, gmux_data);
  524. res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
  525. if (!res) {
  526. pr_err("Failed to find gmux I/O resource\n");
  527. goto err_free;
  528. }
  529. gmux_data->iostart = res->start;
  530. gmux_data->iolen = res->end - res->start;
  531. if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
  532. pr_err("gmux I/O region too small (%lu < %u)\n",
  533. gmux_data->iolen, GMUX_MIN_IO_LEN);
  534. goto err_free;
  535. }
  536. if (!request_region(gmux_data->iostart, gmux_data->iolen,
  537. "Apple gmux")) {
  538. pr_err("gmux I/O already in use\n");
  539. goto err_free;
  540. }
  541. /*
  542. * Invalid version information may indicate either that the gmux
  543. * device isn't present or that it's a new one that uses indexed
  544. * io
  545. */
  546. ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
  547. ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
  548. ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
  549. if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
  550. if (gmux_is_indexed(gmux_data)) {
  551. u32 version;
  552. mutex_init(&gmux_data->index_lock);
  553. gmux_data->indexed = true;
  554. version = gmux_read32(gmux_data,
  555. GMUX_PORT_VERSION_MAJOR);
  556. ver_major = (version >> 24) & 0xff;
  557. ver_minor = (version >> 16) & 0xff;
  558. ver_release = (version >> 8) & 0xff;
  559. } else {
  560. pr_info("gmux device not present or IO disabled\n");
  561. ret = -ENODEV;
  562. goto err_release;
  563. }
  564. }
  565. pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
  566. ver_release, (gmux_data->indexed ? "indexed" : "classic"));
  567. /*
  568. * Apple systems with gmux are EFI based and normally don't use
  569. * VGA. In addition changing IO+MEM ownership between IGP and dGPU
  570. * disables IO/MEM used for backlight control on some systems.
  571. * Lock IO+MEM to GPU with active IO to prevent switch.
  572. */
  573. pdev = gmux_get_io_pdev();
  574. if (pdev && vga_tryget(pdev,
  575. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM)) {
  576. pr_err("IO+MEM vgaarb-locking for PCI:%s failed\n",
  577. pci_name(pdev));
  578. ret = -EBUSY;
  579. goto err_release;
  580. } else if (pdev)
  581. pr_info("locked IO for PCI:%s\n", pci_name(pdev));
  582. gmux_data->pdev = pdev;
  583. memset(&props, 0, sizeof(props));
  584. props.type = BACKLIGHT_PLATFORM;
  585. props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
  586. /*
  587. * Currently it's assumed that the maximum brightness is less than
  588. * 2^24 for compatibility with old gmux versions. Cap the max
  589. * brightness at this value, but print a warning if the hardware
  590. * reports something higher so that it can be fixed.
  591. */
  592. if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
  593. props.max_brightness = GMUX_MAX_BRIGHTNESS;
  594. bdev = backlight_device_register("gmux_backlight", &pnp->dev,
  595. gmux_data, &gmux_bl_ops, &props);
  596. if (IS_ERR(bdev)) {
  597. ret = PTR_ERR(bdev);
  598. goto err_release;
  599. }
  600. gmux_data->bdev = bdev;
  601. bdev->props.brightness = gmux_get_brightness(bdev);
  602. backlight_update_status(bdev);
  603. /*
  604. * The backlight situation on Macs is complicated. If the gmux is
  605. * present it's the best choice, because it always works for
  606. * backlight control and supports more levels than other options.
  607. * Disable the other backlight choices.
  608. */
  609. acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
  610. apple_bl_unregister();
  611. gmux_data->power_state = VGA_SWITCHEROO_ON;
  612. gmux_data->dhandle = ACPI_HANDLE(&pnp->dev);
  613. if (!gmux_data->dhandle) {
  614. pr_err("Cannot find acpi handle for pnp device %s\n",
  615. dev_name(&pnp->dev));
  616. ret = -ENODEV;
  617. goto err_notify;
  618. }
  619. status = acpi_evaluate_integer(gmux_data->dhandle, "GMGP", NULL, &gpe);
  620. if (ACPI_SUCCESS(status)) {
  621. gmux_data->gpe = (int)gpe;
  622. status = acpi_install_notify_handler(gmux_data->dhandle,
  623. ACPI_DEVICE_NOTIFY,
  624. &gmux_notify_handler, pnp);
  625. if (ACPI_FAILURE(status)) {
  626. pr_err("Install notify handler failed: %s\n",
  627. acpi_format_exception(status));
  628. ret = -ENODEV;
  629. goto err_notify;
  630. }
  631. status = acpi_enable_gpe(NULL, gmux_data->gpe);
  632. if (ACPI_FAILURE(status)) {
  633. pr_err("Cannot enable gpe: %s\n",
  634. acpi_format_exception(status));
  635. goto err_enable_gpe;
  636. }
  637. } else {
  638. pr_warn("No GPE found for gmux\n");
  639. gmux_data->gpe = -1;
  640. }
  641. apple_gmux_data = gmux_data;
  642. init_completion(&gmux_data->powerchange_done);
  643. gmux_enable_interrupts(gmux_data);
  644. gmux_read_switch_state(gmux_data);
  645. /*
  646. * Retina MacBook Pros cannot switch the panel's AUX separately
  647. * and need eDP pre-calibration. They are distinguishable from
  648. * pre-retinas by having an "indexed" gmux.
  649. *
  650. * Pre-retina MacBook Pros can switch the panel's DDC separately.
  651. */
  652. if (gmux_data->indexed)
  653. ret = vga_switcheroo_register_handler(&gmux_handler_indexed,
  654. VGA_SWITCHEROO_NEEDS_EDP_CONFIG);
  655. else
  656. ret = vga_switcheroo_register_handler(&gmux_handler_classic,
  657. VGA_SWITCHEROO_CAN_SWITCH_DDC);
  658. if (ret) {
  659. pr_err("Failed to register vga_switcheroo handler\n");
  660. goto err_register_handler;
  661. }
  662. return 0;
  663. err_register_handler:
  664. gmux_disable_interrupts(gmux_data);
  665. apple_gmux_data = NULL;
  666. if (gmux_data->gpe >= 0)
  667. acpi_disable_gpe(NULL, gmux_data->gpe);
  668. err_enable_gpe:
  669. if (gmux_data->gpe >= 0)
  670. acpi_remove_notify_handler(gmux_data->dhandle,
  671. ACPI_DEVICE_NOTIFY,
  672. &gmux_notify_handler);
  673. err_notify:
  674. backlight_device_unregister(bdev);
  675. err_release:
  676. if (gmux_data->pdev)
  677. vga_put(gmux_data->pdev,
  678. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM);
  679. pci_dev_put(pdev);
  680. release_region(gmux_data->iostart, gmux_data->iolen);
  681. err_free:
  682. kfree(gmux_data);
  683. return ret;
  684. }
  685. static void gmux_remove(struct pnp_dev *pnp)
  686. {
  687. struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
  688. vga_switcheroo_unregister_handler();
  689. gmux_disable_interrupts(gmux_data);
  690. if (gmux_data->gpe >= 0) {
  691. acpi_disable_gpe(NULL, gmux_data->gpe);
  692. acpi_remove_notify_handler(gmux_data->dhandle,
  693. ACPI_DEVICE_NOTIFY,
  694. &gmux_notify_handler);
  695. }
  696. if (gmux_data->pdev) {
  697. vga_put(gmux_data->pdev,
  698. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM);
  699. pci_dev_put(gmux_data->pdev);
  700. }
  701. backlight_device_unregister(gmux_data->bdev);
  702. release_region(gmux_data->iostart, gmux_data->iolen);
  703. apple_gmux_data = NULL;
  704. kfree(gmux_data);
  705. acpi_video_register();
  706. apple_bl_register();
  707. }
  708. static const struct pnp_device_id gmux_device_ids[] = {
  709. {GMUX_ACPI_HID, 0},
  710. {"", 0}
  711. };
  712. static const struct dev_pm_ops gmux_dev_pm_ops = {
  713. .suspend = gmux_suspend,
  714. .resume = gmux_resume,
  715. };
  716. static struct pnp_driver gmux_pnp_driver = {
  717. .name = "apple-gmux",
  718. .probe = gmux_probe,
  719. .remove = gmux_remove,
  720. .id_table = gmux_device_ids,
  721. .driver = {
  722. .pm = &gmux_dev_pm_ops,
  723. },
  724. };
  725. module_pnp_driver(gmux_pnp_driver);
  726. MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
  727. MODULE_DESCRIPTION("Apple Gmux Driver");
  728. MODULE_LICENSE("GPL");
  729. MODULE_DEVICE_TABLE(pnp, gmux_device_ids);