Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9T031 CMOS Image Sensor from Micron |
| 3 | * |
| 4 | * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.de> |
| 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 | |
| 11 | #include <linux/videodev2.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/log2.h> |
| 15 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 16 | #include <media/v4l2-subdev.h> |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 17 | #include <media/v4l2-chip-ident.h> |
| 18 | #include <media/soc_camera.h> |
| 19 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 20 | /* |
| 21 | * mt9t031 i2c address 0x5d |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 22 | * The platform has to define i2c_board_info and link to it from |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 23 | * struct soc_camera_link |
| 24 | */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 25 | |
| 26 | /* mt9t031 selected register addresses */ |
| 27 | #define MT9T031_CHIP_VERSION 0x00 |
| 28 | #define MT9T031_ROW_START 0x01 |
| 29 | #define MT9T031_COLUMN_START 0x02 |
| 30 | #define MT9T031_WINDOW_HEIGHT 0x03 |
| 31 | #define MT9T031_WINDOW_WIDTH 0x04 |
| 32 | #define MT9T031_HORIZONTAL_BLANKING 0x05 |
| 33 | #define MT9T031_VERTICAL_BLANKING 0x06 |
| 34 | #define MT9T031_OUTPUT_CONTROL 0x07 |
| 35 | #define MT9T031_SHUTTER_WIDTH_UPPER 0x08 |
| 36 | #define MT9T031_SHUTTER_WIDTH 0x09 |
| 37 | #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a |
| 38 | #define MT9T031_FRAME_RESTART 0x0b |
| 39 | #define MT9T031_SHUTTER_DELAY 0x0c |
| 40 | #define MT9T031_RESET 0x0d |
| 41 | #define MT9T031_READ_MODE_1 0x1e |
| 42 | #define MT9T031_READ_MODE_2 0x20 |
| 43 | #define MT9T031_READ_MODE_3 0x21 |
| 44 | #define MT9T031_ROW_ADDRESS_MODE 0x22 |
| 45 | #define MT9T031_COLUMN_ADDRESS_MODE 0x23 |
| 46 | #define MT9T031_GLOBAL_GAIN 0x35 |
| 47 | #define MT9T031_CHIP_ENABLE 0xF8 |
| 48 | |
| 49 | #define MT9T031_MAX_HEIGHT 1536 |
| 50 | #define MT9T031_MAX_WIDTH 2048 |
| 51 | #define MT9T031_MIN_HEIGHT 2 |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 52 | #define MT9T031_MIN_WIDTH 18 |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 53 | #define MT9T031_HORIZONTAL_BLANK 142 |
| 54 | #define MT9T031_VERTICAL_BLANK 25 |
| 55 | #define MT9T031_COLUMN_SKIP 32 |
| 56 | #define MT9T031_ROW_SKIP 20 |
| 57 | |
| 58 | #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \ |
| 59 | SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \ |
| 60 | SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \ |
| 61 | SOCAM_MASTER | SOCAM_DATAWIDTH_10) |
| 62 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 63 | struct mt9t031 { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 64 | struct v4l2_subdev subdev; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 65 | struct v4l2_rect rect; /* Sensor window */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 66 | int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 67 | u16 xskip; |
| 68 | u16 yskip; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 69 | unsigned int gain; |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 70 | unsigned short y_skip_top; /* Lines to skip at the top */ |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 71 | unsigned int exposure; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 72 | unsigned char autoexposure; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 73 | }; |
| 74 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 75 | static struct mt9t031 *to_mt9t031(const struct i2c_client *client) |
| 76 | { |
| 77 | return container_of(i2c_get_clientdata(client), struct mt9t031, subdev); |
| 78 | } |
| 79 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 80 | static int reg_read(struct i2c_client *client, const u8 reg) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 81 | { |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 82 | s32 data = i2c_smbus_read_word_data(client, reg); |
| 83 | return data < 0 ? data : swab16(data); |
| 84 | } |
| 85 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 86 | static int reg_write(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 87 | const u16 data) |
| 88 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 89 | return i2c_smbus_write_word_data(client, reg, swab16(data)); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 90 | } |
| 91 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 92 | static int reg_set(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 93 | const u16 data) |
| 94 | { |
| 95 | int ret; |
| 96 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 97 | ret = reg_read(client, reg); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 98 | if (ret < 0) |
| 99 | return ret; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 100 | return reg_write(client, reg, ret | data); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 101 | } |
| 102 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 103 | static int reg_clear(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 104 | const u16 data) |
| 105 | { |
| 106 | int ret; |
| 107 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 108 | ret = reg_read(client, reg); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 109 | if (ret < 0) |
| 110 | return ret; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 111 | return reg_write(client, reg, ret & ~data); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 112 | } |
| 113 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 114 | static int set_shutter(struct i2c_client *client, const u32 data) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 115 | { |
| 116 | int ret; |
| 117 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 118 | ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 119 | |
| 120 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 121 | ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 122 | |
| 123 | return ret; |
| 124 | } |
| 125 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 126 | static int get_shutter(struct i2c_client *client, u32 *data) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 127 | { |
| 128 | int ret; |
| 129 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 130 | ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 131 | *data = ret << 16; |
| 132 | |
| 133 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 134 | ret = reg_read(client, MT9T031_SHUTTER_WIDTH); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 135 | *data |= ret & 0xffff; |
| 136 | |
| 137 | return ret < 0 ? ret : 0; |
| 138 | } |
| 139 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 140 | static int mt9t031_idle(struct i2c_client *client) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 141 | { |
| 142 | int ret; |
| 143 | |
| 144 | /* Disable chip output, synchronous option update */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 145 | ret = reg_write(client, MT9T031_RESET, 1); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 146 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 147 | ret = reg_write(client, MT9T031_RESET, 0); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 148 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 149 | ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 150 | |
| 151 | return ret >= 0 ? 0 : -EIO; |
| 152 | } |
| 153 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 154 | static int mt9t031_disable(struct i2c_client *client) |
| 155 | { |
| 156 | /* Disable the chip */ |
| 157 | reg_clear(client, MT9T031_OUTPUT_CONTROL, 2); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 162 | static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 163 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 164 | struct i2c_client *client = sd->priv; |
| 165 | int ret; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 166 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 167 | if (enable) |
| 168 | /* Switch to master "normal" mode */ |
| 169 | ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2); |
| 170 | else |
| 171 | /* Stop sensor readout */ |
| 172 | ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2); |
| 173 | |
| 174 | if (ret < 0) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 175 | return -EIO; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 176 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int mt9t031_set_bus_param(struct soc_camera_device *icd, |
| 181 | unsigned long flags) |
| 182 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 183 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 184 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 185 | /* The caller should have queried our parameters, check anyway */ |
| 186 | if (flags & ~MT9T031_BUS_PARAM) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | if (flags & SOCAM_PCLK_SAMPLE_FALLING) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 190 | reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000); |
Philippe Rétornaz | c98afbf | 2009-03-13 09:42:32 -0300 | [diff] [blame] | 191 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 192 | reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd) |
| 198 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 199 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 200 | |
| 201 | return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM); |
| 202 | } |
| 203 | |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 204 | enum { |
| 205 | MT9T031_CTRL_VFLIP, |
| 206 | MT9T031_CTRL_HFLIP, |
| 207 | MT9T031_CTRL_GAIN, |
| 208 | MT9T031_CTRL_EXPOSURE, |
| 209 | MT9T031_CTRL_EXPOSURE_AUTO, |
| 210 | }; |
| 211 | |
| 212 | static const struct v4l2_queryctrl mt9t031_controls[] = { |
| 213 | [MT9T031_CTRL_VFLIP] = { |
| 214 | .id = V4L2_CID_VFLIP, |
| 215 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 216 | .name = "Flip Vertically", |
| 217 | .minimum = 0, |
| 218 | .maximum = 1, |
| 219 | .step = 1, |
| 220 | .default_value = 0, |
| 221 | }, |
| 222 | [MT9T031_CTRL_HFLIP] = { |
| 223 | .id = V4L2_CID_HFLIP, |
| 224 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 225 | .name = "Flip Horizontally", |
| 226 | .minimum = 0, |
| 227 | .maximum = 1, |
| 228 | .step = 1, |
| 229 | .default_value = 0, |
| 230 | }, |
| 231 | [MT9T031_CTRL_GAIN] = { |
| 232 | .id = V4L2_CID_GAIN, |
| 233 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 234 | .name = "Gain", |
| 235 | .minimum = 0, |
| 236 | .maximum = 127, |
| 237 | .step = 1, |
| 238 | .default_value = 64, |
| 239 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 240 | }, |
| 241 | [MT9T031_CTRL_EXPOSURE] = { |
| 242 | .id = V4L2_CID_EXPOSURE, |
| 243 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 244 | .name = "Exposure", |
| 245 | .minimum = 1, |
| 246 | .maximum = 255, |
| 247 | .step = 1, |
| 248 | .default_value = 255, |
| 249 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 250 | }, |
| 251 | [MT9T031_CTRL_EXPOSURE_AUTO] = { |
| 252 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 253 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 254 | .name = "Automatic Exposure", |
| 255 | .minimum = 0, |
| 256 | .maximum = 1, |
| 257 | .step = 1, |
| 258 | .default_value = 1, |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | static struct soc_camera_ops mt9t031_ops = { |
| 263 | .set_bus_param = mt9t031_set_bus_param, |
| 264 | .query_bus_param = mt9t031_query_bus_param, |
| 265 | .controls = mt9t031_controls, |
| 266 | .num_controls = ARRAY_SIZE(mt9t031_controls), |
| 267 | }; |
| 268 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 269 | /* target must be _even_ */ |
| 270 | static u16 mt9t031_skip(s32 *source, s32 target, s32 max) |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 271 | { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 272 | unsigned int skip; |
| 273 | |
| 274 | if (*source < target + target / 2) { |
| 275 | *source = target; |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | skip = min(max, *source + target / 2) / target; |
| 280 | if (skip > 8) |
| 281 | skip = 8; |
| 282 | *source = target * skip; |
| 283 | |
| 284 | return skip; |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 285 | } |
| 286 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 287 | /* rect is the sensor rectangle, the caller guarantees parameter validity */ |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 288 | static int mt9t031_set_params(struct i2c_client *client, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 289 | struct v4l2_rect *rect, u16 xskip, u16 yskip) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 290 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 291 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 292 | int ret; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 293 | u16 xbin, ybin; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 294 | const u16 hblank = MT9T031_HORIZONTAL_BLANK, |
| 295 | vblank = MT9T031_VERTICAL_BLANK; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 296 | |
| 297 | xbin = min(xskip, (u16)3); |
| 298 | ybin = min(yskip, (u16)3); |
| 299 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 300 | /* |
| 301 | * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper. |
| 302 | * There is always a valid suitably aligned value. The worst case is |
| 303 | * xbin = 3, width = 2048. Then we will start at 36, the last read out |
| 304 | * pixel will be 2083, which is < 2085 - first black pixel. |
| 305 | * |
| 306 | * MT9T031 datasheet imposes window left border alignment, depending on |
| 307 | * the selected xskip. Failing to conform to this requirement produces |
| 308 | * dark horizontal stripes in the image. However, even obeying to this |
| 309 | * requirement doesn't eliminate the stripes in all configurations. They |
| 310 | * appear "locally reproducibly," but can differ between tests under |
| 311 | * different lighting conditions. |
| 312 | */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 313 | switch (xbin) { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 314 | case 1: |
| 315 | rect->left &= ~1; |
| 316 | break; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 317 | case 2: |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 318 | rect->left &= ~3; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 319 | break; |
| 320 | case 3: |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 321 | rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ? |
| 322 | (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 323 | } |
| 324 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 325 | rect->top &= ~1; |
| 326 | |
| 327 | dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n", |
| 328 | xskip, yskip, rect->width, rect->height, rect->left, rect->top); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 329 | |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 330 | /* Disable register update, reconfigure atomically */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 331 | ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1); |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 332 | if (ret < 0) |
| 333 | return ret; |
| 334 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 335 | /* Blanking and start values - default... */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 336 | ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 337 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 338 | ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 339 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 340 | if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) { |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 341 | /* Binning, skipping */ |
| 342 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 343 | ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE, |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 344 | ((xbin - 1) << 4) | (xskip - 1)); |
| 345 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 346 | ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE, |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 347 | ((ybin - 1) << 4) | (yskip - 1)); |
| 348 | } |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 349 | dev_dbg(&client->dev, "new physical left %u, top %u\n", |
| 350 | rect->left, rect->top); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 351 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 352 | /* |
| 353 | * The caller provides a supported format, as guaranteed by |
| 354 | * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() |
| 355 | */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 356 | if (ret >= 0) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 357 | ret = reg_write(client, MT9T031_COLUMN_START, rect->left); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 358 | if (ret >= 0) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 359 | ret = reg_write(client, MT9T031_ROW_START, rect->top); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 360 | if (ret >= 0) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 361 | ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 362 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 363 | ret = reg_write(client, MT9T031_WINDOW_HEIGHT, |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 364 | rect->height + mt9t031->y_skip_top - 1); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 365 | if (ret >= 0 && mt9t031->autoexposure) { |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 366 | unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 367 | ret = set_shutter(client, total_h); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 368 | if (ret >= 0) { |
| 369 | const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank; |
| 370 | const struct v4l2_queryctrl *qctrl = |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 371 | &mt9t031_controls[MT9T031_CTRL_EXPOSURE]; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 372 | mt9t031->exposure = (shutter_max / 2 + (total_h - 1) * |
| 373 | (qctrl->maximum - qctrl->minimum)) / |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 374 | shutter_max + qctrl->minimum; |
| 375 | } |
| 376 | } |
| 377 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 378 | /* Re-enable register update, commit all changes */ |
| 379 | if (ret >= 0) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 380 | ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 381 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 382 | if (ret >= 0) { |
| 383 | mt9t031->rect = *rect; |
| 384 | mt9t031->xskip = xskip; |
| 385 | mt9t031->yskip = yskip; |
| 386 | } |
| 387 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 388 | return ret < 0 ? ret : 0; |
| 389 | } |
| 390 | |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 391 | static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 392 | { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 393 | struct v4l2_rect rect = a->c; |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 394 | struct i2c_client *client = sd->priv; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 395 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 396 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 397 | rect.width = ALIGN(rect.width, 2); |
| 398 | rect.height = ALIGN(rect.height, 2); |
Guennadi Liakhovetski | e330919 | 2009-08-25 11:46:42 -0300 | [diff] [blame] | 399 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 400 | soc_camera_limit_side(&rect.left, &rect.width, |
| 401 | MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH); |
Guennadi Liakhovetski | e330919 | 2009-08-25 11:46:42 -0300 | [diff] [blame] | 402 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 403 | soc_camera_limit_side(&rect.top, &rect.height, |
| 404 | MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT); |
| 405 | |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 406 | return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
| 410 | { |
| 411 | struct i2c_client *client = sd->priv; |
| 412 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
| 413 | |
| 414 | a->c = mt9t031->rect; |
| 415 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a) |
| 421 | { |
| 422 | a->bounds.left = MT9T031_COLUMN_SKIP; |
| 423 | a->bounds.top = MT9T031_ROW_SKIP; |
| 424 | a->bounds.width = MT9T031_MAX_WIDTH; |
| 425 | a->bounds.height = MT9T031_MAX_HEIGHT; |
| 426 | a->defrect = a->bounds; |
| 427 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 428 | a->pixelaspect.numerator = 1; |
| 429 | a->pixelaspect.denominator = 1; |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 434 | static int mt9t031_g_fmt(struct v4l2_subdev *sd, |
| 435 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 436 | { |
| 437 | struct i2c_client *client = sd->priv; |
| 438 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 439 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 440 | mf->width = mt9t031->rect.width / mt9t031->xskip; |
| 441 | mf->height = mt9t031->rect.height / mt9t031->yskip; |
| 442 | mf->code = V4L2_MBUS_FMT_SBGGR10_1X10; |
| 443 | mf->colorspace = V4L2_COLORSPACE_SRGB; |
| 444 | mf->field = V4L2_FIELD_NONE; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 445 | |
| 446 | return 0; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 447 | } |
| 448 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 449 | static int mt9t031_s_fmt(struct v4l2_subdev *sd, |
| 450 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 451 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 452 | struct i2c_client *client = sd->priv; |
| 453 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 454 | u16 xskip, yskip; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 455 | struct v4l2_rect rect = mt9t031->rect; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 456 | |
| 457 | /* |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 458 | * try_fmt has put width and height within limits. |
| 459 | * S_FMT: use binning and skipping for scaling |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 460 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 461 | xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH); |
| 462 | yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT); |
| 463 | |
| 464 | mf->code = V4L2_MBUS_FMT_SBGGR10_1X10; |
| 465 | mf->colorspace = V4L2_COLORSPACE_SRGB; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 466 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 467 | /* mt9t031_set_params() doesn't change width and height */ |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 468 | return mt9t031_set_params(client, &rect, xskip, yskip); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 469 | } |
| 470 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 471 | /* |
| 472 | * If a user window larger than sensor window is requested, we'll increase the |
| 473 | * sensor window. |
| 474 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 475 | static int mt9t031_try_fmt(struct v4l2_subdev *sd, |
| 476 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 477 | { |
Trent Piepho | 653dc59 | 2009-05-30 21:45:46 -0300 | [diff] [blame] | 478 | v4l_bound_align_image( |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 479 | &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1, |
| 480 | &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0); |
| 481 | |
| 482 | mf->code = V4L2_MBUS_FMT_SBGGR10_1X10; |
| 483 | mf->colorspace = V4L2_COLORSPACE_SRGB; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 488 | static int mt9t031_g_chip_ident(struct v4l2_subdev *sd, |
| 489 | struct v4l2_dbg_chip_ident *id) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 490 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 491 | struct i2c_client *client = sd->priv; |
| 492 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 493 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 494 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 495 | return -EINVAL; |
| 496 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 497 | if (id->match.addr != client->addr) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 498 | return -ENODEV; |
| 499 | |
| 500 | id->ident = mt9t031->model; |
| 501 | id->revision = 0; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 507 | static int mt9t031_g_register(struct v4l2_subdev *sd, |
| 508 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 509 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 510 | struct i2c_client *client = sd->priv; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 511 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 512 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 513 | return -EINVAL; |
| 514 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 515 | if (reg->match.addr != client->addr) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 516 | return -ENODEV; |
| 517 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 518 | reg->val = reg_read(client, reg->reg); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 519 | |
| 520 | if (reg->val > 0xffff) |
| 521 | return -EIO; |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 526 | static int mt9t031_s_register(struct v4l2_subdev *sd, |
| 527 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 528 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 529 | struct i2c_client *client = sd->priv; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 530 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 531 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 532 | return -EINVAL; |
| 533 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 534 | if (reg->match.addr != client->addr) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 535 | return -ENODEV; |
| 536 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 537 | if (reg_write(client, reg->reg, reg->val) < 0) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 538 | return -EIO; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | #endif |
| 543 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 544 | static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 545 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 546 | struct i2c_client *client = sd->priv; |
| 547 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 548 | int data; |
| 549 | |
| 550 | switch (ctrl->id) { |
| 551 | case V4L2_CID_VFLIP: |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 552 | data = reg_read(client, MT9T031_READ_MODE_2); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 553 | if (data < 0) |
| 554 | return -EIO; |
| 555 | ctrl->value = !!(data & 0x8000); |
| 556 | break; |
| 557 | case V4L2_CID_HFLIP: |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 558 | data = reg_read(client, MT9T031_READ_MODE_2); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 559 | if (data < 0) |
| 560 | return -EIO; |
| 561 | ctrl->value = !!(data & 0x4000); |
| 562 | break; |
| 563 | case V4L2_CID_EXPOSURE_AUTO: |
| 564 | ctrl->value = mt9t031->autoexposure; |
| 565 | break; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 566 | case V4L2_CID_GAIN: |
| 567 | ctrl->value = mt9t031->gain; |
| 568 | break; |
| 569 | case V4L2_CID_EXPOSURE: |
| 570 | ctrl->value = mt9t031->exposure; |
| 571 | break; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 572 | } |
| 573 | return 0; |
| 574 | } |
| 575 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 576 | static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 577 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 578 | struct i2c_client *client = sd->priv; |
| 579 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 580 | const struct v4l2_queryctrl *qctrl; |
| 581 | int data; |
| 582 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 583 | switch (ctrl->id) { |
| 584 | case V4L2_CID_VFLIP: |
| 585 | if (ctrl->value) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 586 | data = reg_set(client, MT9T031_READ_MODE_2, 0x8000); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 587 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 588 | data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 589 | if (data < 0) |
| 590 | return -EIO; |
| 591 | break; |
| 592 | case V4L2_CID_HFLIP: |
| 593 | if (ctrl->value) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 594 | data = reg_set(client, MT9T031_READ_MODE_2, 0x4000); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 595 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 596 | data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 597 | if (data < 0) |
| 598 | return -EIO; |
| 599 | break; |
| 600 | case V4L2_CID_GAIN: |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 601 | qctrl = &mt9t031_controls[MT9T031_CTRL_GAIN]; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 602 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 603 | return -EINVAL; |
| 604 | /* See Datasheet Table 7, Gain settings. */ |
| 605 | if (ctrl->value <= qctrl->default_value) { |
| 606 | /* Pack it into 0..1 step 0.125, register values 0..8 */ |
| 607 | unsigned long range = qctrl->default_value - qctrl->minimum; |
| 608 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; |
| 609 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 610 | dev_dbg(&client->dev, "Setting gain %d\n", data); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 611 | data = reg_write(client, MT9T031_GLOBAL_GAIN, data); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 612 | if (data < 0) |
| 613 | return -EIO; |
| 614 | } else { |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 615 | /* Pack it into 1.125..128 variable step, register values 9..0x7860 */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 616 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ |
| 617 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 618 | /* calculated gain: map 65..127 to 9..1024 step 0.125 */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 619 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 620 | 1015 + range / 2) / range + 9; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 621 | |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 622 | if (gain <= 32) /* calculated gain 9..32 -> 9..32 */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 623 | data = gain; |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 624 | else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 625 | data = ((gain - 32) * 16 + 16) / 32 + 80; |
| 626 | else |
Guennadi Liakhovetski | 70e1d35 | 2009-02-23 12:13:23 -0300 | [diff] [blame] | 627 | /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */ |
| 628 | data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 629 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 630 | dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n", |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 631 | reg_read(client, MT9T031_GLOBAL_GAIN), data); |
| 632 | data = reg_write(client, MT9T031_GLOBAL_GAIN, data); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 633 | if (data < 0) |
| 634 | return -EIO; |
| 635 | } |
| 636 | |
| 637 | /* Success */ |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 638 | mt9t031->gain = ctrl->value; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 639 | break; |
| 640 | case V4L2_CID_EXPOSURE: |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 641 | qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE]; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 642 | /* mt9t031 has maximum == default */ |
| 643 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 644 | return -EINVAL; |
| 645 | else { |
| 646 | const unsigned long range = qctrl->maximum - qctrl->minimum; |
| 647 | const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 + |
| 648 | range / 2) / range + 1; |
| 649 | u32 old; |
| 650 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 651 | get_shutter(client, &old); |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 652 | dev_dbg(&client->dev, "Set shutter from %u to %u\n", |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 653 | old, shutter); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 654 | if (set_shutter(client, shutter) < 0) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 655 | return -EIO; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 656 | mt9t031->exposure = ctrl->value; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 657 | mt9t031->autoexposure = 0; |
| 658 | } |
| 659 | break; |
| 660 | case V4L2_CID_EXPOSURE_AUTO: |
| 661 | if (ctrl->value) { |
| 662 | const u16 vblank = MT9T031_VERTICAL_BLANK; |
| 663 | const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 664 | unsigned int total_h = mt9t031->rect.height + |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 665 | mt9t031->y_skip_top + vblank; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 666 | |
| 667 | if (set_shutter(client, total_h) < 0) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 668 | return -EIO; |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 669 | qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE]; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 670 | mt9t031->exposure = (shutter_max / 2 + (total_h - 1) * |
| 671 | (qctrl->maximum - qctrl->minimum)) / |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 672 | shutter_max + qctrl->minimum; |
| 673 | mt9t031->autoexposure = 1; |
| 674 | } else |
| 675 | mt9t031->autoexposure = 0; |
| 676 | break; |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 677 | default: |
| 678 | return -EINVAL; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 683 | /* |
| 684 | * Interface active, can use i2c. If it fails, it can indeed mean, that |
| 685 | * this wasn't our capture interface, so, we wait for the right one |
| 686 | */ |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 687 | static int mt9t031_video_probe(struct i2c_client *client) |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 688 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 689 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 690 | s32 data; |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 691 | int ret; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 692 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 693 | /* Enable the chip */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 694 | data = reg_write(client, MT9T031_CHIP_ENABLE, 1); |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 695 | dev_dbg(&client->dev, "write: %d\n", data); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 696 | |
| 697 | /* Read out the chip version register */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 698 | data = reg_read(client, MT9T031_CHIP_VERSION); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 699 | |
| 700 | switch (data) { |
| 701 | case 0x1621: |
| 702 | mt9t031->model = V4L2_IDENT_MT9T031; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 703 | break; |
| 704 | default: |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 705 | dev_err(&client->dev, |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 706 | "No MT9T031 chip detected, register read %x\n", data); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 707 | return -ENODEV; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 708 | } |
| 709 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 710 | dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 711 | |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 712 | ret = mt9t031_idle(client); |
| 713 | if (ret < 0) |
| 714 | dev_err(&client->dev, "Failed to initialise the camera\n"); |
| 715 | |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 716 | /* mt9t031_idle() has reset the chip to default. */ |
| 717 | mt9t031->exposure = 255; |
| 718 | mt9t031->gain = 64; |
| 719 | |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 720 | return ret; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 721 | } |
| 722 | |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 723 | static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines) |
| 724 | { |
| 725 | struct i2c_client *client = sd->priv; |
| 726 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
| 727 | |
| 728 | *lines = mt9t031->y_skip_top; |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 733 | static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = { |
| 734 | .g_ctrl = mt9t031_g_ctrl, |
| 735 | .s_ctrl = mt9t031_s_ctrl, |
| 736 | .g_chip_ident = mt9t031_g_chip_ident, |
| 737 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 738 | .g_register = mt9t031_g_register, |
| 739 | .s_register = mt9t031_s_register, |
| 740 | #endif |
| 741 | }; |
| 742 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 743 | static int mt9t031_enum_fmt(struct v4l2_subdev *sd, int index, |
| 744 | enum v4l2_mbus_pixelcode *code) |
| 745 | { |
| 746 | if (index) |
| 747 | return -EINVAL; |
| 748 | |
| 749 | *code = V4L2_MBUS_FMT_SBGGR10_1X10; |
| 750 | return 0; |
| 751 | } |
| 752 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 753 | static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = { |
| 754 | .s_stream = mt9t031_s_stream, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 755 | .s_mbus_fmt = mt9t031_s_fmt, |
| 756 | .g_mbus_fmt = mt9t031_g_fmt, |
| 757 | .try_mbus_fmt = mt9t031_try_fmt, |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 758 | .s_crop = mt9t031_s_crop, |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 759 | .g_crop = mt9t031_g_crop, |
| 760 | .cropcap = mt9t031_cropcap, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 761 | .enum_mbus_fmt = mt9t031_enum_fmt, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 762 | }; |
| 763 | |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 764 | static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = { |
| 765 | .g_skip_top_lines = mt9t031_g_skip_top_lines, |
| 766 | }; |
| 767 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 768 | static struct v4l2_subdev_ops mt9t031_subdev_ops = { |
| 769 | .core = &mt9t031_subdev_core_ops, |
| 770 | .video = &mt9t031_subdev_video_ops, |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 771 | .sensor = &mt9t031_subdev_sensor_ops, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 772 | }; |
| 773 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 774 | static int mt9t031_probe(struct i2c_client *client, |
| 775 | const struct i2c_device_id *did) |
| 776 | { |
| 777 | struct mt9t031 *mt9t031; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 778 | struct soc_camera_device *icd = client->dev.platform_data; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 779 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 780 | int ret; |
| 781 | |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 782 | if (icd) { |
| 783 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 784 | if (!icl) { |
| 785 | dev_err(&client->dev, "MT9T031 driver needs platform data\n"); |
| 786 | return -EINVAL; |
| 787 | } |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 788 | |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 789 | icd->ops = &mt9t031_ops; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 793 | dev_warn(&adapter->dev, |
| 794 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 795 | return -EIO; |
| 796 | } |
| 797 | |
| 798 | mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL); |
| 799 | if (!mt9t031) |
| 800 | return -ENOMEM; |
| 801 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 802 | v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops); |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 803 | |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 804 | mt9t031->y_skip_top = 0; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 805 | mt9t031->rect.left = MT9T031_COLUMN_SKIP; |
| 806 | mt9t031->rect.top = MT9T031_ROW_SKIP; |
| 807 | mt9t031->rect.width = MT9T031_MAX_WIDTH; |
| 808 | mt9t031->rect.height = MT9T031_MAX_HEIGHT; |
| 809 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 810 | /* |
| 811 | * Simulated autoexposure. If enabled, we calculate shutter width |
| 812 | * ourselves in the driver based on vertical blanking and frame width |
| 813 | */ |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 814 | mt9t031->autoexposure = 1; |
| 815 | |
| 816 | mt9t031->xskip = 1; |
| 817 | mt9t031->yskip = 1; |
| 818 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 819 | mt9t031_idle(client); |
| 820 | |
| 821 | ret = mt9t031_video_probe(client); |
| 822 | |
| 823 | mt9t031_disable(client); |
| 824 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 825 | if (ret) { |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 826 | if (icd) |
| 827 | icd->ops = NULL; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 828 | i2c_set_clientdata(client, NULL); |
| 829 | kfree(mt9t031); |
| 830 | } |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 831 | |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 832 | return ret; |
| 833 | } |
| 834 | |
| 835 | static int mt9t031_remove(struct i2c_client *client) |
| 836 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 837 | struct mt9t031 *mt9t031 = to_mt9t031(client); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 838 | struct soc_camera_device *icd = client->dev.platform_data; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 839 | |
Guennadi Liakhovetski | a3a4ac1 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 840 | if (icd) |
| 841 | icd->ops = NULL; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 842 | i2c_set_clientdata(client, NULL); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 843 | client->driver = NULL; |
Guennadi Liakhovetski | 4e96fd0 | 2008-12-29 06:04:59 -0300 | [diff] [blame] | 844 | kfree(mt9t031); |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static const struct i2c_device_id mt9t031_id[] = { |
| 850 | { "mt9t031", 0 }, |
| 851 | { } |
| 852 | }; |
| 853 | MODULE_DEVICE_TABLE(i2c, mt9t031_id); |
| 854 | |
| 855 | static struct i2c_driver mt9t031_i2c_driver = { |
| 856 | .driver = { |
| 857 | .name = "mt9t031", |
| 858 | }, |
| 859 | .probe = mt9t031_probe, |
| 860 | .remove = mt9t031_remove, |
| 861 | .id_table = mt9t031_id, |
| 862 | }; |
| 863 | |
| 864 | static int __init mt9t031_mod_init(void) |
| 865 | { |
| 866 | return i2c_add_driver(&mt9t031_i2c_driver); |
| 867 | } |
| 868 | |
| 869 | static void __exit mt9t031_mod_exit(void) |
| 870 | { |
| 871 | i2c_del_driver(&mt9t031_i2c_driver); |
| 872 | } |
| 873 | |
| 874 | module_init(mt9t031_mod_init); |
| 875 | module_exit(mt9t031_mod_exit); |
| 876 | |
| 877 | MODULE_DESCRIPTION("Micron MT9T031 Camera driver"); |
| 878 | MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>"); |
| 879 | MODULE_LICENSE("GPL v2"); |