dvo_ivch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright © 2006 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Thomas Richter <thor@math.tu-berlin.de>
  26. *
  27. * Minor modifications (Dithering enable):
  28. * Thomas Richter <thor@math.tu-berlin.de>
  29. *
  30. */
  31. #include "dvo.h"
  32. /*
  33. * register definitions for the i82807aa.
  34. *
  35. * Documentation on this chipset can be found in datasheet #29069001 at
  36. * intel.com.
  37. */
  38. /*
  39. * VCH Revision & GMBus Base Addr
  40. */
  41. #define VR00 0x00
  42. # define VR00_BASE_ADDRESS_MASK 0x007f
  43. /*
  44. * Functionality Enable
  45. */
  46. #define VR01 0x01
  47. /*
  48. * Enable the panel fitter
  49. */
  50. # define VR01_PANEL_FIT_ENABLE (1 << 3)
  51. /*
  52. * Enables the LCD display.
  53. *
  54. * This must not be set while VR01_DVO_BYPASS_ENABLE is set.
  55. */
  56. # define VR01_LCD_ENABLE (1 << 2)
  57. /** Enables the DVO repeater. */
  58. # define VR01_DVO_BYPASS_ENABLE (1 << 1)
  59. /** Enables the DVO clock */
  60. # define VR01_DVO_ENABLE (1 << 0)
  61. /** Enable dithering for 18bpp panels. Not documented. */
  62. # define VR01_DITHER_ENABLE (1 << 4)
  63. /*
  64. * LCD Interface Format
  65. */
  66. #define VR10 0x10
  67. /** Enables LVDS output instead of CMOS */
  68. # define VR10_LVDS_ENABLE (1 << 4)
  69. /** Enables 18-bit LVDS output. */
  70. # define VR10_INTERFACE_1X18 (0 << 2)
  71. /** Enables 24-bit LVDS or CMOS output */
  72. # define VR10_INTERFACE_1X24 (1 << 2)
  73. /** Enables 2x18-bit LVDS or CMOS output. */
  74. # define VR10_INTERFACE_2X18 (2 << 2)
  75. /** Enables 2x24-bit LVDS output */
  76. # define VR10_INTERFACE_2X24 (3 << 2)
  77. /** Mask that defines the depth of the pipeline */
  78. # define VR10_INTERFACE_DEPTH_MASK (3 << 2)
  79. /*
  80. * VR20 LCD Horizontal Display Size
  81. */
  82. #define VR20 0x20
  83. /*
  84. * LCD Vertical Display Size
  85. */
  86. #define VR21 0x21
  87. /*
  88. * Panel power down status
  89. */
  90. #define VR30 0x30
  91. /** Read only bit indicating that the panel is not in a safe poweroff state. */
  92. # define VR30_PANEL_ON (1 << 15)
  93. #define VR40 0x40
  94. # define VR40_STALL_ENABLE (1 << 13)
  95. # define VR40_VERTICAL_INTERP_ENABLE (1 << 12)
  96. # define VR40_ENHANCED_PANEL_FITTING (1 << 11)
  97. # define VR40_HORIZONTAL_INTERP_ENABLE (1 << 10)
  98. # define VR40_AUTO_RATIO_ENABLE (1 << 9)
  99. # define VR40_CLOCK_GATING_ENABLE (1 << 8)
  100. /*
  101. * Panel Fitting Vertical Ratio
  102. * (((image_height - 1) << 16) / ((panel_height - 1))) >> 2
  103. */
  104. #define VR41 0x41
  105. /*
  106. * Panel Fitting Horizontal Ratio
  107. * (((image_width - 1) << 16) / ((panel_width - 1))) >> 2
  108. */
  109. #define VR42 0x42
  110. /*
  111. * Horizontal Image Size
  112. */
  113. #define VR43 0x43
  114. /* VR80 GPIO 0
  115. */
  116. #define VR80 0x80
  117. #define VR81 0x81
  118. #define VR82 0x82
  119. #define VR83 0x83
  120. #define VR84 0x84
  121. #define VR85 0x85
  122. #define VR86 0x86
  123. #define VR87 0x87
  124. /* VR88 GPIO 8
  125. */
  126. #define VR88 0x88
  127. /* Graphics BIOS scratch 0
  128. */
  129. #define VR8E 0x8E
  130. # define VR8E_PANEL_TYPE_MASK (0xf << 0)
  131. # define VR8E_PANEL_INTERFACE_CMOS (0 << 4)
  132. # define VR8E_PANEL_INTERFACE_LVDS (1 << 4)
  133. # define VR8E_FORCE_DEFAULT_PANEL (1 << 5)
  134. /* Graphics BIOS scratch 1
  135. */
  136. #define VR8F 0x8F
  137. # define VR8F_VCH_PRESENT (1 << 0)
  138. # define VR8F_DISPLAY_CONN (1 << 1)
  139. # define VR8F_POWER_MASK (0x3c)
  140. # define VR8F_POWER_POS (2)
  141. /* Some Bios implementations do not restore the DVO state upon
  142. * resume from standby. Thus, this driver has to handle it
  143. * instead. The following list contains all registers that
  144. * require saving.
  145. */
  146. static const uint16_t backup_addresses[] = {
  147. 0x11, 0x12,
  148. 0x18, 0x19, 0x1a, 0x1f,
  149. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  150. 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  151. 0x8e, 0x8f,
  152. 0x10 /* this must come last */
  153. };
  154. struct ivch_priv {
  155. bool quiet;
  156. uint16_t width, height;
  157. /* Register backup */
  158. uint16_t reg_backup[ARRAY_SIZE(backup_addresses)];
  159. };
  160. static void ivch_dump_regs(struct intel_dvo_device *dvo);
  161. /**
  162. * Reads a register on the ivch.
  163. *
  164. * Each of the 256 registers are 16 bits long.
  165. */
  166. static bool ivch_read(struct intel_dvo_device *dvo, int addr, uint16_t *data)
  167. {
  168. struct ivch_priv *priv = dvo->dev_priv;
  169. struct i2c_adapter *adapter = dvo->i2c_bus;
  170. u8 out_buf[1];
  171. u8 in_buf[2];
  172. struct i2c_msg msgs[] = {
  173. {
  174. .addr = dvo->slave_addr,
  175. .flags = I2C_M_RD,
  176. .len = 0,
  177. },
  178. {
  179. .addr = 0,
  180. .flags = I2C_M_NOSTART,
  181. .len = 1,
  182. .buf = out_buf,
  183. },
  184. {
  185. .addr = dvo->slave_addr,
  186. .flags = I2C_M_RD | I2C_M_NOSTART,
  187. .len = 2,
  188. .buf = in_buf,
  189. }
  190. };
  191. out_buf[0] = addr;
  192. if (i2c_transfer(adapter, msgs, 3) == 3) {
  193. *data = (in_buf[1] << 8) | in_buf[0];
  194. return true;
  195. }
  196. if (!priv->quiet) {
  197. DRM_DEBUG_KMS("Unable to read register 0x%02x from "
  198. "%s:%02x.\n",
  199. addr, adapter->name, dvo->slave_addr);
  200. }
  201. return false;
  202. }
  203. /** Writes a 16-bit register on the ivch */
  204. static bool ivch_write(struct intel_dvo_device *dvo, int addr, uint16_t data)
  205. {
  206. struct ivch_priv *priv = dvo->dev_priv;
  207. struct i2c_adapter *adapter = dvo->i2c_bus;
  208. u8 out_buf[3];
  209. struct i2c_msg msg = {
  210. .addr = dvo->slave_addr,
  211. .flags = 0,
  212. .len = 3,
  213. .buf = out_buf,
  214. };
  215. out_buf[0] = addr;
  216. out_buf[1] = data & 0xff;
  217. out_buf[2] = data >> 8;
  218. if (i2c_transfer(adapter, &msg, 1) == 1)
  219. return true;
  220. if (!priv->quiet) {
  221. DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d.\n",
  222. addr, adapter->name, dvo->slave_addr);
  223. }
  224. return false;
  225. }
  226. /** Probes the given bus and slave address for an ivch */
  227. static bool ivch_init(struct intel_dvo_device *dvo,
  228. struct i2c_adapter *adapter)
  229. {
  230. struct ivch_priv *priv;
  231. uint16_t temp;
  232. int i;
  233. priv = kzalloc(sizeof(struct ivch_priv), GFP_KERNEL);
  234. if (priv == NULL)
  235. return false;
  236. dvo->i2c_bus = adapter;
  237. dvo->dev_priv = priv;
  238. priv->quiet = true;
  239. if (!ivch_read(dvo, VR00, &temp))
  240. goto out;
  241. priv->quiet = false;
  242. /* Since the identification bits are probably zeroes, which doesn't seem
  243. * very unique, check that the value in the base address field matches
  244. * the address it's responding on.
  245. */
  246. if ((temp & VR00_BASE_ADDRESS_MASK) != dvo->slave_addr) {
  247. DRM_DEBUG_KMS("ivch detect failed due to address mismatch "
  248. "(%d vs %d)\n",
  249. (temp & VR00_BASE_ADDRESS_MASK), dvo->slave_addr);
  250. goto out;
  251. }
  252. ivch_read(dvo, VR20, &priv->width);
  253. ivch_read(dvo, VR21, &priv->height);
  254. /* Make a backup of the registers to be able to restore them
  255. * upon suspend.
  256. */
  257. for (i = 0; i < ARRAY_SIZE(backup_addresses); i++)
  258. ivch_read(dvo, backup_addresses[i], priv->reg_backup + i);
  259. ivch_dump_regs(dvo);
  260. return true;
  261. out:
  262. kfree(priv);
  263. return false;
  264. }
  265. static enum drm_connector_status ivch_detect(struct intel_dvo_device *dvo)
  266. {
  267. return connector_status_connected;
  268. }
  269. static enum drm_mode_status ivch_mode_valid(struct intel_dvo_device *dvo,
  270. struct drm_display_mode *mode)
  271. {
  272. if (mode->clock > 112000)
  273. return MODE_CLOCK_HIGH;
  274. return MODE_OK;
  275. }
  276. /* Restore the DVO registers after a resume
  277. * from RAM. Registers have been saved during
  278. * the initialization.
  279. */
  280. static void ivch_reset(struct intel_dvo_device *dvo)
  281. {
  282. struct ivch_priv *priv = dvo->dev_priv;
  283. int i;
  284. DRM_DEBUG_KMS("Resetting the IVCH registers\n");
  285. ivch_write(dvo, VR10, 0x0000);
  286. for (i = 0; i < ARRAY_SIZE(backup_addresses); i++)
  287. ivch_write(dvo, backup_addresses[i], priv->reg_backup[i]);
  288. }
  289. /** Sets the power state of the panel connected to the ivch */
  290. static void ivch_dpms(struct intel_dvo_device *dvo, bool enable)
  291. {
  292. int i;
  293. uint16_t vr01, vr30, backlight;
  294. ivch_reset(dvo);
  295. /* Set the new power state of the panel. */
  296. if (!ivch_read(dvo, VR01, &vr01))
  297. return;
  298. if (enable)
  299. backlight = 1;
  300. else
  301. backlight = 0;
  302. ivch_write(dvo, VR80, backlight);
  303. if (enable)
  304. vr01 |= VR01_LCD_ENABLE | VR01_DVO_ENABLE;
  305. else
  306. vr01 &= ~(VR01_LCD_ENABLE | VR01_DVO_ENABLE);
  307. ivch_write(dvo, VR01, vr01);
  308. /* Wait for the panel to make its state transition */
  309. for (i = 0; i < 100; i++) {
  310. if (!ivch_read(dvo, VR30, &vr30))
  311. break;
  312. if (((vr30 & VR30_PANEL_ON) != 0) == enable)
  313. break;
  314. udelay(1000);
  315. }
  316. /* wait some more; vch may fail to resync sometimes without this */
  317. udelay(16 * 1000);
  318. }
  319. static bool ivch_get_hw_state(struct intel_dvo_device *dvo)
  320. {
  321. uint16_t vr01;
  322. ivch_reset(dvo);
  323. /* Set the new power state of the panel. */
  324. if (!ivch_read(dvo, VR01, &vr01))
  325. return false;
  326. if (vr01 & VR01_LCD_ENABLE)
  327. return true;
  328. else
  329. return false;
  330. }
  331. static void ivch_mode_set(struct intel_dvo_device *dvo,
  332. const struct drm_display_mode *mode,
  333. const struct drm_display_mode *adjusted_mode)
  334. {
  335. struct ivch_priv *priv = dvo->dev_priv;
  336. uint16_t vr40 = 0;
  337. uint16_t vr01 = 0;
  338. uint16_t vr10;
  339. ivch_reset(dvo);
  340. vr10 = priv->reg_backup[ARRAY_SIZE(backup_addresses) - 1];
  341. /* Enable dithering for 18 bpp pipelines */
  342. vr10 &= VR10_INTERFACE_DEPTH_MASK;
  343. if (vr10 == VR10_INTERFACE_2X18 || vr10 == VR10_INTERFACE_1X18)
  344. vr01 = VR01_DITHER_ENABLE;
  345. vr40 = (VR40_STALL_ENABLE | VR40_VERTICAL_INTERP_ENABLE |
  346. VR40_HORIZONTAL_INTERP_ENABLE);
  347. if (mode->hdisplay != adjusted_mode->crtc_hdisplay ||
  348. mode->vdisplay != adjusted_mode->crtc_vdisplay) {
  349. uint16_t x_ratio, y_ratio;
  350. vr01 |= VR01_PANEL_FIT_ENABLE;
  351. vr40 |= VR40_CLOCK_GATING_ENABLE;
  352. x_ratio = (((mode->hdisplay - 1) << 16) /
  353. (adjusted_mode->crtc_hdisplay - 1)) >> 2;
  354. y_ratio = (((mode->vdisplay - 1) << 16) /
  355. (adjusted_mode->crtc_vdisplay - 1)) >> 2;
  356. ivch_write(dvo, VR42, x_ratio);
  357. ivch_write(dvo, VR41, y_ratio);
  358. } else {
  359. vr01 &= ~VR01_PANEL_FIT_ENABLE;
  360. vr40 &= ~VR40_CLOCK_GATING_ENABLE;
  361. }
  362. vr40 &= ~VR40_AUTO_RATIO_ENABLE;
  363. ivch_write(dvo, VR01, vr01);
  364. ivch_write(dvo, VR40, vr40);
  365. }
  366. static void ivch_dump_regs(struct intel_dvo_device *dvo)
  367. {
  368. uint16_t val;
  369. ivch_read(dvo, VR00, &val);
  370. DRM_DEBUG_KMS("VR00: 0x%04x\n", val);
  371. ivch_read(dvo, VR01, &val);
  372. DRM_DEBUG_KMS("VR01: 0x%04x\n", val);
  373. ivch_read(dvo, VR10, &val);
  374. DRM_DEBUG_KMS("VR10: 0x%04x\n", val);
  375. ivch_read(dvo, VR30, &val);
  376. DRM_DEBUG_KMS("VR30: 0x%04x\n", val);
  377. ivch_read(dvo, VR40, &val);
  378. DRM_DEBUG_KMS("VR40: 0x%04x\n", val);
  379. /* GPIO registers */
  380. ivch_read(dvo, VR80, &val);
  381. DRM_DEBUG_KMS("VR80: 0x%04x\n", val);
  382. ivch_read(dvo, VR81, &val);
  383. DRM_DEBUG_KMS("VR81: 0x%04x\n", val);
  384. ivch_read(dvo, VR82, &val);
  385. DRM_DEBUG_KMS("VR82: 0x%04x\n", val);
  386. ivch_read(dvo, VR83, &val);
  387. DRM_DEBUG_KMS("VR83: 0x%04x\n", val);
  388. ivch_read(dvo, VR84, &val);
  389. DRM_DEBUG_KMS("VR84: 0x%04x\n", val);
  390. ivch_read(dvo, VR85, &val);
  391. DRM_DEBUG_KMS("VR85: 0x%04x\n", val);
  392. ivch_read(dvo, VR86, &val);
  393. DRM_DEBUG_KMS("VR86: 0x%04x\n", val);
  394. ivch_read(dvo, VR87, &val);
  395. DRM_DEBUG_KMS("VR87: 0x%04x\n", val);
  396. ivch_read(dvo, VR88, &val);
  397. DRM_DEBUG_KMS("VR88: 0x%04x\n", val);
  398. /* Scratch register 0 - AIM Panel type */
  399. ivch_read(dvo, VR8E, &val);
  400. DRM_DEBUG_KMS("VR8E: 0x%04x\n", val);
  401. /* Scratch register 1 - Status register */
  402. ivch_read(dvo, VR8F, &val);
  403. DRM_DEBUG_KMS("VR8F: 0x%04x\n", val);
  404. }
  405. static void ivch_destroy(struct intel_dvo_device *dvo)
  406. {
  407. struct ivch_priv *priv = dvo->dev_priv;
  408. if (priv) {
  409. kfree(priv);
  410. dvo->dev_priv = NULL;
  411. }
  412. }
  413. struct intel_dvo_dev_ops ivch_ops = {
  414. .init = ivch_init,
  415. .dpms = ivch_dpms,
  416. .get_hw_state = ivch_get_hw_state,
  417. .mode_valid = ivch_mode_valid,
  418. .mode_set = ivch_mode_set,
  419. .detect = ivch_detect,
  420. .dump_regs = ivch_dump_regs,
  421. .destroy = ivch_destroy,
  422. };