hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/drivers/media/i2c/dw9763.c
....@@ -15,12 +15,14 @@
1515 #include <media/v4l2-device.h>
1616 #include <linux/rk_vcm_head.h>
1717 #include <linux/compat.h>
18
+#include <linux/regulator/consumer.h>
1819
19
-#define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x0)
20
+#define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x1)
2021 #define DW9763_NAME "dw9763"
2122
2223 #define DW9763_MAX_CURRENT 120U
2324 #define DW9763_MAX_REG 1023U
25
+#define DW9763_GRADUAL_MOVELENS_STEPS 32
2426
2527 #define DW9763_DEFAULT_START_CURRENT 20
2628 #define DW9763_DEFAULT_RATED_CURRENT 90
....@@ -40,6 +42,10 @@
4042 SAC4_MODE = 5,
4143 DIRECT_MODE,
4244 };
45
+
46
+static int debug;
47
+module_param(debug, int, 0644);
48
+MODULE_PARM_DESC(debug, "debug level (0-2)");
4349
4450 /* dw9763 device structure */
4551 struct dw9763_device {
....@@ -69,6 +75,8 @@
6975 struct rk_cam_vcm_cfg vcm_cfg;
7076 int max_ma;
7177 struct mutex lock;
78
+ struct regulator *supply;
79
+ bool power_on;
7280 };
7381
7482 static inline struct dw9763_device *to_dw9763_vcm(struct v4l2_ctrl *ctrl)
....@@ -88,6 +96,7 @@
8896 u8 buf[5];
8997 u8 *val_p;
9098 __be32 val_be;
99
+ struct dw9763_device *dev_vcm = i2c_get_clientdata(client);
91100
92101 if (len > 4)
93102 return -EINVAL;
....@@ -106,7 +115,7 @@
106115 dev_err(&client->dev, "Failed to write 0x%04x,0x%x\n", reg, val);
107116 return -EIO;
108117 }
109
- dev_dbg(&client->dev, "succeed to write 0x%04x,0x%x\n", reg, val);
118
+ v4l2_dbg(1, debug, &dev_vcm->sd, "succeed to write 0x%04x,0x%x\n", reg, val);
110119
111120 return 0;
112121 }
....@@ -120,6 +129,7 @@
120129 u8 *data_be_p;
121130 __be32 data_be = 0;
122131 int ret;
132
+ struct dw9763_device *dev_vcm = i2c_get_clientdata(client);
123133
124134 if (len > 4 || !len)
125135 return -EINVAL;
....@@ -142,6 +152,8 @@
142152 return -EIO;
143153
144154 *val = be32_to_cpu(data_be);
155
+
156
+ v4l2_dbg(1, debug, &dev_vcm->sd, "succeed to read 0x%04x,0x%x\n", reg, *val);
145157
146158 return 0;
147159 }
....@@ -204,11 +216,11 @@
204216 break;
205217 }
206218
207
- dev_dbg(&client->dev,
219
+ v4l2_dbg(1, debug, &dev_vcm->sd,
208220 "%s: vcm_movefull_t is: %d us\n",
209221 __func__, move_time_us);
210222
211
- return move_time_us;
223
+ return ((move_time_us + 500) / 1000);
212224 }
213225
214226 static int dw9763_set_dac(struct dw9763_device *dev_vcm,
....@@ -228,7 +240,7 @@
228240 ret = dw9763_write_reg(client, 0x03, 2, dest_dac);
229241 if (ret != 0)
230242 goto err;
231
- dev_dbg(&client->dev,
243
+ v4l2_dbg(1, debug, &dev_vcm->sd,
232244 "%s: set reg val %d\n", __func__, dest_dac);
233245
234246 return ret;
....@@ -249,7 +261,7 @@
249261 goto err;
250262
251263 *cur_dac = abs_step;
252
- dev_dbg(&client->dev, "%s: get dac %d\n", __func__, *cur_dac);
264
+ v4l2_dbg(1, debug, &dev_vcm->sd, "%s: get dac %d\n", __func__, *cur_dac);
253265
254266 return 0;
255267
....@@ -279,7 +291,7 @@
279291 abs_step = 0;
280292
281293 *cur_pos = abs_step;
282
- dev_dbg(&client->dev, "%s: get position %d\n", __func__, *cur_pos);
294
+ v4l2_dbg(1, debug, &dev_vcm->sd, "%s: get position %d\n", __func__, *cur_pos);
283295 return 0;
284296
285297 err:
....@@ -293,7 +305,6 @@
293305 {
294306 int ret;
295307 unsigned int position = 0;
296
- struct i2c_client *client = dev_vcm->client;
297308
298309 if (dest_pos >= VCMDRV_MAX_LOG)
299310 position = dev_vcm->start_current;
....@@ -308,7 +319,8 @@
308319 dev_vcm->current_related_pos = dest_pos;
309320
310321 ret = dw9763_set_dac(dev_vcm, position);
311
- dev_dbg(&client->dev, "%s: set position %d, dac %d\n", __func__, dest_pos, position);
322
+ v4l2_dbg(1, debug, &dev_vcm->sd, "%s: set position %d, dac %d\n",
323
+ __func__, dest_pos, position);
312324
313325 return ret;
314326 }
....@@ -331,7 +343,7 @@
331343 long mv_us;
332344 int ret = 0;
333345
334
- dev_dbg(&client->dev, "ctrl->id: 0x%x, ctrl->val: 0x%x\n",
346
+ v4l2_dbg(1, debug, &dev_vcm->sd, "ctrl->id: 0x%x, ctrl->val: 0x%x\n",
335347 ctrl->id, ctrl->val);
336348
337349 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
....@@ -345,9 +357,9 @@
345357
346358 ret = dw9763_set_pos(dev_vcm, dest_pos);
347359
348
- dev_vcm->move_us = dev_vcm->vcm_movefull_t;
360
+ dev_vcm->move_us = dev_vcm->vcm_movefull_t * 1000;
349361
350
- dev_dbg(&client->dev,
362
+ v4l2_dbg(1, debug, &dev_vcm->sd,
351363 "dest_pos %d, move_us %ld\n",
352364 dest_pos, dev_vcm->move_us);
353365
....@@ -373,9 +385,19 @@
373385 .s_ctrl = dw9763_set_ctrl,
374386 };
375387
388
+static int dw9763_init(struct i2c_client *client);
376389 static int dw9763_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
377390 {
378391 int rval;
392
+ struct dw9763_device *dev_vcm = sd_to_dw9763_vcm(sd);
393
+ unsigned int move_time;
394
+ int dac = dev_vcm->start_current;
395
+ struct i2c_client *client = dev_vcm->client;
396
+
397
+#ifdef CONFIG_PM
398
+ v4l2_info(sd, "%s: enter, power.usage_count(%d)!\n", __func__,
399
+ atomic_read(&sd->dev->power.usage_count));
400
+#endif
379401
380402 rval = pm_runtime_get_sync(sd->dev);
381403 if (rval < 0) {
....@@ -383,12 +405,72 @@
383405 return rval;
384406 }
385407
408
+ dw9763_init(client);
409
+
410
+ v4l2_dbg(1, debug, sd, "%s: current_lens_pos %d, current_related_pos %d\n",
411
+ __func__, dev_vcm->current_lens_pos, dev_vcm->current_related_pos);
412
+
413
+ move_time = 1000 * dw9763_move_time(dev_vcm, DW9763_GRADUAL_MOVELENS_STEPS);
414
+ while (dac <= dev_vcm->current_lens_pos) {
415
+ dw9763_set_dac(dev_vcm, dac);
416
+ usleep_range(move_time, move_time + 100);
417
+ dac += DW9763_GRADUAL_MOVELENS_STEPS;
418
+ if (dac > dev_vcm->current_lens_pos)
419
+ break;
420
+ }
421
+
422
+ if (dac > dev_vcm->current_lens_pos) {
423
+ dac = dev_vcm->current_lens_pos;
424
+ dw9763_set_dac(dev_vcm, dac);
425
+ }
426
+
427
+#ifdef CONFIG_PM
428
+ v4l2_info(sd, "%s: exit, power.usage_count(%d)!\n", __func__,
429
+ atomic_read(&sd->dev->power.usage_count));
430
+#endif
386431 return 0;
387432 }
388433
389434 static int dw9763_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
390435 {
436
+ struct dw9763_device *dev_vcm = sd_to_dw9763_vcm(sd);
437
+ int dac = dev_vcm->current_lens_pos;
438
+ unsigned int move_time;
439
+ int ret;
440
+ struct i2c_client *client = dev_vcm->client;
441
+
442
+#ifdef CONFIG_PM
443
+ v4l2_info(sd, "%s: enter, power.usage_count(%d)!\n", __func__,
444
+ atomic_read(&sd->dev->power.usage_count));
445
+#endif
446
+
447
+ v4l2_dbg(1, debug, sd, "%s: current_lens_pos %d, current_related_pos %d\n",
448
+ __func__, dev_vcm->current_lens_pos, dev_vcm->current_related_pos);
449
+
450
+ dac -= DW9763_GRADUAL_MOVELENS_STEPS;
451
+ move_time = 1000 * dw9763_move_time(dev_vcm, DW9763_GRADUAL_MOVELENS_STEPS);
452
+ while (dac >= DW9763_GRADUAL_MOVELENS_STEPS) {
453
+ dw9763_set_dac(dev_vcm, dac);
454
+ usleep_range(move_time, move_time + 1000);
455
+ dac -= DW9763_GRADUAL_MOVELENS_STEPS;
456
+ if (dac <= 0)
457
+ break;
458
+ }
459
+
460
+ if (dac < DW9763_GRADUAL_MOVELENS_STEPS) {
461
+ dac = DW9763_GRADUAL_MOVELENS_STEPS / 2;
462
+ dw9763_set_dac(dev_vcm, dac);
463
+ }
464
+ /* set to power down mode */
465
+ ret = dw9763_write_reg(client, 0x02, 1, 0x01);
466
+ if (ret)
467
+ dev_err(&client->dev, "failed to set power down mode!\n");
468
+
391469 pm_runtime_put(sd->dev);
470
+#ifdef CONFIG_PM
471
+ v4l2_info(sd, "%s: exit, power.usage_count(%d)!\n", __func__,
472
+ atomic_read(&sd->dev->power.usage_count));
473
+#endif
392474
393475 return 0;
394476 }
....@@ -442,7 +524,7 @@
442524 vcm_tim->vcm_end_t.tv_sec = dev_vcm->end_move_tv.tv_sec;
443525 vcm_tim->vcm_end_t.tv_usec = dev_vcm->end_move_tv.tv_usec;
444526
445
- dev_dbg(&client->dev, "dw9763_get_move_res 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
527
+ v4l2_dbg(1, debug, &dev_vcm->sd, "dw9763_get_move_res 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
446528 vcm_tim->vcm_start_t.tv_sec,
447529 vcm_tim->vcm_start_t.tv_usec,
448530 vcm_tim->vcm_end_t.tv_sec,
....@@ -634,13 +716,50 @@
634716 }
635717 #endif
636718
637
-static int __dw9763_set_power(struct dw9763_device *dw9763_dev, bool on)
719
+static int __dw9763_set_power(struct dw9763_device *dw9763, bool on)
638720 {
639
- if (dw9763_dev->power_gpio)
640
- gpiod_direction_output(dw9763_dev->power_gpio, on);
641
- usleep_range(10000, 11000);
721
+ struct i2c_client *client = dw9763->client;
722
+ int ret = 0;
642723
643
- return 0;
724
+ dev_info(&client->dev, "%s(%d) on(%d)\n", __func__, __LINE__, on);
725
+
726
+ if (dw9763->power_on == !!on)
727
+ goto unlock_and_return;
728
+
729
+ if (on) {
730
+ ret = regulator_enable(dw9763->supply);
731
+ if (ret < 0) {
732
+ dev_err(&client->dev, "Failed to enable regulator\n");
733
+ goto unlock_and_return;
734
+ }
735
+ dw9763->power_on = true;
736
+ } else {
737
+ ret = regulator_disable(dw9763->supply);
738
+ if (ret < 0) {
739
+ dev_err(&client->dev, "Failed to disable regulator\n");
740
+ goto unlock_and_return;
741
+ }
742
+ dw9763->power_on = false;
743
+ }
744
+
745
+unlock_and_return:
746
+ return ret;
747
+}
748
+
749
+static int dw9763_configure_regulator(struct dw9763_device *dw9763)
750
+{
751
+ struct i2c_client *client = dw9763->client;
752
+ int ret = 0;
753
+
754
+ dw9763->supply = devm_regulator_get(&client->dev, "avdd");
755
+ if (IS_ERR(dw9763->supply)) {
756
+ ret = PTR_ERR(dw9763->supply);
757
+ if (ret != -EPROBE_DEFER)
758
+ dev_err(&client->dev, "could not get regulator avdd\n");
759
+ return ret;
760
+ }
761
+ dw9763->power_on = false;
762
+ return ret;
644763 }
645764
646765 static int __maybe_unused dw9763_check_id(struct dw9763_device *dw9763_dev)
....@@ -661,20 +780,6 @@
661780 dev_info(&dw9763_dev->client->dev,
662781 "Detected dw9763 vcm id:0x%x\n", DW9763_CHIP_ID);
663782 return 0;
664
-}
665
-static int dw9763_probe_init(struct i2c_client *client)
666
-{
667
- int ret = 0;
668
-
669
- /* Default goto power down mode when finished probe */
670
- ret = dw9763_write_reg(client, 0x02, 1, 0x01);
671
- if (ret)
672
- goto err;
673
-
674
- return 0;
675
-err:
676
- dev_err(&client->dev, "probe init failed with error %d\n", ret);
677
- return -1;
678783 }
679784
680785 static int dw9763_probe(struct i2c_client *client,
....@@ -765,9 +870,11 @@
765870 dev_warn(&client->dev,
766871 "Failed to get power-gpios, maybe no use\n");
767872 }
768
-
769
- /* enter power down mode */
770
- dw9763_probe_init(client);
873
+ ret = dw9763_configure_regulator(dw9763_dev);
874
+ if (ret) {
875
+ dev_err(&client->dev, "Failed to get power regulator!\n");
876
+ return ret;
877
+ }
771878
772879 v4l2_i2c_subdev_init(&dw9763_dev->sd, client, &dw9763_ops);
773880 dw9763_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
....@@ -780,6 +887,10 @@
780887 ret = media_entity_pads_init(&dw9763_dev->sd.entity, 0, NULL);
781888 if (ret < 0)
782889 goto err_cleanup;
890
+
891
+ ret = dw9763_check_id(dw9763_dev);
892
+ if (ret)
893
+ goto err_power_off;
783894
784895 sd = &dw9763_dev->sd;
785896 sd->entity.function = MEDIA_ENT_F_LENS;
....@@ -823,6 +934,9 @@
823934 dev_info(&client->dev, "probing successful\n");
824935
825936 return 0;
937
+err_power_off:
938
+ __dw9763_set_power(dw9763_dev, false);
939
+
826940 err_cleanup:
827941 dw9763_subdev_cleanup(dw9763_dev);
828942
....@@ -847,21 +961,18 @@
847961 {
848962 struct dw9763_device *dev_vcm = i2c_get_clientdata(client);
849963 int ret = 0;
850
- u32 ring = 0;
851964 u32 mode_val = 0;
852965 u32 algo_time = 0;
853966
967
+ if (dev_vcm->step_mode == DIRECT_MODE)
968
+ return 0;
854969
855
- /* Delay 200us~300us */
856
- usleep_range(200, 300);
857970 ret = dw9763_write_reg(client, 0x02, 1, 0x00);
858971 if (ret)
859972 goto err;
860
- usleep_range(100, 200);
861973
862
- if (dev_vcm->step_mode != DIRECT_MODE)
863
- ring = 0x02;
864
- ret = dw9763_write_reg(client, 0x02, 1, ring);
974
+ usleep_range(200, 300);
975
+ ret = dw9763_write_reg(client, 0x02, 1, 0x02);
865976 if (ret)
866977 goto err;
867978 switch (dev_vcm->step_mode) {
....@@ -895,13 +1006,15 @@
8951006 static int __maybe_unused dw9763_vcm_suspend(struct device *dev)
8961007 {
8971008 struct i2c_client *client = to_i2c_client(dev);
898
- int ret = 0;
1009
+ struct dw9763_device *dev_vcm = i2c_get_clientdata(client);
1010
+ struct v4l2_subdev *sd = &(dev_vcm->sd);
8991011
900
- /* set to power down mode */
901
- ret = dw9763_write_reg(client, 0x02, 1, 0x01);
902
- if (ret)
903
- dev_err(&client->dev, "failed to set power down mode!\n");
1012
+#ifdef CONFIG_PM
1013
+ v4l2_dbg(1, debug, sd, "%s: enter, power.usage_count(%d)!\n", __func__,
1014
+ atomic_read(&sd->dev->power.usage_count));
1015
+#endif
9041016
1017
+ __dw9763_set_power(dev_vcm, false);
9051018 return 0;
9061019 }
9071020
....@@ -909,9 +1022,14 @@
9091022 {
9101023 struct i2c_client *client = to_i2c_client(dev);
9111024 struct dw9763_device *dev_vcm = i2c_get_clientdata(client);
1025
+ struct v4l2_subdev *sd = &(dev_vcm->sd);
9121026
913
- dw9763_init(client);
914
- dw9763_set_pos(dev_vcm, dev_vcm->current_related_pos);
1027
+#ifdef CONFIG_PM
1028
+ v4l2_dbg(1, debug, sd, "%s: enter, power.usage_count(%d)!\n", __func__,
1029
+ atomic_read(&sd->dev->power.usage_count));
1030
+#endif
1031
+ __dw9763_set_power(dev_vcm, true);
1032
+
9151033 return 0;
9161034 }
9171035