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