]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/media/i2c/noon010pc30.c
Merge tag 'firewire-update' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[mirror_ubuntu-jammy-kernel.git] / drivers / media / i2c / noon010pc30.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for SiliconFile NOON010PC30 CIF (1/11") Image Sensor with ISP
4 *
5 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
6 * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
7 *
8 * Initial register configuration based on a driver authored by
9 * HeungJun Kim <riverful.kim@samsung.com>.
10 */
11
12 #include <linux/delay.h>
13 #include <linux/gpio.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/regulator/consumer.h>
17 #include <media/i2c/noon010pc30.h>
18 #include <linux/videodev2.h>
19 #include <linux/module.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-mediabus.h>
23 #include <media/v4l2-subdev.h>
24
25 static int debug;
26 module_param(debug, int, 0644);
27 MODULE_PARM_DESC(debug, "Enable module debug trace. Set to 1 to enable.");
28
29 #define MODULE_NAME "NOON010PC30"
30
31 /*
32 * Register offsets within a page
33 * b15..b8 - page id, b7..b0 - register address
34 */
35 #define POWER_CTRL_REG 0x0001
36 #define PAGEMODE_REG 0x03
37 #define DEVICE_ID_REG 0x0004
38 #define NOON010PC30_ID 0x86
39 #define VDO_CTL_REG(n) (0x0010 + (n))
40 #define SYNC_CTL_REG 0x0012
41 /* Window size and position */
42 #define WIN_ROWH_REG 0x0013
43 #define WIN_ROWL_REG 0x0014
44 #define WIN_COLH_REG 0x0015
45 #define WIN_COLL_REG 0x0016
46 #define WIN_HEIGHTH_REG 0x0017
47 #define WIN_HEIGHTL_REG 0x0018
48 #define WIN_WIDTHH_REG 0x0019
49 #define WIN_WIDTHL_REG 0x001A
50 #define HBLANKH_REG 0x001B
51 #define HBLANKL_REG 0x001C
52 #define VSYNCH_REG 0x001D
53 #define VSYNCL_REG 0x001E
54 /* VSYNC control */
55 #define VS_CTL_REG(n) (0x00A1 + (n))
56 /* page 1 */
57 #define ISP_CTL_REG(n) (0x0110 + (n))
58 #define YOFS_REG 0x0119
59 #define DARK_YOFS_REG 0x011A
60 #define SAT_CTL_REG 0x0120
61 #define BSAT_REG 0x0121
62 #define RSAT_REG 0x0122
63 /* Color correction */
64 #define CMC_CTL_REG 0x0130
65 #define CMC_OFSGH_REG 0x0133
66 #define CMC_OFSGL_REG 0x0135
67 #define CMC_SIGN_REG 0x0136
68 #define CMC_GOFS_REG 0x0137
69 #define CMC_COEF_REG(n) (0x0138 + (n))
70 #define CMC_OFS_REG(n) (0x0141 + (n))
71 /* Gamma correction */
72 #define GMA_CTL_REG 0x0160
73 #define GMA_COEF_REG(n) (0x0161 + (n))
74 /* Lens Shading */
75 #define LENS_CTRL_REG 0x01D0
76 #define LENS_XCEN_REG 0x01D1
77 #define LENS_YCEN_REG 0x01D2
78 #define LENS_RC_REG 0x01D3
79 #define LENS_GC_REG 0x01D4
80 #define LENS_BC_REG 0x01D5
81 #define L_AGON_REG 0x01D6
82 #define L_AGOFF_REG 0x01D7
83 /* Page 3 - Auto Exposure */
84 #define AE_CTL_REG(n) (0x0310 + (n))
85 #define AE_CTL9_REG 0x032C
86 #define AE_CTL10_REG 0x032D
87 #define AE_YLVL_REG 0x031C
88 #define AE_YTH_REG(n) (0x031D + (n))
89 #define AE_WGT_REG 0x0326
90 #define EXP_TIMEH_REG 0x0333
91 #define EXP_TIMEM_REG 0x0334
92 #define EXP_TIMEL_REG 0x0335
93 #define EXP_MMINH_REG 0x0336
94 #define EXP_MMINL_REG 0x0337
95 #define EXP_MMAXH_REG 0x0338
96 #define EXP_MMAXM_REG 0x0339
97 #define EXP_MMAXL_REG 0x033A
98 /* Page 4 - Auto White Balance */
99 #define AWB_CTL_REG(n) (0x0410 + (n))
100 #define AWB_ENABE 0x80
101 #define AWB_WGHT_REG 0x0419
102 #define BGAIN_PAR_REG(n) (0x044F + (n))
103 /* Manual white balance, when AWB_CTL2[0]=1 */
104 #define MWB_RGAIN_REG 0x0466
105 #define MWB_BGAIN_REG 0x0467
106
107 /* The token to mark an array end */
108 #define REG_TERM 0xFFFF
109
110 struct noon010_format {
111 u32 code;
112 enum v4l2_colorspace colorspace;
113 u16 ispctl1_reg;
114 };
115
116 struct noon010_frmsize {
117 u16 width;
118 u16 height;
119 int vid_ctl1;
120 };
121
122 static const char * const noon010_supply_name[] = {
123 "vdd_core", "vddio", "vdda"
124 };
125
126 #define NOON010_NUM_SUPPLIES ARRAY_SIZE(noon010_supply_name)
127
128 struct noon010_info {
129 struct v4l2_subdev sd;
130 struct media_pad pad;
131 struct v4l2_ctrl_handler hdl;
132 struct regulator_bulk_data supply[NOON010_NUM_SUPPLIES];
133 u32 gpio_nreset;
134 u32 gpio_nstby;
135
136 /* Protects the struct members below */
137 struct mutex lock;
138
139 const struct noon010_format *curr_fmt;
140 const struct noon010_frmsize *curr_win;
141 unsigned int apply_new_cfg:1;
142 unsigned int streaming:1;
143 unsigned int hflip:1;
144 unsigned int vflip:1;
145 unsigned int power:1;
146 u8 i2c_reg_page;
147 };
148
149 struct i2c_regval {
150 u16 addr;
151 u16 val;
152 };
153
154 /* Supported resolutions. */
155 static const struct noon010_frmsize noon010_sizes[] = {
156 {
157 .width = 352,
158 .height = 288,
159 .vid_ctl1 = 0,
160 }, {
161 .width = 176,
162 .height = 144,
163 .vid_ctl1 = 0x10,
164 }, {
165 .width = 88,
166 .height = 72,
167 .vid_ctl1 = 0x20,
168 },
169 };
170
171 /* Supported pixel formats. */
172 static const struct noon010_format noon010_formats[] = {
173 {
174 .code = MEDIA_BUS_FMT_YUYV8_2X8,
175 .colorspace = V4L2_COLORSPACE_JPEG,
176 .ispctl1_reg = 0x03,
177 }, {
178 .code = MEDIA_BUS_FMT_YVYU8_2X8,
179 .colorspace = V4L2_COLORSPACE_JPEG,
180 .ispctl1_reg = 0x02,
181 }, {
182 .code = MEDIA_BUS_FMT_VYUY8_2X8,
183 .colorspace = V4L2_COLORSPACE_JPEG,
184 .ispctl1_reg = 0,
185 }, {
186 .code = MEDIA_BUS_FMT_UYVY8_2X8,
187 .colorspace = V4L2_COLORSPACE_JPEG,
188 .ispctl1_reg = 0x01,
189 }, {
190 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
191 .colorspace = V4L2_COLORSPACE_JPEG,
192 .ispctl1_reg = 0x40,
193 },
194 };
195
196 static const struct i2c_regval noon010_base_regs[] = {
197 { WIN_COLL_REG, 0x06 }, { HBLANKL_REG, 0x7C },
198 /* Color corection and saturation */
199 { ISP_CTL_REG(0), 0x30 }, { ISP_CTL_REG(2), 0x30 },
200 { YOFS_REG, 0x80 }, { DARK_YOFS_REG, 0x04 },
201 { SAT_CTL_REG, 0x1F }, { BSAT_REG, 0x90 },
202 { CMC_CTL_REG, 0x0F }, { CMC_OFSGH_REG, 0x3C },
203 { CMC_OFSGL_REG, 0x2C }, { CMC_SIGN_REG, 0x3F },
204 { CMC_COEF_REG(0), 0x79 }, { CMC_OFS_REG(0), 0x00 },
205 { CMC_COEF_REG(1), 0x39 }, { CMC_OFS_REG(1), 0x00 },
206 { CMC_COEF_REG(2), 0x00 }, { CMC_OFS_REG(2), 0x00 },
207 { CMC_COEF_REG(3), 0x11 }, { CMC_OFS_REG(3), 0x8B },
208 { CMC_COEF_REG(4), 0x65 }, { CMC_OFS_REG(4), 0x07 },
209 { CMC_COEF_REG(5), 0x14 }, { CMC_OFS_REG(5), 0x04 },
210 { CMC_COEF_REG(6), 0x01 }, { CMC_OFS_REG(6), 0x9C },
211 { CMC_COEF_REG(7), 0x33 }, { CMC_OFS_REG(7), 0x89 },
212 { CMC_COEF_REG(8), 0x74 }, { CMC_OFS_REG(8), 0x25 },
213 /* Automatic white balance */
214 { AWB_CTL_REG(0), 0x78 }, { AWB_CTL_REG(1), 0x2E },
215 { AWB_CTL_REG(2), 0x20 }, { AWB_CTL_REG(3), 0x85 },
216 /* Auto exposure */
217 { AE_CTL_REG(0), 0xDC }, { AE_CTL_REG(1), 0x81 },
218 { AE_CTL_REG(2), 0x30 }, { AE_CTL_REG(3), 0xA5 },
219 { AE_CTL_REG(4), 0x40 }, { AE_CTL_REG(5), 0x51 },
220 { AE_CTL_REG(6), 0x33 }, { AE_CTL_REG(7), 0x7E },
221 { AE_CTL9_REG, 0x00 }, { AE_CTL10_REG, 0x02 },
222 { AE_YLVL_REG, 0x44 }, { AE_YTH_REG(0), 0x34 },
223 { AE_YTH_REG(1), 0x30 }, { AE_WGT_REG, 0xD5 },
224 /* Lens shading compensation */
225 { LENS_CTRL_REG, 0x01 }, { LENS_XCEN_REG, 0x80 },
226 { LENS_YCEN_REG, 0x70 }, { LENS_RC_REG, 0x53 },
227 { LENS_GC_REG, 0x40 }, { LENS_BC_REG, 0x3E },
228 { REG_TERM, 0 },
229 };
230
231 static inline struct noon010_info *to_noon010(struct v4l2_subdev *sd)
232 {
233 return container_of(sd, struct noon010_info, sd);
234 }
235
236 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
237 {
238 return &container_of(ctrl->handler, struct noon010_info, hdl)->sd;
239 }
240
241 static inline int set_i2c_page(struct noon010_info *info,
242 struct i2c_client *client, unsigned int reg)
243 {
244 u32 page = reg >> 8 & 0xFF;
245 int ret = 0;
246
247 if (info->i2c_reg_page != page && (reg & 0xFF) != 0x03) {
248 ret = i2c_smbus_write_byte_data(client, PAGEMODE_REG, page);
249 if (!ret)
250 info->i2c_reg_page = page;
251 }
252 return ret;
253 }
254
255 static int cam_i2c_read(struct v4l2_subdev *sd, u32 reg_addr)
256 {
257 struct i2c_client *client = v4l2_get_subdevdata(sd);
258 struct noon010_info *info = to_noon010(sd);
259 int ret = set_i2c_page(info, client, reg_addr);
260
261 if (ret)
262 return ret;
263 return i2c_smbus_read_byte_data(client, reg_addr & 0xFF);
264 }
265
266 static int cam_i2c_write(struct v4l2_subdev *sd, u32 reg_addr, u32 val)
267 {
268 struct i2c_client *client = v4l2_get_subdevdata(sd);
269 struct noon010_info *info = to_noon010(sd);
270 int ret = set_i2c_page(info, client, reg_addr);
271
272 if (ret)
273 return ret;
274 return i2c_smbus_write_byte_data(client, reg_addr & 0xFF, val);
275 }
276
277 static inline int noon010_bulk_write_reg(struct v4l2_subdev *sd,
278 const struct i2c_regval *msg)
279 {
280 while (msg->addr != REG_TERM) {
281 int ret = cam_i2c_write(sd, msg->addr, msg->val);
282
283 if (ret)
284 return ret;
285 msg++;
286 }
287 return 0;
288 }
289
290 /* Device reset and sleep mode control */
291 static int noon010_power_ctrl(struct v4l2_subdev *sd, bool reset, bool sleep)
292 {
293 struct noon010_info *info = to_noon010(sd);
294 u8 reg = sleep ? 0xF1 : 0xF0;
295 int ret = 0;
296
297 if (reset) {
298 ret = cam_i2c_write(sd, POWER_CTRL_REG, reg | 0x02);
299 udelay(20);
300 }
301 if (!ret) {
302 ret = cam_i2c_write(sd, POWER_CTRL_REG, reg);
303 if (reset && !ret)
304 info->i2c_reg_page = -1;
305 }
306 return ret;
307 }
308
309 /* Automatic white balance control */
310 static int noon010_enable_autowhitebalance(struct v4l2_subdev *sd, int on)
311 {
312 int ret;
313
314 ret = cam_i2c_write(sd, AWB_CTL_REG(1), on ? 0x2E : 0x2F);
315 if (!ret)
316 ret = cam_i2c_write(sd, AWB_CTL_REG(0), on ? 0xFB : 0x7B);
317 return ret;
318 }
319
320 /* Called with struct noon010_info.lock mutex held */
321 static int noon010_set_flip(struct v4l2_subdev *sd, int hflip, int vflip)
322 {
323 struct noon010_info *info = to_noon010(sd);
324 int reg, ret;
325
326 reg = cam_i2c_read(sd, VDO_CTL_REG(1));
327 if (reg < 0)
328 return reg;
329
330 reg &= 0x7C;
331 if (hflip)
332 reg |= 0x01;
333 if (vflip)
334 reg |= 0x02;
335
336 ret = cam_i2c_write(sd, VDO_CTL_REG(1), reg | 0x80);
337 if (!ret) {
338 info->hflip = hflip;
339 info->vflip = vflip;
340 }
341 return ret;
342 }
343
344 /* Configure resolution and color format */
345 static int noon010_set_params(struct v4l2_subdev *sd)
346 {
347 struct noon010_info *info = to_noon010(sd);
348
349 int ret = cam_i2c_write(sd, VDO_CTL_REG(0),
350 info->curr_win->vid_ctl1);
351 if (ret)
352 return ret;
353 return cam_i2c_write(sd, ISP_CTL_REG(0),
354 info->curr_fmt->ispctl1_reg);
355 }
356
357 /* Find nearest matching image pixel size. */
358 static int noon010_try_frame_size(struct v4l2_mbus_framefmt *mf,
359 const struct noon010_frmsize **size)
360 {
361 unsigned int min_err = ~0;
362 int i = ARRAY_SIZE(noon010_sizes);
363 const struct noon010_frmsize *fsize = &noon010_sizes[0],
364 *match = NULL;
365
366 while (i--) {
367 int err = abs(fsize->width - mf->width)
368 + abs(fsize->height - mf->height);
369
370 if (err < min_err) {
371 min_err = err;
372 match = fsize;
373 }
374 fsize++;
375 }
376 if (match) {
377 mf->width = match->width;
378 mf->height = match->height;
379 if (size)
380 *size = match;
381 return 0;
382 }
383 return -EINVAL;
384 }
385
386 /* Called with info.lock mutex held */
387 static int power_enable(struct noon010_info *info)
388 {
389 int ret;
390
391 if (info->power) {
392 v4l2_info(&info->sd, "%s: sensor is already on\n", __func__);
393 return 0;
394 }
395
396 if (gpio_is_valid(info->gpio_nstby))
397 gpio_set_value(info->gpio_nstby, 0);
398
399 if (gpio_is_valid(info->gpio_nreset))
400 gpio_set_value(info->gpio_nreset, 0);
401
402 ret = regulator_bulk_enable(NOON010_NUM_SUPPLIES, info->supply);
403 if (ret)
404 return ret;
405
406 if (gpio_is_valid(info->gpio_nreset)) {
407 msleep(50);
408 gpio_set_value(info->gpio_nreset, 1);
409 }
410 if (gpio_is_valid(info->gpio_nstby)) {
411 udelay(1000);
412 gpio_set_value(info->gpio_nstby, 1);
413 }
414 if (gpio_is_valid(info->gpio_nreset)) {
415 udelay(1000);
416 gpio_set_value(info->gpio_nreset, 0);
417 msleep(100);
418 gpio_set_value(info->gpio_nreset, 1);
419 msleep(20);
420 }
421 info->power = 1;
422
423 v4l2_dbg(1, debug, &info->sd, "%s: sensor is on\n", __func__);
424 return 0;
425 }
426
427 /* Called with info.lock mutex held */
428 static int power_disable(struct noon010_info *info)
429 {
430 int ret;
431
432 if (!info->power) {
433 v4l2_info(&info->sd, "%s: sensor is already off\n", __func__);
434 return 0;
435 }
436
437 ret = regulator_bulk_disable(NOON010_NUM_SUPPLIES, info->supply);
438 if (ret)
439 return ret;
440
441 if (gpio_is_valid(info->gpio_nstby))
442 gpio_set_value(info->gpio_nstby, 0);
443
444 if (gpio_is_valid(info->gpio_nreset))
445 gpio_set_value(info->gpio_nreset, 0);
446
447 info->power = 0;
448
449 v4l2_dbg(1, debug, &info->sd, "%s: sensor is off\n", __func__);
450
451 return 0;
452 }
453
454 static int noon010_s_ctrl(struct v4l2_ctrl *ctrl)
455 {
456 struct v4l2_subdev *sd = to_sd(ctrl);
457 struct noon010_info *info = to_noon010(sd);
458 int ret = 0;
459
460 v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
461 __func__, ctrl->id, ctrl->val);
462
463 mutex_lock(&info->lock);
464 /*
465 * If the device is not powered up by the host driver do
466 * not apply any controls to H/W at this time. Instead
467 * the controls will be restored right after power-up.
468 */
469 if (!info->power)
470 goto unlock;
471
472 switch (ctrl->id) {
473 case V4L2_CID_AUTO_WHITE_BALANCE:
474 ret = noon010_enable_autowhitebalance(sd, ctrl->val);
475 break;
476 case V4L2_CID_BLUE_BALANCE:
477 ret = cam_i2c_write(sd, MWB_BGAIN_REG, ctrl->val);
478 break;
479 case V4L2_CID_RED_BALANCE:
480 ret = cam_i2c_write(sd, MWB_RGAIN_REG, ctrl->val);
481 break;
482 default:
483 ret = -EINVAL;
484 }
485 unlock:
486 mutex_unlock(&info->lock);
487 return ret;
488 }
489
490 static int noon010_enum_mbus_code(struct v4l2_subdev *sd,
491 struct v4l2_subdev_state *sd_state,
492 struct v4l2_subdev_mbus_code_enum *code)
493 {
494 if (code->index >= ARRAY_SIZE(noon010_formats))
495 return -EINVAL;
496
497 code->code = noon010_formats[code->index].code;
498 return 0;
499 }
500
501 static int noon010_get_fmt(struct v4l2_subdev *sd,
502 struct v4l2_subdev_state *sd_state,
503 struct v4l2_subdev_format *fmt)
504 {
505 struct noon010_info *info = to_noon010(sd);
506 struct v4l2_mbus_framefmt *mf;
507
508 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
509 if (sd_state) {
510 mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
511 fmt->format = *mf;
512 }
513 return 0;
514 }
515 mf = &fmt->format;
516
517 mutex_lock(&info->lock);
518 mf->width = info->curr_win->width;
519 mf->height = info->curr_win->height;
520 mf->code = info->curr_fmt->code;
521 mf->colorspace = info->curr_fmt->colorspace;
522 mf->field = V4L2_FIELD_NONE;
523
524 mutex_unlock(&info->lock);
525 return 0;
526 }
527
528 /* Return nearest media bus frame format. */
529 static const struct noon010_format *noon010_try_fmt(struct v4l2_subdev *sd,
530 struct v4l2_mbus_framefmt *mf)
531 {
532 int i = ARRAY_SIZE(noon010_formats);
533
534 while (--i)
535 if (mf->code == noon010_formats[i].code)
536 break;
537 mf->code = noon010_formats[i].code;
538
539 return &noon010_formats[i];
540 }
541
542 static int noon010_set_fmt(struct v4l2_subdev *sd,
543 struct v4l2_subdev_state *sd_state,
544 struct v4l2_subdev_format *fmt)
545 {
546 struct noon010_info *info = to_noon010(sd);
547 const struct noon010_frmsize *size = NULL;
548 const struct noon010_format *nf;
549 struct v4l2_mbus_framefmt *mf;
550 int ret = 0;
551
552 nf = noon010_try_fmt(sd, &fmt->format);
553 noon010_try_frame_size(&fmt->format, &size);
554 fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
555 fmt->format.field = V4L2_FIELD_NONE;
556
557 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
558 if (sd_state) {
559 mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
560 *mf = fmt->format;
561 }
562 return 0;
563 }
564 mutex_lock(&info->lock);
565 if (!info->streaming) {
566 info->apply_new_cfg = 1;
567 info->curr_fmt = nf;
568 info->curr_win = size;
569 } else {
570 ret = -EBUSY;
571 }
572 mutex_unlock(&info->lock);
573 return ret;
574 }
575
576 /* Called with struct noon010_info.lock mutex held */
577 static int noon010_base_config(struct v4l2_subdev *sd)
578 {
579 int ret = noon010_bulk_write_reg(sd, noon010_base_regs);
580 if (!ret)
581 ret = noon010_set_params(sd);
582 if (!ret)
583 ret = noon010_set_flip(sd, 1, 0);
584
585 return ret;
586 }
587
588 static int noon010_s_power(struct v4l2_subdev *sd, int on)
589 {
590 struct noon010_info *info = to_noon010(sd);
591 int ret;
592
593 mutex_lock(&info->lock);
594 if (on) {
595 ret = power_enable(info);
596 if (!ret)
597 ret = noon010_base_config(sd);
598 } else {
599 noon010_power_ctrl(sd, false, true);
600 ret = power_disable(info);
601 }
602 mutex_unlock(&info->lock);
603
604 /* Restore the controls state */
605 if (!ret && on)
606 ret = v4l2_ctrl_handler_setup(&info->hdl);
607
608 return ret;
609 }
610
611 static int noon010_s_stream(struct v4l2_subdev *sd, int on)
612 {
613 struct noon010_info *info = to_noon010(sd);
614 int ret = 0;
615
616 mutex_lock(&info->lock);
617 if (!info->streaming != !on) {
618 ret = noon010_power_ctrl(sd, false, !on);
619 if (!ret)
620 info->streaming = on;
621 }
622 if (!ret && on && info->apply_new_cfg) {
623 ret = noon010_set_params(sd);
624 if (!ret)
625 info->apply_new_cfg = 0;
626 }
627 mutex_unlock(&info->lock);
628 return ret;
629 }
630
631 static int noon010_log_status(struct v4l2_subdev *sd)
632 {
633 struct noon010_info *info = to_noon010(sd);
634
635 v4l2_ctrl_handler_log_status(&info->hdl, sd->name);
636 return 0;
637 }
638
639 static int noon010_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
640 {
641 struct v4l2_mbus_framefmt *mf = v4l2_subdev_get_try_format(sd,
642 fh->state,
643 0);
644
645 mf->width = noon010_sizes[0].width;
646 mf->height = noon010_sizes[0].height;
647 mf->code = noon010_formats[0].code;
648 mf->colorspace = V4L2_COLORSPACE_JPEG;
649 mf->field = V4L2_FIELD_NONE;
650 return 0;
651 }
652
653 static const struct v4l2_subdev_internal_ops noon010_subdev_internal_ops = {
654 .open = noon010_open,
655 };
656
657 static const struct v4l2_ctrl_ops noon010_ctrl_ops = {
658 .s_ctrl = noon010_s_ctrl,
659 };
660
661 static const struct v4l2_subdev_core_ops noon010_core_ops = {
662 .s_power = noon010_s_power,
663 .log_status = noon010_log_status,
664 };
665
666 static const struct v4l2_subdev_pad_ops noon010_pad_ops = {
667 .enum_mbus_code = noon010_enum_mbus_code,
668 .get_fmt = noon010_get_fmt,
669 .set_fmt = noon010_set_fmt,
670 };
671
672 static const struct v4l2_subdev_video_ops noon010_video_ops = {
673 .s_stream = noon010_s_stream,
674 };
675
676 static const struct v4l2_subdev_ops noon010_ops = {
677 .core = &noon010_core_ops,
678 .pad = &noon010_pad_ops,
679 .video = &noon010_video_ops,
680 };
681
682 /* Return 0 if NOON010PC30L sensor type was detected or -ENODEV otherwise. */
683 static int noon010_detect(struct i2c_client *client, struct noon010_info *info)
684 {
685 int ret;
686
687 ret = power_enable(info);
688 if (ret)
689 return ret;
690
691 ret = i2c_smbus_read_byte_data(client, DEVICE_ID_REG);
692 if (ret < 0)
693 dev_err(&client->dev, "I2C read failed: 0x%X\n", ret);
694
695 power_disable(info);
696
697 return ret == NOON010PC30_ID ? 0 : -ENODEV;
698 }
699
700 static int noon010_probe(struct i2c_client *client,
701 const struct i2c_device_id *id)
702 {
703 struct noon010_info *info;
704 struct v4l2_subdev *sd;
705 const struct noon010pc30_platform_data *pdata
706 = client->dev.platform_data;
707 int ret;
708 int i;
709
710 if (!pdata) {
711 dev_err(&client->dev, "No platform data!\n");
712 return -EIO;
713 }
714
715 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
716 if (!info)
717 return -ENOMEM;
718
719 mutex_init(&info->lock);
720 sd = &info->sd;
721 v4l2_i2c_subdev_init(sd, client, &noon010_ops);
722 /* Static name; NEVER use in new drivers! */
723 strscpy(sd->name, MODULE_NAME, sizeof(sd->name));
724
725 sd->internal_ops = &noon010_subdev_internal_ops;
726 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
727
728 v4l2_ctrl_handler_init(&info->hdl, 3);
729
730 v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
731 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
732 v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
733 V4L2_CID_RED_BALANCE, 0, 127, 1, 64);
734 v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
735 V4L2_CID_BLUE_BALANCE, 0, 127, 1, 64);
736
737 sd->ctrl_handler = &info->hdl;
738
739 ret = info->hdl.error;
740 if (ret)
741 goto np_err;
742
743 info->i2c_reg_page = -1;
744 info->gpio_nreset = -EINVAL;
745 info->gpio_nstby = -EINVAL;
746 info->curr_fmt = &noon010_formats[0];
747 info->curr_win = &noon010_sizes[0];
748
749 if (gpio_is_valid(pdata->gpio_nreset)) {
750 ret = devm_gpio_request_one(&client->dev, pdata->gpio_nreset,
751 GPIOF_OUT_INIT_LOW,
752 "NOON010PC30 NRST");
753 if (ret) {
754 dev_err(&client->dev, "GPIO request error: %d\n", ret);
755 goto np_err;
756 }
757 info->gpio_nreset = pdata->gpio_nreset;
758 gpio_export(info->gpio_nreset, 0);
759 }
760
761 if (gpio_is_valid(pdata->gpio_nstby)) {
762 ret = devm_gpio_request_one(&client->dev, pdata->gpio_nstby,
763 GPIOF_OUT_INIT_LOW,
764 "NOON010PC30 NSTBY");
765 if (ret) {
766 dev_err(&client->dev, "GPIO request error: %d\n", ret);
767 goto np_err;
768 }
769 info->gpio_nstby = pdata->gpio_nstby;
770 gpio_export(info->gpio_nstby, 0);
771 }
772
773 for (i = 0; i < NOON010_NUM_SUPPLIES; i++)
774 info->supply[i].supply = noon010_supply_name[i];
775
776 ret = devm_regulator_bulk_get(&client->dev, NOON010_NUM_SUPPLIES,
777 info->supply);
778 if (ret)
779 goto np_err;
780
781 info->pad.flags = MEDIA_PAD_FL_SOURCE;
782 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
783 ret = media_entity_pads_init(&sd->entity, 1, &info->pad);
784 if (ret < 0)
785 goto np_err;
786
787 ret = noon010_detect(client, info);
788 if (!ret)
789 return 0;
790
791 np_err:
792 v4l2_ctrl_handler_free(&info->hdl);
793 v4l2_device_unregister_subdev(sd);
794 return ret;
795 }
796
797 static int noon010_remove(struct i2c_client *client)
798 {
799 struct v4l2_subdev *sd = i2c_get_clientdata(client);
800 struct noon010_info *info = to_noon010(sd);
801
802 v4l2_device_unregister_subdev(sd);
803 v4l2_ctrl_handler_free(&info->hdl);
804 media_entity_cleanup(&sd->entity);
805
806 return 0;
807 }
808
809 static const struct i2c_device_id noon010_id[] = {
810 { MODULE_NAME, 0 },
811 { },
812 };
813 MODULE_DEVICE_TABLE(i2c, noon010_id);
814
815
816 static struct i2c_driver noon010_i2c_driver = {
817 .driver = {
818 .name = MODULE_NAME
819 },
820 .probe = noon010_probe,
821 .remove = noon010_remove,
822 .id_table = noon010_id,
823 };
824
825 module_i2c_driver(noon010_i2c_driver);
826
827 MODULE_DESCRIPTION("Siliconfile NOON010PC30 camera driver");
828 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
829 MODULE_LICENSE("GPL");