forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/media/spi/rk1608_dphy.c
....@@ -24,9 +24,10 @@
2424 #include <linux/of_platform.h>
2525 #include <linux/types.h>
2626 #include <linux/rk-preisp.h>
27
-#include <linux/rkisp1-config.h>
27
+#include <linux/rk-isp1-config.h>
2828 #include <linux/rk-camera-module.h>
2929 #include "rk1608_dphy.h"
30
+#include <linux/compat.h>
3031
3132 #define RK1608_DPHY_NAME "RK1608-dphy"
3233
....@@ -109,9 +110,114 @@
109110 return ret;
110111 }
111112
113
+#define RK1608_MAX_BITRATE (1500000000)
114
+static int rk1608_get_link_sensor_timing(struct rk1608_dphy *pdata)
115
+{
116
+ int ret = 0;
117
+ u32 i;
118
+ u32 idx = pdata->fmt_inf_idx;
119
+ struct rk1608_fmt_inf *fmt_inf = &pdata->fmt_inf[idx];
120
+ int sub_sensor_num = pdata->sub_sensor_num;
121
+ u32 width = 0, height = 0, out_width, out_height;
122
+ struct v4l2_subdev *link_sensor;
123
+ u32 id = pdata->sd.grp_id;
124
+ struct v4l2_subdev_frame_interval fi;
125
+ int max_fps = 30;
126
+ u64 bps;
127
+
128
+ struct v4l2_subdev_format fmt = {
129
+ .which = V4L2_SUBDEV_FORMAT_ACTIVE,
130
+ .pad = 0,
131
+ };
132
+
133
+ if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
134
+
135
+ link_sensor = i2c_get_clientdata(
136
+ pdata->link_sensor_client);
137
+ if (IS_ERR_OR_NULL(link_sensor)) {
138
+ dev_err(pdata->dev, "can not get link sensor i2c client\n");
139
+ return -EINVAL;
140
+ }
141
+
142
+ ret = v4l2_subdev_call(link_sensor, pad, get_fmt, NULL, &fmt);
143
+ if (ret) {
144
+ dev_info(pdata->dev, "get link fmt fail\n");
145
+ return -EINVAL;
146
+ }
147
+
148
+ width = fmt.format.width;
149
+ height = fmt.format.height;
150
+ dev_info(pdata->dev, "phy[%d] get fmt w:%d h:%d\n",
151
+ id, width, height);
152
+
153
+ memset(&fi, 0, sizeof(fi));
154
+ ret = v4l2_subdev_call(link_sensor, video, g_frame_interval, &fi);
155
+ if (ret) {
156
+ dev_info(pdata->dev, "get link interval fail\n");
157
+ return -EINVAL;
158
+ }
159
+
160
+ max_fps = fi.interval.denominator / fi.interval.numerator;
161
+ dev_info(pdata->dev, "phy[%d] get fps:%d (%d/%d)\n",
162
+ id, max_fps, fi.interval.denominator, fi.interval.numerator);
163
+
164
+ } else {
165
+ width = fmt_inf->mf.width;
166
+ height = fmt_inf->mf.height;
167
+ dev_info(pdata->dev, "phy[%d] no link sensor\n", id);
168
+ }
169
+
170
+ if (!width || !height) {
171
+ dev_err(pdata->dev, "phy[%d] get fmt error!\n", id);
172
+ return -EINVAL;
173
+ }
174
+
175
+ for (i = 0; i < 4; i++) {
176
+ if (fmt_inf->in_ch[i].width == 0)
177
+ break;
178
+
179
+ fmt_inf->in_ch[i].width = width;
180
+ fmt_inf->in_ch[i].height = height;
181
+ }
182
+
183
+ out_width = width;
184
+ out_height = height * (sub_sensor_num + 1); /* sub add main */
185
+ for (i = 0; i < 4; i++) {
186
+ if (fmt_inf->out_ch[i].width == 0)
187
+ break;
188
+
189
+ fmt_inf->out_ch[i].width = out_width;
190
+ fmt_inf->out_ch[i].height = out_height;
191
+ }
192
+
193
+ fmt_inf->hactive = out_width;
194
+ fmt_inf->vactive = out_height;
195
+ fmt_inf->htotal = out_width + (width * 1 / 3); //1.33
196
+ fmt_inf->vtotal = out_height + (height >> 4);
197
+
198
+ /* max 30 fps, raw 10 */
199
+ bps = fmt_inf->htotal * fmt_inf->vtotal
200
+ / fmt_inf->mipi_lane_out * 10 * max_fps;
201
+
202
+ /* add extra timing */
203
+ bps = bps * 105;
204
+ do_div(bps, 100);
205
+
206
+ if (bps > RK1608_MAX_BITRATE)
207
+ bps = RK1608_MAX_BITRATE;
208
+
209
+ pdata->link_freqs = (u32)(bps/2);
210
+ dev_info(pdata->dev, "target mipi bps:%lld\n", bps);
211
+
212
+ return 0;
213
+}
214
+
112215 static int rk1608_s_stream(struct v4l2_subdev *sd, int enable)
113216 {
114217 struct rk1608_dphy *pdata = to_state(sd);
218
+
219
+ if (enable && pdata->sub_sensor_num)
220
+ rk1608_get_link_sensor_timing(pdata);
115221
116222 pdata->rk1608_sd->grp_id = sd->grp_id;
117223 v4l2_subdev_call(pdata->rk1608_sd, video, s_stream, enable);
....@@ -159,12 +265,43 @@
159265 struct v4l2_mbus_framefmt *mf = &fmt->format;
160266 struct rk1608_dphy *pdata = to_state(sd);
161267 u32 idx = pdata->fmt_inf_idx;
268
+ struct v4l2_subdev *link_sensor;
269
+ int ret = -1;
162270
163
- mf->code = pdata->fmt_inf[idx].mf.code;
164
- mf->width = pdata->fmt_inf[idx].mf.width;
165
- mf->height = pdata->fmt_inf[idx].mf.height;
166
- mf->field = pdata->fmt_inf[idx].mf.field;
167
- mf->colorspace = pdata->fmt_inf[idx].mf.colorspace;
271
+ if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
272
+ link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
273
+ if (IS_ERR_OR_NULL(link_sensor)) {
274
+ dev_err(pdata->dev, "can not get link sensor i2c client\n");
275
+ goto exit;
276
+ }
277
+
278
+ ret = v4l2_subdev_call(link_sensor, pad, get_fmt, NULL, fmt);
279
+ if (ret) {
280
+ dev_info(pdata->dev, "get link fmt fail\n");
281
+ goto exit;
282
+ }
283
+
284
+ dev_info(pdata->dev, "use link sensor fmt w:%d h:%d code:%d\n",
285
+ mf->width, mf->height, mf->code);
286
+ }
287
+
288
+exit:
289
+ if (ret || !mf->width || !mf->height) {
290
+ mf->code = pdata->fmt_inf[idx].mf.code;
291
+ mf->width = pdata->fmt_inf[idx].mf.width;
292
+ mf->height = pdata->fmt_inf[idx].mf.height;
293
+ mf->field = pdata->fmt_inf[idx].mf.field;
294
+ mf->colorspace = pdata->fmt_inf[idx].mf.colorspace;
295
+ } else {
296
+ pdata->fmt_inf[idx].mf.code = mf->code;
297
+ pdata->fmt_inf[idx].mf.width = mf->width;
298
+ pdata->fmt_inf[idx].mf.height = mf->height;
299
+ pdata->fmt_inf[idx].mf.field = mf->field;
300
+ pdata->fmt_inf[idx].mf.colorspace = mf->colorspace;
301
+ }
302
+
303
+ if (pdata->sub_sensor_num)
304
+ rk1608_get_link_sensor_timing(pdata);
168305
169306 return 0;
170307 }
....@@ -201,9 +338,9 @@
201338
202339 pdata->fmt_inf_idx = idx;
203340
341
+ pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
204342 v4l2_subdev_call(pdata->rk1608_sd, pad, set_fmt, cfg, fmt);
205343
206
- pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
207344 remote_ctrl = v4l2_ctrl_find(pdata->rk1608_sd->ctrl_handler,
208345 V4L2_CID_HBLANK);
209346 if (remote_ctrl) {
....@@ -233,7 +370,30 @@
233370 struct v4l2_subdev_frame_interval *fi)
234371 {
235372 struct rk1608_dphy *pdata = to_state(sd);
373
+ struct v4l2_subdev *link_sensor;
374
+ int ret = 0;
236375
376
+ if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
377
+ link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
378
+ if (IS_ERR_OR_NULL(link_sensor)) {
379
+ dev_err(pdata->dev, "can not get link sensor i2c client\n");
380
+ return -EINVAL;
381
+ }
382
+
383
+ ret = v4l2_subdev_call(link_sensor,
384
+ video,
385
+ g_frame_interval,
386
+ fi);
387
+ if (ret)
388
+ dev_info(pdata->dev, "get link interval fail\n");
389
+ else
390
+ return ret;
391
+ }
392
+
393
+ if (!(pdata->rk1608_sd)) {
394
+ dev_info(pdata->dev, "pdata->rk1608_sd NULL\n");
395
+ return -EFAULT;
396
+ }
237397 pdata->rk1608_sd->grp_id = sd->grp_id;
238398 v4l2_subdev_call(pdata->rk1608_sd,
239399 video,
....@@ -249,18 +409,18 @@
249409 return 0;
250410 }
251411
252
-static int rk1608_g_mbus_config(struct v4l2_subdev *sd,
412
+static int rk1608_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
253413 struct v4l2_mbus_config *config)
254414 {
255415
256416 struct rk1608_dphy *pdata = to_state(sd);
257417 u32 val = 0;
258418
259
- val = 1 << (pdata->fmt_inf[pdata->fmt_inf_idx].mipi_lane - 1) |
419
+ val = 1 << (pdata->fmt_inf[pdata->fmt_inf_idx].mipi_lane_out - 1) |
260420 V4L2_MBUS_CSI2_CHANNEL_0 |
261421 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
262422
263
- config->type = V4L2_MBUS_CSI2;
423
+ config->type = V4L2_MBUS_CSI2_DPHY;
264424 config->flags = val;
265425
266426 return 0;
....@@ -269,6 +429,7 @@
269429 static long rk1608_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
270430 {
271431 struct rk1608_dphy *pdata = to_state(sd);
432
+ struct v4l2_subdev *link_sensor;
272433 long ret = 0;
273434
274435 switch (cmd) {
....@@ -289,11 +450,22 @@
289450 case PREISP_DISP_WRITE_EEPROM:
290451 case PREISP_DISP_READ_EEPROM:
291452 case PREISP_DISP_SET_LED_ON_OFF:
453
+ case RKMODULE_SET_QUICK_STREAM:
292454 mutex_lock(&rk1608_dphy_mutex);
293455 pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
294456 ret = v4l2_subdev_call(pdata->rk1608_sd, core, ioctl,
295457 cmd, arg);
296458 mutex_unlock(&rk1608_dphy_mutex);
459
+ break;
460
+ case RKMODULE_GET_CSI_DPHY_PARAM:
461
+ if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
462
+ link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
463
+ if (IS_ERR_OR_NULL(link_sensor)) {
464
+ dev_err(pdata->dev, "can not get link sensor i2c client\n");
465
+ return -EINVAL;
466
+ }
467
+ ret = v4l2_subdev_call(link_sensor, core, ioctl, cmd, arg);
468
+ }
297469 break;
298470 default:
299471 ret = -ENOIOCTLCMD;
....@@ -310,7 +482,9 @@
310482 struct preisp_hdrae_exp_s hdrae_exp;
311483 struct rkmodule_inf *inf;
312484 struct rkmodule_awb_cfg *cfg;
313
- long ret;
485
+ struct rkmodule_csi_dphy_param *dphy_param;
486
+ u32 stream;
487
+ long ret = -EFAULT;
314488
315489 switch (cmd) {
316490 case PREISP_CMD_SET_HDRAE_EXP:
....@@ -327,7 +501,10 @@
327501
328502 ret = rk1608_ioctl(sd, cmd, inf);
329503 if (!ret)
330
- ret = copy_to_user(up, inf, sizeof(*inf));
504
+ if (copy_to_user(up, inf, sizeof(*inf))) {
505
+ kfree(inf);
506
+ return -EFAULT;
507
+ }
331508 kfree(inf);
332509 break;
333510 case RKMODULE_AWB_CFG:
....@@ -336,10 +513,29 @@
336513 ret = -ENOMEM;
337514 return ret;
338515 }
339
- if (copy_from_user(cfg, up, sizeof(cfg)))
516
+ if (copy_from_user(cfg, up, sizeof(*cfg)))
340517 return -EFAULT;
341518 ret = rk1608_ioctl(sd, cmd, cfg);
342519 kfree(cfg);
520
+ break;
521
+ case RKMODULE_SET_QUICK_STREAM:
522
+ ret = copy_from_user(&stream, up, sizeof(u32));
523
+ if (!ret)
524
+ ret = rk1608_ioctl(sd, cmd, &stream);
525
+ else
526
+ ret = -EFAULT;
527
+
528
+ break;
529
+ case RKMODULE_GET_CSI_DPHY_PARAM:
530
+ dphy_param = kzalloc(sizeof(*dphy_param), GFP_KERNEL);
531
+ if (!dphy_param) {
532
+ ret = -ENOMEM;
533
+ return ret;
534
+ }
535
+ if (copy_from_user(dphy_param, up, sizeof(*dphy_param)))
536
+ return -EFAULT;
537
+ ret = rk1608_ioctl(sd, cmd, dphy_param);
538
+ kfree(dphy_param);
343539 break;
344540 default:
345541 ret = -ENOIOCTLCMD;
....@@ -385,6 +581,87 @@
385581 ctrl->id);
386582 if (remote_ctrl)
387583 ret = v4l2_ctrl_s_ctrl(remote_ctrl, ctrl->val);
584
+
585
+ return ret;
586
+}
587
+
588
+#define CROP_START(SRC, DST) (((SRC) - (DST)) / 2 / 4 * 4)
589
+static int rk1608_get_selection(struct v4l2_subdev *sd,
590
+ struct v4l2_subdev_pad_config *cfg,
591
+ struct v4l2_subdev_selection *sel)
592
+{
593
+ struct rk1608_dphy *pdata = to_state(sd);
594
+ u32 idx = pdata->fmt_inf_idx;
595
+ u32 width = pdata->fmt_inf[idx].mf.width;
596
+ u32 height = pdata->fmt_inf[idx].mf.height;
597
+ struct v4l2_subdev *link_sensor;
598
+ int ret = -EINVAL;
599
+
600
+ if (sel->target != V4L2_SEL_TGT_CROP_BOUNDS)
601
+ return -EINVAL;
602
+
603
+ if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
604
+ link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
605
+ if (IS_ERR_OR_NULL(link_sensor)) {
606
+ dev_err(pdata->dev, "can not get link sensor i2c client\n");
607
+ goto err;
608
+ }
609
+
610
+ ret = v4l2_subdev_call(link_sensor, pad, get_selection, NULL, sel);
611
+ if (!ret)
612
+ return 0;
613
+ }
614
+
615
+err:
616
+ if (pdata->fmt_inf[idx].hcrop && pdata->fmt_inf[idx].vcrop) {
617
+ width = pdata->fmt_inf[idx].hcrop;
618
+ height = pdata->fmt_inf[idx].vcrop;
619
+ }
620
+
621
+ sel->r.left = CROP_START(pdata->fmt_inf[idx].mf.width, width);
622
+ sel->r.top = CROP_START(pdata->fmt_inf[idx].mf.height, height);
623
+ sel->r.width = width;
624
+ sel->r.height = height;
625
+
626
+ return 0;
627
+}
628
+
629
+static int rk1608_enum_frame_interval(struct v4l2_subdev *sd,
630
+ struct v4l2_subdev_pad_config *cfg,
631
+ struct v4l2_subdev_frame_interval_enum *fie)
632
+{
633
+ struct rk1608_dphy *pdata = to_state(sd);
634
+ u32 idx = pdata->fmt_inf_idx;
635
+ int ret = 0;
636
+ struct v4l2_fract max_fps = {
637
+ .numerator = 10000,
638
+ .denominator = 300000,
639
+ };
640
+ struct v4l2_subdev *link_sensor;
641
+
642
+ if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
643
+ link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
644
+ if (IS_ERR_OR_NULL(link_sensor)) {
645
+ dev_err(pdata->dev, "can not get link sensor i2c client\n");
646
+ goto err;
647
+ }
648
+
649
+ ret = v4l2_subdev_call(link_sensor,
650
+ pad,
651
+ enum_frame_interval,
652
+ NULL,
653
+ fie);
654
+ return ret;
655
+ }
656
+
657
+err:
658
+ if (fie->index >= pdata->fmt_inf_num)
659
+ return -EINVAL;
660
+
661
+ fie->code = pdata->fmt_inf[idx].mf.code;
662
+ fie->width = pdata->fmt_inf[idx].mf.width;
663
+ fie->height = pdata->fmt_inf[idx].mf.height;
664
+ fie->interval = max_fps;
388665
389666 return ret;
390667 }
....@@ -511,7 +788,6 @@
511788 .s_stream = rk1608_s_stream,
512789 .g_frame_interval = rk1608_g_frame_interval,
513790 .s_frame_interval = rk1608_s_frame_interval,
514
- .g_mbus_config = rk1608_g_mbus_config,
515791 };
516792
517793 static const struct v4l2_subdev_pad_ops rk1608_subdev_pad_ops = {
....@@ -519,6 +795,9 @@
519795 .enum_frame_size = rk1608_enum_frame_sizes,
520796 .get_fmt = rk1608_get_fmt,
521797 .set_fmt = rk1608_set_fmt,
798
+ .get_mbus_config = rk1608_g_mbus_config,
799
+ .get_selection = rk1608_get_selection,
800
+ .enum_frame_interval = rk1608_enum_frame_interval,
522801 };
523802
524803 static const struct v4l2_subdev_core_ops rk1608_core_ops = {
....@@ -541,7 +820,9 @@
541820 struct device_node *node = dphy->dev->of_node;
542821 struct device_node *parent_node = of_node_get(node);
543822 struct device_node *prev_node = NULL;
823
+ struct i2c_client *link_sensor_client;
544824 u32 idx = 0;
825
+ u32 sub_idx = 0;
545826
546827 ret = of_property_read_u32(node, "id", &dphy->sd.grp_id);
547828 if (ret)
....@@ -590,6 +871,11 @@
590871 &dphy->fmt_inf[idx].mipi_lane);
591872 if (ret)
592873 dev_warn(dphy->dev, "Can not get mipi_lane!");
874
+
875
+ ret = of_property_read_u32(node, "mipi_lane_out",
876
+ &dphy->fmt_inf[idx].mipi_lane_out);
877
+ if (ret)
878
+ dev_warn(dphy->dev, "Can not get mipi_lane_out!");
593879
594880 ret = of_property_read_u32(node, "field",
595881 &dphy->fmt_inf[idx].mf.field);
....@@ -676,6 +962,16 @@
676962 if (ret)
677963 dev_info(dphy->dev, "Can not get outch3-info!");
678964
965
+ ret = of_property_read_u32(node, "hcrop",
966
+ &dphy->fmt_inf[idx].hcrop);
967
+ if (ret)
968
+ dev_warn(dphy->dev, "Can not get hcrop!");
969
+
970
+ ret = of_property_read_u32(node, "vcrop",
971
+ &dphy->fmt_inf[idx].vcrop);
972
+ if (ret)
973
+ dev_warn(dphy->dev, "Can not get vcrop!");
974
+
679975 idx++;
680976 }
681977
....@@ -684,6 +980,69 @@
684980 }
685981 dphy->fmt_inf_num = idx;
686982
983
+ prev_node = NULL;
984
+ /* get virtual sub sensor */
985
+ node = NULL;
986
+ while (!IS_ERR_OR_NULL(node =
987
+ of_get_next_child(parent_node, prev_node))) {
988
+ if (!strncasecmp(node->name,
989
+ "virtual-sub-sensor-config",
990
+ strlen("virtual-sub-sensor-config"))) {
991
+
992
+ if (sub_idx >= 4) {
993
+ dev_err(dphy->dev, "get too mach sub_sensor node, max 4.\n");
994
+ break;
995
+ }
996
+
997
+ ret = of_property_read_u32(node, "id",
998
+ &dphy->sub_sensor[sub_idx].id);
999
+ if (ret)
1000
+ dev_warn(dphy->dev, "Can not get sub sensor id!");
1001
+ else
1002
+ dev_info(dphy->dev, "get sub sensor id:%d",
1003
+ dphy->sub_sensor[sub_idx].id);
1004
+
1005
+ ret = of_property_read_u32(node, "in_mipi",
1006
+ &dphy->sub_sensor[sub_idx].in_mipi);
1007
+ if (ret)
1008
+ dev_warn(dphy->dev, "Can not get sub sensor in_mipi!");
1009
+ else
1010
+ dev_info(dphy->dev, "get sub sensor in_mipi:%d",
1011
+ dphy->sub_sensor[sub_idx].in_mipi);
1012
+
1013
+ ret = of_property_read_u32(node, "out_mipi",
1014
+ &dphy->sub_sensor[sub_idx].out_mipi);
1015
+ if (ret)
1016
+ dev_warn(dphy->dev, "Can not get sub sensor out_mipi!");
1017
+ else
1018
+ dev_info(dphy->dev, "get sub sensor out_mipi:%d",
1019
+ dphy->sub_sensor[sub_idx].out_mipi);
1020
+
1021
+ sub_idx++;
1022
+ }
1023
+
1024
+ of_node_put(prev_node);
1025
+ prev_node = node;
1026
+ }
1027
+ dphy->sub_sensor_num = sub_idx;
1028
+
1029
+ node = of_parse_phandle(parent_node, "link-sensor", 0);
1030
+ if (node) {
1031
+ dev_info(dphy->dev, "get link sensor node:%s\n", node->full_name);
1032
+ link_sensor_client =
1033
+ of_find_i2c_device_by_node(node);
1034
+ of_node_put(node);
1035
+ if (IS_ERR_OR_NULL(link_sensor_client)) {
1036
+ dev_err(dphy->dev, "can not get link sensor node\n");
1037
+ } else {
1038
+ dphy->link_sensor_client = link_sensor_client;
1039
+ dev_info(dphy->dev, "get link sensor client\n");
1040
+ }
1041
+ } else {
1042
+ dev_err(dphy->dev, "can not get link-sensor node\n");
1043
+ }
1044
+ /* get virtual sub sensor end */
1045
+
6871046 of_node_put(prev_node);
6881047 of_node_put(parent_node);
6891048