]>
Commit | Line | Data |
---|---|---|
a49d2536 AC |
1 | /* |
2 | * Support for mt9m114 Camera Sensor. | |
3 | * | |
4 | * Copyright (c) 2010 Intel Corporation. All Rights Reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License version | |
8 | * 2 as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | * 02110-1301, USA. | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/types.h> | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/mm.h> | |
26 | #include <linux/string.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/kmod.h> | |
30 | #include <linux/device.h> | |
31 | #include <linux/fs.h> | |
a49d2536 AC |
32 | #include <linux/slab.h> |
33 | #include <linux/delay.h> | |
34 | #include <linux/i2c.h> | |
35 | #include <linux/gpio.h> | |
36 | #include <linux/acpi.h> | |
25016567 | 37 | #include "../include/linux/atomisp_gmin_platform.h" |
a49d2536 AC |
38 | #include <media/v4l2-device.h> |
39 | ||
40 | #include "mt9m114.h" | |
41 | ||
42 | #define to_mt9m114_sensor(sd) container_of(sd, struct mt9m114_device, sd) | |
43 | ||
44 | /* | |
45 | * TODO: use debug parameter to actually define when debug messages should | |
46 | * be printed. | |
47 | */ | |
48 | static int debug; | |
49 | static int aaalock; | |
50 | module_param(debug, int, 0644); | |
51 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); | |
52 | ||
53 | static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value); | |
54 | static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value); | |
55 | static int mt9m114_wait_state(struct i2c_client *client, int timeout); | |
56 | ||
57 | static int | |
58 | mt9m114_read_reg(struct i2c_client *client, u16 data_length, u32 reg, u32 *val) | |
59 | { | |
60 | int err; | |
61 | struct i2c_msg msg[2]; | |
62 | unsigned char data[4]; | |
63 | ||
64 | if (!client->adapter) { | |
65 | v4l2_err(client, "%s error, no client->adapter\n", __func__); | |
66 | return -ENODEV; | |
67 | } | |
68 | ||
69 | if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT | |
70 | && data_length != MISENSOR_32BIT) { | |
71 | v4l2_err(client, "%s error, invalid data length\n", __func__); | |
72 | return -EINVAL; | |
73 | } | |
74 | ||
75 | msg[0].addr = client->addr; | |
76 | msg[0].flags = 0; | |
77 | msg[0].len = MSG_LEN_OFFSET; | |
78 | msg[0].buf = data; | |
79 | ||
80 | /* high byte goes out first */ | |
81 | data[0] = (u16) (reg >> 8); | |
82 | data[1] = (u16) (reg & 0xff); | |
83 | ||
84 | msg[1].addr = client->addr; | |
85 | msg[1].len = data_length; | |
86 | msg[1].flags = I2C_M_RD; | |
87 | msg[1].buf = data; | |
88 | ||
89 | err = i2c_transfer(client->adapter, msg, 2); | |
90 | ||
91 | if (err >= 0) { | |
92 | *val = 0; | |
93 | /* high byte comes first */ | |
94 | if (data_length == MISENSOR_8BIT) | |
95 | *val = data[0]; | |
96 | else if (data_length == MISENSOR_16BIT) | |
97 | *val = data[1] + (data[0] << 8); | |
98 | else | |
99 | *val = data[3] + (data[2] << 8) + | |
100 | (data[1] << 16) + (data[0] << 24); | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | dev_err(&client->dev, "read from offset 0x%x error %d", reg, err); | |
106 | return err; | |
107 | } | |
108 | ||
109 | static int | |
110 | mt9m114_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u32 val) | |
111 | { | |
112 | int num_msg; | |
113 | struct i2c_msg msg; | |
114 | unsigned char data[6] = {0}; | |
115 | u16 *wreg; | |
116 | int retry = 0; | |
117 | ||
118 | if (!client->adapter) { | |
119 | v4l2_err(client, "%s error, no client->adapter\n", __func__); | |
120 | return -ENODEV; | |
121 | } | |
122 | ||
123 | if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT | |
124 | && data_length != MISENSOR_32BIT) { | |
125 | v4l2_err(client, "%s error, invalid data_length\n", __func__); | |
126 | return -EINVAL; | |
127 | } | |
128 | ||
129 | memset(&msg, 0, sizeof(msg)); | |
130 | ||
131 | again: | |
132 | msg.addr = client->addr; | |
133 | msg.flags = 0; | |
134 | msg.len = 2 + data_length; | |
135 | msg.buf = data; | |
136 | ||
137 | /* high byte goes out first */ | |
138 | wreg = (u16 *)data; | |
139 | *wreg = cpu_to_be16(reg); | |
140 | ||
141 | if (data_length == MISENSOR_8BIT) { | |
142 | data[2] = (u8)(val); | |
143 | } else if (data_length == MISENSOR_16BIT) { | |
144 | u16 *wdata = (u16 *)&data[2]; | |
145 | *wdata = be16_to_cpu((u16)val); | |
146 | } else { | |
147 | /* MISENSOR_32BIT */ | |
148 | u32 *wdata = (u32 *)&data[2]; | |
149 | *wdata = be32_to_cpu(val); | |
150 | } | |
151 | ||
152 | num_msg = i2c_transfer(client->adapter, &msg, 1); | |
153 | ||
154 | /* | |
155 | * HACK: Need some delay here for Rev 2 sensors otherwise some | |
156 | * registers do not seem to load correctly. | |
157 | */ | |
158 | mdelay(1); | |
159 | ||
160 | if (num_msg >= 0) | |
161 | return 0; | |
162 | ||
163 | dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d", | |
164 | val, reg, num_msg); | |
165 | if (retry <= I2C_RETRY_COUNT) { | |
166 | dev_dbg(&client->dev, "retrying... %d", retry); | |
167 | retry++; | |
168 | msleep(20); | |
169 | goto again; | |
170 | } | |
171 | ||
172 | return num_msg; | |
173 | } | |
174 | ||
175 | /** | |
176 | * misensor_rmw_reg - Read/Modify/Write a value to a register in the sensor | |
177 | * device | |
178 | * @client: i2c driver client structure | |
179 | * @data_length: 8/16/32-bits length | |
180 | * @reg: register address | |
181 | * @mask: masked out bits | |
182 | * @set: bits set | |
183 | * | |
184 | * Read/modify/write a value to a register in the sensor device. | |
185 | * Returns zero if successful, or non-zero otherwise. | |
186 | */ | |
187 | static int | |
188 | misensor_rmw_reg(struct i2c_client *client, u16 data_length, u16 reg, | |
189 | u32 mask, u32 set) | |
190 | { | |
191 | int err; | |
192 | u32 val; | |
193 | ||
194 | /* Exit when no mask */ | |
195 | if (mask == 0) | |
196 | return 0; | |
197 | ||
198 | /* @mask must not exceed data length */ | |
199 | switch (data_length) { | |
200 | case MISENSOR_8BIT: | |
201 | if (mask & ~0xff) | |
202 | return -EINVAL; | |
203 | break; | |
204 | case MISENSOR_16BIT: | |
205 | if (mask & ~0xffff) | |
206 | return -EINVAL; | |
207 | break; | |
208 | case MISENSOR_32BIT: | |
209 | break; | |
210 | default: | |
211 | /* Wrong @data_length */ | |
212 | return -EINVAL; | |
213 | } | |
214 | ||
215 | err = mt9m114_read_reg(client, data_length, reg, &val); | |
216 | if (err) { | |
217 | v4l2_err(client, "misensor_rmw_reg error exit, read failed\n"); | |
218 | return -EINVAL; | |
219 | } | |
220 | ||
221 | val &= ~mask; | |
222 | ||
223 | /* | |
224 | * Perform the OR function if the @set exists. | |
225 | * Shift @set value to target bit location. @set should set only | |
226 | * bits included in @mask. | |
227 | * | |
228 | * REVISIT: This function expects @set to be non-shifted. Its shift | |
229 | * value is then defined to be equal to mask's LSB position. | |
230 | * How about to inform values in their right offset position and avoid | |
231 | * this unneeded shift operation? | |
232 | */ | |
233 | set <<= ffs(mask) - 1; | |
234 | val |= set & mask; | |
235 | ||
236 | err = mt9m114_write_reg(client, data_length, reg, val); | |
237 | if (err) { | |
238 | v4l2_err(client, "misensor_rmw_reg error exit, write failed\n"); | |
239 | return -EINVAL; | |
240 | } | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
245 | ||
246 | static int __mt9m114_flush_reg_array(struct i2c_client *client, | |
247 | struct mt9m114_write_ctrl *ctrl) | |
248 | { | |
249 | struct i2c_msg msg; | |
250 | const int num_msg = 1; | |
251 | int ret; | |
252 | int retry = 0; | |
253 | ||
254 | if (ctrl->index == 0) | |
255 | return 0; | |
256 | ||
257 | again: | |
258 | msg.addr = client->addr; | |
259 | msg.flags = 0; | |
260 | msg.len = 2 + ctrl->index; | |
261 | ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr); | |
262 | msg.buf = (u8 *)&ctrl->buffer; | |
263 | ||
264 | ret = i2c_transfer(client->adapter, &msg, num_msg); | |
265 | if (ret != num_msg) { | |
266 | if (++retry <= I2C_RETRY_COUNT) { | |
267 | dev_dbg(&client->dev, "retrying... %d\n", retry); | |
268 | msleep(20); | |
269 | goto again; | |
270 | } | |
271 | dev_err(&client->dev, "%s: i2c transfer error\n", __func__); | |
272 | return -EIO; | |
273 | } | |
274 | ||
275 | ctrl->index = 0; | |
276 | ||
277 | /* | |
278 | * REVISIT: Previously we had a delay after writing data to sensor. | |
279 | * But it was removed as our tests have shown it is not necessary | |
280 | * anymore. | |
281 | */ | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
286 | static int __mt9m114_buf_reg_array(struct i2c_client *client, | |
287 | struct mt9m114_write_ctrl *ctrl, | |
288 | const struct misensor_reg *next) | |
289 | { | |
290 | u16 *data16; | |
291 | u32 *data32; | |
292 | int err; | |
293 | ||
294 | /* Insufficient buffer? Let's flush and get more free space. */ | |
295 | if (ctrl->index + next->length >= MT9M114_MAX_WRITE_BUF_SIZE) { | |
296 | err = __mt9m114_flush_reg_array(client, ctrl); | |
297 | if (err) | |
298 | return err; | |
299 | } | |
300 | ||
301 | switch (next->length) { | |
302 | case MISENSOR_8BIT: | |
303 | ctrl->buffer.data[ctrl->index] = (u8)next->val; | |
304 | break; | |
305 | case MISENSOR_16BIT: | |
306 | data16 = (u16 *)&ctrl->buffer.data[ctrl->index]; | |
307 | *data16 = cpu_to_be16((u16)next->val); | |
308 | break; | |
309 | case MISENSOR_32BIT: | |
310 | data32 = (u32 *)&ctrl->buffer.data[ctrl->index]; | |
311 | *data32 = cpu_to_be32(next->val); | |
312 | break; | |
313 | default: | |
314 | return -EINVAL; | |
315 | } | |
316 | ||
317 | /* When first item is added, we need to store its starting address */ | |
318 | if (ctrl->index == 0) | |
319 | ctrl->buffer.addr = next->reg; | |
320 | ||
321 | ctrl->index += next->length; | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
326 | static int | |
327 | __mt9m114_write_reg_is_consecutive(struct i2c_client *client, | |
328 | struct mt9m114_write_ctrl *ctrl, | |
329 | const struct misensor_reg *next) | |
330 | { | |
331 | if (ctrl->index == 0) | |
332 | return 1; | |
333 | ||
334 | return ctrl->buffer.addr + ctrl->index == next->reg; | |
335 | } | |
336 | ||
337 | /* | |
338 | * mt9m114_write_reg_array - Initializes a list of mt9m114 registers | |
339 | * @client: i2c driver client structure | |
340 | * @reglist: list of registers to be written | |
341 | * @poll: completion polling requirement | |
342 | * This function initializes a list of registers. When consecutive addresses | |
343 | * are found in a row on the list, this function creates a buffer and sends | |
344 | * consecutive data in a single i2c_transfer(). | |
345 | * | |
346 | * __mt9m114_flush_reg_array, __mt9m114_buf_reg_array() and | |
347 | * __mt9m114_write_reg_is_consecutive() are internal functions to | |
348 | * mt9m114_write_reg_array() and should be not used anywhere else. | |
349 | * | |
350 | */ | |
351 | static int mt9m114_write_reg_array(struct i2c_client *client, | |
352 | const struct misensor_reg *reglist, | |
353 | int poll) | |
354 | { | |
355 | const struct misensor_reg *next = reglist; | |
356 | struct mt9m114_write_ctrl ctrl; | |
357 | int err; | |
358 | ||
359 | if (poll == PRE_POLLING) { | |
360 | err = mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT); | |
361 | if (err) | |
362 | return err; | |
363 | } | |
364 | ||
365 | ctrl.index = 0; | |
366 | for (; next->length != MISENSOR_TOK_TERM; next++) { | |
367 | switch (next->length & MISENSOR_TOK_MASK) { | |
368 | case MISENSOR_TOK_DELAY: | |
369 | err = __mt9m114_flush_reg_array(client, &ctrl); | |
370 | if (err) | |
371 | return err; | |
372 | msleep(next->val); | |
373 | break; | |
374 | case MISENSOR_TOK_RMW: | |
375 | err = __mt9m114_flush_reg_array(client, &ctrl); | |
376 | err |= misensor_rmw_reg(client, | |
377 | next->length & | |
378 | ~MISENSOR_TOK_RMW, | |
379 | next->reg, next->val, | |
380 | next->val2); | |
381 | if (err) { | |
382 | dev_err(&client->dev, "%s read err. aborted\n", | |
383 | __func__); | |
384 | return -EINVAL; | |
385 | } | |
386 | break; | |
387 | default: | |
388 | /* | |
389 | * If next address is not consecutive, data needs to be | |
390 | * flushed before proceed. | |
391 | */ | |
392 | if (!__mt9m114_write_reg_is_consecutive(client, &ctrl, | |
393 | next)) { | |
394 | err = __mt9m114_flush_reg_array(client, &ctrl); | |
395 | if (err) | |
396 | return err; | |
397 | } | |
398 | err = __mt9m114_buf_reg_array(client, &ctrl, next); | |
399 | if (err) { | |
400 | v4l2_err(client, "%s: write error, aborted\n", | |
401 | __func__); | |
402 | return err; | |
403 | } | |
404 | break; | |
405 | } | |
406 | } | |
407 | ||
408 | err = __mt9m114_flush_reg_array(client, &ctrl); | |
409 | if (err) | |
410 | return err; | |
411 | ||
412 | if (poll == POST_POLLING) | |
413 | return mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT); | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
418 | static int mt9m114_wait_state(struct i2c_client *client, int timeout) | |
419 | { | |
420 | int ret; | |
421 | unsigned int val; | |
422 | ||
423 | while (timeout-- > 0) { | |
424 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, 0x0080, &val); | |
425 | if (ret) | |
426 | return ret; | |
427 | if ((val & 0x2) == 0) | |
428 | return 0; | |
429 | msleep(20); | |
430 | } | |
431 | ||
432 | return -EINVAL; | |
433 | ||
434 | } | |
435 | ||
436 | static int mt9m114_set_suspend(struct v4l2_subdev *sd) | |
437 | { | |
438 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
439 | return mt9m114_write_reg_array(client, | |
440 | mt9m114_standby_reg, POST_POLLING); | |
441 | } | |
442 | ||
443 | static int mt9m114_init_common(struct v4l2_subdev *sd) | |
444 | { | |
445 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
a49d2536 | 446 | |
b612a976 | 447 | return mt9m114_write_reg_array(client, mt9m114_common, PRE_POLLING); |
a49d2536 AC |
448 | } |
449 | ||
450 | static int power_ctrl(struct v4l2_subdev *sd, bool flag) | |
451 | { | |
452 | int ret; | |
453 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
454 | ||
455 | if (!dev || !dev->platform_data) | |
456 | return -ENODEV; | |
457 | ||
458 | /* Non-gmin platforms use the legacy callback */ | |
459 | if (dev->platform_data->power_ctrl) | |
460 | return dev->platform_data->power_ctrl(sd, flag); | |
461 | ||
462 | if (flag) { | |
463 | ret = dev->platform_data->v2p8_ctrl(sd, 1); | |
464 | if (ret == 0) { | |
465 | ret = dev->platform_data->v1p8_ctrl(sd, 1); | |
466 | if (ret) | |
467 | ret = dev->platform_data->v2p8_ctrl(sd, 0); | |
468 | } | |
469 | } else { | |
470 | ret = dev->platform_data->v2p8_ctrl(sd, 0); | |
471 | ret = dev->platform_data->v1p8_ctrl(sd, 0); | |
472 | } | |
473 | return ret; | |
474 | } | |
475 | ||
476 | static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) | |
477 | { | |
478 | int ret; | |
479 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
480 | ||
481 | if (!dev || !dev->platform_data) | |
482 | return -ENODEV; | |
483 | ||
484 | /* Non-gmin platforms use the legacy callback */ | |
485 | if (dev->platform_data->gpio_ctrl) | |
486 | return dev->platform_data->gpio_ctrl(sd, flag); | |
487 | ||
488 | /* Note: current modules wire only one GPIO signal (RESET#), | |
489 | * but the schematic wires up two to the connector. BIOS | |
490 | * versions have been unfortunately inconsistent with which | |
491 | * ACPI index RESET# is on, so hit both */ | |
492 | ||
493 | if (flag) { | |
494 | ret = dev->platform_data->gpio0_ctrl(sd, 0); | |
495 | ret = dev->platform_data->gpio1_ctrl(sd, 0); | |
496 | msleep(60); | |
497 | ret |= dev->platform_data->gpio0_ctrl(sd, 1); | |
498 | ret |= dev->platform_data->gpio1_ctrl(sd, 1); | |
499 | } else { | |
500 | ret = dev->platform_data->gpio0_ctrl(sd, 0); | |
501 | ret = dev->platform_data->gpio1_ctrl(sd, 0); | |
502 | } | |
503 | return ret; | |
504 | } | |
505 | ||
506 | static int power_up(struct v4l2_subdev *sd) | |
507 | { | |
508 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
509 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
510 | int ret; | |
511 | ||
512 | if (NULL == dev->platform_data) { | |
513 | dev_err(&client->dev, "no camera_sensor_platform_data"); | |
514 | return -ENODEV; | |
515 | } | |
516 | ||
517 | /* power control */ | |
518 | ret = power_ctrl(sd, 1); | |
519 | if (ret) | |
520 | goto fail_power; | |
521 | ||
522 | /* flis clock control */ | |
523 | ret = dev->platform_data->flisclk_ctrl(sd, 1); | |
524 | if (ret) | |
525 | goto fail_clk; | |
526 | ||
527 | /* gpio ctrl */ | |
528 | ret = gpio_ctrl(sd, 1); | |
529 | if (ret) | |
530 | dev_err(&client->dev, "gpio failed 1\n"); | |
531 | /* | |
532 | * according to DS, 44ms is needed between power up and first i2c | |
533 | * commend | |
534 | */ | |
535 | msleep(50); | |
536 | ||
537 | return 0; | |
538 | ||
539 | fail_clk: | |
540 | dev->platform_data->flisclk_ctrl(sd, 0); | |
541 | fail_power: | |
542 | power_ctrl(sd, 0); | |
543 | dev_err(&client->dev, "sensor power-up failed\n"); | |
544 | ||
545 | return ret; | |
546 | } | |
547 | ||
548 | static int power_down(struct v4l2_subdev *sd) | |
549 | { | |
550 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
551 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
552 | int ret; | |
553 | ||
554 | if (NULL == dev->platform_data) { | |
555 | dev_err(&client->dev, "no camera_sensor_platform_data"); | |
556 | return -ENODEV; | |
557 | } | |
558 | ||
559 | ret = dev->platform_data->flisclk_ctrl(sd, 0); | |
560 | if (ret) | |
561 | dev_err(&client->dev, "flisclk failed\n"); | |
562 | ||
563 | /* gpio ctrl */ | |
564 | ret = gpio_ctrl(sd, 0); | |
565 | if (ret) | |
566 | dev_err(&client->dev, "gpio failed 1\n"); | |
567 | ||
568 | /* power control */ | |
569 | ret = power_ctrl(sd, 0); | |
570 | if (ret) | |
571 | dev_err(&client->dev, "vprog failed.\n"); | |
572 | ||
573 | /*according to DS, 20ms is needed after power down*/ | |
574 | msleep(20); | |
575 | ||
576 | return ret; | |
577 | } | |
578 | ||
579 | static int mt9m114_s_power(struct v4l2_subdev *sd, int power) | |
580 | { | |
581 | if (power == 0) | |
582 | return power_down(sd); | |
583 | else { | |
584 | if (power_up(sd)) | |
585 | return -EINVAL; | |
586 | ||
587 | return mt9m114_init_common(sd); | |
588 | } | |
589 | } | |
590 | ||
591 | /* | |
592 | * distance - calculate the distance | |
593 | * @res: resolution | |
594 | * @w: width | |
595 | * @h: height | |
596 | * | |
597 | * Get the gap between resolution and w/h. | |
598 | * res->width/height smaller than w/h wouldn't be considered. | |
599 | * Returns the value of gap or -1 if fail. | |
600 | */ | |
601 | #define LARGEST_ALLOWED_RATIO_MISMATCH 600 | |
602 | static int distance(struct mt9m114_res_struct const *res, u32 w, u32 h) | |
603 | { | |
604 | unsigned int w_ratio; | |
605 | unsigned int h_ratio; | |
606 | int match; | |
607 | ||
608 | if (w == 0) | |
609 | return -1; | |
610 | w_ratio = (res->width << 13) / w; | |
611 | if (h == 0) | |
612 | return -1; | |
613 | h_ratio = (res->height << 13) / h; | |
614 | if (h_ratio == 0) | |
615 | return -1; | |
bf5d0300 | 616 | match = abs(((w_ratio << 13) / h_ratio) - 8192); |
a49d2536 | 617 | |
8284d205 VR |
618 | if ((w_ratio < 8192) || (h_ratio < 8192) || |
619 | (match > LARGEST_ALLOWED_RATIO_MISMATCH)) | |
a49d2536 AC |
620 | return -1; |
621 | ||
622 | return w_ratio + h_ratio; | |
623 | } | |
624 | ||
625 | /* Return the nearest higher resolution index */ | |
626 | static int nearest_resolution_index(int w, int h) | |
627 | { | |
628 | int i; | |
629 | int idx = -1; | |
630 | int dist; | |
631 | int min_dist = INT_MAX; | |
632 | const struct mt9m114_res_struct *tmp_res = NULL; | |
633 | ||
634 | for (i = 0; i < ARRAY_SIZE(mt9m114_res); i++) { | |
635 | tmp_res = &mt9m114_res[i]; | |
636 | dist = distance(tmp_res, w, h); | |
637 | if (dist == -1) | |
638 | continue; | |
639 | if (dist < min_dist) { | |
640 | min_dist = dist; | |
641 | idx = i; | |
642 | } | |
643 | } | |
644 | ||
645 | return idx; | |
646 | } | |
647 | ||
648 | static int mt9m114_try_res(u32 *w, u32 *h) | |
649 | { | |
650 | int idx = 0; | |
651 | ||
652 | if ((*w > MT9M114_RES_960P_SIZE_H) | |
653 | || (*h > MT9M114_RES_960P_SIZE_V)) { | |
654 | *w = MT9M114_RES_960P_SIZE_H; | |
655 | *h = MT9M114_RES_960P_SIZE_V; | |
656 | } else { | |
657 | idx = nearest_resolution_index(*w, *h); | |
658 | ||
659 | /* | |
660 | * nearest_resolution_index() doesn't return smaller | |
661 | * resolutions. If it fails, it means the requested | |
662 | * resolution is higher than wecan support. Fallback | |
663 | * to highest possible resolution in this case. | |
664 | */ | |
665 | if (idx == -1) | |
666 | idx = ARRAY_SIZE(mt9m114_res) - 1; | |
667 | ||
668 | *w = mt9m114_res[idx].width; | |
669 | *h = mt9m114_res[idx].height; | |
670 | } | |
671 | ||
672 | return 0; | |
673 | } | |
674 | ||
675 | static struct mt9m114_res_struct *mt9m114_to_res(u32 w, u32 h) | |
676 | { | |
677 | int index; | |
678 | ||
679 | for (index = 0; index < N_RES; index++) { | |
680 | if ((mt9m114_res[index].width == w) && | |
681 | (mt9m114_res[index].height == h)) | |
682 | break; | |
683 | } | |
684 | ||
685 | /* No mode found */ | |
686 | if (index >= N_RES) | |
687 | return NULL; | |
688 | ||
689 | return &mt9m114_res[index]; | |
690 | } | |
691 | ||
8d7cd91d | 692 | static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size) |
a49d2536 | 693 | { |
8d7cd91d | 694 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); |
a49d2536 AC |
695 | unsigned short hsize; |
696 | unsigned short vsize; | |
697 | ||
8d7cd91d | 698 | switch (dev->res) { |
a49d2536 AC |
699 | case MT9M114_RES_736P: |
700 | hsize = MT9M114_RES_736P_SIZE_H; | |
701 | vsize = MT9M114_RES_736P_SIZE_V; | |
702 | break; | |
703 | case MT9M114_RES_864P: | |
704 | hsize = MT9M114_RES_864P_SIZE_H; | |
705 | vsize = MT9M114_RES_864P_SIZE_V; | |
706 | break; | |
707 | case MT9M114_RES_960P: | |
708 | hsize = MT9M114_RES_960P_SIZE_H; | |
709 | vsize = MT9M114_RES_960P_SIZE_V; | |
710 | break; | |
711 | default: | |
8d7cd91d AB |
712 | v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__, |
713 | dev->res); | |
a49d2536 AC |
714 | return -EINVAL; |
715 | } | |
716 | ||
717 | if (h_size != NULL) | |
718 | *h_size = hsize; | |
719 | if (v_size != NULL) | |
720 | *v_size = vsize; | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
725 | static int mt9m114_get_intg_factor(struct i2c_client *client, | |
726 | struct camera_mipi_info *info, | |
727 | const struct mt9m114_res_struct *res) | |
728 | { | |
729 | struct atomisp_sensor_mode_data *buf = &info->data; | |
730 | u32 reg_val; | |
731 | int ret; | |
732 | ||
733 | if (info == NULL) | |
734 | return -EINVAL; | |
735 | ||
736 | ret = mt9m114_read_reg(client, MISENSOR_32BIT, | |
737 | REG_PIXEL_CLK, ®_val); | |
738 | if (ret) | |
739 | return ret; | |
740 | buf->vt_pix_clk_freq_mhz = reg_val; | |
741 | ||
742 | /* get integration time */ | |
743 | buf->coarse_integration_time_min = MT9M114_COARSE_INTG_TIME_MIN; | |
744 | buf->coarse_integration_time_max_margin = | |
745 | MT9M114_COARSE_INTG_TIME_MAX_MARGIN; | |
746 | ||
747 | buf->fine_integration_time_min = MT9M114_FINE_INTG_TIME_MIN; | |
748 | buf->fine_integration_time_max_margin = | |
749 | MT9M114_FINE_INTG_TIME_MAX_MARGIN; | |
750 | ||
751 | buf->fine_integration_time_def = MT9M114_FINE_INTG_TIME_MIN; | |
752 | ||
753 | buf->frame_length_lines = res->lines_per_frame; | |
754 | buf->line_length_pck = res->pixels_per_line; | |
755 | buf->read_mode = res->bin_mode; | |
756 | ||
757 | /* get the cropping and output resolution to ISP for this mode. */ | |
758 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
759 | REG_H_START, ®_val); | |
760 | if (ret) | |
761 | return ret; | |
762 | buf->crop_horizontal_start = reg_val; | |
763 | ||
764 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
765 | REG_V_START, ®_val); | |
766 | if (ret) | |
767 | return ret; | |
768 | buf->crop_vertical_start = reg_val; | |
769 | ||
770 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
771 | REG_H_END, ®_val); | |
772 | if (ret) | |
773 | return ret; | |
774 | buf->crop_horizontal_end = reg_val; | |
775 | ||
776 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
777 | REG_V_END, ®_val); | |
778 | if (ret) | |
779 | return ret; | |
780 | buf->crop_vertical_end = reg_val; | |
781 | ||
782 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
783 | REG_WIDTH, ®_val); | |
784 | if (ret) | |
785 | return ret; | |
786 | buf->output_width = reg_val; | |
787 | ||
788 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
789 | REG_HEIGHT, ®_val); | |
790 | if (ret) | |
791 | return ret; | |
792 | buf->output_height = reg_val; | |
793 | ||
794 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
795 | REG_TIMING_HTS, ®_val); | |
796 | if (ret) | |
797 | return ret; | |
798 | buf->line_length_pck = reg_val; | |
799 | ||
800 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
801 | REG_TIMING_VTS, ®_val); | |
802 | if (ret) | |
803 | return ret; | |
804 | buf->frame_length_lines = reg_val; | |
805 | ||
806 | buf->binning_factor_x = res->bin_factor_x ? | |
807 | res->bin_factor_x : 1; | |
808 | buf->binning_factor_y = res->bin_factor_y ? | |
809 | res->bin_factor_y : 1; | |
810 | return 0; | |
811 | } | |
812 | ||
813 | static int mt9m114_get_fmt(struct v4l2_subdev *sd, | |
814 | struct v4l2_subdev_pad_config *cfg, | |
815 | struct v4l2_subdev_format *format) | |
816 | { | |
8d7cd91d | 817 | struct v4l2_mbus_framefmt *fmt = &format->format; |
a49d2536 AC |
818 | int width, height; |
819 | int ret; | |
820 | if (format->pad) | |
821 | return -EINVAL; | |
822 | fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; | |
823 | ||
8d7cd91d | 824 | ret = mt9m114_res2size(sd, &width, &height); |
a49d2536 AC |
825 | if (ret) |
826 | return ret; | |
827 | fmt->width = width; | |
828 | fmt->height = height; | |
829 | ||
830 | return 0; | |
831 | } | |
832 | ||
833 | static int mt9m114_set_fmt(struct v4l2_subdev *sd, | |
834 | struct v4l2_subdev_pad_config *cfg, | |
835 | struct v4l2_subdev_format *format) | |
836 | { | |
837 | struct v4l2_mbus_framefmt *fmt = &format->format; | |
838 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
839 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
840 | struct mt9m114_res_struct *res_index; | |
841 | u32 width = fmt->width; | |
842 | u32 height = fmt->height; | |
843 | struct camera_mipi_info *mt9m114_info = NULL; | |
844 | ||
845 | int ret; | |
846 | if (format->pad) | |
847 | return -EINVAL; | |
848 | dev->streamon = 0; | |
849 | dev->first_exp = MT9M114_DEFAULT_FIRST_EXP; | |
850 | ||
851 | mt9m114_info = v4l2_get_subdev_hostdata(sd); | |
852 | if (mt9m114_info == NULL) | |
853 | return -EINVAL; | |
854 | ||
855 | mt9m114_try_res(&width, &height); | |
856 | if (format->which == V4L2_SUBDEV_FORMAT_TRY) { | |
857 | cfg->try_fmt = *fmt; | |
858 | return 0; | |
859 | } | |
860 | res_index = mt9m114_to_res(width, height); | |
861 | ||
862 | /* Sanity check */ | |
863 | if (unlikely(!res_index)) { | |
864 | WARN_ON(1); | |
865 | return -EINVAL; | |
866 | } | |
867 | ||
868 | switch (res_index->res) { | |
869 | case MT9M114_RES_736P: | |
870 | ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING); | |
871 | ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, | |
872 | MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET); | |
873 | break; | |
874 | case MT9M114_RES_864P: | |
875 | ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING); | |
876 | ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, | |
877 | MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET); | |
878 | break; | |
879 | case MT9M114_RES_960P: | |
880 | ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING); | |
881 | /* set sensor read_mode to Normal */ | |
882 | ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, | |
883 | MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET); | |
884 | break; | |
885 | default: | |
886 | v4l2_err(sd, "set resolution: %d failed!\n", res_index->res); | |
887 | return -EINVAL; | |
888 | } | |
889 | ||
890 | if (ret) | |
891 | return -EINVAL; | |
892 | ||
893 | ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING); | |
894 | if (ret < 0) | |
895 | return ret; | |
896 | ||
897 | if (mt9m114_set_suspend(sd)) | |
898 | return -EINVAL; | |
899 | ||
900 | if (dev->res != res_index->res) { | |
901 | int index; | |
902 | ||
903 | /* Switch to different size */ | |
904 | if (width <= 640) { | |
905 | dev->nctx = 0x00; /* Set for context A */ | |
906 | } else { | |
907 | /* | |
908 | * Context B is used for resolutions larger than 640x480 | |
909 | * Using YUV for Context B. | |
910 | */ | |
911 | dev->nctx = 0x01; /* set for context B */ | |
912 | } | |
913 | ||
914 | /* | |
915 | * Marked current sensor res as being "used" | |
916 | * | |
917 | * REVISIT: We don't need to use an "used" field on each mode | |
918 | * list entry to know which mode is selected. If this | |
919 | * information is really necessary, how about to use a single | |
920 | * variable on sensor dev struct? | |
921 | */ | |
922 | for (index = 0; index < N_RES; index++) { | |
923 | if ((width == mt9m114_res[index].width) && | |
924 | (height == mt9m114_res[index].height)) { | |
87bb4017 | 925 | mt9m114_res[index].used = true; |
a49d2536 AC |
926 | continue; |
927 | } | |
87bb4017 | 928 | mt9m114_res[index].used = false; |
a49d2536 AC |
929 | } |
930 | } | |
931 | ret = mt9m114_get_intg_factor(c, mt9m114_info, | |
932 | &mt9m114_res[res_index->res]); | |
933 | if (ret) { | |
934 | dev_err(&c->dev, "failed to get integration_factor\n"); | |
935 | return -EINVAL; | |
936 | } | |
937 | /* | |
938 | * mt9m114 - we don't poll for context switch | |
939 | * because it does not happen with streaming disabled. | |
940 | */ | |
941 | dev->res = res_index->res; | |
942 | ||
943 | fmt->width = width; | |
944 | fmt->height = height; | |
945 | fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; | |
946 | return 0; | |
947 | } | |
948 | ||
949 | /* TODO: Update to SOC functions, remove exposure and gain */ | |
950 | static int mt9m114_g_focal(struct v4l2_subdev *sd, s32 *val) | |
951 | { | |
952 | *val = (MT9M114_FOCAL_LENGTH_NUM << 16) | MT9M114_FOCAL_LENGTH_DEM; | |
953 | return 0; | |
954 | } | |
955 | ||
956 | static int mt9m114_g_fnumber(struct v4l2_subdev *sd, s32 *val) | |
957 | { | |
958 | /*const f number for mt9m114*/ | |
959 | *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 16) | MT9M114_F_NUMBER_DEM; | |
960 | return 0; | |
961 | } | |
962 | ||
963 | static int mt9m114_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) | |
964 | { | |
965 | *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 24) | | |
966 | (MT9M114_F_NUMBER_DEM << 16) | | |
967 | (MT9M114_F_NUMBER_DEFAULT_NUM << 8) | MT9M114_F_NUMBER_DEM; | |
968 | return 0; | |
969 | } | |
970 | ||
971 | /* Horizontal flip the image. */ | |
972 | static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val) | |
973 | { | |
974 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
975 | int ret; | |
976 | u32 data; | |
977 | ret = mt9m114_read_reg(c, MISENSOR_16BIT, | |
978 | (u32)MISENSOR_READ_MODE, &data); | |
979 | if (ret) | |
980 | return ret; | |
981 | *val = !!(data & MISENSOR_HFLIP_MASK); | |
982 | ||
983 | return 0; | |
984 | } | |
985 | ||
986 | static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val) | |
987 | { | |
988 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
989 | int ret; | |
990 | u32 data; | |
991 | ||
992 | ret = mt9m114_read_reg(c, MISENSOR_16BIT, | |
993 | (u32)MISENSOR_READ_MODE, &data); | |
994 | if (ret) | |
995 | return ret; | |
996 | *val = !!(data & MISENSOR_VFLIP_MASK); | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
1001 | static long mt9m114_s_exposure(struct v4l2_subdev *sd, | |
1002 | struct atomisp_exposure *exposure) | |
1003 | { | |
1004 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1005 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
1006 | int ret = 0; | |
1007 | unsigned int coarse_integration = 0; | |
1008 | unsigned int fine_integration = 0; | |
1009 | unsigned int FLines = 0; | |
1010 | unsigned int FrameLengthLines = 0; /* ExposureTime.FrameLengthLines; */ | |
1011 | unsigned int AnalogGain, DigitalGain; | |
1012 | u32 AnalogGainToWrite = 0; | |
1013 | u16 exposure_local[3]; | |
1014 | ||
1015 | dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__, | |
1016 | exposure->integration_time[0], exposure->gain[0], | |
1017 | exposure->gain[1]); | |
1018 | ||
1019 | coarse_integration = exposure->integration_time[0]; | |
1020 | /* fine_integration = ExposureTime.FineIntegrationTime; */ | |
1021 | /* FrameLengthLines = ExposureTime.FrameLengthLines; */ | |
1022 | FLines = mt9m114_res[dev->res].lines_per_frame; | |
1023 | AnalogGain = exposure->gain[0]; | |
1024 | DigitalGain = exposure->gain[1]; | |
1025 | if (!dev->streamon) { | |
1026 | /*Save the first exposure values while stream is off*/ | |
1027 | dev->first_exp = coarse_integration; | |
1028 | dev->first_gain = AnalogGain; | |
1029 | dev->first_diggain = DigitalGain; | |
1030 | } | |
1031 | /* DigitalGain = 0x400 * (((u16) DigitalGain) >> 8) + | |
1032 | ((unsigned int)(0x400 * (((u16) DigitalGain) & 0xFF)) >>8); */ | |
1033 | ||
1034 | /* set frame length */ | |
1035 | if (FLines < coarse_integration + 6) | |
1036 | FLines = coarse_integration + 6; | |
1037 | if (FLines < FrameLengthLines) | |
1038 | FLines = FrameLengthLines; | |
1039 | ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, FLines); | |
1040 | if (ret) { | |
1041 | v4l2_err(client, "%s: fail to set FLines\n", __func__); | |
1042 | return -EINVAL; | |
1043 | } | |
1044 | ||
1045 | /* set coarse/fine integration */ | |
1046 | exposure_local[0] = REG_EXPO_COARSE; | |
1047 | exposure_local[1] = (u16)coarse_integration; | |
1048 | exposure_local[2] = (u16)fine_integration; | |
1049 | /* 3A provide real exposure time. | |
1050 | should not translate to any value here. */ | |
1051 | ret = mt9m114_write_reg(client, MISENSOR_16BIT, | |
1052 | REG_EXPO_COARSE, (u16)(coarse_integration)); | |
1053 | if (ret) { | |
1054 | v4l2_err(client, "%s: fail to set exposure time\n", __func__); | |
1055 | return -EINVAL; | |
1056 | } | |
1057 | ||
1058 | /* | |
1059 | // set analog/digital gain | |
1060 | switch(AnalogGain) | |
1061 | { | |
1062 | case 0: | |
1063 | AnalogGainToWrite = 0x0; | |
1064 | break; | |
1065 | case 1: | |
1066 | AnalogGainToWrite = 0x20; | |
1067 | break; | |
1068 | case 2: | |
1069 | AnalogGainToWrite = 0x60; | |
1070 | break; | |
1071 | case 4: | |
1072 | AnalogGainToWrite = 0xA0; | |
1073 | break; | |
1074 | case 8: | |
1075 | AnalogGainToWrite = 0xE0; | |
1076 | break; | |
1077 | default: | |
1078 | AnalogGainToWrite = 0x20; | |
1079 | break; | |
1080 | } | |
1081 | */ | |
1082 | if (DigitalGain >= 16 || DigitalGain <= 1) | |
1083 | DigitalGain = 1; | |
1084 | /* AnalogGainToWrite = | |
1085 | (u16)((DigitalGain << 12) | AnalogGainToWrite); */ | |
1086 | AnalogGainToWrite = (u16)((DigitalGain << 12) | (u16)AnalogGain); | |
1087 | ret = mt9m114_write_reg(client, MISENSOR_16BIT, | |
1088 | REG_GAIN, AnalogGainToWrite); | |
1089 | if (ret) { | |
1090 | v4l2_err(client, "%s: fail to set AnalogGainToWrite\n", | |
1091 | __func__); | |
1092 | return -EINVAL; | |
1093 | } | |
1094 | ||
1095 | return ret; | |
1096 | } | |
1097 | ||
1098 | static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) | |
1099 | { | |
1100 | ||
1101 | switch (cmd) { | |
1102 | case ATOMISP_IOC_S_EXPOSURE: | |
1103 | return mt9m114_s_exposure(sd, arg); | |
1104 | default: | |
1105 | return -EINVAL; | |
1106 | } | |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
1111 | /* This returns the exposure time being used. This should only be used | |
1112 | for filling in EXIF data, not for actual image processing. */ | |
1113 | static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value) | |
1114 | { | |
1115 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1116 | u32 coarse; | |
1117 | int ret; | |
1118 | ||
1119 | /* the fine integration time is currently not calculated */ | |
1120 | ret = mt9m114_read_reg(client, MISENSOR_16BIT, | |
1121 | REG_EXPO_COARSE, &coarse); | |
1122 | if (ret) | |
1123 | return ret; | |
1124 | ||
1125 | *value = coarse; | |
1126 | return 0; | |
1127 | } | |
1128 | #ifndef CSS15 | |
1129 | /* | |
1130 | * This function will return the sensor supported max exposure zone number. | |
1131 | * the sensor which supports max exposure zone number is 1. | |
1132 | */ | |
1133 | static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val) | |
1134 | { | |
1135 | *val = 1; | |
1136 | ||
1137 | return 0; | |
1138 | } | |
1139 | ||
1140 | /* | |
1141 | * set exposure metering, average/center_weighted/spot/matrix. | |
1142 | */ | |
1143 | static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val) | |
1144 | { | |
1145 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1146 | int ret; | |
1147 | ||
1148 | switch (val) { | |
1149 | case V4L2_EXPOSURE_METERING_SPOT: | |
1150 | ret = mt9m114_write_reg_array(client, mt9m114_exp_average, | |
1151 | NO_POLLING); | |
1152 | if (ret) { | |
1153 | dev_err(&client->dev, "write exp_average reg err.\n"); | |
1154 | return ret; | |
1155 | } | |
1156 | break; | |
1157 | case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED: | |
1158 | default: | |
1159 | ret = mt9m114_write_reg_array(client, mt9m114_exp_center, | |
1160 | NO_POLLING); | |
1161 | if (ret) { | |
1162 | dev_err(&client->dev, "write exp_default reg err"); | |
1163 | return ret; | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | return 0; | |
1168 | } | |
1169 | ||
1170 | /* | |
1171 | * This function is for touch exposure feature. | |
1172 | */ | |
1173 | static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd, | |
1174 | struct v4l2_subdev_pad_config *cfg, | |
1175 | struct v4l2_subdev_selection *sel) | |
1176 | { | |
1177 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
a49d2536 AC |
1178 | struct misensor_reg exp_reg; |
1179 | int width, height; | |
1180 | int grid_width, grid_height; | |
1181 | int grid_left, grid_top, grid_right, grid_bottom; | |
1182 | int win_left, win_top, win_right, win_bottom; | |
1183 | int i, j; | |
1184 | int ret; | |
1185 | ||
1186 | if (sel->which != V4L2_SUBDEV_FORMAT_TRY && | |
1187 | sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
1188 | return -EINVAL; | |
1189 | ||
1190 | grid_left = sel->r.left; | |
1191 | grid_top = sel->r.top; | |
1192 | grid_right = sel->r.left + sel->r.width - 1; | |
1193 | grid_bottom = sel->r.top + sel->r.height - 1; | |
1194 | ||
8d7cd91d | 1195 | ret = mt9m114_res2size(sd, &width, &height); |
a49d2536 AC |
1196 | if (ret) |
1197 | return ret; | |
1198 | ||
1199 | grid_width = width / 5; | |
1200 | grid_height = height / 5; | |
1201 | ||
1202 | if (grid_width && grid_height) { | |
1203 | win_left = grid_left / grid_width; | |
1204 | win_top = grid_top / grid_height; | |
1205 | win_right = grid_right / grid_width; | |
1206 | win_bottom = grid_bottom / grid_height; | |
1207 | } else { | |
1208 | dev_err(&client->dev, "Incorrect exp grid.\n"); | |
1209 | return -EINVAL; | |
1210 | } | |
1211 | ||
0b56d1c8 DC |
1212 | win_left = clamp_t(int, win_left, 0, 4); |
1213 | win_top = clamp_t(int, win_top, 0, 4); | |
1214 | win_right = clamp_t(int, win_right, 0, 4); | |
1215 | win_bottom = clamp_t(int, win_bottom, 0, 4); | |
a49d2536 AC |
1216 | |
1217 | ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING); | |
1218 | if (ret) { | |
1219 | dev_err(&client->dev, "write exp_average reg err.\n"); | |
1220 | return ret; | |
1221 | } | |
1222 | ||
1223 | for (i = win_top; i <= win_bottom; i++) { | |
1224 | for (j = win_left; j <= win_right; j++) { | |
1225 | exp_reg = mt9m114_exp_win[i][j]; | |
1226 | ||
1227 | ret = mt9m114_write_reg(client, exp_reg.length, | |
1228 | exp_reg.reg, exp_reg.val); | |
1229 | if (ret) { | |
1230 | dev_err(&client->dev, "write exp_reg err.\n"); | |
1231 | return ret; | |
1232 | } | |
1233 | } | |
1234 | } | |
1235 | ||
1236 | return 0; | |
1237 | } | |
1238 | #endif | |
1239 | ||
1240 | static int mt9m114_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val) | |
1241 | { | |
1242 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
1243 | ||
1244 | *val = mt9m114_res[dev->res].bin_factor_x; | |
1245 | ||
1246 | return 0; | |
1247 | } | |
1248 | ||
1249 | static int mt9m114_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val) | |
1250 | { | |
1251 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
1252 | ||
1253 | *val = mt9m114_res[dev->res].bin_factor_y; | |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val) | |
1259 | { | |
1260 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
1261 | s32 luma = 0x37; | |
1262 | int err; | |
1263 | ||
1264 | /* EV value only support -2 to 2 | |
1265 | * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17 | |
1266 | */ | |
1267 | if (val < -2 || val > 2) | |
1268 | return -EINVAL; | |
1269 | luma += 0x10 * val; | |
1270 | dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma); | |
1271 | err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A); | |
1272 | if (err) { | |
1273 | dev_err(&c->dev, "%s logic addr access error\n", __func__); | |
1274 | return err; | |
1275 | } | |
1276 | err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma); | |
1277 | if (err) { | |
1278 | dev_err(&c->dev, "%s write target_average_luma failed\n", | |
1279 | __func__); | |
1280 | return err; | |
1281 | } | |
1282 | udelay(10); | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val) | |
1288 | { | |
1289 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
1290 | int err; | |
1291 | u32 luma; | |
1292 | ||
1293 | err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A); | |
1294 | if (err) { | |
1295 | dev_err(&c->dev, "%s logic addr access error\n", __func__); | |
1296 | return err; | |
1297 | } | |
1298 | err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma); | |
1299 | if (err) { | |
1300 | dev_err(&c->dev, "%s read target_average_luma failed\n", | |
1301 | __func__); | |
1302 | return err; | |
1303 | } | |
1304 | luma -= 0x17; | |
1305 | luma /= 0x10; | |
1306 | *val = (s32)luma - 2; | |
1307 | dev_dbg(&c->dev, "%s val:%d\n", __func__, *val); | |
1308 | ||
1309 | return 0; | |
1310 | } | |
1311 | ||
1312 | /* Fake interface | |
1313 | * mt9m114 now can not support 3a_lock | |
1314 | */ | |
1315 | static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val) | |
1316 | { | |
1317 | aaalock = val; | |
1318 | return 0; | |
1319 | } | |
1320 | ||
1321 | static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val) | |
1322 | { | |
1323 | if (aaalock) | |
1324 | return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | |
1325 | | V4L2_LOCK_FOCUS; | |
1326 | return 0; | |
1327 | } | |
1328 | ||
1329 | static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl) | |
1330 | { | |
1331 | struct mt9m114_device *dev = | |
1332 | container_of(ctrl->handler, struct mt9m114_device, ctrl_handler); | |
1333 | struct i2c_client *client = v4l2_get_subdevdata(&dev->sd); | |
1334 | int ret = 0; | |
1335 | ||
1336 | switch (ctrl->id) { | |
1337 | case V4L2_CID_VFLIP: | |
1338 | dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n", | |
1339 | __func__, ctrl->val); | |
1340 | ret = mt9m114_t_vflip(&dev->sd, ctrl->val); | |
1341 | break; | |
1342 | case V4L2_CID_HFLIP: | |
1343 | dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n", | |
1344 | __func__, ctrl->val); | |
1345 | ret = mt9m114_t_hflip(&dev->sd, ctrl->val); | |
1346 | break; | |
1347 | #ifndef CSS15 | |
1348 | case V4L2_CID_EXPOSURE_METERING: | |
1349 | ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val); | |
1350 | break; | |
1351 | #endif | |
1352 | case V4L2_CID_EXPOSURE: | |
1353 | ret = mt9m114_s_ev(&dev->sd, ctrl->val); | |
1354 | break; | |
1355 | case V4L2_CID_3A_LOCK: | |
1356 | ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val); | |
1357 | break; | |
1358 | default: | |
1359 | ret = -EINVAL; | |
1360 | } | |
1361 | return ret; | |
1362 | } | |
1363 | ||
1364 | static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl) | |
1365 | { | |
1366 | struct mt9m114_device *dev = | |
1367 | container_of(ctrl->handler, struct mt9m114_device, ctrl_handler); | |
1368 | int ret = 0; | |
1369 | ||
1370 | switch (ctrl->id) { | |
1371 | case V4L2_CID_VFLIP: | |
1372 | ret = mt9m114_g_vflip(&dev->sd, &ctrl->val); | |
1373 | break; | |
1374 | case V4L2_CID_HFLIP: | |
1375 | ret = mt9m114_g_hflip(&dev->sd, &ctrl->val); | |
1376 | break; | |
1377 | case V4L2_CID_FOCAL_ABSOLUTE: | |
1378 | ret = mt9m114_g_focal(&dev->sd, &ctrl->val); | |
1379 | break; | |
1380 | case V4L2_CID_FNUMBER_ABSOLUTE: | |
1381 | ret = mt9m114_g_fnumber(&dev->sd, &ctrl->val); | |
1382 | break; | |
1383 | case V4L2_CID_FNUMBER_RANGE: | |
1384 | ret = mt9m114_g_fnumber_range(&dev->sd, &ctrl->val); | |
1385 | break; | |
1386 | case V4L2_CID_EXPOSURE_ABSOLUTE: | |
1387 | ret = mt9m114_g_exposure(&dev->sd, &ctrl->val); | |
1388 | break; | |
1389 | #ifndef CSS15 | |
1390 | case V4L2_CID_EXPOSURE_ZONE_NUM: | |
1391 | ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val); | |
1392 | break; | |
1393 | #endif | |
1394 | case V4L2_CID_BIN_FACTOR_HORZ: | |
1395 | ret = mt9m114_g_bin_factor_x(&dev->sd, &ctrl->val); | |
1396 | break; | |
1397 | case V4L2_CID_BIN_FACTOR_VERT: | |
1398 | ret = mt9m114_g_bin_factor_y(&dev->sd, &ctrl->val); | |
1399 | break; | |
1400 | case V4L2_CID_EXPOSURE: | |
1401 | ret = mt9m114_g_ev(&dev->sd, &ctrl->val); | |
1402 | break; | |
1403 | case V4L2_CID_3A_LOCK: | |
1404 | ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val); | |
1405 | break; | |
1406 | default: | |
1407 | ret = -EINVAL; | |
1408 | } | |
1409 | ||
1410 | return ret; | |
1411 | } | |
1412 | ||
1413 | static const struct v4l2_ctrl_ops ctrl_ops = { | |
1414 | .s_ctrl = mt9m114_s_ctrl, | |
1415 | .g_volatile_ctrl = mt9m114_g_volatile_ctrl | |
1416 | }; | |
1417 | ||
1418 | static struct v4l2_ctrl_config mt9m114_controls[] = { | |
1419 | { | |
1420 | .ops = &ctrl_ops, | |
1421 | .id = V4L2_CID_VFLIP, | |
1422 | .name = "Image v-Flip", | |
1423 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1424 | .min = 0, | |
1425 | .max = 1, | |
1426 | .step = 1, | |
1427 | .def = 0, | |
1428 | }, | |
1429 | { | |
1430 | .ops = &ctrl_ops, | |
1431 | .id = V4L2_CID_HFLIP, | |
1432 | .name = "Image h-Flip", | |
1433 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1434 | .min = 0, | |
1435 | .max = 1, | |
1436 | .step = 1, | |
1437 | .def = 0, | |
1438 | }, | |
1439 | { | |
1440 | .ops = &ctrl_ops, | |
1441 | .id = V4L2_CID_FOCAL_ABSOLUTE, | |
1442 | .name = "focal length", | |
1443 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1444 | .min = MT9M114_FOCAL_LENGTH_DEFAULT, | |
1445 | .max = MT9M114_FOCAL_LENGTH_DEFAULT, | |
1446 | .step = 1, | |
1447 | .def = MT9M114_FOCAL_LENGTH_DEFAULT, | |
1448 | .flags = 0, | |
1449 | }, | |
1450 | { | |
1451 | .ops = &ctrl_ops, | |
1452 | .id = V4L2_CID_FNUMBER_ABSOLUTE, | |
1453 | .name = "f-number", | |
1454 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1455 | .min = MT9M114_F_NUMBER_DEFAULT, | |
1456 | .max = MT9M114_F_NUMBER_DEFAULT, | |
1457 | .step = 1, | |
1458 | .def = MT9M114_F_NUMBER_DEFAULT, | |
1459 | .flags = 0, | |
1460 | }, | |
1461 | { | |
1462 | .ops = &ctrl_ops, | |
1463 | .id = V4L2_CID_FNUMBER_RANGE, | |
1464 | .name = "f-number range", | |
1465 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1466 | .min = MT9M114_F_NUMBER_RANGE, | |
1467 | .max = MT9M114_F_NUMBER_RANGE, | |
1468 | .step = 1, | |
1469 | .def = MT9M114_F_NUMBER_RANGE, | |
1470 | .flags = 0, | |
1471 | }, | |
1472 | { | |
1473 | .ops = &ctrl_ops, | |
1474 | .id = V4L2_CID_EXPOSURE_ABSOLUTE, | |
1475 | .name = "exposure", | |
1476 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1477 | .min = 0, | |
1478 | .max = 0xffff, | |
1479 | .step = 1, | |
1480 | .def = 0, | |
1481 | .flags = 0, | |
1482 | }, | |
1483 | #ifndef CSS15 | |
1484 | { | |
1485 | .ops = &ctrl_ops, | |
1486 | .id = V4L2_CID_EXPOSURE_ZONE_NUM, | |
1487 | .name = "one-time exposure zone number", | |
1488 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1489 | .min = 0, | |
1490 | .max = 0xffff, | |
1491 | .step = 1, | |
1492 | .def = 0, | |
1493 | .flags = 0, | |
1494 | }, | |
1495 | { | |
1496 | .ops = &ctrl_ops, | |
1497 | .id = V4L2_CID_EXPOSURE_METERING, | |
1498 | .name = "metering", | |
1499 | .type = V4L2_CTRL_TYPE_MENU, | |
1500 | .min = 0, | |
1501 | .max = 3, | |
42c6d864 | 1502 | .step = 0, |
a49d2536 AC |
1503 | .def = 1, |
1504 | .flags = 0, | |
1505 | }, | |
1506 | #endif | |
1507 | { | |
1508 | .ops = &ctrl_ops, | |
1509 | .id = V4L2_CID_BIN_FACTOR_HORZ, | |
1510 | .name = "horizontal binning factor", | |
1511 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1512 | .min = 0, | |
1513 | .max = MT9M114_BIN_FACTOR_MAX, | |
1514 | .step = 1, | |
1515 | .def = 0, | |
1516 | .flags = 0, | |
1517 | }, | |
1518 | { | |
1519 | .ops = &ctrl_ops, | |
1520 | .id = V4L2_CID_BIN_FACTOR_VERT, | |
1521 | .name = "vertical binning factor", | |
1522 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1523 | .min = 0, | |
1524 | .max = MT9M114_BIN_FACTOR_MAX, | |
1525 | .step = 1, | |
1526 | .def = 0, | |
1527 | .flags = 0, | |
1528 | }, | |
1529 | { | |
1530 | .ops = &ctrl_ops, | |
1531 | .id = V4L2_CID_EXPOSURE, | |
1532 | .name = "exposure biasx", | |
1533 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1534 | .min = -2, | |
1535 | .max = 2, | |
1536 | .step = 1, | |
1537 | .def = 0, | |
1538 | .flags = 0, | |
1539 | }, | |
1540 | { | |
1541 | .ops = &ctrl_ops, | |
1542 | .id = V4L2_CID_3A_LOCK, | |
1543 | .name = "3a lock", | |
1544 | .type = V4L2_CTRL_TYPE_BITMASK, | |
1545 | .min = 0, | |
1546 | .max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS, | |
1547 | .step = 1, | |
1548 | .def = 0, | |
1549 | .flags = 0, | |
1550 | }, | |
1551 | }; | |
1552 | ||
1553 | static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client) | |
1554 | { | |
1555 | struct i2c_adapter *adapter = client->adapter; | |
1556 | u32 retvalue; | |
1557 | ||
1558 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { | |
1559 | dev_err(&client->dev, "%s: i2c error", __func__); | |
1560 | return -ENODEV; | |
1561 | } | |
1562 | mt9m114_read_reg(client, MISENSOR_16BIT, (u32)MT9M114_PID, &retvalue); | |
1563 | dev->real_model_id = retvalue; | |
1564 | ||
1565 | if (retvalue != MT9M114_MOD_ID) { | |
1566 | dev_err(&client->dev, "%s: failed: client->addr = %x\n", | |
1567 | __func__, client->addr); | |
1568 | return -ENODEV; | |
1569 | } | |
1570 | ||
1571 | return 0; | |
1572 | } | |
1573 | ||
1574 | static int | |
1575 | mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data) | |
1576 | { | |
1577 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
1578 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1579 | int ret; | |
1580 | ||
1581 | if (NULL == platform_data) | |
1582 | return -ENODEV; | |
1583 | ||
1584 | dev->platform_data = | |
1585 | (struct camera_sensor_platform_data *)platform_data; | |
1586 | ||
1587 | if (dev->platform_data->platform_init) { | |
1588 | ret = dev->platform_data->platform_init(client); | |
1589 | if (ret) { | |
1590 | v4l2_err(client, "mt9m114 platform init err\n"); | |
1591 | return ret; | |
1592 | } | |
1593 | } | |
1594 | ret = power_up(sd); | |
1595 | if (ret) { | |
1596 | v4l2_err(client, "mt9m114 power-up err"); | |
1597 | return ret; | |
1598 | } | |
1599 | ||
1600 | /* config & detect sensor */ | |
1601 | ret = mt9m114_detect(dev, client); | |
1602 | if (ret) { | |
1603 | v4l2_err(client, "mt9m114_detect err s_config.\n"); | |
1604 | goto fail_detect; | |
1605 | } | |
1606 | ||
1607 | ret = dev->platform_data->csi_cfg(sd, 1); | |
1608 | if (ret) | |
1609 | goto fail_csi_cfg; | |
1610 | ||
1611 | ret = mt9m114_set_suspend(sd); | |
1612 | if (ret) { | |
1613 | v4l2_err(client, "mt9m114 suspend err"); | |
1614 | return ret; | |
1615 | } | |
1616 | ||
1617 | ret = power_down(sd); | |
1618 | if (ret) { | |
1619 | v4l2_err(client, "mt9m114 power down err"); | |
1620 | return ret; | |
1621 | } | |
1622 | ||
1623 | return ret; | |
1624 | ||
1625 | fail_csi_cfg: | |
1626 | dev->platform_data->csi_cfg(sd, 0); | |
1627 | fail_detect: | |
1628 | power_down(sd); | |
1629 | dev_err(&client->dev, "sensor power-gating failed\n"); | |
1630 | return ret; | |
1631 | } | |
1632 | ||
1633 | /* Horizontal flip the image. */ | |
1634 | static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value) | |
1635 | { | |
1636 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
1637 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
1638 | int err; | |
1639 | /* set for direct mode */ | |
1640 | err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850); | |
1641 | if (value) { | |
1642 | /* enable H flip ctx A */ | |
1643 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01); | |
1644 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01); | |
1645 | /* ctx B */ | |
1646 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01); | |
1647 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01); | |
1648 | ||
1649 | err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, | |
1650 | MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN); | |
1651 | ||
1652 | dev->bpat = MT9M114_BPAT_GRGRBGBG; | |
1653 | } else { | |
1654 | /* disable H flip ctx A */ | |
1655 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00); | |
1656 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00); | |
1657 | /* ctx B */ | |
1658 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00); | |
1659 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00); | |
1660 | ||
1661 | err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, | |
1662 | MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS); | |
1663 | ||
1664 | dev->bpat = MT9M114_BPAT_BGBGGRGR; | |
1665 | } | |
1666 | ||
1667 | err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06); | |
1668 | udelay(10); | |
1669 | ||
1670 | return !!err; | |
1671 | } | |
1672 | ||
1673 | /* Vertically flip the image */ | |
1674 | static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value) | |
1675 | { | |
1676 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
1677 | int err; | |
1678 | /* set for direct mode */ | |
1679 | err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850); | |
1680 | if (value >= 1) { | |
1681 | /* enable H flip - ctx A */ | |
1682 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01); | |
1683 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01); | |
1684 | /* ctx B */ | |
1685 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01); | |
1686 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01); | |
1687 | ||
1688 | err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, | |
1689 | MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN); | |
1690 | } else { | |
1691 | /* disable H flip - ctx A */ | |
1692 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00); | |
1693 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00); | |
1694 | /* ctx B */ | |
1695 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00); | |
1696 | err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00); | |
1697 | ||
1698 | err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, | |
1699 | MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS); | |
1700 | } | |
1701 | ||
1702 | err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06); | |
1703 | udelay(10); | |
1704 | ||
1705 | return !!err; | |
1706 | } | |
1707 | static int mt9m114_s_parm(struct v4l2_subdev *sd, | |
1708 | struct v4l2_streamparm *param) | |
1709 | { | |
1710 | return 0; | |
1711 | } | |
1712 | ||
1713 | static int mt9m114_g_frame_interval(struct v4l2_subdev *sd, | |
1714 | struct v4l2_subdev_frame_interval *interval) | |
1715 | { | |
1716 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
1717 | ||
1718 | interval->interval.numerator = 1; | |
1719 | interval->interval.denominator = mt9m114_res[dev->res].fps; | |
1720 | ||
1721 | return 0; | |
1722 | } | |
1723 | ||
1724 | static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable) | |
1725 | { | |
1726 | int ret; | |
1727 | struct i2c_client *c = v4l2_get_subdevdata(sd); | |
1728 | struct mt9m114_device *dev = to_mt9m114_sensor(sd); | |
1729 | struct atomisp_exposure exposure; | |
1730 | ||
1731 | if (enable) { | |
1732 | ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, | |
1733 | POST_POLLING); | |
1734 | if (ret < 0) | |
1735 | return ret; | |
1736 | ||
1737 | if (dev->first_exp > MT9M114_MAX_FIRST_EXP) { | |
1738 | exposure.integration_time[0] = dev->first_exp; | |
1739 | exposure.gain[0] = dev->first_gain; | |
1740 | exposure.gain[1] = dev->first_diggain; | |
1741 | mt9m114_s_exposure(sd, &exposure); | |
1742 | } | |
1743 | dev->streamon = 1; | |
1744 | ||
1745 | } else { | |
1746 | dev->streamon = 0; | |
1747 | ret = mt9m114_set_suspend(sd); | |
1748 | } | |
1749 | ||
1750 | return ret; | |
1751 | } | |
1752 | ||
1753 | static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd, | |
1754 | struct v4l2_subdev_pad_config *cfg, | |
1755 | struct v4l2_subdev_mbus_code_enum *code) | |
1756 | { | |
1757 | if (code->index) | |
1758 | return -EINVAL; | |
1759 | code->code = MEDIA_BUS_FMT_SGRBG10_1X10; | |
1760 | ||
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | static int mt9m114_enum_frame_size(struct v4l2_subdev *sd, | |
1765 | struct v4l2_subdev_pad_config *cfg, | |
1766 | struct v4l2_subdev_frame_size_enum *fse) | |
1767 | { | |
1768 | ||
1769 | unsigned int index = fse->index; | |
1770 | ||
1771 | if (index >= N_RES) | |
1772 | return -EINVAL; | |
1773 | ||
1774 | fse->min_width = mt9m114_res[index].width; | |
1775 | fse->min_height = mt9m114_res[index].height; | |
1776 | fse->max_width = mt9m114_res[index].width; | |
1777 | fse->max_height = mt9m114_res[index].height; | |
1778 | ||
1779 | return 0; | |
1780 | } | |
1781 | ||
1782 | static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) | |
1783 | { | |
1784 | int index; | |
1785 | struct mt9m114_device *snr = to_mt9m114_sensor(sd); | |
1786 | ||
1787 | if (frames == NULL) | |
1788 | return -EINVAL; | |
1789 | ||
1790 | for (index = 0; index < N_RES; index++) { | |
1791 | if (mt9m114_res[index].res == snr->res) | |
1792 | break; | |
1793 | } | |
1794 | ||
1795 | if (index >= N_RES) | |
1796 | return -EINVAL; | |
1797 | ||
1798 | *frames = mt9m114_res[index].skip_frames; | |
1799 | ||
1800 | return 0; | |
1801 | } | |
1802 | ||
1803 | static const struct v4l2_subdev_video_ops mt9m114_video_ops = { | |
1804 | .s_parm = mt9m114_s_parm, | |
1805 | .s_stream = mt9m114_s_stream, | |
1806 | .g_frame_interval = mt9m114_g_frame_interval, | |
1807 | }; | |
1808 | ||
65058214 | 1809 | static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = { |
a49d2536 AC |
1810 | .g_skip_frames = mt9m114_g_skip_frames, |
1811 | }; | |
1812 | ||
1813 | static const struct v4l2_subdev_core_ops mt9m114_core_ops = { | |
1814 | .s_power = mt9m114_s_power, | |
1815 | .ioctl = mt9m114_ioctl, | |
1816 | }; | |
1817 | ||
1818 | /* REVISIT: Do we need pad operations? */ | |
1819 | static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = { | |
1820 | .enum_mbus_code = mt9m114_enum_mbus_code, | |
1821 | .enum_frame_size = mt9m114_enum_frame_size, | |
1822 | .get_fmt = mt9m114_get_fmt, | |
1823 | .set_fmt = mt9m114_set_fmt, | |
1824 | #ifndef CSS15 | |
1825 | .set_selection = mt9m114_s_exposure_selection, | |
1826 | #endif | |
1827 | }; | |
1828 | ||
1829 | static const struct v4l2_subdev_ops mt9m114_ops = { | |
1830 | .core = &mt9m114_core_ops, | |
1831 | .video = &mt9m114_video_ops, | |
1832 | .pad = &mt9m114_pad_ops, | |
1833 | .sensor = &mt9m114_sensor_ops, | |
1834 | }; | |
1835 | ||
1836 | static const struct media_entity_operations mt9m114_entity_ops = { | |
1837 | .link_setup = NULL, | |
1838 | }; | |
1839 | ||
1840 | static int mt9m114_remove(struct i2c_client *client) | |
1841 | { | |
1842 | struct mt9m114_device *dev; | |
1843 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
1844 | ||
1845 | dev = container_of(sd, struct mt9m114_device, sd); | |
1846 | dev->platform_data->csi_cfg(sd, 0); | |
1847 | if (dev->platform_data->platform_deinit) | |
1848 | dev->platform_data->platform_deinit(); | |
1849 | v4l2_device_unregister_subdev(sd); | |
1850 | media_entity_cleanup(&dev->sd.entity); | |
1851 | v4l2_ctrl_handler_free(&dev->ctrl_handler); | |
1852 | kfree(dev); | |
1853 | return 0; | |
1854 | } | |
1855 | ||
1856 | static int mt9m114_probe(struct i2c_client *client, | |
1857 | const struct i2c_device_id *id) | |
1858 | { | |
1859 | struct mt9m114_device *dev; | |
1860 | int ret = 0; | |
1861 | unsigned int i; | |
1862 | void *pdata; | |
1863 | ||
1864 | /* Setup sensor configuration structure */ | |
1865 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
309167b9 | 1866 | if (!dev) |
a49d2536 | 1867 | return -ENOMEM; |
a49d2536 AC |
1868 | |
1869 | v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops); | |
1870 | pdata = client->dev.platform_data; | |
1871 | if (ACPI_COMPANION(&client->dev)) | |
1872 | pdata = gmin_camera_platform_data(&dev->sd, | |
1873 | ATOMISP_INPUT_FORMAT_RAW_10, | |
1874 | atomisp_bayer_order_grbg); | |
1875 | if (pdata) | |
1876 | ret = mt9m114_s_config(&dev->sd, client->irq, pdata); | |
1877 | if (!pdata || ret) { | |
1878 | v4l2_device_unregister_subdev(&dev->sd); | |
1879 | kfree(dev); | |
1880 | return ret; | |
1881 | } | |
1882 | ||
1883 | ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA); | |
1884 | if (ret) { | |
1885 | v4l2_device_unregister_subdev(&dev->sd); | |
1886 | kfree(dev); | |
1887 | /* Coverity CID 298095 - return on error */ | |
1888 | return ret; | |
1889 | } | |
1890 | ||
1891 | /*TODO add format code here*/ | |
1892 | dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; | |
1893 | dev->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1894 | dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10; | |
1895 | dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; | |
1896 | ||
1897 | ret = | |
1898 | v4l2_ctrl_handler_init(&dev->ctrl_handler, | |
1899 | ARRAY_SIZE(mt9m114_controls)); | |
1900 | if (ret) { | |
1901 | mt9m114_remove(client); | |
1902 | return ret; | |
1903 | } | |
1904 | ||
1905 | for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++) | |
1906 | v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i], | |
1907 | NULL); | |
1908 | ||
1909 | if (dev->ctrl_handler.error) { | |
1910 | mt9m114_remove(client); | |
1911 | return dev->ctrl_handler.error; | |
1912 | } | |
1913 | ||
1914 | /* Use same lock for controls as for everything else. */ | |
1915 | dev->ctrl_handler.lock = &dev->input_lock; | |
1916 | dev->sd.ctrl_handler = &dev->ctrl_handler; | |
1917 | ||
1918 | /* REVISIT: Do we need media controller? */ | |
1919 | ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); | |
1920 | if (ret) { | |
1921 | mt9m114_remove(client); | |
1922 | return ret; | |
1923 | } | |
1924 | return 0; | |
1925 | } | |
1926 | ||
1927 | MODULE_DEVICE_TABLE(i2c, mt9m114_id); | |
1928 | ||
3f9ae4b9 | 1929 | static const struct acpi_device_id mt9m114_acpi_match[] = { |
a49d2536 AC |
1930 | { "INT33F0" }, |
1931 | { "CRMT1040" }, | |
1932 | {}, | |
1933 | }; | |
1934 | ||
1935 | MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match); | |
1936 | ||
1937 | static struct i2c_driver mt9m114_driver = { | |
1938 | .driver = { | |
a49d2536 AC |
1939 | .name = "mt9m114", |
1940 | .acpi_match_table = ACPI_PTR(mt9m114_acpi_match), | |
1941 | }, | |
1942 | .probe = mt9m114_probe, | |
1943 | .remove = mt9m114_remove, | |
1944 | .id_table = mt9m114_id, | |
1945 | }; | |
2cb63c4c | 1946 | module_i2c_driver(mt9m114_driver); |
a49d2536 AC |
1947 | |
1948 | MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>"); | |
1949 | MODULE_LICENSE("GPL"); |