]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/media/atomisp/i2c/atomisp-gc2235.c
9a97db696f55ad86c64eb6ce11374343bc999d1f
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / media / atomisp / i2c / atomisp-gc2235.c
1 /*
2 * Support for GalaxyCore GC2235 2M camera sensor.
3 *
4 * Copyright (c) 2014 Intel Corporation. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/moduleparam.h>
30 #include <media/v4l2-device.h>
31 #include "../include/linux/atomisp_gmin_platform.h"
32 #include <linux/acpi.h>
33 #include <linux/io.h>
34
35 #include "gc2235.h"
36
37 /* i2c read/write stuff */
38 static int gc2235_read_reg(struct i2c_client *client,
39 u16 data_length, u16 reg, u16 *val)
40 {
41 int err;
42 struct i2c_msg msg[2];
43 unsigned char data[6];
44
45 if (!client->adapter) {
46 dev_err(&client->dev, "%s error, no client->adapter\n",
47 __func__);
48 return -ENODEV;
49 }
50
51 if (data_length != GC2235_8BIT) {
52 dev_err(&client->dev, "%s error, invalid data length\n",
53 __func__);
54 return -EINVAL;
55 }
56
57 memset(msg, 0, sizeof(msg));
58
59 msg[0].addr = client->addr;
60 msg[0].flags = 0;
61 msg[0].len = 1;
62 msg[0].buf = data;
63
64 /* high byte goes out first */
65 data[0] = (u8)(reg & 0xff);
66
67 msg[1].addr = client->addr;
68 msg[1].len = data_length;
69 msg[1].flags = I2C_M_RD;
70 msg[1].buf = data;
71
72 err = i2c_transfer(client->adapter, msg, 2);
73 if (err != 2) {
74 if (err >= 0)
75 err = -EIO;
76 dev_err(&client->dev,
77 "read from offset 0x%x error %d", reg, err);
78 return err;
79 }
80
81 *val = 0;
82 /* high byte comes first */
83 if (data_length == GC2235_8BIT)
84 *val = (u8)data[0];
85
86 return 0;
87 }
88
89 static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data)
90 {
91 struct i2c_msg msg;
92 const int num_msg = 1;
93 int ret;
94
95 msg.addr = client->addr;
96 msg.flags = 0;
97 msg.len = len;
98 msg.buf = data;
99 ret = i2c_transfer(client->adapter, &msg, 1);
100
101 return ret == num_msg ? 0 : -EIO;
102 }
103
104 static int gc2235_write_reg(struct i2c_client *client, u16 data_length,
105 u8 reg, u8 val)
106 {
107 int ret;
108 unsigned char data[4] = {0};
109 const u16 len = data_length + sizeof(u8); /* 16-bit address + data */
110
111 if (data_length != GC2235_8BIT) {
112 dev_err(&client->dev,
113 "%s error, invalid data_length\n", __func__);
114 return -EINVAL;
115 }
116
117 /* high byte goes out first */
118 data[0] = reg;
119 data[1] = val;
120
121 ret = gc2235_i2c_write(client, len, data);
122 if (ret)
123 dev_err(&client->dev,
124 "write error: wrote 0x%x to offset 0x%x error %d",
125 val, reg, ret);
126
127 return ret;
128 }
129
130 static int __gc2235_flush_reg_array(struct i2c_client *client,
131 struct gc2235_write_ctrl *ctrl)
132 {
133 u16 size;
134
135 if (ctrl->index == 0)
136 return 0;
137
138 size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
139 ctrl->index = 0;
140
141 return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer);
142 }
143
144 static int __gc2235_buf_reg_array(struct i2c_client *client,
145 struct gc2235_write_ctrl *ctrl,
146 const struct gc2235_reg *next)
147 {
148 int size;
149
150 if (next->type != GC2235_8BIT)
151 return -EINVAL;
152
153 size = 1;
154 ctrl->buffer.data[ctrl->index] = (u8)next->val;
155
156 /* When first item is added, we need to store its starting address */
157 if (ctrl->index == 0)
158 ctrl->buffer.addr = next->reg;
159
160 ctrl->index += size;
161
162 /*
163 * Buffer cannot guarantee free space for u32? Better flush it to avoid
164 * possible lack of memory for next item.
165 */
166 if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE)
167 return __gc2235_flush_reg_array(client, ctrl);
168
169 return 0;
170 }
171 static int __gc2235_write_reg_is_consecutive(struct i2c_client *client,
172 struct gc2235_write_ctrl *ctrl,
173 const struct gc2235_reg *next)
174 {
175 if (ctrl->index == 0)
176 return 1;
177
178 return ctrl->buffer.addr + ctrl->index == next->reg;
179 }
180 static int gc2235_write_reg_array(struct i2c_client *client,
181 const struct gc2235_reg *reglist)
182 {
183 const struct gc2235_reg *next = reglist;
184 struct gc2235_write_ctrl ctrl;
185 int err;
186
187 ctrl.index = 0;
188 for (; next->type != GC2235_TOK_TERM; next++) {
189 switch (next->type & GC2235_TOK_MASK) {
190 case GC2235_TOK_DELAY:
191 err = __gc2235_flush_reg_array(client, &ctrl);
192 if (err)
193 return err;
194 msleep(next->val);
195 break;
196 default:
197 /*
198 * If next address is not consecutive, data needs to be
199 * flushed before proceed.
200 */
201 if (!__gc2235_write_reg_is_consecutive(client, &ctrl,
202 next)) {
203 err = __gc2235_flush_reg_array(client, &ctrl);
204 if (err)
205 return err;
206 }
207 err = __gc2235_buf_reg_array(client, &ctrl, next);
208 if (err) {
209 dev_err(&client->dev, "%s: write error, aborted\n",
210 __func__);
211 return err;
212 }
213 break;
214 }
215 }
216
217 return __gc2235_flush_reg_array(client, &ctrl);
218 }
219
220 static int gc2235_g_focal(struct v4l2_subdev *sd, s32 *val)
221 {
222 *val = (GC2235_FOCAL_LENGTH_NUM << 16) | GC2235_FOCAL_LENGTH_DEM;
223 return 0;
224 }
225
226 static int gc2235_g_fnumber(struct v4l2_subdev *sd, s32 *val)
227 {
228 /*const f number for imx*/
229 *val = (GC2235_F_NUMBER_DEFAULT_NUM << 16) | GC2235_F_NUMBER_DEM;
230 return 0;
231 }
232
233 static int gc2235_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
234 {
235 *val = (GC2235_F_NUMBER_DEFAULT_NUM << 24) |
236 (GC2235_F_NUMBER_DEM << 16) |
237 (GC2235_F_NUMBER_DEFAULT_NUM << 8) | GC2235_F_NUMBER_DEM;
238 return 0;
239 }
240
241
242 static int gc2235_get_intg_factor(struct i2c_client *client,
243 struct camera_mipi_info *info,
244 const struct gc2235_resolution *res)
245 {
246 struct v4l2_subdev *sd = i2c_get_clientdata(client);
247 struct gc2235_device *dev = to_gc2235_sensor(sd);
248 struct atomisp_sensor_mode_data *buf = &info->data;
249 u16 reg_val, reg_val_h, dummy;
250 int ret;
251
252 if (!info)
253 return -EINVAL;
254
255 /* pixel clock calculattion */
256 buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz = 30000000;
257
258 /* get integration time */
259 buf->coarse_integration_time_min = GC2235_COARSE_INTG_TIME_MIN;
260 buf->coarse_integration_time_max_margin =
261 GC2235_COARSE_INTG_TIME_MAX_MARGIN;
262
263 buf->fine_integration_time_min = GC2235_FINE_INTG_TIME_MIN;
264 buf->fine_integration_time_max_margin =
265 GC2235_FINE_INTG_TIME_MAX_MARGIN;
266
267 buf->fine_integration_time_def = GC2235_FINE_INTG_TIME_MIN;
268 buf->frame_length_lines = res->lines_per_frame;
269 buf->line_length_pck = res->pixels_per_line;
270 buf->read_mode = res->bin_mode;
271
272 /* get the cropping and output resolution to ISP for this mode. */
273 ret = gc2235_read_reg(client, GC2235_8BIT,
274 GC2235_H_CROP_START_H, &reg_val_h);
275 ret = gc2235_read_reg(client, GC2235_8BIT,
276 GC2235_H_CROP_START_L, &reg_val);
277 if (ret)
278 return ret;
279
280 buf->crop_horizontal_start = (reg_val_h << 8) | reg_val;
281
282 ret = gc2235_read_reg(client, GC2235_8BIT,
283 GC2235_V_CROP_START_H, &reg_val_h);
284 ret = gc2235_read_reg(client, GC2235_8BIT,
285 GC2235_V_CROP_START_L, &reg_val);
286 if (ret)
287 return ret;
288
289 buf->crop_vertical_start = (reg_val_h << 8) | reg_val;
290
291 ret = gc2235_read_reg(client, GC2235_8BIT,
292 GC2235_H_OUTSIZE_H, &reg_val_h);
293 ret = gc2235_read_reg(client, GC2235_8BIT,
294 GC2235_H_OUTSIZE_L, &reg_val);
295 if (ret)
296 return ret;
297 buf->output_width = (reg_val_h << 8) | reg_val;
298
299 ret = gc2235_read_reg(client, GC2235_8BIT,
300 GC2235_V_OUTSIZE_H, &reg_val_h);
301 ret = gc2235_read_reg(client, GC2235_8BIT,
302 GC2235_V_OUTSIZE_L, &reg_val);
303 if (ret)
304 return ret;
305 buf->output_height = (reg_val_h << 8) | reg_val;
306
307 buf->crop_horizontal_end = buf->crop_horizontal_start +
308 buf->output_width - 1;
309 buf->crop_vertical_end = buf->crop_vertical_start +
310 buf->output_height - 1;
311
312 ret = gc2235_read_reg(client, GC2235_8BIT,
313 GC2235_HB_H, &reg_val_h);
314 ret = gc2235_read_reg(client, GC2235_8BIT,
315 GC2235_HB_L, &reg_val);
316 if (ret)
317 return ret;
318
319 dummy = (reg_val_h << 8) | reg_val;
320
321 ret = gc2235_read_reg(client, GC2235_8BIT,
322 GC2235_SH_DELAY_H, &reg_val_h);
323 ret = gc2235_read_reg(client, GC2235_8BIT,
324 GC2235_SH_DELAY_L, &reg_val);
325
326 #if 0
327 buf->line_length_pck = buf->output_width + 16 + dummy +
328 (((u16)reg_val_h << 8) | (u16)reg_val) + 4;
329 #endif
330 ret = gc2235_read_reg(client, GC2235_8BIT,
331 GC2235_VB_H, &reg_val_h);
332 ret = gc2235_read_reg(client, GC2235_8BIT,
333 GC2235_VB_L, &reg_val);
334 if (ret)
335 return ret;
336
337 #if 0
338 buf->frame_length_lines = buf->output_height + 32 +
339 (((u16)reg_val_h << 8) | (u16)reg_val);
340 #endif
341 buf->binning_factor_x = res->bin_factor_x ?
342 res->bin_factor_x : 1;
343 buf->binning_factor_y = res->bin_factor_y ?
344 res->bin_factor_y : 1;
345 return 0;
346 }
347
348 static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
349 int gain, int digitgain)
350
351 {
352 struct i2c_client *client = v4l2_get_subdevdata(sd);
353 u16 coarse_integration = (u16)coarse_itg;
354 int ret = 0;
355 u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0;
356 expo_coarse_h = coarse_integration >> 8;
357 expo_coarse_l = coarse_integration & 0xff;
358
359 ret = gc2235_write_reg(client, GC2235_8BIT,
360 GC2235_EXPOSURE_H, expo_coarse_h);
361 ret = gc2235_write_reg(client, GC2235_8BIT,
362 GC2235_EXPOSURE_L, expo_coarse_l);
363
364 if (gain <= 0x58) {
365 gain_val = 0x40;
366 gain_val2 = 0x58;
367 } else if (gain < 256) {
368 gain_val = 0x40;
369 gain_val2 = gain;
370 } else {
371 gain_val2 = 64 * gain / 256;
372 gain_val = 0xff;
373 }
374
375 ret = gc2235_write_reg(client, GC2235_8BIT,
376 GC2235_GLOBAL_GAIN, (u8)gain_val);
377 ret = gc2235_write_reg(client, GC2235_8BIT,
378 GC2235_PRE_GAIN, (u8)gain_val2);
379
380 return ret;
381 }
382
383
384 static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure,
385 int gain, int digitgain)
386 {
387 struct gc2235_device *dev = to_gc2235_sensor(sd);
388 int ret;
389
390 mutex_lock(&dev->input_lock);
391 ret = __gc2235_set_exposure(sd, exposure, gain, digitgain);
392 mutex_unlock(&dev->input_lock);
393
394 return ret;
395 }
396
397 static long gc2235_s_exposure(struct v4l2_subdev *sd,
398 struct atomisp_exposure *exposure)
399 {
400 int exp = exposure->integration_time[0];
401 int gain = exposure->gain[0];
402 int digitgain = exposure->gain[1];
403
404 /* we should not accept the invalid value below. */
405 if (gain == 0) {
406 struct i2c_client *client = v4l2_get_subdevdata(sd);
407 v4l2_err(client, "%s: invalid value\n", __func__);
408 return -EINVAL;
409 }
410
411 return gc2235_set_exposure(sd, exp, gain, digitgain);
412 }
413 static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
414 {
415 switch (cmd) {
416 case ATOMISP_IOC_S_EXPOSURE:
417 return gc2235_s_exposure(sd, arg);
418 default:
419 return -EINVAL;
420 }
421 return 0;
422 }
423 /* This returns the exposure time being used. This should only be used
424 * for filling in EXIF data, not for actual image processing.
425 */
426 static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value)
427 {
428 struct i2c_client *client = v4l2_get_subdevdata(sd);
429 u16 reg_v, reg_v2;
430 int ret;
431
432 /* get exposure */
433 ret = gc2235_read_reg(client, GC2235_8BIT,
434 GC2235_EXPOSURE_L,
435 &reg_v);
436 if (ret)
437 goto err;
438
439 ret = gc2235_read_reg(client, GC2235_8BIT,
440 GC2235_EXPOSURE_H,
441 &reg_v2);
442 if (ret)
443 goto err;
444
445 reg_v += reg_v2 << 8;
446
447 *value = reg_v;
448 err:
449 return ret;
450 }
451
452 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
453 {
454 struct gc2235_device *dev =
455 container_of(ctrl->handler, struct gc2235_device, ctrl_handler);
456 int ret = 0;
457
458 switch (ctrl->id) {
459 case V4L2_CID_EXPOSURE_ABSOLUTE:
460 ret = gc2235_q_exposure(&dev->sd, &ctrl->val);
461 break;
462 case V4L2_CID_FOCAL_ABSOLUTE:
463 ret = gc2235_g_focal(&dev->sd, &ctrl->val);
464 break;
465 case V4L2_CID_FNUMBER_ABSOLUTE:
466 ret = gc2235_g_fnumber(&dev->sd, &ctrl->val);
467 break;
468 case V4L2_CID_FNUMBER_RANGE:
469 ret = gc2235_g_fnumber_range(&dev->sd, &ctrl->val);
470 break;
471 default:
472 ret = -EINVAL;
473 }
474
475 return ret;
476 }
477
478 static const struct v4l2_ctrl_ops ctrl_ops = {
479 .g_volatile_ctrl = gc2235_g_volatile_ctrl
480 };
481
482 static struct v4l2_ctrl_config gc2235_controls[] = {
483 {
484 .ops = &ctrl_ops,
485 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
486 .type = V4L2_CTRL_TYPE_INTEGER,
487 .name = "exposure",
488 .min = 0x0,
489 .max = 0xffff,
490 .step = 0x01,
491 .def = 0x00,
492 .flags = 0,
493 },
494 {
495 .ops = &ctrl_ops,
496 .id = V4L2_CID_FOCAL_ABSOLUTE,
497 .type = V4L2_CTRL_TYPE_INTEGER,
498 .name = "focal length",
499 .min = GC2235_FOCAL_LENGTH_DEFAULT,
500 .max = GC2235_FOCAL_LENGTH_DEFAULT,
501 .step = 0x01,
502 .def = GC2235_FOCAL_LENGTH_DEFAULT,
503 .flags = 0,
504 },
505 {
506 .ops = &ctrl_ops,
507 .id = V4L2_CID_FNUMBER_ABSOLUTE,
508 .type = V4L2_CTRL_TYPE_INTEGER,
509 .name = "f-number",
510 .min = GC2235_F_NUMBER_DEFAULT,
511 .max = GC2235_F_NUMBER_DEFAULT,
512 .step = 0x01,
513 .def = GC2235_F_NUMBER_DEFAULT,
514 .flags = 0,
515 },
516 {
517 .ops = &ctrl_ops,
518 .id = V4L2_CID_FNUMBER_RANGE,
519 .type = V4L2_CTRL_TYPE_INTEGER,
520 .name = "f-number range",
521 .min = GC2235_F_NUMBER_RANGE,
522 .max = GC2235_F_NUMBER_RANGE,
523 .step = 0x01,
524 .def = GC2235_F_NUMBER_RANGE,
525 .flags = 0,
526 },
527 };
528
529 static int __gc2235_init(struct v4l2_subdev *sd)
530 {
531 struct i2c_client *client = v4l2_get_subdevdata(sd);
532
533 /* restore settings */
534 gc2235_res = gc2235_res_preview;
535 N_RES = N_RES_PREVIEW;
536
537 return gc2235_write_reg_array(client, gc2235_init_settings);
538 }
539
540 static int is_init;
541
542 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
543 {
544 int ret = -1;
545 struct gc2235_device *dev = to_gc2235_sensor(sd);
546
547 if (!dev || !dev->platform_data)
548 return -ENODEV;
549
550 /* Non-gmin platforms use the legacy callback */
551 if (dev->platform_data->power_ctrl)
552 return dev->platform_data->power_ctrl(sd, flag);
553
554 if (flag) {
555 ret = dev->platform_data->v1p8_ctrl(sd, 1);
556 usleep_range(60, 90);
557 if (ret == 0)
558 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
559 } else {
560 ret = dev->platform_data->v1p8_ctrl(sd, 0);
561 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
562 }
563 return ret;
564 }
565
566 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
567 {
568 struct gc2235_device *dev = to_gc2235_sensor(sd);
569 int ret = -1;
570
571 if (!dev || !dev->platform_data)
572 return -ENODEV;
573
574 ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
575 usleep_range(60, 90);
576 return dev->platform_data->gpio0_ctrl(sd, flag);
577 }
578
579 static int power_up(struct v4l2_subdev *sd)
580 {
581 struct gc2235_device *dev = to_gc2235_sensor(sd);
582 struct i2c_client *client = v4l2_get_subdevdata(sd);
583 int ret;
584
585 if (!dev->platform_data) {
586 dev_err(&client->dev,
587 "no camera_sensor_platform_data");
588 return -ENODEV;
589 }
590 /* power control */
591 ret = power_ctrl(sd, 1);
592 if (ret)
593 goto fail_power;
594
595 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
596 usleep_range(5000, 6000);
597
598 ret = dev->platform_data->flisclk_ctrl(sd, 1);
599 if (ret)
600 goto fail_clk;
601 usleep_range(5000, 6000);
602
603 /* gpio ctrl */
604 ret = gpio_ctrl(sd, 1);
605 if (ret) {
606 ret = gpio_ctrl(sd, 1);
607 if (ret)
608 goto fail_power;
609 }
610
611 msleep(5);
612 return 0;
613
614 fail_clk:
615 gpio_ctrl(sd, 0);
616 fail_power:
617 power_ctrl(sd, 0);
618 dev_err(&client->dev, "sensor power-up failed\n");
619
620 return ret;
621 }
622
623 static int power_down(struct v4l2_subdev *sd)
624 {
625 struct gc2235_device *dev = to_gc2235_sensor(sd);
626 struct i2c_client *client = v4l2_get_subdevdata(sd);
627 int ret = 0;
628
629 if (!dev->platform_data) {
630 dev_err(&client->dev,
631 "no camera_sensor_platform_data");
632 return -ENODEV;
633 }
634 /* gpio ctrl */
635 ret = gpio_ctrl(sd, 0);
636 if (ret) {
637 ret = gpio_ctrl(sd, 0);
638 if (ret)
639 dev_err(&client->dev, "gpio failed 2\n");
640 }
641
642 ret = dev->platform_data->flisclk_ctrl(sd, 0);
643 if (ret)
644 dev_err(&client->dev, "flisclk failed\n");
645
646 /* power control */
647 ret = power_ctrl(sd, 0);
648 if (ret)
649 dev_err(&client->dev, "vprog failed.\n");
650
651 return ret;
652 }
653
654 static int gc2235_s_power(struct v4l2_subdev *sd, int on)
655 {
656 int ret;
657
658 if (on == 0)
659 ret = power_down(sd);
660 else {
661 ret = power_up(sd);
662 if (!ret)
663 ret = __gc2235_init(sd);
664 is_init = 1;
665 }
666 return ret;
667 }
668
669 /*
670 * distance - calculate the distance
671 * @res: resolution
672 * @w: width
673 * @h: height
674 *
675 * Get the gap between resolution and w/h.
676 * res->width/height smaller than w/h wouldn't be considered.
677 * Returns the value of gap or -1 if fail.
678 */
679 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
680 static int distance(struct gc2235_resolution *res, u32 w, u32 h)
681 {
682 unsigned int w_ratio = (res->width << 13) / w;
683 unsigned int h_ratio;
684 int match;
685
686 if (h == 0)
687 return -1;
688 h_ratio = (res->height << 13) / h;
689 if (h_ratio == 0)
690 return -1;
691 match = abs(((w_ratio << 13) / h_ratio) - 8192);
692
693 if ((w_ratio < 8192) || (h_ratio < 8192) ||
694 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
695 return -1;
696
697 return w_ratio + h_ratio;
698 }
699
700 /* Return the nearest higher resolution index */
701 static int nearest_resolution_index(int w, int h)
702 {
703 int i;
704 int idx = -1;
705 int dist;
706 int min_dist = INT_MAX;
707 struct gc2235_resolution *tmp_res = NULL;
708
709 for (i = 0; i < N_RES; i++) {
710 tmp_res = &gc2235_res[i];
711 dist = distance(tmp_res, w, h);
712 if (dist == -1)
713 continue;
714 if (dist < min_dist) {
715 min_dist = dist;
716 idx = i;
717 }
718 }
719
720 return idx;
721 }
722
723 static int get_resolution_index(int w, int h)
724 {
725 int i;
726
727 for (i = 0; i < N_RES; i++) {
728 if (w != gc2235_res[i].width)
729 continue;
730 if (h != gc2235_res[i].height)
731 continue;
732
733 return i;
734 }
735
736 return -1;
737 }
738
739 static int startup(struct v4l2_subdev *sd)
740 {
741 struct gc2235_device *dev = to_gc2235_sensor(sd);
742 struct i2c_client *client = v4l2_get_subdevdata(sd);
743 int ret = 0;
744 if (is_init == 0) {
745 /* force gc2235 to do a reset in res change, otherwise it
746 * can not output normal after switching res. and it is not
747 * necessary for first time run up after power on, for the sack
748 * of performance
749 */
750 power_down(sd);
751 power_up(sd);
752 gc2235_write_reg_array(client, gc2235_init_settings);
753 }
754
755 ret = gc2235_write_reg_array(client, gc2235_res[dev->fmt_idx].regs);
756 if (ret) {
757 dev_err(&client->dev, "gc2235 write register err.\n");
758 return ret;
759 }
760 is_init = 0;
761
762 return ret;
763 }
764
765 static int gc2235_set_fmt(struct v4l2_subdev *sd,
766 struct v4l2_subdev_pad_config *cfg,
767 struct v4l2_subdev_format *format)
768 {
769
770 struct v4l2_mbus_framefmt *fmt = &format->format;
771 struct gc2235_device *dev = to_gc2235_sensor(sd);
772 struct i2c_client *client = v4l2_get_subdevdata(sd);
773 struct camera_mipi_info *gc2235_info = NULL;
774 int ret = 0;
775 int idx;
776
777 gc2235_info = v4l2_get_subdev_hostdata(sd);
778 if (!gc2235_info)
779 return -EINVAL;
780 if (format->pad)
781 return -EINVAL;
782 if (!fmt)
783 return -EINVAL;
784 mutex_lock(&dev->input_lock);
785 idx = nearest_resolution_index(fmt->width, fmt->height);
786 if (idx == -1) {
787 /* return the largest resolution */
788 fmt->width = gc2235_res[N_RES - 1].width;
789 fmt->height = gc2235_res[N_RES - 1].height;
790 } else {
791 fmt->width = gc2235_res[idx].width;
792 fmt->height = gc2235_res[idx].height;
793 }
794 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
795 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
796 cfg->try_fmt = *fmt;
797 mutex_unlock(&dev->input_lock);
798 return 0;
799 }
800
801 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
802 if (dev->fmt_idx == -1) {
803 dev_err(&client->dev, "get resolution fail\n");
804 mutex_unlock(&dev->input_lock);
805 return -EINVAL;
806 }
807
808 ret = startup(sd);
809 if (ret) {
810 dev_err(&client->dev, "gc2235 startup err\n");
811 goto err;
812 }
813
814 ret = gc2235_get_intg_factor(client, gc2235_info,
815 &gc2235_res[dev->fmt_idx]);
816 if (ret)
817 dev_err(&client->dev, "failed to get integration_factor\n");
818
819 err:
820 mutex_unlock(&dev->input_lock);
821 return ret;
822 }
823
824 static int gc2235_get_fmt(struct v4l2_subdev *sd,
825 struct v4l2_subdev_pad_config *cfg,
826 struct v4l2_subdev_format *format)
827 {
828 struct v4l2_mbus_framefmt *fmt = &format->format;
829 struct gc2235_device *dev = to_gc2235_sensor(sd);
830
831 if (format->pad)
832 return -EINVAL;
833
834 if (!fmt)
835 return -EINVAL;
836
837 fmt->width = gc2235_res[dev->fmt_idx].width;
838 fmt->height = gc2235_res[dev->fmt_idx].height;
839 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
840
841 return 0;
842 }
843
844 static int gc2235_detect(struct i2c_client *client)
845 {
846 struct i2c_adapter *adapter = client->adapter;
847 u16 high, low;
848 int ret;
849 u16 id;
850
851 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
852 return -ENODEV;
853
854 ret = gc2235_read_reg(client, GC2235_8BIT,
855 GC2235_SENSOR_ID_H, &high);
856 if (ret) {
857 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
858 return -ENODEV;
859 }
860 ret = gc2235_read_reg(client, GC2235_8BIT,
861 GC2235_SENSOR_ID_L, &low);
862 id = ((high << 8) | low);
863
864 if (id != GC2235_ID) {
865 dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
866 return -ENODEV;
867 }
868
869 dev_info(&client->dev, "detect gc2235 success\n");
870 return 0;
871 }
872
873 static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
874 {
875 struct gc2235_device *dev = to_gc2235_sensor(sd);
876 struct i2c_client *client = v4l2_get_subdevdata(sd);
877 int ret;
878 mutex_lock(&dev->input_lock);
879
880 if (enable)
881 ret = gc2235_write_reg_array(client, gc2235_stream_on);
882 else
883 ret = gc2235_write_reg_array(client, gc2235_stream_off);
884
885 mutex_unlock(&dev->input_lock);
886 return ret;
887 }
888
889
890 static int gc2235_s_config(struct v4l2_subdev *sd,
891 int irq, void *platform_data)
892 {
893 struct gc2235_device *dev = to_gc2235_sensor(sd);
894 struct i2c_client *client = v4l2_get_subdevdata(sd);
895 int ret = 0;
896
897 if (!platform_data)
898 return -ENODEV;
899
900 dev->platform_data =
901 (struct camera_sensor_platform_data *)platform_data;
902
903 mutex_lock(&dev->input_lock);
904 if (dev->platform_data->platform_init) {
905 ret = dev->platform_data->platform_init(client);
906 if (ret) {
907 dev_err(&client->dev, "platform init err\n");
908 goto platform_init_failed;
909 }
910 }
911 /* power off the module, then power on it in future
912 * as first power on by board may not fulfill the
913 * power on sequqence needed by the module
914 */
915 ret = power_down(sd);
916 if (ret) {
917 dev_err(&client->dev, "gc2235 power-off err.\n");
918 goto fail_power_off;
919 }
920
921 ret = power_up(sd);
922 if (ret) {
923 dev_err(&client->dev, "gc2235 power-up err.\n");
924 goto fail_power_on;
925 }
926
927 ret = dev->platform_data->csi_cfg(sd, 1);
928 if (ret)
929 goto fail_csi_cfg;
930
931 /* config & detect sensor */
932 ret = gc2235_detect(client);
933 if (ret) {
934 dev_err(&client->dev, "gc2235_detect err s_config.\n");
935 goto fail_csi_cfg;
936 }
937
938 /* turn off sensor, after probed */
939 ret = power_down(sd);
940 if (ret) {
941 dev_err(&client->dev, "gc2235 power-off err.\n");
942 goto fail_csi_cfg;
943 }
944 mutex_unlock(&dev->input_lock);
945
946 return 0;
947
948 fail_csi_cfg:
949 dev->platform_data->csi_cfg(sd, 0);
950 fail_power_on:
951 power_down(sd);
952 dev_err(&client->dev, "sensor power-gating failed\n");
953 fail_power_off:
954 if (dev->platform_data->platform_deinit)
955 dev->platform_data->platform_deinit();
956 platform_init_failed:
957 mutex_unlock(&dev->input_lock);
958 return ret;
959 }
960
961 static int gc2235_g_parm(struct v4l2_subdev *sd,
962 struct v4l2_streamparm *param)
963 {
964 struct gc2235_device *dev = to_gc2235_sensor(sd);
965 struct i2c_client *client = v4l2_get_subdevdata(sd);
966
967 if (!param)
968 return -EINVAL;
969
970 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
971 dev_err(&client->dev, "unsupported buffer type.\n");
972 return -EINVAL;
973 }
974
975 memset(param, 0, sizeof(*param));
976 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
977
978 if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
979 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
980 param->parm.capture.timeperframe.numerator = 1;
981 param->parm.capture.capturemode = dev->run_mode;
982 param->parm.capture.timeperframe.denominator =
983 gc2235_res[dev->fmt_idx].fps;
984 }
985 return 0;
986 }
987
988 static int gc2235_s_parm(struct v4l2_subdev *sd,
989 struct v4l2_streamparm *param)
990 {
991 struct gc2235_device *dev = to_gc2235_sensor(sd);
992 dev->run_mode = param->parm.capture.capturemode;
993
994 mutex_lock(&dev->input_lock);
995 switch (dev->run_mode) {
996 case CI_MODE_VIDEO:
997 gc2235_res = gc2235_res_video;
998 N_RES = N_RES_VIDEO;
999 break;
1000 case CI_MODE_STILL_CAPTURE:
1001 gc2235_res = gc2235_res_still;
1002 N_RES = N_RES_STILL;
1003 break;
1004 default:
1005 gc2235_res = gc2235_res_preview;
1006 N_RES = N_RES_PREVIEW;
1007 }
1008 mutex_unlock(&dev->input_lock);
1009 return 0;
1010 }
1011
1012 static int gc2235_g_frame_interval(struct v4l2_subdev *sd,
1013 struct v4l2_subdev_frame_interval *interval)
1014 {
1015 struct gc2235_device *dev = to_gc2235_sensor(sd);
1016
1017 interval->interval.numerator = 1;
1018 interval->interval.denominator = gc2235_res[dev->fmt_idx].fps;
1019
1020 return 0;
1021 }
1022
1023 static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
1024 struct v4l2_subdev_pad_config *cfg,
1025 struct v4l2_subdev_mbus_code_enum *code)
1026 {
1027 if (code->index >= MAX_FMTS)
1028 return -EINVAL;
1029
1030 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1031 return 0;
1032 }
1033
1034 static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
1035 struct v4l2_subdev_pad_config *cfg,
1036 struct v4l2_subdev_frame_size_enum *fse)
1037 {
1038 int index = fse->index;
1039
1040 if (index >= N_RES)
1041 return -EINVAL;
1042
1043 fse->min_width = gc2235_res[index].width;
1044 fse->min_height = gc2235_res[index].height;
1045 fse->max_width = gc2235_res[index].width;
1046 fse->max_height = gc2235_res[index].height;
1047
1048 return 0;
1049
1050 }
1051
1052 static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1053 {
1054 struct gc2235_device *dev = to_gc2235_sensor(sd);
1055
1056 mutex_lock(&dev->input_lock);
1057 *frames = gc2235_res[dev->fmt_idx].skip_frames;
1058 mutex_unlock(&dev->input_lock);
1059
1060 return 0;
1061 }
1062
1063 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
1064 .g_skip_frames = gc2235_g_skip_frames,
1065 };
1066
1067 static const struct v4l2_subdev_video_ops gc2235_video_ops = {
1068 .s_stream = gc2235_s_stream,
1069 .g_parm = gc2235_g_parm,
1070 .s_parm = gc2235_s_parm,
1071 .g_frame_interval = gc2235_g_frame_interval,
1072 };
1073
1074 static const struct v4l2_subdev_core_ops gc2235_core_ops = {
1075 .s_power = gc2235_s_power,
1076 .ioctl = gc2235_ioctl,
1077 };
1078
1079 static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
1080 .enum_mbus_code = gc2235_enum_mbus_code,
1081 .enum_frame_size = gc2235_enum_frame_size,
1082 .get_fmt = gc2235_get_fmt,
1083 .set_fmt = gc2235_set_fmt,
1084 };
1085
1086 static const struct v4l2_subdev_ops gc2235_ops = {
1087 .core = &gc2235_core_ops,
1088 .video = &gc2235_video_ops,
1089 .pad = &gc2235_pad_ops,
1090 .sensor = &gc2235_sensor_ops,
1091 };
1092
1093 static int gc2235_remove(struct i2c_client *client)
1094 {
1095 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1096 struct gc2235_device *dev = to_gc2235_sensor(sd);
1097 dev_dbg(&client->dev, "gc2235_remove...\n");
1098
1099 if (dev->platform_data->platform_deinit)
1100 dev->platform_data->platform_deinit();
1101
1102 dev->platform_data->csi_cfg(sd, 0);
1103
1104 v4l2_device_unregister_subdev(sd);
1105 media_entity_cleanup(&dev->sd.entity);
1106 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1107 kfree(dev);
1108
1109 return 0;
1110 }
1111
1112 static int gc2235_probe(struct i2c_client *client)
1113 {
1114 struct gc2235_device *dev;
1115 void *gcpdev;
1116 int ret;
1117 unsigned int i;
1118
1119 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1120 if (!dev)
1121 return -ENOMEM;
1122
1123 mutex_init(&dev->input_lock);
1124
1125 dev->fmt_idx = 0;
1126 v4l2_i2c_subdev_init(&(dev->sd), client, &gc2235_ops);
1127
1128 gcpdev = client->dev.platform_data;
1129 if (ACPI_COMPANION(&client->dev))
1130 gcpdev = gmin_camera_platform_data(&dev->sd,
1131 ATOMISP_INPUT_FORMAT_RAW_10,
1132 atomisp_bayer_order_grbg);
1133
1134 ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
1135 if (ret)
1136 goto out_free;
1137
1138 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1139 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1140 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1141 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1142 ret =
1143 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1144 ARRAY_SIZE(gc2235_controls));
1145 if (ret) {
1146 gc2235_remove(client);
1147 return ret;
1148 }
1149
1150 for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
1151 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
1152 NULL);
1153
1154 if (dev->ctrl_handler.error) {
1155 gc2235_remove(client);
1156 return dev->ctrl_handler.error;
1157 }
1158
1159 /* Use same lock for controls as for everything else. */
1160 dev->ctrl_handler.lock = &dev->input_lock;
1161 dev->sd.ctrl_handler = &dev->ctrl_handler;
1162
1163 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1164 if (ret)
1165 gc2235_remove(client);
1166
1167 if (ACPI_HANDLE(&client->dev))
1168 ret = atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA);
1169
1170 return ret;
1171 out_free:
1172 v4l2_device_unregister_subdev(&dev->sd);
1173 kfree(dev);
1174
1175 return ret;
1176 }
1177
1178 static const struct acpi_device_id gc2235_acpi_match[] = {
1179 { "INT33F8" },
1180 {},
1181 };
1182 MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
1183
1184 static struct i2c_driver gc2235_driver = {
1185 .driver = {
1186 .name = "gc2235",
1187 .acpi_match_table = gc2235_acpi_match,
1188 },
1189 .probe_new = gc2235_probe,
1190 .remove = gc2235_remove,
1191 };
1192 module_i2c_driver(gc2235_driver);
1193
1194 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1195 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1196 MODULE_LICENSE("GPL");