]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - include/media/camera_common.h
9f34b54a840165221726c8df19bbff77b09e5090
[sojka/nv-tegra/linux-3.10.git] / include / media / camera_common.h
1 /**
2  * camera_common.h - utilities for tegra camera driver
3  *
4  * Copyright (c) 2015, NVIDIA Corporation.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef __camera_common__
20 #define __camera_common__
21
22 #include <linux/delay.h>
23 #include <linux/fs.h>
24 #include <linux/i2c.h>
25 #include <linux/regmap.h>
26 #include <linux/clk.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/videodev2.h>
29
30 #include <linux/kernel.h>
31 #include <linux/debugfs.h>
32
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-subdev.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/soc_camera.h>
37
38 #define V4L2_CID_TEGRA_CAMERA_BASE      (V4L2_CTRL_CLASS_CAMERA | 0x2000)
39
40 #define V4L2_CID_FRAME_LENGTH           (V4L2_CID_TEGRA_CAMERA_BASE+0)
41 #define V4L2_CID_COARSE_TIME            (V4L2_CID_TEGRA_CAMERA_BASE+1)
42 #define V4L2_CID_COARSE_TIME_SHORT      (V4L2_CID_TEGRA_CAMERA_BASE+2)
43 #define V4L2_CID_GROUP_HOLD             (V4L2_CID_TEGRA_CAMERA_BASE+3)
44 #define V4L2_CID_HDR_EN         (V4L2_CID_TEGRA_CAMERA_BASE+4)
45 #define V4L2_CID_EEPROM_DATA            (V4L2_CID_TEGRA_CAMERA_BASE+5)
46 #define V4L2_CID_OTP_DATA               (V4L2_CID_TEGRA_CAMERA_BASE+6)
47 #define V4L2_CID_FUSE_ID                (V4L2_CID_TEGRA_CAMERA_BASE+7)
48
49 #define V4L2_CID_TEGRA_CAMERA_LAST      (V4L2_CID_TEGRA_CAMERA_BASE+8)
50
51 #define MAX_BUFFER_SIZE                 32
52
53 struct reg_8 {
54         u16 addr;
55         u8 val;
56 };
57
58 struct reg_16 {
59         u16 addr;
60         u16 val;
61 };
62
63 struct camera_common_power_rail {
64         struct regulator *dvdd;
65         struct regulator *avdd;
66         struct regulator *iovdd;
67         struct regulator *ext_reg1;
68         struct regulator *ext_reg2;
69         struct regulator *vcmvdd;
70         struct clk *mclk;
71         unsigned int pwdn_gpio;
72         unsigned int reset_gpio;
73         unsigned int af_gpio;
74         bool state;
75 };
76
77 struct camera_common_regulators {
78         const char *avdd;
79         const char *dvdd;
80         const char *iovdd;
81         const char *vcmvdd;
82 };
83
84 struct camera_common_pdata {
85         const char *mclk_name; /* NULL for default default_mclk */
86         unsigned int pwdn_gpio;
87         unsigned int reset_gpio;
88         unsigned int af_gpio;
89         bool ext_reg;
90         int (*power_on)(struct camera_common_power_rail *pw);
91         int (*power_off)(struct camera_common_power_rail *pw);
92         struct camera_common_regulators regulators;
93         bool use_cam_gpio;
94         bool has_eeprom;
95 };
96
97 struct camera_common_eeprom_data {
98         struct i2c_client *i2c_client;
99         struct i2c_adapter *adap;
100         struct i2c_board_info brd;
101         struct regmap *regmap;
102 };
103
104 int
105 regmap_util_write_table_8(struct regmap *regmap,
106                           const struct reg_8 table[],
107                           const struct reg_8 override_list[],
108                           int num_override_regs,
109                           u16 wait_ms_addr, u16 end_addr);
110
111 int
112 regmap_util_write_table_16_as_8(struct regmap *regmap,
113                                 const struct reg_16 table[],
114                                 const struct reg_16 override_list[],
115                                 int num_override_regs,
116                                 u16 wait_ms_addr, u16 end_addr);
117
118 enum switch_state {
119         SWITCH_OFF,
120         SWITCH_ON,
121 };
122
123 static const s64 switch_ctrl_qmenu[] = {
124         SWITCH_OFF, SWITCH_ON
125 };
126
127 struct camera_common_frmfmt {
128         struct v4l2_frmsize_discrete    size;
129         bool    hdr_en;
130         int     mode;
131 };
132
133 struct camera_common_colorfmt {
134         enum v4l2_mbus_pixelcode        code;
135         enum v4l2_colorspace            colorspace;
136 };
137
138 struct camera_common_data;
139
140 struct camera_common_sensor_ops {
141         int (*power_on)(struct camera_common_data *s_data);
142         int (*power_off)(struct camera_common_data *s_data);
143         int (*write_reg)(struct camera_common_data *s_data,
144           u16 addr, u8 val);
145         int (*read_reg)(struct camera_common_data *s_data,
146           u16 addr, u8 *val);
147 };
148
149 struct camera_common_data {
150         struct camera_common_sensor_ops         *ops;
151         struct v4l2_ctrl_handler                *ctrl_handler;
152         struct i2c_client                       *i2c_client;
153         const struct camera_common_frmfmt       *frmfmt;
154         const struct camera_common_colorfmt     *colorfmt;
155         struct dentry                           *debugdir;
156         struct camera_common_power_rail         *power;
157
158         struct v4l2_subdev                      subdev;
159         struct v4l2_ctrl                        **ctrls;
160
161         void    *priv;
162         int     ident;
163         int     numctrls;
164         int csi_port;
165         int numlanes;
166         int     mode;
167         int     numfmts;
168         int     def_mode, def_width, def_height;
169         int     def_clk_freq;
170         int     fmt_width, fmt_height;
171 };
172
173 static inline void msleep_range(unsigned int delay_base)
174 {
175         usleep_range(delay_base * 1000, delay_base * 1000 + 500);
176 }
177
178 static inline struct camera_common_data *to_camera_common_data(
179         const struct i2c_client *client)
180 {
181         return container_of(i2c_get_clientdata(client),
182                             struct camera_common_data, subdev);
183 }
184
185 int camera_common_g_ctrl(struct camera_common_data *s_data,
186                          struct v4l2_control *control);
187
188 int camera_common_regulator_get(struct i2c_client *client,
189                        struct regulator **vreg, const char *vreg_name);
190
191 int camera_common_debugfs_show(struct seq_file *s, void *unused);
192 ssize_t camera_common_debugfs_write(
193         struct file *file,
194         char const __user *buf,
195         size_t count,
196         loff_t *offset);
197 int camera_common_debugfs_open(struct inode *inode, struct file *file);
198 void camera_common_remove_debugfs(struct camera_common_data *s_data);
199 void camera_common_create_debugfs(struct camera_common_data *s_data,
200                 const char *name);
201
202 const struct camera_common_colorfmt *camera_common_find_datafmt(
203                 enum v4l2_mbus_pixelcode code);
204 int camera_common_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
205                          enum v4l2_mbus_pixelcode *code);
206 int camera_common_try_fmt(struct v4l2_subdev *sd,
207                            struct v4l2_mbus_framefmt *mf);
208 int camera_common_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
209 int camera_common_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
210 int camera_common_g_chip_ident(struct v4l2_subdev *sd,
211                              struct v4l2_dbg_chip_ident *id);
212 int camera_common_s_power(struct v4l2_subdev *sd, int on);
213 int camera_common_g_mbus_config(struct v4l2_subdev *sd,
214                               struct v4l2_mbus_config *cfg);
215
216
217 #endif /* __camera_common__ */