1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Support for Medifield PNW Camera Imaging ISP subsystem.
4  *
5  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6  *
7  * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version
11  * 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  *
19  */
20 #ifndef __ATOMISP_INTERNAL_H__
21 #define __ATOMISP_INTERNAL_H__
22 
23 #include "../../include/linux/atomisp_platform.h"
24 #include <linux/firmware.h>
25 #include <linux/kernel.h>
26 #include <linux/pm_qos.h>
27 #include <linux/idr.h>
28 
29 #include <media/media-device.h>
30 #include <media/v4l2-subdev.h>
31 
32 /* ISP2400*/
33 #include "ia_css_types.h"
34 #include "sh_css_legacy.h"
35 
36 #include "atomisp_csi2.h"
37 #include "atomisp_subdev.h"
38 #include "atomisp_tpg.h"
39 #include "atomisp_compat.h"
40 
41 #include "gp_device.h"
42 #include "irq.h"
43 #include <linux/vmalloc.h>
44 
45 #define V4L2_EVENT_FRAME_END          5
46 
47 #define IS_HWREVISION(isp, rev) \
48 	(((isp)->media_dev.hw_revision & ATOMISP_HW_REVISION_MASK) == \
49 	 ((rev) << ATOMISP_HW_REVISION_SHIFT))
50 
51 #define MAX_STREAM_NUM	2
52 
53 #define ATOMISP_PCI_DEVICE_SOC_MASK	0xfff8
54 /* MRFLD with 0x1178: ISP freq can burst to 457MHz */
55 #define ATOMISP_PCI_DEVICE_SOC_MRFLD	0x1178
56 /* MRFLD with 0x1179: max ISP freq limited to 400MHz */
57 #define ATOMISP_PCI_DEVICE_SOC_MRFLD_1179	0x1179
58 /* MRFLD with 0x117a: max ISP freq is 400MHz and max freq at Vmin is 200MHz */
59 #define ATOMISP_PCI_DEVICE_SOC_MRFLD_117A	0x117a
60 #define ATOMISP_PCI_DEVICE_SOC_BYT	0x0f38
61 #define ATOMISP_PCI_DEVICE_SOC_ANN	0x1478
62 #define ATOMISP_PCI_DEVICE_SOC_CHT	0x22b8
63 
64 #define ATOMISP_PCI_REV_MRFLD_A0_MAX	0
65 #define ATOMISP_PCI_REV_BYT_A0_MAX	4
66 
67 #define ATOM_ISP_STEP_WIDTH	2
68 #define ATOM_ISP_STEP_HEIGHT	2
69 
70 #define ATOM_ISP_MIN_WIDTH	4
71 #define ATOM_ISP_MIN_HEIGHT	4
72 #define ATOM_ISP_MAX_WIDTH	UINT_MAX
73 #define ATOM_ISP_MAX_HEIGHT	UINT_MAX
74 
75 /* sub-QCIF resolution */
76 #define ATOM_RESOLUTION_SUBQCIF_WIDTH	128
77 #define ATOM_RESOLUTION_SUBQCIF_HEIGHT	96
78 
79 #define ATOM_ISP_MAX_WIDTH_TMP	1280
80 #define ATOM_ISP_MAX_HEIGHT_TMP	720
81 
82 #define ATOM_ISP_I2C_BUS_1	4
83 #define ATOM_ISP_I2C_BUS_2	5
84 
85 #define ATOM_ISP_POWER_DOWN	0
86 #define ATOM_ISP_POWER_UP	1
87 
88 #define ATOM_ISP_MAX_INPUTS	3
89 
90 #define ATOMISP_SC_TYPE_SIZE	2
91 
92 #define ATOMISP_ISP_TIMEOUT_DURATION		(2 * HZ)
93 #define ATOMISP_EXT_ISP_TIMEOUT_DURATION        (6 * HZ)
94 #define ATOMISP_WDT_KEEP_CURRENT_DELAY          0
95 #define ATOMISP_ISP_MAX_TIMEOUT_COUNT	2
96 #define ATOMISP_CSS_STOP_TIMEOUT_US	200000
97 
98 #define ATOMISP_CSS_Q_DEPTH	3
99 #define ATOMISP_CSS_EVENTS_MAX  16
100 #define ATOMISP_CONT_RAW_FRAMES 15
101 #define ATOMISP_METADATA_QUEUE_DEPTH_FOR_HAL	8
102 #define ATOMISP_S3A_BUF_QUEUE_DEPTH_FOR_HAL	8
103 
104 #define ATOMISP_DELAYED_INIT_NOT_QUEUED	0
105 #define ATOMISP_DELAYED_INIT_QUEUED	1
106 #define ATOMISP_DELAYED_INIT_DONE	2
107 
108 /*
109  * Define how fast CPU should be able to serve ISP interrupts.
110  * The bigger the value, the higher risk that the ISP is not
111  * triggered sufficiently fast for it to process image during
112  * vertical blanking time, increasing risk of dropped frames.
113  * 1000 us is a reasonable value considering that the processing
114  * time is typically ~2000 us.
115  */
116 #define ATOMISP_MAX_ISR_LATENCY	1000
117 
118 /* Add new YUVPP pipe for SOC sensor. */
119 #define ATOMISP_CSS_SUPPORT_YUVPP     1
120 
121 #define ATOMISP_CSS_OUTPUT_SECOND_INDEX     1
122 #define ATOMISP_CSS_OUTPUT_DEFAULT_INDEX    0
123 
124 /*
125  * ATOMISP_SOC_CAMERA
126  * This is to differentiate between ext-isp and soc camera in
127  * Moorefield/Baytrail platform.
128  */
129 #define ATOMISP_SOC_CAMERA(asd)  \
130 	(asd->isp->inputs[asd->input_curr].type == SOC_CAMERA)
131 
132 #define ATOMISP_USE_YUVPP(asd)  \
133 	(ATOMISP_SOC_CAMERA(asd) && ATOMISP_CSS_SUPPORT_YUVPP && \
134 	!asd->copy_mode)
135 
136 #define ATOMISP_DEPTH_SENSOR_STREAMON_COUNT 2
137 
138 #define ATOMISP_DEPTH_DEFAULT_MASTER_SENSOR 0
139 #define ATOMISP_DEPTH_DEFAULT_SLAVE_SENSOR 1
140 
141 /* ISP2401 */
142 #define ATOMISP_ION_DEVICE_FD_OFFSET   16
143 #define ATOMISP_ION_SHARED_FD_MASK     (0xFFFF)
144 #define ATOMISP_ION_DEVICE_FD_MASK     (~ATOMISP_ION_SHARED_FD_MASK)
145 #define ION_FD_UNSET (-1)
146 
147 #define DIV_NEAREST_STEP(n, d, step) \
148 	round_down((2 * (n) + (d) * (step)) / (2 * (d)), (step))
149 
150 struct atomisp_input_subdev {
151 	unsigned int type;
152 	enum atomisp_camera_port port;
153 	struct v4l2_subdev *camera;
154 	struct v4l2_subdev *motor;
155 	struct v4l2_frmsizeenum frame_size;
156 
157 	/*
158 	 * To show this resource is used by
159 	 * which stream, in ISP multiple stream mode
160 	 */
161 	struct atomisp_sub_device *asd;
162 
163 	int sensor_index;
164 };
165 
166 enum atomisp_dfs_mode {
167 	ATOMISP_DFS_MODE_AUTO = 0,
168 	ATOMISP_DFS_MODE_LOW,
169 	ATOMISP_DFS_MODE_MAX,
170 };
171 
172 struct atomisp_regs {
173 	/* PCI config space info */
174 	u16 pcicmdsts;
175 	u32 ispmmadr;
176 	u32 msicap;
177 	u32 msi_addr;
178 	u16 msi_data;
179 	u8 intr;
180 	u32 interrupt_control;
181 	u32 pmcs;
182 	u32 cg_dis;
183 	u32 i_control;
184 
185 	/* I-Unit PHY related info */
186 	u32 csi_rcomp_config;
187 	u32 csi_afe_dly;
188 	u32 csi_control;
189 
190 	/* New for MRFLD */
191 	u32 csi_afe_rcomp_config;
192 	u32 csi_afe_hs_control;
193 	u32 csi_deadline_control;
194 	u32 csi_access_viol;
195 };
196 
197 struct atomisp_sw_contex {
198 	int power_state;
199 	int running_freq;
200 };
201 
202 #define ATOMISP_DEVICE_STREAMING_DISABLED	0
203 #define ATOMISP_DEVICE_STREAMING_ENABLED	1
204 #define ATOMISP_DEVICE_STREAMING_STOPPING	2
205 
206 /*
207  * ci device struct
208  */
209 struct atomisp_device {
210 	struct device *dev;
211 	struct v4l2_device v4l2_dev;
212 	struct media_device media_dev;
213 	struct atomisp_platform_data *pdata;
214 	void *mmu_l1_base;
215 	void __iomem *base;
216 	const struct firmware *firmware;
217 
218 	struct pm_qos_request pm_qos;
219 	s32 max_isr_latency;
220 
221 	/*
222 	 * ISP modules
223 	 * Multiple streams are represents by multiple
224 	 * atomisp_sub_device instances
225 	 */
226 	struct atomisp_sub_device *asd;
227 	/*
228 	 * this will be assigned dyanamically.
229 	 * For Merr/BTY(ISP2400), 2 streams are supported.
230 	 */
231 	unsigned int num_of_streams;
232 
233 	struct atomisp_mipi_csi2_device csi2_port[ATOMISP_CAMERA_NR_PORTS];
234 	struct atomisp_tpg_device tpg;
235 
236 	/* Purpose of mutex is to protect and serialize use of isp data
237 	 * structures and css API calls. */
238 	struct mutex mutex;
239 
240 	unsigned int input_cnt;
241 	struct atomisp_input_subdev inputs[ATOM_ISP_MAX_INPUTS];
242 	struct v4l2_subdev *flash;
243 	struct v4l2_subdev *motor;
244 
245 	struct atomisp_regs saved_regs;
246 	struct atomisp_sw_contex sw_contex;
247 	struct atomisp_css_env css_env;
248 
249 	/* isp timeout status flag */
250 	bool isp_timeout;
251 	bool isp_fatal_error;
252 	struct work_struct assert_recovery_work;
253 
254 	spinlock_t lock; /* Protects asd[i].streaming */
255 
256 	bool need_gfx_throttle;
257 
258 	unsigned int mipi_frame_size;
259 	const struct atomisp_dfs_config *dfs;
260 	unsigned int hpll_freq;
261 
262 	bool css_initialized;
263 };
264 
265 #define v4l2_dev_to_atomisp_device(dev) \
266 	container_of(dev, struct atomisp_device, v4l2_dev)
267 
268 extern struct device *atomisp_dev;
269 
270 #endif /* __ATOMISP_INTERNAL_H__ */
271