1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "dvo.h"
30#include "i915_reg.h"
31#include "i915_drv.h"
32
33#define NS2501_VID 0x1305
34#define NS2501_DID 0x6726
35
36#define NS2501_VID_LO 0x00
37#define NS2501_VID_HI 0x01
38#define NS2501_DID_LO 0x02
39#define NS2501_DID_HI 0x03
40#define NS2501_REV 0x04
41#define NS2501_RSVD 0x05
42#define NS2501_FREQ_LO 0x06
43#define NS2501_FREQ_HI 0x07
44
45#define NS2501_REG8 0x08
46#define NS2501_8_VEN (1<<5)
47#define NS2501_8_HEN (1<<4)
48#define NS2501_8_DSEL (1<<3)
49#define NS2501_8_BPAS (1<<2)
50#define NS2501_8_RSVD (1<<1)
51#define NS2501_8_PD (1<<0)
52
53#define NS2501_REG9 0x09
54#define NS2501_9_VLOW (1<<7)
55#define NS2501_9_MSEL_MASK (0x7<<4)
56#define NS2501_9_TSEL (1<<3)
57#define NS2501_9_RSEN (1<<2)
58#define NS2501_9_RSVD (1<<1)
59#define NS2501_9_MDI (1<<0)
60
61#define NS2501_REGC 0x0c
62
63
64
65
66
67
68
69
70
71
72#define NS2501_REGC0 0xc0
73#define NS2501_C0_ENABLE (1<<0)
74#define NS2501_C0_HSYNC (1<<1)
75#define NS2501_C0_VSYNC (1<<2)
76#define NS2501_C0_RESET (1<<7)
77
78
79
80
81
82
83#define NS2501_REG41 0x41
84
85
86
87
88
89
90#define NS2501_F9_REG 0xf9
91#define NS2501_F9_ENABLE (1<<0)
92#define NS2501_F9_DITHER_MASK (0x7f<<1)
93#define NS2501_F9_DITHER_SHIFT 1
94
95
96
97
98
99
100#define NS2501_REG1B 0x1b
101#define NS2501_REG1C 0x1c
102#define NS2501_REG1D 0x1d
103
104
105
106
107
108
109#define NS2501_REG10 0x10
110#define NS2501_REG11 0x11
111#define NS2501_REGB8 0xb8
112#define NS2501_REGB9 0xb9
113
114
115
116
117
118
119
120
121#define NS2501_REGC1 0xc1
122#define NS2501_REGC2 0xc2
123#define NS2501_REGC3 0xc3
124#define NS2501_REGC4 0xc4
125#define NS2501_REGC5 0xc5
126#define NS2501_REGC6 0xc6
127#define NS2501_REGC7 0xc7
128#define NS2501_REGC8 0xc8
129
130
131
132
133
134
135
136
137#define NS2501_REG80 0x80
138#define NS2501_REG81 0x81
139
140
141
142
143
144
145#define NS2501_REG82 0x82
146#define NS2501_REG83 0x83
147
148
149
150
151
152
153#define NS2501_REG98 0x98
154#define NS2501_REG99 0x99
155#define NS2501_REG8E 0x8e
156#define NS2501_REG8F 0x8f
157
158
159
160
161
162
163#define NS2501_REG34 0x34
164#define NS2501_REG35 0x35
165#define NS2501_34_ENABLE_OUTPUT (1<<0)
166#define NS2501_34_ENABLE_BACKLIGHT (1<<1)
167
168
169
170
171
172#define NS2501_REG9C 0x9c
173#define NS2501_REG9D 0x9d
174
175
176
177
178
179
180
181
182#define NS2501_REGF9 0xf9
183#define NS2501_F9_ENABLE_DITHER (1<<0)
184#define NS2501_F9_DITHER_MASK (0x7f<<1)
185#define NS2501_F9_DITHER_SHIFT 1
186
187enum {
188 MODE_640x480,
189 MODE_800x600,
190 MODE_1024x768,
191};
192
193struct ns2501_reg {
194 uint8_t offset;
195 uint8_t value;
196};
197
198
199
200
201
202
203
204struct ns2501_configuration {
205 uint8_t sync;
206 uint8_t conf;
207 uint8_t syncb;
208 uint8_t dither;
209 uint8_t pll_a;
210 uint16_t pll_b;
211 uint16_t hstart;
212 uint16_t hstop;
213 uint16_t vstart;
214 uint16_t vstop;
215 uint16_t vsync;
216 uint16_t vtotal;
217 uint16_t hpos;
218 uint16_t vpos;
219 uint16_t voffs;
220 uint16_t hscale;
221 uint16_t vscale;
222};
223
224
225
226
227
228
229
230static const struct ns2501_configuration ns2501_modes[] = {
231 [MODE_640x480] = {
232 .sync = NS2501_C0_ENABLE | NS2501_C0_VSYNC,
233 .conf = NS2501_8_VEN | NS2501_8_HEN | NS2501_8_PD,
234 .syncb = 0x32,
235 .dither = 0x0f,
236 .pll_a = 17,
237 .pll_b = 852,
238 .hstart = 144,
239 .hstop = 783,
240 .vstart = 22,
241 .vstop = 514,
242 .vsync = 2047,
243 .vtotal = 1341,
244 .hpos = 0,
245 .vpos = 16,
246 .voffs = 36,
247 .hscale = 40960,
248 .vscale = 40960
249 },
250 [MODE_800x600] = {
251 .sync = NS2501_C0_ENABLE |
252 NS2501_C0_HSYNC | NS2501_C0_VSYNC,
253 .conf = NS2501_8_VEN | NS2501_8_HEN | NS2501_8_PD,
254 .syncb = 0x00,
255 .dither = 0x0f,
256 .pll_a = 25,
257 .pll_b = 612,
258 .hstart = 215,
259 .hstop = 1016,
260 .vstart = 26,
261 .vstop = 627,
262 .vsync = 807,
263 .vtotal = 1341,
264 .hpos = 0,
265 .vpos = 4,
266 .voffs = 35,
267 .hscale = 51248,
268 .vscale = 51232
269 },
270 [MODE_1024x768] = {
271 .sync = NS2501_C0_ENABLE | NS2501_C0_VSYNC,
272 .conf = NS2501_8_VEN | NS2501_8_HEN | NS2501_8_PD,
273 .syncb = 0x32,
274 .dither = 0x0f,
275 .pll_a = 11,
276 .pll_b = 1350,
277 .hstart = 276,
278 .hstop = 1299,
279 .vstart = 15,
280 .vstop = 1056,
281 .vsync = 2047,
282 .vtotal = 1341,
283 .hpos = 0,
284 .vpos = 7,
285 .voffs = 27,
286 .hscale = 65535,
287 .vscale = 65535
288 }
289};
290
291
292
293
294
295
296
297
298static const struct ns2501_reg mode_agnostic_values[] = {
299
300 [0] = { .offset = 0x0a, .value = 0x81, },
301
302 [1] = { .offset = 0x12, .value = 0x02, },
303 [2] = { .offset = 0x18, .value = 0x07, },
304 [3] = { .offset = 0x19, .value = 0x00, },
305 [4] = { .offset = 0x1a, .value = 0x00, },
306
307 [5] = { .offset = 0x1e, .value = 0x02, },
308 [6] = { .offset = 0x1f, .value = 0x40, },
309 [7] = { .offset = 0x20, .value = 0x00, },
310 [8] = { .offset = 0x21, .value = 0x00, },
311 [9] = { .offset = 0x22, .value = 0x00, },
312 [10] = { .offset = 0x23, .value = 0x00, },
313 [11] = { .offset = 0x24, .value = 0x00, },
314 [12] = { .offset = 0x25, .value = 0x00, },
315 [13] = { .offset = 0x26, .value = 0x00, },
316 [14] = { .offset = 0x27, .value = 0x00, },
317 [15] = { .offset = 0x7e, .value = 0x18, },
318
319 [16] = { .offset = 0x84, .value = 0x00, },
320 [17] = { .offset = 0x85, .value = 0x00, },
321 [18] = { .offset = 0x86, .value = 0x00, },
322 [19] = { .offset = 0x87, .value = 0x00, },
323 [20] = { .offset = 0x88, .value = 0x00, },
324 [21] = { .offset = 0x89, .value = 0x00, },
325 [22] = { .offset = 0x8a, .value = 0x00, },
326 [23] = { .offset = 0x8b, .value = 0x00, },
327 [24] = { .offset = 0x8c, .value = 0x10, },
328 [25] = { .offset = 0x8d, .value = 0x02, },
329
330 [26] = { .offset = 0x90, .value = 0xff, },
331 [27] = { .offset = 0x91, .value = 0x07, },
332 [28] = { .offset = 0x92, .value = 0xa0, },
333 [29] = { .offset = 0x93, .value = 0x02, },
334 [30] = { .offset = 0x94, .value = 0x00, },
335 [31] = { .offset = 0x95, .value = 0x00, },
336 [32] = { .offset = 0x96, .value = 0x05, },
337 [33] = { .offset = 0x97, .value = 0x00, },
338
339 [34] = { .offset = 0x9a, .value = 0x88, },
340 [35] = { .offset = 0x9b, .value = 0x00, },
341
342 [36] = { .offset = 0x9e, .value = 0x25, },
343 [37] = { .offset = 0x9f, .value = 0x03, },
344 [38] = { .offset = 0xa0, .value = 0x28, },
345 [39] = { .offset = 0xa1, .value = 0x01, },
346 [40] = { .offset = 0xa2, .value = 0x28, },
347 [41] = { .offset = 0xa3, .value = 0x05, },
348
349 [42] = { .offset = 0xa4, .value = 0x84, },
350 [43] = { .offset = 0xa5, .value = 0x00, },
351 [44] = { .offset = 0xa6, .value = 0x00, },
352 [45] = { .offset = 0xa7, .value = 0x00, },
353 [46] = { .offset = 0xa8, .value = 0x00, },
354
355 [47] = { .offset = 0xa9, .value = 0x04, },
356 [48] = { .offset = 0xaa, .value = 0x70, },
357 [49] = { .offset = 0xab, .value = 0x4f, },
358 [50] = { .offset = 0xac, .value = 0x00, },
359 [51] = { .offset = 0xad, .value = 0x00, },
360 [52] = { .offset = 0xb6, .value = 0x09, },
361 [53] = { .offset = 0xb7, .value = 0x03, },
362
363 [54] = { .offset = 0xba, .value = 0x00, },
364 [55] = { .offset = 0xbb, .value = 0x20, },
365 [56] = { .offset = 0xf3, .value = 0x90, },
366 [57] = { .offset = 0xf4, .value = 0x00, },
367 [58] = { .offset = 0xf7, .value = 0x88, },
368
369 [59] = { .offset = 0xf8, .value = 0x0a, },
370 [60] = { .offset = 0xf9, .value = 0x00, }
371};
372
373static const struct ns2501_reg regs_init[] = {
374 [0] = { .offset = 0x35, .value = 0xff, },
375 [1] = { .offset = 0x34, .value = 0x00, },
376 [2] = { .offset = 0x08, .value = 0x30, },
377};
378
379struct ns2501_priv {
380 bool quiet;
381 const struct ns2501_configuration *conf;
382};
383
384#define NSPTR(d) ((NS2501Ptr)(d->DriverPrivate.ptr))
385
386
387
388
389
390
391
392static bool ns2501_readb(struct intel_dvo_device *dvo, int addr, uint8_t * ch)
393{
394 struct ns2501_priv *ns = dvo->dev_priv;
395 struct i2c_adapter *adapter = dvo->i2c_bus;
396 u8 out_buf[2];
397 u8 in_buf[2];
398
399 struct i2c_msg msgs[] = {
400 {
401 .addr = dvo->slave_addr,
402 .flags = 0,
403 .len = 1,
404 .buf = out_buf,
405 },
406 {
407 .addr = dvo->slave_addr,
408 .flags = I2C_M_RD,
409 .len = 1,
410 .buf = in_buf,
411 }
412 };
413
414 out_buf[0] = addr;
415 out_buf[1] = 0;
416
417 if (i2c_transfer(adapter, msgs, 2) == 2) {
418 *ch = in_buf[0];
419 return true;
420 }
421
422 if (!ns->quiet) {
423 DRM_DEBUG_KMS
424 ("Unable to read register 0x%02x from %s:0x%02x.\n", addr,
425 adapter->name, dvo->slave_addr);
426 }
427
428 return false;
429}
430
431
432
433
434
435
436
437static bool ns2501_writeb(struct intel_dvo_device *dvo, int addr, uint8_t ch)
438{
439 struct ns2501_priv *ns = dvo->dev_priv;
440 struct i2c_adapter *adapter = dvo->i2c_bus;
441 uint8_t out_buf[2];
442
443 struct i2c_msg msg = {
444 .addr = dvo->slave_addr,
445 .flags = 0,
446 .len = 2,
447 .buf = out_buf,
448 };
449
450 out_buf[0] = addr;
451 out_buf[1] = ch;
452
453 if (i2c_transfer(adapter, &msg, 1) == 1) {
454 return true;
455 }
456
457 if (!ns->quiet) {
458 DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d\n",
459 addr, adapter->name, dvo->slave_addr);
460 }
461
462 return false;
463}
464
465
466
467
468
469
470
471static bool ns2501_init(struct intel_dvo_device *dvo,
472 struct i2c_adapter *adapter)
473{
474
475 struct ns2501_priv *ns;
476 unsigned char ch;
477
478 ns = kzalloc(sizeof(struct ns2501_priv), GFP_KERNEL);
479 if (ns == NULL)
480 return false;
481
482 dvo->i2c_bus = adapter;
483 dvo->dev_priv = ns;
484 ns->quiet = true;
485
486 if (!ns2501_readb(dvo, NS2501_VID_LO, &ch))
487 goto out;
488
489 if (ch != (NS2501_VID & 0xff)) {
490 DRM_DEBUG_KMS("ns2501 not detected got %d: from %s Slave %d.\n",
491 ch, adapter->name, dvo->slave_addr);
492 goto out;
493 }
494
495 if (!ns2501_readb(dvo, NS2501_DID_LO, &ch))
496 goto out;
497
498 if (ch != (NS2501_DID & 0xff)) {
499 DRM_DEBUG_KMS("ns2501 not detected got %d: from %s Slave %d.\n",
500 ch, adapter->name, dvo->slave_addr);
501 goto out;
502 }
503 ns->quiet = false;
504
505 DRM_DEBUG_KMS("init ns2501 dvo controller successfully!\n");
506
507 return true;
508
509out:
510 kfree(ns);
511 return false;
512}
513
514static enum drm_connector_status ns2501_detect(struct intel_dvo_device *dvo)
515{
516
517
518
519
520
521
522
523 return connector_status_connected;
524}
525
526static enum drm_mode_status ns2501_mode_valid(struct intel_dvo_device *dvo,
527 struct drm_display_mode *mode)
528{
529 DRM_DEBUG_KMS
530 ("is mode valid (hdisplay=%d,htotal=%d,vdisplay=%d,vtotal=%d)\n",
531 mode->hdisplay, mode->htotal, mode->vdisplay, mode->vtotal);
532
533
534
535
536
537
538
539 if ((mode->hdisplay == 640 && mode->vdisplay == 480 && mode->clock == 25175) ||
540 (mode->hdisplay == 800 && mode->vdisplay == 600 && mode->clock == 40000) ||
541 (mode->hdisplay == 1024 && mode->vdisplay == 768 && mode->clock == 65000)) {
542 return MODE_OK;
543 } else {
544 return MODE_ONE_SIZE;
545 }
546}
547
548static void ns2501_mode_set(struct intel_dvo_device *dvo,
549 const struct drm_display_mode *mode,
550 const struct drm_display_mode *adjusted_mode)
551{
552 const struct ns2501_configuration *conf;
553 struct ns2501_priv *ns = (struct ns2501_priv *)(dvo->dev_priv);
554 int mode_idx, i;
555
556 DRM_DEBUG_KMS
557 ("set mode (hdisplay=%d,htotal=%d,vdisplay=%d,vtotal=%d).\n",
558 mode->hdisplay, mode->htotal, mode->vdisplay, mode->vtotal);
559
560 DRM_DEBUG_KMS("Detailed requested mode settings are:\n"
561 "clock : %d kHz\n"
562 "hdisplay : %d\n"
563 "hblank start : %d\n"
564 "hblank end : %d\n"
565 "hsync start : %d\n"
566 "hsync end : %d\n"
567 "htotal : %d\n"
568 "hskew : %d\n"
569 "vdisplay : %d\n"
570 "vblank start : %d\n"
571 "hblank end : %d\n"
572 "vsync start : %d\n"
573 "vsync end : %d\n"
574 "vtotal : %d\n",
575 adjusted_mode->crtc_clock,
576 adjusted_mode->crtc_hdisplay,
577 adjusted_mode->crtc_hblank_start,
578 adjusted_mode->crtc_hblank_end,
579 adjusted_mode->crtc_hsync_start,
580 adjusted_mode->crtc_hsync_end,
581 adjusted_mode->crtc_htotal,
582 adjusted_mode->crtc_hskew,
583 adjusted_mode->crtc_vdisplay,
584 adjusted_mode->crtc_vblank_start,
585 adjusted_mode->crtc_vblank_end,
586 adjusted_mode->crtc_vsync_start,
587 adjusted_mode->crtc_vsync_end,
588 adjusted_mode->crtc_vtotal);
589
590 if (mode->hdisplay == 640 && mode->vdisplay == 480)
591 mode_idx = MODE_640x480;
592 else if (mode->hdisplay == 800 && mode->vdisplay == 600)
593 mode_idx = MODE_800x600;
594 else if (mode->hdisplay == 1024 && mode->vdisplay == 768)
595 mode_idx = MODE_1024x768;
596 else
597 return;
598
599
600 for (i = 0; i < ARRAY_SIZE(regs_init); i++)
601 ns2501_writeb(dvo, regs_init[i].offset, regs_init[i].value);
602
603
604 for (i = 0; i < ARRAY_SIZE(mode_agnostic_values); i++)
605 ns2501_writeb(dvo, mode_agnostic_values[i].offset,
606 mode_agnostic_values[i].value);
607
608
609 conf = ns2501_modes + mode_idx;
610 ns->conf = conf;
611
612 ns2501_writeb(dvo, NS2501_REG8, conf->conf);
613 ns2501_writeb(dvo, NS2501_REG1B, conf->pll_a);
614 ns2501_writeb(dvo, NS2501_REG1C, conf->pll_b & 0xff);
615 ns2501_writeb(dvo, NS2501_REG1D, conf->pll_b >> 8);
616 ns2501_writeb(dvo, NS2501_REGC1, conf->hstart & 0xff);
617 ns2501_writeb(dvo, NS2501_REGC2, conf->hstart >> 8);
618 ns2501_writeb(dvo, NS2501_REGC3, conf->hstop & 0xff);
619 ns2501_writeb(dvo, NS2501_REGC4, conf->hstop >> 8);
620 ns2501_writeb(dvo, NS2501_REGC5, conf->vstart & 0xff);
621 ns2501_writeb(dvo, NS2501_REGC6, conf->vstart >> 8);
622 ns2501_writeb(dvo, NS2501_REGC7, conf->vstop & 0xff);
623 ns2501_writeb(dvo, NS2501_REGC8, conf->vstop >> 8);
624 ns2501_writeb(dvo, NS2501_REG80, conf->vsync & 0xff);
625 ns2501_writeb(dvo, NS2501_REG81, conf->vsync >> 8);
626 ns2501_writeb(dvo, NS2501_REG82, conf->vtotal & 0xff);
627 ns2501_writeb(dvo, NS2501_REG83, conf->vtotal >> 8);
628 ns2501_writeb(dvo, NS2501_REG98, conf->hpos & 0xff);
629 ns2501_writeb(dvo, NS2501_REG99, conf->hpos >> 8);
630 ns2501_writeb(dvo, NS2501_REG8E, conf->vpos & 0xff);
631 ns2501_writeb(dvo, NS2501_REG8F, conf->vpos >> 8);
632 ns2501_writeb(dvo, NS2501_REG9C, conf->voffs & 0xff);
633 ns2501_writeb(dvo, NS2501_REG9D, conf->voffs >> 8);
634 ns2501_writeb(dvo, NS2501_REGB8, conf->hscale & 0xff);
635 ns2501_writeb(dvo, NS2501_REGB9, conf->hscale >> 8);
636 ns2501_writeb(dvo, NS2501_REG10, conf->vscale & 0xff);
637 ns2501_writeb(dvo, NS2501_REG11, conf->vscale >> 8);
638 ns2501_writeb(dvo, NS2501_REGF9, conf->dither);
639 ns2501_writeb(dvo, NS2501_REG41, conf->syncb);
640 ns2501_writeb(dvo, NS2501_REGC0, conf->sync);
641}
642
643
644static bool ns2501_get_hw_state(struct intel_dvo_device *dvo)
645{
646 unsigned char ch;
647
648 if (!ns2501_readb(dvo, NS2501_REG8, &ch))
649 return false;
650
651 return ch & NS2501_8_PD;
652}
653
654
655static void ns2501_dpms(struct intel_dvo_device *dvo, bool enable)
656{
657 struct ns2501_priv *ns = (struct ns2501_priv *)(dvo->dev_priv);
658
659 DRM_DEBUG_KMS("Trying set the dpms of the DVO to %i\n", enable);
660
661 if (enable) {
662 ns2501_writeb(dvo, NS2501_REGC0, ns->conf->sync | 0x08);
663
664 ns2501_writeb(dvo, NS2501_REG41, ns->conf->syncb);
665
666 ns2501_writeb(dvo, NS2501_REG34, NS2501_34_ENABLE_OUTPUT);
667 msleep(15);
668
669 ns2501_writeb(dvo, NS2501_REG8,
670 ns->conf->conf | NS2501_8_BPAS);
671 if (!(ns->conf->conf & NS2501_8_BPAS))
672 ns2501_writeb(dvo, NS2501_REG8, ns->conf->conf);
673 msleep(200);
674
675 ns2501_writeb(dvo, NS2501_REG34,
676 NS2501_34_ENABLE_OUTPUT | NS2501_34_ENABLE_BACKLIGHT);
677
678 ns2501_writeb(dvo, NS2501_REGC0, ns->conf->sync);
679 } else {
680 ns2501_writeb(dvo, NS2501_REG34, NS2501_34_ENABLE_OUTPUT);
681 msleep(200);
682
683 ns2501_writeb(dvo, NS2501_REG8, NS2501_8_VEN | NS2501_8_HEN |
684 NS2501_8_BPAS);
685 msleep(15);
686
687 ns2501_writeb(dvo, NS2501_REG34, 0x00);
688 }
689}
690
691static void ns2501_destroy(struct intel_dvo_device *dvo)
692{
693 struct ns2501_priv *ns = dvo->dev_priv;
694
695 if (ns) {
696 kfree(ns);
697 dvo->dev_priv = NULL;
698 }
699}
700
701const struct intel_dvo_dev_ops ns2501_ops = {
702 .init = ns2501_init,
703 .detect = ns2501_detect,
704 .mode_valid = ns2501_mode_valid,
705 .mode_set = ns2501_mode_set,
706 .dpms = ns2501_dpms,
707 .get_hw_state = ns2501_get_hw_state,
708 .destroy = ns2501_destroy,
709};
710