-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimx219.c
995 lines (831 loc) · 25.6 KB
/
imx219.c
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*
* imx219.c - imx219 sensor driver
*
* Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dt-bindings/gpio/tegra-gpio.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <media/camera_common.h>
#include <media/soc_camera.h>
#include <media/imx219.h>
#include "imx219_mode_tbls.h"
#define IMX219_MAX_COARSE_DIFF 4
#define IMX219_GAIN_SHIFT (8)
#define IMX219_MIN_GAIN (1 << IMX219_GAIN_SHIFT)
#define IMX219_MAX_GAIN (16 << IMX219_GAIN_SHIFT)
#define IMX219_MIN_FRAME_LENGTH (0x0)
#define IMX219_MAX_FRAME_LENGTH (0x7FFF)
#define IMX219_MIN_EXPOSURE_COARSE (0x0001)
#define IMX219_MAX_EXPOSURE_COARSE \
(IMX219_MAX_FRAME_LENGTH-IMX219_MAX_COARSE_DIFF)
#define IMX219_DEFAULT_GAIN (IMX219_MIN_GAIN)
#define IMX219_DEFAULT_FRAME_LENGTH (1766)
#define IMX219_DEFAULT_EXPOSURE_COARSE \
(IMX219_DEFAULT_FRAME_LENGTH-IMX219_MAX_COARSE_DIFF)
#define IMX219_DEFAULT_MODE IMX219_MODE_1920X1080
#define IMX219_DEFAULT_WIDTH 1920
#define IMX219_DEFAULT_HEIGHT 1080
#define IMX219_DEFAULT_DATAFMT MEDIA_BUS_FMT_SRGGB10_1X10
#define IMX219_DEFAULT_CLK_FREQ 27000000
struct imx219 {
struct camera_common_power_rail power;
int num_ctrls;
struct v4l2_ctrl_handler ctrl_handler;
struct i2c_client *i2c_client;
struct v4l2_subdev *subdev;
struct media_pad pad;
struct regmap *regmap;
struct camera_common_data *s_data;
struct camera_common_pdata *pdata;
struct v4l2_ctrl *ctrls[];
};
static const struct regmap_config sensor_regmap_config = {
.reg_bits = 16,
.val_bits = 8,
.cache_type = REGCACHE_RBTREE,
};
static int imx219_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
static int imx219_s_ctrl(struct v4l2_ctrl *ctrl);
static const struct v4l2_ctrl_ops imx219_ctrl_ops = {
.g_volatile_ctrl = imx219_g_volatile_ctrl,
.s_ctrl = imx219_s_ctrl,
};
static struct v4l2_ctrl_config ctrl_config_list[] = {
/* Do not change the name field for the controls! */
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_GAIN,
.name = "Gain",
.type = V4L2_CTRL_TYPE_INTEGER,
.flags = V4L2_CTRL_FLAG_SLIDER,
.min = IMX219_MIN_GAIN,
.max = IMX219_MAX_GAIN,
.def = IMX219_DEFAULT_GAIN,
.step = 1,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_FRAME_LENGTH,
.name = "Frame Length",
.type = V4L2_CTRL_TYPE_INTEGER,
.flags = V4L2_CTRL_FLAG_SLIDER,
.min = IMX219_MIN_FRAME_LENGTH,
.max = IMX219_MAX_FRAME_LENGTH,
.def = IMX219_DEFAULT_FRAME_LENGTH,
.step = 1,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_COARSE_TIME,
.name = "Coarse Time",
.type = V4L2_CTRL_TYPE_INTEGER,
.flags = V4L2_CTRL_FLAG_SLIDER,
.min = IMX219_MIN_EXPOSURE_COARSE,
.max = IMX219_MAX_EXPOSURE_COARSE,
.def = IMX219_DEFAULT_EXPOSURE_COARSE,
.step = 1,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_GROUP_HOLD,
.name = "Group Hold",
.type = V4L2_CTRL_TYPE_INTEGER_MENU,
.min = 0,
.max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
.menu_skip_mask = 0,
.def = 0,
.qmenu_int = switch_ctrl_qmenu,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_HDR_EN,
.name = "HDR enable",
.type = V4L2_CTRL_TYPE_INTEGER_MENU,
.min = 0,
.max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
.menu_skip_mask = 0,
.def = 0,
.qmenu_int = switch_ctrl_qmenu,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_FUSE_ID,
.name = "Fuse ID",
.type = V4L2_CTRL_TYPE_STRING,
.flags = V4L2_CTRL_FLAG_READ_ONLY,
.min = 0,
.max = IMX219_FUSE_ID_STR_SIZE,
.step = 2,
},
};
static inline void imx219_get_frame_length_regs(struct reg_8 *regs, u16 frame_length)
{
regs->addr = IMX219_FRAME_LENGTH_ADDR_MSB;
regs->val = (frame_length >> 8) & 0xff;
(regs + 1)->addr = IMX219_FRAME_LENGTH_ADDR_LSB;
(regs + 1)->val = (frame_length) & 0xff;
}
static inline void imx219_get_coarse_time_regs(struct reg_8 *regs, u16 coarse_time)
{
regs->addr = IMX219_COARSE_TIME_ADDR_MSB;
regs->val = (coarse_time >> 8) & 0xff;
(regs + 1)->addr = IMX219_COARSE_TIME_ADDR_LSB;
(regs + 1)->val = (coarse_time) & 0xff;
}
static inline void imx219_get_gain_reg(struct reg_8 *regs, u8 gain)
{
regs->addr = IMX219_GAIN_ADDR;
regs->val = gain & 0xff;
}
static int test_mode;
module_param(test_mode, int, 0644);
static inline int imx219_read_reg(struct camera_common_data *s_data, u16 addr, u8 *val)
{
struct imx219 *priv = (struct imx219 *)s_data->priv;
return regmap_read(priv->regmap, addr, (unsigned int *) val);
}
static int imx219_write_reg(struct camera_common_data *s_data, u16 addr, u8 val)
{
int err;
struct imx219 *priv = (struct imx219 *)s_data->priv;
err = regmap_write(priv->regmap, addr, val);
if (err)
pr_err("%s:i2c write failed, %x = %x\n",
__func__, addr, val);
return err;
}
static int imx219_write_table(struct imx219 *priv, const struct reg_8 table[])
{
return regmap_util_write_table_8(priv->regmap,
table,
NULL, 0,
IMX219_TABLE_WAIT_MS,
IMX219_TABLE_END);
}
static void imx219_mclk_disable(struct camera_common_power_rail *pw)
{
clk_disable_unprepare(pw->mclk);
}
static void imx219_mclk_enable(struct camera_common_power_rail *pw)
{
pr_info("\n Enabling clock .\n");
clk_set_rate(pw->mclk, IMX219_DEFAULT_CLK_FREQ);
clk_prepare_enable(pw->mclk);
}
static int imx219_power_on(struct camera_common_data *s_data)
{
//int err = 0;.
struct imx219 *priv = (struct imx219 *)s_data->priv;
struct camera_common_power_rail *pw = &priv->power;
dev_info(&priv->i2c_client->dev, "%s: power on\n", __func__);
if (gpio_cansleep(pw->reset_gpio))
gpio_set_value_cansleep(pw->reset_gpio, 0);
else
gpio_set_value(pw->reset_gpio, 0);
usleep_range(10, 20);
/*
if (pw->avdd)
err = regulator_enable(pw->avdd);
if (err)
goto imx219_avdd_fail;
if (pw->iovdd)
err = regulator_enable(pw->iovdd);
if (err)
goto imx219_iovdd_fail;
if (pw->dvdd)
err = regulator_enable(pw->dvdd);
if (err)
goto imx219_dvdd_fail;
*/
//usleep_range(1, 2);
usleep_range(10, 20);
if (gpio_cansleep(pw->reset_gpio))
gpio_set_value_cansleep(pw->reset_gpio, 1);
else
gpio_set_value(pw->reset_gpio, 1);
//usleep_range(1, 2);
usleep_range(10, 20);
imx219_mclk_enable(pw);
/* Need to wait for t4 + t5 + t9 time as per the data sheet */
/* t4 - 200us, t5 - 6ms, t9 - 1.2ms */
usleep_range(7400, 7410);
pw->state = SWITCH_ON;
return 0;
/*
imx219_dvdd_fail:
regulator_disable(pw->iovdd);
imx219_iovdd_fail:
regulator_disable(pw->avdd);
imx219_avdd_fail:
return -ENODEV;
*/
}
static int imx219_power_off(struct camera_common_data *s_data)
{
struct imx219 *priv = (struct imx219 *)s_data->priv;
struct camera_common_power_rail *pw = &priv->power;
dev_info(&priv->i2c_client->dev, "%s: power off\n", __func__);
usleep_range(1, 2);
if (gpio_cansleep(pw->reset_gpio))
gpio_set_value_cansleep(pw->reset_gpio, 0);
else
gpio_set_value(pw->reset_gpio, 0);
//usleep_range(1, 2);
usleep_range(10, 20);
/*
if (pw->dvdd)
regulator_disable(pw->dvdd);
if (pw->iovdd)
regulator_disable(pw->iovdd);
if (pw->avdd)
regulator_disable(pw->avdd);
*/
imx219_mclk_disable(pw);
pw->state = SWITCH_OFF;
return 0;
}
static int imx219_power_put(struct imx219 *priv)
{
struct camera_common_power_rail *pw = &priv->power;
if (unlikely(!pw))
return -EFAULT;
/*
if (likely(pw->avdd))
regulator_put(pw->avdd);
if (likely(pw->iovdd))
regulator_put(pw->iovdd);
if (likely(pw->dvdd))
regulator_put(pw->dvdd);
*/
pw->avdd = NULL;
pw->iovdd = NULL;
pw->dvdd = NULL;
return 0;
}
static int imx219_power_get(struct imx219 *priv)
{
struct camera_common_power_rail *pw = &priv->power;
struct camera_common_pdata *pdata = priv->pdata;
const char *mclk_name;
struct clk *parent;
int err = 0;
pr_info("Setting parent clock: pllp_grtba .\n");
mclk_name = priv->pdata->mclk_name ?
priv->pdata->mclk_name : "cam_mclk2";
pw->mclk = devm_clk_get(&priv->i2c_client->dev, mclk_name);
if (IS_ERR(pw->mclk)) {
dev_err(&priv->i2c_client->dev,
"unable to get clock %s\n", mclk_name);
return PTR_ERR(pw->mclk);
}
parent = devm_clk_get(&priv->i2c_client->dev, "pllp_grtba");
if (IS_ERR(parent))
{
dev_err(&priv->i2c_client->dev, "unable to get parent clock %s", "pllp_grtba");
}
else
clk_set_parent(pw->mclk, parent);
/*
// ananlog 2.7v
err |= camera_common_regulator_get(priv->i2c_client,
&pw->avdd, pdata->regulators.avdd);
// digital 1.2v
err |= camera_common_regulator_get(priv->i2c_client,
&pw->dvdd, pdata->regulators.dvdd);
// IO 1.8v
err |= camera_common_regulator_get(priv->i2c_client,
&pw->iovdd, pdata->regulators.iovdd);
*/
if (!err)
pw->reset_gpio = pdata->reset_gpio;
pw->state = SWITCH_OFF;
return err;
}
static int imx219_set_gain(struct imx219 *priv, s32 val);
static int imx219_set_frame_length(struct imx219 *priv, s32 val);
static int imx219_set_coarse_time(struct imx219 *priv, s32 val);
static int imx219_s_stream(struct v4l2_subdev *sd, int enable)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct camera_common_data *s_data = to_camera_common_data(client);
struct imx219 *priv = (struct imx219 *)s_data->priv;
struct v4l2_control control;
int err;
dev_info(&client->dev, "%s\n", __func__);
if (!enable) {
dev_info(&client->dev, "%s: Stopping Stream\n", __func__);
return imx219_write_table(priv, mode_table[IMX219_MODE_STOP_STREAM]);
}
err = imx219_write_table(priv, mode_table[IMX219_MODE_COMMON]);
if (err) {
dev_err(&client->dev, "%s: Failed to set common values\n", __func__);
goto exit;
}
err = imx219_write_table(priv, mode_table[s_data->mode]);
dev_info(&client->dev, "%s: Setting mode: %d\n", __func__, s_data->mode);
if (err) {
dev_err(&client->dev, "%s: Error setting camera mode\n", __func__);
goto exit;
}
err = imx219_write_table(priv, mode_table[IMX219_MODE_CLOCK]);
if (err) {
dev_err(&client->dev, "%s: Failed to set clock values\n", __func__);
goto exit;
}
//Write all the overrides
//Gain
control.id = V4L2_CID_GAIN;
err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
err |= imx219_set_gain(priv, control.value);
if (err) {
dev_err(&client->dev, "%s: error gain override\n", __func__);
}
//Frame Length
control.id = V4L2_CID_FRAME_LENGTH;
err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
err |= imx219_set_frame_length(priv, control.value);
if (err) {
dev_err(&client->dev, "%s: error frame length override\n", __func__);
}
//Coarse Time (Exposure Length)
control.id = V4L2_CID_COARSE_TIME;
err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
err |= imx219_set_coarse_time(priv, control.value);
dev_info(&client->dev, "%s: Coarse Time: %d\n", __func__, control.value);
if (err) {
dev_err(&client->dev,
"%s: error coarse time override\n", __func__);
}
dev_info(&client->dev, "%s: Starting stream\n", __func__);
err = imx219_write_table(priv, mode_table[IMX219_MODE_START_STREAM]);
if (err)
goto exit;
return 0;
exit:
dev_info(&client->dev, "%s: error setting stream\n", __func__);
return err;
}
static int imx219_g_input_status(struct v4l2_subdev *sd, u32 *status)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct camera_common_data *s_data = to_camera_common_data(client);
struct imx219 *priv = (struct imx219 *)s_data->priv;
struct camera_common_power_rail *pw = &priv->power;
*status = pw->state == SWITCH_ON;
return 0;
}
static struct v4l2_subdev_video_ops imx219_subdev_video_ops = {
.s_stream = imx219_s_stream,
.g_mbus_config = camera_common_g_mbus_config,
.g_input_status = imx219_g_input_status,
};
static struct v4l2_subdev_core_ops imx219_subdev_core_ops = {
.s_power = camera_common_s_power,
};
static int imx219_get_fmt(struct v4l2_subdev *sd,
struct v4l2_subdev_pad_config *cfg,
struct v4l2_subdev_format *format)
{
return camera_common_g_fmt(sd, &format->format);
}
static int imx219_set_fmt(struct v4l2_subdev *sd,
struct v4l2_subdev_pad_config *cfg,
struct v4l2_subdev_format *format)
{
int ret;
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
ret = camera_common_try_fmt(sd, &format->format);
else
ret = camera_common_s_fmt(sd, &format->format);
return ret;
}
static struct v4l2_subdev_pad_ops imx219_subdev_pad_ops = {
.enum_mbus_code = camera_common_enum_mbus_code,
.set_fmt = imx219_set_fmt,
.get_fmt = imx219_get_fmt,
.enum_frame_size = camera_common_enum_framesizes,
.enum_frame_interval = camera_common_enum_frameintervals,
};
static struct v4l2_subdev_ops imx219_subdev_ops = {
.core = &imx219_subdev_core_ops,
.video = &imx219_subdev_video_ops,
.pad = &imx219_subdev_pad_ops,
};
static struct of_device_id imx219_of_match[] = {
{ .compatible = "nvidia,imx219", },
{ },
};
static struct camera_common_sensor_ops imx219_common_ops = {
.power_on = imx219_power_on,
.power_off = imx219_power_off,
.write_reg = imx219_write_reg,
.read_reg = imx219_read_reg,
};
static int imx219_set_group_hold(struct imx219 *priv, s32 val)
{
/* IMX219 does not support group hold */
return 0;
}
static int imx219_set_gain(struct imx219 *priv, s32 val)
{
struct reg_8 reg;
int err;
u8 gain;
/* translate value */
gain = 256 - (256 * (1 << IMX219_GAIN_SHIFT) / val);
dev_info(&priv->i2c_client->dev,
"%s: val: %d\n", __func__, gain);
imx219_get_gain_reg(®, gain);
err = imx219_write_reg(priv->s_data, reg.addr, reg.val);
if (err)
goto fail;
return 0;
fail:
dev_info(&priv->i2c_client->dev,
"%s: GAIN control error\n", __func__);
return err;
}
static int imx219_set_frame_length(struct imx219 *priv, s32 val)
{
u8 data[2];
int err;
dev_info(&priv->i2c_client->dev,
"%s: val: %d\n", __func__, val);
data[0] = (val >> 8) & 0xff;
data[1] = val & 0xff;
err = regmap_raw_write(priv->regmap, IMX219_FRAME_LENGTH_ADDR_MSB,
data, 2);
if (err)
goto fail;
return 0;
fail:
dev_info(&priv->i2c_client->dev,
"%s: FRAME_LENGTH control error\n", __func__);
return err;
}
static int imx219_set_coarse_time(struct imx219 *priv, s32 val)
{
u8 data[2];
int err;
dev_info(&priv->i2c_client->dev,
"%s: val: %d\n", __func__, val);
data[0] = (val >> 8) & 0xff;
data[1] = val & 0xff;
err = regmap_raw_write(priv->regmap, IMX219_COARSE_TIME_ADDR_MSB,
data, 2);
if (err)
goto fail;
return 0;
fail:
dev_info(&priv->i2c_client->dev,
"%s: COARSE_TIME control error\n", __func__);
return err;
}
static int imx219_verify_streaming(struct imx219 *priv)
{
int err = 0;
err = camera_common_s_power(priv->subdev, true);
if (err)
return err;
err = imx219_s_stream(priv->subdev, true);
if (err)
goto error;
error:
imx219_s_stream(priv->subdev, false);
camera_common_s_power(priv->subdev, false);
return err;
}
static int imx219_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
struct imx219 *priv =
container_of(ctrl->handler, struct imx219, ctrl_handler);
int err = 0;
if (priv->power.state == SWITCH_OFF)
return 0;
switch (ctrl->id) {
default:
pr_err("%s: unknown ctrl id.\n", __func__);
return -EINVAL;
}
return err;
}
static int imx219_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct imx219 *priv =
container_of(ctrl->handler, struct imx219, ctrl_handler);
int err = 0;
if (priv->power.state == SWITCH_OFF)
return 0;
switch (ctrl->id) {
case V4L2_CID_GAIN:
err = imx219_set_gain(priv, ctrl->val);
break;
case V4L2_CID_FRAME_LENGTH:
err = imx219_set_frame_length(priv, ctrl->val);
break;
case V4L2_CID_COARSE_TIME:
err = imx219_set_coarse_time(priv, ctrl->val);
break;
case V4L2_CID_GROUP_HOLD:
err = imx219_set_group_hold(priv, ctrl->val);
break;
case V4L2_CID_HDR_EN:
break;
case V4L2_CID_FUSE_ID:
break;
default:
pr_err("%s: unknown ctrl id.\n", __func__);
return -EINVAL;
}
return err;
}
static int imx219_ctrls_init(struct imx219 *priv)
{
struct i2c_client *client = priv->i2c_client;
struct v4l2_ctrl *ctrl;
int num_ctrls;
int err;
int i;
dev_info(&client->dev, "%s++\n", __func__);
num_ctrls = ARRAY_SIZE(ctrl_config_list);
v4l2_ctrl_handler_init(&priv->ctrl_handler, num_ctrls);
for (i = 0; i < num_ctrls; i++) {
ctrl = v4l2_ctrl_new_custom(&priv->ctrl_handler,
&ctrl_config_list[i], NULL);
if (ctrl == NULL) {
dev_err(&client->dev, "Failed to init %s ctrl\n",
ctrl_config_list[i].name);
continue;
}
if (ctrl_config_list[i].type == V4L2_CTRL_TYPE_STRING &&
ctrl_config_list[i].flags & V4L2_CTRL_FLAG_READ_ONLY) {
ctrl->p_new.p_char = devm_kzalloc(&client->dev,
ctrl_config_list[i].max + 1, GFP_KERNEL);
if (!ctrl->p_new.p_char) {
dev_err(&client->dev,
"Failed to allocate otp data\n");
return -ENOMEM;
}
}
priv->ctrls[i] = ctrl;
}
priv->num_ctrls = num_ctrls;
priv->subdev->ctrl_handler = &priv->ctrl_handler;
if (priv->ctrl_handler.error) {
dev_err(&client->dev, "Error %d adding controls\n",
priv->ctrl_handler.error);
err = priv->ctrl_handler.error;
goto error;
}
err = v4l2_ctrl_handler_setup(&priv->ctrl_handler);
if (err) {
dev_err(&client->dev,
"Error %d setting default controls\n", err);
goto error;
}
return 0;
error:
v4l2_ctrl_handler_free(&priv->ctrl_handler);
return err;
}
MODULE_DEVICE_TABLE(of, imx219_of_match);
static struct camera_common_pdata *imx219_parse_dt(struct i2c_client *client)
{
struct device_node *np = client->dev.of_node;
struct camera_common_pdata *board_priv_pdata;
const struct of_device_id *match;
int gpio;
int err;
struct camera_common_pdata *ret = NULL;
match = of_match_device(imx219_of_match, &client->dev);
if (!match) {
dev_err(&client->dev, "Failed to find matching dt id\n");
return NULL;
}
board_priv_pdata = devm_kzalloc(&client->dev,
sizeof(*board_priv_pdata), GFP_KERNEL);
if (!board_priv_pdata) {
dev_err(&client->dev, "Failed to allocate pdata\n");
return NULL;
}
err = of_property_read_string(np, "mclk", &board_priv_pdata->mclk_name);
if (err) {
dev_err(&client->dev, "mclk not in DT\n");
goto error;
}
gpio = of_get_named_gpio(np, "reset-gpios", 0);
if (gpio < 0) {
/*
if (gpio == -EPROBE_DEFER) {
ret = ERR_PTR(-EPROBE_DEFER);
goto error;
}
*/
dev_err(&client->dev, "reset gpios not in DT\n");
goto error;
}
board_priv_pdata->reset_gpio = (unsigned int)gpio;
/*
err = of_property_read_string(np, "avdd-reg",
&board_priv_pdata->regulators.avdd);
if (err) {
dev_err(&client->dev, "avdd-reg not in DT\n");
goto error;
}
err = of_property_read_string(np, "dvdd-reg",
&board_priv_pdata->regulators.dvdd);
if (err) {
dev_err(&client->dev, "dvdd-reg not in DT\n");
goto error;
}
err = of_property_read_string(np, "iovdd-reg",
&board_priv_pdata->regulators.iovdd);
if (err) {
dev_err(&client->dev, "iovdd-reg not in DT\n");
goto error;
}
*/
err = camera_common_parse_sensor_mode(client, board_priv_pdata);
if (err)
dev_err(&client->dev, "Failed to load mode info %d\n", err);
return board_priv_pdata;
error:
devm_kfree(&client->dev, board_priv_pdata);
return ret;
}
static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
dev_info(&client->dev, "%s:\n", __func__);
return 0;
}
static const struct v4l2_subdev_internal_ops imx219_subdev_internal_ops = {
.open = imx219_open,
};
static const struct media_entity_operations imx219_media_ops = {
.link_validate = v4l2_subdev_link_validate,
};
static int imx219_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct camera_common_data *common_data;
struct device_node *node = client->dev.of_node;
struct imx219 *priv;
char debugfs_name[32];
int err;
pr_info("[IMX219]: probing v4l2 sensor.\n");
if (!IS_ENABLED(CONFIG_OF) || !node)
return -EINVAL;
common_data = devm_kzalloc(&client->dev,
sizeof(struct camera_common_data), GFP_KERNEL);
if (!common_data) {
dev_err(&client->dev, "unable to allocate memory!\n");
return -ENOMEM;
}
priv = devm_kzalloc(&client->dev,
sizeof(struct imx219) + sizeof(struct v4l2_ctrl *) *
ARRAY_SIZE(ctrl_config_list),
GFP_KERNEL);
if (!priv) {
dev_err(&client->dev, "unable to allocate memory!\n");
return -ENOMEM;
}
priv->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
if (IS_ERR(priv->regmap)) {
dev_err(&client->dev,
"regmap init failed: %ld\n", PTR_ERR(priv->regmap));
return -ENODEV;
}
priv->pdata = imx219_parse_dt(client);
dev_info(&client->dev, "Found and parsed of_node in DT\n");
if (PTR_ERR(priv->pdata) == -EPROBE_DEFER) {
dev_err(&client->dev, "Defer openning\n");
devm_kfree(&client->dev, priv);
return -EPROBE_DEFER;
}
if (!priv->pdata) {
dev_err(&client->dev, "unable to get platform data\n");
return -EFAULT;
}
common_data->ops = &imx219_common_ops;
common_data->ctrl_handler = &priv->ctrl_handler;
common_data->i2c_client = client;
common_data->frmfmt = &imx219_frmfmt[0];
common_data->colorfmt = camera_common_find_datafmt(IMX219_DEFAULT_DATAFMT);
common_data->power = &priv->power;
common_data->ctrls = priv->ctrls;
common_data->priv = (void *)priv;
common_data->numctrls = ARRAY_SIZE(ctrl_config_list);
common_data->numfmts = ARRAY_SIZE(imx219_frmfmt);
common_data->def_mode = IMX219_DEFAULT_MODE;
common_data->def_width = IMX219_DEFAULT_WIDTH;
common_data->def_height = IMX219_DEFAULT_HEIGHT;
common_data->fmt_width = common_data->def_width;
common_data->fmt_height = common_data->def_height;
common_data->def_clk_freq = IMX219_DEFAULT_CLK_FREQ;
priv->i2c_client = client;
priv->s_data = common_data;
priv->subdev = &common_data->subdev;
priv->subdev->dev = &client->dev;
priv->s_data->dev = &client->dev;
pr_info("\n[IMX219]: Get power function.\n");
err = imx219_power_get(priv);
if (err)
return err;
err = camera_common_parse_ports(client, common_data);
if (err) {
dev_err(&client->dev, "Failed to find port info\n");
return err;
}
dev_info(&client->dev, "IMX219 CSI PORT: imx219_%d", common_data->csi_port);
dev_info(&client->dev, "IMX219 CSI PORT LANES: imx219_%d", common_data->numlanes);
snprintf(debugfs_name, sizeof(debugfs_name), "%s.%s",
dev_driver_string(&client->dev), dev_name(&client->dev));
dev_info (&client->dev, "%s: Debug FS Name: %s", __func__, debugfs_name);
camera_common_create_debugfs(common_data, debugfs_name);
dev_info(&client->dev, "Created V4l2 subdev\n");
v4l2_i2c_subdev_init(&common_data->subdev, client, &imx219_subdev_ops);
dev_info(&client->dev, "Initialize controls\n");
err = imx219_ctrls_init(priv);
if (err)
return err;
dev_info(&client->dev, "Verify stream\n");
err = imx219_verify_streaming(priv);
if (err) {
dev_info(&client->dev, "Failed to verify streaming\n");
return err;
}
dev_info(&client->dev, "Configure interal operations\n");
priv->subdev->internal_ops = &imx219_subdev_internal_ops;
priv->subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
V4L2_SUBDEV_FL_HAS_EVENTS;
#if defined(CONFIG_MEDIA_CONTROLLER)
dev_info(&client->dev, "Setup media controller\n");
priv->pad.flags = MEDIA_PAD_FL_SOURCE;
priv->subdev->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
priv->subdev->entity.ops = &imx219_media_ops;
err = media_entity_init(&priv->subdev->entity, 1, &priv->pad, 0);
if (err < 0) {
dev_err(&client->dev, "unable to init media entity\n");
return err;
}
#endif
dev_info(&client->dev, "Register subdev\n");
err = v4l2_async_register_subdev(priv->subdev);
if (err)
return err;
dev_info(&client->dev, "IMX219 Probe Finished\n");
return 0;
}
static int
imx219_remove(struct i2c_client *client)
{
struct camera_common_data *s_data = to_camera_common_data(client);
struct imx219 *priv = (struct imx219 *)s_data->priv;
v4l2_async_unregister_subdev(priv->subdev);
#if defined(CONFIG_MEDIA_CONTROLLER)
media_entity_cleanup(&priv->subdev->entity);
#endif
v4l2_ctrl_handler_free(&priv->ctrl_handler);
imx219_power_put(priv);
camera_common_remove_debugfs(s_data);
return 0;
}
static const struct i2c_device_id imx219_id[] = {
{ "imx219", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, imx219_id);
static struct i2c_driver imx219_i2c_driver = {
.driver = {
.name = "imx219",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(imx219_of_match),
},
.probe = imx219_probe,
.remove = imx219_remove,
.id_table = imx219_id,
};
module_i2c_driver(imx219_i2c_driver);
MODULE_DESCRIPTION("SoC Camera driver for Sony IMX219");
MODULE_AUTHOR("Bryan Wu <[email protected]>");
MODULE_LICENSE("GPL v2");