1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Omnivision OV2680 CMOS Image Sensor driver
4 *
5 * Copyright (C) 2018 Linaro Ltd
6 *
7 * Based on OV5640 Sensor Driver
8 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
9 * Copyright (C) 2014-2017 Mentor Graphics Inc.
10 *
11 */
12
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24
25 #include <media/v4l2-cci.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-subdev.h>
30
31 #define OV2680_CHIP_ID 0x2680
32
33 #define OV2680_REG_STREAM_CTRL CCI_REG8(0x0100)
34 #define OV2680_REG_SOFT_RESET CCI_REG8(0x0103)
35
36 #define OV2680_REG_CHIP_ID CCI_REG16(0x300a)
37 #define OV2680_REG_SC_CMMN_SUB_ID CCI_REG8(0x302a)
38 #define OV2680_REG_PLL_MULTIPLIER CCI_REG16(0x3081)
39
40 #define OV2680_REG_EXPOSURE_PK CCI_REG24(0x3500)
41 #define OV2680_REG_R_MANUAL CCI_REG8(0x3503)
42 #define OV2680_REG_GAIN_PK CCI_REG16(0x350a)
43
44 #define OV2680_REG_SENSOR_CTRL_0A CCI_REG8(0x370a)
45
46 #define OV2680_REG_HORIZONTAL_START CCI_REG16(0x3800)
47 #define OV2680_REG_VERTICAL_START CCI_REG16(0x3802)
48 #define OV2680_REG_HORIZONTAL_END CCI_REG16(0x3804)
49 #define OV2680_REG_VERTICAL_END CCI_REG16(0x3806)
50 #define OV2680_REG_HORIZONTAL_OUTPUT_SIZE CCI_REG16(0x3808)
51 #define OV2680_REG_VERTICAL_OUTPUT_SIZE CCI_REG16(0x380a)
52 #define OV2680_REG_TIMING_HTS CCI_REG16(0x380c)
53 #define OV2680_REG_TIMING_VTS CCI_REG16(0x380e)
54 #define OV2680_REG_ISP_X_WIN CCI_REG16(0x3810)
55 #define OV2680_REG_ISP_Y_WIN CCI_REG16(0x3812)
56 #define OV2680_REG_X_INC CCI_REG8(0x3814)
57 #define OV2680_REG_Y_INC CCI_REG8(0x3815)
58 #define OV2680_REG_FORMAT1 CCI_REG8(0x3820)
59 #define OV2680_REG_FORMAT2 CCI_REG8(0x3821)
60
61 #define OV2680_REG_ISP_CTRL00 CCI_REG8(0x5080)
62
63 #define OV2680_REG_X_WIN CCI_REG16(0x5704)
64 #define OV2680_REG_Y_WIN CCI_REG16(0x5706)
65
66 #define OV2680_FRAME_RATE 30
67
68 #define OV2680_NATIVE_WIDTH 1616
69 #define OV2680_NATIVE_HEIGHT 1216
70 #define OV2680_NATIVE_START_LEFT 0
71 #define OV2680_NATIVE_START_TOP 0
72 #define OV2680_ACTIVE_WIDTH 1600
73 #define OV2680_ACTIVE_HEIGHT 1200
74 #define OV2680_ACTIVE_START_LEFT 8
75 #define OV2680_ACTIVE_START_TOP 8
76 #define OV2680_MIN_CROP_WIDTH 2
77 #define OV2680_MIN_CROP_HEIGHT 2
78
79 /* Fixed pre-div of 1/2 */
80 #define OV2680_PLL_PREDIV0 2
81
82 /* Pre-div configurable through reg 0x3080, left at its default of 0x02 : 1/2 */
83 #define OV2680_PLL_PREDIV 2
84
85 /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
86 #define OV2680_PIXELS_PER_LINE 1704
87 #define OV2680_LINES_PER_FRAME 1294
88
89 /* If possible send 16 extra rows / lines to the ISP as padding */
90 #define OV2680_END_MARGIN 16
91
92 /* Max exposure time is VTS - 8 */
93 #define OV2680_INTEGRATION_TIME_MARGIN 8
94
95 #define OV2680_DEFAULT_WIDTH 800
96 #define OV2680_DEFAULT_HEIGHT 600
97
98 /* For enum_frame_size() full-size + binned-/quarter-size */
99 #define OV2680_FRAME_SIZES 2
100
101 static const char * const ov2680_supply_name[] = {
102 "DOVDD",
103 "DVDD",
104 "AVDD",
105 };
106
107 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
108
109 enum {
110 OV2680_19_2_MHZ,
111 OV2680_24_MHZ,
112 };
113
114 static const unsigned long ov2680_xvclk_freqs[] = {
115 [OV2680_19_2_MHZ] = 19200000,
116 [OV2680_24_MHZ] = 24000000,
117 };
118
119 static const u8 ov2680_pll_multipliers[] = {
120 [OV2680_19_2_MHZ] = 69,
121 [OV2680_24_MHZ] = 55,
122 };
123
124 struct ov2680_ctrls {
125 struct v4l2_ctrl_handler handler;
126 struct v4l2_ctrl *exposure;
127 struct v4l2_ctrl *gain;
128 struct v4l2_ctrl *hflip;
129 struct v4l2_ctrl *vflip;
130 struct v4l2_ctrl *test_pattern;
131 struct v4l2_ctrl *link_freq;
132 struct v4l2_ctrl *pixel_rate;
133 };
134
135 struct ov2680_mode {
136 struct v4l2_rect crop;
137 struct v4l2_mbus_framefmt fmt;
138 struct v4l2_fract frame_interval;
139 bool binning;
140 u16 h_start;
141 u16 v_start;
142 u16 h_end;
143 u16 v_end;
144 u16 h_output_size;
145 u16 v_output_size;
146 u16 hts;
147 u16 vts;
148 };
149
150 struct ov2680_dev {
151 struct device *dev;
152 struct regmap *regmap;
153 struct v4l2_subdev sd;
154
155 struct media_pad pad;
156 struct clk *xvclk;
157 u32 xvclk_freq;
158 u8 pll_mult;
159 s64 link_freq[1];
160 u64 pixel_rate;
161 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
162
163 struct gpio_desc *pwdn_gpio;
164 struct mutex lock; /* protect members */
165
166 bool is_streaming;
167
168 struct ov2680_ctrls ctrls;
169 struct ov2680_mode mode;
170 };
171
172 static const struct v4l2_rect ov2680_default_crop = {
173 .left = OV2680_ACTIVE_START_LEFT,
174 .top = OV2680_ACTIVE_START_TOP,
175 .width = OV2680_ACTIVE_WIDTH,
176 .height = OV2680_ACTIVE_HEIGHT,
177 };
178
179 static const char * const test_pattern_menu[] = {
180 "Disabled",
181 "Color Bars",
182 "Random Data",
183 "Square",
184 "Black Image",
185 };
186
187 static const int ov2680_hv_flip_bayer_order[] = {
188 MEDIA_BUS_FMT_SBGGR10_1X10,
189 MEDIA_BUS_FMT_SGRBG10_1X10,
190 MEDIA_BUS_FMT_SGBRG10_1X10,
191 MEDIA_BUS_FMT_SRGGB10_1X10,
192 };
193
194 static const struct reg_sequence ov2680_global_setting[] = {
195 /* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */
196 {0x3016, 0x1c},
197
198 /* R MANUAL set exposure and gain to manual (hw does not do auto) */
199 {0x3503, 0x03},
200
201 /* Analog control register tweaks */
202 {0x3603, 0x39}, /* Reset value 0x99 */
203 {0x3604, 0x24}, /* Reset value 0x74 */
204 {0x3621, 0x37}, /* Reset value 0x44 */
205
206 /* Sensor control register tweaks */
207 {0x3701, 0x64}, /* Reset value 0x61 */
208 {0x3705, 0x3c}, /* Reset value 0x21 */
209 {0x370c, 0x50}, /* Reset value 0x10 */
210 {0x370d, 0xc0}, /* Reset value 0x00 */
211 {0x3718, 0x88}, /* Reset value 0x80 */
212
213 /* PSRAM tweaks */
214 {0x3781, 0x80}, /* Reset value 0x00 */
215 {0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */
216 {0x3789, 0x60}, /* Reset value 0x50 */
217
218 /* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */
219 {0x4000, 0x81},
220
221 /* Set black level compensation range to 0 - 3 (default 0 - 11) */
222 {0x4008, 0x00},
223 {0x4009, 0x03},
224
225 /* VFIFO R2 0x00 -> 0x02 set Frame reset enable */
226 {0x4602, 0x02},
227
228 /* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */
229 {0x481f, 0x36},
230
231 /* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */
232 {0x4825, 0x36},
233
234 /* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */
235 {0x5002, 0x30},
236
237 /*
238 * Window CONTROL 0x00 -> 0x01, enable manual window control,
239 * this is necessary for full size flip and mirror support.
240 */
241 {0x5708, 0x01},
242
243 /*
244 * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster
245 * and enable_general_tail bits based OV2680_R1A_AM10.ovt.
246 */
247 {0x5780, 0x3e},
248
249 /* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */
250 {0x5788, 0x02},
251
252 /* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */
253 {0x578e, 0x08},
254
255 /* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */
256 {0x578f, 0x0c},
257
258 /* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */
259 {0x5792, 0x00},
260 };
261
to_ov2680_dev(struct v4l2_subdev * sd)262 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
263 {
264 return container_of(sd, struct ov2680_dev, sd);
265 }
266
ctrl_to_sd(struct v4l2_ctrl * ctrl)267 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
268 {
269 return &container_of(ctrl->handler, struct ov2680_dev,
270 ctrls.handler)->sd;
271 }
272
ov2680_power_up(struct ov2680_dev * sensor)273 static void ov2680_power_up(struct ov2680_dev *sensor)
274 {
275 if (!sensor->pwdn_gpio)
276 return;
277
278 gpiod_set_value(sensor->pwdn_gpio, 0);
279 usleep_range(5000, 10000);
280 }
281
ov2680_power_down(struct ov2680_dev * sensor)282 static void ov2680_power_down(struct ov2680_dev *sensor)
283 {
284 if (!sensor->pwdn_gpio)
285 return;
286
287 gpiod_set_value(sensor->pwdn_gpio, 1);
288 usleep_range(5000, 10000);
289 }
290
ov2680_set_bayer_order(struct ov2680_dev * sensor,struct v4l2_mbus_framefmt * fmt)291 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
292 struct v4l2_mbus_framefmt *fmt)
293 {
294 int hv_flip = 0;
295
296 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
297 hv_flip += 1;
298
299 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
300 hv_flip += 2;
301
302 fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
303 }
304
305 static struct v4l2_mbus_framefmt *
__ov2680_get_pad_format(struct ov2680_dev * sensor,struct v4l2_subdev_state * state,unsigned int pad,enum v4l2_subdev_format_whence which)306 __ov2680_get_pad_format(struct ov2680_dev *sensor,
307 struct v4l2_subdev_state *state,
308 unsigned int pad,
309 enum v4l2_subdev_format_whence which)
310 {
311 if (which == V4L2_SUBDEV_FORMAT_TRY)
312 return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
313
314 return &sensor->mode.fmt;
315 }
316
317 static struct v4l2_rect *
__ov2680_get_pad_crop(struct ov2680_dev * sensor,struct v4l2_subdev_state * state,unsigned int pad,enum v4l2_subdev_format_whence which)318 __ov2680_get_pad_crop(struct ov2680_dev *sensor,
319 struct v4l2_subdev_state *state,
320 unsigned int pad,
321 enum v4l2_subdev_format_whence which)
322 {
323 if (which == V4L2_SUBDEV_FORMAT_TRY)
324 return v4l2_subdev_get_try_crop(&sensor->sd, state, pad);
325
326 return &sensor->mode.crop;
327 }
328
ov2680_fill_format(struct ov2680_dev * sensor,struct v4l2_mbus_framefmt * fmt,unsigned int width,unsigned int height)329 static void ov2680_fill_format(struct ov2680_dev *sensor,
330 struct v4l2_mbus_framefmt *fmt,
331 unsigned int width, unsigned int height)
332 {
333 memset(fmt, 0, sizeof(*fmt));
334 fmt->width = width;
335 fmt->height = height;
336 fmt->field = V4L2_FIELD_NONE;
337 fmt->colorspace = V4L2_COLORSPACE_SRGB;
338 ov2680_set_bayer_order(sensor, fmt);
339 }
340
ov2680_calc_mode(struct ov2680_dev * sensor)341 static void ov2680_calc_mode(struct ov2680_dev *sensor)
342 {
343 int width = sensor->mode.fmt.width;
344 int height = sensor->mode.fmt.height;
345 int orig_width = width;
346 int orig_height = height;
347
348 if (width <= (sensor->mode.crop.width / 2) &&
349 height <= (sensor->mode.crop.height / 2)) {
350 sensor->mode.binning = true;
351 width *= 2;
352 height *= 2;
353 } else {
354 sensor->mode.binning = false;
355 }
356
357 sensor->mode.h_start = (sensor->mode.crop.left +
358 (sensor->mode.crop.width - width) / 2) & ~1;
359 sensor->mode.v_start = (sensor->mode.crop.top +
360 (sensor->mode.crop.height - height) / 2) & ~1;
361 sensor->mode.h_end =
362 min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
363 OV2680_NATIVE_WIDTH - 1);
364 sensor->mode.v_end =
365 min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
366 OV2680_NATIVE_HEIGHT - 1);
367 sensor->mode.h_output_size = orig_width;
368 sensor->mode.v_output_size = orig_height;
369 sensor->mode.hts = OV2680_PIXELS_PER_LINE;
370 sensor->mode.vts = OV2680_LINES_PER_FRAME;
371 }
372
ov2680_set_mode(struct ov2680_dev * sensor)373 static int ov2680_set_mode(struct ov2680_dev *sensor)
374 {
375 u8 sensor_ctrl_0a, inc, fmt1, fmt2;
376 int ret = 0;
377
378 if (sensor->mode.binning) {
379 sensor_ctrl_0a = 0x23;
380 inc = 0x31;
381 fmt1 = 0xc2;
382 fmt2 = 0x01;
383 } else {
384 sensor_ctrl_0a = 0x21;
385 inc = 0x11;
386 fmt1 = 0xc0;
387 fmt2 = 0x00;
388 }
389
390 cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A,
391 sensor_ctrl_0a, &ret);
392 cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START,
393 sensor->mode.h_start, &ret);
394 cci_write(sensor->regmap, OV2680_REG_VERTICAL_START,
395 sensor->mode.v_start, &ret);
396 cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END,
397 sensor->mode.h_end, &ret);
398 cci_write(sensor->regmap, OV2680_REG_VERTICAL_END,
399 sensor->mode.v_end, &ret);
400 cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE,
401 sensor->mode.h_output_size, &ret);
402 cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE,
403 sensor->mode.v_output_size, &ret);
404 cci_write(sensor->regmap, OV2680_REG_TIMING_HTS,
405 sensor->mode.hts, &ret);
406 cci_write(sensor->regmap, OV2680_REG_TIMING_VTS,
407 sensor->mode.vts, &ret);
408 cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret);
409 cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret);
410 cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret);
411 cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret);
412 cci_write(sensor->regmap, OV2680_REG_X_WIN,
413 sensor->mode.h_output_size, &ret);
414 cci_write(sensor->regmap, OV2680_REG_Y_WIN,
415 sensor->mode.v_output_size, &ret);
416 cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret);
417 cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret);
418
419 return ret;
420 }
421
ov2680_set_vflip(struct ov2680_dev * sensor,s32 val)422 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
423 {
424 int ret;
425
426 if (sensor->is_streaming)
427 return -EBUSY;
428
429 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
430 BIT(2), val ? BIT(2) : 0, NULL);
431 if (ret < 0)
432 return ret;
433
434 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
435 return 0;
436 }
437
ov2680_set_hflip(struct ov2680_dev * sensor,s32 val)438 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
439 {
440 int ret;
441
442 if (sensor->is_streaming)
443 return -EBUSY;
444
445 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
446 BIT(2), val ? BIT(2) : 0, NULL);
447 if (ret < 0)
448 return ret;
449
450 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
451 return 0;
452 }
453
ov2680_test_pattern_set(struct ov2680_dev * sensor,int value)454 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
455 {
456 int ret = 0;
457
458 if (!value)
459 return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
460 BIT(7), 0, NULL);
461
462 cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
463 0x03, value - 1, &ret);
464 cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
465 BIT(7), BIT(7), &ret);
466
467 return ret;
468 }
469
ov2680_gain_set(struct ov2680_dev * sensor,u32 gain)470 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
471 {
472 return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
473 }
474
ov2680_exposure_set(struct ov2680_dev * sensor,u32 exp)475 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
476 {
477 return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
478 NULL);
479 }
480
ov2680_stream_enable(struct ov2680_dev * sensor)481 static int ov2680_stream_enable(struct ov2680_dev *sensor)
482 {
483 int ret;
484
485 ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER,
486 sensor->pll_mult, NULL);
487 if (ret < 0)
488 return ret;
489
490 ret = regmap_multi_reg_write(sensor->regmap,
491 ov2680_global_setting,
492 ARRAY_SIZE(ov2680_global_setting));
493 if (ret < 0)
494 return ret;
495
496 ret = ov2680_set_mode(sensor);
497 if (ret < 0)
498 return ret;
499
500 /* Restore value of all ctrls */
501 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
502 if (ret < 0)
503 return ret;
504
505 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
506 }
507
ov2680_stream_disable(struct ov2680_dev * sensor)508 static int ov2680_stream_disable(struct ov2680_dev *sensor)
509 {
510 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
511 }
512
ov2680_power_off(struct ov2680_dev * sensor)513 static int ov2680_power_off(struct ov2680_dev *sensor)
514 {
515 clk_disable_unprepare(sensor->xvclk);
516 ov2680_power_down(sensor);
517 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
518 return 0;
519 }
520
ov2680_power_on(struct ov2680_dev * sensor)521 static int ov2680_power_on(struct ov2680_dev *sensor)
522 {
523 int ret;
524
525 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
526 if (ret < 0) {
527 dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
528 return ret;
529 }
530
531 if (!sensor->pwdn_gpio) {
532 ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
533 NULL);
534 if (ret != 0) {
535 dev_err(sensor->dev, "sensor soft reset failed\n");
536 goto err_disable_regulators;
537 }
538 usleep_range(1000, 2000);
539 } else {
540 ov2680_power_down(sensor);
541 ov2680_power_up(sensor);
542 }
543
544 ret = clk_prepare_enable(sensor->xvclk);
545 if (ret < 0)
546 goto err_disable_regulators;
547
548 return 0;
549
550 err_disable_regulators:
551 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
552 return ret;
553 }
554
ov2680_s_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)555 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
556 struct v4l2_subdev_frame_interval *fi)
557 {
558 struct ov2680_dev *sensor = to_ov2680_dev(sd);
559
560 mutex_lock(&sensor->lock);
561 fi->interval = sensor->mode.frame_interval;
562 mutex_unlock(&sensor->lock);
563
564 return 0;
565 }
566
ov2680_s_stream(struct v4l2_subdev * sd,int enable)567 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
568 {
569 struct ov2680_dev *sensor = to_ov2680_dev(sd);
570 int ret = 0;
571
572 mutex_lock(&sensor->lock);
573
574 if (sensor->is_streaming == !!enable)
575 goto unlock;
576
577 if (enable) {
578 ret = pm_runtime_resume_and_get(sensor->sd.dev);
579 if (ret < 0)
580 goto unlock;
581
582 ret = ov2680_stream_enable(sensor);
583 if (ret < 0) {
584 pm_runtime_put(sensor->sd.dev);
585 goto unlock;
586 }
587 } else {
588 ret = ov2680_stream_disable(sensor);
589 pm_runtime_put(sensor->sd.dev);
590 }
591
592 sensor->is_streaming = !!enable;
593
594 unlock:
595 mutex_unlock(&sensor->lock);
596
597 return ret;
598 }
599
ov2680_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)600 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
601 struct v4l2_subdev_state *sd_state,
602 struct v4l2_subdev_mbus_code_enum *code)
603 {
604 struct ov2680_dev *sensor = to_ov2680_dev(sd);
605
606 if (code->index != 0)
607 return -EINVAL;
608
609 code->code = sensor->mode.fmt.code;
610
611 return 0;
612 }
613
ov2680_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)614 static int ov2680_get_fmt(struct v4l2_subdev *sd,
615 struct v4l2_subdev_state *sd_state,
616 struct v4l2_subdev_format *format)
617 {
618 struct ov2680_dev *sensor = to_ov2680_dev(sd);
619 struct v4l2_mbus_framefmt *fmt;
620
621 fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad,
622 format->which);
623
624 mutex_lock(&sensor->lock);
625 format->format = *fmt;
626 mutex_unlock(&sensor->lock);
627
628 return 0;
629 }
630
ov2680_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)631 static int ov2680_set_fmt(struct v4l2_subdev *sd,
632 struct v4l2_subdev_state *sd_state,
633 struct v4l2_subdev_format *format)
634 {
635 struct ov2680_dev *sensor = to_ov2680_dev(sd);
636 struct v4l2_mbus_framefmt *try_fmt;
637 const struct v4l2_rect *crop;
638 unsigned int width, height;
639 int ret = 0;
640
641 crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad,
642 format->which);
643
644 /* Limit set_fmt max size to crop width / height */
645 width = clamp_val(ALIGN(format->format.width, 2),
646 OV2680_MIN_CROP_WIDTH, crop->width);
647 height = clamp_val(ALIGN(format->format.height, 2),
648 OV2680_MIN_CROP_HEIGHT, crop->height);
649
650 ov2680_fill_format(sensor, &format->format, width, height);
651
652 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
653 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
654 *try_fmt = format->format;
655 return 0;
656 }
657
658 mutex_lock(&sensor->lock);
659
660 if (sensor->is_streaming) {
661 ret = -EBUSY;
662 goto unlock;
663 }
664
665 sensor->mode.fmt = format->format;
666 ov2680_calc_mode(sensor);
667
668 unlock:
669 mutex_unlock(&sensor->lock);
670
671 return ret;
672 }
673
ov2680_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)674 static int ov2680_get_selection(struct v4l2_subdev *sd,
675 struct v4l2_subdev_state *state,
676 struct v4l2_subdev_selection *sel)
677 {
678 struct ov2680_dev *sensor = to_ov2680_dev(sd);
679
680 switch (sel->target) {
681 case V4L2_SEL_TGT_CROP:
682 mutex_lock(&sensor->lock);
683 sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad,
684 sel->which);
685 mutex_unlock(&sensor->lock);
686 break;
687 case V4L2_SEL_TGT_NATIVE_SIZE:
688 case V4L2_SEL_TGT_CROP_BOUNDS:
689 sel->r.top = 0;
690 sel->r.left = 0;
691 sel->r.width = OV2680_NATIVE_WIDTH;
692 sel->r.height = OV2680_NATIVE_HEIGHT;
693 break;
694 case V4L2_SEL_TGT_CROP_DEFAULT:
695 sel->r = ov2680_default_crop;
696 break;
697 default:
698 return -EINVAL;
699 }
700
701 return 0;
702 }
703
ov2680_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)704 static int ov2680_set_selection(struct v4l2_subdev *sd,
705 struct v4l2_subdev_state *state,
706 struct v4l2_subdev_selection *sel)
707 {
708 struct ov2680_dev *sensor = to_ov2680_dev(sd);
709 struct v4l2_mbus_framefmt *format;
710 struct v4l2_rect *crop;
711 struct v4l2_rect rect;
712
713 if (sel->target != V4L2_SEL_TGT_CROP)
714 return -EINVAL;
715
716 /*
717 * Clamp the boundaries of the crop rectangle to the size of the sensor
718 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
719 * disrupted.
720 */
721 rect.left = clamp_val(ALIGN(sel->r.left, 2),
722 OV2680_NATIVE_START_LEFT, OV2680_NATIVE_WIDTH);
723 rect.top = clamp_val(ALIGN(sel->r.top, 2),
724 OV2680_NATIVE_START_TOP, OV2680_NATIVE_HEIGHT);
725 rect.width = clamp_val(ALIGN(sel->r.width, 2),
726 OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
727 rect.height = clamp_val(ALIGN(sel->r.height, 2),
728 OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);
729
730 /* Make sure the crop rectangle isn't outside the bounds of the array */
731 rect.width = min_t(unsigned int, rect.width,
732 OV2680_NATIVE_WIDTH - rect.left);
733 rect.height = min_t(unsigned int, rect.height,
734 OV2680_NATIVE_HEIGHT - rect.top);
735
736 crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
737
738 mutex_lock(&sensor->lock);
739 if (rect.width != crop->width || rect.height != crop->height) {
740 /*
741 * Reset the output image size if the crop rectangle size has
742 * been modified.
743 */
744 format = __ov2680_get_pad_format(sensor, state, sel->pad,
745 sel->which);
746 format->width = rect.width;
747 format->height = rect.height;
748 }
749
750 *crop = rect;
751 mutex_unlock(&sensor->lock);
752
753 sel->r = rect;
754
755 return 0;
756 }
757
ov2680_init_cfg(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state)758 static int ov2680_init_cfg(struct v4l2_subdev *sd,
759 struct v4l2_subdev_state *sd_state)
760 {
761 struct ov2680_dev *sensor = to_ov2680_dev(sd);
762
763 sd_state->pads[0].try_crop = ov2680_default_crop;
764
765 ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
766 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
767 return 0;
768 }
769
ov2680_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)770 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
771 struct v4l2_subdev_state *sd_state,
772 struct v4l2_subdev_frame_size_enum *fse)
773 {
774 struct ov2680_dev *sensor = to_ov2680_dev(sd);
775 struct v4l2_rect *crop;
776
777 if (fse->index >= OV2680_FRAME_SIZES)
778 return -EINVAL;
779
780 crop = __ov2680_get_pad_crop(sensor, sd_state, fse->pad, fse->which);
781 if (!crop)
782 return -EINVAL;
783
784 fse->min_width = crop->width / (fse->index + 1);
785 fse->min_height = crop->height / (fse->index + 1);
786 fse->max_width = fse->min_width;
787 fse->max_height = fse->min_height;
788
789 return 0;
790 }
791
ov2680_valid_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_interval_enum * fie)792 static bool ov2680_valid_frame_size(struct v4l2_subdev *sd,
793 struct v4l2_subdev_state *sd_state,
794 struct v4l2_subdev_frame_interval_enum *fie)
795 {
796 struct v4l2_subdev_frame_size_enum fse = {
797 .pad = fie->pad,
798 .which = fie->which,
799 };
800 int i;
801
802 for (i = 0; i < OV2680_FRAME_SIZES; i++) {
803 fse.index = i;
804
805 if (ov2680_enum_frame_size(sd, sd_state, &fse))
806 return false;
807
808 if (fie->width == fse.min_width &&
809 fie->height == fse.min_height)
810 return true;
811 }
812
813 return false;
814 }
815
ov2680_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_interval_enum * fie)816 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
817 struct v4l2_subdev_state *sd_state,
818 struct v4l2_subdev_frame_interval_enum *fie)
819 {
820 struct ov2680_dev *sensor = to_ov2680_dev(sd);
821
822 /* Only 1 framerate */
823 if (fie->index || !ov2680_valid_frame_size(sd, sd_state, fie))
824 return -EINVAL;
825
826 fie->interval = sensor->mode.frame_interval;
827
828 return 0;
829 }
830
ov2680_s_ctrl(struct v4l2_ctrl * ctrl)831 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
832 {
833 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
834 struct ov2680_dev *sensor = to_ov2680_dev(sd);
835 int ret;
836
837 /* Only apply changes to the controls if the device is powered up */
838 if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
839 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
840 return 0;
841 }
842
843 switch (ctrl->id) {
844 case V4L2_CID_ANALOGUE_GAIN:
845 ret = ov2680_gain_set(sensor, ctrl->val);
846 break;
847 case V4L2_CID_EXPOSURE:
848 ret = ov2680_exposure_set(sensor, ctrl->val);
849 break;
850 case V4L2_CID_VFLIP:
851 ret = ov2680_set_vflip(sensor, ctrl->val);
852 break;
853 case V4L2_CID_HFLIP:
854 ret = ov2680_set_hflip(sensor, ctrl->val);
855 break;
856 case V4L2_CID_TEST_PATTERN:
857 ret = ov2680_test_pattern_set(sensor, ctrl->val);
858 break;
859 default:
860 ret = -EINVAL;
861 break;
862 }
863
864 pm_runtime_put(sensor->sd.dev);
865 return ret;
866 }
867
868 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
869 .s_ctrl = ov2680_s_ctrl,
870 };
871
872 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
873 .g_frame_interval = ov2680_s_g_frame_interval,
874 .s_frame_interval = ov2680_s_g_frame_interval,
875 .s_stream = ov2680_s_stream,
876 };
877
878 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
879 .init_cfg = ov2680_init_cfg,
880 .enum_mbus_code = ov2680_enum_mbus_code,
881 .enum_frame_size = ov2680_enum_frame_size,
882 .enum_frame_interval = ov2680_enum_frame_interval,
883 .get_fmt = ov2680_get_fmt,
884 .set_fmt = ov2680_set_fmt,
885 .get_selection = ov2680_get_selection,
886 .set_selection = ov2680_set_selection,
887 };
888
889 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
890 .video = &ov2680_video_ops,
891 .pad = &ov2680_pad_ops,
892 };
893
ov2680_mode_init(struct ov2680_dev * sensor)894 static int ov2680_mode_init(struct ov2680_dev *sensor)
895 {
896 /* set initial mode */
897 sensor->mode.crop = ov2680_default_crop;
898 ov2680_fill_format(sensor, &sensor->mode.fmt,
899 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
900 ov2680_calc_mode(sensor);
901
902 sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
903 sensor->mode.frame_interval.numerator = 1;
904
905 return 0;
906 }
907
ov2680_v4l2_register(struct ov2680_dev * sensor)908 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
909 {
910 struct i2c_client *client = to_i2c_client(sensor->dev);
911 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
912 struct ov2680_ctrls *ctrls = &sensor->ctrls;
913 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
914 int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
915 int ret = 0;
916
917 v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
918
919 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
920 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
921 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
922
923 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
924 if (ret < 0)
925 return ret;
926
927 v4l2_ctrl_handler_init(hdl, 5);
928
929 hdl->lock = &sensor->lock;
930
931 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
932 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
933
934 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
935 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
936 ARRAY_SIZE(test_pattern_menu) - 1,
937 0, 0, test_pattern_menu);
938
939 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
940 0, exp_max, 1, exp_max);
941
942 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
943 0, 1023, 1, 250);
944
945 ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, NULL, V4L2_CID_LINK_FREQ,
946 0, 0, sensor->link_freq);
947 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, NULL, V4L2_CID_PIXEL_RATE,
948 0, sensor->pixel_rate,
949 1, sensor->pixel_rate);
950
951 if (hdl->error) {
952 ret = hdl->error;
953 goto cleanup_entity;
954 }
955
956 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
957 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
958 ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
959
960 sensor->sd.ctrl_handler = hdl;
961
962 ret = v4l2_async_register_subdev(&sensor->sd);
963 if (ret < 0)
964 goto cleanup_entity;
965
966 return 0;
967
968 cleanup_entity:
969 media_entity_cleanup(&sensor->sd.entity);
970 v4l2_ctrl_handler_free(hdl);
971
972 return ret;
973 }
974
ov2680_get_regulators(struct ov2680_dev * sensor)975 static int ov2680_get_regulators(struct ov2680_dev *sensor)
976 {
977 int i;
978
979 for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
980 sensor->supplies[i].supply = ov2680_supply_name[i];
981
982 return devm_regulator_bulk_get(sensor->dev,
983 OV2680_NUM_SUPPLIES, sensor->supplies);
984 }
985
ov2680_check_id(struct ov2680_dev * sensor)986 static int ov2680_check_id(struct ov2680_dev *sensor)
987 {
988 u64 chip_id, rev;
989 int ret = 0;
990
991 cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret);
992 cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret);
993 if (ret < 0) {
994 dev_err(sensor->dev, "failed to read chip id\n");
995 return ret;
996 }
997
998 if (chip_id != OV2680_CHIP_ID) {
999 dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
1000 chip_id, OV2680_CHIP_ID);
1001 return -ENODEV;
1002 }
1003
1004 dev_info(sensor->dev, "sensor_revision id = 0x%llx, rev= %lld\n",
1005 chip_id, rev & 0x0f);
1006
1007 return 0;
1008 }
1009
ov2680_parse_dt(struct ov2680_dev * sensor)1010 static int ov2680_parse_dt(struct ov2680_dev *sensor)
1011 {
1012 struct v4l2_fwnode_endpoint bus_cfg = {
1013 .bus_type = V4L2_MBUS_CSI2_DPHY,
1014 };
1015 struct device *dev = sensor->dev;
1016 struct fwnode_handle *ep_fwnode;
1017 struct gpio_desc *gpio;
1018 unsigned int rate = 0;
1019 int i, ret;
1020
1021 /*
1022 * Sometimes the fwnode graph is initialized by the bridge driver.
1023 * Bridge drivers doing this may also add GPIO mappings, wait for this.
1024 */
1025 ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1026 if (!ep_fwnode)
1027 return dev_err_probe(dev, -EPROBE_DEFER,
1028 "waiting for fwnode graph endpoint\n");
1029
1030 ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &bus_cfg);
1031 fwnode_handle_put(ep_fwnode);
1032 if (ret)
1033 return ret;
1034
1035 /*
1036 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
1037 * drivers have standardized on using "powerdown" as con-id name
1038 * for powerdown or shutdown pins. Older DTB files use "reset",
1039 * so fallback to that if there is no "powerdown" pin.
1040 */
1041 gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
1042 if (!gpio)
1043 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1044
1045 ret = PTR_ERR_OR_ZERO(gpio);
1046 if (ret < 0) {
1047 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
1048 goto out_free_bus_cfg;
1049 }
1050
1051 sensor->pwdn_gpio = gpio;
1052
1053 sensor->xvclk = devm_clk_get_optional(dev, "xvclk");
1054 if (IS_ERR(sensor->xvclk)) {
1055 ret = dev_err_probe(dev, PTR_ERR(sensor->xvclk),
1056 "xvclk clock missing or invalid\n");
1057 goto out_free_bus_cfg;
1058 }
1059
1060 /*
1061 * We could have either a 24MHz or 19.2MHz clock rate from either DT or
1062 * ACPI... but we also need to support the weird IPU3 case which will
1063 * have an external clock AND a clock-frequency property. Check for the
1064 * clock-frequency property and if found, set that rate if we managed
1065 * to acquire a clock. This should cover the ACPI case. If the system
1066 * uses devicetree then the configured rate should already be set, so
1067 * we can just read it.
1068 */
1069 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1070 &rate);
1071 if (ret && !sensor->xvclk) {
1072 dev_err_probe(dev, ret, "invalid clock config\n");
1073 goto out_free_bus_cfg;
1074 }
1075
1076 if (!ret && sensor->xvclk) {
1077 ret = clk_set_rate(sensor->xvclk, rate);
1078 if (ret) {
1079 dev_err_probe(dev, ret, "failed to set clock rate\n");
1080 goto out_free_bus_cfg;
1081 }
1082 }
1083
1084 sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk);
1085
1086 for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
1087 if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
1088 break;
1089 }
1090
1091 if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) {
1092 ret = dev_err_probe(dev, -EINVAL,
1093 "unsupported xvclk frequency %d Hz\n",
1094 sensor->xvclk_freq);
1095 goto out_free_bus_cfg;
1096 }
1097
1098 sensor->pll_mult = ov2680_pll_multipliers[i];
1099
1100 sensor->link_freq[0] = sensor->xvclk_freq / OV2680_PLL_PREDIV0 /
1101 OV2680_PLL_PREDIV * sensor->pll_mult;
1102
1103 /* CSI-2 is double data rate, bus-format is 10 bpp */
1104 sensor->pixel_rate = sensor->link_freq[0] * 2;
1105 do_div(sensor->pixel_rate, 10);
1106
1107 if (!bus_cfg.nr_of_link_frequencies) {
1108 dev_warn(dev, "Consider passing 'link-frequencies' in DT\n");
1109 goto skip_link_freq_validation;
1110 }
1111
1112 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1113 if (bus_cfg.link_frequencies[i] == sensor->link_freq[0])
1114 break;
1115
1116 if (bus_cfg.nr_of_link_frequencies == i) {
1117 ret = dev_err_probe(dev, -EINVAL,
1118 "supported link freq %lld not found\n",
1119 sensor->link_freq[0]);
1120 goto out_free_bus_cfg;
1121 }
1122
1123 skip_link_freq_validation:
1124 ret = 0;
1125 out_free_bus_cfg:
1126 v4l2_fwnode_endpoint_free(&bus_cfg);
1127 return ret;
1128 }
1129
ov2680_probe(struct i2c_client * client)1130 static int ov2680_probe(struct i2c_client *client)
1131 {
1132 struct device *dev = &client->dev;
1133 struct ov2680_dev *sensor;
1134 int ret;
1135
1136 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1137 if (!sensor)
1138 return -ENOMEM;
1139
1140 sensor->dev = &client->dev;
1141
1142 sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
1143 if (IS_ERR(sensor->regmap))
1144 return PTR_ERR(sensor->regmap);
1145
1146 ret = ov2680_parse_dt(sensor);
1147 if (ret < 0)
1148 return ret;
1149
1150 ret = ov2680_mode_init(sensor);
1151 if (ret < 0)
1152 return ret;
1153
1154 ret = ov2680_get_regulators(sensor);
1155 if (ret < 0) {
1156 dev_err(dev, "failed to get regulators\n");
1157 return ret;
1158 }
1159
1160 mutex_init(&sensor->lock);
1161
1162 /*
1163 * Power up and verify the chip now, so that if runtime pm is
1164 * disabled the chip is left on and streaming will work.
1165 */
1166 ret = ov2680_power_on(sensor);
1167 if (ret < 0)
1168 goto lock_destroy;
1169
1170 ret = ov2680_check_id(sensor);
1171 if (ret < 0)
1172 goto err_powerdown;
1173
1174 pm_runtime_set_active(&client->dev);
1175 pm_runtime_get_noresume(&client->dev);
1176 pm_runtime_enable(&client->dev);
1177
1178 ret = ov2680_v4l2_register(sensor);
1179 if (ret < 0)
1180 goto err_pm_runtime;
1181
1182 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1183 pm_runtime_use_autosuspend(&client->dev);
1184 pm_runtime_put_autosuspend(&client->dev);
1185
1186 return 0;
1187
1188 err_pm_runtime:
1189 pm_runtime_disable(&client->dev);
1190 pm_runtime_put_noidle(&client->dev);
1191 err_powerdown:
1192 ov2680_power_off(sensor);
1193 lock_destroy:
1194 dev_err(dev, "ov2680 init fail: %d\n", ret);
1195 mutex_destroy(&sensor->lock);
1196
1197 return ret;
1198 }
1199
ov2680_remove(struct i2c_client * client)1200 static void ov2680_remove(struct i2c_client *client)
1201 {
1202 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1203 struct ov2680_dev *sensor = to_ov2680_dev(sd);
1204
1205 v4l2_async_unregister_subdev(&sensor->sd);
1206 mutex_destroy(&sensor->lock);
1207 media_entity_cleanup(&sensor->sd.entity);
1208 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1209
1210 /*
1211 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1212 * make sure to turn power off manually.
1213 */
1214 pm_runtime_disable(&client->dev);
1215 if (!pm_runtime_status_suspended(&client->dev))
1216 ov2680_power_off(sensor);
1217 pm_runtime_set_suspended(&client->dev);
1218 }
1219
ov2680_suspend(struct device * dev)1220 static int ov2680_suspend(struct device *dev)
1221 {
1222 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1223 struct ov2680_dev *sensor = to_ov2680_dev(sd);
1224
1225 if (sensor->is_streaming)
1226 ov2680_stream_disable(sensor);
1227
1228 return ov2680_power_off(sensor);
1229 }
1230
ov2680_resume(struct device * dev)1231 static int ov2680_resume(struct device *dev)
1232 {
1233 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1234 struct ov2680_dev *sensor = to_ov2680_dev(sd);
1235 int ret;
1236
1237 ret = ov2680_power_on(sensor);
1238 if (ret < 0)
1239 goto stream_disable;
1240
1241 if (sensor->is_streaming) {
1242 ret = ov2680_stream_enable(sensor);
1243 if (ret < 0)
1244 goto stream_disable;
1245 }
1246
1247 return 0;
1248
1249 stream_disable:
1250 ov2680_stream_disable(sensor);
1251 sensor->is_streaming = false;
1252
1253 return ret;
1254 }
1255
1256 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
1257 NULL);
1258
1259 static const struct of_device_id ov2680_dt_ids[] = {
1260 { .compatible = "ovti,ov2680" },
1261 { /* sentinel */ },
1262 };
1263 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1264
1265 static const struct acpi_device_id ov2680_acpi_ids[] = {
1266 { "OVTI2680" },
1267 { /* sentinel */ }
1268 };
1269 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
1270
1271 static struct i2c_driver ov2680_i2c_driver = {
1272 .driver = {
1273 .name = "ov2680",
1274 .pm = pm_sleep_ptr(&ov2680_pm_ops),
1275 .of_match_table = ov2680_dt_ids,
1276 .acpi_match_table = ov2680_acpi_ids,
1277 },
1278 .probe = ov2680_probe,
1279 .remove = ov2680_remove,
1280 };
1281 module_i2c_driver(ov2680_i2c_driver);
1282
1283 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1284 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1285 MODULE_LICENSE("GPL v2");
1286