linux/drivers/media/platform/omap3isp/isph3a_aewb.c
<<
>>
Prefs
   1/*
   2 * isph3a.c
   3 *
   4 * TI OMAP3 ISP - H3A module
   5 *
   6 * Copyright (C) 2010 Nokia Corporation
   7 * Copyright (C) 2009 Texas Instruments, Inc.
   8 *
   9 * Contacts: David Cohen <dacohen@gmail.com>
  10 *           Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  11 *           Sakari Ailus <sakari.ailus@iki.fi>
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License version 2 as
  15 * published by the Free Software Foundation.
  16 *
  17 * This program is distributed in the hope that it will be useful, but
  18 * WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20 * General Public License for more details.
  21 *
  22 * You should have received a copy of the GNU General Public License
  23 * along with this program; if not, write to the Free Software
  24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  25 * 02110-1301 USA
  26 */
  27
  28#include <linux/slab.h>
  29#include <linux/uaccess.h>
  30
  31#include "isp.h"
  32#include "isph3a.h"
  33#include "ispstat.h"
  34
  35/*
  36 * h3a_aewb_update_regs - Helper function to update h3a registers.
  37 */
  38static void h3a_aewb_setup_regs(struct ispstat *aewb, void *priv)
  39{
  40        struct omap3isp_h3a_aewb_config *conf = priv;
  41        u32 pcr;
  42        u32 win1;
  43        u32 start;
  44        u32 blk;
  45        u32 subwin;
  46
  47        if (aewb->state == ISPSTAT_DISABLED)
  48                return;
  49
  50        isp_reg_writel(aewb->isp, aewb->active_buf->iommu_addr,
  51                       OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWBUFST);
  52
  53        if (!aewb->update)
  54                return;
  55
  56        /* Converting config metadata into reg values */
  57        pcr = conf->saturation_limit << ISPH3A_PCR_AEW_AVE2LMT_SHIFT;
  58        pcr |= !!conf->alaw_enable << ISPH3A_PCR_AEW_ALAW_EN_SHIFT;
  59
  60        win1 = ((conf->win_height >> 1) - 1) << ISPH3A_AEWWIN1_WINH_SHIFT;
  61        win1 |= ((conf->win_width >> 1) - 1) << ISPH3A_AEWWIN1_WINW_SHIFT;
  62        win1 |= (conf->ver_win_count - 1) << ISPH3A_AEWWIN1_WINVC_SHIFT;
  63        win1 |= (conf->hor_win_count - 1) << ISPH3A_AEWWIN1_WINHC_SHIFT;
  64
  65        start = conf->hor_win_start << ISPH3A_AEWINSTART_WINSH_SHIFT;
  66        start |= conf->ver_win_start << ISPH3A_AEWINSTART_WINSV_SHIFT;
  67
  68        blk = conf->blk_ver_win_start << ISPH3A_AEWINBLK_WINSV_SHIFT;
  69        blk |= ((conf->blk_win_height >> 1) - 1) << ISPH3A_AEWINBLK_WINH_SHIFT;
  70
  71        subwin = ((conf->subsample_ver_inc >> 1) - 1) <<
  72                 ISPH3A_AEWSUBWIN_AEWINCV_SHIFT;
  73        subwin |= ((conf->subsample_hor_inc >> 1) - 1) <<
  74                  ISPH3A_AEWSUBWIN_AEWINCH_SHIFT;
  75
  76        isp_reg_writel(aewb->isp, win1, OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWWIN1);
  77        isp_reg_writel(aewb->isp, start, OMAP3_ISP_IOMEM_H3A,
  78                       ISPH3A_AEWINSTART);
  79        isp_reg_writel(aewb->isp, blk, OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWINBLK);
  80        isp_reg_writel(aewb->isp, subwin, OMAP3_ISP_IOMEM_H3A,
  81                       ISPH3A_AEWSUBWIN);
  82        isp_reg_clr_set(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
  83                        ISPH3A_PCR_AEW_MASK, pcr);
  84
  85        aewb->update = 0;
  86        aewb->config_counter += aewb->inc_config;
  87        aewb->inc_config = 0;
  88        aewb->buf_size = conf->buf_size;
  89}
  90
  91static void h3a_aewb_enable(struct ispstat *aewb, int enable)
  92{
  93        if (enable) {
  94                isp_reg_set(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
  95                            ISPH3A_PCR_AEW_EN);
  96                omap3isp_subclk_enable(aewb->isp, OMAP3_ISP_SUBCLK_AEWB);
  97        } else {
  98                isp_reg_clr(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
  99                            ISPH3A_PCR_AEW_EN);
 100                omap3isp_subclk_disable(aewb->isp, OMAP3_ISP_SUBCLK_AEWB);
 101        }
 102}
 103
 104static int h3a_aewb_busy(struct ispstat *aewb)
 105{
 106        return isp_reg_readl(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR)
 107                                                & ISPH3A_PCR_BUSYAEAWB;
 108}
 109
 110static u32 h3a_aewb_get_buf_size(struct omap3isp_h3a_aewb_config *conf)
 111{
 112        /* Number of configured windows + extra row for black data */
 113        u32 win_count = (conf->ver_win_count + 1) * conf->hor_win_count;
 114
 115        /*
 116         * Unsaturated block counts for each 8 windows.
 117         * 1 extra for the last (win_count % 8) windows if win_count is not
 118         * divisible by 8.
 119         */
 120        win_count += (win_count + 7) / 8;
 121
 122        return win_count * AEWB_PACKET_SIZE;
 123}
 124
 125static int h3a_aewb_validate_params(struct ispstat *aewb, void *new_conf)
 126{
 127        struct omap3isp_h3a_aewb_config *user_cfg = new_conf;
 128        u32 buf_size;
 129
 130        if (unlikely(user_cfg->saturation_limit >
 131                     OMAP3ISP_AEWB_MAX_SATURATION_LIM))
 132                return -EINVAL;
 133
 134        if (unlikely(user_cfg->win_height < OMAP3ISP_AEWB_MIN_WIN_H ||
 135                     user_cfg->win_height > OMAP3ISP_AEWB_MAX_WIN_H ||
 136                     user_cfg->win_height & 0x01))
 137                return -EINVAL;
 138
 139        if (unlikely(user_cfg->win_width < OMAP3ISP_AEWB_MIN_WIN_W ||
 140                     user_cfg->win_width > OMAP3ISP_AEWB_MAX_WIN_W ||
 141                     user_cfg->win_width & 0x01))
 142                return -EINVAL;
 143
 144        if (unlikely(user_cfg->ver_win_count < OMAP3ISP_AEWB_MIN_WINVC ||
 145                     user_cfg->ver_win_count > OMAP3ISP_AEWB_MAX_WINVC))
 146                return -EINVAL;
 147
 148        if (unlikely(user_cfg->hor_win_count < OMAP3ISP_AEWB_MIN_WINHC ||
 149                     user_cfg->hor_win_count > OMAP3ISP_AEWB_MAX_WINHC))
 150                return -EINVAL;
 151
 152        if (unlikely(user_cfg->ver_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
 153                return -EINVAL;
 154
 155        if (unlikely(user_cfg->hor_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
 156                return -EINVAL;
 157
 158        if (unlikely(user_cfg->blk_ver_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
 159                return -EINVAL;
 160
 161        if (unlikely(user_cfg->blk_win_height < OMAP3ISP_AEWB_MIN_WIN_H ||
 162                     user_cfg->blk_win_height > OMAP3ISP_AEWB_MAX_WIN_H ||
 163                     user_cfg->blk_win_height & 0x01))
 164                return -EINVAL;
 165
 166        if (unlikely(user_cfg->subsample_ver_inc < OMAP3ISP_AEWB_MIN_SUB_INC ||
 167                     user_cfg->subsample_ver_inc > OMAP3ISP_AEWB_MAX_SUB_INC ||
 168                     user_cfg->subsample_ver_inc & 0x01))
 169                return -EINVAL;
 170
 171        if (unlikely(user_cfg->subsample_hor_inc < OMAP3ISP_AEWB_MIN_SUB_INC ||
 172                     user_cfg->subsample_hor_inc > OMAP3ISP_AEWB_MAX_SUB_INC ||
 173                     user_cfg->subsample_hor_inc & 0x01))
 174                return -EINVAL;
 175
 176        buf_size = h3a_aewb_get_buf_size(user_cfg);
 177        if (buf_size > user_cfg->buf_size)
 178                user_cfg->buf_size = buf_size;
 179        else if (user_cfg->buf_size > OMAP3ISP_AEWB_MAX_BUF_SIZE)
 180                user_cfg->buf_size = OMAP3ISP_AEWB_MAX_BUF_SIZE;
 181
 182        return 0;
 183}
 184
 185/*
 186 * h3a_aewb_set_params - Helper function to check & store user given params.
 187 * @new_conf: Pointer to AE and AWB parameters struct.
 188 *
 189 * As most of them are busy-lock registers, need to wait until AEW_BUSY = 0 to
 190 * program them during ISR.
 191 */
 192static void h3a_aewb_set_params(struct ispstat *aewb, void *new_conf)
 193{
 194        struct omap3isp_h3a_aewb_config *user_cfg = new_conf;
 195        struct omap3isp_h3a_aewb_config *cur_cfg = aewb->priv;
 196        int update = 0;
 197
 198        if (cur_cfg->saturation_limit != user_cfg->saturation_limit) {
 199                cur_cfg->saturation_limit = user_cfg->saturation_limit;
 200                update = 1;
 201        }
 202        if (cur_cfg->alaw_enable != user_cfg->alaw_enable) {
 203                cur_cfg->alaw_enable = user_cfg->alaw_enable;
 204                update = 1;
 205        }
 206        if (cur_cfg->win_height != user_cfg->win_height) {
 207                cur_cfg->win_height = user_cfg->win_height;
 208                update = 1;
 209        }
 210        if (cur_cfg->win_width != user_cfg->win_width) {
 211                cur_cfg->win_width = user_cfg->win_width;
 212                update = 1;
 213        }
 214        if (cur_cfg->ver_win_count != user_cfg->ver_win_count) {
 215                cur_cfg->ver_win_count = user_cfg->ver_win_count;
 216                update = 1;
 217        }
 218        if (cur_cfg->hor_win_count != user_cfg->hor_win_count) {
 219                cur_cfg->hor_win_count = user_cfg->hor_win_count;
 220                update = 1;
 221        }
 222        if (cur_cfg->ver_win_start != user_cfg->ver_win_start) {
 223                cur_cfg->ver_win_start = user_cfg->ver_win_start;
 224                update = 1;
 225        }
 226        if (cur_cfg->hor_win_start != user_cfg->hor_win_start) {
 227                cur_cfg->hor_win_start = user_cfg->hor_win_start;
 228                update = 1;
 229        }
 230        if (cur_cfg->blk_ver_win_start != user_cfg->blk_ver_win_start) {
 231                cur_cfg->blk_ver_win_start = user_cfg->blk_ver_win_start;
 232                update = 1;
 233        }
 234        if (cur_cfg->blk_win_height != user_cfg->blk_win_height) {
 235                cur_cfg->blk_win_height = user_cfg->blk_win_height;
 236                update = 1;
 237        }
 238        if (cur_cfg->subsample_ver_inc != user_cfg->subsample_ver_inc) {
 239                cur_cfg->subsample_ver_inc = user_cfg->subsample_ver_inc;
 240                update = 1;
 241        }
 242        if (cur_cfg->subsample_hor_inc != user_cfg->subsample_hor_inc) {
 243                cur_cfg->subsample_hor_inc = user_cfg->subsample_hor_inc;
 244                update = 1;
 245        }
 246
 247        if (update || !aewb->configured) {
 248                aewb->inc_config++;
 249                aewb->update = 1;
 250                cur_cfg->buf_size = h3a_aewb_get_buf_size(cur_cfg);
 251        }
 252}
 253
 254static long h3a_aewb_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
 255{
 256        struct ispstat *stat = v4l2_get_subdevdata(sd);
 257
 258        switch (cmd) {
 259        case VIDIOC_OMAP3ISP_AEWB_CFG:
 260                return omap3isp_stat_config(stat, arg);
 261        case VIDIOC_OMAP3ISP_STAT_REQ:
 262                return omap3isp_stat_request_statistics(stat, arg);
 263        case VIDIOC_OMAP3ISP_STAT_EN: {
 264                unsigned long *en = arg;
 265                return omap3isp_stat_enable(stat, !!*en);
 266        }
 267        }
 268
 269        return -ENOIOCTLCMD;
 270}
 271
 272static const struct ispstat_ops h3a_aewb_ops = {
 273        .validate_params        = h3a_aewb_validate_params,
 274        .set_params             = h3a_aewb_set_params,
 275        .setup_regs             = h3a_aewb_setup_regs,
 276        .enable                 = h3a_aewb_enable,
 277        .busy                   = h3a_aewb_busy,
 278};
 279
 280static const struct v4l2_subdev_core_ops h3a_aewb_subdev_core_ops = {
 281        .ioctl = h3a_aewb_ioctl,
 282        .subscribe_event = omap3isp_stat_subscribe_event,
 283        .unsubscribe_event = omap3isp_stat_unsubscribe_event,
 284};
 285
 286static const struct v4l2_subdev_video_ops h3a_aewb_subdev_video_ops = {
 287        .s_stream = omap3isp_stat_s_stream,
 288};
 289
 290static const struct v4l2_subdev_ops h3a_aewb_subdev_ops = {
 291        .core = &h3a_aewb_subdev_core_ops,
 292        .video = &h3a_aewb_subdev_video_ops,
 293};
 294
 295/*
 296 * omap3isp_h3a_aewb_init - Module Initialisation.
 297 */
 298int omap3isp_h3a_aewb_init(struct isp_device *isp)
 299{
 300        struct ispstat *aewb = &isp->isp_aewb;
 301        struct omap3isp_h3a_aewb_config *aewb_cfg;
 302        struct omap3isp_h3a_aewb_config *aewb_recover_cfg;
 303        int ret;
 304
 305        aewb_cfg = kzalloc(sizeof(*aewb_cfg), GFP_KERNEL);
 306        if (!aewb_cfg)
 307                return -ENOMEM;
 308
 309        memset(aewb, 0, sizeof(*aewb));
 310        aewb->ops = &h3a_aewb_ops;
 311        aewb->priv = aewb_cfg;
 312        aewb->dma_ch = -1;
 313        aewb->event_type = V4L2_EVENT_OMAP3ISP_AEWB;
 314        aewb->isp = isp;
 315
 316        /* Set recover state configuration */
 317        aewb_recover_cfg = kzalloc(sizeof(*aewb_recover_cfg), GFP_KERNEL);
 318        if (!aewb_recover_cfg) {
 319                dev_err(aewb->isp->dev, "AEWB: cannot allocate memory for "
 320                                        "recover configuration.\n");
 321                ret = -ENOMEM;
 322                goto err_recover_alloc;
 323        }
 324
 325        aewb_recover_cfg->saturation_limit = OMAP3ISP_AEWB_MAX_SATURATION_LIM;
 326        aewb_recover_cfg->win_height = OMAP3ISP_AEWB_MIN_WIN_H;
 327        aewb_recover_cfg->win_width = OMAP3ISP_AEWB_MIN_WIN_W;
 328        aewb_recover_cfg->ver_win_count = OMAP3ISP_AEWB_MIN_WINVC;
 329        aewb_recover_cfg->hor_win_count = OMAP3ISP_AEWB_MIN_WINHC;
 330        aewb_recover_cfg->blk_ver_win_start = aewb_recover_cfg->ver_win_start +
 331                aewb_recover_cfg->win_height * aewb_recover_cfg->ver_win_count;
 332        aewb_recover_cfg->blk_win_height = OMAP3ISP_AEWB_MIN_WIN_H;
 333        aewb_recover_cfg->subsample_ver_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
 334        aewb_recover_cfg->subsample_hor_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
 335
 336        if (h3a_aewb_validate_params(aewb, aewb_recover_cfg)) {
 337                dev_err(aewb->isp->dev, "AEWB: recover configuration is "
 338                                        "invalid.\n");
 339                ret = -EINVAL;
 340                goto err_conf;
 341        }
 342
 343        aewb_recover_cfg->buf_size = h3a_aewb_get_buf_size(aewb_recover_cfg);
 344        aewb->recover_priv = aewb_recover_cfg;
 345
 346        ret = omap3isp_stat_init(aewb, "AEWB", &h3a_aewb_subdev_ops);
 347        if (ret)
 348                goto err_conf;
 349
 350        return 0;
 351
 352err_conf:
 353        kfree(aewb_recover_cfg);
 354err_recover_alloc:
 355        kfree(aewb_cfg);
 356
 357        return ret;
 358}
 359
 360/*
 361 * omap3isp_h3a_aewb_cleanup - Module exit.
 362 */
 363void omap3isp_h3a_aewb_cleanup(struct isp_device *isp)
 364{
 365        kfree(isp->isp_aewb.priv);
 366        kfree(isp->isp_aewb.recover_priv);
 367        omap3isp_stat_cleanup(&isp->isp_aewb);
 368}
 369