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