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 ATOMISP_PCI_DEVICE_SOC_MASK	0xfff8
52 /* MRFLD with 0x1178: ISP freq can burst to 457MHz */
53 #define ATOMISP_PCI_DEVICE_SOC_MRFLD	0x1178
54 /* MRFLD with 0x1179: max ISP freq limited to 400MHz */
55 #define ATOMISP_PCI_DEVICE_SOC_MRFLD_1179	0x1179
56 /* MRFLD with 0x117a: max ISP freq is 400MHz and max freq at Vmin is 200MHz */
57 #define ATOMISP_PCI_DEVICE_SOC_MRFLD_117A	0x117a
58 #define ATOMISP_PCI_DEVICE_SOC_BYT	0x0f38
59 #define ATOMISP_PCI_DEVICE_SOC_ANN	0x1478
60 #define ATOMISP_PCI_DEVICE_SOC_CHT	0x22b8
61 
62 #define ATOMISP_PCI_REV_MRFLD_A0_MAX	0
63 #define ATOMISP_PCI_REV_BYT_A0_MAX	4
64 
65 #define ATOM_ISP_STEP_WIDTH	2
66 #define ATOM_ISP_STEP_HEIGHT	2
67 
68 #define ATOM_ISP_MIN_WIDTH	4
69 #define ATOM_ISP_MIN_HEIGHT	4
70 #define ATOM_ISP_MAX_WIDTH	UINT_MAX
71 #define ATOM_ISP_MAX_HEIGHT	UINT_MAX
72 
73 /* sub-QCIF resolution */
74 #define ATOM_RESOLUTION_SUBQCIF_WIDTH	128
75 #define ATOM_RESOLUTION_SUBQCIF_HEIGHT	96
76 
77 #define ATOM_ISP_I2C_BUS_1	4
78 #define ATOM_ISP_I2C_BUS_2	5
79 
80 #define ATOM_ISP_POWER_DOWN	0
81 #define ATOM_ISP_POWER_UP	1
82 
83 #define ATOM_ISP_MAX_INPUTS	3
84 
85 #define ATOMISP_SC_TYPE_SIZE	2
86 
87 #define ATOMISP_ISP_TIMEOUT_DURATION		(2 * HZ)
88 #define ATOMISP_EXT_ISP_TIMEOUT_DURATION        (6 * HZ)
89 #define ATOMISP_WDT_KEEP_CURRENT_DELAY          0
90 #define ATOMISP_ISP_MAX_TIMEOUT_COUNT	2
91 #define ATOMISP_CSS_STOP_TIMEOUT_US	200000
92 
93 #define ATOMISP_CSS_Q_DEPTH	3
94 #define ATOMISP_CSS_EVENTS_MAX  16
95 #define ATOMISP_CONT_RAW_FRAMES 15
96 #define ATOMISP_METADATA_QUEUE_DEPTH_FOR_HAL	8
97 #define ATOMISP_S3A_BUF_QUEUE_DEPTH_FOR_HAL	8
98 
99 /*
100  * Define how fast CPU should be able to serve ISP interrupts.
101  * The bigger the value, the higher risk that the ISP is not
102  * triggered sufficiently fast for it to process image during
103  * vertical blanking time, increasing risk of dropped frames.
104  * 1000 us is a reasonable value considering that the processing
105  * time is typically ~2000 us.
106  */
107 #define ATOMISP_MAX_ISR_LATENCY	1000
108 
109 /* Add new YUVPP pipe for SOC sensor. */
110 #define ATOMISP_CSS_SUPPORT_YUVPP     1
111 
112 #define ATOMISP_CSS_OUTPUT_SECOND_INDEX     1
113 #define ATOMISP_CSS_OUTPUT_DEFAULT_INDEX    0
114 
115 /* ISP2401 */
116 #define ATOMISP_ION_DEVICE_FD_OFFSET   16
117 #define ATOMISP_ION_SHARED_FD_MASK     (0xFFFF)
118 #define ATOMISP_ION_DEVICE_FD_MASK     (~ATOMISP_ION_SHARED_FD_MASK)
119 #define ION_FD_UNSET (-1)
120 
121 #define DIV_NEAREST_STEP(n, d, step) \
122 	round_down((2 * (n) + (d) * (step)) / (2 * (d)), (step))
123 
124 struct atomisp_input_subdev {
125 	unsigned int type;
126 	enum atomisp_camera_port port;
127 	struct v4l2_subdev *camera;
128 	struct v4l2_subdev *motor;
129 	struct v4l2_frmsizeenum frame_size;
130 
131 	/*
132 	 * To show this resource is used by
133 	 * which stream, in ISP multiple stream mode
134 	 */
135 	struct atomisp_sub_device *asd;
136 
137 	int sensor_index;
138 };
139 
140 enum atomisp_dfs_mode {
141 	ATOMISP_DFS_MODE_AUTO = 0,
142 	ATOMISP_DFS_MODE_LOW,
143 	ATOMISP_DFS_MODE_MAX,
144 };
145 
146 struct atomisp_regs {
147 	/* PCI config space info */
148 	u16 pcicmdsts;
149 	u32 ispmmadr;
150 	u32 msicap;
151 	u32 msi_addr;
152 	u16 msi_data;
153 	u8 intr;
154 	u32 interrupt_control;
155 	u32 pmcs;
156 	u32 cg_dis;
157 	u32 i_control;
158 
159 	/* I-Unit PHY related info */
160 	u32 csi_rcomp_config;
161 	u32 csi_afe_dly;
162 	u32 csi_control;
163 
164 	/* New for MRFLD */
165 	u32 csi_afe_rcomp_config;
166 	u32 csi_afe_hs_control;
167 	u32 csi_deadline_control;
168 	u32 csi_access_viol;
169 };
170 
171 #define ATOMISP_DEVICE_STREAMING_DISABLED	0
172 #define ATOMISP_DEVICE_STREAMING_ENABLED	1
173 #define ATOMISP_DEVICE_STREAMING_STOPPING	2
174 
175 /*
176  * ci device struct
177  */
178 struct atomisp_device {
179 	struct device *dev;
180 	struct v4l2_device v4l2_dev;
181 	struct media_device media_dev;
182 	struct atomisp_sub_device asd;
183 	struct atomisp_platform_data *pdata;
184 	void *mmu_l1_base;
185 	void __iomem *base;
186 	const struct firmware *firmware;
187 
188 	struct dev_pm_domain pm_domain;
189 	struct pm_qos_request pm_qos;
190 	s32 max_isr_latency;
191 
192 	struct atomisp_mipi_csi2_device csi2_port[ATOMISP_CAMERA_NR_PORTS];
193 	struct atomisp_tpg_device tpg;
194 
195 	/* Purpose of mutex is to protect and serialize use of isp data
196 	 * structures and css API calls. */
197 	struct mutex mutex;
198 
199 	unsigned int input_cnt;
200 	struct atomisp_input_subdev inputs[ATOM_ISP_MAX_INPUTS];
201 	struct v4l2_subdev *flash;
202 	struct v4l2_subdev *motor;
203 
204 	struct atomisp_regs saved_regs;
205 	struct atomisp_css_env css_env;
206 
207 	/* isp timeout status flag */
208 	bool isp_timeout;
209 	bool isp_fatal_error;
210 	struct work_struct assert_recovery_work;
211 
212 	spinlock_t lock; /* Protects asd.streaming */
213 
214 	bool need_gfx_throttle;
215 
216 	unsigned int mipi_frame_size;
217 	const struct atomisp_dfs_config *dfs;
218 	unsigned int hpll_freq;
219 	unsigned int running_freq;
220 
221 	bool css_initialized;
222 };
223 
224 #define v4l2_dev_to_atomisp_device(dev) \
225 	container_of(dev, struct atomisp_device, v4l2_dev)
226 
227 extern struct device *atomisp_dev;
228 
229 #endif /* __ATOMISP_INTERNAL_H__ */
230