apple-gmux.c 25 KB

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