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