1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for GalaxyCore GC0310 VGA camera sensor.
4 *
5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/moduleparam.h>
31 #include <media/v4l2-device.h>
32 #include <linux/io.h>
33 #include "../include/linux/atomisp_gmin_platform.h"
34
35 #include "gc0310.h"
36
37 /* i2c read/write stuff */
gc0310_read_reg(struct i2c_client * client,u16 data_length,u8 reg,u8 * val)38 static int gc0310_read_reg(struct i2c_client *client,
39 u16 data_length, u8 reg, u8 *val)
40 {
41 int err;
42 struct i2c_msg msg[2];
43 unsigned char data[1];
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 != GC0310_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 = I2C_MSG_LENGTH;
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 == GC0310_8BIT)
84 *val = (u8)data[0];
85
86 return 0;
87 }
88
gc0310_i2c_write(struct i2c_client * client,u16 len,u8 * data)89 static int gc0310_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
gc0310_write_reg(struct i2c_client * client,u16 data_length,u8 reg,u8 val)104 static int gc0310_write_reg(struct i2c_client *client, u16 data_length,
105 u8 reg, u8 val)
106 {
107 int ret;
108 unsigned char data[2] = {0};
109 u8 *wreg = (u8 *)data;
110 const u16 len = data_length + sizeof(u8); /* 8-bit address + data */
111
112 if (data_length != GC0310_8BIT) {
113 dev_err(&client->dev,
114 "%s error, invalid data_length\n", __func__);
115 return -EINVAL;
116 }
117
118 /* high byte goes out first */
119 *wreg = (u8)(reg & 0xff);
120
121 if (data_length == GC0310_8BIT)
122 data[1] = (u8)(val);
123
124 ret = gc0310_i2c_write(client, len, data);
125 if (ret)
126 dev_err(&client->dev,
127 "write error: wrote 0x%x to offset 0x%x error %d",
128 val, reg, ret);
129
130 return ret;
131 }
132
133 /*
134 * gc0310_write_reg_array - Initializes a list of GC0310 registers
135 * @client: i2c driver client structure
136 * @reglist: list of registers to be written
137 *
138 * This function initializes a list of registers. When consecutive addresses
139 * are found in a row on the list, this function creates a buffer and sends
140 * consecutive data in a single i2c_transfer().
141 *
142 * __gc0310_flush_reg_array, __gc0310_buf_reg_array() and
143 * __gc0310_write_reg_is_consecutive() are internal functions to
144 * gc0310_write_reg_array_fast() and should be not used anywhere else.
145 *
146 */
147
__gc0310_flush_reg_array(struct i2c_client * client,struct gc0310_write_ctrl * ctrl)148 static int __gc0310_flush_reg_array(struct i2c_client *client,
149 struct gc0310_write_ctrl *ctrl)
150 {
151 u16 size;
152
153 if (ctrl->index == 0)
154 return 0;
155
156 size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
157 ctrl->buffer.addr = (u8)(ctrl->buffer.addr);
158 ctrl->index = 0;
159
160 return gc0310_i2c_write(client, size, (u8 *)&ctrl->buffer);
161 }
162
__gc0310_buf_reg_array(struct i2c_client * client,struct gc0310_write_ctrl * ctrl,const struct gc0310_reg * next)163 static int __gc0310_buf_reg_array(struct i2c_client *client,
164 struct gc0310_write_ctrl *ctrl,
165 const struct gc0310_reg *next)
166 {
167 int size;
168
169 switch (next->type) {
170 case GC0310_8BIT:
171 size = 1;
172 ctrl->buffer.data[ctrl->index] = (u8)next->val;
173 break;
174 default:
175 return -EINVAL;
176 }
177
178 /* When first item is added, we need to store its starting address */
179 if (ctrl->index == 0)
180 ctrl->buffer.addr = next->reg;
181
182 ctrl->index += size;
183
184 /*
185 * Buffer cannot guarantee free space for u32? Better flush it to avoid
186 * possible lack of memory for next item.
187 */
188 if (ctrl->index + sizeof(u8) >= GC0310_MAX_WRITE_BUF_SIZE)
189 return __gc0310_flush_reg_array(client, ctrl);
190
191 return 0;
192 }
193
__gc0310_write_reg_is_consecutive(struct i2c_client * client,struct gc0310_write_ctrl * ctrl,const struct gc0310_reg * next)194 static int __gc0310_write_reg_is_consecutive(struct i2c_client *client,
195 struct gc0310_write_ctrl *ctrl,
196 const struct gc0310_reg *next)
197 {
198 if (ctrl->index == 0)
199 return 1;
200
201 return ctrl->buffer.addr + ctrl->index == next->reg;
202 }
203
gc0310_write_reg_array(struct i2c_client * client,const struct gc0310_reg * reglist)204 static int gc0310_write_reg_array(struct i2c_client *client,
205 const struct gc0310_reg *reglist)
206 {
207 const struct gc0310_reg *next = reglist;
208 struct gc0310_write_ctrl ctrl;
209 int err;
210
211 ctrl.index = 0;
212 for (; next->type != GC0310_TOK_TERM; next++) {
213 switch (next->type & GC0310_TOK_MASK) {
214 case GC0310_TOK_DELAY:
215 err = __gc0310_flush_reg_array(client, &ctrl);
216 if (err)
217 return err;
218 msleep(next->val);
219 break;
220 default:
221 /*
222 * If next address is not consecutive, data needs to be
223 * flushed before proceed.
224 */
225 if (!__gc0310_write_reg_is_consecutive(client, &ctrl,
226 next)) {
227 err = __gc0310_flush_reg_array(client, &ctrl);
228 if (err)
229 return err;
230 }
231 err = __gc0310_buf_reg_array(client, &ctrl, next);
232 if (err) {
233 dev_err(&client->dev, "%s: write error, aborted\n",
234 __func__);
235 return err;
236 }
237 break;
238 }
239 }
240
241 return __gc0310_flush_reg_array(client, &ctrl);
242 }
243
gc0310_g_focal(struct v4l2_subdev * sd,s32 * val)244 static int gc0310_g_focal(struct v4l2_subdev *sd, s32 *val)
245 {
246 *val = (GC0310_FOCAL_LENGTH_NUM << 16) | GC0310_FOCAL_LENGTH_DEM;
247 return 0;
248 }
249
gc0310_g_fnumber(struct v4l2_subdev * sd,s32 * val)250 static int gc0310_g_fnumber(struct v4l2_subdev *sd, s32 *val)
251 {
252 /*const f number for imx*/
253 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 16) | GC0310_F_NUMBER_DEM;
254 return 0;
255 }
256
gc0310_g_fnumber_range(struct v4l2_subdev * sd,s32 * val)257 static int gc0310_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
258 {
259 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 24) |
260 (GC0310_F_NUMBER_DEM << 16) |
261 (GC0310_F_NUMBER_DEFAULT_NUM << 8) | GC0310_F_NUMBER_DEM;
262 return 0;
263 }
264
gc0310_g_bin_factor_x(struct v4l2_subdev * sd,s32 * val)265 static int gc0310_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
266 {
267 struct gc0310_device *dev = to_gc0310_sensor(sd);
268
269 *val = gc0310_res[dev->fmt_idx].bin_factor_x;
270
271 return 0;
272 }
273
gc0310_g_bin_factor_y(struct v4l2_subdev * sd,s32 * val)274 static int gc0310_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
275 {
276 struct gc0310_device *dev = to_gc0310_sensor(sd);
277
278 *val = gc0310_res[dev->fmt_idx].bin_factor_y;
279
280 return 0;
281 }
282
gc0310_get_intg_factor(struct i2c_client * client,struct camera_mipi_info * info,const struct gc0310_resolution * res)283 static int gc0310_get_intg_factor(struct i2c_client *client,
284 struct camera_mipi_info *info,
285 const struct gc0310_resolution *res)
286 {
287 struct v4l2_subdev *sd = i2c_get_clientdata(client);
288 struct gc0310_device *dev = to_gc0310_sensor(sd);
289 struct atomisp_sensor_mode_data *buf = &info->data;
290 u16 val;
291 u8 reg_val;
292 int ret;
293 unsigned int hori_blanking;
294 unsigned int vert_blanking;
295 unsigned int sh_delay;
296
297 if (!info)
298 return -EINVAL;
299
300 /* pixel clock calculattion */
301 dev->vt_pix_clk_freq_mhz = 14400000; // 16.8MHz
302 buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz;
303 pr_info("vt_pix_clk_freq_mhz=%d\n", buf->vt_pix_clk_freq_mhz);
304
305 /* get integration time */
306 buf->coarse_integration_time_min = GC0310_COARSE_INTG_TIME_MIN;
307 buf->coarse_integration_time_max_margin =
308 GC0310_COARSE_INTG_TIME_MAX_MARGIN;
309
310 buf->fine_integration_time_min = GC0310_FINE_INTG_TIME_MIN;
311 buf->fine_integration_time_max_margin =
312 GC0310_FINE_INTG_TIME_MAX_MARGIN;
313
314 buf->fine_integration_time_def = GC0310_FINE_INTG_TIME_MIN;
315 buf->read_mode = res->bin_mode;
316
317 /* get the cropping and output resolution to ISP for this mode. */
318 /* Getting crop_horizontal_start */
319 ret = gc0310_read_reg(client, GC0310_8BIT,
320 GC0310_H_CROP_START_H, ®_val);
321 if (ret)
322 return ret;
323 val = (reg_val & 0xFF) << 8;
324 ret = gc0310_read_reg(client, GC0310_8BIT,
325 GC0310_H_CROP_START_L, ®_val);
326 if (ret)
327 return ret;
328 buf->crop_horizontal_start = val | (reg_val & 0xFF);
329 pr_info("crop_horizontal_start=%d\n", buf->crop_horizontal_start);
330
331 /* Getting crop_vertical_start */
332 ret = gc0310_read_reg(client, GC0310_8BIT,
333 GC0310_V_CROP_START_H, ®_val);
334 if (ret)
335 return ret;
336 val = (reg_val & 0xFF) << 8;
337 ret = gc0310_read_reg(client, GC0310_8BIT,
338 GC0310_V_CROP_START_L, ®_val);
339 if (ret)
340 return ret;
341 buf->crop_vertical_start = val | (reg_val & 0xFF);
342 pr_info("crop_vertical_start=%d\n", buf->crop_vertical_start);
343
344 /* Getting output_width */
345 ret = gc0310_read_reg(client, GC0310_8BIT,
346 GC0310_H_OUTSIZE_H, ®_val);
347 if (ret)
348 return ret;
349 val = (reg_val & 0xFF) << 8;
350 ret = gc0310_read_reg(client, GC0310_8BIT,
351 GC0310_H_OUTSIZE_L, ®_val);
352 if (ret)
353 return ret;
354 buf->output_width = val | (reg_val & 0xFF);
355 pr_info("output_width=%d\n", buf->output_width);
356
357 /* Getting output_height */
358 ret = gc0310_read_reg(client, GC0310_8BIT,
359 GC0310_V_OUTSIZE_H, ®_val);
360 if (ret)
361 return ret;
362 val = (reg_val & 0xFF) << 8;
363 ret = gc0310_read_reg(client, GC0310_8BIT,
364 GC0310_V_OUTSIZE_L, ®_val);
365 if (ret)
366 return ret;
367 buf->output_height = val | (reg_val & 0xFF);
368 pr_info("output_height=%d\n", buf->output_height);
369
370 buf->crop_horizontal_end = buf->crop_horizontal_start + buf->output_width - 1;
371 buf->crop_vertical_end = buf->crop_vertical_start + buf->output_height - 1;
372 pr_info("crop_horizontal_end=%d\n", buf->crop_horizontal_end);
373 pr_info("crop_vertical_end=%d\n", buf->crop_vertical_end);
374
375 /* Getting line_length_pck */
376 ret = gc0310_read_reg(client, GC0310_8BIT,
377 GC0310_H_BLANKING_H, ®_val);
378 if (ret)
379 return ret;
380 val = (reg_val & 0xFF) << 8;
381 ret = gc0310_read_reg(client, GC0310_8BIT,
382 GC0310_H_BLANKING_L, ®_val);
383 if (ret)
384 return ret;
385 hori_blanking = val | (reg_val & 0xFF);
386 ret = gc0310_read_reg(client, GC0310_8BIT,
387 GC0310_SH_DELAY, ®_val);
388 if (ret)
389 return ret;
390 sh_delay = reg_val;
391 buf->line_length_pck = buf->output_width + hori_blanking + sh_delay + 4;
392 pr_info("hori_blanking=%d sh_delay=%d line_length_pck=%d\n", hori_blanking,
393 sh_delay, buf->line_length_pck);
394
395 /* Getting frame_length_lines */
396 ret = gc0310_read_reg(client, GC0310_8BIT,
397 GC0310_V_BLANKING_H, ®_val);
398 if (ret)
399 return ret;
400 val = (reg_val & 0xFF) << 8;
401 ret = gc0310_read_reg(client, GC0310_8BIT,
402 GC0310_V_BLANKING_L, ®_val);
403 if (ret)
404 return ret;
405 vert_blanking = val | (reg_val & 0xFF);
406 buf->frame_length_lines = buf->output_height + vert_blanking;
407 pr_info("vert_blanking=%d frame_length_lines=%d\n", vert_blanking,
408 buf->frame_length_lines);
409
410 buf->binning_factor_x = res->bin_factor_x ?
411 res->bin_factor_x : 1;
412 buf->binning_factor_y = res->bin_factor_y ?
413 res->bin_factor_y : 1;
414 return 0;
415 }
416
gc0310_set_gain(struct v4l2_subdev * sd,int gain)417 static int gc0310_set_gain(struct v4l2_subdev *sd, int gain)
418
419 {
420 struct i2c_client *client = v4l2_get_subdevdata(sd);
421 int ret;
422 u8 again, dgain;
423
424 if (gain < 0x20)
425 gain = 0x20;
426 if (gain > 0x80)
427 gain = 0x80;
428
429 if (gain >= 0x20 && gain < 0x40) {
430 again = 0x0; /* sqrt(2) */
431 dgain = gain;
432 } else {
433 again = 0x2; /* 2 * sqrt(2) */
434 dgain = gain / 2;
435 }
436
437 pr_info("gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain);
438
439 /* set analog gain */
440 ret = gc0310_write_reg(client, GC0310_8BIT,
441 GC0310_AGC_ADJ, again);
442 if (ret)
443 return ret;
444
445 /* set digital gain */
446 ret = gc0310_write_reg(client, GC0310_8BIT,
447 GC0310_DGC_ADJ, dgain);
448 if (ret)
449 return ret;
450
451 return 0;
452 }
453
__gc0310_set_exposure(struct v4l2_subdev * sd,int coarse_itg,int gain,int digitgain)454 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
455 int gain, int digitgain)
456
457 {
458 struct i2c_client *client = v4l2_get_subdevdata(sd);
459 int ret;
460
461 pr_info("coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain);
462
463 /* set exposure */
464 ret = gc0310_write_reg(client, GC0310_8BIT,
465 GC0310_AEC_PK_EXPO_L,
466 coarse_itg & 0xff);
467 if (ret)
468 return ret;
469
470 ret = gc0310_write_reg(client, GC0310_8BIT,
471 GC0310_AEC_PK_EXPO_H,
472 (coarse_itg >> 8) & 0x0f);
473 if (ret)
474 return ret;
475
476 ret = gc0310_set_gain(sd, gain);
477 if (ret)
478 return ret;
479
480 return ret;
481 }
482
gc0310_set_exposure(struct v4l2_subdev * sd,int exposure,int gain,int digitgain)483 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure,
484 int gain, int digitgain)
485 {
486 struct gc0310_device *dev = to_gc0310_sensor(sd);
487 int ret;
488
489 mutex_lock(&dev->input_lock);
490 ret = __gc0310_set_exposure(sd, exposure, gain, digitgain);
491 mutex_unlock(&dev->input_lock);
492
493 return ret;
494 }
495
gc0310_s_exposure(struct v4l2_subdev * sd,struct atomisp_exposure * exposure)496 static long gc0310_s_exposure(struct v4l2_subdev *sd,
497 struct atomisp_exposure *exposure)
498 {
499 int exp = exposure->integration_time[0];
500 int gain = exposure->gain[0];
501 int digitgain = exposure->gain[1];
502
503 /* we should not accept the invalid value below. */
504 if (gain == 0) {
505 struct i2c_client *client = v4l2_get_subdevdata(sd);
506
507 v4l2_err(client, "%s: invalid value\n", __func__);
508 return -EINVAL;
509 }
510
511 return gc0310_set_exposure(sd, exp, gain, digitgain);
512 }
513
514 /* TO DO */
gc0310_v_flip(struct v4l2_subdev * sd,s32 value)515 static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value)
516 {
517 return 0;
518 }
519
520 /* TO DO */
gc0310_h_flip(struct v4l2_subdev * sd,s32 value)521 static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value)
522 {
523 return 0;
524 }
525
gc0310_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)526 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
527 {
528 switch (cmd) {
529 case ATOMISP_IOC_S_EXPOSURE:
530 return gc0310_s_exposure(sd, arg);
531 default:
532 return -EINVAL;
533 }
534 return 0;
535 }
536
537 /* This returns the exposure time being used. This should only be used
538 * for filling in EXIF data, not for actual image processing.
539 */
gc0310_q_exposure(struct v4l2_subdev * sd,s32 * value)540 static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value)
541 {
542 struct i2c_client *client = v4l2_get_subdevdata(sd);
543 u8 reg_v;
544 int ret;
545
546 /* get exposure */
547 ret = gc0310_read_reg(client, GC0310_8BIT,
548 GC0310_AEC_PK_EXPO_L,
549 ®_v);
550 if (ret)
551 goto err;
552
553 *value = reg_v;
554 ret = gc0310_read_reg(client, GC0310_8BIT,
555 GC0310_AEC_PK_EXPO_H,
556 ®_v);
557 if (ret)
558 goto err;
559
560 *value = *value + (reg_v << 8);
561 err:
562 return ret;
563 }
564
gc0310_s_ctrl(struct v4l2_ctrl * ctrl)565 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl)
566 {
567 struct gc0310_device *dev =
568 container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
569 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
570 int ret = 0;
571
572 switch (ctrl->id) {
573 case V4L2_CID_VFLIP:
574 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
575 __func__, ctrl->val);
576 ret = gc0310_v_flip(&dev->sd, ctrl->val);
577 break;
578 case V4L2_CID_HFLIP:
579 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
580 __func__, ctrl->val);
581 ret = gc0310_h_flip(&dev->sd, ctrl->val);
582 break;
583 default:
584 ret = -EINVAL;
585 }
586 return ret;
587 }
588
gc0310_g_volatile_ctrl(struct v4l2_ctrl * ctrl)589 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
590 {
591 struct gc0310_device *dev =
592 container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
593 int ret = 0;
594
595 switch (ctrl->id) {
596 case V4L2_CID_EXPOSURE_ABSOLUTE:
597 ret = gc0310_q_exposure(&dev->sd, &ctrl->val);
598 break;
599 case V4L2_CID_FOCAL_ABSOLUTE:
600 ret = gc0310_g_focal(&dev->sd, &ctrl->val);
601 break;
602 case V4L2_CID_FNUMBER_ABSOLUTE:
603 ret = gc0310_g_fnumber(&dev->sd, &ctrl->val);
604 break;
605 case V4L2_CID_FNUMBER_RANGE:
606 ret = gc0310_g_fnumber_range(&dev->sd, &ctrl->val);
607 break;
608 case V4L2_CID_BIN_FACTOR_HORZ:
609 ret = gc0310_g_bin_factor_x(&dev->sd, &ctrl->val);
610 break;
611 case V4L2_CID_BIN_FACTOR_VERT:
612 ret = gc0310_g_bin_factor_y(&dev->sd, &ctrl->val);
613 break;
614 default:
615 ret = -EINVAL;
616 }
617
618 return ret;
619 }
620
621 static const struct v4l2_ctrl_ops ctrl_ops = {
622 .s_ctrl = gc0310_s_ctrl,
623 .g_volatile_ctrl = gc0310_g_volatile_ctrl
624 };
625
626 static const struct v4l2_ctrl_config gc0310_controls[] = {
627 {
628 .ops = &ctrl_ops,
629 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
630 .type = V4L2_CTRL_TYPE_INTEGER,
631 .name = "exposure",
632 .min = 0x0,
633 .max = 0xffff,
634 .step = 0x01,
635 .def = 0x00,
636 .flags = 0,
637 },
638 {
639 .ops = &ctrl_ops,
640 .id = V4L2_CID_VFLIP,
641 .type = V4L2_CTRL_TYPE_BOOLEAN,
642 .name = "Flip",
643 .min = 0,
644 .max = 1,
645 .step = 1,
646 .def = 0,
647 },
648 {
649 .ops = &ctrl_ops,
650 .id = V4L2_CID_HFLIP,
651 .type = V4L2_CTRL_TYPE_BOOLEAN,
652 .name = "Mirror",
653 .min = 0,
654 .max = 1,
655 .step = 1,
656 .def = 0,
657 },
658 {
659 .ops = &ctrl_ops,
660 .id = V4L2_CID_FOCAL_ABSOLUTE,
661 .type = V4L2_CTRL_TYPE_INTEGER,
662 .name = "focal length",
663 .min = GC0310_FOCAL_LENGTH_DEFAULT,
664 .max = GC0310_FOCAL_LENGTH_DEFAULT,
665 .step = 0x01,
666 .def = GC0310_FOCAL_LENGTH_DEFAULT,
667 .flags = 0,
668 },
669 {
670 .ops = &ctrl_ops,
671 .id = V4L2_CID_FNUMBER_ABSOLUTE,
672 .type = V4L2_CTRL_TYPE_INTEGER,
673 .name = "f-number",
674 .min = GC0310_F_NUMBER_DEFAULT,
675 .max = GC0310_F_NUMBER_DEFAULT,
676 .step = 0x01,
677 .def = GC0310_F_NUMBER_DEFAULT,
678 .flags = 0,
679 },
680 {
681 .ops = &ctrl_ops,
682 .id = V4L2_CID_FNUMBER_RANGE,
683 .type = V4L2_CTRL_TYPE_INTEGER,
684 .name = "f-number range",
685 .min = GC0310_F_NUMBER_RANGE,
686 .max = GC0310_F_NUMBER_RANGE,
687 .step = 0x01,
688 .def = GC0310_F_NUMBER_RANGE,
689 .flags = 0,
690 },
691 {
692 .ops = &ctrl_ops,
693 .id = V4L2_CID_BIN_FACTOR_HORZ,
694 .type = V4L2_CTRL_TYPE_INTEGER,
695 .name = "horizontal binning factor",
696 .min = 0,
697 .max = GC0310_BIN_FACTOR_MAX,
698 .step = 1,
699 .def = 0,
700 .flags = 0,
701 },
702 {
703 .ops = &ctrl_ops,
704 .id = V4L2_CID_BIN_FACTOR_VERT,
705 .type = V4L2_CTRL_TYPE_INTEGER,
706 .name = "vertical binning factor",
707 .min = 0,
708 .max = GC0310_BIN_FACTOR_MAX,
709 .step = 1,
710 .def = 0,
711 .flags = 0,
712 },
713 };
714
gc0310_init(struct v4l2_subdev * sd)715 static int gc0310_init(struct v4l2_subdev *sd)
716 {
717 int ret;
718 struct i2c_client *client = v4l2_get_subdevdata(sd);
719 struct gc0310_device *dev = to_gc0310_sensor(sd);
720
721 pr_info("%s S\n", __func__);
722 mutex_lock(&dev->input_lock);
723
724 /* set initial registers */
725 ret = gc0310_write_reg_array(client, gc0310_reset_register);
726
727 /* restore settings */
728 gc0310_res = gc0310_res_preview;
729 N_RES = N_RES_PREVIEW;
730
731 mutex_unlock(&dev->input_lock);
732
733 pr_info("%s E\n", __func__);
734 return ret;
735 }
736
power_ctrl(struct v4l2_subdev * sd,bool flag)737 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
738 {
739 int ret = 0;
740 struct gc0310_device *dev = to_gc0310_sensor(sd);
741
742 if (!dev || !dev->platform_data)
743 return -ENODEV;
744
745 if (flag) {
746 /* The upstream module driver (written to Crystal
747 * Cove) had this logic to pulse the rails low first.
748 * This appears to break things on the MRD7 with the
749 * X-Powers PMIC...
750 *
751 * ret = dev->platform_data->v1p8_ctrl(sd, 0);
752 * ret |= dev->platform_data->v2p8_ctrl(sd, 0);
753 * mdelay(50);
754 */
755 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
756 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
757 usleep_range(10000, 15000);
758 }
759
760 if (!flag || ret) {
761 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
762 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
763 }
764 return ret;
765 }
766
gpio_ctrl(struct v4l2_subdev * sd,bool flag)767 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
768 {
769 int ret;
770 struct gc0310_device *dev = to_gc0310_sensor(sd);
771
772 if (!dev || !dev->platform_data)
773 return -ENODEV;
774
775 /* GPIO0 == "reset" (active low), GPIO1 == "power down" */
776 if (flag) {
777 /* Pulse reset, then release power down */
778 ret = dev->platform_data->gpio0_ctrl(sd, 0);
779 usleep_range(5000, 10000);
780 ret |= dev->platform_data->gpio0_ctrl(sd, 1);
781 usleep_range(10000, 15000);
782 ret |= dev->platform_data->gpio1_ctrl(sd, 0);
783 usleep_range(10000, 15000);
784 } else {
785 ret = dev->platform_data->gpio1_ctrl(sd, 1);
786 ret |= dev->platform_data->gpio0_ctrl(sd, 0);
787 }
788 return ret;
789 }
790
791 static int power_down(struct v4l2_subdev *sd);
792
power_up(struct v4l2_subdev * sd)793 static int power_up(struct v4l2_subdev *sd)
794 {
795 struct gc0310_device *dev = to_gc0310_sensor(sd);
796 struct i2c_client *client = v4l2_get_subdevdata(sd);
797 int ret;
798
799 pr_info("%s S\n", __func__);
800 if (!dev->platform_data) {
801 dev_err(&client->dev,
802 "no camera_sensor_platform_data");
803 return -ENODEV;
804 }
805
806 /* power control */
807 ret = power_ctrl(sd, 1);
808 if (ret)
809 goto fail_power;
810
811 /* flis clock control */
812 ret = dev->platform_data->flisclk_ctrl(sd, 1);
813 if (ret)
814 goto fail_clk;
815
816 /* gpio ctrl */
817 ret = gpio_ctrl(sd, 1);
818 if (ret) {
819 ret = gpio_ctrl(sd, 1);
820 if (ret)
821 goto fail_gpio;
822 }
823
824 msleep(100);
825
826 pr_info("%s E\n", __func__);
827 return 0;
828
829 fail_gpio:
830 dev->platform_data->flisclk_ctrl(sd, 0);
831 fail_clk:
832 power_ctrl(sd, 0);
833 fail_power:
834 dev_err(&client->dev, "sensor power-up failed\n");
835
836 return ret;
837 }
838
power_down(struct v4l2_subdev * sd)839 static int power_down(struct v4l2_subdev *sd)
840 {
841 struct gc0310_device *dev = to_gc0310_sensor(sd);
842 struct i2c_client *client = v4l2_get_subdevdata(sd);
843 int ret = 0;
844
845 if (!dev->platform_data) {
846 dev_err(&client->dev,
847 "no camera_sensor_platform_data");
848 return -ENODEV;
849 }
850
851 /* gpio ctrl */
852 ret = gpio_ctrl(sd, 0);
853 if (ret) {
854 ret = gpio_ctrl(sd, 0);
855 if (ret)
856 dev_err(&client->dev, "gpio failed 2\n");
857 }
858
859 ret = dev->platform_data->flisclk_ctrl(sd, 0);
860 if (ret)
861 dev_err(&client->dev, "flisclk failed\n");
862
863 /* power control */
864 ret = power_ctrl(sd, 0);
865 if (ret)
866 dev_err(&client->dev, "vprog failed.\n");
867
868 return ret;
869 }
870
gc0310_s_power(struct v4l2_subdev * sd,int on)871 static int gc0310_s_power(struct v4l2_subdev *sd, int on)
872 {
873 int ret;
874
875 if (on == 0)
876 return power_down(sd);
877 else {
878 ret = power_up(sd);
879 if (!ret)
880 return gc0310_init(sd);
881 }
882 return ret;
883 }
884
885 /*
886 * distance - calculate the distance
887 * @res: resolution
888 * @w: width
889 * @h: height
890 *
891 * Get the gap between resolution and w/h.
892 * res->width/height smaller than w/h wouldn't be considered.
893 * Returns the value of gap or -1 if fail.
894 */
895 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
distance(struct gc0310_resolution * res,u32 w,u32 h)896 static int distance(struct gc0310_resolution *res, u32 w, u32 h)
897 {
898 unsigned int w_ratio = (res->width << 13) / w;
899 unsigned int h_ratio;
900 int match;
901
902 if (h == 0)
903 return -1;
904 h_ratio = (res->height << 13) / h;
905 if (h_ratio == 0)
906 return -1;
907 match = abs(((w_ratio << 13) / h_ratio) - 8192);
908
909 if ((w_ratio < 8192) || (h_ratio < 8192) ||
910 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
911 return -1;
912
913 return w_ratio + h_ratio;
914 }
915
916 /* Return the nearest higher resolution index */
nearest_resolution_index(int w,int h)917 static int nearest_resolution_index(int w, int h)
918 {
919 int i;
920 int idx = -1;
921 int dist;
922 int min_dist = INT_MAX;
923 struct gc0310_resolution *tmp_res = NULL;
924
925 for (i = 0; i < N_RES; i++) {
926 tmp_res = &gc0310_res[i];
927 dist = distance(tmp_res, w, h);
928 if (dist == -1)
929 continue;
930 if (dist < min_dist) {
931 min_dist = dist;
932 idx = i;
933 }
934 }
935
936 return idx;
937 }
938
get_resolution_index(int w,int h)939 static int get_resolution_index(int w, int h)
940 {
941 int i;
942
943 for (i = 0; i < N_RES; i++) {
944 if (w != gc0310_res[i].width)
945 continue;
946 if (h != gc0310_res[i].height)
947 continue;
948
949 return i;
950 }
951
952 return -1;
953 }
954
955 /* TODO: remove it. */
startup(struct v4l2_subdev * sd)956 static int startup(struct v4l2_subdev *sd)
957 {
958 struct gc0310_device *dev = to_gc0310_sensor(sd);
959 struct i2c_client *client = v4l2_get_subdevdata(sd);
960 int ret = 0;
961
962 pr_info("%s S\n", __func__);
963
964 ret = gc0310_write_reg_array(client, gc0310_res[dev->fmt_idx].regs);
965 if (ret) {
966 dev_err(&client->dev, "gc0310 write register err.\n");
967 return ret;
968 }
969
970 pr_info("%s E\n", __func__);
971 return ret;
972 }
973
gc0310_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)974 static int gc0310_set_fmt(struct v4l2_subdev *sd,
975 struct v4l2_subdev_pad_config *cfg,
976 struct v4l2_subdev_format *format)
977 {
978 struct v4l2_mbus_framefmt *fmt = &format->format;
979 struct gc0310_device *dev = to_gc0310_sensor(sd);
980 struct i2c_client *client = v4l2_get_subdevdata(sd);
981 struct camera_mipi_info *gc0310_info = NULL;
982 int ret = 0;
983 int idx = 0;
984
985 pr_info("%s S\n", __func__);
986
987 if (format->pad)
988 return -EINVAL;
989
990 if (!fmt)
991 return -EINVAL;
992
993 gc0310_info = v4l2_get_subdev_hostdata(sd);
994 if (!gc0310_info)
995 return -EINVAL;
996
997 mutex_lock(&dev->input_lock);
998
999 idx = nearest_resolution_index(fmt->width, fmt->height);
1000 if (idx == -1) {
1001 /* return the largest resolution */
1002 fmt->width = gc0310_res[N_RES - 1].width;
1003 fmt->height = gc0310_res[N_RES - 1].height;
1004 } else {
1005 fmt->width = gc0310_res[idx].width;
1006 fmt->height = gc0310_res[idx].height;
1007 }
1008 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1009
1010 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1011 cfg->try_fmt = *fmt;
1012 mutex_unlock(&dev->input_lock);
1013 return 0;
1014 }
1015
1016 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1017 if (dev->fmt_idx == -1) {
1018 dev_err(&client->dev, "get resolution fail\n");
1019 mutex_unlock(&dev->input_lock);
1020 return -EINVAL;
1021 }
1022
1023 printk("%s: before gc0310_write_reg_array %s\n", __func__,
1024 gc0310_res[dev->fmt_idx].desc);
1025 ret = startup(sd);
1026 if (ret) {
1027 dev_err(&client->dev, "gc0310 startup err\n");
1028 goto err;
1029 }
1030
1031 ret = gc0310_get_intg_factor(client, gc0310_info,
1032 &gc0310_res[dev->fmt_idx]);
1033 if (ret) {
1034 dev_err(&client->dev, "failed to get integration_factor\n");
1035 goto err;
1036 }
1037
1038 pr_info("%s E\n", __func__);
1039 err:
1040 mutex_unlock(&dev->input_lock);
1041 return ret;
1042 }
1043
gc0310_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)1044 static int gc0310_get_fmt(struct v4l2_subdev *sd,
1045 struct v4l2_subdev_pad_config *cfg,
1046 struct v4l2_subdev_format *format)
1047 {
1048 struct v4l2_mbus_framefmt *fmt = &format->format;
1049 struct gc0310_device *dev = to_gc0310_sensor(sd);
1050
1051 if (format->pad)
1052 return -EINVAL;
1053
1054 if (!fmt)
1055 return -EINVAL;
1056
1057 fmt->width = gc0310_res[dev->fmt_idx].width;
1058 fmt->height = gc0310_res[dev->fmt_idx].height;
1059 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1060
1061 return 0;
1062 }
1063
gc0310_detect(struct i2c_client * client)1064 static int gc0310_detect(struct i2c_client *client)
1065 {
1066 struct i2c_adapter *adapter = client->adapter;
1067 u8 high, low;
1068 int ret;
1069 u16 id;
1070
1071 pr_info("%s S\n", __func__);
1072 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1073 return -ENODEV;
1074
1075 ret = gc0310_read_reg(client, GC0310_8BIT,
1076 GC0310_SC_CMMN_CHIP_ID_H, &high);
1077 if (ret) {
1078 dev_err(&client->dev, "read sensor_id_high failed\n");
1079 return -ENODEV;
1080 }
1081 ret = gc0310_read_reg(client, GC0310_8BIT,
1082 GC0310_SC_CMMN_CHIP_ID_L, &low);
1083 if (ret) {
1084 dev_err(&client->dev, "read sensor_id_low failed\n");
1085 return -ENODEV;
1086 }
1087 id = ((((u16)high) << 8) | (u16)low);
1088 pr_info("sensor ID = 0x%x\n", id);
1089
1090 if (id != GC0310_ID) {
1091 dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id,
1092 GC0310_ID);
1093 return -ENODEV;
1094 }
1095
1096 dev_dbg(&client->dev, "detect gc0310 success\n");
1097
1098 pr_info("%s E\n", __func__);
1099
1100 return 0;
1101 }
1102
gc0310_s_stream(struct v4l2_subdev * sd,int enable)1103 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable)
1104 {
1105 struct gc0310_device *dev = to_gc0310_sensor(sd);
1106 struct i2c_client *client = v4l2_get_subdevdata(sd);
1107 int ret;
1108
1109 pr_info("%s S enable=%d\n", __func__, enable);
1110 mutex_lock(&dev->input_lock);
1111
1112 if (enable) {
1113 /* enable per frame MIPI and sensor ctrl reset */
1114 ret = gc0310_write_reg(client, GC0310_8BIT,
1115 0xFE, 0x30);
1116 if (ret) {
1117 mutex_unlock(&dev->input_lock);
1118 return ret;
1119 }
1120 }
1121
1122 ret = gc0310_write_reg(client, GC0310_8BIT,
1123 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3);
1124 if (ret) {
1125 mutex_unlock(&dev->input_lock);
1126 return ret;
1127 }
1128
1129 ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM,
1130 enable ? GC0310_START_STREAMING :
1131 GC0310_STOP_STREAMING);
1132 if (ret) {
1133 mutex_unlock(&dev->input_lock);
1134 return ret;
1135 }
1136
1137 ret = gc0310_write_reg(client, GC0310_8BIT,
1138 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0);
1139 if (ret) {
1140 mutex_unlock(&dev->input_lock);
1141 return ret;
1142 }
1143
1144 mutex_unlock(&dev->input_lock);
1145 pr_info("%s E\n", __func__);
1146 return ret;
1147 }
1148
gc0310_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)1149 static int gc0310_s_config(struct v4l2_subdev *sd,
1150 int irq, void *platform_data)
1151 {
1152 struct gc0310_device *dev = to_gc0310_sensor(sd);
1153 struct i2c_client *client = v4l2_get_subdevdata(sd);
1154 int ret = 0;
1155
1156 pr_info("%s S\n", __func__);
1157 if (!platform_data)
1158 return -ENODEV;
1159
1160 dev->platform_data =
1161 (struct camera_sensor_platform_data *)platform_data;
1162
1163 mutex_lock(&dev->input_lock);
1164 /* power off the module, then power on it in future
1165 * as first power on by board may not fulfill the
1166 * power on sequqence needed by the module
1167 */
1168 ret = power_down(sd);
1169 if (ret) {
1170 dev_err(&client->dev, "gc0310 power-off err.\n");
1171 goto fail_power_off;
1172 }
1173
1174 ret = power_up(sd);
1175 if (ret) {
1176 dev_err(&client->dev, "gc0310 power-up err.\n");
1177 goto fail_power_on;
1178 }
1179
1180 ret = dev->platform_data->csi_cfg(sd, 1);
1181 if (ret)
1182 goto fail_csi_cfg;
1183
1184 /* config & detect sensor */
1185 ret = gc0310_detect(client);
1186 if (ret) {
1187 dev_err(&client->dev, "gc0310_detect err s_config.\n");
1188 goto fail_csi_cfg;
1189 }
1190
1191 /* turn off sensor, after probed */
1192 ret = power_down(sd);
1193 if (ret) {
1194 dev_err(&client->dev, "gc0310 power-off err.\n");
1195 goto fail_csi_cfg;
1196 }
1197 mutex_unlock(&dev->input_lock);
1198
1199 pr_info("%s E\n", __func__);
1200 return 0;
1201
1202 fail_csi_cfg:
1203 dev->platform_data->csi_cfg(sd, 0);
1204 fail_power_on:
1205 power_down(sd);
1206 dev_err(&client->dev, "sensor power-gating failed\n");
1207 fail_power_off:
1208 mutex_unlock(&dev->input_lock);
1209 return ret;
1210 }
1211
gc0310_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * interval)1212 static int gc0310_g_frame_interval(struct v4l2_subdev *sd,
1213 struct v4l2_subdev_frame_interval *interval)
1214 {
1215 struct gc0310_device *dev = to_gc0310_sensor(sd);
1216
1217 interval->interval.numerator = 1;
1218 interval->interval.denominator = gc0310_res[dev->fmt_idx].fps;
1219
1220 return 0;
1221 }
1222
gc0310_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1223 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd,
1224 struct v4l2_subdev_pad_config *cfg,
1225 struct v4l2_subdev_mbus_code_enum *code)
1226 {
1227 if (code->index >= MAX_FMTS)
1228 return -EINVAL;
1229
1230 code->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1231 return 0;
1232 }
1233
gc0310_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1234 static int gc0310_enum_frame_size(struct v4l2_subdev *sd,
1235 struct v4l2_subdev_pad_config *cfg,
1236 struct v4l2_subdev_frame_size_enum *fse)
1237 {
1238 int index = fse->index;
1239
1240 if (index >= N_RES)
1241 return -EINVAL;
1242
1243 fse->min_width = gc0310_res[index].width;
1244 fse->min_height = gc0310_res[index].height;
1245 fse->max_width = gc0310_res[index].width;
1246 fse->max_height = gc0310_res[index].height;
1247
1248 return 0;
1249 }
1250
gc0310_g_skip_frames(struct v4l2_subdev * sd,u32 * frames)1251 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1252 {
1253 struct gc0310_device *dev = to_gc0310_sensor(sd);
1254
1255 mutex_lock(&dev->input_lock);
1256 *frames = gc0310_res[dev->fmt_idx].skip_frames;
1257 mutex_unlock(&dev->input_lock);
1258
1259 return 0;
1260 }
1261
1262 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = {
1263 .g_skip_frames = gc0310_g_skip_frames,
1264 };
1265
1266 static const struct v4l2_subdev_video_ops gc0310_video_ops = {
1267 .s_stream = gc0310_s_stream,
1268 .g_frame_interval = gc0310_g_frame_interval,
1269 };
1270
1271 static const struct v4l2_subdev_core_ops gc0310_core_ops = {
1272 .s_power = gc0310_s_power,
1273 .ioctl = gc0310_ioctl,
1274 };
1275
1276 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = {
1277 .enum_mbus_code = gc0310_enum_mbus_code,
1278 .enum_frame_size = gc0310_enum_frame_size,
1279 .get_fmt = gc0310_get_fmt,
1280 .set_fmt = gc0310_set_fmt,
1281 };
1282
1283 static const struct v4l2_subdev_ops gc0310_ops = {
1284 .core = &gc0310_core_ops,
1285 .video = &gc0310_video_ops,
1286 .pad = &gc0310_pad_ops,
1287 .sensor = &gc0310_sensor_ops,
1288 };
1289
gc0310_remove(struct i2c_client * client)1290 static int gc0310_remove(struct i2c_client *client)
1291 {
1292 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1293 struct gc0310_device *dev = to_gc0310_sensor(sd);
1294
1295 dev_dbg(&client->dev, "gc0310_remove...\n");
1296
1297 dev->platform_data->csi_cfg(sd, 0);
1298
1299 v4l2_device_unregister_subdev(sd);
1300 media_entity_cleanup(&dev->sd.entity);
1301 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1302 kfree(dev);
1303
1304 return 0;
1305 }
1306
gc0310_probe(struct i2c_client * client)1307 static int gc0310_probe(struct i2c_client *client)
1308 {
1309 struct gc0310_device *dev;
1310 int ret;
1311 void *pdata;
1312 unsigned int i;
1313
1314 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1315 if (!dev)
1316 return -ENOMEM;
1317
1318 mutex_init(&dev->input_lock);
1319
1320 dev->fmt_idx = 0;
1321 v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops);
1322
1323 pdata = gmin_camera_platform_data(&dev->sd,
1324 ATOMISP_INPUT_FORMAT_RAW_8,
1325 atomisp_bayer_order_grbg);
1326 if (!pdata) {
1327 ret = -EINVAL;
1328 goto out_free;
1329 }
1330
1331 ret = gc0310_s_config(&dev->sd, client->irq, pdata);
1332 if (ret)
1333 goto out_free;
1334
1335 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1336 if (ret)
1337 goto out_free;
1338
1339 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1340 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1341 dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8;
1342 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1343 ret =
1344 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1345 ARRAY_SIZE(gc0310_controls));
1346 if (ret) {
1347 gc0310_remove(client);
1348 return ret;
1349 }
1350
1351 for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++)
1352 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i],
1353 NULL);
1354
1355 if (dev->ctrl_handler.error) {
1356 gc0310_remove(client);
1357 return dev->ctrl_handler.error;
1358 }
1359
1360 /* Use same lock for controls as for everything else. */
1361 dev->ctrl_handler.lock = &dev->input_lock;
1362 dev->sd.ctrl_handler = &dev->ctrl_handler;
1363
1364 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1365 if (ret)
1366 gc0310_remove(client);
1367
1368 pr_info("%s E\n", __func__);
1369 return ret;
1370 out_free:
1371 v4l2_device_unregister_subdev(&dev->sd);
1372 kfree(dev);
1373 return ret;
1374 }
1375
1376 static const struct acpi_device_id gc0310_acpi_match[] = {
1377 {"XXGC0310"},
1378 {"INT0310"},
1379 {},
1380 };
1381 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match);
1382
1383 static struct i2c_driver gc0310_driver = {
1384 .driver = {
1385 .name = "gc0310",
1386 .acpi_match_table = gc0310_acpi_match,
1387 },
1388 .probe_new = gc0310_probe,
1389 .remove = gc0310_remove,
1390 };
1391 module_i2c_driver(gc0310_driver);
1392
1393 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
1394 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
1395 MODULE_LICENSE("GPL");
1396