1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for OmniVision OV2722 1080p HD camera sensor.
4  *
5  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kmod.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/moduleparam.h>
21 #include <media/v4l2-device.h>
22 #include "../include/linux/atomisp_gmin_platform.h"
23 #include <linux/acpi.h>
24 #include <linux/io.h>
25 
26 #include "ov2722.h"
27 
28 /* i2c read/write stuff */
ov2722_read_reg(struct i2c_client * client,u16 data_length,u16 reg,u16 * val)29 static int ov2722_read_reg(struct i2c_client *client,
30 			   u16 data_length, u16 reg, u16 *val)
31 {
32 	int err;
33 	struct i2c_msg msg[2];
34 	unsigned char data[6];
35 
36 	if (!client->adapter) {
37 		dev_err(&client->dev, "%s error, no client->adapter\n",
38 			__func__);
39 		return -ENODEV;
40 	}
41 
42 	if (data_length != OV2722_8BIT && data_length != OV2722_16BIT &&
43 	    data_length != OV2722_32BIT) {
44 		dev_err(&client->dev, "%s error, invalid data length\n",
45 			__func__);
46 		return -EINVAL;
47 	}
48 
49 	memset(msg, 0, sizeof(msg));
50 
51 	msg[0].addr = client->addr;
52 	msg[0].flags = 0;
53 	msg[0].len = I2C_MSG_LENGTH;
54 	msg[0].buf = data;
55 
56 	/* high byte goes out first */
57 	data[0] = (u8)(reg >> 8);
58 	data[1] = (u8)(reg & 0xff);
59 
60 	msg[1].addr = client->addr;
61 	msg[1].len = data_length;
62 	msg[1].flags = I2C_M_RD;
63 	msg[1].buf = data;
64 
65 	err = i2c_transfer(client->adapter, msg, 2);
66 	if (err != 2) {
67 		if (err >= 0)
68 			err = -EIO;
69 		dev_err(&client->dev,
70 			"read from offset 0x%x error %d", reg, err);
71 		return err;
72 	}
73 
74 	*val = 0;
75 	/* high byte comes first */
76 	if (data_length == OV2722_8BIT)
77 		*val = (u8)data[0];
78 	else if (data_length == OV2722_16BIT)
79 		*val = be16_to_cpu(*(__be16 *)&data[0]);
80 	else
81 		*val = be32_to_cpu(*(__be32 *)&data[0]);
82 
83 	return 0;
84 }
85 
ov2722_i2c_write(struct i2c_client * client,u16 len,u8 * data)86 static int ov2722_i2c_write(struct i2c_client *client, u16 len, u8 *data)
87 {
88 	struct i2c_msg msg;
89 	const int num_msg = 1;
90 	int ret;
91 
92 	msg.addr = client->addr;
93 	msg.flags = 0;
94 	msg.len = len;
95 	msg.buf = data;
96 	ret = i2c_transfer(client->adapter, &msg, 1);
97 
98 	return ret == num_msg ? 0 : -EIO;
99 }
100 
ov2722_write_reg(struct i2c_client * client,u16 data_length,u16 reg,u16 val)101 static int ov2722_write_reg(struct i2c_client *client, u16 data_length,
102 			    u16 reg, u16 val)
103 {
104 	int ret;
105 	unsigned char data[4] = {0};
106 	__be16 *wreg = (__be16 *)data;
107 	const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
108 
109 	if (data_length != OV2722_8BIT && data_length != OV2722_16BIT) {
110 		dev_err(&client->dev,
111 			"%s error, invalid data_length\n", __func__);
112 		return -EINVAL;
113 	}
114 
115 	/* high byte goes out first */
116 	*wreg = cpu_to_be16(reg);
117 
118 	if (data_length == OV2722_8BIT) {
119 		data[2] = (u8)(val);
120 	} else {
121 		/* OV2722_16BIT */
122 		__be16 *wdata = (__be16 *)&data[2];
123 
124 		*wdata = cpu_to_be16(val);
125 	}
126 
127 	ret = ov2722_i2c_write(client, len, data);
128 	if (ret)
129 		dev_err(&client->dev,
130 			"write error: wrote 0x%x to offset 0x%x error %d",
131 			val, reg, ret);
132 
133 	return ret;
134 }
135 
136 /*
137  * ov2722_write_reg_array - Initializes a list of OV2722 registers
138  * @client: i2c driver client structure
139  * @reglist: list of registers to be written
140  *
141  * This function initializes a list of registers. When consecutive addresses
142  * are found in a row on the list, this function creates a buffer and sends
143  * consecutive data in a single i2c_transfer().
144  *
145  * __ov2722_flush_reg_array, __ov2722_buf_reg_array() and
146  * __ov2722_write_reg_is_consecutive() are internal functions to
147  * ov2722_write_reg_array_fast() and should be not used anywhere else.
148  *
149  */
150 
__ov2722_flush_reg_array(struct i2c_client * client,struct ov2722_write_ctrl * ctrl)151 static int __ov2722_flush_reg_array(struct i2c_client *client,
152 				    struct ov2722_write_ctrl *ctrl)
153 {
154 	u16 size;
155 	__be16 *data16 = (void *)&ctrl->buffer.addr;
156 
157 	if (ctrl->index == 0)
158 		return 0;
159 
160 	size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
161 	*data16 = cpu_to_be16(ctrl->buffer.addr);
162 	ctrl->index = 0;
163 
164 	return ov2722_i2c_write(client, size, (u8 *)&ctrl->buffer);
165 }
166 
__ov2722_buf_reg_array(struct i2c_client * client,struct ov2722_write_ctrl * ctrl,const struct ov2722_reg * next)167 static int __ov2722_buf_reg_array(struct i2c_client *client,
168 				  struct ov2722_write_ctrl *ctrl,
169 				  const struct ov2722_reg *next)
170 {
171 	int size;
172 	__be16 *data16;
173 
174 	switch (next->type) {
175 	case OV2722_8BIT:
176 		size = 1;
177 		ctrl->buffer.data[ctrl->index] = (u8)next->val;
178 		break;
179 	case OV2722_16BIT:
180 		size = 2;
181 		data16 = (void *)&ctrl->buffer.data[ctrl->index];
182 		*data16 = cpu_to_be16((u16)next->val);
183 		break;
184 	default:
185 		return -EINVAL;
186 	}
187 
188 	/* When first item is added, we need to store its starting address */
189 	if (ctrl->index == 0)
190 		ctrl->buffer.addr = next->reg;
191 
192 	ctrl->index += size;
193 
194 	/*
195 	 * Buffer cannot guarantee free space for u32? Better flush it to avoid
196 	 * possible lack of memory for next item.
197 	 */
198 	if (ctrl->index + sizeof(u16) >= OV2722_MAX_WRITE_BUF_SIZE)
199 		return __ov2722_flush_reg_array(client, ctrl);
200 
201 	return 0;
202 }
203 
__ov2722_write_reg_is_consecutive(struct i2c_client * client,struct ov2722_write_ctrl * ctrl,const struct ov2722_reg * next)204 static int __ov2722_write_reg_is_consecutive(struct i2c_client *client,
205 					     struct ov2722_write_ctrl *ctrl,
206 					     const struct ov2722_reg *next)
207 {
208 	if (ctrl->index == 0)
209 		return 1;
210 
211 	return ctrl->buffer.addr + ctrl->index == next->reg;
212 }
213 
ov2722_write_reg_array(struct i2c_client * client,const struct ov2722_reg * reglist)214 static int ov2722_write_reg_array(struct i2c_client *client,
215 				  const struct ov2722_reg *reglist)
216 {
217 	const struct ov2722_reg *next = reglist;
218 	struct ov2722_write_ctrl ctrl;
219 	int err;
220 
221 	ctrl.index = 0;
222 	for (; next->type != OV2722_TOK_TERM; next++) {
223 		switch (next->type & OV2722_TOK_MASK) {
224 		case OV2722_TOK_DELAY:
225 			err = __ov2722_flush_reg_array(client, &ctrl);
226 			if (err)
227 				return err;
228 			msleep(next->val);
229 			break;
230 		default:
231 			/*
232 			 * If next address is not consecutive, data needs to be
233 			 * flushed before proceed.
234 			 */
235 			if (!__ov2722_write_reg_is_consecutive(client, &ctrl,
236 							       next)) {
237 				err = __ov2722_flush_reg_array(client, &ctrl);
238 				if (err)
239 					return err;
240 			}
241 			err = __ov2722_buf_reg_array(client, &ctrl, next);
242 			if (err) {
243 				dev_err(&client->dev, "%s: write error, aborted\n",
244 					__func__);
245 				return err;
246 			}
247 			break;
248 		}
249 	}
250 
251 	return __ov2722_flush_reg_array(client, &ctrl);
252 }
253 
__ov2722_set_exposure(struct v4l2_subdev * sd,int coarse_itg,int gain,int digitgain)254 static long __ov2722_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
255 				  int gain, int digitgain)
256 
257 {
258 	struct i2c_client *client = v4l2_get_subdevdata(sd);
259 	struct ov2722_device *dev = to_ov2722_sensor(sd);
260 	u16 hts, vts;
261 	int ret;
262 
263 	dev_dbg(&client->dev, "set_exposure without group hold\n");
264 
265 	/* clear VTS_DIFF on manual mode */
266 	ret = ov2722_write_reg(client, OV2722_16BIT, OV2722_VTS_DIFF_H, 0);
267 	if (ret)
268 		return ret;
269 
270 	hts = dev->pixels_per_line;
271 	vts = dev->lines_per_frame;
272 
273 	if ((coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN) > vts)
274 		vts = coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN;
275 
276 	coarse_itg <<= 4;
277 	digitgain <<= 2;
278 
279 	ret = ov2722_write_reg(client, OV2722_16BIT,
280 			       OV2722_VTS_H, vts);
281 	if (ret)
282 		return ret;
283 
284 	ret = ov2722_write_reg(client, OV2722_16BIT,
285 			       OV2722_HTS_H, hts);
286 	if (ret)
287 		return ret;
288 
289 	/* set exposure */
290 	ret = ov2722_write_reg(client, OV2722_8BIT,
291 			       OV2722_AEC_PK_EXPO_L,
292 			       coarse_itg & 0xff);
293 	if (ret)
294 		return ret;
295 
296 	ret = ov2722_write_reg(client, OV2722_16BIT,
297 			       OV2722_AEC_PK_EXPO_H,
298 			       (coarse_itg >> 8) & 0xfff);
299 	if (ret)
300 		return ret;
301 
302 	/* set analog gain */
303 	ret = ov2722_write_reg(client, OV2722_16BIT,
304 			       OV2722_AGC_ADJ_H, gain);
305 	if (ret)
306 		return ret;
307 
308 	/* set digital gain */
309 	ret = ov2722_write_reg(client, OV2722_16BIT,
310 			       OV2722_MWB_GAIN_R_H, digitgain);
311 	if (ret)
312 		return ret;
313 
314 	ret = ov2722_write_reg(client, OV2722_16BIT,
315 			       OV2722_MWB_GAIN_G_H, digitgain);
316 	if (ret)
317 		return ret;
318 
319 	ret = ov2722_write_reg(client, OV2722_16BIT,
320 			       OV2722_MWB_GAIN_B_H, digitgain);
321 
322 	return ret;
323 }
324 
ov2722_set_exposure(struct v4l2_subdev * sd,int exposure,int gain,int digitgain)325 static int ov2722_set_exposure(struct v4l2_subdev *sd, int exposure,
326 			       int gain, int digitgain)
327 {
328 	struct ov2722_device *dev = to_ov2722_sensor(sd);
329 	int ret;
330 
331 	mutex_lock(&dev->input_lock);
332 	ret = __ov2722_set_exposure(sd, exposure, gain, digitgain);
333 	mutex_unlock(&dev->input_lock);
334 
335 	return ret;
336 }
337 
ov2722_s_exposure(struct v4l2_subdev * sd,struct atomisp_exposure * exposure)338 static long ov2722_s_exposure(struct v4l2_subdev *sd,
339 			      struct atomisp_exposure *exposure)
340 {
341 	int exp = exposure->integration_time[0];
342 	int gain = exposure->gain[0];
343 	int digitgain = exposure->gain[1];
344 
345 	/* we should not accept the invalid value below. */
346 	if (gain == 0) {
347 		struct i2c_client *client = v4l2_get_subdevdata(sd);
348 
349 		v4l2_err(client, "%s: invalid value\n", __func__);
350 		return -EINVAL;
351 	}
352 
353 	return ov2722_set_exposure(sd, exp, gain, digitgain);
354 }
355 
ov2722_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)356 static long ov2722_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
357 {
358 	switch (cmd) {
359 	case ATOMISP_IOC_S_EXPOSURE:
360 		return ov2722_s_exposure(sd, arg);
361 	default:
362 		return -EINVAL;
363 	}
364 	return 0;
365 }
366 
367 /* This returns the exposure time being used. This should only be used
368  * for filling in EXIF data, not for actual image processing.
369  */
ov2722_q_exposure(struct v4l2_subdev * sd,s32 * value)370 static int ov2722_q_exposure(struct v4l2_subdev *sd, s32 *value)
371 {
372 	struct i2c_client *client = v4l2_get_subdevdata(sd);
373 	u16 reg_v, reg_v2;
374 	int ret;
375 
376 	/* get exposure */
377 	ret = ov2722_read_reg(client, OV2722_8BIT,
378 			      OV2722_AEC_PK_EXPO_L,
379 			      &reg_v);
380 	if (ret)
381 		goto err;
382 
383 	ret = ov2722_read_reg(client, OV2722_8BIT,
384 			      OV2722_AEC_PK_EXPO_M,
385 			      &reg_v2);
386 	if (ret)
387 		goto err;
388 
389 	reg_v += reg_v2 << 8;
390 	ret = ov2722_read_reg(client, OV2722_8BIT,
391 			      OV2722_AEC_PK_EXPO_H,
392 			      &reg_v2);
393 	if (ret)
394 		goto err;
395 
396 	*value = reg_v + (((u32)reg_v2 << 16));
397 err:
398 	return ret;
399 }
400 
ov2722_g_volatile_ctrl(struct v4l2_ctrl * ctrl)401 static int ov2722_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
402 {
403 	struct ov2722_device *dev =
404 	    container_of(ctrl->handler, struct ov2722_device, ctrl_handler);
405 	int ret = 0;
406 	unsigned int val;
407 
408 	switch (ctrl->id) {
409 	case V4L2_CID_EXPOSURE_ABSOLUTE:
410 		ret = ov2722_q_exposure(&dev->sd, &ctrl->val);
411 		break;
412 	case V4L2_CID_LINK_FREQ:
413 		val = dev->res->mipi_freq;
414 		if (val == 0)
415 			return -EINVAL;
416 
417 		ctrl->val = val * 1000;	/* To Hz */
418 		break;
419 	default:
420 		ret = -EINVAL;
421 	}
422 
423 	return ret;
424 }
425 
426 static const struct v4l2_ctrl_ops ctrl_ops = {
427 	.g_volatile_ctrl = ov2722_g_volatile_ctrl
428 };
429 
430 static const struct v4l2_ctrl_config ov2722_controls[] = {
431 	{
432 		.ops = &ctrl_ops,
433 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
434 		.type = V4L2_CTRL_TYPE_INTEGER,
435 		.name = "exposure",
436 		.min = 0x0,
437 		.max = 0xffff,
438 		.step = 0x01,
439 		.def = 0x00,
440 		.flags = 0,
441 	},
442 	{
443 		.ops = &ctrl_ops,
444 		.id = V4L2_CID_LINK_FREQ,
445 		.name = "Link Frequency",
446 		.type = V4L2_CTRL_TYPE_INTEGER,
447 		.min = 1,
448 		.max = 1500000 * 1000,
449 		.step = 1,
450 		.def = 1,
451 		.flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY,
452 	},
453 };
454 
ov2722_init(struct v4l2_subdev * sd)455 static int ov2722_init(struct v4l2_subdev *sd)
456 {
457 	struct ov2722_device *dev = to_ov2722_sensor(sd);
458 
459 	mutex_lock(&dev->input_lock);
460 
461 	/* restore settings */
462 	ov2722_res = ov2722_res_preview;
463 	N_RES = N_RES_PREVIEW;
464 
465 	mutex_unlock(&dev->input_lock);
466 
467 	return 0;
468 }
469 
power_ctrl(struct v4l2_subdev * sd,bool flag)470 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
471 {
472 	int ret = -1;
473 	struct ov2722_device *dev = to_ov2722_sensor(sd);
474 
475 	if (!dev || !dev->platform_data)
476 		return -ENODEV;
477 
478 	if (flag) {
479 		ret = dev->platform_data->v1p8_ctrl(sd, 1);
480 		if (ret == 0) {
481 			ret = dev->platform_data->v2p8_ctrl(sd, 1);
482 			if (ret)
483 				dev->platform_data->v1p8_ctrl(sd, 0);
484 		}
485 	} else {
486 		ret = dev->platform_data->v1p8_ctrl(sd, 0);
487 		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
488 	}
489 
490 	return ret;
491 }
492 
gpio_ctrl(struct v4l2_subdev * sd,bool flag)493 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
494 {
495 	struct ov2722_device *dev = to_ov2722_sensor(sd);
496 	int ret = -1;
497 
498 	if (!dev || !dev->platform_data)
499 		return -ENODEV;
500 
501 	/* Note: the GPIO order is asymmetric: always RESET#
502 	 * before PWDN# when turning it on or off.
503 	 */
504 	ret = dev->platform_data->gpio0_ctrl(sd, flag);
505 	ret |= dev->platform_data->gpio1_ctrl(sd, flag);
506 	return ret;
507 }
508 
power_up(struct v4l2_subdev * sd)509 static int power_up(struct v4l2_subdev *sd)
510 {
511 	struct ov2722_device *dev = to_ov2722_sensor(sd);
512 	struct i2c_client *client = v4l2_get_subdevdata(sd);
513 	int ret;
514 
515 	if (!dev->platform_data) {
516 		dev_err(&client->dev,
517 			"no camera_sensor_platform_data");
518 		return -ENODEV;
519 	}
520 
521 	/* power control */
522 	ret = power_ctrl(sd, 1);
523 	if (ret)
524 		goto fail_power;
525 
526 	/* according to DS, at least 5ms is needed between DOVDD and PWDN */
527 	usleep_range(5000, 6000);
528 
529 	/* gpio ctrl */
530 	ret = gpio_ctrl(sd, 1);
531 	if (ret) {
532 		ret = gpio_ctrl(sd, 0);
533 		if (ret)
534 			goto fail_power;
535 	}
536 
537 	/* flis clock control */
538 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
539 	if (ret)
540 		goto fail_clk;
541 
542 	/* according to DS, 20ms is needed between PWDN and i2c access */
543 	msleep(20);
544 
545 	return 0;
546 
547 fail_clk:
548 	gpio_ctrl(sd, 0);
549 fail_power:
550 	power_ctrl(sd, 0);
551 	dev_err(&client->dev, "sensor power-up failed\n");
552 
553 	return ret;
554 }
555 
power_down(struct v4l2_subdev * sd)556 static int power_down(struct v4l2_subdev *sd)
557 {
558 	struct ov2722_device *dev = to_ov2722_sensor(sd);
559 	struct i2c_client *client = v4l2_get_subdevdata(sd);
560 	int ret = 0;
561 
562 	if (!dev->platform_data) {
563 		dev_err(&client->dev,
564 			"no camera_sensor_platform_data");
565 		return -ENODEV;
566 	}
567 
568 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
569 	if (ret)
570 		dev_err(&client->dev, "flisclk failed\n");
571 
572 	/* gpio ctrl */
573 	ret = gpio_ctrl(sd, 0);
574 	if (ret) {
575 		ret = gpio_ctrl(sd, 0);
576 		if (ret)
577 			dev_err(&client->dev, "gpio failed 2\n");
578 	}
579 
580 	/* power control */
581 	ret = power_ctrl(sd, 0);
582 	if (ret)
583 		dev_err(&client->dev, "vprog failed.\n");
584 
585 	return ret;
586 }
587 
ov2722_s_power(struct v4l2_subdev * sd,int on)588 static int ov2722_s_power(struct v4l2_subdev *sd, int on)
589 {
590 	int ret;
591 
592 	if (on == 0)
593 		return power_down(sd);
594 
595 	ret = power_up(sd);
596 	if (!ret)
597 		return ov2722_init(sd);
598 
599 	return ret;
600 }
601 
602 /* TODO: remove it. */
startup(struct v4l2_subdev * sd)603 static int startup(struct v4l2_subdev *sd)
604 {
605 	struct ov2722_device *dev = to_ov2722_sensor(sd);
606 	struct i2c_client *client = v4l2_get_subdevdata(sd);
607 	int ret = 0;
608 
609 	ret = ov2722_write_reg(client, OV2722_8BIT,
610 			       OV2722_SW_RESET, 0x01);
611 	if (ret) {
612 		dev_err(&client->dev, "ov2722 reset err.\n");
613 		return ret;
614 	}
615 
616 	ret = ov2722_write_reg_array(client, dev->res->regs);
617 	if (ret) {
618 		dev_err(&client->dev, "ov2722 write register err.\n");
619 		return ret;
620 	}
621 
622 	return ret;
623 }
624 
ov2722_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)625 static int ov2722_set_fmt(struct v4l2_subdev *sd,
626 			  struct v4l2_subdev_state *sd_state,
627 			  struct v4l2_subdev_format *format)
628 {
629 	struct v4l2_mbus_framefmt *fmt = &format->format;
630 	struct ov2722_device *dev = to_ov2722_sensor(sd);
631 	struct i2c_client *client = v4l2_get_subdevdata(sd);
632 	struct ov2722_resolution *res;
633 	struct camera_mipi_info *ov2722_info = NULL;
634 	int ret = 0;
635 
636 	if (format->pad)
637 		return -EINVAL;
638 	if (!fmt)
639 		return -EINVAL;
640 	ov2722_info = v4l2_get_subdev_hostdata(sd);
641 	if (!ov2722_info)
642 		return -EINVAL;
643 
644 	res = v4l2_find_nearest_size(ov2722_res_preview,
645 				     ARRAY_SIZE(ov2722_res_preview), width,
646 				     height, fmt->width, fmt->height);
647 	if (!res)
648 		res = &ov2722_res_preview[N_RES - 1];
649 
650 	fmt->width = res->width;
651 	fmt->height = res->height;
652 	dev->res = res;
653 
654 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
655 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
656 		*v4l2_subdev_state_get_format(sd_state, 0) = *fmt;
657 		return 0;
658 	}
659 
660 	mutex_lock(&dev->input_lock);
661 
662 	dev->pixels_per_line = dev->res->pixels_per_line;
663 	dev->lines_per_frame = dev->res->lines_per_frame;
664 
665 	ret = startup(sd);
666 	if (ret) {
667 		int i = 0;
668 
669 		dev_err(&client->dev, "ov2722 startup err, retry to power up\n");
670 		for (i = 0; i < OV2722_POWER_UP_RETRY_NUM; i++) {
671 			dev_err(&client->dev,
672 				"ov2722 retry to power up %d/%d times, result: ",
673 				i + 1, OV2722_POWER_UP_RETRY_NUM);
674 			power_down(sd);
675 			ret = power_up(sd);
676 			if (ret) {
677 				dev_err(&client->dev, "power up failed, continue\n");
678 				continue;
679 			}
680 			ret = startup(sd);
681 			if (ret) {
682 				dev_err(&client->dev, " startup FAILED!\n");
683 			} else {
684 				dev_err(&client->dev, " startup SUCCESS!\n");
685 				break;
686 			}
687 		}
688 		if (ret) {
689 			dev_err(&client->dev, "ov2722 startup err\n");
690 			goto err;
691 		}
692 	}
693 
694 err:
695 	mutex_unlock(&dev->input_lock);
696 	return ret;
697 }
698 
ov2722_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)699 static int ov2722_get_fmt(struct v4l2_subdev *sd,
700 			  struct v4l2_subdev_state *sd_state,
701 			  struct v4l2_subdev_format *format)
702 {
703 	struct v4l2_mbus_framefmt *fmt = &format->format;
704 	struct ov2722_device *dev = to_ov2722_sensor(sd);
705 
706 	if (format->pad)
707 		return -EINVAL;
708 	if (!fmt)
709 		return -EINVAL;
710 
711 	fmt->width = dev->res->width;
712 	fmt->height = dev->res->height;
713 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
714 
715 	return 0;
716 }
717 
ov2722_detect(struct i2c_client * client)718 static int ov2722_detect(struct i2c_client *client)
719 {
720 	struct i2c_adapter *adapter = client->adapter;
721 	u16 high = 0, low = 0;
722 	u16 id;
723 	u8 revision;
724 
725 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
726 		return -ENODEV;
727 
728 	ov2722_read_reg(client, OV2722_8BIT,
729 			OV2722_SC_CMMN_CHIP_ID_H, &high);
730 	ov2722_read_reg(client, OV2722_8BIT,
731 			OV2722_SC_CMMN_CHIP_ID_L, &low);
732 	id = (high << 8) | low;
733 
734 	if ((id != OV2722_ID) && (id != OV2720_ID)) {
735 		dev_err(&client->dev, "sensor ID error\n");
736 		return -ENODEV;
737 	}
738 
739 	high = 0;
740 	ov2722_read_reg(client, OV2722_8BIT,
741 			OV2722_SC_CMMN_SUB_ID, &high);
742 	revision = (u8)high & 0x0f;
743 
744 	dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
745 	dev_dbg(&client->dev, "detect ov2722 success\n");
746 	return 0;
747 }
748 
ov2722_s_stream(struct v4l2_subdev * sd,int enable)749 static int ov2722_s_stream(struct v4l2_subdev *sd, int enable)
750 {
751 	struct ov2722_device *dev = to_ov2722_sensor(sd);
752 	struct i2c_client *client = v4l2_get_subdevdata(sd);
753 	int ret;
754 
755 	mutex_lock(&dev->input_lock);
756 
757 	ret = ov2722_write_reg(client, OV2722_8BIT, OV2722_SW_STREAM,
758 			       enable ? OV2722_START_STREAMING :
759 			       OV2722_STOP_STREAMING);
760 
761 	mutex_unlock(&dev->input_lock);
762 	return ret;
763 }
764 
ov2722_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)765 static int ov2722_s_config(struct v4l2_subdev *sd,
766 			   int irq, void *platform_data)
767 {
768 	struct ov2722_device *dev = to_ov2722_sensor(sd);
769 	struct i2c_client *client = v4l2_get_subdevdata(sd);
770 	int ret = 0;
771 
772 	if (!platform_data)
773 		return -ENODEV;
774 
775 	dev->platform_data =
776 	    (struct camera_sensor_platform_data *)platform_data;
777 
778 	mutex_lock(&dev->input_lock);
779 
780 	/* power off the module, then power on it in future
781 	 * as first power on by board may not fulfill the
782 	 * power on sequqence needed by the module
783 	 */
784 	ret = power_down(sd);
785 	if (ret) {
786 		dev_err(&client->dev, "ov2722 power-off err.\n");
787 		goto fail_power_off;
788 	}
789 
790 	ret = power_up(sd);
791 	if (ret) {
792 		dev_err(&client->dev, "ov2722 power-up err.\n");
793 		goto fail_power_on;
794 	}
795 
796 	ret = dev->platform_data->csi_cfg(sd, 1);
797 	if (ret)
798 		goto fail_csi_cfg;
799 
800 	/* config & detect sensor */
801 	ret = ov2722_detect(client);
802 	if (ret) {
803 		dev_err(&client->dev, "ov2722_detect err s_config.\n");
804 		goto fail_csi_cfg;
805 	}
806 
807 	/* turn off sensor, after probed */
808 	ret = power_down(sd);
809 	if (ret) {
810 		dev_err(&client->dev, "ov2722 power-off err.\n");
811 		goto fail_csi_cfg;
812 	}
813 	mutex_unlock(&dev->input_lock);
814 
815 	return 0;
816 
817 fail_csi_cfg:
818 	dev->platform_data->csi_cfg(sd, 0);
819 fail_power_on:
820 	power_down(sd);
821 	dev_err(&client->dev, "sensor power-gating failed\n");
822 fail_power_off:
823 	mutex_unlock(&dev->input_lock);
824 	return ret;
825 }
826 
ov2722_get_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_interval * interval)827 static int ov2722_get_frame_interval(struct v4l2_subdev *sd,
828 				     struct v4l2_subdev_state *sd_state,
829 				     struct v4l2_subdev_frame_interval *interval)
830 {
831 	struct ov2722_device *dev = to_ov2722_sensor(sd);
832 
833 	/*
834 	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
835 	 * subdev active state API.
836 	 */
837 	if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE)
838 		return -EINVAL;
839 
840 	interval->interval.numerator = 1;
841 	interval->interval.denominator = dev->res->fps;
842 
843 	return 0;
844 }
845 
ov2722_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)846 static int ov2722_enum_mbus_code(struct v4l2_subdev *sd,
847 				 struct v4l2_subdev_state *sd_state,
848 				 struct v4l2_subdev_mbus_code_enum *code)
849 {
850 	if (code->index >= MAX_FMTS)
851 		return -EINVAL;
852 
853 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
854 	return 0;
855 }
856 
ov2722_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)857 static int ov2722_enum_frame_size(struct v4l2_subdev *sd,
858 				  struct v4l2_subdev_state *sd_state,
859 				  struct v4l2_subdev_frame_size_enum *fse)
860 {
861 	int index = fse->index;
862 
863 	if (index >= N_RES)
864 		return -EINVAL;
865 
866 	fse->min_width = ov2722_res[index].width;
867 	fse->min_height = ov2722_res[index].height;
868 	fse->max_width = ov2722_res[index].width;
869 	fse->max_height = ov2722_res[index].height;
870 
871 	return 0;
872 }
873 
ov2722_g_skip_frames(struct v4l2_subdev * sd,u32 * frames)874 static int ov2722_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
875 {
876 	struct ov2722_device *dev = to_ov2722_sensor(sd);
877 
878 	mutex_lock(&dev->input_lock);
879 	*frames = dev->res->skip_frames;
880 	mutex_unlock(&dev->input_lock);
881 
882 	return 0;
883 }
884 
885 static const struct v4l2_subdev_sensor_ops ov2722_sensor_ops = {
886 	.g_skip_frames	= ov2722_g_skip_frames,
887 };
888 
889 static const struct v4l2_subdev_video_ops ov2722_video_ops = {
890 	.s_stream = ov2722_s_stream,
891 };
892 
893 static const struct v4l2_subdev_core_ops ov2722_core_ops = {
894 	.s_power = ov2722_s_power,
895 	.ioctl = ov2722_ioctl,
896 };
897 
898 static const struct v4l2_subdev_pad_ops ov2722_pad_ops = {
899 	.enum_mbus_code = ov2722_enum_mbus_code,
900 	.enum_frame_size = ov2722_enum_frame_size,
901 	.get_fmt = ov2722_get_fmt,
902 	.set_fmt = ov2722_set_fmt,
903 	.get_frame_interval = ov2722_get_frame_interval,
904 };
905 
906 static const struct v4l2_subdev_ops ov2722_ops = {
907 	.core = &ov2722_core_ops,
908 	.video = &ov2722_video_ops,
909 	.pad = &ov2722_pad_ops,
910 	.sensor = &ov2722_sensor_ops,
911 };
912 
ov2722_remove(struct i2c_client * client)913 static void ov2722_remove(struct i2c_client *client)
914 {
915 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
916 	struct ov2722_device *dev = to_ov2722_sensor(sd);
917 
918 	dev->platform_data->csi_cfg(sd, 0);
919 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
920 	v4l2_device_unregister_subdev(sd);
921 
922 	atomisp_gmin_remove_subdev(sd);
923 
924 	media_entity_cleanup(&dev->sd.entity);
925 	kfree(dev);
926 }
927 
__ov2722_init_ctrl_handler(struct ov2722_device * dev)928 static int __ov2722_init_ctrl_handler(struct ov2722_device *dev)
929 {
930 	struct v4l2_ctrl_handler *hdl;
931 	unsigned int i;
932 
933 	hdl = &dev->ctrl_handler;
934 	v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ov2722_controls));
935 	for (i = 0; i < ARRAY_SIZE(ov2722_controls); i++)
936 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2722_controls[i],
937 				     NULL);
938 
939 	dev->link_freq = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_LINK_FREQ);
940 
941 	if (dev->ctrl_handler.error || !dev->link_freq)
942 		return dev->ctrl_handler.error;
943 
944 	dev->sd.ctrl_handler = hdl;
945 
946 	return 0;
947 }
948 
ov2722_probe(struct i2c_client * client)949 static int ov2722_probe(struct i2c_client *client)
950 {
951 	struct ov2722_device *dev;
952 	void *ovpdev;
953 	int ret;
954 
955 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
956 	if (!dev)
957 		return -ENOMEM;
958 
959 	mutex_init(&dev->input_lock);
960 
961 	dev->res = &ov2722_res_preview[0];
962 	v4l2_i2c_subdev_init(&dev->sd, client, &ov2722_ops);
963 
964 	ovpdev = gmin_camera_platform_data(&dev->sd,
965 					   ATOMISP_INPUT_FORMAT_RAW_10,
966 					   atomisp_bayer_order_grbg);
967 
968 	ret = ov2722_s_config(&dev->sd, client->irq, ovpdev);
969 	if (ret)
970 		goto out_free;
971 
972 	ret = __ov2722_init_ctrl_handler(dev);
973 	if (ret)
974 		goto out_ctrl_handler_free;
975 
976 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
977 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
978 	dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
979 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
980 
981 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
982 	if (ret)
983 		ov2722_remove(client);
984 
985 	return atomisp_register_i2c_module(&dev->sd, ovpdev);
986 
987 out_ctrl_handler_free:
988 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
989 
990 out_free:
991 	atomisp_gmin_remove_subdev(&dev->sd);
992 	v4l2_device_unregister_subdev(&dev->sd);
993 	kfree(dev);
994 	return ret;
995 }
996 
997 static const struct acpi_device_id ov2722_acpi_match[] = {
998 	{ "INT33FB" },
999 	{},
1000 };
1001 MODULE_DEVICE_TABLE(acpi, ov2722_acpi_match);
1002 
1003 static struct i2c_driver ov2722_driver = {
1004 	.driver = {
1005 		.name = "ov2722",
1006 		.acpi_match_table = ov2722_acpi_match,
1007 	},
1008 	.probe = ov2722_probe,
1009 	.remove = ov2722_remove,
1010 };
1011 module_i2c_driver(ov2722_driver);
1012 
1013 MODULE_AUTHOR("Wei Liu <wei.liu@intel.com>");
1014 MODULE_DESCRIPTION("A low-level driver for OmniVision 2722 sensors");
1015 MODULE_LICENSE("GPL");
1016