Merge branch 'stanton-cs1-driver' of git://git.alsa-project.org/alsa-kprivate into...
[deliverable/linux.git] / drivers / media / i2c / tcm825x.c
1 /*
2 * drivers/media/i2c/tcm825x.c
3 *
4 * TCM825X camera sensor driver.
5 *
6 * Copyright (C) 2007 Nokia Corporation.
7 *
8 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
9 *
10 * Based on code from David Cohen <david.cohen@indt.org.br>
11 *
12 * This driver was based on ov9640 sensor driver from MontaVista
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * version 2 as published by the Free Software Foundation.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 */
28
29 #include <linux/i2c.h>
30 #include <linux/module.h>
31 #include <media/v4l2-int-device.h>
32
33 #include "tcm825x.h"
34
35 /*
36 * The sensor has two fps modes: the lower one just gives half the fps
37 * at the same xclk than the high one.
38 */
39 #define MAX_FPS 30
40 #define MIN_FPS 8
41 #define MAX_HALF_FPS (MAX_FPS / 2)
42 #define HIGH_FPS_MODE_LOWER_LIMIT 14
43 #define DEFAULT_FPS MAX_HALF_FPS
44
45 struct tcm825x_sensor {
46 const struct tcm825x_platform_data *platform_data;
47 struct v4l2_int_device *v4l2_int_device;
48 struct i2c_client *i2c_client;
49 struct v4l2_pix_format pix;
50 struct v4l2_fract timeperframe;
51 };
52
53 /* list of image formats supported by TCM825X sensor */
54 static const struct v4l2_fmtdesc tcm825x_formats[] = {
55 {
56 .description = "YUYV (YUV 4:2:2), packed",
57 .pixelformat = V4L2_PIX_FMT_UYVY,
58 }, {
59 /* Note: V4L2 defines RGB565 as:
60 *
61 * Byte 0 Byte 1
62 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
63 *
64 * We interpret RGB565 as:
65 *
66 * Byte 0 Byte 1
67 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
68 */
69 .description = "RGB565, le",
70 .pixelformat = V4L2_PIX_FMT_RGB565,
71 },
72 };
73
74 #define TCM825X_NUM_CAPTURE_FORMATS ARRAY_SIZE(tcm825x_formats)
75
76 /*
77 * TCM825X register configuration for all combinations of pixel format and
78 * image size
79 */
80 static const struct tcm825x_reg subqcif = { 0x20, TCM825X_PICSIZ };
81 static const struct tcm825x_reg qcif = { 0x18, TCM825X_PICSIZ };
82 static const struct tcm825x_reg cif = { 0x14, TCM825X_PICSIZ };
83 static const struct tcm825x_reg qqvga = { 0x0c, TCM825X_PICSIZ };
84 static const struct tcm825x_reg qvga = { 0x04, TCM825X_PICSIZ };
85 static const struct tcm825x_reg vga = { 0x00, TCM825X_PICSIZ };
86
87 static const struct tcm825x_reg yuv422 = { 0x00, TCM825X_PICFMT };
88 static const struct tcm825x_reg rgb565 = { 0x02, TCM825X_PICFMT };
89
90 /* Our own specific controls */
91 #define V4L2_CID_ALC V4L2_CID_PRIVATE_BASE
92 #define V4L2_CID_H_EDGE_EN V4L2_CID_PRIVATE_BASE + 1
93 #define V4L2_CID_V_EDGE_EN V4L2_CID_PRIVATE_BASE + 2
94 #define V4L2_CID_LENS V4L2_CID_PRIVATE_BASE + 3
95 #define V4L2_CID_MAX_EXPOSURE_TIME V4L2_CID_PRIVATE_BASE + 4
96 #define V4L2_CID_LAST_PRIV V4L2_CID_MAX_EXPOSURE_TIME
97
98 /* Video controls */
99 static struct vcontrol {
100 struct v4l2_queryctrl qc;
101 u16 reg;
102 u16 start_bit;
103 } video_control[] = {
104 {
105 {
106 .id = V4L2_CID_GAIN,
107 .type = V4L2_CTRL_TYPE_INTEGER,
108 .name = "Gain",
109 .minimum = 0,
110 .maximum = 63,
111 .step = 1,
112 },
113 .reg = TCM825X_AG,
114 .start_bit = 0,
115 },
116 {
117 {
118 .id = V4L2_CID_RED_BALANCE,
119 .type = V4L2_CTRL_TYPE_INTEGER,
120 .name = "Red Balance",
121 .minimum = 0,
122 .maximum = 255,
123 .step = 1,
124 },
125 .reg = TCM825X_MRG,
126 .start_bit = 0,
127 },
128 {
129 {
130 .id = V4L2_CID_BLUE_BALANCE,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Blue Balance",
133 .minimum = 0,
134 .maximum = 255,
135 .step = 1,
136 },
137 .reg = TCM825X_MBG,
138 .start_bit = 0,
139 },
140 {
141 {
142 .id = V4L2_CID_AUTO_WHITE_BALANCE,
143 .type = V4L2_CTRL_TYPE_BOOLEAN,
144 .name = "Auto White Balance",
145 .minimum = 0,
146 .maximum = 1,
147 .step = 0,
148 },
149 .reg = TCM825X_AWBSW,
150 .start_bit = 7,
151 },
152 {
153 {
154 .id = V4L2_CID_EXPOSURE,
155 .type = V4L2_CTRL_TYPE_INTEGER,
156 .name = "Exposure Time",
157 .minimum = 0,
158 .maximum = 0x1fff,
159 .step = 1,
160 },
161 .reg = TCM825X_ESRSPD_U,
162 .start_bit = 0,
163 },
164 {
165 {
166 .id = V4L2_CID_HFLIP,
167 .type = V4L2_CTRL_TYPE_BOOLEAN,
168 .name = "Mirror Image",
169 .minimum = 0,
170 .maximum = 1,
171 .step = 0,
172 },
173 .reg = TCM825X_H_INV,
174 .start_bit = 6,
175 },
176 {
177 {
178 .id = V4L2_CID_VFLIP,
179 .type = V4L2_CTRL_TYPE_BOOLEAN,
180 .name = "Vertical Flip",
181 .minimum = 0,
182 .maximum = 1,
183 .step = 0,
184 },
185 .reg = TCM825X_V_INV,
186 .start_bit = 7,
187 },
188 /* Private controls */
189 {
190 {
191 .id = V4L2_CID_ALC,
192 .type = V4L2_CTRL_TYPE_BOOLEAN,
193 .name = "Auto Luminance Control",
194 .minimum = 0,
195 .maximum = 1,
196 .step = 0,
197 },
198 .reg = TCM825X_ALCSW,
199 .start_bit = 7,
200 },
201 {
202 {
203 .id = V4L2_CID_H_EDGE_EN,
204 .type = V4L2_CTRL_TYPE_INTEGER,
205 .name = "Horizontal Edge Enhancement",
206 .minimum = 0,
207 .maximum = 0xff,
208 .step = 1,
209 },
210 .reg = TCM825X_HDTG,
211 .start_bit = 0,
212 },
213 {
214 {
215 .id = V4L2_CID_V_EDGE_EN,
216 .type = V4L2_CTRL_TYPE_INTEGER,
217 .name = "Vertical Edge Enhancement",
218 .minimum = 0,
219 .maximum = 0xff,
220 .step = 1,
221 },
222 .reg = TCM825X_VDTG,
223 .start_bit = 0,
224 },
225 {
226 {
227 .id = V4L2_CID_LENS,
228 .type = V4L2_CTRL_TYPE_INTEGER,
229 .name = "Lens Shading Compensation",
230 .minimum = 0,
231 .maximum = 0x3f,
232 .step = 1,
233 },
234 .reg = TCM825X_LENS,
235 .start_bit = 0,
236 },
237 {
238 {
239 .id = V4L2_CID_MAX_EXPOSURE_TIME,
240 .type = V4L2_CTRL_TYPE_INTEGER,
241 .name = "Maximum Exposure Time",
242 .minimum = 0,
243 .maximum = 0x3,
244 .step = 1,
245 },
246 .reg = TCM825X_ESRLIM,
247 .start_bit = 5,
248 },
249 };
250
251
252 static const struct tcm825x_reg *tcm825x_siz_reg[NUM_IMAGE_SIZES] =
253 { &subqcif, &qqvga, &qcif, &qvga, &cif, &vga };
254
255 static const struct tcm825x_reg *tcm825x_fmt_reg[NUM_PIXEL_FORMATS] =
256 { &yuv422, &rgb565 };
257
258 /*
259 * Read a value from a register in an TCM825X sensor device. The value is
260 * returned in 'val'.
261 * Returns zero if successful, or non-zero otherwise.
262 */
263 static int tcm825x_read_reg(struct i2c_client *client, int reg)
264 {
265 int err;
266 struct i2c_msg msg[2];
267 u8 reg_buf, data_buf = 0;
268
269 if (!client->adapter)
270 return -ENODEV;
271
272 msg[0].addr = client->addr;
273 msg[0].flags = 0;
274 msg[0].len = 1;
275 msg[0].buf = &reg_buf;
276 msg[1].addr = client->addr;
277 msg[1].flags = I2C_M_RD;
278 msg[1].len = 1;
279 msg[1].buf = &data_buf;
280
281 reg_buf = reg;
282
283 err = i2c_transfer(client->adapter, msg, 2);
284 if (err < 0)
285 return err;
286 return data_buf;
287 }
288
289 /*
290 * Write a value to a register in an TCM825X sensor device.
291 * Returns zero if successful, or non-zero otherwise.
292 */
293 static int tcm825x_write_reg(struct i2c_client *client, u8 reg, u8 val)
294 {
295 int err;
296 struct i2c_msg msg[1];
297 unsigned char data[2];
298
299 if (!client->adapter)
300 return -ENODEV;
301
302 msg->addr = client->addr;
303 msg->flags = 0;
304 msg->len = 2;
305 msg->buf = data;
306 data[0] = reg;
307 data[1] = val;
308 err = i2c_transfer(client->adapter, msg, 1);
309 if (err >= 0)
310 return 0;
311 return err;
312 }
313
314 static int __tcm825x_write_reg_mask(struct i2c_client *client,
315 u8 reg, u8 val, u8 mask)
316 {
317 int rc;
318
319 /* need to do read - modify - write */
320 rc = tcm825x_read_reg(client, reg);
321 if (rc < 0)
322 return rc;
323
324 rc &= (~mask); /* Clear the masked bits */
325 val &= mask; /* Enforce mask on value */
326 val |= rc;
327
328 /* write the new value to the register */
329 rc = tcm825x_write_reg(client, reg, val);
330 if (rc)
331 return rc;
332
333 return 0;
334 }
335
336 #define tcm825x_write_reg_mask(client, regmask, val) \
337 __tcm825x_write_reg_mask(client, TCM825X_ADDR((regmask)), val, \
338 TCM825X_MASK((regmask)))
339
340
341 /*
342 * Initialize a list of TCM825X registers.
343 * The list of registers is terminated by the pair of values
344 * { TCM825X_REG_TERM, TCM825X_VAL_TERM }.
345 * Returns zero if successful, or non-zero otherwise.
346 */
347 static int tcm825x_write_default_regs(struct i2c_client *client,
348 const struct tcm825x_reg *reglist)
349 {
350 int err;
351 const struct tcm825x_reg *next = reglist;
352
353 while (!((next->reg == TCM825X_REG_TERM)
354 && (next->val == TCM825X_VAL_TERM))) {
355 err = tcm825x_write_reg(client, next->reg, next->val);
356 if (err) {
357 dev_err(&client->dev, "register writing failed\n");
358 return err;
359 }
360 next++;
361 }
362
363 return 0;
364 }
365
366 static struct vcontrol *find_vctrl(int id)
367 {
368 int i;
369
370 if (id < V4L2_CID_BASE)
371 return NULL;
372
373 for (i = 0; i < ARRAY_SIZE(video_control); i++)
374 if (video_control[i].qc.id == id)
375 return &video_control[i];
376
377 return NULL;
378 }
379
380 /*
381 * Find the best match for a requested image capture size. The best match
382 * is chosen as the nearest match that has the same number or fewer pixels
383 * as the requested size, or the smallest image size if the requested size
384 * has fewer pixels than the smallest image.
385 */
386 static enum image_size tcm825x_find_size(struct v4l2_int_device *s,
387 unsigned int width,
388 unsigned int height)
389 {
390 enum image_size isize;
391 unsigned long pixels = width * height;
392 struct tcm825x_sensor *sensor = s->priv;
393
394 for (isize = subQCIF; isize < VGA; isize++) {
395 if (tcm825x_sizes[isize + 1].height
396 * tcm825x_sizes[isize + 1].width > pixels) {
397 dev_dbg(&sensor->i2c_client->dev, "size %d\n", isize);
398
399 return isize;
400 }
401 }
402
403 dev_dbg(&sensor->i2c_client->dev, "format default VGA\n");
404
405 return VGA;
406 }
407
408 /*
409 * Configure the TCM825X for current image size, pixel format, and
410 * frame period. fper is the frame period (in seconds) expressed as a
411 * fraction. Returns zero if successful, or non-zero otherwise. The
412 * actual frame period is returned in fper.
413 */
414 static int tcm825x_configure(struct v4l2_int_device *s)
415 {
416 struct tcm825x_sensor *sensor = s->priv;
417 struct v4l2_pix_format *pix = &sensor->pix;
418 enum image_size isize = tcm825x_find_size(s, pix->width, pix->height);
419 struct v4l2_fract *fper = &sensor->timeperframe;
420 enum pixel_format pfmt;
421 int err;
422 u32 tgt_fps;
423 u8 val;
424
425 /* common register initialization */
426 err = tcm825x_write_default_regs(
427 sensor->i2c_client, sensor->platform_data->default_regs());
428 if (err)
429 return err;
430
431 /* configure image size */
432 val = tcm825x_siz_reg[isize]->val;
433 dev_dbg(&sensor->i2c_client->dev,
434 "configuring image size %d\n", isize);
435 err = tcm825x_write_reg_mask(sensor->i2c_client,
436 tcm825x_siz_reg[isize]->reg, val);
437 if (err)
438 return err;
439
440 /* configure pixel format */
441 switch (pix->pixelformat) {
442 default:
443 case V4L2_PIX_FMT_RGB565:
444 pfmt = RGB565;
445 break;
446 case V4L2_PIX_FMT_UYVY:
447 pfmt = YUV422;
448 break;
449 }
450
451 dev_dbg(&sensor->i2c_client->dev,
452 "configuring pixel format %d\n", pfmt);
453 val = tcm825x_fmt_reg[pfmt]->val;
454
455 err = tcm825x_write_reg_mask(sensor->i2c_client,
456 tcm825x_fmt_reg[pfmt]->reg, val);
457 if (err)
458 return err;
459
460 /*
461 * For frame rate < 15, the FPS reg (addr 0x02, bit 7) must be
462 * set. Frame rate will be halved from the normal.
463 */
464 tgt_fps = fper->denominator / fper->numerator;
465 if (tgt_fps <= HIGH_FPS_MODE_LOWER_LIMIT) {
466 val = tcm825x_read_reg(sensor->i2c_client, 0x02);
467 val |= 0x80;
468 tcm825x_write_reg(sensor->i2c_client, 0x02, val);
469 }
470
471 return 0;
472 }
473
474 static int ioctl_queryctrl(struct v4l2_int_device *s,
475 struct v4l2_queryctrl *qc)
476 {
477 struct vcontrol *control;
478
479 control = find_vctrl(qc->id);
480
481 if (control == NULL)
482 return -EINVAL;
483
484 *qc = control->qc;
485
486 return 0;
487 }
488
489 static int ioctl_g_ctrl(struct v4l2_int_device *s,
490 struct v4l2_control *vc)
491 {
492 struct tcm825x_sensor *sensor = s->priv;
493 struct i2c_client *client = sensor->i2c_client;
494 int val, r;
495 struct vcontrol *lvc;
496
497 /* exposure time is special, spread across 2 registers */
498 if (vc->id == V4L2_CID_EXPOSURE) {
499 int val_lower, val_upper;
500
501 val_upper = tcm825x_read_reg(client,
502 TCM825X_ADDR(TCM825X_ESRSPD_U));
503 if (val_upper < 0)
504 return val_upper;
505 val_lower = tcm825x_read_reg(client,
506 TCM825X_ADDR(TCM825X_ESRSPD_L));
507 if (val_lower < 0)
508 return val_lower;
509
510 vc->value = ((val_upper & 0x1f) << 8) | (val_lower);
511 return 0;
512 }
513
514 lvc = find_vctrl(vc->id);
515 if (lvc == NULL)
516 return -EINVAL;
517
518 r = tcm825x_read_reg(client, TCM825X_ADDR(lvc->reg));
519 if (r < 0)
520 return r;
521 val = r & TCM825X_MASK(lvc->reg);
522 val >>= lvc->start_bit;
523
524 if (val < 0)
525 return val;
526
527 if (vc->id == V4L2_CID_HFLIP || vc->id == V4L2_CID_VFLIP)
528 val ^= sensor->platform_data->is_upside_down();
529
530 vc->value = val;
531 return 0;
532 }
533
534 static int ioctl_s_ctrl(struct v4l2_int_device *s,
535 struct v4l2_control *vc)
536 {
537 struct tcm825x_sensor *sensor = s->priv;
538 struct i2c_client *client = sensor->i2c_client;
539 struct vcontrol *lvc;
540 int val = vc->value;
541
542 /* exposure time is special, spread across 2 registers */
543 if (vc->id == V4L2_CID_EXPOSURE) {
544 int val_lower, val_upper;
545 val_lower = val & TCM825X_MASK(TCM825X_ESRSPD_L);
546 val_upper = (val >> 8) & TCM825X_MASK(TCM825X_ESRSPD_U);
547
548 if (tcm825x_write_reg_mask(client,
549 TCM825X_ESRSPD_U, val_upper))
550 return -EIO;
551
552 if (tcm825x_write_reg_mask(client,
553 TCM825X_ESRSPD_L, val_lower))
554 return -EIO;
555
556 return 0;
557 }
558
559 lvc = find_vctrl(vc->id);
560 if (lvc == NULL)
561 return -EINVAL;
562
563 if (vc->id == V4L2_CID_HFLIP || vc->id == V4L2_CID_VFLIP)
564 val ^= sensor->platform_data->is_upside_down();
565
566 val = val << lvc->start_bit;
567 if (tcm825x_write_reg_mask(client, lvc->reg, val))
568 return -EIO;
569
570 return 0;
571 }
572
573 static int ioctl_enum_fmt_cap(struct v4l2_int_device *s,
574 struct v4l2_fmtdesc *fmt)
575 {
576 int index = fmt->index;
577
578 switch (fmt->type) {
579 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
580 if (index >= TCM825X_NUM_CAPTURE_FORMATS)
581 return -EINVAL;
582 break;
583
584 default:
585 return -EINVAL;
586 }
587
588 fmt->flags = tcm825x_formats[index].flags;
589 strlcpy(fmt->description, tcm825x_formats[index].description,
590 sizeof(fmt->description));
591 fmt->pixelformat = tcm825x_formats[index].pixelformat;
592
593 return 0;
594 }
595
596 static int ioctl_try_fmt_cap(struct v4l2_int_device *s,
597 struct v4l2_format *f)
598 {
599 struct tcm825x_sensor *sensor = s->priv;
600 enum image_size isize;
601 int ifmt;
602 struct v4l2_pix_format *pix = &f->fmt.pix;
603
604 isize = tcm825x_find_size(s, pix->width, pix->height);
605 dev_dbg(&sensor->i2c_client->dev, "isize = %d num_capture = %lu\n",
606 isize, (unsigned long)TCM825X_NUM_CAPTURE_FORMATS);
607
608 pix->width = tcm825x_sizes[isize].width;
609 pix->height = tcm825x_sizes[isize].height;
610
611 for (ifmt = 0; ifmt < TCM825X_NUM_CAPTURE_FORMATS; ifmt++)
612 if (pix->pixelformat == tcm825x_formats[ifmt].pixelformat)
613 break;
614
615 if (ifmt == TCM825X_NUM_CAPTURE_FORMATS)
616 ifmt = 0; /* Default = YUV 4:2:2 */
617
618 pix->pixelformat = tcm825x_formats[ifmt].pixelformat;
619 pix->field = V4L2_FIELD_NONE;
620 pix->bytesperline = pix->width * TCM825X_BYTES_PER_PIXEL;
621 pix->sizeimage = pix->bytesperline * pix->height;
622 pix->priv = 0;
623 dev_dbg(&sensor->i2c_client->dev, "format = 0x%08x\n",
624 pix->pixelformat);
625
626 switch (pix->pixelformat) {
627 case V4L2_PIX_FMT_UYVY:
628 default:
629 pix->colorspace = V4L2_COLORSPACE_JPEG;
630 break;
631 case V4L2_PIX_FMT_RGB565:
632 pix->colorspace = V4L2_COLORSPACE_SRGB;
633 break;
634 }
635
636 return 0;
637 }
638
639 static int ioctl_s_fmt_cap(struct v4l2_int_device *s,
640 struct v4l2_format *f)
641 {
642 struct tcm825x_sensor *sensor = s->priv;
643 struct v4l2_pix_format *pix = &f->fmt.pix;
644 int rval;
645
646 rval = ioctl_try_fmt_cap(s, f);
647 if (rval)
648 return rval;
649
650 rval = tcm825x_configure(s);
651
652 sensor->pix = *pix;
653
654 return rval;
655 }
656
657 static int ioctl_g_fmt_cap(struct v4l2_int_device *s,
658 struct v4l2_format *f)
659 {
660 struct tcm825x_sensor *sensor = s->priv;
661
662 f->fmt.pix = sensor->pix;
663
664 return 0;
665 }
666
667 static int ioctl_g_parm(struct v4l2_int_device *s,
668 struct v4l2_streamparm *a)
669 {
670 struct tcm825x_sensor *sensor = s->priv;
671 struct v4l2_captureparm *cparm = &a->parm.capture;
672
673 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
674 return -EINVAL;
675
676 memset(a, 0, sizeof(*a));
677 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
678
679 cparm->capability = V4L2_CAP_TIMEPERFRAME;
680 cparm->timeperframe = sensor->timeperframe;
681
682 return 0;
683 }
684
685 static int ioctl_s_parm(struct v4l2_int_device *s,
686 struct v4l2_streamparm *a)
687 {
688 struct tcm825x_sensor *sensor = s->priv;
689 struct v4l2_fract *timeperframe = &a->parm.capture.timeperframe;
690 u32 tgt_fps; /* target frames per secound */
691 int rval;
692
693 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
694 return -EINVAL;
695
696 if ((timeperframe->numerator == 0)
697 || (timeperframe->denominator == 0)) {
698 timeperframe->denominator = DEFAULT_FPS;
699 timeperframe->numerator = 1;
700 }
701
702 tgt_fps = timeperframe->denominator / timeperframe->numerator;
703
704 if (tgt_fps > MAX_FPS) {
705 timeperframe->denominator = MAX_FPS;
706 timeperframe->numerator = 1;
707 } else if (tgt_fps < MIN_FPS) {
708 timeperframe->denominator = MIN_FPS;
709 timeperframe->numerator = 1;
710 }
711
712 sensor->timeperframe = *timeperframe;
713
714 rval = tcm825x_configure(s);
715
716 return rval;
717 }
718
719 static int ioctl_s_power(struct v4l2_int_device *s, int on)
720 {
721 struct tcm825x_sensor *sensor = s->priv;
722
723 return sensor->platform_data->power_set(on);
724 }
725
726 /*
727 * Given the image capture format in pix, the nominal frame period in
728 * timeperframe, calculate the required xclk frequency.
729 *
730 * TCM825X input frequency characteristics are:
731 * Minimum 11.9 MHz, Typical 24.57 MHz and maximum 25/27 MHz
732 */
733
734 static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
735 {
736 struct tcm825x_sensor *sensor = s->priv;
737 struct v4l2_fract *timeperframe = &sensor->timeperframe;
738 u32 tgt_xclk; /* target xclk */
739 u32 tgt_fps; /* target frames per secound */
740 int rval;
741
742 rval = sensor->platform_data->ifparm(p);
743 if (rval)
744 return rval;
745
746 tgt_fps = timeperframe->denominator / timeperframe->numerator;
747
748 tgt_xclk = (tgt_fps <= HIGH_FPS_MODE_LOWER_LIMIT) ?
749 (2457 * tgt_fps) / MAX_HALF_FPS :
750 (2457 * tgt_fps) / MAX_FPS;
751 tgt_xclk *= 10000;
752
753 tgt_xclk = min(tgt_xclk, (u32)TCM825X_XCLK_MAX);
754 tgt_xclk = max(tgt_xclk, (u32)TCM825X_XCLK_MIN);
755
756 p->u.bt656.clock_curr = tgt_xclk;
757
758 return 0;
759 }
760
761 static int ioctl_g_needs_reset(struct v4l2_int_device *s, void *buf)
762 {
763 struct tcm825x_sensor *sensor = s->priv;
764
765 return sensor->platform_data->needs_reset(s, buf, &sensor->pix);
766 }
767
768 static int ioctl_reset(struct v4l2_int_device *s)
769 {
770 return -EBUSY;
771 }
772
773 static int ioctl_init(struct v4l2_int_device *s)
774 {
775 return tcm825x_configure(s);
776 }
777
778 static int ioctl_dev_exit(struct v4l2_int_device *s)
779 {
780 return 0;
781 }
782
783 static int ioctl_dev_init(struct v4l2_int_device *s)
784 {
785 struct tcm825x_sensor *sensor = s->priv;
786 int r;
787
788 r = tcm825x_read_reg(sensor->i2c_client, 0x01);
789 if (r < 0)
790 return r;
791 if (r == 0) {
792 dev_err(&sensor->i2c_client->dev, "device not detected\n");
793 return -EIO;
794 }
795 return 0;
796 }
797
798 static struct v4l2_int_ioctl_desc tcm825x_ioctl_desc[] = {
799 { vidioc_int_dev_init_num,
800 (v4l2_int_ioctl_func *)ioctl_dev_init },
801 { vidioc_int_dev_exit_num,
802 (v4l2_int_ioctl_func *)ioctl_dev_exit },
803 { vidioc_int_s_power_num,
804 (v4l2_int_ioctl_func *)ioctl_s_power },
805 { vidioc_int_g_ifparm_num,
806 (v4l2_int_ioctl_func *)ioctl_g_ifparm },
807 { vidioc_int_g_needs_reset_num,
808 (v4l2_int_ioctl_func *)ioctl_g_needs_reset },
809 { vidioc_int_reset_num,
810 (v4l2_int_ioctl_func *)ioctl_reset },
811 { vidioc_int_init_num,
812 (v4l2_int_ioctl_func *)ioctl_init },
813 { vidioc_int_enum_fmt_cap_num,
814 (v4l2_int_ioctl_func *)ioctl_enum_fmt_cap },
815 { vidioc_int_try_fmt_cap_num,
816 (v4l2_int_ioctl_func *)ioctl_try_fmt_cap },
817 { vidioc_int_g_fmt_cap_num,
818 (v4l2_int_ioctl_func *)ioctl_g_fmt_cap },
819 { vidioc_int_s_fmt_cap_num,
820 (v4l2_int_ioctl_func *)ioctl_s_fmt_cap },
821 { vidioc_int_g_parm_num,
822 (v4l2_int_ioctl_func *)ioctl_g_parm },
823 { vidioc_int_s_parm_num,
824 (v4l2_int_ioctl_func *)ioctl_s_parm },
825 { vidioc_int_queryctrl_num,
826 (v4l2_int_ioctl_func *)ioctl_queryctrl },
827 { vidioc_int_g_ctrl_num,
828 (v4l2_int_ioctl_func *)ioctl_g_ctrl },
829 { vidioc_int_s_ctrl_num,
830 (v4l2_int_ioctl_func *)ioctl_s_ctrl },
831 };
832
833 static struct v4l2_int_slave tcm825x_slave = {
834 .ioctls = tcm825x_ioctl_desc,
835 .num_ioctls = ARRAY_SIZE(tcm825x_ioctl_desc),
836 };
837
838 static struct tcm825x_sensor tcm825x;
839
840 static struct v4l2_int_device tcm825x_int_device = {
841 .module = THIS_MODULE,
842 .name = TCM825X_NAME,
843 .priv = &tcm825x,
844 .type = v4l2_int_type_slave,
845 .u = {
846 .slave = &tcm825x_slave,
847 },
848 };
849
850 static int tcm825x_probe(struct i2c_client *client,
851 const struct i2c_device_id *did)
852 {
853 struct tcm825x_sensor *sensor = &tcm825x;
854
855 if (i2c_get_clientdata(client))
856 return -EBUSY;
857
858 sensor->platform_data = client->dev.platform_data;
859
860 if (sensor->platform_data == NULL
861 || !sensor->platform_data->is_okay())
862 return -ENODEV;
863
864 sensor->v4l2_int_device = &tcm825x_int_device;
865
866 sensor->i2c_client = client;
867 i2c_set_clientdata(client, sensor);
868
869 /* Make the default capture format QVGA RGB565 */
870 sensor->pix.width = tcm825x_sizes[QVGA].width;
871 sensor->pix.height = tcm825x_sizes[QVGA].height;
872 sensor->pix.pixelformat = V4L2_PIX_FMT_RGB565;
873
874 return v4l2_int_device_register(sensor->v4l2_int_device);
875 }
876
877 static int tcm825x_remove(struct i2c_client *client)
878 {
879 struct tcm825x_sensor *sensor = i2c_get_clientdata(client);
880
881 if (!client->adapter)
882 return -ENODEV; /* our client isn't attached */
883
884 v4l2_int_device_unregister(sensor->v4l2_int_device);
885
886 return 0;
887 }
888
889 static const struct i2c_device_id tcm825x_id[] = {
890 { "tcm825x", 0 },
891 { }
892 };
893 MODULE_DEVICE_TABLE(i2c, tcm825x_id);
894
895 static struct i2c_driver tcm825x_i2c_driver = {
896 .driver = {
897 .name = TCM825X_NAME,
898 },
899 .probe = tcm825x_probe,
900 .remove = tcm825x_remove,
901 .id_table = tcm825x_id,
902 };
903
904 static struct tcm825x_sensor tcm825x = {
905 .timeperframe = {
906 .numerator = 1,
907 .denominator = DEFAULT_FPS,
908 },
909 };
910
911 static int __init tcm825x_init(void)
912 {
913 int rval;
914
915 rval = i2c_add_driver(&tcm825x_i2c_driver);
916 if (rval)
917 printk(KERN_INFO "%s: failed registering " TCM825X_NAME "\n",
918 __func__);
919
920 return rval;
921 }
922
923 static void __exit tcm825x_exit(void)
924 {
925 i2c_del_driver(&tcm825x_i2c_driver);
926 }
927
928 /*
929 * FIXME: Menelaus isn't ready (?) at module_init stage, so use
930 * late_initcall for now.
931 */
932 late_initcall(tcm825x_init);
933 module_exit(tcm825x_exit);
934
935 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
936 MODULE_DESCRIPTION("TCM825x camera sensor driver");
937 MODULE_LICENSE("GPL");
This page took 0.068661 seconds and 6 git commands to generate.