1
2 /*
3 * A V4L2 driver for TP9950 YUV cameras.
4 *
5 * Copyright (c) 2019 by Allwinnertech Co., Ltd. http://www.allwinnertech.com
6 *
7 * Authors: Zheng Zequn <zequnzheng@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("zw");
29 MODULE_DESCRIPTION("A low-level driver for TP9950 sensors");
30 MODULE_LICENSE("GPL");
31
32 #define MCLK (27*1000*1000)
33 #define CLK_POL V4L2_MBUS_PCLK_SAMPLE_FALLING
34 #define V4L2_IDENT_SENSOR 0x5028
35
36
37 /* enable tp9950 sensor detect */
38 #define SENSOR_DETECT_KTHREAD 1
39 /* USE DETECT BY GPIO_IRQ OR POLLING
40 * DET_USE_POLLING 0 meant by gpio_irq
41 * DET_USE_POLLING 1 meant by POLLING
42 * */
43 #define DET_USE_POLLING 1
44
45
46 #if SENSOR_DETECT_KTHREAD
47 struct sensor_indetect {
48 struct class *sensor_class;
49 struct task_struct *sensor_task;
50 struct device *dev;
51 struct cdev *cdev;
52 struct gpio_config detect_power;
53 struct gpio_config detect_gpio;
54 struct delayed_work tp9950_work;
55 #if DET_USE_POLLING
56 data_type last_status;
57 #else
58 unsigned int detect_irq;
59 #endif
60 dev_t devno;
61 } sensor_indetect;
62 static DEFINE_MUTEX(det_mutex);
63 #endif
64
65 /*
66 * Our nominal (default) frame rate.
67 */
68 #define SENSOR_FRAME_RATE 30
69
70 /*
71 * The TP9920 sits on i2c with ID 0x88 or 0x8a
72 * SAD-low:0x88 SAD-high:0x8a
73 */
74 #define I2C_ADDR 0x88
75 #define SENSOR_NAME "tp9950"
76
77 static struct regval_list reg_1080p30_1ch[] = {
78 {0x02, 0xCC},
79 {0x05, 0x00},
80 {0x06, 0x32},
81 {0x07, 0xC0},
82 {0x08, 0x00},
83 {0x09, 0x24},
84 {0x0A, 0x48},
85 {0x0B, 0xC0},
86 {0x0C, 0x03},
87 {0x0D, 0x72},
88 {0x0E, 0x00},
89 {0x0F, 0x00},
90 {0x10, 0x00},
91 {0x11, 0x40},
92 {0x12, 0x60},
93 {0x13, 0x00},
94 {0x14, 0x00},
95 {0x15, 0x01},
96 {0x16, 0xF0},
97 {0x17, 0x80},
98 {0x18, 0x29},
99 {0x19, 0x38},
100 {0x1A, 0x47},
101 {0x1B, 0x01},
102 {0x1C, 0x08},
103 {0x1D, 0x98},
104 {0x1E, 0x80},
105 {0x1F, 0x80},
106 {0x20, 0x38},
107 {0x21, 0x46},
108 {0x22, 0x36},
109 {0x23, 0x3C},
110 {0x24, 0x04},
111 {0x25, 0xFE},
112 {0x26, 0x0D},
113 {0x27, 0x2D},
114 {0x28, 0x00},
115 {0x29, 0x48},
116 {0x2A, 0x30},
117 {0x2B, 0x60},
118 {0x2C, 0x3A},
119 {0x2D, 0x54},
120 {0x2E, 0x40},
121 {0x2F, 0x00},
122 {0x30, 0xA5},
123 {0x31, 0x95},
124 {0x32, 0xE0},
125 {0x33, 0x60},
126 {0x34, 0x00},
127 {0x35, 0x05},
128 {0x36, 0xDC},
129 {0x37, 0x00},
130 {0x38, 0x00},
131 {0x39, 0x1C},
132 {0x3A, 0x32},
133 {0x3B, 0x26},
134 {0x3C, 0x00},
135 {0x3D, 0x60},
136 {0x3E, 0x00},
137 {0x3F, 0x00},
138 {0x40, 0x00},
139 {0x41, 0x00},
140 {0x42, 0x00},
141 {0x43, 0x00},
142 {0x44, 0x00},
143 {0x45, 0x00},
144 {0x46, 0x00},
145 {0x47, 0x00},
146 {0x48, 0x00},
147 {0x49, 0x00},
148 {0x4A, 0x00},
149 {0x4B, 0x00},
150 {0x4C, 0x43},
151 {0x4D, 0x00},
152 {0x4E, 0x17},
153 {0x4F, 0x00},
154 {0x50, 0x00},
155 {0x51, 0x00},
156 {0x52, 0x00},
157 {0x53, 0x00},
158 {0x54, 0x00},
159
160 {0xB3, 0xFA},
161 {0xB4, 0x00},
162 {0xB5, 0x00},
163 {0xB6, 0x00},
164 {0xB7, 0x00},
165 {0xB8, 0x00},
166 {0xB9, 0x00},
167 {0xBA, 0x00},
168 {0xBB, 0x00},
169 {0xBC, 0x00},
170 {0xBD, 0x00},
171 {0xBE, 0x00},
172 {0xBF, 0x00},
173 {0xC0, 0x00},
174 {0xC1, 0x00},
175 {0xC2, 0x0B},
176 {0xC3, 0x0C},
177 {0xC4, 0x00},
178 {0xC5, 0x00},
179 {0xC6, 0x1F},
180 {0xC7, 0x78},
181 {0xC8, 0x27},
182 {0xC9, 0x00},
183 {0xCA, 0x00},
184 {0xCB, 0x07},
185 {0xCC, 0x08},
186 {0xCD, 0x00},
187 {0xCE, 0x00},
188 {0xCF, 0x04},
189 {0xD0, 0x00},
190 {0xD1, 0x00},
191 {0xD2, 0x60},
192 {0xD3, 0x10},
193 {0xD4, 0x06},
194 {0xD5, 0xBE},
195 {0xD6, 0x39},
196 {0xD7, 0x27},
197 {0xD8, 0x00},
198 {0xD9, 0x00},
199 {0xDA, 0x00},
200 {0xDB, 0x00},
201 {0xDC, 0x00},
202 {0xDD, 0x00},
203 {0xDE, 0x00},
204 {0xDF, 0x00},
205 {0xE0, 0x00},
206 {0xE1, 0x00},
207 {0xE2, 0x00},
208 {0xE3, 0x00},
209 {0xE4, 0x00},
210 {0xE5, 0x00},
211 {0xE6, 0x00},
212 {0xE7, 0x13},
213 {0xE8, 0x03},
214 {0xE9, 0x00},
215 {0xEA, 0x00},
216 {0xEB, 0x00},
217 {0xEC, 0x00},
218 {0xED, 0x00},
219 {0xEE, 0x00},
220 {0xEF, 0x00},
221 {0xF0, 0x00},
222 {0xF1, 0x00},
223 {0xF2, 0x00},
224 {0xF3, 0x00},
225 {0xF4, 0x20},
226 {0xF5, 0x10},
227 {0xF6, 0x00},
228 {0xF7, 0x00},
229 {0xF8, 0x00},
230 {0xF9, 0x00},
231 {0xFA, 0x88},
232 {0xFB, 0x00},
233 {0xFC, 0x00},
234
235 {0x40, 0x08},
236 {0x00, 0x00},
237 {0x01, 0xf8},
238 {0x02, 0x01},
239 {0x08, 0xF0},
240 {0x13, 0x04},
241 {0x14, 0x73},
242 {0x15, 0x08},
243 {0x20, 0x12},
244 {0x34, 0x1b},
245 {0x23, 0x02},
246 {0x23, 0x00},
247
248 {0x40, 0x00},
249 };
250
251 static struct regval_list reg_1080p25_1ch[] = {
252 {0x02, 0xCC},
253 {0x05, 0x00},
254 {0x06, 0x32},
255 {0x07, 0xC0},
256 {0x08, 0x00},
257 {0x09, 0x24},
258 {0x0A, 0x48},
259 {0x0B, 0xC0},
260 {0x0C, 0x03},
261 {0x0D, 0x73},
262 {0x0E, 0x00},
263 {0x0F, 0x00},
264 {0x10, 0x00},
265 {0x11, 0x40},
266 {0x12, 0x60},
267 {0x13, 0x00},
268 {0x14, 0x00},
269 {0x15, 0x01},
270 {0x16, 0xF0},
271 {0x17, 0x80},
272 {0x18, 0x29},
273 {0x19, 0x38},
274 {0x1A, 0x47},
275 {0x1B, 0x01},
276 {0x1C, 0x0A},
277 {0x1D, 0x50},
278 {0x1E, 0x80},
279 {0x1F, 0x80},
280 {0x20, 0x3C},
281 {0x21, 0x46},
282 {0x22, 0x36},
283 {0x23, 0x3C},
284 {0x24, 0x04},
285 {0x25, 0xFE},
286 {0x26, 0x0D},
287 {0x27, 0x2D},
288 {0x28, 0x00},
289 {0x29, 0x48},
290 {0x2A, 0x30},
291 {0x2B, 0x60},
292 {0x2C, 0x1A},
293 {0x2D, 0x54},
294 {0x2E, 0x40},
295 {0x2F, 0x00},
296 {0x30, 0xA5},
297 {0x31, 0x86},
298 {0x32, 0xFB},
299 {0x33, 0x60},
300 {0x34, 0x00},
301 {0x35, 0x05},
302 {0x36, 0xDC},
303 {0x37, 0x00},
304 {0x38, 0x00},
305 {0x39, 0x1C},
306 {0x3A, 0x32},
307 {0x3B, 0x26},
308 {0x3C, 0x00},
309 {0x3D, 0x60},
310 {0x3E, 0x00},
311 {0x3F, 0x00},
312 {0x40, 0x00},
313 {0x41, 0x00},
314 {0x42, 0x00},
315 {0x43, 0x00},
316 {0x44, 0x00},
317 {0x45, 0x00},
318 {0x46, 0x00},
319 {0x47, 0x00},
320 {0x48, 0x00},
321 {0x49, 0x00},
322 {0x4A, 0x00},
323 {0x4B, 0x00},
324 {0x4C, 0x43},
325 {0x4D, 0x00},
326 {0x4E, 0x17},
327 {0x4F, 0x00},
328 {0x50, 0x00},
329 {0x51, 0x00},
330 {0x52, 0x00},
331 {0x53, 0x00},
332 {0x54, 0x00},
333
334 {0xB3, 0xFA},
335 {0xB4, 0x00},
336 {0xB5, 0x00},
337 {0xB6, 0x00},
338 {0xB7, 0x00},
339 {0xB8, 0x00},
340 {0xB9, 0x00},
341 {0xBA, 0x00},
342 {0xBB, 0x00},
343 {0xBC, 0x00},
344 {0xBD, 0x00},
345 {0xBE, 0x00},
346 {0xBF, 0x00},
347 {0xC0, 0x00},
348 {0xC1, 0x00},
349 {0xC2, 0x0B},
350 {0xC3, 0x0C},
351 {0xC4, 0x00},
352 {0xC5, 0x00},
353 {0xC6, 0x1F},
354 {0xC7, 0x78},
355 {0xC8, 0x27},
356 {0xC9, 0x00},
357 {0xCA, 0x00},
358 {0xCB, 0x07},
359 {0xCC, 0x08},
360 {0xCD, 0x00},
361 {0xCE, 0x00},
362 {0xCF, 0x04},
363 {0xD0, 0x00},
364 {0xD1, 0x00},
365 {0xD2, 0x60},
366 {0xD3, 0x10},
367 {0xD4, 0x06},
368 {0xD5, 0xBE},
369 {0xD6, 0x39},
370 {0xD7, 0x27},
371 {0xD8, 0x00},
372 {0xD9, 0x00},
373 {0xDA, 0x00},
374 {0xDB, 0x00},
375 {0xDC, 0x00},
376 {0xDD, 0x00},
377 {0xDE, 0x00},
378 {0xDF, 0x00},
379 {0xE0, 0x00},
380 {0xE1, 0x00},
381 {0xE2, 0x00},
382 {0xE3, 0x00},
383 {0xE4, 0x00},
384 {0xE5, 0x00},
385 {0xE6, 0x00},
386 {0xE7, 0x13},
387 {0xE8, 0x03},
388 {0xE9, 0x00},
389 {0xEA, 0x00},
390 {0xEB, 0x00},
391 {0xEC, 0x00},
392 {0xED, 0x00},
393 {0xEE, 0x00},
394 {0xEF, 0x00},
395 {0xF0, 0x00},
396 {0xF1, 0x00},
397 {0xF2, 0x00},
398 {0xF3, 0x00},
399 {0xF4, 0x20},
400 {0xF5, 0x10},
401 {0xF6, 0x00},
402 {0xF7, 0x00},
403 {0xF8, 0x00},
404 {0xF9, 0x00},
405 {0xFA, 0x88},
406 {0xFB, 0x00},
407 {0xFC, 0x00},
408
409 {0x40, 0x08},
410 {0x00, 0x00},
411 {0x01, 0xf8},
412 {0x02, 0x01},
413 {0x08, 0xF0},
414 {0x13, 0x04},
415 {0x14, 0x73},
416 {0x15, 0x08},
417 {0x20, 0x12},
418 {0x34, 0x1b},
419 {0x23, 0x02},
420 {0x23, 0x00},
421
422 {0x40, 0x00},
423 };
424
sensor_s_sw_stby(struct v4l2_subdev * sd,int on_off)425 static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off)
426 {
427 if (on_off)
428 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
429 else
430 vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
431 return 0;
432 }
433
sensor_power(struct v4l2_subdev * sd,int on)434 static int sensor_power(struct v4l2_subdev *sd, int on)
435 {
436 switch (on) {
437 case STBY_ON:
438 sensor_dbg("CSI_SUBDEV_STBY_ON!\n");
439 sensor_s_sw_stby(sd, ON);
440 break;
441 case STBY_OFF:
442 sensor_dbg("CSI_SUBDEV_STBY_OFF!\n");
443 sensor_s_sw_stby(sd, OFF);
444 break;
445 case PWR_ON:
446 sensor_dbg("CSI_SUBDEV_PWR_ON!\n");
447 cci_lock(sd);
448 vin_gpio_set_status(sd, PWDN, 1);
449 vin_gpio_set_status(sd, RESET, 1);
450 vin_gpio_set_status(sd, SM_HS, 1);
451 vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
452 vin_gpio_write(sd, SM_HS, CSI_GPIO_HIGH);
453 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
454 usleep_range(1000, 1200);
455 vin_set_mclk_freq(sd, MCLK);
456 vin_set_mclk(sd, ON);
457 vin_set_pmu_channel(sd, IOVDD, ON);
458 vin_set_pmu_channel(sd, AVDD, ON);
459 vin_set_pmu_channel(sd, DVDD, ON);
460 usleep_range(10000, 12000);
461 vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
462 usleep_range(30000, 31000);
463 cci_unlock(sd);
464 break;
465 case PWR_OFF:
466 sensor_dbg("CSI_SUBDEV_PWR_OFF!\n");
467 cci_lock(sd);
468 vin_set_mclk(sd, OFF);
469 vin_set_pmu_channel(sd, DVDD, OFF);
470 vin_set_pmu_channel(sd, AVDD, OFF);
471 vin_set_pmu_channel(sd, IOVDD, OFF);
472 usleep_range(100, 120);
473 vin_gpio_write(sd, SM_HS, CSI_GPIO_LOW);
474 vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
475 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
476 vin_gpio_set_status(sd, SM_HS, 0);
477 vin_gpio_set_status(sd, RESET, 0);
478 vin_gpio_set_status(sd, PWDN, 0);
479 cci_unlock(sd);
480 break;
481 default:
482 return -EINVAL;
483 }
484
485 return 0;
486 }
487
sensor_reset(struct v4l2_subdev * sd,u32 val)488 static int sensor_reset(struct v4l2_subdev *sd, u32 val)
489 {
490 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
491 usleep_range(5000, 6000);
492 vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
493 usleep_range(5000, 6000);
494 return 0;
495 }
496
sensor_detect(struct v4l2_subdev * sd)497 static int sensor_detect(struct v4l2_subdev *sd)
498 {
499 data_type rdval, rdval1, rdval2;
500 int cnt = 0;
501
502 rdval = 0;
503 rdval1 = 0;
504 rdval2 = 0;
505
506 sensor_read(sd, 0xfe, &rdval1);
507 sensor_read(sd, 0xff, &rdval2);
508 rdval = ((rdval2<<8) & 0xff00) | rdval1;
509 sensor_print("V4L2_IDENT_SENSOR = 0x%x\n", rdval);
510
511 while ((rdval != V4L2_IDENT_SENSOR) && (cnt < 5)) {
512 sensor_read(sd, 0xfe, &rdval1);
513 sensor_read(sd, 0xff, &rdval2);
514 rdval = ((rdval2<<8) & 0xff00) | rdval1;
515 sensor_print("retry = %d, V4L2_IDENT_SENSOR = %x\n", cnt,
516 rdval);
517 cnt++;
518 }
519
520 if (rdval != V4L2_IDENT_SENSOR)
521 return -ENODEV;
522
523 return 0;
524 }
525
sensor_init(struct v4l2_subdev * sd,u32 val)526 static int sensor_init(struct v4l2_subdev *sd, u32 val)
527 {
528 int ret;
529 struct sensor_info *info = to_state(sd);
530
531 sensor_dbg("sensor_init\n");
532
533 /*Make sure it is a target sensor */
534 ret = sensor_detect(sd);
535 if (ret) {
536 sensor_err("chip found is not an target chip.\n");
537 return ret;
538 }
539
540 info->focus_status = 0;
541 info->low_speed = 0;
542 info->width = HD1080_WIDTH;
543 info->height = HD1080_HEIGHT;
544 info->hflip = 0;
545 info->vflip = 0;
546
547 info->tpf.numerator = 1;
548 info->tpf.denominator = 25; /* 25fps */
549
550 info->preview_first_flag = 1;
551 return 0;
552 }
553
sensor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)554 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
555 {
556 int ret = 0;
557 struct sensor_info *info = to_state(sd);
558
559 switch (cmd) {
560 case GET_CURRENT_WIN_CFG:
561 if (info->current_wins != NULL) {
562 memcpy(arg,
563 info->current_wins,
564 sizeof(struct sensor_win_size));
565 ret = 0;
566 } else {
567 sensor_err("empty wins!\n");
568 ret = -1;
569 }
570 break;
571 case SET_FPS:
572 break;
573 case VIDIOC_VIN_SENSOR_CFG_REQ:
574 sensor_cfg_req(sd, (struct sensor_config *)arg);
575 break;
576 default:
577 return -EINVAL;
578 }
579 return ret;
580 }
581
582 /*
583 * Store information about the video data format.
584 */
585 static struct sensor_format_struct sensor_formats[] = {
586 {
587 .desc = "BT656 1CH",
588 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
589 .regs = NULL,
590 .regs_size = 0,
591 .bpp = 1,
592 },
593 };
594 #define N_FMTS ARRAY_SIZE(sensor_formats)
595
596 /*
597 * Then there is the issue of window sizes. Try to capture the info here.
598 */
599
600 static struct sensor_win_size sensor_win_sizes[] = {
601 {
602 .width = HD1080_WIDTH,
603 .height = HD1080_HEIGHT,
604 .hoffset = 0,
605 .voffset = 0,
606 .fps_fixed = 30,
607 .regs = reg_1080p30_1ch,
608 .regs_size = ARRAY_SIZE(reg_1080p30_1ch),
609 .set_size = NULL,
610 },
611 {
612 .width = HD1080_WIDTH,
613 .height = HD1080_HEIGHT,
614 .hoffset = 0,
615 .voffset = 0,
616 .fps_fixed = 25,
617 .regs = reg_1080p25_1ch,
618 .regs_size = ARRAY_SIZE(reg_1080p25_1ch),
619 .set_size = NULL,
620 },
621 };
622 #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))
623
sensor_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)624 static int sensor_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
625 struct v4l2_mbus_config *cfg)
626 {
627 cfg->type = V4L2_MBUS_BT656;
628 cfg->flags = CLK_POL | CSI_CH_0;
629 return 0;
630 }
631
sensor_g_ctrl(struct v4l2_ctrl * ctrl)632 static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
633 {
634 return -EINVAL;
635 }
636
sensor_s_ctrl(struct v4l2_ctrl * ctrl)637 static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
638 {
639 return -EINVAL;
640 }
641
sensor_reg_init(struct sensor_info * info)642 static int sensor_reg_init(struct sensor_info *info)
643 {
644 struct v4l2_subdev *sd = &info->sd;
645 struct sensor_format_struct *sensor_fmt = info->fmt;
646 struct sensor_win_size *wsize = info->current_wins;
647
648 sensor_dbg("sensor_reg_init\n");
649
650 sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);
651
652 if (wsize->regs)
653 sensor_write_array(sd, wsize->regs, wsize->regs_size);
654
655 if (wsize->set_size)
656 wsize->set_size(sd);
657
658 info->fmt = sensor_fmt;
659 info->width = wsize->width;
660 info->height = wsize->height;
661
662 return 0;
663 }
664
665 #if SENSOR_DETECT_KTHREAD
__sensor_insert_detect(data_type * val)666 static int __sensor_insert_detect(data_type *val)
667 {
668 int ret;
669
670 mutex_lock(&det_mutex);
671 /*detecting the sensor insert by detect_gpio
672 * if detect_gpio was height meant sensor was insert
673 * if detect_gpio was low, meant sensor was remove
674 * */
675 ret = gpio_get_value_cansleep(sensor_indetect.detect_gpio.gpio);
676 if (ret) {
677 *val = 0x03;
678 } else {
679 *val = 0x04;
680 }
681
682 mutex_unlock(&det_mutex);
683
684 return 0;
685 }
686
sensor_msg_sent(char * buf)687 void sensor_msg_sent(char *buf)
688 {
689 char *envp[2];
690
691 envp[0] = buf;
692 envp[1] = NULL;
693 kobject_uevent_env(&sensor_indetect.dev->kobj, KOBJ_CHANGE, envp);
694 }
695
sensor_det_work(struct work_struct * work)696 static void sensor_det_work(struct work_struct *work)
697 {
698 char buf[32];
699 data_type val;
700
701 __sensor_insert_detect(&val);
702
703 #if DET_USE_POLLING
704 if (sensor_indetect.last_status != val) {
705 memset(buf, 0, 32);
706 snprintf(buf, sizeof(buf), "SENSOR_RAVAL=0x%x", val);
707
708 sensor_msg_sent(buf);
709 sensor_indetect.last_status = val;
710 vin_print("val = 0x%x, Sent msg to user\n", val);
711 }
712
713 schedule_delayed_work(&sensor_indetect.tp9950_work, (msecs_to_jiffies(1 * 1000)));
714 #else
715 memset(buf, 0, 32);
716 snprintf(buf, sizeof(buf), "SENSOR_RAVAL=0x%x", val);
717
718 sensor_msg_sent(buf);
719 vin_print("val = 0x%x, Sent msg to user\n", val);
720 #endif
721
722 }
723
724
725 #if !DET_USE_POLLING
sensor_det_irq_func(int irq,void * priv)726 static irqreturn_t sensor_det_irq_func(int irq, void *priv)
727 {
728 /* the work of detected was be run in workquen */
729 schedule_delayed_work(&sensor_indetect.tp9950_work, 0);
730 return IRQ_HANDLED;
731 }
732 #endif
733
734 #endif
735
sensor_s_stream(struct v4l2_subdev * sd,int enable)736 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
737 {
738 struct sensor_info *info = to_state(sd);
739
740 sensor_print("%s on = %d, %d*%d %x\n", __func__, enable,
741 info->current_wins->width,
742 info->current_wins->height, info->fmt->mbus_code);
743
744 if (!enable)
745 return 0;
746
747 return sensor_reg_init(info);
748 }
749
750 /* ----------------------------------------------------------------------- */
751 static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
752 .g_volatile_ctrl = sensor_g_ctrl,
753 .s_ctrl = sensor_s_ctrl,
754 };
755
756 static const struct v4l2_subdev_core_ops sensor_core_ops = {
757 .reset = sensor_reset,
758 .init = sensor_init,
759 .s_power = sensor_power,
760 .ioctl = sensor_ioctl,
761 };
762
763 static const struct v4l2_subdev_video_ops sensor_video_ops = {
764 .s_stream = sensor_s_stream,
765 };
766
767 static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
768 .enum_mbus_code = sensor_enum_mbus_code,
769 .enum_frame_size = sensor_enum_frame_size,
770 .get_fmt = sensor_get_fmt,
771 .set_fmt = sensor_set_fmt,
772 .get_mbus_config = sensor_g_mbus_config,
773 };
774
775 static const struct v4l2_subdev_ops sensor_ops = {
776 .core = &sensor_core_ops,
777 .video = &sensor_video_ops,
778 .pad = &sensor_pad_ops,
779 };
780
781
782 /* ----------------------------------------------------------------------- */
783 static struct cci_driver cci_drv = {
784 .name = SENSOR_NAME,
785 .addr_width = CCI_BITS_8,
786 .data_width = CCI_BITS_8,
787 };
788
sensor_init_controls(struct v4l2_subdev * sd,const struct v4l2_ctrl_ops * ops)789 static int sensor_init_controls(struct v4l2_subdev *sd,
790 const struct v4l2_ctrl_ops *ops)
791 {
792 struct sensor_info *info = to_state(sd);
793 struct v4l2_ctrl_handler *handler = &info->handler;
794 struct v4l2_ctrl *ctrl;
795 int ret = 0;
796
797 v4l2_ctrl_handler_init(handler, 2);
798
799 v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 1600,
800 256 * 1600, 1, 1 * 1600);
801 ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 0,
802 65536 * 16, 1, 0);
803 if (ctrl != NULL)
804 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
805
806 if (handler->error) {
807 ret = handler->error;
808 v4l2_ctrl_handler_free(handler);
809 }
810
811 sd->ctrl_handler = handler;
812
813 return ret;
814 }
815
816
817 #if SENSOR_DETECT_KTHREAD
sensor_dev_open(struct inode * inode,struct file * file)818 static int sensor_dev_open(struct inode *inode, struct file *file)
819 {
820 return 0;
821 }
822
sensor_dev_release(struct inode * inode,struct file * file)823 static int sensor_dev_release(struct inode *inode, struct file *file)
824 {
825 return 0;
826 }
827
sensor_dev_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)828 static ssize_t sensor_dev_read(struct file *file, char __user *buf,
829 size_t count, loff_t *ppos)
830 {
831 return -EINVAL;
832 }
833
sensor_dev_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)834 static ssize_t sensor_dev_write(struct file *file, const char __user *buf,
835 size_t count, loff_t *ppos)
836 {
837 return -EINVAL;
838 }
839
sensor_dev_mmap(struct file * file,struct vm_area_struct * vma)840 static int sensor_dev_mmap(struct file *file, struct vm_area_struct *vma)
841 {
842 return 0;
843 }
844
sensor_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)845 static long sensor_dev_ioctl(struct file *file, unsigned int cmd,
846 unsigned long arg)
847 {
848 return 0;
849 }
850
851 static const struct file_operations sensor_dev_fops = {
852 .owner = THIS_MODULE,
853 .open = sensor_dev_open,
854 .release = sensor_dev_release,
855 .write = sensor_dev_write,
856 .read = sensor_dev_read,
857 .unlocked_ioctl = sensor_dev_ioctl,
858 .mmap = sensor_dev_mmap,
859 };
860
get_det_status_show(struct device * dev,struct device_attribute * attr,char * buf)861 static ssize_t get_det_status_show(struct device *dev,
862 struct device_attribute *attr, char *buf)
863 {
864 data_type val;
865 __sensor_insert_detect(&val);
866 return sprintf(buf, "0x%x\n", val);
867 }
868
869 static struct device_attribute detect_dev_attrs = {
870 .attr = {
871 .name = "online",
872 .mode = S_IRUGO,
873 },
874 .show = get_det_status_show,
875 .store = NULL,
876
877 };
878
tp9950_sensor_det_init(struct i2c_client * client)879 static int tp9950_sensor_det_init(struct i2c_client *client)
880 {
881 int ret;
882 struct device_node *np = NULL;
883
884 /* enable detect work queue */
885 INIT_DELAYED_WORK(&sensor_indetect.tp9950_work, sensor_det_work);
886 np = of_find_node_by_name(NULL, "tp9950_detect");
887 if (np == NULL) {
888 sensor_err("can not find the tp9950_detect node, will not \
889 enable detect kthread\n");
890 return -1;
891 }
892
893 sensor_indetect.detect_power.gpio = of_get_named_gpio_flags(
894 np, "gpio_power", 0,
895 (enum of_gpio_flags *)(&(sensor_indetect.detect_power)));
896 sensor_indetect.detect_gpio.gpio = of_get_named_gpio_flags(
897 np, "gpio_detect", 0,
898 (enum of_gpio_flags *)(&(sensor_indetect.detect_gpio)));
899
900 if ((!gpio_is_valid(sensor_indetect.detect_power.gpio) ||
901 sensor_indetect.detect_power.gpio < 0) &&
902 (!gpio_is_valid(sensor_indetect.detect_gpio.gpio) ||
903 sensor_indetect.detect_gpio.gpio < 0)) {
904 sensor_err("enable tp9950 sensor detect fail!!\n");
905 return -1;
906 } else {
907 /* enable the detect power*/
908 ret = gpio_request(sensor_indetect.detect_power.gpio, NULL);
909 if (ret < 0) {
910 sensor_err("enable tp9950 sensor detect fail!!\n");
911 return -1;
912 }
913 gpio_direction_output(sensor_indetect.detect_power.gpio, 1);
914
915 /* enable irq to detect */
916 ret = gpio_request(sensor_indetect.detect_gpio.gpio, NULL);
917 if (ret < 0) {
918 sensor_err("enable gpio_tp9950 fail! \n");
919 return -1;
920 }
921
922 gpio_direction_input(sensor_indetect.detect_gpio.gpio);
923
924 #if DET_USE_POLLING
925 }
926 /* start detect work */
927 schedule_delayed_work(&sensor_indetect.tp9950_work, 0);
928 #else
929 /* request gpio to irq */
930 sensor_indetect.detect_irq =
931 gpio_to_irq(sensor_indetect.detect_gpio.gpio);
932 ret = request_irq(
933 sensor_indetect.detect_irq, sensor_det_irq_func,
934 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
935 "tp9950_sensor_detect", client);
936 }
937 #endif
938 return 0;
939 }
940
tp9950_sensor_det_remove(void)941 static void tp9950_sensor_det_remove(void)
942 {
943 cancel_delayed_work_sync(&sensor_indetect.tp9950_work);
944
945 #if !DET_USE_POLLING
946 /*free irq*/
947 if (free_irq > 0) {
948 disable_irq(sensor_indetect.detect_irq);
949 free_irq(sensor_indetect.detect_irq, NULL);
950 }
951 #endif
952
953 if (!(sensor_indetect.detect_power.gpio < 0 ||
954 sensor_indetect.detect_gpio.gpio < 0)) {
955 gpio_free(sensor_indetect.detect_power.gpio);
956 gpio_free(sensor_indetect.detect_gpio.gpio);
957 }
958 }
959
960 #endif
961
sensor_probe(struct i2c_client * client,const struct i2c_device_id * id)962 static int sensor_probe(struct i2c_client *client,
963 const struct i2c_device_id *id)
964 {
965 struct v4l2_subdev *sd;
966 struct sensor_info *info;
967 int ret;
968
969 info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL);
970 if (info == NULL)
971 return -ENOMEM;
972 sd = &info->sd;
973 cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv);
974 sensor_init_controls(sd, &sensor_ctrl_ops);
975 mutex_init(&info->lock);
976 #ifdef CONFIG_SAME_I2C
977 info->sensor_i2c_addr = I2C_ADDR >> 1;
978 #endif
979 info->fmt = &sensor_formats[0];
980 info->fmt_pt = &sensor_formats[0];
981 info->win_pt = &sensor_win_sizes[0];
982 info->fmt_num = N_FMTS;
983 info->win_size_num = N_WIN_SIZES;
984 info->sensor_field = V4L2_FIELD_NONE;
985
986 #if SENSOR_DETECT_KTHREAD
987 alloc_chrdev_region(&sensor_indetect.devno, 0, 1, "csi");
988 sensor_indetect.cdev = cdev_alloc();
989 if (IS_ERR(sensor_indetect.cdev)) {
990 sensor_err("cdev_alloc fail!\n");
991 goto free_devno;
992 }
993
994 cdev_init(sensor_indetect.cdev, &sensor_dev_fops);
995 sensor_indetect.cdev->owner = THIS_MODULE;
996 ret = cdev_add(sensor_indetect.cdev, sensor_indetect.devno, 1);
997 if (ret) {
998 sensor_err("cdev_add fail.\n");
999 goto free_cdev;
1000 }
1001
1002 sensor_indetect.sensor_class = class_create(THIS_MODULE, "csi");
1003 if (IS_ERR(sensor_indetect.sensor_class)) {
1004 sensor_err("class_create fail!\n");
1005 goto unregister_cdev;
1006 }
1007
1008
1009
1010 sensor_indetect.dev =
1011 device_create(sensor_indetect.sensor_class, NULL,
1012 sensor_indetect.devno, NULL, "tp9950");
1013 if (IS_ERR(sensor_indetect.dev)) {
1014 sensor_err("device_create fail!\n");
1015 goto free_class;
1016 }
1017
1018 ret = device_create_file(sensor_indetect.dev, &detect_dev_attrs);
1019 if (ret) {
1020 sensor_err("class_create file fail!\n");
1021 goto free_class;
1022 }
1023
1024 /* init tp9950 detect mode */
1025 ret = tp9950_sensor_det_init(client);
1026 if (ret) {
1027 goto free_det;
1028 }
1029
1030 return 0;
1031
1032
1033 free_det:
1034 tp9950_sensor_det_remove();
1035
1036 free_class:
1037 class_destroy(sensor_indetect.sensor_class);
1038
1039 unregister_cdev:
1040 cdev_del(sensor_indetect.cdev);
1041
1042 free_cdev:
1043 kfree(sensor_indetect.cdev);
1044
1045 free_devno:
1046 unregister_chrdev_region(sensor_indetect.devno, 1);
1047
1048
1049 #endif
1050
1051 return 0;
1052 }
1053
sensor_remove(struct i2c_client * client)1054 static int sensor_remove(struct i2c_client *client)
1055 {
1056 struct v4l2_subdev *sd;
1057
1058 sd = cci_dev_remove_helper(client, &cci_drv);
1059
1060 #if SENSOR_DETECT_KTHREAD
1061 tp9950_sensor_det_remove();
1062
1063 device_destroy(sensor_indetect.sensor_class, sensor_indetect.devno);
1064 class_destroy(sensor_indetect.sensor_class);
1065 cdev_del(sensor_indetect.cdev);
1066 kfree(sensor_indetect.cdev);
1067 unregister_chrdev_region(sensor_indetect.devno, 1);
1068 #endif
1069
1070 kfree(to_state(sd));
1071 return 0;
1072 }
1073
1074
sensor_shutdown(struct i2c_client * client)1075 static void sensor_shutdown(struct i2c_client *client)
1076 {
1077 struct v4l2_subdev *sd;
1078
1079 sd = cci_dev_remove_helper(client, &cci_drv);
1080
1081 #if SENSOR_DETECT_KTHREAD
1082 tp9950_sensor_det_remove();
1083
1084 device_destroy(sensor_indetect.sensor_class, sensor_indetect.devno);
1085 class_destroy(sensor_indetect.sensor_class);
1086 cdev_del(sensor_indetect.cdev);
1087 kfree(sensor_indetect.cdev);
1088 unregister_chrdev_region(sensor_indetect.devno, 1);
1089
1090 #endif
1091 kfree(to_state(sd));
1092 }
1093
1094 static const struct i2c_device_id sensor_id[] = {
1095 {SENSOR_NAME, 0},
1096 {}
1097 };
1098
1099 MODULE_DEVICE_TABLE(i2c, sensor_id);
1100
1101 static struct i2c_driver sensor_driver = {
1102 .driver = {
1103 .owner = THIS_MODULE,
1104 .name = SENSOR_NAME,
1105 },
1106 .probe = sensor_probe,
1107 .remove = sensor_remove,
1108 .shutdown = sensor_shutdown,
1109 .id_table = sensor_id,
1110 };
init_sensor(void)1111 static __init int init_sensor(void)
1112 {
1113 return cci_dev_init_helper(&sensor_driver);
1114 }
1115
exit_sensor(void)1116 static __exit void exit_sensor(void)
1117 {
1118 cci_dev_exit_helper(&sensor_driver);
1119 }
1120
1121 module_init(init_sensor);
1122 module_exit(exit_sensor);
1123