linux/drivers/media/i2c/m5mols/m5mols_core.c
<<
>>
Prefs
   1/*
   2 * Driver for M-5MOLS 8M Pixel camera sensor with ISP
   3 *
   4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
   5 * Author: HeungJun Kim <riverful.kim@samsung.com>
   6 *
   7 * Copyright (C) 2009 Samsung Electronics Co., Ltd.
   8 * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License as published by
  12 * the Free Software Foundation; either version 2 of the License, or
  13 * (at your option) any later version.
  14 */
  15
  16#include <linux/i2c.h>
  17#include <linux/slab.h>
  18#include <linux/irq.h>
  19#include <linux/interrupt.h>
  20#include <linux/delay.h>
  21#include <linux/gpio.h>
  22#include <linux/regulator/consumer.h>
  23#include <linux/videodev2.h>
  24#include <linux/module.h>
  25#include <media/v4l2-ctrls.h>
  26#include <media/v4l2-device.h>
  27#include <media/v4l2-subdev.h>
  28#include <media/i2c/m5mols.h>
  29
  30#include "m5mols.h"
  31#include "m5mols_reg.h"
  32
  33int m5mols_debug;
  34module_param(m5mols_debug, int, 0644);
  35
  36#define MODULE_NAME             "M5MOLS"
  37#define M5MOLS_I2C_CHECK_RETRY  500
  38
  39/* The regulator consumer names for external voltage regulators */
  40static struct regulator_bulk_data supplies[] = {
  41        {
  42                .supply = "core",       /* ARM core power, 1.2V */
  43        }, {
  44                .supply = "dig_18",     /* digital power 1, 1.8V */
  45        }, {
  46                .supply = "d_sensor",   /* sensor power 1, 1.8V */
  47        }, {
  48                .supply = "dig_28",     /* digital power 2, 2.8V */
  49        }, {
  50                .supply = "a_sensor",   /* analog power */
  51        }, {
  52                .supply = "dig_12",     /* digital power 3, 1.2V */
  53        },
  54};
  55
  56static struct v4l2_mbus_framefmt m5mols_default_ffmt[M5MOLS_RESTYPE_MAX] = {
  57        [M5MOLS_RESTYPE_MONITOR] = {
  58                .width          = 1920,
  59                .height         = 1080,
  60                .code           = MEDIA_BUS_FMT_VYUY8_2X8,
  61                .field          = V4L2_FIELD_NONE,
  62                .colorspace     = V4L2_COLORSPACE_JPEG,
  63        },
  64        [M5MOLS_RESTYPE_CAPTURE] = {
  65                .width          = 1920,
  66                .height         = 1080,
  67                .code           = MEDIA_BUS_FMT_JPEG_1X8,
  68                .field          = V4L2_FIELD_NONE,
  69                .colorspace     = V4L2_COLORSPACE_JPEG,
  70        },
  71};
  72#define SIZE_DEFAULT_FFMT       ARRAY_SIZE(m5mols_default_ffmt)
  73
  74static const struct m5mols_resolution m5mols_reg_res[] = {
  75        { 0x01, M5MOLS_RESTYPE_MONITOR, 128, 96 },      /* SUB-QCIF */
  76        { 0x03, M5MOLS_RESTYPE_MONITOR, 160, 120 },     /* QQVGA */
  77        { 0x05, M5MOLS_RESTYPE_MONITOR, 176, 144 },     /* QCIF */
  78        { 0x06, M5MOLS_RESTYPE_MONITOR, 176, 176 },
  79        { 0x08, M5MOLS_RESTYPE_MONITOR, 240, 320 },     /* QVGA */
  80        { 0x09, M5MOLS_RESTYPE_MONITOR, 320, 240 },     /* QVGA */
  81        { 0x0c, M5MOLS_RESTYPE_MONITOR, 240, 400 },     /* WQVGA */
  82        { 0x0d, M5MOLS_RESTYPE_MONITOR, 400, 240 },     /* WQVGA */
  83        { 0x0e, M5MOLS_RESTYPE_MONITOR, 352, 288 },     /* CIF */
  84        { 0x13, M5MOLS_RESTYPE_MONITOR, 480, 360 },
  85        { 0x15, M5MOLS_RESTYPE_MONITOR, 640, 360 },     /* qHD */
  86        { 0x17, M5MOLS_RESTYPE_MONITOR, 640, 480 },     /* VGA */
  87        { 0x18, M5MOLS_RESTYPE_MONITOR, 720, 480 },
  88        { 0x1a, M5MOLS_RESTYPE_MONITOR, 800, 480 },     /* WVGA */
  89        { 0x1f, M5MOLS_RESTYPE_MONITOR, 800, 600 },     /* SVGA */
  90        { 0x21, M5MOLS_RESTYPE_MONITOR, 1280, 720 },    /* HD */
  91        { 0x25, M5MOLS_RESTYPE_MONITOR, 1920, 1080 },   /* 1080p */
  92        { 0x29, M5MOLS_RESTYPE_MONITOR, 3264, 2448 },   /* 2.63fps 8M */
  93        { 0x39, M5MOLS_RESTYPE_MONITOR, 800, 602 },     /* AHS_MON debug */
  94
  95        { 0x02, M5MOLS_RESTYPE_CAPTURE, 320, 240 },     /* QVGA */
  96        { 0x04, M5MOLS_RESTYPE_CAPTURE, 400, 240 },     /* WQVGA */
  97        { 0x07, M5MOLS_RESTYPE_CAPTURE, 480, 360 },
  98        { 0x08, M5MOLS_RESTYPE_CAPTURE, 640, 360 },     /* qHD */
  99        { 0x09, M5MOLS_RESTYPE_CAPTURE, 640, 480 },     /* VGA */
 100        { 0x0a, M5MOLS_RESTYPE_CAPTURE, 800, 480 },     /* WVGA */
 101        { 0x10, M5MOLS_RESTYPE_CAPTURE, 1280, 720 },    /* HD */
 102        { 0x14, M5MOLS_RESTYPE_CAPTURE, 1280, 960 },    /* 1M */
 103        { 0x17, M5MOLS_RESTYPE_CAPTURE, 1600, 1200 },   /* 2M */
 104        { 0x19, M5MOLS_RESTYPE_CAPTURE, 1920, 1080 },   /* Full-HD */
 105        { 0x1a, M5MOLS_RESTYPE_CAPTURE, 2048, 1152 },   /* 3Mega */
 106        { 0x1b, M5MOLS_RESTYPE_CAPTURE, 2048, 1536 },
 107        { 0x1c, M5MOLS_RESTYPE_CAPTURE, 2560, 1440 },   /* 4Mega */
 108        { 0x1d, M5MOLS_RESTYPE_CAPTURE, 2560, 1536 },
 109        { 0x1f, M5MOLS_RESTYPE_CAPTURE, 2560, 1920 },   /* 5Mega */
 110        { 0x21, M5MOLS_RESTYPE_CAPTURE, 3264, 1836 },   /* 6Mega */
 111        { 0x22, M5MOLS_RESTYPE_CAPTURE, 3264, 1960 },
 112        { 0x25, M5MOLS_RESTYPE_CAPTURE, 3264, 2448 },   /* 8Mega */
 113};
 114
 115/**
 116 * m5mols_swap_byte - an byte array to integer conversion function
 117 * @size: size in bytes of I2C packet defined in the M-5MOLS datasheet
 118 *
 119 * Convert I2C data byte array with performing any required byte
 120 * reordering to assure proper values for each data type, regardless
 121 * of the architecture endianness.
 122 */
 123static u32 m5mols_swap_byte(u8 *data, u8 length)
 124{
 125        if (length == 1)
 126                return *data;
 127        else if (length == 2)
 128                return be16_to_cpu(*((__be16 *)data));
 129        else
 130                return be32_to_cpu(*((__be32 *)data));
 131}
 132
 133/**
 134 * m5mols_read -  I2C read function
 135 * @reg: combination of size, category and command for the I2C packet
 136 * @size: desired size of I2C packet
 137 * @val: read value
 138 *
 139 * Returns 0 on success, or else negative errno.
 140 */
 141static int m5mols_read(struct v4l2_subdev *sd, u32 size, u32 reg, u32 *val)
 142{
 143        struct i2c_client *client = v4l2_get_subdevdata(sd);
 144        struct m5mols_info *info = to_m5mols(sd);
 145        u8 rbuf[M5MOLS_I2C_MAX_SIZE + 1];
 146        u8 category = I2C_CATEGORY(reg);
 147        u8 cmd = I2C_COMMAND(reg);
 148        struct i2c_msg msg[2];
 149        u8 wbuf[5];
 150        int ret;
 151
 152        if (!client->adapter)
 153                return -ENODEV;
 154
 155        msg[0].addr = client->addr;
 156        msg[0].flags = 0;
 157        msg[0].len = 5;
 158        msg[0].buf = wbuf;
 159        wbuf[0] = 5;
 160        wbuf[1] = M5MOLS_BYTE_READ;
 161        wbuf[2] = category;
 162        wbuf[3] = cmd;
 163        wbuf[4] = size;
 164
 165        msg[1].addr = client->addr;
 166        msg[1].flags = I2C_M_RD;
 167        msg[1].len = size + 1;
 168        msg[1].buf = rbuf;
 169
 170        /* minimum stabilization time */
 171        usleep_range(200, 300);
 172
 173        ret = i2c_transfer(client->adapter, msg, 2);
 174
 175        if (ret == 2) {
 176                *val = m5mols_swap_byte(&rbuf[1], size);
 177                return 0;
 178        }
 179
 180        if (info->isp_ready)
 181                v4l2_err(sd, "read failed: size:%d cat:%02x cmd:%02x. %d\n",
 182                         size, category, cmd, ret);
 183
 184        return ret < 0 ? ret : -EIO;
 185}
 186
 187int m5mols_read_u8(struct v4l2_subdev *sd, u32 reg, u8 *val)
 188{
 189        u32 val_32;
 190        int ret;
 191
 192        if (I2C_SIZE(reg) != 1) {
 193                v4l2_err(sd, "Wrong data size\n");
 194                return -EINVAL;
 195        }
 196
 197        ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
 198        if (ret)
 199                return ret;
 200
 201        *val = (u8)val_32;
 202        return ret;
 203}
 204
 205int m5mols_read_u16(struct v4l2_subdev *sd, u32 reg, u16 *val)
 206{
 207        u32 val_32;
 208        int ret;
 209
 210        if (I2C_SIZE(reg) != 2) {
 211                v4l2_err(sd, "Wrong data size\n");
 212                return -EINVAL;
 213        }
 214
 215        ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
 216        if (ret)
 217                return ret;
 218
 219        *val = (u16)val_32;
 220        return ret;
 221}
 222
 223int m5mols_read_u32(struct v4l2_subdev *sd, u32 reg, u32 *val)
 224{
 225        if (I2C_SIZE(reg) != 4) {
 226                v4l2_err(sd, "Wrong data size\n");
 227                return -EINVAL;
 228        }
 229
 230        return m5mols_read(sd, I2C_SIZE(reg), reg, val);
 231}
 232
 233/**
 234 * m5mols_write - I2C command write function
 235 * @reg: combination of size, category and command for the I2C packet
 236 * @val: value to write
 237 *
 238 * Returns 0 on success, or else negative errno.
 239 */
 240int m5mols_write(struct v4l2_subdev *sd, u32 reg, u32 val)
 241{
 242        struct i2c_client *client = v4l2_get_subdevdata(sd);
 243        struct m5mols_info *info = to_m5mols(sd);
 244        u8 wbuf[M5MOLS_I2C_MAX_SIZE + 4];
 245        u8 category = I2C_CATEGORY(reg);
 246        u8 cmd = I2C_COMMAND(reg);
 247        u8 size = I2C_SIZE(reg);
 248        u32 *buf = (u32 *)&wbuf[4];
 249        struct i2c_msg msg[1];
 250        int ret;
 251
 252        if (!client->adapter)
 253                return -ENODEV;
 254
 255        if (size != 1 && size != 2 && size != 4) {
 256                v4l2_err(sd, "Wrong data size\n");
 257                return -EINVAL;
 258        }
 259
 260        msg->addr = client->addr;
 261        msg->flags = 0;
 262        msg->len = (u16)size + 4;
 263        msg->buf = wbuf;
 264        wbuf[0] = size + 4;
 265        wbuf[1] = M5MOLS_BYTE_WRITE;
 266        wbuf[2] = category;
 267        wbuf[3] = cmd;
 268
 269        *buf = m5mols_swap_byte((u8 *)&val, size);
 270
 271        /* minimum stabilization time */
 272        usleep_range(200, 300);
 273
 274        ret = i2c_transfer(client->adapter, msg, 1);
 275        if (ret == 1)
 276                return 0;
 277
 278        if (info->isp_ready)
 279                v4l2_err(sd, "write failed: cat:%02x cmd:%02x ret:%d\n",
 280                         category, cmd, ret);
 281
 282        return ret < 0 ? ret : -EIO;
 283}
 284
 285/**
 286 * m5mols_busy_wait - Busy waiting with I2C register polling
 287 * @reg: the I2C_REG() address of an 8-bit status register to check
 288 * @value: expected status register value
 289 * @mask: bit mask for the read status register value
 290 * @timeout: timeout in miliseconds, or -1 for default timeout
 291 *
 292 * The @reg register value is ORed with @mask before comparing with @value.
 293 *
 294 * Return: 0 if the requested condition became true within less than
 295 *         @timeout ms, or else negative errno.
 296 */
 297int m5mols_busy_wait(struct v4l2_subdev *sd, u32 reg, u32 value, u32 mask,
 298                     int timeout)
 299{
 300        int ms = timeout < 0 ? M5MOLS_BUSY_WAIT_DEF_TIMEOUT : timeout;
 301        unsigned long end = jiffies + msecs_to_jiffies(ms);
 302        u8 status;
 303
 304        do {
 305                int ret = m5mols_read_u8(sd, reg, &status);
 306
 307                if (ret < 0 && !(mask & M5MOLS_I2C_RDY_WAIT_FL))
 308                        return ret;
 309                if (!ret && (status & mask & 0xff) == (value & 0xff))
 310                        return 0;
 311                usleep_range(100, 250);
 312        } while (ms > 0 && time_is_after_jiffies(end));
 313
 314        return -EBUSY;
 315}
 316
 317/**
 318 * m5mols_enable_interrupt - Clear interrupt pending bits and unmask interrupts
 319 *
 320 * Before writing desired interrupt value the INT_FACTOR register should
 321 * be read to clear pending interrupts.
 322 */
 323int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg)
 324{
 325        struct m5mols_info *info = to_m5mols(sd);
 326        u8 mask = is_available_af(info) ? REG_INT_AF : 0;
 327        u8 dummy;
 328        int ret;
 329
 330        ret = m5mols_read_u8(sd, SYSTEM_INT_FACTOR, &dummy);
 331        if (!ret)
 332                ret = m5mols_write(sd, SYSTEM_INT_ENABLE, reg & ~mask);
 333        return ret;
 334}
 335
 336int m5mols_wait_interrupt(struct v4l2_subdev *sd, u8 irq_mask, u32 timeout)
 337{
 338        struct m5mols_info *info = to_m5mols(sd);
 339
 340        int ret = wait_event_interruptible_timeout(info->irq_waitq,
 341                                atomic_add_unless(&info->irq_done, -1, 0),
 342                                msecs_to_jiffies(timeout));
 343        if (ret <= 0)
 344                return ret ? ret : -ETIMEDOUT;
 345
 346        return m5mols_busy_wait(sd, SYSTEM_INT_FACTOR, irq_mask,
 347                                M5MOLS_I2C_RDY_WAIT_FL | irq_mask, -1);
 348}
 349
 350/**
 351 * m5mols_reg_mode - Write the mode and check busy status
 352 *
 353 * It always accompanies a little delay changing the M-5MOLS mode, so it is
 354 * needed checking current busy status to guarantee right mode.
 355 */
 356static int m5mols_reg_mode(struct v4l2_subdev *sd, u8 mode)
 357{
 358        int ret = m5mols_write(sd, SYSTEM_SYSMODE, mode);
 359        if (ret < 0)
 360                return ret;
 361        return m5mols_busy_wait(sd, SYSTEM_SYSMODE, mode, 0xff,
 362                                M5MOLS_MODE_CHANGE_TIMEOUT);
 363}
 364
 365/**
 366 * m5mols_set_mode - set the M-5MOLS controller mode
 367 * @mode: the required operation mode
 368 *
 369 * The commands of M-5MOLS are grouped into specific modes. Each functionality
 370 * can be guaranteed only when the sensor is operating in mode which a command
 371 * belongs to.
 372 */
 373int m5mols_set_mode(struct m5mols_info *info, u8 mode)
 374{
 375        struct v4l2_subdev *sd = &info->sd;
 376        int ret = -EINVAL;
 377        u8 reg;
 378
 379        if (mode < REG_PARAMETER || mode > REG_CAPTURE)
 380                return ret;
 381
 382        ret = m5mols_read_u8(sd, SYSTEM_SYSMODE, &reg);
 383        if (ret || reg == mode)
 384                return ret;
 385
 386        switch (reg) {
 387        case REG_PARAMETER:
 388                ret = m5mols_reg_mode(sd, REG_MONITOR);
 389                if (mode == REG_MONITOR)
 390                        break;
 391                if (!ret)
 392                        ret = m5mols_reg_mode(sd, REG_CAPTURE);
 393                break;
 394
 395        case REG_MONITOR:
 396                if (mode == REG_PARAMETER) {
 397                        ret = m5mols_reg_mode(sd, REG_PARAMETER);
 398                        break;
 399                }
 400
 401                ret = m5mols_reg_mode(sd, REG_CAPTURE);
 402                break;
 403
 404        case REG_CAPTURE:
 405                ret = m5mols_reg_mode(sd, REG_MONITOR);
 406                if (mode == REG_MONITOR)
 407                        break;
 408                if (!ret)
 409                        ret = m5mols_reg_mode(sd, REG_PARAMETER);
 410                break;
 411
 412        default:
 413                v4l2_warn(sd, "Wrong mode: %d\n", mode);
 414        }
 415
 416        if (!ret)
 417                info->mode = mode;
 418
 419        return ret;
 420}
 421
 422/**
 423 * m5mols_get_version - retrieve full revisions information of M-5MOLS
 424 *
 425 * The version information includes revisions of hardware and firmware,
 426 * AutoFocus alghorithm version and the version string.
 427 */
 428static int m5mols_get_version(struct v4l2_subdev *sd)
 429{
 430        struct m5mols_info *info = to_m5mols(sd);
 431        struct m5mols_version *ver = &info->ver;
 432        u8 *str = ver->str;
 433        int i;
 434        int ret;
 435
 436        ret = m5mols_read_u8(sd, SYSTEM_VER_CUSTOMER, &ver->customer);
 437        if (!ret)
 438                ret = m5mols_read_u8(sd, SYSTEM_VER_PROJECT, &ver->project);
 439        if (!ret)
 440                ret = m5mols_read_u16(sd, SYSTEM_VER_FIRMWARE, &ver->fw);
 441        if (!ret)
 442                ret = m5mols_read_u16(sd, SYSTEM_VER_HARDWARE, &ver->hw);
 443        if (!ret)
 444                ret = m5mols_read_u16(sd, SYSTEM_VER_PARAMETER, &ver->param);
 445        if (!ret)
 446                ret = m5mols_read_u16(sd, SYSTEM_VER_AWB, &ver->awb);
 447        if (!ret)
 448                ret = m5mols_read_u8(sd, AF_VERSION, &ver->af);
 449        if (ret)
 450                return ret;
 451
 452        for (i = 0; i < VERSION_STRING_SIZE; i++) {
 453                ret = m5mols_read_u8(sd, SYSTEM_VER_STRING, &str[i]);
 454                if (ret)
 455                        return ret;
 456        }
 457
 458        v4l2_info(sd, "Manufacturer\t[%s]\n",
 459                        is_manufacturer(info, REG_SAMSUNG_ELECTRO) ?
 460                        "Samsung Electro-Mechanics" :
 461                        is_manufacturer(info, REG_SAMSUNG_OPTICS) ?
 462                        "Samsung Fiber-Optics" :
 463                        is_manufacturer(info, REG_SAMSUNG_TECHWIN) ?
 464                        "Samsung Techwin" : "None");
 465        v4l2_info(sd, "Customer/Project\t[0x%02x/0x%02x]\n",
 466                        info->ver.customer, info->ver.project);
 467
 468        if (!is_available_af(info))
 469                v4l2_info(sd, "No support Auto Focus on this firmware\n");
 470
 471        return ret;
 472}
 473
 474/**
 475 * __find_restype - Lookup M-5MOLS resolution type according to pixel code
 476 * @code: pixel code
 477 */
 478static enum m5mols_restype __find_restype(u32 code)
 479{
 480        enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR;
 481
 482        do {
 483                if (code == m5mols_default_ffmt[type].code)
 484                        return type;
 485        } while (type++ != SIZE_DEFAULT_FFMT);
 486
 487        return 0;
 488}
 489
 490/**
 491 * __find_resolution - Lookup preset and type of M-5MOLS's resolution
 492 * @mf: pixel format to find/negotiate the resolution preset for
 493 * @type: M-5MOLS resolution type
 494 * @resolution: M-5MOLS resolution preset register value
 495 *
 496 * Find nearest resolution matching resolution preset and adjust mf
 497 * to supported values.
 498 */
 499static int __find_resolution(struct v4l2_subdev *sd,
 500                             struct v4l2_mbus_framefmt *mf,
 501                             enum m5mols_restype *type,
 502                             u32 *resolution)
 503{
 504        const struct m5mols_resolution *fsize = &m5mols_reg_res[0];
 505        const struct m5mols_resolution *match = NULL;
 506        enum m5mols_restype stype = __find_restype(mf->code);
 507        int i = ARRAY_SIZE(m5mols_reg_res);
 508        unsigned int min_err = ~0;
 509
 510        while (i--) {
 511                int err;
 512                if (stype == fsize->type) {
 513                        err = abs(fsize->width - mf->width)
 514                                + abs(fsize->height - mf->height);
 515
 516                        if (err < min_err) {
 517                                min_err = err;
 518                                match = fsize;
 519                        }
 520                }
 521                fsize++;
 522        }
 523        if (match) {
 524                mf->width  = match->width;
 525                mf->height = match->height;
 526                *resolution = match->reg;
 527                *type = stype;
 528                return 0;
 529        }
 530
 531        return -EINVAL;
 532}
 533
 534static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info,
 535                                struct v4l2_subdev_pad_config *cfg,
 536                                enum v4l2_subdev_format_whence which,
 537                                enum m5mols_restype type)
 538{
 539        if (which == V4L2_SUBDEV_FORMAT_TRY)
 540                return cfg ? v4l2_subdev_get_try_format(&info->sd, cfg, 0) : NULL;
 541
 542        return &info->ffmt[type];
 543}
 544
 545static int m5mols_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
 546                          struct v4l2_subdev_format *fmt)
 547{
 548        struct m5mols_info *info = to_m5mols(sd);
 549        struct v4l2_mbus_framefmt *format;
 550        int ret = 0;
 551
 552        mutex_lock(&info->lock);
 553
 554        format = __find_format(info, cfg, fmt->which, info->res_type);
 555        if (format)
 556                fmt->format = *format;
 557        else
 558                ret = -EINVAL;
 559
 560        mutex_unlock(&info->lock);
 561        return ret;
 562}
 563
 564static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
 565                          struct v4l2_subdev_format *fmt)
 566{
 567        struct m5mols_info *info = to_m5mols(sd);
 568        struct v4l2_mbus_framefmt *format = &fmt->format;
 569        struct v4l2_mbus_framefmt *sfmt;
 570        enum m5mols_restype type;
 571        u32 resolution = 0;
 572        int ret;
 573
 574        ret = __find_resolution(sd, format, &type, &resolution);
 575        if (ret < 0)
 576                return ret;
 577
 578        sfmt = __find_format(info, cfg, fmt->which, type);
 579        if (!sfmt)
 580                return 0;
 581
 582        mutex_lock(&info->lock);
 583
 584        format->code = m5mols_default_ffmt[type].code;
 585        format->colorspace = V4L2_COLORSPACE_JPEG;
 586        format->field = V4L2_FIELD_NONE;
 587
 588        if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
 589                *sfmt = *format;
 590                info->resolution = resolution;
 591                info->res_type = type;
 592        }
 593
 594        mutex_unlock(&info->lock);
 595        return ret;
 596}
 597
 598static int m5mols_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 599                                 struct v4l2_mbus_frame_desc *fd)
 600{
 601        struct m5mols_info *info = to_m5mols(sd);
 602
 603        if (pad != 0 || fd == NULL)
 604                return -EINVAL;
 605
 606        mutex_lock(&info->lock);
 607        /*
 608         * .get_frame_desc is only used for compressed formats,
 609         * thus we always return the capture frame parameters here.
 610         */
 611        fd->entry[0].length = info->cap.buf_size;
 612        fd->entry[0].pixelcode = info->ffmt[M5MOLS_RESTYPE_CAPTURE].code;
 613        mutex_unlock(&info->lock);
 614
 615        fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
 616        fd->num_entries = 1;
 617
 618        return 0;
 619}
 620
 621static int m5mols_set_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 622                                 struct v4l2_mbus_frame_desc *fd)
 623{
 624        struct m5mols_info *info = to_m5mols(sd);
 625        struct v4l2_mbus_framefmt *mf = &info->ffmt[M5MOLS_RESTYPE_CAPTURE];
 626
 627        if (pad != 0 || fd == NULL)
 628                return -EINVAL;
 629
 630        fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
 631        fd->num_entries = 1;
 632        fd->entry[0].length = clamp_t(u32, fd->entry[0].length,
 633                                      mf->width * mf->height,
 634                                      M5MOLS_MAIN_JPEG_SIZE_MAX);
 635        mutex_lock(&info->lock);
 636        info->cap.buf_size = fd->entry[0].length;
 637        mutex_unlock(&info->lock);
 638
 639        return 0;
 640}
 641
 642
 643static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
 644                                 struct v4l2_subdev_pad_config *cfg,
 645                                 struct v4l2_subdev_mbus_code_enum *code)
 646{
 647        if (!code || code->index >= SIZE_DEFAULT_FFMT)
 648                return -EINVAL;
 649
 650        code->code = m5mols_default_ffmt[code->index].code;
 651
 652        return 0;
 653}
 654
 655static const struct v4l2_subdev_pad_ops m5mols_pad_ops = {
 656        .enum_mbus_code = m5mols_enum_mbus_code,
 657        .get_fmt        = m5mols_get_fmt,
 658        .set_fmt        = m5mols_set_fmt,
 659        .get_frame_desc = m5mols_get_frame_desc,
 660        .set_frame_desc = m5mols_set_frame_desc,
 661};
 662
 663/**
 664 * m5mols_restore_controls - Apply current control values to the registers
 665 *
 666 * m5mols_do_scenemode() handles all parameters for which there is yet no
 667 * individual control. It should be replaced at some point by setting each
 668 * control individually, in required register set up order.
 669 */
 670int m5mols_restore_controls(struct m5mols_info *info)
 671{
 672        int ret;
 673
 674        if (info->ctrl_sync)
 675                return 0;
 676
 677        ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
 678        if (ret)
 679                return ret;
 680
 681        ret = v4l2_ctrl_handler_setup(&info->handle);
 682        info->ctrl_sync = !ret;
 683
 684        return ret;
 685}
 686
 687/**
 688 * m5mols_start_monitor - Start the monitor mode
 689 *
 690 * Before applying the controls setup the resolution and frame rate
 691 * in PARAMETER mode, and then switch over to MONITOR mode.
 692 */
 693static int m5mols_start_monitor(struct m5mols_info *info)
 694{
 695        struct v4l2_subdev *sd = &info->sd;
 696        int ret;
 697
 698        ret = m5mols_set_mode(info, REG_PARAMETER);
 699        if (!ret)
 700                ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
 701        if (!ret)
 702                ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
 703        if (!ret)
 704                ret = m5mols_set_mode(info, REG_MONITOR);
 705        if (!ret)
 706                ret = m5mols_restore_controls(info);
 707
 708        return ret;
 709}
 710
 711static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
 712{
 713        struct m5mols_info *info = to_m5mols(sd);
 714        u32 code;
 715        int ret;
 716
 717        mutex_lock(&info->lock);
 718        code = info->ffmt[info->res_type].code;
 719
 720        if (enable) {
 721                if (is_code(code, M5MOLS_RESTYPE_MONITOR))
 722                        ret = m5mols_start_monitor(info);
 723                else if (is_code(code, M5MOLS_RESTYPE_CAPTURE))
 724                        ret = m5mols_start_capture(info);
 725                else
 726                        ret = -EINVAL;
 727        } else {
 728                ret = m5mols_set_mode(info, REG_PARAMETER);
 729        }
 730
 731        mutex_unlock(&info->lock);
 732        return ret;
 733}
 734
 735static const struct v4l2_subdev_video_ops m5mols_video_ops = {
 736        .s_stream       = m5mols_s_stream,
 737};
 738
 739static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
 740{
 741        struct v4l2_subdev *sd = &info->sd;
 742        struct i2c_client *client = v4l2_get_subdevdata(sd);
 743        const struct m5mols_platform_data *pdata = info->pdata;
 744        int ret;
 745
 746        if (info->power == enable)
 747                return 0;
 748
 749        if (enable) {
 750                if (info->set_power) {
 751                        ret = info->set_power(&client->dev, 1);
 752                        if (ret)
 753                                return ret;
 754                }
 755
 756                ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
 757                if (ret) {
 758                        info->set_power(&client->dev, 0);
 759                        return ret;
 760                }
 761
 762                gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity);
 763                info->power = 1;
 764
 765                return ret;
 766        }
 767
 768        ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
 769        if (ret)
 770                return ret;
 771
 772        if (info->set_power)
 773                info->set_power(&client->dev, 0);
 774
 775        gpio_set_value(pdata->gpio_reset, pdata->reset_polarity);
 776
 777        info->isp_ready = 0;
 778        info->power = 0;
 779
 780        return ret;
 781}
 782
 783/* m5mols_update_fw - optional firmware update routine */
 784int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
 785                int (*set_power)(struct m5mols_info *, bool))
 786{
 787        return 0;
 788}
 789
 790/**
 791 * m5mols_fw_start - M-5MOLS internal ARM controller initialization
 792 *
 793 * Execute the M-5MOLS internal ARM controller initialization sequence.
 794 * This function should be called after the supply voltage has been
 795 * applied and before any requests to the device are made.
 796 */
 797static int m5mols_fw_start(struct v4l2_subdev *sd)
 798{
 799        struct m5mols_info *info = to_m5mols(sd);
 800        int ret;
 801
 802        atomic_set(&info->irq_done, 0);
 803        /* Wait until I2C slave is initialized in Flash Writer mode */
 804        ret = m5mols_busy_wait(sd, FLASH_CAM_START, REG_IN_FLASH_MODE,
 805                               M5MOLS_I2C_RDY_WAIT_FL | 0xff, -1);
 806        if (!ret)
 807                ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
 808        if (!ret)
 809                ret = m5mols_wait_interrupt(sd, REG_INT_MODE, 2000);
 810        if (ret < 0)
 811                return ret;
 812
 813        info->isp_ready = 1;
 814
 815        ret = m5mols_get_version(sd);
 816        if (!ret)
 817                ret = m5mols_update_fw(sd, m5mols_sensor_power);
 818        if (ret)
 819                return ret;
 820
 821        v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
 822
 823        ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
 824        if (!ret)
 825                ret = m5mols_enable_interrupt(sd,
 826                                REG_INT_AF | REG_INT_CAPTURE);
 827
 828        return ret;
 829}
 830
 831/* Execute the lens soft-landing algorithm */
 832static int m5mols_auto_focus_stop(struct m5mols_info *info)
 833{
 834        int ret;
 835
 836        ret = m5mols_write(&info->sd, AF_EXECUTE, REG_AF_STOP);
 837        if (!ret)
 838                ret = m5mols_write(&info->sd, AF_MODE, REG_AF_POWEROFF);
 839        if (!ret)
 840                ret = m5mols_busy_wait(&info->sd, SYSTEM_STATUS, REG_AF_IDLE,
 841                                       0xff, -1);
 842        return ret;
 843}
 844
 845/**
 846 * m5mols_s_power - Main sensor power control function
 847 *
 848 * To prevent breaking the lens when the sensor is powered off the Soft-Landing
 849 * algorithm is called where available. The Soft-Landing algorithm availability
 850 * dependends on the firmware provider.
 851 */
 852static int m5mols_s_power(struct v4l2_subdev *sd, int on)
 853{
 854        struct m5mols_info *info = to_m5mols(sd);
 855        int ret;
 856
 857        mutex_lock(&info->lock);
 858
 859        if (on) {
 860                ret = m5mols_sensor_power(info, true);
 861                if (!ret)
 862                        ret = m5mols_fw_start(sd);
 863        } else {
 864                if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
 865                        ret = m5mols_set_mode(info, REG_MONITOR);
 866                        if (!ret)
 867                                ret = m5mols_auto_focus_stop(info);
 868                        if (ret < 0)
 869                                v4l2_warn(sd, "Soft landing lens failed\n");
 870                }
 871                ret = m5mols_sensor_power(info, false);
 872
 873                info->ctrl_sync = 0;
 874        }
 875
 876        mutex_unlock(&info->lock);
 877        return ret;
 878}
 879
 880static int m5mols_log_status(struct v4l2_subdev *sd)
 881{
 882        struct m5mols_info *info = to_m5mols(sd);
 883
 884        v4l2_ctrl_handler_log_status(&info->handle, sd->name);
 885
 886        return 0;
 887}
 888
 889static const struct v4l2_subdev_core_ops m5mols_core_ops = {
 890        .s_power        = m5mols_s_power,
 891        .log_status     = m5mols_log_status,
 892};
 893
 894/*
 895 * V4L2 subdev internal operations
 896 */
 897static int m5mols_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 898{
 899        struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
 900
 901        *format = m5mols_default_ffmt[0];
 902        return 0;
 903}
 904
 905static const struct v4l2_subdev_internal_ops m5mols_subdev_internal_ops = {
 906        .open           = m5mols_open,
 907};
 908
 909static const struct v4l2_subdev_ops m5mols_ops = {
 910        .core           = &m5mols_core_ops,
 911        .pad            = &m5mols_pad_ops,
 912        .video          = &m5mols_video_ops,
 913};
 914
 915static irqreturn_t m5mols_irq_handler(int irq, void *data)
 916{
 917        struct m5mols_info *info = to_m5mols(data);
 918
 919        atomic_set(&info->irq_done, 1);
 920        wake_up_interruptible(&info->irq_waitq);
 921
 922        return IRQ_HANDLED;
 923}
 924
 925static int m5mols_probe(struct i2c_client *client,
 926                        const struct i2c_device_id *id)
 927{
 928        const struct m5mols_platform_data *pdata = client->dev.platform_data;
 929        unsigned long gpio_flags;
 930        struct m5mols_info *info;
 931        struct v4l2_subdev *sd;
 932        int ret;
 933
 934        if (pdata == NULL) {
 935                dev_err(&client->dev, "No platform data\n");
 936                return -EINVAL;
 937        }
 938
 939        if (!gpio_is_valid(pdata->gpio_reset)) {
 940                dev_err(&client->dev, "No valid RESET GPIO specified\n");
 941                return -EINVAL;
 942        }
 943
 944        if (!client->irq) {
 945                dev_err(&client->dev, "Interrupt not assigned\n");
 946                return -EINVAL;
 947        }
 948
 949        info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
 950        if (!info)
 951                return -ENOMEM;
 952
 953        info->pdata = pdata;
 954        info->set_power = pdata->set_power;
 955
 956        gpio_flags = pdata->reset_polarity
 957                   ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
 958        ret = devm_gpio_request_one(&client->dev, pdata->gpio_reset, gpio_flags,
 959                                    "M5MOLS_NRST");
 960        if (ret) {
 961                dev_err(&client->dev, "Failed to request gpio: %d\n", ret);
 962                return ret;
 963        }
 964
 965        ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies),
 966                                      supplies);
 967        if (ret) {
 968                dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
 969                return ret;
 970        }
 971
 972        sd = &info->sd;
 973        v4l2_i2c_subdev_init(sd, client, &m5mols_ops);
 974        strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
 975        sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
 976
 977        sd->internal_ops = &m5mols_subdev_internal_ops;
 978        info->pad.flags = MEDIA_PAD_FL_SOURCE;
 979        ret = media_entity_pads_init(&sd->entity, 1, &info->pad);
 980        if (ret < 0)
 981                return ret;
 982        sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
 983
 984        init_waitqueue_head(&info->irq_waitq);
 985        mutex_init(&info->lock);
 986
 987        ret = devm_request_irq(&client->dev, client->irq, m5mols_irq_handler,
 988                               IRQF_TRIGGER_RISING, MODULE_NAME, sd);
 989        if (ret) {
 990                dev_err(&client->dev, "Interrupt request failed: %d\n", ret);
 991                goto error;
 992        }
 993        info->res_type = M5MOLS_RESTYPE_MONITOR;
 994        info->ffmt[0] = m5mols_default_ffmt[0];
 995        info->ffmt[1] = m5mols_default_ffmt[1];
 996
 997        ret = m5mols_sensor_power(info, true);
 998        if (ret)
 999                goto error;
1000
1001        ret = m5mols_fw_start(sd);
1002        if (!ret)
1003                ret = m5mols_init_controls(sd);
1004
1005        ret = m5mols_sensor_power(info, false);
1006        if (!ret)
1007                return 0;
1008error:
1009        media_entity_cleanup(&sd->entity);
1010        return ret;
1011}
1012
1013static int m5mols_remove(struct i2c_client *client)
1014{
1015        struct v4l2_subdev *sd = i2c_get_clientdata(client);
1016
1017        v4l2_device_unregister_subdev(sd);
1018        v4l2_ctrl_handler_free(sd->ctrl_handler);
1019        media_entity_cleanup(&sd->entity);
1020
1021        return 0;
1022}
1023
1024static const struct i2c_device_id m5mols_id[] = {
1025        { MODULE_NAME, 0 },
1026        { },
1027};
1028MODULE_DEVICE_TABLE(i2c, m5mols_id);
1029
1030static struct i2c_driver m5mols_i2c_driver = {
1031        .driver = {
1032                .name   = MODULE_NAME,
1033        },
1034        .probe          = m5mols_probe,
1035        .remove         = m5mols_remove,
1036        .id_table       = m5mols_id,
1037};
1038
1039module_i2c_driver(m5mols_i2c_driver);
1040
1041MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
1042MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
1043MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
1044MODULE_LICENSE("GPL");
1045