1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for GalaxyCore GC2235 2M camera sensor.
4 *
5 * Copyright (c) 2014 Intel Corporation. All Rights Reserved.
6 */
7
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kmod.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/moduleparam.h>
21 #include <media/v4l2-device.h>
22 #include "../include/linux/atomisp_gmin_platform.h"
23 #include <linux/acpi.h>
24 #include <linux/io.h>
25
26 #include "gc2235.h"
27
28 /* i2c read/write stuff */
gc2235_read_reg(struct i2c_client * client,u16 data_length,u16 reg,u16 * val)29 static int gc2235_read_reg(struct i2c_client *client,
30 u16 data_length, u16 reg, u16 *val)
31 {
32 int err;
33 struct i2c_msg msg[2];
34 unsigned char data[6];
35
36 if (!client->adapter) {
37 dev_err(&client->dev, "%s error, no client->adapter\n",
38 __func__);
39 return -ENODEV;
40 }
41
42 if (data_length != GC2235_8BIT) {
43 dev_err(&client->dev, "%s error, invalid data length\n",
44 __func__);
45 return -EINVAL;
46 }
47
48 memset(msg, 0, sizeof(msg));
49
50 msg[0].addr = client->addr;
51 msg[0].flags = 0;
52 msg[0].len = 1;
53 msg[0].buf = data;
54
55 /* high byte goes out first */
56 data[0] = (u8)(reg & 0xff);
57
58 msg[1].addr = client->addr;
59 msg[1].len = data_length;
60 msg[1].flags = I2C_M_RD;
61 msg[1].buf = data;
62
63 err = i2c_transfer(client->adapter, msg, 2);
64 if (err != 2) {
65 if (err >= 0)
66 err = -EIO;
67 dev_err(&client->dev,
68 "read from offset 0x%x error %d", reg, err);
69 return err;
70 }
71
72 *val = 0;
73 /* high byte comes first */
74 if (data_length == GC2235_8BIT)
75 *val = (u8)data[0];
76
77 return 0;
78 }
79
gc2235_i2c_write(struct i2c_client * client,u16 len,u8 * data)80 static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data)
81 {
82 struct i2c_msg msg;
83 const int num_msg = 1;
84 int ret;
85
86 msg.addr = client->addr;
87 msg.flags = 0;
88 msg.len = len;
89 msg.buf = data;
90 ret = i2c_transfer(client->adapter, &msg, 1);
91
92 return ret == num_msg ? 0 : -EIO;
93 }
94
gc2235_write_reg(struct i2c_client * client,u16 data_length,u8 reg,u8 val)95 static int gc2235_write_reg(struct i2c_client *client, u16 data_length,
96 u8 reg, u8 val)
97 {
98 int ret;
99 unsigned char data[4] = {0};
100 const u16 len = data_length + sizeof(u8); /* 16-bit address + data */
101
102 if (data_length != GC2235_8BIT) {
103 dev_err(&client->dev,
104 "%s error, invalid data_length\n", __func__);
105 return -EINVAL;
106 }
107
108 /* high byte goes out first */
109 data[0] = reg;
110 data[1] = val;
111
112 ret = gc2235_i2c_write(client, len, data);
113 if (ret)
114 dev_err(&client->dev,
115 "write error: wrote 0x%x to offset 0x%x error %d",
116 val, reg, ret);
117
118 return ret;
119 }
120
__gc2235_flush_reg_array(struct i2c_client * client,struct gc2235_write_ctrl * ctrl)121 static int __gc2235_flush_reg_array(struct i2c_client *client,
122 struct gc2235_write_ctrl *ctrl)
123 {
124 u16 size;
125
126 if (ctrl->index == 0)
127 return 0;
128
129 size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
130 ctrl->index = 0;
131
132 return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer);
133 }
134
__gc2235_buf_reg_array(struct i2c_client * client,struct gc2235_write_ctrl * ctrl,const struct gc2235_reg * next)135 static int __gc2235_buf_reg_array(struct i2c_client *client,
136 struct gc2235_write_ctrl *ctrl,
137 const struct gc2235_reg *next)
138 {
139 int size;
140
141 if (next->type != GC2235_8BIT)
142 return -EINVAL;
143
144 size = 1;
145 ctrl->buffer.data[ctrl->index] = (u8)next->val;
146
147 /* When first item is added, we need to store its starting address */
148 if (ctrl->index == 0)
149 ctrl->buffer.addr = next->reg;
150
151 ctrl->index += size;
152
153 /*
154 * Buffer cannot guarantee free space for u32? Better flush it to avoid
155 * possible lack of memory for next item.
156 */
157 if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE)
158 return __gc2235_flush_reg_array(client, ctrl);
159
160 return 0;
161 }
162
__gc2235_write_reg_is_consecutive(struct i2c_client * client,struct gc2235_write_ctrl * ctrl,const struct gc2235_reg * next)163 static int __gc2235_write_reg_is_consecutive(struct i2c_client *client,
164 struct gc2235_write_ctrl *ctrl,
165 const struct gc2235_reg *next)
166 {
167 if (ctrl->index == 0)
168 return 1;
169
170 return ctrl->buffer.addr + ctrl->index == next->reg;
171 }
172
gc2235_write_reg_array(struct i2c_client * client,const struct gc2235_reg * reglist)173 static int gc2235_write_reg_array(struct i2c_client *client,
174 const struct gc2235_reg *reglist)
175 {
176 const struct gc2235_reg *next = reglist;
177 struct gc2235_write_ctrl ctrl;
178 int err;
179
180 ctrl.index = 0;
181 for (; next->type != GC2235_TOK_TERM; next++) {
182 switch (next->type & GC2235_TOK_MASK) {
183 case GC2235_TOK_DELAY:
184 err = __gc2235_flush_reg_array(client, &ctrl);
185 if (err)
186 return err;
187 msleep(next->val);
188 break;
189 default:
190 /*
191 * If next address is not consecutive, data needs to be
192 * flushed before proceed.
193 */
194 if (!__gc2235_write_reg_is_consecutive(client, &ctrl,
195 next)) {
196 err = __gc2235_flush_reg_array(client, &ctrl);
197 if (err)
198 return err;
199 }
200 err = __gc2235_buf_reg_array(client, &ctrl, next);
201 if (err) {
202 dev_err(&client->dev, "%s: write error, aborted\n",
203 __func__);
204 return err;
205 }
206 break;
207 }
208 }
209
210 return __gc2235_flush_reg_array(client, &ctrl);
211 }
212
__gc2235_set_exposure(struct v4l2_subdev * sd,int coarse_itg,int gain,int digitgain)213 static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
214 int gain, int digitgain)
215
216 {
217 struct i2c_client *client = v4l2_get_subdevdata(sd);
218 u16 coarse_integration = (u16)coarse_itg;
219 int ret = 0;
220 u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0;
221
222 expo_coarse_h = coarse_integration >> 8;
223 expo_coarse_l = coarse_integration & 0xff;
224
225 ret = gc2235_write_reg(client, GC2235_8BIT,
226 GC2235_EXPOSURE_H, expo_coarse_h);
227 ret = gc2235_write_reg(client, GC2235_8BIT,
228 GC2235_EXPOSURE_L, expo_coarse_l);
229
230 if (gain <= 0x58) {
231 gain_val = 0x40;
232 gain_val2 = 0x58;
233 } else if (gain < 256) {
234 gain_val = 0x40;
235 gain_val2 = gain;
236 } else {
237 gain_val2 = 64 * gain / 256;
238 gain_val = 0xff;
239 }
240
241 ret = gc2235_write_reg(client, GC2235_8BIT,
242 GC2235_GLOBAL_GAIN, (u8)gain_val);
243 ret = gc2235_write_reg(client, GC2235_8BIT,
244 GC2235_PRE_GAIN, (u8)gain_val2);
245
246 return ret;
247 }
248
gc2235_set_exposure(struct v4l2_subdev * sd,int exposure,int gain,int digitgain)249 static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure,
250 int gain, int digitgain)
251 {
252 struct gc2235_device *dev = to_gc2235_sensor(sd);
253 int ret;
254
255 mutex_lock(&dev->input_lock);
256 ret = __gc2235_set_exposure(sd, exposure, gain, digitgain);
257 mutex_unlock(&dev->input_lock);
258
259 return ret;
260 }
261
gc2235_s_exposure(struct v4l2_subdev * sd,struct atomisp_exposure * exposure)262 static long gc2235_s_exposure(struct v4l2_subdev *sd,
263 struct atomisp_exposure *exposure)
264 {
265 int exp = exposure->integration_time[0];
266 int gain = exposure->gain[0];
267 int digitgain = exposure->gain[1];
268
269 /* we should not accept the invalid value below. */
270 if (gain == 0) {
271 struct i2c_client *client = v4l2_get_subdevdata(sd);
272
273 v4l2_err(client, "%s: invalid value\n", __func__);
274 return -EINVAL;
275 }
276
277 return gc2235_set_exposure(sd, exp, gain, digitgain);
278 }
279
gc2235_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)280 static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
281 {
282 switch (cmd) {
283 case ATOMISP_IOC_S_EXPOSURE:
284 return gc2235_s_exposure(sd, arg);
285 default:
286 return -EINVAL;
287 }
288 return 0;
289 }
290
291 /*
292 * This returns the exposure time being used. This should only be used
293 * for filling in EXIF data, not for actual image processing.
294 */
gc2235_q_exposure(struct v4l2_subdev * sd,s32 * value)295 static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value)
296 {
297 struct i2c_client *client = v4l2_get_subdevdata(sd);
298 u16 reg_v, reg_v2;
299 int ret;
300
301 /* get exposure */
302 ret = gc2235_read_reg(client, GC2235_8BIT,
303 GC2235_EXPOSURE_L,
304 ®_v);
305 if (ret)
306 goto err;
307
308 ret = gc2235_read_reg(client, GC2235_8BIT,
309 GC2235_EXPOSURE_H,
310 ®_v2);
311 if (ret)
312 goto err;
313
314 reg_v += reg_v2 << 8;
315
316 *value = reg_v;
317 err:
318 return ret;
319 }
320
gc2235_g_volatile_ctrl(struct v4l2_ctrl * ctrl)321 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
322 {
323 struct gc2235_device *dev =
324 container_of(ctrl->handler, struct gc2235_device, ctrl_handler);
325 int ret = 0;
326
327 switch (ctrl->id) {
328 case V4L2_CID_EXPOSURE_ABSOLUTE:
329 ret = gc2235_q_exposure(&dev->sd, &ctrl->val);
330 break;
331 default:
332 ret = -EINVAL;
333 }
334
335 return ret;
336 }
337
338 static const struct v4l2_ctrl_ops ctrl_ops = {
339 .g_volatile_ctrl = gc2235_g_volatile_ctrl
340 };
341
342 static struct v4l2_ctrl_config gc2235_controls[] = {
343 {
344 .ops = &ctrl_ops,
345 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
346 .type = V4L2_CTRL_TYPE_INTEGER,
347 .name = "exposure",
348 .min = 0x0,
349 .max = 0xffff,
350 .step = 0x01,
351 .def = 0x00,
352 .flags = 0,
353 },
354 };
355
__gc2235_init(struct v4l2_subdev * sd)356 static int __gc2235_init(struct v4l2_subdev *sd)
357 {
358 struct i2c_client *client = v4l2_get_subdevdata(sd);
359
360 /* restore settings */
361 gc2235_res = gc2235_res_preview;
362 N_RES = N_RES_PREVIEW;
363
364 return gc2235_write_reg_array(client, gc2235_init_settings);
365 }
366
367 static int is_init;
368
power_ctrl(struct v4l2_subdev * sd,bool flag)369 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
370 {
371 int ret = -1;
372 struct gc2235_device *dev = to_gc2235_sensor(sd);
373
374 if (!dev || !dev->platform_data)
375 return -ENODEV;
376
377 if (flag) {
378 ret = dev->platform_data->v1p8_ctrl(sd, 1);
379 usleep_range(60, 90);
380 if (ret == 0)
381 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
382 } else {
383 ret = dev->platform_data->v1p8_ctrl(sd, 0);
384 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
385 }
386 return ret;
387 }
388
gpio_ctrl(struct v4l2_subdev * sd,bool flag)389 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
390 {
391 struct gc2235_device *dev = to_gc2235_sensor(sd);
392 int ret;
393
394 if (!dev || !dev->platform_data)
395 return -ENODEV;
396
397 ret = dev->platform_data->gpio1_ctrl(sd, !flag);
398 usleep_range(60, 90);
399 ret |= dev->platform_data->gpio0_ctrl(sd, flag);
400
401 return ret;
402 }
403
power_up(struct v4l2_subdev * sd)404 static int power_up(struct v4l2_subdev *sd)
405 {
406 struct gc2235_device *dev = to_gc2235_sensor(sd);
407 struct i2c_client *client = v4l2_get_subdevdata(sd);
408 int ret;
409
410 if (!dev->platform_data) {
411 dev_err(&client->dev,
412 "no camera_sensor_platform_data");
413 return -ENODEV;
414 }
415 /* power control */
416 ret = power_ctrl(sd, 1);
417 if (ret)
418 goto fail_power;
419
420 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
421 usleep_range(5000, 6000);
422
423 ret = dev->platform_data->flisclk_ctrl(sd, 1);
424 if (ret)
425 goto fail_clk;
426 usleep_range(5000, 6000);
427
428 /* gpio ctrl */
429 ret = gpio_ctrl(sd, 1);
430 if (ret) {
431 ret = gpio_ctrl(sd, 1);
432 if (ret)
433 goto fail_power;
434 }
435
436 msleep(5);
437 return 0;
438
439 fail_clk:
440 gpio_ctrl(sd, 0);
441 fail_power:
442 power_ctrl(sd, 0);
443 dev_err(&client->dev, "sensor power-up failed\n");
444
445 return ret;
446 }
447
power_down(struct v4l2_subdev * sd)448 static int power_down(struct v4l2_subdev *sd)
449 {
450 struct gc2235_device *dev = to_gc2235_sensor(sd);
451 struct i2c_client *client = v4l2_get_subdevdata(sd);
452 int ret = 0;
453
454 if (!dev->platform_data) {
455 dev_err(&client->dev,
456 "no camera_sensor_platform_data");
457 return -ENODEV;
458 }
459 /* gpio ctrl */
460 ret = gpio_ctrl(sd, 0);
461 if (ret) {
462 ret = gpio_ctrl(sd, 0);
463 if (ret)
464 dev_err(&client->dev, "gpio failed 2\n");
465 }
466
467 ret = dev->platform_data->flisclk_ctrl(sd, 0);
468 if (ret)
469 dev_err(&client->dev, "flisclk failed\n");
470
471 /* power control */
472 ret = power_ctrl(sd, 0);
473 if (ret)
474 dev_err(&client->dev, "vprog failed.\n");
475
476 return ret;
477 }
478
gc2235_s_power(struct v4l2_subdev * sd,int on)479 static int gc2235_s_power(struct v4l2_subdev *sd, int on)
480 {
481 int ret;
482
483 if (on == 0) {
484 ret = power_down(sd);
485 } else {
486 ret = power_up(sd);
487 if (!ret)
488 ret = __gc2235_init(sd);
489 is_init = 1;
490 }
491 return ret;
492 }
493
startup(struct v4l2_subdev * sd)494 static int startup(struct v4l2_subdev *sd)
495 {
496 struct gc2235_device *dev = to_gc2235_sensor(sd);
497 struct i2c_client *client = v4l2_get_subdevdata(sd);
498 int ret = 0;
499
500 if (is_init == 0) {
501 /*
502 * force gc2235 to do a reset in res change, otherwise it
503 * can not output normal after switching res. and it is not
504 * necessary for first time run up after power on, for the sack
505 * of performance
506 */
507 power_down(sd);
508 power_up(sd);
509 gc2235_write_reg_array(client, gc2235_init_settings);
510 }
511
512 ret = gc2235_write_reg_array(client, dev->res->regs);
513 if (ret) {
514 dev_err(&client->dev, "gc2235 write register err.\n");
515 return ret;
516 }
517 is_init = 0;
518
519 return ret;
520 }
521
gc2235_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)522 static int gc2235_set_fmt(struct v4l2_subdev *sd,
523 struct v4l2_subdev_state *sd_state,
524 struct v4l2_subdev_format *format)
525 {
526 struct v4l2_mbus_framefmt *fmt = &format->format;
527 struct gc2235_device *dev = to_gc2235_sensor(sd);
528 struct i2c_client *client = v4l2_get_subdevdata(sd);
529 struct camera_mipi_info *gc2235_info = NULL;
530 struct gc2235_resolution *res;
531 int ret = 0;
532
533 gc2235_info = v4l2_get_subdev_hostdata(sd);
534 if (!gc2235_info)
535 return -EINVAL;
536 if (format->pad)
537 return -EINVAL;
538 if (!fmt)
539 return -EINVAL;
540
541 mutex_lock(&dev->input_lock);
542 res = v4l2_find_nearest_size(gc2235_res_preview,
543 ARRAY_SIZE(gc2235_res_preview), width,
544 height, fmt->width, fmt->height);
545 if (!res)
546 res = &gc2235_res_preview[N_RES - 1];
547
548 fmt->width = res->width;
549 fmt->height = res->height;
550 dev->res = res;
551
552 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
553 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
554 *v4l2_subdev_state_get_format(sd_state, 0) = *fmt;
555 mutex_unlock(&dev->input_lock);
556 return 0;
557 }
558
559 ret = startup(sd);
560 if (ret) {
561 dev_err(&client->dev, "gc2235 startup err\n");
562 goto err;
563 }
564
565 err:
566 mutex_unlock(&dev->input_lock);
567 return ret;
568 }
569
gc2235_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)570 static int gc2235_get_fmt(struct v4l2_subdev *sd,
571 struct v4l2_subdev_state *sd_state,
572 struct v4l2_subdev_format *format)
573 {
574 struct v4l2_mbus_framefmt *fmt = &format->format;
575 struct gc2235_device *dev = to_gc2235_sensor(sd);
576
577 if (format->pad)
578 return -EINVAL;
579
580 if (!fmt)
581 return -EINVAL;
582
583 fmt->width = dev->res->width;
584 fmt->height = dev->res->height;
585 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
586
587 return 0;
588 }
589
gc2235_detect(struct i2c_client * client)590 static int gc2235_detect(struct i2c_client *client)
591 {
592 struct i2c_adapter *adapter = client->adapter;
593 u16 high = 0, low = 0;
594 u16 id;
595
596 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
597 return -ENODEV;
598
599 gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_H, &high);
600 gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_L, &low);
601 id = ((high << 8) | low);
602
603 if (id != GC2235_ID) {
604 dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
605 return -ENODEV;
606 }
607
608 dev_info(&client->dev, "detect gc2235 success\n");
609 return 0;
610 }
611
gc2235_s_stream(struct v4l2_subdev * sd,int enable)612 static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
613 {
614 struct gc2235_device *dev = to_gc2235_sensor(sd);
615 struct i2c_client *client = v4l2_get_subdevdata(sd);
616 int ret;
617
618 mutex_lock(&dev->input_lock);
619
620 if (enable)
621 ret = gc2235_write_reg_array(client, gc2235_stream_on);
622 else
623 ret = gc2235_write_reg_array(client, gc2235_stream_off);
624
625 mutex_unlock(&dev->input_lock);
626 return ret;
627 }
628
gc2235_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)629 static int gc2235_s_config(struct v4l2_subdev *sd,
630 int irq, void *platform_data)
631 {
632 struct gc2235_device *dev = to_gc2235_sensor(sd);
633 struct i2c_client *client = v4l2_get_subdevdata(sd);
634 int ret = 0;
635
636 if (!platform_data)
637 return -ENODEV;
638
639 dev->platform_data =
640 (struct camera_sensor_platform_data *)platform_data;
641
642 mutex_lock(&dev->input_lock);
643 /*
644 * power off the module, then power on it in future
645 * as first power on by board may not fulfill the
646 * power on sequqence needed by the module
647 */
648 ret = power_down(sd);
649 if (ret) {
650 dev_err(&client->dev, "gc2235 power-off err.\n");
651 goto fail_power_off;
652 }
653
654 ret = power_up(sd);
655 if (ret) {
656 dev_err(&client->dev, "gc2235 power-up err.\n");
657 goto fail_power_on;
658 }
659
660 ret = dev->platform_data->csi_cfg(sd, 1);
661 if (ret)
662 goto fail_csi_cfg;
663
664 /* config & detect sensor */
665 ret = gc2235_detect(client);
666 if (ret) {
667 dev_err(&client->dev, "gc2235_detect err s_config.\n");
668 goto fail_csi_cfg;
669 }
670
671 /* turn off sensor, after probed */
672 ret = power_down(sd);
673 if (ret) {
674 dev_err(&client->dev, "gc2235 power-off err.\n");
675 goto fail_csi_cfg;
676 }
677 mutex_unlock(&dev->input_lock);
678
679 return 0;
680
681 fail_csi_cfg:
682 dev->platform_data->csi_cfg(sd, 0);
683 fail_power_on:
684 power_down(sd);
685 dev_err(&client->dev, "sensor power-gating failed\n");
686 fail_power_off:
687 mutex_unlock(&dev->input_lock);
688 return ret;
689 }
690
gc2235_get_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_interval * interval)691 static int gc2235_get_frame_interval(struct v4l2_subdev *sd,
692 struct v4l2_subdev_state *sd_state,
693 struct v4l2_subdev_frame_interval *interval)
694 {
695 struct gc2235_device *dev = to_gc2235_sensor(sd);
696
697 /*
698 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
699 * subdev active state API.
700 */
701 if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE)
702 return -EINVAL;
703
704 interval->interval.numerator = 1;
705 interval->interval.denominator = dev->res->fps;
706
707 return 0;
708 }
709
gc2235_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)710 static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
711 struct v4l2_subdev_state *sd_state,
712 struct v4l2_subdev_mbus_code_enum *code)
713 {
714 if (code->index >= MAX_FMTS)
715 return -EINVAL;
716
717 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
718 return 0;
719 }
720
gc2235_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)721 static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
722 struct v4l2_subdev_state *sd_state,
723 struct v4l2_subdev_frame_size_enum *fse)
724 {
725 int index = fse->index;
726
727 if (index >= N_RES)
728 return -EINVAL;
729
730 fse->min_width = gc2235_res[index].width;
731 fse->min_height = gc2235_res[index].height;
732 fse->max_width = gc2235_res[index].width;
733 fse->max_height = gc2235_res[index].height;
734
735 return 0;
736 }
737
gc2235_g_skip_frames(struct v4l2_subdev * sd,u32 * frames)738 static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
739 {
740 struct gc2235_device *dev = to_gc2235_sensor(sd);
741
742 mutex_lock(&dev->input_lock);
743 *frames = dev->res->skip_frames;
744 mutex_unlock(&dev->input_lock);
745
746 return 0;
747 }
748
749 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
750 .g_skip_frames = gc2235_g_skip_frames,
751 };
752
753 static const struct v4l2_subdev_video_ops gc2235_video_ops = {
754 .s_stream = gc2235_s_stream,
755 };
756
757 static const struct v4l2_subdev_core_ops gc2235_core_ops = {
758 .s_power = gc2235_s_power,
759 .ioctl = gc2235_ioctl,
760 };
761
762 static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
763 .enum_mbus_code = gc2235_enum_mbus_code,
764 .enum_frame_size = gc2235_enum_frame_size,
765 .get_fmt = gc2235_get_fmt,
766 .set_fmt = gc2235_set_fmt,
767 .get_frame_interval = gc2235_get_frame_interval,
768 };
769
770 static const struct v4l2_subdev_ops gc2235_ops = {
771 .core = &gc2235_core_ops,
772 .video = &gc2235_video_ops,
773 .pad = &gc2235_pad_ops,
774 .sensor = &gc2235_sensor_ops,
775 };
776
gc2235_remove(struct i2c_client * client)777 static void gc2235_remove(struct i2c_client *client)
778 {
779 struct v4l2_subdev *sd = i2c_get_clientdata(client);
780 struct gc2235_device *dev = to_gc2235_sensor(sd);
781
782 dev_dbg(&client->dev, "gc2235_remove...\n");
783
784 dev->platform_data->csi_cfg(sd, 0);
785
786 v4l2_device_unregister_subdev(sd);
787 media_entity_cleanup(&dev->sd.entity);
788 v4l2_ctrl_handler_free(&dev->ctrl_handler);
789 kfree(dev);
790 }
791
gc2235_probe(struct i2c_client * client)792 static int gc2235_probe(struct i2c_client *client)
793 {
794 struct gc2235_device *dev;
795 void *gcpdev;
796 int ret;
797 unsigned int i;
798
799 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
800 if (!dev)
801 return -ENOMEM;
802
803 mutex_init(&dev->input_lock);
804
805 dev->res = &gc2235_res_preview[0];
806 v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops);
807
808 gcpdev = gmin_camera_platform_data(&dev->sd,
809 ATOMISP_INPUT_FORMAT_RAW_10,
810 atomisp_bayer_order_grbg);
811
812 ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
813 if (ret)
814 goto out_free;
815
816 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
817 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
818 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
819 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
820 ret =
821 v4l2_ctrl_handler_init(&dev->ctrl_handler,
822 ARRAY_SIZE(gc2235_controls));
823 if (ret) {
824 gc2235_remove(client);
825 return ret;
826 }
827
828 for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
829 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
830 NULL);
831
832 if (dev->ctrl_handler.error) {
833 gc2235_remove(client);
834 return dev->ctrl_handler.error;
835 }
836
837 /* Use same lock for controls as for everything else. */
838 dev->ctrl_handler.lock = &dev->input_lock;
839 dev->sd.ctrl_handler = &dev->ctrl_handler;
840
841 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
842 if (ret)
843 gc2235_remove(client);
844
845 return atomisp_register_i2c_module(&dev->sd, gcpdev);
846
847 out_free:
848 v4l2_device_unregister_subdev(&dev->sd);
849 kfree(dev);
850
851 return ret;
852 }
853
854 static const struct acpi_device_id gc2235_acpi_match[] = {
855 { "INT33F8" },
856 {},
857 };
858 MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
859
860 static struct i2c_driver gc2235_driver = {
861 .driver = {
862 .name = "gc2235",
863 .acpi_match_table = gc2235_acpi_match,
864 },
865 .probe = gc2235_probe,
866 .remove = gc2235_remove,
867 };
868 module_i2c_driver(gc2235_driver);
869
870 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
871 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
872 MODULE_LICENSE("GPL");
873