apple-gmux.c 26 KB

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