aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/ar0832.c
diff options
context:
space:
mode:
authorChristopher Kenna <cjk@cs.unc.edu>2012-09-28 13:46:28 -0400
committerChristopher Kenna <cjk@cs.unc.edu>2012-09-28 14:50:15 -0400
commitdaa22703f14c007e93b464c45fa60019a36f546d (patch)
treea1a130b6e128dc9d57c35c026977e1b4953105e1 /drivers/media/video/ar0832.c
parent5aa287dcf1b5879aa0150b0511833c52885f5b4c (diff)
Apply k4412 kernel from HardKernel for ODROID-X.
Diffstat (limited to 'drivers/media/video/ar0832.c')
-rw-r--r--drivers/media/video/ar0832.c3081
1 files changed, 3081 insertions, 0 deletions
diff --git a/drivers/media/video/ar0832.c b/drivers/media/video/ar0832.c
new file mode 100644
index 00000000000..85d736c2b12
--- /dev/null
+++ b/drivers/media/video/ar0832.c
@@ -0,0 +1,3081 @@
1/*
2 * driver for Fusitju AR0832 LS 8MP camera
3 *
4 * Copyright (c) 2010, Samsung Electronics. All rights reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16#include <linux/i2c.h>
17#include <linux/init.h>
18#include <media/v4l2-device.h>
19#include <linux/delay.h>
20#include <linux/interrupt.h>
21#include <linux/irq.h>
22#include <linux/vmalloc.h>
23#include <linux/firmware.h>
24#include <linux/videodev2.h>
25#include <linux/slab.h>
26
27#ifdef CONFIG_VIDEO_SAMSUNG_V4L2
28#include <linux/videodev2_samsung.h>
29#endif
30
31#include <linux/regulator/machine.h>
32
33#include <media/ar0832_platform.h>
34#include "ar0832.h"
35
36#define AR0832_DRIVER_NAME "AR0832"
37#define SDCARD_FW
38#ifdef SDCARD_FW
39#define AR0832_FW_PATH "/sdcard/RS_M5LS.bin"
40#endif /* SDCARD_FW */
41#define AR0832T_FW_REQUEST_PATH "ar0832/RS_M5LS_T.bin" /* Techwin */
42#define AR0832O_FW_REQUEST_PATH "ar0832/RS_M5LS_O.bin" /* Optical communication */
43#define AR0832_FW_DUMP_PATH "/data/RS_M5LS_dump.bin"
44#define AR0832_FW_VER_LEN 22
45#define AR0832_FW_VER_FILE_CUR 0x16FF00
46
47#define AR0832_FLASH_BASE_ADDR 0x10000000
48#define AR0832_INT_RAM_BASE_ADDR 0x68000000
49
50#define AR0832_I2C_RETRY 5
51#define AR0832_I2C_VERIFY 100
52#define AR0832_ISP_TIMEOUT 3000
53#define AR0832_ISP_AFB_TIMEOUT 15000 /* FIXME */
54#define AR0832_ISP_ESD_TIMEOUT 1000
55
56#define AR0832_JPEG_MAXSIZE 0x3A0000
57#define AR0832_THUMB_MAXSIZE 0xFC00
58#define AR0832_POST_MAXSIZE 0xBB800
59
60#define AR0832_DEF_APEX_DEN 100
61
62#define ar0832_readb(sd, g, b, v) ar0832_read(sd, 1, g, b, v)
63#define ar0832_readw(sd, g, b, v) ar0832_read(sd, 2, g, b, v)
64#define ar0832_readl(sd, g, b, v) ar0832_read(sd, 4, g, b, v)
65
66#define ar0832_writeb(sd, g, b, v) ar0832_write(sd, 1, g, b, v)
67#define ar0832_writew(sd, g, b, v) ar0832_write(sd, 2, g, b, v)
68#define ar0832_writel(sd, g, b, v) ar0832_write(sd, 4, g, b, v)
69
70#define CHECK_ERR(x) if ((x) < 0) { \
71 cam_err("i2c failed, err %d\n", x); \
72 return x; \
73 }
74static const struct ar0832_frmsizeenum preview_frmsizes[] = {
75 { AR0832_PREVIEW_QCIF, 176, 144, 0x05 }, /* 176 x 144 */
76 { AR0832_PREVIEW_QCIF2, 528, 432, 0x2C }, /* 176 x 144 */
77 { AR0832_PREVIEW_QVGA, 320, 240, 0x09 },
78 { AR0832_PREVIEW_VGA, 640, 480, 0x17 },
79 { AR0832_PREVIEW_D1, 720, 480, 0x18 },
80 { AR0832_PREVIEW_WVGA, 800, 480, 0x1A },
81 { AR0832_PREVIEW_720P, 1280, 720, 0x21 },
82 { AR0832_PREVIEW_1080P, 1920, 1080, 0x28 },
83 { AR0832_PREVIEW_HDR, 3264, 2448, 0x27 },
84};
85
86static const struct ar0832_frmsizeenum capture_frmsizes[] = {
87 { AR0832_CAPTURE_VGA, 640, 480, 0x09 },
88 { AR0832_CAPTURE_WVGA, 800, 480, 0x0A },
89 { AR0832_CAPTURE_W2MP, 2048, 1232, 0x2C },
90 { AR0832_CAPTURE_3MP, 2048, 1536, 0x1B },
91 { AR0832_CAPTURE_W7MP, 3264, 1968, 0x2D },
92 { AR0832_CAPTURE_8MP, 3264, 2448, 0x25 },
93};
94
95static struct ar0832_control ar0832_ctrls[] = {
96 {
97 .id = V4L2_CID_CAMERA_ISO,
98 .minimum = ISO_AUTO,
99 .maximum = ISO_800,
100 .step = 1,
101 .value = ISO_AUTO,
102 .default_value = ISO_AUTO,
103 }, {
104 .id = V4L2_CID_CAMERA_BRIGHTNESS,
105 .minimum = EV_MINUS_4,
106 .maximum = EV_MAX - 1,
107 .step = 1,
108 .value = EV_DEFAULT,
109 .default_value = EV_DEFAULT,
110 }, {
111 .id = V4L2_CID_CAMERA_SATURATION,
112 .minimum = SATURATION_MINUS_2,
113 .maximum = SATURATION_MAX - 1,
114 .step = 1,
115 .value = SATURATION_DEFAULT,
116 .default_value = SATURATION_DEFAULT,
117 }, {
118 .id = V4L2_CID_CAMERA_SHARPNESS,
119 .minimum = SHARPNESS_MINUS_2,
120 .maximum = SHARPNESS_MAX - 1,
121 .step = 1,
122 .value = SHARPNESS_DEFAULT,
123 .default_value = SHARPNESS_DEFAULT,
124 }, {
125 .id = V4L2_CID_CAMERA_ZOOM,
126 .minimum = ZOOM_LEVEL_0,
127 .maximum = ZOOM_LEVEL_MAX - 1,
128 .step = 1,
129 .value = ZOOM_LEVEL_0,
130 .default_value = ZOOM_LEVEL_0,
131 }, {
132 .id = V4L2_CID_CAM_JPEG_QUALITY,
133 .minimum = 1,
134 .maximum = 100,
135 .step = 1,
136 .value = 100,
137 .default_value = 100,
138 },
139};
140
141static inline struct ar0832_state *to_state(struct v4l2_subdev *sd)
142{
143 return container_of(sd, struct ar0832_state, sd);
144}
145
146static int ar0832_read(struct v4l2_subdev *sd,
147 u8 len, u8 category, u8 byte, int *val)
148{
149 struct i2c_client *client = v4l2_get_subdevdata(sd);
150 struct i2c_msg msg;
151 unsigned char data[5];
152 unsigned char recv_data[len + 1];
153 int i, err = 0;
154
155 if (!client->adapter)
156 return -ENODEV;
157
158 if (len != 0x01 && len != 0x02 && len != 0x04)
159 return -EINVAL;
160
161 msg.addr = client->addr;
162 msg.flags = 0;
163 msg.len = sizeof(data);
164 msg.buf = data;
165
166 /* high byte goes out first */
167 data[0] = msg.len;
168 data[1] = 0x01; /* Read category parameters */
169 data[2] = category;
170 data[3] = byte;
171 data[4] = len;
172
173 for (i = AR0832_I2C_RETRY; i; i--) {
174 err = i2c_transfer(client->adapter, &msg, 1);
175 if (err == 1)
176 break;
177 msleep(20);
178 }
179
180 if (err != 1) {
181 cam_err("category %#x, byte %#x\n", category, byte);
182 return err;
183 }
184
185 msg.flags = I2C_M_RD;
186 msg.len = sizeof(recv_data);
187 msg.buf = recv_data;
188 for (i = AR0832_I2C_RETRY; i; i--) {
189 err = i2c_transfer(client->adapter, &msg, 1);
190 if (err == 1)
191 break;
192 msleep(20);
193 }
194
195 if (err != 1) {
196 cam_err("category %#x, byte %#x\n", category, byte);
197 return err;
198 }
199
200 if (recv_data[0] != sizeof(recv_data))
201 cam_i2c_dbg("expected length %d, but return length %d\n",
202 sizeof(recv_data), recv_data[0]);
203
204 if (len == 0x01)
205 *val = recv_data[1];
206 else if (len == 0x02)
207 *val = recv_data[1] << 8 | recv_data[2];
208 else
209 *val = recv_data[1] << 24 | recv_data[2] << 16 |
210 recv_data[3] << 8 | recv_data[4];
211
212 cam_i2c_dbg("category %#02x, byte %#x, value %#x\n", category, byte, *val);
213 return err;
214}
215
216static int ar0832_write(struct v4l2_subdev *sd,
217 u8 len, u8 category, u8 byte, int val)
218{
219 struct i2c_client *client = v4l2_get_subdevdata(sd);
220 struct i2c_msg msg;
221 unsigned char data[len + 4];
222 int i, err;
223return 0;
224 if (!client->adapter)
225 return -ENODEV;
226
227 if (len != 0x01 && len != 0x02 && len != 0x04)
228 return -EINVAL;
229
230 msg.addr = client->addr;
231 msg.flags = 0;
232 msg.len = sizeof(data);
233 msg.buf = data;
234
235 data[0] = msg.len;
236 data[1] = 0x02; /* Write category parameters */
237 data[2] = category;
238 data[3] = byte;
239 if (len == 0x01) {
240 data[4] = val & 0xFF;
241 } else if (len == 0x02) {
242 data[4] = (val >> 8) & 0xFF;
243 data[5] = val & 0xFF;
244 } else {
245 data[4] = (val >> 24) & 0xFF;
246 data[5] = (val >> 16) & 0xFF;
247 data[6] = (val >> 8) & 0xFF;
248 data[7] = val & 0xFF;
249 }
250
251 cam_i2c_dbg("category %#x, byte %#x, value %#x\n", category, byte, val);
252
253 for (i = AR0832_I2C_RETRY; i; i--) {
254 err = i2c_transfer(client->adapter, &msg, 1);
255 if (err == 1)
256 break;
257 msleep(20);
258 }
259
260 return err;
261}
262static unsigned short ar0832_i2c_read_reg(struct i2c_client *client, unsigned short reg,int readbyte /*1 or 2*/)
263{
264 int ret;
265 unsigned char i2c_data[10];
266 unsigned char i2c_rdata[10];
267 memset(i2c_rdata,0,10);
268
269 i2c_data[0]= (unsigned char)((reg>>8) & 0xff);
270 i2c_data[1]= (unsigned char)(reg & 0xff);
271 i2c_master_send(client,i2c_data,2);
272
273 ret = i2c_master_recv(client,i2c_rdata,readbyte);
274
275#if 0
276 int i;
277 for(i=0;i<2;i++)
278 printk("retdata %d => %x \n",i,i2c_rdata[i]);
279
280 if (ret < 0)
281 printk( "%s: err %d\n", __func__, ret);
282#endif
283
284 if (readbyte== 1)
285 return i2c_rdata[0];
286 return i2c_rdata[0]<<8 | i2c_rdata[1];
287}
288
289static int ar0832_i2c_write(struct v4l2_subdev *sd, unsigned char i2c_data[],
290 unsigned char length)
291{
292 struct i2c_client *client = v4l2_get_subdevdata(sd);
293 unsigned char buf[length], i;
294 struct i2c_msg msg = {client->addr, 0, length, buf};
295
296 for (i = 0; i < length; i++)
297 buf[i] = i2c_data[i];
298
299 return i2c_transfer(client->adapter, &msg, 1) == 1 ? 0 : -EIO;
300}
301
302static int ar0832_i2c_write_4byte(struct v4l2_subdev *sd, unsigned char d0,unsigned char d1,unsigned char d2,unsigned char d3)
303{
304 struct i2c_client *client = v4l2_get_subdevdata(sd);
305 unsigned char buf[4], i;
306 struct i2c_msg msg = {client->addr, 0, 4, buf};
307
308 buf[0] = d0;
309 buf[1] = d1;
310 buf[2] = d2;
311 buf[3] = d3;
312
313 return i2c_transfer(client->adapter, &msg, 1) == 1 ? 0 : -EIO;
314}
315
316static int ar0832_i2c_write_2short(struct v4l2_subdev *sd, unsigned short d0,unsigned short d1)
317{
318 struct i2c_client *client = v4l2_get_subdevdata(sd);
319 unsigned char buf[4], i;
320 struct i2c_msg msg = {client->addr, 0, 4, buf};
321
322 buf[0] = d0>>8;
323 buf[1] = d0 & 0xff;
324 buf[2] = d1>>8;
325 buf[3] = d1 & 0xff;
326
327 return i2c_transfer(client->adapter, &msg, 1) == 1 ? 0 : -EIO;
328}
329
330
331static int ar0832_write_array(struct v4l2_subdev *sd,unsigned short * reg , int size)
332{
333 unsigned char _tmp[4];
334 int i,err=0;
335 struct i2c_client *client = v4l2_get_subdevdata(sd);
336
337 for (i = 0; i < size ; i=i+2) {
338 if(reg[i] == 0xffff){ //delay
339 mdelay(reg[i+1]);
340 }
341 else {
342// printk("%x %x. ",reg[i],reg[i+1]);
343// printk(".");
344
345 _tmp[0] =(unsigned char)( reg[i] >> 8) ;
346 _tmp[1] =(unsigned char)( reg[i] & 0xff);
347
348 _tmp[2] =(unsigned char)( reg[i+1] >> 8) ;
349 _tmp[3] =(unsigned char)( reg[i+1] & 0xff);
350 err = ar0832_i2c_write(sd,_tmp , 4);
351 if (err < 0){
352 v4l_info(client, "%s: register set failed\n", \
353 __func__);
354 v4l_info(client,"err i=%d %02x %02x %02x %02x \n",\
355 i, _tmp[0],_tmp[1],_tmp[2],_tmp[3]);
356 return -1;
357
358 }
359 }
360// if(i%50 == 0) printk("\n");
361 }
362// printk(" %d\n",i);
363
364 return err;
365}
366
367/* ****************************************
368 data is 3 bytes
369 {address , bit field, set or clear}
370***************************************** */
371static int ar0832_write_array3(struct v4l2_subdev *sd,unsigned short * reg , int size)
372{
373 unsigned char _tmp[4];
374 int i,err=0;
375 int rdata=0;
376 int wdata=0;
377 struct i2c_client *client = v4l2_get_subdevdata(sd);
378
379 for (i = 0; i < size ; i=i+3) {
380
381 rdata = ar0832_i2c_read_reg(client,reg[i],2) & 0xffff;
382 if(reg[i+2] == 1) wdata = rdata | reg[i+1];
383 else wdata = rdata & (~reg[i+1]);
384#if 0
385 printk("%x %x %x read reg: %x --> write %x \n",reg[i],reg[i+1],reg[i+2],rdata,wdata);
386#endif
387 //address
388 _tmp[0] =(unsigned char)( reg[i] >> 8) ;
389 _tmp[1] =(unsigned char)( reg[i] & 0xff);
390
391 // modified data
392 _tmp[2] =(unsigned char)( wdata >> 8) ;
393 _tmp[3] =(unsigned char)( wdata & 0xff);
394 err = ar0832_i2c_write(sd,_tmp , 4);
395 if (err < 0){
396 v4l_info(client, "%s: register set failed\n", \
397 __func__);
398 v4l_info(client,"err i=%d %02x %02x %02x %02x \n",\
399 i, _tmp[0],_tmp[1],_tmp[2],_tmp[3]);
400 return -1;
401
402 }
403
404// if(i%50 == 0) printk("\n");
405 }
406// printk(" %d\n",i);
407
408 return err;
409}
410
411static int ar0832_mem_read(struct v4l2_subdev *sd, u16 len, u32 addr, u8 *val)
412{
413 struct i2c_client *client = v4l2_get_subdevdata(sd);
414 struct i2c_msg msg;
415 unsigned char data[8];
416 unsigned char recv_data[len + 3];
417 int i, err = 0;
418
419 if (!client->adapter)
420 return -ENODEV;
421
422 if (len <= 0)
423 return -EINVAL;
424
425 msg.addr = client->addr;
426 msg.flags = 0;
427 msg.len = sizeof(data);
428 msg.buf = data;
429
430 /* high byte goes out first */
431 data[0] = 0x00;
432 data[1] = 0x03;
433 data[2] = (addr >> 24) & 0xFF;
434 data[3] = (addr >> 16) & 0xFF;
435 data[4] = (addr >> 8) & 0xFF;
436 data[5] = addr & 0xFF;
437 data[6] = (len >> 8) & 0xFF;
438 data[7] = len & 0xFF;
439
440 for (i = AR0832_I2C_RETRY; i; i--) {
441 err = i2c_transfer(client->adapter, &msg, 1);
442 if (err == 1)
443 break;
444 msleep(20);
445 }
446
447 if (err != 1)
448 return err;
449
450 msg.flags = I2C_M_RD;
451 msg.len = sizeof(recv_data);
452 msg.buf = recv_data;
453 for (i = AR0832_I2C_RETRY; i; i--) {
454 err = i2c_transfer(client->adapter, &msg, 1);
455 if (err == 1)
456 break;
457 msleep(20);
458 }
459
460 if (err != 1)
461 return err;
462
463 if (len != (recv_data[1] << 8 | recv_data[2]))
464 cam_i2c_dbg("expected length %d, but return length %d\n",
465 len, recv_data[1] << 8 | recv_data[2]);
466
467 memcpy(val, recv_data + 3, len);
468
469 cam_i2c_dbg("address %#x, length %d\n", addr, len);
470 return err;
471}
472
473static int ar0832_mem_write(struct v4l2_subdev *sd, u8 cmd, u16 len, u32 addr, u8 *val)
474{
475 struct i2c_client *client = v4l2_get_subdevdata(sd);
476 struct i2c_msg msg;
477 unsigned char data[len + 8];
478 int i, err = 0;
479
480 if (!client->adapter)
481 return -ENODEV;
482
483 msg.addr = client->addr;
484 msg.flags = 0;
485 msg.len = sizeof(data);
486 msg.buf = data;
487
488 /* high byte goes out first */
489 data[0] = 0x00;
490 data[1] = cmd;
491 data[2] = (addr >> 24) & 0xFF;
492 data[3] = (addr >> 16) & 0xFF;
493 data[4] = (addr >> 8) & 0xFF;
494 data[5] = addr & 0xFF;
495 data[6] = (len >> 8) & 0xFF;
496 data[7] = len & 0xFF;
497 memcpy(data + 2 + sizeof(addr) + sizeof(len), val, len);
498
499 cam_i2c_dbg("address %#x, length %d\n", addr, len);
500
501 for (i = AR0832_I2C_RETRY; i; i--) {
502 err = i2c_transfer(client->adapter, &msg, 1);
503 if (err == 1)
504 break;
505 msleep(20);
506 }
507
508 return err;
509}
510
511static irqreturn_t ar0832_isp_isr(int irq, void *dev_id)
512{
513 struct v4l2_subdev *sd = (struct v4l2_subdev *)dev_id;
514 struct ar0832_state *state = to_state(sd);
515
516 cam_dbg("**************** interrupt ****************\n");
517 state->isp.issued = 1;
518 wake_up_interruptible(&state->isp.wait);
519
520 return IRQ_HANDLED;
521}
522
523static u32 ar0832_wait_interrupt(struct v4l2_subdev *sd,
524 unsigned int timeout)
525{
526 struct ar0832_state *state = to_state(sd);
527 cam_trace("E\n");
528return 0;
529 if (wait_event_interruptible_timeout(state->isp.wait,
530 state->isp.issued == 1,
531 msecs_to_jiffies(timeout)) == 0) {
532 cam_err("timeout\n");
533 return 0;
534 }
535
536 state->isp.issued = 0;
537
538 ar0832_readb(sd, AR0832_CATEGORY_SYS,
539 AR0832_SYS_INT_FACTOR, &state->isp.int_factor);
540
541 cam_trace("X\n");
542 return state->isp.int_factor;
543}
544
545static int ar0832_set_mode(struct v4l2_subdev *sd, u32 mode)
546{
547 int i, err;
548 u32 old_mode, val;
549 cam_trace("E\n");
550
551 err = ar0832_readb(sd, AR0832_CATEGORY_SYS, AR0832_SYS_MODE, &old_mode);
552
553 if (err < 0)
554 return err;
555
556 if (old_mode == mode) {
557 cam_dbg("%#x -> %#x\n", old_mode, mode);
558 return old_mode;
559 }
560
561 cam_dbg("%#x -> %#x\n", old_mode, mode);
562
563 switch (old_mode) {
564 case AR0832_SYSINIT_MODE:
565 cam_warn("sensor is initializing\n");
566 err = -EBUSY;
567 break;
568
569 case AR0832_PARMSET_MODE:
570 if (mode == AR0832_STILLCAP_MODE) {
571 err = ar0832_writeb(sd, AR0832_CATEGORY_SYS,
572 AR0832_SYS_MODE, AR0832_MONITOR_MODE);
573 if (err < 0)
574 break;
575 for (i = AR0832_I2C_VERIFY; i; i--) {
576 err = ar0832_readb(sd, AR0832_CATEGORY_SYS,
577 AR0832_SYS_MODE, &val);
578 if (val == AR0832_MONITOR_MODE)
579 break;
580 msleep(10);
581 }
582 }
583 case AR0832_MONITOR_MODE:
584 case AR0832_STILLCAP_MODE:
585 err = ar0832_writeb(sd, AR0832_CATEGORY_SYS,
586 AR0832_SYS_MODE, mode);
587 for (i = AR0832_I2C_VERIFY; i; i--) {
588 err = ar0832_readb(sd, AR0832_CATEGORY_SYS,
589 AR0832_SYS_MODE, &val);
590 if (val == AR0832_MONITOR_MODE)
591 break;
592 msleep(10);
593 }
594 break;
595
596 default:
597 cam_warn("current mode is unknown, %d\n", old_mode);
598 err = -EINVAL;
599 }
600
601 if (err < 0)
602 return err;
603
604 for (i = AR0832_I2C_VERIFY; i; i--) {
605 err = ar0832_readb(sd, AR0832_CATEGORY_SYS,
606 AR0832_SYS_MODE, &val);
607 if (val == mode)
608 break;
609 msleep(10);
610 }
611
612 cam_trace("X\n");
613 return old_mode;
614}
615
616/*
617 * v4l2_subdev_core_ops
618 */
619static int ar0832_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
620{
621 int i;
622
623 for (i = 0; i < ARRAY_SIZE(ar0832_ctrls); i++) {
624 if (qc->id == ar0832_ctrls[i].id) {
625 qc->maximum = ar0832_ctrls[i].maximum;
626 qc->minimum = ar0832_ctrls[i].minimum;
627 qc->step = ar0832_ctrls[i].step;
628 qc->default_value = ar0832_ctrls[i].default_value;
629 return 0;
630 }
631 }
632
633 return -EINVAL;
634}
635
636static int ar0832_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
637{
638 struct ar0832_state *state = to_state(sd);
639 int err = 0;
640
641 switch (ctrl->id) {
642 case V4L2_CID_CAMERA_AUTO_FOCUS_RESULT:
643 ctrl->value = state->focus.status;
644 break;
645
646 case V4L2_CID_CAM_JPEG_MEMSIZE:
647 ctrl->value = AR0832_JPEG_MAXSIZE +
648 AR0832_THUMB_MAXSIZE + AR0832_POST_MAXSIZE;
649 break;
650
651 case V4L2_CID_CAM_JPEG_MAIN_SIZE:
652 ctrl->value = state->jpeg.main_size;
653 break;
654
655 case V4L2_CID_CAM_JPEG_MAIN_OFFSET:
656 ctrl->value = state->jpeg.main_offset;
657 break;
658
659 case V4L2_CID_CAM_JPEG_THUMB_SIZE:
660 ctrl->value = state->jpeg.thumb_size;
661 break;
662
663 case V4L2_CID_CAM_JPEG_THUMB_OFFSET:
664 ctrl->value = state->jpeg.thumb_offset;
665 break;
666
667 case V4L2_CID_CAM_JPEG_POSTVIEW_OFFSET:
668 ctrl->value = state->jpeg.postview_offset;
669 break;
670
671 case V4L2_CID_CAMERA_EXIF_FLASH:
672 ctrl->value = state->exif.flash;
673 break;
674
675 case V4L2_CID_CAMERA_EXIF_ISO:
676 ctrl->value = state->exif.iso;
677 break;
678
679 case V4L2_CID_CAMERA_EXIF_TV:
680 ctrl->value = state->exif.tv;
681 break;
682
683 case V4L2_CID_CAMERA_EXIF_BV:
684 ctrl->value = state->exif.bv;
685 break;
686
687 case V4L2_CID_CAMERA_EXIF_EBV:
688 ctrl->value = state->exif.ebv;
689 break;
690
691 default:
692 cam_err("no such control id %d\n",
693 ctrl->id - V4L2_CID_PRIVATE_BASE);
694 /*err = -ENOIOCTLCMD*/
695 err = 0;
696 break;
697 }
698
699 if (err < 0 && err != -ENOIOCTLCMD)
700 cam_err("failed, id %d\n", ctrl->id - V4L2_CID_PRIVATE_BASE);
701
702 return err;
703}
704
705#ifdef CONFIG_TARGET_LOCALE_KOR
706static int ar0832_set_antibanding(struct v4l2_subdev *sd, int val)
707{
708 int antibanding = 0x02; /* Fix 60Hz for domastic */
709 int err = 0;
710
711 cam_dbg("E, value %d\n", val);
712
713 antibanding = val;
714
715 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_FLICKER, antibanding);
716 CHECK_ERR(err);
717
718 cam_trace("X\n");
719 return err;
720}
721#endif
722
723static int ar0832_set_af_softlanding(struct v4l2_subdev *sd)
724{
725 struct ar0832_state *state = to_state(sd);
726 u32 status = 0;
727 int i, err = 0;
728
729 cam_trace("E\n");
730
731 if (unlikely(state->isp.bad_fw)) {
732 cam_err("\"Unknown\" state, please update F/W");
733 return -ENOSYS;
734 }
735
736 err = ar0832_set_mode(sd, AR0832_MONITOR_MODE);
737 if (err <= 0) {
738 cam_err("failed to set mode\n");
739 return err;
740 }
741
742 err = ar0832_writeb(sd, AR0832_CATEGORY_LENS, AR0832_LENS_AF_MODE, 0x07);
743 CHECK_ERR(err);
744
745 for (i = AR0832_I2C_VERIFY; i; i--) {
746 msleep(10);
747 err = ar0832_readb(sd, AR0832_CATEGORY_LENS,
748 AR0832_LENS_AF_STATUS, &status);
749 CHECK_ERR(err);
750
751 if ((status & 0x01) == 0x00)
752 break;
753 }
754
755 if ((status & 0x01) != 0x00) {
756 cam_err("failed\n");
757 return -ETIMEDOUT;
758 }
759
760 cam_trace("X\n");
761 return err;
762}
763
764static int ar0832_dump_fw(struct v4l2_subdev *sd)
765{
766 struct file *fp;
767 mm_segment_t old_fs;
768 u8 *buf, val;
769 u32 addr, unit, count, intram_unit = 0x1000;
770 int i, j, err;
771
772 old_fs = get_fs();
773 set_fs(KERNEL_DS);
774
775 fp = filp_open(AR0832_FW_DUMP_PATH,
776 O_WRONLY|O_CREAT|O_TRUNC, S_IRUGO|S_IWUGO|S_IXUSR);
777 if (IS_ERR(fp)) {
778 cam_err("failed to open %s, err %ld\n",
779 AR0832_FW_DUMP_PATH, PTR_ERR(fp));
780 err = -ENOENT;
781 goto out0;
782 }
783
784 buf = kmalloc(intram_unit, GFP_KERNEL);
785 if (!buf) {
786 cam_err("failed to allocate memory\n");
787 err = -ENOMEM;
788 goto out0;
789 }
790
791 cam_dbg("start, file path %s\n", AR0832_FW_DUMP_PATH);
792
793 /* set pin */
794 val = 0x7E;
795 err = ar0832_mem_write(sd, 0x04, sizeof(val), 0x50000308, &val);
796 if (err < 0) {
797 cam_err("i2c falied, err %d\n", err);
798 goto out1;
799 }
800
801 addr = AR0832_FLASH_BASE_ADDR;
802 unit = SZ_64K;
803 count = 31;
804 for (i = 0; i < count; i++) {
805 for (j = 0; j < unit; j += intram_unit) {
806 err = ar0832_mem_read(sd,
807 intram_unit, addr + (i * unit) + j, buf);
808 if (err < 0) {
809 cam_err("i2c falied, err %d\n", err);
810 goto out1;
811 }
812 vfs_write(fp, buf, intram_unit, &fp->f_pos);
813 }
814 }
815
816 addr = AR0832_FLASH_BASE_ADDR + SZ_64K * count;
817 unit = SZ_8K;
818 count = 4;
819 for (i = 0; i < count; i++) {
820 for (j = 0; j < unit; j += intram_unit) {
821 err = ar0832_mem_read(sd,
822 intram_unit, addr + (i * unit) + j, buf);
823 if (err < 0) {
824 cam_err("i2c falied, err %d\n", err);
825 goto out1;
826 }
827 vfs_write(fp, buf, intram_unit, &fp->f_pos);
828 }
829 }
830
831 cam_dbg("end\n");
832out1:
833 kfree(buf);
834out0:
835 if (!IS_ERR(fp))
836 filp_close(fp, current->files);
837 set_fs(old_fs);
838
839 return err;
840}
841
842static int ar0832_get_sensor_fw_version(struct v4l2_subdev *sd,
843 char *buf)
844{
845 u8 val;
846 int err;
847
848 /* set pin */
849 val = 0x7E;
850 err = ar0832_mem_write(sd, 0x04, sizeof(val), 0x50000308, &val);
851 CHECK_ERR(err);
852
853 err = ar0832_mem_read(sd, AR0832_FW_VER_LEN,
854 AR0832_FLASH_BASE_ADDR + AR0832_FW_VER_FILE_CUR, buf);
855
856 cam_dbg("%s\n", buf);
857 return 0;
858}
859
860static int ar0832_get_phone_fw_version(struct v4l2_subdev *sd,
861 char *buf)
862{
863 struct i2c_client *client = v4l2_get_subdevdata(sd);
864 struct device *dev = &client->adapter->dev;
865 u8 sensor_ver[AR0832_FW_VER_LEN] = {0, };
866 const struct firmware *fentry;
867 int err;
868
869#ifdef SDCARD_FW
870 struct file *fp;
871 mm_segment_t old_fs;
872 long nread;
873 int fw_requested = 1;
874
875 old_fs = get_fs();
876 set_fs(KERNEL_DS);
877
878 fp = filp_open(AR0832_FW_PATH, O_RDONLY, 0);
879 if (IS_ERR(fp)) {
880 cam_trace("failed to open %s, err %ld\n", AR0832_FW_PATH, PTR_ERR(fp));
881 goto request_fw;
882 }
883
884 fw_requested = 0;
885 err = vfs_llseek(fp, AR0832_FW_VER_FILE_CUR, SEEK_SET);
886 if (err < 0) {
887 cam_warn("failed to fseek, %d\n", err);
888 goto out;
889 }
890
891 nread = vfs_read(fp, (char __user *)buf, AR0832_FW_VER_LEN, &fp->f_pos);
892 if (nread != AR0832_FW_VER_LEN) {
893 cam_err("failed to read firmware file, %ld Bytes\n", nread);
894 err = -EIO;
895 goto out;
896 }
897
898request_fw:
899 if (fw_requested) {
900 set_fs(old_fs);
901#endif /* SDCARD_FW */
902 ar0832_get_sensor_fw_version(sd, sensor_ver);
903
904 if (sensor_ver[0] == 'T')
905 err = request_firmware(&fentry, AR0832T_FW_REQUEST_PATH, dev);
906 else
907 err = request_firmware(&fentry, AR0832O_FW_REQUEST_PATH, dev);
908
909 if (err != 0) {
910 cam_err("request_firmware falied\n");
911 err = -EINVAL;
912 goto out;
913 }
914
915 memcpy(buf, (u8 *)&fentry->data[AR0832_FW_VER_FILE_CUR], AR0832_FW_VER_LEN);
916#ifdef SDCARD_FW
917 }
918#endif /* SDCARD_FW */
919
920out:
921#ifdef SDCARD_FW
922 if (!fw_requested) {
923 filp_close(fp, current->files);
924 set_fs(old_fs);
925 }
926#endif /* SDCARD_FW */
927
928 cam_dbg("%s\n", buf);
929 return 0;
930}
931
932static int ar0832_check_fw(struct v4l2_subdev *sd)
933{
934 struct ar0832_state *state = to_state(sd);
935 u8 sensor_ver[AR0832_FW_VER_LEN] = "FAILED Fujitsu AR0832LS";
936 u8 phone_ver[AR0832_FW_VER_LEN] = "FAILED Fujitsu AR0832LS";
937 int af_cal_h = 0, af_cal_l = 0;
938 int rg_cal_h = 0, rg_cal_l = 0;
939 int bg_cal_h = 0, bg_cal_l = 0;
940 int update_count = 0;
941 u32 int_factor;
942 int err;
943
944 cam_trace("E\n");
945
946 /* F/W version */
947 ar0832_get_phone_fw_version(sd, phone_ver);
948
949 if (state->isp.bad_fw)
950 goto out;
951
952 ar0832_get_sensor_fw_version(sd, sensor_ver);
953
954 err = ar0832_writeb(sd, AR0832_CATEGORY_FLASH, AR0832_FLASH_CAM_START, 0x01);
955 CHECK_ERR(err);
956
957 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
958 if (!(int_factor & AR0832_INT_MODE)) {
959 cam_err("firmware was erased?\n");
960 return -ETIMEDOUT;
961 }
962
963 err = ar0832_readb(sd, AR0832_CATEGORY_LENS, AR0832_LENS_AF_CAL, &af_cal_l);
964 CHECK_ERR(err);
965
966 err = ar0832_readb(sd, AR0832_CATEGORY_ADJST, AR0832_ADJST_AWB_RG_H, &rg_cal_h);
967 CHECK_ERR(err);
968 err = ar0832_readb(sd, AR0832_CATEGORY_ADJST, AR0832_ADJST_AWB_RG_L, &rg_cal_l);
969 CHECK_ERR(err);
970
971 err = ar0832_readb(sd, AR0832_CATEGORY_ADJST, AR0832_ADJST_AWB_BG_H, &bg_cal_h);
972 CHECK_ERR(err);
973 err = ar0832_readb(sd, AR0832_CATEGORY_ADJST, AR0832_ADJST_AWB_BG_L, &bg_cal_l);
974 CHECK_ERR(err);
975
976out:
977 if (!state->fw_version) {
978 state->fw_version = kzalloc(50, GFP_KERNEL);
979 if (!state->fw_version) {
980 cam_err("no memory for F/W version\n");
981 return -ENOMEM;
982 }
983 }
984
985 sprintf(state->fw_version, "%s %s %d %x %x %x %x %x %x",
986 sensor_ver, phone_ver, update_count,
987 af_cal_h, af_cal_l, rg_cal_h, rg_cal_l, bg_cal_h, bg_cal_l);
988
989 cam_trace("X\n");
990 return 0;
991}
992
993static int ar0832_set_sensor_mode(struct v4l2_subdev *sd, int val)
994{
995 struct ar0832_state *state = to_state(sd);
996 int err;
997 cam_dbg("E, value %d\n", val);
998
999 err = ar0832_set_mode(sd, AR0832_PARMSET_MODE);
1000 CHECK_ERR(err);
1001
1002 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM,
1003 AR0832_PARM_HDMOVIE, val == SENSOR_MOVIE ? 0x01 : 0x00);
1004 CHECK_ERR(err);
1005
1006 state->sensor_mode = val;
1007
1008 cam_trace("X\n");
1009 return 0;
1010}
1011
1012static int ar0832_set_flash(struct v4l2_subdev *sd, int val, int recording)
1013{
1014 struct ar0832_state *state = to_state(sd);
1015 int light, flash;
1016 int err;
1017 cam_dbg("E, value %d\n", val);
1018
1019 if (!recording)
1020 state->flash_mode = val;
1021
1022 /* movie flash mode should be set when recording is started */
1023 if (state->sensor_mode == SENSOR_MOVIE && !recording)
1024 return 0;
1025
1026retry:
1027 switch (val) {
1028 case FLASH_MODE_OFF:
1029 light = 0x00;
1030 flash = (state->sensor_mode == SENSOR_CAMERA) ? 0x00 : -1;
1031 break;
1032
1033 case FLASH_MODE_AUTO:
1034 light = (state->sensor_mode == SENSOR_CAMERA) ? 0x02 : 0x04;
1035 flash = (state->sensor_mode == SENSOR_CAMERA) ? 0x02 : -1;
1036 break;
1037
1038 case FLASH_MODE_ON:
1039 light = (state->sensor_mode == SENSOR_CAMERA) ? 0x01 : 0x03;
1040 flash = (state->sensor_mode == SENSOR_CAMERA) ? 0x01 : -1;
1041 break;
1042
1043 case FLASH_MODE_TORCH:
1044 light = 0x03;
1045 flash = -1;
1046 break;
1047
1048 default:
1049 cam_warn("invalid value, %d\n", val);
1050 val = FLASH_MODE_OFF;
1051 goto retry;
1052 }
1053
1054 if (light >= 0) {
1055 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
1056 AR0832_CAPPARM_LIGHT_CTRL, light);
1057 CHECK_ERR(err);
1058 }
1059
1060 if (flash >= 0) {
1061 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
1062 AR0832_CAPPARM_FLASH_CTRL, flash);
1063 CHECK_ERR(err);
1064 }
1065
1066 cam_trace("X\n");
1067 return 0;
1068}
1069
1070static int ar0832_set_iso(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1071{
1072 struct v4l2_queryctrl qc = {0,};
1073 int val = ctrl->value, err;
1074 u32 iso[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
1075 cam_dbg("E, value %d\n", val);
1076
1077 qc.id = ctrl->id;
1078 ar0832_queryctrl(sd, &qc);
1079
1080 if (val < qc.minimum || val > qc.maximum) {
1081 cam_warn("invalied value, %d\n", val);
1082 val = qc.default_value;
1083 }
1084
1085 val -= qc.minimum;
1086
1087 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_ISOSEL, iso[val]);
1088 CHECK_ERR(err);
1089
1090 cam_trace("X\n");
1091 return 0;
1092}
1093
1094static int ar0832_set_metering(struct v4l2_subdev *sd, int val)
1095{
1096 int err;
1097 cam_dbg("E, value %d\n", val);
1098
1099retry:
1100 switch (val) {
1101 case METERING_CENTER:
1102 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_MODE, 0x03);
1103 CHECK_ERR(err);
1104 break;
1105 case METERING_SPOT:
1106 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_MODE, 0x06);
1107 CHECK_ERR(err);
1108 break;
1109 case METERING_MATRIX:
1110 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_MODE, 0x01);
1111 CHECK_ERR(err);
1112 break;
1113 default:
1114 cam_warn("invalid value, %d\n", val);
1115 val = METERING_CENTER;
1116 goto retry;
1117 }
1118
1119 cam_trace("X\n");
1120 return 0;
1121}
1122
1123static int ar0832_set_exposure(struct v4l2_subdev *sd,
1124 struct v4l2_control *ctrl)
1125{
1126 struct v4l2_queryctrl qc = {0,};
1127 int val = ctrl->value, err;
1128 u32 exposure[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
1129 cam_dbg("E, value %d\n", val);
1130
1131 qc.id = ctrl->id;
1132 ar0832_queryctrl(sd, &qc);
1133
1134 if (val < qc.minimum || val > qc.maximum) {
1135 cam_warn("invalied value, %d\n", val);
1136 val = qc.default_value;
1137 }
1138
1139 val -= qc.minimum;
1140
1141 err = ar0832_writeb(sd, AR0832_CATEGORY_AE,
1142 AR0832_AE_INDEX, exposure[val]);
1143 CHECK_ERR(err);
1144
1145 cam_trace("X\n");
1146 return 0;
1147}
1148
1149static int ar0832_set_whitebalance(struct v4l2_subdev *sd, int val)
1150{
1151 int err;
1152 cam_dbg("E, value %d\n", val);
1153
1154retry:
1155 switch (val) {
1156 case WHITE_BALANCE_AUTO:
1157 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1158 AR0832_WB_AWB_MODE, 0x01);
1159 CHECK_ERR(err);
1160 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1161 AR0832_WB_AWB_MANUAL, 0x01);
1162 CHECK_ERR(err);
1163 break;
1164
1165 case WHITE_BALANCE_SUNNY:
1166 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1167 AR0832_WB_AWB_MODE, 0x02);
1168 CHECK_ERR(err);
1169 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1170 AR0832_WB_AWB_MANUAL, 0x04);
1171 CHECK_ERR(err);
1172 break;
1173
1174 case WHITE_BALANCE_CLOUDY:
1175 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1176 AR0832_WB_AWB_MODE, 0x02);
1177 CHECK_ERR(err);
1178 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1179 AR0832_WB_AWB_MANUAL, 0x05);
1180 CHECK_ERR(err);
1181 break;
1182
1183 case WHITE_BALANCE_TUNGSTEN:
1184 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1185 AR0832_WB_AWB_MODE, 0x02);
1186 CHECK_ERR(err);
1187 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1188 AR0832_WB_AWB_MANUAL, 0x01);
1189 CHECK_ERR(err);
1190 break;
1191
1192 case WHITE_BALANCE_FLUORESCENT:
1193 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1194 AR0832_WB_AWB_MODE, 0x02);
1195 CHECK_ERR(err);
1196 err = ar0832_writeb(sd, AR0832_CATEGORY_WB,
1197 AR0832_WB_AWB_MANUAL, 0x02);
1198 CHECK_ERR(err);
1199 break;
1200
1201 default:
1202 cam_warn("invalid value, %d\n", val);
1203 val = WHITE_BALANCE_AUTO;
1204 goto retry;
1205 }
1206
1207 cam_trace("X\n");
1208 return 0;
1209}
1210
1211static int ar0832_set_sharpness(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1212{
1213 struct v4l2_queryctrl qc = {0,};
1214 int val = ctrl->value, err;
1215 u32 sharpness[] = {0x03, 0x04, 0x05, 0x06, 0x07};
1216 cam_dbg("E, value %d\n", val);
1217
1218 qc.id = ctrl->id;
1219 ar0832_queryctrl(sd, &qc);
1220
1221 if (val < qc.minimum || val > qc.maximum) {
1222 cam_warn("invalied value, %d\n", val);
1223 val = qc.default_value;
1224 }
1225
1226 val -= qc.minimum;
1227
1228 err = ar0832_writeb(sd, AR0832_CATEGORY_MON,
1229 AR0832_MON_EDGE_LVL, sharpness[val]);
1230 CHECK_ERR(err);
1231
1232 cam_trace("X\n");
1233 return 0;
1234}
1235
1236static int ar0832_set_saturation(struct v4l2_subdev *sd,
1237 struct v4l2_control *ctrl)
1238{
1239 struct v4l2_queryctrl qc = {0,};
1240 int val = ctrl->value, err;
1241 u32 saturation[] = {0x01, 0x02, 0x03, 0x04, 0x05};
1242 cam_dbg("E, value %d\n", val);
1243
1244 qc.id = ctrl->id;
1245 ar0832_queryctrl(sd, &qc);
1246
1247 if (val < qc.minimum || val > qc.maximum) {
1248 cam_warn("invalied value, %d\n", val);
1249 val = qc.default_value;
1250 }
1251
1252 val -= qc.minimum;
1253
1254 err = ar0832_writeb(sd, AR0832_CATEGORY_MON,
1255 AR0832_MON_CHROMA_LVL, saturation[val]);
1256 CHECK_ERR(err);
1257
1258 cam_trace("X\n");
1259 return 0;
1260}
1261
1262static int ar0832_set_scene_mode(struct v4l2_subdev *sd, int val)
1263{
1264 struct v4l2_control ctrl;
1265 int evp, iso, brightness, whitebalance, sharpness, saturation;
1266 int err;
1267 cam_dbg("E, value %d\n", val);
1268
1269 iso = ISO_AUTO;
1270 brightness = EV_DEFAULT;
1271 whitebalance = WHITE_BALANCE_AUTO;
1272 sharpness = SHARPNESS_DEFAULT;
1273 saturation = CONTRAST_DEFAULT;
1274
1275retry:
1276 switch (val) {
1277 case SCENE_MODE_NONE:
1278 evp = 0x00;
1279 break;
1280
1281 case SCENE_MODE_PORTRAIT:
1282 evp = 0x01;
1283 sharpness = SHARPNESS_MINUS_1;
1284 break;
1285
1286 case SCENE_MODE_LANDSCAPE:
1287 evp = 0x02;
1288 sharpness = SHARPNESS_PLUS_1;
1289 saturation = SATURATION_PLUS_1;
1290 break;
1291
1292 case SCENE_MODE_SPORTS:
1293 evp = 0x03;
1294 break;
1295
1296 case SCENE_MODE_PARTY_INDOOR:
1297 evp = 0x04;
1298 /*iso = ISO_200; sensor will set internally */
1299 saturation = SATURATION_PLUS_1;
1300 break;
1301
1302 case SCENE_MODE_BEACH_SNOW:
1303 evp = 0x05;
1304 /*iso = ISO_50; sensor will set internally */
1305 brightness = EV_PLUS_2;
1306 saturation = SATURATION_PLUS_1;
1307 break;
1308
1309 case SCENE_MODE_SUNSET:
1310 evp = 0x06;
1311 whitebalance = WHITE_BALANCE_SUNNY;
1312 break;
1313
1314 case SCENE_MODE_DUSK_DAWN:
1315 evp = 0x07;
1316 whitebalance = WHITE_BALANCE_FLUORESCENT;
1317 break;
1318
1319 case SCENE_MODE_FALL_COLOR:
1320 evp = 0x08;
1321 saturation = SATURATION_PLUS_2;
1322 break;
1323
1324 case SCENE_MODE_NIGHTSHOT:
1325 evp = 0x09;
1326 break;
1327
1328 case SCENE_MODE_BACK_LIGHT:
1329 evp = 0x0A;
1330 break;
1331
1332 case SCENE_MODE_FIREWORKS:
1333 evp = 0x0B;
1334 /*iso = ISO_50; sensor will set internally */
1335 break;
1336
1337 case SCENE_MODE_TEXT:
1338 evp = 0x0C;
1339 sharpness = SHARPNESS_PLUS_2;
1340 break;
1341
1342 case SCENE_MODE_CANDLE_LIGHT:
1343 evp = 0x0D;
1344 whitebalance = WHITE_BALANCE_SUNNY;
1345 break;
1346
1347 default:
1348 cam_warn("invalid value, %d\n", val);
1349 val = SCENE_MODE_NONE;
1350 goto retry;
1351 }
1352
1353 /* EV-P */
1354 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_EP_MODE_MON, evp);
1355 CHECK_ERR(err);
1356 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_EP_MODE_CAP, evp);
1357 CHECK_ERR(err);
1358
1359 /* ISO */
1360 ctrl.id = V4L2_CID_CAMERA_ISO;
1361 ctrl.value = iso;
1362 ar0832_set_iso(sd, &ctrl);
1363
1364 /* EV Bias */
1365 ctrl.id = V4L2_CID_CAMERA_BRIGHTNESS;
1366 ctrl.value = brightness;
1367 ar0832_set_exposure(sd, &ctrl);
1368
1369 /* AWB */
1370 ar0832_set_whitebalance(sd, whitebalance);
1371
1372 /* Chroma Saturation */
1373 ctrl.id = V4L2_CID_CAMERA_SATURATION;
1374 ctrl.value = saturation;
1375 ar0832_set_saturation(sd, &ctrl);
1376
1377 /* Sharpness */
1378 ctrl.id = V4L2_CID_CAMERA_SHARPNESS;
1379 ctrl.value = sharpness;
1380 ar0832_set_sharpness(sd, &ctrl);
1381
1382 /* Emotional Color */
1383 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
1384 AR0832_CAPPARM_MCC_MODE, val == SCENE_MODE_NONE ? 0x01 : 0x00);
1385 CHECK_ERR(err);
1386
1387 cam_trace("X\n");
1388 return 0;
1389}
1390
1391static int ar0832_set_effect_color(struct v4l2_subdev *sd, int val)
1392{
1393 u32 int_factor;
1394 int on, old_mode, cb, cr;
1395 int err;
1396
1397 err = ar0832_readb(sd, AR0832_CATEGORY_PARM, AR0832_PARM_EFFECT, &on);
1398 CHECK_ERR(err);
1399 if (on) {
1400 old_mode = ar0832_set_mode(sd, AR0832_PARMSET_MODE);
1401 CHECK_ERR(old_mode);
1402
1403 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM, AR0832_PARM_EFFECT, 0);
1404 CHECK_ERR(err);
1405
1406 if (old_mode == AR0832_MONITOR_MODE) {
1407 err = ar0832_set_mode(sd, old_mode);
1408 CHECK_ERR(err);
1409
1410 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
1411 if (!(int_factor & AR0832_INT_MODE)) {
1412 cam_err("AR0832_INT_MODE isn't issued, %#x\n",
1413 int_factor);
1414 return -ETIMEDOUT;
1415 }
1416 CHECK_ERR(err);
1417 }
1418 }
1419
1420 switch (val) {
1421 case IMAGE_EFFECT_NONE:
1422 break;
1423
1424 case IMAGE_EFFECT_SEPIA:
1425 cb = 0xD8;
1426 cr = 0x18;
1427 break;
1428
1429 case IMAGE_EFFECT_BNW:
1430 cb = 0x00;
1431 cr = 0x00;
1432 break;
1433 }
1434
1435 err = ar0832_writeb(sd, AR0832_CATEGORY_MON,
1436 AR0832_MON_COLOR_EFFECT, val == IMAGE_EFFECT_NONE ? 0x00 : 0x01);
1437 CHECK_ERR(err);
1438
1439 if (val != IMAGE_EFFECT_NONE) {
1440 err = ar0832_writeb(sd, AR0832_CATEGORY_MON, AR0832_MON_CFIXB, cb);
1441 CHECK_ERR(err);
1442 err = ar0832_writeb(sd, AR0832_CATEGORY_MON, AR0832_MON_CFIXR, cr);
1443 CHECK_ERR(err);
1444 }
1445
1446 return 0;
1447}
1448
1449static int ar0832_set_effect_gamma(struct v4l2_subdev *sd, s32 val)
1450{
1451 u32 int_factor;
1452 int on, effect, old_mode;
1453 int err;
1454
1455 err = ar0832_readb(sd, AR0832_CATEGORY_MON, AR0832_MON_COLOR_EFFECT, &on);
1456 CHECK_ERR(err);
1457 if (on) {
1458 err = ar0832_writeb(sd, AR0832_CATEGORY_MON,
1459 AR0832_MON_COLOR_EFFECT, 0);
1460 CHECK_ERR(err);
1461 }
1462
1463 switch (val) {
1464 case IMAGE_EFFECT_NEGATIVE:
1465 effect = 0x01;
1466 break;
1467
1468 case IMAGE_EFFECT_AQUA:
1469 effect = 0x08;
1470 break;
1471 }
1472
1473 old_mode = ar0832_set_mode(sd, AR0832_PARMSET_MODE);
1474 CHECK_ERR(old_mode);
1475
1476 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM, AR0832_PARM_EFFECT, effect);
1477 CHECK_ERR(err);
1478
1479 if (old_mode == AR0832_MONITOR_MODE) {
1480 err = ar0832_set_mode(sd, old_mode);
1481 CHECK_ERR(err);
1482
1483 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
1484 if (!(int_factor & AR0832_INT_MODE)) {
1485 cam_err("AR0832_INT_MODE isn't issued, %#x\n",
1486 int_factor);
1487 return -ETIMEDOUT;
1488 }
1489 CHECK_ERR(err);
1490 }
1491
1492 return err;
1493}
1494
1495static int ar0832_set_effect(struct v4l2_subdev *sd, int val)
1496{
1497 int err;
1498 cam_dbg("E, value %d\n", val);
1499
1500retry:
1501 switch (val) {
1502 case IMAGE_EFFECT_NONE:
1503 case IMAGE_EFFECT_BNW:
1504 case IMAGE_EFFECT_SEPIA:
1505 err = ar0832_set_effect_color(sd, val);
1506 CHECK_ERR(err);
1507 break;
1508
1509 case IMAGE_EFFECT_AQUA:
1510 case IMAGE_EFFECT_NEGATIVE:
1511 err = ar0832_set_effect_gamma(sd, val);
1512 CHECK_ERR(err);
1513 break;
1514
1515 default:
1516 cam_warn("invalid value, %d\n", val);
1517 val = IMAGE_EFFECT_NONE;
1518 goto retry;
1519 }
1520
1521 cam_trace("X\n");
1522 return 0;
1523}
1524
1525static int ar0832_set_wdr(struct v4l2_subdev *sd, int val)
1526{
1527 int contrast, wdr, err;
1528
1529 cam_dbg("%s\n", val ? "on" : "off");
1530
1531 contrast = (val == 1 ? 0x09 : 0x05);
1532 wdr = (val == 1 ? 0x01 : 0x00);
1533
1534 err = ar0832_writeb(sd, AR0832_CATEGORY_MON,
1535 AR0832_MON_TONE_CTRL, contrast);
1536 CHECK_ERR(err);
1537 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
1538 AR0832_CAPPARM_WDR_EN, wdr);
1539 CHECK_ERR(err);
1540
1541 cam_trace("X\n");
1542 return 0;
1543}
1544
1545static int ar0832_set_antishake(struct v4l2_subdev *sd, int val)
1546{
1547 int ahs, err;
1548
1549 cam_dbg("%s\n", val ? "on" : "off");
1550
1551 ahs = (val == 1 ? 0x0E : 0x00);
1552
1553 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_EP_MODE_MON, ahs);
1554 CHECK_ERR(err);
1555 err = ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_EP_MODE_CAP, ahs);
1556 CHECK_ERR(err);
1557
1558 cam_trace("X\n");
1559 return 0;
1560}
1561
1562static int ar0832_set_face_beauty(struct v4l2_subdev *sd, int val)
1563{
1564 struct ar0832_state *state = to_state(sd);
1565 int err;
1566
1567 cam_dbg("%s\n", val ? "on" : "off");
1568
1569 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
1570 AR0832_CAPPARM_AFB_CAP_EN, val ? 0x01 : 0x00);
1571 CHECK_ERR(err);
1572
1573 state->beauty_mode = val;
1574
1575 cam_trace("X\n");
1576 return 0;
1577}
1578
1579static int ar0832_set_lock(struct v4l2_subdev *sd, int val)
1580{
1581 struct ar0832_state *state = to_state(sd);
1582
1583 cam_trace("%s\n", val ? "on" : "off");
1584
1585 ar0832_writeb(sd, AR0832_CATEGORY_AE, AR0832_AE_LOCK, val);
1586 ar0832_writeb(sd, AR0832_CATEGORY_WB, AR0832_AWB_LOCK, val);
1587 state->focus.lock = val;
1588
1589 cam_trace("X\n");
1590 return 0;
1591}
1592
1593static int ar0832_set_af(struct v4l2_subdev *sd, int val)
1594{
1595 struct ar0832_state *state = to_state(sd);
1596 int i, status, err;
1597
1598 cam_info("%s, mode %#x\n", val ? "start" : "stop", state->focus.mode);
1599
1600 state->focus.status = 0;
1601
1602 if (state->focus.mode != FOCUS_MODE_CONTINOUS) {
1603 err = ar0832_writeb(sd, AR0832_CATEGORY_LENS,
1604 AR0832_LENS_AF_START, val);
1605 CHECK_ERR(err);
1606
1607 if (!(state->focus.touch &&
1608 state->focus.mode == FOCUS_MODE_TOUCH)) {
1609 if (val && state->focus.lock) {
1610 ar0832_set_lock(sd, 0);
1611 msleep(100);
1612 }
1613 ar0832_set_lock(sd, val);
1614 }
1615
1616 /* check AF status for 6 sec */
1617 for (i = 600; i && err; i--) {
1618 msleep(10);
1619 err = ar0832_readb(sd, AR0832_CATEGORY_LENS,
1620 AR0832_LENS_AF_STATUS, &status);
1621 CHECK_ERR(err);
1622
1623 if (!(status & 0x01))
1624 err = 0;
1625 }
1626
1627 state->focus.status = status;
1628 } else {
1629 err = ar0832_writeb(sd, AR0832_CATEGORY_LENS,
1630 AR0832_LENS_AF_START, val ? 0x02 : 0x00);
1631 CHECK_ERR(err);
1632
1633 err = -EBUSY;
1634 for (i = AR0832_I2C_VERIFY; i && err; i--) {
1635 msleep(10);
1636 err = ar0832_readb(sd, AR0832_CATEGORY_LENS,
1637 AR0832_LENS_AF_STATUS, &status);
1638 CHECK_ERR(err);
1639
1640 if ((val && status == 0x05) || (!val && status != 0x05))
1641 err = 0;
1642 }
1643 }
1644
1645 cam_dbg("X\n");
1646 return err;
1647}
1648
1649static int ar0832_set_af_mode(struct v4l2_subdev *sd, int val)
1650{
1651 struct ar0832_state *state = to_state(sd);
1652 struct regulator *movie = regulator_get(NULL, "led_movie");
1653 u32 cancel, mode, status = 0;
1654 int i, err;
1655
1656 cancel = val & FOCUS_MODE_DEFAULT;
1657 val &= 0xFF;
1658
1659retry:
1660 switch (val) {
1661 case FOCUS_MODE_AUTO:
1662 mode = 0x00;
1663 break;
1664
1665 case FOCUS_MODE_MACRO:
1666 mode = 0x01;
1667 break;
1668
1669 case FOCUS_MODE_CONTINOUS:
1670 mode = 0x02;
1671 cancel = 0;
1672 break;
1673
1674 case FOCUS_MODE_FACEDETECT:
1675 mode = 0x03;
1676 break;
1677
1678 case FOCUS_MODE_TOUCH:
1679 mode = 0x04;
1680 cancel = 0;
1681 break;
1682
1683 case FOCUS_MODE_INFINITY:
1684 mode = 0x06;
1685 cancel = 0;
1686 break;
1687
1688 default:
1689 cam_warn("invalid value, %d", val);
1690 val = FOCUS_MODE_AUTO;
1691 goto retry;
1692 }
1693
1694 if (cancel) {
1695 ar0832_set_af(sd, 0);
1696 ar0832_set_lock(sd, 0);
1697 } else {
1698 if (state->focus.mode == val)
1699 return 0;
1700 }
1701
1702 cam_dbg("E, value %d\n", val);
1703
1704 if (val == FOCUS_MODE_FACEDETECT) {
1705 /* enable face detection */
1706 err = ar0832_writeb(sd, AR0832_CATEGORY_FD, AR0832_FD_CTL, 0x11);
1707 CHECK_ERR(err);
1708 msleep(10);
1709 } else if (state->focus.mode == FOCUS_MODE_FACEDETECT) {
1710 /* disable face detection */
1711 err = ar0832_writeb(sd, AR0832_CATEGORY_FD, AR0832_FD_CTL, 0x00);
1712 CHECK_ERR(err);
1713 }
1714
1715 if (val == FOCUS_MODE_MACRO)
1716 regulator_set_current_limit(movie, 15000, 17000);
1717 else if (state->focus.mode == FOCUS_MODE_MACRO)
1718 regulator_set_current_limit(movie, 90000, 110000);
1719
1720 state->focus.mode = val;
1721
1722 err = ar0832_writeb(sd, AR0832_CATEGORY_LENS, AR0832_LENS_AF_MODE, mode);
1723 CHECK_ERR(err);
1724
1725 for (i = AR0832_I2C_VERIFY; i; i--) {
1726 msleep(10);
1727 err = ar0832_readb(sd, AR0832_CATEGORY_LENS,
1728 AR0832_LENS_AF_STATUS, &status);
1729 CHECK_ERR(err);
1730
1731 if (!(status & 0x01))
1732 break;
1733 }
1734
1735 if ((status & 0x01) != 0x00) {
1736 cam_err("failed\n");
1737 return -ETIMEDOUT;
1738 }
1739
1740 cam_trace("X\n");
1741 return 0;
1742}
1743
1744static int ar0832_set_touch_auto_focus(struct v4l2_subdev *sd, int val)
1745{
1746 struct ar0832_state *state = to_state(sd);
1747 int err;
1748 cam_info("%s\n", val ? "start" : "stop");
1749
1750 state->focus.touch = val;
1751
1752 if (val) {
1753 err = ar0832_set_af_mode(sd, FOCUS_MODE_TOUCH);
1754 if (err < 0) {
1755 cam_err("ar0832_set_af_mode failed\n");
1756 return err;
1757 }
1758 err = ar0832_writew(sd, AR0832_CATEGORY_LENS,
1759 AR0832_LENS_AF_TOUCH_POSX, state->focus.pos_x);
1760 CHECK_ERR(err);
1761 err = ar0832_writew(sd, AR0832_CATEGORY_LENS,
1762 AR0832_LENS_AF_TOUCH_POSY, state->focus.pos_y);
1763 CHECK_ERR(err);
1764 }
1765
1766 cam_trace("X\n");
1767 return err;
1768}
1769
1770static int ar0832_set_zoom(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1771{
1772 struct ar0832_state *state = to_state(sd);
1773 struct v4l2_queryctrl qc = {0,};
1774 int val = ctrl->value, err;
1775 int zoom[] = { 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19,
1776 20, 21, 22, 24, 25, 26, 28, 29, 30, 31, 32, 34, 35, 36, 38, 39};
1777 cam_dbg("E, value %d\n", val);
1778
1779 qc.id = ctrl->id;
1780 ar0832_queryctrl(sd, &qc);
1781
1782 if (val < qc.minimum || val > qc.maximum) {
1783 cam_warn("invalied value, %d\n", val);
1784 val = qc.default_value;
1785 }
1786
1787 err = ar0832_writeb(sd, AR0832_CATEGORY_MON, AR0832_MON_ZOOM, zoom[val]);
1788 CHECK_ERR(err);
1789
1790 state->zoom = val;
1791
1792 cam_trace("X\n");
1793 return 0;
1794}
1795
1796static int ar0832_set_jpeg_quality(struct v4l2_subdev *sd,
1797 struct v4l2_control *ctrl)
1798{
1799 struct v4l2_queryctrl qc = {0,};
1800 int val = ctrl->value, ratio, err;
1801 cam_dbg("E, value %d\n", val);
1802
1803 qc.id = ctrl->id;
1804 ar0832_queryctrl(sd, &qc);
1805
1806 if (val < qc.minimum || val > qc.maximum) {
1807 cam_warn("invalied value, %d\n", val);
1808 val = qc.default_value;
1809 }
1810
1811 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
1812 AR0832_CAPPARM_JPEG_RATIO, 0x62);
1813 CHECK_ERR(err);
1814
1815 if (val <= 65) /* Normal */
1816 ratio = 0x0A;
1817 else if (val <= 75) /* Fine */
1818 ratio = 0x05;
1819 else /* Superfine */
1820 ratio = 0x00;
1821
1822 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
1823 AR0832_CAPPARM_JPEG_RATIO_OFS, ratio);
1824 CHECK_ERR(err);
1825
1826 cam_trace("X\n");
1827 return 0;
1828}
1829
1830static int ar0832_get_exif(struct v4l2_subdev *sd)
1831{
1832 struct ar0832_state *state = to_state(sd);
1833 /* standard values */
1834 u16 iso_std_values[] = { 10, 12, 16, 20, 25, 32, 40, 50, 64, 80,
1835 100, 125, 160, 200, 250, 320, 400, 500, 640, 800,
1836 1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000, 6400, 8000};
1837 /* quantization table */
1838 u16 iso_qtable[] = { 11, 14, 17, 22, 28, 35, 44, 56, 71, 89,
1839 112, 141, 178, 224, 282, 356, 449, 565, 712, 890,
1840 1122, 1414, 1782, 2245, 2828, 3564, 4490, 5657, 7127, 8909};
1841 int num, den, i, err;
1842
1843 /* exposure time */
1844 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF,
1845 AR0832_EXIF_EXPTIME_NUM, &num);
1846 CHECK_ERR(err);
1847 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF,
1848 AR0832_EXIF_EXPTIME_DEN, &den);
1849 CHECK_ERR(err);
1850 state->exif.exptime = (u32)num*1000/den;
1851
1852 /* flash */
1853 err = ar0832_readw(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_FLASH, &num);
1854 CHECK_ERR(err);
1855 state->exif.flash = (u16)num;
1856
1857 /* iso */
1858 err = ar0832_readw(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_ISO, &num);
1859 CHECK_ERR(err);
1860 for (i = 0; i < ARRAY_SIZE(iso_qtable); i++) {
1861 if (num <= iso_qtable[i]) {
1862 state->exif.iso = iso_std_values[i];
1863 break;
1864 }
1865 }
1866
1867 /* shutter speed */
1868 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_TV_NUM, &num);
1869 CHECK_ERR(err);
1870 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_TV_DEN, &den);
1871 CHECK_ERR(err);
1872 state->exif.tv = num*AR0832_DEF_APEX_DEN/den;
1873
1874 /* brightness */
1875 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_BV_NUM, &num);
1876 CHECK_ERR(err);
1877 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_BV_DEN, &den);
1878 CHECK_ERR(err);
1879 state->exif.bv = num*AR0832_DEF_APEX_DEN/den;
1880
1881 /* exposure */
1882 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_EBV_NUM, &num);
1883 CHECK_ERR(err);
1884 err = ar0832_readl(sd, AR0832_CATEGORY_EXIF, AR0832_EXIF_EBV_DEN, &den);
1885 CHECK_ERR(err);
1886 state->exif.ebv = num*AR0832_DEF_APEX_DEN/den;
1887
1888 return err;
1889}
1890
1891static int ar0832_start_capture(struct v4l2_subdev *sd, int val)
1892{
1893 struct ar0832_state *state = to_state(sd);
1894 int err, int_factor;
1895 cam_trace("E\n");
1896
1897 if (!(state->isp.int_factor & AR0832_INT_CAPTURE)) {
1898 int_factor = ar0832_wait_interrupt(sd,
1899 state->beauty_mode ? AR0832_ISP_AFB_TIMEOUT : AR0832_ISP_TIMEOUT);
1900 if (!(int_factor & AR0832_INT_CAPTURE)) {
1901 cam_warn("AR0832_INT_CAPTURE isn't issued, %#x\n", int_factor);
1902 return -ETIMEDOUT;
1903 }
1904 }
1905
1906 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPCTRL, AR0832_CAPCTRL_FRM_SEL, 0x01);
1907 CHECK_ERR(err);
1908
1909 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPCTRL,
1910 AR0832_CAPCTRL_TRANSFER, 0x01);
1911 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
1912 if (!(int_factor & AR0832_INT_CAPTURE)) {
1913 cam_warn("AR0832_INT_CAPTURE isn't issued on transfer, %#x\n", int_factor);
1914 return -ETIMEDOUT;
1915 }
1916
1917 err = ar0832_readl(sd, AR0832_CATEGORY_CAPCTRL, AR0832_CAPCTRL_IMG_SIZE,
1918 &state->jpeg.main_size);
1919 CHECK_ERR(err);
1920 err = ar0832_readl(sd, AR0832_CATEGORY_CAPCTRL, AR0832_CAPCTRL_THUMB_SIZE,
1921 &state->jpeg.thumb_size);
1922 CHECK_ERR(err);
1923
1924 state->jpeg.main_offset = 0;
1925 state->jpeg.thumb_offset = AR0832_JPEG_MAXSIZE;
1926 state->jpeg.postview_offset = AR0832_JPEG_MAXSIZE + AR0832_THUMB_MAXSIZE;
1927
1928 ar0832_get_exif(sd);
1929
1930 cam_trace("X\n");
1931 return err;
1932}
1933
1934static int ar0832_set_hdr(struct v4l2_subdev *sd, int val)
1935{
1936 u32 int_factor;
1937 int err;
1938 cam_trace("E\n");
1939
1940 switch (val) {
1941 case 0:
1942 err = ar0832_set_mode(sd, AR0832_MONITOR_MODE);
1943 CHECK_ERR(err);
1944 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
1945 if (!(int_factor & AR0832_INT_MODE)) {
1946 cam_err("AR0832_INT_MODE isn't issued, %#x\n",
1947 int_factor);
1948 return -ETIMEDOUT;
1949 }
1950 break;
1951 case 1:
1952 case 2:
1953 err = ar0832_writeb(sd, AR0832_CATEGORY_SYS,
1954 AR0832_SYS_ROOT_EN, 0x01);
1955 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
1956 break;
1957 default:
1958 cam_err("invalid HDR count\n");
1959 }
1960
1961 cam_trace("X\n");
1962 return 0;
1963}
1964
1965static int ar0832_check_dataline(struct v4l2_subdev *sd, int val)
1966{
1967 int err = 0;
1968
1969 cam_dbg("E, value %d\n", val);
1970
1971 err = ar0832_writeb(sd, AR0832_CATEGORY_TEST,
1972 AR0832_TEST_OUTPUT_YCO_TEST_DATA, val ? 0x01 : 0x00);
1973 CHECK_ERR(err);
1974
1975 cam_trace("X\n");
1976 return 0;
1977}
1978
1979static int ar0832_check_esd(struct v4l2_subdev *sd)
1980{
1981 s32 val = 0;
1982 int err = 0;
1983
1984 /* check ISP */
1985 err = ar0832_readb(sd, AR0832_CATEGORY_TEST, AR0832_TEST_ISP_PROCESS, &val);
1986 CHECK_ERR(err);
1987 cam_dbg("progress %#x\n", val);
1988
1989 if (val != 0x80) {
1990 goto esd_occur;
1991 } else {
1992 ar0832_wait_interrupt(sd, AR0832_ISP_ESD_TIMEOUT);
1993
1994 err = ar0832_readb(sd, AR0832_CATEGORY_SYS, AR0832_SYS_ESD_INT, &val);
1995 CHECK_ERR(err);
1996
1997 if (val & AR0832_INT_ESD)
1998 goto esd_occur;
1999 }
2000
2001 cam_warn("ESD is not detected\n");
2002 return 0;
2003
2004esd_occur:
2005 cam_warn("ESD shock is detected\n");
2006 return -EIO;
2007}
2008
2009static int ar0832_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2010{
2011 struct ar0832_state *state = to_state(sd);
2012 int err = 0;
2013
2014 printk(KERN_INFO "id %d, value %d\n",
2015 ctrl->id - V4L2_CID_PRIVATE_BASE, ctrl->value);
2016
2017 if (unlikely(state->isp.bad_fw && ctrl->id != V4L2_CID_CAM_UPDATE_FW)) {
2018 cam_err("\"Unknown\" state, please update F/W");
2019 return -ENOSYS;
2020 }
2021
2022 switch (ctrl->id) {
2023 case V4L2_CID_CAM_UPDATE_FW:
2024 if (ctrl->value == FW_MODE_DUMP)
2025 err = ar0832_dump_fw(sd);
2026 else
2027 err = ar0832_check_fw(sd);
2028 break;
2029
2030 case V4L2_CID_CAMERA_SENSOR_MODE:
2031 err = ar0832_set_sensor_mode(sd, ctrl->value);
2032 break;
2033
2034 case V4L2_CID_CAMERA_FLASH_MODE:
2035 err = ar0832_set_flash(sd, ctrl->value, 0);
2036 break;
2037
2038 case V4L2_CID_CAMERA_ISO:
2039 err = ar0832_set_iso(sd, ctrl);
2040 break;
2041
2042 case V4L2_CID_CAMERA_METERING:
2043 if (state->sensor_mode == SENSOR_CAMERA)
2044 err = ar0832_set_metering(sd, ctrl->value);
2045 break;
2046
2047 case V4L2_CID_CAMERA_BRIGHTNESS:
2048 err = ar0832_set_exposure(sd, ctrl);
2049 break;
2050
2051 case V4L2_CID_CAMERA_WHITE_BALANCE:
2052 err = ar0832_set_whitebalance(sd, ctrl->value);
2053 break;
2054
2055 case V4L2_CID_CAMERA_SCENE_MODE:
2056 err = ar0832_set_scene_mode(sd, ctrl->value);
2057 break;
2058
2059 case V4L2_CID_CAMERA_EFFECT:
2060 err = ar0832_set_effect(sd, ctrl->value);
2061 break;
2062
2063 case V4L2_CID_CAMERA_WDR:
2064 err = ar0832_set_wdr(sd, ctrl->value);
2065 break;
2066
2067 case V4L2_CID_CAMERA_ANTI_SHAKE:
2068 err = ar0832_set_antishake(sd, ctrl->value);
2069 break;
2070
2071 case V4L2_CID_CAMERA_BEAUTY_SHOT:
2072 err = ar0832_set_face_beauty(sd, ctrl->value);
2073 break;
2074
2075 case V4L2_CID_CAMERA_FOCUS_MODE:
2076 err = ar0832_set_af_mode(sd, ctrl->value);
2077 break;
2078
2079 case V4L2_CID_CAMERA_SET_AUTO_FOCUS:
2080 err = ar0832_set_af(sd, ctrl->value);
2081 break;
2082
2083 case V4L2_CID_CAMERA_OBJECT_POSITION_X:
2084 state->focus.pos_x = ctrl->value;
2085 break;
2086
2087 case V4L2_CID_CAMERA_OBJECT_POSITION_Y:
2088 state->focus.pos_y = ctrl->value;
2089 break;
2090
2091 case V4L2_CID_CAMERA_TOUCH_AF_START_STOP:
2092 err = ar0832_set_touch_auto_focus(sd, ctrl->value);
2093 break;
2094
2095 case V4L2_CID_CAMERA_ZOOM:
2096 err = ar0832_set_zoom(sd, ctrl);
2097 break;
2098
2099 case V4L2_CID_CAM_JPEG_QUALITY:
2100 err = ar0832_set_jpeg_quality(sd, ctrl);
2101 break;
2102
2103 case V4L2_CID_CAMERA_CAPTURE:
2104 err = ar0832_start_capture(sd, ctrl->value);
2105 break;
2106
2107 case V4L2_CID_CAMERA_HDR:
2108 err = ar0832_set_hdr(sd, ctrl->value);
2109 break;
2110
2111 case V4L2_CID_CAMERA_VT_MODE:
2112 state->vt_mode = ctrl->value;
2113 break;
2114
2115 case V4L2_CID_CAMERA_CHECK_DATALINE:
2116 state->check_dataline = ctrl->value;
2117 break;
2118
2119 case V4L2_CID_CAMERA_CHECK_ESD:
2120 err = ar0832_check_esd(sd);
2121 break;
2122
2123 default:
2124 cam_err("no such control id %d, value %d\n",
2125 ctrl->id - V4L2_CID_PRIVATE_BASE, ctrl->value);
2126 /*err = -ENOIOCTLCMD;*/
2127 err = 0;
2128 break;
2129 }
2130
2131 if (err < 0 && err != -ENOIOCTLCMD)
2132 cam_err("failed, id %d, value %d\n",
2133 ctrl->id - V4L2_CID_PRIVATE_BASE, ctrl->value);
2134 return err;
2135}
2136
2137static int ar0832_g_ext_ctrl(struct v4l2_subdev *sd, struct v4l2_ext_control *ctrl)
2138{
2139 struct ar0832_state *state = to_state(sd);
2140 int err = 0;
2141
2142 switch (ctrl->id) {
2143 case V4L2_CID_CAM_SENSOR_FW_VER:
2144 strcpy(ctrl->string, state->exif.unique_id);
2145 break;
2146
2147 default:
2148 cam_err("no such control id %d\n", ctrl->id - V4L2_CID_CAMERA_CLASS_BASE);
2149 /*err = -ENOIOCTLCMD*/
2150 err = 0;
2151 break;
2152 }
2153
2154 if (err < 0 && err != -ENOIOCTLCMD)
2155 cam_err("failed, id %d\n", ctrl->id - V4L2_CID_CAMERA_CLASS_BASE);
2156
2157 return err;
2158}
2159
2160static int ar0832_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
2161{
2162 struct v4l2_ext_control *ctrl = ctrls->controls;
2163 int i, err = 0;
2164
2165 for (i = 0; i < ctrls->count; i++, ctrl++) {
2166 err = ar0832_g_ext_ctrl(sd, ctrl);
2167 if (err) {
2168 ctrls->error_idx = i;
2169 break;
2170 }
2171 }
2172 return err;
2173}
2174
2175static int ar0832_check_manufacturer_id(struct v4l2_subdev *sd)
2176{
2177 int i, err;
2178 u8 id;
2179 u32 addr[] = {0x1000AAAA, 0x10005554, 0x1000AAAA};
2180 u8 val[3][2] = {
2181 [0] = {0x00, 0xAA},
2182 [1] = {0x00, 0x55},
2183 [2] = {0x00, 0x90},
2184 };
2185 u8 reset[] = {0x00, 0xF0};
2186
2187 /* set manufacturer's ID read-mode */
2188 for (i = 0; i < 3; i++) {
2189 err = ar0832_mem_write(sd, 0x06, 2, addr[i], val[i]);
2190 CHECK_ERR(err);
2191 }
2192
2193 /* read manufacturer's ID */
2194 err = ar0832_mem_read(sd, sizeof(id), 0x10000001, &id);
2195 CHECK_ERR(err);
2196
2197 /* reset manufacturer's ID read-mode */
2198 err = ar0832_mem_write(sd, 0x06, sizeof(reset), 0x10000000, reset);
2199 CHECK_ERR(err);
2200
2201 cam_dbg("%#x\n", id);
2202
2203 return id;
2204}
2205
2206static int ar0832_program_fw(struct v4l2_subdev *sd,
2207 u8 *buf, u32 addr, u32 unit, u32 count, u8 id)
2208{
2209 u32 val;
2210 u32 intram_unit = SZ_4K;
2211 int i, j, retries, err = 0;
2212 int erase = 0x01;
2213 if (unit == SZ_64K && id != 0x01)
2214 erase = 0x04;
2215
2216 for (i = 0; i < unit*count; i += unit) {
2217 /* Set Flash ROM memory address */
2218 err = ar0832_writel(sd, AR0832_CATEGORY_FLASH,
2219 AR0832_FLASH_ADDR, addr + i);
2220 CHECK_ERR(err);
2221
2222 /* Erase FLASH ROM entire memory */
2223 err = ar0832_writeb(sd, AR0832_CATEGORY_FLASH,
2224 AR0832_FLASH_ERASE, erase);
2225 CHECK_ERR(err);
2226 /* Response while sector-erase is operating */
2227 retries = 0;
2228 do {
2229 mdelay(50);
2230 err = ar0832_readb(sd, AR0832_CATEGORY_FLASH,
2231 AR0832_FLASH_ERASE, &val);
2232 CHECK_ERR(err);
2233 } while (val == erase && retries++ < AR0832_I2C_VERIFY);
2234
2235 if (val != 0) {
2236 cam_err("failed to erase sector\n");
2237 return -1;
2238 }
2239
2240 /* Set FLASH ROM programming size */
2241 err = ar0832_writew(sd, AR0832_CATEGORY_FLASH, AR0832_FLASH_BYTE,
2242 unit == SZ_64K ? 0 : unit);
2243 CHECK_ERR(err);
2244
2245 /* Clear M-5MoLS internal RAM */
2246 err = ar0832_writeb(sd, AR0832_CATEGORY_FLASH,
2247 AR0832_FLASH_RAM_CLEAR, 0x01);
2248 CHECK_ERR(err);
2249
2250 /* Set Flash ROM programming address */
2251 err = ar0832_writel(sd, AR0832_CATEGORY_FLASH,
2252 AR0832_FLASH_ADDR, addr + i);
2253 CHECK_ERR(err);
2254
2255 /* Send programmed firmware */
2256 for (j = 0; j < unit; j += intram_unit) {
2257 err = ar0832_mem_write(sd, 0x04, intram_unit,
2258 AR0832_INT_RAM_BASE_ADDR + j, buf + i + j);
2259 CHECK_ERR(err);
2260 mdelay(10);
2261 }
2262
2263 /* Start Programming */
2264 err = ar0832_writeb(sd, AR0832_CATEGORY_FLASH, AR0832_FLASH_WR, 0x01);
2265 CHECK_ERR(err);
2266
2267 /* Confirm programming has been completed */
2268 retries = 0;
2269 do {
2270 mdelay(50);
2271 err = ar0832_readb(sd, AR0832_CATEGORY_FLASH,
2272 AR0832_FLASH_WR, &val);
2273 CHECK_ERR(err);
2274 } while (val && retries++ < AR0832_I2C_VERIFY);
2275
2276 if (val != 0) {
2277 cam_err("failed to program\n");
2278 return -1;
2279 }
2280 }
2281
2282 return 0;
2283}
2284
2285static int ar0832_load_fw(struct v4l2_subdev *sd)
2286{
2287 struct i2c_client *client = v4l2_get_subdevdata(sd);
2288 struct device *dev = &client->adapter->dev;
2289 const struct firmware *fentry;
2290 u8 sensor_ver[AR0832_FW_VER_LEN] = {0, };
2291 u8 *buf = NULL, val, id;
2292 int offset, err;
2293
2294#ifdef SDCARD_FW
2295 struct file *fp;
2296 mm_segment_t old_fs;
2297 long fsize, nread;
2298 int fw_requested = 1;
2299
2300 old_fs = get_fs();
2301 set_fs(KERNEL_DS);
2302
2303 fp = filp_open(AR0832_FW_PATH, O_RDONLY, 0);
2304 if (IS_ERR(fp)) {
2305 cam_trace("failed to open %s, err %ld\n",
2306 AR0832_FW_PATH, PTR_ERR(fp));
2307 goto request_fw;
2308 }
2309
2310 fw_requested = 0;
2311 fsize = fp->f_path.dentry->d_inode->i_size;
2312
2313 cam_dbg("start, file path %s, size %ld Bytes\n", AR0832_FW_PATH, fsize);
2314
2315 buf = vmalloc(fsize);
2316 if (!buf) {
2317 cam_err("failed to allocate memory\n");
2318 err = -ENOMEM;
2319 goto out;
2320 }
2321
2322 nread = vfs_read(fp, (char __user *)buf, fsize, &fp->f_pos);
2323 if (nread != fsize) {
2324 cam_err("failed to read firmware file, %ld Bytes\n", nread);
2325 err = -EIO;
2326 goto out;
2327 }
2328
2329request_fw:
2330 if (fw_requested) {
2331 set_fs(old_fs);
2332#endif /* SDCARD_FW */
2333 ar0832_get_sensor_fw_version(sd, sensor_ver);
2334
2335 if (sensor_ver[0] == 'T')
2336 err = request_firmware(&fentry, AR0832T_FW_REQUEST_PATH, dev);
2337 else
2338 err = request_firmware(&fentry, AR0832O_FW_REQUEST_PATH, dev);
2339
2340 if (err != 0) {
2341 cam_err("request_firmware falied\n");
2342 err = -EINVAL;
2343 goto out;
2344 }
2345
2346 cam_dbg("start, size %d Bytes\n", fentry->size);
2347 buf = (u8 *)fentry->data;
2348
2349#ifdef SDCARD_FW
2350 }
2351#endif /* SDCARD_FW */
2352
2353 /* set pin */
2354 val = 0x7E;
2355 err = ar0832_mem_write(sd, 0x04, sizeof(val), 0x50000308, &val);
2356 if (err < 0) {
2357 cam_err("i2c falied, err %d\n", err);
2358 goto out;
2359 }
2360
2361 id = ar0832_check_manufacturer_id(sd);
2362 if (id < 0) {
2363 cam_err("i2c falied, err %d\n", id);
2364 goto out;
2365 }
2366
2367 /* select flash memory */
2368 err = ar0832_writeb(sd, AR0832_CATEGORY_FLASH,
2369 AR0832_FLASH_SEL, id == 0x01 ? 0x00 : 0x01);
2370 if (err < 0) {
2371 cam_err("i2c falied, err %d\n", err);
2372 goto out;
2373 }
2374
2375 /* program FLSH ROM */
2376 err = ar0832_program_fw(sd, buf, AR0832_FLASH_BASE_ADDR, SZ_64K, 31, id);
2377 if (err < 0)
2378 goto out;
2379
2380 offset = SZ_64K * 31;
2381 if (id == 0x01) {
2382 err = ar0832_program_fw(sd,
2383 buf + offset, AR0832_FLASH_BASE_ADDR + offset, SZ_8K, 4, id);
2384 } else {
2385 err = ar0832_program_fw(sd,
2386 buf + offset, AR0832_FLASH_BASE_ADDR + offset, SZ_4K, 8, id);
2387 }
2388
2389 cam_dbg("end\n");
2390
2391out:
2392#ifdef SDCARD_FW
2393 if (!fw_requested) {
2394 vfree(buf);
2395 filp_close(fp, current->files);
2396 set_fs(old_fs);
2397 }
2398#endif /* SDCARD_FW */
2399 return err;
2400}
2401
2402/*
2403 * v4l2_subdev_video_ops
2404 */
2405static const struct ar0832_frmsizeenum *ar0832_get_frmsize
2406 (const struct ar0832_frmsizeenum *frmsizes, int num_entries, int index)
2407{
2408 int i;
2409
2410 for (i = 0; i < num_entries; i++) {
2411 if (frmsizes[i].index == index)
2412 return &frmsizes[i];
2413 }
2414
2415 return NULL;
2416}
2417
2418static int ar0832_set_frmsize(struct v4l2_subdev *sd)
2419{
2420 struct ar0832_state *state = to_state(sd);
2421 struct v4l2_control ctrl;
2422 int err;
2423 u32 old_mode;
2424 cam_trace("E\n");
2425
2426 if (state->format_mode != V4L2_PIX_FMT_MODE_CAPTURE) {
2427 err = ar0832_set_mode(sd, AR0832_PARMSET_MODE);
2428 err = ar0832_readb(sd, AR0832_CATEGORY_SYS, AR0832_SYS_MODE, &old_mode);
2429
2430 CHECK_ERR(err);
2431
2432 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM,
2433 AR0832_PARM_MON_SIZE, state->preview->reg_val);
2434 CHECK_ERR(err);
2435
2436 if (state->zoom) {
2437 /* Zoom position returns to 1 when the monitor size is changed. */
2438 ctrl.id = V4L2_CID_CAMERA_ZOOM;
2439 ctrl.value = state->zoom;
2440 ar0832_set_zoom(sd, &ctrl);
2441 }
2442
2443 cam_info("preview frame size %dx%d\n",
2444 state->preview->width, state->preview->height);
2445 } else {
2446 err = ar0832_writeb(sd, AR0832_CATEGORY_CAPPARM,
2447 AR0832_CAPPARM_MAIN_IMG_SIZE, state->capture->reg_val);
2448 CHECK_ERR(err);
2449 cam_info("capture frame size %dx%d\n",
2450 state->capture->width, state->capture->height);
2451 }
2452
2453 cam_trace("X\n");
2454 return 0;
2455}
2456
2457static int ar0832_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *ffmt)
2458{
2459 struct ar0832_state *state = to_state(sd);
2460 const struct ar0832_frmsizeenum **frmsize;
2461
2462 u32 width = ffmt->width;
2463 u32 height = ffmt->height;
2464 u32 tmp_width;
2465 u32 old_index;
2466 int i, num_entries;
2467 cam_trace("E\n");
2468
2469 if (unlikely(state->isp.bad_fw)) {
2470 cam_err("\"Unknown\" state, please update F/W");
2471 return -ENOSYS;
2472 }
2473 if (ffmt->width < ffmt->height) {
2474 tmp_width = ffmt->height;
2475 height = ffmt->width;
2476 width = tmp_width;
2477 }
2478
2479 if (ffmt->colorspace == V4L2_COLORSPACE_JPEG) {
2480 state->format_mode = V4L2_PIX_FMT_MODE_CAPTURE;
2481 frmsize = &state->capture;
2482 } else {
2483 state->format_mode = V4L2_PIX_FMT_MODE_PREVIEW;
2484 frmsize = &state->preview;
2485 }
2486
2487 old_index = *frmsize ? (*frmsize)->index : -1;
2488 *frmsize = NULL;
2489
2490 if (state->format_mode != V4L2_PIX_FMT_MODE_CAPTURE) {
2491 num_entries = ARRAY_SIZE(preview_frmsizes);
2492 for (i = 0; i < num_entries; i++) {
2493 if (width == preview_frmsizes[i].width &&
2494 height == preview_frmsizes[i].height) {
2495 *frmsize = &preview_frmsizes[i];
2496 break;
2497 }
2498 }
2499 } else {
2500 num_entries = ARRAY_SIZE(capture_frmsizes);
2501 for (i = 0; i < num_entries; i++) {
2502 if (width == capture_frmsizes[i].width &&
2503 height == capture_frmsizes[i].height) {
2504 *frmsize = &capture_frmsizes[i];
2505 break;
2506 }
2507 }
2508 }
2509
2510 if (*frmsize == NULL) {
2511 cam_warn("invalid frame size %dx%d\n", width, height);
2512 *frmsize = state->format_mode != V4L2_PIX_FMT_MODE_CAPTURE ?
2513 ar0832_get_frmsize(preview_frmsizes, num_entries,
2514 AR0832_PREVIEW_VGA) :
2515 ar0832_get_frmsize(capture_frmsizes, num_entries,
2516 AR0832_CAPTURE_3MP);
2517 }
2518
2519 cam_dbg("%dx%d\n", (*frmsize)->width, (*frmsize)->height);
2520 ar0832_set_frmsize(sd);
2521
2522 cam_trace("X\n");
2523 return 0;
2524}
2525
2526static int ar0832_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
2527{
2528 struct ar0832_state *state = to_state(sd);
2529
2530 a->parm.capture.timeperframe.numerator = 1;
2531 a->parm.capture.timeperframe.denominator = state->fps;
2532
2533 return 0;
2534}
2535
2536static int ar0832_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
2537{
2538 struct ar0832_state *state = to_state(sd);
2539 int err;
2540
2541 u32 fps = a->parm.capture.timeperframe.denominator /
2542 a->parm.capture.timeperframe.numerator;
2543
2544 if (unlikely(state->isp.bad_fw)) {
2545 cam_err("\"Unknown\" state, please update F/W");
2546 return -ENOSYS;
2547 }
2548
2549 if (fps != state->fps) {
2550 if (fps <= 0 || fps > 30) {
2551 cam_err("invalid frame rate %d\n", fps);
2552 fps = 30;
2553 }
2554 state->fps = fps;
2555 }
2556
2557 err = ar0832_set_mode(sd, AR0832_PARMSET_MODE);
2558 CHECK_ERR(err);
2559
2560 cam_dbg("fixed fps %d\n", state->fps);
2561 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM,
2562 AR0832_PARM_FLEX_FPS, state->fps != 30 ? state->fps : 0);
2563 CHECK_ERR(err);
2564
2565 return 0;
2566}
2567
2568static int ar0832_enum_framesizes(struct v4l2_subdev *sd,
2569 struct v4l2_frmsizeenum *fsize)
2570{
2571 struct ar0832_state *state = to_state(sd);
2572 u32 err, old_mode;
2573 err = ar0832_readb(sd, AR0832_CATEGORY_SYS, AR0832_SYS_MODE, &old_mode);
2574
2575 /*
2576 * The camera interface should read this value, this is the resolution
2577 * at which the sensor would provide framedata to the camera i/f
2578 * In case of image capture,
2579 * this returns the default camera resolution (VGA)
2580 */
2581 if (state->format_mode != V4L2_PIX_FMT_MODE_CAPTURE) {
2582 if (state->preview == NULL || state->preview->index < 0)
2583 return -EINVAL;
2584
2585 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
2586 fsize->discrete.width = state->preview->width;
2587 fsize->discrete.height = state->preview->height;
2588// fsize->discrete.width = 3272;
2589// fsize->discrete.height =2456;
2590 } else {
2591 if (state->capture == NULL || state->capture->index < 0)
2592 return -EINVAL;
2593
2594 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
2595 fsize->discrete.width = state->capture->width;
2596 fsize->discrete.height = state->capture->height;
2597 }
2598
2599 return 0;
2600}
2601
2602static int ar0832_s_stream_preview(struct v4l2_subdev *sd, int enable)
2603{
2604 struct ar0832_state *state = to_state(sd);
2605 u32 old_mode, int_factor;
2606 int err;
2607
2608 err = ar0832_readb(sd, AR0832_CATEGORY_SYS, AR0832_SYS_MODE, &old_mode);
2609
2610 if (enable) {
2611 ar0832_set_lock(sd, 0);
2612
2613 if (state->vt_mode) {
2614 printk("vt mode\n");
2615 err = ar0832_writeb(sd, AR0832_CATEGORY_AE,
2616 AR0832_AE_EP_MODE_MON, 0x11);
2617 CHECK_ERR(err);
2618 }
2619
2620 old_mode = ar0832_set_mode(sd, AR0832_MONITOR_MODE);
2621 if (old_mode <= 0) {
2622 cam_err("failed to set mode\n");
2623 return old_mode;
2624 }
2625
2626 if (old_mode != AR0832_MONITOR_MODE) {
2627 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
2628 if (!(int_factor & AR0832_INT_MODE)) {
2629 cam_err("AR0832_INT_MODE isn't issued, %#x\n",
2630 int_factor);
2631 return -ETIMEDOUT;
2632 }
2633 }
2634
2635 if (state->check_dataline) {
2636 err = ar0832_check_dataline(sd, state->check_dataline);
2637 CHECK_ERR(err);
2638 }
2639 } else {
2640 err = ar0832_readb(sd, AR0832_CATEGORY_SYS, AR0832_SYS_MODE, &old_mode);
2641 }
2642
2643 return 0;
2644}
2645
2646static int ar0832_s_stream_capture(struct v4l2_subdev *sd, int enable)
2647{
2648 u32 int_factor;
2649 int err;
2650
2651 if (enable) {
2652 err = ar0832_set_mode(sd, AR0832_STILLCAP_MODE);
2653 if (err <= 0) {
2654 cam_err("failed to set mode\n");
2655 return err;
2656 }
2657
2658 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
2659 if (!(int_factor & AR0832_INT_SOUND)) {
2660 cam_err("AR0832_INT_SOUND isn't issued, %#x\n",
2661 int_factor);
2662 return -ETIMEDOUT;
2663 }
2664 } else {
2665 }
2666 return 0;
2667}
2668
2669static int ar0832_s_stream_hdr(struct v4l2_subdev *sd, int enable)
2670{
2671 int err;
2672
2673 err = ar0832_set_mode(sd, AR0832_PARMSET_MODE);
2674 CHECK_ERR(err);
2675
2676 if (enable) {
2677 err = ar0832_writeb(sd, AR0832_CATEGORY_TEST, 0x50, 0x02);
2678 CHECK_ERR(err);
2679
2680 err = ar0832_writeb(sd, AR0832_CATEGORY_TEST, 0x51, 0x80);
2681 CHECK_ERR(err);
2682
2683 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM,
2684 AR0832_PARM_HDR_MON, 0x01);
2685 CHECK_ERR(err);
2686
2687 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM,
2688 AR0832_PARM_HDR_MON_OFFSET_EV, 0x64);
2689 CHECK_ERR(err);
2690 } else {
2691 err = ar0832_writeb(sd, AR0832_CATEGORY_PARM,
2692 AR0832_PARM_HDR_MON, 0x00);
2693 CHECK_ERR(err);
2694 }
2695 return 0;
2696}
2697
2698static int ar0832_s_stream(struct v4l2_subdev *sd, int enable)
2699{
2700 struct ar0832_state *state = to_state(sd);
2701 int err;
2702
2703 cam_trace("E\n");
2704
2705 if (unlikely(state->isp.bad_fw)) {
2706 cam_err("\"Unknown\" state, please update F/W");
2707 return -ENOSYS;
2708 }
2709
2710 switch (enable) {
2711 case STREAM_MODE_CAM_ON:
2712 case STREAM_MODE_CAM_OFF:
2713 switch (state->format_mode) {
2714 case V4L2_PIX_FMT_MODE_CAPTURE:
2715 cam_info("capture %s",
2716 enable == STREAM_MODE_CAM_ON ? "on" : "off");
2717 err = ar0832_s_stream_capture(sd, enable == STREAM_MODE_CAM_ON);
2718 break;
2719 case V4L2_PIX_FMT_MODE_HDR:
2720 err = ar0832_s_stream_hdr(sd, enable == STREAM_MODE_CAM_ON);
2721 break;
2722 default:
2723 cam_info("preview %s",
2724 enable == STREAM_MODE_CAM_ON ? "on" : "off");
2725 //err = ar0832_s_stream_preview(sd, enable == STREAM_MODE_CAM_ON);
2726
2727 if(enable){
2728 //preview start
2729 mdelay(100);
2730 //test
2731 printk("%s stream on\n",__func__);
2732 err=ar0832_write_array(sd,ar0832_reg_test,AR0832_INIT_REG_TEST);
2733 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2734
2735 err=ar0832_write_array3(sd,ar0832_reg_start_streaming1_3,AR0832_REG_START_STREAMING1_3);
2736 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2737 err=ar0832_write_array(sd,ar0832_reg_start_streaming2,AR0832_REG_START_STREAMING2);
2738 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2739 err=ar0832_write_array3(sd,ar0832_reg_start_streaming3_3,AR0832_REG_START_STREAMING3_3);
2740 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2741
2742 mdelay(100);
2743
2744 }
2745 else {
2746 //preview stop
2747 printk("%s stream off\n",__func__);
2748 err=ar0832_write_array3(sd,ar0832_reg_stop_streaming1_3,AR0832_REG_STOP_STREAMING1_3);
2749 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2750 err=ar0832_write_array(sd,ar0832_reg_stop_streaming2,AR0832_REG_STOP_STREAMING2);
2751 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2752 }
2753 cam_info("\n");
2754 break;
2755 }
2756 break;
2757
2758 case STREAM_MODE_MOVIE_ON:
2759 if (state->flash_mode != FLASH_MODE_OFF)
2760 err = ar0832_set_flash(sd, state->flash_mode, 1);
2761
2762 if (state->preview->index == AR0832_PREVIEW_720P ||
2763 state->preview->index == AR0832_PREVIEW_1080P)
2764 err = ar0832_set_af(sd, 1);
2765 break;
2766
2767 case STREAM_MODE_MOVIE_OFF:
2768 if (state->preview->index == AR0832_PREVIEW_720P ||
2769 state->preview->index == AR0832_PREVIEW_1080P)
2770 err = ar0832_set_af(sd, 0);
2771
2772 ar0832_set_flash(sd, FLASH_MODE_OFF, 1);
2773 break;
2774
2775 default:
2776 cam_err("invalid stream option, %d\n", enable);
2777 break;
2778 }
2779
2780 cam_trace("X\n");
2781 return 0;
2782}
2783
2784static int ar0832_check_version(struct v4l2_subdev *sd)
2785{
2786 struct ar0832_state *state = to_state(sd);
2787 struct i2c_client *client = v4l2_get_subdevdata(sd);
2788 int i, val,err;
2789
2790 cam_info("*************************************\n");
2791#if 0
2792 for (i = 0; i < 0xff; i++) {
2793 client->addr=i;
2794 cam_info("client->addr %x \n",client->addr);
2795 err=ar0832_i2c_write_2short(sd,0x1406,0x00);
2796 if(err<0) cam_info("error \n");
2797 }
2798
2799 err=ar0832_i2c_write_2short(sd,0x1406,0x00);
2800 if(err<0) cam_info("error \n");
2801 else cam_info("address check passed \n");
2802#endif
2803
2804 val=ar0832_i2c_read_reg(client,AR0832_CATEGORY_MODEL_ID,2);
2805 cam_info("model_id : %d\n",val);
2806 val=ar0832_i2c_read_reg(client,AR0832_CATEGORY_REVISION_NUM,1);
2807 cam_info("revision_number :%d\n",val);
2808 val=ar0832_i2c_read_reg(client,AR0832_CATEGORY_MANUFACTURE_ID,1);
2809 cam_info("manufacturer_id : %d\n",val);
2810 val=ar0832_i2c_read_reg(client,AR0832_CATEGORY_SMIA_VERSION,1);
2811 cam_info("smia_version : %d\n",val);
2812
2813// state->exif.unique_id[i] = (char)val;
2814// state->exif.unique_id[i] = '\0';
2815
2816 cam_info("*************************************\n");
2817
2818 return val;
2819}
2820
2821static int ar0832_init_param(struct v4l2_subdev *sd)
2822{
2823 int err;
2824 cam_trace("E\n");
2825
2826 err=ar0832_write_array3(sd,ar0832_init_reg_MIPI_IF_CFG1_3,AR0832_INIT_REG_MIPI_IF_CFG1_3);
2827 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2828
2829 err=ar0832_write_array(sd,ar0832_init_reg_MIPI_IF_CFG2,AR0832_INIT_REG_MIPI_IF_CFG2);
2830 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2831
2832 err=ar0832_write_array3(sd,ar0832_reg_stop_streaming1_3,AR0832_REG_STOP_STREAMING1_3);
2833 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2834
2835 err=ar0832_write_array(sd,ar0832_reg_stop_streaming2,AR0832_REG_STOP_STREAMING2);
2836 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2837
2838 err=ar0832_write_array(sd,ar0832_init_reg,AR0832_INIT_REG_REG);
2839 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2840
2841 err=ar0832_write_array(sd,ar0832_init_reg_raw10,AR0832_INIT_REG_RAW10);
2842 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2843
2844 err=ar0832_write_array(sd,ar0832_init_reg_pll,AR0832_INIT_REG_PLL);
2845 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2846
2847 err=ar0832_write_array(sd,ar0832_init_reg_output_size,AR0832_INIT_REG_OUTPUT_SIZE);
2848 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2849
2850 err=ar0832_write_array3(sd,ar0832_init_reg_etc1_3,AR0832_INIT_REG_ETC1_3);
2851 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2852
2853 err=ar0832_write_array(sd,ar0832_init_reg_etc2,AR0832_INIT_REG_ETC2);
2854 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2855
2856 err=ar0832_write_array3(sd,ar0832_init_reg_etc3_3,AR0832_INIT_REG_ETC3_3);
2857 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2858
2859 err=ar0832_write_array(sd,ar0832_init_reg_etc4,AR0832_INIT_REG_ETC4);
2860 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2861#if 1
2862 //start
2863 err=ar0832_write_array3(sd,ar0832_reg_start_streaming1_3,AR0832_REG_START_STREAMING1_3);
2864 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2865 err=ar0832_write_array(sd,ar0832_reg_start_streaming2,AR0832_REG_START_STREAMING2);
2866 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2867 err=ar0832_write_array3(sd,ar0832_reg_start_streaming3_3,AR0832_REG_START_STREAMING3_3);
2868 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2869
2870 //test
2871 err=ar0832_write_array(sd,ar0832_reg_test,AR0832_INIT_REG_TEST);
2872 if(err <0) {cam_err("%s init error %d \n",__func__,err); return -1; }
2873#endif
2874 cam_trace("X\n");
2875 return 0;
2876}
2877
2878static int ar0832_init(struct v4l2_subdev *sd, u32 val)
2879{
2880 struct ar0832_state *state = to_state(sd);
2881 u32 int_factor;
2882 int err;
2883 cam_trace("%s \n",__func__);
2884 printk("%s\n", __func__);
2885
2886 /* Default state values */
2887 state->isp.bad_fw = 0;
2888
2889 state->preview = NULL;
2890 state->capture = NULL;
2891
2892 state->format_mode = V4L2_PIX_FMT_MODE_PREVIEW;
2893 state->sensor_mode = SENSOR_CAMERA;
2894 state->flash_mode = FLASH_MODE_OFF;
2895 state->beauty_mode = 0;
2896
2897 state->fps = 0; /* auto */
2898
2899 memset(&state->focus, 0, sizeof(state->focus));
2900#if 0 //firmware no needed //rapheal
2901 /* start camera program(parallel FLASH ROM) */
2902 err = ar0832_writeb(sd, AR0832_CATEGORY_FLASH,
2903 AR0832_FLASH_CAM_START, 0x01);
2904 CHECK_ERR(err);
2905
2906 int_factor = ar0832_wait_interrupt(sd, AR0832_ISP_TIMEOUT);
2907 if (!(int_factor & AR0832_INT_MODE)) {
2908 cam_err("firmware was erased?\n");
2909 state->isp.bad_fw = 1;
2910 return -ENOSYS;
2911 }
2912#endif
2913
2914 /* check up F/W version */
2915 err = ar0832_check_version(sd);
2916 CHECK_ERR(err);
2917
2918 ar0832_init_param(sd);
2919
2920 return 0;
2921}
2922
2923static const struct v4l2_subdev_core_ops ar0832_core_ops = {
2924 .init = ar0832_init, /* initializing API */
2925 //.load_fw = ar0832_load_fw,
2926 .queryctrl = ar0832_queryctrl,
2927 .g_ctrl = ar0832_g_ctrl,
2928 .s_ctrl = ar0832_s_ctrl,
2929 .g_ext_ctrls = ar0832_g_ext_ctrls,
2930};
2931
2932static const struct v4l2_subdev_video_ops ar0832_video_ops = {
2933 .s_mbus_fmt = ar0832_s_fmt,
2934 .g_parm = ar0832_g_parm,
2935 .s_parm = ar0832_s_parm,
2936 .enum_framesizes = ar0832_enum_framesizes,
2937 .s_stream = ar0832_s_stream,
2938};
2939
2940static const struct v4l2_subdev_ops ar0832_ops = {
2941 .core = &ar0832_core_ops,
2942 .video = &ar0832_video_ops,
2943};
2944
2945static ssize_t ar0832_camera_type_show(struct device *dev,
2946 struct device_attribute *attr, char *buf)
2947{
2948 char type[] = "APTINA_AR0832_NONE";
2949
2950 return sprintf(buf, "%s\n", type);
2951}
2952
2953static ssize_t ar0832_camera_fw_show(struct device *dev,
2954 struct device_attribute *attr, char *buf)
2955{
2956 struct i2c_client *client = to_i2c_client(dev);
2957 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2958 struct ar0832_state *state = to_state(sd);
2959
2960 return sprintf(buf, "%s\n", state->fw_version);
2961}
2962
2963static DEVICE_ATTR(camera_type, S_IRUGO, ar0832_camera_type_show, NULL);
2964static DEVICE_ATTR(camera_fw, S_IRUGO, ar0832_camera_fw_show, NULL);
2965
2966/*
2967 * ar0832_probe
2968 * Fetching platform data is being done with s_config subdev call.
2969 * In probe routine, we just register subdev device
2970 */
2971static int __devinit ar0832_probe(struct i2c_client *client,
2972 const struct i2c_device_id *id)
2973{
2974 struct ar0832_state *state;
2975 struct v4l2_subdev *sd;
2976 printk("%s\n", __func__);
2977
2978 const struct ar0832_platform_data *pdata =
2979 client->dev.platform_data;
2980 int err = 0;
2981
2982 state = kzalloc(sizeof(struct ar0832_state), GFP_KERNEL);
2983 if (state == NULL)
2984 return -ENOMEM;
2985
2986 sd = &state->sd;
2987 strcpy(sd->name, AR0832_DRIVER_NAME);
2988
2989 /* Registering subdev */
2990 v4l2_i2c_subdev_init(sd, client, &ar0832_ops);
2991
2992#ifdef CAM_DEBUG
2993 state->dbg_level = CAM_DEBUG| CAM_TRACE | CAM_I2C;
2994#endif
2995
2996#if 0 //rapheal
2997 if (device_create_file(&client->dev, &dev_attr_camera_type) < 0) {
2998 cam_warn("failed to create device file, %s\n",
2999 dev_attr_camera_type.attr.name);
3000 }
3001
3002 if (device_create_file(&client->dev, &dev_attr_camera_fw) < 0) {
3003 cam_warn("failed to create device file, %s\n",
3004 dev_attr_camera_fw.attr.name);
3005 }
3006
3007 /* wait queue initialize */
3008 init_waitqueue_head(&state->isp.wait);
3009
3010 if (pdata->config_isp_irq)
3011 pdata->config_isp_irq();
3012
3013 err = request_irq(pdata->irq,
3014 ar0832_isp_isr, IRQF_TRIGGER_RISING, "ar0832 isp", sd);
3015 if (err) {
3016 cam_err("failed to request irq\n");
3017 return err;
3018 }
3019 state->isp.irq = pdata->irq;
3020 state->isp.issued = 0;
3021#endif
3022 printk("%s\n", __func__);
3023
3024 return 0;
3025}
3026
3027static int __devexit ar0832_remove(struct i2c_client *client)
3028{
3029 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3030 struct ar0832_state *state = to_state(sd);
3031
3032 if (ar0832_set_af_softlanding(sd) < 0)
3033 cam_err("failed to set soft landing\n");
3034
3035 device_remove_file(&client->dev, &dev_attr_camera_type);
3036 device_remove_file(&client->dev, &dev_attr_camera_fw);
3037
3038 if (state->isp.irq > 0)
3039 free_irq(state->isp.irq, sd);
3040
3041 v4l2_device_unregister_subdev(sd);
3042
3043 kfree(state->fw_version);
3044 kfree(state);
3045
3046 return 0;
3047}
3048
3049static const struct i2c_device_id ar0832_id[] = {
3050 { AR0832_DRIVER_NAME, 0 },
3051 { }
3052};
3053MODULE_DEVICE_TABLE(i2c, ar0832_id);
3054
3055static struct i2c_driver ar0832_i2c_driver = {
3056 .driver = {
3057 .name = AR0832_DRIVER_NAME,
3058 },
3059 .probe = ar0832_probe,
3060 .remove = __devexit_p(ar0832_remove),
3061 .id_table = ar0832_id,
3062};
3063
3064static int __init ar0832_mod_init(void)
3065{
3066 printk("%s\n", __func__);
3067
3068 return i2c_add_driver(&ar0832_i2c_driver);
3069}
3070
3071static void __exit ar0832_mod_exit(void)
3072{
3073 i2c_del_driver(&ar0832_i2c_driver);
3074}
3075module_init(ar0832_mod_init);
3076module_exit(ar0832_mod_exit);
3077
3078
3079MODULE_AUTHOR("Dongyol Yun <ydongyol@hardkenel.com>");
3080MODULE_DESCRIPTION("driver for Aptina AR0832 8MP camera");
3081MODULE_LICENSE("GPL");