Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9M032 CMOS Image Sensor from Micron |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Lund Engineering |
| 5 | * Contact: Gil Lund <gwlund@lundeng.com> |
| 6 | * Author: Martin Hostettler <martin@neutronstar.dyndns.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/math64.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/mutex.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/v4l2-mediabus.h> |
| 27 | |
| 28 | #include <media/media-entity.h> |
Mauro Carvalho Chehab | b5dcee2 | 2015-11-10 12:01:44 -0200 | [diff] [blame] | 29 | #include <media/i2c/mt9m032.h> |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 30 | #include <media/v4l2-ctrls.h> |
| 31 | #include <media/v4l2-device.h> |
| 32 | #include <media/v4l2-subdev.h> |
| 33 | |
| 34 | #include "aptina-pll.h" |
| 35 | |
| 36 | /* |
| 37 | * width and height include active boundary and black parts |
| 38 | * |
| 39 | * column 0- 15 active boundary |
| 40 | * column 16-1455 image |
| 41 | * column 1456-1471 active boundary |
| 42 | * column 1472-1599 black |
| 43 | * |
| 44 | * row 0- 51 black |
| 45 | * row 53- 59 active boundary |
| 46 | * row 60-1139 image |
| 47 | * row 1140-1147 active boundary |
| 48 | * row 1148-1151 black |
| 49 | */ |
| 50 | |
| 51 | #define MT9M032_PIXEL_ARRAY_WIDTH 1600 |
| 52 | #define MT9M032_PIXEL_ARRAY_HEIGHT 1152 |
| 53 | |
| 54 | #define MT9M032_CHIP_VERSION 0x00 |
| 55 | #define MT9M032_CHIP_VERSION_VALUE 0x1402 |
| 56 | #define MT9M032_ROW_START 0x01 |
| 57 | #define MT9M032_ROW_START_MIN 0 |
| 58 | #define MT9M032_ROW_START_MAX 1152 |
| 59 | #define MT9M032_ROW_START_DEF 60 |
| 60 | #define MT9M032_COLUMN_START 0x02 |
| 61 | #define MT9M032_COLUMN_START_MIN 0 |
| 62 | #define MT9M032_COLUMN_START_MAX 1600 |
| 63 | #define MT9M032_COLUMN_START_DEF 16 |
| 64 | #define MT9M032_ROW_SIZE 0x03 |
| 65 | #define MT9M032_ROW_SIZE_MIN 32 |
| 66 | #define MT9M032_ROW_SIZE_MAX 1152 |
| 67 | #define MT9M032_ROW_SIZE_DEF 1080 |
| 68 | #define MT9M032_COLUMN_SIZE 0x04 |
| 69 | #define MT9M032_COLUMN_SIZE_MIN 32 |
| 70 | #define MT9M032_COLUMN_SIZE_MAX 1600 |
| 71 | #define MT9M032_COLUMN_SIZE_DEF 1440 |
| 72 | #define MT9M032_HBLANK 0x05 |
| 73 | #define MT9M032_VBLANK 0x06 |
| 74 | #define MT9M032_VBLANK_MAX 0x7ff |
| 75 | #define MT9M032_SHUTTER_WIDTH_HIGH 0x08 |
| 76 | #define MT9M032_SHUTTER_WIDTH_LOW 0x09 |
| 77 | #define MT9M032_SHUTTER_WIDTH_MIN 1 |
| 78 | #define MT9M032_SHUTTER_WIDTH_MAX 1048575 |
| 79 | #define MT9M032_SHUTTER_WIDTH_DEF 1943 |
| 80 | #define MT9M032_PIX_CLK_CTRL 0x0a |
| 81 | #define MT9M032_PIX_CLK_CTRL_INV_PIXCLK 0x8000 |
| 82 | #define MT9M032_RESTART 0x0b |
| 83 | #define MT9M032_RESET 0x0d |
| 84 | #define MT9M032_PLL_CONFIG1 0x11 |
Laurent Pinchart | a54b80b | 2012-09-25 10:46:34 -0300 | [diff] [blame] | 85 | #define MT9M032_PLL_CONFIG1_PREDIV_MASK 0x3f |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 86 | #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8 |
| 87 | #define MT9M032_READ_MODE1 0x1e |
Laurent Pinchart | 73254c3 | 2012-09-26 05:54:17 -0300 | [diff] [blame] | 88 | #define MT9M032_READ_MODE1_OUTPUT_BAD_FRAMES (1 << 13) |
| 89 | #define MT9M032_READ_MODE1_MAINTAIN_FRAME_RATE (1 << 12) |
| 90 | #define MT9M032_READ_MODE1_XOR_LINE_VALID (1 << 11) |
| 91 | #define MT9M032_READ_MODE1_CONT_LINE_VALID (1 << 10) |
| 92 | #define MT9M032_READ_MODE1_INVERT_TRIGGER (1 << 9) |
| 93 | #define MT9M032_READ_MODE1_SNAPSHOT (1 << 8) |
| 94 | #define MT9M032_READ_MODE1_GLOBAL_RESET (1 << 7) |
| 95 | #define MT9M032_READ_MODE1_BULB_EXPOSURE (1 << 6) |
| 96 | #define MT9M032_READ_MODE1_INVERT_STROBE (1 << 5) |
| 97 | #define MT9M032_READ_MODE1_STROBE_ENABLE (1 << 4) |
| 98 | #define MT9M032_READ_MODE1_STROBE_START_TRIG1 (0 << 2) |
| 99 | #define MT9M032_READ_MODE1_STROBE_START_EXP (1 << 2) |
| 100 | #define MT9M032_READ_MODE1_STROBE_START_SHUTTER (2 << 2) |
| 101 | #define MT9M032_READ_MODE1_STROBE_START_TRIG2 (3 << 2) |
| 102 | #define MT9M032_READ_MODE1_STROBE_END_TRIG1 (0 << 0) |
| 103 | #define MT9M032_READ_MODE1_STROBE_END_EXP (1 << 0) |
| 104 | #define MT9M032_READ_MODE1_STROBE_END_SHUTTER (2 << 0) |
| 105 | #define MT9M032_READ_MODE1_STROBE_END_TRIG2 (3 << 0) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 106 | #define MT9M032_READ_MODE2 0x20 |
| 107 | #define MT9M032_READ_MODE2_VFLIP_SHIFT 15 |
| 108 | #define MT9M032_READ_MODE2_HFLIP_SHIFT 14 |
| 109 | #define MT9M032_READ_MODE2_ROW_BLC 0x40 |
| 110 | #define MT9M032_GAIN_GREEN1 0x2b |
| 111 | #define MT9M032_GAIN_BLUE 0x2c |
| 112 | #define MT9M032_GAIN_RED 0x2d |
| 113 | #define MT9M032_GAIN_GREEN2 0x2e |
| 114 | |
| 115 | /* write only */ |
| 116 | #define MT9M032_GAIN_ALL 0x35 |
| 117 | #define MT9M032_GAIN_DIGITAL_MASK 0x7f |
| 118 | #define MT9M032_GAIN_DIGITAL_SHIFT 8 |
| 119 | #define MT9M032_GAIN_AMUL_SHIFT 6 |
| 120 | #define MT9M032_GAIN_ANALOG_MASK 0x3f |
| 121 | #define MT9M032_FORMATTER1 0x9e |
Laurent Pinchart | a54b80b | 2012-09-25 10:46:34 -0300 | [diff] [blame] | 122 | #define MT9M032_FORMATTER1_PLL_P1_6 (1 << 8) |
| 123 | #define MT9M032_FORMATTER1_PARALLEL (1 << 12) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 124 | #define MT9M032_FORMATTER2 0x9f |
| 125 | #define MT9M032_FORMATTER2_DOUT_EN 0x1000 |
| 126 | #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000 |
| 127 | |
| 128 | /* |
| 129 | * The available MT9M032 datasheet is missing documentation for register 0x10 |
| 130 | * MT9P031 seems to be close enough, so use constants from that datasheet for |
| 131 | * now. |
| 132 | * But keep the name MT9P031 to remind us, that this isn't really confirmed |
| 133 | * for this sensor. |
| 134 | */ |
| 135 | #define MT9P031_PLL_CONTROL 0x10 |
| 136 | #define MT9P031_PLL_CONTROL_PWROFF 0x0050 |
| 137 | #define MT9P031_PLL_CONTROL_PWRON 0x0051 |
| 138 | #define MT9P031_PLL_CONTROL_USEPLL 0x0052 |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 139 | |
| 140 | struct mt9m032 { |
| 141 | struct v4l2_subdev subdev; |
| 142 | struct media_pad pad; |
| 143 | struct mt9m032_platform_data *pdata; |
| 144 | |
| 145 | unsigned int pix_clock; |
| 146 | |
| 147 | struct v4l2_ctrl_handler ctrls; |
| 148 | struct { |
| 149 | struct v4l2_ctrl *hflip; |
| 150 | struct v4l2_ctrl *vflip; |
| 151 | }; |
| 152 | |
| 153 | struct mutex lock; /* Protects streaming, format, interval and crop */ |
| 154 | |
| 155 | bool streaming; |
| 156 | |
| 157 | struct v4l2_mbus_framefmt format; |
| 158 | struct v4l2_rect crop; |
| 159 | struct v4l2_fract frame_interval; |
| 160 | }; |
| 161 | |
| 162 | #define to_mt9m032(sd) container_of(sd, struct mt9m032, subdev) |
| 163 | #define to_dev(sensor) \ |
| 164 | (&((struct i2c_client *)v4l2_get_subdevdata(&(sensor)->subdev))->dev) |
| 165 | |
| 166 | static int mt9m032_read(struct i2c_client *client, u8 reg) |
| 167 | { |
| 168 | return i2c_smbus_read_word_swapped(client, reg); |
| 169 | } |
| 170 | |
| 171 | static int mt9m032_write(struct i2c_client *client, u8 reg, const u16 data) |
| 172 | { |
| 173 | return i2c_smbus_write_word_swapped(client, reg, data); |
| 174 | } |
| 175 | |
| 176 | static u32 mt9m032_row_time(struct mt9m032 *sensor, unsigned int width) |
| 177 | { |
| 178 | unsigned int effective_width; |
| 179 | u32 ns; |
| 180 | |
| 181 | effective_width = width + 716; /* empirical value */ |
| 182 | ns = div_u64(1000000000ULL * effective_width, sensor->pix_clock); |
| 183 | dev_dbg(to_dev(sensor), "MT9M032 line time: %u ns\n", ns); |
| 184 | return ns; |
| 185 | } |
| 186 | |
| 187 | static int mt9m032_update_timing(struct mt9m032 *sensor, |
| 188 | struct v4l2_fract *interval) |
| 189 | { |
| 190 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 191 | struct v4l2_rect *crop = &sensor->crop; |
| 192 | unsigned int min_vblank; |
| 193 | unsigned int vblank; |
| 194 | u32 row_time; |
| 195 | |
| 196 | if (!interval) |
| 197 | interval = &sensor->frame_interval; |
| 198 | |
| 199 | row_time = mt9m032_row_time(sensor, crop->width); |
| 200 | |
| 201 | vblank = div_u64(1000000000ULL * interval->numerator, |
| 202 | (u64)row_time * interval->denominator) |
| 203 | - crop->height; |
| 204 | |
| 205 | if (vblank > MT9M032_VBLANK_MAX) { |
| 206 | /* hardware limits to 11 bit values */ |
| 207 | interval->denominator = 1000; |
| 208 | interval->numerator = |
| 209 | div_u64((crop->height + MT9M032_VBLANK_MAX) * |
| 210 | (u64)row_time * interval->denominator, |
| 211 | 1000000000ULL); |
| 212 | vblank = div_u64(1000000000ULL * interval->numerator, |
| 213 | (u64)row_time * interval->denominator) |
| 214 | - crop->height; |
| 215 | } |
| 216 | /* enforce minimal 1.6ms blanking time. */ |
| 217 | min_vblank = 1600000 / row_time; |
| 218 | vblank = clamp_t(unsigned int, vblank, min_vblank, MT9M032_VBLANK_MAX); |
| 219 | |
| 220 | return mt9m032_write(client, MT9M032_VBLANK, vblank); |
| 221 | } |
| 222 | |
| 223 | static int mt9m032_update_geom_timing(struct mt9m032 *sensor) |
| 224 | { |
| 225 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 226 | int ret; |
| 227 | |
| 228 | ret = mt9m032_write(client, MT9M032_COLUMN_SIZE, |
| 229 | sensor->crop.width - 1); |
| 230 | if (!ret) |
| 231 | ret = mt9m032_write(client, MT9M032_ROW_SIZE, |
| 232 | sensor->crop.height - 1); |
| 233 | if (!ret) |
| 234 | ret = mt9m032_write(client, MT9M032_COLUMN_START, |
| 235 | sensor->crop.left); |
| 236 | if (!ret) |
| 237 | ret = mt9m032_write(client, MT9M032_ROW_START, |
| 238 | sensor->crop.top); |
| 239 | if (!ret) |
| 240 | ret = mt9m032_update_timing(sensor, NULL); |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | static int update_formatter2(struct mt9m032 *sensor, bool streaming) |
| 245 | { |
| 246 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 247 | u16 reg_val = MT9M032_FORMATTER2_DOUT_EN |
| 248 | | 0x0070; /* parts reserved! */ |
| 249 | /* possibly for changing to 14-bit mode */ |
| 250 | |
| 251 | if (streaming) |
| 252 | reg_val |= MT9M032_FORMATTER2_PIXCLK_EN; /* pixclock enable */ |
| 253 | |
| 254 | return mt9m032_write(client, MT9M032_FORMATTER2, reg_val); |
| 255 | } |
| 256 | |
| 257 | static int mt9m032_setup_pll(struct mt9m032 *sensor) |
| 258 | { |
| 259 | static const struct aptina_pll_limits limits = { |
| 260 | .ext_clock_min = 8000000, |
| 261 | .ext_clock_max = 16500000, |
| 262 | .int_clock_min = 2000000, |
| 263 | .int_clock_max = 24000000, |
| 264 | .out_clock_min = 322000000, |
| 265 | .out_clock_max = 693000000, |
| 266 | .pix_clock_max = 99000000, |
| 267 | .n_min = 1, |
| 268 | .n_max = 64, |
| 269 | .m_min = 16, |
| 270 | .m_max = 255, |
Laurent Pinchart | a54b80b | 2012-09-25 10:46:34 -0300 | [diff] [blame] | 271 | .p1_min = 6, |
| 272 | .p1_max = 7, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 276 | struct mt9m032_platform_data *pdata = sensor->pdata; |
| 277 | struct aptina_pll pll; |
Laurent Pinchart | a54b80b | 2012-09-25 10:46:34 -0300 | [diff] [blame] | 278 | u16 reg_val; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 279 | int ret; |
| 280 | |
| 281 | pll.ext_clock = pdata->ext_clock; |
| 282 | pll.pix_clock = pdata->pix_clock; |
| 283 | |
| 284 | ret = aptina_pll_calculate(&client->dev, &limits, &pll); |
| 285 | if (ret < 0) |
| 286 | return ret; |
| 287 | |
| 288 | sensor->pix_clock = pdata->pix_clock; |
| 289 | |
| 290 | ret = mt9m032_write(client, MT9M032_PLL_CONFIG1, |
Laurent Pinchart | a54b80b | 2012-09-25 10:46:34 -0300 | [diff] [blame] | 291 | (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT) | |
| 292 | ((pll.n - 1) & MT9M032_PLL_CONFIG1_PREDIV_MASK)); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 293 | if (!ret) |
| 294 | ret = mt9m032_write(client, MT9P031_PLL_CONTROL, |
| 295 | MT9P031_PLL_CONTROL_PWRON | |
| 296 | MT9P031_PLL_CONTROL_USEPLL); |
| 297 | if (!ret) /* more reserved, Continuous, Master Mode */ |
Laurent Pinchart | 73254c3 | 2012-09-26 05:54:17 -0300 | [diff] [blame] | 298 | ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8000 | |
| 299 | MT9M032_READ_MODE1_STROBE_START_EXP | |
| 300 | MT9M032_READ_MODE1_STROBE_END_SHUTTER); |
Laurent Pinchart | a54b80b | 2012-09-25 10:46:34 -0300 | [diff] [blame] | 301 | if (!ret) { |
| 302 | reg_val = (pll.p1 == 6 ? MT9M032_FORMATTER1_PLL_P1_6 : 0) |
| 303 | | MT9M032_FORMATTER1_PARALLEL | 0x001e; /* 14-bit */ |
| 304 | ret = mt9m032_write(client, MT9M032_FORMATTER1, reg_val); |
| 305 | } |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /* ----------------------------------------------------------------------------- |
| 311 | * Subdev pad operations |
| 312 | */ |
| 313 | |
| 314 | static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 315 | struct v4l2_subdev_pad_config *cfg, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 316 | struct v4l2_subdev_mbus_code_enum *code) |
| 317 | { |
| 318 | if (code->index != 0) |
| 319 | return -EINVAL; |
| 320 | |
Boris BREZILLON | f5fe58f | 2014-11-10 14:28:29 -0300 | [diff] [blame] | 321 | code->code = MEDIA_BUS_FMT_Y8_1X8; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 326 | struct v4l2_subdev_pad_config *cfg, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 327 | struct v4l2_subdev_frame_size_enum *fse) |
| 328 | { |
Boris BREZILLON | f5fe58f | 2014-11-10 14:28:29 -0300 | [diff] [blame] | 329 | if (fse->index != 0 || fse->code != MEDIA_BUS_FMT_Y8_1X8) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 330 | return -EINVAL; |
| 331 | |
| 332 | fse->min_width = MT9M032_COLUMN_SIZE_DEF; |
| 333 | fse->max_width = MT9M032_COLUMN_SIZE_DEF; |
| 334 | fse->min_height = MT9M032_ROW_SIZE_DEF; |
| 335 | fse->max_height = MT9M032_ROW_SIZE_DEF; |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * __mt9m032_get_pad_crop() - get crop rect |
| 342 | * @sensor: pointer to the sensor struct |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 343 | * @cfg: v4l2_subdev_pad_config for getting the try crop rect from |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 344 | * @which: select try or active crop rect |
| 345 | * |
| 346 | * Returns a pointer the current active or fh relative try crop rect |
| 347 | */ |
| 348 | static struct v4l2_rect * |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 349 | __mt9m032_get_pad_crop(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 350 | enum v4l2_subdev_format_whence which) |
| 351 | { |
| 352 | switch (which) { |
| 353 | case V4L2_SUBDEV_FORMAT_TRY: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 354 | return v4l2_subdev_get_try_crop(&sensor->subdev, cfg, 0); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 355 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
| 356 | return &sensor->crop; |
| 357 | default: |
| 358 | return NULL; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * __mt9m032_get_pad_format() - get format |
| 364 | * @sensor: pointer to the sensor struct |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 365 | * @cfg: v4l2_subdev_pad_config for getting the try format from |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 366 | * @which: select try or active format |
| 367 | * |
| 368 | * Returns a pointer the current active or fh relative try format |
| 369 | */ |
| 370 | static struct v4l2_mbus_framefmt * |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 371 | __mt9m032_get_pad_format(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 372 | enum v4l2_subdev_format_whence which) |
| 373 | { |
| 374 | switch (which) { |
| 375 | case V4L2_SUBDEV_FORMAT_TRY: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 376 | return v4l2_subdev_get_try_format(&sensor->subdev, cfg, 0); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 377 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
| 378 | return &sensor->format; |
| 379 | default: |
| 380 | return NULL; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static int mt9m032_get_pad_format(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 385 | struct v4l2_subdev_pad_config *cfg, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 386 | struct v4l2_subdev_format *fmt) |
| 387 | { |
| 388 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 389 | |
| 390 | mutex_lock(&sensor->lock); |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 391 | fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 392 | mutex_unlock(&sensor->lock); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int mt9m032_set_pad_format(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 398 | struct v4l2_subdev_pad_config *cfg, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 399 | struct v4l2_subdev_format *fmt) |
| 400 | { |
| 401 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 402 | int ret; |
| 403 | |
| 404 | mutex_lock(&sensor->lock); |
| 405 | |
| 406 | if (sensor->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 407 | ret = -EBUSY; |
| 408 | goto done; |
| 409 | } |
| 410 | |
| 411 | /* Scaling is not supported, the format is thus fixed. */ |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 412 | fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which); |
Guennadi Liakhovetski | b1e1179 | 2012-04-08 17:31:24 -0300 | [diff] [blame] | 413 | ret = 0; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 414 | |
| 415 | done: |
Guennadi Liakhovetski | b1e1179 | 2012-04-08 17:31:24 -0300 | [diff] [blame] | 416 | mutex_unlock(&sensor->lock); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 417 | return ret; |
| 418 | } |
| 419 | |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 420 | static int mt9m032_get_pad_selection(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 421 | struct v4l2_subdev_pad_config *cfg, |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 422 | struct v4l2_subdev_selection *sel) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 423 | { |
| 424 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 425 | |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 426 | if (sel->target != V4L2_SEL_TGT_CROP) |
| 427 | return -EINVAL; |
| 428 | |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 429 | mutex_lock(&sensor->lock); |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 430 | sel->r = *__mt9m032_get_pad_crop(sensor, cfg, sel->which); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 431 | mutex_unlock(&sensor->lock); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 436 | static int mt9m032_set_pad_selection(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 437 | struct v4l2_subdev_pad_config *cfg, |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 438 | struct v4l2_subdev_selection *sel) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 439 | { |
| 440 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 441 | struct v4l2_mbus_framefmt *format; |
| 442 | struct v4l2_rect *__crop; |
| 443 | struct v4l2_rect rect; |
| 444 | int ret = 0; |
| 445 | |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 446 | if (sel->target != V4L2_SEL_TGT_CROP) |
| 447 | return -EINVAL; |
| 448 | |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 449 | mutex_lock(&sensor->lock); |
| 450 | |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 451 | if (sensor->streaming && sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 452 | ret = -EBUSY; |
| 453 | goto done; |
| 454 | } |
| 455 | |
| 456 | /* Clamp the crop rectangle boundaries and align them to a multiple of 2 |
| 457 | * pixels to ensure a GRBG Bayer pattern. |
| 458 | */ |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 459 | rect.left = clamp(ALIGN(sel->r.left, 2), MT9M032_COLUMN_START_MIN, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 460 | MT9M032_COLUMN_START_MAX); |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 461 | rect.top = clamp(ALIGN(sel->r.top, 2), MT9M032_ROW_START_MIN, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 462 | MT9M032_ROW_START_MAX); |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 463 | rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2), |
Ricardo Ribalda | f90580c | 2013-11-26 05:31:42 -0300 | [diff] [blame] | 464 | MT9M032_COLUMN_SIZE_MIN, MT9M032_COLUMN_SIZE_MAX); |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 465 | rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2), |
Ricardo Ribalda | f90580c | 2013-11-26 05:31:42 -0300 | [diff] [blame] | 466 | MT9M032_ROW_SIZE_MIN, MT9M032_ROW_SIZE_MAX); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 467 | |
Ricardo Ribalda | f90580c | 2013-11-26 05:31:42 -0300 | [diff] [blame] | 468 | rect.width = min_t(unsigned int, rect.width, |
| 469 | MT9M032_PIXEL_ARRAY_WIDTH - rect.left); |
| 470 | rect.height = min_t(unsigned int, rect.height, |
| 471 | MT9M032_PIXEL_ARRAY_HEIGHT - rect.top); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 472 | |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 473 | __crop = __mt9m032_get_pad_crop(sensor, cfg, sel->which); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 474 | |
| 475 | if (rect.width != __crop->width || rect.height != __crop->height) { |
| 476 | /* Reset the output image size if the crop rectangle size has |
| 477 | * been modified. |
| 478 | */ |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 479 | format = __mt9m032_get_pad_format(sensor, cfg, sel->which); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 480 | format->width = rect.width; |
| 481 | format->height = rect.height; |
| 482 | } |
| 483 | |
| 484 | *__crop = rect; |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 485 | sel->r = rect; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 486 | |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 487 | if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 488 | ret = mt9m032_update_geom_timing(sensor); |
| 489 | |
| 490 | done: |
| 491 | mutex_unlock(&sensor->lock); |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev, |
| 496 | struct v4l2_subdev_frame_interval *fi) |
| 497 | { |
| 498 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 499 | |
| 500 | mutex_lock(&sensor->lock); |
| 501 | memset(fi, 0, sizeof(*fi)); |
| 502 | fi->interval = sensor->frame_interval; |
| 503 | mutex_unlock(&sensor->lock); |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev, |
| 509 | struct v4l2_subdev_frame_interval *fi) |
| 510 | { |
| 511 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 512 | int ret; |
| 513 | |
| 514 | mutex_lock(&sensor->lock); |
| 515 | |
| 516 | if (sensor->streaming) { |
| 517 | ret = -EBUSY; |
| 518 | goto done; |
| 519 | } |
| 520 | |
| 521 | /* Avoid divisions by 0. */ |
| 522 | if (fi->interval.denominator == 0) |
| 523 | fi->interval.denominator = 1; |
| 524 | |
| 525 | ret = mt9m032_update_timing(sensor, &fi->interval); |
| 526 | if (!ret) |
| 527 | sensor->frame_interval = fi->interval; |
| 528 | |
| 529 | done: |
| 530 | mutex_unlock(&sensor->lock); |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming) |
| 535 | { |
| 536 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 537 | int ret; |
| 538 | |
| 539 | mutex_lock(&sensor->lock); |
| 540 | ret = update_formatter2(sensor, streaming); |
| 541 | if (!ret) |
| 542 | sensor->streaming = streaming; |
| 543 | mutex_unlock(&sensor->lock); |
| 544 | |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | /* ----------------------------------------------------------------------------- |
| 549 | * V4L2 subdev core operations |
| 550 | */ |
| 551 | |
| 552 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 553 | static int mt9m032_g_register(struct v4l2_subdev *sd, |
| 554 | struct v4l2_dbg_register *reg) |
| 555 | { |
| 556 | struct mt9m032 *sensor = to_mt9m032(sd); |
| 557 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 558 | int val; |
| 559 | |
Hans Verkuil | e127711 | 2013-05-29 06:59:51 -0300 | [diff] [blame] | 560 | if (reg->reg > 0xff) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 561 | return -EINVAL; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 562 | |
| 563 | val = mt9m032_read(client, reg->reg); |
| 564 | if (val < 0) |
| 565 | return -EIO; |
| 566 | |
| 567 | reg->size = 2; |
| 568 | reg->val = val; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int mt9m032_s_register(struct v4l2_subdev *sd, |
Hans Verkuil | 977ba3b1 | 2013-03-24 08:28:46 -0300 | [diff] [blame] | 574 | const struct v4l2_dbg_register *reg) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 575 | { |
| 576 | struct mt9m032 *sensor = to_mt9m032(sd); |
| 577 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 578 | |
Hans Verkuil | e127711 | 2013-05-29 06:59:51 -0300 | [diff] [blame] | 579 | if (reg->reg > 0xff) |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 580 | return -EINVAL; |
| 581 | |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 582 | return mt9m032_write(client, reg->reg, reg->val); |
| 583 | } |
| 584 | #endif |
| 585 | |
| 586 | /* ----------------------------------------------------------------------------- |
| 587 | * V4L2 subdev control operations |
| 588 | */ |
| 589 | |
| 590 | static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip) |
| 591 | { |
| 592 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 593 | int reg_val = (vflip << MT9M032_READ_MODE2_VFLIP_SHIFT) |
| 594 | | (hflip << MT9M032_READ_MODE2_HFLIP_SHIFT) |
| 595 | | MT9M032_READ_MODE2_ROW_BLC |
| 596 | | 0x0007; |
| 597 | |
| 598 | return mt9m032_write(client, MT9M032_READ_MODE2, reg_val); |
| 599 | } |
| 600 | |
| 601 | static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val) |
| 602 | { |
| 603 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 604 | int digital_gain_val; /* in 1/8th (0..127) */ |
| 605 | int analog_mul; /* 0 or 1 */ |
| 606 | int analog_gain_val; /* in 1/16th. (0..63) */ |
| 607 | u16 reg_val; |
| 608 | |
| 609 | digital_gain_val = 51; /* from setup example */ |
| 610 | |
| 611 | if (val < 63) { |
| 612 | analog_mul = 0; |
| 613 | analog_gain_val = val; |
| 614 | } else { |
| 615 | analog_mul = 1; |
| 616 | analog_gain_val = val / 2; |
| 617 | } |
| 618 | |
| 619 | /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */ |
| 620 | /* overall_gain = a_gain * (1 + digital_gain_val / 8) */ |
| 621 | |
| 622 | reg_val = ((digital_gain_val & MT9M032_GAIN_DIGITAL_MASK) |
| 623 | << MT9M032_GAIN_DIGITAL_SHIFT) |
| 624 | | ((analog_mul & 1) << MT9M032_GAIN_AMUL_SHIFT) |
| 625 | | (analog_gain_val & MT9M032_GAIN_ANALOG_MASK); |
| 626 | |
| 627 | return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val); |
| 628 | } |
| 629 | |
| 630 | static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl) |
| 631 | { |
| 632 | if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) { |
| 633 | /* round because of multiplier used for values >= 63 */ |
| 634 | ctrl->val &= ~1; |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl) |
| 641 | { |
| 642 | struct mt9m032 *sensor = |
| 643 | container_of(ctrl->handler, struct mt9m032, ctrls); |
| 644 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); |
| 645 | int ret; |
| 646 | |
| 647 | switch (ctrl->id) { |
| 648 | case V4L2_CID_GAIN: |
| 649 | return mt9m032_set_gain(sensor, ctrl->val); |
| 650 | |
| 651 | case V4L2_CID_HFLIP: |
| 652 | /* case V4L2_CID_VFLIP: -- In the same cluster */ |
| 653 | return update_read_mode2(sensor, sensor->vflip->val, |
| 654 | sensor->hflip->val); |
| 655 | |
| 656 | case V4L2_CID_EXPOSURE: |
| 657 | ret = mt9m032_write(client, MT9M032_SHUTTER_WIDTH_HIGH, |
| 658 | (ctrl->val >> 16) & 0xffff); |
| 659 | if (ret < 0) |
| 660 | return ret; |
| 661 | |
| 662 | return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW, |
| 663 | ctrl->val & 0xffff); |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
Julia Lawall | 217bdb0 | 2015-11-13 20:05:17 -0200 | [diff] [blame] | 669 | static const struct v4l2_ctrl_ops mt9m032_ctrl_ops = { |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 670 | .s_ctrl = mt9m032_set_ctrl, |
| 671 | .try_ctrl = mt9m032_try_ctrl, |
| 672 | }; |
| 673 | |
| 674 | /* -------------------------------------------------------------------------- */ |
| 675 | |
| 676 | static const struct v4l2_subdev_core_ops mt9m032_core_ops = { |
| 677 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 678 | .g_register = mt9m032_g_register, |
| 679 | .s_register = mt9m032_s_register, |
| 680 | #endif |
| 681 | }; |
| 682 | |
| 683 | static const struct v4l2_subdev_video_ops mt9m032_video_ops = { |
| 684 | .s_stream = mt9m032_s_stream, |
| 685 | .g_frame_interval = mt9m032_get_frame_interval, |
| 686 | .s_frame_interval = mt9m032_set_frame_interval, |
| 687 | }; |
| 688 | |
| 689 | static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = { |
| 690 | .enum_mbus_code = mt9m032_enum_mbus_code, |
| 691 | .enum_frame_size = mt9m032_enum_frame_size, |
| 692 | .get_fmt = mt9m032_get_pad_format, |
| 693 | .set_fmt = mt9m032_set_pad_format, |
Hans Verkuil | 1a023fe | 2014-12-04 06:54:52 -0300 | [diff] [blame] | 694 | .set_selection = mt9m032_set_pad_selection, |
| 695 | .get_selection = mt9m032_get_pad_selection, |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | static const struct v4l2_subdev_ops mt9m032_ops = { |
| 699 | .core = &mt9m032_core_ops, |
| 700 | .video = &mt9m032_video_ops, |
| 701 | .pad = &mt9m032_pad_ops, |
| 702 | }; |
| 703 | |
| 704 | /* ----------------------------------------------------------------------------- |
| 705 | * Driver initialization and probing |
| 706 | */ |
| 707 | |
| 708 | static int mt9m032_probe(struct i2c_client *client, |
| 709 | const struct i2c_device_id *devid) |
| 710 | { |
Laurent Pinchart | 5472d3f | 2012-05-09 09:55:59 -0300 | [diff] [blame] | 711 | struct mt9m032_platform_data *pdata = client->dev.platform_data; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 712 | struct i2c_adapter *adapter = client->adapter; |
| 713 | struct mt9m032 *sensor; |
| 714 | int chip_version; |
| 715 | int ret; |
| 716 | |
Laurent Pinchart | 5472d3f | 2012-05-09 09:55:59 -0300 | [diff] [blame] | 717 | if (pdata == NULL) { |
| 718 | dev_err(&client->dev, "No platform data\n"); |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 722 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 723 | dev_warn(&client->dev, |
| 724 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 725 | return -EIO; |
| 726 | } |
| 727 | |
| 728 | if (!client->dev.platform_data) |
| 729 | return -ENODEV; |
| 730 | |
Laurent Pinchart | c02b211 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 731 | sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 732 | if (sensor == NULL) |
| 733 | return -ENOMEM; |
| 734 | |
| 735 | mutex_init(&sensor->lock); |
| 736 | |
Laurent Pinchart | 5472d3f | 2012-05-09 09:55:59 -0300 | [diff] [blame] | 737 | sensor->pdata = pdata; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 738 | |
| 739 | v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops); |
| 740 | sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 741 | |
| 742 | chip_version = mt9m032_read(client, MT9M032_CHIP_VERSION); |
| 743 | if (chip_version != MT9M032_CHIP_VERSION_VALUE) { |
| 744 | dev_err(&client->dev, "MT9M032 not detected, wrong version " |
| 745 | "0x%04x\n", chip_version); |
| 746 | ret = -ENODEV; |
| 747 | goto error_sensor; |
| 748 | } |
| 749 | |
| 750 | dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n", |
| 751 | client->addr); |
| 752 | |
| 753 | sensor->frame_interval.numerator = 1; |
| 754 | sensor->frame_interval.denominator = 30; |
| 755 | |
| 756 | sensor->crop.left = MT9M032_COLUMN_START_DEF; |
| 757 | sensor->crop.top = MT9M032_ROW_START_DEF; |
| 758 | sensor->crop.width = MT9M032_COLUMN_SIZE_DEF; |
| 759 | sensor->crop.height = MT9M032_ROW_SIZE_DEF; |
| 760 | |
| 761 | sensor->format.width = sensor->crop.width; |
| 762 | sensor->format.height = sensor->crop.height; |
Boris BREZILLON | f5fe58f | 2014-11-10 14:28:29 -0300 | [diff] [blame] | 763 | sensor->format.code = MEDIA_BUS_FMT_Y8_1X8; |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 764 | sensor->format.field = V4L2_FIELD_NONE; |
| 765 | sensor->format.colorspace = V4L2_COLORSPACE_SRGB; |
| 766 | |
Laurent Pinchart | 5472d3f | 2012-05-09 09:55:59 -0300 | [diff] [blame] | 767 | v4l2_ctrl_handler_init(&sensor->ctrls, 5); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 768 | |
| 769 | v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops, |
| 770 | V4L2_CID_GAIN, 0, 127, 1, 64); |
| 771 | |
| 772 | sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls, |
| 773 | &mt9m032_ctrl_ops, |
| 774 | V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 775 | sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls, |
| 776 | &mt9m032_ctrl_ops, |
| 777 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 778 | |
| 779 | v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops, |
| 780 | V4L2_CID_EXPOSURE, MT9M032_SHUTTER_WIDTH_MIN, |
| 781 | MT9M032_SHUTTER_WIDTH_MAX, 1, |
| 782 | MT9M032_SHUTTER_WIDTH_DEF); |
Laurent Pinchart | 5472d3f | 2012-05-09 09:55:59 -0300 | [diff] [blame] | 783 | v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops, |
| 784 | V4L2_CID_PIXEL_RATE, pdata->pix_clock, |
| 785 | pdata->pix_clock, 1, pdata->pix_clock); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 786 | |
| 787 | if (sensor->ctrls.error) { |
| 788 | ret = sensor->ctrls.error; |
| 789 | dev_err(&client->dev, "control initialization error %d\n", ret); |
| 790 | goto error_ctrl; |
| 791 | } |
| 792 | |
| 793 | v4l2_ctrl_cluster(2, &sensor->hflip); |
| 794 | |
| 795 | sensor->subdev.ctrl_handler = &sensor->ctrls; |
| 796 | sensor->pad.flags = MEDIA_PAD_FL_SOURCE; |
Mauro Carvalho Chehab | ab22e77 | 2015-12-11 07:44:40 -0200 | [diff] [blame] | 797 | ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 798 | if (ret < 0) |
| 799 | goto error_ctrl; |
| 800 | |
| 801 | ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */ |
| 802 | if (ret < 0) |
| 803 | goto error_entity; |
Julia Lawall | 612cd9e | 2012-08-18 17:25:56 -0300 | [diff] [blame] | 804 | ret = mt9m032_write(client, MT9M032_RESET, 0); /* reset off */ |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 805 | if (ret < 0) |
| 806 | goto error_entity; |
| 807 | |
| 808 | ret = mt9m032_setup_pll(sensor); |
| 809 | if (ret < 0) |
| 810 | goto error_entity; |
| 811 | usleep_range(10000, 11000); |
| 812 | |
| 813 | ret = v4l2_ctrl_handler_setup(&sensor->ctrls); |
| 814 | if (ret < 0) |
| 815 | goto error_entity; |
| 816 | |
| 817 | /* SIZE */ |
| 818 | ret = mt9m032_update_geom_timing(sensor); |
| 819 | if (ret < 0) |
| 820 | goto error_entity; |
| 821 | |
| 822 | ret = mt9m032_write(client, 0x41, 0x0000); /* reserved !!! */ |
| 823 | if (ret < 0) |
| 824 | goto error_entity; |
| 825 | ret = mt9m032_write(client, 0x42, 0x0003); /* reserved !!! */ |
| 826 | if (ret < 0) |
| 827 | goto error_entity; |
| 828 | ret = mt9m032_write(client, 0x43, 0x0003); /* reserved !!! */ |
| 829 | if (ret < 0) |
| 830 | goto error_entity; |
| 831 | ret = mt9m032_write(client, 0x7f, 0x0000); /* reserved !!! */ |
| 832 | if (ret < 0) |
| 833 | goto error_entity; |
| 834 | if (sensor->pdata->invert_pixclock) { |
| 835 | ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL, |
| 836 | MT9M032_PIX_CLK_CTRL_INV_PIXCLK); |
| 837 | if (ret < 0) |
| 838 | goto error_entity; |
| 839 | } |
| 840 | |
| 841 | ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */ |
| 842 | if (ret < 0) |
| 843 | goto error_entity; |
| 844 | msleep(100); |
| 845 | ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */ |
| 846 | if (ret < 0) |
| 847 | goto error_entity; |
| 848 | msleep(100); |
| 849 | ret = update_formatter2(sensor, false); |
| 850 | if (ret < 0) |
| 851 | goto error_entity; |
| 852 | |
| 853 | return ret; |
| 854 | |
| 855 | error_entity: |
| 856 | media_entity_cleanup(&sensor->subdev.entity); |
| 857 | error_ctrl: |
| 858 | v4l2_ctrl_handler_free(&sensor->ctrls); |
| 859 | error_sensor: |
| 860 | mutex_destroy(&sensor->lock); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | static int mt9m032_remove(struct i2c_client *client) |
| 865 | { |
| 866 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); |
| 867 | struct mt9m032 *sensor = to_mt9m032(subdev); |
| 868 | |
Guennadi Liakhovetski | b22b9f3 | 2012-04-18 05:00:52 -0300 | [diff] [blame] | 869 | v4l2_device_unregister_subdev(subdev); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 870 | v4l2_ctrl_handler_free(&sensor->ctrls); |
Guennadi Liakhovetski | b22b9f3 | 2012-04-18 05:00:52 -0300 | [diff] [blame] | 871 | media_entity_cleanup(&subdev->entity); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 872 | mutex_destroy(&sensor->lock); |
Martin Hostettler | 251cb88 | 2011-12-17 07:10:55 -0300 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | static const struct i2c_device_id mt9m032_id_table[] = { |
| 877 | { MT9M032_NAME, 0 }, |
| 878 | { } |
| 879 | }; |
| 880 | |
| 881 | MODULE_DEVICE_TABLE(i2c, mt9m032_id_table); |
| 882 | |
| 883 | static struct i2c_driver mt9m032_i2c_driver = { |
| 884 | .driver = { |
| 885 | .name = MT9M032_NAME, |
| 886 | }, |
| 887 | .probe = mt9m032_probe, |
| 888 | .remove = mt9m032_remove, |
| 889 | .id_table = mt9m032_id_table, |
| 890 | }; |
| 891 | |
| 892 | module_i2c_driver(mt9m032_i2c_driver); |
| 893 | |
| 894 | MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>"); |
| 895 | MODULE_DESCRIPTION("MT9M032 camera sensor driver"); |
| 896 | MODULE_LICENSE("GPL v2"); |