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