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; |
| 155 | unsigned int left, top, width, height; |
| 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 | |
| 252 | static int mt9m111_setup_rect(struct soc_camera_device *icd) |
| 253 | { |
| 254 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 255 | int ret, is_raw_format; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 256 | int width = mt9m111->width; |
| 257 | int height = mt9m111->height; |
| 258 | |
| 259 | if ((mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8) |
| 260 | || (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16)) |
| 261 | is_raw_format = 1; |
| 262 | else |
| 263 | is_raw_format = 0; |
| 264 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 265 | ret = reg_write(COLUMN_START, mt9m111->left); |
| 266 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 267 | ret = reg_write(ROW_START, mt9m111->top); |
| 268 | |
| 269 | if (is_raw_format) { |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 270 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 271 | ret = reg_write(WINDOW_WIDTH, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 272 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 273 | ret = reg_write(WINDOW_HEIGHT, height); |
| 274 | } else { |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 275 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 276 | ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 277 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 278 | ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 279 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 280 | ret = reg_write(REDUCER_XSIZE_B, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 281 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 282 | ret = reg_write(REDUCER_YSIZE_B, height); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 283 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 284 | ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 285 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 286 | ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 287 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 288 | ret = reg_write(REDUCER_XSIZE_A, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 289 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 290 | ret = reg_write(REDUCER_YSIZE_A, height); |
| 291 | } |
| 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static int mt9m111_setup_pixfmt(struct soc_camera_device *icd, u16 outfmt) |
| 297 | { |
| 298 | int ret; |
| 299 | |
| 300 | ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 301 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 302 | ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt); |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static int mt9m111_setfmt_bayer8(struct soc_camera_device *icd) |
| 307 | { |
| 308 | return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_PROCESSED_BAYER); |
| 309 | } |
| 310 | |
| 311 | static int mt9m111_setfmt_bayer10(struct soc_camera_device *icd) |
| 312 | { |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 313 | return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_BYPASS_IFP); |
| 314 | } |
| 315 | |
| 316 | static int mt9m111_setfmt_rgb565(struct soc_camera_device *icd) |
| 317 | { |
| 318 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 319 | int val = 0; |
| 320 | |
| 321 | if (mt9m111->swap_rgb_red_blue) |
| 322 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 323 | if (mt9m111->swap_rgb_even_odd) |
| 324 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 325 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565; |
| 326 | |
| 327 | return mt9m111_setup_pixfmt(icd, val); |
| 328 | } |
| 329 | |
| 330 | static int mt9m111_setfmt_rgb555(struct soc_camera_device *icd) |
| 331 | { |
| 332 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 333 | int val = 0; |
| 334 | |
| 335 | if (mt9m111->swap_rgb_red_blue) |
| 336 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 337 | if (mt9m111->swap_rgb_even_odd) |
| 338 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 339 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555; |
| 340 | |
| 341 | return mt9m111_setup_pixfmt(icd, val); |
| 342 | } |
| 343 | |
| 344 | static int mt9m111_setfmt_yuv(struct soc_camera_device *icd) |
| 345 | { |
| 346 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 347 | int val = 0; |
| 348 | |
| 349 | if (mt9m111->swap_yuv_cb_cr) |
| 350 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 351 | if (mt9m111->swap_yuv_y_chromas) |
| 352 | val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y; |
| 353 | |
| 354 | return mt9m111_setup_pixfmt(icd, val); |
| 355 | } |
| 356 | |
| 357 | static int mt9m111_enable(struct soc_camera_device *icd) |
| 358 | { |
| 359 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 360 | struct soc_camera_link *icl = mt9m111->client->dev.platform_data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 361 | int ret; |
| 362 | |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 363 | if (icl->power) { |
| 364 | ret = icl->power(&mt9m111->client->dev, 1); |
| 365 | if (ret < 0) { |
| 366 | dev_err(icd->vdev->parent, |
| 367 | "Platform failed to power-on the camera.\n"); |
| 368 | return ret; |
| 369 | } |
| 370 | } |
| 371 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 372 | ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 373 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 374 | mt9m111->powered = 1; |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | static int mt9m111_disable(struct soc_camera_device *icd) |
| 379 | { |
| 380 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 381 | struct soc_camera_link *icl = mt9m111->client->dev.platform_data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 382 | int ret; |
| 383 | |
| 384 | ret = reg_clear(RESET, MT9M111_RESET_CHIP_ENABLE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 385 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 386 | mt9m111->powered = 0; |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 387 | |
| 388 | if (icl->power) |
| 389 | icl->power(&mt9m111->client->dev, 0); |
| 390 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | static int mt9m111_reset(struct soc_camera_device *icd) |
| 395 | { |
| 396 | int ret; |
| 397 | |
| 398 | ret = reg_set(RESET, MT9M111_RESET_RESET_MODE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 399 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 400 | ret = reg_set(RESET, MT9M111_RESET_RESET_SOC); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 401 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 402 | ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE |
| 403 | | MT9M111_RESET_RESET_SOC); |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | static int mt9m111_start_capture(struct soc_camera_device *icd) |
| 408 | { |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int mt9m111_stop_capture(struct soc_camera_device *icd) |
| 413 | { |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd) |
| 418 | { |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 419 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 420 | struct soc_camera_link *icl = mt9m111->client->dev.platform_data; |
| 421 | unsigned long flags = SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 422 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
| 423 | SOCAM_DATAWIDTH_8; |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 424 | |
| 425 | return soc_camera_apply_sensor_flags(icl, flags); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f) |
| 429 | { |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int mt9m111_set_pixfmt(struct soc_camera_device *icd, u32 pixfmt) |
| 434 | { |
| 435 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 436 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 437 | |
| 438 | switch (pixfmt) { |
| 439 | case V4L2_PIX_FMT_SBGGR8: |
| 440 | ret = mt9m111_setfmt_bayer8(icd); |
| 441 | break; |
| 442 | case V4L2_PIX_FMT_SBGGR16: |
| 443 | ret = mt9m111_setfmt_bayer10(icd); |
| 444 | break; |
| 445 | case V4L2_PIX_FMT_RGB555: |
| 446 | ret = mt9m111_setfmt_rgb555(icd); |
| 447 | break; |
| 448 | case V4L2_PIX_FMT_RGB565: |
| 449 | ret = mt9m111_setfmt_rgb565(icd); |
| 450 | break; |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 451 | case V4L2_PIX_FMT_UYVY: |
| 452 | mt9m111->swap_yuv_y_chromas = 0; |
| 453 | mt9m111->swap_yuv_cb_cr = 0; |
| 454 | ret = mt9m111_setfmt_yuv(icd); |
| 455 | break; |
| 456 | case V4L2_PIX_FMT_VYUY: |
| 457 | mt9m111->swap_yuv_y_chromas = 0; |
| 458 | mt9m111->swap_yuv_cb_cr = 1; |
| 459 | ret = mt9m111_setfmt_yuv(icd); |
| 460 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 461 | case V4L2_PIX_FMT_YUYV: |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 462 | mt9m111->swap_yuv_y_chromas = 1; |
| 463 | mt9m111->swap_yuv_cb_cr = 0; |
| 464 | ret = mt9m111_setfmt_yuv(icd); |
| 465 | break; |
| 466 | case V4L2_PIX_FMT_YVYU: |
| 467 | mt9m111->swap_yuv_y_chromas = 1; |
| 468 | mt9m111->swap_yuv_cb_cr = 1; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 469 | ret = mt9m111_setfmt_yuv(icd); |
| 470 | break; |
| 471 | default: |
| 472 | dev_err(&icd->dev, "Pixel format not handled : %x\n", pixfmt); |
| 473 | ret = -EINVAL; |
| 474 | } |
| 475 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 476 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 477 | mt9m111->pixfmt = pixfmt; |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 482 | static int mt9m111_set_fmt(struct soc_camera_device *icd, |
| 483 | __u32 pixfmt, struct v4l2_rect *rect) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 484 | { |
| 485 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 486 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 487 | |
| 488 | mt9m111->left = rect->left; |
| 489 | mt9m111->top = rect->top; |
| 490 | mt9m111->width = rect->width; |
| 491 | mt9m111->height = rect->height; |
| 492 | |
| 493 | dev_dbg(&icd->dev, "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n", |
| 494 | __func__, pixfmt, mt9m111->left, mt9m111->top, mt9m111->width, |
| 495 | mt9m111->height); |
| 496 | |
| 497 | ret = mt9m111_setup_rect(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 498 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 499 | ret = mt9m111_set_pixfmt(icd, pixfmt); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 500 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 501 | } |
| 502 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 503 | static int mt9m111_try_fmt(struct soc_camera_device *icd, |
| 504 | struct v4l2_format *f) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 505 | { |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 506 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 507 | |
| 508 | if (pix->height > MT9M111_MAX_HEIGHT) |
| 509 | pix->height = MT9M111_MAX_HEIGHT; |
| 510 | if (pix->width > MT9M111_MAX_WIDTH) |
| 511 | pix->width = MT9M111_MAX_WIDTH; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int mt9m111_get_chip_id(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 517 | struct v4l2_dbg_chip_ident *id) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 518 | { |
| 519 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 520 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 521 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 522 | return -EINVAL; |
| 523 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 524 | if (id->match.addr != mt9m111->client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 525 | return -ENODEV; |
| 526 | |
| 527 | id->ident = mt9m111->model; |
| 528 | id->revision = 0; |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 534 | static int mt9m111_get_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 535 | struct v4l2_dbg_register *reg) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 536 | { |
| 537 | int val; |
| 538 | |
| 539 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 540 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 541 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 542 | return -EINVAL; |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 543 | if (reg->match.addr != mt9m111->client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 544 | return -ENODEV; |
| 545 | |
| 546 | val = mt9m111_reg_read(icd, reg->reg); |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 547 | reg->size = 2; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 548 | reg->val = (u64)val; |
| 549 | |
| 550 | if (reg->val > 0xffff) |
| 551 | return -EIO; |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int mt9m111_set_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 557 | struct v4l2_dbg_register *reg) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 558 | { |
| 559 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 560 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 561 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 562 | return -EINVAL; |
| 563 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 564 | if (reg->match.addr != mt9m111->client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 565 | return -ENODEV; |
| 566 | |
| 567 | if (mt9m111_reg_write(icd, reg->reg, reg->val) < 0) |
| 568 | return -EIO; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | #endif |
| 573 | |
| 574 | static const struct v4l2_queryctrl mt9m111_controls[] = { |
| 575 | { |
| 576 | .id = V4L2_CID_VFLIP, |
| 577 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 578 | .name = "Flip Verticaly", |
| 579 | .minimum = 0, |
| 580 | .maximum = 1, |
| 581 | .step = 1, |
| 582 | .default_value = 0, |
| 583 | }, { |
| 584 | .id = V4L2_CID_HFLIP, |
| 585 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 586 | .name = "Flip Horizontaly", |
| 587 | .minimum = 0, |
| 588 | .maximum = 1, |
| 589 | .step = 1, |
| 590 | .default_value = 0, |
| 591 | }, { /* gain = 1/32*val (=>gain=1 if val==32) */ |
| 592 | .id = V4L2_CID_GAIN, |
| 593 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 594 | .name = "Gain", |
| 595 | .minimum = 0, |
| 596 | .maximum = 63 * 2 * 2, |
| 597 | .step = 1, |
| 598 | .default_value = 32, |
| 599 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 600 | }, { |
| 601 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 602 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 603 | .name = "Auto Exposure", |
| 604 | .minimum = 0, |
| 605 | .maximum = 1, |
| 606 | .step = 1, |
| 607 | .default_value = 1, |
| 608 | } |
| 609 | }; |
| 610 | |
| 611 | static int mt9m111_video_probe(struct soc_camera_device *); |
| 612 | static void mt9m111_video_remove(struct soc_camera_device *); |
| 613 | static int mt9m111_get_control(struct soc_camera_device *, |
| 614 | struct v4l2_control *); |
| 615 | static int mt9m111_set_control(struct soc_camera_device *, |
| 616 | struct v4l2_control *); |
| 617 | static int mt9m111_resume(struct soc_camera_device *icd); |
| 618 | static int mt9m111_init(struct soc_camera_device *icd); |
| 619 | static int mt9m111_release(struct soc_camera_device *icd); |
| 620 | |
| 621 | static struct soc_camera_ops mt9m111_ops = { |
| 622 | .owner = THIS_MODULE, |
| 623 | .probe = mt9m111_video_probe, |
| 624 | .remove = mt9m111_video_remove, |
| 625 | .init = mt9m111_init, |
| 626 | .resume = mt9m111_resume, |
| 627 | .release = mt9m111_release, |
| 628 | .start_capture = mt9m111_start_capture, |
| 629 | .stop_capture = mt9m111_stop_capture, |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 630 | .set_fmt = mt9m111_set_fmt, |
| 631 | .try_fmt = mt9m111_try_fmt, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 632 | .query_bus_param = mt9m111_query_bus_param, |
| 633 | .set_bus_param = mt9m111_set_bus_param, |
| 634 | .controls = mt9m111_controls, |
| 635 | .num_controls = ARRAY_SIZE(mt9m111_controls), |
| 636 | .get_control = mt9m111_get_control, |
| 637 | .set_control = mt9m111_set_control, |
| 638 | .get_chip_id = mt9m111_get_chip_id, |
| 639 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 640 | .get_register = mt9m111_get_register, |
| 641 | .set_register = mt9m111_set_register, |
| 642 | #endif |
| 643 | }; |
| 644 | |
| 645 | static int mt9m111_set_flip(struct soc_camera_device *icd, int flip, int mask) |
| 646 | { |
| 647 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 648 | int ret; |
| 649 | |
| 650 | if (mt9m111->context == HIGHPOWER) { |
| 651 | if (flip) |
| 652 | ret = reg_set(READ_MODE_B, mask); |
| 653 | else |
| 654 | ret = reg_clear(READ_MODE_B, mask); |
| 655 | } else { |
| 656 | if (flip) |
| 657 | ret = reg_set(READ_MODE_A, mask); |
| 658 | else |
| 659 | ret = reg_clear(READ_MODE_A, mask); |
| 660 | } |
| 661 | |
| 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | static int mt9m111_get_global_gain(struct soc_camera_device *icd) |
| 666 | { |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 667 | int data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 668 | |
| 669 | data = reg_read(GLOBAL_GAIN); |
| 670 | if (data >= 0) |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 671 | return (data & 0x2f) * (1 << ((data >> 10) & 1)) * |
| 672 | (1 << ((data >> 9) & 1)); |
| 673 | return data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 674 | } |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 675 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 676 | static int mt9m111_set_global_gain(struct soc_camera_device *icd, int gain) |
| 677 | { |
| 678 | u16 val; |
| 679 | |
| 680 | if (gain > 63 * 2 * 2) |
| 681 | return -EINVAL; |
| 682 | |
| 683 | icd->gain = gain; |
| 684 | if ((gain >= 64 * 2) && (gain < 63 * 2 * 2)) |
| 685 | val = (1 << 10) | (1 << 9) | (gain / 4); |
| 686 | else if ((gain >= 64) && (gain < 64 * 2)) |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 687 | val = (1 << 9) | (gain / 2); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 688 | else |
| 689 | val = gain; |
| 690 | |
| 691 | return reg_write(GLOBAL_GAIN, val); |
| 692 | } |
| 693 | |
| 694 | static int mt9m111_set_autoexposure(struct soc_camera_device *icd, int on) |
| 695 | { |
| 696 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 697 | int ret; |
| 698 | |
| 699 | if (on) |
| 700 | ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 701 | else |
| 702 | ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 703 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 704 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 705 | mt9m111->autoexposure = on; |
| 706 | |
| 707 | return ret; |
| 708 | } |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 709 | |
| 710 | static int mt9m111_set_autowhitebalance(struct soc_camera_device *icd, int on) |
| 711 | { |
| 712 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 713 | int ret; |
| 714 | |
| 715 | if (on) |
| 716 | ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN); |
| 717 | else |
| 718 | ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN); |
| 719 | |
| 720 | if (!ret) |
| 721 | mt9m111->autowhitebalance = on; |
| 722 | |
| 723 | return ret; |
| 724 | } |
| 725 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 726 | static int mt9m111_get_control(struct soc_camera_device *icd, |
| 727 | struct v4l2_control *ctrl) |
| 728 | { |
| 729 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 730 | int data; |
| 731 | |
| 732 | switch (ctrl->id) { |
| 733 | case V4L2_CID_VFLIP: |
| 734 | if (mt9m111->context == HIGHPOWER) |
| 735 | data = reg_read(READ_MODE_B); |
| 736 | else |
| 737 | data = reg_read(READ_MODE_A); |
| 738 | |
| 739 | if (data < 0) |
| 740 | return -EIO; |
| 741 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS); |
| 742 | break; |
| 743 | case V4L2_CID_HFLIP: |
| 744 | if (mt9m111->context == HIGHPOWER) |
| 745 | data = reg_read(READ_MODE_B); |
| 746 | else |
| 747 | data = reg_read(READ_MODE_A); |
| 748 | |
| 749 | if (data < 0) |
| 750 | return -EIO; |
| 751 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS); |
| 752 | break; |
| 753 | case V4L2_CID_GAIN: |
| 754 | data = mt9m111_get_global_gain(icd); |
| 755 | if (data < 0) |
| 756 | return data; |
| 757 | ctrl->value = data; |
| 758 | break; |
| 759 | case V4L2_CID_EXPOSURE_AUTO: |
| 760 | ctrl->value = mt9m111->autoexposure; |
| 761 | break; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 762 | case V4L2_CID_AUTO_WHITE_BALANCE: |
| 763 | ctrl->value = mt9m111->autowhitebalance; |
| 764 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 765 | } |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | static int mt9m111_set_control(struct soc_camera_device *icd, |
| 770 | struct v4l2_control *ctrl) |
| 771 | { |
| 772 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 773 | const struct v4l2_queryctrl *qctrl; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 774 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 775 | |
| 776 | qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id); |
| 777 | |
| 778 | if (!qctrl) |
| 779 | return -EINVAL; |
| 780 | |
| 781 | switch (ctrl->id) { |
| 782 | case V4L2_CID_VFLIP: |
| 783 | mt9m111->vflip = ctrl->value; |
| 784 | ret = mt9m111_set_flip(icd, ctrl->value, |
| 785 | MT9M111_RMB_MIRROR_ROWS); |
| 786 | break; |
| 787 | case V4L2_CID_HFLIP: |
| 788 | mt9m111->hflip = ctrl->value; |
| 789 | ret = mt9m111_set_flip(icd, ctrl->value, |
| 790 | MT9M111_RMB_MIRROR_COLS); |
| 791 | break; |
| 792 | case V4L2_CID_GAIN: |
| 793 | ret = mt9m111_set_global_gain(icd, ctrl->value); |
| 794 | break; |
| 795 | case V4L2_CID_EXPOSURE_AUTO: |
| 796 | ret = mt9m111_set_autoexposure(icd, ctrl->value); |
| 797 | break; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 798 | case V4L2_CID_AUTO_WHITE_BALANCE: |
| 799 | ret = mt9m111_set_autowhitebalance(icd, ctrl->value); |
| 800 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 801 | default: |
| 802 | ret = -EINVAL; |
| 803 | } |
| 804 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 805 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 806 | } |
| 807 | |
Harvey Harrison | ba77531 | 2008-08-27 17:20:01 -0300 | [diff] [blame] | 808 | static int mt9m111_restore_state(struct soc_camera_device *icd) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 809 | { |
| 810 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 811 | |
| 812 | mt9m111_set_context(icd, mt9m111->context); |
| 813 | mt9m111_set_pixfmt(icd, mt9m111->pixfmt); |
| 814 | mt9m111_setup_rect(icd); |
| 815 | mt9m111_set_flip(icd, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS); |
| 816 | mt9m111_set_flip(icd, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS); |
| 817 | mt9m111_set_global_gain(icd, icd->gain); |
| 818 | mt9m111_set_autoexposure(icd, mt9m111->autoexposure); |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 819 | mt9m111_set_autowhitebalance(icd, mt9m111->autowhitebalance); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int mt9m111_resume(struct soc_camera_device *icd) |
| 824 | { |
| 825 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 826 | int ret = 0; |
| 827 | |
| 828 | if (mt9m111->powered) { |
| 829 | ret = mt9m111_enable(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 830 | if (!ret) |
| 831 | ret = mt9m111_reset(icd); |
| 832 | if (!ret) |
| 833 | ret = mt9m111_restore_state(icd); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 834 | } |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | static int mt9m111_init(struct soc_camera_device *icd) |
| 839 | { |
| 840 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 841 | int ret; |
| 842 | |
| 843 | mt9m111->context = HIGHPOWER; |
| 844 | ret = mt9m111_enable(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 845 | if (!ret) |
| 846 | ret = mt9m111_reset(icd); |
| 847 | if (!ret) |
| 848 | ret = mt9m111_set_context(icd, mt9m111->context); |
| 849 | if (!ret) |
| 850 | ret = mt9m111_set_autoexposure(icd, mt9m111->autoexposure); |
| 851 | if (ret) |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 852 | dev_err(&icd->dev, "mt9m11x init failed: %d\n", ret); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 853 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static int mt9m111_release(struct soc_camera_device *icd) |
| 857 | { |
| 858 | int ret; |
| 859 | |
| 860 | ret = mt9m111_disable(icd); |
| 861 | if (ret < 0) |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 862 | dev_err(&icd->dev, "mt9m11x release failed: %d\n", ret); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 863 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 864 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* |
| 868 | * Interface active, can use i2c. If it fails, it can indeed mean, that |
| 869 | * this wasn't our capture interface, so, we wait for the right one |
| 870 | */ |
| 871 | static int mt9m111_video_probe(struct soc_camera_device *icd) |
| 872 | { |
| 873 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 874 | s32 data; |
| 875 | int ret; |
| 876 | |
| 877 | /* |
| 878 | * We must have a parent by now. And it cannot be a wrong one. |
| 879 | * So this entire test is completely redundant. |
| 880 | */ |
| 881 | if (!icd->dev.parent || |
| 882 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 883 | return -ENODEV; |
| 884 | |
| 885 | ret = mt9m111_enable(icd); |
| 886 | if (ret) |
| 887 | goto ei2c; |
| 888 | ret = mt9m111_reset(icd); |
| 889 | if (ret) |
| 890 | goto ei2c; |
| 891 | |
| 892 | data = reg_read(CHIP_VERSION); |
| 893 | |
| 894 | switch (data) { |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 895 | case 0x143a: /* MT9M111 */ |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 896 | mt9m111->model = V4L2_IDENT_MT9M111; |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 897 | break; |
| 898 | case 0x148c: /* MT9M112 */ |
| 899 | mt9m111->model = V4L2_IDENT_MT9M112; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 900 | break; |
| 901 | default: |
| 902 | ret = -ENODEV; |
| 903 | dev_err(&icd->dev, |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 904 | "No MT9M11x chip detected, register read %x\n", data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 905 | goto ei2c; |
| 906 | } |
| 907 | |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 908 | icd->formats = mt9m111_colour_formats; |
| 909 | icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats); |
| 910 | |
| 911 | dev_info(&icd->dev, "Detected a MT9M11x chip ID %x\n", data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 912 | |
| 913 | ret = soc_camera_video_start(icd); |
| 914 | if (ret) |
| 915 | goto eisis; |
| 916 | |
| 917 | mt9m111->autoexposure = 1; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 918 | mt9m111->autowhitebalance = 1; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 919 | |
| 920 | mt9m111->swap_rgb_even_odd = 1; |
| 921 | mt9m111->swap_rgb_red_blue = 1; |
| 922 | |
| 923 | return 0; |
| 924 | eisis: |
| 925 | ei2c: |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | static void mt9m111_video_remove(struct soc_camera_device *icd) |
| 930 | { |
| 931 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 932 | |
| 933 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m111->client->addr, |
| 934 | mt9m111->icd.dev.parent, mt9m111->icd.vdev); |
| 935 | soc_camera_video_stop(&mt9m111->icd); |
| 936 | } |
| 937 | |
| 938 | static int mt9m111_probe(struct i2c_client *client, |
| 939 | const struct i2c_device_id *did) |
| 940 | { |
| 941 | struct mt9m111 *mt9m111; |
| 942 | struct soc_camera_device *icd; |
| 943 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 944 | struct soc_camera_link *icl = client->dev.platform_data; |
| 945 | int ret; |
| 946 | |
| 947 | if (!icl) { |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 948 | dev_err(&client->dev, "MT9M11x driver needs platform data\n"); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 949 | return -EINVAL; |
| 950 | } |
| 951 | |
| 952 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 953 | dev_warn(&adapter->dev, |
| 954 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 955 | return -EIO; |
| 956 | } |
| 957 | |
| 958 | mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL); |
| 959 | if (!mt9m111) |
| 960 | return -ENOMEM; |
| 961 | |
| 962 | mt9m111->client = client; |
| 963 | i2c_set_clientdata(client, mt9m111); |
| 964 | |
| 965 | /* Second stage probe - when a capture adapter is there */ |
| 966 | icd = &mt9m111->icd; |
| 967 | icd->ops = &mt9m111_ops; |
| 968 | icd->control = &client->dev; |
| 969 | icd->x_min = MT9M111_MIN_DARK_COLS; |
| 970 | icd->y_min = MT9M111_MIN_DARK_ROWS; |
| 971 | icd->x_current = icd->x_min; |
| 972 | icd->y_current = icd->y_min; |
| 973 | icd->width_min = MT9M111_MIN_DARK_ROWS; |
| 974 | icd->width_max = MT9M111_MAX_WIDTH; |
| 975 | icd->height_min = MT9M111_MIN_DARK_COLS; |
| 976 | icd->height_max = MT9M111_MAX_HEIGHT; |
| 977 | icd->y_skip_top = 0; |
| 978 | icd->iface = icl->bus_id; |
| 979 | |
| 980 | ret = soc_camera_device_register(icd); |
| 981 | if (ret) |
| 982 | goto eisdr; |
| 983 | return 0; |
| 984 | |
| 985 | eisdr: |
| 986 | kfree(mt9m111); |
| 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | static int mt9m111_remove(struct i2c_client *client) |
| 991 | { |
| 992 | struct mt9m111 *mt9m111 = i2c_get_clientdata(client); |
| 993 | soc_camera_device_unregister(&mt9m111->icd); |
| 994 | kfree(mt9m111); |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | static const struct i2c_device_id mt9m111_id[] = { |
| 1000 | { "mt9m111", 0 }, |
| 1001 | { } |
| 1002 | }; |
| 1003 | MODULE_DEVICE_TABLE(i2c, mt9m111_id); |
| 1004 | |
| 1005 | static struct i2c_driver mt9m111_i2c_driver = { |
| 1006 | .driver = { |
| 1007 | .name = "mt9m111", |
| 1008 | }, |
| 1009 | .probe = mt9m111_probe, |
| 1010 | .remove = mt9m111_remove, |
| 1011 | .id_table = mt9m111_id, |
| 1012 | }; |
| 1013 | |
| 1014 | static int __init mt9m111_mod_init(void) |
| 1015 | { |
| 1016 | return i2c_add_driver(&mt9m111_i2c_driver); |
| 1017 | } |
| 1018 | |
| 1019 | static void __exit mt9m111_mod_exit(void) |
| 1020 | { |
| 1021 | i2c_del_driver(&mt9m111_i2c_driver); |
| 1022 | } |
| 1023 | |
| 1024 | module_init(mt9m111_mod_init); |
| 1025 | module_exit(mt9m111_mod_exit); |
| 1026 | |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 1027 | MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver"); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1028 | MODULE_AUTHOR("Robert Jarzmik"); |
| 1029 | MODULE_LICENSE("GPL"); |