aboutsummaryrefslogblamecommitdiffstats
path: root/drivers/media/video/samsung/fimc/csis.c
blob: 6dc29a0de6f33d5899a9027aa846f304a9d2c6a1 (plain) (tree)
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















































































































































































































































































































































































































































































                                                                                                          
/* linux/drivers/media/video/samsung/csis.c
 *
 * Copyright (c) 2010 Samsung Electronics Co,. Ltd.
 *	http://www.samsung.com/
 *
 * MIPI-CSI2 Support file for FIMC driver
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/clk.h>
#include <linux/fs.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/videodev2.h>
#include <linux/videodev2_samsung.h>

#include <linux/io.h>
#include <linux/memory.h>
#include <linux/slab.h>
#include <plat/clock.h>
#include <plat/regs-csis.h>
#include <plat/csis.h>

#include "csis.h"
static s32 err_print_cnt;

static struct s3c_csis_info *s3c_csis[S3C_CSIS_CH_NUM];

static int s3c_csis_set_info(struct platform_device *pdev)
{
	s3c_csis[pdev->id] = (struct s3c_csis_info *)
			kzalloc(sizeof(struct s3c_csis_info), GFP_KERNEL);
	if (!s3c_csis[pdev->id]) {
		err("no memory for configuration\n");
		return -ENOMEM;
	}

	sprintf(s3c_csis[pdev->id]->name, "%s%d", S3C_CSIS_NAME, pdev->id);
	s3c_csis[pdev->id]->nr_lanes = S3C_CSIS_NR_LANES;

	return 0;
}

static void s3c_csis_reset(struct platform_device *pdev)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
	cfg |= S3C_CSIS_CONTROL_RESET;
	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
}

static void s3c_csis_set_nr_lanes(struct platform_device *pdev, int lanes)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONFIG);
	cfg &= ~S3C_CSIS_CONFIG_NR_LANE_MASK;

	if (lanes == 1)
		cfg |= S3C_CSIS_CONFIG_NR_LANE_1;
	else if (lanes == 2)
		cfg |= S3C_CSIS_CONFIG_NR_LANE_2;
	else if (lanes == 3)
		cfg |= S3C_CSIS_CONFIG_NR_LANE_3;
	else if (lanes == 4)
		cfg |= S3C_CSIS_CONFIG_NR_LANE_4;
	else
		err("%d is not supported lane\n", lanes);

	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONFIG);
}

static void s3c_csis_enable_interrupt(struct platform_device *pdev)
{
	u32 cfg = 0;

	/* enable all interrupts */
	cfg |= S3C_CSIS_INTMSK_EVEN_BEFORE_ENABLE | \
		S3C_CSIS_INTMSK_EVEN_AFTER_ENABLE | \
		S3C_CSIS_INTMSK_ODD_BEFORE_ENABLE | \
		S3C_CSIS_INTMSK_ODD_AFTER_ENABLE | \
		S3C_CSIS_INTMSK_ERR_SOT_HS_ENABLE | \
		S3C_CSIS_INTMSK_ERR_LOST_FS_ENABLE | \
		S3C_CSIS_INTMSK_ERR_LOST_FE_ENABLE | \
		S3C_CSIS_INTMSK_ERR_OVER_ENABLE |\
		S3C_CSIS_INTMSK_ERR_ECC_ENABLE | \
		S3C_CSIS_INTMSK_ERR_CRC_ENABLE | \
		S3C_CSIS_INTMSK_ERR_ID_ENABLE;

	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_INTMSK);
}

static void s3c_csis_disable_interrupt(struct platform_device *pdev)
{
	/* disable all interrupts */
	writel(0, s3c_csis[pdev->id]->regs + S3C_CSIS_INTMSK);
}

static void s3c_csis_system_on(struct platform_device *pdev)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
	cfg |= S3C_CSIS_CONTROL_ENABLE;
	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
}

static void s3c_csis_system_off(struct platform_device *pdev)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
	cfg &= ~S3C_CSIS_CONTROL_ENABLE;
	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
}

static void s3c_csis_phy_on(struct platform_device *pdev)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_DPHYCTRL);
	cfg |= S3C_CSIS_DPHYCTRL_ENABLE;
	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_DPHYCTRL);
}

static void s3c_csis_phy_off(struct platform_device *pdev)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_DPHYCTRL);
	cfg &= ~S3C_CSIS_DPHYCTRL_ENABLE;
	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_DPHYCTRL);
}

#ifdef CONFIG_MIPI_CSI_ADV_FEATURE
static void s3c_csis_update_shadow(struct platform_device *pdev)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
	cfg |= S3C_CSIS_CONTROL_UPDATE_SHADOW;
	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
}

static void s3c_csis_set_data_align(struct platform_device *pdev, int align)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
	cfg &= ~S3C_CSIS_CONTROL_ALIGN_MASK;

	if (align == 24)
		cfg |= S3C_CSIS_CONTROL_ALIGN_24BIT;
	else
		cfg |= S3C_CSIS_CONTROL_ALIGN_32BIT;

	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
}

static void s3c_csis_set_wclk(struct platform_device *pdev, int extclk)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
	cfg &= ~S3C_CSIS_CONTROL_WCLK_MASK;

	if (extclk)
		cfg |= S3C_CSIS_CONTROL_WCLK_EXTCLK;
	else
		cfg |= S3C_CSIS_CONTROL_WCLK_PCLK;

	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONTROL);
}

static void s3c_csis_set_format(struct platform_device *pdev, enum mipi_format fmt)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_CONFIG);
	cfg &= ~S3C_CSIS_CONFIG_FORMAT_MASK;
	cfg |= (fmt << S3C_CSIS_CONFIG_FORMAT_SHIFT);

	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_CONFIG);
}

static void s3c_csis_set_resol(struct platform_device *pdev, int width, int height)
{
	u32 cfg = 0;

	cfg |= width << S3C_CSIS_RESOL_HOR_SHIFT;
	cfg |= height << S3C_CSIS_RESOL_VER_SHIFT;

	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_RESOL);
}

static void s3c_csis_set_hs_settle(struct platform_device *pdev, int settle)
{
	u32 cfg;

	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_DPHYCTRL);
	cfg &= ~S3C_CSIS_DPHYCTRL_HS_SETTLE_MASK;
	cfg |= (settle << S3C_CSIS_DPHYCTRL_HS_SETTLE_SHIFT);

	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_DPHYCTRL);
}
#endif

int s3c_csis_get_pkt(int csis_id, void *pktdata)
{
	memcpy(pktdata, s3c_csis[csis_id]->bufs.pktdata, CSIS_PKTSIZE);
	return 0;
}

void s3c_csis_enable_pktdata(int csis_id, bool enable)
{
	s3c_csis[csis_id]->pktdata_enable = enable;
}

void s3c_csis_start(int csis_id, int lanes, int settle, int align, int width, \
				int height, int pixel_format)
{
	struct platform_device *pdev = NULL;
	struct s3c_platform_csis *pdata = NULL;
	int i;

	memset(&s3c_csis[csis_id]->bufs, 0, sizeof(s3c_csis[csis_id]->bufs));

	/* clock & power on */
	pdev = to_platform_device(s3c_csis[csis_id]->dev);
	pdata = to_csis_plat(&pdev->dev);

	if (pdata->clk_on)
		pdata->clk_on(to_platform_device(s3c_csis[csis_id]->dev),
			&s3c_csis[csis_id]->clock);
	if (pdata->cfg_phy_global)
		pdata->cfg_phy_global(1);

	s3c_csis_reset(pdev);
	s3c_csis_set_nr_lanes(pdev, lanes);

#ifdef CONFIG_MIPI_CSI_ADV_FEATURE
	/* FIXME: how configure the followings with FIMC dynamically? */
	s3c_csis_set_hs_settle(pdev, settle);	/* s5k6aa */
	s3c_csis_set_data_align(pdev, align);
	s3c_csis_set_wclk(pdev, 1);
	if (pixel_format == V4L2_PIX_FMT_JPEG ||
		pixel_format == V4L2_PIX_FMT_INTERLEAVED) {
		s3c_csis_set_format(pdev, MIPI_USER_DEF_PACKET_1);
	} else if (pixel_format == V4L2_PIX_FMT_SGRBG10)
		s3c_csis_set_format(pdev, MIPI_CSI_RAW10);
	else
		s3c_csis_set_format(pdev, MIPI_CSI_YCBCR422_8BIT);
	s3c_csis_set_resol(pdev, width, height);
	s3c_csis_update_shadow(pdev);
#endif

	s3c_csis_enable_interrupt(pdev);
	s3c_csis_system_on(pdev);
	s3c_csis_phy_on(pdev);

	err_print_cnt = 0;
	info("Samsung MIPI-CSIS%d operation started\n", pdev->id);
}

void s3c_csis_stop(int csis_id)
{
	struct platform_device *pdev = NULL;
	struct s3c_platform_csis *pdata = NULL;

	pdev = to_platform_device(s3c_csis[csis_id]->dev);
	pdata = to_csis_plat(&pdev->dev);

	s3c_csis_disable_interrupt(pdev);
	s3c_csis_system_off(pdev);
	s3c_csis_phy_off(pdev);
	s3c_csis[csis_id]->pktdata_enable = 0;

	if (pdata->cfg_phy_global)
		pdata->cfg_phy_global(0);

	if (pdata->clk_off) {
		if (s3c_csis[csis_id]->clock != NULL)
			pdata->clk_off(pdev, &s3c_csis[csis_id]->clock);
	}
}

static irqreturn_t s3c_csis_irq(int irq, void *dev_id)
{
	u32 cfg;

	struct platform_device *pdev = (struct platform_device *) dev_id;
	int bufnum = 0;
	/* just clearing the pends */
	cfg = readl(s3c_csis[pdev->id]->regs + S3C_CSIS_INTSRC);
	writel(cfg, s3c_csis[pdev->id]->regs + S3C_CSIS_INTSRC);
	/* receiving non-image data is not error */
	cfg &= 0xFFFFFFFF;

#ifdef CONFIG_VIDEO_FIMC_MIPI_IRQ_DEBUG
	if (unlikely(cfg & S3C_CSIS_INTSRC_ERR)) {
		if (err_print_cnt < 30) {
			err("csis error interrupt[%d]: %#x\n", err_print_cnt, cfg);
			err_print_cnt++;
		}
	}
#endif
	if(s3c_csis[pdev->id]->pktdata_enable) {
		if (unlikely(cfg & S3C_CSIS_INTSRC_NON_IMAGE_DATA)) {
			/* printk("%s NON Image Data bufnum = %d 0x%x\n", __func__, bufnum, cfg); */

			if (cfg & S3C_CSIS_INTSRC_EVEN_BEFORE) {
				/* printk(KERN_INFO "S3C_CSIS_INTSRC_EVEN_BEFORE\n"); */
				memcpy_fromio(s3c_csis[pdev->id]->bufs.pktdata,
					(s3c_csis[pdev->id]->regs + S3C_CSIS_PKTDATA_EVEN), CSIS_PKTSIZE);
			} else if (cfg & S3C_CSIS_INTSRC_EVEN_AFTER) {
				/* printk(KERN_INFO "S3C_CSIS_INTSRC_EVEN_AFTER\n"); */
				memcpy_fromio(s3c_csis[pdev->id]->bufs.pktdata,
					(s3c_csis[pdev->id]->regs + S3C_CSIS_PKTDATA_EVEN), CSIS_PKTSIZE);
			} else if (cfg & S3C_CSIS_INTSRC_ODD_BEFORE) {
				/* printk(KERN_INFO "S3C_CSIS_INTSRC_ODD_BEFORE\n"); */
				memcpy_fromio(s3c_csis[pdev->id]->bufs.pktdata,
					(s3c_csis[pdev->id]->regs + S3C_CSIS_PKTDATA_ODD), CSIS_PKTSIZE);
			} else if (cfg & S3C_CSIS_INTSRC_ODD_AFTER) {
				/* printk(KERN_INFO "S3C_CSIS_INTSRC_ODD_AFTER\n"); */
				memcpy_fromio(s3c_csis[pdev->id]->bufs.pktdata,
					(s3c_csis[pdev->id]->regs + S3C_CSIS_PKTDATA_ODD), CSIS_PKTSIZE);
			}
			/* printk(KERN_INFO "0x%x\n", s3c_csis[pdev->id]->bufs.pktdata[0x2c/4]); */
			/* printk(KERN_INFO "0x%x\n", s3c_csis[pdev->id]->bufs.pktdata[0x30/4]); */
		}
	}

	return IRQ_HANDLED;
}

static int s3c_csis_probe(struct platform_device *pdev)
{
	struct s3c_platform_csis *pdata;
	struct resource *res;
	int ret = 0;

	ret = s3c_csis_set_info(pdev);

	s3c_csis[pdev->id]->dev = &pdev->dev;

	pdata = to_csis_plat(&pdev->dev);
	if (pdata->cfg_gpio)
		pdata->cfg_gpio();

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		err("failed to get io memory region\n");
		ret = -ENOENT;
		goto err_info;
	}

	s3c_csis[pdev->id]->regs_res = request_mem_region(res->start,
				resource_size(res), pdev->name);
	if (!s3c_csis[pdev->id]->regs_res) {
		err("failed to request io memory region\n");
		ret = -ENOENT;
		goto err_info;
	}

	/* ioremap for register block */
	s3c_csis[pdev->id]->regs = ioremap(res->start, resource_size(res));
	if (!s3c_csis[pdev->id]->regs) {
		err("failed to remap io region\n");
		ret = -ENXIO;
		goto err_req_region;
	}

	/* irq */
	s3c_csis[pdev->id]->irq = platform_get_irq(pdev, 0);
	ret = request_irq(s3c_csis[pdev->id]->irq, s3c_csis_irq, IRQF_DISABLED,
			s3c_csis[pdev->id]->name, pdev);
	if (ret) {
		err("request_irq failed\n");
		goto err_regs_unmap;
	}

	info("Samsung MIPI-CSIS%d driver probed successfully\n", pdev->id);

	return 0;

err_regs_unmap:
	iounmap(s3c_csis[pdev->id]->regs);
err_req_region:
	release_resource(s3c_csis[pdev->id]->regs_res);
	kfree(s3c_csis[pdev->id]->regs_res);
err_info:
	kfree(s3c_csis[pdev->id]);

	return ret;
}

static int s3c_csis_remove(struct platform_device *pdev)
{
	s3c_csis_stop(pdev->id);

	free_irq(s3c_csis[pdev->id]->irq, s3c_csis[pdev->id]);
	iounmap(s3c_csis[pdev->id]->regs);
	release_resource(s3c_csis[pdev->id]->regs_res);

	kfree(s3c_csis[pdev->id]);

	return 0;
}

/* sleep */
int s3c_csis_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct s3c_platform_csis *pdata = NULL;
	pdata = to_csis_plat(&pdev->dev);

	return 0;
}

/* wakeup */
int s3c_csis_resume(struct platform_device *pdev)
{
	struct s3c_platform_csis *pdata = NULL;
	pdata = to_csis_plat(&pdev->dev);

	return 0;
}

static struct platform_driver s3c_csis_driver = {
	.probe		= s3c_csis_probe,
	.remove		= s3c_csis_remove,
	.suspend	= s3c_csis_suspend,
	.resume		= s3c_csis_resume,
	.driver		= {
		.name	= "s3c-csis",
		.owner	= THIS_MODULE,
	},
};

static int s3c_csis_register(void)
{
	return platform_driver_register(&s3c_csis_driver);
}

static void s3c_csis_unregister(void)
{
	platform_driver_unregister(&s3c_csis_driver);
}

module_init(s3c_csis_register);
module_exit(s3c_csis_unregister);

MODULE_AUTHOR("Jinsung, Yang <jsgood.yang@samsung.com>");
MODULE_AUTHOR("Sewoon, Park <seuni.park@samsung.com>");
MODULE_AUTHOR("Sungchun, Kang<sungchun.kang@samsung.com>");
MODULE_DESCRIPTION("MIPI-CSI2 support for FIMC driver");
MODULE_LICENSE("GPL");