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