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