[media] m5mols: Improve the interrupt handling routines
[deliverable/linux.git] / drivers / media / video / m5mols / m5mols_core.c
CommitLineData
bc125106
HK
1/*
2 * Driver for M-5MOLS 8M Pixel camera sensor with ISP
3 *
4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
c3070113 5 * Author: HeungJun Kim <riverful.kim@samsung.com>
bc125106
HK
6 *
7 * Copyright (C) 2009 Samsung Electronics Co., Ltd.
c3070113 8 * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
bc125106
HK
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
13 * (at your option) any later version.
14 */
15
16#include <linux/i2c.h>
17#include <linux/slab.h>
18#include <linux/irq.h>
19#include <linux/interrupt.h>
20#include <linux/delay.h>
bc125106
HK
21#include <linux/gpio.h>
22#include <linux/regulator/consumer.h>
23#include <linux/videodev2.h>
7a707b89 24#include <linux/module.h>
bc125106
HK
25#include <media/v4l2-ctrls.h>
26#include <media/v4l2-device.h>
27#include <media/v4l2-subdev.h>
28#include <media/m5mols.h>
29
30#include "m5mols.h"
31#include "m5mols_reg.h"
32
33int m5mols_debug;
34module_param(m5mols_debug, int, 0644);
35
36#define MODULE_NAME "M5MOLS"
37#define M5MOLS_I2C_CHECK_RETRY 500
38
39/* The regulator consumer names for external voltage regulators */
40static struct regulator_bulk_data supplies[] = {
41 {
42 .supply = "core", /* ARM core power, 1.2V */
43 }, {
44 .supply = "dig_18", /* digital power 1, 1.8V */
45 }, {
46 .supply = "d_sensor", /* sensor power 1, 1.8V */
47 }, {
48 .supply = "dig_28", /* digital power 2, 2.8V */
49 }, {
50 .supply = "a_sensor", /* analog power */
51 }, {
52 .supply = "dig_12", /* digital power 3, 1.2V */
53 },
54};
55
56static struct v4l2_mbus_framefmt m5mols_default_ffmt[M5MOLS_RESTYPE_MAX] = {
57 [M5MOLS_RESTYPE_MONITOR] = {
58 .width = 1920,
59 .height = 1080,
60 .code = V4L2_MBUS_FMT_VYUY8_2X8,
61 .field = V4L2_FIELD_NONE,
62 .colorspace = V4L2_COLORSPACE_JPEG,
63 },
64 [M5MOLS_RESTYPE_CAPTURE] = {
65 .width = 1920,
66 .height = 1080,
67 .code = V4L2_MBUS_FMT_JPEG_1X8,
68 .field = V4L2_FIELD_NONE,
69 .colorspace = V4L2_COLORSPACE_JPEG,
70 },
71};
72#define SIZE_DEFAULT_FFMT ARRAY_SIZE(m5mols_default_ffmt)
73
74static const struct m5mols_resolution m5mols_reg_res[] = {
75 { 0x01, M5MOLS_RESTYPE_MONITOR, 128, 96 }, /* SUB-QCIF */
76 { 0x03, M5MOLS_RESTYPE_MONITOR, 160, 120 }, /* QQVGA */
77 { 0x05, M5MOLS_RESTYPE_MONITOR, 176, 144 }, /* QCIF */
78 { 0x06, M5MOLS_RESTYPE_MONITOR, 176, 176 },
79 { 0x08, M5MOLS_RESTYPE_MONITOR, 240, 320 }, /* QVGA */
80 { 0x09, M5MOLS_RESTYPE_MONITOR, 320, 240 }, /* QVGA */
81 { 0x0c, M5MOLS_RESTYPE_MONITOR, 240, 400 }, /* WQVGA */
82 { 0x0d, M5MOLS_RESTYPE_MONITOR, 400, 240 }, /* WQVGA */
83 { 0x0e, M5MOLS_RESTYPE_MONITOR, 352, 288 }, /* CIF */
84 { 0x13, M5MOLS_RESTYPE_MONITOR, 480, 360 },
85 { 0x15, M5MOLS_RESTYPE_MONITOR, 640, 360 }, /* qHD */
86 { 0x17, M5MOLS_RESTYPE_MONITOR, 640, 480 }, /* VGA */
87 { 0x18, M5MOLS_RESTYPE_MONITOR, 720, 480 },
88 { 0x1a, M5MOLS_RESTYPE_MONITOR, 800, 480 }, /* WVGA */
89 { 0x1f, M5MOLS_RESTYPE_MONITOR, 800, 600 }, /* SVGA */
90 { 0x21, M5MOLS_RESTYPE_MONITOR, 1280, 720 }, /* HD */
91 { 0x25, M5MOLS_RESTYPE_MONITOR, 1920, 1080 }, /* 1080p */
92 { 0x29, M5MOLS_RESTYPE_MONITOR, 3264, 2448 }, /* 2.63fps 8M */
93 { 0x39, M5MOLS_RESTYPE_MONITOR, 800, 602 }, /* AHS_MON debug */
94
95 { 0x02, M5MOLS_RESTYPE_CAPTURE, 320, 240 }, /* QVGA */
96 { 0x04, M5MOLS_RESTYPE_CAPTURE, 400, 240 }, /* WQVGA */
97 { 0x07, M5MOLS_RESTYPE_CAPTURE, 480, 360 },
98 { 0x08, M5MOLS_RESTYPE_CAPTURE, 640, 360 }, /* qHD */
99 { 0x09, M5MOLS_RESTYPE_CAPTURE, 640, 480 }, /* VGA */
100 { 0x0a, M5MOLS_RESTYPE_CAPTURE, 800, 480 }, /* WVGA */
101 { 0x10, M5MOLS_RESTYPE_CAPTURE, 1280, 720 }, /* HD */
102 { 0x14, M5MOLS_RESTYPE_CAPTURE, 1280, 960 }, /* 1M */
103 { 0x17, M5MOLS_RESTYPE_CAPTURE, 1600, 1200 }, /* 2M */
104 { 0x19, M5MOLS_RESTYPE_CAPTURE, 1920, 1080 }, /* Full-HD */
105 { 0x1a, M5MOLS_RESTYPE_CAPTURE, 2048, 1152 }, /* 3Mega */
106 { 0x1b, M5MOLS_RESTYPE_CAPTURE, 2048, 1536 },
107 { 0x1c, M5MOLS_RESTYPE_CAPTURE, 2560, 1440 }, /* 4Mega */
108 { 0x1d, M5MOLS_RESTYPE_CAPTURE, 2560, 1536 },
109 { 0x1f, M5MOLS_RESTYPE_CAPTURE, 2560, 1920 }, /* 5Mega */
110 { 0x21, M5MOLS_RESTYPE_CAPTURE, 3264, 1836 }, /* 6Mega */
111 { 0x22, M5MOLS_RESTYPE_CAPTURE, 3264, 1960 },
112 { 0x25, M5MOLS_RESTYPE_CAPTURE, 3264, 2448 }, /* 8Mega */
113};
114
115/**
116 * m5mols_swap_byte - an byte array to integer conversion function
117 * @size: size in bytes of I2C packet defined in the M-5MOLS datasheet
118 *
119 * Convert I2C data byte array with performing any required byte
120 * reordering to assure proper values for each data type, regardless
121 * of the architecture endianness.
122 */
123static u32 m5mols_swap_byte(u8 *data, u8 length)
124{
125 if (length == 1)
126 return *data;
127 else if (length == 2)
128 return be16_to_cpu(*((u16 *)data));
129 else
130 return be32_to_cpu(*((u32 *)data));
131}
132
133/**
134 * m5mols_read - I2C read function
135 * @reg: combination of size, category and command for the I2C packet
57644f56 136 * @size: desired size of I2C packet
bc125106
HK
137 * @val: read value
138 */
57644f56 139static int m5mols_read(struct v4l2_subdev *sd, u32 size, u32 reg, u32 *val)
bc125106
HK
140{
141 struct i2c_client *client = v4l2_get_subdevdata(sd);
142 u8 rbuf[M5MOLS_I2C_MAX_SIZE + 1];
bc125106
HK
143 u8 category = I2C_CATEGORY(reg);
144 u8 cmd = I2C_COMMAND(reg);
145 struct i2c_msg msg[2];
146 u8 wbuf[5];
147 int ret;
148
149 if (!client->adapter)
150 return -ENODEV;
151
bc125106
HK
152 msg[0].addr = client->addr;
153 msg[0].flags = 0;
154 msg[0].len = 5;
155 msg[0].buf = wbuf;
156 wbuf[0] = 5;
157 wbuf[1] = M5MOLS_BYTE_READ;
158 wbuf[2] = category;
159 wbuf[3] = cmd;
160 wbuf[4] = size;
161
162 msg[1].addr = client->addr;
163 msg[1].flags = I2C_M_RD;
164 msg[1].len = size + 1;
165 msg[1].buf = rbuf;
166
167 /* minimum stabilization time */
168 usleep_range(200, 200);
169
170 ret = i2c_transfer(client->adapter, msg, 2);
171 if (ret < 0) {
172 v4l2_err(sd, "read failed: size:%d cat:%02x cmd:%02x. %d\n",
173 size, category, cmd, ret);
174 return ret;
175 }
176
177 *val = m5mols_swap_byte(&rbuf[1], size);
178
179 return 0;
180}
181
57644f56
HK
182int m5mols_read_u8(struct v4l2_subdev *sd, u32 reg, u8 *val)
183{
184 u32 val_32;
185 int ret;
186
187 if (I2C_SIZE(reg) != 1) {
188 v4l2_err(sd, "Wrong data size\n");
189 return -EINVAL;
190 }
191
192 ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
193 if (ret)
194 return ret;
195
196 *val = (u8)val_32;
197 return ret;
198}
199
200int m5mols_read_u16(struct v4l2_subdev *sd, u32 reg, u16 *val)
201{
202 u32 val_32;
203 int ret;
204
205 if (I2C_SIZE(reg) != 2) {
206 v4l2_err(sd, "Wrong data size\n");
207 return -EINVAL;
208 }
209
210 ret = m5mols_read(sd, I2C_SIZE(reg), reg, &val_32);
211 if (ret)
212 return ret;
213
214 *val = (u16)val_32;
215 return ret;
216}
217
218int m5mols_read_u32(struct v4l2_subdev *sd, u32 reg, u32 *val)
219{
220 if (I2C_SIZE(reg) != 4) {
221 v4l2_err(sd, "Wrong data size\n");
222 return -EINVAL;
223 }
224
225 return m5mols_read(sd, I2C_SIZE(reg), reg, val);
226}
227
bc125106
HK
228/**
229 * m5mols_write - I2C command write function
230 * @reg: combination of size, category and command for the I2C packet
231 * @val: value to write
232 */
233int m5mols_write(struct v4l2_subdev *sd, u32 reg, u32 val)
234{
235 struct i2c_client *client = v4l2_get_subdevdata(sd);
236 u8 wbuf[M5MOLS_I2C_MAX_SIZE + 4];
237 u8 category = I2C_CATEGORY(reg);
238 u8 cmd = I2C_COMMAND(reg);
239 u8 size = I2C_SIZE(reg);
240 u32 *buf = (u32 *)&wbuf[4];
241 struct i2c_msg msg[1];
242 int ret;
243
244 if (!client->adapter)
245 return -ENODEV;
246
247 if (size != 1 && size != 2 && size != 4) {
248 v4l2_err(sd, "Wrong data size\n");
249 return -EINVAL;
250 }
251
252 msg->addr = client->addr;
253 msg->flags = 0;
254 msg->len = (u16)size + 4;
255 msg->buf = wbuf;
256 wbuf[0] = size + 4;
257 wbuf[1] = M5MOLS_BYTE_WRITE;
258 wbuf[2] = category;
259 wbuf[3] = cmd;
260
261 *buf = m5mols_swap_byte((u8 *)&val, size);
262
263 usleep_range(200, 200);
264
265 ret = i2c_transfer(client->adapter, msg, 1);
266 if (ret < 0) {
267 v4l2_err(sd, "write failed: size:%d cat:%02x cmd:%02x. %d\n",
268 size, category, cmd, ret);
269 return ret;
270 }
271
272 return 0;
273}
274
575d6252
HK
275/**
276 * m5mols_busy_wait - Busy waiting with I2C register polling
277 * @reg: the I2C_REG() address of an 8-bit status register to check
278 * @value: expected status register value
279 * @mask: bit mask for the read status register value
280 * @timeout: timeout in miliseconds, or -1 for default timeout
281 *
282 * The @reg register value is ORed with @mask before comparing with @value.
283 *
284 * Return: 0 if the requested condition became true within less than
285 * @timeout ms, or else negative errno.
286 */
287int m5mols_busy_wait(struct v4l2_subdev *sd, u32 reg, u32 value, u32 mask,
288 int timeout)
bc125106 289{
575d6252
HK
290 int ms = timeout < 0 ? M5MOLS_BUSY_WAIT_DEF_TIMEOUT : timeout;
291 unsigned long end = jiffies + msecs_to_jiffies(ms);
292 u8 status;
bc125106 293
575d6252
HK
294 do {
295 int ret = m5mols_read_u8(sd, reg, &status);
296
297 if (ret < 0 && !(mask & M5MOLS_I2C_RDY_WAIT_FL))
bc125106 298 return ret;
575d6252 299 if (!ret && (status & mask & 0xff) == (value & 0xff))
bc125106 300 return 0;
575d6252
HK
301 usleep_range(100, 250);
302 } while (ms > 0 && time_is_after_jiffies(end));
303
bc125106
HK
304 return -EBUSY;
305}
306
307/**
308 * m5mols_enable_interrupt - Clear interrupt pending bits and unmask interrupts
309 *
310 * Before writing desired interrupt value the INT_FACTOR register should
311 * be read to clear pending interrupts.
312 */
57644f56 313int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg)
bc125106
HK
314{
315 struct m5mols_info *info = to_m5mols(sd);
57644f56
HK
316 u8 mask = is_available_af(info) ? REG_INT_AF : 0;
317 u8 dummy;
bc125106
HK
318 int ret;
319
57644f56 320 ret = m5mols_read_u8(sd, SYSTEM_INT_FACTOR, &dummy);
bc125106
HK
321 if (!ret)
322 ret = m5mols_write(sd, SYSTEM_INT_ENABLE, reg & ~mask);
323 return ret;
324}
325
ce808a47
HK
326int m5mols_wait_interrupt(struct v4l2_subdev *sd, u8 irq_mask, u32 timeout)
327{
328 struct m5mols_info *info = to_m5mols(sd);
329
330 int ret = wait_event_interruptible_timeout(info->irq_waitq,
331 atomic_add_unless(&info->irq_done, -1, 0),
332 msecs_to_jiffies(timeout));
333 if (ret <= 0)
334 return ret ? ret : -ETIMEDOUT;
335
336 return m5mols_busy_wait(sd, SYSTEM_INT_FACTOR, irq_mask,
337 M5MOLS_I2C_RDY_WAIT_FL | irq_mask, -1);
338}
339
bc125106
HK
340/**
341 * m5mols_reg_mode - Write the mode and check busy status
342 *
343 * It always accompanies a little delay changing the M-5MOLS mode, so it is
344 * needed checking current busy status to guarantee right mode.
345 */
57644f56 346static int m5mols_reg_mode(struct v4l2_subdev *sd, u8 mode)
bc125106
HK
347{
348 int ret = m5mols_write(sd, SYSTEM_SYSMODE, mode);
575d6252
HK
349 if (ret < 0)
350 return ret;
351 return m5mols_busy_wait(sd, SYSTEM_SYSMODE, mode, 0xff,
352 M5MOLS_MODE_CHANGE_TIMEOUT);
bc125106
HK
353}
354
355/**
356 * m5mols_mode - manage the M-5MOLS's mode
357 * @mode: the required operation mode
358 *
359 * The commands of M-5MOLS are grouped into specific modes. Each functionality
360 * can be guaranteed only when the sensor is operating in mode which which
361 * a command belongs to.
362 */
57644f56 363int m5mols_mode(struct m5mols_info *info, u8 mode)
bc125106
HK
364{
365 struct v4l2_subdev *sd = &info->sd;
366 int ret = -EINVAL;
57644f56 367 u8 reg;
bc125106 368
a32390d8 369 if (mode < REG_PARAMETER || mode > REG_CAPTURE)
bc125106
HK
370 return ret;
371
57644f56 372 ret = m5mols_read_u8(sd, SYSTEM_SYSMODE, &reg);
bc125106
HK
373 if ((!ret && reg == mode) || ret)
374 return ret;
375
376 switch (reg) {
377 case REG_PARAMETER:
378 ret = m5mols_reg_mode(sd, REG_MONITOR);
379 if (!ret && mode == REG_MONITOR)
380 break;
381 if (!ret)
382 ret = m5mols_reg_mode(sd, REG_CAPTURE);
383 break;
384
385 case REG_MONITOR:
386 if (mode == REG_PARAMETER) {
387 ret = m5mols_reg_mode(sd, REG_PARAMETER);
388 break;
389 }
390
391 ret = m5mols_reg_mode(sd, REG_CAPTURE);
392 break;
393
394 case REG_CAPTURE:
395 ret = m5mols_reg_mode(sd, REG_MONITOR);
396 if (!ret && mode == REG_MONITOR)
397 break;
398 if (!ret)
399 ret = m5mols_reg_mode(sd, REG_PARAMETER);
400 break;
401
402 default:
403 v4l2_warn(sd, "Wrong mode: %d\n", mode);
404 }
405
406 if (!ret)
407 info->mode = mode;
408
409 return ret;
410}
411
412/**
413 * m5mols_get_version - retrieve full revisions information of M-5MOLS
414 *
415 * The version information includes revisions of hardware and firmware,
416 * AutoFocus alghorithm version and the version string.
417 */
418static int m5mols_get_version(struct v4l2_subdev *sd)
419{
420 struct m5mols_info *info = to_m5mols(sd);
a6354d2e
HK
421 struct m5mols_version *ver = &info->ver;
422 u8 *str = ver->str;
423 int i;
bc125106
HK
424 int ret;
425
a6354d2e
HK
426 ret = m5mols_read_u8(sd, SYSTEM_VER_CUSTOMER, &ver->customer);
427 if (!ret)
428 ret = m5mols_read_u8(sd, SYSTEM_VER_PROJECT, &ver->project);
429 if (!ret)
430 ret = m5mols_read_u16(sd, SYSTEM_VER_FIRMWARE, &ver->fw);
431 if (!ret)
432 ret = m5mols_read_u16(sd, SYSTEM_VER_HARDWARE, &ver->hw);
433 if (!ret)
434 ret = m5mols_read_u16(sd, SYSTEM_VER_PARAMETER, &ver->param);
435 if (!ret)
436 ret = m5mols_read_u16(sd, SYSTEM_VER_AWB, &ver->awb);
437 if (!ret)
438 ret = m5mols_read_u8(sd, AF_VERSION, &ver->af);
439 if (ret)
440 return ret;
bc125106 441
a6354d2e
HK
442 for (i = 0; i < VERSION_STRING_SIZE; i++) {
443 ret = m5mols_read_u8(sd, SYSTEM_VER_STRING, &str[i]);
bc125106
HK
444 if (ret)
445 return ret;
a6354d2e 446 }
bc125106 447
a6354d2e
HK
448 ver->fw = be16_to_cpu(ver->fw);
449 ver->hw = be16_to_cpu(ver->hw);
450 ver->param = be16_to_cpu(ver->param);
451 ver->awb = be16_to_cpu(ver->awb);
bc125106
HK
452
453 v4l2_info(sd, "Manufacturer\t[%s]\n",
454 is_manufacturer(info, REG_SAMSUNG_ELECTRO) ?
455 "Samsung Electro-Machanics" :
456 is_manufacturer(info, REG_SAMSUNG_OPTICS) ?
457 "Samsung Fiber-Optics" :
458 is_manufacturer(info, REG_SAMSUNG_TECHWIN) ?
459 "Samsung Techwin" : "None");
460 v4l2_info(sd, "Customer/Project\t[0x%02x/0x%02x]\n",
461 info->ver.customer, info->ver.project);
462
463 if (!is_available_af(info))
464 v4l2_info(sd, "No support Auto Focus on this firmware\n");
465
466 return ret;
467}
468
469/**
470 * __find_restype - Lookup M-5MOLS resolution type according to pixel code
471 * @code: pixel code
472 */
473static enum m5mols_restype __find_restype(enum v4l2_mbus_pixelcode code)
474{
475 enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR;
476
477 do {
478 if (code == m5mols_default_ffmt[type].code)
479 return type;
480 } while (type++ != SIZE_DEFAULT_FFMT);
481
482 return 0;
483}
484
485/**
486 * __find_resolution - Lookup preset and type of M-5MOLS's resolution
487 * @mf: pixel format to find/negotiate the resolution preset for
488 * @type: M-5MOLS resolution type
489 * @resolution: M-5MOLS resolution preset register value
490 *
491 * Find nearest resolution matching resolution preset and adjust mf
492 * to supported values.
493 */
494static int __find_resolution(struct v4l2_subdev *sd,
495 struct v4l2_mbus_framefmt *mf,
496 enum m5mols_restype *type,
497 u32 *resolution)
498{
499 const struct m5mols_resolution *fsize = &m5mols_reg_res[0];
500 const struct m5mols_resolution *match = NULL;
501 enum m5mols_restype stype = __find_restype(mf->code);
502 int i = ARRAY_SIZE(m5mols_reg_res);
503 unsigned int min_err = ~0;
504
505 while (i--) {
506 int err;
507 if (stype == fsize->type) {
508 err = abs(fsize->width - mf->width)
509 + abs(fsize->height - mf->height);
510
511 if (err < min_err) {
512 min_err = err;
513 match = fsize;
514 }
515 }
516 fsize++;
517 }
518 if (match) {
519 mf->width = match->width;
520 mf->height = match->height;
521 *resolution = match->reg;
522 *type = stype;
523 return 0;
524 }
525
526 return -EINVAL;
527}
528
529static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info,
530 struct v4l2_subdev_fh *fh,
531 enum v4l2_subdev_format_whence which,
532 enum m5mols_restype type)
533{
534 if (which == V4L2_SUBDEV_FORMAT_TRY)
535 return fh ? v4l2_subdev_get_try_format(fh, 0) : NULL;
536
537 return &info->ffmt[type];
538}
539
540static int m5mols_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
541 struct v4l2_subdev_format *fmt)
542{
543 struct m5mols_info *info = to_m5mols(sd);
544 struct v4l2_mbus_framefmt *format;
545
bc125106
HK
546 format = __find_format(info, fh, fmt->which, info->res_type);
547 if (!format)
548 return -EINVAL;
549
550 fmt->format = *format;
551 return 0;
552}
553
554static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
555 struct v4l2_subdev_format *fmt)
556{
557 struct m5mols_info *info = to_m5mols(sd);
558 struct v4l2_mbus_framefmt *format = &fmt->format;
559 struct v4l2_mbus_framefmt *sfmt;
560 enum m5mols_restype type;
561 u32 resolution = 0;
562 int ret;
563
bc125106
HK
564 ret = __find_resolution(sd, format, &type, &resolution);
565 if (ret < 0)
566 return ret;
567
568 sfmt = __find_format(info, fh, fmt->which, type);
569 if (!sfmt)
570 return 0;
571
fbe78ddd
SN
572
573 format->code = m5mols_default_ffmt[type].code;
574 format->colorspace = V4L2_COLORSPACE_JPEG;
575 format->field = V4L2_FIELD_NONE;
bc125106
HK
576
577 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
fbe78ddd 578 *sfmt = *format;
bc125106 579 info->resolution = resolution;
bc125106
HK
580 info->res_type = type;
581 }
582
583 return 0;
584}
585
586static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
587 struct v4l2_subdev_fh *fh,
588 struct v4l2_subdev_mbus_code_enum *code)
589{
590 if (!code || code->index >= SIZE_DEFAULT_FFMT)
591 return -EINVAL;
592
593 code->code = m5mols_default_ffmt[code->index].code;
594
595 return 0;
596}
597
598static struct v4l2_subdev_pad_ops m5mols_pad_ops = {
599 .enum_mbus_code = m5mols_enum_mbus_code,
600 .get_fmt = m5mols_get_fmt,
601 .set_fmt = m5mols_set_fmt,
602};
603
604/**
605 * m5mols_sync_controls - Apply default scene mode and the current controls
606 *
607 * This is used only streaming for syncing between v4l2_ctrl framework and
608 * m5mols's controls. First, do the scenemode to the sensor, then call
609 * v4l2_ctrl_handler_setup. It can be same between some commands and
610 * the scenemode's in the default v4l2_ctrls. But, such commands of control
611 * should be prior to the scenemode's one.
612 */
613int m5mols_sync_controls(struct m5mols_info *info)
614{
615 int ret = -EINVAL;
616
617 if (!is_ctrl_synced(info)) {
618 ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
619 if (ret)
620 return ret;
621
622 v4l2_ctrl_handler_setup(&info->handle);
623 info->ctrl_sync = true;
624 }
625
626 return ret;
627}
628
629/**
630 * m5mols_start_monitor - Start the monitor mode
631 *
632 * Before applying the controls setup the resolution and frame rate
633 * in PARAMETER mode, and then switch over to MONITOR mode.
634 */
635static int m5mols_start_monitor(struct m5mols_info *info)
636{
637 struct v4l2_subdev *sd = &info->sd;
638 int ret;
639
640 ret = m5mols_mode(info, REG_PARAMETER);
641 if (!ret)
642 ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
643 if (!ret)
644 ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
645 if (!ret)
646 ret = m5mols_mode(info, REG_MONITOR);
647 if (!ret)
648 ret = m5mols_sync_controls(info);
649
650 return ret;
651}
652
653static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
654{
655 struct m5mols_info *info = to_m5mols(sd);
fbe78ddd 656 u32 code = info->ffmt[info->res_type].code;
bc125106
HK
657
658 if (enable) {
659 int ret = -EINVAL;
660
fbe78ddd 661 if (is_code(code, M5MOLS_RESTYPE_MONITOR))
bc125106 662 ret = m5mols_start_monitor(info);
fbe78ddd 663 if (is_code(code, M5MOLS_RESTYPE_CAPTURE))
bc125106
HK
664 ret = m5mols_start_capture(info);
665
666 return ret;
667 }
668
669 return m5mols_mode(info, REG_PARAMETER);
670}
671
672static const struct v4l2_subdev_video_ops m5mols_video_ops = {
673 .s_stream = m5mols_s_stream,
674};
675
676static int m5mols_s_ctrl(struct v4l2_ctrl *ctrl)
677{
678 struct v4l2_subdev *sd = to_sd(ctrl);
679 struct m5mols_info *info = to_m5mols(sd);
680 int ret;
681
682 info->mode_save = info->mode;
683
684 ret = m5mols_mode(info, REG_PARAMETER);
685 if (!ret)
686 ret = m5mols_set_ctrl(ctrl);
687 if (!ret)
688 ret = m5mols_mode(info, info->mode_save);
689
690 return ret;
691}
692
693static const struct v4l2_ctrl_ops m5mols_ctrl_ops = {
694 .s_ctrl = m5mols_s_ctrl,
695};
696
697static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
698{
699 struct v4l2_subdev *sd = &info->sd;
700 struct i2c_client *client = v4l2_get_subdevdata(sd);
701 const struct m5mols_platform_data *pdata = info->pdata;
702 int ret;
703
704 if (enable) {
705 if (is_powered(info))
706 return 0;
707
708 if (info->set_power) {
709 ret = info->set_power(&client->dev, 1);
710 if (ret)
711 return ret;
712 }
713
714 ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
715 if (ret) {
716 info->set_power(&client->dev, 0);
717 return ret;
718 }
719
720 gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity);
721 usleep_range(1000, 1000);
722 info->power = true;
723
724 return ret;
725 }
726
727 if (!is_powered(info))
728 return 0;
729
730 ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
731 if (ret)
732 return ret;
733
734 if (info->set_power)
735 info->set_power(&client->dev, 0);
736
737 gpio_set_value(pdata->gpio_reset, pdata->reset_polarity);
738 usleep_range(1000, 1000);
739 info->power = false;
740
741 return ret;
742}
743
744/* m5mols_update_fw - optional firmware update routine */
745int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
746 int (*set_power)(struct m5mols_info *, bool))
747{
748 return 0;
749}
750
751/**
752 * m5mols_sensor_armboot - Booting M-5MOLS internal ARM core.
753 *
754 * Booting internal ARM core makes the M-5MOLS is ready for getting commands
755 * with I2C. It's the first thing to be done after it powered up. It must wait
756 * at least 520ms recommended by M-5MOLS datasheet, after executing arm booting.
757 */
758static int m5mols_sensor_armboot(struct v4l2_subdev *sd)
759{
760 int ret;
761
762 ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
763 if (ret < 0)
764 return ret;
765
766 msleep(520);
767
768 ret = m5mols_get_version(sd);
769 if (!ret)
770 ret = m5mols_update_fw(sd, m5mols_sensor_power);
771 if (ret)
772 return ret;
773
774 v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
775
776 ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
777 if (!ret)
778 ret = m5mols_enable_interrupt(sd, REG_INT_AF);
779
780 return ret;
781}
782
783static int m5mols_init_controls(struct m5mols_info *info)
784{
785 struct v4l2_subdev *sd = &info->sd;
786 u16 max_exposure;
787 u16 step_zoom;
788 int ret;
789
790 /* Determine value's range & step of controls for various FW version */
57644f56 791 ret = m5mols_read_u16(sd, AE_MAX_GAIN_MON, &max_exposure);
bc125106
HK
792 if (!ret)
793 step_zoom = is_manufacturer(info, REG_SAMSUNG_OPTICS) ? 31 : 1;
794 if (ret)
795 return ret;
796
797 v4l2_ctrl_handler_init(&info->handle, 6);
798 info->autowb = v4l2_ctrl_new_std(&info->handle,
799 &m5mols_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
800 0, 1, 1, 0);
801 info->saturation = v4l2_ctrl_new_std(&info->handle,
802 &m5mols_ctrl_ops, V4L2_CID_SATURATION,
803 1, 5, 1, 3);
804 info->zoom = v4l2_ctrl_new_std(&info->handle,
805 &m5mols_ctrl_ops, V4L2_CID_ZOOM_ABSOLUTE,
806 1, 70, step_zoom, 1);
807 info->exposure = v4l2_ctrl_new_std(&info->handle,
808 &m5mols_ctrl_ops, V4L2_CID_EXPOSURE,
809 0, max_exposure, 1, (int)max_exposure/2);
810 info->colorfx = v4l2_ctrl_new_std_menu(&info->handle,
811 &m5mols_ctrl_ops, V4L2_CID_COLORFX,
812 4, (1 << V4L2_COLORFX_BW), V4L2_COLORFX_NONE);
813 info->autoexposure = v4l2_ctrl_new_std_menu(&info->handle,
814 &m5mols_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
815 1, 0, V4L2_EXPOSURE_MANUAL);
816
817 sd->ctrl_handler = &info->handle;
818 if (info->handle.error) {
819 v4l2_err(sd, "Failed to initialize controls: %d\n", ret);
820 v4l2_ctrl_handler_free(&info->handle);
821 return info->handle.error;
822 }
823
824 v4l2_ctrl_cluster(2, &info->autoexposure);
825
826 return 0;
827}
828
829/**
830 * m5mols_s_power - Main sensor power control function
831 *
832 * To prevent breaking the lens when the sensor is powered off the Soft-Landing
833 * algorithm is called where available. The Soft-Landing algorithm availability
834 * dependends on the firmware provider.
835 */
836static int m5mols_s_power(struct v4l2_subdev *sd, int on)
837{
838 struct m5mols_info *info = to_m5mols(sd);
839 int ret;
840
841 if (on) {
842 ret = m5mols_sensor_power(info, true);
843 if (!ret)
844 ret = m5mols_sensor_armboot(sd);
845 if (!ret)
846 ret = m5mols_init_controls(info);
847 if (ret)
848 return ret;
849
850 info->ffmt[M5MOLS_RESTYPE_MONITOR] =
851 m5mols_default_ffmt[M5MOLS_RESTYPE_MONITOR];
852 info->ffmt[M5MOLS_RESTYPE_CAPTURE] =
853 m5mols_default_ffmt[M5MOLS_RESTYPE_CAPTURE];
854 return ret;
855 }
856
857 if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
858 ret = m5mols_mode(info, REG_MONITOR);
859 if (!ret)
860 ret = m5mols_write(sd, AF_EXECUTE, REG_AF_STOP);
861 if (!ret)
862 ret = m5mols_write(sd, AF_MODE, REG_AF_POWEROFF);
863 if (!ret)
575d6252
HK
864 ret = m5mols_busy_wait(sd, SYSTEM_STATUS, REG_AF_IDLE,
865 0xff, -1);
866 if (ret < 0)
867 v4l2_warn(sd, "Soft landing lens failed\n");
bc125106
HK
868 }
869
870 ret = m5mols_sensor_power(info, false);
871 if (!ret) {
872 v4l2_ctrl_handler_free(&info->handle);
873 info->ctrl_sync = false;
874 }
875
876 return ret;
877}
878
879static int m5mols_log_status(struct v4l2_subdev *sd)
880{
881 struct m5mols_info *info = to_m5mols(sd);
882
883 v4l2_ctrl_handler_log_status(&info->handle, sd->name);
884
885 return 0;
886}
887
888static const struct v4l2_subdev_core_ops m5mols_core_ops = {
889 .s_power = m5mols_s_power,
890 .g_ctrl = v4l2_subdev_g_ctrl,
891 .s_ctrl = v4l2_subdev_s_ctrl,
892 .queryctrl = v4l2_subdev_queryctrl,
893 .querymenu = v4l2_subdev_querymenu,
894 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
895 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
896 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
897 .log_status = m5mols_log_status,
898};
899
900static const struct v4l2_subdev_ops m5mols_ops = {
901 .core = &m5mols_core_ops,
902 .pad = &m5mols_pad_ops,
903 .video = &m5mols_video_ops,
904};
905
bc125106
HK
906static irqreturn_t m5mols_irq_handler(int irq, void *data)
907{
ce808a47 908 struct m5mols_info *info = to_m5mols(data);
bc125106 909
ce808a47
HK
910 atomic_set(&info->irq_done, 1);
911 wake_up_interruptible(&info->irq_waitq);
bc125106
HK
912
913 return IRQ_HANDLED;
914}
915
916static int __devinit m5mols_probe(struct i2c_client *client,
917 const struct i2c_device_id *id)
918{
919 const struct m5mols_platform_data *pdata = client->dev.platform_data;
920 struct m5mols_info *info;
921 struct v4l2_subdev *sd;
922 int ret;
923
924 if (pdata == NULL) {
925 dev_err(&client->dev, "No platform data\n");
926 return -EINVAL;
927 }
928
929 if (!gpio_is_valid(pdata->gpio_reset)) {
930 dev_err(&client->dev, "No valid RESET GPIO specified\n");
931 return -EINVAL;
932 }
933
5b3bdfce 934 if (!client->irq) {
bc125106
HK
935 dev_err(&client->dev, "Interrupt not assigned\n");
936 return -EINVAL;
937 }
938
939 info = kzalloc(sizeof(struct m5mols_info), GFP_KERNEL);
940 if (!info)
941 return -ENOMEM;
942
943 info->pdata = pdata;
944 info->set_power = pdata->set_power;
945
946 ret = gpio_request(pdata->gpio_reset, "M5MOLS_NRST");
947 if (ret) {
948 dev_err(&client->dev, "Failed to request gpio: %d\n", ret);
949 goto out_free;
950 }
951 gpio_direction_output(pdata->gpio_reset, pdata->reset_polarity);
952
953 ret = regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies), supplies);
954 if (ret) {
955 dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
956 goto out_gpio;
957 }
958
959 sd = &info->sd;
960 strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
961 v4l2_i2c_subdev_init(sd, client, &m5mols_ops);
962
963 info->pad.flags = MEDIA_PAD_FL_SOURCE;
964 ret = media_entity_init(&sd->entity, 1, &info->pad, 0);
965 if (ret < 0)
966 goto out_reg;
967 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
968
969 init_waitqueue_head(&info->irq_waitq);
5b3bdfce 970 ret = request_irq(client->irq, m5mols_irq_handler,
bc125106
HK
971 IRQF_TRIGGER_RISING, MODULE_NAME, sd);
972 if (ret) {
973 dev_err(&client->dev, "Interrupt request failed: %d\n", ret);
974 goto out_me;
975 }
976 info->res_type = M5MOLS_RESTYPE_MONITOR;
ce808a47 977
bc125106
HK
978 return 0;
979out_me:
980 media_entity_cleanup(&sd->entity);
981out_reg:
982 regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
983out_gpio:
984 gpio_free(pdata->gpio_reset);
985out_free:
986 kfree(info);
987 return ret;
988}
989
990static int __devexit m5mols_remove(struct i2c_client *client)
991{
992 struct v4l2_subdev *sd = i2c_get_clientdata(client);
993 struct m5mols_info *info = to_m5mols(sd);
994
995 v4l2_device_unregister_subdev(sd);
5b3bdfce 996 free_irq(client->irq, sd);
bc125106
HK
997
998 regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
999 gpio_free(info->pdata->gpio_reset);
1000 media_entity_cleanup(&sd->entity);
1001 kfree(info);
1002 return 0;
1003}
1004
1005static const struct i2c_device_id m5mols_id[] = {
1006 { MODULE_NAME, 0 },
1007 { },
1008};
1009MODULE_DEVICE_TABLE(i2c, m5mols_id);
1010
1011static struct i2c_driver m5mols_i2c_driver = {
1012 .driver = {
1013 .name = MODULE_NAME,
1014 },
1015 .probe = m5mols_probe,
1016 .remove = __devexit_p(m5mols_remove),
1017 .id_table = m5mols_id,
1018};
1019
1020static int __init m5mols_mod_init(void)
1021{
1022 return i2c_add_driver(&m5mols_i2c_driver);
1023}
1024
1025static void __exit m5mols_mod_exit(void)
1026{
1027 i2c_del_driver(&m5mols_i2c_driver);
1028}
1029
1030module_init(m5mols_mod_init);
1031module_exit(m5mols_mod_exit);
1032
1033MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
1034MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
1035MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
1036MODULE_LICENSE("GPL");
This page took 0.0987440000000001 seconds and 5 git commands to generate.