1/*
2	Haiku S3 Savage driver adapted from the X.org Savage driver.
3
4	Copyright (C) 1994-2000 The XFree86 Project, Inc.	All Rights Reserved.
5	Copyright (c) 2003-2006, X.Org Foundation
6
7	Copyright 2007-2008 Haiku, Inc.  All rights reserved.
8	Distributed under the terms of the MIT license.
9
10	Authors:
11	Gerald Zajac 2007-2008
12*/
13
14#include "accel.h"
15#include "savage.h"
16
17#include <string.h>
18#include <ddc.h>
19#include <edid.h>
20
21
22
23static status_t
24GetI2CSignals(void* cookie, int* _clock, int* data)
25{
26	uint32 index = (uint32)(addr_t)cookie;
27	uint8 value = ReadCrtcReg(index);
28
29	*_clock = (value & 0x4) != 0;
30	*data = (value & 0x8) != 0;
31	return B_OK;
32}
33
34
35static status_t
36SetI2CSignals(void* cookie, int _clock, int data)
37{
38	uint32 index = (uint32)(addr_t)cookie;
39	uint8 value = 0x10;
40
41	if (_clock)
42		value |= 0x1;
43	if (data)
44		value |= 0x2;
45
46	WriteCrtcReg(index, value);
47	return B_OK;
48}
49
50
51
52bool
53Savage_GetEdidInfo(edid1_info& edidInfo)
54{
55	// Get the EDID info and return true if successful.
56
57	SharedInfo& si = *gInfo.sharedInfo;
58
59	uint32 DDCPort = 0;
60
61    switch (si.chipType) {
62	case S3_SAVAGE_3D:
63	case S3_SAVAGE_MX:
64	case S3_SUPERSAVAGE:
65	case S3_SAVAGE2000:
66		DDCPort = 0xAA;
67		break;
68
69	case S3_SAVAGE4:
70	case S3_PROSAVAGE:
71	case S3_TWISTER:
72	case S3_PROSAVAGE_DDR:
73		DDCPort = 0xB1;
74		break;
75    }
76
77	i2c_bus bus;
78	bus.cookie = (void*)(addr_t)DDCPort;
79	bus.set_signals = &SetI2CSignals;
80	bus.get_signals = &GetI2CSignals;
81	ddc2_init_timing(&bus);
82
83	uint8 tmp = ReadCrtcReg(DDCPort);
84	WriteCrtcReg(DDCPort, tmp | 0x13);
85
86	bool bResult = (ddc2_read_edid1(&bus, &edidInfo, NULL, NULL) == B_OK);
87	WriteCrtcReg(DDCPort, tmp);
88
89	return bResult;
90}
91