Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9M001 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/log2.h> |
| 15 | |
| 16 | #include <media/v4l2-common.h> |
| 17 | #include <media/v4l2-chip-ident.h> |
| 18 | #include <media/soc_camera.h> |
| 19 | |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 20 | /* mt9m001 i2c address 0x5d |
| 21 | * The platform has to define i2c_board_info |
| 22 | * and call i2c_register_board_info() */ |
| 23 | |
| 24 | /* mt9m001 selected register addresses */ |
| 25 | #define MT9M001_CHIP_VERSION 0x00 |
| 26 | #define MT9M001_ROW_START 0x01 |
| 27 | #define MT9M001_COLUMN_START 0x02 |
| 28 | #define MT9M001_WINDOW_HEIGHT 0x03 |
| 29 | #define MT9M001_WINDOW_WIDTH 0x04 |
| 30 | #define MT9M001_HORIZONTAL_BLANKING 0x05 |
| 31 | #define MT9M001_VERTICAL_BLANKING 0x06 |
| 32 | #define MT9M001_OUTPUT_CONTROL 0x07 |
| 33 | #define MT9M001_SHUTTER_WIDTH 0x09 |
| 34 | #define MT9M001_FRAME_RESTART 0x0b |
| 35 | #define MT9M001_SHUTTER_DELAY 0x0c |
| 36 | #define MT9M001_RESET 0x0d |
| 37 | #define MT9M001_READ_OPTIONS1 0x1e |
| 38 | #define MT9M001_READ_OPTIONS2 0x20 |
| 39 | #define MT9M001_GLOBAL_GAIN 0x35 |
| 40 | #define MT9M001_CHIP_ENABLE 0xF1 |
| 41 | |
| 42 | static const struct soc_camera_data_format mt9m001_colour_formats[] = { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 43 | /* Order important: first natively supported, |
| 44 | * second supported with a GPIO extender */ |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 45 | { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 46 | .name = "Bayer (sRGB) 10 bit", |
| 47 | .depth = 10, |
| 48 | .fourcc = V4L2_PIX_FMT_SBGGR16, |
| 49 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 50 | }, { |
| 51 | .name = "Bayer (sRGB) 8 bit", |
| 52 | .depth = 8, |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 53 | .fourcc = V4L2_PIX_FMT_SBGGR8, |
| 54 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | static const struct soc_camera_data_format mt9m001_monochrome_formats[] = { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 59 | /* Order important - see above */ |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 60 | { |
| 61 | .name = "Monochrome 10 bit", |
| 62 | .depth = 10, |
| 63 | .fourcc = V4L2_PIX_FMT_Y16, |
| 64 | }, { |
| 65 | .name = "Monochrome 8 bit", |
| 66 | .depth = 8, |
| 67 | .fourcc = V4L2_PIX_FMT_GREY, |
| 68 | }, |
| 69 | }; |
| 70 | |
| 71 | struct mt9m001 { |
| 72 | struct i2c_client *client; |
| 73 | struct soc_camera_device icd; |
| 74 | int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 75 | unsigned char autoexposure; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static int reg_read(struct soc_camera_device *icd, const u8 reg) |
| 79 | { |
| 80 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 81 | struct i2c_client *client = mt9m001->client; |
| 82 | s32 data = i2c_smbus_read_word_data(client, reg); |
| 83 | return data < 0 ? data : swab16(data); |
| 84 | } |
| 85 | |
| 86 | static int reg_write(struct soc_camera_device *icd, const u8 reg, |
| 87 | const u16 data) |
| 88 | { |
| 89 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 90 | return i2c_smbus_write_word_data(mt9m001->client, reg, swab16(data)); |
| 91 | } |
| 92 | |
| 93 | static int reg_set(struct soc_camera_device *icd, const u8 reg, |
| 94 | const u16 data) |
| 95 | { |
| 96 | int ret; |
| 97 | |
| 98 | ret = reg_read(icd, reg); |
| 99 | if (ret < 0) |
| 100 | return ret; |
| 101 | return reg_write(icd, reg, ret | data); |
| 102 | } |
| 103 | |
| 104 | static int reg_clear(struct soc_camera_device *icd, const u8 reg, |
| 105 | const u16 data) |
| 106 | { |
| 107 | int ret; |
| 108 | |
| 109 | ret = reg_read(icd, reg); |
| 110 | if (ret < 0) |
| 111 | return ret; |
| 112 | return reg_write(icd, reg, ret & ~data); |
| 113 | } |
| 114 | |
| 115 | static int mt9m001_init(struct soc_camera_device *icd) |
| 116 | { |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 117 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 118 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 119 | int ret; |
| 120 | |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 121 | dev_dbg(icd->vdev->parent, "%s\n", __func__); |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 122 | |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 123 | if (icl->power) { |
| 124 | ret = icl->power(&mt9m001->client->dev, 1); |
| 125 | if (ret < 0) { |
| 126 | dev_err(icd->vdev->parent, |
| 127 | "Platform failed to power-on the camera.\n"); |
| 128 | return ret; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* The camera could have been already on, we reset it additionally */ |
| 133 | if (icl->reset) |
| 134 | ret = icl->reset(&mt9m001->client->dev); |
| 135 | else |
| 136 | ret = -ENODEV; |
| 137 | |
| 138 | if (ret < 0) { |
| 139 | /* Either no platform reset, or platform reset failed */ |
| 140 | ret = reg_write(icd, MT9M001_RESET, 1); |
| 141 | if (!ret) |
| 142 | ret = reg_write(icd, MT9M001_RESET, 0); |
| 143 | } |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 144 | /* Disable chip, synchronous option update */ |
| 145 | if (!ret) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 146 | ret = reg_write(icd, MT9M001_OUTPUT_CONTROL, 0); |
| 147 | |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 148 | return ret; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int mt9m001_release(struct soc_camera_device *icd) |
| 152 | { |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 153 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 154 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
| 155 | |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 156 | /* Disable the chip */ |
| 157 | reg_write(icd, MT9M001_OUTPUT_CONTROL, 0); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 158 | |
| 159 | if (icl->power) |
| 160 | icl->power(&mt9m001->client->dev, 0); |
| 161 | |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int mt9m001_start_capture(struct soc_camera_device *icd) |
| 166 | { |
| 167 | /* Switch to master "normal" mode */ |
| 168 | if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 2) < 0) |
| 169 | return -EIO; |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int mt9m001_stop_capture(struct soc_camera_device *icd) |
| 174 | { |
| 175 | /* Stop sensor readout */ |
| 176 | if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 0) < 0) |
| 177 | return -EIO; |
| 178 | return 0; |
| 179 | } |
| 180 | |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 181 | static int mt9m001_set_bus_param(struct soc_camera_device *icd, |
| 182 | unsigned long flags) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 183 | { |
| 184 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 185 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
| 186 | unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 187 | |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 188 | /* Only one width bit may be set */ |
| 189 | if (!is_power_of_2(width_flag)) |
| 190 | return -EINVAL; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 191 | |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 192 | if (icl->set_bus_param) |
| 193 | return icl->set_bus_param(icl, width_flag); |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 194 | |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 195 | /* |
| 196 | * Without board specific bus width settings we only support the |
| 197 | * sensors native bus width |
| 198 | */ |
| 199 | if (width_flag == SOCAM_DATAWIDTH_10) |
| 200 | return 0; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 201 | |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 202 | return -EINVAL; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd) |
| 206 | { |
| 207 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 208 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
| 209 | /* MT9M001 has all capture_format parameters fixed */ |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 210 | unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 211 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
Guennadi Liakhovetski | 2d9329f | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 212 | SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 213 | |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 214 | if (icl->query_bus_param) |
| 215 | flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK; |
| 216 | else |
| 217 | flags |= SOCAM_DATAWIDTH_10; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 218 | |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 219 | return soc_camera_apply_sensor_flags(icl, flags); |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 220 | } |
| 221 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 222 | static int mt9m001_set_crop(struct soc_camera_device *icd, |
| 223 | struct v4l2_rect *rect) |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 224 | { |
| 225 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 226 | int ret; |
| 227 | const u16 hblank = 9, vblank = 25; |
| 228 | |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 229 | /* Blanking and start values - default... */ |
| 230 | ret = reg_write(icd, MT9M001_HORIZONTAL_BLANKING, hblank); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 231 | if (!ret) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 232 | ret = reg_write(icd, MT9M001_VERTICAL_BLANKING, vblank); |
| 233 | |
| 234 | /* The caller provides a supported format, as verified per |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 235 | * call to icd->try_fmt() */ |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 236 | if (!ret) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 237 | ret = reg_write(icd, MT9M001_COLUMN_START, rect->left); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 238 | if (!ret) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 239 | ret = reg_write(icd, MT9M001_ROW_START, rect->top); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 240 | if (!ret) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 241 | ret = reg_write(icd, MT9M001_WINDOW_WIDTH, rect->width - 1); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 242 | if (!ret) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 243 | ret = reg_write(icd, MT9M001_WINDOW_HEIGHT, |
| 244 | rect->height + icd->y_skip_top - 1); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 245 | if (!ret && mt9m001->autoexposure) { |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 246 | ret = reg_write(icd, MT9M001_SHUTTER_WIDTH, |
| 247 | rect->height + icd->y_skip_top + vblank); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 248 | if (!ret) { |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 249 | const struct v4l2_queryctrl *qctrl = |
| 250 | soc_camera_find_qctrl(icd->ops, |
| 251 | V4L2_CID_EXPOSURE); |
| 252 | icd->exposure = (524 + (rect->height + icd->y_skip_top + |
| 253 | vblank - 1) * |
| 254 | (qctrl->maximum - qctrl->minimum)) / |
| 255 | 1048 + qctrl->minimum; |
| 256 | } |
| 257 | } |
| 258 | |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 259 | return ret; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 260 | } |
| 261 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 262 | static int mt9m001_set_fmt(struct soc_camera_device *icd, |
| 263 | struct v4l2_format *f) |
| 264 | { |
| 265 | struct v4l2_rect rect = { |
| 266 | .left = icd->x_current, |
| 267 | .top = icd->y_current, |
| 268 | .width = f->fmt.pix.width, |
| 269 | .height = f->fmt.pix.height, |
| 270 | }; |
| 271 | |
| 272 | /* No support for scaling so far, just crop. TODO: use skipping */ |
| 273 | return mt9m001_set_crop(icd, &rect); |
| 274 | } |
| 275 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 276 | static int mt9m001_try_fmt(struct soc_camera_device *icd, |
| 277 | struct v4l2_format *f) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 278 | { |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 279 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 280 | |
| 281 | if (pix->height < 32 + icd->y_skip_top) |
| 282 | pix->height = 32 + icd->y_skip_top; |
| 283 | if (pix->height > 1024 + icd->y_skip_top) |
| 284 | pix->height = 1024 + icd->y_skip_top; |
| 285 | if (pix->width < 48) |
| 286 | pix->width = 48; |
| 287 | if (pix->width > 1280) |
| 288 | pix->width = 1280; |
| 289 | pix->width &= ~0x01; /* has to be even, unsure why was ~3 */ |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int mt9m001_get_chip_id(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 295 | struct v4l2_dbg_chip_ident *id) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 296 | { |
| 297 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 298 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 299 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 300 | return -EINVAL; |
| 301 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 302 | if (id->match.addr != mt9m001->client->addr) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 303 | return -ENODEV; |
| 304 | |
| 305 | id->ident = mt9m001->model; |
| 306 | id->revision = 0; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 312 | static int mt9m001_get_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 313 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 314 | { |
| 315 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 316 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 317 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 318 | return -EINVAL; |
| 319 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 320 | if (reg->match.addr != mt9m001->client->addr) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 321 | return -ENODEV; |
| 322 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 323 | reg->size = 2; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 324 | reg->val = reg_read(icd, reg->reg); |
| 325 | |
| 326 | if (reg->val > 0xffff) |
| 327 | return -EIO; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int mt9m001_set_register(struct soc_camera_device *icd, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 333 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 334 | { |
| 335 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 336 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 337 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 338 | return -EINVAL; |
| 339 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 340 | if (reg->match.addr != mt9m001->client->addr) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 341 | return -ENODEV; |
| 342 | |
| 343 | if (reg_write(icd, reg->reg, reg->val) < 0) |
| 344 | return -EIO; |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | #endif |
| 349 | |
Adrian Bunk | 4407a46 | 2008-04-28 17:13:51 -0300 | [diff] [blame] | 350 | static const struct v4l2_queryctrl mt9m001_controls[] = { |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 351 | { |
| 352 | .id = V4L2_CID_VFLIP, |
| 353 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 354 | .name = "Flip Vertically", |
| 355 | .minimum = 0, |
| 356 | .maximum = 1, |
| 357 | .step = 1, |
| 358 | .default_value = 0, |
| 359 | }, { |
| 360 | .id = V4L2_CID_GAIN, |
| 361 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 362 | .name = "Gain", |
| 363 | .minimum = 0, |
| 364 | .maximum = 127, |
| 365 | .step = 1, |
| 366 | .default_value = 64, |
| 367 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 368 | }, { |
| 369 | .id = V4L2_CID_EXPOSURE, |
| 370 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 371 | .name = "Exposure", |
| 372 | .minimum = 1, |
| 373 | .maximum = 255, |
| 374 | .step = 1, |
| 375 | .default_value = 255, |
| 376 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 377 | }, { |
| 378 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 379 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 380 | .name = "Automatic Exposure", |
| 381 | .minimum = 0, |
| 382 | .maximum = 1, |
| 383 | .step = 1, |
| 384 | .default_value = 1, |
| 385 | } |
| 386 | }; |
| 387 | |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 388 | static int mt9m001_video_probe(struct soc_camera_device *); |
| 389 | static void mt9m001_video_remove(struct soc_camera_device *); |
| 390 | static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *); |
| 391 | static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *); |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 392 | |
| 393 | static struct soc_camera_ops mt9m001_ops = { |
| 394 | .owner = THIS_MODULE, |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 395 | .probe = mt9m001_video_probe, |
| 396 | .remove = mt9m001_video_remove, |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 397 | .init = mt9m001_init, |
| 398 | .release = mt9m001_release, |
| 399 | .start_capture = mt9m001_start_capture, |
| 400 | .stop_capture = mt9m001_stop_capture, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 401 | .set_crop = mt9m001_set_crop, |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 402 | .set_fmt = mt9m001_set_fmt, |
| 403 | .try_fmt = mt9m001_try_fmt, |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 404 | .set_bus_param = mt9m001_set_bus_param, |
| 405 | .query_bus_param = mt9m001_query_bus_param, |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 406 | .controls = mt9m001_controls, |
| 407 | .num_controls = ARRAY_SIZE(mt9m001_controls), |
| 408 | .get_control = mt9m001_get_control, |
| 409 | .set_control = mt9m001_set_control, |
| 410 | .get_chip_id = mt9m001_get_chip_id, |
| 411 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 412 | .get_register = mt9m001_get_register, |
| 413 | .set_register = mt9m001_set_register, |
| 414 | #endif |
| 415 | }; |
| 416 | |
| 417 | static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) |
| 418 | { |
| 419 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 420 | int data; |
| 421 | |
| 422 | switch (ctrl->id) { |
| 423 | case V4L2_CID_VFLIP: |
| 424 | data = reg_read(icd, MT9M001_READ_OPTIONS2); |
| 425 | if (data < 0) |
| 426 | return -EIO; |
| 427 | ctrl->value = !!(data & 0x8000); |
| 428 | break; |
| 429 | case V4L2_CID_EXPOSURE_AUTO: |
| 430 | ctrl->value = mt9m001->autoexposure; |
| 431 | break; |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) |
| 437 | { |
| 438 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 439 | const struct v4l2_queryctrl *qctrl; |
| 440 | int data; |
| 441 | |
| 442 | qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id); |
| 443 | |
| 444 | if (!qctrl) |
| 445 | return -EINVAL; |
| 446 | |
| 447 | switch (ctrl->id) { |
| 448 | case V4L2_CID_VFLIP: |
| 449 | if (ctrl->value) |
| 450 | data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000); |
| 451 | else |
| 452 | data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000); |
| 453 | if (data < 0) |
| 454 | return -EIO; |
| 455 | break; |
| 456 | case V4L2_CID_GAIN: |
| 457 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 458 | return -EINVAL; |
| 459 | /* See Datasheet Table 7, Gain settings. */ |
| 460 | if (ctrl->value <= qctrl->default_value) { |
| 461 | /* Pack it into 0..1 step 0.125, register values 0..8 */ |
| 462 | unsigned long range = qctrl->default_value - qctrl->minimum; |
| 463 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; |
| 464 | |
| 465 | dev_dbg(&icd->dev, "Setting gain %d\n", data); |
| 466 | data = reg_write(icd, MT9M001_GLOBAL_GAIN, data); |
| 467 | if (data < 0) |
| 468 | return -EIO; |
| 469 | } else { |
| 470 | /* Pack it into 1.125..15 variable step, register values 9..67 */ |
| 471 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ |
| 472 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; |
| 473 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * |
| 474 | 111 + range / 2) / range + 9; |
| 475 | |
| 476 | if (gain <= 32) |
| 477 | data = gain; |
| 478 | else if (gain <= 64) |
| 479 | data = ((gain - 32) * 16 + 16) / 32 + 80; |
| 480 | else |
| 481 | data = ((gain - 64) * 7 + 28) / 56 + 96; |
| 482 | |
| 483 | dev_dbg(&icd->dev, "Setting gain from %d to %d\n", |
| 484 | reg_read(icd, MT9M001_GLOBAL_GAIN), data); |
| 485 | data = reg_write(icd, MT9M001_GLOBAL_GAIN, data); |
| 486 | if (data < 0) |
| 487 | return -EIO; |
| 488 | } |
| 489 | |
| 490 | /* Success */ |
| 491 | icd->gain = ctrl->value; |
| 492 | break; |
| 493 | case V4L2_CID_EXPOSURE: |
| 494 | /* mt9m001 has maximum == default */ |
| 495 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 496 | return -EINVAL; |
| 497 | else { |
| 498 | unsigned long range = qctrl->maximum - qctrl->minimum; |
| 499 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 + |
| 500 | range / 2) / range + 1; |
| 501 | |
| 502 | dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n", |
| 503 | reg_read(icd, MT9M001_SHUTTER_WIDTH), shutter); |
| 504 | if (reg_write(icd, MT9M001_SHUTTER_WIDTH, shutter) < 0) |
| 505 | return -EIO; |
| 506 | icd->exposure = ctrl->value; |
| 507 | mt9m001->autoexposure = 0; |
| 508 | } |
| 509 | break; |
| 510 | case V4L2_CID_EXPOSURE_AUTO: |
| 511 | if (ctrl->value) { |
| 512 | const u16 vblank = 25; |
| 513 | if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height + |
| 514 | icd->y_skip_top + vblank) < 0) |
| 515 | return -EIO; |
| 516 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); |
| 517 | icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) * |
| 518 | (qctrl->maximum - qctrl->minimum)) / |
| 519 | 1048 + qctrl->minimum; |
| 520 | mt9m001->autoexposure = 1; |
| 521 | } else |
| 522 | mt9m001->autoexposure = 0; |
| 523 | break; |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* Interface active, can use i2c. If it fails, it can indeed mean, that |
| 529 | * this wasn't our capture interface, so, we wait for the right one */ |
| 530 | static int mt9m001_video_probe(struct soc_camera_device *icd) |
| 531 | { |
| 532 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
Guennadi Liakhovetski | 297a7ef | 2008-12-17 14:05:38 -0300 | [diff] [blame] | 533 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 534 | s32 data; |
| 535 | int ret; |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 536 | unsigned long flags; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 537 | |
| 538 | /* We must have a parent by now. And it cannot be a wrong one. |
| 539 | * So this entire test is completely redundant. */ |
| 540 | if (!icd->dev.parent || |
| 541 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 542 | return -ENODEV; |
| 543 | |
| 544 | /* Enable the chip */ |
Guennadi Liakhovetski | a85bdac | 2008-12-18 12:54:33 -0300 | [diff] [blame] | 545 | data = reg_write(icd, MT9M001_CHIP_ENABLE, 1); |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 546 | dev_dbg(&icd->dev, "write: %d\n", data); |
| 547 | |
| 548 | /* Read out the chip version register */ |
| 549 | data = reg_read(icd, MT9M001_CHIP_VERSION); |
| 550 | |
| 551 | /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ |
| 552 | switch (data) { |
| 553 | case 0x8411: |
| 554 | case 0x8421: |
| 555 | mt9m001->model = V4L2_IDENT_MT9M001C12ST; |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 556 | icd->formats = mt9m001_colour_formats; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 557 | break; |
| 558 | case 0x8431: |
| 559 | mt9m001->model = V4L2_IDENT_MT9M001C12STM; |
Guennadi Liakhovetski | 26f1b94 | 2008-03-24 12:18:36 -0300 | [diff] [blame] | 560 | icd->formats = mt9m001_monochrome_formats; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 561 | break; |
| 562 | default: |
| 563 | ret = -ENODEV; |
| 564 | dev_err(&icd->dev, |
| 565 | "No MT9M001 chip detected, register read %x\n", data); |
| 566 | goto ei2c; |
| 567 | } |
| 568 | |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame^] | 569 | icd->num_formats = 0; |
| 570 | |
| 571 | /* |
| 572 | * This is a 10bit sensor, so by default we only allow 10bit. |
| 573 | * The platform may support different bus widths due to |
| 574 | * different routing of the data lines. |
| 575 | */ |
| 576 | if (icl->query_bus_param) |
| 577 | flags = icl->query_bus_param(icl); |
| 578 | else |
| 579 | flags = SOCAM_DATAWIDTH_10; |
| 580 | |
| 581 | if (flags & SOCAM_DATAWIDTH_10) |
| 582 | icd->num_formats++; |
| 583 | else |
| 584 | icd->formats++; |
| 585 | |
| 586 | if (flags & SOCAM_DATAWIDTH_8) |
| 587 | icd->num_formats++; |
| 588 | |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 589 | dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, |
| 590 | data == 0x8431 ? "C12STM" : "C12ST"); |
| 591 | |
| 592 | /* Now that we know the model, we can start video */ |
| 593 | ret = soc_camera_video_start(icd); |
| 594 | if (ret) |
| 595 | goto eisis; |
| 596 | |
| 597 | return 0; |
| 598 | |
| 599 | eisis: |
| 600 | ei2c: |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static void mt9m001_video_remove(struct soc_camera_device *icd) |
| 605 | { |
| 606 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 607 | |
| 608 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr, |
Guennadi Liakhovetski | a85bdac | 2008-12-18 12:54:33 -0300 | [diff] [blame] | 609 | icd->dev.parent, icd->vdev); |
| 610 | soc_camera_video_stop(icd); |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 611 | } |
| 612 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 613 | static int mt9m001_probe(struct i2c_client *client, |
| 614 | const struct i2c_device_id *did) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 615 | { |
| 616 | struct mt9m001 *mt9m001; |
| 617 | struct soc_camera_device *icd; |
| 618 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 619 | struct soc_camera_link *icl = client->dev.platform_data; |
| 620 | int ret; |
| 621 | |
| 622 | if (!icl) { |
| 623 | dev_err(&client->dev, "MT9M001 driver needs platform data\n"); |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | |
| 627 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 628 | dev_warn(&adapter->dev, |
| 629 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 630 | return -EIO; |
| 631 | } |
| 632 | |
| 633 | mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL); |
| 634 | if (!mt9m001) |
| 635 | return -ENOMEM; |
| 636 | |
| 637 | mt9m001->client = client; |
| 638 | i2c_set_clientdata(client, mt9m001); |
| 639 | |
| 640 | /* Second stage probe - when a capture adapter is there */ |
| 641 | icd = &mt9m001->icd; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 642 | icd->ops = &mt9m001_ops; |
| 643 | icd->control = &client->dev; |
| 644 | icd->x_min = 20; |
| 645 | icd->y_min = 12; |
| 646 | icd->x_current = 20; |
| 647 | icd->y_current = 12; |
| 648 | icd->width_min = 48; |
| 649 | icd->width_max = 1280; |
| 650 | icd->height_min = 32; |
| 651 | icd->height_max = 1024; |
| 652 | icd->y_skip_top = 1; |
| 653 | icd->iface = icl->bus_id; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 654 | /* Simulated autoexposure. If enabled, we calculate shutter width |
| 655 | * ourselves in the driver based on vertical blanking and frame width */ |
| 656 | mt9m001->autoexposure = 1; |
| 657 | |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 658 | ret = soc_camera_device_register(icd); |
| 659 | if (ret) |
| 660 | goto eisdr; |
| 661 | |
| 662 | return 0; |
| 663 | |
| 664 | eisdr: |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 665 | kfree(mt9m001); |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | static int mt9m001_remove(struct i2c_client *client) |
| 670 | { |
| 671 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); |
| 672 | |
| 673 | soc_camera_device_unregister(&mt9m001->icd); |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 674 | kfree(mt9m001); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 679 | static const struct i2c_device_id mt9m001_id[] = { |
| 680 | { "mt9m001", 0 }, |
| 681 | { } |
| 682 | }; |
| 683 | MODULE_DEVICE_TABLE(i2c, mt9m001_id); |
| 684 | |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 685 | static struct i2c_driver mt9m001_i2c_driver = { |
| 686 | .driver = { |
| 687 | .name = "mt9m001", |
| 688 | }, |
| 689 | .probe = mt9m001_probe, |
| 690 | .remove = mt9m001_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 691 | .id_table = mt9m001_id, |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 692 | }; |
| 693 | |
| 694 | static int __init mt9m001_mod_init(void) |
| 695 | { |
| 696 | return i2c_add_driver(&mt9m001_i2c_driver); |
| 697 | } |
| 698 | |
| 699 | static void __exit mt9m001_mod_exit(void) |
| 700 | { |
| 701 | i2c_del_driver(&mt9m001_i2c_driver); |
| 702 | } |
| 703 | |
| 704 | module_init(mt9m001_mod_init); |
| 705 | module_exit(mt9m001_mod_exit); |
| 706 | |
| 707 | MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); |
| 708 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 709 | MODULE_LICENSE("GPL"); |