linux/include/media/soc_camera.h
<<
>>
Prefs
   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
  12#ifndef SOC_CAMERA_H
  13#define SOC_CAMERA_H
  14
  15#include <linux/bitops.h>
  16#include <linux/device.h>
  17#include <linux/mutex.h>
  18#include <linux/pm.h>
  19#include <linux/videodev2.h>
  20#include <media/videobuf-core.h>
  21#include <media/videobuf2-v4l2.h>
  22#include <media/v4l2-async.h>
  23#include <media/v4l2-ctrls.h>
  24#include <media/v4l2-device.h>
  25
  26struct file;
  27struct soc_camera_desc;
  28struct soc_camera_async_client;
  29
  30struct soc_camera_device {
  31        struct list_head list;          /* list of all registered devices */
  32        struct soc_camera_desc *sdesc;
  33        struct device *pdev;            /* Platform device */
  34        struct device *parent;          /* Camera host device */
  35        struct device *control;         /* E.g., the i2c client */
  36        s32 user_width;
  37        s32 user_height;
  38        u32 bytesperline;               /* for padding, zero if unused */
  39        u32 sizeimage;
  40        enum v4l2_colorspace colorspace;
  41        unsigned char iface;            /* Host number */
  42        unsigned char devnum;           /* Device number per host */
  43        struct soc_camera_sense *sense; /* See comment in struct definition */
  44        struct video_device *vdev;
  45        struct v4l2_ctrl_handler ctrl_handler;
  46        const struct soc_camera_format_xlate *current_fmt;
  47        struct soc_camera_format_xlate *user_formats;
  48        int num_user_formats;
  49        enum v4l2_field field;          /* Preserve field over close() */
  50        void *host_priv;                /* Per-device host private data */
  51        /* soc_camera.c private count. Only accessed with .host_lock held */
  52        int use_count;
  53        struct file *streamer;          /* stream owner */
  54        struct v4l2_clk *clk;
  55        /* Asynchronous subdevice management */
  56        struct soc_camera_async_client *sasc;
  57        /* video buffer queue */
  58        union {
  59                struct videobuf_queue vb_vidq;
  60                struct vb2_queue vb2_vidq;
  61        };
  62};
  63
  64/* Host supports programmable stride */
  65#define SOCAM_HOST_CAP_STRIDE           (1 << 0)
  66
  67enum soc_camera_subdev_role {
  68        SOCAM_SUBDEV_DATA_SOURCE = 1,
  69        SOCAM_SUBDEV_DATA_SINK,
  70        SOCAM_SUBDEV_DATA_PROCESSOR,
  71};
  72
  73struct soc_camera_async_subdev {
  74        struct v4l2_async_subdev asd;
  75        enum soc_camera_subdev_role role;
  76};
  77
  78struct soc_camera_host {
  79        struct v4l2_device v4l2_dev;
  80        struct list_head list;
  81        struct mutex host_lock;         /* Main synchronisation lock */
  82        struct mutex clk_lock;          /* Protect pipeline modifications */
  83        unsigned char nr;               /* Host number */
  84        u32 capabilities;
  85        struct soc_camera_device *icd;  /* Currently attached client */
  86        void *priv;
  87        const char *drv_name;
  88        struct soc_camera_host_ops *ops;
  89        struct v4l2_async_subdev **asd; /* Flat array, arranged in groups */
  90        unsigned int *asd_sizes;        /* 0-terminated array of asd group sizes */
  91};
  92
  93struct soc_camera_host_ops {
  94        struct module *owner;
  95        int (*add)(struct soc_camera_device *);
  96        void (*remove)(struct soc_camera_device *);
  97        int (*clock_start)(struct soc_camera_host *);
  98        void (*clock_stop)(struct soc_camera_host *);
  99        /*
 100         * .get_formats() is called for each client device format, but
 101         * .put_formats() is only called once. Further, if any of the calls to
 102         * .get_formats() fail, .put_formats() will not be called at all, the
 103         * failing .get_formats() must then clean up internally.
 104         */
 105        int (*get_formats)(struct soc_camera_device *, unsigned int,
 106                           struct soc_camera_format_xlate *);
 107        void (*put_formats)(struct soc_camera_device *);
 108        int (*get_selection)(struct soc_camera_device *, struct v4l2_selection *);
 109        int (*set_selection)(struct soc_camera_device *, struct v4l2_selection *);
 110        /*
 111         * The difference to .set_selection() is, that .set_liveselection is not allowed
 112         * to change the output sizes
 113         */
 114        int (*set_liveselection)(struct soc_camera_device *, struct v4l2_selection *);
 115        int (*set_fmt)(struct soc_camera_device *, struct v4l2_format *);
 116        int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
 117        void (*init_videobuf)(struct videobuf_queue *,
 118                              struct soc_camera_device *);
 119        int (*init_videobuf2)(struct vb2_queue *,
 120                              struct soc_camera_device *);
 121        int (*reqbufs)(struct soc_camera_device *, struct v4l2_requestbuffers *);
 122        int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
 123        int (*set_bus_param)(struct soc_camera_device *);
 124        int (*get_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
 125        int (*set_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
 126        int (*enum_framesizes)(struct soc_camera_device *, struct v4l2_frmsizeenum *);
 127        unsigned int (*poll)(struct file *, poll_table *);
 128};
 129
 130#define SOCAM_SENSOR_INVERT_PCLK        (1 << 0)
 131#define SOCAM_SENSOR_INVERT_MCLK        (1 << 1)
 132#define SOCAM_SENSOR_INVERT_HSYNC       (1 << 2)
 133#define SOCAM_SENSOR_INVERT_VSYNC       (1 << 3)
 134#define SOCAM_SENSOR_INVERT_DATA        (1 << 4)
 135
 136struct i2c_board_info;
 137struct regulator_bulk_data;
 138
 139struct soc_camera_subdev_desc {
 140        /* Per camera SOCAM_SENSOR_* bus flags */
 141        unsigned long flags;
 142
 143        /* sensor driver private platform data */
 144        void *drv_priv;
 145
 146        /*
 147         * Set unbalanced_power to true to deal with legacy drivers, failing to
 148         * balance their calls to subdevice's .s_power() method. clock_state is
 149         * then used internally by helper functions, it shouldn't be touched by
 150         * drivers or the platform code.
 151         */
 152        bool unbalanced_power;
 153        unsigned long clock_state;
 154
 155        /* Optional callbacks to power on or off and reset the sensor */
 156        int (*power)(struct device *, int);
 157        int (*reset)(struct device *);
 158
 159        /*
 160         * some platforms may support different data widths than the sensors
 161         * native ones due to different data line routing. Let the board code
 162         * overwrite the width flags.
 163         */
 164        int (*set_bus_param)(struct soc_camera_subdev_desc *, unsigned long flags);
 165        unsigned long (*query_bus_param)(struct soc_camera_subdev_desc *);
 166        void (*free_bus)(struct soc_camera_subdev_desc *);
 167
 168        /* Optional regulators that have to be managed on power on/off events */
 169        struct v4l2_subdev_platform_data sd_pdata;
 170};
 171
 172struct soc_camera_host_desc {
 173        /* Camera bus id, used to match a camera and a bus */
 174        int bus_id;
 175        int i2c_adapter_id;
 176        struct i2c_board_info *board_info;
 177        const char *module_name;
 178
 179        /*
 180         * For non-I2C devices platform has to provide methods to add a device
 181         * to the system and to remove it
 182         */
 183        int (*add_device)(struct soc_camera_device *);
 184        void (*del_device)(struct soc_camera_device *);
 185};
 186
 187/*
 188 * Platform data for "soc-camera-pdrv"
 189 * This MUST be kept binary-identical to struct soc_camera_link below, until
 190 * it is completely replaced by this one, after which we can split it into its
 191 * two components.
 192 */
 193struct soc_camera_desc {
 194        struct soc_camera_subdev_desc subdev_desc;
 195        struct soc_camera_host_desc host_desc;
 196};
 197
 198/* Prepare to replace this struct: don't change its layout any more! */
 199struct soc_camera_link {
 200        /*
 201         * Subdevice part - keep at top and compatible to
 202         * struct soc_camera_subdev_desc
 203         */
 204
 205        /* Per camera SOCAM_SENSOR_* bus flags */
 206        unsigned long flags;
 207
 208        void *priv;
 209
 210        /* Set by platforms to handle misbehaving drivers */
 211        bool unbalanced_power;
 212        /* Used by soc-camera helper functions */
 213        unsigned long clock_state;
 214
 215        /* Optional callbacks to power on or off and reset the sensor */
 216        int (*power)(struct device *, int);
 217        int (*reset)(struct device *);
 218        /*
 219         * some platforms may support different data widths than the sensors
 220         * native ones due to different data line routing. Let the board code
 221         * overwrite the width flags.
 222         */
 223        int (*set_bus_param)(struct soc_camera_link *, unsigned long flags);
 224        unsigned long (*query_bus_param)(struct soc_camera_link *);
 225        void (*free_bus)(struct soc_camera_link *);
 226
 227        /* Optional regulators that have to be managed on power on/off events */
 228        struct regulator_bulk_data *regulators;
 229        int num_regulators;
 230
 231        void *host_priv;
 232
 233        /*
 234         * Host part - keep at bottom and compatible to
 235         * struct soc_camera_host_desc
 236         */
 237
 238        /* Camera bus id, used to match a camera and a bus */
 239        int bus_id;
 240        int i2c_adapter_id;
 241        struct i2c_board_info *board_info;
 242        const char *module_name;
 243
 244        /*
 245         * For non-I2C devices platform has to provide methods to add a device
 246         * to the system and to remove it
 247         */
 248        int (*add_device)(struct soc_camera_device *);
 249        void (*del_device)(struct soc_camera_device *);
 250};
 251
 252static inline struct soc_camera_host *to_soc_camera_host(
 253        const struct device *dev)
 254{
 255        struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
 256
 257        return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
 258}
 259
 260static inline struct soc_camera_desc *to_soc_camera_desc(
 261        const struct soc_camera_device *icd)
 262{
 263        return icd->sdesc;
 264}
 265
 266static inline struct device *to_soc_camera_control(
 267        const struct soc_camera_device *icd)
 268{
 269        return icd->control;
 270}
 271
 272static inline struct v4l2_subdev *soc_camera_to_subdev(
 273        const struct soc_camera_device *icd)
 274{
 275        struct device *control = to_soc_camera_control(icd);
 276        return dev_get_drvdata(control);
 277}
 278
 279int soc_camera_host_register(struct soc_camera_host *ici);
 280void soc_camera_host_unregister(struct soc_camera_host *ici);
 281
 282const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
 283        struct soc_camera_device *icd, unsigned int fourcc);
 284
 285/**
 286 * struct soc_camera_format_xlate - match between host and sensor formats
 287 * @code: code of a sensor provided format
 288 * @host_fmt: host format after host translation from code
 289 *
 290 * Host and sensor translation structure. Used in table of host and sensor
 291 * formats matchings in soc_camera_device. A host can override the generic list
 292 * generation by implementing get_formats(), and use it for format checks and
 293 * format setup.
 294 */
 295struct soc_camera_format_xlate {
 296        u32 code;
 297        const struct soc_mbus_pixelfmt *host_fmt;
 298};
 299
 300#define SOCAM_SENSE_PCLK_CHANGED        (1 << 0)
 301
 302/**
 303 * This struct can be attached to struct soc_camera_device by the host driver
 304 * to request sense from the camera, for example, when calling .set_fmt(). The
 305 * host then can check which flags are set and verify respective values if any.
 306 * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
 307 * changed during this operation. After completion the host should detach sense.
 308 *
 309 * @flags               ored SOCAM_SENSE_* flags
 310 * @master_clock        if the host wants to be informed about pixel-clock
 311 *                      change, it better set master_clock.
 312 * @pixel_clock_max     maximum pixel clock frequency supported by the host,
 313 *                      camera is not allowed to exceed this.
 314 * @pixel_clock         if the camera driver changed pixel clock during this
 315 *                      operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
 316 *                      master_clock to calculate the new pixel-clock and
 317 *                      sets this field.
 318 */
 319struct soc_camera_sense {
 320        unsigned long flags;
 321        unsigned long master_clock;
 322        unsigned long pixel_clock_max;
 323        unsigned long pixel_clock;
 324};
 325
 326#define SOCAM_DATAWIDTH(x)      BIT((x) - 1)
 327#define SOCAM_DATAWIDTH_4       SOCAM_DATAWIDTH(4)
 328#define SOCAM_DATAWIDTH_8       SOCAM_DATAWIDTH(8)
 329#define SOCAM_DATAWIDTH_9       SOCAM_DATAWIDTH(9)
 330#define SOCAM_DATAWIDTH_10      SOCAM_DATAWIDTH(10)
 331#define SOCAM_DATAWIDTH_12      SOCAM_DATAWIDTH(12)
 332#define SOCAM_DATAWIDTH_15      SOCAM_DATAWIDTH(15)
 333#define SOCAM_DATAWIDTH_16      SOCAM_DATAWIDTH(16)
 334#define SOCAM_DATAWIDTH_18      SOCAM_DATAWIDTH(18)
 335#define SOCAM_DATAWIDTH_24      SOCAM_DATAWIDTH(24)
 336
 337#define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
 338                              SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
 339                              SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
 340                              SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
 341                              SOCAM_DATAWIDTH_24)
 342
 343static inline void soc_camera_limit_side(int *start, int *length,
 344                unsigned int start_min,
 345                unsigned int length_min, unsigned int length_max)
 346{
 347        if (*length < length_min)
 348                *length = length_min;
 349        else if (*length > length_max)
 350                *length = length_max;
 351
 352        if (*start < start_min)
 353                *start = start_min;
 354        else if (*start > start_min + length_max - *length)
 355                *start = start_min + length_max - *length;
 356}
 357
 358unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
 359                                           const struct v4l2_mbus_config *cfg);
 360
 361int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd);
 362int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
 363                        struct v4l2_clk *clk);
 364int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
 365                         struct v4l2_clk *clk);
 366
 367static inline int soc_camera_set_power(struct device *dev,
 368                struct soc_camera_subdev_desc *ssdd, struct v4l2_clk *clk, bool on)
 369{
 370        return on ? soc_camera_power_on(dev, ssdd, clk)
 371                  : soc_camera_power_off(dev, ssdd, clk);
 372}
 373
 374/* This is only temporary here - until v4l2-subdev begins to link to video_device */
 375#include <linux/i2c.h>
 376static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client)
 377{
 378        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 379        struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
 380        return icd ? icd->vdev : NULL;
 381}
 382
 383static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
 384{
 385        return client->dev.platform_data;
 386}
 387
 388static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(struct video_device *vdev)
 389{
 390        struct soc_camera_device *icd = video_get_drvdata(vdev);
 391        return soc_camera_to_subdev(icd);
 392}
 393
 394static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
 395{
 396        return container_of(vq, struct soc_camera_device, vb2_vidq);
 397}
 398
 399static inline struct soc_camera_device *soc_camera_from_vbq(const struct videobuf_queue *vq)
 400{
 401        return container_of(vq, struct soc_camera_device, vb_vidq);
 402}
 403
 404static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd)
 405{
 406        return (icd->iface << 8) | (icd->devnum + 1);
 407}
 408
 409void soc_camera_lock(struct vb2_queue *vq);
 410void soc_camera_unlock(struct vb2_queue *vq);
 411
 412#endif
 413