2 * Driver for M-5MOLS 8M Pixel camera sensor with ISP
4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5 * Author: HeungJun Kim <riverful.kim@samsung.com>
7 * Copyright (C) 2009 Samsung Electronics Co., Ltd.
8 * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
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.
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/m5mols.h>
31 #include "m5mols_reg.h"
34 module_param(m5mols_debug
, int, 0644);
36 #define MODULE_NAME "M5MOLS"
37 #define M5MOLS_I2C_CHECK_RETRY 500
39 /* The regulator consumer names for external voltage regulators */
40 static struct regulator_bulk_data supplies
[] = {
42 .supply
= "core", /* ARM core power, 1.2V */
44 .supply
= "dig_18", /* digital power 1, 1.8V */
46 .supply
= "d_sensor", /* sensor power 1, 1.8V */
48 .supply
= "dig_28", /* digital power 2, 2.8V */
50 .supply
= "a_sensor", /* analog power */
52 .supply
= "dig_12", /* digital power 3, 1.2V */
56 static struct v4l2_mbus_framefmt m5mols_default_ffmt
[M5MOLS_RESTYPE_MAX
] = {
57 [M5MOLS_RESTYPE_MONITOR
] = {
60 .code
= V4L2_MBUS_FMT_VYUY8_2X8
,
61 .field
= V4L2_FIELD_NONE
,
62 .colorspace
= V4L2_COLORSPACE_JPEG
,
64 [M5MOLS_RESTYPE_CAPTURE
] = {
67 .code
= V4L2_MBUS_FMT_JPEG_1X8
,
68 .field
= V4L2_FIELD_NONE
,
69 .colorspace
= V4L2_COLORSPACE_JPEG
,
72 #define SIZE_DEFAULT_FFMT ARRAY_SIZE(m5mols_default_ffmt)
74 static 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 */
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 */
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
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.
123 static u32
m5mols_swap_byte(u8
*data
, u8 length
)
127 else if (length
== 2)
128 return be16_to_cpu(*((u16
*)data
));
130 return be32_to_cpu(*((u32
*)data
));
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
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 u8 rbuf
[M5MOLS_I2C_MAX_SIZE
+ 1];
143 u8 category
= I2C_CATEGORY(reg
);
144 u8 cmd
= I2C_COMMAND(reg
);
145 struct i2c_msg msg
[2];
149 if (!client
->adapter
)
152 msg
[0].addr
= client
->addr
;
157 wbuf
[1] = M5MOLS_BYTE_READ
;
162 msg
[1].addr
= client
->addr
;
163 msg
[1].flags
= I2C_M_RD
;
164 msg
[1].len
= size
+ 1;
167 /* minimum stabilization time */
168 usleep_range(200, 200);
170 ret
= i2c_transfer(client
->adapter
, msg
, 2);
172 v4l2_err(sd
, "read failed: size:%d cat:%02x cmd:%02x. %d\n",
173 size
, category
, cmd
, ret
);
177 *val
= m5mols_swap_byte(&rbuf
[1], size
);
182 int m5mols_read_u8(struct v4l2_subdev
*sd
, u32 reg
, u8
*val
)
187 if (I2C_SIZE(reg
) != 1) {
188 v4l2_err(sd
, "Wrong data size\n");
192 ret
= m5mols_read(sd
, I2C_SIZE(reg
), reg
, &val_32
);
200 int m5mols_read_u16(struct v4l2_subdev
*sd
, u32 reg
, u16
*val
)
205 if (I2C_SIZE(reg
) != 2) {
206 v4l2_err(sd
, "Wrong data size\n");
210 ret
= m5mols_read(sd
, I2C_SIZE(reg
), reg
, &val_32
);
218 int m5mols_read_u32(struct v4l2_subdev
*sd
, u32 reg
, u32
*val
)
220 if (I2C_SIZE(reg
) != 4) {
221 v4l2_err(sd
, "Wrong data size\n");
225 return m5mols_read(sd
, I2C_SIZE(reg
), reg
, val
);
229 * m5mols_write - I2C command write function
230 * @reg: combination of size, category and command for the I2C packet
231 * @val: value to write
233 int m5mols_write(struct v4l2_subdev
*sd
, u32 reg
, u32 val
)
235 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
236 u8 wbuf
[M5MOLS_I2C_MAX_SIZE
+ 4];
237 u8 category
= I2C_CATEGORY(reg
);
238 u8 cmd
= I2C_COMMAND(reg
);
239 u8 size
= I2C_SIZE(reg
);
240 u32
*buf
= (u32
*)&wbuf
[4];
241 struct i2c_msg msg
[1];
244 if (!client
->adapter
)
247 if (size
!= 1 && size
!= 2 && size
!= 4) {
248 v4l2_err(sd
, "Wrong data size\n");
252 msg
->addr
= client
->addr
;
254 msg
->len
= (u16
)size
+ 4;
257 wbuf
[1] = M5MOLS_BYTE_WRITE
;
261 *buf
= m5mols_swap_byte((u8
*)&val
, size
);
263 usleep_range(200, 200);
265 ret
= i2c_transfer(client
->adapter
, msg
, 1);
267 v4l2_err(sd
, "write failed: size:%d cat:%02x cmd:%02x. %d\n",
268 size
, category
, cmd
, ret
);
275 int m5mols_busy(struct v4l2_subdev
*sd
, u8 category
, u8 cmd
, u8 mask
)
281 for (i
= 0; i
< M5MOLS_I2C_CHECK_RETRY
; i
++) {
282 ret
= m5mols_read_u8(sd
, I2C_REG(category
, cmd
, 1), &busy
);
285 if ((busy
& mask
) == mask
)
292 * m5mols_enable_interrupt - Clear interrupt pending bits and unmask interrupts
294 * Before writing desired interrupt value the INT_FACTOR register should
295 * be read to clear pending interrupts.
297 int m5mols_enable_interrupt(struct v4l2_subdev
*sd
, u8 reg
)
299 struct m5mols_info
*info
= to_m5mols(sd
);
300 u8 mask
= is_available_af(info
) ? REG_INT_AF
: 0;
304 ret
= m5mols_read_u8(sd
, SYSTEM_INT_FACTOR
, &dummy
);
306 ret
= m5mols_write(sd
, SYSTEM_INT_ENABLE
, reg
& ~mask
);
311 * m5mols_reg_mode - Write the mode and check busy status
313 * It always accompanies a little delay changing the M-5MOLS mode, so it is
314 * needed checking current busy status to guarantee right mode.
316 static int m5mols_reg_mode(struct v4l2_subdev
*sd
, u8 mode
)
318 int ret
= m5mols_write(sd
, SYSTEM_SYSMODE
, mode
);
320 return ret
? ret
: m5mols_busy(sd
, CAT_SYSTEM
, CAT0_SYSMODE
, mode
);
324 * m5mols_mode - manage the M-5MOLS's mode
325 * @mode: the required operation mode
327 * The commands of M-5MOLS are grouped into specific modes. Each functionality
328 * can be guaranteed only when the sensor is operating in mode which which
329 * a command belongs to.
331 int m5mols_mode(struct m5mols_info
*info
, u8 mode
)
333 struct v4l2_subdev
*sd
= &info
->sd
;
337 if (mode
< REG_PARAMETER
|| mode
> REG_CAPTURE
)
340 ret
= m5mols_read_u8(sd
, SYSTEM_SYSMODE
, ®
);
341 if ((!ret
&& reg
== mode
) || ret
)
346 ret
= m5mols_reg_mode(sd
, REG_MONITOR
);
347 if (!ret
&& mode
== REG_MONITOR
)
350 ret
= m5mols_reg_mode(sd
, REG_CAPTURE
);
354 if (mode
== REG_PARAMETER
) {
355 ret
= m5mols_reg_mode(sd
, REG_PARAMETER
);
359 ret
= m5mols_reg_mode(sd
, REG_CAPTURE
);
363 ret
= m5mols_reg_mode(sd
, REG_MONITOR
);
364 if (!ret
&& mode
== REG_MONITOR
)
367 ret
= m5mols_reg_mode(sd
, REG_PARAMETER
);
371 v4l2_warn(sd
, "Wrong mode: %d\n", mode
);
381 * m5mols_get_version - retrieve full revisions information of M-5MOLS
383 * The version information includes revisions of hardware and firmware,
384 * AutoFocus alghorithm version and the version string.
386 static int m5mols_get_version(struct v4l2_subdev
*sd
)
388 struct m5mols_info
*info
= to_m5mols(sd
);
389 struct m5mols_version
*ver
= &info
->ver
;
394 ret
= m5mols_read_u8(sd
, SYSTEM_VER_CUSTOMER
, &ver
->customer
);
396 ret
= m5mols_read_u8(sd
, SYSTEM_VER_PROJECT
, &ver
->project
);
398 ret
= m5mols_read_u16(sd
, SYSTEM_VER_FIRMWARE
, &ver
->fw
);
400 ret
= m5mols_read_u16(sd
, SYSTEM_VER_HARDWARE
, &ver
->hw
);
402 ret
= m5mols_read_u16(sd
, SYSTEM_VER_PARAMETER
, &ver
->param
);
404 ret
= m5mols_read_u16(sd
, SYSTEM_VER_AWB
, &ver
->awb
);
406 ret
= m5mols_read_u8(sd
, AF_VERSION
, &ver
->af
);
410 for (i
= 0; i
< VERSION_STRING_SIZE
; i
++) {
411 ret
= m5mols_read_u8(sd
, SYSTEM_VER_STRING
, &str
[i
]);
416 ver
->fw
= be16_to_cpu(ver
->fw
);
417 ver
->hw
= be16_to_cpu(ver
->hw
);
418 ver
->param
= be16_to_cpu(ver
->param
);
419 ver
->awb
= be16_to_cpu(ver
->awb
);
421 v4l2_info(sd
, "Manufacturer\t[%s]\n",
422 is_manufacturer(info
, REG_SAMSUNG_ELECTRO
) ?
423 "Samsung Electro-Machanics" :
424 is_manufacturer(info
, REG_SAMSUNG_OPTICS
) ?
425 "Samsung Fiber-Optics" :
426 is_manufacturer(info
, REG_SAMSUNG_TECHWIN
) ?
427 "Samsung Techwin" : "None");
428 v4l2_info(sd
, "Customer/Project\t[0x%02x/0x%02x]\n",
429 info
->ver
.customer
, info
->ver
.project
);
431 if (!is_available_af(info
))
432 v4l2_info(sd
, "No support Auto Focus on this firmware\n");
438 * __find_restype - Lookup M-5MOLS resolution type according to pixel code
441 static enum m5mols_restype
__find_restype(enum v4l2_mbus_pixelcode code
)
443 enum m5mols_restype type
= M5MOLS_RESTYPE_MONITOR
;
446 if (code
== m5mols_default_ffmt
[type
].code
)
448 } while (type
++ != SIZE_DEFAULT_FFMT
);
454 * __find_resolution - Lookup preset and type of M-5MOLS's resolution
455 * @mf: pixel format to find/negotiate the resolution preset for
456 * @type: M-5MOLS resolution type
457 * @resolution: M-5MOLS resolution preset register value
459 * Find nearest resolution matching resolution preset and adjust mf
460 * to supported values.
462 static int __find_resolution(struct v4l2_subdev
*sd
,
463 struct v4l2_mbus_framefmt
*mf
,
464 enum m5mols_restype
*type
,
467 const struct m5mols_resolution
*fsize
= &m5mols_reg_res
[0];
468 const struct m5mols_resolution
*match
= NULL
;
469 enum m5mols_restype stype
= __find_restype(mf
->code
);
470 int i
= ARRAY_SIZE(m5mols_reg_res
);
471 unsigned int min_err
= ~0;
475 if (stype
== fsize
->type
) {
476 err
= abs(fsize
->width
- mf
->width
)
477 + abs(fsize
->height
- mf
->height
);
487 mf
->width
= match
->width
;
488 mf
->height
= match
->height
;
489 *resolution
= match
->reg
;
497 static struct v4l2_mbus_framefmt
*__find_format(struct m5mols_info
*info
,
498 struct v4l2_subdev_fh
*fh
,
499 enum v4l2_subdev_format_whence which
,
500 enum m5mols_restype type
)
502 if (which
== V4L2_SUBDEV_FORMAT_TRY
)
503 return fh
? v4l2_subdev_get_try_format(fh
, 0) : NULL
;
505 return &info
->ffmt
[type
];
508 static int m5mols_get_fmt(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
,
509 struct v4l2_subdev_format
*fmt
)
511 struct m5mols_info
*info
= to_m5mols(sd
);
512 struct v4l2_mbus_framefmt
*format
;
514 format
= __find_format(info
, fh
, fmt
->which
, info
->res_type
);
518 fmt
->format
= *format
;
522 static int m5mols_set_fmt(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
,
523 struct v4l2_subdev_format
*fmt
)
525 struct m5mols_info
*info
= to_m5mols(sd
);
526 struct v4l2_mbus_framefmt
*format
= &fmt
->format
;
527 struct v4l2_mbus_framefmt
*sfmt
;
528 enum m5mols_restype type
;
532 ret
= __find_resolution(sd
, format
, &type
, &resolution
);
536 sfmt
= __find_format(info
, fh
, fmt
->which
, type
);
541 format
->code
= m5mols_default_ffmt
[type
].code
;
542 format
->colorspace
= V4L2_COLORSPACE_JPEG
;
543 format
->field
= V4L2_FIELD_NONE
;
545 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
547 info
->resolution
= resolution
;
548 info
->res_type
= type
;
554 static int m5mols_enum_mbus_code(struct v4l2_subdev
*sd
,
555 struct v4l2_subdev_fh
*fh
,
556 struct v4l2_subdev_mbus_code_enum
*code
)
558 if (!code
|| code
->index
>= SIZE_DEFAULT_FFMT
)
561 code
->code
= m5mols_default_ffmt
[code
->index
].code
;
566 static struct v4l2_subdev_pad_ops m5mols_pad_ops
= {
567 .enum_mbus_code
= m5mols_enum_mbus_code
,
568 .get_fmt
= m5mols_get_fmt
,
569 .set_fmt
= m5mols_set_fmt
,
573 * m5mols_sync_controls - Apply default scene mode and the current controls
575 * This is used only streaming for syncing between v4l2_ctrl framework and
576 * m5mols's controls. First, do the scenemode to the sensor, then call
577 * v4l2_ctrl_handler_setup. It can be same between some commands and
578 * the scenemode's in the default v4l2_ctrls. But, such commands of control
579 * should be prior to the scenemode's one.
581 int m5mols_sync_controls(struct m5mols_info
*info
)
585 if (!is_ctrl_synced(info
)) {
586 ret
= m5mols_do_scenemode(info
, REG_SCENE_NORMAL
);
590 v4l2_ctrl_handler_setup(&info
->handle
);
591 info
->ctrl_sync
= true;
598 * m5mols_start_monitor - Start the monitor mode
600 * Before applying the controls setup the resolution and frame rate
601 * in PARAMETER mode, and then switch over to MONITOR mode.
603 static int m5mols_start_monitor(struct m5mols_info
*info
)
605 struct v4l2_subdev
*sd
= &info
->sd
;
608 ret
= m5mols_mode(info
, REG_PARAMETER
);
610 ret
= m5mols_write(sd
, PARM_MON_SIZE
, info
->resolution
);
612 ret
= m5mols_write(sd
, PARM_MON_FPS
, REG_FPS_30
);
614 ret
= m5mols_mode(info
, REG_MONITOR
);
616 ret
= m5mols_sync_controls(info
);
621 static int m5mols_s_stream(struct v4l2_subdev
*sd
, int enable
)
623 struct m5mols_info
*info
= to_m5mols(sd
);
624 u32 code
= info
->ffmt
[info
->res_type
].code
;
629 if (is_code(code
, M5MOLS_RESTYPE_MONITOR
))
630 ret
= m5mols_start_monitor(info
);
631 if (is_code(code
, M5MOLS_RESTYPE_CAPTURE
))
632 ret
= m5mols_start_capture(info
);
637 return m5mols_mode(info
, REG_PARAMETER
);
640 static const struct v4l2_subdev_video_ops m5mols_video_ops
= {
641 .s_stream
= m5mols_s_stream
,
644 static int m5mols_s_ctrl(struct v4l2_ctrl
*ctrl
)
646 struct v4l2_subdev
*sd
= to_sd(ctrl
);
647 struct m5mols_info
*info
= to_m5mols(sd
);
650 info
->mode_save
= info
->mode
;
652 ret
= m5mols_mode(info
, REG_PARAMETER
);
654 ret
= m5mols_set_ctrl(ctrl
);
656 ret
= m5mols_mode(info
, info
->mode_save
);
661 static const struct v4l2_ctrl_ops m5mols_ctrl_ops
= {
662 .s_ctrl
= m5mols_s_ctrl
,
665 static int m5mols_sensor_power(struct m5mols_info
*info
, bool enable
)
667 struct v4l2_subdev
*sd
= &info
->sd
;
668 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
669 const struct m5mols_platform_data
*pdata
= info
->pdata
;
673 if (is_powered(info
))
676 if (info
->set_power
) {
677 ret
= info
->set_power(&client
->dev
, 1);
682 ret
= regulator_bulk_enable(ARRAY_SIZE(supplies
), supplies
);
684 info
->set_power(&client
->dev
, 0);
688 gpio_set_value(pdata
->gpio_reset
, !pdata
->reset_polarity
);
689 usleep_range(1000, 1000);
695 if (!is_powered(info
))
698 ret
= regulator_bulk_disable(ARRAY_SIZE(supplies
), supplies
);
703 info
->set_power(&client
->dev
, 0);
705 gpio_set_value(pdata
->gpio_reset
, pdata
->reset_polarity
);
706 usleep_range(1000, 1000);
712 /* m5mols_update_fw - optional firmware update routine */
713 int __attribute__ ((weak
)) m5mols_update_fw(struct v4l2_subdev
*sd
,
714 int (*set_power
)(struct m5mols_info
*, bool))
720 * m5mols_sensor_armboot - Booting M-5MOLS internal ARM core.
722 * Booting internal ARM core makes the M-5MOLS is ready for getting commands
723 * with I2C. It's the first thing to be done after it powered up. It must wait
724 * at least 520ms recommended by M-5MOLS datasheet, after executing arm booting.
726 static int m5mols_sensor_armboot(struct v4l2_subdev
*sd
)
730 ret
= m5mols_write(sd
, FLASH_CAM_START
, REG_START_ARM_BOOT
);
736 ret
= m5mols_get_version(sd
);
738 ret
= m5mols_update_fw(sd
, m5mols_sensor_power
);
742 v4l2_dbg(1, m5mols_debug
, sd
, "Success ARM Booting\n");
744 ret
= m5mols_write(sd
, PARM_INTERFACE
, REG_INTERFACE_MIPI
);
746 ret
= m5mols_enable_interrupt(sd
, REG_INT_AF
);
751 static int m5mols_init_controls(struct m5mols_info
*info
)
753 struct v4l2_subdev
*sd
= &info
->sd
;
758 /* Determine value's range & step of controls for various FW version */
759 ret
= m5mols_read_u16(sd
, AE_MAX_GAIN_MON
, &max_exposure
);
761 step_zoom
= is_manufacturer(info
, REG_SAMSUNG_OPTICS
) ? 31 : 1;
765 v4l2_ctrl_handler_init(&info
->handle
, 6);
766 info
->autowb
= v4l2_ctrl_new_std(&info
->handle
,
767 &m5mols_ctrl_ops
, V4L2_CID_AUTO_WHITE_BALANCE
,
769 info
->saturation
= v4l2_ctrl_new_std(&info
->handle
,
770 &m5mols_ctrl_ops
, V4L2_CID_SATURATION
,
772 info
->zoom
= v4l2_ctrl_new_std(&info
->handle
,
773 &m5mols_ctrl_ops
, V4L2_CID_ZOOM_ABSOLUTE
,
774 1, 70, step_zoom
, 1);
775 info
->exposure
= v4l2_ctrl_new_std(&info
->handle
,
776 &m5mols_ctrl_ops
, V4L2_CID_EXPOSURE
,
777 0, max_exposure
, 1, (int)max_exposure
/2);
778 info
->colorfx
= v4l2_ctrl_new_std_menu(&info
->handle
,
779 &m5mols_ctrl_ops
, V4L2_CID_COLORFX
,
780 4, (1 << V4L2_COLORFX_BW
), V4L2_COLORFX_NONE
);
781 info
->autoexposure
= v4l2_ctrl_new_std_menu(&info
->handle
,
782 &m5mols_ctrl_ops
, V4L2_CID_EXPOSURE_AUTO
,
783 1, 0, V4L2_EXPOSURE_MANUAL
);
785 sd
->ctrl_handler
= &info
->handle
;
786 if (info
->handle
.error
) {
787 v4l2_err(sd
, "Failed to initialize controls: %d\n", ret
);
788 v4l2_ctrl_handler_free(&info
->handle
);
789 return info
->handle
.error
;
792 v4l2_ctrl_cluster(2, &info
->autoexposure
);
798 * m5mols_s_power - Main sensor power control function
800 * To prevent breaking the lens when the sensor is powered off the Soft-Landing
801 * algorithm is called where available. The Soft-Landing algorithm availability
802 * dependends on the firmware provider.
804 static int m5mols_s_power(struct v4l2_subdev
*sd
, int on
)
806 struct m5mols_info
*info
= to_m5mols(sd
);
810 ret
= m5mols_sensor_power(info
, true);
812 ret
= m5mols_sensor_armboot(sd
);
814 ret
= m5mols_init_controls(info
);
818 info
->ffmt
[M5MOLS_RESTYPE_MONITOR
] =
819 m5mols_default_ffmt
[M5MOLS_RESTYPE_MONITOR
];
820 info
->ffmt
[M5MOLS_RESTYPE_CAPTURE
] =
821 m5mols_default_ffmt
[M5MOLS_RESTYPE_CAPTURE
];
825 if (is_manufacturer(info
, REG_SAMSUNG_TECHWIN
)) {
826 ret
= m5mols_mode(info
, REG_MONITOR
);
828 ret
= m5mols_write(sd
, AF_EXECUTE
, REG_AF_STOP
);
830 ret
= m5mols_write(sd
, AF_MODE
, REG_AF_POWEROFF
);
832 ret
= m5mols_busy(sd
, CAT_SYSTEM
, CAT0_STATUS
,
835 v4l2_info(sd
, "Success soft-landing lens\n");
838 ret
= m5mols_sensor_power(info
, false);
840 v4l2_ctrl_handler_free(&info
->handle
);
841 info
->ctrl_sync
= false;
847 static int m5mols_log_status(struct v4l2_subdev
*sd
)
849 struct m5mols_info
*info
= to_m5mols(sd
);
851 v4l2_ctrl_handler_log_status(&info
->handle
, sd
->name
);
856 static const struct v4l2_subdev_core_ops m5mols_core_ops
= {
857 .s_power
= m5mols_s_power
,
858 .g_ctrl
= v4l2_subdev_g_ctrl
,
859 .s_ctrl
= v4l2_subdev_s_ctrl
,
860 .queryctrl
= v4l2_subdev_queryctrl
,
861 .querymenu
= v4l2_subdev_querymenu
,
862 .g_ext_ctrls
= v4l2_subdev_g_ext_ctrls
,
863 .try_ext_ctrls
= v4l2_subdev_try_ext_ctrls
,
864 .s_ext_ctrls
= v4l2_subdev_s_ext_ctrls
,
865 .log_status
= m5mols_log_status
,
868 static const struct v4l2_subdev_ops m5mols_ops
= {
869 .core
= &m5mols_core_ops
,
870 .pad
= &m5mols_pad_ops
,
871 .video
= &m5mols_video_ops
,
874 static void m5mols_irq_work(struct work_struct
*work
)
876 struct m5mols_info
*info
=
877 container_of(work
, struct m5mols_info
, work_irq
);
878 struct v4l2_subdev
*sd
= &info
->sd
;
882 if (!is_powered(info
) ||
883 m5mols_read_u8(sd
, SYSTEM_INT_FACTOR
, &info
->interrupt
))
886 switch (info
->interrupt
& REG_INT_MASK
) {
888 if (!is_available_af(info
))
890 ret
= m5mols_read_u8(sd
, AF_STATUS
, ®
);
891 v4l2_dbg(2, m5mols_debug
, sd
, "AF %s\n",
892 reg
== REG_AF_FAIL
? "Failed" :
893 reg
== REG_AF_SUCCESS
? "Success" :
894 reg
== REG_AF_IDLE
? "Idle" : "Busy");
896 case REG_INT_CAPTURE
:
897 if (!test_and_set_bit(ST_CAPT_IRQ
, &info
->flags
))
898 wake_up_interruptible(&info
->irq_waitq
);
900 v4l2_dbg(2, m5mols_debug
, sd
, "CAPTURE\n");
903 v4l2_dbg(2, m5mols_debug
, sd
, "Undefined: %02x\n", reg
);
908 static irqreturn_t
m5mols_irq_handler(int irq
, void *data
)
910 struct v4l2_subdev
*sd
= data
;
911 struct m5mols_info
*info
= to_m5mols(sd
);
913 schedule_work(&info
->work_irq
);
918 static int __devinit
m5mols_probe(struct i2c_client
*client
,
919 const struct i2c_device_id
*id
)
921 const struct m5mols_platform_data
*pdata
= client
->dev
.platform_data
;
922 struct m5mols_info
*info
;
923 struct v4l2_subdev
*sd
;
927 dev_err(&client
->dev
, "No platform data\n");
931 if (!gpio_is_valid(pdata
->gpio_reset
)) {
932 dev_err(&client
->dev
, "No valid RESET GPIO specified\n");
937 dev_err(&client
->dev
, "Interrupt not assigned\n");
941 info
= kzalloc(sizeof(struct m5mols_info
), GFP_KERNEL
);
946 info
->set_power
= pdata
->set_power
;
948 ret
= gpio_request(pdata
->gpio_reset
, "M5MOLS_NRST");
950 dev_err(&client
->dev
, "Failed to request gpio: %d\n", ret
);
953 gpio_direction_output(pdata
->gpio_reset
, pdata
->reset_polarity
);
955 ret
= regulator_bulk_get(&client
->dev
, ARRAY_SIZE(supplies
), supplies
);
957 dev_err(&client
->dev
, "Failed to get regulators: %d\n", ret
);
962 strlcpy(sd
->name
, MODULE_NAME
, sizeof(sd
->name
));
963 v4l2_i2c_subdev_init(sd
, client
, &m5mols_ops
);
965 info
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
966 ret
= media_entity_init(&sd
->entity
, 1, &info
->pad
, 0);
969 sd
->entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV_SENSOR
;
971 init_waitqueue_head(&info
->irq_waitq
);
972 INIT_WORK(&info
->work_irq
, m5mols_irq_work
);
973 ret
= request_irq(client
->irq
, m5mols_irq_handler
,
974 IRQF_TRIGGER_RISING
, MODULE_NAME
, sd
);
976 dev_err(&client
->dev
, "Interrupt request failed: %d\n", ret
);
979 info
->res_type
= M5MOLS_RESTYPE_MONITOR
;
982 media_entity_cleanup(&sd
->entity
);
984 regulator_bulk_free(ARRAY_SIZE(supplies
), supplies
);
986 gpio_free(pdata
->gpio_reset
);
992 static int __devexit
m5mols_remove(struct i2c_client
*client
)
994 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
995 struct m5mols_info
*info
= to_m5mols(sd
);
997 v4l2_device_unregister_subdev(sd
);
998 free_irq(client
->irq
, sd
);
1000 regulator_bulk_free(ARRAY_SIZE(supplies
), supplies
);
1001 gpio_free(info
->pdata
->gpio_reset
);
1002 media_entity_cleanup(&sd
->entity
);
1007 static const struct i2c_device_id m5mols_id
[] = {
1011 MODULE_DEVICE_TABLE(i2c
, m5mols_id
);
1013 static struct i2c_driver m5mols_i2c_driver
= {
1015 .name
= MODULE_NAME
,
1017 .probe
= m5mols_probe
,
1018 .remove
= __devexit_p(m5mols_remove
),
1019 .id_table
= m5mols_id
,
1022 static int __init
m5mols_mod_init(void)
1024 return i2c_add_driver(&m5mols_i2c_driver
);
1027 static void __exit
m5mols_mod_exit(void)
1029 i2c_del_driver(&m5mols_i2c_driver
);
1032 module_init(m5mols_mod_init
);
1033 module_exit(m5mols_mod_exit
);
1035 MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
1036 MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
1037 MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
1038 MODULE_LICENSE("GPL");