spnego: add missing OID to oid registry
[sfrench/cifs-2.6.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 "ov2680.h"
32
33 static const struct v4l2_rect ov2680_default_crop = {
34         .left = OV2680_ACTIVE_START_LEFT,
35         .top = OV2680_ACTIVE_START_TOP,
36         .width = OV2680_ACTIVE_WIDTH,
37         .height = OV2680_ACTIVE_HEIGHT,
38 };
39
40 static int ov2680_write_reg_array(struct i2c_client *client,
41                                   const struct ov2680_reg *reglist)
42 {
43         const struct ov2680_reg *next = reglist;
44         int ret;
45
46         for (; next->reg != 0; next++) {
47                 ret = ov_write_reg8(client, next->reg, next->val);
48                 if (ret)
49                         return ret;
50         }
51
52         return 0;
53 }
54
55 static void ov2680_set_bayer_order(struct ov2680_dev *sensor, struct v4l2_mbus_framefmt *fmt)
56 {
57         static const int ov2680_hv_flip_bayer_order[] = {
58                 MEDIA_BUS_FMT_SBGGR10_1X10,
59                 MEDIA_BUS_FMT_SGRBG10_1X10,
60                 MEDIA_BUS_FMT_SGBRG10_1X10,
61                 MEDIA_BUS_FMT_SRGGB10_1X10,
62         };
63         int hv_flip = 0;
64
65         if (sensor->ctrls.vflip->val)
66                 hv_flip += 1;
67
68         if (sensor->ctrls.hflip->val)
69                 hv_flip += 2;
70
71         fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
72 }
73
74 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
75 {
76         int ret;
77
78         if (sensor->is_streaming)
79                 return -EBUSY;
80
81         ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
82         if (ret < 0)
83                 return ret;
84
85         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
86         return 0;
87 }
88
89 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
90 {
91         int ret;
92
93         if (sensor->is_streaming)
94                 return -EBUSY;
95
96         ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
97         if (ret < 0)
98                 return ret;
99
100         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
101         return 0;
102 }
103
104 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
105 {
106         return ov_write_reg24(sensor->client, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
107 }
108
109 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
110 {
111         return ov_write_reg16(sensor->client, OV2680_REG_GAIN_PK, gain);
112 }
113
114 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
115 {
116         int ret;
117
118         if (!value)
119                 return ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), 0);
120
121         ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
122         if (ret < 0)
123                 return ret;
124
125         ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
126         if (ret < 0)
127                 return ret;
128
129         return 0;
130 }
131
132 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
133 {
134         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
135         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
136         int ret;
137
138         /* Only apply changes to the controls if the device is powered up */
139         if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
140                 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
141                 return 0;
142         }
143
144         switch (ctrl->id) {
145         case V4L2_CID_VFLIP:
146                 ret = ov2680_set_vflip(sensor, ctrl->val);
147                 break;
148         case V4L2_CID_HFLIP:
149                 ret = ov2680_set_hflip(sensor, ctrl->val);
150                 break;
151         case V4L2_CID_EXPOSURE:
152                 ret = ov2680_exposure_set(sensor, ctrl->val);
153                 break;
154         case V4L2_CID_GAIN:
155                 ret = ov2680_gain_set(sensor, ctrl->val);
156                 break;
157         case V4L2_CID_TEST_PATTERN:
158                 ret = ov2680_test_pattern_set(sensor, ctrl->val);
159                 break;
160         default:
161                 ret = -EINVAL;
162         }
163
164         pm_runtime_put(sensor->sd.dev);
165         return ret;
166 }
167
168 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
169         .s_ctrl = ov2680_s_ctrl,
170 };
171
172 static int ov2680_init_registers(struct v4l2_subdev *sd)
173 {
174         struct i2c_client *client = v4l2_get_subdevdata(sd);
175         int ret;
176
177         ret = ov_write_reg8(client, OV2680_SW_RESET, 0x01);
178
179         /* Wait for sensor reset */
180         usleep_range(1000, 2000);
181
182         ret |= ov2680_write_reg_array(client, ov2680_global_setting);
183
184         return ret;
185 }
186
187 static struct v4l2_mbus_framefmt *
188 __ov2680_get_pad_format(struct ov2680_dev *sensor, struct v4l2_subdev_state *state,
189                         unsigned int pad, enum v4l2_subdev_format_whence which)
190 {
191         if (which == V4L2_SUBDEV_FORMAT_TRY)
192                 return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
193
194         return &sensor->mode.fmt;
195 }
196
197 static struct v4l2_rect *
198 __ov2680_get_pad_crop(struct ov2680_dev *sensor, struct v4l2_subdev_state *state,
199                       unsigned int pad, enum v4l2_subdev_format_whence which)
200 {
201         if (which == V4L2_SUBDEV_FORMAT_TRY)
202                 return v4l2_subdev_get_try_crop(&sensor->sd, state, pad);
203
204         return &sensor->mode.crop;
205 }
206
207 static void ov2680_fill_format(struct ov2680_dev *sensor,
208                                struct v4l2_mbus_framefmt *fmt,
209                                unsigned int width, unsigned int height)
210 {
211         memset(fmt, 0, sizeof(*fmt));
212         fmt->width = width;
213         fmt->height = height;
214         fmt->field = V4L2_FIELD_NONE;
215         ov2680_set_bayer_order(sensor, fmt);
216 }
217
218 static void ov2680_calc_mode(struct ov2680_dev *sensor)
219 {
220         int width = sensor->mode.fmt.width;
221         int height = sensor->mode.fmt.height;
222         int orig_width = width;
223         int orig_height = height;
224
225         if (width  <= (sensor->mode.crop.width / 2) &&
226             height <= (sensor->mode.crop.height / 2)) {
227                 sensor->mode.binning = true;
228                 width *= 2;
229                 height *= 2;
230         } else {
231                 sensor->mode.binning = false;
232         }
233
234         sensor->mode.h_start =
235                 (sensor->mode.crop.left + (sensor->mode.crop.width - width) / 2) & ~1;
236         sensor->mode.v_start =
237                 (sensor->mode.crop.top + (sensor->mode.crop.height - height) / 2) & ~1;
238         sensor->mode.h_end = min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
239                                  OV2680_NATIVE_WIDTH - 1);
240         sensor->mode.v_end = min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
241                                  OV2680_NATIVE_HEIGHT - 1);
242         sensor->mode.h_output_size = orig_width;
243         sensor->mode.v_output_size = orig_height;
244         sensor->mode.hts = OV2680_PIXELS_PER_LINE;
245         sensor->mode.vts = OV2680_LINES_PER_FRAME;
246 }
247
248 static int ov2680_set_mode(struct ov2680_dev *sensor)
249 {
250         struct i2c_client *client = sensor->client;
251         u8 sensor_ctrl_0a, inc, fmt1, fmt2;
252         int ret;
253
254         if (sensor->mode.binning) {
255                 sensor_ctrl_0a = 0x23;
256                 inc = 0x31;
257                 fmt1 = 0xc2;
258                 fmt2 = 0x01;
259         } else {
260                 sensor_ctrl_0a = 0x21;
261                 inc = 0x11;
262                 fmt1 = 0xc0;
263                 fmt2 = 0x00;
264         }
265
266         ret = ov_write_reg8(client, OV2680_REG_SENSOR_CTRL_0A, sensor_ctrl_0a);
267         if (ret)
268                 return ret;
269
270         ret = ov_write_reg16(client, OV2680_HORIZONTAL_START_H, sensor->mode.h_start);
271         if (ret)
272                 return ret;
273
274         ret = ov_write_reg16(client, OV2680_VERTICAL_START_H, sensor->mode.v_start);
275         if (ret)
276                 return ret;
277
278         ret = ov_write_reg16(client, OV2680_HORIZONTAL_END_H, sensor->mode.h_end);
279         if (ret)
280                 return ret;
281
282         ret = ov_write_reg16(client, OV2680_VERTICAL_END_H, sensor->mode.v_end);
283         if (ret)
284                 return ret;
285
286         ret = ov_write_reg16(client, OV2680_HORIZONTAL_OUTPUT_SIZE_H,
287                                  sensor->mode.h_output_size);
288         if (ret)
289                 return ret;
290
291         ret = ov_write_reg16(client, OV2680_VERTICAL_OUTPUT_SIZE_H,
292                                  sensor->mode.v_output_size);
293         if (ret)
294                 return ret;
295
296         ret = ov_write_reg16(client, OV2680_HTS, sensor->mode.hts);
297         if (ret)
298                 return ret;
299
300         ret = ov_write_reg16(client, OV2680_VTS, sensor->mode.vts);
301         if (ret)
302                 return ret;
303
304         ret = ov_write_reg16(client, OV2680_ISP_X_WIN, 0);
305         if (ret)
306                 return ret;
307
308         ret = ov_write_reg16(client, OV2680_ISP_Y_WIN, 0);
309         if (ret)
310                 return ret;
311
312         ret = ov_write_reg8(client, OV2680_X_INC, inc);
313         if (ret)
314                 return ret;
315
316         ret = ov_write_reg8(client, OV2680_Y_INC, inc);
317         if (ret)
318                 return ret;
319
320         ret = ov_write_reg16(client, OV2680_X_WIN, sensor->mode.h_output_size);
321         if (ret)
322                 return ret;
323
324         ret = ov_write_reg16(client, OV2680_Y_WIN, sensor->mode.v_output_size);
325         if (ret)
326                 return ret;
327
328         ret = ov_write_reg8(client, OV2680_REG_FORMAT1, fmt1);
329         if (ret)
330                 return ret;
331
332         ret = ov_write_reg8(client, OV2680_REG_FORMAT2, fmt2);
333         if (ret)
334                 return ret;
335
336         return 0;
337 }
338
339 static int ov2680_set_fmt(struct v4l2_subdev *sd,
340                           struct v4l2_subdev_state *sd_state,
341                           struct v4l2_subdev_format *format)
342 {
343         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
344         struct v4l2_mbus_framefmt *fmt;
345         const struct v4l2_rect *crop;
346         unsigned int width, height;
347
348         crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad, format->which);
349
350         /* Limit set_fmt max size to crop width / height */
351         width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
352                         OV2680_MIN_CROP_WIDTH, crop->width);
353         height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
354                          OV2680_MIN_CROP_HEIGHT, crop->height);
355
356         fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
357         ov2680_fill_format(sensor, fmt, width, height);
358
359         format->format = *fmt;
360
361         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
362                 return 0;
363
364         mutex_lock(&sensor->lock);
365         ov2680_calc_mode(sensor);
366         mutex_unlock(&sensor->lock);
367         return 0;
368 }
369
370 static int ov2680_get_fmt(struct v4l2_subdev *sd,
371                           struct v4l2_subdev_state *sd_state,
372                           struct v4l2_subdev_format *format)
373 {
374         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
375         struct v4l2_mbus_framefmt *fmt;
376
377         fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
378         format->format = *fmt;
379         return 0;
380 }
381
382 static int ov2680_get_selection(struct v4l2_subdev *sd,
383                                 struct v4l2_subdev_state *state,
384                                 struct v4l2_subdev_selection *sel)
385 {
386         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
387
388         switch (sel->target) {
389         case V4L2_SEL_TGT_CROP:
390                 mutex_lock(&sensor->lock);
391                 sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
392                 mutex_unlock(&sensor->lock);
393                 break;
394         case V4L2_SEL_TGT_NATIVE_SIZE:
395         case V4L2_SEL_TGT_CROP_BOUNDS:
396                 sel->r.top = 0;
397                 sel->r.left = 0;
398                 sel->r.width = OV2680_NATIVE_WIDTH;
399                 sel->r.height = OV2680_NATIVE_HEIGHT;
400                 break;
401         case V4L2_SEL_TGT_CROP_DEFAULT:
402                 sel->r.top = OV2680_ACTIVE_START_TOP;
403                 sel->r.left = OV2680_ACTIVE_START_LEFT;
404                 sel->r.width = OV2680_ACTIVE_WIDTH;
405                 sel->r.height = OV2680_ACTIVE_HEIGHT;
406                 break;
407         default:
408                 return -EINVAL;
409         }
410
411         return 0;
412 }
413
414 static int ov2680_set_selection(struct v4l2_subdev *sd,
415                                 struct v4l2_subdev_state *state,
416                                 struct v4l2_subdev_selection *sel)
417 {
418         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
419         struct v4l2_mbus_framefmt *format;
420         struct v4l2_rect *__crop;
421         struct v4l2_rect rect;
422
423         if (sel->target != V4L2_SEL_TGT_CROP)
424                 return -EINVAL;
425
426         /*
427          * Clamp the boundaries of the crop rectangle to the size of the sensor
428          * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
429          * disrupted.
430          */
431         rect.left = clamp(ALIGN(sel->r.left, 2), OV2680_NATIVE_START_LEFT,
432                           OV2680_NATIVE_WIDTH);
433         rect.top = clamp(ALIGN(sel->r.top, 2), OV2680_NATIVE_START_TOP,
434                          OV2680_NATIVE_HEIGHT);
435         rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
436                              OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
437         rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
438                               OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);
439
440         /* Make sure the crop rectangle isn't outside the bounds of the array */
441         rect.width = min_t(unsigned int, rect.width,
442                            OV2680_NATIVE_WIDTH - rect.left);
443         rect.height = min_t(unsigned int, rect.height,
444                             OV2680_NATIVE_HEIGHT - rect.top);
445
446         __crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
447
448         if (rect.width != __crop->width || rect.height != __crop->height) {
449                 /*
450                  * Reset the output image size if the crop rectangle size has
451                  * been modified.
452                  */
453                 format = __ov2680_get_pad_format(sensor, state, sel->pad, sel->which);
454                 format->width = rect.width;
455                 format->height = rect.height;
456         }
457
458         *__crop = rect;
459         sel->r = rect;
460
461         return 0;
462 }
463
464 static int ov2680_init_cfg(struct v4l2_subdev *sd,
465                            struct v4l2_subdev_state *sd_state)
466 {
467         struct v4l2_subdev_format fmt = {
468                 .which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
469                 : V4L2_SUBDEV_FORMAT_ACTIVE,
470                 .format = {
471                         .width = 800,
472                         .height = 600,
473                 },
474         };
475
476         sd_state->pads[0].try_crop = ov2680_default_crop;
477
478         return ov2680_set_fmt(sd, sd_state, &fmt);
479 }
480
481 static int ov2680_detect(struct i2c_client *client)
482 {
483         struct i2c_adapter *adapter = client->adapter;
484         u32 high = 0, low = 0;
485         int ret;
486         u16 id;
487         u8 revision;
488
489         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
490                 return -ENODEV;
491
492         ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_H, &high);
493         if (ret) {
494                 dev_err(&client->dev, "sensor_id_high read failed (%d)\n", ret);
495                 return -ENODEV;
496         }
497         ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_L, &low);
498         id = ((((u16)high) << 8) | (u16)low);
499
500         if (id != OV2680_ID) {
501                 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
502                 return -ENODEV;
503         }
504
505         ret = ov_read_reg8(client, OV2680_SC_CMMN_SUB_ID, &high);
506         revision = (u8)high & 0x0f;
507
508         dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
509                  id, revision);
510
511         return 0;
512 }
513
514 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
515 {
516         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
517         struct i2c_client *client = v4l2_get_subdevdata(sd);
518         int ret = 0;
519
520         mutex_lock(&sensor->lock);
521
522         if (sensor->is_streaming == enable) {
523                 dev_warn(&client->dev, "stream already %s\n", enable ? "started" : "stopped");
524                 goto error_unlock;
525         }
526
527         if (enable) {
528                 ret = pm_runtime_get_sync(sensor->sd.dev);
529                 if (ret < 0)
530                         goto error_power_down;
531
532                 ret = ov2680_set_mode(sensor);
533                 if (ret)
534                         goto error_power_down;
535
536                 /* Restore value of all ctrls */
537                 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
538                 if (ret)
539                         goto error_power_down;
540
541                 ret = ov_write_reg8(client, OV2680_SW_STREAM, OV2680_START_STREAMING);
542                 if (ret)
543                         goto error_power_down;
544         } else {
545                 ov_write_reg8(client, OV2680_SW_STREAM, OV2680_STOP_STREAMING);
546                 pm_runtime_put(sensor->sd.dev);
547         }
548
549         sensor->is_streaming = enable;
550         v4l2_ctrl_activate(sensor->ctrls.vflip, !enable);
551         v4l2_ctrl_activate(sensor->ctrls.hflip, !enable);
552
553         mutex_unlock(&sensor->lock);
554         return 0;
555
556 error_power_down:
557         pm_runtime_put(sensor->sd.dev);
558         sensor->is_streaming = false;
559 error_unlock:
560         mutex_unlock(&sensor->lock);
561         return ret;
562 }
563
564 static int ov2680_s_config(struct v4l2_subdev *sd)
565 {
566         struct i2c_client *client = v4l2_get_subdevdata(sd);
567         int ret;
568
569         ret = pm_runtime_get_sync(&client->dev);
570         if (ret < 0) {
571                 dev_err(&client->dev, "ov2680 power-up err.\n");
572                 goto fail_power_on;
573         }
574
575         /* config & detect sensor */
576         ret = ov2680_detect(client);
577         if (ret)
578                 dev_err(&client->dev, "ov2680_detect err s_config.\n");
579
580 fail_power_on:
581         pm_runtime_put(&client->dev);
582         return ret;
583 }
584
585 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
586                                    struct v4l2_subdev_frame_interval *interval)
587 {
588         interval->interval.numerator = 1;
589         interval->interval.denominator = OV2680_FPS;
590         return 0;
591 }
592
593 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
594                                  struct v4l2_subdev_state *sd_state,
595                                  struct v4l2_subdev_mbus_code_enum *code)
596 {
597         /* We support only a single format */
598         if (code->index)
599                 return -EINVAL;
600
601         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
602         return 0;
603 }
604
605 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
606                                   struct v4l2_subdev_state *sd_state,
607                                   struct v4l2_subdev_frame_size_enum *fse)
608 {
609         static const struct v4l2_frmsize_discrete ov2680_frame_sizes[] = {
610                 { 1616, 1216 },
611                 { 1616, 1096 },
612                 { 1616,  916 },
613                 { 1456, 1096 },
614                 { 1296,  976 },
615                 { 1296,  736 },
616                 {  784,  592 },
617                 {  656,  496 },
618         };
619         int index = fse->index;
620
621         if (index >= ARRAY_SIZE(ov2680_frame_sizes))
622                 return -EINVAL;
623
624         fse->min_width = ov2680_frame_sizes[index].width;
625         fse->min_height = ov2680_frame_sizes[index].height;
626         fse->max_width = ov2680_frame_sizes[index].width;
627         fse->max_height = ov2680_frame_sizes[index].height;
628
629         return 0;
630 }
631
632 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
633                                       struct v4l2_subdev_state *sd_state,
634                                       struct v4l2_subdev_frame_interval_enum *fie)
635 {
636         /* Only 1 framerate */
637         if (fie->index)
638                 return -EINVAL;
639
640         fie->interval.numerator = 1;
641         fie->interval.denominator = OV2680_FPS;
642         return 0;
643 }
644
645 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
646 {
647         *frames = OV2680_SKIP_FRAMES;
648         return 0;
649 }
650
651 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
652         .s_stream = ov2680_s_stream,
653         .g_frame_interval = ov2680_g_frame_interval,
654 };
655
656 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
657         .g_skip_frames  = ov2680_g_skip_frames,
658 };
659
660 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
661         .init_cfg = ov2680_init_cfg,
662         .enum_mbus_code = ov2680_enum_mbus_code,
663         .enum_frame_size = ov2680_enum_frame_size,
664         .enum_frame_interval = ov2680_enum_frame_interval,
665         .get_fmt = ov2680_get_fmt,
666         .set_fmt = ov2680_set_fmt,
667         .get_selection = ov2680_get_selection,
668         .set_selection = ov2680_set_selection,
669 };
670
671 static const struct v4l2_subdev_ops ov2680_ops = {
672         .video = &ov2680_video_ops,
673         .pad = &ov2680_pad_ops,
674         .sensor = &ov2680_sensor_ops,
675 };
676
677 static int ov2680_init_controls(struct ov2680_dev *sensor)
678 {
679         static const char * const test_pattern_menu[] = {
680                 "Disabled",
681                 "Color Bars",
682                 "Random Data",
683                 "Square",
684                 "Black Image",
685         };
686         const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
687         struct ov2680_ctrls *ctrls = &sensor->ctrls;
688         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
689         int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
690
691         v4l2_ctrl_handler_init(hdl, 4);
692
693         hdl->lock = &sensor->lock;
694
695         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
696         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
697         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
698                                             0, exp_max, 1, exp_max);
699         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 1023, 1, 250);
700         ctrls->test_pattern =
701                 v4l2_ctrl_new_std_menu_items(hdl,
702                                              &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
703                                              ARRAY_SIZE(test_pattern_menu) - 1,
704                                              0, 0, test_pattern_menu);
705
706         ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
707         ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
708
709         if (hdl->error)
710                 return hdl->error;
711
712         sensor->sd.ctrl_handler = hdl;
713         return 0;
714 }
715
716 static void ov2680_remove(struct i2c_client *client)
717 {
718         struct v4l2_subdev *sd = i2c_get_clientdata(client);
719         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
720
721         dev_dbg(&client->dev, "ov2680_remove...\n");
722
723         v4l2_async_unregister_subdev(&sensor->sd);
724         media_entity_cleanup(&sensor->sd.entity);
725         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
726         mutex_destroy(&sensor->lock);
727         fwnode_handle_put(sensor->ep_fwnode);
728         pm_runtime_disable(&client->dev);
729 }
730
731 static int ov2680_probe(struct i2c_client *client)
732 {
733         struct device *dev = &client->dev;
734         struct ov2680_dev *sensor;
735         int ret;
736
737         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
738         if (!sensor)
739                 return -ENOMEM;
740
741         mutex_init(&sensor->lock);
742
743         sensor->client = client;
744         v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_ops);
745
746         /*
747          * Sometimes the fwnode graph is initialized by the bridge driver.
748          * Bridge drivers doing this may also add GPIO mappings, wait for this.
749          */
750         sensor->ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
751         if (!sensor->ep_fwnode)
752                 return dev_err_probe(dev, -EPROBE_DEFER, "waiting for fwnode graph endpoint\n");
753
754         sensor->powerdown = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
755         if (IS_ERR(sensor->powerdown)) {
756                 fwnode_handle_put(sensor->ep_fwnode);
757                 return dev_err_probe(dev, PTR_ERR(sensor->powerdown), "getting powerdown GPIO\n");
758         }
759
760         pm_runtime_set_suspended(dev);
761         pm_runtime_enable(dev);
762         pm_runtime_set_autosuspend_delay(dev, 1000);
763         pm_runtime_use_autosuspend(dev);
764
765         ret = ov2680_s_config(&sensor->sd);
766         if (ret) {
767                 ov2680_remove(client);
768                 return ret;
769         }
770
771         sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
772         sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
773         sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
774         sensor->sd.fwnode = sensor->ep_fwnode;
775
776         ret = ov2680_init_controls(sensor);
777         if (ret) {
778                 ov2680_remove(client);
779                 return ret;
780         }
781
782         ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
783         if (ret) {
784                 ov2680_remove(client);
785                 return ret;
786         }
787
788         sensor->mode.crop = ov2680_default_crop;
789         ov2680_fill_format(sensor, &sensor->mode.fmt, OV2680_NATIVE_WIDTH, OV2680_NATIVE_HEIGHT);
790         ov2680_calc_mode(sensor);
791
792         ret = v4l2_async_register_subdev_sensor(&sensor->sd);
793         if (ret) {
794                 ov2680_remove(client);
795                 return ret;
796         }
797
798         return 0;
799 }
800
801 static int ov2680_suspend(struct device *dev)
802 {
803         struct v4l2_subdev *sd = dev_get_drvdata(dev);
804         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
805
806         gpiod_set_value_cansleep(sensor->powerdown, 1);
807         return 0;
808 }
809
810 static int ov2680_resume(struct device *dev)
811 {
812         struct v4l2_subdev *sd = dev_get_drvdata(dev);
813         struct ov2680_dev *sensor = to_ov2680_sensor(sd);
814
815         /* according to DS, at least 5ms is needed after DOVDD (enabled by ACPI) */
816         usleep_range(5000, 6000);
817
818         gpiod_set_value_cansleep(sensor->powerdown, 0);
819
820         /* according to DS, 20ms is needed between PWDN and i2c access */
821         msleep(20);
822
823         ov2680_init_registers(sd);
824         return 0;
825 }
826
827 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume, NULL);
828
829 static const struct acpi_device_id ov2680_acpi_match[] = {
830         {"XXOV2680"},
831         {"OVTI2680"},
832         {},
833 };
834 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
835
836 static struct i2c_driver ov2680_driver = {
837         .driver = {
838                 .name = "ov2680",
839                 .pm = pm_sleep_ptr(&ov2680_pm_ops),
840                 .acpi_match_table = ov2680_acpi_match,
841         },
842         .probe = ov2680_probe,
843         .remove = ov2680_remove,
844 };
845 module_i2c_driver(ov2680_driver);
846
847 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
848 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
849 MODULE_LICENSE("GPL");