apple-gmux.c 25 KB

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