blob: 996fc62a837ab9c9936f93dcdb78c1878b9b98e3 [file] [log] [blame]
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -08001/* Copyright (c) 2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef _CAM_CCI_DEV_H_
14#define _CAM_CCI_DEV_H_
15
16#include <linux/delay.h>
17#include <linux/clk.h>
18#include <linux/io.h>
19#include <linux/of.h>
20#include <linux/of_gpio.h>
21#include <linux/of_platform.h>
22#include <linux/module.h>
23#include <linux/irqreturn.h>
24#include <linux/ion.h>
25#include <linux/iommu.h>
26#include <linux/timer.h>
27#include <linux/kernel.h>
28#include <linux/platform_device.h>
29#include <media/cam_sensor.h>
30#include <media/v4l2-event.h>
31#include <media/v4l2-ioctl.h>
32#include <media/v4l2-subdev.h>
33#include <cam_sensor_cmn_header.h>
34#include <cam_sensor_soc_api.h>
35#include <cam_io_util.h>
36#include <cam_sensor_util.h>
37#include <cam_subdev.h>
38#include <cam_cpas_api.h>
39#include "cam_cci_hwreg.h"
40
41#define V4L2_IDENT_CCI 50005
42#define CCI_I2C_QUEUE_0_SIZE 128
43#define CCI_I2C_QUEUE_1_SIZE 32
44#define CYCLES_PER_MICRO_SEC_DEFAULT 4915
45#define CCI_MAX_DELAY 1000000
46
47#define CCI_TIMEOUT msecs_to_jiffies(500)
48
49#define NUM_MASTERS 2
50#define NUM_QUEUES 2
51
52#define TRUE 1
53#define FALSE 0
54
55#define CCI_PINCTRL_STATE_DEFAULT "cci_default"
56#define CCI_PINCTRL_STATE_SLEEP "cci_suspend"
57
58#define CCI_NUM_CLK_MAX 16
59#define CCI_NUM_CLK_CASES 5
60#define CCI_CLK_SRC_NAME "cci_src_clk"
61#define MSM_CCI_WRITE_DATA_PAYLOAD_SIZE_10 10
62#define MSM_CCI_WRITE_DATA_PAYLOAD_SIZE_11 11
63#define BURST_MIN_FREE_SIZE 8
64#define MAX_LRME_V4l2_EVENTS 30
65
66/* Max bytes that can be read per CCI read transaction */
67#define CCI_READ_MAX 12
68#define CCI_I2C_READ_MAX_RETRIES 3
69#define CCI_I2C_MAX_READ 8192
70#define CCI_I2C_MAX_WRITE 8192
71
72#define CAMX_CCI_DEV_NAME "cam-cci-driver"
73
74/* Max bytes that can be read per CCI read transaction */
75#define CCI_READ_MAX 12
76#define CCI_I2C_READ_MAX_RETRIES 3
77#define CCI_I2C_MAX_READ 8192
78#define CCI_I2C_MAX_WRITE 8192
79
80#define PRIORITY_QUEUE (QUEUE_0)
81#define SYNC_QUEUE (QUEUE_1)
82
83#undef CDBG
84#define CDBG(fmt, args...) pr_debug(fmt, ##args)
85
86#undef CCI_DBG
87#ifdef MSM_CCI_DEBUG
88#define CCI_DBG(fmt, args...) pr_err(fmt, ##args)
89#else
90#define CCI_DBG(fmt, args...) pr_debug(fmt, ##args)
91#endif
92
93enum cci_i2c_sync {
94 MSM_SYNC_DISABLE,
95 MSM_SYNC_ENABLE,
96};
97
98enum cam_cci_cmd_type {
99 MSM_CCI_INIT,
100 MSM_CCI_RELEASE,
101 MSM_CCI_SET_SID,
102 MSM_CCI_SET_FREQ,
103 MSM_CCI_SET_SYNC_CID,
104 MSM_CCI_I2C_READ,
105 MSM_CCI_I2C_WRITE,
106 MSM_CCI_I2C_WRITE_SEQ,
107 MSM_CCI_I2C_WRITE_ASYNC,
108 MSM_CCI_GPIO_WRITE,
109 MSM_CCI_I2C_WRITE_SYNC,
110 MSM_CCI_I2C_WRITE_SYNC_BLOCK,
111};
112
113enum cci_i2c_queue_t {
114 QUEUE_0,
115 QUEUE_1,
116 QUEUE_INVALID,
117};
118
119struct cam_cci_wait_sync_cfg {
120 uint16_t cid;
121 int16_t csid;
122 uint16_t line;
123 uint16_t delay;
124};
125
126struct cam_cci_gpio_cfg {
127 uint16_t gpio_queue;
128 uint16_t i2c_queue;
129};
130
131struct cam_cci_read_cfg {
132 uint32_t addr;
133 uint16_t addr_type;
134 uint8_t *data;
135 uint16_t num_byte;
136};
137
138struct cam_cci_i2c_queue_info {
139 uint32_t max_queue_size;
140 uint32_t report_id;
141 uint32_t irq_en;
142 uint32_t capture_rep_data;
143};
144
145struct cam_cci_master_info {
146 uint32_t status;
147 atomic_t q_free[NUM_QUEUES];
148 uint8_t q_lock[NUM_QUEUES];
149 uint8_t reset_pending;
150 struct mutex mutex;
151 struct completion reset_complete;
152 struct mutex mutex_q[NUM_QUEUES];
153 struct completion report_q[NUM_QUEUES];
154 atomic_t done_pending[NUM_QUEUES];
155};
156
157struct cam_cci_clk_params_t {
158 uint16_t hw_thigh;
159 uint16_t hw_tlow;
160 uint16_t hw_tsu_sto;
161 uint16_t hw_tsu_sta;
162 uint16_t hw_thd_dat;
163 uint16_t hw_thd_sta;
164 uint16_t hw_tbuf;
165 uint8_t hw_scl_stretch_en;
166 uint8_t hw_trdhld;
167 uint8_t hw_tsp;
168 uint32_t cci_clk_src;
169};
170
171enum cam_cci_state_t {
172 CCI_STATE_ENABLED,
173 CCI_STATE_DISABLED,
174};
175
176/**
177 * struct cci_device
178 * @pdev: Platform device
179 * @subdev: V4L2 sub device
180 * @base: Base address of CCI device
181 * @hw_version: Hardware version
182 * @ref_count: Reference Count
183 * @cci_state: CCI state machine
184 * @num_clk: Number of CCI clock
185 * @cci_clk: CCI clock structure
186 * @cci_clk_info: CCI clock information
187 * @cam_cci_i2c_queue_info: CCI queue information
188 * @i2c_freq_mode: I2C frequency of operations
189 * @cci_clk_params: CCI hw clk params
190 * @cci_gpio_tbl: CCI GPIO table
191 * @cci_gpio_tbl_size: GPIO table size
192 * @cci_pinctrl: Pinctrl structure
193 * @cci_pinctrl_status: CCI pinctrl status
194 * @cci_clk_src: CCI clk src rate
195 * @cci_vreg: CCI regulator structure
196 * @cci_reg_ptr: CCI individual regulator structure
197 * @regulator_count: Regulator count
198 * @support_seq_write:
199 * Set this flag when sequential write is enabled
200 * @write_wq: Work queue structure
201 * @valid_sync: Is it a valid sync with CSID
202 * @v4l2_dev_str: V4L2 device structure
203 * @cci_wait_sync_cfg: CCI sync config
204 * @cycles_per_us: Cycles per micro sec
205 * @payload_size: CCI packet payload size
206 */
207struct cci_device {
208 struct v4l2_subdev subdev;
209 struct resource *irq;
210 void __iomem *base;
211 uint32_t hw_version;
212 uint8_t ref_count;
213 enum cam_cci_state_t cci_state;
214 size_t num_clk;
215 struct clk **cci_clk;
216 struct msm_cam_clk_info *cci_clk_info;
217 struct cam_cci_i2c_queue_info
218 cci_i2c_queue_info[NUM_MASTERS][NUM_QUEUES];
219 struct cam_cci_master_info cci_master_info[NUM_MASTERS];
220 enum i2c_freq_mode i2c_freq_mode[NUM_MASTERS];
221 struct cam_cci_clk_params_t cci_clk_params[I2C_MAX_MODES];
222 struct gpio *cci_gpio_tbl;
223 uint8_t cci_gpio_tbl_size;
224 struct msm_pinctrl_info cci_pinctrl;
225 uint8_t cci_pinctrl_status;
226 uint32_t cci_clk_src;
227 struct camera_vreg_t *cci_vreg;
228 struct regulator *cci_reg_ptr[MAX_REGULATOR];
229 int32_t regulator_count;
230 uint8_t support_seq_write;
231 struct workqueue_struct *write_wq[MASTER_MAX];
232 struct cam_cci_wait_sync_cfg cci_wait_sync_cfg;
233 uint8_t valid_sync;
234 struct cam_subdev v4l2_dev_str;
235 uint32_t cycles_per_us;
236 uint8_t payload_size;
237 size_t num_clk_cases;
238 uint32_t **cci_clk_rates;
239 char device_name[20];
240 uint32_t cpas_handle;
241};
242
243enum cam_cci_i2c_cmd_type {
244 CCI_I2C_SET_PARAM_CMD = 1,
245 CCI_I2C_WAIT_CMD,
246 CCI_I2C_WAIT_SYNC_CMD,
247 CCI_I2C_WAIT_GPIO_EVENT_CMD,
248 CCI_I2C_TRIG_I2C_EVENT_CMD,
249 CCI_I2C_LOCK_CMD,
250 CCI_I2C_UNLOCK_CMD,
251 CCI_I2C_REPORT_CMD,
252 CCI_I2C_WRITE_CMD,
253 CCI_I2C_READ_CMD,
254 CCI_I2C_WRITE_DISABLE_P_CMD,
255 CCI_I2C_READ_DISABLE_P_CMD,
256 CCI_I2C_WRITE_CMD2,
257 CCI_I2C_WRITE_CMD3,
258 CCI_I2C_REPEAT_CMD,
259 CCI_I2C_INVALID_CMD,
260};
261
262enum cam_cci_gpio_cmd_type {
263 CCI_GPIO_SET_PARAM_CMD = 1,
264 CCI_GPIO_WAIT_CMD,
265 CCI_GPIO_WAIT_SYNC_CMD,
266 CCI_GPIO_WAIT_GPIO_IN_EVENT_CMD,
267 CCI_GPIO_WAIT_I2C_Q_TRIG_EVENT_CMD,
268 CCI_GPIO_OUT_CMD,
269 CCI_GPIO_TRIG_EVENT_CMD,
270 CCI_GPIO_REPORT_CMD,
271 CCI_GPIO_REPEAT_CMD,
272 CCI_GPIO_CONTINUE_CMD,
273 CCI_GPIO_INVALID_CMD,
274};
275
276struct cam_sensor_cci_client {
277 struct v4l2_subdev *cci_subdev;
278 uint32_t freq;
279 enum i2c_freq_mode i2c_freq_mode;
280 enum cci_i2c_master_t cci_i2c_master;
281 uint16_t sid;
282 uint16_t cid;
283 uint32_t timeout;
284 uint16_t retries;
285 uint16_t id_map;
286};
287
288struct cam_cci_ctrl {
289 int32_t status;
290 struct cam_sensor_cci_client *cci_info;
291 enum cam_cci_cmd_type cmd;
292 union {
293 struct cam_sensor_i2c_reg_setting cci_i2c_write_cfg;
294 struct cam_cci_read_cfg cci_i2c_read_cfg;
295 struct cam_cci_wait_sync_cfg cci_wait_sync_cfg;
296 struct cam_cci_gpio_cfg gpio_cfg;
297 } cfg;
298};
299
300struct cci_write_async {
301 struct cci_device *cci_dev;
302 struct cam_cci_ctrl c_ctrl;
303 enum cci_i2c_queue_t queue;
304 struct work_struct work;
305 enum cci_i2c_sync sync_en;
306};
307
308irqreturn_t cam_cci_irq(int irq_num, void *data);
309
310#ifdef CONFIG_SPECTRA_CAMERA
311struct v4l2_subdev *cam_cci_get_subdev(void);
312#else
313static inline struct v4l2_subdev *cam_cci_get_subdev(void)
314{
315 return NULL;
316}
317#endif
318
319#define VIDIOC_MSM_CCI_CFG \
320 _IOWR('V', BASE_VIDIOC_PRIVATE + 23, struct cam_cci_ctrl *)
321
322#endif /* _CAM_CCI_DEV_H_ */