soc_camera.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  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. #ifndef SOC_CAMERA_H
  12. #define SOC_CAMERA_H
  13. #include <linux/mutex.h>
  14. #include <linux/pm.h>
  15. #include <linux/videodev2.h>
  16. #include <media/videobuf-core.h>
  17. struct soc_camera_device {
  18. struct list_head list;
  19. struct device dev;
  20. struct device *control;
  21. unsigned short width; /* Current window */
  22. unsigned short height; /* sizes */
  23. unsigned short x_min; /* Camera capabilities */
  24. unsigned short y_min;
  25. unsigned short x_current; /* Current window location */
  26. unsigned short y_current;
  27. unsigned short width_min;
  28. unsigned short width_max;
  29. unsigned short height_min;
  30. unsigned short height_max;
  31. unsigned short y_skip_top; /* Lines to skip at the top */
  32. unsigned short gain;
  33. unsigned short exposure;
  34. unsigned char iface; /* Host number */
  35. unsigned char devnum; /* Device number per host */
  36. unsigned char buswidth; /* See comment in .c */
  37. struct soc_camera_sense *sense; /* See comment in struct definition */
  38. struct soc_camera_ops *ops;
  39. struct video_device *vdev;
  40. const struct soc_camera_data_format *current_fmt;
  41. const struct soc_camera_data_format *formats;
  42. int num_formats;
  43. struct soc_camera_format_xlate *user_formats;
  44. int num_user_formats;
  45. struct module *owner;
  46. void *host_priv; /* Per-device host private data */
  47. /* soc_camera.c private count. Only accessed with .video_lock held */
  48. int use_count;
  49. struct mutex video_lock; /* Protects device data */
  50. };
  51. struct soc_camera_file {
  52. struct soc_camera_device *icd;
  53. struct videobuf_queue vb_vidq;
  54. };
  55. struct soc_camera_host {
  56. struct list_head list;
  57. struct device dev;
  58. unsigned char nr; /* Host number */
  59. void *priv;
  60. const char *drv_name;
  61. struct soc_camera_host_ops *ops;
  62. };
  63. struct soc_camera_host_ops {
  64. struct module *owner;
  65. int (*add)(struct soc_camera_device *);
  66. void (*remove)(struct soc_camera_device *);
  67. int (*suspend)(struct soc_camera_device *, pm_message_t);
  68. int (*resume)(struct soc_camera_device *);
  69. int (*get_formats)(struct soc_camera_device *, int,
  70. struct soc_camera_format_xlate *);
  71. int (*set_fmt)(struct soc_camera_device *, __u32, struct v4l2_rect *);
  72. int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
  73. void (*init_videobuf)(struct videobuf_queue *,
  74. struct soc_camera_device *);
  75. int (*reqbufs)(struct soc_camera_file *, struct v4l2_requestbuffers *);
  76. int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
  77. int (*set_bus_param)(struct soc_camera_device *, __u32);
  78. unsigned int (*poll)(struct file *, poll_table *);
  79. };
  80. #define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
  81. #define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
  82. #define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
  83. #define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
  84. #define SOCAM_SENSOR_INVERT_DATA (1 << 4)
  85. struct soc_camera_link {
  86. /* Camera bus id, used to match a camera and a bus */
  87. int bus_id;
  88. /* GPIO number to switch between 8 and 10 bit modes */
  89. unsigned int gpio;
  90. /* Per camera SOCAM_SENSOR_* bus flags */
  91. unsigned long flags;
  92. /* Optional callbacks to power on or off and reset the sensor */
  93. int (*power)(struct device *, int);
  94. int (*reset)(struct device *);
  95. };
  96. static inline struct soc_camera_device *to_soc_camera_dev(struct device *dev)
  97. {
  98. return container_of(dev, struct soc_camera_device, dev);
  99. }
  100. static inline struct soc_camera_host *to_soc_camera_host(struct device *dev)
  101. {
  102. return container_of(dev, struct soc_camera_host, dev);
  103. }
  104. extern int soc_camera_host_register(struct soc_camera_host *ici);
  105. extern void soc_camera_host_unregister(struct soc_camera_host *ici);
  106. extern int soc_camera_device_register(struct soc_camera_device *icd);
  107. extern void soc_camera_device_unregister(struct soc_camera_device *icd);
  108. extern int soc_camera_video_start(struct soc_camera_device *icd);
  109. extern void soc_camera_video_stop(struct soc_camera_device *icd);
  110. extern const struct soc_camera_data_format *soc_camera_format_by_fourcc(
  111. struct soc_camera_device *icd, unsigned int fourcc);
  112. extern const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  113. struct soc_camera_device *icd, unsigned int fourcc);
  114. struct soc_camera_data_format {
  115. const char *name;
  116. unsigned int depth;
  117. __u32 fourcc;
  118. enum v4l2_colorspace colorspace;
  119. };
  120. /**
  121. * struct soc_camera_format_xlate - match between host and sensor formats
  122. * @cam_fmt: sensor format provided by the sensor
  123. * @host_fmt: host format after host translation from cam_fmt
  124. * @buswidth: bus width for this format
  125. *
  126. * Host and sensor translation structure. Used in table of host and sensor
  127. * formats matchings in soc_camera_device. A host can override the generic list
  128. * generation by implementing get_formats(), and use it for format checks and
  129. * format setup.
  130. */
  131. struct soc_camera_format_xlate {
  132. const struct soc_camera_data_format *cam_fmt;
  133. const struct soc_camera_data_format *host_fmt;
  134. unsigned char buswidth;
  135. };
  136. struct soc_camera_ops {
  137. struct module *owner;
  138. int (*probe)(struct soc_camera_device *);
  139. void (*remove)(struct soc_camera_device *);
  140. int (*suspend)(struct soc_camera_device *, pm_message_t state);
  141. int (*resume)(struct soc_camera_device *);
  142. int (*init)(struct soc_camera_device *);
  143. int (*release)(struct soc_camera_device *);
  144. int (*start_capture)(struct soc_camera_device *);
  145. int (*stop_capture)(struct soc_camera_device *);
  146. int (*set_fmt)(struct soc_camera_device *, __u32, struct v4l2_rect *);
  147. int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
  148. unsigned long (*query_bus_param)(struct soc_camera_device *);
  149. int (*set_bus_param)(struct soc_camera_device *, unsigned long);
  150. int (*get_chip_id)(struct soc_camera_device *,
  151. struct v4l2_dbg_chip_ident *);
  152. int (*set_std)(struct soc_camera_device *, v4l2_std_id *);
  153. int (*enum_input)(struct soc_camera_device *, struct v4l2_input *);
  154. #ifdef CONFIG_VIDEO_ADV_DEBUG
  155. int (*get_register)(struct soc_camera_device *, struct v4l2_dbg_register *);
  156. int (*set_register)(struct soc_camera_device *, struct v4l2_dbg_register *);
  157. #endif
  158. int (*get_control)(struct soc_camera_device *, struct v4l2_control *);
  159. int (*set_control)(struct soc_camera_device *, struct v4l2_control *);
  160. const struct v4l2_queryctrl *controls;
  161. int num_controls;
  162. };
  163. #define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
  164. /**
  165. * This struct can be attached to struct soc_camera_device by the host driver
  166. * to request sense from the camera, for example, when calling .set_fmt(). The
  167. * host then can check which flags are set and verify respective values if any.
  168. * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
  169. * changed during this operation. After completion the host should detach sense.
  170. *
  171. * @flags ored SOCAM_SENSE_* flags
  172. * @master_clock if the host wants to be informed about pixel-clock
  173. * change, it better set master_clock.
  174. * @pixel_clock_max maximum pixel clock frequency supported by the host,
  175. * camera is not allowed to exceed this.
  176. * @pixel_clock if the camera driver changed pixel clock during this
  177. * operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
  178. * master_clock to calculate the new pixel-clock and
  179. * sets this field.
  180. */
  181. struct soc_camera_sense {
  182. unsigned long flags;
  183. unsigned long master_clock;
  184. unsigned long pixel_clock_max;
  185. unsigned long pixel_clock;
  186. };
  187. static inline struct v4l2_queryctrl const *soc_camera_find_qctrl(
  188. struct soc_camera_ops *ops, int id)
  189. {
  190. int i;
  191. for (i = 0; i < ops->num_controls; i++)
  192. if (ops->controls[i].id == id)
  193. return &ops->controls[i];
  194. return NULL;
  195. }
  196. #define SOCAM_MASTER (1 << 0)
  197. #define SOCAM_SLAVE (1 << 1)
  198. #define SOCAM_HSYNC_ACTIVE_HIGH (1 << 2)
  199. #define SOCAM_HSYNC_ACTIVE_LOW (1 << 3)
  200. #define SOCAM_VSYNC_ACTIVE_HIGH (1 << 4)
  201. #define SOCAM_VSYNC_ACTIVE_LOW (1 << 5)
  202. #define SOCAM_DATAWIDTH_4 (1 << 6)
  203. #define SOCAM_DATAWIDTH_8 (1 << 7)
  204. #define SOCAM_DATAWIDTH_9 (1 << 8)
  205. #define SOCAM_DATAWIDTH_10 (1 << 9)
  206. #define SOCAM_DATAWIDTH_15 (1 << 10)
  207. #define SOCAM_DATAWIDTH_16 (1 << 11)
  208. #define SOCAM_PCLK_SAMPLE_RISING (1 << 12)
  209. #define SOCAM_PCLK_SAMPLE_FALLING (1 << 13)
  210. #define SOCAM_DATA_ACTIVE_HIGH (1 << 14)
  211. #define SOCAM_DATA_ACTIVE_LOW (1 << 15)
  212. #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
  213. SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
  214. SOCAM_DATAWIDTH_15 | SOCAM_DATAWIDTH_16)
  215. static inline unsigned long soc_camera_bus_param_compatible(
  216. unsigned long camera_flags, unsigned long bus_flags)
  217. {
  218. unsigned long common_flags, hsync, vsync, pclk;
  219. common_flags = camera_flags & bus_flags;
  220. hsync = common_flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
  221. vsync = common_flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
  222. pclk = common_flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
  223. return (!hsync || !vsync || !pclk) ? 0 : common_flags;
  224. }
  225. extern unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
  226. unsigned long flags);
  227. #endif