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