apple-gmux.c 23 KB

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