soc_camera.h 8.5 KB

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