summaryrefslogtreecommitdiff
path: root/drivers/gpu/imx/dpu/dpu-extdst.c
blob: 013e03a2537ea794cb9f5ed318de1041958b45cf (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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * Copyright (C) 2016 Freescale Semiconductor, Inc.
 * Copyright 2017-2019 NXP
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <linux/io.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <video/dpu.h>
#include "dpu-prv.h"

#define PIXENGCFG_STATIC		0x8
#define POWERDOWN			BIT(4)
#define SYNC_MODE			BIT(8)
#define SW_RESET			BIT(11)
#define DIV(n)				(((n) & 0xFF) << 16)
#define DIV_RESET			0x80
#define PIXENGCFG_DYNAMIC		0xC
#define PIXENGCFG_REQUEST		0x10
#define SHDLDREQ(n)			BIT(n)
#define SEL_SHDLDREQ			BIT(0)
#define PIXENGCFG_TRIGGER		0x14
#define SYNC_TRIGGER			BIT(0)
#define TRIGGER_SEQUENCE_COMPLETE	BIT(4)
#define PIXENGCFG_STATUS		0x18
#define SYNC_BUSY			BIT(8)
#define KICK_MODE			BIT(8)
#define PERFCOUNTMODE			BIT(12)
#define CONTROL				0xC
#define GAMMAAPPLYENABLE		BIT(0)
#define SOFTWAREKICK			0x10
#define KICK				BIT(0)
#define STATUS				0x14
#define CNT_ERR_STS			BIT(0)
#define CONTROLWORD			0x18
#define CURPIXELCNT			0x1C
static u16 get_xval(u32 pixel_cnt)
{
	return pixel_cnt & 0xFFFF;
}

static u16 get_yval(u32 pixel_cnt)
{
	return pixel_cnt >> 16;
}
#define LASTPIXELCNT			0x20
#define PERFCOUNTER			0x24

struct dpu_extdst {
	void __iomem *pec_base;
	void __iomem *base;
	struct mutex mutex;
	int id;
	bool inuse;
	struct dpu_soc *dpu;
};

static inline u32 dpu_pec_ed_read(struct dpu_extdst *ed, unsigned int offset)
{
	return readl(ed->pec_base + offset);
}

static inline void dpu_pec_ed_write(struct dpu_extdst *ed,
				unsigned int offset, u32 value)
{
	writel(value, ed->pec_base + offset);
}

static inline u32 dpu_ed_read(struct dpu_extdst *ed, unsigned int offset)
{
	return readl(ed->base + offset);
}

static inline void dpu_ed_write(struct dpu_extdst *ed,
				unsigned int offset, u32 value)
{
	writel(value, ed->base + offset);
}

static inline bool dpu_ed_is_safety_stream(struct dpu_extdst *ed)
{
	if (ed->id == 4 || ed->id == 5)
		return true;

	return false;
}

void extdst_pixengcfg_shden(struct dpu_extdst *ed, bool enable)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATIC);
	if (enable)
		val |= SHDEN;
	else
		val &= ~SHDEN;
	dpu_pec_ed_write(ed, PIXENGCFG_STATIC, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_shden);

void extdst_pixengcfg_powerdown(struct dpu_extdst *ed, bool powerdown)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATIC);
	if (powerdown)
		val |= POWERDOWN;
	else
		val &= ~POWERDOWN;
	dpu_pec_ed_write(ed, PIXENGCFG_STATIC, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_powerdown);

void extdst_pixengcfg_sync_mode(struct dpu_extdst *ed, ed_sync_mode_t mode)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATIC);
	if (mode == AUTO)
		val |= SYNC_MODE;
	else
		val &= ~SYNC_MODE;
	dpu_pec_ed_write(ed, PIXENGCFG_STATIC, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_sync_mode);

void extdst_pixengcfg_reset(struct dpu_extdst *ed, bool reset)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATIC);
	if (reset)
		val |= SW_RESET;
	else
		val &= ~SW_RESET;
	dpu_pec_ed_write(ed, PIXENGCFG_STATIC, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_reset);

void extdst_pixengcfg_div(struct dpu_extdst *ed, u16 div)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATIC);
	val &= ~0xFF0000;
	val |= DIV(div);
	dpu_pec_ed_write(ed, PIXENGCFG_STATIC, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_div);

void extdst_pixengcfg_syncmode_master(struct dpu_extdst *ed, bool enable)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATIC);
	if (enable)
		val |= BIT(16);
	else
		val &= ~BIT(16);
	dpu_pec_ed_write(ed, PIXENGCFG_STATIC, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_syncmode_master);

int extdst_pixengcfg_src_sel(struct dpu_extdst *ed, extdst_src_sel_t src)
{
	mutex_lock(&ed->mutex);
	dpu_pec_ed_write(ed, PIXENGCFG_DYNAMIC, src);
	mutex_unlock(&ed->mutex);

	return 0;
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_src_sel);

void extdst_pixengcfg_sel_shdldreq(struct dpu_extdst *ed)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_REQUEST);
	val |= SEL_SHDLDREQ;
	dpu_pec_ed_write(ed, PIXENGCFG_REQUEST, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_sel_shdldreq);

void extdst_pixengcfg_shdldreq(struct dpu_extdst *ed, u32 req_mask)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_REQUEST);
	val |= req_mask;
	dpu_pec_ed_write(ed, PIXENGCFG_REQUEST, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_shdldreq);

void extdst_pixengcfg_sync_trigger(struct dpu_extdst *ed)
{
	mutex_lock(&ed->mutex);
	dpu_pec_ed_write(ed, PIXENGCFG_TRIGGER, SYNC_TRIGGER);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_sync_trigger);

void extdst_pixengcfg_trigger_sequence_complete(struct dpu_extdst *ed)
{
	mutex_lock(&ed->mutex);
	dpu_pec_ed_write(ed, PIXENGCFG_TRIGGER, TRIGGER_SEQUENCE_COMPLETE);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_trigger_sequence_complete);

bool extdst_pixengcfg_is_sync_busy(struct dpu_extdst *ed)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATUS);
	mutex_unlock(&ed->mutex);

	return val & SYNC_BUSY;
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_is_sync_busy);

ed_pipeline_status_t extdst_pixengcfg_pipeline_status(struct dpu_extdst *ed)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_pec_ed_read(ed, PIXENGCFG_STATUS);
	mutex_unlock(&ed->mutex);

	return val & 0x3;
}
EXPORT_SYMBOL_GPL(extdst_pixengcfg_pipeline_status);

void extdst_shden(struct dpu_extdst *ed, bool enable)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, STATICCONTROL);
	if (enable)
		val |= SHDEN;
	else
		val &= ~SHDEN;
	dpu_ed_write(ed, STATICCONTROL, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_shden);

void extdst_kick_mode(struct dpu_extdst *ed, ed_kick_mode_t mode)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, STATICCONTROL);
	val &= ~KICK_MODE;
	val |= mode;
	dpu_ed_write(ed, STATICCONTROL, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_kick_mode);

void extdst_perfcountmode(struct dpu_extdst *ed, bool enable)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, STATICCONTROL);
	if (enable)
		val |= PERFCOUNTMODE;
	else
		val &= ~PERFCOUNTMODE;
	dpu_ed_write(ed, STATICCONTROL, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_perfcountmode);

void extdst_gamma_apply_enable(struct dpu_extdst *ed, bool enable)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, CONTROL);
	if (enable)
		val |= GAMMAAPPLYENABLE;
	else
		val &= ~GAMMAAPPLYENABLE;
	dpu_ed_write(ed, CONTROL, val);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_gamma_apply_enable);

void extdst_kick(struct dpu_extdst *ed)
{
	mutex_lock(&ed->mutex);
	dpu_ed_write(ed, SOFTWAREKICK, KICK);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_kick);

void extdst_cnt_err_clear(struct dpu_extdst *ed)
{
	mutex_lock(&ed->mutex);
	dpu_ed_write(ed, STATUS, CNT_ERR_STS);
	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(extdst_cnt_err_clear);

bool extdst_cnt_err_status(struct dpu_extdst *ed)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, STATUS);
	mutex_unlock(&ed->mutex);

	return val & CNT_ERR_STS;
}
EXPORT_SYMBOL_GPL(extdst_cnt_err_status);

u32 extdst_last_control_word(struct dpu_extdst *ed)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, CONTROLWORD);
	mutex_unlock(&ed->mutex);

	return val;
}
EXPORT_SYMBOL_GPL(extdst_last_control_word);

void extdst_pixel_cnt(struct dpu_extdst *ed, u16 *x, u16 *y)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, CURPIXELCNT);
	mutex_unlock(&ed->mutex);

	*x = get_xval(val);
	*y = get_yval(val);
}
EXPORT_SYMBOL_GPL(extdst_pixel_cnt);

void extdst_last_pixel_cnt(struct dpu_extdst *ed, u16 *x, u16 *y)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, LASTPIXELCNT);
	mutex_unlock(&ed->mutex);

	*x = get_xval(val);
	*y = get_yval(val);
}
EXPORT_SYMBOL_GPL(extdst_last_pixel_cnt);

u32 extdst_perfresult(struct dpu_extdst *ed)
{
	u32 val;

	mutex_lock(&ed->mutex);
	val = dpu_ed_read(ed, PERFCOUNTER);
	mutex_unlock(&ed->mutex);

	return val;
}
EXPORT_SYMBOL_GPL(extdst_perfresult);

bool extdst_is_master(struct dpu_extdst *ed)
{
	const struct dpu_data *data = ed->dpu->data;

	return ed->id == data->master_stream_id;
}
EXPORT_SYMBOL_GPL(extdst_is_master);

struct dpu_extdst *dpu_ed_get(struct dpu_soc *dpu, int id)
{
	struct dpu_extdst *ed;
	int i;

	for (i = 0; i < ARRAY_SIZE(ed_ids); i++)
		if (ed_ids[i] == id)
			break;

	if (i == ARRAY_SIZE(ed_ids))
		return ERR_PTR(-EINVAL);

	ed = dpu->ed_priv[i];

	mutex_lock(&ed->mutex);

	if (ed->inuse) {
		mutex_unlock(&ed->mutex);
		return ERR_PTR(-EBUSY);
	}

	ed->inuse = true;

	mutex_unlock(&ed->mutex);

	return ed;
}
EXPORT_SYMBOL_GPL(dpu_ed_get);

void dpu_ed_put(struct dpu_extdst *ed)
{
	mutex_lock(&ed->mutex);

	ed->inuse = false;

	mutex_unlock(&ed->mutex);
}
EXPORT_SYMBOL_GPL(dpu_ed_put);

struct dpu_extdst *dpu_aux_ed_peek(struct dpu_extdst *ed)
{
	unsigned int aux_id = ed->id ^ 1;
	int i;

	for (i = 0; i < ARRAY_SIZE(ed_ids); i++)
		if (ed_ids[i] == aux_id)
			return ed->dpu->ed_priv[i];

	return NULL;
}
EXPORT_SYMBOL_GPL(dpu_aux_ed_peek);

void _dpu_ed_init(struct dpu_soc *dpu, unsigned int id)
{
	struct dpu_extdst *ed;
	int i;

	for (i = 0; i < ARRAY_SIZE(ed_ids); i++)
		if (ed_ids[i] == id)
			break;

	if (WARN_ON(i == ARRAY_SIZE(ed_ids)))
		return;

	ed = dpu->ed_priv[i];

	extdst_pixengcfg_src_sel(ed, ED_SRC_DISABLE);
	extdst_pixengcfg_shden(ed, true);
	extdst_pixengcfg_powerdown(ed, false);
	extdst_pixengcfg_sync_mode(ed, SINGLE);
	extdst_pixengcfg_reset(ed, false);
	extdst_pixengcfg_div(ed, DIV_RESET);
	extdst_shden(ed, true);
	extdst_perfcountmode(ed, false);
	extdst_kick_mode(ed, EXTERNAL);
}

int dpu_ed_init(struct dpu_soc *dpu, unsigned int id,
		unsigned long pec_base, unsigned long base)
{
	struct dpu_extdst *ed;
	int ret, i;

	ed = devm_kzalloc(dpu->dev, sizeof(*ed), GFP_KERNEL);
	if (!ed)
		return -ENOMEM;

	for (i = 0; i < ARRAY_SIZE(ed_ids); i++)
		if (ed_ids[i] == id)
			break;

	if (i == ARRAY_SIZE(ed_ids))
		return -EINVAL;

	dpu->ed_priv[i] = ed;

	ed->pec_base = devm_ioremap(dpu->dev, pec_base, SZ_32);
	if (!ed->pec_base)
		return -ENOMEM;

	ed->base = devm_ioremap(dpu->dev, base, SZ_64);
	if (!ed->base)
		return -ENOMEM;

	ed->dpu = dpu;
	ed->id = id;
	mutex_init(&ed->mutex);

	ret = extdst_pixengcfg_src_sel(ed, ED_SRC_DISABLE);
	if (ret < 0)
		return ret;

	_dpu_ed_init(dpu, id);

	return 0;
}