Merge 6.4-rc5 into usb-next
[platform/kernel/linux-starfive.git] / drivers / staging / media / atomisp / i2c / atomisp-ov2680.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for OmniVision OV2680 1080p HD camera sensor.
4  *
5  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6  * Copyright (c) 2023 Hans de Goede <hdegoede@redhat.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version
10  * 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18
19 #include <linux/acpi.h>
20 #include <linux/device.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio/machine.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/types.h>
27
28 #include <media/ov_16bit_addr_reg_helpers.h>
29 #include <media/v4l2-device.h>
30
31 #include "../include/linux/atomisp_gmin_platform.h"
32
33 #include "ov2680.h"
34
35 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
36         atomisp_bayer_order_bggr,
37         atomisp_bayer_order_grbg,
38         atomisp_bayer_order_gbrg,
39         atomisp_bayer_order_rggb,
40 };
41
42 static int ov2680_write_reg_array(struct i2c_client *client,
43                                   const struct ov2680_reg *reglist)
44 {
45         const struct ov2680_reg *next = reglist;
46         int ret;
47
48         for (; next->reg != 0; next++) {
49                 ret = ov_write_reg8(client, next->reg, next->val);
50                 if (ret)
51                         return ret;
52         }
53
54         return 0;
55 }
56
57 static void ov2680_set_bayer_order(struct ov2680_device *sensor, struct v4l2_mbus_framefmt *fmt)
58 {
59         static const int ov2680_hv_flip_bayer_order[] = {
60                 MEDIA_BUS_FMT_SBGGR10_1X10,
61                 MEDIA_BUS_FMT_SGRBG10_1X10,
62                 MEDIA_BUS_FMT_SGBRG10_1X10,
63                 MEDIA_BUS_FMT_SRGGB10_1X10,
64         };
65         struct camera_mipi_info *ov2680_info;
66         int hv_flip = 0;
67
68         if (sensor->ctrls.vflip->val)
69                 hv_flip += 1;
70
71         if (sensor->ctrls.hflip->val)
72                 hv_flip += 2;
73
74         fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
75
76         /* TODO atomisp specific custom API, should be removed */
77         ov2680_info = v4l2_get_subdev_hostdata(&sensor->sd);
78         if (ov2680_info)
79                 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[hv_flip];
80 }
81
82 static int ov2680_set_vflip(struct ov2680_device *sensor, s32 val)
83 {
84         int ret;
85
86         if (sensor->is_streaming)
87                 return -EBUSY;
88
89         ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
90         if (ret < 0)
91                 return ret;
92
93         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
94         return 0;
95 }
96
97 static int ov2680_set_hflip(struct ov2680_device *sensor, s32 val)
98 {
99         int ret;
100
101         if (sensor->is_streaming)
102                 return -EBUSY;
103
104         ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
105         if (ret < 0)
106                 return ret;
107
108         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
109         return 0;
110 }
111
112 static int ov2680_exposure_set(struct ov2680_device *sensor, u32 exp)
113 {
114         return ov_write_reg24(sensor->client, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
115 }
116
117 static int ov2680_gain_set(struct ov2680_device *sensor, u32 gain)
118 {
119         return ov_write_reg16(sensor->client, OV2680_REG_GAIN_PK, gain);
120 }
121
122 static int ov2680_test_pattern_set(struct ov2680_device *sensor, int value)
123 {
124         int ret;
125
126         if (!value)
127                 return ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), 0);
128
129         ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
130         if (ret < 0)
131                 return ret;
132
133         ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
134         if (ret < 0)
135                 return ret;
136
137         return 0;
138 }
139
140 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
141 {
142         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
143         struct ov2680_device *sensor = to_ov2680_sensor(sd);
144         int ret;
145
146         /* Only apply changes to the controls if the device is powered up */
147         if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
148                 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
149                 return 0;
150         }
151
152         switch (ctrl->id) {
153         case V4L2_CID_VFLIP:
154                 ret = ov2680_set_vflip(sensor, ctrl->val);
155                 break;
156         case V4L2_CID_HFLIP:
157                 ret = ov2680_set_hflip(sensor, ctrl->val);
158                 break;
159         case V4L2_CID_EXPOSURE:
160                 ret = ov2680_exposure_set(sensor, ctrl->val);
161                 break;
162         case V4L2_CID_GAIN:
163                 ret = ov2680_gain_set(sensor, ctrl->val);
164                 break;
165         case V4L2_CID_TEST_PATTERN:
166                 ret = ov2680_test_pattern_set(sensor, ctrl->val);
167                 break;
168         default:
169                 ret = -EINVAL;
170         }
171
172         pm_runtime_put(sensor->sd.dev);
173         return ret;
174 }
175
176 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
177         .s_ctrl = ov2680_s_ctrl,
178 };
179
180 static int ov2680_init_registers(struct v4l2_subdev *sd)
181 {
182         struct i2c_client *client = v4l2_get_subdevdata(sd);
183         int ret;
184
185         ret = ov_write_reg8(client, OV2680_SW_RESET, 0x01);
186         ret |= ov2680_write_reg_array(client, ov2680_global_setting);
187
188         return ret;
189 }
190
191 static struct v4l2_mbus_framefmt *
192 __ov2680_get_pad_format(struct ov2680_device *sensor,
193                         struct v4l2_subdev_state *state,
194                         unsigned int pad, enum v4l2_subdev_format_whence which)
195 {
196         if (which == V4L2_SUBDEV_FORMAT_TRY)
197                 return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
198
199         return &sensor->mode.fmt;
200 }
201
202 static void ov2680_fill_format(struct ov2680_device *sensor,
203                                struct v4l2_mbus_framefmt *fmt,
204                                unsigned int width, unsigned int height)
205 {
206         memset(fmt, 0, sizeof(*fmt));
207         fmt->width = width;
208         fmt->height = height;
209         fmt->field = V4L2_FIELD_NONE;
210         ov2680_set_bayer_order(sensor, fmt);
211 }
212
213 static void ov2680_calc_mode(struct ov2680_device *sensor, int width, int height)
214 {
215         int orig_width = width;
216         int orig_height = height;
217
218         if (width  <= (OV2680_NATIVE_WIDTH / 2) &&
219             height <= (OV2680_NATIVE_HEIGHT / 2)) {
220                 sensor->mode.binning = true;
221                 width *= 2;
222                 height *= 2;
223         } else {
224                 sensor->mode.binning = false;
225         }
226
227         sensor->mode.h_start = ((OV2680_NATIVE_WIDTH - width) / 2) & ~1;
228         sensor->mode.v_start = ((OV2680_NATIVE_HEIGHT - height) / 2) & ~1;
229         sensor->mode.h_end = min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
230                                  OV2680_NATIVE_WIDTH - 1);
231         sensor->mode.v_end = min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
232                                  OV2680_NATIVE_HEIGHT - 1);
233         sensor->mode.h_output_size = orig_width;
234         sensor->mode.v_output_size = orig_height;
235         sensor->mode.hts = OV2680_PIXELS_PER_LINE;
236         sensor->mode.vts = OV2680_LINES_PER_FRAME;
237 }
238
239 static int ov2680_set_mode(struct ov2680_device *sensor)
240 {
241         struct i2c_client *client = sensor->client;
242         u8 pll_div, unknown, inc, fmt1, fmt2;
243         int ret;
244
245         if (sensor->mode.binning) {
246                 pll_div = 1;
247                 unknown = 0x23;
248                 inc = 0x31;
249                 fmt1 = 0xc2;
250                 fmt2 = 0x01;
251         } else {
252                 pll_div = 0;
253                 unknown = 0x21;
254                 inc = 0x11;
255                 fmt1 = 0xc0;
256                 fmt2 = 0x00;
257         }
258
259         ret = ov_write_reg8(client, 0x3086, pll_div);
260         if (ret)
261                 return ret;
262
263         ret = ov_write_reg8(client, 0x370a, unknown);
264         if (ret)
265                 return ret;
266
267         ret = ov_write_reg16(client, OV2680_HORIZONTAL_START_H, sensor->mode.h_start);
268         if (ret)
269                 return ret;
270
271         ret = ov_write_reg16(client, OV2680_VERTICAL_START_H, sensor->mode.v_start);
272         if (ret)
273                 return ret;
274
275         ret = ov_write_reg16(client, OV2680_HORIZONTAL_END_H, sensor->mode.h_end);
276         if (ret)
277                 return ret;
278
279         ret = ov_write_reg16(client, OV2680_VERTICAL_END_H, sensor->mode.v_end);
280         if (ret)
281                 return ret;
282
283         ret = ov_write_reg16(client, OV2680_HORIZONTAL_OUTPUT_SIZE_H,
284                                  sensor->mode.h_output_size);
285         if (ret)
286                 return ret;
287
288         ret = ov_write_reg16(client, OV2680_VERTICAL_OUTPUT_SIZE_H,
289                                  sensor->mode.v_output_size);
290         if (ret)
291                 return ret;
292
293         ret = ov_write_reg16(client, OV2680_HTS, sensor->mode.hts);
294         if (ret)
295                 return ret;
296
297         ret = ov_write_reg16(client, OV2680_VTS, sensor->mode.vts);
298         if (ret)
299                 return ret;
300
301         ret = ov_write_reg16(client, OV2680_ISP_X_WIN, 0);
302         if (ret)
303                 return ret;
304
305         ret = ov_write_reg16(client, OV2680_ISP_Y_WIN, 0);
306         if (ret)
307                 return ret;
308
309         ret = ov_write_reg8(client, OV2680_X_INC, inc);
310         if (ret)
311                 return ret;
312
313         ret = ov_write_reg8(client, OV2680_Y_INC, inc);
314         if (ret)
315                 return ret;
316
317         ret = ov_write_reg16(client, OV2680_X_WIN, sensor->mode.h_output_size);
318         if (ret)
319                 return ret;
320
321         ret = ov_write_reg16(client, OV2680_Y_WIN, sensor->mode.v_output_size);
322         if (ret)
323                 return ret;
324
325         ret = ov_write_reg8(client, OV2680_REG_FORMAT1, fmt1);
326         if (ret)
327                 return ret;
328
329         ret = ov_write_reg8(client, OV2680_REG_FORMAT2, fmt2);
330         if (ret)
331                 return ret;
332
333         return 0;
334 }
335
336 static int ov2680_set_fmt(struct v4l2_subdev *sd,
337                           struct v4l2_subdev_state *sd_state,
338                           struct v4l2_subdev_format *format)
339 {
340         struct ov2680_device *sensor = to_ov2680_sensor(sd);
341         struct v4l2_mbus_framefmt *fmt;
342         unsigned int width, height;
343
344         width = min_t(unsigned int, ALIGN(format->format.width, 2), OV2680_NATIVE_WIDTH);
345         height = min_t(unsigned int, ALIGN(format->format.height, 2), OV2680_NATIVE_HEIGHT);
346
347         fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
348         ov2680_fill_format(sensor, fmt, width, height);
349
350         format->format = *fmt;
351
352         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
353                 return 0;
354
355         mutex_lock(&sensor->input_lock);
356         ov2680_calc_mode(sensor, fmt->width, fmt->height);
357         mutex_unlock(&sensor->input_lock);
358         return 0;
359 }
360
361 static int ov2680_get_fmt(struct v4l2_subdev *sd,
362                           struct v4l2_subdev_state *sd_state,
363                           struct v4l2_subdev_format *format)
364 {
365         struct ov2680_device *sensor = to_ov2680_sensor(sd);
366         struct v4l2_mbus_framefmt *fmt;
367
368         fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
369         format->format = *fmt;
370         return 0;
371 }
372
373 static int ov2680_detect(struct i2c_client *client)
374 {
375         struct i2c_adapter *adapter = client->adapter;
376         u32 high = 0, low = 0;
377         int ret;
378         u16 id;
379         u8 revision;
380
381         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
382                 return -ENODEV;
383
384         ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_H, &high);
385         if (ret) {
386                 dev_err(&client->dev, "sensor_id_high read failed (%d)\n", ret);
387                 return -ENODEV;
388         }
389         ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_L, &low);
390         id = ((((u16)high) << 8) | (u16)low);
391
392         if (id != OV2680_ID) {
393                 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
394                 return -ENODEV;
395         }
396
397         ret = ov_read_reg8(client, OV2680_SC_CMMN_SUB_ID, &high);
398         revision = (u8)high & 0x0f;
399
400         dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
401                  id, revision);
402
403         return 0;
404 }
405
406 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
407 {
408         struct ov2680_device *sensor = to_ov2680_sensor(sd);
409         struct i2c_client *client = v4l2_get_subdevdata(sd);
410         int ret = 0;
411
412         mutex_lock(&sensor->input_lock);
413
414         if (sensor->is_streaming == enable) {
415                 dev_warn(&client->dev, "stream already %s\n", enable ? "started" : "stopped");
416                 goto error_unlock;
417         }
418
419         if (enable) {
420                 ret = pm_runtime_get_sync(sensor->sd.dev);
421                 if (ret < 0)
422                         goto error_power_down;
423
424                 ret = ov2680_set_mode(sensor);
425                 if (ret)
426                         goto error_power_down;
427
428                 /* Restore value of all ctrls */
429                 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
430                 if (ret)
431                         goto error_power_down;
432
433                 ret = ov_write_reg8(client, OV2680_SW_STREAM, OV2680_START_STREAMING);
434                 if (ret)
435                         goto error_power_down;
436         } else {
437                 ov_write_reg8(client, OV2680_SW_STREAM, OV2680_STOP_STREAMING);
438                 pm_runtime_put(sensor->sd.dev);
439         }
440
441         sensor->is_streaming = enable;
442         v4l2_ctrl_activate(sensor->ctrls.vflip, !enable);
443         v4l2_ctrl_activate(sensor->ctrls.hflip, !enable);
444
445         mutex_unlock(&sensor->input_lock);
446         return 0;
447
448 error_power_down:
449         pm_runtime_put(sensor->sd.dev);
450         sensor->is_streaming = false;
451 error_unlock:
452         mutex_unlock(&sensor->input_lock);
453         return ret;
454 }
455
456 static int ov2680_s_config(struct v4l2_subdev *sd)
457 {
458         struct i2c_client *client = v4l2_get_subdevdata(sd);
459         int ret;
460
461         ret = pm_runtime_get_sync(&client->dev);
462         if (ret < 0) {
463                 dev_err(&client->dev, "ov2680 power-up err.\n");
464                 goto fail_power_on;
465         }
466
467         /* config & detect sensor */
468         ret = ov2680_detect(client);
469         if (ret)
470                 dev_err(&client->dev, "ov2680_detect err s_config.\n");
471
472 fail_power_on:
473         pm_runtime_put(&client->dev);
474         return ret;
475 }
476
477 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
478                                    struct v4l2_subdev_frame_interval *interval)
479 {
480         interval->interval.numerator = 1;
481         interval->interval.denominator = OV2680_FPS;
482         return 0;
483 }
484
485 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
486                                  struct v4l2_subdev_state *sd_state,
487                                  struct v4l2_subdev_mbus_code_enum *code)
488 {
489         /* We support only a single format */
490         if (code->index)
491                 return -EINVAL;
492
493         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
494         return 0;
495 }
496
497 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
498                                   struct v4l2_subdev_state *sd_state,
499                                   struct v4l2_subdev_frame_size_enum *fse)
500 {
501         static const struct v4l2_frmsize_discrete ov2680_frame_sizes[] = {
502                 { 1616, 1216 },
503                 { 1616, 1096 },
504                 { 1616,  916 },
505                 { 1456, 1096 },
506                 { 1296,  976 },
507                 { 1296,  736 },
508                 {  784,  592 },
509                 {  656,  496 },
510         };
511         int index = fse->index;
512
513         if (index >= ARRAY_SIZE(ov2680_frame_sizes))
514                 return -EINVAL;
515
516         fse->min_width = ov2680_frame_sizes[index].width;
517         fse->min_height = ov2680_frame_sizes[index].height;
518         fse->max_width = ov2680_frame_sizes[index].width;
519         fse->max_height = ov2680_frame_sizes[index].height;
520
521         return 0;
522 }
523
524 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
525                                       struct v4l2_subdev_state *sd_state,
526                                       struct v4l2_subdev_frame_interval_enum *fie)
527 {
528         /* Only 1 framerate */
529         if (fie->index)
530                 return -EINVAL;
531
532         fie->interval.numerator = 1;
533         fie->interval.denominator = OV2680_FPS;
534         return 0;
535 }
536
537 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
538 {
539         *frames = OV2680_SKIP_FRAMES;
540         return 0;
541 }
542
543 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
544         .s_stream = ov2680_s_stream,
545         .g_frame_interval = ov2680_g_frame_interval,
546 };
547
548 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
549         .g_skip_frames  = ov2680_g_skip_frames,
550 };
551
552 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
553         .enum_mbus_code = ov2680_enum_mbus_code,
554         .enum_frame_size = ov2680_enum_frame_size,
555         .enum_frame_interval = ov2680_enum_frame_interval,
556         .get_fmt = ov2680_get_fmt,
557         .set_fmt = ov2680_set_fmt,
558 };
559
560 static const struct v4l2_subdev_ops ov2680_ops = {
561         .video = &ov2680_video_ops,
562         .pad = &ov2680_pad_ops,
563         .sensor = &ov2680_sensor_ops,
564 };
565
566 static int ov2680_init_controls(struct ov2680_device *sensor)
567 {
568         static const char * const test_pattern_menu[] = {
569                 "Disabled",
570                 "Color Bars",
571                 "Random Data",
572                 "Square",
573                 "Black Image",
574         };
575         const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
576         struct ov2680_ctrls *ctrls = &sensor->ctrls;
577         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
578         int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
579
580         v4l2_ctrl_handler_init(hdl, 4);
581
582         hdl->lock = &sensor->input_lock;
583
584         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
585         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
586         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
587                                             0, exp_max, 1, exp_max);
588         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 1023, 1, 250);
589         ctrls->test_pattern =
590                 v4l2_ctrl_new_std_menu_items(hdl,
591                                              &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
592                                              ARRAY_SIZE(test_pattern_menu) - 1,
593                                              0, 0, test_pattern_menu);
594
595         ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
596         ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
597
598         if (hdl->error)
599                 return hdl->error;
600
601         sensor->sd.ctrl_handler = hdl;
602         return 0;
603 }
604
605 static void ov2680_remove(struct i2c_client *client)
606 {
607         struct v4l2_subdev *sd = i2c_get_clientdata(client);
608         struct ov2680_device *sensor = to_ov2680_sensor(sd);
609
610         dev_dbg(&client->dev, "ov2680_remove...\n");
611
612         atomisp_unregister_subdev(sd);
613         v4l2_device_unregister_subdev(sd);
614         media_entity_cleanup(&sensor->sd.entity);
615         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
616         pm_runtime_disable(&client->dev);
617 }
618
619 static int ov2680_probe(struct i2c_client *client)
620 {
621         struct device *dev = &client->dev;
622         struct ov2680_device *sensor;
623         int ret;
624
625         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
626         if (!sensor)
627                 return -ENOMEM;
628
629         mutex_init(&sensor->input_lock);
630
631         sensor->client = client;
632         v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_ops);
633
634         ret = v4l2_get_acpi_sensor_info(dev, NULL);
635         if (ret)
636                 return ret;
637
638         sensor->powerdown = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
639         if (IS_ERR(sensor->powerdown))
640                 return dev_err_probe(dev, PTR_ERR(sensor->powerdown), "getting powerdown GPIO\n");
641
642         pm_runtime_set_suspended(dev);
643         pm_runtime_enable(dev);
644         pm_runtime_set_autosuspend_delay(dev, 1000);
645         pm_runtime_use_autosuspend(dev);
646
647         ret = ov2680_s_config(&sensor->sd);
648         if (ret) {
649                 ov2680_remove(client);
650                 return ret;
651         }
652
653         sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
654         sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
655         sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
656
657         ret = ov2680_init_controls(sensor);
658         if (ret) {
659                 ov2680_remove(client);
660                 return ret;
661         }
662
663         ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
664         if (ret) {
665                 ov2680_remove(client);
666                 return ret;
667         }
668
669         ov2680_fill_format(sensor, &sensor->mode.fmt, OV2680_NATIVE_WIDTH, OV2680_NATIVE_HEIGHT);
670
671         ret = atomisp_register_sensor_no_gmin(&sensor->sd, 1, ATOMISP_INPUT_FORMAT_RAW_10,
672                                               atomisp_bayer_order_bggr);
673         if (ret) {
674                 ov2680_remove(client);
675                 return ret;
676         }
677
678         return 0;
679 }
680
681 static int ov2680_suspend(struct device *dev)
682 {
683         struct v4l2_subdev *sd = dev_get_drvdata(dev);
684         struct ov2680_device *sensor = to_ov2680_sensor(sd);
685
686         gpiod_set_value_cansleep(sensor->powerdown, 1);
687         return 0;
688 }
689
690 static int ov2680_resume(struct device *dev)
691 {
692         struct v4l2_subdev *sd = dev_get_drvdata(dev);
693         struct ov2680_device *sensor = to_ov2680_sensor(sd);
694
695         /* according to DS, at least 5ms is needed after DOVDD (enabled by ACPI) */
696         usleep_range(5000, 6000);
697
698         gpiod_set_value_cansleep(sensor->powerdown, 0);
699
700         /* according to DS, 20ms is needed between PWDN and i2c access */
701         msleep(20);
702
703         ov2680_init_registers(sd);
704         return 0;
705 }
706
707 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume, NULL);
708
709 static const struct acpi_device_id ov2680_acpi_match[] = {
710         {"XXOV2680"},
711         {"OVTI2680"},
712         {},
713 };
714 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
715
716 static struct i2c_driver ov2680_driver = {
717         .driver = {
718                 .name = "ov2680",
719                 .pm = pm_sleep_ptr(&ov2680_pm_ops),
720                 .acpi_match_table = ov2680_acpi_match,
721         },
722         .probe_new = ov2680_probe,
723         .remove = ov2680_remove,
724 };
725 module_i2c_driver(ov2680_driver);
726
727 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
728 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
729 MODULE_LICENSE("GPL");