summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/openchrome/via_analog.c
blob: 4eac3dea5e1f40155ecf711c65a1423b5da8fec7 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright © 2012 James Simmons
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 * James Simmons <jsimmons@infradead.org>
 */
#include "via_drv.h"
#include "crtc_hw.h"


/*
 * Enables or disables analog (VGA) output.
 */
static void
via_analog_power(struct via_device *dev_priv, bool outputState)
{
	DRM_DEBUG_KMS("Entered via_analog_power.\n");

	via_analog_set_power(VGABASE, outputState);
	DRM_INFO("Analog (VGA) Power: %s\n",
			outputState ? "On" : "Off");

	DRM_DEBUG_KMS("Exiting via_analog_power.\n");
}

/*
 * Set analog (VGA) sync polarity.
 */
static void
via_analog_sync_polarity(struct via_device *dev_priv, unsigned int flags)
{
	u8 syncPolarity = 0x00;

	DRM_DEBUG_KMS("Entered via_analog_sync_polarity.\n");

	if (flags & DRM_MODE_FLAG_NHSYNC) {
		syncPolarity |= BIT(0);
	}

	if (flags & DRM_MODE_FLAG_NVSYNC) {
		syncPolarity |= BIT(1);
	}

	via_analog_set_sync_polarity(VGABASE, syncPolarity);
	DRM_INFO("Analog (VGA) Horizontal Sync Polarity: %s\n",
		(syncPolarity & BIT(0)) ? "-" : "+");
	DRM_INFO("Analog (VGA) Vertical Sync Polarity: %s\n",
		(syncPolarity & BIT(1)) ? "-" : "+");

	DRM_DEBUG_KMS("Exiting via_analog_sync_polarity.\n");
}

/*
 * Sets analog (VGA) display source.
 */
static void
via_analog_display_source(struct via_device *dev_priv, int index)
{
	u8 displaySource = index;

	DRM_DEBUG_KMS("Entered via_analog_display_source.\n");

	via_analog_set_display_source(VGABASE, displaySource & 0x01);
	DRM_INFO("Analog (VGA) Display Source: IGA%d\n",
			(displaySource & 0x01) + 1);

	DRM_DEBUG_KMS("Exiting via_analog_display_source.\n");
}

/*
 * Routines for controlling stuff on the analog port
 */
static const struct drm_encoder_funcs via_dac_enc_funcs = {
	.destroy = via_encoder_cleanup,
};

/*
 * Manage the power state of analog (VGA) DAC.
 */
static void
via_analog_dpms(struct drm_encoder *encoder, int mode)
{
	struct via_device *dev_priv = encoder->dev->dev_private;

	DRM_DEBUG_KMS("Entered via_analog_dpms.\n");

	switch (mode) {
	case DRM_MODE_DPMS_ON:
		via_analog_set_dpms_control(VGABASE, VIA_ANALOG_DPMS_ON);
		via_analog_power(dev_priv, true);
		break;
	case DRM_MODE_DPMS_STANDBY:
		via_analog_set_dpms_control(VGABASE, VIA_ANALOG_DPMS_STANDBY);
		via_analog_power(dev_priv, true);
		break;
	case DRM_MODE_DPMS_SUSPEND:
		via_analog_set_dpms_control(VGABASE, VIA_ANALOG_DPMS_SUSPEND);
		via_analog_power(dev_priv, true);
		break;
	case DRM_MODE_DPMS_OFF:
		via_analog_set_dpms_control(VGABASE, VIA_ANALOG_DPMS_OFF);
		via_analog_power(dev_priv, false);
		break;
	default:
		DRM_ERROR("Bad DPMS mode.");
		break;
	}

	DRM_DEBUG_KMS("Exiting via_analog_dpms.\n");
}

/* Pass our mode to the connectors and the CRTC to give them a chance to
 * adjust it according to limitations or connector properties, and also
 * a chance to reject the mode entirely. Useful for things like scaling.
 */
static bool
via_analog_mode_fixup(struct drm_encoder *encoder,
		 const struct drm_display_mode *mode,
		 struct drm_display_mode *adjusted_mode)
{
	drm_mode_set_crtcinfo(adjusted_mode, 0);
	return true;
}

/*
 * Handle analog (VGA) mode setting.
 */
static void
via_analog_mode_set(struct drm_encoder *encoder,
			struct drm_display_mode *mode,
			struct drm_display_mode *adjusted_mode)
{
	struct via_device *dev_priv = encoder->dev->dev_private;
	struct via_crtc *iga = container_of(encoder->crtc, struct via_crtc, base);

	DRM_DEBUG_KMS("Entered via_analog_mode_set.\n");

	via_analog_sync_polarity(dev_priv, adjusted_mode->flags);
	via_analog_display_source(dev_priv, iga->index);

	DRM_DEBUG_KMS("Exiting via_analog_mode_set.\n");
}

static void
via_analog_prepare(struct drm_encoder *encoder)
{
	struct via_device *dev_priv = encoder->dev->dev_private;

	DRM_DEBUG_KMS("Entered via_analog_prepare.\n");

	if (encoder->crtc) {
		via_analog_set_dpms_control(VGABASE, VIA_ANALOG_DPMS_OFF);
		via_analog_power(dev_priv, false);
	}

	DRM_DEBUG_KMS("Exiting via_analog_prepare.\n");
}

static void
via_analog_commit(struct drm_encoder *encoder)
{
	struct via_device *dev_priv = encoder->dev->dev_private;

	DRM_DEBUG_KMS("Entered via_analog_commit.\n");

	if (encoder->crtc) {
		via_analog_set_dpms_control(VGABASE, VIA_ANALOG_DPMS_ON);
		via_analog_power(dev_priv, true);
	}

	DRM_DEBUG_KMS("Exiting via_analog_commit.\n");
}

static void
via_analog_disable(struct drm_encoder *encoder)
{
	struct via_device *dev_priv = encoder->dev->dev_private;

	DRM_DEBUG_KMS("Entered via_analog_disable.\n");

	via_analog_set_dpms_control(VGABASE, VIA_ANALOG_DPMS_OFF);
	via_analog_power(dev_priv, false);

	DRM_DEBUG_KMS("Exiting via_analog_disable.\n");
}

static const struct drm_encoder_helper_funcs via_dac_enc_helper_funcs = {
	.dpms = via_analog_dpms,
	.mode_fixup = via_analog_mode_fixup,
	.mode_set = via_analog_mode_set,
	.prepare = via_analog_prepare,
	.commit = via_analog_commit,
	.disable = via_analog_disable,
};

static enum drm_connector_status
via_analog_detect(struct drm_connector *connector, bool force)
{
	struct via_connector *con = container_of(connector,
					struct via_connector, base);
	enum drm_connector_status ret = connector_status_disconnected;
	struct i2c_adapter *i2c_bus;
	struct edid *edid = NULL;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	drm_mode_connector_update_edid_property(connector, edid);

	if (con->i2c_bus & VIA_I2C_BUS1) {
		i2c_bus = via_find_ddc_bus(0x26);
	} else {
		i2c_bus = NULL;
	}

	if (i2c_bus) {
		edid = drm_get_edid(&con->base, i2c_bus);
		if (edid) {
			drm_mode_connector_update_edid_property(connector,
								edid);
			kfree(edid);
			ret = connector_status_connected;
		}
	}

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
	return ret;
}

static const struct drm_connector_funcs via_analog_connector_funcs = {
	.dpms = drm_helper_connector_dpms,
	.detect = via_analog_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.set_property = via_connector_set_property,
	.destroy = via_connector_destroy,
};

static int via_analog_get_modes(struct drm_connector *connector)
{
	struct via_connector *con = container_of(connector,
					struct via_connector, base);
	int count = 0;
	struct i2c_adapter *i2c_bus;
	struct edid *edid = NULL;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	if (con->i2c_bus & VIA_I2C_BUS1) {
		i2c_bus = via_find_ddc_bus(0x26);
	} else {
		i2c_bus = NULL;
	}

	if (i2c_bus) {
		edid = drm_get_edid(&con->base, i2c_bus);
		if (edid) {
			count = drm_add_edid_modes(connector, edid);
			kfree(edid);
		}
	}

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
	return count;
}

static const struct drm_connector_helper_funcs via_analog_connector_helper_funcs = {
	.mode_valid = via_connector_mode_valid,
	.get_modes = via_analog_get_modes,
	.best_encoder = via_best_encoder,
};

void via_analog_init(struct drm_device *dev)
{
	struct via_connector *con;
	struct via_encoder *enc;
	struct via_device *dev_priv = dev->dev_private;

	dev_priv->analog_i2c_bus = VIA_I2C_BUS1;

	enc = kzalloc(sizeof(*enc) + sizeof(*con), GFP_KERNEL);
	if (!enc) {
		DRM_ERROR("Failed to allocate connector and encoder\n");
		return;
	}
	con = &enc->cons[0];
	INIT_LIST_HEAD(&con->props);

	/* Piece together our connector */
	drm_connector_init(dev, &con->base, &via_analog_connector_funcs,
				DRM_MODE_CONNECTOR_VGA);
	drm_connector_helper_add(&con->base, &via_analog_connector_helper_funcs);
	drm_connector_register(&con->base);

	con->i2c_bus = dev_priv->analog_i2c_bus;
	con->base.doublescan_allowed = false;
	con->base.interlace_allowed = true;

	/* Setup the encoders and attach them */
	drm_encoder_init(dev, &enc->base, &via_dac_enc_funcs,
						DRM_MODE_ENCODER_DAC, NULL);
	drm_encoder_helper_add(&enc->base, &via_dac_enc_helper_funcs);

	enc->base.possible_crtcs = BIT(1) | BIT(0);
	enc->base.possible_clones = 0;
	enc->di_port = VIA_DI_PORT_NONE;

	drm_mode_connector_attach_encoder(&con->base, &enc->base);
}