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 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 116 | #define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg) |
| 117 | #define reg_write(reg, val) mt9m111_reg_write(client, MT9M111_##reg, (val)) |
| 118 | #define reg_set(reg, val) mt9m111_reg_set(client, MT9M111_##reg, (val)) |
| 119 | #define reg_clear(reg, val) mt9m111_reg_clear(client, MT9M111_##reg, (val)) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 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 { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 151 | struct v4l2_subdev subdev; |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 152 | int model; /* V4L2_IDENT_MT9M11x* codes from v4l2-chip-ident.h */ |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 153 | enum mt9m111_context context; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 154 | struct v4l2_rect rect; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 155 | u32 pixfmt; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 156 | unsigned int gain; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 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 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 169 | static struct mt9m111 *to_mt9m111(const struct i2c_client *client) |
| 170 | { |
| 171 | return container_of(i2c_get_clientdata(client), struct mt9m111, subdev); |
| 172 | } |
| 173 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 174 | static int reg_page_map_set(struct i2c_client *client, const u16 reg) |
| 175 | { |
| 176 | int ret; |
| 177 | u16 page; |
| 178 | static int lastpage = -1; /* PageMap cache value */ |
| 179 | |
| 180 | page = (reg >> 8); |
| 181 | if (page == lastpage) |
| 182 | return 0; |
| 183 | if (page > 2) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page)); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 187 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 188 | lastpage = page; |
| 189 | return ret; |
| 190 | } |
| 191 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 192 | static int mt9m111_reg_read(struct i2c_client *client, const u16 reg) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 193 | { |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 194 | int ret; |
| 195 | |
| 196 | ret = reg_page_map_set(client, reg); |
| 197 | if (!ret) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 198 | ret = swab16(i2c_smbus_read_word_data(client, reg & 0xff)); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 199 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 200 | dev_dbg(&client->dev, "read reg.%03x -> %04x\n", reg, ret); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 201 | return ret; |
| 202 | } |
| 203 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 204 | static int mt9m111_reg_write(struct i2c_client *client, const u16 reg, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 205 | const u16 data) |
| 206 | { |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 207 | int ret; |
| 208 | |
| 209 | ret = reg_page_map_set(client, reg); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 210 | if (!ret) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 211 | ret = i2c_smbus_write_word_data(client, reg & 0xff, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 212 | swab16(data)); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 213 | dev_dbg(&client->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 214 | return ret; |
| 215 | } |
| 216 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 217 | static int mt9m111_reg_set(struct i2c_client *client, const u16 reg, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 218 | const u16 data) |
| 219 | { |
| 220 | int ret; |
| 221 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 222 | ret = mt9m111_reg_read(client, reg); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 223 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 224 | ret = mt9m111_reg_write(client, reg, ret | data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 225 | return ret; |
| 226 | } |
| 227 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 228 | static int mt9m111_reg_clear(struct i2c_client *client, const u16 reg, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 229 | const u16 data) |
| 230 | { |
| 231 | int ret; |
| 232 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 233 | ret = mt9m111_reg_read(client, reg); |
| 234 | return mt9m111_reg_write(client, reg, ret & ~data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 235 | } |
| 236 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 237 | static int mt9m111_set_context(struct i2c_client *client, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 238 | enum mt9m111_context ctxt) |
| 239 | { |
| 240 | int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B |
| 241 | | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B |
| 242 | | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B |
| 243 | | MT9M111_CTXT_CTRL_VBLANK_SEL_B |
| 244 | | MT9M111_CTXT_CTRL_HBLANK_SEL_B; |
| 245 | int valA = MT9M111_CTXT_CTRL_RESTART; |
| 246 | |
| 247 | if (ctxt == HIGHPOWER) |
| 248 | return reg_write(CONTEXT_CONTROL, valB); |
| 249 | else |
| 250 | return reg_write(CONTEXT_CONTROL, valA); |
| 251 | } |
| 252 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 253 | static int mt9m111_setup_rect(struct i2c_client *client, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 254 | struct v4l2_rect *rect) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 255 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 256 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 257 | int ret, is_raw_format; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 258 | int width = rect->width; |
| 259 | int height = rect->height; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 260 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 261 | if (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8 || |
| 262 | mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 263 | is_raw_format = 1; |
| 264 | else |
| 265 | is_raw_format = 0; |
| 266 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 267 | ret = reg_write(COLUMN_START, rect->left); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 268 | if (!ret) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 269 | ret = reg_write(ROW_START, rect->top); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 270 | |
| 271 | if (is_raw_format) { |
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_WIDTH, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 274 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 275 | ret = reg_write(WINDOW_HEIGHT, height); |
| 276 | } else { |
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_B, 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_B, 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_B, 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_B, height); |
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_XZOOM_A, MT9M111_MAX_WIDTH); |
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_YZOOM_A, MT9M111_MAX_HEIGHT); |
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_XSIZE_A, width); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 291 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 292 | ret = reg_write(REDUCER_YSIZE_A, height); |
| 293 | } |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 298 | static int mt9m111_setup_pixfmt(struct i2c_client *client, u16 outfmt) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 299 | { |
| 300 | int ret; |
| 301 | |
| 302 | ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 303 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 304 | ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt); |
| 305 | return ret; |
| 306 | } |
| 307 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 308 | static int mt9m111_setfmt_bayer8(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 309 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 310 | return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_PROCESSED_BAYER); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 311 | } |
| 312 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 313 | static int mt9m111_setfmt_bayer10(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 314 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 315 | return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_BYPASS_IFP); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 316 | } |
| 317 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 318 | static int mt9m111_setfmt_rgb565(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 319 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 320 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 321 | int val = 0; |
| 322 | |
| 323 | if (mt9m111->swap_rgb_red_blue) |
| 324 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 325 | if (mt9m111->swap_rgb_even_odd) |
| 326 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 327 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565; |
| 328 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 329 | return mt9m111_setup_pixfmt(client, val); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 330 | } |
| 331 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 332 | static int mt9m111_setfmt_rgb555(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 333 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 334 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 335 | int val = 0; |
| 336 | |
| 337 | if (mt9m111->swap_rgb_red_blue) |
| 338 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 339 | if (mt9m111->swap_rgb_even_odd) |
| 340 | val |= MT9M111_OUTFMT_SWAP_RGB_EVEN; |
| 341 | val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555; |
| 342 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 343 | return mt9m111_setup_pixfmt(client, val); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 344 | } |
| 345 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 346 | static int mt9m111_setfmt_yuv(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 347 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 348 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 349 | int val = 0; |
| 350 | |
| 351 | if (mt9m111->swap_yuv_cb_cr) |
| 352 | val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr; |
| 353 | if (mt9m111->swap_yuv_y_chromas) |
| 354 | val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y; |
| 355 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 356 | return mt9m111_setup_pixfmt(client, val); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 357 | } |
| 358 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 359 | static int mt9m111_enable(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 360 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 361 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 362 | int ret; |
| 363 | |
| 364 | ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 365 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 366 | mt9m111->powered = 1; |
| 367 | return ret; |
| 368 | } |
| 369 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 370 | static int mt9m111_reset(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 371 | { |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 372 | int ret; |
| 373 | |
| 374 | ret = reg_set(RESET, MT9M111_RESET_RESET_MODE); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 375 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 376 | ret = reg_set(RESET, MT9M111_RESET_RESET_SOC); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 377 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 378 | ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE |
| 379 | | MT9M111_RESET_RESET_SOC); |
Antonio Ospite | afb1368 | 2009-02-23 12:13:24 -0300 | [diff] [blame] | 380 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 381 | return ret; |
| 382 | } |
| 383 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 384 | static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd) |
| 385 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 386 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 387 | unsigned long flags = SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 388 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
Guennadi Liakhovetski | 2d9329f | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 389 | SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8; |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 390 | |
| 391 | return soc_camera_apply_sensor_flags(icl, flags); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f) |
| 395 | { |
| 396 | return 0; |
| 397 | } |
| 398 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 399 | static int mt9m111_make_rect(struct i2c_client *client, |
| 400 | struct v4l2_rect *rect) |
| 401 | { |
| 402 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
| 403 | |
| 404 | if (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8 || |
| 405 | mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16) { |
| 406 | /* Bayer format - even size lengths */ |
| 407 | rect->width = ALIGN(rect->width, 2); |
| 408 | rect->height = ALIGN(rect->height, 2); |
| 409 | /* Let the user play with the starting pixel */ |
| 410 | } |
| 411 | |
| 412 | /* FIXME: the datasheet doesn't specify minimum sizes */ |
| 413 | soc_camera_limit_side(&rect->left, &rect->width, |
| 414 | MT9M111_MIN_DARK_COLS, 2, MT9M111_MAX_WIDTH); |
| 415 | |
| 416 | soc_camera_limit_side(&rect->top, &rect->height, |
| 417 | MT9M111_MIN_DARK_ROWS, 2, MT9M111_MAX_HEIGHT); |
| 418 | |
| 419 | return mt9m111_setup_rect(client, rect); |
| 420 | } |
| 421 | |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 422 | static int mt9m111_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 423 | { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 424 | struct v4l2_rect rect = a->c; |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 425 | struct i2c_client *client = sd->priv; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 426 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 427 | int ret; |
| 428 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 429 | dev_dbg(&client->dev, "%s left=%d, top=%d, width=%d, height=%d\n", |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 430 | __func__, rect.left, rect.top, rect.width, rect.height); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 431 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 432 | ret = mt9m111_make_rect(client, &rect); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 433 | if (!ret) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 434 | mt9m111->rect = rect; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 435 | return ret; |
| 436 | } |
| 437 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 438 | static int mt9m111_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
| 439 | { |
| 440 | struct i2c_client *client = sd->priv; |
| 441 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
| 442 | |
| 443 | a->c = mt9m111->rect; |
| 444 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int mt9m111_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a) |
| 450 | { |
| 451 | a->bounds.left = MT9M111_MIN_DARK_COLS; |
| 452 | a->bounds.top = MT9M111_MIN_DARK_ROWS; |
| 453 | a->bounds.width = MT9M111_MAX_WIDTH; |
| 454 | a->bounds.height = MT9M111_MAX_HEIGHT; |
| 455 | a->defrect = a->bounds; |
| 456 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 457 | a->pixelaspect.numerator = 1; |
| 458 | a->pixelaspect.denominator = 1; |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static int mt9m111_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) |
| 464 | { |
| 465 | struct i2c_client *client = sd->priv; |
| 466 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
| 467 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 468 | |
| 469 | pix->width = mt9m111->rect.width; |
| 470 | pix->height = mt9m111->rect.height; |
| 471 | pix->pixelformat = mt9m111->pixfmt; |
| 472 | pix->field = V4L2_FIELD_NONE; |
| 473 | pix->colorspace = V4L2_COLORSPACE_SRGB; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 478 | static int mt9m111_set_pixfmt(struct i2c_client *client, u32 pixfmt) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 479 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 480 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 481 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 482 | |
| 483 | switch (pixfmt) { |
| 484 | case V4L2_PIX_FMT_SBGGR8: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 485 | ret = mt9m111_setfmt_bayer8(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 486 | break; |
| 487 | case V4L2_PIX_FMT_SBGGR16: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 488 | ret = mt9m111_setfmt_bayer10(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 489 | break; |
| 490 | case V4L2_PIX_FMT_RGB555: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 491 | ret = mt9m111_setfmt_rgb555(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 492 | break; |
| 493 | case V4L2_PIX_FMT_RGB565: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 494 | ret = mt9m111_setfmt_rgb565(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 495 | break; |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 496 | case V4L2_PIX_FMT_UYVY: |
| 497 | mt9m111->swap_yuv_y_chromas = 0; |
| 498 | mt9m111->swap_yuv_cb_cr = 0; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 499 | ret = mt9m111_setfmt_yuv(client); |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 500 | break; |
| 501 | case V4L2_PIX_FMT_VYUY: |
| 502 | mt9m111->swap_yuv_y_chromas = 0; |
| 503 | mt9m111->swap_yuv_cb_cr = 1; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 504 | ret = mt9m111_setfmt_yuv(client); |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 505 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 506 | case V4L2_PIX_FMT_YUYV: |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 507 | mt9m111->swap_yuv_y_chromas = 1; |
| 508 | mt9m111->swap_yuv_cb_cr = 0; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 509 | ret = mt9m111_setfmt_yuv(client); |
Robert Jarzmik | 88f4b89 | 2008-12-17 14:05:31 -0300 | [diff] [blame] | 510 | break; |
| 511 | case V4L2_PIX_FMT_YVYU: |
| 512 | mt9m111->swap_yuv_y_chromas = 1; |
| 513 | mt9m111->swap_yuv_cb_cr = 1; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 514 | ret = mt9m111_setfmt_yuv(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 515 | break; |
| 516 | default: |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 517 | dev_err(&client->dev, "Pixel format not handled : %x\n", |
| 518 | pixfmt); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 519 | ret = -EINVAL; |
| 520 | } |
| 521 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 522 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 523 | mt9m111->pixfmt = pixfmt; |
| 524 | |
| 525 | return ret; |
| 526 | } |
| 527 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 528 | static int mt9m111_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 529 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 530 | struct i2c_client *client = sd->priv; |
| 531 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 532 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 533 | struct v4l2_rect rect = { |
| 534 | .left = mt9m111->rect.left, |
| 535 | .top = mt9m111->rect.top, |
| 536 | .width = pix->width, |
| 537 | .height = pix->height, |
| 538 | }; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 539 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 540 | |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 541 | dev_dbg(&client->dev, |
| 542 | "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n", __func__, |
| 543 | pix->pixelformat, rect.left, rect.top, rect.width, rect.height); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 544 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 545 | ret = mt9m111_make_rect(client, &rect); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 546 | if (!ret) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 547 | ret = mt9m111_set_pixfmt(client, pix->pixelformat); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 548 | if (!ret) |
| 549 | mt9m111->rect = rect; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 550 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 551 | } |
| 552 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 553 | static int mt9m111_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 554 | { |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 555 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 556 | bool bayer = pix->pixelformat == V4L2_PIX_FMT_SBGGR8 || |
| 557 | pix->pixelformat == V4L2_PIX_FMT_SBGGR16; |
| 558 | |
| 559 | /* |
| 560 | * With Bayer format enforce even side lengths, but let the user play |
| 561 | * with the starting pixel |
| 562 | */ |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 563 | |
| 564 | if (pix->height > MT9M111_MAX_HEIGHT) |
| 565 | pix->height = MT9M111_MAX_HEIGHT; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 566 | else if (pix->height < 2) |
| 567 | pix->height = 2; |
| 568 | else if (bayer) |
| 569 | pix->height = ALIGN(pix->height, 2); |
| 570 | |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 571 | if (pix->width > MT9M111_MAX_WIDTH) |
| 572 | pix->width = MT9M111_MAX_WIDTH; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 573 | else if (pix->width < 2) |
| 574 | pix->width = 2; |
| 575 | else if (bayer) |
| 576 | pix->width = ALIGN(pix->width, 2); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 581 | static int mt9m111_g_chip_ident(struct v4l2_subdev *sd, |
| 582 | struct v4l2_dbg_chip_ident *id) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 583 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 584 | struct i2c_client *client = sd->priv; |
| 585 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 586 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 587 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 588 | return -EINVAL; |
| 589 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 590 | if (id->match.addr != client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 591 | return -ENODEV; |
| 592 | |
| 593 | id->ident = mt9m111->model; |
| 594 | id->revision = 0; |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 600 | static int mt9m111_g_register(struct v4l2_subdev *sd, |
| 601 | struct v4l2_dbg_register *reg) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 602 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 603 | struct i2c_client *client = sd->priv; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 604 | int val; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 605 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 606 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 607 | return -EINVAL; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 608 | if (reg->match.addr != client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 609 | return -ENODEV; |
| 610 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 611 | val = mt9m111_reg_read(client, reg->reg); |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 612 | reg->size = 2; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 613 | reg->val = (u64)val; |
| 614 | |
| 615 | if (reg->val > 0xffff) |
| 616 | return -EIO; |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 621 | static int mt9m111_s_register(struct v4l2_subdev *sd, |
| 622 | struct v4l2_dbg_register *reg) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 623 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 624 | struct i2c_client *client = sd->priv; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 625 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 626 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 627 | return -EINVAL; |
| 628 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 629 | if (reg->match.addr != client->addr) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 630 | return -ENODEV; |
| 631 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 632 | if (mt9m111_reg_write(client, reg->reg, reg->val) < 0) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 633 | return -EIO; |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | #endif |
| 638 | |
| 639 | static const struct v4l2_queryctrl mt9m111_controls[] = { |
| 640 | { |
| 641 | .id = V4L2_CID_VFLIP, |
| 642 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 643 | .name = "Flip Verticaly", |
| 644 | .minimum = 0, |
| 645 | .maximum = 1, |
| 646 | .step = 1, |
| 647 | .default_value = 0, |
| 648 | }, { |
| 649 | .id = V4L2_CID_HFLIP, |
| 650 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 651 | .name = "Flip Horizontaly", |
| 652 | .minimum = 0, |
| 653 | .maximum = 1, |
| 654 | .step = 1, |
| 655 | .default_value = 0, |
| 656 | }, { /* gain = 1/32*val (=>gain=1 if val==32) */ |
| 657 | .id = V4L2_CID_GAIN, |
| 658 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 659 | .name = "Gain", |
| 660 | .minimum = 0, |
| 661 | .maximum = 63 * 2 * 2, |
| 662 | .step = 1, |
| 663 | .default_value = 32, |
| 664 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 665 | }, { |
| 666 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 667 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 668 | .name = "Auto Exposure", |
| 669 | .minimum = 0, |
| 670 | .maximum = 1, |
| 671 | .step = 1, |
| 672 | .default_value = 1, |
| 673 | } |
| 674 | }; |
| 675 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 676 | static int mt9m111_resume(struct soc_camera_device *icd); |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 677 | static int mt9m111_suspend(struct soc_camera_device *icd, pm_message_t state); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 678 | |
| 679 | static struct soc_camera_ops mt9m111_ops = { |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 680 | .suspend = mt9m111_suspend, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 681 | .resume = mt9m111_resume, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 682 | .query_bus_param = mt9m111_query_bus_param, |
| 683 | .set_bus_param = mt9m111_set_bus_param, |
| 684 | .controls = mt9m111_controls, |
| 685 | .num_controls = ARRAY_SIZE(mt9m111_controls), |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 686 | }; |
| 687 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 688 | static int mt9m111_set_flip(struct i2c_client *client, int flip, int mask) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 689 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 690 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 691 | int ret; |
| 692 | |
| 693 | if (mt9m111->context == HIGHPOWER) { |
| 694 | if (flip) |
| 695 | ret = reg_set(READ_MODE_B, mask); |
| 696 | else |
| 697 | ret = reg_clear(READ_MODE_B, mask); |
| 698 | } else { |
| 699 | if (flip) |
| 700 | ret = reg_set(READ_MODE_A, mask); |
| 701 | else |
| 702 | ret = reg_clear(READ_MODE_A, mask); |
| 703 | } |
| 704 | |
| 705 | return ret; |
| 706 | } |
| 707 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 708 | static int mt9m111_get_global_gain(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 709 | { |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 710 | int data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 711 | |
| 712 | data = reg_read(GLOBAL_GAIN); |
| 713 | if (data >= 0) |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 714 | return (data & 0x2f) * (1 << ((data >> 10) & 1)) * |
| 715 | (1 << ((data >> 9) & 1)); |
| 716 | return data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 717 | } |
roel kluin | 0f28b79 | 2008-12-17 14:01:07 -0300 | [diff] [blame] | 718 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 719 | static int mt9m111_set_global_gain(struct i2c_client *client, int gain) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 720 | { |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 721 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 722 | u16 val; |
| 723 | |
| 724 | if (gain > 63 * 2 * 2) |
| 725 | return -EINVAL; |
| 726 | |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 727 | mt9m111->gain = gain; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 728 | if ((gain >= 64 * 2) && (gain < 63 * 2 * 2)) |
| 729 | val = (1 << 10) | (1 << 9) | (gain / 4); |
| 730 | else if ((gain >= 64) && (gain < 64 * 2)) |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 731 | val = (1 << 9) | (gain / 2); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 732 | else |
| 733 | val = gain; |
| 734 | |
| 735 | return reg_write(GLOBAL_GAIN, val); |
| 736 | } |
| 737 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 738 | static int mt9m111_set_autoexposure(struct i2c_client *client, int on) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 739 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 740 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 741 | int ret; |
| 742 | |
| 743 | if (on) |
| 744 | ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 745 | else |
| 746 | ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN); |
| 747 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 748 | if (!ret) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 749 | mt9m111->autoexposure = on; |
| 750 | |
| 751 | return ret; |
| 752 | } |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 753 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 754 | static int mt9m111_set_autowhitebalance(struct i2c_client *client, int on) |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 755 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 756 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 757 | int ret; |
| 758 | |
| 759 | if (on) |
| 760 | ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN); |
| 761 | else |
| 762 | ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN); |
| 763 | |
| 764 | if (!ret) |
| 765 | mt9m111->autowhitebalance = on; |
| 766 | |
| 767 | return ret; |
| 768 | } |
| 769 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 770 | static int mt9m111_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 771 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 772 | struct i2c_client *client = sd->priv; |
| 773 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 774 | int data; |
| 775 | |
| 776 | switch (ctrl->id) { |
| 777 | case V4L2_CID_VFLIP: |
| 778 | if (mt9m111->context == HIGHPOWER) |
| 779 | data = reg_read(READ_MODE_B); |
| 780 | else |
| 781 | data = reg_read(READ_MODE_A); |
| 782 | |
| 783 | if (data < 0) |
| 784 | return -EIO; |
| 785 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS); |
| 786 | break; |
| 787 | case V4L2_CID_HFLIP: |
| 788 | if (mt9m111->context == HIGHPOWER) |
| 789 | data = reg_read(READ_MODE_B); |
| 790 | else |
| 791 | data = reg_read(READ_MODE_A); |
| 792 | |
| 793 | if (data < 0) |
| 794 | return -EIO; |
| 795 | ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS); |
| 796 | break; |
| 797 | case V4L2_CID_GAIN: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 798 | data = mt9m111_get_global_gain(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 799 | if (data < 0) |
| 800 | return data; |
| 801 | ctrl->value = data; |
| 802 | break; |
| 803 | case V4L2_CID_EXPOSURE_AUTO: |
| 804 | ctrl->value = mt9m111->autoexposure; |
| 805 | break; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 806 | case V4L2_CID_AUTO_WHITE_BALANCE: |
| 807 | ctrl->value = mt9m111->autowhitebalance; |
| 808 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 809 | } |
| 810 | return 0; |
| 811 | } |
| 812 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 813 | static int mt9m111_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 814 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 815 | struct i2c_client *client = sd->priv; |
| 816 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 817 | const struct v4l2_queryctrl *qctrl; |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 818 | int ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 819 | |
| 820 | qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 821 | if (!qctrl) |
| 822 | return -EINVAL; |
| 823 | |
| 824 | switch (ctrl->id) { |
| 825 | case V4L2_CID_VFLIP: |
| 826 | mt9m111->vflip = ctrl->value; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 827 | ret = mt9m111_set_flip(client, ctrl->value, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 828 | MT9M111_RMB_MIRROR_ROWS); |
| 829 | break; |
| 830 | case V4L2_CID_HFLIP: |
| 831 | mt9m111->hflip = ctrl->value; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 832 | ret = mt9m111_set_flip(client, ctrl->value, |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 833 | MT9M111_RMB_MIRROR_COLS); |
| 834 | break; |
| 835 | case V4L2_CID_GAIN: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 836 | ret = mt9m111_set_global_gain(client, ctrl->value); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 837 | break; |
| 838 | case V4L2_CID_EXPOSURE_AUTO: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 839 | ret = mt9m111_set_autoexposure(client, ctrl->value); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 840 | break; |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 841 | case V4L2_CID_AUTO_WHITE_BALANCE: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 842 | ret = mt9m111_set_autowhitebalance(client, ctrl->value); |
Robert Jarzmik | 39bf372 | 2008-12-18 11:29:05 -0300 | [diff] [blame] | 843 | break; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 844 | default: |
| 845 | ret = -EINVAL; |
| 846 | } |
| 847 | |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 848 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 849 | } |
| 850 | |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 851 | static int mt9m111_suspend(struct soc_camera_device *icd, pm_message_t state) |
| 852 | { |
| 853 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
| 854 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
| 855 | |
| 856 | mt9m111->gain = mt9m111_get_global_gain(client); |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 861 | static int mt9m111_restore_state(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 862 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 863 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 864 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 865 | mt9m111_set_context(client, mt9m111->context); |
| 866 | mt9m111_set_pixfmt(client, mt9m111->pixfmt); |
| 867 | mt9m111_setup_rect(client, &mt9m111->rect); |
| 868 | mt9m111_set_flip(client, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS); |
| 869 | mt9m111_set_flip(client, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS); |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 870 | mt9m111_set_global_gain(client, mt9m111->gain); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 871 | mt9m111_set_autoexposure(client, mt9m111->autoexposure); |
| 872 | mt9m111_set_autowhitebalance(client, mt9m111->autowhitebalance); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | static int mt9m111_resume(struct soc_camera_device *icd) |
| 877 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 878 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 879 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 880 | int ret = 0; |
| 881 | |
| 882 | if (mt9m111->powered) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 883 | ret = mt9m111_enable(client); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 884 | if (!ret) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 885 | ret = mt9m111_reset(client); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 886 | if (!ret) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 887 | ret = mt9m111_restore_state(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 888 | } |
| 889 | return ret; |
| 890 | } |
| 891 | |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 892 | static int mt9m111_init(struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 893 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 894 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 895 | int ret; |
| 896 | |
| 897 | mt9m111->context = HIGHPOWER; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 898 | ret = mt9m111_enable(client); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 899 | if (!ret) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 900 | ret = mt9m111_reset(client); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 901 | if (!ret) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 902 | ret = mt9m111_set_context(client, mt9m111->context); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 903 | if (!ret) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 904 | ret = mt9m111_set_autoexposure(client, mt9m111->autoexposure); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 905 | if (ret) |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 906 | dev_err(&client->dev, "mt9m11x init failed: %d\n", ret); |
Guennadi Liakhovetski | 506c629 | 2008-08-14 12:03:49 -0300 | [diff] [blame] | 907 | return ret; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 908 | } |
| 909 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 910 | /* |
| 911 | * Interface active, can use i2c. If it fails, it can indeed mean, that |
| 912 | * this wasn't our capture interface, so, we wait for the right one |
| 913 | */ |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 914 | static int mt9m111_video_probe(struct soc_camera_device *icd, |
| 915 | struct i2c_client *client) |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 916 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 917 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 918 | s32 data; |
| 919 | int ret; |
| 920 | |
| 921 | /* |
| 922 | * We must have a parent by now. And it cannot be a wrong one. |
| 923 | * So this entire test is completely redundant. |
| 924 | */ |
| 925 | if (!icd->dev.parent || |
| 926 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 927 | return -ENODEV; |
| 928 | |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 929 | mt9m111->autoexposure = 1; |
| 930 | mt9m111->autowhitebalance = 1; |
| 931 | |
| 932 | mt9m111->swap_rgb_even_odd = 1; |
| 933 | mt9m111->swap_rgb_red_blue = 1; |
| 934 | |
| 935 | ret = mt9m111_init(client); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 936 | if (ret) |
| 937 | goto ei2c; |
| 938 | |
| 939 | data = reg_read(CHIP_VERSION); |
| 940 | |
| 941 | switch (data) { |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 942 | case 0x143a: /* MT9M111 */ |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 943 | mt9m111->model = V4L2_IDENT_MT9M111; |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 944 | break; |
| 945 | case 0x148c: /* MT9M112 */ |
| 946 | mt9m111->model = V4L2_IDENT_MT9M112; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 947 | break; |
| 948 | default: |
| 949 | ret = -ENODEV; |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 950 | dev_err(&client->dev, |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 951 | "No MT9M11x chip detected, register read %x\n", data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 952 | goto ei2c; |
| 953 | } |
| 954 | |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 955 | icd->formats = mt9m111_colour_formats; |
| 956 | icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats); |
| 957 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 958 | dev_info(&client->dev, "Detected a MT9M11x chip ID %x\n", data); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 959 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 960 | ei2c: |
| 961 | return ret; |
| 962 | } |
| 963 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 964 | static struct v4l2_subdev_core_ops mt9m111_subdev_core_ops = { |
| 965 | .g_ctrl = mt9m111_g_ctrl, |
| 966 | .s_ctrl = mt9m111_s_ctrl, |
| 967 | .g_chip_ident = mt9m111_g_chip_ident, |
| 968 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 969 | .g_register = mt9m111_g_register, |
| 970 | .s_register = mt9m111_s_register, |
| 971 | #endif |
| 972 | }; |
| 973 | |
| 974 | static struct v4l2_subdev_video_ops mt9m111_subdev_video_ops = { |
| 975 | .s_fmt = mt9m111_s_fmt, |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 976 | .g_fmt = mt9m111_g_fmt, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 977 | .try_fmt = mt9m111_try_fmt, |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 978 | .s_crop = mt9m111_s_crop, |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 979 | .g_crop = mt9m111_g_crop, |
| 980 | .cropcap = mt9m111_cropcap, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 981 | }; |
| 982 | |
| 983 | static struct v4l2_subdev_ops mt9m111_subdev_ops = { |
| 984 | .core = &mt9m111_subdev_core_ops, |
| 985 | .video = &mt9m111_subdev_video_ops, |
| 986 | }; |
| 987 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 988 | static int mt9m111_probe(struct i2c_client *client, |
| 989 | const struct i2c_device_id *did) |
| 990 | { |
| 991 | struct mt9m111 *mt9m111; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 992 | struct soc_camera_device *icd = client->dev.platform_data; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 993 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 994 | struct soc_camera_link *icl; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 995 | int ret; |
| 996 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 997 | if (!icd) { |
| 998 | dev_err(&client->dev, "MT9M11x: missing soc-camera data!\n"); |
| 999 | return -EINVAL; |
| 1000 | } |
| 1001 | |
| 1002 | icl = to_soc_camera_link(icd); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1003 | if (!icl) { |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 1004 | dev_err(&client->dev, "MT9M11x driver needs platform data\n"); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | |
| 1008 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 1009 | dev_warn(&adapter->dev, |
| 1010 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 1011 | return -EIO; |
| 1012 | } |
| 1013 | |
| 1014 | mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL); |
| 1015 | if (!mt9m111) |
| 1016 | return -ENOMEM; |
| 1017 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1018 | v4l2_i2c_subdev_init(&mt9m111->subdev, client, &mt9m111_subdev_ops); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1019 | |
| 1020 | /* Second stage probe - when a capture adapter is there */ |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 1021 | icd->ops = &mt9m111_ops; |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 1022 | icd->y_skip_top = 0; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1023 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1024 | mt9m111->rect.left = MT9M111_MIN_DARK_COLS; |
| 1025 | mt9m111->rect.top = MT9M111_MIN_DARK_ROWS; |
| 1026 | mt9m111->rect.width = MT9M111_MAX_WIDTH; |
| 1027 | mt9m111->rect.height = MT9M111_MAX_HEIGHT; |
| 1028 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1029 | ret = mt9m111_video_probe(icd, client); |
| 1030 | if (ret) { |
| 1031 | icd->ops = NULL; |
| 1032 | i2c_set_clientdata(client, NULL); |
| 1033 | kfree(mt9m111); |
| 1034 | } |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1035 | |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1036 | return ret; |
| 1037 | } |
| 1038 | |
| 1039 | static int mt9m111_remove(struct i2c_client *client) |
| 1040 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1041 | struct mt9m111 *mt9m111 = to_mt9m111(client); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1042 | struct soc_camera_device *icd = client->dev.platform_data; |
| 1043 | |
| 1044 | icd->ops = NULL; |
| 1045 | i2c_set_clientdata(client, NULL); |
| 1046 | client->driver = NULL; |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1047 | kfree(mt9m111); |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static const struct i2c_device_id mt9m111_id[] = { |
| 1053 | { "mt9m111", 0 }, |
| 1054 | { } |
| 1055 | }; |
| 1056 | MODULE_DEVICE_TABLE(i2c, mt9m111_id); |
| 1057 | |
| 1058 | static struct i2c_driver mt9m111_i2c_driver = { |
| 1059 | .driver = { |
| 1060 | .name = "mt9m111", |
| 1061 | }, |
| 1062 | .probe = mt9m111_probe, |
| 1063 | .remove = mt9m111_remove, |
| 1064 | .id_table = mt9m111_id, |
| 1065 | }; |
| 1066 | |
| 1067 | static int __init mt9m111_mod_init(void) |
| 1068 | { |
| 1069 | return i2c_add_driver(&mt9m111_i2c_driver); |
| 1070 | } |
| 1071 | |
| 1072 | static void __exit mt9m111_mod_exit(void) |
| 1073 | { |
| 1074 | i2c_del_driver(&mt9m111_i2c_driver); |
| 1075 | } |
| 1076 | |
| 1077 | module_init(mt9m111_mod_init); |
| 1078 | module_exit(mt9m111_mod_exit); |
| 1079 | |
Mike Rapoport | d7f83a5 | 2008-12-18 11:44:15 -0300 | [diff] [blame] | 1080 | MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver"); |
Robert Jarzmik | 77110ab | 2008-08-14 12:02:51 -0300 | [diff] [blame] | 1081 | MODULE_AUTHOR("Robert Jarzmik"); |
| 1082 | MODULE_LICENSE("GPL"); |