1
2
3
4
5
6
7
8
9
10
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/irq.h>
19#include <linux/kernel.h>
20#include <linux/memory.h>
21#include <linux/module.h>
22#include <linux/of.h>
23#include <linux/of_graph.h>
24#include <linux/phy/phy.h>
25#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
27#include <linux/regulator/consumer.h>
28#include <linux/sizes.h>
29#include <linux/slab.h>
30#include <linux/spinlock.h>
31#include <linux/videodev2.h>
32#include <media/drv-intf/exynos-fimc.h>
33#include <media/v4l2-fwnode.h>
34#include <media/v4l2-subdev.h>
35
36#include "mipi-csis.h"
37
38static int debug;
39module_param(debug, int, 0644);
40MODULE_PARM_DESC(debug, "Debug level (0-2)");
41
42
43
44
45#define S5PCSIS_CTRL 0x00
46#define S5PCSIS_CTRL_DPDN_DEFAULT (0 << 31)
47#define S5PCSIS_CTRL_DPDN_SWAP (1 << 31)
48#define S5PCSIS_CTRL_ALIGN_32BIT (1 << 20)
49#define S5PCSIS_CTRL_UPDATE_SHADOW (1 << 16)
50#define S5PCSIS_CTRL_WCLK_EXTCLK (1 << 8)
51#define S5PCSIS_CTRL_RESET (1 << 4)
52#define S5PCSIS_CTRL_ENABLE (1 << 0)
53
54
55#define S5PCSIS_DPHYCTRL 0x04
56#define S5PCSIS_DPHYCTRL_HSS_MASK (0x1f << 27)
57#define S5PCSIS_DPHYCTRL_ENABLE (0x1f << 0)
58
59#define S5PCSIS_CONFIG 0x08
60#define S5PCSIS_CFG_FMT_YCBCR422_8BIT (0x1e << 2)
61#define S5PCSIS_CFG_FMT_RAW8 (0x2a << 2)
62#define S5PCSIS_CFG_FMT_RAW10 (0x2b << 2)
63#define S5PCSIS_CFG_FMT_RAW12 (0x2c << 2)
64
65#define S5PCSIS_CFG_FMT_USER(x) ((0x30 + x - 1) << 2)
66#define S5PCSIS_CFG_FMT_MASK (0x3f << 2)
67#define S5PCSIS_CFG_NR_LANE_MASK 3
68
69
70#define S5PCSIS_INTMSK 0x10
71#define S5PCSIS_INTMSK_EVEN_BEFORE (1 << 31)
72#define S5PCSIS_INTMSK_EVEN_AFTER (1 << 30)
73#define S5PCSIS_INTMSK_ODD_BEFORE (1 << 29)
74#define S5PCSIS_INTMSK_ODD_AFTER (1 << 28)
75#define S5PCSIS_INTMSK_FRAME_START (1 << 27)
76#define S5PCSIS_INTMSK_FRAME_END (1 << 26)
77#define S5PCSIS_INTMSK_ERR_SOT_HS (1 << 12)
78#define S5PCSIS_INTMSK_ERR_LOST_FS (1 << 5)
79#define S5PCSIS_INTMSK_ERR_LOST_FE (1 << 4)
80#define S5PCSIS_INTMSK_ERR_OVER (1 << 3)
81#define S5PCSIS_INTMSK_ERR_ECC (1 << 2)
82#define S5PCSIS_INTMSK_ERR_CRC (1 << 1)
83#define S5PCSIS_INTMSK_ERR_UNKNOWN (1 << 0)
84#define S5PCSIS_INTMSK_EXYNOS4_EN_ALL 0xf000103f
85#define S5PCSIS_INTMSK_EXYNOS5_EN_ALL 0xfc00103f
86
87
88#define S5PCSIS_INTSRC 0x14
89#define S5PCSIS_INTSRC_EVEN_BEFORE (1 << 31)
90#define S5PCSIS_INTSRC_EVEN_AFTER (1 << 30)
91#define S5PCSIS_INTSRC_EVEN (0x3 << 30)
92#define S5PCSIS_INTSRC_ODD_BEFORE (1 << 29)
93#define S5PCSIS_INTSRC_ODD_AFTER (1 << 28)
94#define S5PCSIS_INTSRC_ODD (0x3 << 28)
95#define S5PCSIS_INTSRC_NON_IMAGE_DATA (0xf << 28)
96#define S5PCSIS_INTSRC_FRAME_START (1 << 27)
97#define S5PCSIS_INTSRC_FRAME_END (1 << 26)
98#define S5PCSIS_INTSRC_ERR_SOT_HS (0xf << 12)
99#define S5PCSIS_INTSRC_ERR_LOST_FS (1 << 5)
100#define S5PCSIS_INTSRC_ERR_LOST_FE (1 << 4)
101#define S5PCSIS_INTSRC_ERR_OVER (1 << 3)
102#define S5PCSIS_INTSRC_ERR_ECC (1 << 2)
103#define S5PCSIS_INTSRC_ERR_CRC (1 << 1)
104#define S5PCSIS_INTSRC_ERR_UNKNOWN (1 << 0)
105#define S5PCSIS_INTSRC_ERRORS 0xf03f
106
107
108#define S5PCSIS_RESOL 0x2c
109#define CSIS_MAX_PIX_WIDTH 0xffff
110#define CSIS_MAX_PIX_HEIGHT 0xffff
111
112
113#define S5PCSIS_PKTDATA_ODD 0x2000
114#define S5PCSIS_PKTDATA_EVEN 0x3000
115#define S5PCSIS_PKTDATA_SIZE SZ_4K
116
117enum {
118 CSIS_CLK_MUX,
119 CSIS_CLK_GATE,
120};
121
122static char *csi_clock_name[] = {
123 [CSIS_CLK_MUX] = "sclk_csis",
124 [CSIS_CLK_GATE] = "csis",
125};
126#define NUM_CSIS_CLOCKS ARRAY_SIZE(csi_clock_name)
127#define DEFAULT_SCLK_CSIS_FREQ 166000000UL
128
129static const char * const csis_supply_name[] = {
130 "vddcore",
131 "vddio",
132};
133#define CSIS_NUM_SUPPLIES ARRAY_SIZE(csis_supply_name)
134
135enum {
136 ST_POWERED = 1,
137 ST_STREAMING = 2,
138 ST_SUSPENDED = 4,
139};
140
141struct s5pcsis_event {
142 u32 mask;
143 const char * const name;
144 unsigned int counter;
145};
146
147static const struct s5pcsis_event s5pcsis_events[] = {
148
149 { S5PCSIS_INTSRC_ERR_SOT_HS, "SOT Error" },
150 { S5PCSIS_INTSRC_ERR_LOST_FS, "Lost Frame Start Error" },
151 { S5PCSIS_INTSRC_ERR_LOST_FE, "Lost Frame End Error" },
152 { S5PCSIS_INTSRC_ERR_OVER, "FIFO Overflow Error" },
153 { S5PCSIS_INTSRC_ERR_ECC, "ECC Error" },
154 { S5PCSIS_INTSRC_ERR_CRC, "CRC Error" },
155 { S5PCSIS_INTSRC_ERR_UNKNOWN, "Unknown Error" },
156
157 { S5PCSIS_INTSRC_EVEN_BEFORE, "Non-image data before even frame" },
158 { S5PCSIS_INTSRC_EVEN_AFTER, "Non-image data after even frame" },
159 { S5PCSIS_INTSRC_ODD_BEFORE, "Non-image data before odd frame" },
160 { S5PCSIS_INTSRC_ODD_AFTER, "Non-image data after odd frame" },
161
162 { S5PCSIS_INTSRC_FRAME_START, "Frame Start" },
163 { S5PCSIS_INTSRC_FRAME_END, "Frame End" },
164};
165#define S5PCSIS_NUM_EVENTS ARRAY_SIZE(s5pcsis_events)
166
167struct csis_pktbuf {
168 u32 *data;
169 unsigned int len;
170};
171
172struct csis_drvdata {
173
174 u32 interrupt_mask;
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
203struct csis_state {
204 struct mutex lock;
205 struct media_pad pads[CSIS_PADS_NUM];
206 struct v4l2_subdev sd;
207 u8 index;
208 struct platform_device *pdev;
209 struct phy *phy;
210 void __iomem *regs;
211 struct regulator_bulk_data supplies[CSIS_NUM_SUPPLIES];
212 struct clk *clock[NUM_CSIS_CLOCKS];
213 int irq;
214 u32 interrupt_mask;
215 u32 flags;
216
217 u32 clk_frequency;
218 u32 hs_settle;
219 u32 num_lanes;
220 u32 max_num_lanes;
221 u8 wclk_ext;
222
223 const struct csis_pix_format *csis_fmt;
224 struct v4l2_mbus_framefmt format;
225
226 spinlock_t slock;
227 struct csis_pktbuf pkt_buf;
228 struct s5pcsis_event events[S5PCSIS_NUM_EVENTS];
229};
230
231
232
233
234
235
236
237
238
239struct csis_pix_format {
240 unsigned int pix_width_alignment;
241 u32 code;
242 u32 fmt_reg;
243 u8 data_alignment;
244};
245
246static const struct csis_pix_format s5pcsis_formats[] = {
247 {
248 .code = MEDIA_BUS_FMT_VYUY8_2X8,
249 .fmt_reg = S5PCSIS_CFG_FMT_YCBCR422_8BIT,
250 .data_alignment = 32,
251 }, {
252 .code = MEDIA_BUS_FMT_JPEG_1X8,
253 .fmt_reg = S5PCSIS_CFG_FMT_USER(1),
254 .data_alignment = 32,
255 }, {
256 .code = MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8,
257 .fmt_reg = S5PCSIS_CFG_FMT_USER(1),
258 .data_alignment = 32,
259 }, {
260 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
261 .fmt_reg = S5PCSIS_CFG_FMT_RAW8,
262 .data_alignment = 24,
263 }, {
264 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
265 .fmt_reg = S5PCSIS_CFG_FMT_RAW10,
266 .data_alignment = 24,
267 }, {
268 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
269 .fmt_reg = S5PCSIS_CFG_FMT_RAW12,
270 .data_alignment = 24,
271 }
272};
273
274#define s5pcsis_write(__csis, __r, __v) writel(__v, __csis->regs + __r)
275#define s5pcsis_read(__csis, __r) readl(__csis->regs + __r)
276
277static struct csis_state *sd_to_csis_state(struct v4l2_subdev *sdev)
278{
279 return container_of(sdev, struct csis_state, sd);
280}
281
282static const struct csis_pix_format *find_csis_format(
283 struct v4l2_mbus_framefmt *mf)
284{
285 int i;
286
287 for (i = 0; i < ARRAY_SIZE(s5pcsis_formats); i++)
288 if (mf->code == s5pcsis_formats[i].code)
289 return &s5pcsis_formats[i];
290 return NULL;
291}
292
293static void s5pcsis_enable_interrupts(struct csis_state *state, bool on)
294{
295 u32 val = s5pcsis_read(state, S5PCSIS_INTMSK);
296 if (on)
297 val |= state->interrupt_mask;
298 else
299 val &= ~state->interrupt_mask;
300 s5pcsis_write(state, S5PCSIS_INTMSK, val);
301}
302
303static void s5pcsis_reset(struct csis_state *state)
304{
305 u32 val = s5pcsis_read(state, S5PCSIS_CTRL);
306
307 s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_RESET);
308 udelay(10);
309}
310
311static void s5pcsis_system_enable(struct csis_state *state, int on)
312{
313 u32 val, mask;
314
315 val = s5pcsis_read(state, S5PCSIS_CTRL);
316 if (on)
317 val |= S5PCSIS_CTRL_ENABLE;
318 else
319 val &= ~S5PCSIS_CTRL_ENABLE;
320 s5pcsis_write(state, S5PCSIS_CTRL, val);
321
322 val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
323 val &= ~S5PCSIS_DPHYCTRL_ENABLE;
324 if (on) {
325 mask = (1 << (state->num_lanes + 1)) - 1;
326 val |= (mask & S5PCSIS_DPHYCTRL_ENABLE);
327 }
328 s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
329}
330
331
332static void __s5pcsis_set_format(struct csis_state *state)
333{
334 struct v4l2_mbus_framefmt *mf = &state->format;
335 u32 val;
336
337 v4l2_dbg(1, debug, &state->sd, "fmt: %#x, %d x %d\n",
338 mf->code, mf->width, mf->height);
339
340
341 val = s5pcsis_read(state, S5PCSIS_CONFIG);
342 val = (val & ~S5PCSIS_CFG_FMT_MASK) | state->csis_fmt->fmt_reg;
343 s5pcsis_write(state, S5PCSIS_CONFIG, val);
344
345
346 val = (mf->width << 16) | mf->height;
347 s5pcsis_write(state, S5PCSIS_RESOL, val);
348}
349
350static void s5pcsis_set_hsync_settle(struct csis_state *state, int settle)
351{
352 u32 val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
353
354 val = (val & ~S5PCSIS_DPHYCTRL_HSS_MASK) | (settle << 27);
355 s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
356}
357
358static void s5pcsis_set_params(struct csis_state *state)
359{
360 u32 val;
361
362 val = s5pcsis_read(state, S5PCSIS_CONFIG);
363 val = (val & ~S5PCSIS_CFG_NR_LANE_MASK) | (state->num_lanes - 1);
364 s5pcsis_write(state, S5PCSIS_CONFIG, val);
365
366 __s5pcsis_set_format(state);
367 s5pcsis_set_hsync_settle(state, state->hs_settle);
368
369 val = s5pcsis_read(state, S5PCSIS_CTRL);
370 if (state->csis_fmt->data_alignment == 32)
371 val |= S5PCSIS_CTRL_ALIGN_32BIT;
372 else
373 val &= ~S5PCSIS_CTRL_ALIGN_32BIT;
374
375 val &= ~S5PCSIS_CTRL_WCLK_EXTCLK;
376 if (state->wclk_ext)
377 val |= S5PCSIS_CTRL_WCLK_EXTCLK;
378 s5pcsis_write(state, S5PCSIS_CTRL, val);
379
380
381 val = s5pcsis_read(state, S5PCSIS_CTRL);
382 s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_UPDATE_SHADOW);
383}
384
385static void s5pcsis_clk_put(struct csis_state *state)
386{
387 int i;
388
389 for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
390 if (IS_ERR(state->clock[i]))
391 continue;
392 clk_unprepare(state->clock[i]);
393 clk_put(state->clock[i]);
394 state->clock[i] = ERR_PTR(-EINVAL);
395 }
396}
397
398static int s5pcsis_clk_get(struct csis_state *state)
399{
400 struct device *dev = &state->pdev->dev;
401 int i, ret;
402
403 for (i = 0; i < NUM_CSIS_CLOCKS; i++)
404 state->clock[i] = ERR_PTR(-EINVAL);
405
406 for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
407 state->clock[i] = clk_get(dev, csi_clock_name[i]);
408 if (IS_ERR(state->clock[i])) {
409 ret = PTR_ERR(state->clock[i]);
410 goto err;
411 }
412 ret = clk_prepare(state->clock[i]);
413 if (ret < 0) {
414 clk_put(state->clock[i]);
415 state->clock[i] = ERR_PTR(-EINVAL);
416 goto err;
417 }
418 }
419 return 0;
420err:
421 s5pcsis_clk_put(state);
422 dev_err(dev, "failed to get clock: %s\n", csi_clock_name[i]);
423 return ret;
424}
425
426static void dump_regs(struct csis_state *state, const char *label)
427{
428 struct {
429 u32 offset;
430 const char * const name;
431 } registers[] = {
432 { 0x00, "CTRL" },
433 { 0x04, "DPHYCTRL" },
434 { 0x08, "CONFIG" },
435 { 0x0c, "DPHYSTS" },
436 { 0x10, "INTMSK" },
437 { 0x2c, "RESOL" },
438 { 0x38, "SDW_CONFIG" },
439 };
440 u32 i;
441
442 v4l2_info(&state->sd, "--- %s ---\n", label);
443
444 for (i = 0; i < ARRAY_SIZE(registers); i++) {
445 u32 cfg = s5pcsis_read(state, registers[i].offset);
446 v4l2_info(&state->sd, "%10s: 0x%08x\n", registers[i].name, cfg);
447 }
448}
449
450static void s5pcsis_start_stream(struct csis_state *state)
451{
452 s5pcsis_reset(state);
453 s5pcsis_set_params(state);
454 s5pcsis_system_enable(state, true);
455 s5pcsis_enable_interrupts(state, true);
456}
457
458static void s5pcsis_stop_stream(struct csis_state *state)
459{
460 s5pcsis_enable_interrupts(state, false);
461 s5pcsis_system_enable(state, false);
462}
463
464static void s5pcsis_clear_counters(struct csis_state *state)
465{
466 unsigned long flags;
467 int i;
468
469 spin_lock_irqsave(&state->slock, flags);
470 for (i = 0; i < S5PCSIS_NUM_EVENTS; i++)
471 state->events[i].counter = 0;
472 spin_unlock_irqrestore(&state->slock, flags);
473}
474
475static void s5pcsis_log_counters(struct csis_state *state, bool non_errors)
476{
477 int i = non_errors ? S5PCSIS_NUM_EVENTS : S5PCSIS_NUM_EVENTS - 4;
478 unsigned long flags;
479
480 spin_lock_irqsave(&state->slock, flags);
481
482 for (i--; i >= 0; i--) {
483 if (state->events[i].counter > 0 || debug)
484 v4l2_info(&state->sd, "%s events: %d\n",
485 state->events[i].name,
486 state->events[i].counter);
487 }
488 spin_unlock_irqrestore(&state->slock, flags);
489}
490
491
492
493
494static int s5pcsis_s_power(struct v4l2_subdev *sd, int on)
495{
496 struct csis_state *state = sd_to_csis_state(sd);
497 struct device *dev = &state->pdev->dev;
498
499 if (on)
500 return pm_runtime_get_sync(dev);
501
502 return pm_runtime_put_sync(dev);
503}
504
505static int s5pcsis_s_stream(struct v4l2_subdev *sd, int enable)
506{
507 struct csis_state *state = sd_to_csis_state(sd);
508 int ret = 0;
509
510 v4l2_dbg(1, debug, sd, "%s: %d, state: 0x%x\n",
511 __func__, enable, state->flags);
512
513 if (enable) {
514 s5pcsis_clear_counters(state);
515 ret = pm_runtime_get_sync(&state->pdev->dev);
516 if (ret && ret != 1)
517 return ret;
518 }
519
520 mutex_lock(&state->lock);
521 if (enable) {
522 if (state->flags & ST_SUSPENDED) {
523 ret = -EBUSY;
524 goto unlock;
525 }
526 s5pcsis_start_stream(state);
527 state->flags |= ST_STREAMING;
528 } else {
529 s5pcsis_stop_stream(state);
530 state->flags &= ~ST_STREAMING;
531 if (debug > 0)
532 s5pcsis_log_counters(state, true);
533 }
534unlock:
535 mutex_unlock(&state->lock);
536 if (!enable)
537 pm_runtime_put(&state->pdev->dev);
538
539 return ret == 1 ? 0 : ret;
540}
541
542static int s5pcsis_enum_mbus_code(struct v4l2_subdev *sd,
543 struct v4l2_subdev_pad_config *cfg,
544 struct v4l2_subdev_mbus_code_enum *code)
545{
546 if (code->index >= ARRAY_SIZE(s5pcsis_formats))
547 return -EINVAL;
548
549 code->code = s5pcsis_formats[code->index].code;
550 return 0;
551}
552
553static struct csis_pix_format const *s5pcsis_try_format(
554 struct v4l2_mbus_framefmt *mf)
555{
556 struct csis_pix_format const *csis_fmt;
557
558 csis_fmt = find_csis_format(mf);
559 if (csis_fmt == NULL)
560 csis_fmt = &s5pcsis_formats[0];
561
562 mf->code = csis_fmt->code;
563 v4l_bound_align_image(&mf->width, 1, CSIS_MAX_PIX_WIDTH,
564 csis_fmt->pix_width_alignment,
565 &mf->height, 1, CSIS_MAX_PIX_HEIGHT, 1,
566 0);
567 return csis_fmt;
568}
569
570static struct v4l2_mbus_framefmt *__s5pcsis_get_format(
571 struct csis_state *state, struct v4l2_subdev_pad_config *cfg,
572 enum v4l2_subdev_format_whence which)
573{
574 if (which == V4L2_SUBDEV_FORMAT_TRY)
575 return cfg ? v4l2_subdev_get_try_format(&state->sd, cfg, 0) : NULL;
576
577 return &state->format;
578}
579
580static int s5pcsis_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
581 struct v4l2_subdev_format *fmt)
582{
583 struct csis_state *state = sd_to_csis_state(sd);
584 struct csis_pix_format const *csis_fmt;
585 struct v4l2_mbus_framefmt *mf;
586
587 mf = __s5pcsis_get_format(state, cfg, fmt->which);
588
589 if (fmt->pad == CSIS_PAD_SOURCE) {
590 if (mf) {
591 mutex_lock(&state->lock);
592 fmt->format = *mf;
593 mutex_unlock(&state->lock);
594 }
595 return 0;
596 }
597 csis_fmt = s5pcsis_try_format(&fmt->format);
598 if (mf) {
599 mutex_lock(&state->lock);
600 *mf = fmt->format;
601 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
602 state->csis_fmt = csis_fmt;
603 mutex_unlock(&state->lock);
604 }
605 return 0;
606}
607
608static int s5pcsis_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
609 struct v4l2_subdev_format *fmt)
610{
611 struct csis_state *state = sd_to_csis_state(sd);
612 struct v4l2_mbus_framefmt *mf;
613
614 mf = __s5pcsis_get_format(state, cfg, fmt->which);
615 if (!mf)
616 return -EINVAL;
617
618 mutex_lock(&state->lock);
619 fmt->format = *mf;
620 mutex_unlock(&state->lock);
621 return 0;
622}
623
624static int s5pcsis_s_rx_buffer(struct v4l2_subdev *sd, void *buf,
625 unsigned int *size)
626{
627 struct csis_state *state = sd_to_csis_state(sd);
628 unsigned long flags;
629
630 *size = min_t(unsigned int, *size, S5PCSIS_PKTDATA_SIZE);
631
632 spin_lock_irqsave(&state->slock, flags);
633 state->pkt_buf.data = buf;
634 state->pkt_buf.len = *size;
635 spin_unlock_irqrestore(&state->slock, flags);
636
637 return 0;
638}
639
640static int s5pcsis_log_status(struct v4l2_subdev *sd)
641{
642 struct csis_state *state = sd_to_csis_state(sd);
643
644 mutex_lock(&state->lock);
645 s5pcsis_log_counters(state, true);
646 if (debug && (state->flags & ST_POWERED))
647 dump_regs(state, __func__);
648 mutex_unlock(&state->lock);
649 return 0;
650}
651
652static const struct v4l2_subdev_core_ops s5pcsis_core_ops = {
653 .s_power = s5pcsis_s_power,
654 .log_status = s5pcsis_log_status,
655};
656
657static const struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
658 .enum_mbus_code = s5pcsis_enum_mbus_code,
659 .get_fmt = s5pcsis_get_fmt,
660 .set_fmt = s5pcsis_set_fmt,
661};
662
663static const struct v4l2_subdev_video_ops s5pcsis_video_ops = {
664 .s_rx_buffer = s5pcsis_s_rx_buffer,
665 .s_stream = s5pcsis_s_stream,
666};
667
668static const struct v4l2_subdev_ops s5pcsis_subdev_ops = {
669 .core = &s5pcsis_core_ops,
670 .pad = &s5pcsis_pad_ops,
671 .video = &s5pcsis_video_ops,
672};
673
674static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
675{
676 struct csis_state *state = dev_id;
677 struct csis_pktbuf *pktbuf = &state->pkt_buf;
678 unsigned long flags;
679 u32 status;
680
681 status = s5pcsis_read(state, S5PCSIS_INTSRC);
682 spin_lock_irqsave(&state->slock, flags);
683
684 if ((status & S5PCSIS_INTSRC_NON_IMAGE_DATA) && pktbuf->data) {
685 u32 offset;
686
687 if (status & S5PCSIS_INTSRC_EVEN)
688 offset = S5PCSIS_PKTDATA_EVEN;
689 else
690 offset = S5PCSIS_PKTDATA_ODD;
691
692 memcpy(pktbuf->data, (u8 __force *)state->regs + offset,
693 pktbuf->len);
694 pktbuf->data = NULL;
695 rmb();
696 }
697
698
699 if ((status & S5PCSIS_INTSRC_ERRORS) || debug) {
700 int i;
701 for (i = 0; i < S5PCSIS_NUM_EVENTS; i++) {
702 if (!(status & state->events[i].mask))
703 continue;
704 state->events[i].counter++;
705 v4l2_dbg(2, debug, &state->sd, "%s: %d\n",
706 state->events[i].name,
707 state->events[i].counter);
708 }
709 v4l2_dbg(2, debug, &state->sd, "status: %08x\n", status);
710 }
711 spin_unlock_irqrestore(&state->slock, flags);
712
713 s5pcsis_write(state, S5PCSIS_INTSRC, status);
714 return IRQ_HANDLED;
715}
716
717static int s5pcsis_parse_dt(struct platform_device *pdev,
718 struct csis_state *state)
719{
720 struct device_node *node = pdev->dev.of_node;
721 struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
722 int ret;
723
724 if (of_property_read_u32(node, "clock-frequency",
725 &state->clk_frequency))
726 state->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
727 if (of_property_read_u32(node, "bus-width",
728 &state->max_num_lanes))
729 return -EINVAL;
730
731 node = of_graph_get_next_endpoint(node, NULL);
732 if (!node) {
733 dev_err(&pdev->dev, "No port node at %pOF\n",
734 pdev->dev.of_node);
735 return -EINVAL;
736 }
737
738 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(node), &endpoint);
739 if (ret)
740 goto err;
741
742 state->index = endpoint.base.port - FIMC_INPUT_MIPI_CSI2_0;
743 if (state->index >= CSIS_MAX_ENTITIES) {
744 ret = -ENXIO;
745 goto err;
746 }
747
748
749 of_property_read_u32(node, "samsung,csis-hs-settle",
750 &state->hs_settle);
751 state->wclk_ext = of_property_read_bool(node,
752 "samsung,csis-wclk");
753
754 state->num_lanes = endpoint.bus.mipi_csi2.num_data_lanes;
755
756err:
757 of_node_put(node);
758 return ret;
759}
760
761static int s5pcsis_pm_resume(struct device *dev, bool runtime);
762static const struct of_device_id s5pcsis_of_match[];
763
764static int s5pcsis_probe(struct platform_device *pdev)
765{
766 const struct of_device_id *of_id;
767 const struct csis_drvdata *drv_data;
768 struct device *dev = &pdev->dev;
769 struct resource *mem_res;
770 struct csis_state *state;
771 int ret = -ENOMEM;
772 int i;
773
774 state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
775 if (!state)
776 return -ENOMEM;
777
778 mutex_init(&state->lock);
779 spin_lock_init(&state->slock);
780 state->pdev = pdev;
781
782 of_id = of_match_node(s5pcsis_of_match, dev->of_node);
783 if (WARN_ON(of_id == NULL))
784 return -EINVAL;
785
786 drv_data = of_id->data;
787 state->interrupt_mask = drv_data->interrupt_mask;
788
789 ret = s5pcsis_parse_dt(pdev, state);
790 if (ret < 0)
791 return ret;
792
793 if (state->num_lanes == 0 || state->num_lanes > state->max_num_lanes) {
794 dev_err(dev, "Unsupported number of data lanes: %d (max. %d)\n",
795 state->num_lanes, state->max_num_lanes);
796 return -EINVAL;
797 }
798
799 state->phy = devm_phy_get(dev, "csis");
800 if (IS_ERR(state->phy))
801 return PTR_ERR(state->phy);
802
803 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
804 state->regs = devm_ioremap_resource(dev, mem_res);
805 if (IS_ERR(state->regs))
806 return PTR_ERR(state->regs);
807
808 state->irq = platform_get_irq(pdev, 0);
809 if (state->irq < 0) {
810 dev_err(dev, "Failed to get irq\n");
811 return state->irq;
812 }
813
814 for (i = 0; i < CSIS_NUM_SUPPLIES; i++)
815 state->supplies[i].supply = csis_supply_name[i];
816
817 ret = devm_regulator_bulk_get(dev, CSIS_NUM_SUPPLIES,
818 state->supplies);
819 if (ret)
820 return ret;
821
822 ret = s5pcsis_clk_get(state);
823 if (ret < 0)
824 return ret;
825
826 if (state->clk_frequency)
827 ret = clk_set_rate(state->clock[CSIS_CLK_MUX],
828 state->clk_frequency);
829 else
830 dev_WARN(dev, "No clock frequency specified!\n");
831 if (ret < 0)
832 goto e_clkput;
833
834 ret = clk_enable(state->clock[CSIS_CLK_MUX]);
835 if (ret < 0)
836 goto e_clkput;
837
838 ret = devm_request_irq(dev, state->irq, s5pcsis_irq_handler,
839 0, dev_name(dev), state);
840 if (ret) {
841 dev_err(dev, "Interrupt request failed\n");
842 goto e_clkdis;
843 }
844
845 v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
846 state->sd.owner = THIS_MODULE;
847 snprintf(state->sd.name, sizeof(state->sd.name), "%s.%d",
848 CSIS_SUBDEV_NAME, state->index);
849 state->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
850 state->csis_fmt = &s5pcsis_formats[0];
851
852 state->format.code = s5pcsis_formats[0].code;
853 state->format.width = S5PCSIS_DEF_PIX_WIDTH;
854 state->format.height = S5PCSIS_DEF_PIX_HEIGHT;
855
856 state->sd.entity.function = MEDIA_ENT_F_IO_V4L;
857 state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
858 state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
859 ret = media_entity_pads_init(&state->sd.entity,
860 CSIS_PADS_NUM, state->pads);
861 if (ret < 0)
862 goto e_clkdis;
863
864
865 v4l2_set_subdevdata(&state->sd, pdev);
866
867
868 platform_set_drvdata(pdev, &state->sd);
869 memcpy(state->events, s5pcsis_events, sizeof(state->events));
870
871 pm_runtime_enable(dev);
872 if (!pm_runtime_enabled(dev)) {
873 ret = s5pcsis_pm_resume(dev, true);
874 if (ret < 0)
875 goto e_m_ent;
876 }
877
878 dev_info(&pdev->dev, "lanes: %d, hs_settle: %d, wclk: %d, freq: %u\n",
879 state->num_lanes, state->hs_settle, state->wclk_ext,
880 state->clk_frequency);
881 return 0;
882
883e_m_ent:
884 media_entity_cleanup(&state->sd.entity);
885e_clkdis:
886 clk_disable(state->clock[CSIS_CLK_MUX]);
887e_clkput:
888 s5pcsis_clk_put(state);
889 return ret;
890}
891
892static int s5pcsis_pm_suspend(struct device *dev, bool runtime)
893{
894 struct v4l2_subdev *sd = dev_get_drvdata(dev);
895 struct csis_state *state = sd_to_csis_state(sd);
896 int ret = 0;
897
898 v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
899 __func__, state->flags);
900
901 mutex_lock(&state->lock);
902 if (state->flags & ST_POWERED) {
903 s5pcsis_stop_stream(state);
904 ret = phy_power_off(state->phy);
905 if (ret)
906 goto unlock;
907 ret = regulator_bulk_disable(CSIS_NUM_SUPPLIES,
908 state->supplies);
909 if (ret)
910 goto unlock;
911 clk_disable(state->clock[CSIS_CLK_GATE]);
912 state->flags &= ~ST_POWERED;
913 if (!runtime)
914 state->flags |= ST_SUSPENDED;
915 }
916 unlock:
917 mutex_unlock(&state->lock);
918 return ret ? -EAGAIN : 0;
919}
920
921static int s5pcsis_pm_resume(struct device *dev, bool runtime)
922{
923 struct v4l2_subdev *sd = dev_get_drvdata(dev);
924 struct csis_state *state = sd_to_csis_state(sd);
925 int ret = 0;
926
927 v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
928 __func__, state->flags);
929
930 mutex_lock(&state->lock);
931 if (!runtime && !(state->flags & ST_SUSPENDED))
932 goto unlock;
933
934 if (!(state->flags & ST_POWERED)) {
935 ret = regulator_bulk_enable(CSIS_NUM_SUPPLIES,
936 state->supplies);
937 if (ret)
938 goto unlock;
939 ret = phy_power_on(state->phy);
940 if (!ret) {
941 state->flags |= ST_POWERED;
942 } else {
943 regulator_bulk_disable(CSIS_NUM_SUPPLIES,
944 state->supplies);
945 goto unlock;
946 }
947 clk_enable(state->clock[CSIS_CLK_GATE]);
948 }
949 if (state->flags & ST_STREAMING)
950 s5pcsis_start_stream(state);
951
952 state->flags &= ~ST_SUSPENDED;
953 unlock:
954 mutex_unlock(&state->lock);
955 return ret ? -EAGAIN : 0;
956}
957
958#ifdef CONFIG_PM_SLEEP
959static int s5pcsis_suspend(struct device *dev)
960{
961 return s5pcsis_pm_suspend(dev, false);
962}
963
964static int s5pcsis_resume(struct device *dev)
965{
966 return s5pcsis_pm_resume(dev, false);
967}
968#endif
969
970#ifdef CONFIG_PM
971static int s5pcsis_runtime_suspend(struct device *dev)
972{
973 return s5pcsis_pm_suspend(dev, true);
974}
975
976static int s5pcsis_runtime_resume(struct device *dev)
977{
978 return s5pcsis_pm_resume(dev, true);
979}
980#endif
981
982static int s5pcsis_remove(struct platform_device *pdev)
983{
984 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
985 struct csis_state *state = sd_to_csis_state(sd);
986
987 pm_runtime_disable(&pdev->dev);
988 s5pcsis_pm_suspend(&pdev->dev, true);
989 clk_disable(state->clock[CSIS_CLK_MUX]);
990 pm_runtime_set_suspended(&pdev->dev);
991 s5pcsis_clk_put(state);
992
993 media_entity_cleanup(&state->sd.entity);
994
995 return 0;
996}
997
998static const struct dev_pm_ops s5pcsis_pm_ops = {
999 SET_RUNTIME_PM_OPS(s5pcsis_runtime_suspend, s5pcsis_runtime_resume,
1000 NULL)
1001 SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_suspend, s5pcsis_resume)
1002};
1003
1004static const struct csis_drvdata exynos4_csis_drvdata = {
1005 .interrupt_mask = S5PCSIS_INTMSK_EXYNOS4_EN_ALL,
1006};
1007
1008static const struct csis_drvdata exynos5_csis_drvdata = {
1009 .interrupt_mask = S5PCSIS_INTMSK_EXYNOS5_EN_ALL,
1010};
1011
1012static const struct of_device_id s5pcsis_of_match[] = {
1013 {
1014 .compatible = "samsung,s5pv210-csis",
1015 .data = &exynos4_csis_drvdata,
1016 }, {
1017 .compatible = "samsung,exynos4210-csis",
1018 .data = &exynos4_csis_drvdata,
1019 }, {
1020 .compatible = "samsung,exynos5250-csis",
1021 .data = &exynos5_csis_drvdata,
1022 },
1023 { },
1024};
1025MODULE_DEVICE_TABLE(of, s5pcsis_of_match);
1026
1027static struct platform_driver s5pcsis_driver = {
1028 .probe = s5pcsis_probe,
1029 .remove = s5pcsis_remove,
1030 .driver = {
1031 .of_match_table = s5pcsis_of_match,
1032 .name = CSIS_DRIVER_NAME,
1033 .pm = &s5pcsis_pm_ops,
1034 },
1035};
1036
1037module_platform_driver(s5pcsis_driver);
1038
1039MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1040MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI-CSI2 receiver driver");
1041MODULE_LICENSE("GPL");
1042