xref: /linux/drivers/media/i2c/ar0521.c (revision ed5c2f5fd10dda07263f79f338a512c0f49f76f5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 Sieć Badawcza Łukasiewicz
4  * - Przemysłowy Instytut Automatyki i Pomiarów PIAP
5  * Written by Krzysztof Hałasa
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/pm_runtime.h>
11 
12 #include <media/v4l2-ctrls.h>
13 #include <media/v4l2-fwnode.h>
14 #include <media/v4l2-subdev.h>
15 
16 /* External clock (extclk) frequencies */
17 #define AR0521_EXTCLK_MIN	  (10 * 1000 * 1000)
18 #define AR0521_EXTCLK_MAX	  (48 * 1000 * 1000)
19 
20 /* PLL and PLL2 */
21 #define AR0521_PLL_MIN		 (320 * 1000 * 1000)
22 #define AR0521_PLL_MAX		(1280 * 1000 * 1000)
23 
24 /* Effective pixel clocks, the registers may be DDR */
25 #define AR0521_PIXEL_CLOCK_RATE	 (184 * 1000 * 1000)
26 #define AR0521_PIXEL_CLOCK_MIN	 (168 * 1000 * 1000)
27 #define AR0521_PIXEL_CLOCK_MAX	 (414 * 1000 * 1000)
28 
29 #define AR0521_WIDTH_MIN	       8u
30 #define AR0521_WIDTH_MAX	    2608u
31 #define AR0521_HEIGHT_MIN	       8u
32 #define AR0521_HEIGHT_MAX	    1958u
33 
34 #define AR0521_WIDTH_BLANKING_MIN     572u
35 #define AR0521_HEIGHT_BLANKING_MIN     38u /* must be even */
36 #define AR0521_TOTAL_WIDTH_MIN	     2968u
37 
38 /* AR0521 registers */
39 #define AR0521_REG_VT_PIX_CLK_DIV		0x0300
40 #define AR0521_REG_FRAME_LENGTH_LINES		0x0340
41 
42 #define AR0521_REG_CHIP_ID			0x3000
43 #define AR0521_REG_COARSE_INTEGRATION_TIME	0x3012
44 #define AR0521_REG_ROW_SPEED			0x3016
45 #define AR0521_REG_EXTRA_DELAY			0x3018
46 #define AR0521_REG_RESET			0x301A
47 #define   AR0521_REG_RESET_DEFAULTS		  0x0238
48 #define   AR0521_REG_RESET_GROUP_PARAM_HOLD	  0x8000
49 #define   AR0521_REG_RESET_STREAM		  BIT(2)
50 #define   AR0521_REG_RESET_RESTART		  BIT(1)
51 #define   AR0521_REG_RESET_INIT			  BIT(0)
52 
53 #define AR0521_REG_GREEN1_GAIN			0x3056
54 #define AR0521_REG_BLUE_GAIN			0x3058
55 #define AR0521_REG_RED_GAIN			0x305A
56 #define AR0521_REG_GREEN2_GAIN			0x305C
57 #define AR0521_REG_GLOBAL_GAIN			0x305E
58 
59 #define AR0521_REG_HISPI_TEST_MODE		0x3066
60 #define AR0521_REG_HISPI_TEST_MODE_LP11		  0x0004
61 
62 #define AR0521_REG_TEST_PATTERN_MODE		0x3070
63 
64 #define AR0521_REG_SERIAL_FORMAT		0x31AE
65 #define AR0521_REG_SERIAL_FORMAT_MIPI		  0x0200
66 
67 #define AR0521_REG_HISPI_CONTROL_STATUS		0x31C6
68 #define AR0521_REG_HISPI_CONTROL_STATUS_FRAMER_TEST_MODE_ENABLE 0x80
69 
70 #define be		cpu_to_be16
71 
72 static const char * const ar0521_supply_names[] = {
73 	"vdd_io",	/* I/O (1.8V) supply */
74 	"vdd",		/* Core, PLL and MIPI (1.2V) supply */
75 	"vaa",		/* Analog (2.7V) supply */
76 };
77 
78 struct ar0521_ctrls {
79 	struct v4l2_ctrl_handler handler;
80 	struct {
81 		struct v4l2_ctrl *gain;
82 		struct v4l2_ctrl *red_balance;
83 		struct v4l2_ctrl *blue_balance;
84 	};
85 	struct {
86 		struct v4l2_ctrl *hblank;
87 		struct v4l2_ctrl *vblank;
88 	};
89 	struct v4l2_ctrl *pixrate;
90 	struct v4l2_ctrl *exposure;
91 	struct v4l2_ctrl *test_pattern;
92 };
93 
94 struct ar0521_dev {
95 	struct i2c_client *i2c_client;
96 	struct v4l2_subdev sd;
97 	struct media_pad pad;
98 	struct clk *extclk;
99 	u32 extclk_freq;
100 
101 	struct regulator *supplies[ARRAY_SIZE(ar0521_supply_names)];
102 	struct gpio_desc *reset_gpio;
103 
104 	/* lock to protect all members below */
105 	struct mutex lock;
106 
107 	struct v4l2_mbus_framefmt fmt;
108 	struct ar0521_ctrls ctrls;
109 	unsigned int lane_count;
110 	u16 total_width;
111 	u16 total_height;
112 	u16 pll_pre;
113 	u16 pll_mult;
114 	u16 pll_pre2;
115 	u16 pll_mult2;
116 	bool streaming;
117 };
118 
119 static inline struct ar0521_dev *to_ar0521_dev(struct v4l2_subdev *sd)
120 {
121 	return container_of(sd, struct ar0521_dev, sd);
122 }
123 
124 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
125 {
126 	return &container_of(ctrl->handler, struct ar0521_dev,
127 			     ctrls.handler)->sd;
128 }
129 
130 static u32 div64_round(u64 v, u32 d)
131 {
132 	return div_u64(v + (d >> 1), d);
133 }
134 
135 static u32 div64_round_up(u64 v, u32 d)
136 {
137 	return div_u64(v + d - 1, d);
138 }
139 
140 /* Data must be BE16, the first value is the register address */
141 static int ar0521_write_regs(struct ar0521_dev *sensor, const __be16 *data,
142 			     unsigned int count)
143 {
144 	struct i2c_client *client = sensor->i2c_client;
145 	struct i2c_msg msg;
146 	int ret;
147 
148 	msg.addr = client->addr;
149 	msg.flags = client->flags;
150 	msg.buf = (u8 *)data;
151 	msg.len = count * sizeof(*data);
152 
153 	ret = i2c_transfer(client->adapter, &msg, 1);
154 
155 	if (ret < 0) {
156 		v4l2_err(&sensor->sd, "%s: I2C write error\n", __func__);
157 		return ret;
158 	}
159 
160 	return 0;
161 }
162 
163 static int ar0521_write_reg(struct ar0521_dev *sensor, u16 reg, u16 val)
164 {
165 	__be16 buf[2] = {be(reg), be(val)};
166 
167 	return ar0521_write_regs(sensor, buf, 2);
168 }
169 
170 static int ar0521_set_geometry(struct ar0521_dev *sensor)
171 {
172 	/* All dimensions are unsigned 12-bit integers */
173 	u16 x = (AR0521_WIDTH_MAX - sensor->fmt.width) / 2;
174 	u16 y = ((AR0521_HEIGHT_MAX - sensor->fmt.height) / 2) & ~1;
175 	__be16 regs[] = {
176 		be(AR0521_REG_FRAME_LENGTH_LINES),
177 		be(sensor->total_height),
178 		be(sensor->total_width),
179 		be(x),
180 		be(y),
181 		be(x + sensor->fmt.width - 1),
182 		be(y + sensor->fmt.height - 1),
183 		be(sensor->fmt.width),
184 		be(sensor->fmt.height)
185 	};
186 
187 	return ar0521_write_regs(sensor, regs, ARRAY_SIZE(regs));
188 }
189 
190 static int ar0521_set_gains(struct ar0521_dev *sensor)
191 {
192 	int green = sensor->ctrls.gain->val;
193 	int red = max(green + sensor->ctrls.red_balance->val, 0);
194 	int blue = max(green + sensor->ctrls.blue_balance->val, 0);
195 	unsigned int gain = min(red, min(green, blue));
196 	unsigned int analog = min(gain, 64u); /* range is 0 - 127 */
197 	__be16 regs[5];
198 
199 	red   = min(red   - analog + 64, 511u);
200 	green = min(green - analog + 64, 511u);
201 	blue  = min(blue  - analog + 64, 511u);
202 	regs[0] = be(AR0521_REG_GREEN1_GAIN);
203 	regs[1] = be(green << 7 | analog);
204 	regs[2] = be(blue  << 7 | analog);
205 	regs[3] = be(red   << 7 | analog);
206 	regs[4] = be(green << 7 | analog);
207 
208 	return ar0521_write_regs(sensor, regs, ARRAY_SIZE(regs));
209 }
210 
211 static u32 calc_pll(struct ar0521_dev *sensor, int num, u32 freq, u16 *pre_ptr,
212 		    u16 *mult_ptr)
213 {
214 	u16 pre = 1, mult = 1, new_pre;
215 	u32 pll = AR0521_PLL_MAX + 1;
216 
217 	for (new_pre = 1; new_pre < 64; new_pre++) {
218 		u32 new_pll;
219 		u32 new_mult = div64_round_up((u64)freq * new_pre,
220 					      sensor->extclk_freq);
221 
222 		if (new_mult < 32)
223 			continue; /* Minimum value */
224 		if (new_mult > 254)
225 			break; /* Maximum, larger pre won't work either */
226 		if (sensor->extclk_freq * (u64)new_mult < AR0521_PLL_MIN *
227 		    new_pre)
228 			continue;
229 		if (sensor->extclk_freq * (u64)new_mult > AR0521_PLL_MAX *
230 		    new_pre)
231 			break; /* Larger pre won't work either */
232 		new_pll = div64_round_up(sensor->extclk_freq * (u64)new_mult,
233 					 new_pre);
234 		if (new_pll < pll) {
235 			pll = new_pll;
236 			pre = new_pre;
237 			mult = new_mult;
238 		}
239 	}
240 
241 	pll = div64_round(sensor->extclk_freq * (u64)mult, pre);
242 	*pre_ptr = pre;
243 	*mult_ptr = mult;
244 	return pll;
245 }
246 
247 #define DIV 4
248 static void ar0521_calc_mode(struct ar0521_dev *sensor)
249 {
250 	unsigned int speed_mod = 4 / sensor->lane_count; /* 1 with 4 DDR lanes */
251 	u16 total_width = max(sensor->fmt.width + AR0521_WIDTH_BLANKING_MIN,
252 			      AR0521_TOTAL_WIDTH_MIN);
253 	u16 total_height = sensor->fmt.height + AR0521_HEIGHT_BLANKING_MIN;
254 
255 	/* Calculate approximate pixel clock first */
256 	u64 pix_clk = AR0521_PIXEL_CLOCK_RATE;
257 
258 	/* PLL1 drives pixel clock - dual rate */
259 	pix_clk = calc_pll(sensor, 1, pix_clk * (DIV / 2), &sensor->pll_pre,
260 			   &sensor->pll_mult);
261 	pix_clk = div64_round(pix_clk, (DIV / 2));
262 	calc_pll(sensor, 2, pix_clk * (DIV / 2) * speed_mod, &sensor->pll_pre2,
263 		 &sensor->pll_mult2);
264 
265 	sensor->total_width = total_width;
266 	sensor->total_height = total_height;
267 }
268 
269 static int ar0521_write_mode(struct ar0521_dev *sensor)
270 {
271 	__be16 pll_regs[] = {
272 		be(AR0521_REG_VT_PIX_CLK_DIV),
273 		/* 0x300 */ be(4), /* vt_pix_clk_div = number of bits / 2 */
274 		/* 0x302 */ be(1), /* vt_sys_clk_div */
275 		/* 0x304 */ be((sensor->pll_pre2 << 8) | sensor->pll_pre),
276 		/* 0x306 */ be((sensor->pll_mult2 << 8) | sensor->pll_mult),
277 		/* 0x308 */ be(8), /* op_pix_clk_div = 2 * vt_pix_clk_div */
278 		/* 0x30A */ be(1)  /* op_sys_clk_div */
279 	};
280 	int ret;
281 
282 	/* Stop streaming for just a moment */
283 	ret = ar0521_write_reg(sensor, AR0521_REG_RESET,
284 			       AR0521_REG_RESET_DEFAULTS);
285 	if (ret)
286 		return ret;
287 
288 	ret = ar0521_set_geometry(sensor);
289 	if (ret)
290 		return ret;
291 
292 	ret = ar0521_write_regs(sensor, pll_regs, ARRAY_SIZE(pll_regs));
293 	if (ret)
294 		return ret;
295 
296 	ret = ar0521_write_reg(sensor, AR0521_REG_COARSE_INTEGRATION_TIME,
297 			       sensor->ctrls.exposure->val);
298 	if (ret)
299 		return ret;
300 
301 	ret = ar0521_write_reg(sensor, AR0521_REG_RESET,
302 			       AR0521_REG_RESET_DEFAULTS |
303 			       AR0521_REG_RESET_STREAM);
304 	if (ret)
305 		return ret;
306 
307 	ret = ar0521_write_reg(sensor, AR0521_REG_TEST_PATTERN_MODE,
308 			       sensor->ctrls.test_pattern->val);
309 	return ret;
310 }
311 
312 static int ar0521_set_stream(struct ar0521_dev *sensor, bool on)
313 {
314 	int ret;
315 
316 	if (on) {
317 		ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
318 		if (ret < 0)
319 			return ret;
320 
321 		ar0521_calc_mode(sensor);
322 		ret = ar0521_write_mode(sensor);
323 		if (ret)
324 			goto err;
325 
326 		ret = ar0521_set_gains(sensor);
327 		if (ret)
328 			goto err;
329 
330 		/* Exit LP-11 mode on clock and data lanes */
331 		ret = ar0521_write_reg(sensor, AR0521_REG_HISPI_CONTROL_STATUS,
332 				       0);
333 		if (ret)
334 			goto err;
335 
336 		/* Start streaming */
337 		ret = ar0521_write_reg(sensor, AR0521_REG_RESET,
338 				       AR0521_REG_RESET_DEFAULTS |
339 				       AR0521_REG_RESET_STREAM);
340 		if (ret)
341 			goto err;
342 
343 		return 0;
344 
345 err:
346 		pm_runtime_put(&sensor->i2c_client->dev);
347 		return ret;
348 
349 	} else {
350 		/*
351 		 * Reset gain, the sensor may produce all white pixels without
352 		 * this
353 		 */
354 		ret = ar0521_write_reg(sensor, AR0521_REG_GLOBAL_GAIN, 0x2000);
355 		if (ret)
356 			return ret;
357 
358 		/* Stop streaming */
359 		ret = ar0521_write_reg(sensor, AR0521_REG_RESET,
360 				       AR0521_REG_RESET_DEFAULTS);
361 		if (ret)
362 			return ret;
363 
364 		pm_runtime_put(&sensor->i2c_client->dev);
365 		return 0;
366 	}
367 }
368 
369 static void ar0521_adj_fmt(struct v4l2_mbus_framefmt *fmt)
370 {
371 	fmt->width = clamp(ALIGN(fmt->width, 4), AR0521_WIDTH_MIN,
372 			   AR0521_WIDTH_MAX);
373 	fmt->height = clamp(ALIGN(fmt->height, 4), AR0521_HEIGHT_MIN,
374 			    AR0521_HEIGHT_MAX);
375 	fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
376 	fmt->field = V4L2_FIELD_NONE;
377 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
378 	fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
379 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
380 	fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
381 }
382 
383 static int ar0521_get_fmt(struct v4l2_subdev *sd,
384 			  struct v4l2_subdev_state *sd_state,
385 			  struct v4l2_subdev_format *format)
386 {
387 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
388 	struct v4l2_mbus_framefmt *fmt;
389 
390 	mutex_lock(&sensor->lock);
391 
392 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
393 		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, 0
394 						 /* pad */);
395 	else
396 		fmt = &sensor->fmt;
397 
398 	format->format = *fmt;
399 
400 	mutex_unlock(&sensor->lock);
401 	return 0;
402 }
403 
404 static int ar0521_set_fmt(struct v4l2_subdev *sd,
405 			  struct v4l2_subdev_state *sd_state,
406 			  struct v4l2_subdev_format *format)
407 {
408 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
409 	int ret = 0;
410 
411 	ar0521_adj_fmt(&format->format);
412 
413 	mutex_lock(&sensor->lock);
414 
415 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
416 		struct v4l2_mbus_framefmt *fmt;
417 
418 		fmt = v4l2_subdev_get_try_format(sd, sd_state, 0 /* pad */);
419 		*fmt = format->format;
420 	} else {
421 		sensor->fmt = format->format;
422 		ar0521_calc_mode(sensor);
423 	}
424 
425 	mutex_unlock(&sensor->lock);
426 	return ret;
427 }
428 
429 static int ar0521_s_ctrl(struct v4l2_ctrl *ctrl)
430 {
431 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
432 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
433 	int ret;
434 
435 	/* v4l2_ctrl_lock() locks our own mutex */
436 
437 	switch (ctrl->id) {
438 	case V4L2_CID_HBLANK:
439 	case V4L2_CID_VBLANK:
440 		sensor->total_width = sensor->fmt.width +
441 			sensor->ctrls.hblank->val;
442 		sensor->total_height = sensor->fmt.width +
443 			sensor->ctrls.vblank->val;
444 		break;
445 	default:
446 		ret = -EINVAL;
447 		break;
448 	}
449 
450 	/* access the sensor only if it's powered up */
451 	if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
452 		return 0;
453 
454 	switch (ctrl->id) {
455 	case V4L2_CID_HBLANK:
456 	case V4L2_CID_VBLANK:
457 		ret = ar0521_set_geometry(sensor);
458 		break;
459 	case V4L2_CID_GAIN:
460 	case V4L2_CID_RED_BALANCE:
461 	case V4L2_CID_BLUE_BALANCE:
462 		ret = ar0521_set_gains(sensor);
463 		break;
464 	case V4L2_CID_EXPOSURE:
465 		ret = ar0521_write_reg(sensor,
466 				       AR0521_REG_COARSE_INTEGRATION_TIME,
467 				       ctrl->val);
468 		break;
469 	case V4L2_CID_TEST_PATTERN:
470 		ret = ar0521_write_reg(sensor, AR0521_REG_TEST_PATTERN_MODE,
471 				       ctrl->val);
472 		break;
473 	}
474 
475 	pm_runtime_put(&sensor->i2c_client->dev);
476 	return ret;
477 }
478 
479 static const struct v4l2_ctrl_ops ar0521_ctrl_ops = {
480 	.s_ctrl = ar0521_s_ctrl,
481 };
482 
483 static const char * const test_pattern_menu[] = {
484 	"Disabled",
485 	"Solid color",
486 	"Color bars",
487 	"Faded color bars"
488 };
489 
490 static int ar0521_init_controls(struct ar0521_dev *sensor)
491 {
492 	const struct v4l2_ctrl_ops *ops = &ar0521_ctrl_ops;
493 	struct ar0521_ctrls *ctrls = &sensor->ctrls;
494 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
495 	int ret;
496 
497 	v4l2_ctrl_handler_init(hdl, 32);
498 
499 	/* We can use our own mutex for the ctrl lock */
500 	hdl->lock = &sensor->lock;
501 
502 	/* Manual gain */
503 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 511, 1, 0);
504 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
505 					       -512, 511, 1, 0);
506 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
507 						-512, 511, 1, 0);
508 	v4l2_ctrl_cluster(3, &ctrls->gain);
509 
510 	ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK,
511 					  AR0521_WIDTH_BLANKING_MIN, 4094, 1,
512 					  AR0521_WIDTH_BLANKING_MIN);
513 	ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
514 					  AR0521_HEIGHT_BLANKING_MIN, 4094, 2,
515 					  AR0521_HEIGHT_BLANKING_MIN);
516 	v4l2_ctrl_cluster(2, &ctrls->hblank);
517 
518 	/* Read-only */
519 	ctrls->pixrate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
520 					   AR0521_PIXEL_CLOCK_MIN,
521 					   AR0521_PIXEL_CLOCK_MAX, 1,
522 					   AR0521_PIXEL_CLOCK_RATE);
523 
524 	/* Manual exposure time */
525 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 0,
526 					    65535, 1, 360);
527 
528 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl, ops,
529 					V4L2_CID_TEST_PATTERN,
530 					ARRAY_SIZE(test_pattern_menu) - 1,
531 					0, 0, test_pattern_menu);
532 
533 	if (hdl->error) {
534 		ret = hdl->error;
535 		goto free_ctrls;
536 	}
537 
538 	sensor->sd.ctrl_handler = hdl;
539 	return 0;
540 
541 free_ctrls:
542 	v4l2_ctrl_handler_free(hdl);
543 	return ret;
544 }
545 
546 #define REGS_ENTRY(a)	{(a), ARRAY_SIZE(a)}
547 #define REGS(...)	REGS_ENTRY(((const __be16[]){__VA_ARGS__}))
548 
549 static const struct initial_reg {
550 	const __be16 *data; /* data[0] is register address */
551 	unsigned int count;
552 } initial_regs[] = {
553 	REGS(be(0x0112), be(0x0808)), /* 8-bit/8-bit mode */
554 
555 	/* PEDESTAL+2 :+2 is a workaround for 10bit mode +0.5 rounding */
556 	REGS(be(0x301E), be(0x00AA)),
557 
558 	/* corrections_recommended_bayer */
559 	REGS(be(0x3042),
560 	     be(0x0004),  /* 3042: RNC: enable b/w rnc mode */
561 	     be(0x4580)), /* 3044: RNC: enable row noise correction */
562 
563 	REGS(be(0x30D2),
564 	     be(0x0000),  /* 30D2: CRM/CC: enable crm on Visible and CC rows */
565 	     be(0x0000),  /* 30D4: CC: CC enabled with 16 samples per column */
566 	     /* 30D6: CC: bw mode enabled/12 bit data resolution/bw mode */
567 	     be(0x2FFF)),
568 
569 	REGS(be(0x30DA),
570 	     be(0x0FFF),  /* 30DA: CC: column correction clip level 2 is 0 */
571 	     be(0x0FFF),  /* 30DC: CC: column correction clip level 3 is 0 */
572 	     be(0x0000)), /* 30DE: CC: Group FPN correction */
573 
574 	/* RNC: rnc scaling factor = * 54 / 64 (32 / 38 * 64 = 53.9) */
575 	REGS(be(0x30EE), be(0x1136)),
576 	REGS(be(0x30FA), be(0xFD00)), /* GPIO0 = flash, GPIO1 = shutter */
577 	REGS(be(0x3120), be(0x0005)), /* p1 dither enabled for 10bit mode */
578 	REGS(be(0x3172), be(0x0206)), /* txlo clk divider options */
579 	/* FDOC:fdoc settings with fdoc every frame turned of */
580 	REGS(be(0x3180), be(0x9434)),
581 
582 	REGS(be(0x31B0),
583 	     be(0x008B),  /* 31B0: frame_preamble - FIXME check WRT lanes# */
584 	     be(0x0050)), /* 31B2: line_preamble - FIXME check WRT lanes# */
585 
586 	/* don't use continuous clock mode while shut down */
587 	REGS(be(0x31BC), be(0x068C)),
588 	REGS(be(0x31E0), be(0x0781)), /* Fuse/2DDC: enable 2ddc */
589 
590 	/* analog_setup_recommended_10bit */
591 	REGS(be(0x341A), be(0x4735)), /* Samp&Hold pulse in ADC */
592 	REGS(be(0x3420), be(0x4735)), /* Samp&Hold pulse in ADC */
593 	REGS(be(0x3426), be(0x8A1A)), /* ADC offset distribution pulse */
594 	REGS(be(0x342A), be(0x0018)), /* pulse_config */
595 
596 	/* pixel_timing_recommended */
597 	REGS(be(0x3D00),
598 	     /* 3D00 */ be(0x043E), be(0x4760), be(0xFFFF), be(0xFFFF),
599 	     /* 3D08 */ be(0x8000), be(0x0510), be(0xAF08), be(0x0252),
600 	     /* 3D10 */ be(0x486F), be(0x5D5D), be(0x8056), be(0x8313),
601 	     /* 3D18 */ be(0x0087), be(0x6A48), be(0x6982), be(0x0280),
602 	     /* 3D20 */ be(0x8359), be(0x8D02), be(0x8020), be(0x4882),
603 	     /* 3D28 */ be(0x4269), be(0x6A95), be(0x5988), be(0x5A83),
604 	     /* 3D30 */ be(0x5885), be(0x6280), be(0x6289), be(0x6097),
605 	     /* 3D38 */ be(0x5782), be(0x605C), be(0xBF18), be(0x0961),
606 	     /* 3D40 */ be(0x5080), be(0x2090), be(0x4390), be(0x4382),
607 	     /* 3D48 */ be(0x5F8A), be(0x5D5D), be(0x9C63), be(0x8063),
608 	     /* 3D50 */ be(0xA960), be(0x9757), be(0x8260), be(0x5CFF),
609 	     /* 3D58 */ be(0xBF10), be(0x1681), be(0x0802), be(0x8000),
610 	     /* 3D60 */ be(0x141C), be(0x6000), be(0x6022), be(0x4D80),
611 	     /* 3D68 */ be(0x5C97), be(0x6A69), be(0xAC6F), be(0x4645),
612 	     /* 3D70 */ be(0x4400), be(0x0513), be(0x8069), be(0x6AC6),
613 	     /* 3D78 */ be(0x5F95), be(0x5F70), be(0x8040), be(0x4A81),
614 	     /* 3D80 */ be(0x0300), be(0xE703), be(0x0088), be(0x4A83),
615 	     /* 3D88 */ be(0x40FF), be(0xFFFF), be(0xFD70), be(0x8040),
616 	     /* 3D90 */ be(0x4A85), be(0x4FA8), be(0x4F8C), be(0x0070),
617 	     /* 3D98 */ be(0xBE47), be(0x8847), be(0xBC78), be(0x6B89),
618 	     /* 3DA0 */ be(0x6A80), be(0x6986), be(0x6B8E), be(0x6B80),
619 	     /* 3DA8 */ be(0x6980), be(0x6A88), be(0x7C9F), be(0x866B),
620 	     /* 3DB0 */ be(0x8765), be(0x46FF), be(0xE365), be(0xA679),
621 	     /* 3DB8 */ be(0x4A40), be(0x4580), be(0x44BC), be(0x7000),
622 	     /* 3DC0 */ be(0x8040), be(0x0802), be(0x10EF), be(0x0104),
623 	     /* 3DC8 */ be(0x3860), be(0x5D5D), be(0x5682), be(0x1300),
624 	     /* 3DD0 */ be(0x8648), be(0x8202), be(0x8082), be(0x598A),
625 	     /* 3DD8 */ be(0x0280), be(0x2048), be(0x3060), be(0x8042),
626 	     /* 3DE0 */ be(0x9259), be(0x865A), be(0x8258), be(0x8562),
627 	     /* 3DE8 */ be(0x8062), be(0x8560), be(0x9257), be(0x8221),
628 	     /* 3DF0 */ be(0x10FF), be(0xB757), be(0x9361), be(0x1019),
629 	     /* 3DF8 */ be(0x8020), be(0x9043), be(0x8E43), be(0x845F),
630 	     /* 3E00 */ be(0x835D), be(0x805D), be(0x8163), be(0x8063),
631 	     /* 3E08 */ be(0xA060), be(0x9157), be(0x8260), be(0x5CFF),
632 	     /* 3E10 */ be(0xFFFF), be(0xFFE5), be(0x1016), be(0x2048),
633 	     /* 3E18 */ be(0x0802), be(0x1C60), be(0x0014), be(0x0060),
634 	     /* 3E20 */ be(0x2205), be(0x8120), be(0x908F), be(0x6A80),
635 	     /* 3E28 */ be(0x6982), be(0x5F9F), be(0x6F46), be(0x4544),
636 	     /* 3E30 */ be(0x0005), be(0x8013), be(0x8069), be(0x6A80),
637 	     /* 3E38 */ be(0x7000), be(0x0000), be(0x0000), be(0x0000),
638 	     /* 3E40 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
639 	     /* 3E48 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
640 	     /* 3E50 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
641 	     /* 3E58 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
642 	     /* 3E60 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
643 	     /* 3E68 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
644 	     /* 3E70 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
645 	     /* 3E78 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
646 	     /* 3E80 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
647 	     /* 3E88 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
648 	     /* 3E90 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
649 	     /* 3E98 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
650 	     /* 3EA0 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
651 	     /* 3EA8 */ be(0x0000), be(0x0000), be(0x0000), be(0x0000),
652 	     /* 3EB0 */ be(0x0000), be(0x0000), be(0x0000)),
653 
654 	REGS(be(0x3EB6), be(0x004C)), /* ECL */
655 
656 	REGS(be(0x3EBA),
657 	     be(0xAAAD),  /* 3EBA */
658 	     be(0x0086)), /* 3EBC: Bias currents for FSC/ECL */
659 
660 	REGS(be(0x3EC0),
661 	     be(0x1E00),  /* 3EC0: SFbin/SH mode settings */
662 	     be(0x100A),  /* 3EC2: CLK divider for ramp for 10 bit 400MH */
663 	     /* 3EC4: FSC clamps for HDR mode and adc comp power down co */
664 	     be(0x3300),
665 	     be(0xEA44),  /* 3EC6: VLN and clk gating controls */
666 	     be(0x6F6F),  /* 3EC8: Txl0 and Txlo1 settings for normal mode */
667 	     be(0x2F4A),  /* 3ECA: CDAC/Txlo2/RSTGHI/RSTGLO settings */
668 	     be(0x0506),  /* 3ECC: RSTDHI/RSTDLO/CDAC/TXHI settings */
669 	     /* 3ECE: Ramp buffer settings and Booster enable (bits 0-5) */
670 	     be(0x203B),
671 	     be(0x13F0),  /* 3ED0: TXLO from atest/sf bin settings */
672 	     be(0xA53D),  /* 3ED2: Ramp offset */
673 	     be(0x862F),  /* 3ED4: TXLO open loop/row driver settings */
674 	     be(0x4081),  /* 3ED6: Txlatch fr cfpn rows/vln bias */
675 	     be(0x8003),  /* 3ED8: Ramp step setting for 10 bit 400 Mhz */
676 	     be(0xA580),  /* 3EDA: Ramp Offset */
677 	     be(0xC000),  /* 3EDC: over range for rst and under range for sig */
678 	     be(0xC103)), /* 3EDE: over range for sig and col dec clk settings */
679 
680 	/* corrections_recommended_bayer */
681 	REGS(be(0x3F00),
682 	     be(0x0017),  /* 3F00: BM_T0 */
683 	     be(0x02DD),  /* 3F02: BM_T1 */
684 	     /* 3F04: if Ana_gain less than 2, use noise_floor0, multipl */
685 	     be(0x0020),
686 	     /* 3F06: if Ana_gain between 4 and 7, use noise_floor2 and */
687 	     be(0x0040),
688 	     /* 3F08: if Ana_gain between 4 and 7, use noise_floor2 and */
689 	     be(0x0070),
690 	     /* 3F0A: Define noise_floor0(low address) and noise_floor1 */
691 	     be(0x0101),
692 	     be(0x0302)), /* 3F0C: Define noise_floor2 and noise_floor3 */
693 
694 	REGS(be(0x3F10),
695 	     be(0x0505),  /* 3F10: single k factor 0 */
696 	     be(0x0505),  /* 3F12: single k factor 1 */
697 	     be(0x0505),  /* 3F14: single k factor 2 */
698 	     be(0x01FF),  /* 3F16: cross factor 0 */
699 	     be(0x01FF),  /* 3F18: cross factor 1 */
700 	     be(0x01FF),  /* 3F1A: cross factor 2 */
701 	     be(0x0022)), /* 3F1E */
702 
703 	/* GTH_THRES_RTN: 4max,4min filtered out of every 46 samples and */
704 	REGS(be(0x3F2C), be(0x442E)),
705 
706 	REGS(be(0x3F3E),
707 	     be(0x0000),  /* 3F3E: Switch ADC from 12 bit to 10 bit mode */
708 	     be(0x1511),  /* 3F40: couple k factor 0 */
709 	     be(0x1511),  /* 3F42: couple k factor 1 */
710 	     be(0x0707)), /* 3F44: couple k factor 2 */
711 };
712 
713 static int ar0521_power_off(struct device *dev)
714 {
715 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
716 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
717 	int i;
718 
719 	clk_disable_unprepare(sensor->extclk);
720 
721 	if (sensor->reset_gpio)
722 		gpiod_set_value(sensor->reset_gpio, 1); /* assert RESET signal */
723 
724 	for (i = ARRAY_SIZE(ar0521_supply_names) - 1; i >= 0; i--) {
725 		if (sensor->supplies[i])
726 			regulator_disable(sensor->supplies[i]);
727 	}
728 	return 0;
729 }
730 
731 static int ar0521_power_on(struct device *dev)
732 {
733 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
734 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
735 	unsigned int cnt;
736 	int ret;
737 
738 	for (cnt = 0; cnt < ARRAY_SIZE(ar0521_supply_names); cnt++)
739 		if (sensor->supplies[cnt]) {
740 			ret = regulator_enable(sensor->supplies[cnt]);
741 			if (ret < 0)
742 				goto off;
743 
744 			usleep_range(1000, 1500); /* min 1 ms */
745 		}
746 
747 	ret = clk_prepare_enable(sensor->extclk);
748 	if (ret < 0) {
749 		v4l2_err(&sensor->sd, "error enabling sensor clock\n");
750 		goto off;
751 	}
752 	usleep_range(1000, 1500); /* min 1 ms */
753 
754 	if (sensor->reset_gpio)
755 		/* deassert RESET signal */
756 		gpiod_set_value(sensor->reset_gpio, 0);
757 	usleep_range(4500, 5000); /* min 45000 clocks */
758 
759 	for (cnt = 0; cnt < ARRAY_SIZE(initial_regs); cnt++)
760 		if (ar0521_write_regs(sensor, initial_regs[cnt].data,
761 				      initial_regs[cnt].count))
762 			goto off;
763 
764 	ret = ar0521_write_reg(sensor, AR0521_REG_SERIAL_FORMAT,
765 			       AR0521_REG_SERIAL_FORMAT_MIPI |
766 			       sensor->lane_count);
767 	if (ret)
768 		goto off;
769 
770 	/* set MIPI test mode - disabled for now */
771 	ret = ar0521_write_reg(sensor, AR0521_REG_HISPI_TEST_MODE,
772 			       ((0x40 << sensor->lane_count) - 0x40) |
773 			       AR0521_REG_HISPI_TEST_MODE_LP11);
774 	if (ret)
775 		goto off;
776 
777 	ret = ar0521_write_reg(sensor, AR0521_REG_ROW_SPEED, 0x110 |
778 			       4 / sensor->lane_count);
779 	if (ret)
780 		goto off;
781 
782 	return 0;
783 off:
784 	ar0521_power_off(dev);
785 	return ret;
786 }
787 
788 static int ar0521_enum_mbus_code(struct v4l2_subdev *sd,
789 				 struct v4l2_subdev_state *sd_state,
790 				 struct v4l2_subdev_mbus_code_enum *code)
791 {
792 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
793 
794 	if (code->index)
795 		return -EINVAL;
796 
797 	code->code = sensor->fmt.code;
798 	return 0;
799 }
800 
801 static int ar0521_pre_streamon(struct v4l2_subdev *sd, u32 flags)
802 {
803 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
804 	int ret;
805 
806 	if (!(flags & V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP))
807 		return -EACCES;
808 
809 	ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
810 	if (ret < 0)
811 		return ret;
812 
813 	/* Set LP-11 on clock and data lanes */
814 	ret = ar0521_write_reg(sensor, AR0521_REG_HISPI_CONTROL_STATUS,
815 			AR0521_REG_HISPI_CONTROL_STATUS_FRAMER_TEST_MODE_ENABLE);
816 	if (ret)
817 		goto err;
818 
819 	/* Start streaming LP-11 */
820 	ret = ar0521_write_reg(sensor, AR0521_REG_RESET,
821 			       AR0521_REG_RESET_DEFAULTS |
822 			       AR0521_REG_RESET_STREAM);
823 	if (ret)
824 		goto err;
825 	return 0;
826 
827 err:
828 	pm_runtime_put(&sensor->i2c_client->dev);
829 	return ret;
830 }
831 
832 static int ar0521_post_streamoff(struct v4l2_subdev *sd)
833 {
834 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
835 
836 	pm_runtime_put(&sensor->i2c_client->dev);
837 	return 0;
838 }
839 
840 static int ar0521_s_stream(struct v4l2_subdev *sd, int enable)
841 {
842 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
843 	int ret;
844 
845 	mutex_lock(&sensor->lock);
846 
847 	ret = ar0521_set_stream(sensor, enable);
848 	if (!ret)
849 		sensor->streaming = enable;
850 
851 	mutex_unlock(&sensor->lock);
852 	return ret;
853 }
854 
855 static const struct v4l2_subdev_core_ops ar0521_core_ops = {
856 	.log_status = v4l2_ctrl_subdev_log_status,
857 };
858 
859 static const struct v4l2_subdev_video_ops ar0521_video_ops = {
860 	.s_stream = ar0521_s_stream,
861 	.pre_streamon = ar0521_pre_streamon,
862 	.post_streamoff = ar0521_post_streamoff,
863 };
864 
865 static const struct v4l2_subdev_pad_ops ar0521_pad_ops = {
866 	.enum_mbus_code = ar0521_enum_mbus_code,
867 	.get_fmt = ar0521_get_fmt,
868 	.set_fmt = ar0521_set_fmt,
869 };
870 
871 static const struct v4l2_subdev_ops ar0521_subdev_ops = {
872 	.core = &ar0521_core_ops,
873 	.video = &ar0521_video_ops,
874 	.pad = &ar0521_pad_ops,
875 };
876 
877 static int __maybe_unused ar0521_suspend(struct device *dev)
878 {
879 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
880 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
881 
882 	if (sensor->streaming)
883 		ar0521_set_stream(sensor, 0);
884 
885 	return 0;
886 }
887 
888 static int __maybe_unused ar0521_resume(struct device *dev)
889 {
890 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
891 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
892 
893 	if (sensor->streaming)
894 		return ar0521_set_stream(sensor, 1);
895 
896 	return 0;
897 }
898 
899 static int ar0521_probe(struct i2c_client *client)
900 {
901 	struct v4l2_fwnode_endpoint ep = {
902 		.bus_type = V4L2_MBUS_CSI2_DPHY
903 	};
904 	struct device *dev = &client->dev;
905 	struct fwnode_handle *endpoint;
906 	struct ar0521_dev *sensor;
907 	unsigned int cnt;
908 	int ret;
909 
910 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
911 	if (!sensor)
912 		return -ENOMEM;
913 
914 	sensor->i2c_client = client;
915 	sensor->fmt.width = AR0521_WIDTH_MAX;
916 	sensor->fmt.height = AR0521_HEIGHT_MAX;
917 
918 	endpoint = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 0, 0,
919 						   FWNODE_GRAPH_ENDPOINT_NEXT);
920 	if (!endpoint) {
921 		dev_err(dev, "endpoint node not found\n");
922 		return -EINVAL;
923 	}
924 
925 	ret = v4l2_fwnode_endpoint_parse(endpoint, &ep);
926 	fwnode_handle_put(endpoint);
927 	if (ret) {
928 		dev_err(dev, "could not parse endpoint\n");
929 		return ret;
930 	}
931 
932 	if (ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
933 		dev_err(dev, "invalid bus type, must be MIPI CSI2\n");
934 		return -EINVAL;
935 	}
936 
937 	sensor->lane_count = ep.bus.mipi_csi2.num_data_lanes;
938 	switch (sensor->lane_count) {
939 	case 1:
940 	case 2:
941 	case 4:
942 		break;
943 	default:
944 		dev_err(dev, "invalid number of MIPI data lanes\n");
945 		return -EINVAL;
946 	}
947 
948 	/* Get master clock (extclk) */
949 	sensor->extclk = devm_clk_get(dev, "extclk");
950 	if (IS_ERR(sensor->extclk)) {
951 		dev_err(dev, "failed to get extclk\n");
952 		return PTR_ERR(sensor->extclk);
953 	}
954 
955 	sensor->extclk_freq = clk_get_rate(sensor->extclk);
956 
957 	if (sensor->extclk_freq < AR0521_EXTCLK_MIN ||
958 	    sensor->extclk_freq > AR0521_EXTCLK_MAX) {
959 		dev_err(dev, "extclk frequency out of range: %u Hz\n",
960 			sensor->extclk_freq);
961 		return -EINVAL;
962 	}
963 
964 	/* Request optional reset pin (usually active low) and assert it */
965 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
966 						     GPIOD_OUT_HIGH);
967 
968 	v4l2_i2c_subdev_init(&sensor->sd, client, &ar0521_subdev_ops);
969 
970 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
971 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
972 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
973 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
974 	if (ret)
975 		return ret;
976 
977 	for (cnt = 0; cnt < ARRAY_SIZE(ar0521_supply_names); cnt++) {
978 		struct regulator *supply = devm_regulator_get(dev,
979 						ar0521_supply_names[cnt]);
980 
981 		if (IS_ERR(supply)) {
982 			dev_info(dev, "no %s regulator found: %li\n",
983 				 ar0521_supply_names[cnt], PTR_ERR(supply));
984 			return PTR_ERR(supply);
985 		}
986 		sensor->supplies[cnt] = supply;
987 	}
988 
989 	mutex_init(&sensor->lock);
990 
991 	ret = ar0521_init_controls(sensor);
992 	if (ret)
993 		goto entity_cleanup;
994 
995 	ar0521_adj_fmt(&sensor->fmt);
996 
997 	ret = v4l2_async_register_subdev(&sensor->sd);
998 	if (ret)
999 		goto free_ctrls;
1000 
1001 	/* Turn on the device and enable runtime PM */
1002 	ret = ar0521_power_on(&client->dev);
1003 	if (ret)
1004 		goto disable;
1005 	pm_runtime_set_active(&client->dev);
1006 	pm_runtime_enable(&client->dev);
1007 	pm_runtime_idle(&client->dev);
1008 	return 0;
1009 
1010 disable:
1011 	v4l2_async_unregister_subdev(&sensor->sd);
1012 	media_entity_cleanup(&sensor->sd.entity);
1013 free_ctrls:
1014 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1015 entity_cleanup:
1016 	media_entity_cleanup(&sensor->sd.entity);
1017 	mutex_destroy(&sensor->lock);
1018 	return ret;
1019 }
1020 
1021 static void ar0521_remove(struct i2c_client *client)
1022 {
1023 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1024 	struct ar0521_dev *sensor = to_ar0521_dev(sd);
1025 
1026 	v4l2_async_unregister_subdev(&sensor->sd);
1027 	media_entity_cleanup(&sensor->sd.entity);
1028 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1029 	pm_runtime_disable(&client->dev);
1030 	if (!pm_runtime_status_suspended(&client->dev))
1031 		ar0521_power_off(&client->dev);
1032 	pm_runtime_set_suspended(&client->dev);
1033 	mutex_destroy(&sensor->lock);
1034 }
1035 
1036 static const struct dev_pm_ops ar0521_pm_ops = {
1037 	SET_SYSTEM_SLEEP_PM_OPS(ar0521_suspend, ar0521_resume)
1038 	SET_RUNTIME_PM_OPS(ar0521_power_off, ar0521_power_on, NULL)
1039 };
1040 static const struct of_device_id ar0521_dt_ids[] = {
1041 	{.compatible = "onnn,ar0521"},
1042 	{}
1043 };
1044 MODULE_DEVICE_TABLE(of, ar0521_dt_ids);
1045 
1046 static struct i2c_driver ar0521_i2c_driver = {
1047 	.driver = {
1048 		.name  = "ar0521",
1049 		.pm = &ar0521_pm_ops,
1050 		.of_match_table = ar0521_dt_ids,
1051 	},
1052 	.probe_new = ar0521_probe,
1053 	.remove = ar0521_remove,
1054 };
1055 
1056 module_i2c_driver(ar0521_i2c_driver);
1057 
1058 MODULE_DESCRIPTION("AR0521 MIPI Camera subdev driver");
1059 MODULE_AUTHOR("Krzysztof Hałasa <khalasa@piap.pl>");
1060 MODULE_LICENSE("GPL");
1061