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