aboutsummaryrefslogtreecommitdiff
path: root/drivers/video/mcde/mcde_display.c
blob: 1537980dcaf03a32bb89c1ae8dec04ddd8ae8f83 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * ST-Ericsson MCDE display driver
 *
 * Author: Marcus Lorentzon <marcus.xm.lorentzon@stericsson.com>
 * for ST-Ericsson.
 *
 * License terms: GNU General Public License (GPL), version 2.
 */

#include <linux/kernel.h>
#include <linux/device.h>

#include <video/mcde_display.h>

/*temp*/
#include <linux/delay.h>

static void mcde_display_get_native_resolution_default(
	struct mcde_display_device *ddev, u16 *x_res, u16 *y_res)
{
	if (x_res)
		*x_res = ddev->native_x_res;
	if (y_res)
		*y_res = ddev->native_y_res;
}

static enum mcde_ovly_pix_fmt mcde_display_get_default_pixel_format_default(
	struct mcde_display_device *ddev)
{
	return ddev->default_pixel_format;
}

static void mcde_display_get_physical_size_default(
	struct mcde_display_device *ddev, u16 *width, u16 *height)
{
	if (width)
		*width = ddev->physical_width;
	if (height)
		*height = ddev->physical_height;
}

static int mcde_display_set_power_mode_default(struct mcde_display_device *ddev,
	enum mcde_display_power_mode power_mode)
{
	int ret = 0;

	/* OFF -> STANDBY */
	if (ddev->power_mode == MCDE_DISPLAY_PM_OFF &&
		power_mode >= MCDE_DISPLAY_PM_OFF) {
		if (ddev->platform_enable)
			ret = ddev->platform_enable(ddev);
		if (!ret)
			ddev->power_mode = MCDE_DISPLAY_PM_STANDBY;
	}

	if (!ret && ddev->port->type == MCDE_PORTTYPE_DSI) {
		/* STANDBY -> ON */
		if (ddev->power_mode == MCDE_DISPLAY_PM_STANDBY &&
			power_mode == MCDE_DISPLAY_PM_ON) {
			ret = mcde_display_dsi_dcs_write(ddev,
				DCS_CMD_EXIT_SLEEP_MODE, NULL, 0);
			if (!ret)
				ret = mcde_display_dsi_dcs_write(ddev,
					DCS_CMD_SET_DISPLAY_ON, NULL, 0);
			if (!ret)
				ddev->power_mode = MCDE_DISPLAY_PM_ON;
		}
		/* ON -> STANDBY */
		else if (ddev->power_mode == MCDE_DISPLAY_PM_ON &&
			power_mode <= MCDE_DISPLAY_PM_STANDBY) {
			ret = mcde_display_dsi_dcs_write(ddev,
				DCS_CMD_SET_DISPLAY_OFF, NULL, 0);
			if (!ret)
				ret = mcde_display_dsi_dcs_write(ddev,
					DCS_CMD_ENTER_SLEEP_MODE, NULL, 0);
			if (!ret)
				ddev->power_mode = MCDE_DISPLAY_PM_STANDBY;
		}
	} else if (!ret && ddev->port->type == MCDE_PORTTYPE_DPI)
		ddev->power_mode = power_mode;
	else if (ddev->power_mode != power_mode)
		return -EINVAL;

	/* SLEEP -> OFF */
	if (!ret && ddev->power_mode == MCDE_DISPLAY_PM_STANDBY &&
		power_mode == MCDE_DISPLAY_PM_OFF) {
		if (ddev->platform_disable)
			ret = ddev->platform_disable(ddev);
		if (!ret)
			ddev->power_mode = MCDE_DISPLAY_PM_OFF;
	}

	mcde_chnl_set_power_mode(ddev->chnl_state, ddev->power_mode);

	return ret;
}

static enum mcde_display_power_mode mcde_display_get_power_mode_default(
	struct mcde_display_device *ddev)
{
	return ddev->power_mode;
}

static int mcde_display_try_video_mode_default(struct mcde_display_device *ddev,
	struct mcde_video_mode *video_mode)
{
	/* TODO Check if inside native_xres and native_yres */
	return 0;
}

static int mcde_display_set_video_mode_default(struct mcde_display_device *ddev,
	struct mcde_video_mode *video_mode)
{
	int ret = 0;
	struct mcde_video_mode channel_video_mode;
	if (!video_mode)
		return -EINVAL;

	ddev->video_mode = *video_mode;
	channel_video_mode = ddev->video_mode;
	/* Dependant on if display should rotate or MCDE should rotate */
	if (ddev->rotation == MCDE_DISPLAY_ROT_90_CCW ||
				ddev->rotation == MCDE_DISPLAY_ROT_90_CW) {
		channel_video_mode.xres = ddev->native_x_res;
		channel_video_mode.yres = ddev->native_y_res;
	}
	ret = mcde_chnl_set_video_mode(ddev->chnl_state, &channel_video_mode);
	if (ret < 0) {
		dev_warn(&ddev->dev, "%s:Failed to set video mode\n", __func__);
		goto out;
	}
	ddev->update_flags |= UPDATE_FLAG_VIDEO_MODE;

out:
	return ret;
}

static void mcde_display_get_video_mode_default(
	struct mcde_display_device *ddev, struct mcde_video_mode *video_mode)
{
	if (video_mode)
		*video_mode = ddev->video_mode;
}

static int mcde_display_set_pixel_format_default(
	struct mcde_display_device *ddev, enum mcde_ovly_pix_fmt format)
{
	int ret = 0;
	ddev->pixel_format = format;
	ret = mcde_chnl_set_pixel_format(ddev->chnl_state,
						ddev->port->pixel_format);
	if (ret < 0) {
		dev_warn(&ddev->dev, "%s:Failed to set pixel format = %d\n",
							__func__, format);
		goto out;
	}

	ddev->update_flags |= UPDATE_FLAG_PIXEL_FORMAT;
out:
	return ret;
}

static enum mcde_ovly_pix_fmt mcde_display_get_pixel_format_default(
	struct mcde_display_device *ddev)
{
	return ddev->pixel_format;
}

static enum mcde_port_pix_fmt mcde_display_get_port_pixel_format_default(
	struct mcde_display_device *ddev)
{
	return ddev->port->pixel_format;
}

static int mcde_display_set_rotation_default(struct mcde_display_device *ddev,
	enum mcde_display_rotation rotation)
{
	int ret = 0;

	ret = mcde_chnl_set_rotation(ddev->chnl_state, rotation,
		ddev->rotbuf1, ddev->rotbuf2);
	if (ret < 0) {
		dev_warn(&ddev->dev, "%s:Failed to set rotation = %d\n",
							__func__, rotation);
		goto out;
	}

	if (!ret) {
		ddev->rotation = rotation;
		ddev->update_flags |= UPDATE_FLAG_ROTATION;
	}
out:
	return ret;
}

static enum mcde_display_rotation mcde_display_get_rotation_default(
	struct mcde_display_device *ddev)
{
	return ddev->rotation;
}

static int mcde_display_set_synchronized_update_default(
	struct mcde_display_device *ddev, bool enable)
{
	int ret = 0;
	if (ddev->port->type == MCDE_PORTTYPE_DSI) {
		if (ddev->port->sync_src == MCDE_SYNCSRC_TE0 ||
			 ddev->port->sync_src == MCDE_SYNCSRC_TE1 ||
			 ddev->port->sync_src == MCDE_SYNCSRC_BTA) {
			u8 m = 0;
			ret = mcde_display_dsi_dcs_write(ddev,
				DCS_CMD_SET_TEAR_ON, &m, 1);
		} else {
			ret = -EINVAL;
		}
	} else {
		ret = -EINVAL;
	}

	if (ret < 0) {
		dev_warn(&ddev->dev,
			"%s:Failed to set synchornized update = %d\n",
			__func__, enable);
		goto out;
	}

	ddev->synchronized_update = enable;

	return ret;
out:
	return ret;
}

static bool mcde_display_get_synchronized_update_default(
	struct mcde_display_device *ddev)
{
	return ddev->synchronized_update;
}

static int mcde_display_apply_config_default(struct mcde_display_device *ddev)
{
	int ret = 0;
	ret = mcde_chnl_enable_synchronized_update(ddev->chnl_state,
		ddev->synchronized_update);

	if (ret < 0) {
		dev_warn(&ddev->dev,
			"%s:Failed to enable synchronized update\n",
			__func__);
		goto out;
	}

	if (ddev->update_flags) {
		ret = mcde_chnl_apply(ddev->chnl_state);
		if (ret < 0) {
			dev_warn(&ddev->dev, "%s:Failed to apply to channel\n",
								__func__);
			goto out;
		}
		ddev->update_flags = 0;
		ddev->first_update = true;
	}
out:
	return ret;
}

static int mcde_display_invalidate_area_default(
					struct mcde_display_device *ddev,
					struct mcde_rectangle *area)
{
	dev_vdbg(&ddev->dev, "%s\n", __func__);
	if (area) {
		/* take union of rects */
		u16 t;
		t = min(ddev->update_area.x, area->x);
		/* note should be > 0 */
		ddev->update_area.w = max(ddev->update_area.x +
							ddev->update_area.w,
							area->x + area->w) - t;
		ddev->update_area.x = t;
		t = min(ddev->update_area.y, area->y);
		ddev->update_area.h = max(ddev->update_area.y +
							ddev->update_area.h,
							area->y + area->h) - t;
		ddev->update_area.y = t;
		/* TODO: Implement real clipping when partial refresh is
		activated.*/
		ddev->update_area.w = min((u16) ddev->video_mode.xres,
					(u16) ddev->update_area.w);
		ddev->update_area.h = min((u16) ddev->video_mode.yres,
					(u16) ddev->update_area.h);
	} else {
		ddev->update_area.x = 0;
		ddev->update_area.y = 0;
		ddev->update_area.w = ddev->video_mode.xres;
		ddev->update_area.h = ddev->video_mode.yres;
		/* Invalidate_area(ddev, NULL) means reset area to empty
		 * rectangle really. After that the rectangle should grow by
		 * taking an union (above). This means that the code should
		 * really look like below, however the code above is a temp fix
		 * for rotation.
		 * TODO: fix
		 * ddev->update_area.x = ddev->video_mode.xres;
		 * ddev->update_area.y = ddev->video_mode.yres;
		 * ddev->update_area.w = 0;
		 * ddev->update_area.h = 0;
		 */
	}

	return 0;
}

static int mcde_display_update_default(struct mcde_display_device *ddev)
{
	int ret;
	/* TODO: Dirty */
	if (ddev->prepare_for_update) {
		/* TODO: Send dirty rectangle */
		ret = ddev->prepare_for_update(ddev, 0, 0,
			ddev->native_x_res, ddev->native_y_res);
		if (ret < 0) {
			dev_warn(&ddev->dev,
				"%s:Failed to prepare for update\n", __func__);
			goto out;
		}
	}
	/* TODO: Calculate & set update rect */
	ret = mcde_chnl_update(ddev->chnl_state, &ddev->update_area);
	if (ret < 0) {
		dev_warn(&ddev->dev, "%s:Failed to update channel\n", __func__);
		goto out;
	}
	if (ddev->first_update && ddev->on_first_update)
		ddev->on_first_update(ddev);

	if (ddev->power_mode != MCDE_DISPLAY_PM_ON && ddev->set_power_mode) {
		ret = ddev->set_power_mode(ddev, MCDE_DISPLAY_PM_ON);
		if (ret < 0) {
			dev_warn(&ddev->dev,
				"%s:Failed to set power mode to on\n",
				__func__);
			goto out;
		}
	}

	dev_vdbg(&ddev->dev, "Overlay updated, chnl=%d\n", ddev->chnl_id);
out:
	return ret;
}

static int mcde_display_prepare_for_update_default(
					struct mcde_display_device *ddev,
					u16 x, u16 y, u16 w, u16 h)
{
	int ret = 0;
	if (ddev->port->type == MCDE_PORTTYPE_DSI) {
		u8 params[8] = { x >> 8, x & 0xff,
				(x + w - 1) >> 8, (x + w - 1) & 0xff,
				 y >> 8, y & 0xff,
				(y + h - 1) >> 8, (y + h - 1) & 0xff };
		ret = mcde_display_dsi_dcs_write(ddev,
			DCS_CMD_SET_COLUMN_ADDRESS, &params[0], 4);
		if (ret)
			return ret;
		ret = mcde_display_dsi_dcs_write(ddev,
			DCS_CMD_SET_PAGE_ADDRESS, &params[4], 4);
	} else
		ret = -EINVAL;
	return ret;
}

static int mcde_display_on_first_update_default(
					struct mcde_display_device *ddev)
{
	ddev->first_update = false;
	return 0;
}

void mcde_display_init_device(struct mcde_display_device *ddev)
{
	/* Setup default callbacks */
	ddev->get_native_resolution =
				mcde_display_get_native_resolution_default;
	ddev->get_default_pixel_format =
				mcde_display_get_default_pixel_format_default;
	ddev->get_physical_size = mcde_display_get_physical_size_default;
	ddev->set_power_mode = mcde_display_set_power_mode_default;
	ddev->get_power_mode = mcde_display_get_power_mode_default;
	ddev->try_video_mode = mcde_display_try_video_mode_default;
	ddev->set_video_mode = mcde_display_set_video_mode_default;
	ddev->get_video_mode = mcde_display_get_video_mode_default;
	ddev->set_pixel_format = mcde_display_set_pixel_format_default;
	ddev->get_pixel_format = mcde_display_get_pixel_format_default;
	ddev->get_port_pixel_format =
				mcde_display_get_port_pixel_format_default;
	ddev->set_rotation = mcde_display_set_rotation_default;
	ddev->get_rotation = mcde_display_get_rotation_default;
	ddev->set_synchronized_update =
				mcde_display_set_synchronized_update_default;
	ddev->get_synchronized_update =
				mcde_display_get_synchronized_update_default;
	ddev->apply_config = mcde_display_apply_config_default;
	ddev->invalidate_area = mcde_display_invalidate_area_default;
	ddev->update = mcde_display_update_default;
	ddev->prepare_for_update = mcde_display_prepare_for_update_default;
	ddev->on_first_update = mcde_display_on_first_update_default;
}

int mcde_display_dsi_dcs_write(struct mcde_display_device *ddev,
	u8 cmd, u8 *data, int len)
{
	return mcde_dsi_dcs_write(ddev->chnl_state, cmd, data, len);
}
EXPORT_SYMBOL(mcde_display_dsi_dcs_write);

int mcde_display_dsi_dcs_read(struct mcde_display_device *ddev,
	u8 cmd, u8 *data, int *len)
{
	return mcde_dsi_dcs_read(ddev->chnl_state, cmd, data, len);
}
EXPORT_SYMBOL(mcde_display_dsi_dcs_read);