Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9V022 CMOS Image Sensor from Micron |
| 3 | * |
| 4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.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/delay.h> |
| 15 | #include <linux/log2.h> |
| 16 | |
| 17 | #include <media/v4l2-common.h> |
| 18 | #include <media/v4l2-chip-ident.h> |
| 19 | #include <media/soc_camera.h> |
| 20 | |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 21 | /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c |
| 22 | * The platform has to define i2c_board_info |
| 23 | * and call i2c_register_board_info() */ |
| 24 | |
| 25 | static char *sensor_type; |
| 26 | module_param(sensor_type, charp, S_IRUGO); |
Niels de Vos | 61a2d07 | 2008-07-31 00:07:23 -0700 | [diff] [blame] | 27 | MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\""); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 28 | |
| 29 | /* mt9v022 selected register addresses */ |
| 30 | #define MT9V022_CHIP_VERSION 0x00 |
| 31 | #define MT9V022_COLUMN_START 0x01 |
| 32 | #define MT9V022_ROW_START 0x02 |
| 33 | #define MT9V022_WINDOW_HEIGHT 0x03 |
| 34 | #define MT9V022_WINDOW_WIDTH 0x04 |
| 35 | #define MT9V022_HORIZONTAL_BLANKING 0x05 |
| 36 | #define MT9V022_VERTICAL_BLANKING 0x06 |
| 37 | #define MT9V022_CHIP_CONTROL 0x07 |
| 38 | #define MT9V022_SHUTTER_WIDTH1 0x08 |
| 39 | #define MT9V022_SHUTTER_WIDTH2 0x09 |
| 40 | #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a |
| 41 | #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b |
| 42 | #define MT9V022_RESET 0x0c |
| 43 | #define MT9V022_READ_MODE 0x0d |
| 44 | #define MT9V022_MONITOR_MODE 0x0e |
| 45 | #define MT9V022_PIXEL_OPERATION_MODE 0x0f |
| 46 | #define MT9V022_LED_OUT_CONTROL 0x1b |
| 47 | #define MT9V022_ADC_MODE_CONTROL 0x1c |
| 48 | #define MT9V022_ANALOG_GAIN 0x34 |
| 49 | #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47 |
| 50 | #define MT9V022_PIXCLK_FV_LV 0x74 |
| 51 | #define MT9V022_DIGITAL_TEST_PATTERN 0x7f |
| 52 | #define MT9V022_AEC_AGC_ENABLE 0xAF |
| 53 | #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD |
| 54 | |
| 55 | /* Progressive scan, master, defaults */ |
| 56 | #define MT9V022_CHIP_CONTROL_DEFAULT 0x188 |
| 57 | |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 58 | static const struct soc_camera_data_format mt9v022_colour_formats[] = { |
| 59 | /* Order important: first natively supported, |
| 60 | * second supported with a GPIO extender */ |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 61 | { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 62 | .name = "Bayer (sRGB) 10 bit", |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 63 | .depth = 10, |
| 64 | .fourcc = V4L2_PIX_FMT_SBGGR16, |
| 65 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 66 | }, { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 67 | .name = "Bayer (sRGB) 8 bit", |
| 68 | .depth = 8, |
| 69 | .fourcc = V4L2_PIX_FMT_SBGGR8, |
| 70 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | static const struct soc_camera_data_format mt9v022_monochrome_formats[] = { |
| 75 | /* Order important - see above */ |
| 76 | { |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 77 | .name = "Monochrome 10 bit", |
| 78 | .depth = 10, |
| 79 | .fourcc = V4L2_PIX_FMT_Y16, |
| 80 | }, { |
| 81 | .name = "Monochrome 8 bit", |
| 82 | .depth = 8, |
| 83 | .fourcc = V4L2_PIX_FMT_GREY, |
| 84 | }, |
| 85 | }; |
| 86 | |
| 87 | struct mt9v022 { |
| 88 | struct i2c_client *client; |
| 89 | struct soc_camera_device icd; |
Guennadi Liakhovetski | 7fb0fd0 | 2008-05-05 14:12:30 -0300 | [diff] [blame] | 90 | int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */ |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 91 | u16 chip_control; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 92 | }; |
| 93 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 94 | static int reg_read(struct i2c_client *client, const u8 reg) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 95 | { |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 96 | s32 data = i2c_smbus_read_word_data(client, reg); |
| 97 | return data < 0 ? data : swab16(data); |
| 98 | } |
| 99 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 100 | static int reg_write(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 101 | const u16 data) |
| 102 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 103 | return i2c_smbus_write_word_data(client, reg, swab16(data)); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 104 | } |
| 105 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 106 | static int reg_set(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -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 | 7397bfbe | 2008-04-22 14:42:04 -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 | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 115 | } |
| 116 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 117 | static int reg_clear(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 118 | const u16 data) |
| 119 | { |
| 120 | int ret; |
| 121 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 122 | ret = reg_read(client, reg); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 123 | if (ret < 0) |
| 124 | return ret; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 125 | return reg_write(client, reg, ret & ~data); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static int mt9v022_init(struct soc_camera_device *icd) |
| 129 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 130 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 131 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 132 | struct soc_camera_link *icl = client->dev.platform_data; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 133 | int ret; |
| 134 | |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 135 | if (icl->power) { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 136 | ret = icl->power(&client->dev, 1); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 137 | if (ret < 0) { |
| 138 | dev_err(icd->vdev->parent, |
| 139 | "Platform failed to power-on the camera.\n"); |
| 140 | return ret; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * The camera could have been already on, we hard-reset it additionally, |
| 146 | * if available. Soft reset is done in video_probe(). |
| 147 | */ |
| 148 | if (icl->reset) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 149 | icl->reset(&client->dev); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 150 | |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 151 | /* Almost the default mode: master, parallel, simultaneous, and an |
| 152 | * undocumented bit 0x200, which is present in table 7, but not in 8, |
| 153 | * plus snapshot mode to disable scan for now */ |
| 154 | mt9v022->chip_control |= 0x10; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 155 | ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 156 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 157 | ret = reg_write(client, MT9V022_READ_MODE, 0x300); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 158 | |
| 159 | /* All defaults */ |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 160 | if (!ret) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 161 | /* AEC, AGC on */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 162 | ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 163 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 164 | ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 165 | if (!ret) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 166 | /* default - auto */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 167 | ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 168 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 169 | ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 170 | |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 171 | return ret; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static int mt9v022_release(struct soc_camera_device *icd) |
| 175 | { |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 176 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 177 | struct soc_camera_link *icl = mt9v022->client->dev.platform_data; |
| 178 | |
| 179 | if (icl->power) |
| 180 | icl->power(&mt9v022->client->dev, 0); |
| 181 | |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int mt9v022_start_capture(struct soc_camera_device *icd) |
| 186 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 187 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 188 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 189 | /* Switch to master "normal" mode */ |
| 190 | mt9v022->chip_control &= ~0x10; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 191 | if (reg_write(client, MT9V022_CHIP_CONTROL, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 192 | mt9v022->chip_control) < 0) |
| 193 | return -EIO; |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int mt9v022_stop_capture(struct soc_camera_device *icd) |
| 198 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 199 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 200 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 201 | /* Switch to snapshot mode */ |
| 202 | mt9v022->chip_control |= 0x10; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 203 | if (reg_write(client, MT9V022_CHIP_CONTROL, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 204 | mt9v022->chip_control) < 0) |
| 205 | return -EIO; |
| 206 | return 0; |
| 207 | } |
| 208 | |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 209 | static int mt9v022_set_bus_param(struct soc_camera_device *icd, |
| 210 | unsigned long flags) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 211 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 212 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 213 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 214 | struct soc_camera_link *icl = client->dev.platform_data; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 215 | unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 216 | int ret; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 217 | u16 pixclk = 0; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 218 | |
| 219 | /* Only one width bit may be set */ |
| 220 | if (!is_power_of_2(width_flag)) |
| 221 | return -EINVAL; |
| 222 | |
Sascha Hauer | e958e27 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 223 | if (icl->set_bus_param) { |
| 224 | ret = icl->set_bus_param(icl, width_flag); |
| 225 | if (ret) |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 226 | return ret; |
Sascha Hauer | e958e27 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 227 | } else { |
| 228 | /* |
| 229 | * Without board specific bus width settings we only support the |
| 230 | * sensors native bus width |
| 231 | */ |
| 232 | if (width_flag != SOCAM_DATAWIDTH_10) |
| 233 | return -EINVAL; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 234 | } |
| 235 | |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 236 | flags = soc_camera_apply_sensor_flags(icl, flags); |
| 237 | |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 238 | if (flags & SOCAM_PCLK_SAMPLE_RISING) |
| 239 | pixclk |= 0x10; |
| 240 | |
| 241 | if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH)) |
| 242 | pixclk |= 0x1; |
| 243 | |
| 244 | if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH)) |
| 245 | pixclk |= 0x2; |
| 246 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 247 | ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk); |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 248 | if (ret < 0) |
| 249 | return ret; |
| 250 | |
| 251 | if (!(flags & SOCAM_MASTER)) |
| 252 | mt9v022->chip_control &= ~0x8; |
| 253 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 254 | ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control); |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 255 | if (ret < 0) |
| 256 | return ret; |
| 257 | |
| 258 | dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n", |
| 259 | pixclk, mt9v022->chip_control); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd) |
| 265 | { |
| 266 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
Sascha Hauer | e958e27 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 267 | struct soc_camera_link *icl = mt9v022->client->dev.platform_data; |
| 268 | unsigned int width_flag; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 269 | |
Sascha Hauer | e958e27 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 270 | if (icl->query_bus_param) |
| 271 | width_flag = icl->query_bus_param(icl) & |
| 272 | SOCAM_DATAWIDTH_MASK; |
| 273 | else |
| 274 | width_flag = SOCAM_DATAWIDTH_10; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 275 | |
| 276 | return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | |
| 277 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW | |
| 278 | SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | |
Guennadi Liakhovetski | 2d9329f | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 279 | SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE | |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 280 | width_flag; |
| 281 | } |
| 282 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 283 | static int mt9v022_set_crop(struct soc_camera_device *icd, |
| 284 | struct v4l2_rect *rect) |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 285 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 286 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 287 | int ret; |
| 288 | |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 289 | /* Like in example app. Contradicts the datasheet though */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 290 | ret = reg_read(client, MT9V022_AEC_AGC_ENABLE); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 291 | if (ret >= 0) { |
| 292 | if (ret & 1) /* Autoexposure */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 293 | ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 294 | rect->height + icd->y_skip_top + 43); |
| 295 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 296 | ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 297 | rect->height + icd->y_skip_top + 43); |
| 298 | } |
| 299 | /* Setup frame format: defaults apart from width and height */ |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 300 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 301 | ret = reg_write(client, MT9V022_COLUMN_START, rect->left); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 302 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 303 | ret = reg_write(client, MT9V022_ROW_START, rect->top); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 304 | if (!ret) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 305 | /* Default 94, Phytec driver says: |
| 306 | * "width + horizontal blank >= 660" */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 307 | ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 308 | rect->width > 660 - 43 ? 43 : |
| 309 | 660 - rect->width); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 310 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 311 | ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 312 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 313 | ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect->width); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 314 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 315 | ret = reg_write(client, MT9V022_WINDOW_HEIGHT, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 316 | rect->height + icd->y_skip_top); |
| 317 | |
| 318 | if (ret < 0) |
| 319 | return ret; |
| 320 | |
| 321 | dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height); |
| 322 | |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 326 | static int mt9v022_set_fmt(struct soc_camera_device *icd, |
| 327 | struct v4l2_format *f) |
| 328 | { |
| 329 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 330 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 331 | struct v4l2_rect rect = { |
| 332 | .left = icd->x_current, |
| 333 | .top = icd->y_current, |
| 334 | .width = pix->width, |
| 335 | .height = pix->height, |
| 336 | }; |
| 337 | |
| 338 | /* The caller provides a supported format, as verified per call to |
| 339 | * icd->try_fmt(), datawidth is from our supported format list */ |
| 340 | switch (pix->pixelformat) { |
| 341 | case V4L2_PIX_FMT_GREY: |
| 342 | case V4L2_PIX_FMT_Y16: |
| 343 | if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM) |
| 344 | return -EINVAL; |
| 345 | break; |
| 346 | case V4L2_PIX_FMT_SBGGR8: |
| 347 | case V4L2_PIX_FMT_SBGGR16: |
| 348 | if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC) |
| 349 | return -EINVAL; |
| 350 | break; |
| 351 | case 0: |
| 352 | /* No format change, only geometry */ |
| 353 | break; |
| 354 | default: |
| 355 | return -EINVAL; |
| 356 | } |
| 357 | |
| 358 | /* No support for scaling on this camera, just crop. */ |
| 359 | return mt9v022_set_crop(icd, &rect); |
| 360 | } |
| 361 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 362 | static int mt9v022_try_fmt(struct soc_camera_device *icd, |
| 363 | struct v4l2_format *f) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 364 | { |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 365 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 366 | |
Trent Piepho | 653dc59 | 2009-05-30 21:45:46 -0300 | [diff] [blame] | 367 | v4l_bound_align_image(&pix->width, 48, 752, 2 /* ? */, |
| 368 | &pix->height, 32 + icd->y_skip_top, |
| 369 | 480 + icd->y_skip_top, 0, 0); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int mt9v022_get_chip_id(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 375 | struct v4l2_dbg_chip_ident *id) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 376 | { |
| 377 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 378 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 379 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 382 | if (id->match.addr != mt9v022->client->addr) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 383 | return -ENODEV; |
| 384 | |
| 385 | id->ident = mt9v022->model; |
| 386 | id->revision = 0; |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 392 | static int mt9v022_get_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 393 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 394 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 395 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 396 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 397 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 398 | return -EINVAL; |
| 399 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 400 | if (reg->match.addr != client->addr) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 401 | return -ENODEV; |
| 402 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 403 | reg->size = 2; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 404 | reg->val = reg_read(client, reg->reg); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 405 | |
| 406 | if (reg->val > 0xffff) |
| 407 | return -EIO; |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int mt9v022_set_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 413 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 414 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 415 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 416 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 417 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 418 | return -EINVAL; |
| 419 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 420 | if (reg->match.addr != client->addr) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 421 | return -ENODEV; |
| 422 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 423 | if (reg_write(client, reg->reg, reg->val) < 0) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 424 | return -EIO; |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | #endif |
| 429 | |
Adrian Bunk | 4407a46 | 2008-04-28 17:13:51 -0300 | [diff] [blame] | 430 | static const struct v4l2_queryctrl mt9v022_controls[] = { |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 431 | { |
| 432 | .id = V4L2_CID_VFLIP, |
| 433 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 434 | .name = "Flip Vertically", |
| 435 | .minimum = 0, |
| 436 | .maximum = 1, |
| 437 | .step = 1, |
| 438 | .default_value = 0, |
| 439 | }, { |
| 440 | .id = V4L2_CID_HFLIP, |
| 441 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 442 | .name = "Flip Horizontally", |
| 443 | .minimum = 0, |
| 444 | .maximum = 1, |
| 445 | .step = 1, |
| 446 | .default_value = 0, |
| 447 | }, { |
| 448 | .id = V4L2_CID_GAIN, |
| 449 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 450 | .name = "Analog Gain", |
| 451 | .minimum = 64, |
| 452 | .maximum = 127, |
| 453 | .step = 1, |
| 454 | .default_value = 64, |
| 455 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 456 | }, { |
| 457 | .id = V4L2_CID_EXPOSURE, |
| 458 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 459 | .name = "Exposure", |
| 460 | .minimum = 1, |
| 461 | .maximum = 255, |
| 462 | .step = 1, |
| 463 | .default_value = 255, |
| 464 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 465 | }, { |
| 466 | .id = V4L2_CID_AUTOGAIN, |
| 467 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 468 | .name = "Automatic Gain", |
| 469 | .minimum = 0, |
| 470 | .maximum = 1, |
| 471 | .step = 1, |
| 472 | .default_value = 1, |
| 473 | }, { |
| 474 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 475 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 476 | .name = "Automatic Exposure", |
| 477 | .minimum = 0, |
| 478 | .maximum = 1, |
| 479 | .step = 1, |
| 480 | .default_value = 1, |
| 481 | } |
| 482 | }; |
| 483 | |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 484 | static int mt9v022_video_probe(struct soc_camera_device *); |
| 485 | static void mt9v022_video_remove(struct soc_camera_device *); |
| 486 | static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *); |
| 487 | static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 488 | |
| 489 | static struct soc_camera_ops mt9v022_ops = { |
| 490 | .owner = THIS_MODULE, |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 491 | .probe = mt9v022_video_probe, |
| 492 | .remove = mt9v022_video_remove, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 493 | .init = mt9v022_init, |
| 494 | .release = mt9v022_release, |
| 495 | .start_capture = mt9v022_start_capture, |
| 496 | .stop_capture = mt9v022_stop_capture, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 497 | .set_crop = mt9v022_set_crop, |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 498 | .set_fmt = mt9v022_set_fmt, |
| 499 | .try_fmt = mt9v022_try_fmt, |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 500 | .set_bus_param = mt9v022_set_bus_param, |
| 501 | .query_bus_param = mt9v022_query_bus_param, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 502 | .controls = mt9v022_controls, |
| 503 | .num_controls = ARRAY_SIZE(mt9v022_controls), |
| 504 | .get_control = mt9v022_get_control, |
| 505 | .set_control = mt9v022_set_control, |
| 506 | .get_chip_id = mt9v022_get_chip_id, |
| 507 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 508 | .get_register = mt9v022_get_register, |
| 509 | .set_register = mt9v022_set_register, |
| 510 | #endif |
| 511 | }; |
| 512 | |
| 513 | static int mt9v022_get_control(struct soc_camera_device *icd, |
| 514 | struct v4l2_control *ctrl) |
| 515 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 516 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 517 | int data; |
| 518 | |
| 519 | switch (ctrl->id) { |
| 520 | case V4L2_CID_VFLIP: |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 521 | data = reg_read(client, MT9V022_READ_MODE); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 522 | if (data < 0) |
| 523 | return -EIO; |
| 524 | ctrl->value = !!(data & 0x10); |
| 525 | break; |
| 526 | case V4L2_CID_HFLIP: |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 527 | data = reg_read(client, MT9V022_READ_MODE); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 528 | if (data < 0) |
| 529 | return -EIO; |
| 530 | ctrl->value = !!(data & 0x20); |
| 531 | break; |
| 532 | case V4L2_CID_EXPOSURE_AUTO: |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 533 | data = reg_read(client, MT9V022_AEC_AGC_ENABLE); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 534 | if (data < 0) |
| 535 | return -EIO; |
| 536 | ctrl->value = !!(data & 0x1); |
| 537 | break; |
| 538 | case V4L2_CID_AUTOGAIN: |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 539 | data = reg_read(client, MT9V022_AEC_AGC_ENABLE); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 540 | if (data < 0) |
| 541 | return -EIO; |
| 542 | ctrl->value = !!(data & 0x2); |
| 543 | break; |
| 544 | } |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int mt9v022_set_control(struct soc_camera_device *icd, |
| 549 | struct v4l2_control *ctrl) |
| 550 | { |
| 551 | int data; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 552 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 553 | const struct v4l2_queryctrl *qctrl; |
| 554 | |
| 555 | qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id); |
| 556 | |
| 557 | if (!qctrl) |
| 558 | return -EINVAL; |
| 559 | |
| 560 | switch (ctrl->id) { |
| 561 | case V4L2_CID_VFLIP: |
| 562 | if (ctrl->value) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 563 | data = reg_set(client, MT9V022_READ_MODE, 0x10); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 564 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 565 | data = reg_clear(client, MT9V022_READ_MODE, 0x10); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 566 | if (data < 0) |
| 567 | return -EIO; |
| 568 | break; |
| 569 | case V4L2_CID_HFLIP: |
| 570 | if (ctrl->value) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 571 | data = reg_set(client, MT9V022_READ_MODE, 0x20); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 572 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 573 | data = reg_clear(client, MT9V022_READ_MODE, 0x20); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 574 | if (data < 0) |
| 575 | return -EIO; |
| 576 | break; |
| 577 | case V4L2_CID_GAIN: |
| 578 | /* mt9v022 has minimum == default */ |
| 579 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 580 | return -EINVAL; |
| 581 | else { |
| 582 | unsigned long range = qctrl->maximum - qctrl->minimum; |
| 583 | /* Datasheet says 16 to 64. autogain only works properly |
| 584 | * after setting gain to maximum 14. Larger values |
| 585 | * produce "white fly" noise effect. On the whole, |
| 586 | * manually setting analog gain does no good. */ |
| 587 | unsigned long gain = ((ctrl->value - qctrl->minimum) * |
| 588 | 10 + range / 2) / range + 4; |
| 589 | if (gain >= 32) |
| 590 | gain &= ~1; |
| 591 | /* The user wants to set gain manually, hope, she |
| 592 | * knows, what she's doing... Switch AGC off. */ |
| 593 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 594 | if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 595 | return -EIO; |
| 596 | |
| 597 | dev_info(&icd->dev, "Setting gain from %d to %lu\n", |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 598 | reg_read(client, MT9V022_ANALOG_GAIN), gain); |
| 599 | if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 600 | return -EIO; |
| 601 | icd->gain = ctrl->value; |
| 602 | } |
| 603 | break; |
| 604 | case V4L2_CID_EXPOSURE: |
| 605 | /* mt9v022 has maximum == default */ |
| 606 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 607 | return -EINVAL; |
| 608 | else { |
| 609 | unsigned long range = qctrl->maximum - qctrl->minimum; |
| 610 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * |
| 611 | 479 + range / 2) / range + 1; |
| 612 | /* The user wants to set shutter width manually, hope, |
| 613 | * she knows, what she's doing... Switch AEC off. */ |
| 614 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 615 | if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 616 | return -EIO; |
| 617 | |
| 618 | dev_dbg(&icd->dev, "Shutter width from %d to %lu\n", |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 619 | reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH), |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 620 | shutter); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 621 | if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 622 | shutter) < 0) |
| 623 | return -EIO; |
| 624 | icd->exposure = ctrl->value; |
| 625 | } |
| 626 | break; |
| 627 | case V4L2_CID_AUTOGAIN: |
| 628 | if (ctrl->value) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 629 | data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 630 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 631 | data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 632 | if (data < 0) |
| 633 | return -EIO; |
| 634 | break; |
| 635 | case V4L2_CID_EXPOSURE_AUTO: |
| 636 | if (ctrl->value) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 637 | data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 638 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 639 | data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 640 | if (data < 0) |
| 641 | return -EIO; |
| 642 | break; |
| 643 | } |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | /* Interface active, can use i2c. If it fails, it can indeed mean, that |
| 648 | * this wasn't our capture interface, so, we wait for the right one */ |
| 649 | static int mt9v022_video_probe(struct soc_camera_device *icd) |
| 650 | { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 651 | struct i2c_client *client = to_i2c_client(icd->control); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 652 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 653 | struct soc_camera_link *icl = client->dev.platform_data; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 654 | s32 data; |
| 655 | int ret; |
Sascha Hauer | e958e27 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 656 | unsigned long flags; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 657 | |
| 658 | if (!icd->dev.parent || |
| 659 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 660 | return -ENODEV; |
| 661 | |
| 662 | /* Read out the chip version register */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 663 | data = reg_read(client, MT9V022_CHIP_VERSION); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 664 | |
| 665 | /* must be 0x1311 or 0x1313 */ |
| 666 | if (data != 0x1311 && data != 0x1313) { |
| 667 | ret = -ENODEV; |
| 668 | dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n", |
| 669 | data); |
| 670 | goto ei2c; |
| 671 | } |
| 672 | |
| 673 | /* Soft reset */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 674 | ret = reg_write(client, MT9V022_RESET, 1); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 675 | if (ret < 0) |
| 676 | goto ei2c; |
| 677 | /* 15 clock cycles */ |
| 678 | udelay(200); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 679 | if (reg_read(client, MT9V022_RESET)) { |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 680 | dev_err(&icd->dev, "Resetting MT9V022 failed!\n"); |
| 681 | goto ei2c; |
| 682 | } |
| 683 | |
| 684 | /* Set monochrome or colour sensor type */ |
| 685 | if (sensor_type && (!strcmp("colour", sensor_type) || |
| 686 | !strcmp("color", sensor_type))) { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 687 | ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 688 | mt9v022->model = V4L2_IDENT_MT9V022IX7ATC; |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 689 | icd->formats = mt9v022_colour_formats; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 690 | } else { |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 691 | ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 692 | mt9v022->model = V4L2_IDENT_MT9V022IX7ATM; |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 693 | icd->formats = mt9v022_monochrome_formats; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 694 | } |
| 695 | |
Sascha Hauer | e958e27 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 696 | if (ret < 0) |
| 697 | goto eisis; |
| 698 | |
| 699 | icd->num_formats = 0; |
| 700 | |
| 701 | /* |
| 702 | * This is a 10bit sensor, so by default we only allow 10bit. |
| 703 | * The platform may support different bus widths due to |
| 704 | * different routing of the data lines. |
| 705 | */ |
| 706 | if (icl->query_bus_param) |
| 707 | flags = icl->query_bus_param(icl); |
| 708 | else |
| 709 | flags = SOCAM_DATAWIDTH_10; |
| 710 | |
| 711 | if (flags & SOCAM_DATAWIDTH_10) |
| 712 | icd->num_formats++; |
| 713 | else |
| 714 | icd->formats++; |
| 715 | |
| 716 | if (flags & SOCAM_DATAWIDTH_8) |
| 717 | icd->num_formats++; |
| 718 | |
| 719 | ret = soc_camera_video_start(icd); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 720 | if (ret < 0) |
| 721 | goto eisis; |
| 722 | |
| 723 | dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n", |
| 724 | data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ? |
| 725 | "monochrome" : "colour"); |
| 726 | |
| 727 | return 0; |
| 728 | |
| 729 | eisis: |
| 730 | ei2c: |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | static void mt9v022_video_remove(struct soc_camera_device *icd) |
| 735 | { |
| 736 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
Guennadi Liakhovetski | 594bb46 | 2009-04-24 12:53:51 -0300 | [diff] [blame] | 737 | struct soc_camera_link *icl = mt9v022->client->dev.platform_data; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 738 | |
| 739 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr, |
Guennadi Liakhovetski | a85bdac | 2008-12-18 12:54:33 -0300 | [diff] [blame] | 740 | icd->dev.parent, icd->vdev); |
| 741 | soc_camera_video_stop(icd); |
Guennadi Liakhovetski | 594bb46 | 2009-04-24 12:53:51 -0300 | [diff] [blame] | 742 | if (icl->free_bus) |
| 743 | icl->free_bus(icl); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 744 | } |
| 745 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 746 | static int mt9v022_probe(struct i2c_client *client, |
| 747 | const struct i2c_device_id *did) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 748 | { |
| 749 | struct mt9v022 *mt9v022; |
| 750 | struct soc_camera_device *icd; |
| 751 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 752 | struct soc_camera_link *icl = client->dev.platform_data; |
| 753 | int ret; |
| 754 | |
| 755 | if (!icl) { |
| 756 | dev_err(&client->dev, "MT9V022 driver needs platform data\n"); |
| 757 | return -EINVAL; |
| 758 | } |
| 759 | |
| 760 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 761 | dev_warn(&adapter->dev, |
| 762 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 763 | return -EIO; |
| 764 | } |
| 765 | |
| 766 | mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL); |
| 767 | if (!mt9v022) |
| 768 | return -ENOMEM; |
| 769 | |
| 770 | mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT; |
| 771 | mt9v022->client = client; |
| 772 | i2c_set_clientdata(client, mt9v022); |
| 773 | |
| 774 | icd = &mt9v022->icd; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 775 | icd->ops = &mt9v022_ops; |
| 776 | icd->control = &client->dev; |
| 777 | icd->x_min = 1; |
| 778 | icd->y_min = 4; |
| 779 | icd->x_current = 1; |
| 780 | icd->y_current = 4; |
| 781 | icd->width_min = 48; |
| 782 | icd->width_max = 752; |
| 783 | icd->height_min = 32; |
| 784 | icd->height_max = 480; |
| 785 | icd->y_skip_top = 1; |
| 786 | icd->iface = icl->bus_id; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 787 | |
| 788 | ret = soc_camera_device_register(icd); |
| 789 | if (ret) |
| 790 | goto eisdr; |
| 791 | |
| 792 | return 0; |
| 793 | |
| 794 | eisdr: |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 795 | kfree(mt9v022); |
| 796 | return ret; |
| 797 | } |
| 798 | |
| 799 | static int mt9v022_remove(struct i2c_client *client) |
| 800 | { |
| 801 | struct mt9v022 *mt9v022 = i2c_get_clientdata(client); |
| 802 | |
| 803 | soc_camera_device_unregister(&mt9v022->icd); |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 804 | kfree(mt9v022); |
| 805 | |
| 806 | return 0; |
| 807 | } |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 808 | static const struct i2c_device_id mt9v022_id[] = { |
| 809 | { "mt9v022", 0 }, |
| 810 | { } |
| 811 | }; |
| 812 | MODULE_DEVICE_TABLE(i2c, mt9v022_id); |
| 813 | |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 814 | static struct i2c_driver mt9v022_i2c_driver = { |
| 815 | .driver = { |
| 816 | .name = "mt9v022", |
| 817 | }, |
| 818 | .probe = mt9v022_probe, |
| 819 | .remove = mt9v022_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 820 | .id_table = mt9v022_id, |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 821 | }; |
| 822 | |
| 823 | static int __init mt9v022_mod_init(void) |
| 824 | { |
| 825 | return i2c_add_driver(&mt9v022_i2c_driver); |
| 826 | } |
| 827 | |
| 828 | static void __exit mt9v022_mod_exit(void) |
| 829 | { |
| 830 | i2c_del_driver(&mt9v022_i2c_driver); |
| 831 | } |
| 832 | |
| 833 | module_init(mt9v022_mod_init); |
| 834 | module_exit(mt9v022_mod_exit); |
| 835 | |
| 836 | MODULE_DESCRIPTION("Micron MT9V022 Camera driver"); |
| 837 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 838 | MODULE_LICENSE("GPL"); |