]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/media/atomisp/i2c/atomisp-gc2235.c
media: staging: atomisp: Remove unneeded gpio.h inclusion
[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 /* Non-gmin platforms use the legacy callback */
575 if (dev->platform_data->gpio_ctrl)
576 return dev->platform_data->gpio_ctrl(sd, flag);
577
578 ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
579 usleep_range(60, 90);
580 return dev->platform_data->gpio0_ctrl(sd, flag);
581 }
582
583 static int power_up(struct v4l2_subdev *sd)
584 {
585 struct gc2235_device *dev = to_gc2235_sensor(sd);
586 struct i2c_client *client = v4l2_get_subdevdata(sd);
587 int ret;
588
589 if (!dev->platform_data) {
590 dev_err(&client->dev,
591 "no camera_sensor_platform_data");
592 return -ENODEV;
593 }
594 /* power control */
595 ret = power_ctrl(sd, 1);
596 if (ret)
597 goto fail_power;
598
599 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
600 usleep_range(5000, 6000);
601
602 ret = dev->platform_data->flisclk_ctrl(sd, 1);
603 if (ret)
604 goto fail_clk;
605 usleep_range(5000, 6000);
606
607 /* gpio ctrl */
608 ret = gpio_ctrl(sd, 1);
609 if (ret) {
610 ret = gpio_ctrl(sd, 1);
611 if (ret)
612 goto fail_power;
613 }
614
615 msleep(5);
616 return 0;
617
618 fail_clk:
619 gpio_ctrl(sd, 0);
620 fail_power:
621 power_ctrl(sd, 0);
622 dev_err(&client->dev, "sensor power-up failed\n");
623
624 return ret;
625 }
626
627 static int power_down(struct v4l2_subdev *sd)
628 {
629 struct gc2235_device *dev = to_gc2235_sensor(sd);
630 struct i2c_client *client = v4l2_get_subdevdata(sd);
631 int ret = 0;
632
633 if (!dev->platform_data) {
634 dev_err(&client->dev,
635 "no camera_sensor_platform_data");
636 return -ENODEV;
637 }
638 /* gpio ctrl */
639 ret = gpio_ctrl(sd, 0);
640 if (ret) {
641 ret = gpio_ctrl(sd, 0);
642 if (ret)
643 dev_err(&client->dev, "gpio failed 2\n");
644 }
645
646 ret = dev->platform_data->flisclk_ctrl(sd, 0);
647 if (ret)
648 dev_err(&client->dev, "flisclk failed\n");
649
650 /* power control */
651 ret = power_ctrl(sd, 0);
652 if (ret)
653 dev_err(&client->dev, "vprog failed.\n");
654
655 return ret;
656 }
657
658 static int gc2235_s_power(struct v4l2_subdev *sd, int on)
659 {
660 int ret;
661
662 if (on == 0)
663 ret = power_down(sd);
664 else {
665 ret = power_up(sd);
666 if (!ret)
667 ret = __gc2235_init(sd);
668 is_init = 1;
669 }
670 return ret;
671 }
672
673 /*
674 * distance - calculate the distance
675 * @res: resolution
676 * @w: width
677 * @h: height
678 *
679 * Get the gap between resolution and w/h.
680 * res->width/height smaller than w/h wouldn't be considered.
681 * Returns the value of gap or -1 if fail.
682 */
683 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
684 static int distance(struct gc2235_resolution *res, u32 w, u32 h)
685 {
686 unsigned int w_ratio = (res->width << 13) / w;
687 unsigned int h_ratio;
688 int match;
689
690 if (h == 0)
691 return -1;
692 h_ratio = (res->height << 13) / h;
693 if (h_ratio == 0)
694 return -1;
695 match = abs(((w_ratio << 13) / h_ratio) - 8192);
696
697 if ((w_ratio < 8192) || (h_ratio < 8192) ||
698 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
699 return -1;
700
701 return w_ratio + h_ratio;
702 }
703
704 /* Return the nearest higher resolution index */
705 static int nearest_resolution_index(int w, int h)
706 {
707 int i;
708 int idx = -1;
709 int dist;
710 int min_dist = INT_MAX;
711 struct gc2235_resolution *tmp_res = NULL;
712
713 for (i = 0; i < N_RES; i++) {
714 tmp_res = &gc2235_res[i];
715 dist = distance(tmp_res, w, h);
716 if (dist == -1)
717 continue;
718 if (dist < min_dist) {
719 min_dist = dist;
720 idx = i;
721 }
722 }
723
724 return idx;
725 }
726
727 static int get_resolution_index(int w, int h)
728 {
729 int i;
730
731 for (i = 0; i < N_RES; i++) {
732 if (w != gc2235_res[i].width)
733 continue;
734 if (h != gc2235_res[i].height)
735 continue;
736
737 return i;
738 }
739
740 return -1;
741 }
742
743 static int startup(struct v4l2_subdev *sd)
744 {
745 struct gc2235_device *dev = to_gc2235_sensor(sd);
746 struct i2c_client *client = v4l2_get_subdevdata(sd);
747 int ret = 0;
748 if (is_init == 0) {
749 /* force gc2235 to do a reset in res change, otherwise it
750 * can not output normal after switching res. and it is not
751 * necessary for first time run up after power on, for the sack
752 * of performance
753 */
754 power_down(sd);
755 power_up(sd);
756 gc2235_write_reg_array(client, gc2235_init_settings);
757 }
758
759 ret = gc2235_write_reg_array(client, gc2235_res[dev->fmt_idx].regs);
760 if (ret) {
761 dev_err(&client->dev, "gc2235 write register err.\n");
762 return ret;
763 }
764 is_init = 0;
765
766 return ret;
767 }
768
769 static int gc2235_set_fmt(struct v4l2_subdev *sd,
770 struct v4l2_subdev_pad_config *cfg,
771 struct v4l2_subdev_format *format)
772 {
773
774 struct v4l2_mbus_framefmt *fmt = &format->format;
775 struct gc2235_device *dev = to_gc2235_sensor(sd);
776 struct i2c_client *client = v4l2_get_subdevdata(sd);
777 struct camera_mipi_info *gc2235_info = NULL;
778 int ret = 0;
779 int idx;
780
781 gc2235_info = v4l2_get_subdev_hostdata(sd);
782 if (!gc2235_info)
783 return -EINVAL;
784 if (format->pad)
785 return -EINVAL;
786 if (!fmt)
787 return -EINVAL;
788 mutex_lock(&dev->input_lock);
789 idx = nearest_resolution_index(fmt->width, fmt->height);
790 if (idx == -1) {
791 /* return the largest resolution */
792 fmt->width = gc2235_res[N_RES - 1].width;
793 fmt->height = gc2235_res[N_RES - 1].height;
794 } else {
795 fmt->width = gc2235_res[idx].width;
796 fmt->height = gc2235_res[idx].height;
797 }
798 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
799 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
800 cfg->try_fmt = *fmt;
801 mutex_unlock(&dev->input_lock);
802 return 0;
803 }
804
805 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
806 if (dev->fmt_idx == -1) {
807 dev_err(&client->dev, "get resolution fail\n");
808 mutex_unlock(&dev->input_lock);
809 return -EINVAL;
810 }
811
812 ret = startup(sd);
813 if (ret) {
814 dev_err(&client->dev, "gc2235 startup err\n");
815 goto err;
816 }
817
818 ret = gc2235_get_intg_factor(client, gc2235_info,
819 &gc2235_res[dev->fmt_idx]);
820 if (ret)
821 dev_err(&client->dev, "failed to get integration_factor\n");
822
823 err:
824 mutex_unlock(&dev->input_lock);
825 return ret;
826 }
827
828 static int gc2235_get_fmt(struct v4l2_subdev *sd,
829 struct v4l2_subdev_pad_config *cfg,
830 struct v4l2_subdev_format *format)
831 {
832 struct v4l2_mbus_framefmt *fmt = &format->format;
833 struct gc2235_device *dev = to_gc2235_sensor(sd);
834
835 if (format->pad)
836 return -EINVAL;
837
838 if (!fmt)
839 return -EINVAL;
840
841 fmt->width = gc2235_res[dev->fmt_idx].width;
842 fmt->height = gc2235_res[dev->fmt_idx].height;
843 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
844
845 return 0;
846 }
847
848 static int gc2235_detect(struct i2c_client *client)
849 {
850 struct i2c_adapter *adapter = client->adapter;
851 u16 high, low;
852 int ret;
853 u16 id;
854
855 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
856 return -ENODEV;
857
858 ret = gc2235_read_reg(client, GC2235_8BIT,
859 GC2235_SENSOR_ID_H, &high);
860 if (ret) {
861 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
862 return -ENODEV;
863 }
864 ret = gc2235_read_reg(client, GC2235_8BIT,
865 GC2235_SENSOR_ID_L, &low);
866 id = ((high << 8) | low);
867
868 if (id != GC2235_ID) {
869 dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
870 return -ENODEV;
871 }
872
873 dev_info(&client->dev, "detect gc2235 success\n");
874 return 0;
875 }
876
877 static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
878 {
879 struct gc2235_device *dev = to_gc2235_sensor(sd);
880 struct i2c_client *client = v4l2_get_subdevdata(sd);
881 int ret;
882 mutex_lock(&dev->input_lock);
883
884 if (enable)
885 ret = gc2235_write_reg_array(client, gc2235_stream_on);
886 else
887 ret = gc2235_write_reg_array(client, gc2235_stream_off);
888
889 mutex_unlock(&dev->input_lock);
890 return ret;
891 }
892
893
894 static int gc2235_s_config(struct v4l2_subdev *sd,
895 int irq, void *platform_data)
896 {
897 struct gc2235_device *dev = to_gc2235_sensor(sd);
898 struct i2c_client *client = v4l2_get_subdevdata(sd);
899 int ret = 0;
900
901 if (!platform_data)
902 return -ENODEV;
903
904 dev->platform_data =
905 (struct camera_sensor_platform_data *)platform_data;
906
907 mutex_lock(&dev->input_lock);
908 if (dev->platform_data->platform_init) {
909 ret = dev->platform_data->platform_init(client);
910 if (ret) {
911 dev_err(&client->dev, "platform init err\n");
912 goto platform_init_failed;
913 }
914 }
915 /* power off the module, then power on it in future
916 * as first power on by board may not fulfill the
917 * power on sequqence needed by the module
918 */
919 ret = power_down(sd);
920 if (ret) {
921 dev_err(&client->dev, "gc2235 power-off err.\n");
922 goto fail_power_off;
923 }
924
925 ret = power_up(sd);
926 if (ret) {
927 dev_err(&client->dev, "gc2235 power-up err.\n");
928 goto fail_power_on;
929 }
930
931 ret = dev->platform_data->csi_cfg(sd, 1);
932 if (ret)
933 goto fail_csi_cfg;
934
935 /* config & detect sensor */
936 ret = gc2235_detect(client);
937 if (ret) {
938 dev_err(&client->dev, "gc2235_detect err s_config.\n");
939 goto fail_csi_cfg;
940 }
941
942 /* turn off sensor, after probed */
943 ret = power_down(sd);
944 if (ret) {
945 dev_err(&client->dev, "gc2235 power-off err.\n");
946 goto fail_csi_cfg;
947 }
948 mutex_unlock(&dev->input_lock);
949
950 return 0;
951
952 fail_csi_cfg:
953 dev->platform_data->csi_cfg(sd, 0);
954 fail_power_on:
955 power_down(sd);
956 dev_err(&client->dev, "sensor power-gating failed\n");
957 fail_power_off:
958 if (dev->platform_data->platform_deinit)
959 dev->platform_data->platform_deinit();
960 platform_init_failed:
961 mutex_unlock(&dev->input_lock);
962 return ret;
963 }
964
965 static int gc2235_g_parm(struct v4l2_subdev *sd,
966 struct v4l2_streamparm *param)
967 {
968 struct gc2235_device *dev = to_gc2235_sensor(sd);
969 struct i2c_client *client = v4l2_get_subdevdata(sd);
970
971 if (!param)
972 return -EINVAL;
973
974 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
975 dev_err(&client->dev, "unsupported buffer type.\n");
976 return -EINVAL;
977 }
978
979 memset(param, 0, sizeof(*param));
980 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
981
982 if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
983 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
984 param->parm.capture.timeperframe.numerator = 1;
985 param->parm.capture.capturemode = dev->run_mode;
986 param->parm.capture.timeperframe.denominator =
987 gc2235_res[dev->fmt_idx].fps;
988 }
989 return 0;
990 }
991
992 static int gc2235_s_parm(struct v4l2_subdev *sd,
993 struct v4l2_streamparm *param)
994 {
995 struct gc2235_device *dev = to_gc2235_sensor(sd);
996 dev->run_mode = param->parm.capture.capturemode;
997
998 mutex_lock(&dev->input_lock);
999 switch (dev->run_mode) {
1000 case CI_MODE_VIDEO:
1001 gc2235_res = gc2235_res_video;
1002 N_RES = N_RES_VIDEO;
1003 break;
1004 case CI_MODE_STILL_CAPTURE:
1005 gc2235_res = gc2235_res_still;
1006 N_RES = N_RES_STILL;
1007 break;
1008 default:
1009 gc2235_res = gc2235_res_preview;
1010 N_RES = N_RES_PREVIEW;
1011 }
1012 mutex_unlock(&dev->input_lock);
1013 return 0;
1014 }
1015
1016 static int gc2235_g_frame_interval(struct v4l2_subdev *sd,
1017 struct v4l2_subdev_frame_interval *interval)
1018 {
1019 struct gc2235_device *dev = to_gc2235_sensor(sd);
1020
1021 interval->interval.numerator = 1;
1022 interval->interval.denominator = gc2235_res[dev->fmt_idx].fps;
1023
1024 return 0;
1025 }
1026
1027 static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
1028 struct v4l2_subdev_pad_config *cfg,
1029 struct v4l2_subdev_mbus_code_enum *code)
1030 {
1031 if (code->index >= MAX_FMTS)
1032 return -EINVAL;
1033
1034 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1035 return 0;
1036 }
1037
1038 static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
1039 struct v4l2_subdev_pad_config *cfg,
1040 struct v4l2_subdev_frame_size_enum *fse)
1041 {
1042 int index = fse->index;
1043
1044 if (index >= N_RES)
1045 return -EINVAL;
1046
1047 fse->min_width = gc2235_res[index].width;
1048 fse->min_height = gc2235_res[index].height;
1049 fse->max_width = gc2235_res[index].width;
1050 fse->max_height = gc2235_res[index].height;
1051
1052 return 0;
1053
1054 }
1055
1056 static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1057 {
1058 struct gc2235_device *dev = to_gc2235_sensor(sd);
1059
1060 mutex_lock(&dev->input_lock);
1061 *frames = gc2235_res[dev->fmt_idx].skip_frames;
1062 mutex_unlock(&dev->input_lock);
1063
1064 return 0;
1065 }
1066
1067 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
1068 .g_skip_frames = gc2235_g_skip_frames,
1069 };
1070
1071 static const struct v4l2_subdev_video_ops gc2235_video_ops = {
1072 .s_stream = gc2235_s_stream,
1073 .g_parm = gc2235_g_parm,
1074 .s_parm = gc2235_s_parm,
1075 .g_frame_interval = gc2235_g_frame_interval,
1076 };
1077
1078 static const struct v4l2_subdev_core_ops gc2235_core_ops = {
1079 .s_power = gc2235_s_power,
1080 .ioctl = gc2235_ioctl,
1081 };
1082
1083 static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
1084 .enum_mbus_code = gc2235_enum_mbus_code,
1085 .enum_frame_size = gc2235_enum_frame_size,
1086 .get_fmt = gc2235_get_fmt,
1087 .set_fmt = gc2235_set_fmt,
1088 };
1089
1090 static const struct v4l2_subdev_ops gc2235_ops = {
1091 .core = &gc2235_core_ops,
1092 .video = &gc2235_video_ops,
1093 .pad = &gc2235_pad_ops,
1094 .sensor = &gc2235_sensor_ops,
1095 };
1096
1097 static int gc2235_remove(struct i2c_client *client)
1098 {
1099 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1100 struct gc2235_device *dev = to_gc2235_sensor(sd);
1101 dev_dbg(&client->dev, "gc2235_remove...\n");
1102
1103 if (dev->platform_data->platform_deinit)
1104 dev->platform_data->platform_deinit();
1105
1106 dev->platform_data->csi_cfg(sd, 0);
1107
1108 v4l2_device_unregister_subdev(sd);
1109 media_entity_cleanup(&dev->sd.entity);
1110 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1111 kfree(dev);
1112
1113 return 0;
1114 }
1115
1116 static int gc2235_probe(struct i2c_client *client)
1117 {
1118 struct gc2235_device *dev;
1119 void *gcpdev;
1120 int ret;
1121 unsigned int i;
1122
1123 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1124 if (!dev)
1125 return -ENOMEM;
1126
1127 mutex_init(&dev->input_lock);
1128
1129 dev->fmt_idx = 0;
1130 v4l2_i2c_subdev_init(&(dev->sd), client, &gc2235_ops);
1131
1132 gcpdev = client->dev.platform_data;
1133 if (ACPI_COMPANION(&client->dev))
1134 gcpdev = gmin_camera_platform_data(&dev->sd,
1135 ATOMISP_INPUT_FORMAT_RAW_10,
1136 atomisp_bayer_order_grbg);
1137
1138 ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
1139 if (ret)
1140 goto out_free;
1141
1142 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1143 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1144 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1145 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1146 ret =
1147 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1148 ARRAY_SIZE(gc2235_controls));
1149 if (ret) {
1150 gc2235_remove(client);
1151 return ret;
1152 }
1153
1154 for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
1155 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
1156 NULL);
1157
1158 if (dev->ctrl_handler.error) {
1159 gc2235_remove(client);
1160 return dev->ctrl_handler.error;
1161 }
1162
1163 /* Use same lock for controls as for everything else. */
1164 dev->ctrl_handler.lock = &dev->input_lock;
1165 dev->sd.ctrl_handler = &dev->ctrl_handler;
1166
1167 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1168 if (ret)
1169 gc2235_remove(client);
1170
1171 if (ACPI_HANDLE(&client->dev))
1172 ret = atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA);
1173
1174 return ret;
1175 out_free:
1176 v4l2_device_unregister_subdev(&dev->sd);
1177 kfree(dev);
1178
1179 return ret;
1180 }
1181
1182 static const struct acpi_device_id gc2235_acpi_match[] = {
1183 { "INT33F8" },
1184 {},
1185 };
1186 MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
1187
1188 static struct i2c_driver gc2235_driver = {
1189 .driver = {
1190 .name = "gc2235",
1191 .acpi_match_table = gc2235_acpi_match,
1192 },
1193 .probe_new = gc2235_probe,
1194 .remove = gc2235_remove,
1195 };
1196 module_i2c_driver(gc2235_driver);
1197
1198 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1199 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1200 MODULE_LICENSE("GPL");