• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * A V4L2 driver for s5k5e9_mipi cameras.
3  *
4  * Copyright (c) 2018 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
5  *
6  * Authors:  Zheng ZeQun <zequnzheng@allwinnertech.com>
7  *    Liang WeiJie <liangweijie@allwinnertech.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/videodev2.h>
20 #include <linux/clk.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-mediabus.h>
23 #include <linux/io.h>
24 
25 #include "camera.h"
26 #include "sensor_helper.h"
27 
28 MODULE_AUTHOR("lwj");
29 MODULE_DESCRIPTION("A low-level driver for s5k5e9 sensors");
30 MODULE_LICENSE("GPL");
31 
32 #define MCLK              (24*1000*1000)
33 #define V4L2_IDENT_SENSOR 0x559b
34 
35 /*
36  * Our nominal (default) frame rate.
37  */
38 
39 #define SENSOR_FRAME_RATE 15
40 
41 /*
42  * The s5k5e9_mipi sits on i2c with ID 0x20
43  */
44 #define I2C_ADDR 0x20
45 
46 #define SENSOR_NAME "s5k5e9"
47 
48 
49 
50 struct cfg_array { /* coming later */
51 	struct regval_list *regs;
52 	int size;
53 };
54 
55 /*
56  * The default register settings
57  *
58  */
59 static struct regval_list sensor_default_regs[] = {
60 
61 };
62 
63 /* 1280*720 15fps */
64 static struct regval_list sensor_720p_regs[] = {
65 	{0x0100, 0x00},
66 	{0x3B45, 0x01},
67 	{0x0B05, 0x01},
68 	{0x392F, 0x01},
69 	{0x3930, 0x00},
70 	{0x3924, 0x7F},
71 	{0x3925, 0xFD},
72 	{0x3C08, 0xFF},
73 	{0x3C09, 0xFF},
74 	{0x3C31, 0xFF},
75 	{0x3C32, 0xFF},
76 	{0x3290, 0x10},
77 	{0x3200, 0x01},
78 	{0x3074, 0x06},
79 	{0x3075, 0x2F},
80 	{0x308A, 0x20},
81 	{0x308B, 0x08},
82 	{0x308C, 0x0B},
83 	{0x3081, 0x07},
84 	{0x307B, 0x85},
85 	{0x307A, 0x0A},
86 	{0x3079, 0x0A},
87 	{0x306E, 0x71},
88 	{0x306F, 0x28},
89 	{0x301F, 0x20},
90 	{0x3012, 0x4E},
91 	{0x306B, 0x9A},
92 	{0x3091, 0x16},
93 	{0x30C4, 0x06},
94 	{0x306A, 0x79},
95 	{0x30B0, 0xFF},
96 	{0x306D, 0x08},
97 	{0x3084, 0x16},
98 	{0x3070, 0x0F},
99 	{0x30C2, 0x05},
100 	{0x3069, 0x87},
101 	{0x3C0F, 0x00},
102 	{0x0A02, 0x3F},
103 	{0x3083, 0x14},
104 	{0x3080, 0x08},
105 	{0x3C34, 0xEA},
106 	{0x3C35, 0x5C},
107 
108 	/* mode */
109 	{0x0136, 0x18},
110 	{0x0137, 0x00},
111 	{0x0305, 0x04},
112 	{0x0306, 0x00},
113 	{0x0307, 0x5F},
114 	{0x030D, 0x04},
115 	{0x030E, 0x00},
116 	{0x030F, 0x92},
117 	{0x3C1F, 0x00},
118 	{0x3C17, 0x00},
119 	{0x0112, 0x0A},
120 	{0x0113, 0x0A},
121 	{0x0114, 0x01},
122 	{0x0820, 0x03},
123 	{0x0821, 0x6C},
124 	{0x0822, 0x00},
125 	{0x0823, 0x00},
126 	{0x3929, 0x0F},
127 	{0x0344, 0x00},
128 	{0x0345, 0x18},
129 	{0x0346, 0x01},
130 	{0x0347, 0x04},
131 	{0x0348, 0x0A},
132 	{0x0349, 0x17},
133 	{0x034A, 0x06},
134 	{0x034B, 0xA3},
135 	{0x034C, 0x05},
136 	{0x034D, 0x00},
137 	{0x034E, 0x02},
138 	{0x034F, 0xD0},
139 	{0x0900, 0x01},
140 	{0x0901, 0x22},
141 	{0x0381, 0x01},
142 	{0x0383, 0x01},
143 	{0x0385, 0x01},
144 	{0x0387, 0x03},
145 	{0x0101, 0x00},
146 	{0x0340, 0x0F},
147 	{0x0341, 0xE4},
148 	{0x0342, 0x0C},
149 	{0x0343, 0x28},
150 	{0x0200, 0x0B},
151 	{0x0201, 0x9C},
152 	{0x0202, 0x00},
153 	{0x0203, 0x02},
154 	{0x30B8, 0x2A},
155 	{0x30BA, 0x2E},
156 	{0x0100, 0x01},
157 };
158 
159 /*
160  * Here we'll try to encapsulate the changes for just the output
161  * video format.
162  *
163  */
164 static struct regval_list sensor_fmt_raw[] = {
165 
166 };
167 
168 /*
169  * Code for dealing with controls.
170  * fill with different sensor module
171  * different sensor module has different settings here
172  * if not support the follow function ,retrun -EINVAL
173  */
174 
sensor_g_exp(struct v4l2_subdev * sd,__s32 * value)175 static int sensor_g_exp(struct v4l2_subdev *sd, __s32 *value)
176 {
177 	struct sensor_info *info = to_state(sd);
178 	*value = info->exp;
179 	sensor_print("sensor_get_exposure = %d\n", info->exp);
180 	return 0;
181 }
182 
sensor_s_exp(struct v4l2_subdev * sd,unsigned int exp_val)183 static int sensor_s_exp(struct v4l2_subdev *sd, unsigned int exp_val)
184 {
185 	data_type explow, exphigh, tmp1, tmp2;
186 	unsigned short tmp = 0;
187 	struct sensor_info *info = to_state(sd);
188 
189 	if (exp_val > 0xffffff)
190 		exp_val = 0xfffff0;
191 	if (exp_val < 16)
192 		exp_val = 16;
193 	exp_val = (exp_val + 8) >> 4; /* rounding to 1 */
194 	exphigh = (unsigned char)((0xff00 & exp_val) >> 8);
195 	explow  = (unsigned char)((0x00ff & exp_val));
196 
197 
198 	sensor_write(sd, 0x0104, 0x01);
199 	sensor_write(sd, 0x0203, explow);/* coarse integration time */
200 	sensor_write(sd, 0x0202, exphigh);
201 
202 	sensor_write(sd, 0x0104, 0x00);
203 	sensor_read(sd, 0x0203, &tmp1);
204 	sensor_read(sd, 0x0202, &tmp2);
205 	tmp = ((tmp2 << 8) | tmp1);
206 
207 	sensor_dbg("sensor_s_exp info->exp %d\n", exp_val);
208 	info->exp = exp_val;
209 	return 0;
210 }
211 
sensor_g_gain(struct v4l2_subdev * sd,__s32 * value)212 static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value)
213 {
214 	struct sensor_info *info = to_state(sd);
215 	*value = info->gain;
216 	sensor_print("sensor_get_gain = %d\n", info->gain);
217 	return 0;
218 }
219 
220 
sensor_s_gain(struct v4l2_subdev * sd,unsigned int gain_val)221 static int sensor_s_gain(struct v4l2_subdev *sd, unsigned int gain_val)
222 {
223 	struct sensor_info *info = to_state(sd);
224 	data_type gainlow = 0;
225 	data_type gainhigh = 0;
226 
227 	gain_val = gain_val * 2;
228 	gainlow = (unsigned char)(gain_val & 0xff);
229 	gainhigh = (unsigned char)((gain_val >> 8) & 0xff);
230 
231 	sensor_write(sd, 0x0104, 0x01);
232 	sensor_write(sd, 0x0205, gainlow);
233 	sensor_write(sd, 0x0204, gainhigh);
234 	sensor_write(sd, 0x0104, 0x00);
235 
236 	sensor_dbg("sensor_s_gain info->gain %d\n", gain_val);
237 	info->gain = gain_val;
238 
239 	return 0;
240 }
241 
242 static int s5k5e9_sensor_vts;
sensor_s_exp_gain(struct v4l2_subdev * sd,struct sensor_exp_gain * exp_gain)243 static int sensor_s_exp_gain(struct v4l2_subdev *sd,
244 			     struct sensor_exp_gain *exp_gain)
245 {
246 	int exp_val, gain_val, shutter;
247 	unsigned char explow, exphigh;
248 	unsigned char gainlow = 0;
249 	unsigned char gainhigh = 0;
250 	struct sensor_info *info = to_state(sd);
251 
252 	exp_val = exp_gain->exp_val;
253 	gain_val = exp_gain->gain_val;
254 
255 	exp_val = exp_val >> 4; /* rounding to 1 */
256 	shutter = exp_val;
257 	gain_val = gain_val * 2; /* shift to 1/32 step */
258 
259 	exphigh = (unsigned char)((0xff00 & exp_val) >> 8);
260 	explow  = (unsigned char)((0x00ff & exp_val));
261 	gainlow = (unsigned char)(gain_val & 0xff);
262 	gainhigh = (unsigned char)((gain_val >> 8) & 0xff);
263 
264 	sensor_write(sd, 0x0104, 0x01);
265 	sensor_write(sd, 0x0203, explow);
266 	sensor_write(sd, 0x0202, exphigh);
267 	sensor_write(sd, 0x0205, gainlow);
268 	sensor_write(sd, 0x0204, gainhigh);
269 
270 	sensor_write(sd, 0x0104, 0x00);
271 
272 	sensor_dbg("sensor_s_exp_gain info->exp %d info->gain %d\n",
273 									exp_val, gain_val);
274 
275 	info->exp = exp_val;
276 	info->gain = gain_val;
277 	return 0;
278 }
279 
sensor_s_sw_stby(struct v4l2_subdev * sd,int on_off)280 static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off)
281 {
282 	int ret;
283 	data_type rdval;
284 
285 	ret = sensor_read(sd, 0x0100, &rdval);
286 
287 	if (ret != 0)
288 		return ret;
289 
290 	if (on_off == STBY_ON)
291 		ret = sensor_write(sd, 0x0100, rdval & 0xfe);
292 	else
293 		ret = sensor_write(sd, 0x0100, rdval | 0x01);
294 
295 	return ret;
296 }
297 
298 /*
299  * Stuff that knows about the sensor.
300  */
sensor_power(struct v4l2_subdev * sd,int on)301 static int sensor_power(struct v4l2_subdev *sd, int on)
302 {
303 	int ret;
304 
305 	ret = 0;
306 	switch (on) {
307 	case STBY_ON:
308 		ret = sensor_s_sw_stby(sd, 1);
309 		if (ret < 0)
310 			sensor_err("soft stby falied!\n");
311 		usleep_range(10000, 12000);
312 
313 		cci_lock(sd);
314 		/* standby on io */
315 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
316 		cci_unlock(sd);
317 		/* inactive mclk after stadby in */
318 		vin_set_mclk(sd, OFF);
319 		break;
320 	case STBY_OFF:
321 		cci_lock(sd);
322 
323 		vin_set_mclk_freq(sd, MCLK);
324 		vin_set_mclk(sd, ON);
325 		usleep_range(10000, 12000);
326 
327 		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
328 		usleep_range(10000, 12000);
329 
330 		cci_unlock(sd);
331 		ret = sensor_s_sw_stby(sd, 0);
332 		if (ret < 0)
333 			sensor_err("soft stby off falied!\n");
334 		usleep_range(10000, 12000);
335 
336 		break;
337 	case PWR_ON:
338 		sensor_print("PWR_ON!\n");
339 
340 		cci_lock(sd);
341 		vin_set_pmu_channel(sd, CAMERAVDD, ON);
342 
343 		vin_gpio_set_status(sd, PWDN, 1);
344 		vin_gpio_set_status(sd, RESET, 1);
345 
346 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
347 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
348 		usleep_range(1000, 1200);
349 
350 		vin_set_mclk(sd, ON);
351 		usleep_range(100, 120);
352 		vin_set_mclk_freq(sd, MCLK);
353 		usleep_range(10000, 12000);
354 
355 		vin_set_pmu_channel(sd, IOVDD, ON);
356 		vin_set_pmu_channel(sd, AVDD, ON);
357 		vin_set_pmu_channel(sd, DVDD, ON);
358 		vin_set_pmu_channel(sd, AFVDD, ON);
359 		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
360 		usleep_range(10000, 12000);
361 
362 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
363 		usleep_range(30000, 31000);
364 		cci_unlock(sd);
365 		break;
366 	case PWR_OFF:
367 		sensor_print("PWR_OFF!\n");
368 		cci_lock(sd);
369 
370 		vin_set_mclk(sd, OFF);
371 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
372 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
373 
374 		vin_set_pmu_channel(sd, DVDD, OFF);
375 		vin_set_pmu_channel(sd, AVDD, OFF);
376 		vin_set_pmu_channel(sd, IOVDD, OFF);
377 		vin_set_pmu_channel(sd, AFVDD, OFF);
378 		vin_set_pmu_channel(sd, CAMERAVDD, OFF);
379 
380 		vin_gpio_set_status(sd, RESET, 0);
381 		vin_gpio_set_status(sd, PWDN, 0);
382 
383 		cci_unlock(sd);
384 		break;
385 	default:
386 		return -EINVAL;
387 	}
388 
389 	return 0;
390 }
391 
sensor_reset(struct v4l2_subdev * sd,u32 val)392 static int sensor_reset(struct v4l2_subdev *sd, u32 val)
393 {
394 	switch (val) {
395 	case 0:
396 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
397 		usleep_range(10000, 12000);
398 		break;
399 	case 1:
400 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
401 		usleep_range(10000, 12000);
402 		break;
403 	default:
404 		return -EINVAL;
405 	}
406 	return 0;
407 }
408 
sensor_detect(struct v4l2_subdev * sd)409 static int sensor_detect(struct v4l2_subdev *sd)
410 {
411 	data_type rdval = 0;
412 
413 	sensor_read(sd, 0x0000, &rdval);
414 	if (rdval != 0x55)
415 		return -ENODEV;
416 
417 	sensor_read(sd, 0x0001, &rdval);
418 	if (rdval != 0x9b)
419 		return -ENODEV;
420 
421 	return 0;
422 }
423 
sensor_init(struct v4l2_subdev * sd,u32 val)424 static int sensor_init(struct v4l2_subdev *sd, u32 val)
425 {
426 	int ret;
427 	struct sensor_info *info = to_state(sd);
428 
429 	sensor_dbg("sensor_init\n");
430 
431 	/*Make sure it is a target sensor */
432 	ret = sensor_detect(sd);
433 	if (ret) {
434 		sensor_err("chip found is not an target chip.\n");
435 		return ret;
436 	}
437 
438 	info->focus_status = 0;
439 	info->low_speed = 0;
440 	info->width = HD720_WIDTH;
441 	info->height = HD720_HEIGHT;
442 	info->hflip = 0;
443 	info->vflip = 0;
444 	info->exp = 0;
445 	info->gain = 0;
446 
447 	info->tpf.numerator = 1;
448 	info->tpf.denominator = SENSOR_FRAME_RATE; /* 15fps */
449 	info->preview_first_flag = 1;
450 
451 	return 0;
452 }
453 
sensor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)454 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
455 {
456 	int ret = 0;
457 	struct sensor_info *info = to_state(sd);
458 
459 	switch (cmd) {
460 	case GET_CURRENT_WIN_CFG:
461 		if (info->current_wins != NULL) {
462 			memcpy(arg, info->current_wins,
463 			       sizeof(struct sensor_win_size));
464 			ret = 0;
465 		} else {
466 			sensor_err("empty wins!\n");
467 			ret = -1;
468 		}
469 		break;
470 	case SET_FPS:
471 		ret = 0;
472 		break;
473 	case VIDIOC_VIN_SENSOR_EXP_GAIN:
474 		ret = sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg);
475 		break;
476 	case VIDIOC_VIN_SENSOR_CFG_REQ:
477 		sensor_cfg_req(sd, (struct sensor_config *)arg);
478 		break;
479 	default:
480 		return -EINVAL;
481 	}
482 	return ret;
483 }
484 
485 /*
486  * Store information about the video data format.
487  */
488 static struct sensor_format_struct sensor_formats[] = {
489 	{
490 		.desc = "Raw RGB Bayer",
491 		.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
492 		.regs = sensor_fmt_raw,
493 		.regs_size = ARRAY_SIZE(sensor_fmt_raw),
494 		.bpp = 1
495 	},
496 };
497 #define N_FMTS ARRAY_SIZE(sensor_formats)
498 
499 /*
500  * Then there is the issue of window sizes.  Try to capture the info here.
501  */
502 
503 static struct sensor_win_size sensor_win_sizes[] = {
504 	{
505 		.width      = HD720_WIDTH,
506 		.height     = HD720_HEIGHT,
507 		.hoffset    = 0,
508 		.voffset    = 0,
509 		.hts        = 3112,
510 		.vts        = 4068,
511 		.pclk       = 190 * 1000 * 1000,
512 		.mipi_bps   = 70 * 1000 * 1000,
513 		.fps_fixed  = 15,
514 		.bin_factor = 1,
515 		.intg_min   = 3 << 4,
516 		.intg_max   = (4068-10) << 4,
517 		.gain_min   = 1 << 4,
518 		.gain_max   = 16 << 4,
519 		.regs       = sensor_720p_regs,
520 		.regs_size  = ARRAY_SIZE(sensor_720p_regs),
521 		.set_size   = NULL,
522 	},
523 };
524 
525 #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))
526 
sensor_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)527 static int sensor_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
528 				struct v4l2_mbus_config *cfg)
529 {
530 	struct sensor_info *info = to_state(sd);
531 
532 	cfg->type = V4L2_MBUS_CSI2;
533 	cfg->flags = 0 | V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0;
534 
535 	return 0;
536 }
537 
sensor_g_ctrl(struct v4l2_ctrl * ctrl)538 static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
539 {
540 	struct sensor_info *info =
541 		container_of(ctrl->handler, struct sensor_info, handler);
542 	struct v4l2_subdev *sd = &info->sd;
543 
544 	switch (ctrl->id) {
545 	case V4L2_CID_GAIN:
546 		return sensor_g_gain(sd, &ctrl->val);
547 	case V4L2_CID_EXPOSURE:
548 		return sensor_g_exp(sd, &ctrl->val);
549 	}
550 	return -EINVAL;
551 }
552 
sensor_s_ctrl(struct v4l2_ctrl * ctrl)553 static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
554 {
555 	struct sensor_info *info =
556 		container_of(ctrl->handler, struct sensor_info, handler);
557 	struct v4l2_subdev *sd = &info->sd;
558 
559 	switch (ctrl->id) {
560 	case V4L2_CID_GAIN:
561 		return sensor_s_gain(sd, ctrl->val);
562 	case V4L2_CID_EXPOSURE:
563 		return sensor_s_exp(sd, ctrl->val);
564 	}
565 	return -EINVAL;
566 }
567 
sensor_reg_init(struct sensor_info * info)568 static int sensor_reg_init(struct sensor_info *info)
569 {
570 	int ret;
571 	struct v4l2_subdev *sd = &info->sd;
572 	struct sensor_format_struct *sensor_fmt = info->fmt;
573 	struct sensor_win_size *wsize = info->current_wins;
574 
575 	ret = sensor_write_array(sd, sensor_default_regs,
576 				 ARRAY_SIZE(sensor_default_regs));
577 	if (ret < 0) {
578 		sensor_err("write sensor_default_regs error\n");
579 		return ret;
580 	}
581 
582 	sensor_dbg("sensor_reg_init\n");
583 
584 	sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);
585 
586 	if (wsize->regs)
587 		sensor_write_array(sd, wsize->regs, wsize->regs_size);
588 
589 	if (wsize->set_size)
590 		wsize->set_size(sd);
591 
592 	info->width = wsize->width;
593 	info->height = wsize->height;
594 	s5k5e9_sensor_vts = wsize->vts;
595 	info->exp = 0;
596 	info->gain = 0;
597 
598 	sensor_print("s_fmt set width = %d, height = %d\n", wsize->width,
599 	       wsize->height);
600 
601 	return 0;
602 }
603 
sensor_s_stream(struct v4l2_subdev * sd,int enable)604 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
605 {
606 	struct sensor_info *info = to_state(sd);
607 
608 	sensor_print("%s on = %d, %d*%d fps: %d code: %x\n", __func__, enable,
609 	       info->current_wins->width, info->current_wins->height,
610 	       info->current_wins->fps_fixed, info->fmt->mbus_code);
611 
612 	if (!enable)
613 		return 0;
614 
615 	return sensor_reg_init(info);
616 }
617 
618 /* ----------------------------------------------------------------------- */
619 
620 static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
621 	.g_volatile_ctrl = sensor_g_ctrl,
622 	.s_ctrl = sensor_s_ctrl,
623 };
624 
625 static const struct v4l2_subdev_core_ops sensor_core_ops = {
626 	.reset = sensor_reset,
627 	.init = sensor_init,
628 	.s_power = sensor_power,
629 	.ioctl = sensor_ioctl,
630 #ifdef CONFIG_COMPAT
631 	.compat_ioctl32 = sensor_compat_ioctl32,
632 #endif
633 };
634 
635 static const struct v4l2_subdev_video_ops sensor_video_ops = {
636 	.s_parm = sensor_s_parm,
637 	.g_parm = sensor_g_parm,
638 	.s_stream = sensor_s_stream,
639 };
640 
641 static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
642 	.enum_mbus_code = sensor_enum_mbus_code,
643 	.enum_frame_size = sensor_enum_frame_size,
644 	.get_fmt = sensor_get_fmt,
645 	.set_fmt = sensor_set_fmt,
646 	.get_mbus_config = sensor_g_mbus_config,
647 };
648 
649 static const struct v4l2_subdev_ops sensor_ops = {
650 	.core = &sensor_core_ops,
651 	.video = &sensor_video_ops,
652 	.pad = &sensor_pad_ops,
653 };
654 
655 /* ----------------------------------------------------------------------- */
656 static struct cci_driver cci_drv = {
657 	.name = SENSOR_NAME,
658 	.addr_width = CCI_BITS_16,
659 	.data_width = CCI_BITS_8,
660 };
661 
662 static const struct v4l2_ctrl_config sensor_custom_ctrls[] = {
663 	{
664 		.ops = &sensor_ctrl_ops,
665 		.id = V4L2_CID_FRAME_RATE,
666 		.name = "frame rate",
667 		.type = V4L2_CTRL_TYPE_INTEGER,
668 		.min = 15,
669 		.max = 120,
670 		.step = 1,
671 		.def = 120,
672 	},
673 };
674 
sensor_init_controls(struct v4l2_subdev * sd,const struct v4l2_ctrl_ops * ops)675 static int sensor_init_controls(struct v4l2_subdev *sd, const struct v4l2_ctrl_ops *ops)
676 {
677 	struct sensor_info *info = to_state(sd);
678 	struct v4l2_ctrl_handler *handler = &info->handler;
679 	struct v4l2_ctrl *ctrl;
680 	int ret = 0;
681 
682 	v4l2_ctrl_handler_init(handler, 2);
683 
684 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 32, 16 * 32, 1, 32);
685 	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 3 * 16, 65536 * 16, 1, 3 * 16);
686 	if (ctrl != NULL)
687 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
688 
689 	if (handler->error) {
690 		ret = handler->error;
691 		v4l2_ctrl_handler_free(handler);
692 	}
693 
694 	sd->ctrl_handler = handler;
695 
696 	return ret;
697 }
698 
sensor_probe(struct i2c_client * client,const struct i2c_device_id * id)699 static int sensor_probe(struct i2c_client *client,
700 			const struct i2c_device_id *id)
701 {
702 	struct v4l2_subdev *sd;
703 	struct sensor_info *info;
704 
705 	info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL);
706 	if (info == NULL)
707 		return -ENOMEM;
708 	sd = &info->sd;
709 
710 	cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv);
711 	sensor_init_controls(sd, &sensor_ctrl_ops);
712 
713 	mutex_init(&info->lock);
714 #ifdef CONFIG_SAME_I2C
715 	info->sensor_i2c_addr = I2C_ADDR >> 1;
716 #endif
717 	info->fmt = &sensor_formats[0];
718 	info->fmt_pt = &sensor_formats[0];
719 	info->win_pt = &sensor_win_sizes[0];
720 	info->fmt_num = N_FMTS;
721 	info->win_size_num = N_WIN_SIZES;
722 	info->sensor_field = V4L2_FIELD_NONE;
723 	info->stream_seq = MIPI_BEFORE_SENSOR;
724 	info->af_first_flag = 1;
725 	info->exp = 0;
726 	info->gain = 0;
727 
728 	return 0;
729 }
730 
sensor_remove(struct i2c_client * client)731 static int sensor_remove(struct i2c_client *client)
732 {
733 	struct v4l2_subdev *sd;
734 
735 	sd = cci_dev_remove_helper(client, &cci_drv);
736 
737 	kfree(to_state(sd));
738 	return 0;
739 }
740 
741 static const struct i2c_device_id sensor_id[] = {
742 	{SENSOR_NAME, 0},
743 	{}
744 };
745 
746 MODULE_DEVICE_TABLE(i2c, sensor_id);
747 
748 static struct i2c_driver sensor_driver = {
749 	.driver = {
750 		.owner = THIS_MODULE,
751 		.name = SENSOR_NAME,
752 	},
753 	.probe = sensor_probe,
754 	.remove = sensor_remove,
755 	.id_table = sensor_id,
756 };
init_sensor(void)757 static __init int init_sensor(void)
758 {
759 
760 	return cci_dev_init_helper(&sensor_driver);
761 }
762 
exit_sensor(void)763 static __exit void exit_sensor(void)
764 {
765 
766 	cci_dev_exit_helper(&sensor_driver);
767 }
768 
769 module_init(init_sensor);
770 module_exit(exit_sensor);
771