1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * gc2093 sensor driver
4 *
5 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6 *
7 * V0.0X01.0X00 first version.
8 * V0.0X01.0X01 Add HDR support.
9 * V0.0X01.0X02 update sensor driver
10 * 1. fix linear mode ae flicker issue.
11 * 2. add hdr mode exposure limit issue.
12 * 3. fix hdr mode highlighting pink issue.
13 * 4. add some debug info.
14 */
15
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/of_graph.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/version.h>
26 #include <linux/rk-camera-module.h>
27 #include <linux/rk-preisp.h>
28
29 #include <media/v4l2-async.h>
30 #include <media/media-entity.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-fwnode.h>
34 #include <media/v4l2-subdev.h>
35
36 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x02)
37 #define GC2093_NAME "gc2093"
38 #define GC2093_MEDIA_BUS_FMT MEDIA_BUS_FMT_SRGGB10_1X10
39
40 #define MIPI_FREQ_150M 150000000
41 #define MIPI_FREQ_300M 300000000
42 #define MIPI_FREQ_594M 594000000
43
44 #define GC2093_XVCLK_FREQ 27000000
45
46 #define GC2093_REG_CHIP_ID_H 0x03F0
47 #define GC2093_REG_CHIP_ID_L 0x03F1
48
49 #define GC2093_REG_EXP_SHORT_H 0x0001
50 #define GC2093_REG_EXP_SHORT_L 0x0002
51 #define GC2093_REG_EXP_LONG_H 0x0003
52 #define GC2093_REG_EXP_LONG_L 0x0004
53
54 #define GC2093_REG_VB_H 0x0007
55 #define GC2093_REG_VB_L 0x0008
56
57 #define GC2093_MIRROR_FLIP_REG 0x0017
58 #define MIRROR_MASK BIT(0)
59 #define FLIP_MASK BIT(1)
60
61 #define GC2093_REG_CTRL_MODE 0x003E
62 #define GC2093_MODE_SW_STANDBY 0x11
63 #define GC2093_MODE_STREAMING 0x91
64
65 #define GC2093_CHIP_ID 0x2093
66
67 #define GC2093_VTS_MAX 0x3FFF
68 #define GC2093_HTS_MAX 0xFFF
69
70 #define GC2093_EXPOSURE_MAX 0x3FFF
71 #define GC2093_EXPOSURE_MIN 1
72 #define GC2093_EXPOSURE_STEP 1
73
74 #define GC2093_GAIN_MIN 0x40
75 #define GC2093_GAIN_MAX 0x2000
76 #define GC2093_GAIN_STEP 1
77 #define GC2093_GAIN_DEFAULT 64
78 #define REG_NULL 0xFFFF
79
80 #define GC2093_LANES 2
81 #define PI_REG_VALUE_FO 4
82 #define PI_REG_VALUE_SI 6
83 #define PI_REG_VALUE_EI 8
84 #define PI_RATE_VALUE 10
85 #define PI_OFFSET_VALUE 16
86 #define PRE_GIN_COUNT 64
87 #define SLEEP_LOW_VALUE 1000
88 #define SLEEP_HIGH_VALUE 2000
89 #define SLEEP_SLOW_VALUE 10000
90 #define SLEEP_SHIGH_VALUE 20000
91
92 static const char *const gc2093_supply_names[] = {
93 "dovdd", /* Digital I/O power */
94 "avdd", /* Analog power */
95 "dvdd", /* Digital power */
96 };
97
98 #define GC2093_NUM_SUPPLIES ARRAY_SIZE(gc2093_supply_names)
99
100 #define to_gc2093(sd) container_of(sd, struct gc2093, subdev)
101
102 enum {
103 LINK_FREQ_150M_INDEX,
104 LINK_FREQ_300M_INDEX,
105 };
106
107 struct gain_reg_config {
108 u32 value;
109 u16 analog_gain;
110 u16 col_gain;
111 u16 analog_sw;
112 u16 ram_width;
113 };
114
115 struct gc2093_mode {
116 u32 width;
117 u32 height;
118 struct v4l2_fract max_fps;
119 u32 hts_def;
120 u32 vts_def;
121 u32 exp_def;
122 u32 link_freq_index;
123 const struct reg_sequence *reg_list;
124 u32 reg_num;
125 u32 hdr_mode;
126 u32 vc[PAD_MAX];
127 };
128
129 struct gc2093 {
130 struct device *dev;
131 struct clk *xvclk;
132 struct regmap *regmap;
133 struct gpio_desc *reset_gpio;
134 struct gpio_desc *pwdn_gpio;
135 struct regulator_bulk_data supplies[GC2093_NUM_SUPPLIES];
136
137 struct v4l2_subdev subdev;
138 struct media_pad pad;
139 struct v4l2_ctrl_handler ctrl_handler;
140 struct v4l2_ctrl *exposure;
141 struct v4l2_ctrl *anal_gain;
142 struct v4l2_ctrl *hblank;
143 struct v4l2_ctrl *vblank;
144 struct v4l2_ctrl *h_flip;
145 struct v4l2_ctrl *v_flip;
146 struct v4l2_ctrl *link_freq;
147 struct v4l2_ctrl *pixel_rate;
148
149 struct mutex lock;
150 bool streaming;
151 bool power_on;
152 unsigned int cfg_num;
153 const struct gc2093_mode *cur_mode;
154
155 u32 module_index;
156 const char *module_facing;
157 const char *module_name;
158 const char *len_name;
159 u32 cur_vts;
160
161 bool has_init_exp;
162 struct preisp_hdrae_exp_s init_hdrae_exp;
163 };
164
165 static const struct regmap_config gc2093_regmap_config = {
166 .reg_bits = 16,
167 .val_bits = 8,
168 .max_register = 0x04f0,
169 };
170
171 static const s64 link_freq_menu_items[] = {
172 // MIPI_FREQ_150M,
173 MIPI_FREQ_594M,
174 };
175
176 /*
177 * window size=1920*1080 mipi@2lane
178 * mclk=27M mipi_clk=594Mbps
179 * pixel_line_total=2200 line_frame_total=1125
180 * row_time=29.62us frame_rate=30fps
181 */
182 static const struct reg_sequence gc2093_1080p_liner_settings[] = {
183 /* System */
184 {0x03fe, 0x80},
185 {0x03fe, 0x80},
186 {0x03fe, 0x80},
187 {0x03fe, 0x00},
188 {0x03f2, 0x00},
189 {0x03f3, 0x00},
190 {0x03f4, 0x36},
191 {0x03f5, 0xc0},
192 {0x03f6, 0x0a},
193 {0x03f7, 0x01},
194 {0x03f8, 0x2c},
195 {0x03f9, 0x10},
196 {0x03fc, 0x8e},
197 /* Cisctl & Analog */
198 {0x0087, 0x18},
199 {0x00ee, 0x30},
200 {0x00d0, 0xb7},
201 {0x01a0, 0x00},
202 {0x01a4, 0x40},
203 {0x01a5, 0x40},
204 {0x01a6, 0x40},
205 {0x01af, 0x09},
206 {0x0001, 0x00},
207 {0x0002, 0x02},
208 {0x0003, 0x00},
209 {0x0004, 0x02},
210 {0x0005, 0x04},
211 {0x0006, 0x4c},
212 {0x0007, 0x00},
213 {0x0008, 0x11},
214 {0x0009, 0x00},
215 {0x000a, 0x02},
216 {0x000b, 0x00},
217 {0x000c, 0x04},
218 {0x000d, 0x04},
219 {0x000e, 0x40},
220 {0x000f, 0x07},
221 {0x0010, 0x8c},
222 {0x0013, 0x15},
223 {0x0019, 0x0c},
224 {0x0041, 0x04},
225 {0x0042, 0x65},
226 {0x0053, 0x60},
227 {0x008d, 0x92},
228 {0x0090, 0x00},
229 {0x00c7, 0xe1},
230 {0x001b, 0x73},
231 {0x0028, 0x0d},
232 {0x0029, 0x24},
233 {0x002b, 0x04},
234 {0x002e, 0x23},
235 {0x0037, 0x03},
236 {0x0043, 0x04},
237 {0x0044, 0x38},
238 {0x004a, 0x01},
239 {0x004b, 0x28},
240 {0x0055, 0x38},
241 {0x006b, 0x44},
242 {0x0077, 0x00},
243 {0x0078, 0x20},
244 {0x007c, 0xa1},
245 {0x00d3, 0xd4},
246 {0x00e6, 0x50},
247 /* Gain */
248 {0x00b6, 0xc0},
249 {0x00b0, 0x60},
250 /* Isp */
251 {0x0102, 0x89},
252 {0x0104, 0x01},
253 {0x010f, 0x00},
254 {0x0158, 0x00},
255 {0x0123, 0x08},
256 {0x0123, 0x00},
257 {0x0120, 0x01},
258 {0x0121, 0x00},
259 {0x0122, 0x10},
260 {0x0124, 0x03},
261 {0x0125, 0xff},
262 {0x0126, 0x3c},
263 {0x001a, 0x8c},
264 {0x00c6, 0xe0},
265 /* Blk */
266 {0x0026, 0x30},
267 {0x0142, 0x00},
268 {0x0149, 0x1e},
269 {0x014a, 0x07},
270 {0x014b, 0x80},
271 {0x0155, 0x00},
272 {0x0414, 0x78},
273 {0x0415, 0x78},
274 {0x0416, 0x78},
275 {0x0417, 0x78},
276 /* Window */
277 {0x0192, 0x02},
278 {0x0194, 0x03},
279 {0x0195, 0x04},
280 {0x0196, 0x38},
281 {0x0197, 0x07},
282 {0x0198, 0x80},
283 /* MIPI */
284 {0x019a, 0x06},
285 {0x007b, 0x2a},
286 {0x0023, 0x2d},
287 {0x0201, 0x27},
288 {0x0202, 0x56},
289 {0x0203, 0xce},
290 {0x0212, 0x80},
291 {0x0213, 0x07},
292 {0x003e, 0x91},
293 };
294
295 /*
296 * window size=1920*1080 mipi@2lane
297 * mclk=27M mipi_clk=792Mbps
298 * pixel_line_total=2640 line_frame_total=1250
299 * row_time=13.33us frame_rate=60fps
300 */
301 static const struct reg_sequence gc2093_1080p_hdr_settings[] = {
302 /* System */
303 {0x03fe, 0x80},
304 {0x03fe, 0x80},
305 {0x03fe, 0x80},
306 {0x03fe, 0x00},
307 {0x03f2, 0x00},
308 {0x03f3, 0x00},
309 {0x03f4, 0x36},
310 {0x03f5, 0xc0},
311 {0x03f6, 0x0B},
312 {0x03f7, 0x01},
313 {0x03f8, 0x58},
314 {0x03f9, 0x40},
315 {0x03fc, 0x8e},
316 /* Cisctl & Analog */
317 {0x0087, 0x18},
318 {0x00ee, 0x30},
319 {0x00d0, 0xbf},
320 {0x01a0, 0x00},
321 {0x01a4, 0x40},
322 {0x01a5, 0x40},
323 {0x01a6, 0x40},
324 {0x01af, 0x09},
325 {0x0001, 0x00},
326 {0x0002, 0x02},
327 {0x0003, 0x04},
328 {0x0004, 0x02},
329 {0x0005, 0x02},
330 {0x0006, 0x94},
331 {0x0007, 0x00},
332 {0x0008, 0x11},
333 {0x0009, 0x00},
334 {0x000a, 0x02},
335 {0x000b, 0x00},
336 {0x000c, 0x04},
337 {0x000d, 0x04},
338 {0x000e, 0x40},
339 {0x000f, 0x07},
340 {0x0010, 0x8c},
341 {0x0013, 0x15},
342 {0x0019, 0x0c},
343 {0x0041, 0x04},
344 {0x0042, 0xe2},
345 {0x0053, 0x60},
346 {0x008d, 0x92},
347 {0x0090, 0x00},
348 {0x00c7, 0xe1},
349 {0x001b, 0x73},
350 {0x0028, 0x0d},
351 {0x0029, 0x24},
352 {0x002b, 0x04},
353 {0x002e, 0x23},
354 {0x0037, 0x03},
355 {0x0043, 0x04},
356 {0x0044, 0x20},
357 {0x004a, 0x01},
358 {0x004b, 0x20},
359 {0x0055, 0x30},
360 {0x006b, 0x44},
361 {0x0077, 0x00},
362 {0x0078, 0x20},
363 {0x007c, 0xa1},
364 {0x00d3, 0xd4},
365 {0x00e6, 0x50},
366 /* Gain */
367 {0x00b6, 0xc0},
368 {0x00b0, 0x60},
369 /* Isp */
370 {0x0102, 0x89},
371 {0x0104, 0x01},
372 {0x010e, 0x01},
373 {0x0158, 0x00},
374 /* Dark sun */
375 {0x0123, 0x08},
376 {0x0123, 0x00},
377 {0x0120, 0x01},
378 {0x0121, 0x00},
379 {0x0122, 0x10},
380 {0x0124, 0x03},
381 {0x0125, 0xff},
382 {0x0126, 0x3c},
383 {0x001a, 0x8c},
384 {0x00c6, 0xe0},
385 /* Blk */
386 {0x0026, 0x30},
387 {0x0142, 0x00},
388 {0x0149, 0x1e},
389 {0x014a, 0x0f},
390 {0x014b, 0x00},
391 {0x0155, 0x00},
392 {0x0414, 0x78},
393 {0x0415, 0x78},
394 {0x0416, 0x78},
395 {0x0417, 0x78},
396 {0x0454, 0x78},
397 {0x0455, 0x78},
398 {0x0456, 0x78},
399 {0x0457, 0x78},
400 {0x04e0, 0x18},
401 /* Window */
402 {0x0192, 0x02},
403 {0x0194, 0x03},
404 {0x0195, 0x04},
405 {0x0196, 0x38},
406 {0x0197, 0x07},
407 {0x0198, 0x80},
408 /* MIPI */
409 {0x019a, 0x06},
410 {0x007b, 0x2a},
411 {0x0023, 0x2d},
412 {0x0201, 0x27},
413 {0x0202, 0x56},
414 {0x0203, 0xb6},
415 {0x0212, 0x80},
416 {0x0213, 0x07},
417 {0x0215, 0x12},
418 {0x003e, 0x91},
419 /* HDR En */
420 {0x0027, 0x71},
421 {0x0215, 0x92},
422 {0x024d, 0x01},
423 };
424
425 static const struct gc2093_mode supported_modes[] = {
426 {
427 .width = 1920,
428 .height = 1080,
429 .max_fps =
430 {
431 .numerator = 10000,
432 .denominator = 300000,
433 },
434 .exp_def = 0x460,
435 .hts_def = 0x898,
436 .vts_def = 0x465,
437 .link_freq_index = LINK_FREQ_150M_INDEX,
438 .reg_list = gc2093_1080p_liner_settings,
439 .reg_num = ARRAY_SIZE(gc2093_1080p_liner_settings),
440 .hdr_mode = NO_HDR,
441 .vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
442 },
443 {
444 .width = 1920,
445 .height = 1080,
446 .max_fps =
447 {
448 .numerator = 10000,
449 .denominator = 300000,
450 },
451 .exp_def = 0x460,
452 .hts_def = 0xa50,
453 .vts_def = 0x4e2,
454 .link_freq_index = LINK_FREQ_300M_INDEX,
455 .reg_list = gc2093_1080p_hdr_settings,
456 .reg_num = ARRAY_SIZE(gc2093_1080p_hdr_settings),
457 .hdr_mode = HDR_X2,
458 .vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_1,
459 .vc[PAD1] = V4L2_MBUS_CSI2_CHANNEL_0, // L->csi wr0
460 .vc[PAD2] = V4L2_MBUS_CSI2_CHANNEL_1,
461 .vc[PAD3] = V4L2_MBUS_CSI2_CHANNEL_1, // M->csi wr2
462 },
463 };
464
465 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
466 /* * 2, to match suitable isp freq */
to_pixel_rate(u32 index)467 static u64 to_pixel_rate(u32 index)
468 {
469 u64 pixel_rate = link_freq_menu_items[index] * 2 * GC2093_LANES * 2;
470
471 do_div(pixel_rate, PI_RATE_VALUE);
472
473 return pixel_rate;
474 }
475
gc2093_read_reg(struct gc2093 * gc2093,u16 addr,u8 * value)476 static inline int gc2093_read_reg(struct gc2093 *gc2093, u16 addr, u8 *value)
477 {
478 unsigned int val;
479 int ret;
480
481 ret = regmap_read(gc2093->regmap, addr, &val);
482 if (ret) {
483 dev_err(gc2093->dev, "i2c read failed at addr: %x\n", addr);
484 return ret;
485 }
486
487 *value = val & 0xff;
488
489 return 0;
490 }
491
gc2093_write_reg(struct gc2093 * gc2093,u16 addr,u8 value)492 static inline int gc2093_write_reg(struct gc2093 *gc2093, u16 addr, u8 value)
493 {
494 int ret;
495
496 ret = regmap_write(gc2093->regmap, addr, value);
497 if (ret) {
498 dev_err(gc2093->dev, "i2c write failed at addr: %x\n", addr);
499 return ret;
500 }
501
502 return ret;
503 }
504
505 static const struct gain_reg_config gain_reg_configs[] = {
506 {64, 0x0000, 0x0100, 0x6807, 0x00f8}, {75, 0x0010, 0x010c, 0x6807, 0x00f8},
507 {90, 0x0020, 0x011b, 0x6c08, 0x00f9}, {105, 0x0030, 0x012c, 0x6c0a, 0x00fa},
508 {122, 0x0040, 0x013f, 0x7c0b, 0x00fb}, {142, 0x0050, 0x0216, 0x7c0d, 0x00fe},
509 {167, 0x0060, 0x0235, 0x7c0e, 0x00ff}, {193, 0x0070, 0x0316, 0x7c10, 0x0801},
510 {223, 0x0080, 0x0402, 0x7c12, 0x0802}, {257, 0x0090, 0x0431, 0x7c13, 0x0803},
511 {299, 0x00a0, 0x0532, 0x7c15, 0x0805}, {346, 0x00b0, 0x0635, 0x7c17, 0x0807},
512 {397, 0x00c0, 0x0804, 0x7c18, 0x0808}, {444, 0x005a, 0x0919, 0x7c17, 0x0807},
513 {523, 0x0083, 0x0b0f, 0x7c17, 0x0807}, {607, 0x0093, 0x0d12, 0x7c19, 0x0809},
514 {700, 0x0084, 0x1000, 0x7c1b, 0x080c}, {817, 0x0094, 0x123a, 0x7c1e, 0x080f},
515 {1131, 0x005d, 0x1a02, 0x7c23, 0x0814}, {1142, 0x009b, 0x1b20, 0x7c25, 0x0816},
516 {1334, 0x008c, 0x200f, 0x7c27, 0x0818}, {1568, 0x009c, 0x2607, 0x7c2a, 0x081b},
517 {2195, 0x00b6, 0x3621, 0x7c32, 0x0823}, {2637, 0x00ad, 0x373a, 0x7c36, 0x0827},
518 {3121, 0x00bd, 0x3d02, 0x7c3a, 0x082b},
519 };
520
gc2093_set_gain(struct gc2093 * gc2093,u32 gain)521 static int gc2093_set_gain(struct gc2093 *gc2093, u32 gain)
522 {
523 int ret, i = 0;
524 u16 pre_gain = 0;
525
526 for (i = 0; i < ARRAY_SIZE(gain_reg_configs) - 1; i++) {
527 if ((gain_reg_configs[i].value <= gain) && (gain < gain_reg_configs[i + 1].value)) {
528 break;
529 }
530 }
531
532 ret = gc2093_write_reg(gc2093, 0x00b4, gain_reg_configs[i].analog_gain >> PI_REG_VALUE_EI);
533 ret |= gc2093_write_reg(gc2093, 0x00b3, gain_reg_configs[i].analog_gain & 0xff);
534 ret |= gc2093_write_reg(gc2093, 0x00b8, gain_reg_configs[i].col_gain >> PI_REG_VALUE_EI);
535 ret |= gc2093_write_reg(gc2093, 0x00b9, gain_reg_configs[i].col_gain & 0xff);
536 ret |= gc2093_write_reg(gc2093, 0x00ce, gain_reg_configs[i].analog_sw >> PI_REG_VALUE_EI);
537 ret |= gc2093_write_reg(gc2093, 0x00c2, gain_reg_configs[i].analog_sw & 0xff);
538 ret |= gc2093_write_reg(gc2093, 0x00cf, gain_reg_configs[i].ram_width >> PI_REG_VALUE_EI);
539 ret |= gc2093_write_reg(gc2093, 0x00d9, gain_reg_configs[i].ram_width & 0xff);
540
541 pre_gain = PRE_GIN_COUNT * gain / gain_reg_configs[i].value;
542
543 ret |= gc2093_write_reg(gc2093, 0x00b1, (pre_gain >> PI_REG_VALUE_SI));
544 ret |= gc2093_write_reg(gc2093, 0x00b2, ((pre_gain & 0x3f) << GC2093_LANES));
545
546 return ret;
547 }
548
gc2093_set_ctrl(struct v4l2_ctrl * ctrl)549 static int gc2093_set_ctrl(struct v4l2_ctrl *ctrl)
550 {
551 struct gc2093 *gc2093 = container_of(ctrl->handler, struct gc2093, ctrl_handler);
552 s64 max;
553 int ret = 0;
554
555 /* Propagate change of current control to all related controls */
556 switch (ctrl->id) {
557 case V4L2_CID_VBLANK:
558 /* Update max exposure while meeting expected vblanking */
559 max = gc2093->cur_mode->height + ctrl->val - PI_REG_VALUE_FO;
560 __v4l2_ctrl_modify_range(gc2093->exposure, gc2093->exposure->minimum, max, gc2093->exposure->step,
561 gc2093->exposure->default_value);
562 break;
563 default:
564 break;
565 }
566 if (!pm_runtime_get_if_in_use(gc2093->dev)) {
567 return 0;
568 }
569
570 switch (ctrl->id) {
571 case V4L2_CID_EXPOSURE:
572 dev_dbg(gc2093->dev, "set exposure value 0x%x\n", ctrl->val);
573 ret = gc2093_write_reg(gc2093, GC2093_REG_EXP_LONG_H, (ctrl->val >> PI_REG_VALUE_EI) & 0x3f);
574 ret |= gc2093_write_reg(gc2093, GC2093_REG_EXP_LONG_L, ctrl->val & 0xff);
575 break;
576 case V4L2_CID_ANALOGUE_GAIN:
577 dev_dbg(gc2093->dev, "set gain value 0x%x\n", ctrl->val);
578 gc2093_set_gain(gc2093, ctrl->val);
579 break;
580 case V4L2_CID_VBLANK:
581 /* The exposure goes up and reduces the frame rate, no need to write vb */
582 dev_dbg(gc2093->dev, " set blank value 0x%x\n", ctrl->val);
583 break;
584 case V4L2_CID_HFLIP:
585 regmap_update_bits(gc2093->regmap, GC2093_MIRROR_FLIP_REG, MIRROR_MASK, ctrl->val ? MIRROR_MASK : 0);
586 break;
587 case V4L2_CID_VFLIP:
588 regmap_update_bits(gc2093->regmap, GC2093_MIRROR_FLIP_REG, FLIP_MASK, ctrl->val ? FLIP_MASK : 0);
589 break;
590 default:
591 dev_warn(gc2093->dev, "%s Unhandled id:0x%x, val:0x%x\n", __func__, ctrl->id, ctrl->val);
592 break;
593 }
594
595 pm_runtime_put(gc2093->dev);
596 return ret;
597 }
598
599 static const struct v4l2_ctrl_ops gc2093_ctrl_ops = {
600 .s_ctrl = gc2093_set_ctrl,
601 };
602
gc2093_get_regulators(struct gc2093 * gc2093)603 static int gc2093_get_regulators(struct gc2093 *gc2093)
604 {
605 unsigned int i;
606
607 for (i = 0; i < GC2093_NUM_SUPPLIES; i++) {
608 gc2093->supplies[i].supply = gc2093_supply_names[i];
609 }
610
611 return devm_regulator_bulk_get(gc2093->dev, GC2093_NUM_SUPPLIES, gc2093->supplies);
612 }
613
gc2093_initialize_controls(struct gc2093 * gc2093)614 static int gc2093_initialize_controls(struct gc2093 *gc2093)
615 {
616 const struct gc2093_mode *mode;
617 struct v4l2_ctrl_handler *handler;
618 s64 exposure_max, vblank_def;
619 u32 h_blank;
620 int ret;
621
622 handler = &gc2093->ctrl_handler;
623 mode = gc2093->cur_mode;
624 ret = v4l2_ctrl_handler_init(handler, PI_REG_VALUE_EI);
625 if (ret) {
626 return ret;
627 }
628 handler->lock = &gc2093->lock;
629
630 gc2093->link_freq = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ, ARRAY_SIZE(link_freq_menu_items) - 1,
631 0, link_freq_menu_items);
632 if (gc2093->link_freq) {
633 gc2093->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
634 }
635
636 gc2093->pixel_rate = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 0, to_pixel_rate(LINK_FREQ_150M_INDEX),
637 1, to_pixel_rate(LINK_FREQ_150M_INDEX));
638
639 h_blank = mode->hts_def - mode->width;
640 gc2093->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK, h_blank, h_blank, 1, h_blank);
641 if (gc2093->hblank) {
642 gc2093->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
643 }
644
645 vblank_def = mode->vts_def - mode->height;
646 gc2093->vblank = v4l2_ctrl_new_std(handler, &gc2093_ctrl_ops, V4L2_CID_VBLANK, vblank_def,
647 GC2093_VTS_MAX - mode->height, 1, vblank_def);
648
649 exposure_max = mode->vts_def - PI_REG_VALUE_FO;
650 gc2093->exposure = v4l2_ctrl_new_std(handler, &gc2093_ctrl_ops, V4L2_CID_EXPOSURE, GC2093_EXPOSURE_MIN,
651 exposure_max, GC2093_EXPOSURE_STEP, mode->exp_def);
652
653 gc2093->anal_gain = v4l2_ctrl_new_std(handler, &gc2093_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, GC2093_GAIN_MIN,
654 GC2093_GAIN_MAX, GC2093_GAIN_STEP, GC2093_GAIN_DEFAULT);
655
656 gc2093->h_flip = v4l2_ctrl_new_std(handler, &gc2093_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
657
658 gc2093->v_flip = v4l2_ctrl_new_std(handler, &gc2093_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
659
660 if (handler->error) {
661 ret = handler->error;
662 dev_err(gc2093->dev, "Failed to init controls(%d)\n", ret);
663 goto err_free_handler;
664 }
665
666 gc2093->subdev.ctrl_handler = handler;
667 gc2093->has_init_exp = false;
668
669 return 0;
670
671 err_free_handler:
672 v4l2_ctrl_handler_free(handler);
673 return ret;
674 }
675
__gc2093_power_on(struct gc2093 * gc2093)676 static int __gc2093_power_on(struct gc2093 *gc2093)
677 {
678 int ret;
679 struct device *dev = gc2093->dev;
680
681 ret = clk_set_rate(gc2093->xvclk, GC2093_XVCLK_FREQ);
682 if (ret < 0) {
683 dev_warn(dev, "Failed to set xvclk rate\n");
684 }
685
686 if (clk_get_rate(gc2093->xvclk) != GC2093_XVCLK_FREQ) {
687 dev_warn(dev, "xvclk mismatched, modes are based on 27MHz\n");
688 }
689
690 ret = clk_prepare_enable(gc2093->xvclk);
691 if (ret < 0) {
692 dev_err(dev, "Failed to enable xvclk\n");
693 return ret;
694 }
695
696 ret = regulator_bulk_enable(GC2093_NUM_SUPPLIES, gc2093->supplies);
697 if (ret < 0) {
698 dev_err(dev, "Failed to enable regulators\n");
699 goto disable_clk;
700 }
701
702 if (!IS_ERR(gc2093->reset_gpio)) {
703 gpiod_set_value_cansleep(gc2093->reset_gpio, 1);
704 }
705
706 usleep_range(SLEEP_LOW_VALUE, SLEEP_HIGH_VALUE);
707
708 if (!IS_ERR(gc2093->pwdn_gpio)) {
709 gpiod_set_value_cansleep(gc2093->pwdn_gpio, 1);
710 }
711 if (!IS_ERR(gc2093->reset_gpio)) {
712 gpiod_set_value_cansleep(gc2093->reset_gpio, 0);
713 }
714
715 usleep_range(SLEEP_SLOW_VALUE, SLEEP_SHIGH_VALUE);
716
717 return 0;
718
719 disable_clk:
720 clk_disable_unprepare(gc2093->xvclk);
721 return ret;
722 }
723
__gc2093_power_off(struct gc2093 * gc2093)724 static void __gc2093_power_off(struct gc2093 *gc2093)
725 {
726 if (!IS_ERR(gc2093->reset_gpio)) {
727 gpiod_set_value_cansleep(gc2093->reset_gpio, 1);
728 }
729 if (!IS_ERR(gc2093->pwdn_gpio)) {
730 gpiod_set_value_cansleep(gc2093->pwdn_gpio, 0);
731 }
732
733 regulator_bulk_disable(GC2093_NUM_SUPPLIES, gc2093->supplies);
734 clk_disable_unprepare(gc2093->xvclk);
735 }
736
gc2093_check_sensor_id(struct gc2093 * gc2093)737 static int gc2093_check_sensor_id(struct gc2093 *gc2093)
738 {
739 u8 id_h = 0, id_l = 0;
740 u16 id = 0;
741 int ret = 0;
742
743 ret = gc2093_read_reg(gc2093, GC2093_REG_CHIP_ID_H, &id_h);
744 ret |= gc2093_read_reg(gc2093, GC2093_REG_CHIP_ID_L, &id_l);
745 if (ret) {
746 dev_err(gc2093->dev, "Failed to read sensor id, (%d)\n", ret);
747 return ret;
748 }
749
750 id = (id_h << PI_REG_VALUE_EI) | id_l;
751 if (id != GC2093_CHIP_ID) {
752 dev_err(gc2093->dev, "sensor id: %04X mismatched\n", id);
753 return -ENODEV;
754 }
755
756 dev_info(gc2093->dev, "Detected GC2093 sensor\n");
757 return 0;
758 }
759
gc2093_get_module_inf(struct gc2093 * gc2093,struct rkmodule_inf * inf)760 static void gc2093_get_module_inf(struct gc2093 *gc2093, struct rkmodule_inf *inf)
761 {
762 memset(inf, 0, sizeof(*inf));
763 strlcpy(inf->base.lens, gc2093->len_name, sizeof(inf->base.lens));
764 strlcpy(inf->base.sensor, GC2093_NAME, sizeof(inf->base.sensor));
765 strlcpy(inf->base.module, gc2093->module_name, sizeof(inf->base.module));
766 }
767
gc2093_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)768 static long gc2093_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
769 {
770 struct gc2093 *gc2093 = to_gc2093(sd);
771 struct preisp_hdrae_exp_s *hdrae_exp = arg;
772 struct rkmodule_hdr_cfg *hdr_cfg;
773 long ret = 0;
774 u32 i, h, w;
775 u32 stream = 0;
776 u8 vb_h = 0, vb_l = 0;
777 u16 vb = 0, cur_vts = 0, short_exp = 0, middle_exp = 0;
778
779 switch (cmd) {
780 case PREISP_CMD_SET_HDRAE_EXP:
781 if (!gc2093->has_init_exp && !gc2093->streaming) {
782 gc2093->init_hdrae_exp = *hdrae_exp;
783 gc2093->has_init_exp = true;
784 dev_info(gc2093->dev, "don't streaming, record hdrae\n");
785 break;
786 }
787
788 dev_dbg(gc2093->dev, "%s short_gain_reg: 0x%x\n", __func__, hdrae_exp->short_gain_reg);
789 ret = gc2093_set_gain(gc2093, hdrae_exp->short_gain_reg);
790 if (ret) {
791 dev_err(gc2093->dev, "Failed to set gain!)\n");
792 return ret;
793 }
794
795 dev_dbg(gc2093->dev, "%s exp_reg middle: 0x%x, short: 0x%x\n", __func__, hdrae_exp->middle_exp_reg,
796 hdrae_exp->short_exp_reg);
797 // Optimize blooming effect
798 if (hdrae_exp->middle_exp_reg < 0x30 || hdrae_exp->short_exp_reg < PI_REG_VALUE_FO) {
799 gc2093_write_reg(gc2093, 0x0032, 0xfd);
800 } else {
801 gc2093_write_reg(gc2093, 0x0032, 0xf8);
802 }
803
804 /* hdr exp limit
805 * 1. max short_exp_reg < VB
806 * 2. short_exp_reg + middle_exp_reg < framelength
807 */
808 /* 30FPS sample */
809
810 ret = gc2093_read_reg(gc2093, GC2093_REG_VB_H, &vb_h);
811 ret |= gc2093_read_reg(gc2093, GC2093_REG_VB_L, &vb_l);
812 if (ret) {
813 dev_err(gc2093->dev, "Failed to read vb data)\n");
814 return ret;
815 }
816 vb = (vb_h << PI_REG_VALUE_EI) | vb_l;
817
818 /* max short exposure limit to 3 ms */
819 if (hdrae_exp->short_exp_reg <= (vb - PI_REG_VALUE_EI)) {
820 short_exp = hdrae_exp->short_exp_reg;
821 } else {
822 short_exp = vb - PI_REG_VALUE_EI;
823 }
824 cur_vts = gc2093->cur_vts;
825 dev_info(gc2093->dev, "%s cur_vts: 0x%x\n", __func__, cur_vts);
826
827 if (short_exp + hdrae_exp->middle_exp_reg > cur_vts) {
828 middle_exp = cur_vts - short_exp;
829 } else {
830 middle_exp = hdrae_exp->middle_exp_reg;
831 }
832
833 dev_dbg(gc2093->dev, "%s cal exp_reg middle: 0x%x, short: 0x%x\n", __func__, middle_exp, short_exp);
834 ret |= gc2093_write_reg(gc2093, GC2093_REG_EXP_LONG_H, (middle_exp >> PI_REG_VALUE_EI) & 0x3f);
835 ret |= gc2093_write_reg(gc2093, GC2093_REG_EXP_LONG_L, middle_exp & 0xff);
836 ret |= gc2093_write_reg(gc2093, GC2093_REG_EXP_SHORT_H, (short_exp >> PI_REG_VALUE_EI) & 0x3f);
837 ret |= gc2093_write_reg(gc2093, GC2093_REG_EXP_SHORT_L, short_exp & 0xff);
838 break;
839 case RKMODULE_GET_HDR_CFG:
840 hdr_cfg = (struct rkmodule_hdr_cfg *)arg;
841 hdr_cfg->esp.mode = HDR_NORMAL_VC;
842 hdr_cfg->hdr_mode = gc2093->cur_mode->hdr_mode;
843 break;
844 case RKMODULE_SET_HDR_CFG:
845 hdr_cfg = (struct rkmodule_hdr_cfg *)arg;
846 w = gc2093->cur_mode->width;
847 h = gc2093->cur_mode->height;
848 for (i = 0; i < gc2093->cfg_num; i++) {
849 if (w == supported_modes[i].width && h == supported_modes[i].height &&
850 supported_modes[i].hdr_mode == hdr_cfg->hdr_mode) {
851 gc2093->cur_mode = &supported_modes[i];
852 break;
853 }
854 }
855 if (i == gc2093->cfg_num) {
856 dev_err(gc2093->dev, "not find hdr mode:%d %dx%d config\n", hdr_cfg->hdr_mode, w, h);
857 ret = -EINVAL;
858 } else {
859 w = gc2093->cur_mode->hts_def - gc2093->cur_mode->width;
860 h = gc2093->cur_mode->vts_def - gc2093->cur_mode->height;
861 __v4l2_ctrl_modify_range(gc2093->hblank, w, w, 1, w);
862 __v4l2_ctrl_modify_range(gc2093->vblank, h, GC2093_VTS_MAX - gc2093->cur_mode->height, 1, h);
863 gc2093->cur_vts = gc2093->cur_mode->vts_def;
864 dev_info(gc2093->dev, "sensor mode: %d\n", gc2093->cur_mode->hdr_mode);
865 }
866 break;
867 case RKMODULE_GET_MODULE_INFO:
868 gc2093_get_module_inf(gc2093, (struct rkmodule_inf *)arg);
869 break;
870 case RKMODULE_SET_QUICK_STREAM:
871
872 stream = *((u32 *)arg);
873
874 if (stream) {
875 ret = gc2093_write_reg(gc2093, GC2093_REG_CTRL_MODE, GC2093_MODE_STREAMING);
876 } else {
877 ret = gc2093_write_reg(gc2093, GC2093_REG_CTRL_MODE, GC2093_MODE_SW_STANDBY);
878 }
879 break;
880 default:
881 ret = -ENOIOCTLCMD;
882 break;
883 }
884 return ret;
885 }
886
__gc2093_start_stream(struct gc2093 * gc2093)887 static int __gc2093_start_stream(struct gc2093 *gc2093)
888 {
889 int ret;
890
891 ret = regmap_multi_reg_write(gc2093->regmap, gc2093->cur_mode->reg_list, gc2093->cur_mode->reg_num);
892 if (ret) {
893 return ret;
894 }
895
896 /* Apply customized control from user */
897 mutex_unlock(&gc2093->lock);
898 v4l2_ctrl_handler_setup(&gc2093->ctrl_handler);
899 mutex_lock(&gc2093->lock);
900
901 if (gc2093->has_init_exp && gc2093->cur_mode->hdr_mode != NO_HDR) {
902 ret = gc2093_ioctl(&gc2093->subdev, PREISP_CMD_SET_HDRAE_EXP, &gc2093->init_hdrae_exp);
903 if (ret) {
904 dev_err(gc2093->dev, "init exp fail in hdr mode\n");
905 return ret;
906 }
907 }
908
909 return gc2093_write_reg(gc2093, GC2093_REG_CTRL_MODE, GC2093_MODE_STREAMING);
910 }
911
__gc2093_stop_stream(struct gc2093 * gc2093)912 static int __gc2093_stop_stream(struct gc2093 *gc2093)
913 {
914 gc2093->has_init_exp = false;
915 return gc2093_write_reg(gc2093, GC2093_REG_CTRL_MODE, GC2093_MODE_SW_STANDBY);
916 }
917
918 #ifdef CONFIG_COMPAT
gc2093_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)919 static long gc2093_compat_ioctl32(struct v4l2_subdev *sd, unsigned int cmd, unsigned long arg)
920 {
921 void __user *up = compat_ptr(arg);
922 struct rkmodule_inf *inf;
923 struct rkmodule_hdr_cfg *hdr;
924 struct preisp_hdrae_exp_s *hdrae;
925 long ret = 0;
926 u32 stream = 0;
927
928 switch (cmd) {
929 case RKMODULE_GET_MODULE_INFO:
930 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
931 if (!inf) {
932 ret = -ENOMEM;
933 return ret;
934 }
935
936 ret = gc2093_ioctl(sd, cmd, inf);
937 if (!ret) {
938 ret = copy_to_user(up, inf, sizeof(*inf));
939 if (ret) {
940 ret = -EFAULT;
941 }
942 }
943 kfree(inf);
944 break;
945 case RKMODULE_GET_HDR_CFG:
946 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
947 if (!hdr) {
948 ret = -ENOMEM;
949 return ret;
950 }
951
952 ret = gc2093_ioctl(sd, cmd, hdr);
953 if (!ret) {
954 ret = copy_to_user(up, hdr, sizeof(*hdr));
955 if (ret) {
956 ret = -EFAULT;
957 }
958 }
959 kfree(hdr);
960 break;
961 case RKMODULE_SET_HDR_CFG:
962 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
963 if (!hdr) {
964 ret = -ENOMEM;
965 return ret;
966 }
967
968 ret = copy_from_user(hdr, up, sizeof(*hdr));
969 if (!ret) {
970 ret = gc2093_ioctl(sd, cmd, hdr);
971 } else {
972 ret = -EFAULT;
973 }
974 kfree(hdr);
975 break;
976 case PREISP_CMD_SET_HDRAE_EXP:
977 hdrae = kzalloc(sizeof(*hdrae), GFP_KERNEL);
978 if (!hdrae) {
979 ret = -ENOMEM;
980 return ret;
981 }
982
983 ret = copy_from_user(hdrae, up, sizeof(*hdrae));
984 if (!ret) {
985 ret = gc2093_ioctl(sd, cmd, hdrae);
986 } else {
987 ret = -EFAULT;
988 }
989 kfree(hdrae);
990 break;
991 case RKMODULE_SET_QUICK_STREAM:
992 ret = copy_from_user(&stream, up, sizeof(u32));
993 if (!ret) {
994 ret = gc2093_ioctl(sd, cmd, &stream);
995 } else {
996 ret = -EFAULT;
997 }
998 break;
999 default:
1000 ret = -ENOIOCTLCMD;
1001 break;
1002 }
1003 return ret;
1004 }
1005 #endif
1006
gc2093_s_stream(struct v4l2_subdev * sd,int on)1007 static int gc2093_s_stream(struct v4l2_subdev *sd, int on)
1008 {
1009 struct gc2093 *gc2093 = to_gc2093(sd);
1010 int ret = 0;
1011 unsigned int fps;
1012 unsigned int delay_us;
1013
1014 fps = DIV_ROUND_CLOSEST(gc2093->cur_mode->max_fps.denominator, gc2093->cur_mode->max_fps.numerator);
1015
1016 dev_info(gc2093->dev, "%s: on: %d, %dx%d@%d\n", __func__, on, gc2093->cur_mode->width, gc2093->cur_mode->height,
1017 fps);
1018
1019 mutex_lock(&gc2093->lock);
1020 on = !!on;
1021 if (on == gc2093->streaming) {
1022 goto unlock_and_return;
1023 }
1024
1025 if (on) {
1026 ret = pm_runtime_get_sync(gc2093->dev);
1027 if (ret < 0) {
1028 pm_runtime_put_noidle(gc2093->dev);
1029 goto unlock_and_return;
1030 }
1031
1032 ret = __gc2093_start_stream(gc2093);
1033 if (ret) {
1034 dev_err(gc2093->dev, "Failed to start gc2093 stream\n");
1035 pm_runtime_put(gc2093->dev);
1036 goto unlock_and_return;
1037 }
1038 } else {
1039 __gc2093_stop_stream(gc2093);
1040 /* delay to enable oneframe complete */
1041 delay_us = SLEEP_LOW_VALUE * SLEEP_LOW_VALUE / fps;
1042 usleep_range(delay_us, delay_us + PI_RATE_VALUE);
1043 dev_info(gc2093->dev, "%s: on: %d, sleep(%dus)\n", __func__, on, delay_us);
1044
1045 pm_runtime_put(gc2093->dev);
1046 }
1047
1048 gc2093->streaming = on;
1049
1050 unlock_and_return:
1051 mutex_unlock(&gc2093->lock);
1052 return 0;
1053 }
1054
gc2093_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1055 static int gc2093_g_frame_interval(struct v4l2_subdev *sd, struct v4l2_subdev_frame_interval *fi)
1056 {
1057 struct gc2093 *gc2093 = to_gc2093(sd);
1058 const struct gc2093_mode *mode = gc2093->cur_mode;
1059
1060 mutex_lock(&gc2093->lock);
1061 fi->interval = mode->max_fps;
1062 mutex_unlock(&gc2093->lock);
1063
1064 return 0;
1065 }
1066
gc2093_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * config)1067 int gc2093_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad, struct v4l2_mbus_config *config)
1068 {
1069 struct gc2093 *gc2093 = to_gc2093(sd);
1070 u32 val = V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0 | V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
1071 config->flags = val;
1072
1073 config->type = V4L2_MBUS_CSI2_DPHY; // V4L2_MBUS_CSI2;
1074
1075 return 0;
1076 }
1077
gc2093_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1078 static int gc2093_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1079 struct v4l2_subdev_mbus_code_enum *code)
1080 {
1081 if (code->index != 0) {
1082 return -EINVAL;
1083 }
1084 code->code = GC2093_MEDIA_BUS_FMT;
1085 return 0;
1086 }
1087
gc2093_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1088 static int gc2093_enum_frame_sizes(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1089 struct v4l2_subdev_frame_size_enum *fse)
1090 {
1091 struct gc2093 *gc2093 = to_gc2093(sd);
1092
1093 if (fse->index >= gc2093->cfg_num) {
1094 return -EINVAL;
1095 }
1096
1097 if (fse->code != GC2093_MEDIA_BUS_FMT) {
1098 return -EINVAL;
1099 }
1100
1101 fse->min_width = supported_modes[fse->index].width;
1102 fse->max_width = supported_modes[fse->index].width;
1103 fse->max_height = supported_modes[fse->index].height;
1104 fse->min_height = supported_modes[fse->index].height;
1105 return 0;
1106 }
1107
gc2093_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)1108 static int gc2093_enum_frame_interval(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1109 struct v4l2_subdev_frame_interval_enum *fie)
1110 {
1111 struct gc2093 *gc2093 = to_gc2093(sd);
1112
1113 if (fie->index >= gc2093->cfg_num) {
1114 return -EINVAL;
1115 }
1116
1117 fie->code = GC2093_MEDIA_BUS_FMT;
1118 fie->width = supported_modes[fie->index].width;
1119 fie->height = supported_modes[fie->index].height;
1120 fie->interval = supported_modes[fie->index].max_fps;
1121 fie->reserved[0] = supported_modes[fie->index].hdr_mode;
1122 return 0;
1123 }
1124
gc2093_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1125 static int gc2093_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_format *fmt)
1126 {
1127 struct gc2093 *gc2093 = to_gc2093(sd);
1128 const struct gc2093_mode *mode;
1129 s64 h_blank, vblank_def;
1130
1131 mutex_lock(&gc2093->lock);
1132
1133 mode = v4l2_find_nearest_size(supported_modes, ARRAY_SIZE(supported_modes), width, height, fmt->format.width,
1134 fmt->format.height);
1135
1136 fmt->format.code = GC2093_MEDIA_BUS_FMT;
1137 fmt->format.width = mode->width;
1138 fmt->format.height = mode->height;
1139 fmt->format.field = V4L2_FIELD_NONE;
1140 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1141 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1142 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
1143 #else
1144 mutex_unlock(&gc2093->lock);
1145 return -ENOTTY;
1146 #endif
1147 } else {
1148 gc2093->cur_mode = mode;
1149 __v4l2_ctrl_s_ctrl(gc2093->link_freq, mode->link_freq_index);
1150 __v4l2_ctrl_s_ctrl_int64(gc2093->pixel_rate, to_pixel_rate(mode->link_freq_index));
1151 h_blank = mode->hts_def - mode->width;
1152 __v4l2_ctrl_modify_range(gc2093->hblank, h_blank, h_blank, 1, h_blank);
1153 vblank_def = mode->vts_def - mode->height;
1154 __v4l2_ctrl_modify_range(gc2093->vblank, vblank_def, GC2093_VTS_MAX - mode->height, 1, vblank_def);
1155 }
1156
1157 mutex_unlock(&gc2093->lock);
1158 return 0;
1159 }
1160
gc2093_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1161 static int gc2093_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_format *fmt)
1162 {
1163 struct gc2093 *gc2093 = to_gc2093(sd);
1164 const struct gc2093_mode *mode = gc2093->cur_mode;
1165
1166 mutex_lock(&gc2093->lock);
1167 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1168 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1169 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1170 #else
1171 mutex_unlock(&gc2093->lock);
1172 return -ENOTTY;
1173 #endif
1174 } else {
1175 fmt->format.width = mode->width;
1176 fmt->format.height = mode->height;
1177 fmt->format.code = GC2093_MEDIA_BUS_FMT;
1178 fmt->format.field = V4L2_FIELD_NONE;
1179 }
1180 mutex_unlock(&gc2093->lock);
1181 return 0;
1182 }
1183
1184 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
gc2093_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1185 static int gc2093_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1186 {
1187 struct gc2093 *gc2093 = to_gc2093(sd);
1188 struct v4l2_mbus_framefmt *try_fmt = v4l2_subdev_get_try_format(sd, fh->pad, 0);
1189 const struct gc2093_mode *def_mode = &supported_modes[0];
1190
1191 mutex_lock(&gc2093->lock);
1192 /* Initialize try_fmt */
1193 try_fmt->width = def_mode->width;
1194 try_fmt->height = def_mode->height;
1195 try_fmt->code = GC2093_MEDIA_BUS_FMT;
1196 try_fmt->field = V4L2_FIELD_NONE;
1197 mutex_unlock(&gc2093->lock);
1198
1199 return 0;
1200 }
1201 #endif
1202
1203 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1204 static const struct v4l2_subdev_internal_ops gc2093_internal_ops = {
1205 .open = gc2093_open,
1206 };
1207 #endif
1208
gc2093_s_power(struct v4l2_subdev * sd,int on)1209 static int gc2093_s_power(struct v4l2_subdev *sd, int on)
1210 {
1211 struct gc2093 *gc2093 = to_gc2093(sd);
1212 int ret = 0;
1213
1214 mutex_lock(&gc2093->lock);
1215
1216 if (gc2093->power_on == !!on) {
1217 goto unlock_and_return;
1218 }
1219
1220 if (on) {
1221 ret = pm_runtime_get_sync(gc2093->dev);
1222 if (ret < 0) {
1223 pm_runtime_put_noidle(gc2093->dev);
1224 goto unlock_and_return;
1225 }
1226 gc2093->power_on = true;
1227 } else {
1228 pm_runtime_put(gc2093->dev);
1229 gc2093->power_on = false;
1230 }
1231
1232 unlock_and_return:
1233 mutex_unlock(&gc2093->lock);
1234
1235 return ret;
1236 }
1237
1238 static const struct v4l2_subdev_core_ops gc2093_core_ops = {
1239 .s_power = gc2093_s_power,
1240 .ioctl = gc2093_ioctl,
1241 #ifdef CONFIG_COMPAT
1242 .compat_ioctl32 = gc2093_compat_ioctl32,
1243 #endif
1244 };
1245
1246 static const struct v4l2_subdev_video_ops gc2093_video_ops = {
1247 .s_stream = gc2093_s_stream,
1248 .g_frame_interval = gc2093_g_frame_interval,
1249 };
1250
1251 static const struct v4l2_subdev_pad_ops gc2093_pad_ops = {
1252 .enum_mbus_code = gc2093_enum_mbus_code,
1253 .enum_frame_size = gc2093_enum_frame_sizes,
1254 .enum_frame_interval = gc2093_enum_frame_interval,
1255 .get_fmt = gc2093_get_fmt,
1256 .set_fmt = gc2093_set_fmt,
1257 .get_mbus_config = gc2093_g_mbus_config,
1258 };
1259
1260 static const struct v4l2_subdev_ops gc2093_subdev_ops = {
1261 .core = &gc2093_core_ops,
1262 .video = &gc2093_video_ops,
1263 .pad = &gc2093_pad_ops,
1264 };
1265
gc2093_runtime_resume(struct device * dev)1266 static int gc2093_runtime_resume(struct device *dev)
1267 {
1268 struct i2c_client *client = to_i2c_client(dev);
1269 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1270 struct gc2093 *gc2093 = to_gc2093(sd);
1271
1272 __gc2093_power_on(gc2093);
1273 return 0;
1274 }
1275
gc2093_runtime_suspend(struct device * dev)1276 static int gc2093_runtime_suspend(struct device *dev)
1277 {
1278 struct i2c_client *client = to_i2c_client(dev);
1279 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1280 struct gc2093 *gc2093 = to_gc2093(sd);
1281
1282 __gc2093_power_off(gc2093);
1283 return 0;
1284 }
1285
1286 static const struct dev_pm_ops gc2093_pm_ops = {
1287 SET_RUNTIME_PM_OPS(gc2093_runtime_suspend, gc2093_runtime_resume, NULL)};
1288
gc2093_probe(struct i2c_client * client,const struct i2c_device_id * id)1289 static int gc2093_probe(struct i2c_client *client, const struct i2c_device_id *id)
1290 {
1291 struct device *dev = &client->dev;
1292 struct device_node *node = dev->of_node;
1293 struct gc2093 *gc2093;
1294 struct v4l2_subdev *sd;
1295 char facing[2];
1296 int ret;
1297
1298 dev_info(dev, "driver version: %02x.%02x.%02x", DRIVER_VERSION >> PI_OFFSET_VALUE,
1299 (DRIVER_VERSION & 0xff00) >> PI_REG_VALUE_EI, DRIVER_VERSION & 0x00ff);
1300
1301 gc2093 = devm_kzalloc(dev, sizeof(*gc2093), GFP_KERNEL);
1302 if (!gc2093) {
1303 return -ENOMEM;
1304 }
1305
1306 gc2093->dev = dev;
1307 gc2093->regmap = devm_regmap_init_i2c(client, &gc2093_regmap_config);
1308 if (IS_ERR(gc2093->regmap)) {
1309 dev_err(dev, "Failed to initialize I2C\n");
1310 return -ENODEV;
1311 }
1312
1313 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX, &gc2093->module_index);
1314 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING, &gc2093->module_facing);
1315 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME, &gc2093->module_name);
1316 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME, &gc2093->len_name);
1317 if (ret) {
1318 dev_err(dev, "Failed to get module information\n");
1319 return -EINVAL;
1320 }
1321
1322 gc2093->xvclk = devm_clk_get(gc2093->dev, "xvclk");
1323 if (IS_ERR(gc2093->xvclk)) {
1324 dev_err(gc2093->dev, "Failed to get xvclk\n");
1325 return -EINVAL;
1326 }
1327
1328 gc2093->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1329 if (IS_ERR(gc2093->reset_gpio)) {
1330 dev_warn(dev, "Failed to get reset-gpios\n");
1331 }
1332
1333 gc2093->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_HIGH);
1334 if (IS_ERR(gc2093->pwdn_gpio)) {
1335 dev_warn(dev, "Failed to get pwdn-gpios\n");
1336 }
1337
1338 ret = gc2093_get_regulators(gc2093);
1339 if (ret) {
1340 dev_err(dev, "Failed to get regulators\n");
1341 return ret;
1342 }
1343
1344 mutex_init(&gc2093->lock);
1345
1346 /* set default mode */
1347 gc2093->cur_mode = &supported_modes[0];
1348 gc2093->cfg_num = ARRAY_SIZE(supported_modes);
1349 gc2093->cur_vts = gc2093->cur_mode->vts_def;
1350
1351 sd = &gc2093->subdev;
1352 v4l2_i2c_subdev_init(sd, client, &gc2093_subdev_ops);
1353 ret = gc2093_initialize_controls(gc2093);
1354 if (ret) {
1355 goto err_destroy_mutex;
1356 }
1357
1358 ret = __gc2093_power_on(gc2093);
1359 if (ret) {
1360 goto err_free_handler;
1361 }
1362
1363 ret = gc2093_check_sensor_id(gc2093);
1364 if (ret) {
1365 goto err_power_off;
1366 }
1367
1368 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1369 sd->internal_ops = &gc2093_internal_ops;
1370 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1371 #endif
1372
1373 #ifdef CONFIG_MEDIA_CONTROLLER
1374 gc2093->pad.flags = MEDIA_PAD_FL_SOURCE;
1375 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1376 ret = media_entity_pads_init(&sd->entity, 1, &gc2093->pad);
1377 if (ret < 0) {
1378 goto err_power_off;
1379 }
1380 #endif
1381
1382 memset(facing, 0, sizeof(facing));
1383 if (strcmp(gc2093->module_facing, "back") == 0) {
1384 facing[0] = 'b';
1385 } else {
1386 facing[0] = 'f';
1387 }
1388
1389 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s", gc2093->module_index, facing, GC2093_NAME,
1390 dev_name(sd->dev));
1391
1392 ret = v4l2_async_register_subdev_sensor_common(sd);
1393 if (ret) {
1394 dev_err(dev, "Failed to register v4l2 async subdev\n");
1395 goto err_clean_entity;
1396 }
1397
1398 pm_runtime_set_active(dev);
1399 pm_runtime_enable(dev);
1400 pm_runtime_idle(dev);
1401
1402 return 0;
1403
1404 err_clean_entity:
1405 #ifdef CONFIG_MEDIA_CONTROLLER
1406 media_entity_cleanup(&sd->entity);
1407 #endif
1408 err_power_off:
1409 __gc2093_power_off(gc2093);
1410 err_free_handler:
1411 v4l2_ctrl_handler_free(&gc2093->ctrl_handler);
1412 err_destroy_mutex:
1413 mutex_destroy(&gc2093->lock);
1414
1415 return ret;
1416 }
1417
gc2093_remove(struct i2c_client * client)1418 static int gc2093_remove(struct i2c_client *client)
1419 {
1420 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1421 struct gc2093 *gc2093 = to_gc2093(sd);
1422
1423 v4l2_async_unregister_subdev(sd);
1424 #ifdef CONFIG_MEDIA_CONTROLLER
1425 media_entity_cleanup(&sd->entity);
1426 #endif
1427 v4l2_ctrl_handler_free(&gc2093->ctrl_handler);
1428 mutex_destroy(&gc2093->lock);
1429
1430 pm_runtime_disable(&client->dev);
1431 if (!pm_runtime_status_suspended(&client->dev)) {
1432 __gc2093_power_off(gc2093);
1433 }
1434 pm_runtime_set_suspended(&client->dev);
1435 return 0;
1436 }
1437
1438 static const struct i2c_device_id gc2093_match_id[] = {
1439 {"gc2093", 0},
1440 {},
1441 };
1442
1443 static const struct of_device_id gc2093_of_match[] = {
1444 {.compatible = "galaxycore,gc2093"},
1445 {},
1446 };
1447 MODULE_DEVICE_TABLE(of, gc2093_of_match);
1448
1449 static struct i2c_driver gc2093_i2c_driver = {
1450 .driver =
1451 {
1452 .name = GC2093_NAME,
1453 .pm = &gc2093_pm_ops,
1454 .of_match_table = of_match_ptr(gc2093_of_match),
1455 },
1456 .probe = &gc2093_probe,
1457 .remove = &gc2093_remove,
1458 .id_table = gc2093_match_id,
1459 };
1460
sensor_mod_init(void)1461 static int __init sensor_mod_init(void)
1462 {
1463 return i2c_add_driver(&gc2093_i2c_driver);
1464 }
sensor_mod_exit(void)1465 static void __exit sensor_mod_exit(void)
1466 {
1467 i2c_del_driver(&gc2093_i2c_driver);
1468 }
1469
1470 device_initcall_sync(sensor_mod_init);
1471 module_exit(sensor_mod_exit);
1472
1473 MODULE_DESCRIPTION("Galaxycore GC2093 Image Sensor driver");
1474 MODULE_LICENSE("GPL v2");
1475