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