aboutsummaryrefslogtreecommitdiff
path: root/drivers/media/i2c/imx296.c
blob: 6f92f7b4a86fb330215b4ba4788f3a8409535468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// SPDX-License-Identifier: GPL-2.0
/*
 * Sony IMX296 CMOS Image Sensor Driver
 *
 * Copyright (C) 2019 FRAMOS GmbH.
 *
 * Copyright (C) 2019 Linaro Ltd.
 * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <media/media-entity.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-subdev.h>

#define IMX296_STANDBY 0x3000
#define IMX296_REGHOLD 0x3008
#define IMX296_XMSTA 0x300a
#define IMX296_GAIN_LOW 0x3204
#define IMX296_GAIN_HIGH 0x3205

#define IMX296_DEFAULT_FORMAT MEDIA_BUS_FMT_SRGGB10_1X10

static const char * const imx296_supply_name[] = {
	"vdda",
	"vddd",
	"vddo",
};

#define IMX296_NUM_SUPPLIES ARRAY_SIZE(imx296_supply_name)

struct imx296_regval {
	u16 reg;
	u8 val;
};

struct imx296_mode {
	u32 width;
	u32 height;
	u32 pixel_rate;

	const struct imx296_regval *data;
	u32 data_size;
};

struct imx296 {
	struct device *dev;
	struct clk *mclk;
	struct regmap *regmap;

	struct v4l2_subdev sd;
	struct v4l2_fwnode_endpoint *ep;
	struct media_pad pad;
	struct v4l2_mbus_framefmt current_format;
	const struct imx296_mode *current_mode;

	struct regulator_bulk_data supplies[IMX296_NUM_SUPPLIES];
	struct gpio_desc *rst_gpio;

	struct v4l2_ctrl_handler ctrls;
	struct v4l2_ctrl *pixel_rate;

	struct mutex lock;
};

static const struct regmap_config imx296_regmap_config = {
	.reg_bits = 16,
	.val_bits = 8,
	.cache_type = REGCACHE_RBTREE,
};

static const struct imx296_regval imx296_global_init_settings[] = {
	{ 0x3a00, 0x80 },
	{ 0x3005, 0xf0 },
	{ 0x350b, 0x0f },
	{ 0x300d, 0x00 },
	{ 0x400e, 0x58 },
	{ 0x3010, 0x5e },
	{ 0x3011, 0x04 },
	{ 0x3014, 0x4c },
	{ 0x4014, 0x1c },
	{ 0x3015, 0x04 },
	{ 0x3516, 0x77 },
	{ 0x321a, 0x00 },
	{ 0x3226, 0x02 },
	{ 0x3832, 0xf5 },
	{ 0x3833, 0x00 },
	{ 0x3541, 0x72 },
	{ 0x4041, 0x2a },
	{ 0x3d48, 0xa3 },
	{ 0x3d49, 0x00 },
	{ 0x3d4a, 0x85 },
	{ 0x3d4b, 0x00 },
	{ 0x3256, 0x01 },
	{ 0x3758, 0xa3 },
	{ 0x3759, 0x00 },
	{ 0x375a, 0x85 },
	{ 0x375b, 0x00 },
	{ 0x3165, 0x00 },
	{ 0x3169, 0x10 },
	{ 0x316a, 0x02 },
	{ 0x4174, 0x00 },
	{ 0x3079, 0x08 },
	{ 0x3090, 0x04 },
	{ 0x3094, 0x04 },
	{ 0x3098, 0x04 },
	{ 0x309e, 0x04 },
	{ 0x30a0, 0x04 },
	{ 0x30a1, 0x3c },
	{ 0x38a2, 0xf6 },
	{ 0x40a2, 0x06 },
	{ 0x38a3, 0x00 },
	{ 0x30a4, 0x5f },
	{ 0x30a8, 0x91 },
	{ 0x30ac, 0x28 },
	{ 0x30af, 0x09 },
	{ 0x40c1, 0xf6 },
	{ 0x40c7, 0x0f },
	{ 0x31c8, 0xf3 },
	{ 0x40c8, 0x00 },
	{ 0x31d0, 0xf4 },
	{ 0x30df, 0x00 },
};

static const struct imx296_regval imx296_1440_1088_settings[] = {
	{ 0x300d, 0x00 },
	{ 0x3010, 0xcb },
	{ 0x3011, 0x08 },
	{ 0x3014, 0x4c },
	{ 0x3015, 0x04 },
	{ 0x3300, 0x03 },
	{ 0x3310, 0x08 },
	{ 0x3311, 0x00 },
	{ 0x3312, 0x00 },
	{ 0x3313, 0x00 },
	{ 0x3314, 0xa0 },
	{ 0x3315, 0x05 },
	{ 0x3316, 0x40 },
	{ 0x3317, 0x04 },

	{ 0x3204, 0x00 },
	{ 0x3205, 0x00 },
	{ 0x3212, 0x08 },
	{ 0x3254, 0x3c },
	{ 0x3255, 0x00 },
	{ 0x3089, 0x80 },
	{ 0x308a, 0x0b },
	{ 0x308b, 0x80 },
	{ 0x308c, 0x08 },
	{ 0x418c, 0x74 },
	{ 0x308d, 0x0e },
	{ 0x308e, 0x00 },
	{ 0x308f, 0x00 },
};

/* Mode configs */
static const struct imx296_mode imx296_modes[] = {
	{
		.width = 1440,
		.height = 1088,
		.data = imx296_1440_1088_settings,
		.data_size = ARRAY_SIZE(imx296_1440_1088_settings),
		.pixel_rate = 80000000,
	},
};

static inline struct imx296 *to_imx296(struct v4l2_subdev *_sd)
{
	return container_of(_sd, struct imx296, sd);
}

static inline int imx296_read_reg(struct imx296 *imx296, u16 addr, u8 *value)
{
	unsigned int regval;
	int ret;

	ret = regmap_read(imx296->regmap, addr, &regval);
	if (ret) {
		dev_err(imx296->dev, "I2C read failed for addr: %x\n", addr);
		return ret;
	}

	*value = regval & 0xff;

	return 0;
}

static int imx296_write_reg(struct imx296 *imx296, u16 addr, u8 value)
{
	int ret;

	ret = regmap_write(imx296->regmap, addr, value);
	if (ret) {
		dev_err(imx296->dev, "I2C write failed for addr: %x\n", addr);
		return ret;
	}

	return ret;
}

static int imx296_set_register_array(struct imx296 *imx296,
				     const struct imx296_regval *settings,
				     unsigned int num_settings)
{
	unsigned int i;
	int ret;

	for (i = 0; i < num_settings; ++i, ++settings) {
		ret = imx296_write_reg(imx296, settings->reg, settings->val);
		if (ret < 0)
			return ret;

		/* Settle time is 10ms for all registers */
		msleep(10);
	}

	return 0;
}

static int imx296_write_buffered_reg(struct imx296 *imx296, u16 address_low,
				     u8 nr_regs, u32 value)
{
	unsigned int i;
	int ret;

	ret = imx296_write_reg(imx296, IMX296_REGHOLD, 0x01);
	if (ret) {
		dev_err(imx296->dev, "Error setting hold register\n");
		return ret;
	}

	for (i = 0; i < nr_regs; i++) {
		ret = imx296_write_reg(imx296, address_low + i,
				       (u8)(value >> (i * 8)));
		if (ret) {
			dev_err(imx296->dev, "Error writing buffered registers\n");
			return ret;
		}
	}

	ret = imx296_write_reg(imx296, IMX296_REGHOLD, 0x00);
	if (ret) {
		dev_err(imx296->dev, "Error setting hold register\n");
		return ret;
	}

	return ret;
}

static int imx296_set_gain(struct imx296 *imx296, u32 value)
{
	int ret;

	ret = imx296_write_buffered_reg(imx296, IMX296_GAIN_HIGH, 1,
					(u8)((value >> 8) & 0xFF));
	if (ret < 0)
		return ret;

	ret = imx296_write_buffered_reg(imx296, IMX296_GAIN_HIGH, 1,
					(u8)((value) & 0xFF));
	if (ret < 0)
		return ret;

	return 0;
}

/* Stop streaming */
static int imx296_stop_streaming(struct imx296 *imx296)
{
	int ret;

	ret = imx296_write_reg(imx296, IMX296_STANDBY, 0x01);
	if (ret < 0)
		return ret;

	msleep(30);

	return imx296_write_reg(imx296, IMX296_XMSTA, 0x01);
}

static int imx296_set_ctrl(struct v4l2_ctrl *ctrl)
{
	struct imx296 *imx296 = container_of(ctrl->handler,
					     struct imx296, ctrls);
	int ret = 0;

	/* V4L2 controls values will be applied only when power is already up */
	if (!pm_runtime_get_if_in_use(imx296->dev))
		return 0;

	switch (ctrl->id) {
	case V4L2_CID_GAIN:
		ret = imx296_set_gain(imx296, ctrl->val);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	pm_runtime_put(imx296->dev);

	return ret;
}

static const struct v4l2_ctrl_ops imx296_ctrl_ops = {
	.s_ctrl = imx296_set_ctrl,
};

static int imx296_enum_mbus_code(struct v4l2_subdev *sd,
				 struct v4l2_subdev_pad_config *cfg,
				 struct v4l2_subdev_mbus_code_enum *code)
{
	code->code = IMX296_DEFAULT_FORMAT;

	return 0;
}

static int imx296_get_fmt(struct v4l2_subdev *sd,
			  struct v4l2_subdev_pad_config *cfg,
			  struct v4l2_subdev_format *fmt)
{
	struct imx296 *imx296 = to_imx296(sd);
	struct v4l2_mbus_framefmt *framefmt;

	mutex_lock(&imx296->lock);

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
		framefmt = v4l2_subdev_get_try_format(&imx296->sd, cfg,
						      fmt->pad);
	else
		framefmt = &imx296->current_format;

	fmt->format = *framefmt;

	mutex_unlock(&imx296->lock);

	return 0;
}

static const struct imx296_mode *
imx296_find_nearest_mode(unsigned int width, unsigned int height)
{
	unsigned int max_dist_match = (unsigned int) - 1;
	int i, n = 0;

	for (i = 0; i < ARRAY_SIZE(imx296_modes); i++) {
		unsigned int dist = min(width, imx296_modes[i].width)
				* min(height, imx296_modes[i].height);
		dist = imx296_modes[i].width * imx296_modes[i].height +
			width * height - 2 * dist;
		if (dist < max_dist_match) {
			n = i;
			max_dist_match = dist;
		}
	}

	return &imx296_modes[n];
}

static int imx296_set_fmt(struct v4l2_subdev *sd,
			  struct v4l2_subdev_pad_config *cfg,
			  struct v4l2_subdev_format *fmt)
{
	struct imx296 *imx296 = to_imx296(sd);
	const struct imx296_mode *mode;
	struct v4l2_mbus_framefmt *format;
	int ret = 0;

	mutex_lock(&imx296->lock);

	mode = imx296_find_nearest_mode(fmt->format.width, fmt->format.height);

	fmt->format.width = mode->width;
	fmt->format.height = mode->height;

	fmt->format.code = IMX296_DEFAULT_FORMAT;
	fmt->format.field = V4L2_FIELD_NONE;

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
		format = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
	} else {
		format = &imx296->current_format;
		__v4l2_ctrl_s_ctrl_int64(imx296->pixel_rate, mode->pixel_rate);

		imx296->current_mode = mode;
	}

	*format = fmt->format;

	mutex_unlock(&imx296->lock);

	return ret;
}

static int imx296_entity_init_cfg(struct v4l2_subdev *subdev,
				  struct v4l2_subdev_pad_config *cfg)
{
	struct v4l2_subdev_format fmt = { 0 };

	fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
	fmt.format.width = 1440;
	fmt.format.height = 1088;

	imx296_set_fmt(subdev, cfg, &fmt);

	return 0;
}

/* Start streaming */
static int imx296_start_streaming(struct imx296 *imx296)
{
	int ret;

	/* Set init register settings */
	ret = imx296_set_register_array(imx296, imx296_global_init_settings,
				ARRAY_SIZE(imx296_global_init_settings));
	if (ret < 0) {
		dev_err(imx296->dev, "Could not set init registers\n");
		return ret;
	}

	/* Apply default values of current mode */
	ret = imx296_set_register_array(imx296, imx296->current_mode->data,
					imx296->current_mode->data_size);
	if (ret < 0) {
		dev_err(imx296->dev, "Could not set current mode\n");
		return ret;
	}

	/* Apply customized values from user */
	ret = v4l2_ctrl_handler_setup(imx296->sd.ctrl_handler);
	if (ret) {
		dev_err(imx296->dev, "Could not sync v4l2 controls\n");
		return ret;
	}

	ret = imx296_write_reg(imx296, IMX296_STANDBY, 0x00);
	if (ret < 0)
		return ret;

	msleep(30);

	/* Start streaming */
	return imx296_write_reg(imx296, IMX296_XMSTA, 0x00);
}

static int imx296_set_stream(struct v4l2_subdev *sd, int enable)
{
	struct imx296 *imx296 = to_imx296(sd);
	int ret;

	if (enable) {
		ret = pm_runtime_get_sync(imx296->dev);
		if (ret < 0) {
			pm_runtime_put_noidle(imx296->dev);
			return ret;
		}

		ret = imx296_start_streaming(imx296);
		if (ret) {
			dev_err(imx296->dev, "Start stream failed\n");
			pm_runtime_put(imx296->dev);
			return ret;
		}
	} else {
		imx296_stop_streaming(imx296);
		pm_runtime_put(imx296->dev);
	}

	return 0;
}

static int imx296_get_regulators(struct device *dev, struct imx296 *imx296)
{
	unsigned int i;

	for (i = 0; i < IMX296_NUM_SUPPLIES; i++)
		imx296->supplies[i].supply = imx296_supply_name[i];

	return devm_regulator_bulk_get(dev, IMX296_NUM_SUPPLIES,
				       imx296->supplies);
}

static int imx296_power_on(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct imx296 *imx296 = to_imx296(sd);
	int ret;

	ret = regulator_bulk_enable(IMX296_NUM_SUPPLIES, imx296->supplies);
	if (ret) {
		dev_err(imx296->dev, "Failed to enable regulators\n");
		return ret;
	}

	usleep_range(1, 2);

	gpiod_set_value_cansleep(imx296->rst_gpio, 1);

	usleep_range(1, 2);

	ret = clk_prepare_enable(imx296->mclk);
	if (ret) {
		dev_err(imx296->dev, "Failed to enable clock\n");
		return ret;
	}

	usleep_range(30000, 31000);

	return 0;
}

static int imx296_power_off(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct imx296 *imx296 = to_imx296(sd);

	clk_disable_unprepare(imx296->mclk);
	gpiod_set_value_cansleep(imx296->rst_gpio, 0);
	regulator_bulk_disable(IMX296_NUM_SUPPLIES, imx296->supplies);

	return 0;
}

static const struct dev_pm_ops imx296_pm_ops = {
	SET_RUNTIME_PM_OPS(imx296_power_on, imx296_power_off, NULL)
};

static const struct v4l2_subdev_video_ops imx296_video_ops = {
	.s_stream = imx296_set_stream,
};

static const struct v4l2_subdev_pad_ops imx296_pad_ops = {
	.init_cfg = imx296_entity_init_cfg,
	.enum_mbus_code = imx296_enum_mbus_code,
	.get_fmt = imx296_get_fmt,
	.set_fmt = imx296_set_fmt,
};

static const struct v4l2_subdev_ops imx296_subdev_ops = {
	.video = &imx296_video_ops,
	.pad = &imx296_pad_ops,
};

static const struct media_entity_operations imx296_subdev_entity_ops = {
	.link_validate = v4l2_subdev_link_validate,
};

static int imx296_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	struct fwnode_handle *endpoint;
	struct imx296 *imx296;
	u32 mclk_freq;
	int ret;

	imx296 = devm_kzalloc(dev, sizeof(*imx296), GFP_KERNEL);
	if (!imx296)
		return -ENOMEM;

	imx296->dev = dev;
	imx296->regmap = devm_regmap_init_i2c(client, &imx296_regmap_config);
	if (IS_ERR(imx296->regmap)) {
		dev_err(dev, "Unable to initialize I2C\n");
		return -ENODEV;
	}

	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
	if (!endpoint) {
		dev_err(dev, "Endpoint node not found\n");
		return -EINVAL;
	}

	imx296->ep = v4l2_fwnode_endpoint_alloc_parse(endpoint);
	fwnode_handle_put(endpoint);
	if (IS_ERR(imx296->ep)) {
		dev_err(dev, "Parsing endpoint node failed\n");
		ret = PTR_ERR(imx296->ep);
		goto free_err;
	}

	/* Only CSI2 is supported for now */
	if (imx296->ep->bus_type != V4L2_MBUS_CSI2) {
		dev_err(dev, "Unsupported bus type, should be CSI2\n");
		ret = -EINVAL;
		goto free_err;
	}

	/* Set default mode to max resolution */
	imx296->current_mode = &imx296_modes[0];

	/* get system clock (mclk) */
	imx296->mclk = devm_clk_get(dev, "mclk");
	if (IS_ERR(imx296->mclk)) {
		dev_err(dev, "Could not get mclk");
		ret = PTR_ERR(imx296->mclk);
		goto free_err;
	}

	ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
				       &mclk_freq);
	if (ret) {
		dev_err(dev, "Could not get mclk frequency\n");
		goto free_err;
	}

	/* external clock must be 37.125 MHz */
	if (mclk_freq != 37125000) {
		dev_err(dev, "External clock frequency %u is not supported\n",
			mclk_freq);
		ret = -EINVAL;
		goto free_err;
	}

	ret = clk_set_rate(imx296->mclk, mclk_freq);
	if (ret) {
		dev_err(dev, "Could not set mclk frequency\n");
		goto free_err;
	}

	ret = imx296_get_regulators(dev, imx296);
	if (ret < 0) {
		dev_err(dev, "Cannot get regulators\n");
		goto free_err;
	}

	imx296->rst_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
	if (IS_ERR(imx296->rst_gpio)) {
		dev_err(dev, "Cannot get reset gpio\n");
		ret = PTR_ERR(imx296->rst_gpio);
		goto free_err;
	}

	mutex_init(&imx296->lock);

	v4l2_ctrl_handler_init(&imx296->ctrls, 3);

	v4l2_ctrl_new_std(&imx296->ctrls, &imx296_ctrl_ops,
			  V4L2_CID_GAIN, 0, 72, 1, 0);

	imx296->pixel_rate = v4l2_ctrl_new_std(&imx296->ctrls, &imx296_ctrl_ops,
					       V4L2_CID_PIXEL_RATE, 1,
					       INT_MAX, 1,
					       imx296_modes[0].pixel_rate);

	imx296->sd.ctrl_handler = &imx296->ctrls;

	if (imx296->ctrls.error) {
		dev_err(dev, "Control initialization error %d\n",
			imx296->ctrls.error);
		ret = imx296->ctrls.error;
		goto free_ctrl;
	}

	v4l2_i2c_subdev_init(&imx296->sd, client, &imx296_subdev_ops);
	imx296->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
	imx296->sd.dev = &client->dev;
	imx296->sd.entity.ops = &imx296_subdev_entity_ops;
	imx296->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;

	imx296->pad.flags = MEDIA_PAD_FL_SOURCE;
	ret = media_entity_pads_init(&imx296->sd.entity, 1, &imx296->pad);
	if (ret < 0) {
		dev_err(dev, "Could not register media entity\n");
		goto free_ctrl;
	}

	ret = v4l2_async_register_subdev(&imx296->sd);
	if (ret < 0) {
		dev_err(dev, "Could not register v4l2 device\n");
		goto free_entity;
	}

	/* Power on the device to match runtime PM state below */
	ret = imx296_power_on(dev);
	if (ret < 0) {
		dev_err(dev, "Could not power on the device\n");
		goto free_entity;
	}

	pm_runtime_set_active(dev);
	pm_runtime_enable(dev);
	pm_runtime_idle(dev);

	v4l2_fwnode_endpoint_free(imx296->ep);

	return 0;

free_entity:
	media_entity_cleanup(&imx296->sd.entity);
free_ctrl:
	v4l2_ctrl_handler_free(&imx296->ctrls);
	mutex_destroy(&imx296->lock);
free_err:
	v4l2_fwnode_endpoint_free(imx296->ep);

	return ret;
}

static int imx296_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct imx296 *imx296 = to_imx296(sd);

	v4l2_async_unregister_subdev(sd);
	media_entity_cleanup(&sd->entity);
	v4l2_ctrl_handler_free(sd->ctrl_handler);

	mutex_destroy(&imx296->lock);

	pm_runtime_disable(imx296->dev);
	if (!pm_runtime_status_suspended(imx296->dev))
		imx296_power_off(imx296->dev);
	pm_runtime_set_suspended(imx296->dev);

	return 0;
}

static const struct of_device_id imx296_of_match[] = {
	{ .compatible = "sony,imx296" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx296_of_match);

static struct i2c_driver imx296_i2c_driver = {
	.probe_new  = imx296_probe,
	.remove = imx296_remove,
	.driver = {
		.name  = "imx296",
		.pm = &imx296_pm_ops,
		.of_match_table = of_match_ptr(imx296_of_match),
	},
};

module_i2c_driver(imx296_i2c_driver);

MODULE_DESCRIPTION("Sony IMX296 CMOS Image Sensor Driver");
MODULE_AUTHOR("FRAMOS GmbH");
MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
MODULE_LICENSE("GPL v2");