dvo_sil164.c revision 296548
1/**************************************************************************
2
3Copyright �� 2006 Dave Airlie
4
5All Rights Reserved.
6
7Permission is hereby granted, free of charge, to any person obtaining a
8copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sub license, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice (including the
16next paragraph) shall be included in all copies or substantial portions
17of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
23ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27**************************************************************************/
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/drm2/i915/dvo_sil164.c 296548 2016-03-08 20:33:02Z dumbbell $");
31
32#include "dvo.h"
33
34#define SIL164_VID 0x0001
35#define SIL164_DID 0x0006
36
37#define SIL164_VID_LO 0x00
38#define SIL164_VID_HI 0x01
39#define SIL164_DID_LO 0x02
40#define SIL164_DID_HI 0x03
41#define SIL164_REV    0x04
42#define SIL164_RSVD   0x05
43#define SIL164_FREQ_LO 0x06
44#define SIL164_FREQ_HI 0x07
45
46#define SIL164_REG8 0x08
47#define SIL164_8_VEN (1<<5)
48#define SIL164_8_HEN (1<<4)
49#define SIL164_8_DSEL (1<<3)
50#define SIL164_8_BSEL (1<<2)
51#define SIL164_8_EDGE (1<<1)
52#define SIL164_8_PD   (1<<0)
53
54#define SIL164_REG9 0x09
55#define SIL164_9_VLOW (1<<7)
56#define SIL164_9_MSEL_MASK (0x7<<4)
57#define SIL164_9_TSEL (1<<3)
58#define SIL164_9_RSEN (1<<2)
59#define SIL164_9_HTPLG (1<<1)
60#define SIL164_9_MDI (1<<0)
61
62#define SIL164_REGC 0x0c
63
64struct sil164_priv {
65	//I2CDevRec d;
66	bool quiet;
67};
68
69#define SILPTR(d) ((SIL164Ptr)(d->DriverPrivate.ptr))
70
71static bool sil164_readb(struct intel_dvo_device *dvo, int addr, uint8_t *ch)
72{
73	struct sil164_priv *sil = dvo->dev_priv;
74	device_t adapter = dvo->i2c_bus;
75	u8 out_buf[2];
76	u8 in_buf[2];
77
78	struct iic_msg msgs[] = {
79		{
80			.slave = dvo->slave_addr << 1,
81			.flags = 0,
82			.len = 1,
83			.buf = out_buf,
84		},
85		{
86			.slave = dvo->slave_addr << 1,
87			.flags = I2C_M_RD,
88			.len = 1,
89			.buf = in_buf,
90		}
91	};
92
93	out_buf[0] = addr;
94	out_buf[1] = 0;
95
96	if (-iicbus_transfer(adapter, msgs, 2) == 0) {
97		*ch = in_buf[0];
98		return true;
99	}
100
101	if (!sil->quiet) {
102		DRM_DEBUG_KMS("Unable to read register 0x%02x from %s:%02x.\n",
103			  addr, device_get_nameunit(adapter), dvo->slave_addr);
104	}
105	return false;
106}
107
108static bool sil164_writeb(struct intel_dvo_device *dvo, int addr, uint8_t ch)
109{
110	struct sil164_priv *sil = dvo->dev_priv;
111	device_t adapter = dvo->i2c_bus;
112	uint8_t out_buf[2];
113	struct iic_msg msg = {
114		.slave = dvo->slave_addr << 1,
115		.flags = 0,
116		.len = 2,
117		.buf = out_buf,
118	};
119
120	out_buf[0] = addr;
121	out_buf[1] = ch;
122
123	if (-iicbus_transfer(adapter, &msg, 1) == 0)
124		return true;
125
126	if (!sil->quiet) {
127		DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d.\n",
128			  addr, device_get_nameunit(adapter), dvo->slave_addr);
129	}
130
131	return false;
132}
133
134/* Silicon Image 164 driver for chip on i2c bus */
135static bool sil164_init(struct intel_dvo_device *dvo,
136			device_t adapter)
137{
138	/* this will detect the SIL164 chip on the specified i2c bus */
139	struct sil164_priv *sil;
140	unsigned char ch;
141
142	sil = malloc(sizeof(struct sil164_priv), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
143	if (sil == NULL)
144		return false;
145
146	dvo->i2c_bus = adapter;
147	dvo->dev_priv = sil;
148	sil->quiet = true;
149
150	if (!sil164_readb(dvo, SIL164_VID_LO, &ch))
151		goto out;
152
153	if (ch != (SIL164_VID & 0xff)) {
154		DRM_DEBUG_KMS("sil164 not detected got %d: from %s Slave %d.\n",
155			  ch, device_get_nameunit(adapter), dvo->slave_addr);
156		goto out;
157	}
158
159	if (!sil164_readb(dvo, SIL164_DID_LO, &ch))
160		goto out;
161
162	if (ch != (SIL164_DID & 0xff)) {
163		DRM_DEBUG_KMS("sil164 not detected got %d: from %s Slave %d.\n",
164			  ch, device_get_nameunit(adapter), dvo->slave_addr);
165		goto out;
166	}
167	sil->quiet = false;
168
169	DRM_DEBUG_KMS("init sil164 dvo controller successfully!\n");
170	return true;
171
172out:
173	free(sil, DRM_MEM_KMS);
174	return false;
175}
176
177static enum drm_connector_status sil164_detect(struct intel_dvo_device *dvo)
178{
179	uint8_t reg9;
180
181	sil164_readb(dvo, SIL164_REG9, &reg9);
182
183	if (reg9 & SIL164_9_HTPLG)
184		return connector_status_connected;
185	else
186		return connector_status_disconnected;
187}
188
189static enum drm_mode_status sil164_mode_valid(struct intel_dvo_device *dvo,
190					      struct drm_display_mode *mode)
191{
192	return MODE_OK;
193}
194
195static void sil164_mode_set(struct intel_dvo_device *dvo,
196			    struct drm_display_mode *mode,
197			    struct drm_display_mode *adjusted_mode)
198{
199	/* As long as the basics are set up, since we don't have clock
200	 * dependencies in the mode setup, we can just leave the
201	 * registers alone and everything will work fine.
202	 */
203	/* recommended programming sequence from doc */
204	/*sil164_writeb(sil, 0x08, 0x30);
205	  sil164_writeb(sil, 0x09, 0x00);
206	  sil164_writeb(sil, 0x0a, 0x90);
207	  sil164_writeb(sil, 0x0c, 0x89);
208	  sil164_writeb(sil, 0x08, 0x31);*/
209	/* don't do much */
210	return;
211}
212
213/* set the SIL164 power state */
214static void sil164_dpms(struct intel_dvo_device *dvo, bool enable)
215{
216	int ret;
217	unsigned char ch;
218
219	ret = sil164_readb(dvo, SIL164_REG8, &ch);
220	if (ret == false)
221		return;
222
223	if (enable)
224		ch |= SIL164_8_PD;
225	else
226		ch &= ~SIL164_8_PD;
227
228	sil164_writeb(dvo, SIL164_REG8, ch);
229	return;
230}
231
232static bool sil164_get_hw_state(struct intel_dvo_device *dvo)
233{
234	int ret;
235	unsigned char ch;
236
237	ret = sil164_readb(dvo, SIL164_REG8, &ch);
238	if (ret == false)
239		return false;
240
241	if (ch & SIL164_8_PD)
242		return true;
243	else
244		return false;
245}
246
247static void sil164_dump_regs(struct intel_dvo_device *dvo)
248{
249	uint8_t val;
250
251	sil164_readb(dvo, SIL164_FREQ_LO, &val);
252	DRM_LOG_KMS("SIL164_FREQ_LO: 0x%02x\n", val);
253	sil164_readb(dvo, SIL164_FREQ_HI, &val);
254	DRM_LOG_KMS("SIL164_FREQ_HI: 0x%02x\n", val);
255	sil164_readb(dvo, SIL164_REG8, &val);
256	DRM_LOG_KMS("SIL164_REG8: 0x%02x\n", val);
257	sil164_readb(dvo, SIL164_REG9, &val);
258	DRM_LOG_KMS("SIL164_REG9: 0x%02x\n", val);
259	sil164_readb(dvo, SIL164_REGC, &val);
260	DRM_LOG_KMS("SIL164_REGC: 0x%02x\n", val);
261}
262
263static void sil164_destroy(struct intel_dvo_device *dvo)
264{
265	struct sil164_priv *sil = dvo->dev_priv;
266
267	if (sil) {
268		free(sil, DRM_MEM_KMS);
269		dvo->dev_priv = NULL;
270	}
271}
272
273struct intel_dvo_dev_ops sil164_ops = {
274	.init = sil164_init,
275	.detect = sil164_detect,
276	.mode_valid = sil164_mode_valid,
277	.mode_set = sil164_mode_set,
278	.dpms = sil164_dpms,
279	.get_hw_state = sil164_get_hw_state,
280	.dump_regs = sil164_dump_regs,
281	.destroy = sil164_destroy,
282};
283