root/drivers/media/i2c/m5mols/m5mols_core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. m5mols_swap_byte
  2. m5mols_read
  3. m5mols_read_u8
  4. m5mols_read_u16
  5. m5mols_read_u32
  6. m5mols_write
  7. m5mols_busy_wait
  8. m5mols_enable_interrupt
  9. m5mols_wait_interrupt
  10. m5mols_reg_mode
  11. m5mols_set_mode
  12. m5mols_get_version
  13. __find_restype
  14. __find_resolution
  15. __find_format
  16. m5mols_get_fmt
  17. m5mols_set_fmt
  18. m5mols_get_frame_desc
  19. m5mols_set_frame_desc
  20. m5mols_enum_mbus_code
  21. m5mols_restore_controls
  22. m5mols_start_monitor
  23. m5mols_s_stream
  24. m5mols_sensor_power
  25. m5mols_update_fw
  26. m5mols_fw_start
  27. m5mols_auto_focus_stop
  28. m5mols_s_power
  29. m5mols_log_status
  30. m5mols_open
  31. m5mols_irq_handler
  32. m5mols_probe
  33. m5mols_remove

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

/* [<][>][^][v][top][bottom][index][help] */