1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for M-5MOLS 8M Pixel camera sensor with ISP
5 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
6 * Author: HeungJun Kim <riverful.kim@samsung.com>
8 * Copyright (C) 2009 Samsung Electronics Co., Ltd.
9 * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
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/consumer.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>
27 #include "m5mols_reg.h"
30 module_param(m5mols_debug, int, 0644);
32 #define MODULE_NAME "M5MOLS"
33 #define M5MOLS_I2C_CHECK_RETRY 500
35 /* The regulator consumer names for external voltage regulators */
36 static struct regulator_bulk_data supplies[] = {
38 .supply = "core", /* ARM core power, 1.2V */
40 .supply = "dig_18", /* digital power 1, 1.8V */
42 .supply = "d_sensor", /* sensor power 1, 1.8V */
44 .supply = "dig_28", /* digital power 2, 2.8V */
46 .supply = "a_sensor", /* analog power */
48 .supply = "dig_12", /* digital power 3, 1.2V */
52 static struct v4l2_mbus_framefmt m5mols_default_ffmt[M5MOLS_RESTYPE_MAX] = {
53 [M5MOLS_RESTYPE_MONITOR] = {
56 .code = MEDIA_BUS_FMT_VYUY8_2X8,
57 .field = V4L2_FIELD_NONE,
58 .colorspace = V4L2_COLORSPACE_JPEG,
60 [M5MOLS_RESTYPE_CAPTURE] = {
63 .code = MEDIA_BUS_FMT_JPEG_1X8,
64 .field = V4L2_FIELD_NONE,
65 .colorspace = V4L2_COLORSPACE_JPEG,
68 #define SIZE_DEFAULT_FFMT ARRAY_SIZE(m5mols_default_ffmt)
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 */
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 */
112 * m5mols_swap_byte - an byte array to integer conversion function
114 * @length: size in bytes of I2C packet defined in the M-5MOLS datasheet
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.
120 static u32 m5mols_swap_byte(u8 *data, u8 length)
124 else if (length == 2)
125 return be16_to_cpu(*((__be16 *)data));
127 return be32_to_cpu(*((__be32 *)data));
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
137 * Returns 0 on success, or else negative errno.
139 static int m5mols_read(struct v4l2_subdev *sd, u32 size, u32 reg, u32 *val)
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];
150 if (!client->adapter)
153 msg[0].addr = client->addr;
158 wbuf[1] = M5MOLS_BYTE_READ;
163 msg[1].addr = client->addr;
164 msg[1].flags = I2C_M_RD;
165 msg[1].len = size + 1;
168 /* minimum stabilization time */
169 usleep_range(200, 300);
171 ret = i2c_transfer(client->adapter, msg, 2);
174 *val = m5mols_swap_byte(&rbuf[1], size);
179 v4l2_err(sd, "read failed: size:%d cat:%02x cmd:%02x. %d\n",
180 size, category, cmd, ret);
182 return ret < 0 ? ret : -EIO;
185 int m5mols_read_u8(struct v4l2_subdev *sd, u32 reg, u8 *val)
190 if (I2C_SIZE(reg) != 1) {
191 v4l2_err(sd, "Wrong data size\n");
195 ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
203 int m5mols_read_u16(struct v4l2_subdev *sd, u32 reg, u16 *val)
208 if (I2C_SIZE(reg) != 2) {
209 v4l2_err(sd, "Wrong data size\n");
213 ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
221 int m5mols_read_u32(struct v4l2_subdev *sd, u32 reg, u32 *val)
223 if (I2C_SIZE(reg) != 4) {
224 v4l2_err(sd, "Wrong data size\n");
228 return m5mols_read(sd, I2C_SIZE(reg), reg, val);
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
237 * Returns 0 on success, or else negative errno.
239 int m5mols_write(struct v4l2_subdev *sd, u32 reg, u32 val)
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];
251 if (!client->adapter)
254 if (size != 1 && size != 2 && size != 4) {
255 v4l2_err(sd, "Wrong data size\n");
259 msg->addr = client->addr;
261 msg->len = (u16)size + 4;
264 wbuf[1] = M5MOLS_BYTE_WRITE;
268 *buf = m5mols_swap_byte((u8 *)&val, size);
270 /* minimum stabilization time */
271 usleep_range(200, 300);
273 ret = i2c_transfer(client->adapter, msg, 1);
278 v4l2_err(sd, "write failed: cat:%02x cmd:%02x ret:%d\n",
281 return ret < 0 ? ret : -EIO;
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
292 * The @reg register value is ORed with @mask before comparing with @value.
294 * Return: 0 if the requested condition became true within less than
295 * @timeout ms, or else negative errno.
297 int m5mols_busy_wait(struct v4l2_subdev *sd, u32 reg, u32 value, u32 mask,
300 int ms = timeout < 0 ? M5MOLS_BUSY_WAIT_DEF_TIMEOUT : timeout;
301 unsigned long end = jiffies + msecs_to_jiffies(ms);
305 int ret = m5mols_read_u8(sd, reg, &status);
307 if (ret < 0 && !(mask & M5MOLS_I2C_RDY_WAIT_FL))
309 if (!ret && (status & mask & 0xff) == (value & 0xff))
311 usleep_range(100, 250);
312 } while (ms > 0 && time_is_after_jiffies(end));
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
322 * Before writing desired interrupt value the INT_FACTOR register should
323 * be read to clear pending interrupts.
325 int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg)
327 struct m5mols_info *info = to_m5mols(sd);
328 u8 mask = is_available_af(info) ? REG_INT_AF : 0;
332 ret = m5mols_read_u8(sd, SYSTEM_INT_FACTOR, &dummy);
334 ret = m5mols_write(sd, SYSTEM_INT_ENABLE, reg & ~mask);
338 int m5mols_wait_interrupt(struct v4l2_subdev *sd, u8 irq_mask, u32 timeout)
340 struct m5mols_info *info = to_m5mols(sd);
342 int ret = wait_event_interruptible_timeout(info->irq_waitq,
343 atomic_add_unless(&info->irq_done, -1, 0),
344 msecs_to_jiffies(timeout));
346 return ret ? ret : -ETIMEDOUT;
348 return m5mols_busy_wait(sd, SYSTEM_INT_FACTOR, irq_mask,
349 M5MOLS_I2C_RDY_WAIT_FL | irq_mask, -1);
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
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.
360 static int m5mols_reg_mode(struct v4l2_subdev *sd, u8 mode)
362 int ret = m5mols_write(sd, SYSTEM_SYSMODE, mode);
365 return m5mols_busy_wait(sd, SYSTEM_SYSMODE, mode, 0xff,
366 M5MOLS_MODE_CHANGE_TIMEOUT);
370 * m5mols_set_mode - set the M-5MOLS controller mode
371 * @info: M-5MOLS driver data structure
372 * @mode: the required operation mode
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
378 int m5mols_set_mode(struct m5mols_info *info, u8 mode)
380 struct v4l2_subdev *sd = &info->sd;
384 if (mode < REG_PARAMETER || mode > REG_CAPTURE)
387 ret = m5mols_read_u8(sd, SYSTEM_SYSMODE, ®);
388 if (ret || reg == mode)
393 ret = m5mols_reg_mode(sd, REG_MONITOR);
394 if (mode == REG_MONITOR)
397 ret = m5mols_reg_mode(sd, REG_CAPTURE);
401 if (mode == REG_PARAMETER) {
402 ret = m5mols_reg_mode(sd, REG_PARAMETER);
406 ret = m5mols_reg_mode(sd, REG_CAPTURE);
410 ret = m5mols_reg_mode(sd, REG_MONITOR);
411 if (mode == REG_MONITOR)
414 ret = m5mols_reg_mode(sd, REG_PARAMETER);
418 v4l2_warn(sd, "Wrong mode: %d\n", mode);
428 * m5mols_get_version - retrieve full revisions information of M-5MOLS
429 * @sd: sub-device, as pointed by struct v4l2_subdev
431 * The version information includes revisions of hardware and firmware,
432 * AutoFocus alghorithm version and the version string.
434 static int m5mols_get_version(struct v4l2_subdev *sd)
436 struct m5mols_info *info = to_m5mols(sd);
437 struct m5mols_version *ver = &info->ver;
442 ret = m5mols_read_u8(sd, SYSTEM_VER_CUSTOMER, &ver->customer);
444 ret = m5mols_read_u8(sd, SYSTEM_VER_PROJECT, &ver->project);
446 ret = m5mols_read_u16(sd, SYSTEM_VER_FIRMWARE, &ver->fw);
448 ret = m5mols_read_u16(sd, SYSTEM_VER_HARDWARE, &ver->hw);
450 ret = m5mols_read_u16(sd, SYSTEM_VER_PARAMETER, &ver->param);
452 ret = m5mols_read_u16(sd, SYSTEM_VER_AWB, &ver->awb);
454 ret = m5mols_read_u8(sd, AF_VERSION, &ver->af);
458 for (i = 0; i < VERSION_STRING_SIZE; i++) {
459 ret = m5mols_read_u8(sd, SYSTEM_VER_STRING, &str[i]);
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);
474 if (!is_available_af(info))
475 v4l2_info(sd, "No support Auto Focus on this firmware\n");
481 * __find_restype - Lookup M-5MOLS resolution type according to pixel code
484 static enum m5mols_restype __find_restype(u32 code)
486 enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR;
489 if (code == m5mols_default_ffmt[type].code)
491 } while (++type != SIZE_DEFAULT_FFMT);
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
503 * Find nearest resolution matching resolution preset and adjust mf
504 * to supported values.
506 static int __find_resolution(struct v4l2_subdev *sd,
507 struct v4l2_mbus_framefmt *mf,
508 enum m5mols_restype *type,
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;
519 if (stype == fsize->type) {
520 err = abs(fsize->width - mf->width)
521 + abs(fsize->height - mf->height);
531 mf->width = match->width;
532 mf->height = match->height;
533 *resolution = match->reg;
541 static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info,
542 struct v4l2_subdev_state *sd_state,
543 enum v4l2_subdev_format_whence which,
544 enum m5mols_restype type)
546 if (which == V4L2_SUBDEV_FORMAT_TRY)
547 return sd_state ? v4l2_subdev_get_try_format(&info->sd,
550 return &info->ffmt[type];
553 static int m5mols_get_fmt(struct v4l2_subdev *sd,
554 struct v4l2_subdev_state *sd_state,
555 struct v4l2_subdev_format *fmt)
557 struct m5mols_info *info = to_m5mols(sd);
558 struct v4l2_mbus_framefmt *format;
561 mutex_lock(&info->lock);
563 format = __find_format(info, sd_state, fmt->which, info->res_type);
565 fmt->format = *format;
569 mutex_unlock(&info->lock);
573 static int m5mols_set_fmt(struct v4l2_subdev *sd,
574 struct v4l2_subdev_state *sd_state,
575 struct v4l2_subdev_format *fmt)
577 struct m5mols_info *info = to_m5mols(sd);
578 struct v4l2_mbus_framefmt *format = &fmt->format;
579 struct v4l2_mbus_framefmt *sfmt;
580 enum m5mols_restype type;
584 ret = __find_resolution(sd, format, &type, &resolution);
588 sfmt = __find_format(info, sd_state, fmt->which, type);
592 mutex_lock(&info->lock);
594 format->code = m5mols_default_ffmt[type].code;
595 format->colorspace = V4L2_COLORSPACE_JPEG;
596 format->field = V4L2_FIELD_NONE;
598 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
600 info->resolution = resolution;
601 info->res_type = type;
604 mutex_unlock(&info->lock);
608 static int m5mols_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
609 struct v4l2_mbus_frame_desc *fd)
611 struct m5mols_info *info = to_m5mols(sd);
613 if (pad != 0 || fd == NULL)
616 mutex_lock(&info->lock);
618 * .get_frame_desc is only used for compressed formats,
619 * thus we always return the capture frame parameters here.
621 fd->entry[0].length = info->cap.buf_size;
622 fd->entry[0].pixelcode = info->ffmt[M5MOLS_RESTYPE_CAPTURE].code;
623 mutex_unlock(&info->lock);
625 fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
631 static int m5mols_set_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
632 struct v4l2_mbus_frame_desc *fd)
634 struct m5mols_info *info = to_m5mols(sd);
635 struct v4l2_mbus_framefmt *mf = &info->ffmt[M5MOLS_RESTYPE_CAPTURE];
637 if (pad != 0 || fd == NULL)
640 fd->entry[0].flags = V4L2_MBUS_FRAME_DESC_FL_LEN_MAX;
642 fd->entry[0].length = clamp_t(u32, fd->entry[0].length,
643 mf->width * mf->height,
644 M5MOLS_MAIN_JPEG_SIZE_MAX);
645 mutex_lock(&info->lock);
646 info->cap.buf_size = fd->entry[0].length;
647 mutex_unlock(&info->lock);
653 static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
654 struct v4l2_subdev_state *sd_state,
655 struct v4l2_subdev_mbus_code_enum *code)
657 if (!code || code->index >= SIZE_DEFAULT_FFMT)
660 code->code = m5mols_default_ffmt[code->index].code;
665 static const struct v4l2_subdev_pad_ops m5mols_pad_ops = {
666 .enum_mbus_code = m5mols_enum_mbus_code,
667 .get_fmt = m5mols_get_fmt,
668 .set_fmt = m5mols_set_fmt,
669 .get_frame_desc = m5mols_get_frame_desc,
670 .set_frame_desc = m5mols_set_frame_desc,
674 * m5mols_restore_controls - Apply current control values to the registers
675 * @info: M-5MOLS driver data structure
677 * m5mols_do_scenemode() handles all parameters for which there is yet no
678 * individual control. It should be replaced at some point by setting each
679 * control individually, in required register set up order.
681 int m5mols_restore_controls(struct m5mols_info *info)
688 ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
692 ret = v4l2_ctrl_handler_setup(&info->handle);
693 info->ctrl_sync = !ret;
699 * m5mols_start_monitor - Start the monitor mode
700 * @info: M-5MOLS driver data structure
702 * Before applying the controls setup the resolution and frame rate
703 * in PARAMETER mode, and then switch over to MONITOR mode.
705 static int m5mols_start_monitor(struct m5mols_info *info)
707 struct v4l2_subdev *sd = &info->sd;
710 ret = m5mols_set_mode(info, REG_PARAMETER);
712 ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
714 ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
716 ret = m5mols_set_mode(info, REG_MONITOR);
718 ret = m5mols_restore_controls(info);
723 static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
725 struct m5mols_info *info = to_m5mols(sd);
729 mutex_lock(&info->lock);
730 code = info->ffmt[info->res_type].code;
733 if (is_code(code, M5MOLS_RESTYPE_MONITOR))
734 ret = m5mols_start_monitor(info);
735 else if (is_code(code, M5MOLS_RESTYPE_CAPTURE))
736 ret = m5mols_start_capture(info);
740 ret = m5mols_set_mode(info, REG_PARAMETER);
743 mutex_unlock(&info->lock);
747 static const struct v4l2_subdev_video_ops m5mols_video_ops = {
748 .s_stream = m5mols_s_stream,
751 static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
753 struct v4l2_subdev *sd = &info->sd;
754 struct i2c_client *client = v4l2_get_subdevdata(sd);
757 if (info->power == enable)
761 if (info->set_power) {
762 ret = info->set_power(&client->dev, 1);
767 ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
770 info->set_power(&client->dev, 0);
774 gpiod_set_value(info->reset, 0);
780 ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
785 info->set_power(&client->dev, 0);
787 gpiod_set_value(info->reset, 1);
795 /* m5mols_update_fw - optional firmware update routine */
796 int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
797 int (*set_power)(struct m5mols_info *, bool))
803 * m5mols_fw_start - M-5MOLS internal ARM controller initialization
804 * @sd: sub-device, as pointed by struct v4l2_subdev
806 * Execute the M-5MOLS internal ARM controller initialization sequence.
807 * This function should be called after the supply voltage has been
808 * applied and before any requests to the device are made.
810 static int m5mols_fw_start(struct v4l2_subdev *sd)
812 struct m5mols_info *info = to_m5mols(sd);
815 atomic_set(&info->irq_done, 0);
816 /* Wait until I2C slave is initialized in Flash Writer mode */
817 ret = m5mols_busy_wait(sd, FLASH_CAM_START, REG_IN_FLASH_MODE,
818 M5MOLS_I2C_RDY_WAIT_FL | 0xff, -1);
820 ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
822 ret = m5mols_wait_interrupt(sd, REG_INT_MODE, 2000);
828 ret = m5mols_get_version(sd);
830 ret = m5mols_update_fw(sd, m5mols_sensor_power);
834 v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
836 ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
838 ret = m5mols_enable_interrupt(sd,
839 REG_INT_AF | REG_INT_CAPTURE);
844 /* Execute the lens soft-landing algorithm */
845 static int m5mols_auto_focus_stop(struct m5mols_info *info)
849 ret = m5mols_write(&info->sd, AF_EXECUTE, REG_AF_STOP);
851 ret = m5mols_write(&info->sd, AF_MODE, REG_AF_POWEROFF);
853 ret = m5mols_busy_wait(&info->sd, SYSTEM_STATUS, REG_AF_IDLE,
859 * m5mols_s_power - Main sensor power control function
860 * @sd: sub-device, as pointed by struct v4l2_subdev
861 * @on: if true, powers on the device; powers off otherwise.
863 * To prevent breaking the lens when the sensor is powered off the Soft-Landing
864 * algorithm is called where available. The Soft-Landing algorithm availability
865 * dependends on the firmware provider.
867 static int m5mols_s_power(struct v4l2_subdev *sd, int on)
869 struct m5mols_info *info = to_m5mols(sd);
872 mutex_lock(&info->lock);
875 ret = m5mols_sensor_power(info, true);
877 ret = m5mols_fw_start(sd);
879 if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
880 ret = m5mols_set_mode(info, REG_MONITOR);
882 ret = m5mols_auto_focus_stop(info);
884 v4l2_warn(sd, "Soft landing lens failed\n");
886 ret = m5mols_sensor_power(info, false);
891 mutex_unlock(&info->lock);
895 static int m5mols_log_status(struct v4l2_subdev *sd)
897 struct m5mols_info *info = to_m5mols(sd);
899 v4l2_ctrl_handler_log_status(&info->handle, sd->name);
904 static const struct v4l2_subdev_core_ops m5mols_core_ops = {
905 .s_power = m5mols_s_power,
906 .log_status = m5mols_log_status,
910 * V4L2 subdev internal operations
912 static int m5mols_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
914 struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd,
918 *format = m5mols_default_ffmt[0];
922 static const struct v4l2_subdev_internal_ops m5mols_subdev_internal_ops = {
926 static const struct v4l2_subdev_ops m5mols_ops = {
927 .core = &m5mols_core_ops,
928 .pad = &m5mols_pad_ops,
929 .video = &m5mols_video_ops,
932 static irqreturn_t m5mols_irq_handler(int irq, void *data)
934 struct m5mols_info *info = to_m5mols(data);
936 atomic_set(&info->irq_done, 1);
937 wake_up_interruptible(&info->irq_waitq);
942 static int m5mols_probe(struct i2c_client *client,
943 const struct i2c_device_id *id)
945 const struct m5mols_platform_data *pdata = client->dev.platform_data;
946 struct m5mols_info *info;
947 struct v4l2_subdev *sd;
951 dev_err(&client->dev, "No platform data\n");
956 dev_err(&client->dev, "Interrupt not assigned\n");
960 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
964 /* This asserts reset, descriptor shall have polarity specified */
965 info->reset = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
966 if (IS_ERR(info->reset))
967 return PTR_ERR(info->reset);
968 /* Notice: the "N" in M5MOLS_NRST implies active low */
969 gpiod_set_consumer_name(info->reset, "M5MOLS_NRST");
972 info->set_power = pdata->set_power;
974 ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies),
977 dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
982 v4l2_i2c_subdev_init(sd, client, &m5mols_ops);
983 /* Static name; NEVER use in new drivers! */
984 strscpy(sd->name, MODULE_NAME, sizeof(sd->name));
985 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
987 sd->internal_ops = &m5mols_subdev_internal_ops;
988 info->pad.flags = MEDIA_PAD_FL_SOURCE;
989 ret = media_entity_pads_init(&sd->entity, 1, &info->pad);
992 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
994 init_waitqueue_head(&info->irq_waitq);
995 mutex_init(&info->lock);
997 ret = devm_request_irq(&client->dev, client->irq, m5mols_irq_handler,
998 IRQF_TRIGGER_RISING, MODULE_NAME, sd);
1000 dev_err(&client->dev, "Interrupt request failed: %d\n", ret);
1003 info->res_type = M5MOLS_RESTYPE_MONITOR;
1004 info->ffmt[0] = m5mols_default_ffmt[0];
1005 info->ffmt[1] = m5mols_default_ffmt[1];
1007 ret = m5mols_sensor_power(info, true);
1011 ret = m5mols_fw_start(sd);
1013 ret = m5mols_init_controls(sd);
1015 ret = m5mols_sensor_power(info, false);
1019 media_entity_cleanup(&sd->entity);
1023 static void m5mols_remove(struct i2c_client *client)
1025 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1027 v4l2_device_unregister_subdev(sd);
1028 v4l2_ctrl_handler_free(sd->ctrl_handler);
1029 media_entity_cleanup(&sd->entity);
1032 static const struct i2c_device_id m5mols_id[] = {
1036 MODULE_DEVICE_TABLE(i2c, m5mols_id);
1038 static struct i2c_driver m5mols_i2c_driver = {
1040 .name = MODULE_NAME,
1042 .probe = m5mols_probe,
1043 .remove = m5mols_remove,
1044 .id_table = m5mols_id,
1047 module_i2c_driver(m5mols_i2c_driver);
1049 MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
1050 MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
1051 MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
1052 MODULE_LICENSE("GPL");