Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1 | /* |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 2 | * Driver for MT9M111/MT9M112 CMOS Image Sensor from Micron |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/videodev2.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/log2.h> |
| 14 | #include <linux/gpio.h> |
| 15 | #include <linux/delay.h> |
| 16 | |
| 17 | #include <media/v4l2-common.h> |
| 18 | #include <media/v4l2-chip-ident.h> |
| 19 | #include <media/soc_camera.h> |
| 20 | |
| 21 | /* |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 22 | * mt9m111 and mt9m112 i2c address is 0x5d or 0x48 (depending on SAddr pin) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 23 | * The platform has to define i2c_board_info and call i2c_register_board_info() |
| 24 | */ |
| 25 | |
| 26 | /* mt9m111: Sensor register addresses */ |
| 27 | #define MT9M111_CHIP_VERSION 0x000 |
| 28 | #define MT9M111_ROW_START 0x001 |
| 29 | #define MT9M111_COLUMN_START 0x002 |
| 30 | #define MT9M111_WINDOW_HEIGHT 0x003 |
| 31 | #define MT9M111_WINDOW_WIDTH 0x004 |
| 32 | #define MT9M111_HORIZONTAL_BLANKING_B 0x005 |
| 33 | #define MT9M111_VERTICAL_BLANKING_B 0x006 |
| 34 | #define MT9M111_HORIZONTAL_BLANKING_A 0x007 |
| 35 | #define MT9M111_VERTICAL_BLANKING_A 0x008 |
| 36 | #define MT9M111_SHUTTER_WIDTH 0x009 |
| 37 | #define MT9M111_ROW_SPEED 0x00a |
| 38 | #define MT9M111_EXTRA_DELAY 0x00b |
| 39 | #define MT9M111_SHUTTER_DELAY 0x00c |
| 40 | #define MT9M111_RESET 0x00d |
| 41 | #define MT9M111_READ_MODE_B 0x020 |
| 42 | #define MT9M111_READ_MODE_A 0x021 |
| 43 | #define MT9M111_FLASH_CONTROL 0x023 |
| 44 | #define MT9M111_GREEN1_GAIN 0x02b |
| 45 | #define MT9M111_BLUE_GAIN 0x02c |
| 46 | #define MT9M111_RED_GAIN 0x02d |
| 47 | #define MT9M111_GREEN2_GAIN 0x02e |
| 48 | #define MT9M111_GLOBAL_GAIN 0x02f |
| 49 | #define MT9M111_CONTEXT_CONTROL 0x0c8 |
| 50 | #define MT9M111_PAGE_MAP 0x0f0 |
| 51 | #define MT9M111_BYTE_WISE_ADDR 0x0f1 |
| 52 | |
| 53 | #define MT9M111_RESET_SYNC_CHANGES (1 << 15) |
| 54 | #define MT9M111_RESET_RESTART_BAD_FRAME (1 << 9) |
| 55 | #define MT9M111_RESET_SHOW_BAD_FRAMES (1 << 8) |
| 56 | #define MT9M111_RESET_RESET_SOC (1 << 5) |
| 57 | #define MT9M111_RESET_OUTPUT_DISABLE (1 << 4) |
| 58 | #define MT9M111_RESET_CHIP_ENABLE (1 << 3) |
| 59 | #define MT9M111_RESET_ANALOG_STANDBY (1 << 2) |
| 60 | #define MT9M111_RESET_RESTART_FRAME (1 << 1) |
| 61 | #define MT9M111_RESET_RESET_MODE (1 << 0) |
| 62 | |
| 63 | #define MT9M111_RMB_MIRROR_COLS (1 << 1) |
| 64 | #define MT9M111_RMB_MIRROR_ROWS (1 << 0) |
| 65 | #define MT9M111_CTXT_CTRL_RESTART (1 << 15) |
| 66 | #define MT9M111_CTXT_CTRL_DEFECTCOR_B (1 << 12) |
| 67 | #define MT9M111_CTXT_CTRL_RESIZE_B (1 << 10) |
| 68 | #define MT9M111_CTXT_CTRL_CTRL2_B (1 << 9) |
| 69 | #define MT9M111_CTXT_CTRL_GAMMA_B (1 << 8) |
| 70 | #define MT9M111_CTXT_CTRL_XENON_EN (1 << 7) |
| 71 | #define MT9M111_CTXT_CTRL_READ_MODE_B (1 << 3) |
| 72 | #define MT9M111_CTXT_CTRL_LED_FLASH_EN (1 << 2) |
| 73 | #define MT9M111_CTXT_CTRL_VBLANK_SEL_B (1 << 1) |
| 74 | #define MT9M111_CTXT_CTRL_HBLANK_SEL_B (1 << 0) |
| 75 | /* |
| 76 | * mt9m111: Colorpipe register addresses (0x100..0x1ff) |
| 77 | */ |
| 78 | #define MT9M111_OPER_MODE_CTRL 0x106 |
| 79 | #define MT9M111_OUTPUT_FORMAT_CTRL 0x108 |
| 80 | #define MT9M111_REDUCER_XZOOM_B 0x1a0 |
| 81 | #define MT9M111_REDUCER_XSIZE_B 0x1a1 |
| 82 | #define MT9M111_REDUCER_YZOOM_B 0x1a3 |
| 83 | #define MT9M111_REDUCER_YSIZE_B 0x1a4 |
| 84 | #define MT9M111_REDUCER_XZOOM_A 0x1a6 |
| 85 | #define MT9M111_REDUCER_XSIZE_A 0x1a7 |
| 86 | #define MT9M111_REDUCER_YZOOM_A 0x1a9 |
| 87 | #define MT9M111_REDUCER_YSIZE_A 0x1aa |
| 88 | |
| 89 | #define MT9M111_OUTPUT_FORMAT_CTRL2_A 0x13a |
| 90 | #define MT9M111_OUTPUT_FORMAT_CTRL2_B 0x19b |
| 91 | |
| 92 | #define MT9M111_OPMODE_AUTOEXPO_EN (1 << 14) |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 93 | #define MT9M111_OPMODE_AUTOWHITEBAL_EN (1 << 1) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 94 | |
| 95 | #define MT9M111_OUTFMT_PROCESSED_BAYER (1 << 14) |
| 96 | #define MT9M111_OUTFMT_BYPASS_IFP (1 << 10) |
| 97 | #define MT9M111_OUTFMT_INV_PIX_CLOCK (1 << 9) |
| 98 | #define MT9M111_OUTFMT_RGB (1 << 8) |
| 99 | #define MT9M111_OUTFMT_RGB565 (0x0 << 6) |
| 100 | #define MT9M111_OUTFMT_RGB555 (0x1 << 6) |
| 101 | #define MT9M111_OUTFMT_RGB444x (0x2 << 6) |
| 102 | #define MT9M111_OUTFMT_RGBx444 (0x3 << 6) |
| 103 | #define MT9M111_OUTFMT_TST_RAMP_OFF (0x0 << 4) |
| 104 | #define MT9M111_OUTFMT_TST_RAMP_COL (0x1 << 4) |
| 105 | #define MT9M111_OUTFMT_TST_RAMP_ROW (0x2 << 4) |
| 106 | #define MT9M111_OUTFMT_TST_RAMP_FRAME (0x3 << 4) |
| 107 | #define MT9M111_OUTFMT_SHIFT_3_UP (1 << 3) |
| 108 | #define MT9M111_OUTFMT_AVG_CHROMA (1 << 2) |
| 109 | #define MT9M111_OUTFMT_SWAP_YCbCr_C_Y (1 << 1) |
| 110 | #define MT9M111_OUTFMT_SWAP_RGB_EVEN (1 << 1) |
| 111 | #define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr (1 << 0) |
| 112 | /* |
| 113 | * mt9m111: Camera control register addresses (0x200..0x2ff not implemented) |
| 114 | */ |
| 115 | |
| 116 | #define reg_read(reg) mt9m111_reg_read(icd, MT9M111_##reg) |
| 117 | #define reg_write(reg, val) mt9m111_reg_write(icd, MT9M111_##reg, (val)) |
| 118 | #define reg_set(reg, val) mt9m111_reg_set(icd, MT9M111_##reg, (val)) |
| 119 | #define reg_clear(reg, val) mt9m111_reg_clear(icd, MT9M111_##reg, (val)) |
| 120 | |
| 121 | #define MT9M111_MIN_DARK_ROWS 8 |
| 122 | #define MT9M111_MIN_DARK_COLS 24 |
| 123 | #define MT9M111_MAX_HEIGHT 1024 |
| 124 | #define MT9M111_MAX_WIDTH 1280 |
| 125 | |
| 126 | #define COL_FMT(_name, _depth, _fourcc, _colorspace) \ |
| 127 | { .name = _name, .depth = _depth, .fourcc = _fourcc, \ |
| 128 | .colorspace = _colorspace } |
| 129 | #define RGB_FMT(_name, _depth, _fourcc) \ |
| 130 | COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_SRGB) |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 131 | #define JPG_FMT(_name, _depth, _fourcc) \ |
| 132 | COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 133 | |
| 134 | static const struct soc_camera_data_format mt9m111_colour_formats[] = { |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 135 | JPG_FMT("CbYCrY 16 bit", 16, V4L2_PIX_FMT_UYVY), |
| 136 | JPG_FMT("CrYCbY 16 bit", 16, V4L2_PIX_FMT_VYUY), |
| 137 | JPG_FMT("YCbYCr 16 bit", 16, V4L2_PIX_FMT_YUYV), |
| 138 | JPG_FMT("YCrYCb 16 bit", 16, V4L2_PIX_FMT_YVYU), |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 139 | RGB_FMT("RGB 565", 16, V4L2_PIX_FMT_RGB565), |
| 140 | RGB_FMT("RGB 555", 16, V4L2_PIX_FMT_RGB555), |
| 141 | RGB_FMT("Bayer (sRGB) 10 bit", 10, V4L2_PIX_FMT_SBGGR16), |
| 142 | RGB_FMT("Bayer (sRGB) 8 bit", 8, V4L2_PIX_FMT_SBGGR8), |
| 143 | }; |
| 144 | |
| 145 | enum mt9m111_context { |
| 146 | HIGHPOWER = 0, |
| 147 | LOWPOWER, |
| 148 | }; |
| 149 | |
| 150 | struct mt9m111 { |
| 151 | struct i2c_client *client; |
| 152 | struct soc_camera_device icd; |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 153 | int model; /* V4L2_IDENT_MT9M11x* codes from v4l2-chip-ident.h */ |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 154 | enum mt9m111_context context; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 155 | struct v4l2_rect rect; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 156 | u32 pixfmt; |
| 157 | unsigned char autoexposure; |
| 158 | unsigned char datawidth; |
| 159 | unsigned int powered:1; |
| 160 | unsigned int hflip:1; |
| 161 | unsigned int vflip:1; |
| 162 | unsigned int swap_rgb_even_odd:1; |
| 163 | unsigned int swap_rgb_red_blue:1; |
| 164 | unsigned int swap_yuv_y_chromas:1; |
| 165 | unsigned int swap_yuv_cb_cr:1; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 166 | unsigned int autowhitebalance:1; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | static int reg_page_map_set(struct i2c_client *client, const u16 reg) |
| 170 | { |
| 171 | int ret; |
| 172 | u16 page; |
| 173 | static int lastpage = -1; /* PageMap cache value */ |
| 174 | |
| 175 | page = (reg >> 8); |
| 176 | if (page == lastpage) |
| 177 | return 0; |
| 178 | if (page > 2) |
| 179 | return -EINVAL; |
| 180 | |
| 181 | ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page)); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 182 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 183 | lastpage = page; |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | static int mt9m111_reg_read(struct soc_camera_device *icd, const u16 reg) |
| 188 | { |
| 189 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 190 | struct i2c_client *client = mt9m111->client; |
| 191 | int ret; |
| 192 | |
| 193 | ret = reg_page_map_set(client, reg); |
| 194 | if (!ret) |
| 195 | ret = swab16(i2c_smbus_read_word_data(client, (reg & 0xff))); |
| 196 | |
| 197 | dev_dbg(&icd->dev, "read reg.%03x -> %04x\n", reg, ret); |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static int mt9m111_reg_write(struct soc_camera_device *icd, const u16 reg, |
| 202 | const u16 data) |
| 203 | { |
| 204 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 205 | struct i2c_client *client = mt9m111->client; |
| 206 | int ret; |
| 207 | |
| 208 | ret = reg_page_map_set(client, reg); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 209 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 210 | ret = i2c_smbus_write_word_data(mt9m111->client, (reg & 0xff), |
| 211 | swab16(data)); |
| 212 | dev_dbg(&icd->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret); |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static int mt9m111_reg_set(struct soc_camera_device *icd, const u16 reg, |
| 217 | const u16 data) |
| 218 | { |
| 219 | int ret; |
| 220 | |
| 221 | ret = mt9m111_reg_read(icd, reg); |
| 222 | if (ret >= 0) |
| 223 | ret = mt9m111_reg_write(icd, reg, ret | data); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static int mt9m111_reg_clear(struct soc_camera_device *icd, const u16 reg, |
| 228 | const u16 data) |
| 229 | { |
| 230 | int ret; |
| 231 | |
| 232 | ret = mt9m111_reg_read(icd, reg); |
| 233 | return mt9m111_reg_write(icd, reg, ret & ~data); |
| 234 | } |
| 235 | |
| 236 | static int mt9m111_set_context(struct soc_camera_device *icd, |
| 237 | enum mt9m111_context ctxt) |
| 238 | { |
| 239 | int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B |
| 240 | | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B |
| 241 | | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B |
| 242 | | MT9M111_CTXT_CTRL_VBLANK_SEL_B |
| 243 | | MT9M111_CTXT_CTRL_HBLANK_SEL_B; |
| 244 | int valA = MT9M111_CTXT_CTRL_RESTART; |
| 245 | |
| 246 | if (ctxt == HIGHPOWER) |
| 247 | return reg_write(CONTEXT_CONTROL, valB); |
| 248 | else |
| 249 | return reg_write(CONTEXT_CONTROL, valA); |
| 250 | } |
| 251 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 252 | static int mt9m111_setup_rect(struct soc_camera_device *icd, |
| 253 | struct v4l2_rect *rect) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 254 | { |
| 255 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 256 | int ret, is_raw_format; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 257 | int width = rect->width; |
| 258 | int height = rect->height; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 259 | |
| 260 | if ((mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8) |
| 261 | || (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16)) |
| 262 | is_raw_format = 1; |
| 263 | else |
| 264 | is_raw_format = 0; |
| 265 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 266 | ret = reg_write(COLUMN_START, rect->left); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 267 | if (!ret) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 268 | ret = reg_write(ROW_START, rect->top); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 269 | |
| 270 | if (is_raw_format) { |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 271 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 272 | ret = reg_write(WINDOW_WIDTH, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 273 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 274 | ret = reg_write(WINDOW_HEIGHT, height); |
| 275 | } else { |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 276 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 277 | ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 278 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 279 | ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 280 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 281 | ret = reg_write(REDUCER_XSIZE_B, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 282 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 283 | ret = reg_write(REDUCER_YSIZE_B, height); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 284 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 285 | ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 286 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 287 | ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 288 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 289 | ret = reg_write(REDUCER_XSIZE_A, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 290 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 291 | ret = reg_write(REDUCER_YSIZE_A, height); |
| 292 | } |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int mt9m111_setup_pixfmt(struct soc_camera_device *icd, u16 outfmt) |
| 298 | { |
| 299 | int ret; |
| 300 | |
| 301 | ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 302 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 303 | ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static int mt9m111_setfmt_bayer8(struct soc_camera_device *icd) |
| 308 | { |
| 309 | return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_PROCESSED_BAYER); |
| 310 | } |
| 311 | |
| 312 | static int mt9m111_setfmt_bayer10(struct soc_camera_device *icd) |
| 313 | { |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 314 | return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_BYPASS_IFP); |
| 315 | } |
| 316 | |
| 317 | static int mt9m111_setfmt_rgb565(struct soc_camera_device *icd) |
| 318 | { |
| 319 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 320 | int val = 0; |
| 321 | |
| 322 | if (mt9m111->swap_rgb_red_blue) |
| 323 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 324 | if (mt9m111->swap_rgb_even_odd) |
| 325 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 326 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565; |
| 327 | |
| 328 | return mt9m111_setup_pixfmt(icd, val); |
| 329 | } |
| 330 | |
| 331 | static int mt9m111_setfmt_rgb555(struct soc_camera_device *icd) |
| 332 | { |
| 333 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 334 | int val = 0; |
| 335 | |
| 336 | if (mt9m111->swap_rgb_red_blue) |
| 337 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 338 | if (mt9m111->swap_rgb_even_odd) |
| 339 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 340 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555; |
| 341 | |
| 342 | return mt9m111_setup_pixfmt(icd, val); |
| 343 | } |
| 344 | |
| 345 | static int mt9m111_setfmt_yuv(struct soc_camera_device *icd) |
| 346 | { |
| 347 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 348 | int val = 0; |
| 349 | |
| 350 | if (mt9m111->swap_yuv_cb_cr) |
| 351 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 352 | if (mt9m111->swap_yuv_y_chromas) |
| 353 | val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y; |
| 354 | |
| 355 | return mt9m111_setup_pixfmt(icd, val); |
| 356 | } |
| 357 | |
| 358 | static int mt9m111_enable(struct soc_camera_device *icd) |
| 359 | { |
| 360 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 361 | struct soc_camera_link *icl = mt9m111->client->dev.platform_data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 362 | int ret; |
| 363 | |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 364 | if (icl->power) { |
| 365 | ret = icl->power(&mt9m111->client->dev, 1); |
| 366 | if (ret < 0) { |
| 367 | dev_err(icd->vdev->parent, |
| 368 | "Platform failed to power-on the camera.\n"); |
| 369 | return ret; |
| 370 | } |
| 371 | } |
| 372 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 373 | ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 374 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 375 | mt9m111->powered = 1; |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | static int mt9m111_disable(struct soc_camera_device *icd) |
| 380 | { |
| 381 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 382 | struct soc_camera_link *icl = mt9m111->client->dev.platform_data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 383 | int ret; |
| 384 | |
| 385 | ret = reg_clear(RESET, MT9M111_RESET_CHIP_ENABLE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 386 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 387 | mt9m111->powered = 0; |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 388 | |
| 389 | if (icl->power) |
| 390 | icl->power(&mt9m111->client->dev, 0); |
| 391 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | static int mt9m111_reset(struct soc_camera_device *icd) |
| 396 | { |
Antonio Ospite | afb1368 | 2009-02-23 12:13:24 -0300 | [diff] [blame] | 397 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 398 | struct soc_camera_link *icl = mt9m111->client->dev.platform_data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 399 | int ret; |
| 400 | |
| 401 | ret = reg_set(RESET, MT9M111_RESET_RESET_MODE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 402 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 403 | ret = reg_set(RESET, MT9M111_RESET_RESET_SOC); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 404 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 405 | ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE |
| 406 | | MT9M111_RESET_RESET_SOC); |
Antonio Ospite | afb1368 | 2009-02-23 12:13:24 -0300 | [diff] [blame] | 407 | |
| 408 | if (icl->reset) |
| 409 | icl->reset(&mt9m111->client->dev); |
| 410 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | static int mt9m111_start_capture(struct soc_camera_device *icd) |
| 415 | { |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static int mt9m111_stop_capture(struct soc_camera_device *icd) |
| 420 | { |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd) |
| 425 | { |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 426 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 427 | struct soc_camera_link *icl = mt9m111->client->dev.platform_data; |
| 428 | unsigned long flags = SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 429 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
Guennadi Liakhovetski | 2d9329f | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 430 | SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8; |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 431 | |
| 432 | return soc_camera_apply_sensor_flags(icl, flags); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f) |
| 436 | { |
| 437 | return 0; |
| 438 | } |
| 439 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 440 | static int mt9m111_set_crop(struct soc_camera_device *icd, |
| 441 | struct v4l2_rect *rect) |
| 442 | { |
| 443 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 444 | int ret; |
| 445 | |
| 446 | dev_dbg(&icd->dev, "%s left=%d, top=%d, width=%d, height=%d\n", |
| 447 | __func__, rect->left, rect->top, rect->width, |
| 448 | rect->height); |
| 449 | |
| 450 | ret = mt9m111_setup_rect(icd, rect); |
| 451 | if (!ret) |
| 452 | mt9m111->rect = *rect; |
| 453 | return ret; |
| 454 | } |
| 455 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 456 | static int mt9m111_set_pixfmt(struct soc_camera_device *icd, u32 pixfmt) |
| 457 | { |
| 458 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 459 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 460 | |
| 461 | switch (pixfmt) { |
| 462 | case V4L2_PIX_FMT_SBGGR8: |
| 463 | ret = mt9m111_setfmt_bayer8(icd); |
| 464 | break; |
| 465 | case V4L2_PIX_FMT_SBGGR16: |
| 466 | ret = mt9m111_setfmt_bayer10(icd); |
| 467 | break; |
| 468 | case V4L2_PIX_FMT_RGB555: |
| 469 | ret = mt9m111_setfmt_rgb555(icd); |
| 470 | break; |
| 471 | case V4L2_PIX_FMT_RGB565: |
| 472 | ret = mt9m111_setfmt_rgb565(icd); |
| 473 | break; |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 474 | case V4L2_PIX_FMT_UYVY: |
| 475 | mt9m111->swap_yuv_y_chromas = 0; |
| 476 | mt9m111->swap_yuv_cb_cr = 0; |
| 477 | ret = mt9m111_setfmt_yuv(icd); |
| 478 | break; |
| 479 | case V4L2_PIX_FMT_VYUY: |
| 480 | mt9m111->swap_yuv_y_chromas = 0; |
| 481 | mt9m111->swap_yuv_cb_cr = 1; |
| 482 | ret = mt9m111_setfmt_yuv(icd); |
| 483 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 484 | case V4L2_PIX_FMT_YUYV: |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 485 | mt9m111->swap_yuv_y_chromas = 1; |
| 486 | mt9m111->swap_yuv_cb_cr = 0; |
| 487 | ret = mt9m111_setfmt_yuv(icd); |
| 488 | break; |
| 489 | case V4L2_PIX_FMT_YVYU: |
| 490 | mt9m111->swap_yuv_y_chromas = 1; |
| 491 | mt9m111->swap_yuv_cb_cr = 1; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 492 | ret = mt9m111_setfmt_yuv(icd); |
| 493 | break; |
| 494 | default: |
| 495 | dev_err(&icd->dev, "Pixel format not handled : %x\n", pixfmt); |
| 496 | ret = -EINVAL; |
| 497 | } |
| 498 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 499 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 500 | mt9m111->pixfmt = pixfmt; |
| 501 | |
| 502 | return ret; |
| 503 | } |
| 504 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 505 | static int mt9m111_set_fmt(struct soc_camera_device *icd, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 506 | struct v4l2_format *f) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 507 | { |
| 508 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 509 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 510 | struct v4l2_rect rect = { |
| 511 | .left = mt9m111->rect.left, |
| 512 | .top = mt9m111->rect.top, |
| 513 | .width = pix->width, |
| 514 | .height = pix->height, |
| 515 | }; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 516 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 517 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 518 | dev_dbg(&icd->dev, "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n", |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 519 | __func__, pix->pixelformat, rect.left, rect.top, rect.width, |
| 520 | rect.height); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 521 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 522 | ret = mt9m111_setup_rect(icd, &rect); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 523 | if (!ret) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 524 | ret = mt9m111_set_pixfmt(icd, pix->pixelformat); |
| 525 | if (!ret) |
| 526 | mt9m111->rect = rect; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 527 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 528 | } |
| 529 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 530 | static int mt9m111_try_fmt(struct soc_camera_device *icd, |
| 531 | struct v4l2_format *f) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 532 | { |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 533 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 534 | |
| 535 | if (pix->height > MT9M111_MAX_HEIGHT) |
| 536 | pix->height = MT9M111_MAX_HEIGHT; |
| 537 | if (pix->width > MT9M111_MAX_WIDTH) |
| 538 | pix->width = MT9M111_MAX_WIDTH; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int mt9m111_get_chip_id(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 544 | struct v4l2_dbg_chip_ident *id) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 545 | { |
| 546 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 547 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 548 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 549 | return -EINVAL; |
| 550 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 551 | if (id->match.addr != mt9m111->client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 552 | return -ENODEV; |
| 553 | |
| 554 | id->ident = mt9m111->model; |
| 555 | id->revision = 0; |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 561 | static int mt9m111_get_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 562 | struct v4l2_dbg_register *reg) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 563 | { |
| 564 | int val; |
| 565 | |
| 566 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 567 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 568 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 569 | return -EINVAL; |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 570 | if (reg->match.addr != mt9m111->client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 571 | return -ENODEV; |
| 572 | |
| 573 | val = mt9m111_reg_read(icd, reg->reg); |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 574 | reg->size = 2; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 575 | reg->val = (u64)val; |
| 576 | |
| 577 | if (reg->val > 0xffff) |
| 578 | return -EIO; |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int mt9m111_set_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 584 | struct v4l2_dbg_register *reg) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 585 | { |
| 586 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 587 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 588 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 589 | return -EINVAL; |
| 590 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 591 | if (reg->match.addr != mt9m111->client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 592 | return -ENODEV; |
| 593 | |
| 594 | if (mt9m111_reg_write(icd, reg->reg, reg->val) < 0) |
| 595 | return -EIO; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | #endif |
| 600 | |
| 601 | static const struct v4l2_queryctrl mt9m111_controls[] = { |
| 602 | { |
| 603 | .id = V4L2_CID_VFLIP, |
| 604 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 605 | .name = "Flip Verticaly", |
| 606 | .minimum = 0, |
| 607 | .maximum = 1, |
| 608 | .step = 1, |
| 609 | .default_value = 0, |
| 610 | }, { |
| 611 | .id = V4L2_CID_HFLIP, |
| 612 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 613 | .name = "Flip Horizontaly", |
| 614 | .minimum = 0, |
| 615 | .maximum = 1, |
| 616 | .step = 1, |
| 617 | .default_value = 0, |
| 618 | }, { /* gain = 1/32*val (=>gain=1 if val==32) */ |
| 619 | .id = V4L2_CID_GAIN, |
| 620 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 621 | .name = "Gain", |
| 622 | .minimum = 0, |
| 623 | .maximum = 63 * 2 * 2, |
| 624 | .step = 1, |
| 625 | .default_value = 32, |
| 626 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 627 | }, { |
| 628 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 629 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 630 | .name = "Auto Exposure", |
| 631 | .minimum = 0, |
| 632 | .maximum = 1, |
| 633 | .step = 1, |
| 634 | .default_value = 1, |
| 635 | } |
| 636 | }; |
| 637 | |
| 638 | static int mt9m111_video_probe(struct soc_camera_device *); |
| 639 | static void mt9m111_video_remove(struct soc_camera_device *); |
| 640 | static int mt9m111_get_control(struct soc_camera_device *, |
| 641 | struct v4l2_control *); |
| 642 | static int mt9m111_set_control(struct soc_camera_device *, |
| 643 | struct v4l2_control *); |
| 644 | static int mt9m111_resume(struct soc_camera_device *icd); |
| 645 | static int mt9m111_init(struct soc_camera_device *icd); |
| 646 | static int mt9m111_release(struct soc_camera_device *icd); |
| 647 | |
| 648 | static struct soc_camera_ops mt9m111_ops = { |
| 649 | .owner = THIS_MODULE, |
| 650 | .probe = mt9m111_video_probe, |
| 651 | .remove = mt9m111_video_remove, |
| 652 | .init = mt9m111_init, |
| 653 | .resume = mt9m111_resume, |
| 654 | .release = mt9m111_release, |
| 655 | .start_capture = mt9m111_start_capture, |
| 656 | .stop_capture = mt9m111_stop_capture, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 657 | .set_crop = mt9m111_set_crop, |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 658 | .set_fmt = mt9m111_set_fmt, |
| 659 | .try_fmt = mt9m111_try_fmt, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 660 | .query_bus_param = mt9m111_query_bus_param, |
| 661 | .set_bus_param = mt9m111_set_bus_param, |
| 662 | .controls = mt9m111_controls, |
| 663 | .num_controls = ARRAY_SIZE(mt9m111_controls), |
| 664 | .get_control = mt9m111_get_control, |
| 665 | .set_control = mt9m111_set_control, |
| 666 | .get_chip_id = mt9m111_get_chip_id, |
| 667 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 668 | .get_register = mt9m111_get_register, |
| 669 | .set_register = mt9m111_set_register, |
| 670 | #endif |
| 671 | }; |
| 672 | |
| 673 | static int mt9m111_set_flip(struct soc_camera_device *icd, int flip, int mask) |
| 674 | { |
| 675 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 676 | int ret; |
| 677 | |
| 678 | if (mt9m111->context == HIGHPOWER) { |
| 679 | if (flip) |
| 680 | ret = reg_set(READ_MODE_B, mask); |
| 681 | else |
| 682 | ret = reg_clear(READ_MODE_B, mask); |
| 683 | } else { |
| 684 | if (flip) |
| 685 | ret = reg_set(READ_MODE_A, mask); |
| 686 | else |
| 687 | ret = reg_clear(READ_MODE_A, mask); |
| 688 | } |
| 689 | |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static int mt9m111_get_global_gain(struct soc_camera_device *icd) |
| 694 | { |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 695 | int data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 696 | |
| 697 | data = reg_read(GLOBAL_GAIN); |
| 698 | if (data >= 0) |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 699 | return (data & 0x2f) * (1 << ((data >> 10) & 1)) * |
| 700 | (1 << ((data >> 9) & 1)); |
| 701 | return data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 702 | } |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 703 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 704 | static int mt9m111_set_global_gain(struct soc_camera_device *icd, int gain) |
| 705 | { |
| 706 | u16 val; |
| 707 | |
| 708 | if (gain > 63 * 2 * 2) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | icd->gain = gain; |
| 712 | if ((gain >= 64 * 2) && (gain < 63 * 2 * 2)) |
| 713 | val = (1 << 10) | (1 << 9) | (gain / 4); |
| 714 | else if ((gain >= 64) && (gain < 64 * 2)) |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 715 | val = (1 << 9) | (gain / 2); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 716 | else |
| 717 | val = gain; |
| 718 | |
| 719 | return reg_write(GLOBAL_GAIN, val); |
| 720 | } |
| 721 | |
| 722 | static int mt9m111_set_autoexposure(struct soc_camera_device *icd, int on) |
| 723 | { |
| 724 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 725 | int ret; |
| 726 | |
| 727 | if (on) |
| 728 | ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 729 | else |
| 730 | ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 731 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 732 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 733 | mt9m111->autoexposure = on; |
| 734 | |
| 735 | return ret; |
| 736 | } |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 737 | |
| 738 | static int mt9m111_set_autowhitebalance(struct soc_camera_device *icd, int on) |
| 739 | { |
| 740 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 741 | int ret; |
| 742 | |
| 743 | if (on) |
| 744 | ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN); |
| 745 | else |
| 746 | ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN); |
| 747 | |
| 748 | if (!ret) |
| 749 | mt9m111->autowhitebalance = on; |
| 750 | |
| 751 | return ret; |
| 752 | } |
| 753 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 754 | static int mt9m111_get_control(struct soc_camera_device *icd, |
| 755 | struct v4l2_control *ctrl) |
| 756 | { |
| 757 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 758 | int data; |
| 759 | |
| 760 | switch (ctrl->id) { |
| 761 | case V4L2_CID_VFLIP: |
| 762 | if (mt9m111->context == HIGHPOWER) |
| 763 | data = reg_read(READ_MODE_B); |
| 764 | else |
| 765 | data = reg_read(READ_MODE_A); |
| 766 | |
| 767 | if (data < 0) |
| 768 | return -EIO; |
| 769 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS); |
| 770 | break; |
| 771 | case V4L2_CID_HFLIP: |
| 772 | if (mt9m111->context == HIGHPOWER) |
| 773 | data = reg_read(READ_MODE_B); |
| 774 | else |
| 775 | data = reg_read(READ_MODE_A); |
| 776 | |
| 777 | if (data < 0) |
| 778 | return -EIO; |
| 779 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS); |
| 780 | break; |
| 781 | case V4L2_CID_GAIN: |
| 782 | data = mt9m111_get_global_gain(icd); |
| 783 | if (data < 0) |
| 784 | return data; |
| 785 | ctrl->value = data; |
| 786 | break; |
| 787 | case V4L2_CID_EXPOSURE_AUTO: |
| 788 | ctrl->value = mt9m111->autoexposure; |
| 789 | break; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 790 | case V4L2_CID_AUTO_WHITE_BALANCE: |
| 791 | ctrl->value = mt9m111->autowhitebalance; |
| 792 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 793 | } |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static int mt9m111_set_control(struct soc_camera_device *icd, |
| 798 | struct v4l2_control *ctrl) |
| 799 | { |
| 800 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 801 | const struct v4l2_queryctrl *qctrl; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 802 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 803 | |
| 804 | qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id); |
| 805 | |
| 806 | if (!qctrl) |
| 807 | return -EINVAL; |
| 808 | |
| 809 | switch (ctrl->id) { |
| 810 | case V4L2_CID_VFLIP: |
| 811 | mt9m111->vflip = ctrl->value; |
| 812 | ret = mt9m111_set_flip(icd, ctrl->value, |
| 813 | MT9M111_RMB_MIRROR_ROWS); |
| 814 | break; |
| 815 | case V4L2_CID_HFLIP: |
| 816 | mt9m111->hflip = ctrl->value; |
| 817 | ret = mt9m111_set_flip(icd, ctrl->value, |
| 818 | MT9M111_RMB_MIRROR_COLS); |
| 819 | break; |
| 820 | case V4L2_CID_GAIN: |
| 821 | ret = mt9m111_set_global_gain(icd, ctrl->value); |
| 822 | break; |
| 823 | case V4L2_CID_EXPOSURE_AUTO: |
| 824 | ret = mt9m111_set_autoexposure(icd, ctrl->value); |
| 825 | break; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 826 | case V4L2_CID_AUTO_WHITE_BALANCE: |
| 827 | ret = mt9m111_set_autowhitebalance(icd, ctrl->value); |
| 828 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 829 | default: |
| 830 | ret = -EINVAL; |
| 831 | } |
| 832 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 833 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 834 | } |
| 835 | |
Harvey Harrison | ba77531 | 2008-08-27 17:20:01 -0300 | [diff] [blame] | 836 | static int mt9m111_restore_state(struct soc_camera_device *icd) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 837 | { |
| 838 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 839 | |
| 840 | mt9m111_set_context(icd, mt9m111->context); |
| 841 | mt9m111_set_pixfmt(icd, mt9m111->pixfmt); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 842 | mt9m111_setup_rect(icd, &mt9m111->rect); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 843 | mt9m111_set_flip(icd, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS); |
| 844 | mt9m111_set_flip(icd, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS); |
| 845 | mt9m111_set_global_gain(icd, icd->gain); |
| 846 | mt9m111_set_autoexposure(icd, mt9m111->autoexposure); |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 847 | mt9m111_set_autowhitebalance(icd, mt9m111->autowhitebalance); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int mt9m111_resume(struct soc_camera_device *icd) |
| 852 | { |
| 853 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 854 | int ret = 0; |
| 855 | |
| 856 | if (mt9m111->powered) { |
| 857 | ret = mt9m111_enable(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 858 | if (!ret) |
| 859 | ret = mt9m111_reset(icd); |
| 860 | if (!ret) |
| 861 | ret = mt9m111_restore_state(icd); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 862 | } |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | static int mt9m111_init(struct soc_camera_device *icd) |
| 867 | { |
| 868 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 869 | int ret; |
| 870 | |
| 871 | mt9m111->context = HIGHPOWER; |
| 872 | ret = mt9m111_enable(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 873 | if (!ret) |
| 874 | ret = mt9m111_reset(icd); |
| 875 | if (!ret) |
| 876 | ret = mt9m111_set_context(icd, mt9m111->context); |
| 877 | if (!ret) |
| 878 | ret = mt9m111_set_autoexposure(icd, mt9m111->autoexposure); |
| 879 | if (ret) |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 880 | dev_err(&icd->dev, "mt9m11x init failed: %d\n", ret); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 881 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | static int mt9m111_release(struct soc_camera_device *icd) |
| 885 | { |
| 886 | int ret; |
| 887 | |
| 888 | ret = mt9m111_disable(icd); |
| 889 | if (ret < 0) |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 890 | dev_err(&icd->dev, "mt9m11x release failed: %d\n", ret); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 891 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 892 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Interface active, can use i2c. If it fails, it can indeed mean, that |
| 897 | * this wasn't our capture interface, so, we wait for the right one |
| 898 | */ |
| 899 | static int mt9m111_video_probe(struct soc_camera_device *icd) |
| 900 | { |
| 901 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 902 | s32 data; |
| 903 | int ret; |
| 904 | |
| 905 | /* |
| 906 | * We must have a parent by now. And it cannot be a wrong one. |
| 907 | * So this entire test is completely redundant. |
| 908 | */ |
| 909 | if (!icd->dev.parent || |
| 910 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 911 | return -ENODEV; |
| 912 | |
| 913 | ret = mt9m111_enable(icd); |
| 914 | if (ret) |
| 915 | goto ei2c; |
| 916 | ret = mt9m111_reset(icd); |
| 917 | if (ret) |
| 918 | goto ei2c; |
| 919 | |
| 920 | data = reg_read(CHIP_VERSION); |
| 921 | |
| 922 | switch (data) { |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 923 | case 0x143a: /* MT9M111 */ |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 924 | mt9m111->model = V4L2_IDENT_MT9M111; |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 925 | break; |
| 926 | case 0x148c: /* MT9M112 */ |
| 927 | mt9m111->model = V4L2_IDENT_MT9M112; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 928 | break; |
| 929 | default: |
| 930 | ret = -ENODEV; |
| 931 | dev_err(&icd->dev, |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 932 | "No MT9M11x chip detected, register read %x\n", data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 933 | goto ei2c; |
| 934 | } |
| 935 | |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 936 | icd->formats = mt9m111_colour_formats; |
| 937 | icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats); |
| 938 | |
| 939 | dev_info(&icd->dev, "Detected a MT9M11x chip ID %x\n", data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 940 | |
| 941 | ret = soc_camera_video_start(icd); |
| 942 | if (ret) |
| 943 | goto eisis; |
| 944 | |
| 945 | mt9m111->autoexposure = 1; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 946 | mt9m111->autowhitebalance = 1; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 947 | |
| 948 | mt9m111->swap_rgb_even_odd = 1; |
| 949 | mt9m111->swap_rgb_red_blue = 1; |
| 950 | |
| 951 | return 0; |
| 952 | eisis: |
| 953 | ei2c: |
| 954 | return ret; |
| 955 | } |
| 956 | |
| 957 | static void mt9m111_video_remove(struct soc_camera_device *icd) |
| 958 | { |
| 959 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 960 | |
| 961 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m111->client->addr, |
| 962 | mt9m111->icd.dev.parent, mt9m111->icd.vdev); |
| 963 | soc_camera_video_stop(&mt9m111->icd); |
| 964 | } |
| 965 | |
| 966 | static int mt9m111_probe(struct i2c_client *client, |
| 967 | const struct i2c_device_id *did) |
| 968 | { |
| 969 | struct mt9m111 *mt9m111; |
| 970 | struct soc_camera_device *icd; |
| 971 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 972 | struct soc_camera_link *icl = client->dev.platform_data; |
| 973 | int ret; |
| 974 | |
| 975 | if (!icl) { |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 976 | dev_err(&client->dev, "MT9M11x driver needs platform data\n"); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 977 | return -EINVAL; |
| 978 | } |
| 979 | |
| 980 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 981 | dev_warn(&adapter->dev, |
| 982 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 983 | return -EIO; |
| 984 | } |
| 985 | |
| 986 | mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL); |
| 987 | if (!mt9m111) |
| 988 | return -ENOMEM; |
| 989 | |
| 990 | mt9m111->client = client; |
| 991 | i2c_set_clientdata(client, mt9m111); |
| 992 | |
| 993 | /* Second stage probe - when a capture adapter is there */ |
| 994 | icd = &mt9m111->icd; |
| 995 | icd->ops = &mt9m111_ops; |
| 996 | icd->control = &client->dev; |
| 997 | icd->x_min = MT9M111_MIN_DARK_COLS; |
| 998 | icd->y_min = MT9M111_MIN_DARK_ROWS; |
| 999 | icd->x_current = icd->x_min; |
| 1000 | icd->y_current = icd->y_min; |
| 1001 | icd->width_min = MT9M111_MIN_DARK_ROWS; |
| 1002 | icd->width_max = MT9M111_MAX_WIDTH; |
| 1003 | icd->height_min = MT9M111_MIN_DARK_COLS; |
| 1004 | icd->height_max = MT9M111_MAX_HEIGHT; |
| 1005 | icd->y_skip_top = 0; |
| 1006 | icd->iface = icl->bus_id; |
| 1007 | |
| 1008 | ret = soc_camera_device_register(icd); |
| 1009 | if (ret) |
| 1010 | goto eisdr; |
| 1011 | return 0; |
| 1012 | |
| 1013 | eisdr: |
| 1014 | kfree(mt9m111); |
| 1015 | return ret; |
| 1016 | } |
| 1017 | |
| 1018 | static int mt9m111_remove(struct i2c_client *client) |
| 1019 | { |
| 1020 | struct mt9m111 *mt9m111 = i2c_get_clientdata(client); |
| 1021 | soc_camera_device_unregister(&mt9m111->icd); |
| 1022 | kfree(mt9m111); |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | static const struct i2c_device_id mt9m111_id[] = { |
| 1028 | { "mt9m111", 0 }, |
| 1029 | { } |
| 1030 | }; |
| 1031 | MODULE_DEVICE_TABLE(i2c, mt9m111_id); |
| 1032 | |
| 1033 | static struct i2c_driver mt9m111_i2c_driver = { |
| 1034 | .driver = { |
| 1035 | .name = "mt9m111", |
| 1036 | }, |
| 1037 | .probe = mt9m111_probe, |
| 1038 | .remove = mt9m111_remove, |
| 1039 | .id_table = mt9m111_id, |
| 1040 | }; |
| 1041 | |
| 1042 | static int __init mt9m111_mod_init(void) |
| 1043 | { |
| 1044 | return i2c_add_driver(&mt9m111_i2c_driver); |
| 1045 | } |
| 1046 | |
| 1047 | static void __exit mt9m111_mod_exit(void) |
| 1048 | { |
| 1049 | i2c_del_driver(&mt9m111_i2c_driver); |
| 1050 | } |
| 1051 | |
| 1052 | module_init(mt9m111_mod_init); |
| 1053 | module_exit(mt9m111_mod_exit); |
| 1054 | |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 1055 | MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver"); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1056 | MODULE_AUTHOR("Robert Jarzmik"); |
| 1057 | MODULE_LICENSE("GPL"); |