Commit | Line | Data |
---|---|---|
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 | ||
33 | int m5mols_debug; | |
34 | module_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 */ | |
40 | static 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 | ||
56 | static 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 | ||
74 | static 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 | */ | |
123 | static 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 | 139 | static 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 |
182 | int 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 | ||
200 | int 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 | ||
218 | int 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 | */ | |
233 | int 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 | ||
57644f56 | 275 | int m5mols_busy(struct v4l2_subdev *sd, u8 category, u8 cmd, u8 mask) |
bc125106 | 276 | { |
57644f56 HK |
277 | u8 busy; |
278 | int i; | |
bc125106 HK |
279 | int ret; |
280 | ||
281 | for (i = 0; i < M5MOLS_I2C_CHECK_RETRY; i++) { | |
57644f56 | 282 | ret = m5mols_read_u8(sd, I2C_REG(category, cmd, 1), &busy); |
bc125106 HK |
283 | if (ret < 0) |
284 | return ret; | |
285 | if ((busy & mask) == mask) | |
286 | return 0; | |
287 | } | |
288 | return -EBUSY; | |
289 | } | |
290 | ||
291 | /** | |
292 | * m5mols_enable_interrupt - Clear interrupt pending bits and unmask interrupts | |
293 | * | |
294 | * Before writing desired interrupt value the INT_FACTOR register should | |
295 | * be read to clear pending interrupts. | |
296 | */ | |
57644f56 | 297 | int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg) |
bc125106 HK |
298 | { |
299 | struct m5mols_info *info = to_m5mols(sd); | |
57644f56 HK |
300 | u8 mask = is_available_af(info) ? REG_INT_AF : 0; |
301 | u8 dummy; | |
bc125106 HK |
302 | int ret; |
303 | ||
57644f56 | 304 | ret = m5mols_read_u8(sd, SYSTEM_INT_FACTOR, &dummy); |
bc125106 HK |
305 | if (!ret) |
306 | ret = m5mols_write(sd, SYSTEM_INT_ENABLE, reg & ~mask); | |
307 | return ret; | |
308 | } | |
309 | ||
310 | /** | |
311 | * m5mols_reg_mode - Write the mode and check busy status | |
312 | * | |
313 | * It always accompanies a little delay changing the M-5MOLS mode, so it is | |
314 | * needed checking current busy status to guarantee right mode. | |
315 | */ | |
57644f56 | 316 | static int m5mols_reg_mode(struct v4l2_subdev *sd, u8 mode) |
bc125106 HK |
317 | { |
318 | int ret = m5mols_write(sd, SYSTEM_SYSMODE, mode); | |
319 | ||
320 | return ret ? ret : m5mols_busy(sd, CAT_SYSTEM, CAT0_SYSMODE, mode); | |
321 | } | |
322 | ||
323 | /** | |
324 | * m5mols_mode - manage the M-5MOLS's mode | |
325 | * @mode: the required operation mode | |
326 | * | |
327 | * The commands of M-5MOLS are grouped into specific modes. Each functionality | |
328 | * can be guaranteed only when the sensor is operating in mode which which | |
329 | * a command belongs to. | |
330 | */ | |
57644f56 | 331 | int m5mols_mode(struct m5mols_info *info, u8 mode) |
bc125106 HK |
332 | { |
333 | struct v4l2_subdev *sd = &info->sd; | |
334 | int ret = -EINVAL; | |
57644f56 | 335 | u8 reg; |
bc125106 HK |
336 | |
337 | if (mode < REG_PARAMETER && mode > REG_CAPTURE) | |
338 | return ret; | |
339 | ||
57644f56 | 340 | ret = m5mols_read_u8(sd, SYSTEM_SYSMODE, ®); |
bc125106 HK |
341 | if ((!ret && reg == mode) || ret) |
342 | return ret; | |
343 | ||
344 | switch (reg) { | |
345 | case REG_PARAMETER: | |
346 | ret = m5mols_reg_mode(sd, REG_MONITOR); | |
347 | if (!ret && mode == REG_MONITOR) | |
348 | break; | |
349 | if (!ret) | |
350 | ret = m5mols_reg_mode(sd, REG_CAPTURE); | |
351 | break; | |
352 | ||
353 | case REG_MONITOR: | |
354 | if (mode == REG_PARAMETER) { | |
355 | ret = m5mols_reg_mode(sd, REG_PARAMETER); | |
356 | break; | |
357 | } | |
358 | ||
359 | ret = m5mols_reg_mode(sd, REG_CAPTURE); | |
360 | break; | |
361 | ||
362 | case REG_CAPTURE: | |
363 | ret = m5mols_reg_mode(sd, REG_MONITOR); | |
364 | if (!ret && mode == REG_MONITOR) | |
365 | break; | |
366 | if (!ret) | |
367 | ret = m5mols_reg_mode(sd, REG_PARAMETER); | |
368 | break; | |
369 | ||
370 | default: | |
371 | v4l2_warn(sd, "Wrong mode: %d\n", mode); | |
372 | } | |
373 | ||
374 | if (!ret) | |
375 | info->mode = mode; | |
376 | ||
377 | return ret; | |
378 | } | |
379 | ||
380 | /** | |
381 | * m5mols_get_version - retrieve full revisions information of M-5MOLS | |
382 | * | |
383 | * The version information includes revisions of hardware and firmware, | |
384 | * AutoFocus alghorithm version and the version string. | |
385 | */ | |
386 | static int m5mols_get_version(struct v4l2_subdev *sd) | |
387 | { | |
388 | struct m5mols_info *info = to_m5mols(sd); | |
a6354d2e HK |
389 | struct m5mols_version *ver = &info->ver; |
390 | u8 *str = ver->str; | |
391 | int i; | |
bc125106 HK |
392 | int ret; |
393 | ||
a6354d2e HK |
394 | ret = m5mols_read_u8(sd, SYSTEM_VER_CUSTOMER, &ver->customer); |
395 | if (!ret) | |
396 | ret = m5mols_read_u8(sd, SYSTEM_VER_PROJECT, &ver->project); | |
397 | if (!ret) | |
398 | ret = m5mols_read_u16(sd, SYSTEM_VER_FIRMWARE, &ver->fw); | |
399 | if (!ret) | |
400 | ret = m5mols_read_u16(sd, SYSTEM_VER_HARDWARE, &ver->hw); | |
401 | if (!ret) | |
402 | ret = m5mols_read_u16(sd, SYSTEM_VER_PARAMETER, &ver->param); | |
403 | if (!ret) | |
404 | ret = m5mols_read_u16(sd, SYSTEM_VER_AWB, &ver->awb); | |
405 | if (!ret) | |
406 | ret = m5mols_read_u8(sd, AF_VERSION, &ver->af); | |
407 | if (ret) | |
408 | return ret; | |
bc125106 | 409 | |
a6354d2e HK |
410 | for (i = 0; i < VERSION_STRING_SIZE; i++) { |
411 | ret = m5mols_read_u8(sd, SYSTEM_VER_STRING, &str[i]); | |
bc125106 HK |
412 | if (ret) |
413 | return ret; | |
a6354d2e | 414 | } |
bc125106 | 415 | |
a6354d2e HK |
416 | ver->fw = be16_to_cpu(ver->fw); |
417 | ver->hw = be16_to_cpu(ver->hw); | |
418 | ver->param = be16_to_cpu(ver->param); | |
419 | ver->awb = be16_to_cpu(ver->awb); | |
bc125106 HK |
420 | |
421 | v4l2_info(sd, "Manufacturer\t[%s]\n", | |
422 | is_manufacturer(info, REG_SAMSUNG_ELECTRO) ? | |
423 | "Samsung Electro-Machanics" : | |
424 | is_manufacturer(info, REG_SAMSUNG_OPTICS) ? | |
425 | "Samsung Fiber-Optics" : | |
426 | is_manufacturer(info, REG_SAMSUNG_TECHWIN) ? | |
427 | "Samsung Techwin" : "None"); | |
428 | v4l2_info(sd, "Customer/Project\t[0x%02x/0x%02x]\n", | |
429 | info->ver.customer, info->ver.project); | |
430 | ||
431 | if (!is_available_af(info)) | |
432 | v4l2_info(sd, "No support Auto Focus on this firmware\n"); | |
433 | ||
434 | return ret; | |
435 | } | |
436 | ||
437 | /** | |
438 | * __find_restype - Lookup M-5MOLS resolution type according to pixel code | |
439 | * @code: pixel code | |
440 | */ | |
441 | static enum m5mols_restype __find_restype(enum v4l2_mbus_pixelcode code) | |
442 | { | |
443 | enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR; | |
444 | ||
445 | do { | |
446 | if (code == m5mols_default_ffmt[type].code) | |
447 | return type; | |
448 | } while (type++ != SIZE_DEFAULT_FFMT); | |
449 | ||
450 | return 0; | |
451 | } | |
452 | ||
453 | /** | |
454 | * __find_resolution - Lookup preset and type of M-5MOLS's resolution | |
455 | * @mf: pixel format to find/negotiate the resolution preset for | |
456 | * @type: M-5MOLS resolution type | |
457 | * @resolution: M-5MOLS resolution preset register value | |
458 | * | |
459 | * Find nearest resolution matching resolution preset and adjust mf | |
460 | * to supported values. | |
461 | */ | |
462 | static int __find_resolution(struct v4l2_subdev *sd, | |
463 | struct v4l2_mbus_framefmt *mf, | |
464 | enum m5mols_restype *type, | |
465 | u32 *resolution) | |
466 | { | |
467 | const struct m5mols_resolution *fsize = &m5mols_reg_res[0]; | |
468 | const struct m5mols_resolution *match = NULL; | |
469 | enum m5mols_restype stype = __find_restype(mf->code); | |
470 | int i = ARRAY_SIZE(m5mols_reg_res); | |
471 | unsigned int min_err = ~0; | |
472 | ||
473 | while (i--) { | |
474 | int err; | |
475 | if (stype == fsize->type) { | |
476 | err = abs(fsize->width - mf->width) | |
477 | + abs(fsize->height - mf->height); | |
478 | ||
479 | if (err < min_err) { | |
480 | min_err = err; | |
481 | match = fsize; | |
482 | } | |
483 | } | |
484 | fsize++; | |
485 | } | |
486 | if (match) { | |
487 | mf->width = match->width; | |
488 | mf->height = match->height; | |
489 | *resolution = match->reg; | |
490 | *type = stype; | |
491 | return 0; | |
492 | } | |
493 | ||
494 | return -EINVAL; | |
495 | } | |
496 | ||
497 | static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info, | |
498 | struct v4l2_subdev_fh *fh, | |
499 | enum v4l2_subdev_format_whence which, | |
500 | enum m5mols_restype type) | |
501 | { | |
502 | if (which == V4L2_SUBDEV_FORMAT_TRY) | |
503 | return fh ? v4l2_subdev_get_try_format(fh, 0) : NULL; | |
504 | ||
505 | return &info->ffmt[type]; | |
506 | } | |
507 | ||
508 | static int m5mols_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh, | |
509 | struct v4l2_subdev_format *fmt) | |
510 | { | |
511 | struct m5mols_info *info = to_m5mols(sd); | |
512 | struct v4l2_mbus_framefmt *format; | |
513 | ||
514 | if (fmt->pad != 0) | |
515 | return -EINVAL; | |
516 | ||
517 | format = __find_format(info, fh, fmt->which, info->res_type); | |
518 | if (!format) | |
519 | return -EINVAL; | |
520 | ||
521 | fmt->format = *format; | |
522 | return 0; | |
523 | } | |
524 | ||
525 | static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh, | |
526 | struct v4l2_subdev_format *fmt) | |
527 | { | |
528 | struct m5mols_info *info = to_m5mols(sd); | |
529 | struct v4l2_mbus_framefmt *format = &fmt->format; | |
530 | struct v4l2_mbus_framefmt *sfmt; | |
531 | enum m5mols_restype type; | |
532 | u32 resolution = 0; | |
533 | int ret; | |
534 | ||
535 | if (fmt->pad != 0) | |
536 | return -EINVAL; | |
537 | ||
538 | ret = __find_resolution(sd, format, &type, &resolution); | |
539 | if (ret < 0) | |
540 | return ret; | |
541 | ||
542 | sfmt = __find_format(info, fh, fmt->which, type); | |
543 | if (!sfmt) | |
544 | return 0; | |
545 | ||
546 | *sfmt = m5mols_default_ffmt[type]; | |
547 | sfmt->width = format->width; | |
548 | sfmt->height = format->height; | |
549 | ||
550 | if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) { | |
551 | info->resolution = resolution; | |
552 | info->code = format->code; | |
553 | info->res_type = type; | |
554 | } | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
559 | static int m5mols_enum_mbus_code(struct v4l2_subdev *sd, | |
560 | struct v4l2_subdev_fh *fh, | |
561 | struct v4l2_subdev_mbus_code_enum *code) | |
562 | { | |
563 | if (!code || code->index >= SIZE_DEFAULT_FFMT) | |
564 | return -EINVAL; | |
565 | ||
566 | code->code = m5mols_default_ffmt[code->index].code; | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
571 | static struct v4l2_subdev_pad_ops m5mols_pad_ops = { | |
572 | .enum_mbus_code = m5mols_enum_mbus_code, | |
573 | .get_fmt = m5mols_get_fmt, | |
574 | .set_fmt = m5mols_set_fmt, | |
575 | }; | |
576 | ||
577 | /** | |
578 | * m5mols_sync_controls - Apply default scene mode and the current controls | |
579 | * | |
580 | * This is used only streaming for syncing between v4l2_ctrl framework and | |
581 | * m5mols's controls. First, do the scenemode to the sensor, then call | |
582 | * v4l2_ctrl_handler_setup. It can be same between some commands and | |
583 | * the scenemode's in the default v4l2_ctrls. But, such commands of control | |
584 | * should be prior to the scenemode's one. | |
585 | */ | |
586 | int m5mols_sync_controls(struct m5mols_info *info) | |
587 | { | |
588 | int ret = -EINVAL; | |
589 | ||
590 | if (!is_ctrl_synced(info)) { | |
591 | ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL); | |
592 | if (ret) | |
593 | return ret; | |
594 | ||
595 | v4l2_ctrl_handler_setup(&info->handle); | |
596 | info->ctrl_sync = true; | |
597 | } | |
598 | ||
599 | return ret; | |
600 | } | |
601 | ||
602 | /** | |
603 | * m5mols_start_monitor - Start the monitor mode | |
604 | * | |
605 | * Before applying the controls setup the resolution and frame rate | |
606 | * in PARAMETER mode, and then switch over to MONITOR mode. | |
607 | */ | |
608 | static int m5mols_start_monitor(struct m5mols_info *info) | |
609 | { | |
610 | struct v4l2_subdev *sd = &info->sd; | |
611 | int ret; | |
612 | ||
613 | ret = m5mols_mode(info, REG_PARAMETER); | |
614 | if (!ret) | |
615 | ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution); | |
616 | if (!ret) | |
617 | ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30); | |
618 | if (!ret) | |
619 | ret = m5mols_mode(info, REG_MONITOR); | |
620 | if (!ret) | |
621 | ret = m5mols_sync_controls(info); | |
622 | ||
623 | return ret; | |
624 | } | |
625 | ||
626 | static int m5mols_s_stream(struct v4l2_subdev *sd, int enable) | |
627 | { | |
628 | struct m5mols_info *info = to_m5mols(sd); | |
629 | ||
630 | if (enable) { | |
631 | int ret = -EINVAL; | |
632 | ||
633 | if (is_code(info->code, M5MOLS_RESTYPE_MONITOR)) | |
634 | ret = m5mols_start_monitor(info); | |
635 | if (is_code(info->code, M5MOLS_RESTYPE_CAPTURE)) | |
636 | ret = m5mols_start_capture(info); | |
637 | ||
638 | return ret; | |
639 | } | |
640 | ||
641 | return m5mols_mode(info, REG_PARAMETER); | |
642 | } | |
643 | ||
644 | static const struct v4l2_subdev_video_ops m5mols_video_ops = { | |
645 | .s_stream = m5mols_s_stream, | |
646 | }; | |
647 | ||
648 | static int m5mols_s_ctrl(struct v4l2_ctrl *ctrl) | |
649 | { | |
650 | struct v4l2_subdev *sd = to_sd(ctrl); | |
651 | struct m5mols_info *info = to_m5mols(sd); | |
652 | int ret; | |
653 | ||
654 | info->mode_save = info->mode; | |
655 | ||
656 | ret = m5mols_mode(info, REG_PARAMETER); | |
657 | if (!ret) | |
658 | ret = m5mols_set_ctrl(ctrl); | |
659 | if (!ret) | |
660 | ret = m5mols_mode(info, info->mode_save); | |
661 | ||
662 | return ret; | |
663 | } | |
664 | ||
665 | static const struct v4l2_ctrl_ops m5mols_ctrl_ops = { | |
666 | .s_ctrl = m5mols_s_ctrl, | |
667 | }; | |
668 | ||
669 | static int m5mols_sensor_power(struct m5mols_info *info, bool enable) | |
670 | { | |
671 | struct v4l2_subdev *sd = &info->sd; | |
672 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
673 | const struct m5mols_platform_data *pdata = info->pdata; | |
674 | int ret; | |
675 | ||
676 | if (enable) { | |
677 | if (is_powered(info)) | |
678 | return 0; | |
679 | ||
680 | if (info->set_power) { | |
681 | ret = info->set_power(&client->dev, 1); | |
682 | if (ret) | |
683 | return ret; | |
684 | } | |
685 | ||
686 | ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies); | |
687 | if (ret) { | |
688 | info->set_power(&client->dev, 0); | |
689 | return ret; | |
690 | } | |
691 | ||
692 | gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity); | |
693 | usleep_range(1000, 1000); | |
694 | info->power = true; | |
695 | ||
696 | return ret; | |
697 | } | |
698 | ||
699 | if (!is_powered(info)) | |
700 | return 0; | |
701 | ||
702 | ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies); | |
703 | if (ret) | |
704 | return ret; | |
705 | ||
706 | if (info->set_power) | |
707 | info->set_power(&client->dev, 0); | |
708 | ||
709 | gpio_set_value(pdata->gpio_reset, pdata->reset_polarity); | |
710 | usleep_range(1000, 1000); | |
711 | info->power = false; | |
712 | ||
713 | return ret; | |
714 | } | |
715 | ||
716 | /* m5mols_update_fw - optional firmware update routine */ | |
717 | int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd, | |
718 | int (*set_power)(struct m5mols_info *, bool)) | |
719 | { | |
720 | return 0; | |
721 | } | |
722 | ||
723 | /** | |
724 | * m5mols_sensor_armboot - Booting M-5MOLS internal ARM core. | |
725 | * | |
726 | * Booting internal ARM core makes the M-5MOLS is ready for getting commands | |
727 | * with I2C. It's the first thing to be done after it powered up. It must wait | |
728 | * at least 520ms recommended by M-5MOLS datasheet, after executing arm booting. | |
729 | */ | |
730 | static int m5mols_sensor_armboot(struct v4l2_subdev *sd) | |
731 | { | |
732 | int ret; | |
733 | ||
734 | ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT); | |
735 | if (ret < 0) | |
736 | return ret; | |
737 | ||
738 | msleep(520); | |
739 | ||
740 | ret = m5mols_get_version(sd); | |
741 | if (!ret) | |
742 | ret = m5mols_update_fw(sd, m5mols_sensor_power); | |
743 | if (ret) | |
744 | return ret; | |
745 | ||
746 | v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n"); | |
747 | ||
748 | ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI); | |
749 | if (!ret) | |
750 | ret = m5mols_enable_interrupt(sd, REG_INT_AF); | |
751 | ||
752 | return ret; | |
753 | } | |
754 | ||
755 | static int m5mols_init_controls(struct m5mols_info *info) | |
756 | { | |
757 | struct v4l2_subdev *sd = &info->sd; | |
758 | u16 max_exposure; | |
759 | u16 step_zoom; | |
760 | int ret; | |
761 | ||
762 | /* Determine value's range & step of controls for various FW version */ | |
57644f56 | 763 | ret = m5mols_read_u16(sd, AE_MAX_GAIN_MON, &max_exposure); |
bc125106 HK |
764 | if (!ret) |
765 | step_zoom = is_manufacturer(info, REG_SAMSUNG_OPTICS) ? 31 : 1; | |
766 | if (ret) | |
767 | return ret; | |
768 | ||
769 | v4l2_ctrl_handler_init(&info->handle, 6); | |
770 | info->autowb = v4l2_ctrl_new_std(&info->handle, | |
771 | &m5mols_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE, | |
772 | 0, 1, 1, 0); | |
773 | info->saturation = v4l2_ctrl_new_std(&info->handle, | |
774 | &m5mols_ctrl_ops, V4L2_CID_SATURATION, | |
775 | 1, 5, 1, 3); | |
776 | info->zoom = v4l2_ctrl_new_std(&info->handle, | |
777 | &m5mols_ctrl_ops, V4L2_CID_ZOOM_ABSOLUTE, | |
778 | 1, 70, step_zoom, 1); | |
779 | info->exposure = v4l2_ctrl_new_std(&info->handle, | |
780 | &m5mols_ctrl_ops, V4L2_CID_EXPOSURE, | |
781 | 0, max_exposure, 1, (int)max_exposure/2); | |
782 | info->colorfx = v4l2_ctrl_new_std_menu(&info->handle, | |
783 | &m5mols_ctrl_ops, V4L2_CID_COLORFX, | |
784 | 4, (1 << V4L2_COLORFX_BW), V4L2_COLORFX_NONE); | |
785 | info->autoexposure = v4l2_ctrl_new_std_menu(&info->handle, | |
786 | &m5mols_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, | |
787 | 1, 0, V4L2_EXPOSURE_MANUAL); | |
788 | ||
789 | sd->ctrl_handler = &info->handle; | |
790 | if (info->handle.error) { | |
791 | v4l2_err(sd, "Failed to initialize controls: %d\n", ret); | |
792 | v4l2_ctrl_handler_free(&info->handle); | |
793 | return info->handle.error; | |
794 | } | |
795 | ||
796 | v4l2_ctrl_cluster(2, &info->autoexposure); | |
797 | ||
798 | return 0; | |
799 | } | |
800 | ||
801 | /** | |
802 | * m5mols_s_power - Main sensor power control function | |
803 | * | |
804 | * To prevent breaking the lens when the sensor is powered off the Soft-Landing | |
805 | * algorithm is called where available. The Soft-Landing algorithm availability | |
806 | * dependends on the firmware provider. | |
807 | */ | |
808 | static int m5mols_s_power(struct v4l2_subdev *sd, int on) | |
809 | { | |
810 | struct m5mols_info *info = to_m5mols(sd); | |
811 | int ret; | |
812 | ||
813 | if (on) { | |
814 | ret = m5mols_sensor_power(info, true); | |
815 | if (!ret) | |
816 | ret = m5mols_sensor_armboot(sd); | |
817 | if (!ret) | |
818 | ret = m5mols_init_controls(info); | |
819 | if (ret) | |
820 | return ret; | |
821 | ||
822 | info->ffmt[M5MOLS_RESTYPE_MONITOR] = | |
823 | m5mols_default_ffmt[M5MOLS_RESTYPE_MONITOR]; | |
824 | info->ffmt[M5MOLS_RESTYPE_CAPTURE] = | |
825 | m5mols_default_ffmt[M5MOLS_RESTYPE_CAPTURE]; | |
826 | return ret; | |
827 | } | |
828 | ||
829 | if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) { | |
830 | ret = m5mols_mode(info, REG_MONITOR); | |
831 | if (!ret) | |
832 | ret = m5mols_write(sd, AF_EXECUTE, REG_AF_STOP); | |
833 | if (!ret) | |
834 | ret = m5mols_write(sd, AF_MODE, REG_AF_POWEROFF); | |
835 | if (!ret) | |
836 | ret = m5mols_busy(sd, CAT_SYSTEM, CAT0_STATUS, | |
837 | REG_AF_IDLE); | |
838 | if (!ret) | |
839 | v4l2_info(sd, "Success soft-landing lens\n"); | |
840 | } | |
841 | ||
842 | ret = m5mols_sensor_power(info, false); | |
843 | if (!ret) { | |
844 | v4l2_ctrl_handler_free(&info->handle); | |
845 | info->ctrl_sync = false; | |
846 | } | |
847 | ||
848 | return ret; | |
849 | } | |
850 | ||
851 | static int m5mols_log_status(struct v4l2_subdev *sd) | |
852 | { | |
853 | struct m5mols_info *info = to_m5mols(sd); | |
854 | ||
855 | v4l2_ctrl_handler_log_status(&info->handle, sd->name); | |
856 | ||
857 | return 0; | |
858 | } | |
859 | ||
860 | static const struct v4l2_subdev_core_ops m5mols_core_ops = { | |
861 | .s_power = m5mols_s_power, | |
862 | .g_ctrl = v4l2_subdev_g_ctrl, | |
863 | .s_ctrl = v4l2_subdev_s_ctrl, | |
864 | .queryctrl = v4l2_subdev_queryctrl, | |
865 | .querymenu = v4l2_subdev_querymenu, | |
866 | .g_ext_ctrls = v4l2_subdev_g_ext_ctrls, | |
867 | .try_ext_ctrls = v4l2_subdev_try_ext_ctrls, | |
868 | .s_ext_ctrls = v4l2_subdev_s_ext_ctrls, | |
869 | .log_status = m5mols_log_status, | |
870 | }; | |
871 | ||
872 | static const struct v4l2_subdev_ops m5mols_ops = { | |
873 | .core = &m5mols_core_ops, | |
874 | .pad = &m5mols_pad_ops, | |
875 | .video = &m5mols_video_ops, | |
876 | }; | |
877 | ||
878 | static void m5mols_irq_work(struct work_struct *work) | |
879 | { | |
880 | struct m5mols_info *info = | |
881 | container_of(work, struct m5mols_info, work_irq); | |
882 | struct v4l2_subdev *sd = &info->sd; | |
57644f56 | 883 | u8 reg; |
bc125106 HK |
884 | int ret; |
885 | ||
886 | if (!is_powered(info) || | |
57644f56 | 887 | m5mols_read_u8(sd, SYSTEM_INT_FACTOR, &info->interrupt)) |
bc125106 HK |
888 | return; |
889 | ||
890 | switch (info->interrupt & REG_INT_MASK) { | |
891 | case REG_INT_AF: | |
892 | if (!is_available_af(info)) | |
893 | break; | |
57644f56 | 894 | ret = m5mols_read_u8(sd, AF_STATUS, ®); |
bc125106 HK |
895 | v4l2_dbg(2, m5mols_debug, sd, "AF %s\n", |
896 | reg == REG_AF_FAIL ? "Failed" : | |
897 | reg == REG_AF_SUCCESS ? "Success" : | |
898 | reg == REG_AF_IDLE ? "Idle" : "Busy"); | |
899 | break; | |
900 | case REG_INT_CAPTURE: | |
901 | if (!test_and_set_bit(ST_CAPT_IRQ, &info->flags)) | |
902 | wake_up_interruptible(&info->irq_waitq); | |
903 | ||
904 | v4l2_dbg(2, m5mols_debug, sd, "CAPTURE\n"); | |
905 | break; | |
906 | default: | |
907 | v4l2_dbg(2, m5mols_debug, sd, "Undefined: %02x\n", reg); | |
908 | break; | |
909 | }; | |
910 | } | |
911 | ||
912 | static irqreturn_t m5mols_irq_handler(int irq, void *data) | |
913 | { | |
914 | struct v4l2_subdev *sd = data; | |
915 | struct m5mols_info *info = to_m5mols(sd); | |
916 | ||
917 | schedule_work(&info->work_irq); | |
918 | ||
919 | return IRQ_HANDLED; | |
920 | } | |
921 | ||
922 | static int __devinit m5mols_probe(struct i2c_client *client, | |
923 | const struct i2c_device_id *id) | |
924 | { | |
925 | const struct m5mols_platform_data *pdata = client->dev.platform_data; | |
926 | struct m5mols_info *info; | |
927 | struct v4l2_subdev *sd; | |
928 | int ret; | |
929 | ||
930 | if (pdata == NULL) { | |
931 | dev_err(&client->dev, "No platform data\n"); | |
932 | return -EINVAL; | |
933 | } | |
934 | ||
935 | if (!gpio_is_valid(pdata->gpio_reset)) { | |
936 | dev_err(&client->dev, "No valid RESET GPIO specified\n"); | |
937 | return -EINVAL; | |
938 | } | |
939 | ||
940 | if (!pdata->irq) { | |
941 | dev_err(&client->dev, "Interrupt not assigned\n"); | |
942 | return -EINVAL; | |
943 | } | |
944 | ||
945 | info = kzalloc(sizeof(struct m5mols_info), GFP_KERNEL); | |
946 | if (!info) | |
947 | return -ENOMEM; | |
948 | ||
949 | info->pdata = pdata; | |
950 | info->set_power = pdata->set_power; | |
951 | ||
952 | ret = gpio_request(pdata->gpio_reset, "M5MOLS_NRST"); | |
953 | if (ret) { | |
954 | dev_err(&client->dev, "Failed to request gpio: %d\n", ret); | |
955 | goto out_free; | |
956 | } | |
957 | gpio_direction_output(pdata->gpio_reset, pdata->reset_polarity); | |
958 | ||
959 | ret = regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies), supplies); | |
960 | if (ret) { | |
961 | dev_err(&client->dev, "Failed to get regulators: %d\n", ret); | |
962 | goto out_gpio; | |
963 | } | |
964 | ||
965 | sd = &info->sd; | |
966 | strlcpy(sd->name, MODULE_NAME, sizeof(sd->name)); | |
967 | v4l2_i2c_subdev_init(sd, client, &m5mols_ops); | |
968 | ||
969 | info->pad.flags = MEDIA_PAD_FL_SOURCE; | |
970 | ret = media_entity_init(&sd->entity, 1, &info->pad, 0); | |
971 | if (ret < 0) | |
972 | goto out_reg; | |
973 | sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR; | |
974 | ||
975 | init_waitqueue_head(&info->irq_waitq); | |
976 | INIT_WORK(&info->work_irq, m5mols_irq_work); | |
977 | ret = request_irq(pdata->irq, m5mols_irq_handler, | |
978 | IRQF_TRIGGER_RISING, MODULE_NAME, sd); | |
979 | if (ret) { | |
980 | dev_err(&client->dev, "Interrupt request failed: %d\n", ret); | |
981 | goto out_me; | |
982 | } | |
983 | info->res_type = M5MOLS_RESTYPE_MONITOR; | |
984 | return 0; | |
985 | out_me: | |
986 | media_entity_cleanup(&sd->entity); | |
987 | out_reg: | |
988 | regulator_bulk_free(ARRAY_SIZE(supplies), supplies); | |
989 | out_gpio: | |
990 | gpio_free(pdata->gpio_reset); | |
991 | out_free: | |
992 | kfree(info); | |
993 | return ret; | |
994 | } | |
995 | ||
996 | static int __devexit m5mols_remove(struct i2c_client *client) | |
997 | { | |
998 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
999 | struct m5mols_info *info = to_m5mols(sd); | |
1000 | ||
1001 | v4l2_device_unregister_subdev(sd); | |
1002 | free_irq(info->pdata->irq, sd); | |
1003 | ||
1004 | regulator_bulk_free(ARRAY_SIZE(supplies), supplies); | |
1005 | gpio_free(info->pdata->gpio_reset); | |
1006 | media_entity_cleanup(&sd->entity); | |
1007 | kfree(info); | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | static const struct i2c_device_id m5mols_id[] = { | |
1012 | { MODULE_NAME, 0 }, | |
1013 | { }, | |
1014 | }; | |
1015 | MODULE_DEVICE_TABLE(i2c, m5mols_id); | |
1016 | ||
1017 | static struct i2c_driver m5mols_i2c_driver = { | |
1018 | .driver = { | |
1019 | .name = MODULE_NAME, | |
1020 | }, | |
1021 | .probe = m5mols_probe, | |
1022 | .remove = __devexit_p(m5mols_remove), | |
1023 | .id_table = m5mols_id, | |
1024 | }; | |
1025 | ||
1026 | static int __init m5mols_mod_init(void) | |
1027 | { | |
1028 | return i2c_add_driver(&m5mols_i2c_driver); | |
1029 | } | |
1030 | ||
1031 | static void __exit m5mols_mod_exit(void) | |
1032 | { | |
1033 | i2c_del_driver(&m5mols_i2c_driver); | |
1034 | } | |
1035 | ||
1036 | module_init(m5mols_mod_init); | |
1037 | module_exit(m5mols_mod_exit); | |
1038 | ||
1039 | MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>"); | |
1040 | MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>"); | |
1041 | MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver"); | |
1042 | MODULE_LICENSE("GPL"); |