hc
2023-11-22 f743a7adbd6e230d66a6206fa115b59fec2d88eb
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
// SPDX-License-Identifier: GPL-2.0
/*
 * ov7725 driver
 *
 * Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd.
 */
 
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/sysfs.h>
#include <media/media-entity.h>
#include <media/v4l2-async.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-subdev.h>
 
#define REG_CHIP_ID_H            0x0a
#define REG_CHIP_ID_L            0x0b
#define CHIP_ID_H            0x77
#define CHIP_ID_L            0x21
 
#define REG_NULL            0xFF
 
#define ov7725_XVCLK_FREQ        24000000
 
static const char * const ov7725_supply_names[] = {
   "avdd",
   "dovdd",
   "dvdd",
};
 
#define ov7725_NUM_SUPPLIES ARRAY_SIZE(ov7725_supply_names)
 
struct regval {
   u8 addr;
   u8 val;
};
 
struct ov7725_mode {
   u32 width;
   u32 height;
   const struct regval *reg_list;
};
 
struct ov7725 {
   struct i2c_client    *client;
   struct clk        *xvclk;
   struct gpio_desc    *reset_gpio;
   struct gpio_desc    *pwdn_gpio;
   struct regulator_bulk_data supplies[ov7725_NUM_SUPPLIES];
 
   bool            streaming;
   struct mutex        mutex; /* lock to serialize v4l2 callback */
   struct v4l2_subdev    subdev;
   struct media_pad    pad;
 
   const struct ov7725_mode *cur_mode;
};
 
#define to_ov7725(sd) container_of(sd, struct ov7725, subdev)
 
/* 30fps at 24MHz input clock,4x maximum gain */
static struct regval ov7725_640x480_30fps[] = {
   {0x12, 0x80},
   {0x3d, 0x03},
   {0x17, 0x25}, /* Raw: 0x17,0x22 */
   {0x18, 0xa4}, /* Raw: 0x18,0xa4 */
   {0x19, 0x06}, /* Raw: 0x19,0x07 */
   {0x1a, 0xf0},
   {0x32, 0x60}, /* Raw: 0x32,0x00 */
   {0x29, 0xa0},
   {0x2c, 0xf0},
   {0x2a, 0x00},
   {0x11, 0x01},
   {0x42, 0x7f},
   {0x4d, 0x00},
   {0x63, 0xe0},
   {0x64, 0xff},
   {0x65, 0x20},
   {0x66, 0x00},
   {0x67, 0x48},
   {0x13, 0xf0},
   {0x0d, 0x41},
   {0x0f, 0xc5},
 
   {0x14, 0x17}, /* 0x14,0x11 */
   {0x22, 0x3f},
   {0x23, 0x07},
   {0x24, 0x44},
   {0x25, 0x3c},
   {0x26, 0xa1},
   {0x2b, 0x00},
   {0x6b, 0xaa},
   {0x13, 0xff},
   {0x90, 0x05},
   {0x91, 0x01},
   {0x92, 0x03},
   {0x93, 0x00},
   {0x94, 0x40},
   {0x95, 0x40},
   {0x96, 0x00},
   {0x97, 0x11},
   {0x98, 0x2f},
   {0x99, 0x40},
   {0x9a, 0x9e},
   {0x9b, 0x08},
   {0x9c, 0x20},
   {0x9e, 0x81},
   {0xa6, 0x06},
   {0x7e, 0x0c},
   {0x7f, 0x16},
   {0x80, 0x2a},
   {0x81, 0x4e},
   {0x82, 0x61},
   {0x83, 0x6f},
   {0x84, 0x7b},
   {0x85, 0x86},
   {0x86, 0x8e},
   {0x87, 0x97},
   {0x88, 0xa4},
   {0x89, 0xaf},
   {0x8a, 0xc5},
   {0x8b, 0xd7},
   {0x8c, 0xe8},
   {0x8d, 0x20},
   {0x33, 0x00},
   {0x22, 0x99},
   {0x23, 0x03},
   {0x4a, 0x00},
   {0x49, 0x13},
   {0x47, 0x08},
   {0x4b, 0x14},
   {0x4c, 0x17},
   {0x46, 0x05},
   {0x0e, 0x65},
   {0x0c, 0x00},
   {REG_NULL, 0x0},
};
 
static struct regval ov7725_1600x1200_7fps[] = {
   {REG_NULL, 0x0},
};
 
static const struct ov7725_mode supported_modes[] = {
   {
       .width = 640,
       .height = 480,
       .reg_list = ov7725_640x480_30fps,
   },
   {
       .width = 1600,
       .height = 1200,
       .reg_list = ov7725_1600x1200_7fps,
   },
};
 
static int ov7725_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
   int ret;
 
   ret = i2c_smbus_write_byte_data(client, reg, val);
 
   if (ret < 0)
       dev_err(&client->dev, "write reg error: %d\n", ret);
 
   return ret;
}
 
static int ov7725_write_array(struct i2c_client *client,
   const struct regval *regs)
{
   int i, ret = 0;
 
   for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
       ret = ov7725_write_reg(client, regs[i].addr, regs[i].val);
 
   return ret;
}
 
static inline u8 ov7725_read_reg(struct i2c_client *client, u8 reg)
{
   return i2c_smbus_read_byte_data(client, reg);
}
 
static int ov7725_get_reso_dist(const struct ov7725_mode *mode,
   struct v4l2_mbus_framefmt *framefmt)
{
   return abs(mode->width - framefmt->width) +
       abs(mode->height - framefmt->height);
}
 
static const struct ov7725_mode *
ov7725_find_best_fit(struct v4l2_subdev_format *fmt)
{
   struct v4l2_mbus_framefmt *framefmt = &fmt->format;
   int dist;
   int cur_best_fit = 0;
   int cur_best_fit_dist = -1;
   size_t i;
 
   for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
       dist = ov7725_get_reso_dist(&supported_modes[i], framefmt);
       if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
           cur_best_fit_dist = dist;
           cur_best_fit = i;
       }
   }
 
   return &supported_modes[cur_best_fit];
}
 
static int ov7725_set_fmt(struct v4l2_subdev *sd,
              struct v4l2_subdev_pad_config *cfg,
              struct v4l2_subdev_format *fmt)
{
   struct ov7725 *ov7725 = to_ov7725(sd);
   const struct ov7725_mode *mode;
 
   mutex_lock(&ov7725->mutex);
 
   mode = ov7725_find_best_fit(fmt);
   fmt->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
   fmt->format.width = mode->width;
   fmt->format.height = mode->height;
   fmt->format.field = V4L2_FIELD_NONE;
   fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
   if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
       *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
#else
       mutex_unlock(&ov7725->mutex);
       return -ENOTTY;
#endif
   } else {
       ov7725->cur_mode = mode;
   }
 
   mutex_unlock(&ov7725->mutex);
 
   return 0;
}
 
static int ov7725_get_fmt(struct v4l2_subdev *sd,
              struct v4l2_subdev_pad_config *cfg,
              struct v4l2_subdev_format *fmt)
{
   struct ov7725 *ov7725 = to_ov7725(sd);
   const struct ov7725_mode *mode = ov7725->cur_mode;
 
   mutex_lock(&ov7725->mutex);
   if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
       fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
#else
       mutex_unlock(&ov7725->mutex);
       return -ENOTTY;
#endif
   } else {
       fmt->format.width = mode->width;
       fmt->format.height = mode->height;
       fmt->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
       fmt->format.field = V4L2_FIELD_NONE;
       fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
   }
   mutex_unlock(&ov7725->mutex);
 
   return 0;
}
 
static int ov7725_enum_mbus_code(struct v4l2_subdev *sd,
                 struct v4l2_subdev_pad_config *cfg,
                 struct v4l2_subdev_mbus_code_enum *code)
{
   if (code->index >= ARRAY_SIZE(supported_modes))
       return -EINVAL;
 
   code->code = MEDIA_BUS_FMT_UYVY8_2X8;
 
   return 0;
}
 
static int ov7725_enum_frame_sizes(struct v4l2_subdev *sd,
                   struct v4l2_subdev_pad_config *cfg,
                   struct v4l2_subdev_frame_size_enum *fse)
{
   u32 index = fse->index;
 
   if (index >= ARRAY_SIZE(supported_modes))
       return -EINVAL;
 
   fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
 
   fse->min_width  = supported_modes[index].width;
   fse->max_width  = supported_modes[index].width;
   fse->max_height = supported_modes[index].height;
   fse->min_height = supported_modes[index].height;
 
   return 0;
}
 
static int __ov7725_power_on(struct ov7725 *ov7725)
{
   int ret;
   struct device *dev = &ov7725->client->dev;
 
   if (!IS_ERR(ov7725->reset_gpio))
       gpiod_set_value_cansleep(ov7725->reset_gpio, 0);
 
   ret = regulator_bulk_enable(ov7725_NUM_SUPPLIES, ov7725->supplies);
   if (ret < 0) {
       dev_err(dev, "Failed to enable regulators\n");
       return ret;
   }
 
   if (!IS_ERR(ov7725->xvclk)) {
       ret = clk_prepare_enable(ov7725->xvclk);
       if (ret < 0) {
           dev_err(dev, "Failed to enable xvclk\n");
           return ret;
       }
   }
 
   if (!IS_ERR(ov7725->pwdn_gpio))
       gpiod_set_value_cansleep(ov7725->pwdn_gpio, 0);
 
   if (!IS_ERR(ov7725->reset_gpio))
       gpiod_set_value_cansleep(ov7725->reset_gpio, 1);
 
   return 0;
}
 
static void __ov7725_power_off(struct ov7725 *ov7725)
{
   if (!IS_ERR(ov7725->reset_gpio))
       gpiod_set_value_cansleep(ov7725->reset_gpio, 0);
   if (!IS_ERR(ov7725->pwdn_gpio))
       gpiod_set_value_cansleep(ov7725->pwdn_gpio, 1);
 
   if (!IS_ERR(ov7725->xvclk))
       clk_disable_unprepare(ov7725->xvclk);
 
   regulator_bulk_disable(ov7725_NUM_SUPPLIES, ov7725->supplies);
}
 
static int ov7725_s_stream(struct v4l2_subdev *sd, int on)
{
   struct ov7725 *ov7725 = to_ov7725(sd);
   struct i2c_client *client = ov7725->client;
   int ret = 0;
 
   mutex_lock(&ov7725->mutex);
 
   on = !!on;
   if (on == ov7725->streaming)
       goto unlock_and_return;
 
   if (on) {
       ret = pm_runtime_get_sync(&ov7725->client->dev);
       if (ret < 0) {
           pm_runtime_put_noidle(&client->dev);
           goto unlock_and_return;
       }
 
       ret = ov7725_write_array(ov7725->client,
                     ov7725->cur_mode->reg_list);
       if (ret) {
           pm_runtime_put(&client->dev);
           goto unlock_and_return;
       }
 
   } else {
       pm_runtime_put(&client->dev);
   }
 
   ov7725->streaming = on;
 
unlock_and_return:
   mutex_unlock(&ov7725->mutex);
 
   return ret;
}
 
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static int ov7725_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
   struct ov7725 *ov7725 = to_ov7725(sd);
   struct v4l2_mbus_framefmt *try_fmt =
       v4l2_subdev_get_try_format(sd, fh->pad, 0);
   const struct ov7725_mode *def_mode = &supported_modes[0];
 
   mutex_lock(&ov7725->mutex);
 
   try_fmt->width = def_mode->width;
   try_fmt->height = def_mode->height;
   try_fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
   try_fmt->field = V4L2_FIELD_NONE;
   try_fmt->colorspace = V4L2_COLORSPACE_JPEG;
 
   mutex_unlock(&ov7725->mutex);
 
   return 0;
}
#endif
 
static int ov7725_runtime_resume(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct ov7725 *ov7725 = to_ov7725(sd);
 
   return __ov7725_power_on(ov7725);
}
 
static int ov7725_runtime_suspend(struct device *dev)
{
   struct i2c_client *client = to_i2c_client(dev);
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct ov7725 *ov7725 = to_ov7725(sd);
 
   __ov7725_power_off(ov7725);
 
   return 0;
}
 
static const struct dev_pm_ops ov7725_pm_ops = {
   SET_RUNTIME_PM_OPS(ov7725_runtime_suspend,
              ov7725_runtime_resume, NULL)
};
 
static const struct v4l2_subdev_video_ops ov7725_video_ops = {
   .s_stream = ov7725_s_stream,
};
 
static const struct v4l2_subdev_pad_ops ov7725_pad_ops = {
   .enum_mbus_code = ov7725_enum_mbus_code,
   .enum_frame_size = ov7725_enum_frame_sizes,
   .get_fmt = ov7725_get_fmt,
   .set_fmt = ov7725_set_fmt,
};
 
static const struct v4l2_subdev_ops ov7725_subdev_ops = {
   .video    = &ov7725_video_ops,
   .pad    = &ov7725_pad_ops,
};
 
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
static const struct v4l2_subdev_internal_ops ov7725_internal_ops = {
   .open = ov7725_open,
};
#endif
 
static int ov7725_check_sensor_id(struct ov7725 *ov7725,
                 struct i2c_client *client)
{
   struct device *dev = &ov7725->client->dev;
   u8 id_h = 0, id_l = 0;
 
   id_h = ov7725_read_reg(client, REG_CHIP_ID_H);
   id_l = ov7725_read_reg(client, REG_CHIP_ID_L);
   if (id_h != CHIP_ID_H && id_l != CHIP_ID_L) {
       dev_err(dev, "Wrong camera sensor id(0x%02x%02x)\n",
           id_h, id_l);
       return -EINVAL;
   }
 
   dev_info(dev, "Detected ov7725 (0x%02x%02x) sensor\n",
       CHIP_ID_H, CHIP_ID_L);
 
   return 0;
}
 
static int ov7725_configure_regulators(struct ov7725 *ov7725)
{
   u32 i;
 
   for (i = 0; i < ov7725_NUM_SUPPLIES; i++)
       ov7725->supplies[i].supply = ov7725_supply_names[i];
 
   return devm_regulator_bulk_get(&ov7725->client->dev,
                      ov7725_NUM_SUPPLIES,
                      ov7725->supplies);
}
 
static int ov7725_probe(struct i2c_client *client,
            const struct i2c_device_id *id)
{
   struct device *dev = &client->dev;
   struct ov7725 *ov7725;
   int ret;
 
   ov7725 = devm_kzalloc(dev, sizeof(*ov7725), GFP_KERNEL);
   if (!ov7725)
       return -ENOMEM;
 
   ov7725->client = client;
   ov7725->cur_mode = &supported_modes[0];
 
   ov7725->xvclk = devm_clk_get(dev, "xvclk");
   if (IS_ERR(ov7725->xvclk)) {
       dev_err(dev, "Failed to get xvclk\n");
       return -EINVAL;
   }
   ret = clk_set_rate(ov7725->xvclk, ov7725_XVCLK_FREQ);
   if (ret < 0) {
       dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
       return ret;
   }
   if (clk_get_rate(ov7725->xvclk) != ov7725_XVCLK_FREQ)
       dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
 
   ov7725->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
   if (IS_ERR(ov7725->reset_gpio))
       dev_warn(dev, "Failed to get reset-gpios\n");
 
   ov7725->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
   if (IS_ERR(ov7725->pwdn_gpio))
       dev_warn(dev, "Failed to get ov7725-gpios\n");
 
   ret = ov7725_configure_regulators(ov7725);
   if (ret) {
       dev_warn(dev, "Failed to get power regulators\n");
       return ret;
   }
 
   mutex_init(&ov7725->mutex);
   v4l2_i2c_subdev_init(&ov7725->subdev, client, &ov7725_subdev_ops);
 
   ret = __ov7725_power_on(ov7725);
   if (ret)
       goto err_destroy_mutex;
 
   ret = ov7725_check_sensor_id(ov7725, client);
   if (ret)
       goto err_power_off;
#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
   ov7725->subdev.internal_ops = &ov7725_internal_ops;
   ov7725->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
#endif
#if defined(CONFIG_MEDIA_CONTROLLER)
   ov7725->pad.flags = MEDIA_PAD_FL_SOURCE;
   ov7725->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
   ret = media_entity_init(&ov7725->subdev.entity, 1, &ov7725->pad, 0);
   if (ret < 0)
       goto err_power_off;
#endif
 
   ret = v4l2_async_register_subdev(&ov7725->subdev);
   if (ret) {
       dev_err(dev, "v4l2 async register subdev failed\n");
       goto err_clean_entity;
   }
 
   pm_runtime_set_active(dev);
   pm_runtime_enable(dev);
   pm_runtime_idle(dev);
 
   return 0;
 
err_clean_entity:
#if defined(CONFIG_MEDIA_CONTROLLER)
   media_entity_cleanup(&ov7725->subdev.entity);
#endif
err_power_off:
   __ov7725_power_off(ov7725);
err_destroy_mutex:
   mutex_destroy(&ov7725->mutex);
 
   return ret;
}
 
static int ov7725_remove(struct i2c_client *client)
{
   struct v4l2_subdev *sd = i2c_get_clientdata(client);
   struct ov7725 *ov7725 = to_ov7725(sd);
 
   v4l2_async_unregister_subdev(sd);
#if defined(CONFIG_MEDIA_CONTROLLER)
   media_entity_cleanup(&sd->entity);
#endif
   mutex_destroy(&ov7725->mutex);
 
   pm_runtime_disable(&client->dev);
   if (!pm_runtime_status_suspended(&client->dev))
       __ov7725_power_off(ov7725);
   pm_runtime_set_suspended(&client->dev);
 
   return 0;
}
 
#if IS_ENABLED(CONFIG_OF)
static const struct of_device_id ov7725_of_match[] = {
   { .compatible = "ovti,ov7725" },
   {},
};
MODULE_DEVICE_TABLE(of, ov7725_of_match);
#endif
 
static const struct i2c_device_id ov7725_match_id[] = {
   {"ovti,ov7251", 0},
   {},
};
 
static struct i2c_driver ov7725_i2c_driver = {
   .driver = {
       .name = "ov7725",
       .pm = &ov7725_pm_ops,
       .of_match_table = of_match_ptr(ov7725_of_match),
   },
   .probe        = ov7725_probe,
   .remove        = ov7725_remove,
   .id_table    = ov7725_match_id,
};
 
static int __init sensor_mod_init(void)
{
   return i2c_add_driver(&ov7725_i2c_driver);
}
 
static void __exit sensor_mod_exit(void)
{
   i2c_del_driver(&ov7725_i2c_driver);
}
 
device_initcall_sync(sensor_mod_init);
module_exit(sensor_mod_exit);
 
MODULE_DESCRIPTION("OmniVision ov7725 sensor driver");
MODULE_LICENSE("GPL v2");