blob: 1a874ec8f9e00202a79814a8137f4910f70b2457 [file] [log] [blame]
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -03001/*
2 * Driver for SiliconFile NOON010PC30 CIF (1/11") Image Sensor with ISP
3 *
Sylwester Nawrocki6426e142011-09-16 12:33:08 -03004 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -03005 * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
6 *
7 * Initial register configuration based on a driver authored by
8 * HeungJun Kim <riverful.kim@samsung.com>.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Sylwester Nawrocki6426e142011-09-16 12:33:08 -030013 * (at your option) any later version.
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -030014 */
15
16#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/i2c.h>
19#include <linux/slab.h>
20#include <linux/regulator/consumer.h>
21#include <media/noon010pc30.h>
22#include <media/v4l2-chip-ident.h>
23#include <linux/videodev2.h>
24#include <media/v4l2-ctrls.h>
25#include <media/v4l2-device.h>
26#include <media/v4l2-mediabus.h>
27#include <media/v4l2-subdev.h>
28
29static int debug;
30module_param(debug, int, 0644);
31MODULE_PARM_DESC(debug, "Enable module debug trace. Set to 1 to enable.");
32
33#define MODULE_NAME "NOON010PC30"
34
35/*
36 * Register offsets within a page
37 * b15..b8 - page id, b7..b0 - register address
38 */
39#define POWER_CTRL_REG 0x0001
40#define PAGEMODE_REG 0x03
41#define DEVICE_ID_REG 0x0004
42#define NOON010PC30_ID 0x86
43#define VDO_CTL_REG(n) (0x0010 + (n))
44#define SYNC_CTL_REG 0x0012
45/* Window size and position */
46#define WIN_ROWH_REG 0x0013
47#define WIN_ROWL_REG 0x0014
48#define WIN_COLH_REG 0x0015
49#define WIN_COLL_REG 0x0016
50#define WIN_HEIGHTH_REG 0x0017
51#define WIN_HEIGHTL_REG 0x0018
52#define WIN_WIDTHH_REG 0x0019
53#define WIN_WIDTHL_REG 0x001A
54#define HBLANKH_REG 0x001B
55#define HBLANKL_REG 0x001C
56#define VSYNCH_REG 0x001D
57#define VSYNCL_REG 0x001E
58/* VSYNC control */
59#define VS_CTL_REG(n) (0x00A1 + (n))
60/* page 1 */
61#define ISP_CTL_REG(n) (0x0110 + (n))
62#define YOFS_REG 0x0119
63#define DARK_YOFS_REG 0x011A
64#define SAT_CTL_REG 0x0120
65#define BSAT_REG 0x0121
66#define RSAT_REG 0x0122
67/* Color correction */
68#define CMC_CTL_REG 0x0130
69#define CMC_OFSGH_REG 0x0133
70#define CMC_OFSGL_REG 0x0135
71#define CMC_SIGN_REG 0x0136
72#define CMC_GOFS_REG 0x0137
73#define CMC_COEF_REG(n) (0x0138 + (n))
74#define CMC_OFS_REG(n) (0x0141 + (n))
75/* Gamma correction */
76#define GMA_CTL_REG 0x0160
77#define GMA_COEF_REG(n) (0x0161 + (n))
78/* Lens Shading */
79#define LENS_CTRL_REG 0x01D0
80#define LENS_XCEN_REG 0x01D1
81#define LENS_YCEN_REG 0x01D2
82#define LENS_RC_REG 0x01D3
83#define LENS_GC_REG 0x01D4
84#define LENS_BC_REG 0x01D5
85#define L_AGON_REG 0x01D6
86#define L_AGOFF_REG 0x01D7
87/* Page 3 - Auto Exposure */
88#define AE_CTL_REG(n) (0x0310 + (n))
89#define AE_CTL9_REG 0x032C
90#define AE_CTL10_REG 0x032D
91#define AE_YLVL_REG 0x031C
92#define AE_YTH_REG(n) (0x031D + (n))
93#define AE_WGT_REG 0x0326
94#define EXP_TIMEH_REG 0x0333
95#define EXP_TIMEM_REG 0x0334
96#define EXP_TIMEL_REG 0x0335
97#define EXP_MMINH_REG 0x0336
98#define EXP_MMINL_REG 0x0337
99#define EXP_MMAXH_REG 0x0338
100#define EXP_MMAXM_REG 0x0339
101#define EXP_MMAXL_REG 0x033A
102/* Page 4 - Auto White Balance */
103#define AWB_CTL_REG(n) (0x0410 + (n))
104#define AWB_ENABE 0x80
105#define AWB_WGHT_REG 0x0419
106#define BGAIN_PAR_REG(n) (0x044F + (n))
107/* Manual white balance, when AWB_CTL2[0]=1 */
108#define MWB_RGAIN_REG 0x0466
109#define MWB_BGAIN_REG 0x0467
110
111/* The token to mark an array end */
112#define REG_TERM 0xFFFF
113
114struct noon010_format {
115 enum v4l2_mbus_pixelcode code;
116 enum v4l2_colorspace colorspace;
117 u16 ispctl1_reg;
118};
119
120struct noon010_frmsize {
121 u16 width;
122 u16 height;
123 int vid_ctl1;
124};
125
126static const char * const noon010_supply_name[] = {
127 "vdd_core", "vddio", "vdda"
128};
129
130#define NOON010_NUM_SUPPLIES ARRAY_SIZE(noon010_supply_name)
131
132struct noon010_info {
133 struct v4l2_subdev sd;
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300134 struct media_pad pad;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300135 struct v4l2_ctrl_handler hdl;
136 const struct noon010pc30_platform_data *pdata;
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300137 struct regulator_bulk_data supply[NOON010_NUM_SUPPLIES];
138 u32 gpio_nreset;
139 u32 gpio_nstby;
140
141 /* Protects the struct members below */
142 struct mutex lock;
143
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300144 const struct noon010_format *curr_fmt;
145 const struct noon010_frmsize *curr_win;
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300146 unsigned int apply_new_cfg:1;
147 unsigned int streaming:1;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300148 unsigned int hflip:1;
149 unsigned int vflip:1;
150 unsigned int power:1;
151 u8 i2c_reg_page;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300152};
153
154struct i2c_regval {
155 u16 addr;
156 u16 val;
157};
158
159/* Supported resolutions. */
160static const struct noon010_frmsize noon010_sizes[] = {
161 {
162 .width = 352,
163 .height = 288,
164 .vid_ctl1 = 0,
165 }, {
166 .width = 176,
167 .height = 144,
168 .vid_ctl1 = 0x10,
169 }, {
170 .width = 88,
171 .height = 72,
172 .vid_ctl1 = 0x20,
173 },
174};
175
176/* Supported pixel formats. */
177static const struct noon010_format noon010_formats[] = {
178 {
179 .code = V4L2_MBUS_FMT_YUYV8_2X8,
180 .colorspace = V4L2_COLORSPACE_JPEG,
181 .ispctl1_reg = 0x03,
182 }, {
183 .code = V4L2_MBUS_FMT_YVYU8_2X8,
184 .colorspace = V4L2_COLORSPACE_JPEG,
185 .ispctl1_reg = 0x02,
186 }, {
187 .code = V4L2_MBUS_FMT_VYUY8_2X8,
188 .colorspace = V4L2_COLORSPACE_JPEG,
189 .ispctl1_reg = 0,
190 }, {
191 .code = V4L2_MBUS_FMT_UYVY8_2X8,
192 .colorspace = V4L2_COLORSPACE_JPEG,
193 .ispctl1_reg = 0x01,
194 }, {
195 .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
196 .colorspace = V4L2_COLORSPACE_JPEG,
197 .ispctl1_reg = 0x40,
198 },
199};
200
201static const struct i2c_regval noon010_base_regs[] = {
202 { WIN_COLL_REG, 0x06 }, { HBLANKL_REG, 0x7C },
203 /* Color corection and saturation */
204 { ISP_CTL_REG(0), 0x30 }, { ISP_CTL_REG(2), 0x30 },
205 { YOFS_REG, 0x80 }, { DARK_YOFS_REG, 0x04 },
206 { SAT_CTL_REG, 0x1F }, { BSAT_REG, 0x90 },
207 { CMC_CTL_REG, 0x0F }, { CMC_OFSGH_REG, 0x3C },
208 { CMC_OFSGL_REG, 0x2C }, { CMC_SIGN_REG, 0x3F },
209 { CMC_COEF_REG(0), 0x79 }, { CMC_OFS_REG(0), 0x00 },
210 { CMC_COEF_REG(1), 0x39 }, { CMC_OFS_REG(1), 0x00 },
211 { CMC_COEF_REG(2), 0x00 }, { CMC_OFS_REG(2), 0x00 },
212 { CMC_COEF_REG(3), 0x11 }, { CMC_OFS_REG(3), 0x8B },
213 { CMC_COEF_REG(4), 0x65 }, { CMC_OFS_REG(4), 0x07 },
214 { CMC_COEF_REG(5), 0x14 }, { CMC_OFS_REG(5), 0x04 },
215 { CMC_COEF_REG(6), 0x01 }, { CMC_OFS_REG(6), 0x9C },
216 { CMC_COEF_REG(7), 0x33 }, { CMC_OFS_REG(7), 0x89 },
217 { CMC_COEF_REG(8), 0x74 }, { CMC_OFS_REG(8), 0x25 },
218 /* Automatic white balance */
219 { AWB_CTL_REG(0), 0x78 }, { AWB_CTL_REG(1), 0x2E },
220 { AWB_CTL_REG(2), 0x20 }, { AWB_CTL_REG(3), 0x85 },
221 /* Auto exposure */
222 { AE_CTL_REG(0), 0xDC }, { AE_CTL_REG(1), 0x81 },
223 { AE_CTL_REG(2), 0x30 }, { AE_CTL_REG(3), 0xA5 },
224 { AE_CTL_REG(4), 0x40 }, { AE_CTL_REG(5), 0x51 },
225 { AE_CTL_REG(6), 0x33 }, { AE_CTL_REG(7), 0x7E },
226 { AE_CTL9_REG, 0x00 }, { AE_CTL10_REG, 0x02 },
227 { AE_YLVL_REG, 0x44 }, { AE_YTH_REG(0), 0x34 },
228 { AE_YTH_REG(1), 0x30 }, { AE_WGT_REG, 0xD5 },
229 /* Lens shading compensation */
230 { LENS_CTRL_REG, 0x01 }, { LENS_XCEN_REG, 0x80 },
231 { LENS_YCEN_REG, 0x70 }, { LENS_RC_REG, 0x53 },
232 { LENS_GC_REG, 0x40 }, { LENS_BC_REG, 0x3E },
233 { REG_TERM, 0 },
234};
235
236static inline struct noon010_info *to_noon010(struct v4l2_subdev *sd)
237{
238 return container_of(sd, struct noon010_info, sd);
239}
240
241static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
242{
243 return &container_of(ctrl->handler, struct noon010_info, hdl)->sd;
244}
245
246static inline int set_i2c_page(struct noon010_info *info,
247 struct i2c_client *client, unsigned int reg)
248{
249 u32 page = reg >> 8 & 0xFF;
250 int ret = 0;
251
252 if (info->i2c_reg_page != page && (reg & 0xFF) != 0x03) {
253 ret = i2c_smbus_write_byte_data(client, PAGEMODE_REG, page);
254 if (!ret)
255 info->i2c_reg_page = page;
256 }
257 return ret;
258}
259
260static int cam_i2c_read(struct v4l2_subdev *sd, u32 reg_addr)
261{
262 struct i2c_client *client = v4l2_get_subdevdata(sd);
263 struct noon010_info *info = to_noon010(sd);
264 int ret = set_i2c_page(info, client, reg_addr);
265
266 if (ret)
267 return ret;
268 return i2c_smbus_read_byte_data(client, reg_addr & 0xFF);
269}
270
271static int cam_i2c_write(struct v4l2_subdev *sd, u32 reg_addr, u32 val)
272{
273 struct i2c_client *client = v4l2_get_subdevdata(sd);
274 struct noon010_info *info = to_noon010(sd);
275 int ret = set_i2c_page(info, client, reg_addr);
276
277 if (ret)
278 return ret;
279 return i2c_smbus_write_byte_data(client, reg_addr & 0xFF, val);
280}
281
282static inline int noon010_bulk_write_reg(struct v4l2_subdev *sd,
283 const struct i2c_regval *msg)
284{
285 while (msg->addr != REG_TERM) {
286 int ret = cam_i2c_write(sd, msg->addr, msg->val);
287
288 if (ret)
289 return ret;
290 msg++;
291 }
292 return 0;
293}
294
295/* Device reset and sleep mode control */
296static int noon010_power_ctrl(struct v4l2_subdev *sd, bool reset, bool sleep)
297{
298 struct noon010_info *info = to_noon010(sd);
299 u8 reg = sleep ? 0xF1 : 0xF0;
300 int ret = 0;
301
302 if (reset)
303 ret = cam_i2c_write(sd, POWER_CTRL_REG, reg | 0x02);
304 if (!ret) {
305 ret = cam_i2c_write(sd, POWER_CTRL_REG, reg);
306 if (reset && !ret)
307 info->i2c_reg_page = -1;
308 }
309 return ret;
310}
311
312/* Automatic white balance control */
313static int noon010_enable_autowhitebalance(struct v4l2_subdev *sd, int on)
314{
315 int ret;
316
317 ret = cam_i2c_write(sd, AWB_CTL_REG(1), on ? 0x2E : 0x2F);
318 if (!ret)
319 ret = cam_i2c_write(sd, AWB_CTL_REG(0), on ? 0xFB : 0x7B);
320 return ret;
321}
322
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300323/* Called with struct noon010_info.lock mutex held */
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300324static int noon010_set_flip(struct v4l2_subdev *sd, int hflip, int vflip)
325{
326 struct noon010_info *info = to_noon010(sd);
327 int reg, ret;
328
329 reg = cam_i2c_read(sd, VDO_CTL_REG(1));
330 if (reg < 0)
331 return reg;
332
333 reg &= 0x7C;
334 if (hflip)
335 reg |= 0x01;
336 if (vflip)
337 reg |= 0x02;
338
339 ret = cam_i2c_write(sd, VDO_CTL_REG(1), reg | 0x80);
340 if (!ret) {
341 info->hflip = hflip;
342 info->vflip = vflip;
343 }
344 return ret;
345}
346
347/* Configure resolution and color format */
348static int noon010_set_params(struct v4l2_subdev *sd)
349{
350 struct noon010_info *info = to_noon010(sd);
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300351
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300352 int ret = cam_i2c_write(sd, VDO_CTL_REG(0),
353 info->curr_win->vid_ctl1);
354 if (ret)
355 return ret;
356 return cam_i2c_write(sd, ISP_CTL_REG(0),
357 info->curr_fmt->ispctl1_reg);
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300358}
359
360/* Find nearest matching image pixel size. */
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300361static int noon010_try_frame_size(struct v4l2_mbus_framefmt *mf,
362 const struct noon010_frmsize **size)
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300363{
364 unsigned int min_err = ~0;
365 int i = ARRAY_SIZE(noon010_sizes);
366 const struct noon010_frmsize *fsize = &noon010_sizes[0],
367 *match = NULL;
368
369 while (i--) {
370 int err = abs(fsize->width - mf->width)
371 + abs(fsize->height - mf->height);
372
373 if (err < min_err) {
374 min_err = err;
375 match = fsize;
376 }
377 fsize++;
378 }
379 if (match) {
380 mf->width = match->width;
381 mf->height = match->height;
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300382 if (size)
383 *size = match;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300384 return 0;
385 }
386 return -EINVAL;
387}
388
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300389/* Called with info.lock mutex held */
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300390static int power_enable(struct noon010_info *info)
391{
392 int ret;
393
394 if (info->power) {
395 v4l2_info(&info->sd, "%s: sensor is already on\n", __func__);
396 return 0;
397 }
398
399 if (gpio_is_valid(info->gpio_nstby))
400 gpio_set_value(info->gpio_nstby, 0);
401
402 if (gpio_is_valid(info->gpio_nreset))
403 gpio_set_value(info->gpio_nreset, 0);
404
405 ret = regulator_bulk_enable(NOON010_NUM_SUPPLIES, info->supply);
406 if (ret)
407 return ret;
408
409 if (gpio_is_valid(info->gpio_nreset)) {
410 msleep(50);
411 gpio_set_value(info->gpio_nreset, 1);
412 }
413 if (gpio_is_valid(info->gpio_nstby)) {
414 udelay(1000);
415 gpio_set_value(info->gpio_nstby, 1);
416 }
417 if (gpio_is_valid(info->gpio_nreset)) {
418 udelay(1000);
419 gpio_set_value(info->gpio_nreset, 0);
420 msleep(100);
421 gpio_set_value(info->gpio_nreset, 1);
422 msleep(20);
423 }
424 info->power = 1;
425
426 v4l2_dbg(1, debug, &info->sd, "%s: sensor is on\n", __func__);
427 return 0;
428}
429
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300430/* Called with info.lock mutex held */
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300431static int power_disable(struct noon010_info *info)
432{
433 int ret;
434
435 if (!info->power) {
436 v4l2_info(&info->sd, "%s: sensor is already off\n", __func__);
437 return 0;
438 }
439
440 ret = regulator_bulk_disable(NOON010_NUM_SUPPLIES, info->supply);
441 if (ret)
442 return ret;
443
444 if (gpio_is_valid(info->gpio_nstby))
445 gpio_set_value(info->gpio_nstby, 0);
446
447 if (gpio_is_valid(info->gpio_nreset))
448 gpio_set_value(info->gpio_nreset, 0);
449
450 info->power = 0;
451
452 v4l2_dbg(1, debug, &info->sd, "%s: sensor is off\n", __func__);
453
454 return 0;
455}
456
457static int noon010_s_ctrl(struct v4l2_ctrl *ctrl)
458{
459 struct v4l2_subdev *sd = to_sd(ctrl);
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300460 struct noon010_info *info = to_noon010(sd);
461 int ret = 0;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300462
463 v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
464 __func__, ctrl->id, ctrl->val);
465
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300466 mutex_lock(&info->lock);
467 /*
468 * If the device is not powered up by the host driver do
469 * not apply any controls to H/W at this time. Instead
470 * the controls will be restored right after power-up.
471 */
472 if (!info->power)
473 goto unlock;
474
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300475 switch (ctrl->id) {
476 case V4L2_CID_AUTO_WHITE_BALANCE:
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300477 ret = noon010_enable_autowhitebalance(sd, ctrl->val);
478 break;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300479 case V4L2_CID_BLUE_BALANCE:
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300480 ret = cam_i2c_write(sd, MWB_BGAIN_REG, ctrl->val);
481 break;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300482 case V4L2_CID_RED_BALANCE:
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300483 ret = cam_i2c_write(sd, MWB_RGAIN_REG, ctrl->val);
484 break;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300485 default:
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300486 ret = -EINVAL;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300487 }
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300488unlock:
489 mutex_unlock(&info->lock);
490 return ret;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300491}
492
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300493static int noon010_enum_mbus_code(struct v4l2_subdev *sd,
494 struct v4l2_subdev_fh *fh,
495 struct v4l2_subdev_mbus_code_enum *code)
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300496{
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300497 if (code->index >= ARRAY_SIZE(noon010_formats))
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300498 return -EINVAL;
499
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300500 code->code = noon010_formats[code->index].code;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300501 return 0;
502}
503
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300504static int noon010_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
505 struct v4l2_subdev_format *fmt)
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300506{
507 struct noon010_info *info = to_noon010(sd);
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300508 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300509
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300510 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
511 if (fh) {
512 mf = v4l2_subdev_get_try_format(fh, 0);
513 fmt->format = *mf;
514 }
515 return 0;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300516 }
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300517 mf = &fmt->format;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300518
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300519 mutex_lock(&info->lock);
520 mf->width = info->curr_win->width;
521 mf->height = info->curr_win->height;
522 mf->code = info->curr_fmt->code;
523 mf->colorspace = info->curr_fmt->colorspace;
524 mf->field = V4L2_FIELD_NONE;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300525
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300526 mutex_unlock(&info->lock);
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300527 return 0;
528}
529
530/* Return nearest media bus frame format. */
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300531static const struct noon010_format *noon010_try_fmt(struct v4l2_subdev *sd,
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300532 struct v4l2_mbus_framefmt *mf)
533{
534 int i = ARRAY_SIZE(noon010_formats);
535
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300536 while (--i)
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300537 if (mf->code == noon010_formats[i].code)
538 break;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300539 mf->code = noon010_formats[i].code;
540
541 return &noon010_formats[i];
542}
543
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300544static int noon010_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
545 struct v4l2_subdev_format *fmt)
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300546{
547 struct noon010_info *info = to_noon010(sd);
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300548 const struct noon010_frmsize *size = NULL;
549 const struct noon010_format *nf;
550 struct v4l2_mbus_framefmt *mf;
551 int ret = 0;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300552
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300553 nf = noon010_try_fmt(sd, &fmt->format);
554 noon010_try_frame_size(&fmt->format, &size);
555 fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300556
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300557 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
558 if (fh) {
559 mf = v4l2_subdev_get_try_format(fh, 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;
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300574}
575
576static int noon010_base_config(struct v4l2_subdev *sd)
577{
578 struct noon010_info *info = to_noon010(sd);
579 int ret;
580
581 ret = noon010_bulk_write_reg(sd, noon010_base_regs);
582 if (!ret) {
583 info->curr_fmt = &noon010_formats[0];
584 info->curr_win = &noon010_sizes[0];
585 ret = noon010_set_params(sd);
586 }
587 if (!ret)
588 ret = noon010_set_flip(sd, 1, 0);
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300589
590 /* sync the handler and the registers state */
591 v4l2_ctrl_handler_setup(&to_noon010(sd)->hdl);
592 return ret;
593}
594
595static int noon010_s_power(struct v4l2_subdev *sd, int on)
596{
597 struct noon010_info *info = to_noon010(sd);
598 const struct noon010pc30_platform_data *pdata = info->pdata;
599 int ret = 0;
600
601 if (WARN(pdata == NULL, "No platform data!\n"))
602 return -ENOMEM;
603
604 if (on) {
605 ret = power_enable(info);
606 if (ret)
607 return ret;
608 ret = noon010_base_config(sd);
609 } else {
610 noon010_power_ctrl(sd, false, true);
611 ret = power_disable(info);
612 info->curr_win = NULL;
613 info->curr_fmt = NULL;
614 }
615
616 return ret;
617}
618
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300619static int noon010_s_stream(struct v4l2_subdev *sd, int on)
620{
621 struct noon010_info *info = to_noon010(sd);
622 int ret = 0;
623
624 mutex_lock(&info->lock);
625 if (!info->streaming != !on) {
626 ret = noon010_power_ctrl(sd, false, !on);
627 if (!ret)
628 info->streaming = on;
629 }
630 if (!ret && on && info->apply_new_cfg) {
631 ret = noon010_set_params(sd);
632 if (!ret)
633 info->apply_new_cfg = 0;
634 }
635 mutex_unlock(&info->lock);
636 return ret;
637}
638
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300639static int noon010_g_chip_ident(struct v4l2_subdev *sd,
640 struct v4l2_dbg_chip_ident *chip)
641{
642 struct i2c_client *client = v4l2_get_subdevdata(sd);
643
644 return v4l2_chip_ident_i2c_client(client, chip,
645 V4L2_IDENT_NOON010PC30, 0);
646}
647
648static int noon010_log_status(struct v4l2_subdev *sd)
649{
650 struct noon010_info *info = to_noon010(sd);
651
652 v4l2_ctrl_handler_log_status(&info->hdl, sd->name);
653 return 0;
654}
655
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300656static int noon010_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
657{
658 struct v4l2_mbus_framefmt *mf = v4l2_subdev_get_try_format(fh, 0);
659
660 mf->width = noon010_sizes[0].width;
661 mf->height = noon010_sizes[0].height;
662 mf->code = noon010_formats[0].code;
663 mf->colorspace = V4L2_COLORSPACE_JPEG;
664 mf->field = V4L2_FIELD_NONE;
665 return 0;
666}
667
668static const struct v4l2_subdev_internal_ops noon010_subdev_internal_ops = {
669 .open = noon010_open,
670};
671
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300672static const struct v4l2_ctrl_ops noon010_ctrl_ops = {
673 .s_ctrl = noon010_s_ctrl,
674};
675
676static const struct v4l2_subdev_core_ops noon010_core_ops = {
677 .g_chip_ident = noon010_g_chip_ident,
678 .s_power = noon010_s_power,
679 .g_ctrl = v4l2_subdev_g_ctrl,
680 .s_ctrl = v4l2_subdev_s_ctrl,
681 .queryctrl = v4l2_subdev_queryctrl,
682 .querymenu = v4l2_subdev_querymenu,
683 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
684 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
685 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
686 .log_status = noon010_log_status,
687};
688
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300689static struct v4l2_subdev_pad_ops noon010_pad_ops = {
690 .enum_mbus_code = noon010_enum_mbus_code,
691 .get_fmt = noon010_get_fmt,
692 .set_fmt = noon010_set_fmt,
693};
694
695static struct v4l2_subdev_video_ops noon010_video_ops = {
696 .s_stream = noon010_s_stream,
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300697};
698
699static const struct v4l2_subdev_ops noon010_ops = {
700 .core = &noon010_core_ops,
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300701 .pad = &noon010_pad_ops,
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300702 .video = &noon010_video_ops,
703};
704
705/* Return 0 if NOON010PC30L sensor type was detected or -ENODEV otherwise. */
706static int noon010_detect(struct i2c_client *client, struct noon010_info *info)
707{
708 int ret;
709
710 ret = power_enable(info);
711 if (ret)
712 return ret;
713
714 ret = i2c_smbus_read_byte_data(client, DEVICE_ID_REG);
715 if (ret < 0)
716 dev_err(&client->dev, "I2C read failed: 0x%X\n", ret);
717
718 power_disable(info);
719
720 return ret == NOON010PC30_ID ? 0 : -ENODEV;
721}
722
723static int noon010_probe(struct i2c_client *client,
724 const struct i2c_device_id *id)
725{
726 struct noon010_info *info;
727 struct v4l2_subdev *sd;
728 const struct noon010pc30_platform_data *pdata
729 = client->dev.platform_data;
730 int ret;
731 int i;
732
733 if (!pdata) {
734 dev_err(&client->dev, "No platform data!\n");
735 return -EIO;
736 }
737
738 info = kzalloc(sizeof(*info), GFP_KERNEL);
739 if (!info)
740 return -ENOMEM;
741
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300742 mutex_init(&info->lock);
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300743 sd = &info->sd;
744 strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
745 v4l2_i2c_subdev_init(sd, client, &noon010_ops);
746
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300747 sd->internal_ops = &noon010_subdev_internal_ops;
748 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
749
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300750 v4l2_ctrl_handler_init(&info->hdl, 3);
751
752 v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
753 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
754 v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
755 V4L2_CID_RED_BALANCE, 0, 127, 1, 64);
756 v4l2_ctrl_new_std(&info->hdl, &noon010_ctrl_ops,
757 V4L2_CID_BLUE_BALANCE, 0, 127, 1, 64);
758
759 sd->ctrl_handler = &info->hdl;
760
761 ret = info->hdl.error;
762 if (ret)
763 goto np_err;
764
765 info->pdata = client->dev.platform_data;
766 info->i2c_reg_page = -1;
767 info->gpio_nreset = -EINVAL;
768 info->gpio_nstby = -EINVAL;
769
770 if (gpio_is_valid(pdata->gpio_nreset)) {
771 ret = gpio_request(pdata->gpio_nreset, "NOON010PC30 NRST");
772 if (ret) {
773 dev_err(&client->dev, "GPIO request error: %d\n", ret);
774 goto np_err;
775 }
776 info->gpio_nreset = pdata->gpio_nreset;
777 gpio_direction_output(info->gpio_nreset, 0);
778 gpio_export(info->gpio_nreset, 0);
779 }
780
781 if (gpio_is_valid(pdata->gpio_nstby)) {
782 ret = gpio_request(pdata->gpio_nstby, "NOON010PC30 NSTBY");
783 if (ret) {
784 dev_err(&client->dev, "GPIO request error: %d\n", ret);
785 goto np_gpio_err;
786 }
787 info->gpio_nstby = pdata->gpio_nstby;
788 gpio_direction_output(info->gpio_nstby, 0);
789 gpio_export(info->gpio_nstby, 0);
790 }
791
792 for (i = 0; i < NOON010_NUM_SUPPLIES; i++)
793 info->supply[i].supply = noon010_supply_name[i];
794
795 ret = regulator_bulk_get(&client->dev, NOON010_NUM_SUPPLIES,
796 info->supply);
797 if (ret)
798 goto np_reg_err;
799
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300800 info->pad.flags = MEDIA_PAD_FL_SOURCE;
801 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
802 ret = media_entity_init(&sd->entity, 1, &info->pad, 0);
803 if (ret < 0)
804 goto np_me_err;
805
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300806 ret = noon010_detect(client, info);
807 if (!ret)
808 return 0;
809
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300810np_me_err:
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300811 regulator_bulk_free(NOON010_NUM_SUPPLIES, info->supply);
812np_reg_err:
813 if (gpio_is_valid(info->gpio_nstby))
814 gpio_free(info->gpio_nstby);
815np_gpio_err:
816 if (gpio_is_valid(info->gpio_nreset))
817 gpio_free(info->gpio_nreset);
818np_err:
819 v4l2_ctrl_handler_free(&info->hdl);
820 v4l2_device_unregister_subdev(sd);
821 kfree(info);
822 return ret;
823}
824
825static int noon010_remove(struct i2c_client *client)
826{
827 struct v4l2_subdev *sd = i2c_get_clientdata(client);
828 struct noon010_info *info = to_noon010(sd);
829
830 v4l2_device_unregister_subdev(sd);
831 v4l2_ctrl_handler_free(&info->hdl);
832
833 regulator_bulk_free(NOON010_NUM_SUPPLIES, info->supply);
834
835 if (gpio_is_valid(info->gpio_nreset))
836 gpio_free(info->gpio_nreset);
837
838 if (gpio_is_valid(info->gpio_nstby))
839 gpio_free(info->gpio_nstby);
840
Sylwester Nawrocki6426e142011-09-16 12:33:08 -0300841 media_entity_cleanup(&sd->entity);
Sylwester Nawrockif8e2e3e2011-01-03 08:07:30 -0300842 kfree(info);
843 return 0;
844}
845
846static const struct i2c_device_id noon010_id[] = {
847 { MODULE_NAME, 0 },
848 { },
849};
850MODULE_DEVICE_TABLE(i2c, noon010_id);
851
852
853static struct i2c_driver noon010_i2c_driver = {
854 .driver = {
855 .name = MODULE_NAME
856 },
857 .probe = noon010_probe,
858 .remove = noon010_remove,
859 .id_table = noon010_id,
860};
861
862static int __init noon010_init(void)
863{
864 return i2c_add_driver(&noon010_i2c_driver);
865}
866
867static void __exit noon010_exit(void)
868{
869 i2c_del_driver(&noon010_i2c_driver);
870}
871
872module_init(noon010_init);
873module_exit(noon010_exit);
874
875MODULE_DESCRIPTION("Siliconfile NOON010PC30 camera driver");
876MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
877MODULE_LICENSE("GPL");