linux/drivers/staging/media/atomisp/i2c/ap1302.c
<<
>>
Prefs
   1/*
   2 *
   3 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or
   6 * modify it under the terms of the GNU General Public License version
   7 * 2 as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 */
  15
  16#include "../include/linux/atomisp.h"
  17#include <linux/delay.h>
  18#include <linux/firmware.h>
  19#include <linux/i2c.h>
  20#include <linux/module.h>
  21#include <linux/moduleparam.h>
  22#include <linux/string.h>
  23#include <linux/types.h>
  24#include <media/v4l2-ctrls.h>
  25#include <media/v4l2-device.h>
  26#include "ap1302.h"
  27
  28#define to_ap1302_device(sub_dev) \
  29                container_of(sub_dev, struct ap1302_device, sd)
  30
  31/* Static definitions */
  32static struct regmap_config ap1302_reg16_config = {
  33        .reg_bits = 16,
  34        .val_bits = 16,
  35        .reg_format_endian = REGMAP_ENDIAN_BIG,
  36        .val_format_endian = REGMAP_ENDIAN_BIG,
  37};
  38
  39static struct regmap_config ap1302_reg32_config = {
  40        .reg_bits = 16,
  41        .val_bits = 32,
  42        .reg_format_endian = REGMAP_ENDIAN_BIG,
  43        .val_format_endian = REGMAP_ENDIAN_BIG,
  44};
  45
  46static enum ap1302_contexts ap1302_cntx_mapping[] = {
  47        CONTEXT_PREVIEW,        /* Invalid atomisp run mode */
  48        CONTEXT_VIDEO,          /* ATOMISP_RUN_MODE_VIDEO */
  49        CONTEXT_SNAPSHOT,       /* ATOMISP_RUN_MODE_STILL_CAPTURE */
  50        CONTEXT_SNAPSHOT,       /* ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE */
  51        CONTEXT_PREVIEW,        /* ATOMISP_RUN_MODE_PREVIEW */
  52};
  53
  54static struct ap1302_res_struct ap1302_preview_res[] = {
  55        {
  56                .width = 640,
  57                .height = 480,
  58                .fps = 30,
  59        },
  60        {
  61                .width = 720,
  62                .height = 480,
  63                .fps = 30,
  64        },
  65        {
  66                .width = 1280,
  67                .height = 720,
  68                .fps = 30,
  69        },
  70        {
  71                .width = 1920,
  72                .height = 1080,
  73                .fps = 30,
  74        }
  75};
  76
  77static struct ap1302_res_struct ap1302_snapshot_res[] = {
  78        {
  79                .width = 640,
  80                .height = 480,
  81                .fps = 30,
  82        },
  83        {
  84                .width = 720,
  85                .height = 480,
  86                .fps = 30,
  87        },
  88        {
  89                .width = 1280,
  90                .height = 720,
  91                .fps = 30,
  92        },
  93        {
  94                .width = 1920,
  95                .height = 1080,
  96                .fps = 30,
  97        }
  98};
  99
 100static struct ap1302_res_struct ap1302_video_res[] = {
 101        {
 102                .width = 640,
 103                .height = 480,
 104                .fps = 30,
 105        },
 106        {
 107                .width = 720,
 108                .height = 480,
 109                .fps = 30,
 110        },
 111        {
 112                .width = 1280,
 113                .height = 720,
 114                .fps = 30,
 115        },
 116        {
 117                .width = 1920,
 118                .height = 1080,
 119                .fps = 30,
 120        }
 121};
 122
 123static enum ap1302_contexts stream_to_context[] = {
 124        CONTEXT_SNAPSHOT,
 125        CONTEXT_PREVIEW,
 126        CONTEXT_PREVIEW,
 127        CONTEXT_VIDEO
 128};
 129
 130static u16 aux_stream_config[CONTEXT_NUM][CONTEXT_NUM] = {
 131        {0, 0, 0},      /* Preview: No aux streams. */
 132        {1, 0, 2},      /* Snapshot: 1 for postview. 2 for video */
 133        {1, 0, 0},      /* Video: 1 for preview. */
 134};
 135
 136static struct ap1302_context_info context_info[] = {
 137        {CNTX_WIDTH, AP1302_REG16, "width"},
 138        {CNTX_HEIGHT, AP1302_REG16, "height"},
 139        {CNTX_ROI_X0, AP1302_REG16, "roi_x0"},
 140        {CNTX_ROI_X1, AP1302_REG16, "roi_x1"},
 141        {CNTX_ROI_Y0, AP1302_REG16, "roi_y0"},
 142        {CNTX_ROI_Y1, AP1302_REG16, "roi_y1"},
 143        {CNTX_ASPECT, AP1302_REG16, "aspect"},
 144        {CNTX_LOCK, AP1302_REG16, "lock"},
 145        {CNTX_ENABLE, AP1302_REG16, "enable"},
 146        {CNTX_OUT_FMT, AP1302_REG16, "out_fmt"},
 147        {CNTX_SENSOR_MODE, AP1302_REG16, "sensor_mode"},
 148        {CNTX_MIPI_CTRL, AP1302_REG16, "mipi_ctrl"},
 149        {CNTX_MIPI_II_CTRL, AP1302_REG16, "mipi_ii_ctrl"},
 150        {CNTX_LINE_TIME, AP1302_REG32, "line_time"},
 151        {CNTX_MAX_FPS, AP1302_REG16, "max_fps"},
 152        {CNTX_AE_USG, AP1302_REG16, "ae_usg"},
 153        {CNTX_AE_UPPER_ET, AP1302_REG32, "ae_upper_et"},
 154        {CNTX_AE_MAX_ET, AP1302_REG32, "ae_max_et"},
 155        {CNTX_SS, AP1302_REG16, "ss"},
 156        {CNTX_S1_SENSOR_MODE, AP1302_REG16, "s1_sensor_mode"},
 157        {CNTX_HINF_CTRL, AP1302_REG16, "hinf_ctrl"},
 158};
 159
 160/* This array stores the description list for metadata.
 161   The metadata contains exposure settings and face
 162   detection results. */
 163static u16 ap1302_ss_list[] = {
 164        0xb01c, /* From 0x0186 with size 0x1C are exposure settings. */
 165        0x0186,
 166        0xb002, /* 0x71c0 is for F-number */
 167        0x71c0,
 168        0xb010, /* From 0x03dc with size 0x10 are face general infos. */
 169        0x03dc,
 170        0xb0a0, /* From 0x03e4 with size 0xa0 are face detail infos. */
 171        0x03e4,
 172        0xb020, /* From 0x0604 with size 0x20 are smile rate infos. */
 173        0x0604,
 174        0x0000
 175};
 176
 177/* End of static definitions */
 178
 179static int ap1302_i2c_read_reg(struct v4l2_subdev *sd,
 180                                u16 reg, u16 len, void *val)
 181{
 182        struct ap1302_device *dev = to_ap1302_device(sd);
 183        struct i2c_client *client = v4l2_get_subdevdata(sd);
 184        int ret;
 185
 186        if (len == AP1302_REG16)
 187                ret = regmap_read(dev->regmap16, reg, val);
 188        else if (len == AP1302_REG32)
 189                ret = regmap_read(dev->regmap32, reg, val);
 190        else
 191                ret = -EINVAL;
 192        if (ret) {
 193                dev_dbg(&client->dev, "Read reg failed. reg=0x%04X\n", reg);
 194                return ret;
 195        }
 196        if (len == AP1302_REG16)
 197                dev_dbg(&client->dev, "read_reg[0x%04X] = 0x%04X\n",
 198                        reg, *(u16 *)val);
 199        else
 200                dev_dbg(&client->dev, "read_reg[0x%04X] = 0x%08X\n",
 201                        reg, *(u32 *)val);
 202        return ret;
 203}
 204
 205static int ap1302_i2c_write_reg(struct v4l2_subdev *sd,
 206                                u16 reg, u16 len, u32 val)
 207{
 208        struct ap1302_device *dev = to_ap1302_device(sd);
 209        struct i2c_client *client = v4l2_get_subdevdata(sd);
 210        int ret;
 211        if (len == AP1302_REG16)
 212                ret = regmap_write(dev->regmap16, reg, val);
 213        else if (len == AP1302_REG32)
 214                ret = regmap_write(dev->regmap32, reg, val);
 215        else
 216                ret = -EINVAL;
 217        if (ret) {
 218                dev_dbg(&client->dev, "Write reg failed. reg=0x%04X\n", reg);
 219                return ret;
 220        }
 221        if (len == AP1302_REG16)
 222                dev_dbg(&client->dev, "write_reg[0x%04X] = 0x%04X\n",
 223                        reg, (u16)val);
 224        else
 225                dev_dbg(&client->dev, "write_reg[0x%04X] = 0x%08X\n",
 226                        reg, (u32)val);
 227        return ret;
 228}
 229
 230static u16
 231ap1302_calculate_context_reg_addr(enum ap1302_contexts context, u16 offset)
 232{
 233        u16 reg_addr;
 234        /* The register offset is defined according to preview/video registers.
 235           Preview and video context have the same register definition.
 236           But snapshot context does not have register S1_SENSOR_MODE.
 237           When setting snapshot registers, if the offset exceeds
 238           S1_SENSOR_MODE, the actual offset needs to minus 2. */
 239        if (context == CONTEXT_SNAPSHOT) {
 240                if (offset == CNTX_S1_SENSOR_MODE)
 241                        return 0;
 242                if (offset > CNTX_S1_SENSOR_MODE)
 243                        offset -= 2;
 244        }
 245        if (context == CONTEXT_PREVIEW)
 246                reg_addr = REG_PREVIEW_BASE + offset;
 247        else if (context == CONTEXT_VIDEO)
 248                reg_addr = REG_VIDEO_BASE + offset;
 249        else
 250                reg_addr = REG_SNAPSHOT_BASE + offset;
 251        return reg_addr;
 252}
 253
 254static int ap1302_read_context_reg(struct v4l2_subdev *sd,
 255                enum ap1302_contexts context, u16 offset, u16 len)
 256{
 257        struct ap1302_device *dev = to_ap1302_device(sd);
 258        u16 reg_addr = ap1302_calculate_context_reg_addr(context, offset);
 259        if (reg_addr == 0)
 260                return -EINVAL;
 261        return ap1302_i2c_read_reg(sd, reg_addr, len,
 262                            ((u8 *)&dev->cntx_config[context]) + offset);
 263}
 264
 265static int ap1302_write_context_reg(struct v4l2_subdev *sd,
 266                enum ap1302_contexts context, u16 offset, u16 len)
 267{
 268        struct ap1302_device *dev = to_ap1302_device(sd);
 269        u16 reg_addr = ap1302_calculate_context_reg_addr(context, offset);
 270        if (reg_addr == 0)
 271                return -EINVAL;
 272        return ap1302_i2c_write_reg(sd, reg_addr, len,
 273                        *(u32 *)(((u8 *)&dev->cntx_config[context]) + offset));
 274}
 275
 276static int ap1302_dump_context_reg(struct v4l2_subdev *sd,
 277                                   enum ap1302_contexts context)
 278{
 279        struct i2c_client *client = v4l2_get_subdevdata(sd);
 280        struct ap1302_device *dev = to_ap1302_device(sd);
 281        int i;
 282        dev_dbg(&client->dev, "Dump registers for context[%d]:\n", context);
 283        for (i = 0; i < ARRAY_SIZE(context_info); i++) {
 284                struct ap1302_context_info *info = &context_info[i];
 285                u8 *var = (u8 *)&dev->cntx_config[context] + info->offset;
 286                /* Snapshot context does not have s1_sensor_mode register. */
 287                if (context == CONTEXT_SNAPSHOT &&
 288                        info->offset == CNTX_S1_SENSOR_MODE)
 289                        continue;
 290                ap1302_read_context_reg(sd, context, info->offset, info->len);
 291                if (info->len == AP1302_REG16)
 292                        dev_dbg(&client->dev, "context.%s = 0x%04X (%d)\n",
 293                                info->name, *(u16 *)var, *(u16 *)var);
 294                else
 295                        dev_dbg(&client->dev, "context.%s = 0x%08X (%d)\n",
 296                                info->name, *(u32 *)var, *(u32 *)var);
 297        }
 298        return 0;
 299}
 300
 301static int ap1302_request_firmware(struct v4l2_subdev *sd)
 302{
 303        struct i2c_client *client = v4l2_get_subdevdata(sd);
 304        struct ap1302_device *dev = to_ap1302_device(sd);
 305        int ret;
 306        ret = request_firmware(&dev->fw, "ap1302_fw.bin", &client->dev);
 307        if (ret)
 308                dev_err(&client->dev,
 309                        "ap1302_request_firmware failed. ret=%d\n", ret);
 310        return ret;
 311}
 312
 313/* When loading firmware, host writes firmware data from address 0x8000.
 314   When the address reaches 0x9FFF, the next address should return to 0x8000.
 315   This function handles this address window and load firmware data to AP1302.
 316   win_pos indicates the offset within this window. Firmware loading procedure
 317   may call this function several times. win_pos records the current position
 318   that has been written to.*/
 319static int ap1302_write_fw_window(struct v4l2_subdev *sd,
 320                                  u16 *win_pos, const u8 *buf, u32 len)
 321{
 322        struct ap1302_device *dev = to_ap1302_device(sd);
 323        int ret;
 324        u32 pos;
 325        u32 sub_len;
 326        for (pos = 0; pos < len; pos += sub_len) {
 327                if (len - pos < AP1302_FW_WINDOW_SIZE - *win_pos)
 328                        sub_len = len - pos;
 329                else
 330                        sub_len = AP1302_FW_WINDOW_SIZE - *win_pos;
 331                ret = regmap_raw_write(dev->regmap16,
 332                                        *win_pos + AP1302_FW_WINDOW_OFFSET,
 333                                        buf + pos, sub_len);
 334                if (ret)
 335                        return ret;
 336                *win_pos += sub_len;
 337                if (*win_pos >= AP1302_FW_WINDOW_SIZE)
 338                        *win_pos = 0;
 339        }
 340        return 0;
 341}
 342
 343static int ap1302_load_firmware(struct v4l2_subdev *sd)
 344{
 345        struct i2c_client *client = v4l2_get_subdevdata(sd);
 346        struct ap1302_device *dev = to_ap1302_device(sd);
 347        const struct ap1302_firmware *fw;
 348        const u8 *fw_data;
 349        u16 reg_val = 0;
 350        u16 win_pos = 0;
 351        int ret;
 352
 353        dev_info(&client->dev, "Start to load firmware.\n");
 354        if (!dev->fw) {
 355                dev_err(&client->dev, "firmware not requested.\n");
 356                return -EINVAL;
 357        }
 358        fw = (const struct ap1302_firmware *) dev->fw->data;
 359        if (dev->fw->size != (sizeof(*fw) + fw->total_size)) {
 360                dev_err(&client->dev, "firmware size does not match.\n");
 361                return -EINVAL;
 362        }
 363        /* The fw binary contains a header of struct ap1302_firmware.
 364           Following the header is the bootdata of AP1302.
 365           The bootdata pointer can be referenced as &fw[1]. */
 366        fw_data = (u8 *)&fw[1];
 367
 368        /* Clear crc register. */
 369        ret = ap1302_i2c_write_reg(sd, REG_SIP_CRC, AP1302_REG16, 0xFFFF);
 370        if (ret)
 371                return ret;
 372
 373        /* Load FW data for PLL init stage. */
 374        ret = ap1302_write_fw_window(sd, &win_pos, fw_data, fw->pll_init_size);
 375        if (ret)
 376                return ret;
 377
 378        /* Write 2 to bootdata_stage register to apply basic_init_hp
 379           settings and enable PLL. */
 380        ret = ap1302_i2c_write_reg(sd, REG_BOOTDATA_STAGE,
 381                                   AP1302_REG16, 0x0002);
 382        if (ret)
 383                return ret;
 384
 385        /* Wait 1ms for PLL to lock. */
 386        msleep(20);
 387
 388        /* Load the rest of bootdata content. */
 389        ret = ap1302_write_fw_window(sd, &win_pos, fw_data + fw->pll_init_size,
 390                                     fw->total_size - fw->pll_init_size);
 391        if (ret)
 392                return ret;
 393
 394        /* Check crc. */
 395        ret = ap1302_i2c_read_reg(sd, REG_SIP_CRC, AP1302_REG16, &reg_val);
 396        if (ret)
 397                return ret;
 398        if (reg_val != fw->crc) {
 399                dev_err(&client->dev,
 400                        "crc does not match. T:0x%04X F:0x%04X\n",
 401                        fw->crc, reg_val);
 402                return -EAGAIN;
 403        }
 404
 405        /* Write 0xFFFF to bootdata_stage register to indicate AP1302 that
 406           the whole bootdata content has been loaded. */
 407        ret = ap1302_i2c_write_reg(sd, REG_BOOTDATA_STAGE,
 408                                   AP1302_REG16, 0xFFFF);
 409        if (ret)
 410                return ret;
 411        dev_info(&client->dev, "Load firmware successfully.\n");
 412
 413        return 0;
 414}
 415
 416static int __ap1302_s_power(struct v4l2_subdev *sd, int on, int load_fw)
 417{
 418        struct ap1302_device *dev = to_ap1302_device(sd);
 419        struct i2c_client *client = v4l2_get_subdevdata(sd);
 420        int ret, i;
 421        u16 ss_ptr;
 422
 423        dev_info(&client->dev, "ap1302_s_power is called.\n");
 424        ret = dev->platform_data->power_ctrl(sd, on);
 425        if (ret) {
 426                dev_err(&client->dev,
 427                        "ap1302_s_power error. on=%d ret=%d\n", on, ret);
 428                return ret;
 429        }
 430        dev->power_on = on;
 431        if (!on || !load_fw)
 432                return 0;
 433        /* Load firmware after power on. */
 434        ret = ap1302_load_firmware(sd);
 435        if (ret) {
 436                dev_err(&client->dev,
 437                        "ap1302_load_firmware failed. ret=%d\n", ret);
 438                return ret;
 439        }
 440        ret = ap1302_i2c_read_reg(sd, REG_SS_HEAD_PT0, AP1302_REG16, &ss_ptr);
 441        if (ret)
 442                return ret;
 443        for (i = 0; i < ARRAY_SIZE(ap1302_ss_list); i++) {
 444                ret = ap1302_i2c_write_reg(sd, ss_ptr + i * 2,
 445                        AP1302_REG16, ap1302_ss_list[i]);
 446                if (ret)
 447                        return ret;
 448        }
 449        return ret;
 450}
 451
 452static int ap1302_s_power(struct v4l2_subdev *sd, int on)
 453{
 454        struct ap1302_device *dev = to_ap1302_device(sd);
 455        int ret;
 456
 457        mutex_lock(&dev->input_lock);
 458        ret = __ap1302_s_power(sd, on, 1);
 459        dev->sys_activated = 0;
 460        mutex_unlock(&dev->input_lock);
 461
 462        return ret;
 463}
 464
 465static int ap1302_s_config(struct v4l2_subdev *sd, void *pdata)
 466{
 467        struct ap1302_device *dev = to_ap1302_device(sd);
 468        struct i2c_client *client = v4l2_get_subdevdata(sd);
 469        struct camera_mipi_info *mipi_info;
 470        u16 reg_val = 0;
 471        int ret;
 472
 473        dev_info(&client->dev, "ap1302_s_config is called.\n");
 474        if (pdata == NULL)
 475                return -ENODEV;
 476
 477        dev->platform_data = pdata;
 478
 479        mutex_lock(&dev->input_lock);
 480
 481        if (dev->platform_data->platform_init) {
 482                ret = dev->platform_data->platform_init(client);
 483                if (ret)
 484                        goto fail_power;
 485        }
 486
 487        ret = __ap1302_s_power(sd, 1, 0);
 488        if (ret)
 489                goto fail_power;
 490
 491        /* Detect for AP1302 */
 492        ret = ap1302_i2c_read_reg(sd, REG_CHIP_VERSION, AP1302_REG16, &reg_val);
 493        if (ret || (reg_val != AP1302_CHIP_ID)) {
 494                dev_err(&client->dev,
 495                        "Chip version does no match. ret=%d ver=0x%04x\n",
 496                        ret, reg_val);
 497                goto fail_config;
 498        }
 499        dev_info(&client->dev, "AP1302 Chip ID is 0x%X\n", reg_val);
 500
 501        /* Detect revision for AP1302 */
 502        ret = ap1302_i2c_read_reg(sd, REG_CHIP_REV, AP1302_REG16, &reg_val);
 503        if (ret)
 504                goto fail_config;
 505        dev_info(&client->dev, "AP1302 Chip Rev is 0x%X\n", reg_val);
 506        ret = dev->platform_data->csi_cfg(sd, 1);
 507        if (ret)
 508                goto fail_config;
 509
 510        mipi_info = v4l2_get_subdev_hostdata(sd);
 511        if (!mipi_info)
 512                goto fail_config;
 513        dev->num_lanes = mipi_info->num_lanes;
 514
 515        ret = __ap1302_s_power(sd, 0, 0);
 516        if (ret)
 517                goto fail_power;
 518
 519        mutex_unlock(&dev->input_lock);
 520
 521        return ret;
 522
 523fail_config:
 524        __ap1302_s_power(sd, 0, 0);
 525fail_power:
 526        mutex_unlock(&dev->input_lock);
 527        dev_err(&client->dev, "ap1302_s_config failed\n");
 528        return ret;
 529}
 530
 531static enum ap1302_contexts ap1302_get_context(struct v4l2_subdev *sd)
 532{
 533        struct ap1302_device *dev = to_ap1302_device(sd);
 534        return dev->cur_context;
 535}
 536
 537static int ap1302_enum_mbus_code(struct v4l2_subdev *sd,
 538                                 struct v4l2_subdev_pad_config *cfg,
 539                                 struct v4l2_subdev_mbus_code_enum *code)
 540{
 541        if (code->index)
 542                return -EINVAL;
 543
 544        code->code = MEDIA_BUS_FMT_UYVY8_1X16;
 545
 546        return 0;
 547}
 548
 549static int ap1302_match_resolution(struct ap1302_context_res *res,
 550                                   struct v4l2_mbus_framefmt *fmt)
 551{
 552        s32 w0, h0, mismatch, distance;
 553        s32 w1 = fmt->width;
 554        s32 h1 = fmt->height;
 555        s32 min_distance = INT_MAX;
 556        s32 i, idx = -1;
 557
 558        if (w1 == 0 || h1 == 0)
 559                return -1;
 560
 561        for (i = 0; i < res->res_num; i++) {
 562                w0 = res->res_table[i].width;
 563                h0 = res->res_table[i].height;
 564                if (w0 < w1 || h0 < h1)
 565                        continue;
 566                mismatch = abs(w0 * h1 - w1 * h0) * 8192 / w1 / h0;
 567                if (mismatch > 8192 * AP1302_MAX_RATIO_MISMATCH / 100)
 568                        continue;
 569                distance = (w0 * h1 + w1 * h0) * 8192 / w1 / h1;
 570                if (distance < min_distance) {
 571                        min_distance = distance;
 572                        idx = i;
 573                }
 574        }
 575
 576        return idx;
 577}
 578
 579static s32 ap1302_try_mbus_fmt_locked(struct v4l2_subdev *sd,
 580                                enum ap1302_contexts context,
 581                                struct v4l2_mbus_framefmt *fmt)
 582{
 583        struct ap1302_device *dev = to_ap1302_device(sd);
 584        struct ap1302_res_struct *res_table;
 585        s32 res_num, idx = -1;
 586
 587        res_table = dev->cntx_res[context].res_table;
 588        res_num = dev->cntx_res[context].res_num;
 589
 590        if ((fmt->width <= res_table[res_num - 1].width) &&
 591                (fmt->height <= res_table[res_num - 1].height))
 592                idx = ap1302_match_resolution(&dev->cntx_res[context], fmt);
 593        if (idx == -1)
 594                idx = res_num - 1;
 595
 596        fmt->width = res_table[idx].width;
 597        fmt->height = res_table[idx].height;
 598        fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
 599        return idx;
 600}
 601
 602
 603static int ap1302_get_fmt(struct v4l2_subdev *sd,
 604                         struct v4l2_subdev_pad_config *cfg,
 605                                         struct v4l2_subdev_format *format)
 606
 607{
 608    struct v4l2_mbus_framefmt *fmt = &format->format;
 609    struct ap1302_device *dev = to_ap1302_device(sd);
 610        enum ap1302_contexts context;
 611        struct ap1302_res_struct *res_table;
 612        s32 cur_res;
 613     if (format->pad)
 614                return -EINVAL;
 615        mutex_lock(&dev->input_lock);
 616        context = ap1302_get_context(sd);
 617        res_table = dev->cntx_res[context].res_table;
 618        cur_res = dev->cntx_res[context].cur_res;
 619        fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
 620        fmt->width = res_table[cur_res].width;
 621        fmt->height = res_table[cur_res].height;
 622        mutex_unlock(&dev->input_lock);
 623        return 0;
 624}
 625
 626static int ap1302_set_fmt(struct v4l2_subdev *sd,
 627                          struct v4l2_subdev_pad_config *cfg,
 628                          struct v4l2_subdev_format *format)
 629{
 630        struct v4l2_mbus_framefmt *fmt = &format->format;
 631        struct ap1302_device *dev = to_ap1302_device(sd);
 632        struct i2c_client *client = v4l2_get_subdevdata(sd);
 633        struct atomisp_input_stream_info *stream_info =
 634                (struct atomisp_input_stream_info *)fmt->reserved;
 635        enum ap1302_contexts context, main_context;
 636        if (format->pad)
 637                return -EINVAL;
 638        if (!fmt)
 639                return -EINVAL;
 640        mutex_lock(&dev->input_lock);
 641        if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
 642                context = ap1302_get_context(sd);
 643                ap1302_try_mbus_fmt_locked(sd, context, fmt);
 644                cfg->try_fmt = *fmt;
 645            mutex_unlock(&dev->input_lock);
 646                return 0;
 647                }
 648        context = stream_to_context[stream_info->stream];
 649        dev_dbg(&client->dev, "ap1302_set_mbus_fmt. stream=%d context=%d\n",
 650                stream_info->stream, context);
 651        dev->cntx_res[context].cur_res =
 652                ap1302_try_mbus_fmt_locked(sd, context, fmt);
 653        dev->cntx_config[context].width = fmt->width;
 654        dev->cntx_config[context].height = fmt->height;
 655        ap1302_write_context_reg(sd, context, CNTX_WIDTH, AP1302_REG16);
 656        ap1302_write_context_reg(sd, context, CNTX_HEIGHT, AP1302_REG16);
 657        ap1302_read_context_reg(sd, context, CNTX_OUT_FMT, AP1302_REG16);
 658        dev->cntx_config[context].out_fmt &= ~OUT_FMT_TYPE_MASK;
 659        dev->cntx_config[context].out_fmt |= AP1302_FMT_UYVY422;
 660        ap1302_write_context_reg(sd, context, CNTX_OUT_FMT, AP1302_REG16);
 661
 662        main_context = ap1302_get_context(sd);
 663        if (context == main_context) {
 664                ap1302_read_context_reg(sd, context,
 665                        CNTX_MIPI_CTRL, AP1302_REG16);
 666                dev->cntx_config[context].mipi_ctrl &= ~MIPI_CTRL_IMGVC_MASK;
 667                dev->cntx_config[context].mipi_ctrl |=
 668                        (context << MIPI_CTRL_IMGVC_OFFSET);
 669                dev->cntx_config[context].mipi_ctrl &= ~MIPI_CTRL_SSVC_MASK;
 670                dev->cntx_config[context].mipi_ctrl |=
 671                        (context << MIPI_CTRL_SSVC_OFFSET);
 672                dev->cntx_config[context].mipi_ctrl &= ~MIPI_CTRL_SSTYPE_MASK;
 673                dev->cntx_config[context].mipi_ctrl |=
 674                        (0x12 << MIPI_CTRL_SSTYPE_OFFSET);
 675                ap1302_write_context_reg(sd, context,
 676                        CNTX_MIPI_CTRL, AP1302_REG16);
 677                ap1302_read_context_reg(sd, context,
 678                        CNTX_SS, AP1302_REG16);
 679                dev->cntx_config[context].ss = AP1302_SS_CTRL;
 680                ap1302_write_context_reg(sd, context,
 681                        CNTX_SS, AP1302_REG16);
 682        } else {
 683                /* Configure aux stream */
 684                ap1302_read_context_reg(sd, context,
 685                        CNTX_MIPI_II_CTRL, AP1302_REG16);
 686                dev->cntx_config[context].mipi_ii_ctrl &= ~MIPI_CTRL_IMGVC_MASK;
 687                dev->cntx_config[context].mipi_ii_ctrl |=
 688                        (context << MIPI_CTRL_IMGVC_OFFSET);
 689                ap1302_write_context_reg(sd, context,
 690                        CNTX_MIPI_II_CTRL, AP1302_REG16);
 691                if (stream_info->enable) {
 692                        ap1302_read_context_reg(sd, main_context,
 693                                CNTX_OUT_FMT, AP1302_REG16);
 694                        dev->cntx_config[context].out_fmt |=
 695                                (aux_stream_config[main_context][context]
 696                                 << OUT_FMT_IIS_OFFSET);
 697                        ap1302_write_context_reg(sd, main_context,
 698                                CNTX_OUT_FMT, AP1302_REG16);
 699                }
 700        }
 701        stream_info->ch_id = context;
 702        mutex_unlock(&dev->input_lock);
 703
 704        return 0;
 705}
 706
 707
 708static int ap1302_g_frame_interval(struct v4l2_subdev *sd,
 709                        struct v4l2_subdev_frame_interval *interval)
 710{
 711        struct ap1302_device *dev = to_ap1302_device(sd);
 712        enum ap1302_contexts context;
 713        struct ap1302_res_struct *res_table;
 714        u32 cur_res;
 715
 716        mutex_lock(&dev->input_lock);
 717        context = ap1302_get_context(sd);
 718        res_table = dev->cntx_res[context].res_table;
 719        cur_res = dev->cntx_res[context].cur_res;
 720        interval->interval.denominator = res_table[cur_res].fps;
 721        interval->interval.numerator = 1;
 722        mutex_unlock(&dev->input_lock);
 723        return 0;
 724}
 725
 726static int ap1302_enum_frame_size(struct v4l2_subdev *sd,
 727                                  struct v4l2_subdev_pad_config *cfg,
 728                                  struct v4l2_subdev_frame_size_enum *fse)
 729{
 730        struct ap1302_device *dev = to_ap1302_device(sd);
 731        enum ap1302_contexts context;
 732        struct ap1302_res_struct *res_table;
 733        int index = fse->index;
 734
 735        mutex_lock(&dev->input_lock);
 736        context = ap1302_get_context(sd);
 737        if (index >= dev->cntx_res[context].res_num) {
 738                mutex_unlock(&dev->input_lock);
 739                return -EINVAL;
 740        }
 741
 742        res_table = dev->cntx_res[context].res_table;
 743        fse->min_width = res_table[index].width;
 744        fse->min_height = res_table[index].height;
 745        fse->max_width = res_table[index].width;
 746        fse->max_height = res_table[index].height;
 747        mutex_unlock(&dev->input_lock);
 748
 749        return 0;
 750}
 751
 752
 753static int ap1302_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
 754{
 755        *frames = 0;
 756        return 0;
 757}
 758
 759static int ap1302_s_stream(struct v4l2_subdev *sd, int enable)
 760{
 761        struct ap1302_device *dev = to_ap1302_device(sd);
 762        struct i2c_client *client = v4l2_get_subdevdata(sd);
 763        enum ap1302_contexts context;
 764        u32 reg_val;
 765        int ret;
 766
 767        mutex_lock(&dev->input_lock);
 768        context = ap1302_get_context(sd);
 769        dev_dbg(&client->dev, "ap1302_s_stream. context=%d enable=%d\n",
 770                        context, enable);
 771        /* Switch context */
 772        ap1302_i2c_read_reg(sd, REG_CTRL,
 773                            AP1302_REG16, &reg_val);
 774        reg_val &= ~CTRL_CNTX_MASK;
 775        reg_val |= (context<<CTRL_CNTX_OFFSET);
 776        ap1302_i2c_write_reg(sd, REG_CTRL,
 777                            AP1302_REG16, reg_val);
 778        /* Select sensor */
 779        ap1302_i2c_read_reg(sd, REG_SENSOR_SELECT,
 780                            AP1302_REG16, &reg_val);
 781        reg_val &= ~SENSOR_SELECT_MASK;
 782        reg_val |= (AP1302_SENSOR_PRI<<SENSOR_SELECT_OFFSET);
 783        ap1302_i2c_write_reg(sd, REG_SENSOR_SELECT,
 784                            AP1302_REG16, reg_val);
 785        if (enable) {
 786                dev_info(&client->dev, "Start stream. context=%d\n", context);
 787                ap1302_dump_context_reg(sd, context);
 788                if (!dev->sys_activated) {
 789                        reg_val = AP1302_SYS_ACTIVATE;
 790                        dev->sys_activated = 1;
 791                } else {
 792                        reg_val = AP1302_SYS_SWITCH;
 793                }
 794        } else {
 795                dev_info(&client->dev, "Stop stream. context=%d\n", context);
 796                reg_val = AP1302_SYS_SWITCH;
 797        }
 798        ret = ap1302_i2c_write_reg(sd, REG_SYS_START, AP1302_REG16, reg_val);
 799        if (ret)
 800                dev_err(&client->dev,
 801                        "AP1302 set stream failed. enable=%d\n", enable);
 802        mutex_unlock(&dev->input_lock);
 803        return ret;
 804}
 805
 806static u16 ap1302_ev_values[] = {0xfd00, 0xfe80, 0x0, 0x180, 0x300};
 807
 808static int ap1302_set_exposure_off(struct v4l2_subdev *sd, s32 val)
 809{
 810        val -= AP1302_MIN_EV;
 811        return ap1302_i2c_write_reg(sd, REG_AE_BV_OFF, AP1302_REG16,
 812                                ap1302_ev_values[val]);
 813}
 814
 815static u16 ap1302_wb_values[] = {
 816        0, /* V4L2_WHITE_BALANCE_MANUAL */
 817        0xf, /* V4L2_WHITE_BALANCE_AUTO */
 818        0x2, /* V4L2_WHITE_BALANCE_INCANDESCENT */
 819        0x4, /* V4L2_WHITE_BALANCE_FLUORESCENT */
 820        0x5, /* V4L2_WHITE_BALANCE_FLUORESCENT_H */
 821        0x1, /* V4L2_WHITE_BALANCE_HORIZON */
 822        0x5, /* V4L2_WHITE_BALANCE_DAYLIGHT */
 823        0xf, /* V4L2_WHITE_BALANCE_FLASH */
 824        0x6, /* V4L2_WHITE_BALANCE_CLOUDY */
 825        0x6, /* V4L2_WHITE_BALANCE_SHADE */
 826};
 827
 828static int ap1302_set_wb_mode(struct v4l2_subdev *sd, s32 val)
 829{
 830        int ret = 0;
 831        u16 reg_val;
 832
 833        ret = ap1302_i2c_read_reg(sd, REG_AWB_CTRL, AP1302_REG16, &reg_val);
 834        if (ret)
 835                return ret;
 836        reg_val &= ~AWB_CTRL_MODE_MASK;
 837        reg_val |= ap1302_wb_values[val] << AWB_CTRL_MODE_OFFSET;
 838        if (val == V4L2_WHITE_BALANCE_FLASH)
 839                reg_val |= AWB_CTRL_FLASH_MASK;
 840        else
 841                reg_val &= ~AWB_CTRL_FLASH_MASK;
 842        ret = ap1302_i2c_write_reg(sd, REG_AWB_CTRL, AP1302_REG16, reg_val);
 843        return ret;
 844}
 845
 846static int ap1302_set_zoom(struct v4l2_subdev *sd, s32 val)
 847{
 848        ap1302_i2c_write_reg(sd, REG_DZ_TGT_FCT, AP1302_REG16,
 849                val * 4 + 0x100);
 850        return 0;
 851}
 852
 853static u16 ap1302_sfx_values[] = {
 854        0x00, /* V4L2_COLORFX_NONE */
 855        0x03, /* V4L2_COLORFX_BW */
 856        0x0d, /* V4L2_COLORFX_SEPIA */
 857        0x07, /* V4L2_COLORFX_NEGATIVE */
 858        0x04, /* V4L2_COLORFX_EMBOSS */
 859        0x0f, /* V4L2_COLORFX_SKETCH */
 860        0x08, /* V4L2_COLORFX_SKY_BLUE */
 861        0x09, /* V4L2_COLORFX_GRASS_GREEN */
 862        0x0a, /* V4L2_COLORFX_SKIN_WHITEN */
 863        0x00, /* V4L2_COLORFX_VIVID */
 864        0x00, /* V4L2_COLORFX_AQUA */
 865        0x00, /* V4L2_COLORFX_ART_FREEZE */
 866        0x00, /* V4L2_COLORFX_SILHOUETTE */
 867        0x10, /* V4L2_COLORFX_SOLARIZATION */
 868        0x02, /* V4L2_COLORFX_ANTIQUE */
 869        0x00, /* V4L2_COLORFX_SET_CBCR */
 870};
 871
 872static int ap1302_set_special_effect(struct v4l2_subdev *sd, s32 val)
 873{
 874        ap1302_i2c_write_reg(sd, REG_SFX_MODE, AP1302_REG16,
 875                ap1302_sfx_values[val]);
 876        return 0;
 877}
 878
 879static u16 ap1302_scene_mode_values[] = {
 880        0x00, /* V4L2_SCENE_MODE_NONE */
 881        0x07, /* V4L2_SCENE_MODE_BACKLIGHT */
 882        0x0a, /* V4L2_SCENE_MODE_BEACH_SNOW */
 883        0x06, /* V4L2_SCENE_MODE_CANDLE_LIGHT */
 884        0x00, /* V4L2_SCENE_MODE_DAWN_DUSK */
 885        0x00, /* V4L2_SCENE_MODE_FALL_COLORS */
 886        0x0d, /* V4L2_SCENE_MODE_FIREWORKS */
 887        0x02, /* V4L2_SCENE_MODE_LANDSCAPE */
 888        0x05, /* V4L2_SCENE_MODE_NIGHT */
 889        0x0c, /* V4L2_SCENE_MODE_PARTY_INDOOR */
 890        0x01, /* V4L2_SCENE_MODE_PORTRAIT */
 891        0x03, /* V4L2_SCENE_MODE_SPORTS */
 892        0x0e, /* V4L2_SCENE_MODE_SUNSET */
 893        0x0b, /* V4L2_SCENE_MODE_TEXT */
 894};
 895
 896static int ap1302_set_scene_mode(struct v4l2_subdev *sd, s32 val)
 897{
 898        ap1302_i2c_write_reg(sd, REG_SCENE_CTRL, AP1302_REG16,
 899                ap1302_scene_mode_values[val]);
 900        return 0;
 901}
 902
 903static u16 ap1302_flicker_values[] = {
 904        0x0,    /* OFF */
 905        0x3201, /* 50HZ */
 906        0x3c01, /* 60HZ */
 907        0x2     /* AUTO */
 908};
 909
 910static int ap1302_set_flicker_freq(struct v4l2_subdev *sd, s32 val)
 911{
 912        ap1302_i2c_write_reg(sd, REG_FLICK_CTRL, AP1302_REG16,
 913                ap1302_flicker_values[val]);
 914        return 0;
 915}
 916
 917static int ap1302_s_ctrl(struct v4l2_ctrl *ctrl)
 918{
 919        struct ap1302_device *dev = container_of(
 920                ctrl->handler, struct ap1302_device, ctrl_handler);
 921
 922        switch (ctrl->id) {
 923        case V4L2_CID_RUN_MODE:
 924                dev->cur_context = ap1302_cntx_mapping[ctrl->val];
 925                break;
 926        case V4L2_CID_EXPOSURE:
 927                ap1302_set_exposure_off(&dev->sd, ctrl->val);
 928                break;
 929        case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
 930                ap1302_set_wb_mode(&dev->sd, ctrl->val);
 931                break;
 932        case V4L2_CID_ZOOM_ABSOLUTE:
 933                ap1302_set_zoom(&dev->sd, ctrl->val);
 934                break;
 935        case V4L2_CID_COLORFX:
 936                ap1302_set_special_effect(&dev->sd, ctrl->val);
 937                break;
 938        case V4L2_CID_SCENE_MODE:
 939                ap1302_set_scene_mode(&dev->sd, ctrl->val);
 940                break;
 941        case V4L2_CID_POWER_LINE_FREQUENCY:
 942                ap1302_set_flicker_freq(&dev->sd, ctrl->val);
 943                break;
 944        default:
 945                return -EINVAL;
 946        }
 947
 948        return 0;
 949}
 950
 951static int ap1302_g_register(struct v4l2_subdev *sd,
 952                             struct v4l2_dbg_register *reg)
 953{
 954        struct ap1302_device *dev = to_ap1302_device(sd);
 955        int ret;
 956        u32 reg_val;
 957
 958        if (reg->size != AP1302_REG16 &&
 959            reg->size != AP1302_REG32)
 960                return -EINVAL;
 961
 962        mutex_lock(&dev->input_lock);
 963        if (dev->power_on)
 964                ret = ap1302_i2c_read_reg(sd, reg->reg, reg->size, &reg_val);
 965        else
 966                ret = -EIO;
 967        mutex_unlock(&dev->input_lock);
 968        if (ret)
 969                return ret;
 970
 971        reg->val = reg_val;
 972
 973        return 0;
 974}
 975
 976static int ap1302_s_register(struct v4l2_subdev *sd,
 977                             const struct v4l2_dbg_register *reg)
 978{
 979        struct ap1302_device *dev = to_ap1302_device(sd);
 980        int ret;
 981
 982        if (reg->size != AP1302_REG16 &&
 983            reg->size != AP1302_REG32)
 984                return -EINVAL;
 985
 986        mutex_lock(&dev->input_lock);
 987        if (dev->power_on)
 988                ret = ap1302_i2c_write_reg(sd, reg->reg, reg->size, reg->val);
 989        else
 990                ret = -EIO;
 991        mutex_unlock(&dev->input_lock);
 992        return ret;
 993}
 994
 995static long ap1302_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
 996{
 997        long ret = 0;
 998        switch (cmd) {
 999        case VIDIOC_DBG_G_REGISTER:
1000                ret = ap1302_g_register(sd, arg);
1001                break;
1002        case VIDIOC_DBG_S_REGISTER:
1003                ret = ap1302_s_register(sd, arg);
1004                break;
1005        default:
1006                ret = -EINVAL;
1007        }
1008        return ret;
1009}
1010
1011static const struct v4l2_ctrl_ops ctrl_ops = {
1012        .s_ctrl = ap1302_s_ctrl,
1013};
1014
1015static const char * const ctrl_run_mode_menu[] = {
1016        NULL,
1017        "Video",
1018        "Still capture",
1019        "Continuous capture",
1020        "Preview",
1021};
1022
1023static const struct v4l2_ctrl_config ctrls[] = {
1024        {
1025                .ops = &ctrl_ops,
1026                .id = V4L2_CID_RUN_MODE,
1027                .name = "Run Mode",
1028                .type = V4L2_CTRL_TYPE_MENU,
1029                .min = 1,
1030                .def = 4,
1031                .max = 4,
1032                .qmenu = ctrl_run_mode_menu,
1033        },
1034        {
1035                .ops = &ctrl_ops,
1036                .id = V4L2_CID_EXPOSURE,
1037                .name = "Exposure",
1038                .type = V4L2_CTRL_TYPE_INTEGER,
1039                .min = AP1302_MIN_EV,
1040                .def = 0,
1041                .max = AP1302_MAX_EV,
1042                .step = 1,
1043        },
1044        {
1045                .ops = &ctrl_ops,
1046                .id = V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
1047                .name = "White Balance",
1048                .type = V4L2_CTRL_TYPE_INTEGER,
1049                .min = 0,
1050                .def = 0,
1051                .max = 9,
1052                .step = 1,
1053        },
1054        {
1055                .ops = &ctrl_ops,
1056                .id = V4L2_CID_ZOOM_ABSOLUTE,
1057                .name = "Zoom Absolute",
1058                .type = V4L2_CTRL_TYPE_INTEGER,
1059                .min = 0,
1060                .def = 0,
1061                .max = 1024,
1062                .step = 1,
1063        },
1064        {
1065                .ops = &ctrl_ops,
1066                .id = V4L2_CID_COLORFX,
1067                .name = "Color Special Effect",
1068                .type = V4L2_CTRL_TYPE_INTEGER,
1069                .min = 0,
1070                .def = 0,
1071                .max = 15,
1072                .step = 1,
1073        },
1074        {
1075                .ops = &ctrl_ops,
1076                .id = V4L2_CID_SCENE_MODE,
1077                .name = "Scene Mode",
1078                .type = V4L2_CTRL_TYPE_INTEGER,
1079                .min = 0,
1080                .def = 0,
1081                .max = 13,
1082                .step = 1,
1083        },
1084        {
1085                .ops = &ctrl_ops,
1086                .id = V4L2_CID_POWER_LINE_FREQUENCY,
1087                .name = "Light frequency filter",
1088                .type = V4L2_CTRL_TYPE_INTEGER,
1089                .min = 0,
1090                .def = 3,
1091                .max = 3,
1092                .step = 1,
1093        },
1094};
1095
1096static const struct v4l2_subdev_sensor_ops ap1302_sensor_ops = {
1097        .g_skip_frames  = ap1302_g_skip_frames,
1098};
1099
1100static const struct v4l2_subdev_video_ops ap1302_video_ops = {
1101        .s_stream = ap1302_s_stream,
1102        .g_frame_interval = ap1302_g_frame_interval,
1103};
1104
1105static const struct v4l2_subdev_core_ops ap1302_core_ops = {
1106        .s_power = ap1302_s_power,
1107        .ioctl = ap1302_ioctl,
1108#ifdef CONFIG_VIDEO_ADV_DEBUG
1109        .g_register = ap1302_g_register,
1110        .s_register = ap1302_s_register,
1111#endif
1112};
1113
1114static const struct v4l2_subdev_pad_ops ap1302_pad_ops = {
1115        .enum_mbus_code = ap1302_enum_mbus_code,
1116        .enum_frame_size = ap1302_enum_frame_size,
1117        .get_fmt = ap1302_get_fmt,
1118        .set_fmt = ap1302_set_fmt,
1119};
1120
1121static const struct v4l2_subdev_ops ap1302_ops = {
1122        .core = &ap1302_core_ops,
1123        .pad = &ap1302_pad_ops,
1124        .video = &ap1302_video_ops,
1125        .sensor = &ap1302_sensor_ops
1126};
1127
1128static int ap1302_remove(struct i2c_client *client)
1129{
1130        struct v4l2_subdev *sd = i2c_get_clientdata(client);
1131        struct ap1302_device *dev = to_ap1302_device(sd);
1132
1133        if (dev->platform_data->platform_deinit)
1134                dev->platform_data->platform_deinit();
1135
1136        release_firmware(dev->fw);
1137
1138        media_entity_cleanup(&dev->sd.entity);
1139        dev->platform_data->csi_cfg(sd, 0);
1140        v4l2_device_unregister_subdev(sd);
1141
1142        return 0;
1143}
1144
1145static int ap1302_probe(struct i2c_client *client,
1146                        const struct i2c_device_id *id)
1147{
1148        struct ap1302_device *dev;
1149        int ret;
1150        unsigned int i;
1151
1152        dev_info(&client->dev, "ap1302 probe called.\n");
1153
1154        /* allocate device & init sub device */
1155        dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
1156        if (!dev) {
1157                dev_err(&client->dev, "%s: out of memory\n", __func__);
1158                return -ENOMEM;
1159        }
1160
1161        mutex_init(&dev->input_lock);
1162
1163        v4l2_i2c_subdev_init(&(dev->sd), client, &ap1302_ops);
1164
1165        ret = ap1302_request_firmware(&(dev->sd));
1166        if (ret) {
1167                dev_err(&client->dev, "Cannot request ap1302 firmware.\n");
1168                goto out_free;
1169        }
1170
1171        dev->regmap16 = devm_regmap_init_i2c(client, &ap1302_reg16_config);
1172        if (IS_ERR(dev->regmap16)) {
1173                ret = PTR_ERR(dev->regmap16);
1174                dev_err(&client->dev,
1175                        "Failed to allocate 16bit register map: %d\n", ret);
1176                return ret;
1177        }
1178
1179        dev->regmap32 = devm_regmap_init_i2c(client, &ap1302_reg32_config);
1180        if (IS_ERR(dev->regmap32)) {
1181                ret = PTR_ERR(dev->regmap32);
1182                dev_err(&client->dev,
1183                        "Failed to allocate 32bit register map: %d\n", ret);
1184                return ret;
1185        }
1186
1187        if (client->dev.platform_data) {
1188                ret = ap1302_s_config(&dev->sd, client->dev.platform_data);
1189                if (ret)
1190                        goto out_free;
1191        }
1192
1193        dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1194        dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1195        dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1196
1197        dev->cntx_res[CONTEXT_PREVIEW].res_num = ARRAY_SIZE(ap1302_preview_res);
1198        dev->cntx_res[CONTEXT_PREVIEW].res_table = ap1302_preview_res;
1199        dev->cntx_res[CONTEXT_SNAPSHOT].res_num =
1200                ARRAY_SIZE(ap1302_snapshot_res);
1201        dev->cntx_res[CONTEXT_SNAPSHOT].res_table = ap1302_snapshot_res;
1202        dev->cntx_res[CONTEXT_VIDEO].res_num = ARRAY_SIZE(ap1302_video_res);
1203        dev->cntx_res[CONTEXT_VIDEO].res_table = ap1302_video_res;
1204
1205        ret = v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ctrls));
1206        if (ret) {
1207                ap1302_remove(client);
1208                return ret;
1209        }
1210
1211        for (i = 0; i < ARRAY_SIZE(ctrls); i++)
1212                v4l2_ctrl_new_custom(&dev->ctrl_handler, &ctrls[i], NULL);
1213
1214        if (dev->ctrl_handler.error) {
1215                ap1302_remove(client);
1216                return dev->ctrl_handler.error;
1217        }
1218
1219        /* Use same lock for controls as for everything else. */
1220        dev->ctrl_handler.lock = &dev->input_lock;
1221        dev->sd.ctrl_handler = &dev->ctrl_handler;
1222        v4l2_ctrl_handler_setup(&dev->ctrl_handler);
1223
1224        dev->run_mode = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_RUN_MODE);
1225        v4l2_ctrl_s_ctrl(dev->run_mode, ATOMISP_RUN_MODE_PREVIEW);
1226
1227        ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1228        if (ret)
1229                ap1302_remove(client);
1230        return ret;
1231out_free:
1232        v4l2_device_unregister_subdev(&dev->sd);
1233        return ret;
1234}
1235
1236static const struct i2c_device_id ap1302_id[] = {
1237        {AP1302_NAME, 0},
1238        {}
1239};
1240MODULE_DEVICE_TABLE(i2c, ap1302_id);
1241
1242static struct i2c_driver ap1302_driver = {
1243        .driver = {
1244                .name = AP1302_NAME,
1245        },
1246        .probe = ap1302_probe,
1247        .remove = ap1302_remove,
1248        .id_table = ap1302_id,
1249};
1250
1251module_i2c_driver(ap1302_driver);
1252
1253MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
1254MODULE_DESCRIPTION("AP1302 Driver");
1255MODULE_LICENSE("GPL");
1256