dvo_ns2501.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. *
  3. * Copyright (c) 2012 Gilles Dartiguelongue, Thomas Richter
  4. *
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  23. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28. #include "dvo.h"
  29. #include "i915_reg.h"
  30. #include "i915_drv.h"
  31. #define NS2501_VID 0x1305
  32. #define NS2501_DID 0x6726
  33. #define NS2501_VID_LO 0x00
  34. #define NS2501_VID_HI 0x01
  35. #define NS2501_DID_LO 0x02
  36. #define NS2501_DID_HI 0x03
  37. #define NS2501_REV 0x04
  38. #define NS2501_RSVD 0x05
  39. #define NS2501_FREQ_LO 0x06
  40. #define NS2501_FREQ_HI 0x07
  41. #define NS2501_REG8 0x08
  42. #define NS2501_8_VEN (1<<5)
  43. #define NS2501_8_HEN (1<<4)
  44. #define NS2501_8_DSEL (1<<3)
  45. #define NS2501_8_BPAS (1<<2)
  46. #define NS2501_8_RSVD (1<<1)
  47. #define NS2501_8_PD (1<<0)
  48. #define NS2501_REG9 0x09
  49. #define NS2501_9_VLOW (1<<7)
  50. #define NS2501_9_MSEL_MASK (0x7<<4)
  51. #define NS2501_9_TSEL (1<<3)
  52. #define NS2501_9_RSEN (1<<2)
  53. #define NS2501_9_RSVD (1<<1)
  54. #define NS2501_9_MDI (1<<0)
  55. #define NS2501_REGC 0x0c
  56. struct ns2501_priv {
  57. //I2CDevRec d;
  58. bool quiet;
  59. int reg_8_shadow;
  60. int reg_8_set;
  61. // Shadow registers for i915
  62. int dvoc;
  63. int pll_a;
  64. int srcdim;
  65. int fw_blc;
  66. };
  67. #define NSPTR(d) ((NS2501Ptr)(d->DriverPrivate.ptr))
  68. /*
  69. * For reasons unclear to me, the ns2501 at least on the Fujitsu/Siemens
  70. * laptops does not react on the i2c bus unless
  71. * both the PLL is running and the display is configured in its native
  72. * resolution.
  73. * This function forces the DVO on, and stores the registers it touches.
  74. * Afterwards, registers are restored to regular values.
  75. *
  76. * This is pretty much a hack, though it works.
  77. * Without that, ns2501_readb and ns2501_writeb fail
  78. * when switching the resolution.
  79. */
  80. /*
  81. ** Read a register from the ns2501.
  82. ** Returns true if successful, false otherwise.
  83. ** If it returns false, it might be wise to enable the
  84. ** DVO with the above function.
  85. */
  86. static bool ns2501_readb(struct intel_dvo_device *dvo, int addr, uint8_t * ch)
  87. {
  88. struct ns2501_priv *ns = dvo->dev_priv;
  89. struct i2c_adapter *adapter = dvo->i2c_bus;
  90. u8 out_buf[2];
  91. u8 in_buf[2];
  92. struct i2c_msg msgs[] = {
  93. {
  94. .addr = dvo->slave_addr,
  95. .flags = 0,
  96. .len = 1,
  97. .buf = out_buf,
  98. },
  99. {
  100. .addr = dvo->slave_addr,
  101. .flags = I2C_M_RD,
  102. .len = 1,
  103. .buf = in_buf,
  104. }
  105. };
  106. out_buf[0] = addr;
  107. out_buf[1] = 0;
  108. if (i2c_transfer(adapter, msgs, 2) == 2) {
  109. *ch = in_buf[0];
  110. return true;
  111. }
  112. if (!ns->quiet) {
  113. DRM_DEBUG_KMS
  114. ("Unable to read register 0x%02x from %s:0x%02x.\n", addr,
  115. adapter->name, dvo->slave_addr);
  116. }
  117. return false;
  118. }
  119. /*
  120. ** Write a register to the ns2501.
  121. ** Returns true if successful, false otherwise.
  122. ** If it returns false, it might be wise to enable the
  123. ** DVO with the above function.
  124. */
  125. static bool ns2501_writeb(struct intel_dvo_device *dvo, int addr, uint8_t ch)
  126. {
  127. struct ns2501_priv *ns = dvo->dev_priv;
  128. struct i2c_adapter *adapter = dvo->i2c_bus;
  129. uint8_t out_buf[2];
  130. struct i2c_msg msg = {
  131. .addr = dvo->slave_addr,
  132. .flags = 0,
  133. .len = 2,
  134. .buf = out_buf,
  135. };
  136. out_buf[0] = addr;
  137. out_buf[1] = ch;
  138. if (i2c_transfer(adapter, &msg, 1) == 1) {
  139. return true;
  140. }
  141. if (!ns->quiet) {
  142. DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d\n",
  143. addr, adapter->name, dvo->slave_addr);
  144. }
  145. return false;
  146. }
  147. /* National Semiconductor 2501 driver for chip on i2c bus
  148. * scan for the chip on the bus.
  149. * Hope the VBIOS initialized the PLL correctly so we can
  150. * talk to it. If not, it will not be seen and not detected.
  151. * Bummer!
  152. */
  153. static bool ns2501_init(struct intel_dvo_device *dvo,
  154. struct i2c_adapter *adapter)
  155. {
  156. /* this will detect the NS2501 chip on the specified i2c bus */
  157. struct ns2501_priv *ns;
  158. unsigned char ch;
  159. ns = kzalloc(sizeof(struct ns2501_priv), GFP_KERNEL);
  160. if (ns == NULL)
  161. return false;
  162. dvo->i2c_bus = adapter;
  163. dvo->dev_priv = ns;
  164. ns->quiet = true;
  165. if (!ns2501_readb(dvo, NS2501_VID_LO, &ch))
  166. goto out;
  167. if (ch != (NS2501_VID & 0xff)) {
  168. DRM_DEBUG_KMS("ns2501 not detected got %d: from %s Slave %d.\n",
  169. ch, adapter->name, dvo->slave_addr);
  170. goto out;
  171. }
  172. if (!ns2501_readb(dvo, NS2501_DID_LO, &ch))
  173. goto out;
  174. if (ch != (NS2501_DID & 0xff)) {
  175. DRM_DEBUG_KMS("ns2501 not detected got %d: from %s Slave %d.\n",
  176. ch, adapter->name, dvo->slave_addr);
  177. goto out;
  178. }
  179. ns->quiet = false;
  180. ns->reg_8_set = 0;
  181. ns->reg_8_shadow =
  182. NS2501_8_PD | NS2501_8_BPAS | NS2501_8_VEN | NS2501_8_HEN;
  183. DRM_DEBUG_KMS("init ns2501 dvo controller successfully!\n");
  184. return true;
  185. out:
  186. kfree(ns);
  187. return false;
  188. }
  189. static enum drm_connector_status ns2501_detect(struct intel_dvo_device *dvo)
  190. {
  191. /*
  192. * This is a Laptop display, it doesn't have hotplugging.
  193. * Even if not, the detection bit of the 2501 is unreliable as
  194. * it only works for some display types.
  195. * It is even more unreliable as the PLL must be active for
  196. * allowing reading from the chiop.
  197. */
  198. return connector_status_connected;
  199. }
  200. static enum drm_mode_status ns2501_mode_valid(struct intel_dvo_device *dvo,
  201. struct drm_display_mode *mode)
  202. {
  203. DRM_DEBUG_KMS
  204. ("is mode valid (hdisplay=%d,htotal=%d,vdisplay=%d,vtotal=%d)\n",
  205. mode->hdisplay, mode->htotal, mode->vdisplay, mode->vtotal);
  206. /*
  207. * Currently, these are all the modes I have data from.
  208. * More might exist. Unclear how to find the native resolution
  209. * of the panel in here so we could always accept it
  210. * by disabling the scaler.
  211. */
  212. if ((mode->hdisplay == 800 && mode->vdisplay == 600) ||
  213. (mode->hdisplay == 640 && mode->vdisplay == 480) ||
  214. (mode->hdisplay == 1024 && mode->vdisplay == 768)) {
  215. return MODE_OK;
  216. } else {
  217. return MODE_ONE_SIZE; /* Is this a reasonable error? */
  218. }
  219. }
  220. static void ns2501_mode_set(struct intel_dvo_device *dvo,
  221. struct drm_display_mode *mode,
  222. struct drm_display_mode *adjusted_mode)
  223. {
  224. bool ok;
  225. int retries = 10;
  226. struct ns2501_priv *ns = (struct ns2501_priv *)(dvo->dev_priv);
  227. DRM_DEBUG_KMS
  228. ("set mode (hdisplay=%d,htotal=%d,vdisplay=%d,vtotal=%d).\n",
  229. mode->hdisplay, mode->htotal, mode->vdisplay, mode->vtotal);
  230. /*
  231. * Where do I find the native resolution for which scaling is not required???
  232. *
  233. * First trigger the DVO on as otherwise the chip does not appear on the i2c
  234. * bus.
  235. */
  236. do {
  237. ok = true;
  238. if (mode->hdisplay == 800 && mode->vdisplay == 600) {
  239. /* mode 277 */
  240. ns->reg_8_shadow &= ~NS2501_8_BPAS;
  241. DRM_DEBUG_KMS("switching to 800x600\n");
  242. /*
  243. * No, I do not know where this data comes from.
  244. * It is just what the video bios left in the DVO, so
  245. * I'm just copying it here over.
  246. * This also means that I cannot support any other modes
  247. * except the ones supported by the bios.
  248. */
  249. ok &= ns2501_writeb(dvo, 0x11, 0xc8); // 0xc7 also works.
  250. ok &= ns2501_writeb(dvo, 0x1b, 0x19);
  251. ok &= ns2501_writeb(dvo, 0x1c, 0x62); // VBIOS left 0x64 here, but 0x62 works nicer
  252. ok &= ns2501_writeb(dvo, 0x1d, 0x02);
  253. ok &= ns2501_writeb(dvo, 0x34, 0x03);
  254. ok &= ns2501_writeb(dvo, 0x35, 0xff);
  255. ok &= ns2501_writeb(dvo, 0x80, 0x27);
  256. ok &= ns2501_writeb(dvo, 0x81, 0x03);
  257. ok &= ns2501_writeb(dvo, 0x82, 0x41);
  258. ok &= ns2501_writeb(dvo, 0x83, 0x05);
  259. ok &= ns2501_writeb(dvo, 0x8d, 0x02);
  260. ok &= ns2501_writeb(dvo, 0x8e, 0x04);
  261. ok &= ns2501_writeb(dvo, 0x8f, 0x00);
  262. ok &= ns2501_writeb(dvo, 0x90, 0xfe); /* vertical. VBIOS left 0xff here, but 0xfe works better */
  263. ok &= ns2501_writeb(dvo, 0x91, 0x07);
  264. ok &= ns2501_writeb(dvo, 0x94, 0x00);
  265. ok &= ns2501_writeb(dvo, 0x95, 0x00);
  266. ok &= ns2501_writeb(dvo, 0x96, 0x00);
  267. ok &= ns2501_writeb(dvo, 0x99, 0x00);
  268. ok &= ns2501_writeb(dvo, 0x9a, 0x88);
  269. ok &= ns2501_writeb(dvo, 0x9c, 0x23); /* Looks like first and last line of the image. */
  270. ok &= ns2501_writeb(dvo, 0x9d, 0x00);
  271. ok &= ns2501_writeb(dvo, 0x9e, 0x25);
  272. ok &= ns2501_writeb(dvo, 0x9f, 0x03);
  273. ok &= ns2501_writeb(dvo, 0xa4, 0x80);
  274. ok &= ns2501_writeb(dvo, 0xb6, 0x00);
  275. ok &= ns2501_writeb(dvo, 0xb9, 0xc8); /* horizontal? */
  276. ok &= ns2501_writeb(dvo, 0xba, 0x00); /* horizontal? */
  277. ok &= ns2501_writeb(dvo, 0xc0, 0x05); /* horizontal? */
  278. ok &= ns2501_writeb(dvo, 0xc1, 0xd7);
  279. ok &= ns2501_writeb(dvo, 0xc2, 0x00);
  280. ok &= ns2501_writeb(dvo, 0xc3, 0xf8);
  281. ok &= ns2501_writeb(dvo, 0xc4, 0x03);
  282. ok &= ns2501_writeb(dvo, 0xc5, 0x1a);
  283. ok &= ns2501_writeb(dvo, 0xc6, 0x00);
  284. ok &= ns2501_writeb(dvo, 0xc7, 0x73);
  285. ok &= ns2501_writeb(dvo, 0xc8, 0x02);
  286. } else if (mode->hdisplay == 640 && mode->vdisplay == 480) {
  287. /* mode 274 */
  288. DRM_DEBUG_KMS("switching to 640x480\n");
  289. /*
  290. * No, I do not know where this data comes from.
  291. * It is just what the video bios left in the DVO, so
  292. * I'm just copying it here over.
  293. * This also means that I cannot support any other modes
  294. * except the ones supported by the bios.
  295. */
  296. ns->reg_8_shadow &= ~NS2501_8_BPAS;
  297. ok &= ns2501_writeb(dvo, 0x11, 0xa0);
  298. ok &= ns2501_writeb(dvo, 0x1b, 0x11);
  299. ok &= ns2501_writeb(dvo, 0x1c, 0x54);
  300. ok &= ns2501_writeb(dvo, 0x1d, 0x03);
  301. ok &= ns2501_writeb(dvo, 0x34, 0x03);
  302. ok &= ns2501_writeb(dvo, 0x35, 0xff);
  303. ok &= ns2501_writeb(dvo, 0x80, 0xff);
  304. ok &= ns2501_writeb(dvo, 0x81, 0x07);
  305. ok &= ns2501_writeb(dvo, 0x82, 0x3d);
  306. ok &= ns2501_writeb(dvo, 0x83, 0x05);
  307. ok &= ns2501_writeb(dvo, 0x8d, 0x02);
  308. ok &= ns2501_writeb(dvo, 0x8e, 0x10);
  309. ok &= ns2501_writeb(dvo, 0x8f, 0x00);
  310. ok &= ns2501_writeb(dvo, 0x90, 0xff); /* vertical */
  311. ok &= ns2501_writeb(dvo, 0x91, 0x07);
  312. ok &= ns2501_writeb(dvo, 0x94, 0x00);
  313. ok &= ns2501_writeb(dvo, 0x95, 0x00);
  314. ok &= ns2501_writeb(dvo, 0x96, 0x05);
  315. ok &= ns2501_writeb(dvo, 0x99, 0x00);
  316. ok &= ns2501_writeb(dvo, 0x9a, 0x88);
  317. ok &= ns2501_writeb(dvo, 0x9c, 0x24);
  318. ok &= ns2501_writeb(dvo, 0x9d, 0x00);
  319. ok &= ns2501_writeb(dvo, 0x9e, 0x25);
  320. ok &= ns2501_writeb(dvo, 0x9f, 0x03);
  321. ok &= ns2501_writeb(dvo, 0xa4, 0x84);
  322. ok &= ns2501_writeb(dvo, 0xb6, 0x09);
  323. ok &= ns2501_writeb(dvo, 0xb9, 0xa0); /* horizontal? */
  324. ok &= ns2501_writeb(dvo, 0xba, 0x00); /* horizontal? */
  325. ok &= ns2501_writeb(dvo, 0xc0, 0x05); /* horizontal? */
  326. ok &= ns2501_writeb(dvo, 0xc1, 0x90);
  327. ok &= ns2501_writeb(dvo, 0xc2, 0x00);
  328. ok &= ns2501_writeb(dvo, 0xc3, 0x0f);
  329. ok &= ns2501_writeb(dvo, 0xc4, 0x03);
  330. ok &= ns2501_writeb(dvo, 0xc5, 0x16);
  331. ok &= ns2501_writeb(dvo, 0xc6, 0x00);
  332. ok &= ns2501_writeb(dvo, 0xc7, 0x02);
  333. ok &= ns2501_writeb(dvo, 0xc8, 0x02);
  334. } else if (mode->hdisplay == 1024 && mode->vdisplay == 768) {
  335. /* mode 280 */
  336. DRM_DEBUG_KMS("switching to 1024x768\n");
  337. /*
  338. * This might or might not work, actually. I'm silently
  339. * assuming here that the native panel resolution is
  340. * 1024x768. If not, then this leaves the scaler disabled
  341. * generating a picture that is likely not the expected.
  342. *
  343. * Problem is that I do not know where to take the panel
  344. * dimensions from.
  345. *
  346. * Enable the bypass, scaling not required.
  347. *
  348. * The scaler registers are irrelevant here....
  349. *
  350. */
  351. ns->reg_8_shadow |= NS2501_8_BPAS;
  352. ok &= ns2501_writeb(dvo, 0x37, 0x44);
  353. } else {
  354. /*
  355. * Data not known. Bummer!
  356. * Hopefully, the code should not go here
  357. * as mode_OK delivered no other modes.
  358. */
  359. ns->reg_8_shadow |= NS2501_8_BPAS;
  360. }
  361. ok &= ns2501_writeb(dvo, NS2501_REG8, ns->reg_8_shadow);
  362. } while (!ok && retries--);
  363. }
  364. /* set the NS2501 power state */
  365. static bool ns2501_get_hw_state(struct intel_dvo_device *dvo)
  366. {
  367. unsigned char ch;
  368. if (!ns2501_readb(dvo, NS2501_REG8, &ch))
  369. return false;
  370. if (ch & NS2501_8_PD)
  371. return true;
  372. else
  373. return false;
  374. }
  375. /* set the NS2501 power state */
  376. static void ns2501_dpms(struct intel_dvo_device *dvo, bool enable)
  377. {
  378. bool ok;
  379. int retries = 10;
  380. struct ns2501_priv *ns = (struct ns2501_priv *)(dvo->dev_priv);
  381. unsigned char ch;
  382. DRM_DEBUG_KMS("Trying set the dpms of the DVO to %i\n", enable);
  383. ch = ns->reg_8_shadow;
  384. if (enable)
  385. ch |= NS2501_8_PD;
  386. else
  387. ch &= ~NS2501_8_PD;
  388. if (ns->reg_8_set == 0 || ns->reg_8_shadow != ch) {
  389. ns->reg_8_set = 1;
  390. ns->reg_8_shadow = ch;
  391. do {
  392. ok = true;
  393. ok &= ns2501_writeb(dvo, NS2501_REG8, ch);
  394. ok &=
  395. ns2501_writeb(dvo, 0x34,
  396. enable ? 0x03 : 0x00);
  397. ok &=
  398. ns2501_writeb(dvo, 0x35,
  399. enable ? 0xff : 0x00);
  400. } while (!ok && retries--);
  401. }
  402. }
  403. static void ns2501_dump_regs(struct intel_dvo_device *dvo)
  404. {
  405. uint8_t val;
  406. ns2501_readb(dvo, NS2501_FREQ_LO, &val);
  407. DRM_DEBUG_KMS("NS2501_FREQ_LO: 0x%02x\n", val);
  408. ns2501_readb(dvo, NS2501_FREQ_HI, &val);
  409. DRM_DEBUG_KMS("NS2501_FREQ_HI: 0x%02x\n", val);
  410. ns2501_readb(dvo, NS2501_REG8, &val);
  411. DRM_DEBUG_KMS("NS2501_REG8: 0x%02x\n", val);
  412. ns2501_readb(dvo, NS2501_REG9, &val);
  413. DRM_DEBUG_KMS("NS2501_REG9: 0x%02x\n", val);
  414. ns2501_readb(dvo, NS2501_REGC, &val);
  415. DRM_DEBUG_KMS("NS2501_REGC: 0x%02x\n", val);
  416. }
  417. static void ns2501_destroy(struct intel_dvo_device *dvo)
  418. {
  419. struct ns2501_priv *ns = dvo->dev_priv;
  420. if (ns) {
  421. kfree(ns);
  422. dvo->dev_priv = NULL;
  423. }
  424. }
  425. struct intel_dvo_dev_ops ns2501_ops = {
  426. .init = ns2501_init,
  427. .detect = ns2501_detect,
  428. .mode_valid = ns2501_mode_valid,
  429. .mode_set = ns2501_mode_set,
  430. .dpms = ns2501_dpms,
  431. .get_hw_state = ns2501_get_hw_state,
  432. .dump_regs = ns2501_dump_regs,
  433. .destroy = ns2501_destroy,
  434. };