1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ov7725 driver
4 *
5 * Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd.
6 */
7
8 #include <linux/clk.h>
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/sysfs.h>
16 #include <media/media-entity.h>
17 #include <media/v4l2-async.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-subdev.h>
20
21 #define REG_CHIP_ID_H 0x0a
22 #define REG_CHIP_ID_L 0x0b
23 #define CHIP_ID_H 0x77
24 #define CHIP_ID_L 0x21
25
26 #define REG_NULL 0xFF
27
28 #define ov7725_XVCLK_FREQ 24000000
29
30 static const char * const ov7725_supply_names[] = {
31 "avdd",
32 "dovdd",
33 "dvdd",
34 };
35
36 #define ov7725_NUM_SUPPLIES ARRAY_SIZE(ov7725_supply_names)
37
38 struct regval {
39 u8 addr;
40 u8 val;
41 };
42
43 struct ov7725_mode {
44 u32 width;
45 u32 height;
46 const struct regval *reg_list;
47 };
48
49 struct ov7725 {
50 struct i2c_client *client;
51 struct clk *xvclk;
52 struct gpio_desc *reset_gpio;
53 struct gpio_desc *pwdn_gpio;
54 struct regulator_bulk_data supplies[ov7725_NUM_SUPPLIES];
55
56 bool streaming;
57 struct mutex mutex; /* lock to serialize v4l2 callback */
58 struct v4l2_subdev subdev;
59 struct media_pad pad;
60
61 const struct ov7725_mode *cur_mode;
62 };
63
64 #define to_ov7725(sd) container_of(sd, struct ov7725, subdev)
65
66 /* 30fps at 24MHz input clock,4x maximum gain */
67 static struct regval ov7725_640x480_30fps[] = {
68 {0x12, 0x80},
69 {0x3d, 0x03},
70 {0x17, 0x25}, /* Raw: 0x17,0x22 */
71 {0x18, 0xa4}, /* Raw: 0x18,0xa4 */
72 {0x19, 0x06}, /* Raw: 0x19,0x07 */
73 {0x1a, 0xf0},
74 {0x32, 0x60}, /* Raw: 0x32,0x00 */
75 {0x29, 0xa0},
76 {0x2c, 0xf0},
77 {0x2a, 0x00},
78 {0x11, 0x01},
79 {0x42, 0x7f},
80 {0x4d, 0x00},
81 {0x63, 0xe0},
82 {0x64, 0xff},
83 {0x65, 0x20},
84 {0x66, 0x00},
85 {0x67, 0x48},
86 {0x13, 0xf0},
87 {0x0d, 0x41},
88 {0x0f, 0xc5},
89
90 {0x14, 0x17}, /* 0x14,0x11 */
91 {0x22, 0x3f},
92 {0x23, 0x07},
93 {0x24, 0x44},
94 {0x25, 0x3c},
95 {0x26, 0xa1},
96 {0x2b, 0x00},
97 {0x6b, 0xaa},
98 {0x13, 0xff},
99 {0x90, 0x05},
100 {0x91, 0x01},
101 {0x92, 0x03},
102 {0x93, 0x00},
103 {0x94, 0x40},
104 {0x95, 0x40},
105 {0x96, 0x00},
106 {0x97, 0x11},
107 {0x98, 0x2f},
108 {0x99, 0x40},
109 {0x9a, 0x9e},
110 {0x9b, 0x08},
111 {0x9c, 0x20},
112 {0x9e, 0x81},
113 {0xa6, 0x06},
114 {0x7e, 0x0c},
115 {0x7f, 0x16},
116 {0x80, 0x2a},
117 {0x81, 0x4e},
118 {0x82, 0x61},
119 {0x83, 0x6f},
120 {0x84, 0x7b},
121 {0x85, 0x86},
122 {0x86, 0x8e},
123 {0x87, 0x97},
124 {0x88, 0xa4},
125 {0x89, 0xaf},
126 {0x8a, 0xc5},
127 {0x8b, 0xd7},
128 {0x8c, 0xe8},
129 {0x8d, 0x20},
130 {0x33, 0x00},
131 {0x22, 0x99},
132 {0x23, 0x03},
133 {0x4a, 0x00},
134 {0x49, 0x13},
135 {0x47, 0x08},
136 {0x4b, 0x14},
137 {0x4c, 0x17},
138 {0x46, 0x05},
139 {0x0e, 0x65},
140 {0x0c, 0x00},
141 {REG_NULL, 0x0},
142 };
143
144 static struct regval ov7725_1600x1200_7fps[] = {
145 {REG_NULL, 0x0},
146 };
147
148 static const struct ov7725_mode supported_modes[] = {
149 {
150 .width = 640,
151 .height = 480,
152 .reg_list = ov7725_640x480_30fps,
153 },
154 {
155 .width = 1600,
156 .height = 1200,
157 .reg_list = ov7725_1600x1200_7fps,
158 },
159 };
160
ov7725_write_reg(struct i2c_client * client,u8 reg,u8 val)161 static int ov7725_write_reg(struct i2c_client *client, u8 reg, u8 val)
162 {
163 int ret;
164
165 ret = i2c_smbus_write_byte_data(client, reg, val);
166
167 if (ret < 0)
168 dev_err(&client->dev, "write reg error: %d\n", ret);
169
170 return ret;
171 }
172
ov7725_write_array(struct i2c_client * client,const struct regval * regs)173 static int ov7725_write_array(struct i2c_client *client,
174 const struct regval *regs)
175 {
176 int i, ret = 0;
177
178 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
179 ret = ov7725_write_reg(client, regs[i].addr, regs[i].val);
180
181 return ret;
182 }
183
ov7725_read_reg(struct i2c_client * client,u8 reg)184 static inline u8 ov7725_read_reg(struct i2c_client *client, u8 reg)
185 {
186 return i2c_smbus_read_byte_data(client, reg);
187 }
188
ov7725_get_reso_dist(const struct ov7725_mode * mode,struct v4l2_mbus_framefmt * framefmt)189 static int ov7725_get_reso_dist(const struct ov7725_mode *mode,
190 struct v4l2_mbus_framefmt *framefmt)
191 {
192 return abs(mode->width - framefmt->width) +
193 abs(mode->height - framefmt->height);
194 }
195
196 static const struct ov7725_mode *
ov7725_find_best_fit(struct v4l2_subdev_format * fmt)197 ov7725_find_best_fit(struct v4l2_subdev_format *fmt)
198 {
199 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
200 int dist;
201 int cur_best_fit = 0;
202 int cur_best_fit_dist = -1;
203 size_t i;
204
205 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
206 dist = ov7725_get_reso_dist(&supported_modes[i], framefmt);
207 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
208 cur_best_fit_dist = dist;
209 cur_best_fit = i;
210 }
211 }
212
213 return &supported_modes[cur_best_fit];
214 }
215
ov7725_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)216 static int ov7725_set_fmt(struct v4l2_subdev *sd,
217 struct v4l2_subdev_pad_config *cfg,
218 struct v4l2_subdev_format *fmt)
219 {
220 struct ov7725 *ov7725 = to_ov7725(sd);
221 const struct ov7725_mode *mode;
222
223 mutex_lock(&ov7725->mutex);
224
225 mode = ov7725_find_best_fit(fmt);
226 fmt->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
227 fmt->format.width = mode->width;
228 fmt->format.height = mode->height;
229 fmt->format.field = V4L2_FIELD_NONE;
230 fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
231 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
232 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
233 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
234 #else
235 mutex_unlock(&ov7725->mutex);
236 return -ENOTTY;
237 #endif
238 } else {
239 ov7725->cur_mode = mode;
240 }
241
242 mutex_unlock(&ov7725->mutex);
243
244 return 0;
245 }
246
ov7725_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)247 static int ov7725_get_fmt(struct v4l2_subdev *sd,
248 struct v4l2_subdev_pad_config *cfg,
249 struct v4l2_subdev_format *fmt)
250 {
251 struct ov7725 *ov7725 = to_ov7725(sd);
252 const struct ov7725_mode *mode = ov7725->cur_mode;
253
254 mutex_lock(&ov7725->mutex);
255 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
256 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
257 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
258 #else
259 mutex_unlock(&ov7725->mutex);
260 return -ENOTTY;
261 #endif
262 } else {
263 fmt->format.width = mode->width;
264 fmt->format.height = mode->height;
265 fmt->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
266 fmt->format.field = V4L2_FIELD_NONE;
267 fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
268 }
269 mutex_unlock(&ov7725->mutex);
270
271 return 0;
272 }
273
ov7725_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)274 static int ov7725_enum_mbus_code(struct v4l2_subdev *sd,
275 struct v4l2_subdev_pad_config *cfg,
276 struct v4l2_subdev_mbus_code_enum *code)
277 {
278 if (code->index >= ARRAY_SIZE(supported_modes))
279 return -EINVAL;
280
281 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
282
283 return 0;
284 }
285
ov7725_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)286 static int ov7725_enum_frame_sizes(struct v4l2_subdev *sd,
287 struct v4l2_subdev_pad_config *cfg,
288 struct v4l2_subdev_frame_size_enum *fse)
289 {
290 u32 index = fse->index;
291
292 if (index >= ARRAY_SIZE(supported_modes))
293 return -EINVAL;
294
295 fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
296
297 fse->min_width = supported_modes[index].width;
298 fse->max_width = supported_modes[index].width;
299 fse->max_height = supported_modes[index].height;
300 fse->min_height = supported_modes[index].height;
301
302 return 0;
303 }
304
__ov7725_power_on(struct ov7725 * ov7725)305 static int __ov7725_power_on(struct ov7725 *ov7725)
306 {
307 int ret;
308 struct device *dev = &ov7725->client->dev;
309
310 if (!IS_ERR(ov7725->reset_gpio))
311 gpiod_set_value_cansleep(ov7725->reset_gpio, 0);
312
313 ret = regulator_bulk_enable(ov7725_NUM_SUPPLIES, ov7725->supplies);
314 if (ret < 0) {
315 dev_err(dev, "Failed to enable regulators\n");
316 return ret;
317 }
318
319 if (!IS_ERR(ov7725->xvclk)) {
320 ret = clk_prepare_enable(ov7725->xvclk);
321 if (ret < 0) {
322 dev_err(dev, "Failed to enable xvclk\n");
323 return ret;
324 }
325 }
326
327 if (!IS_ERR(ov7725->pwdn_gpio))
328 gpiod_set_value_cansleep(ov7725->pwdn_gpio, 0);
329
330 if (!IS_ERR(ov7725->reset_gpio))
331 gpiod_set_value_cansleep(ov7725->reset_gpio, 1);
332
333 return 0;
334 }
335
__ov7725_power_off(struct ov7725 * ov7725)336 static void __ov7725_power_off(struct ov7725 *ov7725)
337 {
338 if (!IS_ERR(ov7725->reset_gpio))
339 gpiod_set_value_cansleep(ov7725->reset_gpio, 0);
340 if (!IS_ERR(ov7725->pwdn_gpio))
341 gpiod_set_value_cansleep(ov7725->pwdn_gpio, 1);
342
343 if (!IS_ERR(ov7725->xvclk))
344 clk_disable_unprepare(ov7725->xvclk);
345
346 regulator_bulk_disable(ov7725_NUM_SUPPLIES, ov7725->supplies);
347 }
348
ov7725_s_stream(struct v4l2_subdev * sd,int on)349 static int ov7725_s_stream(struct v4l2_subdev *sd, int on)
350 {
351 struct ov7725 *ov7725 = to_ov7725(sd);
352 struct i2c_client *client = ov7725->client;
353 int ret = 0;
354
355 mutex_lock(&ov7725->mutex);
356
357 on = !!on;
358 if (on == ov7725->streaming)
359 goto unlock_and_return;
360
361 if (on) {
362 ret = pm_runtime_get_sync(&ov7725->client->dev);
363 if (ret < 0) {
364 pm_runtime_put_noidle(&client->dev);
365 goto unlock_and_return;
366 }
367
368 ret = ov7725_write_array(ov7725->client,
369 ov7725->cur_mode->reg_list);
370 if (ret) {
371 pm_runtime_put(&client->dev);
372 goto unlock_and_return;
373 }
374
375 } else {
376 pm_runtime_put(&client->dev);
377 }
378
379 ov7725->streaming = on;
380
381 unlock_and_return:
382 mutex_unlock(&ov7725->mutex);
383
384 return ret;
385 }
386
387 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
ov7725_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)388 static int ov7725_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
389 {
390 struct ov7725 *ov7725 = to_ov7725(sd);
391 struct v4l2_mbus_framefmt *try_fmt =
392 v4l2_subdev_get_try_format(sd, fh->pad, 0);
393 const struct ov7725_mode *def_mode = &supported_modes[0];
394
395 mutex_lock(&ov7725->mutex);
396
397 try_fmt->width = def_mode->width;
398 try_fmt->height = def_mode->height;
399 try_fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
400 try_fmt->field = V4L2_FIELD_NONE;
401 try_fmt->colorspace = V4L2_COLORSPACE_JPEG;
402
403 mutex_unlock(&ov7725->mutex);
404
405 return 0;
406 }
407 #endif
408
ov7725_runtime_resume(struct device * dev)409 static int ov7725_runtime_resume(struct device *dev)
410 {
411 struct i2c_client *client = to_i2c_client(dev);
412 struct v4l2_subdev *sd = i2c_get_clientdata(client);
413 struct ov7725 *ov7725 = to_ov7725(sd);
414
415 return __ov7725_power_on(ov7725);
416 }
417
ov7725_runtime_suspend(struct device * dev)418 static int ov7725_runtime_suspend(struct device *dev)
419 {
420 struct i2c_client *client = to_i2c_client(dev);
421 struct v4l2_subdev *sd = i2c_get_clientdata(client);
422 struct ov7725 *ov7725 = to_ov7725(sd);
423
424 __ov7725_power_off(ov7725);
425
426 return 0;
427 }
428
429 static const struct dev_pm_ops ov7725_pm_ops = {
430 SET_RUNTIME_PM_OPS(ov7725_runtime_suspend,
431 ov7725_runtime_resume, NULL)
432 };
433
434 static const struct v4l2_subdev_video_ops ov7725_video_ops = {
435 .s_stream = ov7725_s_stream,
436 };
437
438 static const struct v4l2_subdev_pad_ops ov7725_pad_ops = {
439 .enum_mbus_code = ov7725_enum_mbus_code,
440 .enum_frame_size = ov7725_enum_frame_sizes,
441 .get_fmt = ov7725_get_fmt,
442 .set_fmt = ov7725_set_fmt,
443 };
444
445 static const struct v4l2_subdev_ops ov7725_subdev_ops = {
446 .video = &ov7725_video_ops,
447 .pad = &ov7725_pad_ops,
448 };
449
450 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
451 static const struct v4l2_subdev_internal_ops ov7725_internal_ops = {
452 .open = ov7725_open,
453 };
454 #endif
455
ov7725_check_sensor_id(struct ov7725 * ov7725,struct i2c_client * client)456 static int ov7725_check_sensor_id(struct ov7725 *ov7725,
457 struct i2c_client *client)
458 {
459 struct device *dev = &ov7725->client->dev;
460 u8 id_h = 0, id_l = 0;
461
462 id_h = ov7725_read_reg(client, REG_CHIP_ID_H);
463 id_l = ov7725_read_reg(client, REG_CHIP_ID_L);
464 if (id_h != CHIP_ID_H && id_l != CHIP_ID_L) {
465 dev_err(dev, "Wrong camera sensor id(0x%02x%02x)\n",
466 id_h, id_l);
467 return -EINVAL;
468 }
469
470 dev_info(dev, "Detected ov7725 (0x%02x%02x) sensor\n",
471 CHIP_ID_H, CHIP_ID_L);
472
473 return 0;
474 }
475
ov7725_configure_regulators(struct ov7725 * ov7725)476 static int ov7725_configure_regulators(struct ov7725 *ov7725)
477 {
478 u32 i;
479
480 for (i = 0; i < ov7725_NUM_SUPPLIES; i++)
481 ov7725->supplies[i].supply = ov7725_supply_names[i];
482
483 return devm_regulator_bulk_get(&ov7725->client->dev,
484 ov7725_NUM_SUPPLIES,
485 ov7725->supplies);
486 }
487
ov7725_probe(struct i2c_client * client,const struct i2c_device_id * id)488 static int ov7725_probe(struct i2c_client *client,
489 const struct i2c_device_id *id)
490 {
491 struct device *dev = &client->dev;
492 struct ov7725 *ov7725;
493 int ret;
494
495 ov7725 = devm_kzalloc(dev, sizeof(*ov7725), GFP_KERNEL);
496 if (!ov7725)
497 return -ENOMEM;
498
499 ov7725->client = client;
500 ov7725->cur_mode = &supported_modes[0];
501
502 ov7725->xvclk = devm_clk_get(dev, "xvclk");
503 if (IS_ERR(ov7725->xvclk)) {
504 dev_err(dev, "Failed to get xvclk\n");
505 return -EINVAL;
506 }
507 ret = clk_set_rate(ov7725->xvclk, ov7725_XVCLK_FREQ);
508 if (ret < 0) {
509 dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
510 return ret;
511 }
512 if (clk_get_rate(ov7725->xvclk) != ov7725_XVCLK_FREQ)
513 dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
514
515 ov7725->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
516 if (IS_ERR(ov7725->reset_gpio))
517 dev_warn(dev, "Failed to get reset-gpios\n");
518
519 ov7725->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
520 if (IS_ERR(ov7725->pwdn_gpio))
521 dev_warn(dev, "Failed to get ov7725-gpios\n");
522
523 ret = ov7725_configure_regulators(ov7725);
524 if (ret) {
525 dev_warn(dev, "Failed to get power regulators\n");
526 return ret;
527 }
528
529 mutex_init(&ov7725->mutex);
530 v4l2_i2c_subdev_init(&ov7725->subdev, client, &ov7725_subdev_ops);
531
532 ret = __ov7725_power_on(ov7725);
533 if (ret)
534 goto err_destroy_mutex;
535
536 ret = ov7725_check_sensor_id(ov7725, client);
537 if (ret)
538 goto err_power_off;
539 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
540 ov7725->subdev.internal_ops = &ov7725_internal_ops;
541 ov7725->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
542 #endif
543 #if defined(CONFIG_MEDIA_CONTROLLER)
544 ov7725->pad.flags = MEDIA_PAD_FL_SOURCE;
545 ov7725->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
546 ret = media_entity_init(&ov7725->subdev.entity, 1, &ov7725->pad, 0);
547 if (ret < 0)
548 goto err_power_off;
549 #endif
550
551 ret = v4l2_async_register_subdev(&ov7725->subdev);
552 if (ret) {
553 dev_err(dev, "v4l2 async register subdev failed\n");
554 goto err_clean_entity;
555 }
556
557 pm_runtime_set_active(dev);
558 pm_runtime_enable(dev);
559 pm_runtime_idle(dev);
560
561 return 0;
562
563 err_clean_entity:
564 #if defined(CONFIG_MEDIA_CONTROLLER)
565 media_entity_cleanup(&ov7725->subdev.entity);
566 #endif
567 err_power_off:
568 __ov7725_power_off(ov7725);
569 err_destroy_mutex:
570 mutex_destroy(&ov7725->mutex);
571
572 return ret;
573 }
574
ov7725_remove(struct i2c_client * client)575 static int ov7725_remove(struct i2c_client *client)
576 {
577 struct v4l2_subdev *sd = i2c_get_clientdata(client);
578 struct ov7725 *ov7725 = to_ov7725(sd);
579
580 v4l2_async_unregister_subdev(sd);
581 #if defined(CONFIG_MEDIA_CONTROLLER)
582 media_entity_cleanup(&sd->entity);
583 #endif
584 mutex_destroy(&ov7725->mutex);
585
586 pm_runtime_disable(&client->dev);
587 if (!pm_runtime_status_suspended(&client->dev))
588 __ov7725_power_off(ov7725);
589 pm_runtime_set_suspended(&client->dev);
590
591 return 0;
592 }
593
594 #if IS_ENABLED(CONFIG_OF)
595 static const struct of_device_id ov7725_of_match[] = {
596 { .compatible = "ovti,ov7725" },
597 {},
598 };
599 MODULE_DEVICE_TABLE(of, ov7725_of_match);
600 #endif
601
602 static const struct i2c_device_id ov7725_match_id[] = {
603 {"ovti,ov7251", 0},
604 {},
605 };
606
607 static struct i2c_driver ov7725_i2c_driver = {
608 .driver = {
609 .name = "ov7725",
610 .pm = &ov7725_pm_ops,
611 .of_match_table = of_match_ptr(ov7725_of_match),
612 },
613 .probe = ov7725_probe,
614 .remove = ov7725_remove,
615 .id_table = ov7725_match_id,
616 };
617
sensor_mod_init(void)618 static int __init sensor_mod_init(void)
619 {
620 return i2c_add_driver(&ov7725_i2c_driver);
621 }
622
sensor_mod_exit(void)623 static void __exit sensor_mod_exit(void)
624 {
625 i2c_del_driver(&ov7725_i2c_driver);
626 }
627
628 device_initcall_sync(sensor_mod_init);
629 module_exit(sensor_mod_exit);
630
631 MODULE_DESCRIPTION("OmniVision ov7725 sensor driver");
632 MODULE_LICENSE("GPL v2");