xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/max96714.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * max96714 GMSL2/GMSL1 to CSI-2 Deserializer driver
4  *
5  * Copyright (C) 2022 Rockchip Electronics Co., Ltd.
6  *
7  * V0.0X01.0X00 first version.
8  *
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/sysfs.h>
20 #include <linux/slab.h>
21 #include <linux/version.h>
22 #include <linux/compat.h>
23 #include <linux/rk-camera-module.h>
24 #include <media/media-entity.h>
25 #include <media/v4l2-async.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-subdev.h>
28 #include <linux/pinctrl/consumer.h>
29 #include "max96714.h"
30 
31 #define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x00)
32 
33 #ifndef V4L2_CID_DIGITAL_GAIN
34 #define V4L2_CID_DIGITAL_GAIN		V4L2_CID_GAIN
35 #endif
36 
37 #define MAX96714_LINK_FREQ_150MHZ	150000000UL
38 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
39 #define MAX96714_PIXEL_RATE		(MAX96714_LINK_FREQ_150MHZ * 2LL * 4LL / 8LL)
40 #define MAX96714_XVCLK_FREQ		24000000
41 
42 #define CHIP_ID				0xC9
43 #define MAX96714_REG_CHIP_ID		0x0D
44 
45 #define MAX96714_REG_CTRL_MODE		0x0313
46 #define MAX96714_MODE_SW_STANDBY	0x0
47 #define MAX96714_MODE_STREAMING		BIT(1)
48 
49 #define REG_NULL			0xFFFF
50 
51 #define MAX96714_LANES			4
52 #define MAX96714_BITS_PER_SAMPLE	8
53 
54 #define OF_CAMERA_PINCTRL_STATE_DEFAULT	"rockchip,camera_default"
55 #define OF_CAMERA_PINCTRL_STATE_SLEEP	"rockchip,camera_sleep"
56 
57 #define MAX96714_REG_VALUE_08BIT	1
58 #define MAX96714_REG_VALUE_16BIT	2
59 #define MAX96714_REG_VALUE_24BIT	3
60 
61 #define MAX96714_NAME			"max96714"
62 #define MAX96714_MEDIA_BUS_FMT		MEDIA_BUS_FMT_UYVY8_2X8
63 
64 static const char * const max96714_supply_names[] = {
65 	"avdd",		/* Analog power */
66 	"dovdd",	/* Digital I/O power */
67 	"dvdd",		/* Digital core power */
68 };
69 
70 #define MAX96714_NUM_SUPPLIES ARRAY_SIZE(max96714_supply_names)
71 
72 struct regval {
73 	u16 i2c_addr;
74 	u16 addr;
75 	u8 val;
76 	u16 delay;
77 };
78 
79 struct max96714_mode {
80 	u32 width;
81 	u32 height;
82 	struct v4l2_fract max_fps;
83 	u32 hts_def;
84 	u32 vts_def;
85 	u32 exp_def;
86 	u32 link_freq_idx;
87 	u32 bpp;
88 	const struct regval *reg_list;
89 };
90 
91 struct max96714 {
92 	struct i2c_client	*client;
93 	struct clk		*xvclk;
94 	struct gpio_desc	*power_gpio;
95 	struct gpio_desc	*reset_gpio;
96 	struct gpio_desc	*pwdn_gpio;
97 	struct regulator_bulk_data supplies[MAX96714_NUM_SUPPLIES];
98 
99 	struct pinctrl		*pinctrl;
100 	struct pinctrl_state	*pins_default;
101 	struct pinctrl_state	*pins_sleep;
102 
103 	struct v4l2_subdev	subdev;
104 	struct media_pad	pad;
105 	struct v4l2_ctrl_handler ctrl_handler;
106 	struct v4l2_ctrl	*exposure;
107 	struct v4l2_ctrl	*anal_gain;
108 	struct v4l2_ctrl	*digi_gain;
109 	struct v4l2_ctrl	*hblank;
110 	struct v4l2_ctrl	*vblank;
111 	struct v4l2_ctrl	*pixel_rate;
112 	struct v4l2_ctrl	*link_freq;
113 	struct v4l2_ctrl	*test_pattern;
114 	struct mutex		mutex;
115 	bool			streaming;
116 	bool			power_on;
117 	bool			hot_plug;
118 	u8			is_reset;
119 	const struct max96714_mode *cur_mode;
120 	u32			module_index;
121 	const char		*module_facing;
122 	const char		*module_name;
123 	const char		*len_name;
124 };
125 
126 #define to_max96714(sd) container_of(sd, struct max96714, subdev)
127 
128 static const struct regval max96714_mipi_1080p_30fps[] = {
129 	{0x4C, 0x0313, 0x00, 0x00},
130 	{0x4C, 0x0001, 0x01, 0x00},
131 	{0x4C, 0x0010, 0x21, 0x00},
132 	{0x4C, 0x0320, 0x23, 0x00},
133 	{0x4C, 0x0325, 0x80, 0x00},
134 	{0x4C, 0x0313, 0x00, 0x00},
135 	{0x4C, REG_NULL, 0x00, 0x00},
136 };
137 
138 static const struct max96714_mode supported_modes[] = {
139 	{
140 		.width = 1920,
141 		.height = 1080,
142 		.max_fps = {
143 			.numerator = 10000,
144 			.denominator = 300000,
145 		},
146 		.reg_list = max96714_mipi_1080p_30fps,
147 		.link_freq_idx = 0,
148 	},
149 };
150 
151 static const s64 link_freq_items[] = {
152 	MAX96714_LINK_FREQ_150MHZ,
153 };
154 
155 /* Write registers up to 4 at a time */
max96714_write_reg(struct i2c_client * client,u16 reg,u32 len,u32 val)156 static int max96714_write_reg(struct i2c_client *client, u16 reg,
157 			     u32 len, u32 val)
158 {
159 	u32 buf_i, val_i;
160 	u8 buf[6];
161 	u8 *val_p;
162 	__be32 val_be;
163 
164 	dev_dbg(&client->dev, "write reg(0x%x val:0x%x)!\n", reg, val);
165 
166 	if (len > 4)
167 		return -EINVAL;
168 
169 	buf[0] = reg >> 8;
170 	buf[1] = reg & 0xff;
171 
172 	val_be = cpu_to_be32(val);
173 	val_p = (u8 *)&val_be;
174 	buf_i = 2;
175 	val_i = 4 - len;
176 
177 	while (val_i < 4)
178 		buf[buf_i++] = val_p[val_i++];
179 
180 	if (i2c_master_send(client, buf, len + 2) != len + 2)
181 		return -EIO;
182 
183 	return 0;
184 }
185 
max96714_write_array(struct i2c_client * client,const struct regval * regs)186 static int max96714_write_array(struct i2c_client *client,
187 			       const struct regval *regs)
188 {
189 	u32 i;
190 	int ret = 0;
191 
192 	for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++) {
193 		client->addr = regs[i].i2c_addr;
194 		ret = max96714_write_reg(client, regs[i].addr,
195 					MAX96714_REG_VALUE_08BIT,
196 					regs[i].val);
197 		msleep(regs[i].delay);
198 	}
199 
200 	return ret;
201 }
202 
203 /* Read registers up to 4 at a time */
max96714_read_reg(struct i2c_client * client,u16 reg,unsigned int len,u32 * val)204 static int max96714_read_reg(struct i2c_client *client, u16 reg,
205 			    unsigned int len, u32 *val)
206 {
207 	struct i2c_msg msgs[2];
208 	u8 *data_be_p;
209 	__be32 data_be = 0;
210 	__be16 reg_addr_be = cpu_to_be16(reg);
211 	int ret;
212 
213 	if (len > 4 || !len)
214 		return -EINVAL;
215 
216 	data_be_p = (u8 *)&data_be;
217 	/* Write register address */
218 	msgs[0].addr = client->addr;
219 	msgs[0].flags = 0;
220 	msgs[0].len = 2;
221 	msgs[0].buf = (u8 *)&reg_addr_be;
222 
223 	/* Read data from register */
224 	msgs[1].addr = client->addr;
225 	msgs[1].flags = I2C_M_RD;
226 	msgs[1].len = len;
227 	msgs[1].buf = &data_be_p[4 - len];
228 
229 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
230 	if (ret != ARRAY_SIZE(msgs))
231 		return -EIO;
232 
233 	*val = be32_to_cpu(data_be);
234 
235 	return 0;
236 }
237 
max96714_get_reso_dist(const struct max96714_mode * mode,struct v4l2_mbus_framefmt * framefmt)238 static int max96714_get_reso_dist(const struct max96714_mode *mode,
239 				 struct v4l2_mbus_framefmt *framefmt)
240 {
241 	return abs(mode->width - framefmt->width) +
242 		abs(mode->height - framefmt->height);
243 }
244 
245 static const struct max96714_mode *
max96714_find_best_fit(struct v4l2_subdev_format * fmt)246 max96714_find_best_fit(struct v4l2_subdev_format *fmt)
247 {
248 	struct v4l2_mbus_framefmt *framefmt = &fmt->format;
249 	int dist;
250 	int cur_best_fit = 0;
251 	int cur_best_fit_dist = -1;
252 	unsigned int i;
253 
254 	for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
255 		dist = max96714_get_reso_dist(&supported_modes[i], framefmt);
256 		if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
257 			cur_best_fit_dist = dist;
258 			cur_best_fit = i;
259 		}
260 	}
261 
262 	return &supported_modes[cur_best_fit];
263 }
264 
max96714_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)265 static int max96714_set_fmt(struct v4l2_subdev *sd,
266 			   struct v4l2_subdev_pad_config *cfg,
267 			  struct v4l2_subdev_format *fmt)
268 {
269 	struct max96714 *max96714 = to_max96714(sd);
270 	const struct max96714_mode *mode;
271 
272 	mutex_lock(&max96714->mutex);
273 
274 	mode = max96714_find_best_fit(fmt);
275 	fmt->format.code = MAX96714_MEDIA_BUS_FMT;
276 	fmt->format.width = mode->width;
277 	fmt->format.height = mode->height;
278 	fmt->format.field = V4L2_FIELD_NONE;
279 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
280 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
281 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
282 #else
283 		mutex_unlock(&max96714->mutex);
284 		return -ENOTTY;
285 #endif
286 	} else {
287 		if (max96714->streaming) {
288 			mutex_unlock(&max96714->mutex);
289 			return -EBUSY;
290 		}
291 	}
292 
293 	mutex_unlock(&max96714->mutex);
294 
295 	return 0;
296 }
297 
max96714_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)298 static int max96714_get_fmt(struct v4l2_subdev *sd,
299 			   struct v4l2_subdev_pad_config *cfg,
300 			   struct v4l2_subdev_format *fmt)
301 {
302 	struct max96714 *max96714 = to_max96714(sd);
303 	const struct max96714_mode *mode = max96714->cur_mode;
304 
305 	mutex_lock(&max96714->mutex);
306 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
307 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
308 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
309 #else
310 		mutex_unlock(&max96714->mutex);
311 		return -ENOTTY;
312 #endif
313 	} else {
314 		fmt->format.width = mode->width;
315 		fmt->format.height = mode->height;
316 		fmt->format.code = MAX96714_MEDIA_BUS_FMT;
317 		fmt->format.field = V4L2_FIELD_NONE;
318 	}
319 	mutex_unlock(&max96714->mutex);
320 
321 	return 0;
322 }
323 
max96714_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)324 static int max96714_enum_mbus_code(struct v4l2_subdev *sd,
325 				  struct v4l2_subdev_pad_config *cfg,
326 				  struct v4l2_subdev_mbus_code_enum *code)
327 {
328 	if (code->index != 0)
329 		return -EINVAL;
330 	code->code = MAX96714_MEDIA_BUS_FMT;
331 
332 	return 0;
333 }
334 
max96714_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)335 static int max96714_enum_frame_sizes(struct v4l2_subdev *sd,
336 				    struct v4l2_subdev_pad_config *cfg,
337 				   struct v4l2_subdev_frame_size_enum *fse)
338 {
339 	if (fse->index >= ARRAY_SIZE(supported_modes))
340 		return -EINVAL;
341 
342 	if (fse->code != MAX96714_MEDIA_BUS_FMT)
343 		return -EINVAL;
344 
345 	fse->min_width  = supported_modes[fse->index].width;
346 	fse->max_width  = supported_modes[fse->index].width;
347 	fse->max_height = supported_modes[fse->index].height;
348 	fse->min_height = supported_modes[fse->index].height;
349 
350 	return 0;
351 }
352 
max96714_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)353 static int max96714_g_frame_interval(struct v4l2_subdev *sd,
354 				    struct v4l2_subdev_frame_interval *fi)
355 {
356 	struct max96714 *max96714 = to_max96714(sd);
357 	const struct max96714_mode *mode = max96714->cur_mode;
358 
359 	mutex_lock(&max96714->mutex);
360 	fi->interval = mode->max_fps;
361 	mutex_unlock(&max96714->mutex);
362 
363 	return 0;
364 }
365 
max96714_get_module_inf(struct max96714 * max96714,struct rkmodule_inf * inf)366 static void max96714_get_module_inf(struct max96714 *max96714,
367 				   struct rkmodule_inf *inf)
368 {
369 	memset(inf, 0, sizeof(*inf));
370 	strscpy(inf->base.sensor, MAX96714_NAME, sizeof(inf->base.sensor));
371 	strscpy(inf->base.module, max96714->module_name,
372 		sizeof(inf->base.module));
373 	strscpy(inf->base.lens, max96714->len_name, sizeof(inf->base.lens));
374 }
375 
max96714_get_vicap_rst_inf(struct max96714 * max96714,struct rkmodule_vicap_reset_info * rst_info)376 static void max96714_get_vicap_rst_inf(struct max96714 *max96714,
377 				   struct rkmodule_vicap_reset_info *rst_info)
378 {
379 	struct i2c_client *client = max96714->client;
380 
381 	rst_info->is_reset = max96714->hot_plug;
382 	max96714->hot_plug = false;
383 	rst_info->src = RKCIF_RESET_SRC_ERR_HOTPLUG;
384 	dev_info(&client->dev, "%s: rst_info->is_reset:%d.\n", __func__, rst_info->is_reset);
385 }
386 
max96714_set_vicap_rst_inf(struct max96714 * max96714,struct rkmodule_vicap_reset_info rst_info)387 static void max96714_set_vicap_rst_inf(struct max96714 *max96714,
388 				   struct rkmodule_vicap_reset_info rst_info)
389 {
390 	max96714->is_reset = rst_info.is_reset;
391 }
392 
max96714_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)393 static long max96714_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
394 {
395 	struct max96714 *max96714 = to_max96714(sd);
396 	long ret = 0;
397 	u32 stream = 0;
398 
399 	switch (cmd) {
400 	case RKMODULE_GET_MODULE_INFO:
401 		max96714_get_module_inf(max96714, (struct rkmodule_inf *)arg);
402 		break;
403 	case RKMODULE_SET_QUICK_STREAM:
404 
405 		stream = *((u32 *)arg);
406 
407 		if (stream)
408 			ret = max96714_write_reg(max96714->client,
409 				 MAX96714_REG_CTRL_MODE,
410 				 MAX96714_REG_VALUE_08BIT,
411 				 MAX96714_MODE_STREAMING);
412 		else
413 			ret = max96714_write_reg(max96714->client,
414 				 MAX96714_REG_CTRL_MODE,
415 				 MAX96714_REG_VALUE_08BIT,
416 				 MAX96714_MODE_SW_STANDBY);
417 		break;
418 	case RKMODULE_GET_VICAP_RST_INFO:
419 		max96714_get_vicap_rst_inf(max96714,
420 			(struct rkmodule_vicap_reset_info *)arg);
421 		break;
422 	case RKMODULE_SET_VICAP_RST_INFO:
423 		max96714_set_vicap_rst_inf(max96714,
424 			*(struct rkmodule_vicap_reset_info *)arg);
425 		break;
426 	case RKMODULE_GET_START_STREAM_SEQ:
427 		// +*(int *)arg = RKMODULE_START_STREAM_FRONT;
428 		// *(int *)arg = RKMODULE_START_STREAM_BEHIND;
429 		break;
430 	default:
431 		ret = -ENOIOCTLCMD;
432 		break;
433 	}
434 
435 	return ret;
436 }
437 
438 #ifdef CONFIG_COMPAT
max96714_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)439 static long max96714_compat_ioctl32(struct v4l2_subdev *sd,
440 				   unsigned int cmd, unsigned long arg)
441 {
442 	void __user *up = compat_ptr(arg);
443 	struct rkmodule_inf *inf;
444 	struct rkmodule_awb_cfg *cfg;
445 	struct rkmodule_vicap_reset_info *vicap_rst_inf;
446 	long ret = 0;
447 	int *seq;
448 	u32 stream = 0;
449 
450 	switch (cmd) {
451 	case RKMODULE_GET_MODULE_INFO:
452 		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
453 		if (!inf) {
454 			ret = -ENOMEM;
455 			return ret;
456 		}
457 
458 		ret = max96714_ioctl(sd, cmd, inf);
459 		if (!ret) {
460 			ret = copy_to_user(up, inf, sizeof(*inf));
461 			if (ret)
462 				ret = -EFAULT;
463 		}
464 		kfree(inf);
465 		break;
466 	case RKMODULE_AWB_CFG:
467 		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
468 		if (!cfg) {
469 			ret = -ENOMEM;
470 			return ret;
471 		}
472 
473 		ret = copy_from_user(cfg, up, sizeof(*cfg));
474 		if (!ret)
475 			ret = max96714_ioctl(sd, cmd, cfg);
476 		else
477 			ret = -EFAULT;
478 		kfree(cfg);
479 		break;
480 	case RKMODULE_GET_VICAP_RST_INFO:
481 		vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
482 		if (!vicap_rst_inf) {
483 			ret = -ENOMEM;
484 			return ret;
485 		}
486 
487 		ret = max96714_ioctl(sd, cmd, vicap_rst_inf);
488 		if (!ret) {
489 			ret = copy_to_user(up, vicap_rst_inf, sizeof(*vicap_rst_inf));
490 			if (ret)
491 				ret = -EFAULT;
492 		}
493 		kfree(vicap_rst_inf);
494 		break;
495 	case RKMODULE_SET_VICAP_RST_INFO:
496 		vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
497 		if (!vicap_rst_inf) {
498 			ret = -ENOMEM;
499 			return ret;
500 		}
501 
502 		ret = copy_from_user(vicap_rst_inf, up, sizeof(*vicap_rst_inf));
503 		if (!ret)
504 			ret = max96714_ioctl(sd, cmd, vicap_rst_inf);
505 		else
506 			ret = -EFAULT;
507 		kfree(vicap_rst_inf);
508 		break;
509 	case RKMODULE_GET_START_STREAM_SEQ:
510 		seq = kzalloc(sizeof(*seq), GFP_KERNEL);
511 		if (!seq) {
512 			ret = -ENOMEM;
513 			return ret;
514 		}
515 
516 		ret = max96714_ioctl(sd, cmd, seq);
517 		if (!ret) {
518 			ret = copy_to_user(up, seq, sizeof(*seq));
519 			if (ret)
520 				ret = -EFAULT;
521 		}
522 		kfree(seq);
523 		break;
524 	case RKMODULE_SET_QUICK_STREAM:
525 		ret = copy_from_user(&stream, up, sizeof(u32));
526 		if (!ret)
527 			ret = max96714_ioctl(sd, cmd, &stream);
528 		else
529 			ret = -EFAULT;
530 		break;
531 	default:
532 		ret = -ENOIOCTLCMD;
533 		break;
534 	}
535 
536 	return ret;
537 }
538 #endif
539 
__max96714_start_stream(struct max96714 * max96714)540 static int __max96714_start_stream(struct max96714 *max96714)
541 {
542 	int ret;
543 
544 	ret = max96714_write_array(max96714->client, max96714->cur_mode->reg_list);
545 	if (ret)
546 		return ret;
547 
548 	/* In case these controls are set before streaming */
549 	mutex_unlock(&max96714->mutex);
550 	ret = v4l2_ctrl_handler_setup(&max96714->ctrl_handler);
551 	mutex_lock(&max96714->mutex);
552 	if (ret)
553 		return ret;
554 
555 	return max96714_write_reg(max96714->client,
556 				 MAX96714_REG_CTRL_MODE,
557 				 MAX96714_REG_VALUE_08BIT,
558 				 MAX96714_MODE_STREAMING);
559 }
560 
__max96714_stop_stream(struct max96714 * max96714)561 static int __max96714_stop_stream(struct max96714 *max96714)
562 {
563 	return max96714_write_reg(max96714->client,
564 				 MAX96714_REG_CTRL_MODE,
565 				 MAX96714_REG_VALUE_08BIT,
566 				 MAX96714_MODE_SW_STANDBY);
567 }
568 
max96714_s_stream(struct v4l2_subdev * sd,int on)569 static int max96714_s_stream(struct v4l2_subdev *sd, int on)
570 {
571 	struct max96714 *max96714 = to_max96714(sd);
572 	struct i2c_client *client = max96714->client;
573 	int ret = 0;
574 
575 	dev_info(&client->dev, "%s: on: %d, %dx%d@%d\n", __func__, on,
576 				max96714->cur_mode->width,
577 				max96714->cur_mode->height,
578 		DIV_ROUND_CLOSEST(max96714->cur_mode->max_fps.denominator,
579 				  max96714->cur_mode->max_fps.numerator));
580 
581 	mutex_lock(&max96714->mutex);
582 	on = !!on;
583 	if (on == max96714->streaming)
584 		goto unlock_and_return;
585 
586 	if (on) {
587 		ret = pm_runtime_get_sync(&client->dev);
588 		if (ret < 0) {
589 			pm_runtime_put_noidle(&client->dev);
590 			goto unlock_and_return;
591 		}
592 
593 		ret = __max96714_start_stream(max96714);
594 		if (ret) {
595 			v4l2_err(sd, "start stream failed while write regs\n");
596 			pm_runtime_put(&client->dev);
597 			goto unlock_and_return;
598 		}
599 	} else {
600 		__max96714_stop_stream(max96714);
601 		pm_runtime_put(&client->dev);
602 	}
603 
604 	max96714->streaming = on;
605 
606 unlock_and_return:
607 	mutex_unlock(&max96714->mutex);
608 
609 	return ret;
610 }
611 
max96714_s_power(struct v4l2_subdev * sd,int on)612 static int max96714_s_power(struct v4l2_subdev *sd, int on)
613 {
614 	struct max96714 *max96714 = to_max96714(sd);
615 	struct i2c_client *client = max96714->client;
616 	int ret = 0;
617 
618 	mutex_lock(&max96714->mutex);
619 
620 	/* If the power state is not modified - no work to do. */
621 	if (max96714->power_on == !!on)
622 		goto unlock_and_return;
623 
624 	if (on) {
625 		ret = pm_runtime_get_sync(&client->dev);
626 		if (ret < 0) {
627 			pm_runtime_put_noidle(&client->dev);
628 			goto unlock_and_return;
629 		}
630 
631 		max96714->power_on = true;
632 	} else {
633 		pm_runtime_put(&client->dev);
634 		max96714->power_on = false;
635 	}
636 
637 unlock_and_return:
638 	mutex_unlock(&max96714->mutex);
639 
640 	return ret;
641 }
642 
643 /* Calculate the delay in us by clock rate and clock cycles */
max96714_cal_delay(u32 cycles)644 static inline u32 max96714_cal_delay(u32 cycles)
645 {
646 	return DIV_ROUND_UP(cycles, MAX96714_XVCLK_FREQ / 1000 / 1000);
647 }
648 
__max96714_power_on(struct max96714 * max96714)649 static int __max96714_power_on(struct max96714 *max96714)
650 {
651 	int ret;
652 	u32 delay_us;
653 	struct device *dev = &max96714->client->dev;
654 
655 	if (!IS_ERR(max96714->power_gpio))
656 		gpiod_set_value_cansleep(max96714->power_gpio, 1);
657 
658 	usleep_range(1000, 2000);
659 
660 	if (!IS_ERR_OR_NULL(max96714->pins_default)) {
661 		ret = pinctrl_select_state(max96714->pinctrl,
662 					   max96714->pins_default);
663 		if (ret < 0)
664 			dev_err(dev, "could not set pins\n");
665 	}
666 
667 	if (!IS_ERR(max96714->reset_gpio))
668 		gpiod_set_value_cansleep(max96714->reset_gpio, 0);
669 
670 	ret = regulator_bulk_enable(MAX96714_NUM_SUPPLIES, max96714->supplies);
671 	if (ret < 0) {
672 		dev_err(dev, "Failed to enable regulators\n");
673 		goto disable_clk;
674 	}
675 
676 	if (!IS_ERR(max96714->reset_gpio))
677 		gpiod_set_value_cansleep(max96714->reset_gpio, 1);
678 
679 	usleep_range(500, 1000);
680 	if (!IS_ERR(max96714->pwdn_gpio))
681 		gpiod_set_value_cansleep(max96714->pwdn_gpio, 1);
682 
683 	/* 8192 cycles prior to first SCCB transaction */
684 	delay_us = max96714_cal_delay(8192);
685 	usleep_range(delay_us, delay_us * 2);
686 
687 	return 0;
688 
689 disable_clk:
690 	clk_disable_unprepare(max96714->xvclk);
691 
692 	return ret;
693 }
694 
__max96714_power_off(struct max96714 * max96714)695 static void __max96714_power_off(struct max96714 *max96714)
696 {
697 	int ret;
698 	struct device *dev = &max96714->client->dev;
699 
700 	if (!IS_ERR(max96714->pwdn_gpio))
701 		gpiod_set_value_cansleep(max96714->pwdn_gpio, 0);
702 	clk_disable_unprepare(max96714->xvclk);
703 	if (!IS_ERR(max96714->reset_gpio))
704 		gpiod_set_value_cansleep(max96714->reset_gpio, 0);
705 
706 	if (!IS_ERR_OR_NULL(max96714->pins_sleep)) {
707 		ret = pinctrl_select_state(max96714->pinctrl,
708 					   max96714->pins_sleep);
709 		if (ret < 0)
710 			dev_dbg(dev, "could not set pins\n");
711 	}
712 	if (!IS_ERR(max96714->power_gpio))
713 		gpiod_set_value_cansleep(max96714->power_gpio, 0);
714 
715 	regulator_bulk_disable(MAX96714_NUM_SUPPLIES, max96714->supplies);
716 }
717 
max96714_runtime_resume(struct device * dev)718 static int max96714_runtime_resume(struct device *dev)
719 {
720 	struct i2c_client *client = to_i2c_client(dev);
721 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
722 	struct max96714 *max96714 = to_max96714(sd);
723 
724 	return __max96714_power_on(max96714);
725 }
726 
max96714_runtime_suspend(struct device * dev)727 static int max96714_runtime_suspend(struct device *dev)
728 {
729 	struct i2c_client *client = to_i2c_client(dev);
730 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
731 	struct max96714 *max96714 = to_max96714(sd);
732 
733 	__max96714_power_off(max96714);
734 
735 	return 0;
736 }
737 
738 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
max96714_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)739 static int max96714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
740 {
741 	struct max96714 *max96714 = to_max96714(sd);
742 	struct v4l2_mbus_framefmt *try_fmt =
743 				v4l2_subdev_get_try_format(sd, fh->pad, 0);
744 	const struct max96714_mode *def_mode = &supported_modes[0];
745 
746 	mutex_lock(&max96714->mutex);
747 	/* Initialize try_fmt */
748 	try_fmt->width = def_mode->width;
749 	try_fmt->height = def_mode->height;
750 	try_fmt->code = MAX96714_MEDIA_BUS_FMT;
751 	try_fmt->field = V4L2_FIELD_NONE;
752 
753 	mutex_unlock(&max96714->mutex);
754 	/* No crop or compose */
755 
756 	return 0;
757 }
758 #endif
759 
max96714_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)760 static int max96714_enum_frame_interval(struct v4l2_subdev *sd,
761 				       struct v4l2_subdev_pad_config *cfg,
762 				       struct v4l2_subdev_frame_interval_enum *fie)
763 {
764 	if (fie->index >= ARRAY_SIZE(supported_modes))
765 		return -EINVAL;
766 
767 	fie->code = MAX96714_MEDIA_BUS_FMT;
768 
769 	fie->width = supported_modes[fie->index].width;
770 	fie->height = supported_modes[fie->index].height;
771 	fie->interval = supported_modes[fie->index].max_fps;
772 
773 	return 0;
774 }
775 
max96714_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * config)776 static int max96714_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
777 				struct v4l2_mbus_config *config)
778 {
779 	config->type = V4L2_MBUS_CSI2_DPHY;
780 	config->flags = V4L2_MBUS_CSI2_4_LANE |
781 			V4L2_MBUS_CSI2_CHANNEL_0 |
782 			V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
783 
784 	return 0;
785 }
786 
max96714_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)787 static int max96714_get_selection(struct v4l2_subdev *sd,
788 				struct v4l2_subdev_pad_config *cfg,
789 				struct v4l2_subdev_selection *sel)
790 {
791 	struct max96714 *max96714 = to_max96714(sd);
792 
793 	if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
794 		sel->r.left = 0;
795 		sel->r.width = max96714->cur_mode->width;
796 		sel->r.top = 0;
797 		sel->r.height = max96714->cur_mode->height;
798 		return 0;
799 	}
800 
801 	return -EINVAL;
802 }
803 
804 static const struct dev_pm_ops max96714_pm_ops = {
805 	SET_RUNTIME_PM_OPS(max96714_runtime_suspend,
806 			   max96714_runtime_resume, NULL)
807 };
808 
809 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
810 static const struct v4l2_subdev_internal_ops max96714_internal_ops = {
811 	.open = max96714_open,
812 };
813 #endif
814 
815 static const struct v4l2_subdev_core_ops max96714_core_ops = {
816 	.s_power = max96714_s_power,
817 	.ioctl = max96714_ioctl,
818 #ifdef CONFIG_COMPAT
819 	.compat_ioctl32 = max96714_compat_ioctl32,
820 #endif
821 };
822 
823 static const struct v4l2_subdev_video_ops max96714_video_ops = {
824 	.s_stream = max96714_s_stream,
825 	.g_frame_interval = max96714_g_frame_interval,
826 };
827 
828 static const struct v4l2_subdev_pad_ops max96714_pad_ops = {
829 	.enum_mbus_code = max96714_enum_mbus_code,
830 	.enum_frame_size = max96714_enum_frame_sizes,
831 	.enum_frame_interval = max96714_enum_frame_interval,
832 	.get_fmt = max96714_get_fmt,
833 	.set_fmt = max96714_set_fmt,
834 	.get_selection = max96714_get_selection,
835 	.get_mbus_config = max96714_g_mbus_config,
836 };
837 
838 static const struct v4l2_subdev_ops max96714_subdev_ops = {
839 	.core	= &max96714_core_ops,
840 	.video	= &max96714_video_ops,
841 	.pad	= &max96714_pad_ops,
842 };
843 
max96714_initialize_controls(struct max96714 * max96714)844 static int max96714_initialize_controls(struct max96714 *max96714)
845 {
846 	const struct max96714_mode *mode;
847 	struct v4l2_ctrl_handler *handler;
848 	int ret;
849 
850 	handler = &max96714->ctrl_handler;
851 	mode = max96714->cur_mode;
852 	ret = v4l2_ctrl_handler_init(handler, 2);
853 	if (ret)
854 		return ret;
855 	handler->lock = &max96714->mutex;
856 
857 	max96714->link_freq = v4l2_ctrl_new_int_menu(handler, NULL,
858 			V4L2_CID_LINK_FREQ,
859 			1, 0, link_freq_items);
860 
861 	max96714->pixel_rate = v4l2_ctrl_new_std(handler, NULL,
862 			V4L2_CID_PIXEL_RATE,
863 			0, MAX96714_PIXEL_RATE,
864 			1, MAX96714_PIXEL_RATE);
865 
866 	__v4l2_ctrl_s_ctrl(max96714->link_freq,
867 			   mode->link_freq_idx);
868 
869 	if (handler->error) {
870 		ret = handler->error;
871 		dev_err(&max96714->client->dev,
872 			"Failed to init controls(%d)\n", ret);
873 		goto err_free_handler;
874 	}
875 
876 	max96714->subdev.ctrl_handler = handler;
877 
878 	return 0;
879 
880 err_free_handler:
881 	v4l2_ctrl_handler_free(handler);
882 
883 	return ret;
884 }
885 
max96714_check_sensor_id(struct max96714 * max96714,struct i2c_client * client)886 static int max96714_check_sensor_id(struct max96714 *max96714,
887 				   struct i2c_client *client)
888 {
889 	struct device *dev = &max96714->client->dev;
890 	u32 id = 0;
891 	int ret;
892 
893 	ret = max96714_read_reg(client, MAX96714_REG_CHIP_ID,
894 			       MAX96714_REG_VALUE_08BIT, &id);
895 	if (id != CHIP_ID) {
896 		dev_err(dev, "Unexpected sensor id(%02x), ret(%d)\n", id, ret);
897 		return -ENODEV;
898 	}
899 
900 	dev_info(dev, "Detected %02x sensor\n", CHIP_ID);
901 
902 	return 0;
903 }
904 
max96714_configure_regulators(struct max96714 * max96714)905 static int max96714_configure_regulators(struct max96714 *max96714)
906 {
907 	unsigned int i;
908 
909 	for (i = 0; i < MAX96714_NUM_SUPPLIES; i++)
910 		max96714->supplies[i].supply = max96714_supply_names[i];
911 
912 	return devm_regulator_bulk_get(&max96714->client->dev,
913 					MAX96714_NUM_SUPPLIES,
914 					max96714->supplies);
915 }
916 
max96714_probe(struct i2c_client * client,const struct i2c_device_id * id)917 static int max96714_probe(struct i2c_client *client,
918 			 const struct i2c_device_id *id)
919 {
920 	struct device *dev = &client->dev;
921 	struct device_node *node = dev->of_node;
922 	struct max96714 *max96714;
923 	struct v4l2_subdev *sd;
924 	char facing[2];
925 	int ret;
926 
927 	dev_info(dev, "driver version: %02x.%02x.%02x",
928 		DRIVER_VERSION >> 16,
929 		(DRIVER_VERSION & 0xff00) >> 8,
930 		DRIVER_VERSION & 0x00ff);
931 
932 	max96714 = devm_kzalloc(dev, sizeof(*max96714), GFP_KERNEL);
933 	if (!max96714)
934 		return -ENOMEM;
935 
936 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
937 				   &max96714->module_index);
938 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
939 				       &max96714->module_facing);
940 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
941 				       &max96714->module_name);
942 	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
943 				       &max96714->len_name);
944 	if (ret) {
945 		dev_err(dev, "could not get module information!\n");
946 		return -EINVAL;
947 	}
948 
949 	max96714->client = client;
950 	max96714->cur_mode = &supported_modes[0];
951 
952 	max96714->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
953 	if (IS_ERR(max96714->power_gpio))
954 		dev_warn(dev, "Failed to get power-gpios, maybe no use\n");
955 
956 	max96714->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
957 	if (IS_ERR(max96714->reset_gpio))
958 		dev_warn(dev, "Failed to get reset-gpios\n");
959 
960 	max96714->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
961 	if (IS_ERR(max96714->pwdn_gpio))
962 		dev_warn(dev, "Failed to get pwdn-gpios\n");
963 
964 	ret = max96714_configure_regulators(max96714);
965 	if (ret) {
966 		dev_err(dev, "Failed to get power regulators\n");
967 		return ret;
968 	}
969 
970 	max96714->pinctrl = devm_pinctrl_get(dev);
971 	if (!IS_ERR(max96714->pinctrl)) {
972 		max96714->pins_default =
973 			pinctrl_lookup_state(max96714->pinctrl,
974 					     OF_CAMERA_PINCTRL_STATE_DEFAULT);
975 		if (IS_ERR(max96714->pins_default))
976 			dev_err(dev, "could not get default pinstate\n");
977 
978 		max96714->pins_sleep =
979 			pinctrl_lookup_state(max96714->pinctrl,
980 					     OF_CAMERA_PINCTRL_STATE_SLEEP);
981 		if (IS_ERR(max96714->pins_sleep))
982 			dev_err(dev, "could not get sleep pinstate\n");
983 	}
984 
985 	mutex_init(&max96714->mutex);
986 
987 	sd = &max96714->subdev;
988 	v4l2_i2c_subdev_init(sd, client, &max96714_subdev_ops);
989 	ret = max96714_initialize_controls(max96714);
990 	if (ret)
991 		goto err_destroy_mutex;
992 
993 	ret = __max96714_power_on(max96714);
994 	if (ret)
995 		goto err_free_handler;
996 
997 	ret = max96714_check_sensor_id(max96714, client);
998 	if (ret)
999 		goto err_power_off;
1000 
1001 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1002 	sd->internal_ops = &max96714_internal_ops;
1003 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1004 #endif
1005 #if defined(CONFIG_MEDIA_CONTROLLER)
1006 	max96714->pad.flags = MEDIA_PAD_FL_SOURCE;
1007 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1008 	ret = media_entity_pads_init(&sd->entity, 1, &max96714->pad);
1009 	if (ret < 0)
1010 		goto err_power_off;
1011 #endif
1012 
1013 	memset(facing, 0, sizeof(facing));
1014 	if (strcmp(max96714->module_facing, "back") == 0)
1015 		facing[0] = 'b';
1016 	else
1017 		facing[0] = 'f';
1018 
1019 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1020 		 max96714->module_index, facing,
1021 		 MAX96714_NAME, dev_name(sd->dev));
1022 	ret = v4l2_async_register_subdev_sensor_common(sd);
1023 	if (ret) {
1024 		dev_err(dev, "v4l2 async register subdev failed\n");
1025 		goto err_clean_entity;
1026 	}
1027 
1028 	pm_runtime_set_active(dev);
1029 	pm_runtime_enable(dev);
1030 	pm_runtime_idle(dev);
1031 
1032 	return 0;
1033 
1034 err_clean_entity:
1035 #if defined(CONFIG_MEDIA_CONTROLLER)
1036 	media_entity_cleanup(&sd->entity);
1037 #endif
1038 err_power_off:
1039 	__max96714_power_off(max96714);
1040 err_free_handler:
1041 	v4l2_ctrl_handler_free(&max96714->ctrl_handler);
1042 err_destroy_mutex:
1043 	mutex_destroy(&max96714->mutex);
1044 
1045 	return ret;
1046 }
1047 
max96714_remove(struct i2c_client * client)1048 static int max96714_remove(struct i2c_client *client)
1049 {
1050 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1051 	struct max96714 *max96714 = to_max96714(sd);
1052 
1053 	v4l2_async_unregister_subdev(sd);
1054 #if defined(CONFIG_MEDIA_CONTROLLER)
1055 	media_entity_cleanup(&sd->entity);
1056 #endif
1057 	v4l2_ctrl_handler_free(&max96714->ctrl_handler);
1058 	mutex_destroy(&max96714->mutex);
1059 
1060 	pm_runtime_disable(&client->dev);
1061 	if (!pm_runtime_status_suspended(&client->dev))
1062 		__max96714_power_off(max96714);
1063 	pm_runtime_set_suspended(&client->dev);
1064 
1065 	return 0;
1066 }
1067 
1068 #if IS_ENABLED(CONFIG_OF)
1069 static const struct of_device_id max96714_of_match[] = {
1070 	{ .compatible = "maxim,max96714" },
1071 	{},
1072 };
1073 MODULE_DEVICE_TABLE(of, max96714_of_match);
1074 #endif
1075 
1076 static const struct i2c_device_id max96714_match_id[] = {
1077 	{ "maxim,max96714", 0 },
1078 	{},
1079 };
1080 
1081 static struct i2c_driver max96714_i2c_driver = {
1082 	.driver = {
1083 		.name = MAX96714_NAME,
1084 		.pm = &max96714_pm_ops,
1085 		.of_match_table = of_match_ptr(max96714_of_match),
1086 	},
1087 	.probe		= &max96714_probe,
1088 	.remove		= &max96714_remove,
1089 	.id_table	= max96714_match_id,
1090 };
1091 
max96714_sensor_mod_init(void)1092 int max96714_sensor_mod_init(void)
1093 {
1094 	return i2c_add_driver(&max96714_i2c_driver);
1095 }
1096 
1097 #ifndef CONFIG_VIDEO_REVERSE_IMAGE
1098 device_initcall_sync(max96714_sensor_mod_init);
1099 #endif
1100 
sensor_mod_exit(void)1101 static void __exit sensor_mod_exit(void)
1102 {
1103 	i2c_del_driver(&max96714_i2c_driver);
1104 }
1105 
1106 module_exit(sensor_mod_exit);
1107 
1108 MODULE_DESCRIPTION("Maxim max96714 sensor driver");
1109 MODULE_LICENSE("GPL");
1110