2 * Samsung S5P/EXYNOS SoC series MIPI-CSI receiver driver
4 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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>
18 #include <linux/irq.h>
19 #include <linux/kernel.h>
20 #include <linux/memory.h>
21 #include <linux/module.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-of.h>
34 #include <media/v4l2-subdev.h>
36 #include "mipi-csis.h"
39 module_param(debug, int, 0644);
40 MODULE_PARM_DESC(debug, "Debug level (0-2)");
42 /* Register map definition */
44 /* CSIS global control */
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)
55 #define S5PCSIS_DPHYCTRL 0x04
56 #define S5PCSIS_DPHYCTRL_HSS_MASK (0x1f << 27)
57 #define S5PCSIS_DPHYCTRL_ENABLE (0x1f << 0)
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 /* User defined formats, x = 1...4 */
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
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
87 /* Interrupt source */
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
107 /* Pixel resolution */
108 #define S5PCSIS_RESOL 0x2c
109 #define CSIS_MAX_PIX_WIDTH 0xffff
110 #define CSIS_MAX_PIX_HEIGHT 0xffff
112 /* Non-image packet data buffers */
113 #define S5PCSIS_PKTDATA_ODD 0x2000
114 #define S5PCSIS_PKTDATA_EVEN 0x3000
115 #define S5PCSIS_PKTDATA_SIZE SZ_4K
122 static char *csi_clock_name[] = {
123 [CSIS_CLK_MUX] = "sclk_csis",
124 [CSIS_CLK_GATE] = "csis",
126 #define NUM_CSIS_CLOCKS ARRAY_SIZE(csi_clock_name)
127 #define DEFAULT_SCLK_CSIS_FREQ 166000000UL
129 static const char * const csis_supply_name[] = {
130 "vddcore", /* CSIS Core (1.0V, 1.1V or 1.2V) suppply */
131 "vddio", /* CSIS I/O and PLL (1.8V) supply */
133 #define CSIS_NUM_SUPPLIES ARRAY_SIZE(csis_supply_name)
141 struct s5pcsis_event {
143 const char * const name;
144 unsigned int counter;
147 static const struct s5pcsis_event s5pcsis_events[] = {
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 /* Non-image data receive events */
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 /* Frame start/end */
162 { S5PCSIS_INTSRC_FRAME_START, "Frame Start" },
163 { S5PCSIS_INTSRC_FRAME_END, "Frame End" },
165 #define S5PCSIS_NUM_EVENTS ARRAY_SIZE(s5pcsis_events)
172 struct csis_drvdata {
173 /* Mask of all used interrupts in S5PCSIS_INTMSK register */
178 * struct csis_state - the driver's internal state data structure
179 * @lock: mutex serializing the subdev and power management operations,
180 * protecting @format and @flags members
181 * @pads: CSIS pads array
182 * @sd: v4l2_subdev associated with CSIS device instance
183 * @index: the hardware instance index
184 * @pdev: CSIS platform device
185 * @phy: pointer to the CSIS generic PHY
186 * @regs: mmaped I/O registers memory
187 * @supplies: CSIS regulator supplies
188 * @clock: CSIS clocks
189 * @irq: requested s5p-mipi-csis irq number
190 * @interrupt_mask: interrupt mask of the all used interrupts
191 * @flags: the state variable for power and streaming control
192 * @clock_frequency: device bus clock frequency
193 * @hs_settle: HS-RX settle time
194 * @num_lanes: number of MIPI-CSI data lanes used
195 * @max_num_lanes: maximum number of MIPI-CSI data lanes supported
196 * @wclk_ext: CSI wrapper clock: 0 - bus clock, 1 - external SCLK_CAM
197 * @csis_fmt: current CSIS pixel format
198 * @format: common media bus format for the source and sink pad
199 * @slock: spinlock protecting structure members below
200 * @pkt_buf: the frame embedded (non-image) data buffer
201 * @events: MIPI-CSIS event (error) counters
205 struct media_pad pads[CSIS_PADS_NUM];
206 struct v4l2_subdev sd;
208 struct platform_device *pdev;
211 struct regulator_bulk_data supplies[CSIS_NUM_SUPPLIES];
212 struct clk *clock[NUM_CSIS_CLOCKS];
223 const struct csis_pix_format *csis_fmt;
224 struct v4l2_mbus_framefmt format;
227 struct csis_pktbuf pkt_buf;
228 struct s5pcsis_event events[S5PCSIS_NUM_EVENTS];
232 * struct csis_pix_format - CSIS pixel format description
233 * @pix_width_alignment: horizontal pixel alignment, width will be
234 * multiple of 2^pix_width_alignment
235 * @code: corresponding media bus code
236 * @fmt_reg: S5PCSIS_CONFIG register value
237 * @data_alignment: MIPI-CSI data alignment in bits
239 struct csis_pix_format {
240 unsigned int pix_width_alignment;
246 static const struct csis_pix_format s5pcsis_formats[] = {
248 .code = MEDIA_BUS_FMT_VYUY8_2X8,
249 .fmt_reg = S5PCSIS_CFG_FMT_YCBCR422_8BIT,
250 .data_alignment = 32,
252 .code = MEDIA_BUS_FMT_JPEG_1X8,
253 .fmt_reg = S5PCSIS_CFG_FMT_USER(1),
254 .data_alignment = 32,
256 .code = MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8,
257 .fmt_reg = S5PCSIS_CFG_FMT_USER(1),
258 .data_alignment = 32,
260 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
261 .fmt_reg = S5PCSIS_CFG_FMT_RAW8,
262 .data_alignment = 24,
264 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
265 .fmt_reg = S5PCSIS_CFG_FMT_RAW10,
266 .data_alignment = 24,
268 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
269 .fmt_reg = S5PCSIS_CFG_FMT_RAW12,
270 .data_alignment = 24,
274 #define s5pcsis_write(__csis, __r, __v) writel(__v, __csis->regs + __r)
275 #define s5pcsis_read(__csis, __r) readl(__csis->regs + __r)
277 static struct csis_state *sd_to_csis_state(struct v4l2_subdev *sdev)
279 return container_of(sdev, struct csis_state, sd);
282 static const struct csis_pix_format *find_csis_format(
283 struct v4l2_mbus_framefmt *mf)
287 for (i = 0; i < ARRAY_SIZE(s5pcsis_formats); i++)
288 if (mf->code == s5pcsis_formats[i].code)
289 return &s5pcsis_formats[i];
293 static void s5pcsis_enable_interrupts(struct csis_state *state, bool on)
295 u32 val = s5pcsis_read(state, S5PCSIS_INTMSK);
297 val |= state->interrupt_mask;
299 val &= ~state->interrupt_mask;
300 s5pcsis_write(state, S5PCSIS_INTMSK, val);
303 static void s5pcsis_reset(struct csis_state *state)
305 u32 val = s5pcsis_read(state, S5PCSIS_CTRL);
307 s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_RESET);
311 static void s5pcsis_system_enable(struct csis_state *state, int on)
315 val = s5pcsis_read(state, S5PCSIS_CTRL);
317 val |= S5PCSIS_CTRL_ENABLE;
319 val &= ~S5PCSIS_CTRL_ENABLE;
320 s5pcsis_write(state, S5PCSIS_CTRL, val);
322 val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
323 val &= ~S5PCSIS_DPHYCTRL_ENABLE;
325 mask = (1 << (state->num_lanes + 1)) - 1;
326 val |= (mask & S5PCSIS_DPHYCTRL_ENABLE);
328 s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
331 /* Called with the state.lock mutex held */
332 static void __s5pcsis_set_format(struct csis_state *state)
334 struct v4l2_mbus_framefmt *mf = &state->format;
337 v4l2_dbg(1, debug, &state->sd, "fmt: %#x, %d x %d\n",
338 mf->code, mf->width, mf->height);
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);
345 /* Pixel resolution */
346 val = (mf->width << 16) | mf->height;
347 s5pcsis_write(state, S5PCSIS_RESOL, val);
350 static void s5pcsis_set_hsync_settle(struct csis_state *state, int settle)
352 u32 val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
354 val = (val & ~S5PCSIS_DPHYCTRL_HSS_MASK) | (settle << 27);
355 s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
358 static void s5pcsis_set_params(struct csis_state *state)
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);
366 __s5pcsis_set_format(state);
367 s5pcsis_set_hsync_settle(state, state->hs_settle);
369 val = s5pcsis_read(state, S5PCSIS_CTRL);
370 if (state->csis_fmt->data_alignment == 32)
371 val |= S5PCSIS_CTRL_ALIGN_32BIT;
373 val &= ~S5PCSIS_CTRL_ALIGN_32BIT;
375 val &= ~S5PCSIS_CTRL_WCLK_EXTCLK;
377 val |= S5PCSIS_CTRL_WCLK_EXTCLK;
378 s5pcsis_write(state, S5PCSIS_CTRL, val);
380 /* Update the shadow register. */
381 val = s5pcsis_read(state, S5PCSIS_CTRL);
382 s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_UPDATE_SHADOW);
385 static void s5pcsis_clk_put(struct csis_state *state)
389 for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
390 if (IS_ERR(state->clock[i]))
392 clk_unprepare(state->clock[i]);
393 clk_put(state->clock[i]);
394 state->clock[i] = ERR_PTR(-EINVAL);
398 static int s5pcsis_clk_get(struct csis_state *state)
400 struct device *dev = &state->pdev->dev;
403 for (i = 0; i < NUM_CSIS_CLOCKS; i++)
404 state->clock[i] = ERR_PTR(-EINVAL);
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]);
412 ret = clk_prepare(state->clock[i]);
414 clk_put(state->clock[i]);
415 state->clock[i] = ERR_PTR(-EINVAL);
421 s5pcsis_clk_put(state);
422 dev_err(dev, "failed to get clock: %s\n", csi_clock_name[i]);
426 static void dump_regs(struct csis_state *state, const char *label)
430 const char * const name;
433 { 0x04, "DPHYCTRL" },
438 { 0x38, "SDW_CONFIG" },
442 v4l2_info(&state->sd, "--- %s ---\n", label);
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);
450 static void s5pcsis_start_stream(struct csis_state *state)
452 s5pcsis_reset(state);
453 s5pcsis_set_params(state);
454 s5pcsis_system_enable(state, true);
455 s5pcsis_enable_interrupts(state, true);
458 static void s5pcsis_stop_stream(struct csis_state *state)
460 s5pcsis_enable_interrupts(state, false);
461 s5pcsis_system_enable(state, false);
464 static void s5pcsis_clear_counters(struct csis_state *state)
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);
475 static void s5pcsis_log_counters(struct csis_state *state, bool non_errors)
477 int i = non_errors ? S5PCSIS_NUM_EVENTS : S5PCSIS_NUM_EVENTS - 4;
480 spin_lock_irqsave(&state->slock, flags);
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);
488 spin_unlock_irqrestore(&state->slock, flags);
492 * V4L2 subdev operations
494 static int s5pcsis_s_power(struct v4l2_subdev *sd, int on)
496 struct csis_state *state = sd_to_csis_state(sd);
497 struct device *dev = &state->pdev->dev;
500 return pm_runtime_get_sync(dev);
502 return pm_runtime_put_sync(dev);
505 static int s5pcsis_s_stream(struct v4l2_subdev *sd, int enable)
507 struct csis_state *state = sd_to_csis_state(sd);
510 v4l2_dbg(1, debug, sd, "%s: %d, state: 0x%x\n",
511 __func__, enable, state->flags);
514 s5pcsis_clear_counters(state);
515 ret = pm_runtime_get_sync(&state->pdev->dev);
516 if (ret && ret != 1) {
517 pm_runtime_put_noidle(&state->pdev->dev);
522 mutex_lock(&state->lock);
524 if (state->flags & ST_SUSPENDED) {
528 s5pcsis_start_stream(state);
529 state->flags |= ST_STREAMING;
531 s5pcsis_stop_stream(state);
532 state->flags &= ~ST_STREAMING;
534 s5pcsis_log_counters(state, true);
537 mutex_unlock(&state->lock);
539 pm_runtime_put(&state->pdev->dev);
541 return ret == 1 ? 0 : ret;
544 static int s5pcsis_enum_mbus_code(struct v4l2_subdev *sd,
545 struct v4l2_subdev_pad_config *cfg,
546 struct v4l2_subdev_mbus_code_enum *code)
548 if (code->index >= ARRAY_SIZE(s5pcsis_formats))
551 code->code = s5pcsis_formats[code->index].code;
555 static struct csis_pix_format const *s5pcsis_try_format(
556 struct v4l2_mbus_framefmt *mf)
558 struct csis_pix_format const *csis_fmt;
560 csis_fmt = find_csis_format(mf);
561 if (csis_fmt == NULL)
562 csis_fmt = &s5pcsis_formats[0];
564 mf->code = csis_fmt->code;
565 v4l_bound_align_image(&mf->width, 1, CSIS_MAX_PIX_WIDTH,
566 csis_fmt->pix_width_alignment,
567 &mf->height, 1, CSIS_MAX_PIX_HEIGHT, 1,
572 static struct v4l2_mbus_framefmt *__s5pcsis_get_format(
573 struct csis_state *state, struct v4l2_subdev_pad_config *cfg,
574 enum v4l2_subdev_format_whence which)
576 if (which == V4L2_SUBDEV_FORMAT_TRY)
577 return cfg ? v4l2_subdev_get_try_format(&state->sd, cfg, 0) : NULL;
579 return &state->format;
582 static int s5pcsis_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
583 struct v4l2_subdev_format *fmt)
585 struct csis_state *state = sd_to_csis_state(sd);
586 struct csis_pix_format const *csis_fmt;
587 struct v4l2_mbus_framefmt *mf;
589 mf = __s5pcsis_get_format(state, cfg, fmt->which);
591 if (fmt->pad == CSIS_PAD_SOURCE) {
593 mutex_lock(&state->lock);
595 mutex_unlock(&state->lock);
599 csis_fmt = s5pcsis_try_format(&fmt->format);
601 mutex_lock(&state->lock);
603 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
604 state->csis_fmt = csis_fmt;
605 mutex_unlock(&state->lock);
610 static int s5pcsis_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
611 struct v4l2_subdev_format *fmt)
613 struct csis_state *state = sd_to_csis_state(sd);
614 struct v4l2_mbus_framefmt *mf;
616 mf = __s5pcsis_get_format(state, cfg, fmt->which);
620 mutex_lock(&state->lock);
622 mutex_unlock(&state->lock);
626 static int s5pcsis_s_rx_buffer(struct v4l2_subdev *sd, void *buf,
629 struct csis_state *state = sd_to_csis_state(sd);
632 *size = min_t(unsigned int, *size, S5PCSIS_PKTDATA_SIZE);
634 spin_lock_irqsave(&state->slock, flags);
635 state->pkt_buf.data = buf;
636 state->pkt_buf.len = *size;
637 spin_unlock_irqrestore(&state->slock, flags);
642 static int s5pcsis_log_status(struct v4l2_subdev *sd)
644 struct csis_state *state = sd_to_csis_state(sd);
646 mutex_lock(&state->lock);
647 s5pcsis_log_counters(state, true);
648 if (debug && (state->flags & ST_POWERED))
649 dump_regs(state, __func__);
650 mutex_unlock(&state->lock);
654 static struct v4l2_subdev_core_ops s5pcsis_core_ops = {
655 .s_power = s5pcsis_s_power,
656 .log_status = s5pcsis_log_status,
659 static struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
660 .enum_mbus_code = s5pcsis_enum_mbus_code,
661 .get_fmt = s5pcsis_get_fmt,
662 .set_fmt = s5pcsis_set_fmt,
665 static struct v4l2_subdev_video_ops s5pcsis_video_ops = {
666 .s_rx_buffer = s5pcsis_s_rx_buffer,
667 .s_stream = s5pcsis_s_stream,
670 static struct v4l2_subdev_ops s5pcsis_subdev_ops = {
671 .core = &s5pcsis_core_ops,
672 .pad = &s5pcsis_pad_ops,
673 .video = &s5pcsis_video_ops,
676 static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
678 struct csis_state *state = dev_id;
679 struct csis_pktbuf *pktbuf = &state->pkt_buf;
683 status = s5pcsis_read(state, S5PCSIS_INTSRC);
684 spin_lock_irqsave(&state->slock, flags);
686 if ((status & S5PCSIS_INTSRC_NON_IMAGE_DATA) && pktbuf->data) {
689 if (status & S5PCSIS_INTSRC_EVEN)
690 offset = S5PCSIS_PKTDATA_EVEN;
692 offset = S5PCSIS_PKTDATA_ODD;
694 memcpy(pktbuf->data, (u8 __force *)state->regs + offset,
700 /* Update the event/error counters */
701 if ((status & S5PCSIS_INTSRC_ERRORS) || debug) {
703 for (i = 0; i < S5PCSIS_NUM_EVENTS; i++) {
704 if (!(status & state->events[i].mask))
706 state->events[i].counter++;
707 v4l2_dbg(2, debug, &state->sd, "%s: %d\n",
708 state->events[i].name,
709 state->events[i].counter);
711 v4l2_dbg(2, debug, &state->sd, "status: %08x\n", status);
713 spin_unlock_irqrestore(&state->slock, flags);
715 s5pcsis_write(state, S5PCSIS_INTSRC, status);
719 static int s5pcsis_parse_dt(struct platform_device *pdev,
720 struct csis_state *state)
722 struct device_node *node = pdev->dev.of_node;
723 struct v4l2_of_endpoint endpoint;
726 if (of_property_read_u32(node, "clock-frequency",
727 &state->clk_frequency))
728 state->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
729 if (of_property_read_u32(node, "bus-width",
730 &state->max_num_lanes))
733 node = of_graph_get_next_endpoint(node, NULL);
735 dev_err(&pdev->dev, "No port node at %s\n",
736 pdev->dev.of_node->full_name);
739 /* Get port node and validate MIPI-CSI channel id. */
740 ret = v4l2_of_parse_endpoint(node, &endpoint);
744 state->index = endpoint.base.port - FIMC_INPUT_MIPI_CSI2_0;
745 if (state->index >= CSIS_MAX_ENTITIES) {
750 /* Get MIPI CSI-2 bus configration from the endpoint node. */
751 of_property_read_u32(node, "samsung,csis-hs-settle",
753 state->wclk_ext = of_property_read_bool(node,
754 "samsung,csis-wclk");
756 state->num_lanes = endpoint.bus.mipi_csi2.num_data_lanes;
763 static int s5pcsis_pm_resume(struct device *dev, bool runtime);
764 static const struct of_device_id s5pcsis_of_match[];
766 static int s5pcsis_probe(struct platform_device *pdev)
768 const struct of_device_id *of_id;
769 const struct csis_drvdata *drv_data;
770 struct device *dev = &pdev->dev;
771 struct resource *mem_res;
772 struct csis_state *state;
776 state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
780 mutex_init(&state->lock);
781 spin_lock_init(&state->slock);
784 of_id = of_match_node(s5pcsis_of_match, dev->of_node);
785 if (WARN_ON(of_id == NULL))
788 drv_data = of_id->data;
789 state->interrupt_mask = drv_data->interrupt_mask;
791 ret = s5pcsis_parse_dt(pdev, state);
795 if (state->num_lanes == 0 || state->num_lanes > state->max_num_lanes) {
796 dev_err(dev, "Unsupported number of data lanes: %d (max. %d)\n",
797 state->num_lanes, state->max_num_lanes);
801 state->phy = devm_phy_get(dev, "csis");
802 if (IS_ERR(state->phy))
803 return PTR_ERR(state->phy);
805 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
806 state->regs = devm_ioremap_resource(dev, mem_res);
807 if (IS_ERR(state->regs))
808 return PTR_ERR(state->regs);
810 state->irq = platform_get_irq(pdev, 0);
811 if (state->irq < 0) {
812 dev_err(dev, "Failed to get irq\n");
816 for (i = 0; i < CSIS_NUM_SUPPLIES; i++)
817 state->supplies[i].supply = csis_supply_name[i];
819 ret = devm_regulator_bulk_get(dev, CSIS_NUM_SUPPLIES,
824 ret = s5pcsis_clk_get(state);
828 if (state->clk_frequency)
829 ret = clk_set_rate(state->clock[CSIS_CLK_MUX],
830 state->clk_frequency);
832 dev_WARN(dev, "No clock frequency specified!\n");
836 ret = clk_enable(state->clock[CSIS_CLK_MUX]);
840 ret = devm_request_irq(dev, state->irq, s5pcsis_irq_handler,
841 0, dev_name(dev), state);
843 dev_err(dev, "Interrupt request failed\n");
847 v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
848 state->sd.owner = THIS_MODULE;
849 snprintf(state->sd.name, sizeof(state->sd.name), "%s.%d",
850 CSIS_SUBDEV_NAME, state->index);
851 state->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
852 state->csis_fmt = &s5pcsis_formats[0];
854 state->format.code = s5pcsis_formats[0].code;
855 state->format.width = S5PCSIS_DEF_PIX_WIDTH;
856 state->format.height = S5PCSIS_DEF_PIX_HEIGHT;
858 state->sd.entity.function = MEDIA_ENT_F_IO_V4L;
859 state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
860 state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
861 ret = media_entity_pads_init(&state->sd.entity,
862 CSIS_PADS_NUM, state->pads);
866 /* This allows to retrieve the platform device id by the host driver */
867 v4l2_set_subdevdata(&state->sd, pdev);
869 /* .. and a pointer to the subdev. */
870 platform_set_drvdata(pdev, &state->sd);
871 memcpy(state->events, s5pcsis_events, sizeof(state->events));
873 pm_runtime_enable(dev);
874 if (!pm_runtime_enabled(dev)) {
875 ret = s5pcsis_pm_resume(dev, true);
880 dev_info(&pdev->dev, "lanes: %d, hs_settle: %d, wclk: %d, freq: %u\n",
881 state->num_lanes, state->hs_settle, state->wclk_ext,
882 state->clk_frequency);
886 media_entity_cleanup(&state->sd.entity);
888 clk_disable(state->clock[CSIS_CLK_MUX]);
890 s5pcsis_clk_put(state);
894 static int s5pcsis_pm_suspend(struct device *dev, bool runtime)
896 struct platform_device *pdev = to_platform_device(dev);
897 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
898 struct csis_state *state = sd_to_csis_state(sd);
901 v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
902 __func__, state->flags);
904 mutex_lock(&state->lock);
905 if (state->flags & ST_POWERED) {
906 s5pcsis_stop_stream(state);
907 ret = phy_power_off(state->phy);
910 ret = regulator_bulk_disable(CSIS_NUM_SUPPLIES,
914 clk_disable(state->clock[CSIS_CLK_GATE]);
915 state->flags &= ~ST_POWERED;
917 state->flags |= ST_SUSPENDED;
920 mutex_unlock(&state->lock);
921 return ret ? -EAGAIN : 0;
924 static int s5pcsis_pm_resume(struct device *dev, bool runtime)
926 struct platform_device *pdev = to_platform_device(dev);
927 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
928 struct csis_state *state = sd_to_csis_state(sd);
931 v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
932 __func__, state->flags);
934 mutex_lock(&state->lock);
935 if (!runtime && !(state->flags & ST_SUSPENDED))
938 if (!(state->flags & ST_POWERED)) {
939 ret = regulator_bulk_enable(CSIS_NUM_SUPPLIES,
943 ret = phy_power_on(state->phy);
945 state->flags |= ST_POWERED;
947 regulator_bulk_disable(CSIS_NUM_SUPPLIES,
951 clk_enable(state->clock[CSIS_CLK_GATE]);
953 if (state->flags & ST_STREAMING)
954 s5pcsis_start_stream(state);
956 state->flags &= ~ST_SUSPENDED;
958 mutex_unlock(&state->lock);
959 return ret ? -EAGAIN : 0;
962 #ifdef CONFIG_PM_SLEEP
963 static int s5pcsis_suspend(struct device *dev)
965 return s5pcsis_pm_suspend(dev, false);
968 static int s5pcsis_resume(struct device *dev)
970 return s5pcsis_pm_resume(dev, false);
975 static int s5pcsis_runtime_suspend(struct device *dev)
977 return s5pcsis_pm_suspend(dev, true);
980 static int s5pcsis_runtime_resume(struct device *dev)
982 return s5pcsis_pm_resume(dev, true);
986 static int s5pcsis_remove(struct platform_device *pdev)
988 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
989 struct csis_state *state = sd_to_csis_state(sd);
991 pm_runtime_disable(&pdev->dev);
992 s5pcsis_pm_suspend(&pdev->dev, true);
993 clk_disable(state->clock[CSIS_CLK_MUX]);
994 pm_runtime_set_suspended(&pdev->dev);
995 s5pcsis_clk_put(state);
997 media_entity_cleanup(&state->sd.entity);
1002 static const struct dev_pm_ops s5pcsis_pm_ops = {
1003 SET_RUNTIME_PM_OPS(s5pcsis_runtime_suspend, s5pcsis_runtime_resume,
1005 SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_suspend, s5pcsis_resume)
1008 static const struct csis_drvdata exynos4_csis_drvdata = {
1009 .interrupt_mask = S5PCSIS_INTMSK_EXYNOS4_EN_ALL,
1012 static const struct csis_drvdata exynos5_csis_drvdata = {
1013 .interrupt_mask = S5PCSIS_INTMSK_EXYNOS5_EN_ALL,
1016 static const struct of_device_id s5pcsis_of_match[] = {
1018 .compatible = "samsung,s5pv210-csis",
1019 .data = &exynos4_csis_drvdata,
1021 .compatible = "samsung,exynos4210-csis",
1022 .data = &exynos4_csis_drvdata,
1024 .compatible = "samsung,exynos5250-csis",
1025 .data = &exynos5_csis_drvdata,
1029 MODULE_DEVICE_TABLE(of, s5pcsis_of_match);
1031 static struct platform_driver s5pcsis_driver = {
1032 .probe = s5pcsis_probe,
1033 .remove = s5pcsis_remove,
1035 .of_match_table = s5pcsis_of_match,
1036 .name = CSIS_DRIVER_NAME,
1037 .pm = &s5pcsis_pm_ops,
1041 module_platform_driver(s5pcsis_driver);
1043 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1044 MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI-CSI2 receiver driver");
1045 MODULE_LICENSE("GPL");