dvo_ns2501.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. /*
  57. * The following registers are not part of the official datasheet
  58. * and are the result of reverse engineering.
  59. */
  60. /*
  61. * Register c0 controls how the DVO synchronizes with
  62. * its input.
  63. */
  64. #define NS2501_REGC0 0xc0
  65. #define NS2501_C0_ENABLE (1<<0) /* enable the DVO sync in general */
  66. #define NS2501_C0_HSYNC (1<<1) /* synchronize horizontal with input */
  67. #define NS2501_C0_VSYNC (1<<2) /* synchronize vertical with input */
  68. #define NS2501_C0_RESET (1<<7) /* reset the synchronization flip/flops */
  69. /*
  70. * Register 41 is somehow related to the sync register and sync
  71. * configuration. It should be 0x32 whenever regC0 is 0x05 (hsync off)
  72. * and 0x00 otherwise.
  73. */
  74. #define NS2501_REG41 0x41
  75. /*
  76. * this register controls the dithering of the DVO
  77. * One bit enables it, the other define the dithering depth.
  78. * The higher the value, the lower the dithering depth.
  79. */
  80. #define NS2501_F9_REG 0xf9
  81. #define NS2501_F9_ENABLE (1<<0) /* if set, dithering is enabled */
  82. #define NS2501_F9_DITHER_MASK (0x7f<<1) /* controls the dither depth */
  83. #define NS2501_F9_DITHER_SHIFT 1 /* shifts the dither mask */
  84. /*
  85. * PLL configuration register. This is a pair of registers,
  86. * one single byte register at 1B, and a pair at 1C,1D.
  87. * These registers are counters/dividers.
  88. */
  89. #define NS2501_REG1B 0x1b /* one byte PLL control register */
  90. #define NS2501_REG1C 0x1c /* low-part of the second register */
  91. #define NS2501_REG1D 0x1d /* high-part of the second register */
  92. /*
  93. * Scaler control registers. Horizontal at b8,b9,
  94. * vertical at 10,11. The scale factor is computed as
  95. * 2^16/control-value. The low-byte comes first.
  96. */
  97. #define NS2501_REG10 0x10 /* low-byte vertical scaler */
  98. #define NS2501_REG11 0x11 /* high-byte vertical scaler */
  99. #define NS2501_REGB8 0xb8 /* low-byte horizontal scaler */
  100. #define NS2501_REGB9 0xb9 /* high-byte horizontal scaler */
  101. /*
  102. * Display window definition. This consists of four registers
  103. * per dimension. One register pair defines the start of the
  104. * display, one the end.
  105. * As far as I understand, this defines the window within which
  106. * the scaler samples the input.
  107. */
  108. #define NS2501_REGC1 0xc1 /* low-byte horizontal display start */
  109. #define NS2501_REGC2 0xc2 /* high-byte horizontal display start */
  110. #define NS2501_REGC3 0xc3 /* low-byte horizontal display stop */
  111. #define NS2501_REGC4 0xc4 /* high-byte horizontal display stop */
  112. #define NS2501_REGC5 0xc5 /* low-byte vertical display start */
  113. #define NS2501_REGC6 0xc6 /* high-byte vertical display start */
  114. #define NS2501_REGC7 0xc7 /* low-byte vertical display stop */
  115. #define NS2501_REGC8 0xc8 /* high-byte vertical display stop */
  116. /*
  117. * The following register pair seems to define the start of
  118. * the vertical sync. If automatic syncing is enabled, and the
  119. * register value defines a sync pulse that is later than the
  120. * incoming sync, then the register value is ignored and the
  121. * external hsync triggers the synchronization.
  122. */
  123. #define NS2501_REG80 0x80 /* low-byte vsync-start */
  124. #define NS2501_REG81 0x81 /* high-byte vsync-start */
  125. /*
  126. * The following register pair seems to define the total number
  127. * of lines created at the output side of the scaler.
  128. * This is again a low-high register pair.
  129. */
  130. #define NS2501_REG82 0x82 /* output display height, low byte */
  131. #define NS2501_REG83 0x83 /* output display height, high byte */
  132. /*
  133. * The following registers define the end of the front-porch
  134. * in horizontal and vertical position and hence allow to shift
  135. * the image left/right or up/down.
  136. */
  137. #define NS2501_REG98 0x98 /* horizontal start of display + 256, low */
  138. #define NS2501_REG99 0x99 /* horizontal start of display + 256, high */
  139. #define NS2501_REG8E 0x8e /* vertical start of the display, low byte */
  140. #define NS2501_REG8F 0x8f /* vertical start of the display, high byte */
  141. /*
  142. * The following register pair control the function of the
  143. * backlight and the DVO output. To enable the corresponding
  144. * function, the corresponding bit must be set in both registers.
  145. */
  146. #define NS2501_REG34 0x34 /* DVO enable functions, first register */
  147. #define NS2501_REG35 0x35 /* DVO enable functions, second register */
  148. #define NS2501_34_ENABLE_OUTPUT (1<<0) /* enable DVO output */
  149. #define NS2501_34_ENABLE_BACKLIGHT (1<<1) /* enable backlight */
  150. /*
  151. * Registers 9C and 9D define the vertical output offset
  152. * of the visible region.
  153. */
  154. #define NS2501_REG9C 0x9c
  155. #define NS2501_REG9D 0x9d
  156. /*
  157. * The register 9F defines the dithering. This requires the
  158. * scaler to be ON. Bit 0 enables dithering, the remaining
  159. * bits control the depth of the dither. The higher the value,
  160. * the LOWER the dithering amplitude. A good value seems to be
  161. * 15 (total register value).
  162. */
  163. #define NS2501_REGF9 0xf9
  164. #define NS2501_F9_ENABLE_DITHER (1<<0) /* enable dithering */
  165. #define NS2501_F9_DITHER_MASK (0x7f<<1) /* dither masking */
  166. #define NS2501_F9_DITHER_SHIFT 1 /* upshift of the dither mask */
  167. enum {
  168. MODE_640x480,
  169. MODE_800x600,
  170. MODE_1024x768,
  171. };
  172. struct ns2501_reg {
  173. uint8_t offset;
  174. uint8_t value;
  175. };
  176. /*
  177. * The following structure keeps the complete configuration of
  178. * the DVO, given a specific output configuration.
  179. * This is pretty much guess-work from reverse-engineering, so
  180. * read all this with a grain of salt.
  181. */
  182. struct ns2501_configuration {
  183. uint8_t sync; /* configuration of the C0 register */
  184. uint8_t conf; /* configuration register 8 */
  185. uint8_t syncb; /* configuration register 41 */
  186. uint8_t dither; /* configuration of the dithering */
  187. uint8_t pll_a; /* PLL configuration, register A, 1B */
  188. uint16_t pll_b; /* PLL configuration, register B, 1C/1D */
  189. uint16_t hstart; /* horizontal start, registers C1/C2 */
  190. uint16_t hstop; /* horizontal total, registers C3/C4 */
  191. uint16_t vstart; /* vertical start, registers C5/C6 */
  192. uint16_t vstop; /* vertical total, registers C7/C8 */
  193. uint16_t vsync; /* manual vertical sync start, 80/81 */
  194. uint16_t vtotal; /* number of lines generated, 82/83 */
  195. uint16_t hpos; /* horizontal position + 256, 98/99 */
  196. uint16_t vpos; /* vertical position, 8e/8f */
  197. uint16_t voffs; /* vertical output offset, 9c/9d */
  198. uint16_t hscale; /* horizontal scaling factor, b8/b9 */
  199. uint16_t vscale; /* vertical scaling factor, 10/11 */
  200. };
  201. /*
  202. * DVO configuration values, partially based on what the BIOS
  203. * of the Fujitsu Lifebook S6010 writes into registers,
  204. * partially found by manual tweaking. These configurations assume
  205. * a 1024x768 panel.
  206. */
  207. static const struct ns2501_configuration ns2501_modes[] = {
  208. [MODE_640x480] = {
  209. .sync = NS2501_C0_ENABLE | NS2501_C0_VSYNC,
  210. .conf = NS2501_8_VEN | NS2501_8_HEN | NS2501_8_PD,
  211. .syncb = 0x32,
  212. .dither = 0x0f,
  213. .pll_a = 17,
  214. .pll_b = 852,
  215. .hstart = 144,
  216. .hstop = 783,
  217. .vstart = 22,
  218. .vstop = 514,
  219. .vsync = 2047, /* actually, ignored with this config */
  220. .vtotal = 1341,
  221. .hpos = 0,
  222. .vpos = 16,
  223. .voffs = 36,
  224. .hscale = 40960,
  225. .vscale = 40960
  226. },
  227. [MODE_800x600] = {
  228. .sync = NS2501_C0_ENABLE |
  229. NS2501_C0_HSYNC | NS2501_C0_VSYNC,
  230. .conf = NS2501_8_VEN | NS2501_8_HEN | NS2501_8_PD,
  231. .syncb = 0x00,
  232. .dither = 0x0f,
  233. .pll_a = 25,
  234. .pll_b = 612,
  235. .hstart = 215,
  236. .hstop = 1016,
  237. .vstart = 26,
  238. .vstop = 627,
  239. .vsync = 807,
  240. .vtotal = 1341,
  241. .hpos = 0,
  242. .vpos = 4,
  243. .voffs = 35,
  244. .hscale = 51248,
  245. .vscale = 51232
  246. },
  247. [MODE_1024x768] = {
  248. .sync = NS2501_C0_ENABLE | NS2501_C0_VSYNC,
  249. .conf = NS2501_8_VEN | NS2501_8_HEN | NS2501_8_PD,
  250. .syncb = 0x32,
  251. .dither = 0x0f,
  252. .pll_a = 11,
  253. .pll_b = 1350,
  254. .hstart = 276,
  255. .hstop = 1299,
  256. .vstart = 15,
  257. .vstop = 1056,
  258. .vsync = 2047,
  259. .vtotal = 1341,
  260. .hpos = 0,
  261. .vpos = 7,
  262. .voffs = 27,
  263. .hscale = 65535,
  264. .vscale = 65535
  265. }
  266. };
  267. /*
  268. * Other configuration values left by the BIOS of the
  269. * Fujitsu S6010 in the DVO control registers. Their
  270. * value does not depend on the BIOS and their meaning
  271. * is unknown.
  272. */
  273. static const struct ns2501_reg mode_agnostic_values[] = {
  274. /* 08 is mode specific */
  275. [0] = { .offset = 0x0a, .value = 0x81, },
  276. /* 10,11 are part of the mode specific configuration */
  277. [1] = { .offset = 0x12, .value = 0x02, },
  278. [2] = { .offset = 0x18, .value = 0x07, },
  279. [3] = { .offset = 0x19, .value = 0x00, },
  280. [4] = { .offset = 0x1a, .value = 0x00, }, /* PLL?, ignored */
  281. /* 1b,1c,1d are part of the mode specific configuration */
  282. [5] = { .offset = 0x1e, .value = 0x02, },
  283. [6] = { .offset = 0x1f, .value = 0x40, },
  284. [7] = { .offset = 0x20, .value = 0x00, },
  285. [8] = { .offset = 0x21, .value = 0x00, },
  286. [9] = { .offset = 0x22, .value = 0x00, },
  287. [10] = { .offset = 0x23, .value = 0x00, },
  288. [11] = { .offset = 0x24, .value = 0x00, },
  289. [12] = { .offset = 0x25, .value = 0x00, },
  290. [13] = { .offset = 0x26, .value = 0x00, },
  291. [14] = { .offset = 0x27, .value = 0x00, },
  292. [15] = { .offset = 0x7e, .value = 0x18, },
  293. /* 80-84 are part of the mode-specific configuration */
  294. [16] = { .offset = 0x84, .value = 0x00, },
  295. [17] = { .offset = 0x85, .value = 0x00, },
  296. [18] = { .offset = 0x86, .value = 0x00, },
  297. [19] = { .offset = 0x87, .value = 0x00, },
  298. [20] = { .offset = 0x88, .value = 0x00, },
  299. [21] = { .offset = 0x89, .value = 0x00, },
  300. [22] = { .offset = 0x8a, .value = 0x00, },
  301. [23] = { .offset = 0x8b, .value = 0x00, },
  302. [24] = { .offset = 0x8c, .value = 0x10, },
  303. [25] = { .offset = 0x8d, .value = 0x02, },
  304. /* 8e,8f are part of the mode-specific configuration */
  305. [26] = { .offset = 0x90, .value = 0xff, },
  306. [27] = { .offset = 0x91, .value = 0x07, },
  307. [28] = { .offset = 0x92, .value = 0xa0, },
  308. [29] = { .offset = 0x93, .value = 0x02, },
  309. [30] = { .offset = 0x94, .value = 0x00, },
  310. [31] = { .offset = 0x95, .value = 0x00, },
  311. [32] = { .offset = 0x96, .value = 0x05, },
  312. [33] = { .offset = 0x97, .value = 0x00, },
  313. /* 98,99 are part of the mode-specific configuration */
  314. [34] = { .offset = 0x9a, .value = 0x88, },
  315. [35] = { .offset = 0x9b, .value = 0x00, },
  316. /* 9c,9d are part of the mode-specific configuration */
  317. [36] = { .offset = 0x9e, .value = 0x25, },
  318. [37] = { .offset = 0x9f, .value = 0x03, },
  319. [38] = { .offset = 0xa0, .value = 0x28, },
  320. [39] = { .offset = 0xa1, .value = 0x01, },
  321. [40] = { .offset = 0xa2, .value = 0x28, },
  322. [41] = { .offset = 0xa3, .value = 0x05, },
  323. /* register 0xa4 is mode specific, but 0x80..0x84 works always */
  324. [42] = { .offset = 0xa4, .value = 0x84, },
  325. [43] = { .offset = 0xa5, .value = 0x00, },
  326. [44] = { .offset = 0xa6, .value = 0x00, },
  327. [45] = { .offset = 0xa7, .value = 0x00, },
  328. [46] = { .offset = 0xa8, .value = 0x00, },
  329. /* 0xa9 to 0xab are mode specific, but have no visible effect */
  330. [47] = { .offset = 0xa9, .value = 0x04, },
  331. [48] = { .offset = 0xaa, .value = 0x70, },
  332. [49] = { .offset = 0xab, .value = 0x4f, },
  333. [50] = { .offset = 0xac, .value = 0x00, },
  334. [51] = { .offset = 0xad, .value = 0x00, },
  335. [52] = { .offset = 0xb6, .value = 0x09, },
  336. [53] = { .offset = 0xb7, .value = 0x03, },
  337. /* b8,b9 are part of the mode-specific configuration */
  338. [54] = { .offset = 0xba, .value = 0x00, },
  339. [55] = { .offset = 0xbb, .value = 0x20, },
  340. [56] = { .offset = 0xf3, .value = 0x90, },
  341. [57] = { .offset = 0xf4, .value = 0x00, },
  342. [58] = { .offset = 0xf7, .value = 0x88, },
  343. /* f8 is mode specific, but the value does not matter */
  344. [59] = { .offset = 0xf8, .value = 0x0a, },
  345. [60] = { .offset = 0xf9, .value = 0x00, }
  346. };
  347. static const struct ns2501_reg regs_init[] = {
  348. [0] = { .offset = 0x35, .value = 0xff, },
  349. [1] = { .offset = 0x34, .value = 0x00, },
  350. [2] = { .offset = 0x08, .value = 0x30, },
  351. };
  352. struct ns2501_priv {
  353. bool quiet;
  354. const struct ns2501_configuration *conf;
  355. };
  356. #define NSPTR(d) ((NS2501Ptr)(d->DriverPrivate.ptr))
  357. /*
  358. ** Read a register from the ns2501.
  359. ** Returns true if successful, false otherwise.
  360. ** If it returns false, it might be wise to enable the
  361. ** DVO with the above function.
  362. */
  363. static bool ns2501_readb(struct intel_dvo_device *dvo, int addr, uint8_t * ch)
  364. {
  365. struct ns2501_priv *ns = dvo->dev_priv;
  366. struct i2c_adapter *adapter = dvo->i2c_bus;
  367. u8 out_buf[2];
  368. u8 in_buf[2];
  369. struct i2c_msg msgs[] = {
  370. {
  371. .addr = dvo->slave_addr,
  372. .flags = 0,
  373. .len = 1,
  374. .buf = out_buf,
  375. },
  376. {
  377. .addr = dvo->slave_addr,
  378. .flags = I2C_M_RD,
  379. .len = 1,
  380. .buf = in_buf,
  381. }
  382. };
  383. out_buf[0] = addr;
  384. out_buf[1] = 0;
  385. if (i2c_transfer(adapter, msgs, 2) == 2) {
  386. *ch = in_buf[0];
  387. return true;
  388. }
  389. if (!ns->quiet) {
  390. DRM_DEBUG_KMS
  391. ("Unable to read register 0x%02x from %s:0x%02x.\n", addr,
  392. adapter->name, dvo->slave_addr);
  393. }
  394. return false;
  395. }
  396. /*
  397. ** Write a register to the ns2501.
  398. ** Returns true if successful, false otherwise.
  399. ** If it returns false, it might be wise to enable the
  400. ** DVO with the above function.
  401. */
  402. static bool ns2501_writeb(struct intel_dvo_device *dvo, int addr, uint8_t ch)
  403. {
  404. struct ns2501_priv *ns = dvo->dev_priv;
  405. struct i2c_adapter *adapter = dvo->i2c_bus;
  406. uint8_t out_buf[2];
  407. struct i2c_msg msg = {
  408. .addr = dvo->slave_addr,
  409. .flags = 0,
  410. .len = 2,
  411. .buf = out_buf,
  412. };
  413. out_buf[0] = addr;
  414. out_buf[1] = ch;
  415. if (i2c_transfer(adapter, &msg, 1) == 1) {
  416. return true;
  417. }
  418. if (!ns->quiet) {
  419. DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d\n",
  420. addr, adapter->name, dvo->slave_addr);
  421. }
  422. return false;
  423. }
  424. /* National Semiconductor 2501 driver for chip on i2c bus
  425. * scan for the chip on the bus.
  426. * Hope the VBIOS initialized the PLL correctly so we can
  427. * talk to it. If not, it will not be seen and not detected.
  428. * Bummer!
  429. */
  430. static bool ns2501_init(struct intel_dvo_device *dvo,
  431. struct i2c_adapter *adapter)
  432. {
  433. /* this will detect the NS2501 chip on the specified i2c bus */
  434. struct ns2501_priv *ns;
  435. unsigned char ch;
  436. ns = kzalloc(sizeof(struct ns2501_priv), GFP_KERNEL);
  437. if (ns == NULL)
  438. return false;
  439. dvo->i2c_bus = adapter;
  440. dvo->dev_priv = ns;
  441. ns->quiet = true;
  442. if (!ns2501_readb(dvo, NS2501_VID_LO, &ch))
  443. goto out;
  444. if (ch != (NS2501_VID & 0xff)) {
  445. DRM_DEBUG_KMS("ns2501 not detected got %d: from %s Slave %d.\n",
  446. ch, adapter->name, dvo->slave_addr);
  447. goto out;
  448. }
  449. if (!ns2501_readb(dvo, NS2501_DID_LO, &ch))
  450. goto out;
  451. if (ch != (NS2501_DID & 0xff)) {
  452. DRM_DEBUG_KMS("ns2501 not detected got %d: from %s Slave %d.\n",
  453. ch, adapter->name, dvo->slave_addr);
  454. goto out;
  455. }
  456. ns->quiet = false;
  457. DRM_DEBUG_KMS("init ns2501 dvo controller successfully!\n");
  458. return true;
  459. out:
  460. kfree(ns);
  461. return false;
  462. }
  463. static enum drm_connector_status ns2501_detect(struct intel_dvo_device *dvo)
  464. {
  465. /*
  466. * This is a Laptop display, it doesn't have hotplugging.
  467. * Even if not, the detection bit of the 2501 is unreliable as
  468. * it only works for some display types.
  469. * It is even more unreliable as the PLL must be active for
  470. * allowing reading from the chiop.
  471. */
  472. return connector_status_connected;
  473. }
  474. static enum drm_mode_status ns2501_mode_valid(struct intel_dvo_device *dvo,
  475. struct drm_display_mode *mode)
  476. {
  477. DRM_DEBUG_KMS
  478. ("is mode valid (hdisplay=%d,htotal=%d,vdisplay=%d,vtotal=%d)\n",
  479. mode->hdisplay, mode->htotal, mode->vdisplay, mode->vtotal);
  480. /*
  481. * Currently, these are all the modes I have data from.
  482. * More might exist. Unclear how to find the native resolution
  483. * of the panel in here so we could always accept it
  484. * by disabling the scaler.
  485. */
  486. if ((mode->hdisplay == 640 && mode->vdisplay == 480 && mode->clock == 25175) ||
  487. (mode->hdisplay == 800 && mode->vdisplay == 600 && mode->clock == 40000) ||
  488. (mode->hdisplay == 1024 && mode->vdisplay == 768 && mode->clock == 65000)) {
  489. return MODE_OK;
  490. } else {
  491. return MODE_ONE_SIZE; /* Is this a reasonable error? */
  492. }
  493. }
  494. static void ns2501_mode_set(struct intel_dvo_device *dvo,
  495. const struct drm_display_mode *mode,
  496. const struct drm_display_mode *adjusted_mode)
  497. {
  498. const struct ns2501_configuration *conf;
  499. struct ns2501_priv *ns = (struct ns2501_priv *)(dvo->dev_priv);
  500. int mode_idx, i;
  501. DRM_DEBUG_KMS
  502. ("set mode (hdisplay=%d,htotal=%d,vdisplay=%d,vtotal=%d).\n",
  503. mode->hdisplay, mode->htotal, mode->vdisplay, mode->vtotal);
  504. DRM_DEBUG_KMS("Detailed requested mode settings are:\n"
  505. "clock : %d kHz\n"
  506. "hdisplay : %d\n"
  507. "hblank start : %d\n"
  508. "hblank end : %d\n"
  509. "hsync start : %d\n"
  510. "hsync end : %d\n"
  511. "htotal : %d\n"
  512. "hskew : %d\n"
  513. "vdisplay : %d\n"
  514. "vblank start : %d\n"
  515. "hblank end : %d\n"
  516. "vsync start : %d\n"
  517. "vsync end : %d\n"
  518. "vtotal : %d\n",
  519. adjusted_mode->crtc_clock,
  520. adjusted_mode->crtc_hdisplay,
  521. adjusted_mode->crtc_hblank_start,
  522. adjusted_mode->crtc_hblank_end,
  523. adjusted_mode->crtc_hsync_start,
  524. adjusted_mode->crtc_hsync_end,
  525. adjusted_mode->crtc_htotal,
  526. adjusted_mode->crtc_hskew,
  527. adjusted_mode->crtc_vdisplay,
  528. adjusted_mode->crtc_vblank_start,
  529. adjusted_mode->crtc_vblank_end,
  530. adjusted_mode->crtc_vsync_start,
  531. adjusted_mode->crtc_vsync_end,
  532. adjusted_mode->crtc_vtotal);
  533. if (mode->hdisplay == 640 && mode->vdisplay == 480)
  534. mode_idx = MODE_640x480;
  535. else if (mode->hdisplay == 800 && mode->vdisplay == 600)
  536. mode_idx = MODE_800x600;
  537. else if (mode->hdisplay == 1024 && mode->vdisplay == 768)
  538. mode_idx = MODE_1024x768;
  539. else
  540. return;
  541. /* Hopefully doing it every time won't hurt... */
  542. for (i = 0; i < ARRAY_SIZE(regs_init); i++)
  543. ns2501_writeb(dvo, regs_init[i].offset, regs_init[i].value);
  544. /* Write the mode-agnostic values */
  545. for (i = 0; i < ARRAY_SIZE(mode_agnostic_values); i++)
  546. ns2501_writeb(dvo, mode_agnostic_values[i].offset,
  547. mode_agnostic_values[i].value);
  548. /* Write now the mode-specific configuration */
  549. conf = ns2501_modes + mode_idx;
  550. ns->conf = conf;
  551. ns2501_writeb(dvo, NS2501_REG8, conf->conf);
  552. ns2501_writeb(dvo, NS2501_REG1B, conf->pll_a);
  553. ns2501_writeb(dvo, NS2501_REG1C, conf->pll_b & 0xff);
  554. ns2501_writeb(dvo, NS2501_REG1D, conf->pll_b >> 8);
  555. ns2501_writeb(dvo, NS2501_REGC1, conf->hstart & 0xff);
  556. ns2501_writeb(dvo, NS2501_REGC2, conf->hstart >> 8);
  557. ns2501_writeb(dvo, NS2501_REGC3, conf->hstop & 0xff);
  558. ns2501_writeb(dvo, NS2501_REGC4, conf->hstop >> 8);
  559. ns2501_writeb(dvo, NS2501_REGC5, conf->vstart & 0xff);
  560. ns2501_writeb(dvo, NS2501_REGC6, conf->vstart >> 8);
  561. ns2501_writeb(dvo, NS2501_REGC7, conf->vstop & 0xff);
  562. ns2501_writeb(dvo, NS2501_REGC8, conf->vstop >> 8);
  563. ns2501_writeb(dvo, NS2501_REG80, conf->vsync & 0xff);
  564. ns2501_writeb(dvo, NS2501_REG81, conf->vsync >> 8);
  565. ns2501_writeb(dvo, NS2501_REG82, conf->vtotal & 0xff);
  566. ns2501_writeb(dvo, NS2501_REG83, conf->vtotal >> 8);
  567. ns2501_writeb(dvo, NS2501_REG98, conf->hpos & 0xff);
  568. ns2501_writeb(dvo, NS2501_REG99, conf->hpos >> 8);
  569. ns2501_writeb(dvo, NS2501_REG8E, conf->vpos & 0xff);
  570. ns2501_writeb(dvo, NS2501_REG8F, conf->vpos >> 8);
  571. ns2501_writeb(dvo, NS2501_REG9C, conf->voffs & 0xff);
  572. ns2501_writeb(dvo, NS2501_REG9D, conf->voffs >> 8);
  573. ns2501_writeb(dvo, NS2501_REGB8, conf->hscale & 0xff);
  574. ns2501_writeb(dvo, NS2501_REGB9, conf->hscale >> 8);
  575. ns2501_writeb(dvo, NS2501_REG10, conf->vscale & 0xff);
  576. ns2501_writeb(dvo, NS2501_REG11, conf->vscale >> 8);
  577. ns2501_writeb(dvo, NS2501_REGF9, conf->dither);
  578. ns2501_writeb(dvo, NS2501_REG41, conf->syncb);
  579. ns2501_writeb(dvo, NS2501_REGC0, conf->sync);
  580. }
  581. /* set the NS2501 power state */
  582. static bool ns2501_get_hw_state(struct intel_dvo_device *dvo)
  583. {
  584. unsigned char ch;
  585. if (!ns2501_readb(dvo, NS2501_REG8, &ch))
  586. return false;
  587. return ch & NS2501_8_PD;
  588. }
  589. /* set the NS2501 power state */
  590. static void ns2501_dpms(struct intel_dvo_device *dvo, bool enable)
  591. {
  592. struct ns2501_priv *ns = (struct ns2501_priv *)(dvo->dev_priv);
  593. DRM_DEBUG_KMS("Trying set the dpms of the DVO to %i\n", enable);
  594. if (enable) {
  595. ns2501_writeb(dvo, NS2501_REGC0, ns->conf->sync | 0x08);
  596. ns2501_writeb(dvo, NS2501_REG41, ns->conf->syncb);
  597. ns2501_writeb(dvo, NS2501_REG34, NS2501_34_ENABLE_OUTPUT);
  598. msleep(15);
  599. ns2501_writeb(dvo, NS2501_REG8,
  600. ns->conf->conf | NS2501_8_BPAS);
  601. if (!(ns->conf->conf & NS2501_8_BPAS))
  602. ns2501_writeb(dvo, NS2501_REG8, ns->conf->conf);
  603. msleep(200);
  604. ns2501_writeb(dvo, NS2501_REG34,
  605. NS2501_34_ENABLE_OUTPUT | NS2501_34_ENABLE_BACKLIGHT);
  606. ns2501_writeb(dvo, NS2501_REGC0, ns->conf->sync);
  607. } else {
  608. ns2501_writeb(dvo, NS2501_REG34, NS2501_34_ENABLE_OUTPUT);
  609. msleep(200);
  610. ns2501_writeb(dvo, NS2501_REG8, NS2501_8_VEN | NS2501_8_HEN |
  611. NS2501_8_BPAS);
  612. msleep(15);
  613. ns2501_writeb(dvo, NS2501_REG34, 0x00);
  614. }
  615. }
  616. static void ns2501_destroy(struct intel_dvo_device *dvo)
  617. {
  618. struct ns2501_priv *ns = dvo->dev_priv;
  619. if (ns) {
  620. kfree(ns);
  621. dvo->dev_priv = NULL;
  622. }
  623. }
  624. struct intel_dvo_dev_ops ns2501_ops = {
  625. .init = ns2501_init,
  626. .detect = ns2501_detect,
  627. .mode_valid = ns2501_mode_valid,
  628. .mode_set = ns2501_mode_set,
  629. .dpms = ns2501_dpms,
  630. .get_hw_state = ns2501_get_hw_state,
  631. .destroy = ns2501_destroy,
  632. };