blob: e6ed0d94ac1b3d98502da7580e176e0dd22c6202 [file] [log] [blame]
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001/*
2 * camera image capture (abstract) bus driver header
3 *
4 * Copyright (C) 2006, Sascha Hauer, Pengutronix
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#ifndef SOC_CAMERA_H
13#define SOC_CAMERA_H
14
15#include <linux/videodev2.h>
Paulius Zaleckas092d3922008-07-11 20:50:31 -030016#include <media/videobuf-core.h>
Robert Jarzmik2e521062008-08-01 20:14:50 -030017#include <linux/pm.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030018
19struct soc_camera_device {
20 struct list_head list;
21 struct device dev;
22 struct device *control;
23 unsigned short width; /* Current window */
24 unsigned short height; /* sizes */
25 unsigned short x_min; /* Camera capabilities */
26 unsigned short y_min;
27 unsigned short x_current; /* Current window location */
28 unsigned short y_current;
29 unsigned short width_min;
30 unsigned short width_max;
31 unsigned short height_min;
32 unsigned short height_max;
33 unsigned short y_skip_top; /* Lines to skip at the top */
34 unsigned short gain;
35 unsigned short exposure;
36 unsigned char iface; /* Host number */
37 unsigned char devnum; /* Device number per host */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -030038 unsigned char buswidth; /* See comment in .c */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030039 struct soc_camera_ops *ops;
40 struct video_device *vdev;
41 const struct soc_camera_data_format *current_fmt;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -030042 const struct soc_camera_data_format *formats;
43 int num_formats;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030044 struct soc_camera_format_xlate *user_formats;
45 int num_user_formats;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030046 struct module *owner;
Guennadi Liakhovetskid2e3dce2008-12-01 09:45:24 -030047 void *host_priv; /* per-device host private data */
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -030048 /* soc_camera.c private count. Only accessed with video_lock held */
49 int use_count;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030050};
51
52struct soc_camera_file {
53 struct soc_camera_device *icd;
54 struct videobuf_queue vb_vidq;
55};
56
57struct soc_camera_host {
58 struct list_head list;
59 struct device dev;
60 unsigned char nr; /* Host number */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030061 void *priv;
Kay Sieversaf128a12008-10-30 00:51:46 -030062 const char *drv_name;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -030063 struct soc_camera_host_ops *ops;
64};
65
66struct soc_camera_host_ops {
67 struct module *owner;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030068 int (*add)(struct soc_camera_device *);
69 void (*remove)(struct soc_camera_device *);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030070 int (*suspend)(struct soc_camera_device *, pm_message_t);
Robert Jarzmik2e521062008-08-01 20:14:50 -030071 int (*resume)(struct soc_camera_device *);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030072 int (*get_formats)(struct soc_camera_device *, int,
73 struct soc_camera_format_xlate *);
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -030074 int (*set_fmt)(struct soc_camera_device *, __u32, struct v4l2_rect *);
75 int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
Magnus Damma034d1b2008-07-11 20:59:34 -030076 void (*init_videobuf)(struct videobuf_queue *,
Paulius Zaleckas092d3922008-07-11 20:50:31 -030077 struct soc_camera_device *);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030078 int (*reqbufs)(struct soc_camera_file *, struct v4l2_requestbuffers *);
79 int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -030080 int (*set_bus_param)(struct soc_camera_device *, __u32);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030081 unsigned int (*poll)(struct file *, poll_table *);
82};
83
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -030084#define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
85#define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
86#define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
87#define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
88#define SOCAM_SENSOR_INVERT_DATA (1 << 4)
89
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030090struct soc_camera_link {
91 /* Camera bus id, used to match a camera and a bus */
92 int bus_id;
93 /* GPIO number to switch between 8 and 10 bit modes */
94 unsigned int gpio;
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -030095 /* Per camera SOCAM_SENSOR_* bus flags */
96 unsigned long flags;
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -030097 /* Optional callbacks to power on or off and reset the sensor */
98 int (*power)(struct device *, int);
99 int (*reset)(struct device *);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300100};
101
102static inline struct soc_camera_device *to_soc_camera_dev(struct device *dev)
103{
104 return container_of(dev, struct soc_camera_device, dev);
105}
106
107static inline struct soc_camera_host *to_soc_camera_host(struct device *dev)
108{
109 return container_of(dev, struct soc_camera_host, dev);
110}
111
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300112extern int soc_camera_host_register(struct soc_camera_host *ici);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300113extern void soc_camera_host_unregister(struct soc_camera_host *ici);
114extern int soc_camera_device_register(struct soc_camera_device *icd);
115extern void soc_camera_device_unregister(struct soc_camera_device *icd);
116
117extern int soc_camera_video_start(struct soc_camera_device *icd);
118extern void soc_camera_video_stop(struct soc_camera_device *icd);
119
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300120extern const struct soc_camera_data_format *soc_camera_format_by_fourcc(
121 struct soc_camera_device *icd, unsigned int fourcc);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300122extern const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
123 struct soc_camera_device *icd, unsigned int fourcc);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300124
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300125struct soc_camera_data_format {
Kay Sieversaf128a12008-10-30 00:51:46 -0300126 const char *name;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300127 unsigned int depth;
128 __u32 fourcc;
129 enum v4l2_colorspace colorspace;
130};
131
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300132/**
133 * struct soc_camera_format_xlate - match between host and sensor formats
134 * @cam_fmt: sensor format provided by the sensor
135 * @host_fmt: host format after host translation from cam_fmt
136 * @buswidth: bus width for this format
137 *
138 * Host and sensor translation structure. Used in table of host and sensor
139 * formats matchings in soc_camera_device. A host can override the generic list
140 * generation by implementing get_formats(), and use it for format checks and
141 * format setup.
142 */
143struct soc_camera_format_xlate {
144 const struct soc_camera_data_format *cam_fmt;
145 const struct soc_camera_data_format *host_fmt;
146 unsigned char buswidth;
147};
148
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300149struct soc_camera_ops {
150 struct module *owner;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300151 int (*probe)(struct soc_camera_device *);
152 void (*remove)(struct soc_camera_device *);
Robert Jarzmik2e521062008-08-01 20:14:50 -0300153 int (*suspend)(struct soc_camera_device *, pm_message_t state);
154 int (*resume)(struct soc_camera_device *);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300155 int (*init)(struct soc_camera_device *);
156 int (*release)(struct soc_camera_device *);
157 int (*start_capture)(struct soc_camera_device *);
158 int (*stop_capture)(struct soc_camera_device *);
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300159 int (*set_fmt)(struct soc_camera_device *, __u32, struct v4l2_rect *);
160 int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300161 unsigned long (*query_bus_param)(struct soc_camera_device *);
162 int (*set_bus_param)(struct soc_camera_device *, unsigned long);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300163 int (*get_chip_id)(struct soc_camera_device *,
164 struct v4l2_chip_ident *);
165#ifdef CONFIG_VIDEO_ADV_DEBUG
166 int (*get_register)(struct soc_camera_device *, struct v4l2_register *);
167 int (*set_register)(struct soc_camera_device *, struct v4l2_register *);
168#endif
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300169 int (*get_control)(struct soc_camera_device *, struct v4l2_control *);
170 int (*set_control)(struct soc_camera_device *, struct v4l2_control *);
171 const struct v4l2_queryctrl *controls;
172 int num_controls;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300173};
174
175static inline struct v4l2_queryctrl const *soc_camera_find_qctrl(
176 struct soc_camera_ops *ops, int id)
177{
178 int i;
179
180 for (i = 0; i < ops->num_controls; i++)
181 if (ops->controls[i].id == id)
182 return &ops->controls[i];
183
184 return NULL;
185}
186
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300187#define SOCAM_MASTER (1 << 0)
188#define SOCAM_SLAVE (1 << 1)
189#define SOCAM_HSYNC_ACTIVE_HIGH (1 << 2)
190#define SOCAM_HSYNC_ACTIVE_LOW (1 << 3)
191#define SOCAM_VSYNC_ACTIVE_HIGH (1 << 4)
192#define SOCAM_VSYNC_ACTIVE_LOW (1 << 5)
193#define SOCAM_DATAWIDTH_8 (1 << 6)
194#define SOCAM_DATAWIDTH_9 (1 << 7)
195#define SOCAM_DATAWIDTH_10 (1 << 8)
Magnus Dammb15cf1f2008-07-11 21:03:25 -0300196#define SOCAM_DATAWIDTH_16 (1 << 9)
197#define SOCAM_PCLK_SAMPLE_RISING (1 << 10)
198#define SOCAM_PCLK_SAMPLE_FALLING (1 << 11)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300199
200#define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_8 | SOCAM_DATAWIDTH_9 | \
Magnus Dammb15cf1f2008-07-11 21:03:25 -0300201 SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_16)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300202
203static inline unsigned long soc_camera_bus_param_compatible(
204 unsigned long camera_flags, unsigned long bus_flags)
205{
206 unsigned long common_flags, hsync, vsync, pclk;
207
208 common_flags = camera_flags & bus_flags;
209
210 hsync = common_flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
211 vsync = common_flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
212 pclk = common_flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
213
214 return (!hsync || !vsync || !pclk) ? 0 : common_flags;
215}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300216
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300217extern unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
218 unsigned long flags);
219
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300220#endif