14 #include <linux/device.h>
15 #include <linux/errno.h>
19 #include <linux/kernel.h>
21 #include <linux/module.h>
25 #include <linux/slab.h>
27 #include <linux/videodev2.h>
39 #define S5PCSIS_CTRL 0x00
40 #define S5PCSIS_CTRL_DPDN_DEFAULT (0 << 31)
41 #define S5PCSIS_CTRL_DPDN_SWAP (1 << 31)
42 #define S5PCSIS_CTRL_ALIGN_32BIT (1 << 20)
43 #define S5PCSIS_CTRL_UPDATE_SHADOW (1 << 16)
44 #define S5PCSIS_CTRL_WCLK_EXTCLK (1 << 8)
45 #define S5PCSIS_CTRL_RESET (1 << 4)
46 #define S5PCSIS_CTRL_ENABLE (1 << 0)
49 #define S5PCSIS_DPHYCTRL 0x04
50 #define S5PCSIS_DPHYCTRL_HSS_MASK (0x1f << 27)
51 #define S5PCSIS_DPHYCTRL_ENABLE (0x1f << 0)
53 #define S5PCSIS_CONFIG 0x08
54 #define S5PCSIS_CFG_FMT_YCBCR422_8BIT (0x1e << 2)
55 #define S5PCSIS_CFG_FMT_RAW8 (0x2a << 2)
56 #define S5PCSIS_CFG_FMT_RAW10 (0x2b << 2)
57 #define S5PCSIS_CFG_FMT_RAW12 (0x2c << 2)
59 #define S5PCSIS_CFG_FMT_USER(x) ((0x30 + x - 1) << 2)
60 #define S5PCSIS_CFG_FMT_MASK (0x3f << 2)
61 #define S5PCSIS_CFG_NR_LANE_MASK 3
64 #define S5PCSIS_INTMSK 0x10
65 #define S5PCSIS_INTMSK_EN_ALL 0xf000103f
66 #define S5PCSIS_INTMSK_EVEN_BEFORE (1 << 31)
67 #define S5PCSIS_INTMSK_EVEN_AFTER (1 << 30)
68 #define S5PCSIS_INTMSK_ODD_BEFORE (1 << 29)
69 #define S5PCSIS_INTMSK_ODD_AFTER (1 << 28)
70 #define S5PCSIS_INTMSK_ERR_SOT_HS (1 << 12)
71 #define S5PCSIS_INTMSK_ERR_LOST_FS (1 << 5)
72 #define S5PCSIS_INTMSK_ERR_LOST_FE (1 << 4)
73 #define S5PCSIS_INTMSK_ERR_OVER (1 << 3)
74 #define S5PCSIS_INTMSK_ERR_ECC (1 << 2)
75 #define S5PCSIS_INTMSK_ERR_CRC (1 << 1)
76 #define S5PCSIS_INTMSK_ERR_UNKNOWN (1 << 0)
79 #define S5PCSIS_INTSRC 0x14
80 #define S5PCSIS_INTSRC_EVEN_BEFORE (1 << 31)
81 #define S5PCSIS_INTSRC_EVEN_AFTER (1 << 30)
82 #define S5PCSIS_INTSRC_EVEN (0x3 << 30)
83 #define S5PCSIS_INTSRC_ODD_BEFORE (1 << 29)
84 #define S5PCSIS_INTSRC_ODD_AFTER (1 << 28)
85 #define S5PCSIS_INTSRC_ODD (0x3 << 28)
86 #define S5PCSIS_INTSRC_NON_IMAGE_DATA (0xff << 28)
87 #define S5PCSIS_INTSRC_ERR_SOT_HS (0xf << 12)
88 #define S5PCSIS_INTSRC_ERR_LOST_FS (1 << 5)
89 #define S5PCSIS_INTSRC_ERR_LOST_FE (1 << 4)
90 #define S5PCSIS_INTSRC_ERR_OVER (1 << 3)
91 #define S5PCSIS_INTSRC_ERR_ECC (1 << 2)
92 #define S5PCSIS_INTSRC_ERR_CRC (1 << 1)
93 #define S5PCSIS_INTSRC_ERR_UNKNOWN (1 << 0)
94 #define S5PCSIS_INTSRC_ERRORS 0xf03f
97 #define S5PCSIS_RESOL 0x2c
98 #define CSIS_MAX_PIX_WIDTH 0xffff
99 #define CSIS_MAX_PIX_HEIGHT 0xffff
102 #define S5PCSIS_PKTDATA_ODD 0x2000
103 #define S5PCSIS_PKTDATA_EVEN 0x3000
104 #define S5PCSIS_PKTDATA_SIZE SZ_4K
111 static char *csi_clock_name[] = {
115 #define NUM_CSIS_CLOCKS ARRAY_SIZE(csi_clock_name)
117 static const char *
const csis_supply_name[] = {
121 #define CSIS_NUM_SUPPLIES ARRAY_SIZE(csis_supply_name)
150 #define S5PCSIS_NUM_EVENTS ARRAY_SIZE(s5pcsis_events)
203 struct csis_pix_format {
204 unsigned int pix_width_alignment;
210 static const struct csis_pix_format s5pcsis_formats[] = {
214 .data_alignment = 32,
218 .data_alignment = 32,
222 .data_alignment = 32,
226 #define s5pcsis_write(__csis, __r, __v) writel(__v, __csis->regs + __r)
227 #define s5pcsis_read(__csis, __r) readl(__csis->regs + __r)
234 static const struct csis_pix_format *find_csis_format(
235 struct v4l2_mbus_framefmt *mf)
239 for (i = 0; i <
ARRAY_SIZE(s5pcsis_formats); i++)
240 if (mf->code == s5pcsis_formats[i].code)
241 return &s5pcsis_formats[
i];
245 static void s5pcsis_enable_interrupts(
struct csis_state *
state,
bool on)
282 static void __s5pcsis_set_format(
struct csis_state *state)
284 struct v4l2_mbus_framefmt *mf = &state->
format;
288 mf->code, mf->width, mf->height);
296 val = (mf->width << 16) | mf->height;
300 static void s5pcsis_set_hsync_settle(
struct csis_state *state,
int settle)
308 static void s5pcsis_set_params(
struct csis_state *state)
317 __s5pcsis_set_format(state);
318 s5pcsis_set_hsync_settle(state, pdata->
hs_settle);
321 if (state->
csis_fmt->data_alignment == 32)
336 static void s5pcsis_clk_put(
struct csis_state *state)
341 if (IS_ERR_OR_NULL(state->
clock[i]))
349 static int s5pcsis_clk_get(
struct csis_state *state)
356 if (IS_ERR(state->
clock[i]))
367 s5pcsis_clk_put(state);
368 dev_err(dev,
"failed to get clock: %s\n", csi_clock_name[i]);
372 static void s5pcsis_start_stream(
struct csis_state *state)
374 s5pcsis_reset(state);
375 s5pcsis_set_params(state);
376 s5pcsis_system_enable(state,
true);
377 s5pcsis_enable_interrupts(state,
true);
380 static void s5pcsis_stop_stream(
struct csis_state *state)
382 s5pcsis_enable_interrupts(state,
false);
383 s5pcsis_system_enable(state,
false);
386 static void s5pcsis_clear_counters(
struct csis_state *state)
393 state->
events[i].counter = 0;
394 spin_unlock_irqrestore(&state->
slock, flags);
397 static void s5pcsis_log_counters(
struct csis_state *state,
bool non_errors)
399 int i = non_errors ? S5PCSIS_NUM_EVENTS : S5PCSIS_NUM_EVENTS - 4;
404 for (i--; i >= 0; i--)
405 if (state->
events[i].counter >= 0)
408 state->
events[i].counter);
410 spin_unlock_irqrestore(&state->
slock, flags);
418 struct csis_state *state = sd_to_csis_state(sd);
422 return pm_runtime_get_sync(dev);
424 return pm_runtime_put_sync(dev);
429 struct csis_state *state = sd_to_csis_state(sd);
433 __func__, enable, state->
flags);
436 s5pcsis_clear_counters(state);
437 ret = pm_runtime_get_sync(&state->
pdev->dev);
448 s5pcsis_start_stream(state);
451 s5pcsis_stop_stream(state);
454 s5pcsis_log_counters(state,
true);
459 pm_runtime_put(&state->
pdev->dev);
461 return ret == 1 ? 0 :
ret;
464 static int s5pcsis_enum_mbus_code(
struct v4l2_subdev *sd,
466 struct v4l2_subdev_mbus_code_enum *
code)
468 if (code->index >=
ARRAY_SIZE(s5pcsis_formats))
471 code->code = s5pcsis_formats[code->index].code;
475 static struct csis_pix_format
const *s5pcsis_try_format(
476 struct v4l2_mbus_framefmt *mf)
478 struct csis_pix_format
const *csis_fmt;
480 csis_fmt = find_csis_format(mf);
481 if (csis_fmt ==
NULL)
482 csis_fmt = &s5pcsis_formats[0];
484 mf->code = csis_fmt->code;
486 csis_fmt->pix_width_alignment,
492 static struct v4l2_mbus_framefmt *__s5pcsis_get_format(
497 return fh ? v4l2_subdev_get_try_format(fh, pad) :
NULL;
505 struct csis_state *state = sd_to_csis_state(sd);
506 struct csis_pix_format
const *csis_fmt;
507 struct v4l2_mbus_framefmt *mf;
512 mf = __s5pcsis_get_format(state, fh, fmt->
pad, fmt->
which);
522 csis_fmt = s5pcsis_try_format(&fmt->
format);
536 struct csis_state *state = sd_to_csis_state(sd);
537 struct v4l2_mbus_framefmt *mf;
542 mf = __s5pcsis_get_format(state, fh, fmt->
pad, fmt->
which);
555 struct csis_state *state = sd_to_csis_state(sd);
563 spin_unlock_irqrestore(&state->
slock, flags);
568 static int s5pcsis_log_status(
struct v4l2_subdev *sd)
570 struct csis_state *state = sd_to_csis_state(sd);
572 s5pcsis_log_counters(state,
true);
578 struct v4l2_mbus_framefmt *
format = v4l2_subdev_get_try_format(fh, 0);
581 format->code = s5pcsis_formats[0].code;
590 .
open = s5pcsis_open,
595 .log_status = s5pcsis_log_status,
600 .get_fmt = s5pcsis_get_fmt,
601 .set_fmt = s5pcsis_set_fmt,
606 .s_stream = s5pcsis_s_stream,
610 .
core = &s5pcsis_core_ops,
611 .pad = &s5pcsis_pad_ops,
612 .video = &s5pcsis_video_ops,
642 if (!(status & state->
events[i].mask))
647 state->
events[i].counter);
651 spin_unlock_irqrestore(&state->
slock, flags);
675 pdata = pdev->
dev.platform_data;
677 dev_err(&pdev->
dev,
"Platform data not fully specified\n");
683 dev_err(&pdev->
dev,
"Unsupported number of data lanes: %d\n",
691 dev_err(&pdev->
dev,
"Failed to request and remap io memory\n");
696 if (state->
irq < 0) {
702 state->
supplies[i].supply = csis_supply_name[i];
709 ret = s5pcsis_clk_get(state);
717 dev_WARN(&pdev->
dev,
"No clock frequency specified!\n");
719 ret = devm_request_irq(&pdev->
dev, state->
irq, s5pcsis_irq_handler,
720 0, dev_name(&pdev->
dev), state);
722 dev_err(&pdev->
dev,
"Interrupt request failed\n");
728 strlcpy(state->
sd.name, dev_name(&pdev->
dev),
sizeof(state->
sd.name));
730 state->
csis_fmt = &s5pcsis_formats[0];
732 state->
format.code = s5pcsis_formats[0].code;
744 v4l2_set_subdevdata(&state->
sd, pdev);
747 platform_set_drvdata(pdev, &state->
sd);
758 s5pcsis_clk_put(state);
762 static int s5pcsis_pm_suspend(
struct device *dev,
bool runtime)
765 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
766 struct csis_state *state = sd_to_csis_state(sd);
770 __func__, state->
flags);
774 s5pcsis_stop_stream(state);
792 static int s5pcsis_pm_resume(
struct device *dev,
bool runtime)
795 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
796 struct csis_state *state = sd_to_csis_state(sd);
800 __func__, state->
flags);
822 s5pcsis_start_stream(state);
830 #ifdef CONFIG_PM_SLEEP
831 static int s5pcsis_suspend(
struct device *dev)
833 return s5pcsis_pm_suspend(dev,
false);
836 static int s5pcsis_resume(
struct device *dev)
838 return s5pcsis_pm_resume(dev,
false);
842 #ifdef CONFIG_PM_RUNTIME
843 static int s5pcsis_runtime_suspend(
struct device *dev)
845 return s5pcsis_pm_suspend(dev,
true);
848 static int s5pcsis_runtime_resume(
struct device *dev)
850 return s5pcsis_pm_resume(dev,
true);
856 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
857 struct csis_state *state = sd_to_csis_state(sd);
859 pm_runtime_disable(&pdev->
dev);
860 s5pcsis_pm_suspend(&pdev->
dev,
false);
862 pm_runtime_set_suspended(&pdev->
dev);
863 s5pcsis_clk_put(state);
871 static const struct dev_pm_ops s5pcsis_pm_ops = {
878 .
probe = s5pcsis_probe,
883 .pm = &s5pcsis_pm_ops,