blob: 506c53acd53040cd0d79ea7c683be05611398cd6 [file] [log] [blame]
VijayaKumar T M08a82c82020-08-05 14:05:26 +05301/* Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -08002 *
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>
Om Parkashcf52e092019-09-18 15:49:19 +053029#include <linux/mutex.h>
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080030#include <media/cam_sensor.h>
31#include <media/v4l2-event.h>
32#include <media/v4l2-ioctl.h>
33#include <media/v4l2-subdev.h>
34#include <cam_sensor_cmn_header.h>
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080035#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"
Alok Pandey01b1b352017-06-25 20:38:54 +053040#include "cam_soc_util.h"
Jigarkumar Zalacbb5a382017-07-17 19:06:42 -070041#include "cam_debug_util.h"
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080042
43#define V4L2_IDENT_CCI 50005
44#define CCI_I2C_QUEUE_0_SIZE 128
45#define CCI_I2C_QUEUE_1_SIZE 32
46#define CYCLES_PER_MICRO_SEC_DEFAULT 4915
47#define CCI_MAX_DELAY 1000000
48
Shankar Ravie09480f2018-04-02 15:27:28 +053049#define CCI_TIMEOUT msecs_to_jiffies(1500)
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080050
51#define NUM_MASTERS 2
52#define NUM_QUEUES 2
53
54#define TRUE 1
55#define FALSE 0
56
57#define CCI_PINCTRL_STATE_DEFAULT "cci_default"
58#define CCI_PINCTRL_STATE_SLEEP "cci_suspend"
59
60#define CCI_NUM_CLK_MAX 16
61#define CCI_NUM_CLK_CASES 5
62#define CCI_CLK_SRC_NAME "cci_src_clk"
63#define MSM_CCI_WRITE_DATA_PAYLOAD_SIZE_10 10
64#define MSM_CCI_WRITE_DATA_PAYLOAD_SIZE_11 11
65#define BURST_MIN_FREE_SIZE 8
66#define MAX_LRME_V4l2_EVENTS 30
67
68/* Max bytes that can be read per CCI read transaction */
Karthik Anantha Ramd87c67b2018-05-03 14:36:43 -070069#define CCI_READ_MAX 256
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080070#define CCI_I2C_READ_MAX_RETRIES 3
71#define CCI_I2C_MAX_READ 8192
72#define CCI_I2C_MAX_WRITE 8192
Karthik Anantha Ramd87c67b2018-05-03 14:36:43 -070073#define CCI_I2C_MAX_BYTE_COUNT 65535
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080074
75#define CAMX_CCI_DEV_NAME "cam-cci-driver"
76
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080077#define PRIORITY_QUEUE (QUEUE_0)
78#define SYNC_QUEUE (QUEUE_1)
79
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080080enum cci_i2c_sync {
81 MSM_SYNC_DISABLE,
82 MSM_SYNC_ENABLE,
83};
84
85enum cam_cci_cmd_type {
86 MSM_CCI_INIT,
87 MSM_CCI_RELEASE,
88 MSM_CCI_SET_SID,
89 MSM_CCI_SET_FREQ,
90 MSM_CCI_SET_SYNC_CID,
91 MSM_CCI_I2C_READ,
92 MSM_CCI_I2C_WRITE,
93 MSM_CCI_I2C_WRITE_SEQ,
Karthik Anantha Ram0fb02a32017-08-23 11:52:50 -070094 MSM_CCI_I2C_WRITE_BURST,
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -080095 MSM_CCI_I2C_WRITE_ASYNC,
96 MSM_CCI_GPIO_WRITE,
97 MSM_CCI_I2C_WRITE_SYNC,
98 MSM_CCI_I2C_WRITE_SYNC_BLOCK,
99};
100
101enum cci_i2c_queue_t {
102 QUEUE_0,
103 QUEUE_1,
104 QUEUE_INVALID,
105};
106
107struct cam_cci_wait_sync_cfg {
108 uint16_t cid;
109 int16_t csid;
110 uint16_t line;
111 uint16_t delay;
112};
113
114struct cam_cci_gpio_cfg {
115 uint16_t gpio_queue;
116 uint16_t i2c_queue;
117};
118
119struct cam_cci_read_cfg {
120 uint32_t addr;
121 uint16_t addr_type;
122 uint8_t *data;
123 uint16_t num_byte;
Karthik Anantha Ramd87c67b2018-05-03 14:36:43 -0700124 uint16_t data_type;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800125};
126
127struct cam_cci_i2c_queue_info {
128 uint32_t max_queue_size;
129 uint32_t report_id;
130 uint32_t irq_en;
131 uint32_t capture_rep_data;
132};
133
134struct cam_cci_master_info {
135 uint32_t status;
136 atomic_t q_free[NUM_QUEUES];
137 uint8_t q_lock[NUM_QUEUES];
138 uint8_t reset_pending;
139 struct mutex mutex;
140 struct completion reset_complete;
Karthik Anantha Ram5da5db22018-08-10 15:39:30 -0700141 struct completion th_complete;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800142 struct mutex mutex_q[NUM_QUEUES];
143 struct completion report_q[NUM_QUEUES];
144 atomic_t done_pending[NUM_QUEUES];
Jigarkumar Zala47f70e72018-02-23 11:00:06 -0800145 spinlock_t lock_q[NUM_QUEUES];
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800146};
147
148struct cam_cci_clk_params_t {
149 uint16_t hw_thigh;
150 uint16_t hw_tlow;
151 uint16_t hw_tsu_sto;
152 uint16_t hw_tsu_sta;
153 uint16_t hw_thd_dat;
154 uint16_t hw_thd_sta;
155 uint16_t hw_tbuf;
156 uint8_t hw_scl_stretch_en;
157 uint8_t hw_trdhld;
158 uint8_t hw_tsp;
159 uint32_t cci_clk_src;
160};
161
162enum cam_cci_state_t {
163 CCI_STATE_ENABLED,
164 CCI_STATE_DISABLED,
165};
166
167/**
168 * struct cci_device
169 * @pdev: Platform device
170 * @subdev: V4L2 sub device
171 * @base: Base address of CCI device
172 * @hw_version: Hardware version
173 * @ref_count: Reference Count
174 * @cci_state: CCI state machine
175 * @num_clk: Number of CCI clock
176 * @cci_clk: CCI clock structure
177 * @cci_clk_info: CCI clock information
178 * @cam_cci_i2c_queue_info: CCI queue information
179 * @i2c_freq_mode: I2C frequency of operations
180 * @cci_clk_params: CCI hw clk params
181 * @cci_gpio_tbl: CCI GPIO table
182 * @cci_gpio_tbl_size: GPIO table size
183 * @cci_pinctrl: Pinctrl structure
184 * @cci_pinctrl_status: CCI pinctrl status
185 * @cci_clk_src: CCI clk src rate
186 * @cci_vreg: CCI regulator structure
187 * @cci_reg_ptr: CCI individual regulator structure
188 * @regulator_count: Regulator count
189 * @support_seq_write:
190 * Set this flag when sequential write is enabled
191 * @write_wq: Work queue structure
192 * @valid_sync: Is it a valid sync with CSID
193 * @v4l2_dev_str: V4L2 device structure
194 * @cci_wait_sync_cfg: CCI sync config
195 * @cycles_per_us: Cycles per micro sec
196 * @payload_size: CCI packet payload size
Karthik Anantha Ram5da5db22018-08-10 15:39:30 -0700197 * @irq_status1: Store irq_status1 to be cleared after
198 * draining FIFO buffer for burst read
199 * @lock_status: to protect changes to irq_status1
200 * @is_burst_read: Flag to determine if we are performing
201 * a burst read operation or not
Om Parkashcf52e092019-09-18 15:49:19 +0530202 * @init_mutex: Mutex for maintaining refcount for attached
203 * devices to cci during init/deinit.
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800204 */
205struct cci_device {
206 struct v4l2_subdev subdev;
Alok Pandey01b1b352017-06-25 20:38:54 +0530207 struct cam_hw_soc_info soc_info;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800208 uint32_t hw_version;
209 uint8_t ref_count;
210 enum cam_cci_state_t cci_state;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800211 struct cam_cci_i2c_queue_info
212 cci_i2c_queue_info[NUM_MASTERS][NUM_QUEUES];
213 struct cam_cci_master_info cci_master_info[NUM_MASTERS];
214 enum i2c_freq_mode i2c_freq_mode[NUM_MASTERS];
215 struct cam_cci_clk_params_t cci_clk_params[I2C_MAX_MODES];
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800216 struct msm_pinctrl_info cci_pinctrl;
217 uint8_t cci_pinctrl_status;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800218 uint8_t support_seq_write;
219 struct workqueue_struct *write_wq[MASTER_MAX];
220 struct cam_cci_wait_sync_cfg cci_wait_sync_cfg;
221 uint8_t valid_sync;
222 struct cam_subdev v4l2_dev_str;
223 uint32_t cycles_per_us;
Alok Pandey01b1b352017-06-25 20:38:54 +0530224 int32_t clk_level_index;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800225 uint8_t payload_size;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800226 char device_name[20];
227 uint32_t cpas_handle;
Karthik Anantha Ram5da5db22018-08-10 15:39:30 -0700228 uint32_t irq_status1;
229 spinlock_t lock_status;
230 bool is_burst_read;
Om Parkashcf52e092019-09-18 15:49:19 +0530231 struct mutex init_mutex;
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800232};
233
234enum cam_cci_i2c_cmd_type {
235 CCI_I2C_SET_PARAM_CMD = 1,
236 CCI_I2C_WAIT_CMD,
237 CCI_I2C_WAIT_SYNC_CMD,
238 CCI_I2C_WAIT_GPIO_EVENT_CMD,
239 CCI_I2C_TRIG_I2C_EVENT_CMD,
240 CCI_I2C_LOCK_CMD,
241 CCI_I2C_UNLOCK_CMD,
242 CCI_I2C_REPORT_CMD,
243 CCI_I2C_WRITE_CMD,
244 CCI_I2C_READ_CMD,
245 CCI_I2C_WRITE_DISABLE_P_CMD,
246 CCI_I2C_READ_DISABLE_P_CMD,
247 CCI_I2C_WRITE_CMD2,
248 CCI_I2C_WRITE_CMD3,
249 CCI_I2C_REPEAT_CMD,
250 CCI_I2C_INVALID_CMD,
251};
252
253enum cam_cci_gpio_cmd_type {
254 CCI_GPIO_SET_PARAM_CMD = 1,
255 CCI_GPIO_WAIT_CMD,
256 CCI_GPIO_WAIT_SYNC_CMD,
257 CCI_GPIO_WAIT_GPIO_IN_EVENT_CMD,
258 CCI_GPIO_WAIT_I2C_Q_TRIG_EVENT_CMD,
259 CCI_GPIO_OUT_CMD,
260 CCI_GPIO_TRIG_EVENT_CMD,
261 CCI_GPIO_REPORT_CMD,
262 CCI_GPIO_REPEAT_CMD,
263 CCI_GPIO_CONTINUE_CMD,
264 CCI_GPIO_INVALID_CMD,
265};
266
267struct cam_sensor_cci_client {
268 struct v4l2_subdev *cci_subdev;
269 uint32_t freq;
270 enum i2c_freq_mode i2c_freq_mode;
271 enum cci_i2c_master_t cci_i2c_master;
272 uint16_t sid;
273 uint16_t cid;
274 uint32_t timeout;
275 uint16_t retries;
276 uint16_t id_map;
277};
278
279struct cam_cci_ctrl {
280 int32_t status;
281 struct cam_sensor_cci_client *cci_info;
282 enum cam_cci_cmd_type cmd;
283 union {
284 struct cam_sensor_i2c_reg_setting cci_i2c_write_cfg;
285 struct cam_cci_read_cfg cci_i2c_read_cfg;
286 struct cam_cci_wait_sync_cfg cci_wait_sync_cfg;
287 struct cam_cci_gpio_cfg gpio_cfg;
288 } cfg;
289};
290
291struct cci_write_async {
292 struct cci_device *cci_dev;
293 struct cam_cci_ctrl c_ctrl;
294 enum cci_i2c_queue_t queue;
295 struct work_struct work;
296 enum cci_i2c_sync sync_en;
297};
298
299irqreturn_t cam_cci_irq(int irq_num, void *data);
300
301#ifdef CONFIG_SPECTRA_CAMERA
Jigarkumar Zalab2009712017-10-25 19:16:39 -0700302extern struct v4l2_subdev *cam_cci_get_subdev(void);
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800303#else
304static inline struct v4l2_subdev *cam_cci_get_subdev(void)
305{
306 return NULL;
307}
308#endif
309
310#define VIDIOC_MSM_CCI_CFG \
VijayaKumar T M08a82c82020-08-05 14:05:26 +0530311 _IOWR('V', BASE_VIDIOC_PRIVATE + 23, struct cam_cci_ctrl)
Viswanadha Raju Thotakura9a3795e2017-02-25 13:27:08 -0800312
313#endif /* _CAM_CCI_DEV_H_ */