xref: /linux/drivers/media/i2c/imx214.c (revision 1f2367a39f17bd553a75e179a747f9b257bc9478)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * imx214.c - imx214 sensor driver
4  *
5  * Copyright 2018 Qtechnology A/S
6  *
7  * Ricardo Ribalda <ricardo.ribalda@gmail.com>
8  */
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <media/media-entity.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-subdev.h>
21 
22 #define IMX214_DEFAULT_CLK_FREQ	24000000
23 #define IMX214_DEFAULT_LINK_FREQ 480000000
24 #define IMX214_DEFAULT_PIXEL_RATE ((IMX214_DEFAULT_LINK_FREQ * 8LL) / 10)
25 #define IMX214_FPS 30
26 #define IMX214_MBUS_CODE MEDIA_BUS_FMT_SRGGB10_1X10
27 
28 static const char * const imx214_supply_name[] = {
29 	"vdda",
30 	"vddd",
31 	"vdddo",
32 };
33 
34 #define IMX214_NUM_SUPPLIES ARRAY_SIZE(imx214_supply_name)
35 
36 struct imx214 {
37 	struct device *dev;
38 	struct clk *xclk;
39 	struct regmap *regmap;
40 
41 	struct v4l2_subdev sd;
42 	struct media_pad pad;
43 	struct v4l2_mbus_framefmt fmt;
44 	struct v4l2_rect crop;
45 
46 	struct v4l2_ctrl_handler ctrls;
47 	struct v4l2_ctrl *pixel_rate;
48 	struct v4l2_ctrl *link_freq;
49 	struct v4l2_ctrl *exposure;
50 
51 	struct regulator_bulk_data	supplies[IMX214_NUM_SUPPLIES];
52 
53 	struct gpio_desc *enable_gpio;
54 
55 	/*
56 	 * Serialize control access, get/set format, get selection
57 	 * and start streaming.
58 	 */
59 	struct mutex mutex;
60 
61 	bool streaming;
62 };
63 
64 struct reg_8 {
65 	u16 addr;
66 	u8 val;
67 };
68 
69 enum {
70 	IMX214_TABLE_WAIT_MS = 0,
71 	IMX214_TABLE_END,
72 	IMX214_MAX_RETRIES,
73 	IMX214_WAIT_MS
74 };
75 
76 /*From imx214_mode_tbls.h*/
77 static const struct reg_8 mode_4096x2304[] = {
78 	{0x0114, 0x03},
79 	{0x0220, 0x00},
80 	{0x0221, 0x11},
81 	{0x0222, 0x01},
82 	{0x0340, 0x0C},
83 	{0x0341, 0x7A},
84 	{0x0342, 0x13},
85 	{0x0343, 0x90},
86 	{0x0344, 0x00},
87 	{0x0345, 0x38},
88 	{0x0346, 0x01},
89 	{0x0347, 0x98},
90 	{0x0348, 0x10},
91 	{0x0349, 0x37},
92 	{0x034A, 0x0A},
93 	{0x034B, 0x97},
94 	{0x0381, 0x01},
95 	{0x0383, 0x01},
96 	{0x0385, 0x01},
97 	{0x0387, 0x01},
98 	{0x0900, 0x00},
99 	{0x0901, 0x00},
100 	{0x0902, 0x00},
101 	{0x3000, 0x35},
102 	{0x3054, 0x01},
103 	{0x305C, 0x11},
104 
105 	{0x0112, 0x0A},
106 	{0x0113, 0x0A},
107 	{0x034C, 0x10},
108 	{0x034D, 0x00},
109 	{0x034E, 0x09},
110 	{0x034F, 0x00},
111 	{0x0401, 0x00},
112 	{0x0404, 0x00},
113 	{0x0405, 0x10},
114 	{0x0408, 0x00},
115 	{0x0409, 0x00},
116 	{0x040A, 0x00},
117 	{0x040B, 0x00},
118 	{0x040C, 0x10},
119 	{0x040D, 0x00},
120 	{0x040E, 0x09},
121 	{0x040F, 0x00},
122 
123 	{0x0301, 0x05},
124 	{0x0303, 0x02},
125 	{0x0305, 0x03},
126 	{0x0306, 0x00},
127 	{0x0307, 0x96},
128 	{0x0309, 0x0A},
129 	{0x030B, 0x01},
130 	{0x0310, 0x00},
131 
132 	{0x0820, 0x12},
133 	{0x0821, 0xC0},
134 	{0x0822, 0x00},
135 	{0x0823, 0x00},
136 
137 	{0x3A03, 0x09},
138 	{0x3A04, 0x50},
139 	{0x3A05, 0x01},
140 
141 	{0x0B06, 0x01},
142 	{0x30A2, 0x00},
143 
144 	{0x30B4, 0x00},
145 
146 	{0x3A02, 0xFF},
147 
148 	{0x3011, 0x00},
149 	{0x3013, 0x01},
150 
151 	{0x0202, 0x0C},
152 	{0x0203, 0x70},
153 	{0x0224, 0x01},
154 	{0x0225, 0xF4},
155 
156 	{0x0204, 0x00},
157 	{0x0205, 0x00},
158 	{0x020E, 0x01},
159 	{0x020F, 0x00},
160 	{0x0210, 0x01},
161 	{0x0211, 0x00},
162 	{0x0212, 0x01},
163 	{0x0213, 0x00},
164 	{0x0214, 0x01},
165 	{0x0215, 0x00},
166 	{0x0216, 0x00},
167 	{0x0217, 0x00},
168 
169 	{0x4170, 0x00},
170 	{0x4171, 0x10},
171 	{0x4176, 0x00},
172 	{0x4177, 0x3C},
173 	{0xAE20, 0x04},
174 	{0xAE21, 0x5C},
175 
176 	{IMX214_TABLE_WAIT_MS, 10},
177 	{0x0138, 0x01},
178 	{IMX214_TABLE_END, 0x00}
179 };
180 
181 static const struct reg_8 mode_1920x1080[] = {
182 	{0x0114, 0x03},
183 	{0x0220, 0x00},
184 	{0x0221, 0x11},
185 	{0x0222, 0x01},
186 	{0x0340, 0x0C},
187 	{0x0341, 0x7A},
188 	{0x0342, 0x13},
189 	{0x0343, 0x90},
190 	{0x0344, 0x04},
191 	{0x0345, 0x78},
192 	{0x0346, 0x03},
193 	{0x0347, 0xFC},
194 	{0x0348, 0x0B},
195 	{0x0349, 0xF7},
196 	{0x034A, 0x08},
197 	{0x034B, 0x33},
198 	{0x0381, 0x01},
199 	{0x0383, 0x01},
200 	{0x0385, 0x01},
201 	{0x0387, 0x01},
202 	{0x0900, 0x00},
203 	{0x0901, 0x00},
204 	{0x0902, 0x00},
205 	{0x3000, 0x35},
206 	{0x3054, 0x01},
207 	{0x305C, 0x11},
208 
209 	{0x0112, 0x0A},
210 	{0x0113, 0x0A},
211 	{0x034C, 0x07},
212 	{0x034D, 0x80},
213 	{0x034E, 0x04},
214 	{0x034F, 0x38},
215 	{0x0401, 0x00},
216 	{0x0404, 0x00},
217 	{0x0405, 0x10},
218 	{0x0408, 0x00},
219 	{0x0409, 0x00},
220 	{0x040A, 0x00},
221 	{0x040B, 0x00},
222 	{0x040C, 0x07},
223 	{0x040D, 0x80},
224 	{0x040E, 0x04},
225 	{0x040F, 0x38},
226 
227 	{0x0301, 0x05},
228 	{0x0303, 0x02},
229 	{0x0305, 0x03},
230 	{0x0306, 0x00},
231 	{0x0307, 0x96},
232 	{0x0309, 0x0A},
233 	{0x030B, 0x01},
234 	{0x0310, 0x00},
235 
236 	{0x0820, 0x12},
237 	{0x0821, 0xC0},
238 	{0x0822, 0x00},
239 	{0x0823, 0x00},
240 
241 	{0x3A03, 0x04},
242 	{0x3A04, 0xF8},
243 	{0x3A05, 0x02},
244 
245 	{0x0B06, 0x01},
246 	{0x30A2, 0x00},
247 
248 	{0x30B4, 0x00},
249 
250 	{0x3A02, 0xFF},
251 
252 	{0x3011, 0x00},
253 	{0x3013, 0x01},
254 
255 	{0x0202, 0x0C},
256 	{0x0203, 0x70},
257 	{0x0224, 0x01},
258 	{0x0225, 0xF4},
259 
260 	{0x0204, 0x00},
261 	{0x0205, 0x00},
262 	{0x020E, 0x01},
263 	{0x020F, 0x00},
264 	{0x0210, 0x01},
265 	{0x0211, 0x00},
266 	{0x0212, 0x01},
267 	{0x0213, 0x00},
268 	{0x0214, 0x01},
269 	{0x0215, 0x00},
270 	{0x0216, 0x00},
271 	{0x0217, 0x00},
272 
273 	{0x4170, 0x00},
274 	{0x4171, 0x10},
275 	{0x4176, 0x00},
276 	{0x4177, 0x3C},
277 	{0xAE20, 0x04},
278 	{0xAE21, 0x5C},
279 
280 	{IMX214_TABLE_WAIT_MS, 10},
281 	{0x0138, 0x01},
282 	{IMX214_TABLE_END, 0x00}
283 };
284 
285 static const struct reg_8 mode_table_common[] = {
286 	/* software reset */
287 
288 	/* software standby settings */
289 	{0x0100, 0x00},
290 
291 	/* ATR setting */
292 	{0x9300, 0x02},
293 
294 	/* external clock setting */
295 	{0x0136, 0x18},
296 	{0x0137, 0x00},
297 
298 	/* global setting */
299 	/* basic config */
300 	{0x0101, 0x00},
301 	{0x0105, 0x01},
302 	{0x0106, 0x01},
303 	{0x4550, 0x02},
304 	{0x4601, 0x00},
305 	{0x4642, 0x05},
306 	{0x6227, 0x11},
307 	{0x6276, 0x00},
308 	{0x900E, 0x06},
309 	{0xA802, 0x90},
310 	{0xA803, 0x11},
311 	{0xA804, 0x62},
312 	{0xA805, 0x77},
313 	{0xA806, 0xAE},
314 	{0xA807, 0x34},
315 	{0xA808, 0xAE},
316 	{0xA809, 0x35},
317 	{0xA80A, 0x62},
318 	{0xA80B, 0x83},
319 	{0xAE33, 0x00},
320 
321 	/* analog setting */
322 	{0x4174, 0x00},
323 	{0x4175, 0x11},
324 	{0x4612, 0x29},
325 	{0x461B, 0x12},
326 	{0x461F, 0x06},
327 	{0x4635, 0x07},
328 	{0x4637, 0x30},
329 	{0x463F, 0x18},
330 	{0x4641, 0x0D},
331 	{0x465B, 0x12},
332 	{0x465F, 0x11},
333 	{0x4663, 0x11},
334 	{0x4667, 0x0F},
335 	{0x466F, 0x0F},
336 	{0x470E, 0x09},
337 	{0x4909, 0xAB},
338 	{0x490B, 0x95},
339 	{0x4915, 0x5D},
340 	{0x4A5F, 0xFF},
341 	{0x4A61, 0xFF},
342 	{0x4A73, 0x62},
343 	{0x4A85, 0x00},
344 	{0x4A87, 0xFF},
345 
346 	/* embedded data */
347 	{0x5041, 0x04},
348 	{0x583C, 0x04},
349 	{0x620E, 0x04},
350 	{0x6EB2, 0x01},
351 	{0x6EB3, 0x00},
352 	{0x9300, 0x02},
353 
354 	/* imagequality */
355 	/* HDR setting */
356 	{0x3001, 0x07},
357 	{0x6D12, 0x3F},
358 	{0x6D13, 0xFF},
359 	{0x9344, 0x03},
360 	{0x9706, 0x10},
361 	{0x9707, 0x03},
362 	{0x9708, 0x03},
363 	{0x9E04, 0x01},
364 	{0x9E05, 0x00},
365 	{0x9E0C, 0x01},
366 	{0x9E0D, 0x02},
367 	{0x9E24, 0x00},
368 	{0x9E25, 0x8C},
369 	{0x9E26, 0x00},
370 	{0x9E27, 0x94},
371 	{0x9E28, 0x00},
372 	{0x9E29, 0x96},
373 
374 	/* CNR parameter setting */
375 	{0x69DB, 0x01},
376 
377 	/* Moire reduction */
378 	{0x6957, 0x01},
379 
380 	/* image enhancement */
381 	{0x6987, 0x17},
382 	{0x698A, 0x03},
383 	{0x698B, 0x03},
384 
385 	/* white balanace */
386 	{0x0B8E, 0x01},
387 	{0x0B8F, 0x00},
388 	{0x0B90, 0x01},
389 	{0x0B91, 0x00},
390 	{0x0B92, 0x01},
391 	{0x0B93, 0x00},
392 	{0x0B94, 0x01},
393 	{0x0B95, 0x00},
394 
395 	/* ATR setting */
396 	{0x6E50, 0x00},
397 	{0x6E51, 0x32},
398 	{0x9340, 0x00},
399 	{0x9341, 0x3C},
400 	{0x9342, 0x03},
401 	{0x9343, 0xFF},
402 	{IMX214_TABLE_END, 0x00}
403 };
404 
405 /*
406  * Declare modes in order, from biggest
407  * to smallest height.
408  */
409 static const struct imx214_mode {
410 	u32 width;
411 	u32 height;
412 	const struct reg_8 *reg_table;
413 } imx214_modes[] = {
414 	{
415 		.width = 4096,
416 		.height = 2304,
417 		.reg_table = mode_4096x2304,
418 	},
419 	{
420 		.width = 1920,
421 		.height = 1080,
422 		.reg_table = mode_1920x1080,
423 	},
424 };
425 
426 static inline struct imx214 *to_imx214(struct v4l2_subdev *sd)
427 {
428 	return container_of(sd, struct imx214, sd);
429 }
430 
431 static int __maybe_unused imx214_power_on(struct device *dev)
432 {
433 	struct i2c_client *client = to_i2c_client(dev);
434 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
435 	struct imx214 *imx214 = to_imx214(sd);
436 	int ret;
437 
438 	ret = regulator_bulk_enable(IMX214_NUM_SUPPLIES, imx214->supplies);
439 	if (ret < 0) {
440 		dev_err(imx214->dev, "failed to enable regulators: %d\n", ret);
441 		return ret;
442 	}
443 
444 	usleep_range(2000, 3000);
445 
446 	ret = clk_prepare_enable(imx214->xclk);
447 	if (ret < 0) {
448 		regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies);
449 		dev_err(imx214->dev, "clk prepare enable failed\n");
450 		return ret;
451 	}
452 
453 	gpiod_set_value_cansleep(imx214->enable_gpio, 1);
454 	usleep_range(12000, 15000);
455 
456 	return 0;
457 }
458 
459 static int __maybe_unused imx214_power_off(struct device *dev)
460 {
461 	struct i2c_client *client = to_i2c_client(dev);
462 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
463 	struct imx214 *imx214 = to_imx214(sd);
464 
465 	gpiod_set_value_cansleep(imx214->enable_gpio, 0);
466 
467 	clk_disable_unprepare(imx214->xclk);
468 
469 	regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies);
470 	usleep_range(10, 20);
471 
472 	return 0;
473 }
474 
475 static int imx214_enum_mbus_code(struct v4l2_subdev *sd,
476 				 struct v4l2_subdev_pad_config *cfg,
477 				 struct v4l2_subdev_mbus_code_enum *code)
478 {
479 	if (code->index > 0)
480 		return -EINVAL;
481 
482 	code->code = IMX214_MBUS_CODE;
483 
484 	return 0;
485 }
486 
487 static int imx214_enum_frame_size(struct v4l2_subdev *subdev,
488 				  struct v4l2_subdev_pad_config *cfg,
489 				  struct v4l2_subdev_frame_size_enum *fse)
490 {
491 	if (fse->code != IMX214_MBUS_CODE)
492 		return -EINVAL;
493 
494 	if (fse->index >= ARRAY_SIZE(imx214_modes))
495 		return -EINVAL;
496 
497 	fse->min_width = fse->max_width = imx214_modes[fse->index].width;
498 	fse->min_height = fse->max_height = imx214_modes[fse->index].height;
499 
500 	return 0;
501 }
502 
503 #ifdef CONFIG_VIDEO_ADV_DEBUG
504 static int imx214_s_register(struct v4l2_subdev *subdev,
505 			     const struct v4l2_dbg_register *reg)
506 {
507 	struct imx214 *imx214 = container_of(subdev, struct imx214, sd);
508 
509 	return regmap_write(imx214->regmap, reg->reg, reg->val);
510 }
511 
512 static int imx214_g_register(struct v4l2_subdev *subdev,
513 			     struct v4l2_dbg_register *reg)
514 {
515 	struct imx214 *imx214 = container_of(subdev, struct imx214, sd);
516 	unsigned int aux;
517 	int ret;
518 
519 	reg->size = 1;
520 	ret = regmap_read(imx214->regmap, reg->reg, &aux);
521 	reg->val = aux;
522 
523 	return ret;
524 }
525 #endif
526 
527 static const struct v4l2_subdev_core_ops imx214_core_ops = {
528 #ifdef CONFIG_VIDEO_ADV_DEBUG
529 	.g_register = imx214_g_register,
530 	.s_register = imx214_s_register,
531 #endif
532 };
533 
534 static struct v4l2_mbus_framefmt *
535 __imx214_get_pad_format(struct imx214 *imx214,
536 			struct v4l2_subdev_pad_config *cfg,
537 			unsigned int pad,
538 			enum v4l2_subdev_format_whence which)
539 {
540 	switch (which) {
541 	case V4L2_SUBDEV_FORMAT_TRY:
542 		return v4l2_subdev_get_try_format(&imx214->sd, cfg, pad);
543 	case V4L2_SUBDEV_FORMAT_ACTIVE:
544 		return &imx214->fmt;
545 	default:
546 		return NULL;
547 	}
548 }
549 
550 static int imx214_get_format(struct v4l2_subdev *sd,
551 			     struct v4l2_subdev_pad_config *cfg,
552 			     struct v4l2_subdev_format *format)
553 {
554 	struct imx214 *imx214 = to_imx214(sd);
555 
556 	mutex_lock(&imx214->mutex);
557 	format->format = *__imx214_get_pad_format(imx214, cfg, format->pad,
558 						  format->which);
559 	mutex_unlock(&imx214->mutex);
560 
561 	return 0;
562 }
563 
564 static struct v4l2_rect *
565 __imx214_get_pad_crop(struct imx214 *imx214, struct v4l2_subdev_pad_config *cfg,
566 		      unsigned int pad, enum v4l2_subdev_format_whence which)
567 {
568 	switch (which) {
569 	case V4L2_SUBDEV_FORMAT_TRY:
570 		return v4l2_subdev_get_try_crop(&imx214->sd, cfg, pad);
571 	case V4L2_SUBDEV_FORMAT_ACTIVE:
572 		return &imx214->crop;
573 	default:
574 		return NULL;
575 	}
576 }
577 
578 static int imx214_set_format(struct v4l2_subdev *sd,
579 			     struct v4l2_subdev_pad_config *cfg,
580 			     struct v4l2_subdev_format *format)
581 {
582 	struct imx214 *imx214 = to_imx214(sd);
583 	struct v4l2_mbus_framefmt *__format;
584 	struct v4l2_rect *__crop;
585 	const struct imx214_mode *mode;
586 
587 	mutex_lock(&imx214->mutex);
588 
589 	__crop = __imx214_get_pad_crop(imx214, cfg, format->pad, format->which);
590 
591 	if (format)
592 		mode = v4l2_find_nearest_size(imx214_modes,
593 				ARRAY_SIZE(imx214_modes), width, height,
594 				format->format.width, format->format.height);
595 	else
596 		mode = &imx214_modes[0];
597 
598 	__crop->width = mode->width;
599 	__crop->height = mode->height;
600 
601 	__format = __imx214_get_pad_format(imx214, cfg, format->pad,
602 					   format->which);
603 	__format->width = __crop->width;
604 	__format->height = __crop->height;
605 	__format->code = IMX214_MBUS_CODE;
606 	__format->field = V4L2_FIELD_NONE;
607 	__format->colorspace = V4L2_COLORSPACE_SRGB;
608 	__format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
609 	__format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
610 				__format->colorspace, __format->ycbcr_enc);
611 	__format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
612 
613 	format->format = *__format;
614 
615 	mutex_unlock(&imx214->mutex);
616 
617 	return 0;
618 }
619 
620 static int imx214_get_selection(struct v4l2_subdev *sd,
621 				struct v4l2_subdev_pad_config *cfg,
622 				struct v4l2_subdev_selection *sel)
623 {
624 	struct imx214 *imx214 = to_imx214(sd);
625 
626 	if (sel->target != V4L2_SEL_TGT_CROP)
627 		return -EINVAL;
628 
629 	mutex_lock(&imx214->mutex);
630 	sel->r = *__imx214_get_pad_crop(imx214, cfg, sel->pad,
631 					sel->which);
632 	mutex_unlock(&imx214->mutex);
633 	return 0;
634 }
635 
636 static int imx214_entity_init_cfg(struct v4l2_subdev *subdev,
637 				  struct v4l2_subdev_pad_config *cfg)
638 {
639 	struct v4l2_subdev_format fmt = { };
640 
641 	fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
642 	fmt.format.width = imx214_modes[0].width;
643 	fmt.format.height = imx214_modes[0].height;
644 
645 	imx214_set_format(subdev, cfg, &fmt);
646 
647 	return 0;
648 }
649 
650 static int imx214_set_ctrl(struct v4l2_ctrl *ctrl)
651 {
652 	struct imx214 *imx214 = container_of(ctrl->handler,
653 					     struct imx214, ctrls);
654 	u8 vals[2];
655 	int ret;
656 
657 	/*
658 	 * Applying V4L2 control value only happens
659 	 * when power is up for streaming
660 	 */
661 	if (!pm_runtime_get_if_in_use(imx214->dev))
662 		return 0;
663 
664 	switch (ctrl->id) {
665 	case V4L2_CID_EXPOSURE:
666 		vals[1] = ctrl->val;
667 		vals[0] = ctrl->val >> 8;
668 		ret = regmap_bulk_write(imx214->regmap, 0x202, vals, 2);
669 		if (ret < 0)
670 			dev_err(imx214->dev, "Error %d\n", ret);
671 		ret = 0;
672 		break;
673 
674 	default:
675 		ret = -EINVAL;
676 	}
677 
678 	pm_runtime_put(imx214->dev);
679 
680 	return ret;
681 }
682 
683 static const struct v4l2_ctrl_ops imx214_ctrl_ops = {
684 	.s_ctrl = imx214_set_ctrl,
685 };
686 
687 #define MAX_CMD 4
688 static int imx214_write_table(struct imx214 *imx214,
689 			      const struct reg_8 table[])
690 {
691 	u8 vals[MAX_CMD];
692 	int i;
693 	int ret;
694 
695 	for (; table->addr != IMX214_TABLE_END ; table++) {
696 		if (table->addr == IMX214_TABLE_WAIT_MS) {
697 			usleep_range(table->val * 1000,
698 				     table->val * 1000 + 500);
699 			continue;
700 		}
701 
702 		for (i = 0; i < MAX_CMD; i++) {
703 			if (table[i].addr != (table[0].addr + i))
704 				break;
705 			vals[i] = table[i].val;
706 		}
707 
708 		ret = regmap_bulk_write(imx214->regmap, table->addr, vals, i);
709 
710 		if (ret) {
711 			dev_err(imx214->dev, "write_table error: %d\n", ret);
712 			return ret;
713 		}
714 
715 		table += i - 1;
716 	}
717 
718 	return 0;
719 }
720 
721 static int imx214_start_streaming(struct imx214 *imx214)
722 {
723 	const struct imx214_mode *mode;
724 	int ret;
725 
726 	mutex_lock(&imx214->mutex);
727 	ret = imx214_write_table(imx214, mode_table_common);
728 	if (ret < 0) {
729 		dev_err(imx214->dev, "could not sent common table %d\n", ret);
730 		goto error;
731 	}
732 
733 	mode = v4l2_find_nearest_size(imx214_modes,
734 				ARRAY_SIZE(imx214_modes), width, height,
735 				imx214->fmt.width, imx214->fmt.height);
736 	ret = imx214_write_table(imx214, mode->reg_table);
737 	if (ret < 0) {
738 		dev_err(imx214->dev, "could not sent mode table %d\n", ret);
739 		goto error;
740 	}
741 	ret = __v4l2_ctrl_handler_setup(&imx214->ctrls);
742 	if (ret < 0) {
743 		dev_err(imx214->dev, "could not sync v4l2 controls\n");
744 		goto error;
745 	}
746 	ret = regmap_write(imx214->regmap, 0x100, 1);
747 	if (ret < 0) {
748 		dev_err(imx214->dev, "could not sent start table %d\n", ret);
749 		goto error;
750 	}
751 
752 	mutex_unlock(&imx214->mutex);
753 	return 0;
754 
755 error:
756 	mutex_unlock(&imx214->mutex);
757 	return ret;
758 }
759 
760 static int imx214_stop_streaming(struct imx214 *imx214)
761 {
762 	int ret;
763 
764 	ret = regmap_write(imx214->regmap, 0x100, 0);
765 	if (ret < 0)
766 		dev_err(imx214->dev, "could not sent stop table %d\n",	ret);
767 
768 	return ret;
769 }
770 
771 static int imx214_s_stream(struct v4l2_subdev *subdev, int enable)
772 {
773 	struct imx214 *imx214 = to_imx214(subdev);
774 	int ret;
775 
776 	if (imx214->streaming == enable)
777 		return 0;
778 
779 	if (enable) {
780 		ret = pm_runtime_get_sync(imx214->dev);
781 		if (ret < 0) {
782 			pm_runtime_put_noidle(imx214->dev);
783 			return ret;
784 		}
785 
786 		ret = imx214_start_streaming(imx214);
787 		if (ret < 0)
788 			goto err_rpm_put;
789 	} else {
790 		ret = imx214_start_streaming(imx214);
791 		if (ret < 0)
792 			goto err_rpm_put;
793 		pm_runtime_put(imx214->dev);
794 	}
795 
796 	imx214->streaming = enable;
797 	return 0;
798 
799 err_rpm_put:
800 	pm_runtime_put(imx214->dev);
801 	return ret;
802 }
803 
804 static int imx214_g_frame_interval(struct v4l2_subdev *subdev,
805 				   struct v4l2_subdev_frame_interval *fival)
806 {
807 	fival->pad = 0;
808 	fival->interval.numerator = 1;
809 	fival->interval.denominator = IMX214_FPS;
810 
811 	return 0;
812 }
813 
814 static int imx214_enum_frame_interval(struct v4l2_subdev *subdev,
815 				struct v4l2_subdev_pad_config *cfg,
816 				struct v4l2_subdev_frame_interval_enum *fie)
817 {
818 	const struct imx214_mode *mode;
819 
820 	if (fie->index != 0)
821 		return -EINVAL;
822 
823 	mode = v4l2_find_nearest_size(imx214_modes,
824 				ARRAY_SIZE(imx214_modes), width, height,
825 				fie->width, fie->height);
826 
827 	fie->code = IMX214_MBUS_CODE;
828 	fie->width = mode->width;
829 	fie->height = mode->height;
830 	fie->interval.numerator = 1;
831 	fie->interval.denominator = IMX214_FPS;
832 
833 	return 0;
834 }
835 
836 static const struct v4l2_subdev_video_ops imx214_video_ops = {
837 	.s_stream = imx214_s_stream,
838 	.g_frame_interval = imx214_g_frame_interval,
839 	.s_frame_interval = imx214_g_frame_interval,
840 };
841 
842 static const struct v4l2_subdev_pad_ops imx214_subdev_pad_ops = {
843 	.enum_mbus_code = imx214_enum_mbus_code,
844 	.enum_frame_size = imx214_enum_frame_size,
845 	.enum_frame_interval = imx214_enum_frame_interval,
846 	.get_fmt = imx214_get_format,
847 	.set_fmt = imx214_set_format,
848 	.get_selection = imx214_get_selection,
849 	.init_cfg = imx214_entity_init_cfg,
850 };
851 
852 static const struct v4l2_subdev_ops imx214_subdev_ops = {
853 	.core = &imx214_core_ops,
854 	.video = &imx214_video_ops,
855 	.pad = &imx214_subdev_pad_ops,
856 };
857 
858 static const struct regmap_config sensor_regmap_config = {
859 	.reg_bits = 16,
860 	.val_bits = 8,
861 	.cache_type = REGCACHE_RBTREE,
862 };
863 
864 static int imx214_get_regulators(struct device *dev, struct imx214 *imx214)
865 {
866 	unsigned int i;
867 
868 	for (i = 0; i < IMX214_NUM_SUPPLIES; i++)
869 		imx214->supplies[i].supply = imx214_supply_name[i];
870 
871 	return devm_regulator_bulk_get(dev, IMX214_NUM_SUPPLIES,
872 				       imx214->supplies);
873 }
874 
875 static int imx214_parse_fwnode(struct device *dev)
876 {
877 	struct fwnode_handle *endpoint;
878 	struct v4l2_fwnode_endpoint bus_cfg = {
879 		.bus_type = V4L2_MBUS_CSI2_DPHY,
880 	};
881 	unsigned int i;
882 	int ret;
883 
884 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
885 	if (!endpoint) {
886 		dev_err(dev, "endpoint node not found\n");
887 		return -EINVAL;
888 	}
889 
890 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
891 	if (ret) {
892 		dev_err(dev, "parsing endpoint node failed\n");
893 		goto done;
894 	}
895 
896 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
897 		if (bus_cfg.link_frequencies[i] == IMX214_DEFAULT_LINK_FREQ)
898 			break;
899 
900 	if (i == bus_cfg.nr_of_link_frequencies) {
901 		dev_err(dev, "link-frequencies %d not supported, Please review your DT\n",
902 			IMX214_DEFAULT_LINK_FREQ);
903 		ret = -EINVAL;
904 		goto done;
905 	}
906 
907 done:
908 	v4l2_fwnode_endpoint_free(&bus_cfg);
909 	fwnode_handle_put(endpoint);
910 	return ret;
911 }
912 
913 static int __maybe_unused imx214_suspend(struct device *dev)
914 {
915 	struct i2c_client *client = to_i2c_client(dev);
916 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
917 	struct imx214 *imx214 = to_imx214(sd);
918 
919 	if (imx214->streaming)
920 		imx214_stop_streaming(imx214);
921 
922 	return 0;
923 }
924 
925 static int __maybe_unused imx214_resume(struct device *dev)
926 {
927 	struct i2c_client *client = to_i2c_client(dev);
928 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
929 	struct imx214 *imx214 = to_imx214(sd);
930 	int ret;
931 
932 	if (imx214->streaming) {
933 		ret = imx214_start_streaming(imx214);
934 		if (ret)
935 			goto error;
936 	}
937 
938 	return 0;
939 
940 error:
941 	imx214_stop_streaming(imx214);
942 	imx214->streaming = 0;
943 	return ret;
944 }
945 
946 static int imx214_probe(struct i2c_client *client)
947 {
948 	struct device *dev = &client->dev;
949 	struct imx214 *imx214;
950 	static const s64 link_freq[] = {
951 		IMX214_DEFAULT_LINK_FREQ,
952 	};
953 	int ret;
954 
955 	ret = imx214_parse_fwnode(dev);
956 	if (ret)
957 		return ret;
958 
959 	imx214 = devm_kzalloc(dev, sizeof(*imx214), GFP_KERNEL);
960 	if (!imx214)
961 		return -ENOMEM;
962 
963 	imx214->dev = dev;
964 
965 	imx214->xclk = devm_clk_get(dev, NULL);
966 	if (IS_ERR(imx214->xclk)) {
967 		dev_err(dev, "could not get xclk");
968 		return PTR_ERR(imx214->xclk);
969 	}
970 
971 	ret = clk_set_rate(imx214->xclk, IMX214_DEFAULT_CLK_FREQ);
972 	if (ret) {
973 		dev_err(dev, "could not set xclk frequency\n");
974 		return ret;
975 	}
976 
977 	ret = imx214_get_regulators(dev, imx214);
978 	if (ret < 0) {
979 		dev_err(dev, "cannot get regulators\n");
980 		return ret;
981 	}
982 
983 	imx214->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
984 	if (IS_ERR(imx214->enable_gpio)) {
985 		dev_err(dev, "cannot get enable gpio\n");
986 		return PTR_ERR(imx214->enable_gpio);
987 	}
988 
989 	imx214->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
990 	if (IS_ERR(imx214->regmap)) {
991 		dev_err(dev, "regmap init failed\n");
992 		return PTR_ERR(imx214->regmap);
993 	}
994 
995 	v4l2_i2c_subdev_init(&imx214->sd, client, &imx214_subdev_ops);
996 
997 	/*
998 	 * Enable power initially, to avoid warnings
999 	 * from clk_disable on power_off
1000 	 */
1001 	imx214_power_on(imx214->dev);
1002 
1003 	pm_runtime_set_active(imx214->dev);
1004 	pm_runtime_enable(imx214->dev);
1005 	pm_runtime_idle(imx214->dev);
1006 
1007 	v4l2_ctrl_handler_init(&imx214->ctrls, 3);
1008 
1009 	imx214->pixel_rate = v4l2_ctrl_new_std(&imx214->ctrls, NULL,
1010 					       V4L2_CID_PIXEL_RATE, 0,
1011 					       IMX214_DEFAULT_PIXEL_RATE, 1,
1012 					       IMX214_DEFAULT_PIXEL_RATE);
1013 	imx214->link_freq = v4l2_ctrl_new_int_menu(&imx214->ctrls, NULL,
1014 						   V4L2_CID_LINK_FREQ,
1015 						   ARRAY_SIZE(link_freq) - 1,
1016 						   0, link_freq);
1017 	if (imx214->link_freq)
1018 		imx214->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1019 
1020 	/*
1021 	 * WARNING!
1022 	 * Values obtained reverse engineering blobs and/or devices.
1023 	 * Ranges and functionality might be wrong.
1024 	 *
1025 	 * Sony, please release some register set documentation for the
1026 	 * device.
1027 	 *
1028 	 * Yours sincerely, Ricardo.
1029 	 */
1030 	imx214->exposure = v4l2_ctrl_new_std(&imx214->ctrls, &imx214_ctrl_ops,
1031 					     V4L2_CID_EXPOSURE,
1032 					     0, 3184, 1, 0x0c70);
1033 
1034 	ret = imx214->ctrls.error;
1035 	if (ret) {
1036 		dev_err(&client->dev, "%s control init failed (%d)\n",
1037 			__func__, ret);
1038 		goto free_ctrl;
1039 	}
1040 
1041 	imx214->sd.ctrl_handler = &imx214->ctrls;
1042 	mutex_init(&imx214->mutex);
1043 	imx214->ctrls.lock = &imx214->mutex;
1044 
1045 	imx214->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1046 	imx214->pad.flags = MEDIA_PAD_FL_SOURCE;
1047 	imx214->sd.dev = &client->dev;
1048 	imx214->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1049 
1050 	ret = media_entity_pads_init(&imx214->sd.entity, 1, &imx214->pad);
1051 	if (ret < 0) {
1052 		dev_err(dev, "could not register media entity\n");
1053 		goto free_ctrl;
1054 	}
1055 
1056 	imx214_entity_init_cfg(&imx214->sd, NULL);
1057 
1058 	ret = v4l2_async_register_subdev_sensor_common(&imx214->sd);
1059 	if (ret < 0) {
1060 		dev_err(dev, "could not register v4l2 device\n");
1061 		goto free_entity;
1062 	}
1063 
1064 	return 0;
1065 
1066 free_entity:
1067 	media_entity_cleanup(&imx214->sd.entity);
1068 free_ctrl:
1069 	mutex_destroy(&imx214->mutex);
1070 	v4l2_ctrl_handler_free(&imx214->ctrls);
1071 	pm_runtime_disable(imx214->dev);
1072 
1073 	return ret;
1074 }
1075 
1076 static int imx214_remove(struct i2c_client *client)
1077 {
1078 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1079 	struct imx214 *imx214 = to_imx214(sd);
1080 
1081 	v4l2_async_unregister_subdev(&imx214->sd);
1082 	media_entity_cleanup(&imx214->sd.entity);
1083 	v4l2_ctrl_handler_free(&imx214->ctrls);
1084 
1085 	pm_runtime_disable(&client->dev);
1086 	pm_runtime_set_suspended(&client->dev);
1087 
1088 	mutex_destroy(&imx214->mutex);
1089 
1090 	return 0;
1091 }
1092 
1093 static const struct of_device_id imx214_of_match[] = {
1094 	{ .compatible = "sony,imx214" },
1095 	{ }
1096 };
1097 MODULE_DEVICE_TABLE(of, imx214_of_match);
1098 
1099 static const struct dev_pm_ops imx214_pm_ops = {
1100 	SET_SYSTEM_SLEEP_PM_OPS(imx214_suspend, imx214_resume)
1101 	SET_RUNTIME_PM_OPS(imx214_power_off, imx214_power_on, NULL)
1102 };
1103 
1104 static struct i2c_driver imx214_i2c_driver = {
1105 	.driver = {
1106 		.of_match_table = imx214_of_match,
1107 		.pm = &imx214_pm_ops,
1108 		.name  = "imx214",
1109 	},
1110 	.probe_new  = imx214_probe,
1111 	.remove = imx214_remove,
1112 };
1113 
1114 module_i2c_driver(imx214_i2c_driver);
1115 
1116 MODULE_DESCRIPTION("Sony IMX214 Camera drier");
1117 MODULE_AUTHOR("Ricardo Ribalda <ricardo.ribalda@gmail.com>");
1118 MODULE_LICENSE("GPL v2");
1119