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