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