Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9M111 CMOS Image Sensor from Micron |
| 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 | /* |
| 22 | * mt9m111 i2c address is 0x5d or 0x48 (depending on SAddr pin) |
| 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) |
| 93 | |
| 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) |
| 131 | |
| 132 | static const struct soc_camera_data_format mt9m111_colour_formats[] = { |
| 133 | COL_FMT("YCrYCb 8 bit", 8, V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_JPEG), |
| 134 | RGB_FMT("RGB 565", 16, V4L2_PIX_FMT_RGB565), |
| 135 | RGB_FMT("RGB 555", 16, V4L2_PIX_FMT_RGB555), |
| 136 | RGB_FMT("Bayer (sRGB) 10 bit", 10, V4L2_PIX_FMT_SBGGR16), |
| 137 | RGB_FMT("Bayer (sRGB) 8 bit", 8, V4L2_PIX_FMT_SBGGR8), |
| 138 | }; |
| 139 | |
| 140 | enum mt9m111_context { |
| 141 | HIGHPOWER = 0, |
| 142 | LOWPOWER, |
| 143 | }; |
| 144 | |
| 145 | struct mt9m111 { |
| 146 | struct i2c_client *client; |
| 147 | struct soc_camera_device icd; |
| 148 | int model; /* V4L2_IDENT_MT9M111* codes from v4l2-chip-ident.h */ |
| 149 | enum mt9m111_context context; |
| 150 | unsigned int left, top, width, height; |
| 151 | u32 pixfmt; |
| 152 | unsigned char autoexposure; |
| 153 | unsigned char datawidth; |
| 154 | unsigned int powered:1; |
| 155 | unsigned int hflip:1; |
| 156 | unsigned int vflip:1; |
| 157 | unsigned int swap_rgb_even_odd:1; |
| 158 | unsigned int swap_rgb_red_blue:1; |
| 159 | unsigned int swap_yuv_y_chromas:1; |
| 160 | unsigned int swap_yuv_cb_cr:1; |
| 161 | }; |
| 162 | |
| 163 | static int reg_page_map_set(struct i2c_client *client, const u16 reg) |
| 164 | { |
| 165 | int ret; |
| 166 | u16 page; |
| 167 | static int lastpage = -1; /* PageMap cache value */ |
| 168 | |
| 169 | page = (reg >> 8); |
| 170 | if (page == lastpage) |
| 171 | return 0; |
| 172 | if (page > 2) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page)); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 176 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 177 | lastpage = page; |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int mt9m111_reg_read(struct soc_camera_device *icd, const u16 reg) |
| 182 | { |
| 183 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 184 | struct i2c_client *client = mt9m111->client; |
| 185 | int ret; |
| 186 | |
| 187 | ret = reg_page_map_set(client, reg); |
| 188 | if (!ret) |
| 189 | ret = swab16(i2c_smbus_read_word_data(client, (reg & 0xff))); |
| 190 | |
| 191 | dev_dbg(&icd->dev, "read reg.%03x -> %04x\n", reg, ret); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static int mt9m111_reg_write(struct soc_camera_device *icd, const u16 reg, |
| 196 | const u16 data) |
| 197 | { |
| 198 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 199 | struct i2c_client *client = mt9m111->client; |
| 200 | int ret; |
| 201 | |
| 202 | ret = reg_page_map_set(client, reg); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 203 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 204 | ret = i2c_smbus_write_word_data(mt9m111->client, (reg & 0xff), |
| 205 | swab16(data)); |
| 206 | dev_dbg(&icd->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static int mt9m111_reg_set(struct soc_camera_device *icd, const u16 reg, |
| 211 | const u16 data) |
| 212 | { |
| 213 | int ret; |
| 214 | |
| 215 | ret = mt9m111_reg_read(icd, reg); |
| 216 | if (ret >= 0) |
| 217 | ret = mt9m111_reg_write(icd, reg, ret | data); |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static int mt9m111_reg_clear(struct soc_camera_device *icd, const u16 reg, |
| 222 | const u16 data) |
| 223 | { |
| 224 | int ret; |
| 225 | |
| 226 | ret = mt9m111_reg_read(icd, reg); |
| 227 | return mt9m111_reg_write(icd, reg, ret & ~data); |
| 228 | } |
| 229 | |
| 230 | static int mt9m111_set_context(struct soc_camera_device *icd, |
| 231 | enum mt9m111_context ctxt) |
| 232 | { |
| 233 | int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B |
| 234 | | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B |
| 235 | | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B |
| 236 | | MT9M111_CTXT_CTRL_VBLANK_SEL_B |
| 237 | | MT9M111_CTXT_CTRL_HBLANK_SEL_B; |
| 238 | int valA = MT9M111_CTXT_CTRL_RESTART; |
| 239 | |
| 240 | if (ctxt == HIGHPOWER) |
| 241 | return reg_write(CONTEXT_CONTROL, valB); |
| 242 | else |
| 243 | return reg_write(CONTEXT_CONTROL, valA); |
| 244 | } |
| 245 | |
| 246 | static int mt9m111_setup_rect(struct soc_camera_device *icd) |
| 247 | { |
| 248 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 249 | int ret, is_raw_format; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 250 | int width = mt9m111->width; |
| 251 | int height = mt9m111->height; |
| 252 | |
| 253 | if ((mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8) |
| 254 | || (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16)) |
| 255 | is_raw_format = 1; |
| 256 | else |
| 257 | is_raw_format = 0; |
| 258 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 259 | ret = reg_write(COLUMN_START, mt9m111->left); |
| 260 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 261 | ret = reg_write(ROW_START, mt9m111->top); |
| 262 | |
| 263 | if (is_raw_format) { |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 264 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 265 | ret = reg_write(WINDOW_WIDTH, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 266 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 267 | ret = reg_write(WINDOW_HEIGHT, height); |
| 268 | } else { |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 269 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 270 | ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH); |
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(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT); |
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(REDUCER_XSIZE_B, width); |
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_YSIZE_B, height); |
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_XZOOM_A, MT9M111_MAX_WIDTH); |
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_YZOOM_A, MT9M111_MAX_HEIGHT); |
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_XSIZE_A, width); |
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_YSIZE_A, height); |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static int mt9m111_setup_pixfmt(struct soc_camera_device *icd, u16 outfmt) |
| 291 | { |
| 292 | int ret; |
| 293 | |
| 294 | ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 295 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 296 | ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt); |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static int mt9m111_setfmt_bayer8(struct soc_camera_device *icd) |
| 301 | { |
| 302 | return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_PROCESSED_BAYER); |
| 303 | } |
| 304 | |
| 305 | static int mt9m111_setfmt_bayer10(struct soc_camera_device *icd) |
| 306 | { |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 307 | return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_BYPASS_IFP); |
| 308 | } |
| 309 | |
| 310 | static int mt9m111_setfmt_rgb565(struct soc_camera_device *icd) |
| 311 | { |
| 312 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 313 | int val = 0; |
| 314 | |
| 315 | if (mt9m111->swap_rgb_red_blue) |
| 316 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 317 | if (mt9m111->swap_rgb_even_odd) |
| 318 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 319 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565; |
| 320 | |
| 321 | return mt9m111_setup_pixfmt(icd, val); |
| 322 | } |
| 323 | |
| 324 | static int mt9m111_setfmt_rgb555(struct soc_camera_device *icd) |
| 325 | { |
| 326 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 327 | int val = 0; |
| 328 | |
| 329 | if (mt9m111->swap_rgb_red_blue) |
| 330 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 331 | if (mt9m111->swap_rgb_even_odd) |
| 332 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 333 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555; |
| 334 | |
| 335 | return mt9m111_setup_pixfmt(icd, val); |
| 336 | } |
| 337 | |
| 338 | static int mt9m111_setfmt_yuv(struct soc_camera_device *icd) |
| 339 | { |
| 340 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 341 | int val = 0; |
| 342 | |
| 343 | if (mt9m111->swap_yuv_cb_cr) |
| 344 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 345 | if (mt9m111->swap_yuv_y_chromas) |
| 346 | val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y; |
| 347 | |
| 348 | return mt9m111_setup_pixfmt(icd, val); |
| 349 | } |
| 350 | |
| 351 | static int mt9m111_enable(struct soc_camera_device *icd) |
| 352 | { |
| 353 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 354 | int ret; |
| 355 | |
| 356 | ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 357 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 358 | mt9m111->powered = 1; |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | static int mt9m111_disable(struct soc_camera_device *icd) |
| 363 | { |
| 364 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 365 | int ret; |
| 366 | |
| 367 | ret = reg_clear(RESET, MT9M111_RESET_CHIP_ENABLE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 368 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 369 | mt9m111->powered = 0; |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static int mt9m111_reset(struct soc_camera_device *icd) |
| 374 | { |
| 375 | int ret; |
| 376 | |
| 377 | ret = reg_set(RESET, MT9M111_RESET_RESET_MODE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 378 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 379 | ret = reg_set(RESET, MT9M111_RESET_RESET_SOC); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 380 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 381 | ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE |
| 382 | | MT9M111_RESET_RESET_SOC); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static int mt9m111_start_capture(struct soc_camera_device *icd) |
| 387 | { |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static int mt9m111_stop_capture(struct soc_camera_device *icd) |
| 392 | { |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd) |
| 397 | { |
| 398 | return SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING | |
| 399 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
| 400 | SOCAM_DATAWIDTH_8; |
| 401 | } |
| 402 | |
| 403 | static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f) |
| 404 | { |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int mt9m111_set_pixfmt(struct soc_camera_device *icd, u32 pixfmt) |
| 409 | { |
| 410 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 411 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 412 | |
| 413 | switch (pixfmt) { |
| 414 | case V4L2_PIX_FMT_SBGGR8: |
| 415 | ret = mt9m111_setfmt_bayer8(icd); |
| 416 | break; |
| 417 | case V4L2_PIX_FMT_SBGGR16: |
| 418 | ret = mt9m111_setfmt_bayer10(icd); |
| 419 | break; |
| 420 | case V4L2_PIX_FMT_RGB555: |
| 421 | ret = mt9m111_setfmt_rgb555(icd); |
| 422 | break; |
| 423 | case V4L2_PIX_FMT_RGB565: |
| 424 | ret = mt9m111_setfmt_rgb565(icd); |
| 425 | break; |
| 426 | case V4L2_PIX_FMT_YUYV: |
| 427 | ret = mt9m111_setfmt_yuv(icd); |
| 428 | break; |
| 429 | default: |
| 430 | dev_err(&icd->dev, "Pixel format not handled : %x\n", pixfmt); |
| 431 | ret = -EINVAL; |
| 432 | } |
| 433 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 434 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 435 | mt9m111->pixfmt = pixfmt; |
| 436 | |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | static int mt9m111_set_fmt_cap(struct soc_camera_device *icd, |
| 441 | __u32 pixfmt, struct v4l2_rect *rect) |
| 442 | { |
| 443 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 444 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 445 | |
| 446 | mt9m111->left = rect->left; |
| 447 | mt9m111->top = rect->top; |
| 448 | mt9m111->width = rect->width; |
| 449 | mt9m111->height = rect->height; |
| 450 | |
| 451 | dev_dbg(&icd->dev, "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n", |
| 452 | __func__, pixfmt, mt9m111->left, mt9m111->top, mt9m111->width, |
| 453 | mt9m111->height); |
| 454 | |
| 455 | ret = mt9m111_setup_rect(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 456 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 457 | ret = mt9m111_set_pixfmt(icd, pixfmt); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 458 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static int mt9m111_try_fmt_cap(struct soc_camera_device *icd, |
| 462 | struct v4l2_format *f) |
| 463 | { |
| 464 | if (f->fmt.pix.height > MT9M111_MAX_HEIGHT) |
| 465 | f->fmt.pix.height = MT9M111_MAX_HEIGHT; |
| 466 | if (f->fmt.pix.width > MT9M111_MAX_WIDTH) |
| 467 | f->fmt.pix.width = MT9M111_MAX_WIDTH; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int mt9m111_get_chip_id(struct soc_camera_device *icd, |
| 473 | struct v4l2_chip_ident *id) |
| 474 | { |
| 475 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 476 | |
| 477 | if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | if (id->match_chip != mt9m111->client->addr) |
| 481 | return -ENODEV; |
| 482 | |
| 483 | id->ident = mt9m111->model; |
| 484 | id->revision = 0; |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 490 | static int mt9m111_get_register(struct soc_camera_device *icd, |
| 491 | struct v4l2_register *reg) |
| 492 | { |
| 493 | int val; |
| 494 | |
| 495 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 496 | |
| 497 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
| 498 | return -EINVAL; |
| 499 | if (reg->match_chip != mt9m111->client->addr) |
| 500 | return -ENODEV; |
| 501 | |
| 502 | val = mt9m111_reg_read(icd, reg->reg); |
| 503 | reg->val = (u64)val; |
| 504 | |
| 505 | if (reg->val > 0xffff) |
| 506 | return -EIO; |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int mt9m111_set_register(struct soc_camera_device *icd, |
| 512 | struct v4l2_register *reg) |
| 513 | { |
| 514 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 515 | |
| 516 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
| 517 | return -EINVAL; |
| 518 | |
| 519 | if (reg->match_chip != mt9m111->client->addr) |
| 520 | return -ENODEV; |
| 521 | |
| 522 | if (mt9m111_reg_write(icd, reg->reg, reg->val) < 0) |
| 523 | return -EIO; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | #endif |
| 528 | |
| 529 | static const struct v4l2_queryctrl mt9m111_controls[] = { |
| 530 | { |
| 531 | .id = V4L2_CID_VFLIP, |
| 532 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 533 | .name = "Flip Verticaly", |
| 534 | .minimum = 0, |
| 535 | .maximum = 1, |
| 536 | .step = 1, |
| 537 | .default_value = 0, |
| 538 | }, { |
| 539 | .id = V4L2_CID_HFLIP, |
| 540 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 541 | .name = "Flip Horizontaly", |
| 542 | .minimum = 0, |
| 543 | .maximum = 1, |
| 544 | .step = 1, |
| 545 | .default_value = 0, |
| 546 | }, { /* gain = 1/32*val (=>gain=1 if val==32) */ |
| 547 | .id = V4L2_CID_GAIN, |
| 548 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 549 | .name = "Gain", |
| 550 | .minimum = 0, |
| 551 | .maximum = 63 * 2 * 2, |
| 552 | .step = 1, |
| 553 | .default_value = 32, |
| 554 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 555 | }, { |
| 556 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 557 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 558 | .name = "Auto Exposure", |
| 559 | .minimum = 0, |
| 560 | .maximum = 1, |
| 561 | .step = 1, |
| 562 | .default_value = 1, |
| 563 | } |
| 564 | }; |
| 565 | |
| 566 | static int mt9m111_video_probe(struct soc_camera_device *); |
| 567 | static void mt9m111_video_remove(struct soc_camera_device *); |
| 568 | static int mt9m111_get_control(struct soc_camera_device *, |
| 569 | struct v4l2_control *); |
| 570 | static int mt9m111_set_control(struct soc_camera_device *, |
| 571 | struct v4l2_control *); |
| 572 | static int mt9m111_resume(struct soc_camera_device *icd); |
| 573 | static int mt9m111_init(struct soc_camera_device *icd); |
| 574 | static int mt9m111_release(struct soc_camera_device *icd); |
| 575 | |
| 576 | static struct soc_camera_ops mt9m111_ops = { |
| 577 | .owner = THIS_MODULE, |
| 578 | .probe = mt9m111_video_probe, |
| 579 | .remove = mt9m111_video_remove, |
| 580 | .init = mt9m111_init, |
| 581 | .resume = mt9m111_resume, |
| 582 | .release = mt9m111_release, |
| 583 | .start_capture = mt9m111_start_capture, |
| 584 | .stop_capture = mt9m111_stop_capture, |
| 585 | .set_fmt_cap = mt9m111_set_fmt_cap, |
| 586 | .try_fmt_cap = mt9m111_try_fmt_cap, |
| 587 | .query_bus_param = mt9m111_query_bus_param, |
| 588 | .set_bus_param = mt9m111_set_bus_param, |
| 589 | .controls = mt9m111_controls, |
| 590 | .num_controls = ARRAY_SIZE(mt9m111_controls), |
| 591 | .get_control = mt9m111_get_control, |
| 592 | .set_control = mt9m111_set_control, |
| 593 | .get_chip_id = mt9m111_get_chip_id, |
| 594 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 595 | .get_register = mt9m111_get_register, |
| 596 | .set_register = mt9m111_set_register, |
| 597 | #endif |
| 598 | }; |
| 599 | |
| 600 | static int mt9m111_set_flip(struct soc_camera_device *icd, int flip, int mask) |
| 601 | { |
| 602 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 603 | int ret; |
| 604 | |
| 605 | if (mt9m111->context == HIGHPOWER) { |
| 606 | if (flip) |
| 607 | ret = reg_set(READ_MODE_B, mask); |
| 608 | else |
| 609 | ret = reg_clear(READ_MODE_B, mask); |
| 610 | } else { |
| 611 | if (flip) |
| 612 | ret = reg_set(READ_MODE_A, mask); |
| 613 | else |
| 614 | ret = reg_clear(READ_MODE_A, mask); |
| 615 | } |
| 616 | |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | static int mt9m111_get_global_gain(struct soc_camera_device *icd) |
| 621 | { |
| 622 | unsigned int data, gain; |
| 623 | |
| 624 | data = reg_read(GLOBAL_GAIN); |
| 625 | if (data >= 0) |
| 626 | gain = ((data & (1 << 10)) * 2) |
| 627 | | ((data & (1 << 9)) * 2) |
| 628 | | (data & 0x2f); |
| 629 | else |
| 630 | gain = data; |
| 631 | |
| 632 | return gain; |
| 633 | } |
| 634 | static int mt9m111_set_global_gain(struct soc_camera_device *icd, int gain) |
| 635 | { |
| 636 | u16 val; |
| 637 | |
| 638 | if (gain > 63 * 2 * 2) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | icd->gain = gain; |
| 642 | if ((gain >= 64 * 2) && (gain < 63 * 2 * 2)) |
| 643 | val = (1 << 10) | (1 << 9) | (gain / 4); |
| 644 | else if ((gain >= 64) && (gain < 64 * 2)) |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 645 | val = (1 << 9) | (gain / 2); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 646 | else |
| 647 | val = gain; |
| 648 | |
| 649 | return reg_write(GLOBAL_GAIN, val); |
| 650 | } |
| 651 | |
| 652 | static int mt9m111_set_autoexposure(struct soc_camera_device *icd, int on) |
| 653 | { |
| 654 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 655 | int ret; |
| 656 | |
| 657 | if (on) |
| 658 | ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 659 | else |
| 660 | ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 661 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 662 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 663 | mt9m111->autoexposure = on; |
| 664 | |
| 665 | return ret; |
| 666 | } |
| 667 | static int mt9m111_get_control(struct soc_camera_device *icd, |
| 668 | struct v4l2_control *ctrl) |
| 669 | { |
| 670 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 671 | int data; |
| 672 | |
| 673 | switch (ctrl->id) { |
| 674 | case V4L2_CID_VFLIP: |
| 675 | if (mt9m111->context == HIGHPOWER) |
| 676 | data = reg_read(READ_MODE_B); |
| 677 | else |
| 678 | data = reg_read(READ_MODE_A); |
| 679 | |
| 680 | if (data < 0) |
| 681 | return -EIO; |
| 682 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS); |
| 683 | break; |
| 684 | case V4L2_CID_HFLIP: |
| 685 | if (mt9m111->context == HIGHPOWER) |
| 686 | data = reg_read(READ_MODE_B); |
| 687 | else |
| 688 | data = reg_read(READ_MODE_A); |
| 689 | |
| 690 | if (data < 0) |
| 691 | return -EIO; |
| 692 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS); |
| 693 | break; |
| 694 | case V4L2_CID_GAIN: |
| 695 | data = mt9m111_get_global_gain(icd); |
| 696 | if (data < 0) |
| 697 | return data; |
| 698 | ctrl->value = data; |
| 699 | break; |
| 700 | case V4L2_CID_EXPOSURE_AUTO: |
| 701 | ctrl->value = mt9m111->autoexposure; |
| 702 | break; |
| 703 | } |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int mt9m111_set_control(struct soc_camera_device *icd, |
| 708 | struct v4l2_control *ctrl) |
| 709 | { |
| 710 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 711 | const struct v4l2_queryctrl *qctrl; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 712 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 713 | |
| 714 | qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id); |
| 715 | |
| 716 | if (!qctrl) |
| 717 | return -EINVAL; |
| 718 | |
| 719 | switch (ctrl->id) { |
| 720 | case V4L2_CID_VFLIP: |
| 721 | mt9m111->vflip = ctrl->value; |
| 722 | ret = mt9m111_set_flip(icd, ctrl->value, |
| 723 | MT9M111_RMB_MIRROR_ROWS); |
| 724 | break; |
| 725 | case V4L2_CID_HFLIP: |
| 726 | mt9m111->hflip = ctrl->value; |
| 727 | ret = mt9m111_set_flip(icd, ctrl->value, |
| 728 | MT9M111_RMB_MIRROR_COLS); |
| 729 | break; |
| 730 | case V4L2_CID_GAIN: |
| 731 | ret = mt9m111_set_global_gain(icd, ctrl->value); |
| 732 | break; |
| 733 | case V4L2_CID_EXPOSURE_AUTO: |
| 734 | ret = mt9m111_set_autoexposure(icd, ctrl->value); |
| 735 | break; |
| 736 | default: |
| 737 | ret = -EINVAL; |
| 738 | } |
| 739 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 740 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | int mt9m111_restore_state(struct soc_camera_device *icd) |
| 744 | { |
| 745 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 746 | |
| 747 | mt9m111_set_context(icd, mt9m111->context); |
| 748 | mt9m111_set_pixfmt(icd, mt9m111->pixfmt); |
| 749 | mt9m111_setup_rect(icd); |
| 750 | mt9m111_set_flip(icd, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS); |
| 751 | mt9m111_set_flip(icd, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS); |
| 752 | mt9m111_set_global_gain(icd, icd->gain); |
| 753 | mt9m111_set_autoexposure(icd, mt9m111->autoexposure); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int mt9m111_resume(struct soc_camera_device *icd) |
| 758 | { |
| 759 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 760 | int ret = 0; |
| 761 | |
| 762 | if (mt9m111->powered) { |
| 763 | ret = mt9m111_enable(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 764 | if (!ret) |
| 765 | ret = mt9m111_reset(icd); |
| 766 | if (!ret) |
| 767 | ret = mt9m111_restore_state(icd); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 768 | } |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | static int mt9m111_init(struct soc_camera_device *icd) |
| 773 | { |
| 774 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 775 | int ret; |
| 776 | |
| 777 | mt9m111->context = HIGHPOWER; |
| 778 | ret = mt9m111_enable(icd); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 779 | if (!ret) |
| 780 | ret = mt9m111_reset(icd); |
| 781 | if (!ret) |
| 782 | ret = mt9m111_set_context(icd, mt9m111->context); |
| 783 | if (!ret) |
| 784 | ret = mt9m111_set_autoexposure(icd, mt9m111->autoexposure); |
| 785 | if (ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 786 | dev_err(&icd->dev, "mt9m111 init failed: %d\n", ret); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 787 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static int mt9m111_release(struct soc_camera_device *icd) |
| 791 | { |
| 792 | int ret; |
| 793 | |
| 794 | ret = mt9m111_disable(icd); |
| 795 | if (ret < 0) |
| 796 | dev_err(&icd->dev, "mt9m111 release failed: %d\n", ret); |
| 797 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame^] | 798 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Interface active, can use i2c. If it fails, it can indeed mean, that |
| 803 | * this wasn't our capture interface, so, we wait for the right one |
| 804 | */ |
| 805 | static int mt9m111_video_probe(struct soc_camera_device *icd) |
| 806 | { |
| 807 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 808 | s32 data; |
| 809 | int ret; |
| 810 | |
| 811 | /* |
| 812 | * We must have a parent by now. And it cannot be a wrong one. |
| 813 | * So this entire test is completely redundant. |
| 814 | */ |
| 815 | if (!icd->dev.parent || |
| 816 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 817 | return -ENODEV; |
| 818 | |
| 819 | ret = mt9m111_enable(icd); |
| 820 | if (ret) |
| 821 | goto ei2c; |
| 822 | ret = mt9m111_reset(icd); |
| 823 | if (ret) |
| 824 | goto ei2c; |
| 825 | |
| 826 | data = reg_read(CHIP_VERSION); |
| 827 | |
| 828 | switch (data) { |
| 829 | case 0x143a: |
| 830 | mt9m111->model = V4L2_IDENT_MT9M111; |
| 831 | icd->formats = mt9m111_colour_formats; |
| 832 | icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats); |
| 833 | break; |
| 834 | default: |
| 835 | ret = -ENODEV; |
| 836 | dev_err(&icd->dev, |
| 837 | "No MT9M111 chip detected, register read %x\n", data); |
| 838 | goto ei2c; |
| 839 | } |
| 840 | |
| 841 | dev_info(&icd->dev, "Detected a MT9M111 chip ID 0x143a\n"); |
| 842 | |
| 843 | ret = soc_camera_video_start(icd); |
| 844 | if (ret) |
| 845 | goto eisis; |
| 846 | |
| 847 | mt9m111->autoexposure = 1; |
| 848 | |
| 849 | mt9m111->swap_rgb_even_odd = 1; |
| 850 | mt9m111->swap_rgb_red_blue = 1; |
| 851 | |
| 852 | return 0; |
| 853 | eisis: |
| 854 | ei2c: |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static void mt9m111_video_remove(struct soc_camera_device *icd) |
| 859 | { |
| 860 | struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd); |
| 861 | |
| 862 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m111->client->addr, |
| 863 | mt9m111->icd.dev.parent, mt9m111->icd.vdev); |
| 864 | soc_camera_video_stop(&mt9m111->icd); |
| 865 | } |
| 866 | |
| 867 | static int mt9m111_probe(struct i2c_client *client, |
| 868 | const struct i2c_device_id *did) |
| 869 | { |
| 870 | struct mt9m111 *mt9m111; |
| 871 | struct soc_camera_device *icd; |
| 872 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 873 | struct soc_camera_link *icl = client->dev.platform_data; |
| 874 | int ret; |
| 875 | |
| 876 | if (!icl) { |
| 877 | dev_err(&client->dev, "MT9M111 driver needs platform data\n"); |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
| 881 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 882 | dev_warn(&adapter->dev, |
| 883 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 884 | return -EIO; |
| 885 | } |
| 886 | |
| 887 | mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL); |
| 888 | if (!mt9m111) |
| 889 | return -ENOMEM; |
| 890 | |
| 891 | mt9m111->client = client; |
| 892 | i2c_set_clientdata(client, mt9m111); |
| 893 | |
| 894 | /* Second stage probe - when a capture adapter is there */ |
| 895 | icd = &mt9m111->icd; |
| 896 | icd->ops = &mt9m111_ops; |
| 897 | icd->control = &client->dev; |
| 898 | icd->x_min = MT9M111_MIN_DARK_COLS; |
| 899 | icd->y_min = MT9M111_MIN_DARK_ROWS; |
| 900 | icd->x_current = icd->x_min; |
| 901 | icd->y_current = icd->y_min; |
| 902 | icd->width_min = MT9M111_MIN_DARK_ROWS; |
| 903 | icd->width_max = MT9M111_MAX_WIDTH; |
| 904 | icd->height_min = MT9M111_MIN_DARK_COLS; |
| 905 | icd->height_max = MT9M111_MAX_HEIGHT; |
| 906 | icd->y_skip_top = 0; |
| 907 | icd->iface = icl->bus_id; |
| 908 | |
| 909 | ret = soc_camera_device_register(icd); |
| 910 | if (ret) |
| 911 | goto eisdr; |
| 912 | return 0; |
| 913 | |
| 914 | eisdr: |
| 915 | kfree(mt9m111); |
| 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | static int mt9m111_remove(struct i2c_client *client) |
| 920 | { |
| 921 | struct mt9m111 *mt9m111 = i2c_get_clientdata(client); |
| 922 | soc_camera_device_unregister(&mt9m111->icd); |
| 923 | kfree(mt9m111); |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | static const struct i2c_device_id mt9m111_id[] = { |
| 929 | { "mt9m111", 0 }, |
| 930 | { } |
| 931 | }; |
| 932 | MODULE_DEVICE_TABLE(i2c, mt9m111_id); |
| 933 | |
| 934 | static struct i2c_driver mt9m111_i2c_driver = { |
| 935 | .driver = { |
| 936 | .name = "mt9m111", |
| 937 | }, |
| 938 | .probe = mt9m111_probe, |
| 939 | .remove = mt9m111_remove, |
| 940 | .id_table = mt9m111_id, |
| 941 | }; |
| 942 | |
| 943 | static int __init mt9m111_mod_init(void) |
| 944 | { |
| 945 | return i2c_add_driver(&mt9m111_i2c_driver); |
| 946 | } |
| 947 | |
| 948 | static void __exit mt9m111_mod_exit(void) |
| 949 | { |
| 950 | i2c_del_driver(&mt9m111_i2c_driver); |
| 951 | } |
| 952 | |
| 953 | module_init(mt9m111_mod_init); |
| 954 | module_exit(mt9m111_mod_exit); |
| 955 | |
| 956 | MODULE_DESCRIPTION("Micron MT9M111 Camera driver"); |
| 957 | MODULE_AUTHOR("Robert Jarzmik"); |
| 958 | MODULE_LICENSE("GPL"); |