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