forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/media/i2c/sc4336.c
....@@ -5,6 +5,7 @@
55 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
66 *
77 * V0.0X01.0X01 first version
8
+ * V0.0X01.0X02 support fastboot
89 */
910
1011 //#define DEBUG
....@@ -26,6 +27,7 @@
2627 #include <media/v4l2-ctrls.h>
2728 #include <media/v4l2-subdev.h>
2829 #include <linux/pinctrl/consumer.h>
30
+#include "../platform/rockchip/isp/rkisp_tb_helper.h"
2931
3032 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x01)
3133
....@@ -110,14 +112,6 @@
110112 u8 val;
111113 };
112114
113
-enum sc4336_max_pad {
114
- PAD0, /* link to isp */
115
- PAD1, /* link to csi wr0 | hdr x2:L x3:M */
116
- PAD2, /* link to csi wr1 | hdr x3:L */
117
- PAD3, /* link to csi wr2 | hdr x2:M x3:S */
118
- PAD_MAX,
119
-};
120
-
121115 struct sc4336_mode {
122116 u32 bus_fmt;
123117 u32 width;
....@@ -155,11 +149,14 @@
155149 bool streaming;
156150 bool power_on;
157151 const struct sc4336_mode *cur_mode;
152
+ struct v4l2_fract cur_fps;
158153 u32 module_index;
159154 const char *module_facing;
160155 const char *module_name;
161156 const char *len_name;
162157 u32 cur_vts;
158
+ bool is_thunderboot;
159
+ bool is_first_streamoff;
163160 };
164161
165162 #define to_sc4336(sd) container_of(sd, struct sc4336, subdev)
....@@ -314,6 +311,8 @@
314311 {0x5aed, 0x2c},
315312 {0x36e9, 0x53},
316313 {0x37f9, 0x23},
314
+ {0x320e, 0x07},
315
+ {0x320f, 0x08},
317316 {REG_NULL, 0x00},
318317 };
319318
....@@ -323,11 +322,11 @@
323322 .height = 1440,
324323 .max_fps = {
325324 .numerator = 10000,
326
- .denominator = 300000,
325
+ .denominator = 250000,
327326 },
328327 .exp_def = 0x0080,
329328 .hts_def = 0x0578 * 2,
330
- .vts_def = 0x05dc,
329
+ .vts_def = 0x0708,
331330 .bus_fmt = MEDIA_BUS_FMT_SBGGR10_1X10,
332331 .reg_list = sc4336_linear_10_2560x1440_regs,
333332 .hdr_mode = NO_HDR,
....@@ -552,6 +551,7 @@
552551 __v4l2_ctrl_modify_range(sc4336->vblank, vblank_def,
553552 SC4336_VTS_MAX - mode->height,
554553 1, vblank_def);
554
+ sc4336->cur_fps = mode->max_fps;
555555 }
556556
557557 mutex_unlock(&sc4336->mutex);
....@@ -644,14 +644,16 @@
644644 struct sc4336 *sc4336 = to_sc4336(sd);
645645 const struct sc4336_mode *mode = sc4336->cur_mode;
646646
647
- mutex_lock(&sc4336->mutex);
648
- fi->interval = mode->max_fps;
649
- mutex_unlock(&sc4336->mutex);
647
+ if (sc4336->streaming)
648
+ fi->interval = sc4336->cur_fps;
649
+ else
650
+ fi->interval = mode->max_fps;
650651
651652 return 0;
652653 }
653654
654655 static int sc4336_g_mbus_config(struct v4l2_subdev *sd,
656
+ unsigned int pad_id,
655657 struct v4l2_mbus_config *config)
656658 {
657659 struct sc4336 *sc4336 = to_sc4336(sd);
....@@ -665,7 +667,7 @@
665667 if (mode->hdr_mode == HDR_X3)
666668 val |= V4L2_MBUS_CSI2_CHANNEL_2;
667669
668
- config->type = V4L2_MBUS_CSI2;
670
+ config->type = V4L2_MBUS_CSI2_DPHY;
669671 config->flags = val;
670672
671673 return 0;
....@@ -831,15 +833,16 @@
831833 static int __sc4336_start_stream(struct sc4336 *sc4336)
832834 {
833835 int ret;
836
+ if (!sc4336->is_thunderboot) {
837
+ ret = sc4336_write_array(sc4336->client, sc4336->cur_mode->reg_list);
838
+ if (ret)
839
+ return ret;
834840
835
- ret = sc4336_write_array(sc4336->client, sc4336->cur_mode->reg_list);
836
- if (ret)
837
- return ret;
838
-
839
- /* In case these controls are set before streaming */
840
- ret = __v4l2_ctrl_handler_setup(&sc4336->ctrl_handler);
841
- if (ret)
842
- return ret;
841
+ /* In case these controls are set before streaming */
842
+ ret = __v4l2_ctrl_handler_setup(&sc4336->ctrl_handler);
843
+ if (ret)
844
+ return ret;
845
+ }
843846
844847 return sc4336_write_reg(sc4336->client, SC4336_REG_CTRL_MODE,
845848 SC4336_REG_VALUE_08BIT, SC4336_MODE_STREAMING);
....@@ -847,10 +850,15 @@
847850
848851 static int __sc4336_stop_stream(struct sc4336 *sc4336)
849852 {
853
+ if (sc4336->is_thunderboot) {
854
+ sc4336->is_first_streamoff = true;
855
+ pm_runtime_put(&sc4336->client->dev);
856
+ }
850857 return sc4336_write_reg(sc4336->client, SC4336_REG_CTRL_MODE,
851858 SC4336_REG_VALUE_08BIT, SC4336_MODE_SW_STANDBY);
852859 }
853860
861
+static int __sc4336_power_on(struct sc4336 *sc4336);
854862 static int sc4336_s_stream(struct v4l2_subdev *sd, int on)
855863 {
856864 struct sc4336 *sc4336 = to_sc4336(sd);
....@@ -863,6 +871,11 @@
863871 goto unlock_and_return;
864872
865873 if (on) {
874
+ if (sc4336->is_thunderboot && rkisp_tb_get_state() == RKISP_TB_NG) {
875
+ sc4336->is_thunderboot = false;
876
+ __sc4336_power_on(sc4336);
877
+ }
878
+
866879 ret = pm_runtime_get_sync(&client->dev);
867880 if (ret < 0) {
868881 pm_runtime_put_noidle(&client->dev);
....@@ -907,11 +920,13 @@
907920 goto unlock_and_return;
908921 }
909922
910
- ret = sc4336_write_array(sc4336->client, sc4336_global_regs);
911
- if (ret) {
912
- v4l2_err(sd, "could not set init registers\n");
913
- pm_runtime_put_noidle(&client->dev);
914
- goto unlock_and_return;
923
+ if (!sc4336->is_thunderboot) {
924
+ ret = sc4336_write_array(sc4336->client, sc4336_global_regs);
925
+ if (ret) {
926
+ v4l2_err(sd, "could not set init registers\n");
927
+ pm_runtime_put_noidle(&client->dev);
928
+ goto unlock_and_return;
929
+ }
915930 }
916931
917932 sc4336->power_on = true;
....@@ -954,6 +969,9 @@
954969 dev_err(dev, "Failed to enable xvclk\n");
955970 return ret;
956971 }
972
+ if (sc4336->is_thunderboot)
973
+ return 0;
974
+
957975 if (!IS_ERR(sc4336->reset_gpio))
958976 gpiod_set_value_cansleep(sc4336->reset_gpio, 0);
959977
....@@ -992,9 +1010,18 @@
9921010 int ret;
9931011 struct device *dev = &sc4336->client->dev;
9941012
1013
+ clk_disable_unprepare(sc4336->xvclk);
1014
+ if (sc4336->is_thunderboot) {
1015
+ if (sc4336->is_first_streamoff) {
1016
+ sc4336->is_thunderboot = false;
1017
+ sc4336->is_first_streamoff = false;
1018
+ } else {
1019
+ return;
1020
+ }
1021
+ }
1022
+
9951023 if (!IS_ERR(sc4336->pwdn_gpio))
9961024 gpiod_set_value_cansleep(sc4336->pwdn_gpio, 0);
997
- clk_disable_unprepare(sc4336->xvclk);
9981025 if (!IS_ERR(sc4336->reset_gpio))
9991026 gpiod_set_value_cansleep(sc4336->reset_gpio, 0);
10001027 if (!IS_ERR_OR_NULL(sc4336->pins_sleep)) {
....@@ -1006,7 +1033,7 @@
10061033 regulator_bulk_disable(SC4336_NUM_SUPPLIES, sc4336->supplies);
10071034 }
10081035
1009
-static int sc4336_runtime_resume(struct device *dev)
1036
+static int __maybe_unused sc4336_runtime_resume(struct device *dev)
10101037 {
10111038 struct i2c_client *client = to_i2c_client(dev);
10121039 struct v4l2_subdev *sd = i2c_get_clientdata(client);
....@@ -1015,7 +1042,7 @@
10151042 return __sc4336_power_on(sc4336);
10161043 }
10171044
1018
-static int sc4336_runtime_suspend(struct device *dev)
1045
+static int __maybe_unused sc4336_runtime_suspend(struct device *dev)
10191046 {
10201047 struct i2c_client *client = to_i2c_client(dev);
10211048 struct v4l2_subdev *sd = i2c_get_clientdata(client);
....@@ -1085,7 +1112,6 @@
10851112 static const struct v4l2_subdev_video_ops sc4336_video_ops = {
10861113 .s_stream = sc4336_s_stream,
10871114 .g_frame_interval = sc4336_g_frame_interval,
1088
- .g_mbus_config = sc4336_g_mbus_config,
10891115 };
10901116
10911117 static const struct v4l2_subdev_pad_ops sc4336_pad_ops = {
....@@ -1094,6 +1120,7 @@
10941120 .enum_frame_interval = sc4336_enum_frame_interval,
10951121 .get_fmt = sc4336_get_fmt,
10961122 .set_fmt = sc4336_set_fmt,
1123
+ .get_mbus_config = sc4336_g_mbus_config,
10971124 };
10981125
10991126 static const struct v4l2_subdev_ops sc4336_subdev_ops = {
....@@ -1101,6 +1128,14 @@
11011128 .video = &sc4336_video_ops,
11021129 .pad = &sc4336_pad_ops,
11031130 };
1131
+
1132
+static void sc4336_modify_fps_info(struct sc4336 *sc4336)
1133
+{
1134
+ const struct sc4336_mode *mode = sc4336->cur_mode;
1135
+
1136
+ sc4336->cur_fps.denominator = mode->max_fps.denominator * mode->vts_def /
1137
+ sc4336->cur_vts;
1138
+}
11041139
11051140 static int sc4336_set_ctrl(struct v4l2_ctrl *ctrl)
11061141 {
....@@ -1164,6 +1199,7 @@
11641199 (ctrl->val + sc4336->cur_mode->height)
11651200 & 0xff);
11661201 sc4336->cur_vts = ctrl->val + sc4336->cur_mode->height;
1202
+ sc4336_modify_fps_info(sc4336);
11671203 break;
11681204 case V4L2_CID_TEST_PATTERN:
11691205 ret = sc4336_enable_test_pattern(sc4336, ctrl->val);
....@@ -1231,6 +1267,7 @@
12311267 V4L2_CID_VBLANK, vblank_def,
12321268 SC4336_VTS_MAX - mode->height,
12331269 1, vblank_def);
1270
+ sc4336->cur_fps = mode->max_fps;
12341271 exposure_max = mode->vts_def - 8;
12351272 sc4336->exposure = v4l2_ctrl_new_std(handler, &sc4336_ctrl_ops,
12361273 V4L2_CID_EXPOSURE, SC4336_EXPOSURE_MIN,
....@@ -1272,6 +1309,11 @@
12721309 struct device *dev = &sc4336->client->dev;
12731310 u32 id = 0;
12741311 int ret;
1312
+
1313
+ if (sc4336->is_thunderboot) {
1314
+ dev_info(dev, "Enable thunderboot mode, skip sensor id check\n");
1315
+ return 0;
1316
+ }
12751317
12761318 ret = sc4336_read_reg(client, SC4336_REG_CHIP_ID,
12771319 SC4336_REG_VALUE_16BIT, &id);
....@@ -1329,6 +1371,7 @@
13291371 return -EINVAL;
13301372 }
13311373
1374
+ sc4336->is_thunderboot = IS_ENABLED(CONFIG_VIDEO_ROCKCHIP_THUNDER_BOOT_ISP);
13321375 sc4336->client = client;
13331376 sc4336->cur_mode = &supported_modes[0];
13341377
....@@ -1338,14 +1381,23 @@
13381381 return -EINVAL;
13391382 }
13401383
1341
- sc4336->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1342
- if (IS_ERR(sc4336->reset_gpio))
1343
- dev_warn(dev, "Failed to get reset-gpios\n");
1384
+ if (sc4336->is_thunderboot) {
1385
+ sc4336->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_ASIS);
1386
+ if (IS_ERR(sc4336->reset_gpio))
1387
+ dev_warn(dev, "Failed to get reset-gpios\n");
13441388
1345
- sc4336->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
1346
- if (IS_ERR(sc4336->pwdn_gpio))
1347
- dev_warn(dev, "Failed to get pwdn-gpios\n");
1389
+ sc4336->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_ASIS);
1390
+ if (IS_ERR(sc4336->pwdn_gpio))
1391
+ dev_warn(dev, "Failed to get pwdn-gpios\n");
1392
+ } else {
1393
+ sc4336->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1394
+ if (IS_ERR(sc4336->reset_gpio))
1395
+ dev_warn(dev, "Failed to get reset-gpios\n");
13481396
1397
+ sc4336->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
1398
+ if (IS_ERR(sc4336->pwdn_gpio))
1399
+ dev_warn(dev, "Failed to get pwdn-gpios\n");
1400
+ }
13491401 sc4336->pinctrl = devm_pinctrl_get(dev);
13501402 if (!IS_ERR(sc4336->pinctrl)) {
13511403 sc4336->pins_default =
....@@ -1415,7 +1467,10 @@
14151467
14161468 pm_runtime_set_active(dev);
14171469 pm_runtime_enable(dev);
1418
- pm_runtime_idle(dev);
1470
+ if (sc4336->is_thunderboot)
1471
+ pm_runtime_get_sync(dev);
1472
+ else
1473
+ pm_runtime_idle(dev);
14191474
14201475 return 0;
14211476
....@@ -1487,7 +1542,11 @@
14871542 i2c_del_driver(&sc4336_i2c_driver);
14881543 }
14891544
1545
+#if defined(CONFIG_VIDEO_ROCKCHIP_THUNDER_BOOT_ISP) && !defined(CONFIG_INITCALL_ASYNC)
1546
+subsys_initcall(sensor_mod_init);
1547
+#else
14901548 device_initcall_sync(sensor_mod_init);
1549
+#endif
14911550 module_exit(sensor_mod_exit);
14921551
14931552 MODULE_DESCRIPTION("smartsens sc4336 sensor driver");