]>
Commit | Line | Data |
---|---|---|
a49d2536 AC |
1 | /* |
2 | * Support for Sony imx 8MP camera sensor. | |
3 | * | |
4 | * Copyright (c) 2012 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 | */ | |
25016567 | 21 | #include "../../include/linux/atomisp_platform.h" |
a49d2536 AC |
22 | #include <linux/bitops.h> |
23 | #include <linux/device.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/fs.h> | |
27 | #include <linux/gpio.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/i2c.h> | |
30 | #include <linux/io.h> | |
31 | #include <linux/kernel.h> | |
25016567 | 32 | #include "../../include/linux/libmsrlisthelper.h" |
a49d2536 AC |
33 | #include <linux/mm.h> |
34 | #include <linux/kmod.h> | |
35 | #include <linux/module.h> | |
36 | #include <linux/moduleparam.h> | |
37 | #include <linux/string.h> | |
38 | #include <linux/slab.h> | |
39 | #include <linux/types.h> | |
40 | #include <media/v4l2-ctrls.h> | |
41 | #include <media/v4l2-device.h> | |
42 | #include "imx.h" | |
43 | ||
44 | /* | |
45 | * The imx135 embedded data info: | |
46 | * embedded data line num: 2 | |
47 | * line 0 effective data size(byte): 76 | |
48 | * line 1 effective data size(byte): 113 | |
49 | */ | |
50 | static const uint32_t | |
51 | imx135_embedded_effective_size[IMX135_EMBEDDED_DATA_LINE_NUM] | |
52 | = {76, 113}; | |
53 | ||
54 | static enum atomisp_bayer_order imx_bayer_order_mapping[] = { | |
55 | atomisp_bayer_order_rggb, | |
56 | atomisp_bayer_order_grbg, | |
57 | atomisp_bayer_order_gbrg, | |
58 | atomisp_bayer_order_bggr | |
59 | }; | |
60 | ||
61 | static const unsigned int | |
62 | IMX227_BRACKETING_LUT_FRAME_ENTRY[IMX_MAX_AE_LUT_LENGTH] = { | |
63 | 0x0E10, 0x0E1E, 0x0E2C, 0x0E3A, 0x0E48}; | |
64 | ||
65 | static int | |
66 | imx_read_reg(struct i2c_client *client, u16 len, u16 reg, u16 *val) | |
67 | { | |
68 | struct i2c_msg msg[2]; | |
69 | u16 data[IMX_SHORT_MAX]; | |
70 | int ret, i; | |
71 | int retry = 0; | |
72 | ||
73 | if (len > IMX_BYTE_MAX) { | |
74 | dev_err(&client->dev, "%s error, invalid data length\n", | |
75 | __func__); | |
76 | return -EINVAL; | |
77 | } | |
78 | ||
79 | do { | |
80 | memset(msg, 0 , sizeof(msg)); | |
81 | memset(data, 0 , sizeof(data)); | |
82 | ||
83 | msg[0].addr = client->addr; | |
84 | msg[0].flags = 0; | |
85 | msg[0].len = I2C_MSG_LENGTH; | |
86 | msg[0].buf = (u8 *)data; | |
87 | /* high byte goes first */ | |
88 | data[0] = cpu_to_be16(reg); | |
89 | ||
90 | msg[1].addr = client->addr; | |
91 | msg[1].len = len; | |
92 | msg[1].flags = I2C_M_RD; | |
93 | msg[1].buf = (u8 *)data; | |
94 | ||
95 | ret = i2c_transfer(client->adapter, msg, 2); | |
96 | if (ret != 2) { | |
97 | dev_err(&client->dev, | |
98 | "retrying i2c read from offset 0x%x error %d... %d\n", | |
99 | reg, ret, retry); | |
100 | msleep(20); | |
101 | } | |
102 | } while (ret != 2 && retry++ < I2C_RETRY_COUNT); | |
103 | ||
104 | if (ret != 2) | |
105 | return -EIO; | |
106 | ||
107 | /* high byte comes first */ | |
108 | if (len == IMX_8BIT) { | |
109 | *val = (u8)data[0]; | |
110 | } else { | |
111 | /* 16-bit access is default when len > 1 */ | |
112 | for (i = 0; i < (len >> 1); i++) | |
113 | val[i] = be16_to_cpu(data[i]); | |
114 | } | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | static int imx_i2c_write(struct i2c_client *client, u16 len, u8 *data) | |
120 | { | |
121 | struct i2c_msg msg; | |
122 | int ret; | |
123 | int retry = 0; | |
124 | ||
125 | do { | |
126 | msg.addr = client->addr; | |
127 | msg.flags = 0; | |
128 | msg.len = len; | |
129 | msg.buf = data; | |
130 | ||
131 | ret = i2c_transfer(client->adapter, &msg, 1); | |
132 | if (ret != 1) { | |
133 | dev_err(&client->dev, | |
134 | "retrying i2c write transfer... %d\n", retry); | |
135 | msleep(20); | |
136 | } | |
137 | } while (ret != 1 && retry++ < I2C_RETRY_COUNT); | |
138 | ||
139 | return ret == 1 ? 0 : -EIO; | |
140 | } | |
141 | ||
142 | int | |
143 | imx_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u16 val) | |
144 | { | |
145 | int ret; | |
146 | unsigned char data[4] = {0}; | |
147 | u16 *wreg = (u16 *)data; | |
148 | const u16 len = data_length + sizeof(u16); /* 16-bit address + data */ | |
149 | ||
150 | if (data_length != IMX_8BIT && data_length != IMX_16BIT) { | |
151 | v4l2_err(client, "%s error, invalid data_length\n", __func__); | |
152 | return -EINVAL; | |
153 | } | |
154 | ||
155 | /* high byte goes out first */ | |
156 | *wreg = cpu_to_be16(reg); | |
157 | ||
158 | if (data_length == IMX_8BIT) | |
159 | data[2] = (u8)(val); | |
160 | else { | |
161 | /* IMX_16BIT */ | |
162 | u16 *wdata = (u16 *)&data[2]; | |
163 | *wdata = cpu_to_be16(val); | |
164 | } | |
165 | ||
166 | ret = imx_i2c_write(client, len, data); | |
167 | if (ret) | |
168 | dev_err(&client->dev, | |
169 | "write error: wrote 0x%x to offset 0x%x error %d", | |
170 | val, reg, ret); | |
171 | ||
172 | return ret; | |
173 | } | |
174 | ||
175 | /* | |
176 | * imx_write_reg_array - Initializes a list of imx registers | |
177 | * @client: i2c driver client structure | |
178 | * @reglist: list of registers to be written | |
179 | * | |
180 | * This function initializes a list of registers. When consecutive addresses | |
181 | * are found in a row on the list, this function creates a buffer and sends | |
182 | * consecutive data in a single i2c_transfer(). | |
183 | * | |
184 | * __imx_flush_reg_array, __imx_buf_reg_array() and | |
185 | * __imx_write_reg_is_consecutive() are internal functions to | |
186 | * imx_write_reg_array_fast() and should be not used anywhere else. | |
187 | * | |
188 | */ | |
189 | ||
190 | static int __imx_flush_reg_array(struct i2c_client *client, | |
191 | struct imx_write_ctrl *ctrl) | |
192 | { | |
193 | u16 size; | |
194 | ||
195 | if (ctrl->index == 0) | |
196 | return 0; | |
197 | ||
198 | size = sizeof(u16) + ctrl->index; /* 16-bit address + data */ | |
199 | ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr); | |
200 | ctrl->index = 0; | |
201 | ||
202 | return imx_i2c_write(client, size, (u8 *)&ctrl->buffer); | |
203 | } | |
204 | ||
205 | static int __imx_buf_reg_array(struct i2c_client *client, | |
206 | struct imx_write_ctrl *ctrl, | |
207 | const struct imx_reg *next) | |
208 | { | |
209 | int size; | |
210 | u16 *data16; | |
211 | ||
212 | switch (next->type) { | |
213 | case IMX_8BIT: | |
214 | size = 1; | |
215 | ctrl->buffer.data[ctrl->index] = (u8)next->val; | |
216 | break; | |
217 | case IMX_16BIT: | |
218 | size = 2; | |
219 | data16 = (u16 *)&ctrl->buffer.data[ctrl->index]; | |
220 | *data16 = cpu_to_be16((u16)next->val); | |
221 | break; | |
222 | default: | |
223 | return -EINVAL; | |
224 | } | |
225 | ||
226 | /* When first item is added, we need to store its starting address */ | |
227 | if (ctrl->index == 0) | |
228 | ctrl->buffer.addr = next->sreg; | |
229 | ||
230 | ctrl->index += size; | |
231 | ||
232 | /* | |
233 | * Buffer cannot guarantee free space for u32? Better flush it to avoid | |
234 | * possible lack of memory for next item. | |
235 | */ | |
236 | if (ctrl->index + sizeof(u16) >= IMX_MAX_WRITE_BUF_SIZE) | |
237 | return __imx_flush_reg_array(client, ctrl); | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | static int | |
243 | __imx_write_reg_is_consecutive(struct i2c_client *client, | |
244 | struct imx_write_ctrl *ctrl, | |
245 | const struct imx_reg *next) | |
246 | { | |
247 | if (ctrl->index == 0) | |
248 | return 1; | |
249 | ||
250 | return ctrl->buffer.addr + ctrl->index == next->sreg; | |
251 | } | |
252 | ||
253 | static int imx_write_reg_array(struct i2c_client *client, | |
254 | const struct imx_reg *reglist) | |
255 | { | |
256 | const struct imx_reg *next = reglist; | |
257 | struct imx_write_ctrl ctrl; | |
258 | int err; | |
259 | ||
260 | ctrl.index = 0; | |
261 | for (; next->type != IMX_TOK_TERM; next++) { | |
262 | switch (next->type & IMX_TOK_MASK) { | |
263 | case IMX_TOK_DELAY: | |
264 | err = __imx_flush_reg_array(client, &ctrl); | |
265 | if (err) | |
266 | return err; | |
267 | msleep(next->val); | |
268 | break; | |
269 | ||
270 | default: | |
271 | /* | |
272 | * If next address is not consecutive, data needs to be | |
273 | * flushed before proceed. | |
274 | */ | |
275 | if (!__imx_write_reg_is_consecutive(client, &ctrl, | |
276 | next)) { | |
277 | err = __imx_flush_reg_array(client, &ctrl); | |
278 | if (err) | |
279 | return err; | |
280 | } | |
281 | err = __imx_buf_reg_array(client, &ctrl, next); | |
282 | if (err) { | |
283 | v4l2_err(client, "%s: write error, aborted\n", | |
284 | __func__); | |
285 | return err; | |
286 | } | |
287 | break; | |
288 | } | |
289 | } | |
290 | ||
291 | return __imx_flush_reg_array(client, &ctrl); | |
292 | } | |
293 | ||
294 | static int __imx_min_fps_diff(int fps, const struct imx_fps_setting *fps_list) | |
295 | { | |
296 | int diff = INT_MAX; | |
297 | int i; | |
298 | ||
299 | if (fps == 0) | |
300 | return 0; | |
301 | ||
302 | for (i = 0; i < MAX_FPS_OPTIONS_SUPPORTED; i++) { | |
303 | if (!fps_list[i].fps) | |
304 | break; | |
305 | if (abs(fps_list[i].fps - fps) < diff) | |
306 | diff = abs(fps_list[i].fps - fps); | |
307 | } | |
308 | ||
309 | return diff; | |
310 | } | |
311 | ||
312 | static int __imx_nearest_fps_index(int fps, | |
313 | const struct imx_fps_setting *fps_list) | |
314 | { | |
315 | int fps_index = 0; | |
316 | int i; | |
317 | ||
318 | for (i = 0; i < MAX_FPS_OPTIONS_SUPPORTED; i++) { | |
319 | if (!fps_list[i].fps) | |
320 | break; | |
321 | if (abs(fps_list[i].fps - fps) | |
322 | < abs(fps_list[fps_index].fps - fps)) | |
323 | fps_index = i; | |
324 | } | |
325 | return fps_index; | |
326 | } | |
327 | ||
328 | /* | |
329 | * This is to choose the nearest fps setting above the requested fps | |
330 | * fps_list should be in ascendant order. | |
331 | */ | |
332 | static int __imx_above_nearest_fps_index(int fps, | |
333 | const struct imx_fps_setting *fps_list) | |
334 | { | |
335 | int fps_index = 0; | |
336 | int i; | |
337 | ||
338 | for (i = 0; i < MAX_FPS_OPTIONS_SUPPORTED; i++) { | |
339 | if (!fps_list[i].fps) | |
340 | break; | |
341 | if (fps <= fps_list[i].fps) { | |
342 | fps_index = i; | |
343 | break; | |
344 | } | |
345 | } | |
346 | ||
347 | return fps_index; | |
348 | } | |
349 | ||
350 | static int imx_get_lanes(struct v4l2_subdev *sd) | |
351 | { | |
352 | struct camera_mipi_info *imx_info = v4l2_get_subdev_hostdata(sd); | |
353 | ||
354 | if (!imx_info) | |
355 | return -ENOSYS; | |
356 | if (imx_info->num_lanes < 1 || imx_info->num_lanes > 4 || | |
357 | imx_info->num_lanes == 3) | |
358 | return -EINVAL; | |
359 | ||
360 | return imx_info->num_lanes; | |
361 | } | |
362 | ||
363 | static int __imx_update_exposure_timing(struct i2c_client *client, u16 exposure, | |
364 | u16 llp, u16 fll) | |
365 | { | |
366 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
367 | struct imx_device *dev = to_imx_sensor(sd); | |
368 | int ret = 0; | |
369 | ||
370 | if (dev->sensor_id != IMX227_ID) { | |
371 | /* Increase the VTS to match exposure + margin */ | |
372 | if (exposure > fll - IMX_INTEGRATION_TIME_MARGIN) | |
373 | fll = exposure + IMX_INTEGRATION_TIME_MARGIN; | |
374 | } | |
375 | ||
376 | ret = imx_write_reg(client, IMX_16BIT, | |
377 | dev->reg_addr->line_length_pixels, llp); | |
378 | if (ret) | |
379 | return ret; | |
380 | ||
381 | ret = imx_write_reg(client, IMX_16BIT, | |
382 | dev->reg_addr->frame_length_lines, fll); | |
383 | if (ret) | |
384 | return ret; | |
385 | ||
386 | if (exposure) | |
387 | ret = imx_write_reg(client, IMX_16BIT, | |
388 | dev->reg_addr->coarse_integration_time, exposure); | |
389 | ||
390 | return ret; | |
391 | } | |
392 | ||
393 | static int __imx_update_gain(struct v4l2_subdev *sd, u16 gain) | |
394 | { | |
395 | struct imx_device *dev = to_imx_sensor(sd); | |
396 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
397 | int ret; | |
398 | ||
399 | /* set global gain */ | |
400 | ret = imx_write_reg(client, IMX_8BIT, dev->reg_addr->global_gain, gain); | |
401 | if (ret) | |
402 | return ret; | |
403 | ||
404 | /* set short analog gain */ | |
405 | if (dev->sensor_id == IMX135_ID) | |
406 | ret = imx_write_reg(client, IMX_8BIT, IMX_SHORT_AGC_GAIN, gain); | |
407 | ||
408 | return ret; | |
409 | } | |
410 | ||
411 | static int __imx_update_digital_gain(struct i2c_client *client, u16 digitgain) | |
412 | { | |
413 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
414 | struct imx_device *dev = to_imx_sensor(sd); | |
415 | struct imx_write_buffer digit_gain; | |
416 | ||
417 | digit_gain.addr = cpu_to_be16(dev->reg_addr->dgc_adj); | |
418 | digit_gain.data[0] = (digitgain >> 8) & 0xFF; | |
419 | digit_gain.data[1] = digitgain & 0xFF; | |
420 | ||
421 | if (dev->sensor_id == IMX219_ID) { | |
422 | return imx_i2c_write(client, IMX219_DGC_LEN, (u8 *)&digit_gain); | |
423 | } else if (dev->sensor_id == IMX227_ID) { | |
424 | return imx_i2c_write(client, IMX227_DGC_LEN, (u8 *)&digit_gain); | |
425 | } else { | |
426 | digit_gain.data[2] = (digitgain >> 8) & 0xFF; | |
427 | digit_gain.data[3] = digitgain & 0xFF; | |
428 | digit_gain.data[4] = (digitgain >> 8) & 0xFF; | |
429 | digit_gain.data[5] = digitgain & 0xFF; | |
430 | digit_gain.data[6] = (digitgain >> 8) & 0xFF; | |
431 | digit_gain.data[7] = digitgain & 0xFF; | |
432 | return imx_i2c_write(client, IMX_DGC_LEN, (u8 *)&digit_gain); | |
433 | } | |
434 | return 0; | |
435 | } | |
436 | ||
437 | static int imx_set_exposure_gain(struct v4l2_subdev *sd, u16 coarse_itg, | |
438 | u16 gain, u16 digitgain) | |
439 | { | |
440 | struct imx_device *dev = to_imx_sensor(sd); | |
441 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
442 | int lanes = imx_get_lanes(sd); | |
443 | unsigned int digitgain_scaled; | |
444 | int ret = 0; | |
445 | ||
446 | /* Validate exposure: cannot exceed VTS-4 where VTS is 16bit */ | |
447 | coarse_itg = clamp_t(u16, coarse_itg, 0, IMX_MAX_EXPOSURE_SUPPORTED); | |
448 | ||
449 | /* Validate gain: must not exceed maximum 8bit value */ | |
450 | gain = clamp_t(u16, gain, 0, IMX_MAX_GLOBAL_GAIN_SUPPORTED); | |
451 | ||
452 | mutex_lock(&dev->input_lock); | |
453 | ||
454 | if (dev->sensor_id == IMX227_ID) { | |
455 | ret = imx_write_reg_array(client, imx_param_hold); | |
9bc30b3a AC |
456 | if (ret) { |
457 | mutex_unlock(&dev->input_lock); | |
a49d2536 | 458 | return ret; |
9bc30b3a | 459 | } |
a49d2536 AC |
460 | } |
461 | ||
462 | /* For imx175, setting gain must be delayed by one */ | |
463 | if ((dev->sensor_id == IMX175_ID) && dev->digital_gain) | |
464 | digitgain_scaled = dev->digital_gain; | |
465 | else | |
466 | digitgain_scaled = digitgain; | |
467 | /* imx132 with two lanes needs more gain to saturate at max */ | |
468 | if (dev->sensor_id == IMX132_ID && lanes > 1) { | |
469 | digitgain_scaled *= IMX132_2LANES_GAINFACT; | |
470 | digitgain_scaled >>= IMX132_2LANES_GAINFACT_SHIFT; | |
471 | } | |
472 | /* Validate digital gain: must not exceed 12 bit value*/ | |
473 | digitgain_scaled = clamp_t(unsigned int, digitgain_scaled, | |
474 | 0, IMX_MAX_DIGITAL_GAIN_SUPPORTED); | |
475 | ||
476 | ret = __imx_update_exposure_timing(client, coarse_itg, | |
477 | dev->pixels_per_line, dev->lines_per_frame); | |
478 | if (ret) | |
479 | goto out; | |
480 | dev->coarse_itg = coarse_itg; | |
481 | ||
482 | if (dev->sensor_id == IMX175_ID) | |
483 | ret = __imx_update_gain(sd, dev->gain); | |
484 | else | |
485 | ret = __imx_update_gain(sd, gain); | |
486 | if (ret) | |
487 | goto out; | |
488 | dev->gain = gain; | |
489 | ||
490 | ret = __imx_update_digital_gain(client, digitgain_scaled); | |
491 | if (ret) | |
492 | goto out; | |
493 | dev->digital_gain = digitgain; | |
494 | ||
495 | out: | |
496 | if (dev->sensor_id == IMX227_ID) | |
497 | ret = imx_write_reg_array(client, imx_param_update); | |
498 | mutex_unlock(&dev->input_lock); | |
499 | return ret; | |
500 | } | |
501 | ||
502 | static long imx_s_exposure(struct v4l2_subdev *sd, | |
503 | struct atomisp_exposure *exposure) | |
504 | { | |
505 | return imx_set_exposure_gain(sd, exposure->integration_time[0], | |
506 | exposure->gain[0], exposure->gain[1]); | |
507 | } | |
508 | ||
509 | /* FIXME -To be updated with real OTP reading */ | |
510 | static int imx_g_priv_int_data(struct v4l2_subdev *sd, | |
511 | struct v4l2_private_int_data *priv) | |
512 | { | |
513 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
514 | struct imx_device *dev = to_imx_sensor(sd); | |
515 | u8 __user *to = priv->data; | |
516 | u32 read_size = priv->size; | |
517 | int ret; | |
518 | ||
519 | /* No need to copy data if size is 0 */ | |
520 | if (!read_size) | |
521 | goto out; | |
522 | ||
523 | if (IS_ERR(dev->otp_data)) { | |
524 | dev_err(&client->dev, "OTP data not available"); | |
525 | return PTR_ERR(dev->otp_data); | |
526 | } | |
527 | /* Correct read_size value only if bigger than maximum */ | |
528 | if (read_size > dev->otp_driver->size) | |
529 | read_size = dev->otp_driver->size; | |
530 | ||
531 | ret = copy_to_user(to, dev->otp_data, read_size); | |
532 | if (ret) { | |
533 | dev_err(&client->dev, "%s: failed to copy OTP data to user\n", | |
534 | __func__); | |
535 | return -EFAULT; | |
536 | } | |
537 | out: | |
538 | /* Return correct size */ | |
539 | priv->size = dev->otp_driver->size; | |
540 | ||
541 | return 0; | |
542 | } | |
543 | ||
544 | static int __imx_init(struct v4l2_subdev *sd, u32 val) | |
545 | { | |
546 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
547 | struct imx_device *dev = to_imx_sensor(sd); | |
548 | int lanes = imx_get_lanes(sd); | |
549 | int ret; | |
550 | ||
551 | if (dev->sensor_id == IMX_ID_DEFAULT) | |
552 | return 0; | |
553 | ||
554 | /* The default is no flip at sensor initialization */ | |
555 | dev->h_flip->cur.val = 0; | |
556 | dev->v_flip->cur.val = 0; | |
557 | /* Sets the default FPS */ | |
558 | dev->fps_index = 0; | |
559 | dev->curr_res_table = dev->mode_tables->res_preview; | |
560 | dev->entries_curr_table = dev->mode_tables->n_res_preview; | |
561 | ||
562 | ret = imx_write_reg_array(client, dev->mode_tables->init_settings); | |
563 | if (ret) | |
564 | return ret; | |
565 | ||
566 | if (dev->sensor_id == IMX132_ID && lanes > 0) { | |
567 | static const u8 imx132_rglanesel[] = { | |
568 | IMX132_RGLANESEL_1LANE, /* 1 lane */ | |
569 | IMX132_RGLANESEL_2LANES, /* 2 lanes */ | |
570 | IMX132_RGLANESEL_1LANE, /* undefined */ | |
571 | IMX132_RGLANESEL_4LANES, /* 4 lanes */ | |
572 | }; | |
573 | ret = imx_write_reg(client, IMX_8BIT, | |
574 | IMX132_RGLANESEL, imx132_rglanesel[lanes - 1]); | |
575 | } | |
576 | ||
577 | return ret; | |
578 | } | |
579 | ||
580 | static int imx_init(struct v4l2_subdev *sd, u32 val) | |
581 | { | |
582 | struct imx_device *dev = to_imx_sensor(sd); | |
583 | int ret = 0; | |
584 | ||
585 | mutex_lock(&dev->input_lock); | |
586 | ret = __imx_init(sd, val); | |
587 | mutex_unlock(&dev->input_lock); | |
588 | ||
589 | return ret; | |
590 | } | |
591 | ||
592 | static long imx_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) | |
593 | { | |
594 | ||
595 | switch (cmd) { | |
596 | case ATOMISP_IOC_S_EXPOSURE: | |
597 | return imx_s_exposure(sd, arg); | |
598 | case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA: | |
599 | return imx_g_priv_int_data(sd, arg); | |
600 | default: | |
601 | return -EINVAL; | |
602 | } | |
603 | return 0; | |
604 | } | |
605 | ||
606 | static int power_up(struct v4l2_subdev *sd) | |
607 | { | |
608 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
609 | struct imx_device *dev = to_imx_sensor(sd); | |
610 | int ret; | |
611 | ||
612 | /* power control */ | |
613 | ret = dev->platform_data->power_ctrl(sd, 1); | |
614 | if (ret) | |
615 | goto fail_power; | |
616 | ||
617 | /* flis clock control */ | |
618 | ret = dev->platform_data->flisclk_ctrl(sd, 1); | |
619 | if (ret) | |
620 | goto fail_clk; | |
621 | ||
622 | /* gpio ctrl */ | |
623 | ret = dev->platform_data->gpio_ctrl(sd, 1); | |
624 | if (ret) { | |
625 | dev_err(&client->dev, "gpio failed\n"); | |
626 | goto fail_gpio; | |
627 | } | |
628 | ||
629 | return 0; | |
630 | fail_gpio: | |
631 | dev->platform_data->gpio_ctrl(sd, 0); | |
632 | fail_clk: | |
633 | dev->platform_data->flisclk_ctrl(sd, 0); | |
634 | fail_power: | |
635 | dev->platform_data->power_ctrl(sd, 0); | |
636 | dev_err(&client->dev, "sensor power-up failed\n"); | |
637 | ||
638 | return ret; | |
639 | } | |
640 | ||
641 | static int power_down(struct v4l2_subdev *sd) | |
642 | { | |
643 | struct imx_device *dev = to_imx_sensor(sd); | |
644 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
645 | int ret; | |
646 | ||
647 | ret = dev->platform_data->flisclk_ctrl(sd, 0); | |
648 | if (ret) | |
649 | dev_err(&client->dev, "flisclk failed\n"); | |
650 | ||
651 | /* gpio ctrl */ | |
652 | ret = dev->platform_data->gpio_ctrl(sd, 0); | |
653 | if (ret) | |
654 | dev_err(&client->dev, "gpio failed\n"); | |
655 | ||
656 | /* power control */ | |
657 | ret = dev->platform_data->power_ctrl(sd, 0); | |
658 | if (ret) | |
659 | dev_err(&client->dev, "vprog failed.\n"); | |
660 | ||
661 | return ret; | |
662 | } | |
663 | ||
664 | static int __imx_s_power(struct v4l2_subdev *sd, int on) | |
665 | { | |
666 | struct imx_device *dev = to_imx_sensor(sd); | |
667 | int ret = 0; | |
668 | int r = 0; | |
669 | ||
670 | if (on == 0) { | |
671 | ret = power_down(sd); | |
672 | if (dev->vcm_driver && dev->vcm_driver->power_down) | |
673 | r = dev->vcm_driver->power_down(sd); | |
674 | if (ret == 0) | |
675 | ret = r; | |
676 | dev->power = 0; | |
677 | } else { | |
678 | if (dev->vcm_driver && dev->vcm_driver->power_up) | |
679 | ret = dev->vcm_driver->power_up(sd); | |
680 | if (ret) | |
681 | return ret; | |
682 | ret = power_up(sd); | |
683 | if (!ret) { | |
684 | dev->power = 1; | |
685 | return __imx_init(sd, 0); | |
686 | } | |
687 | } | |
688 | ||
689 | return ret; | |
690 | } | |
691 | ||
692 | static int imx_s_power(struct v4l2_subdev *sd, int on) | |
693 | { | |
694 | int ret; | |
695 | struct imx_device *dev = to_imx_sensor(sd); | |
696 | ||
697 | mutex_lock(&dev->input_lock); | |
698 | ret = __imx_s_power(sd, on); | |
699 | mutex_unlock(&dev->input_lock); | |
700 | ||
701 | return ret; | |
702 | } | |
703 | ||
704 | static int imx_get_intg_factor(struct i2c_client *client, | |
705 | struct camera_mipi_info *info, | |
706 | const struct imx_reg *reglist) | |
707 | { | |
708 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
709 | struct imx_device *dev = to_imx_sensor(sd); | |
710 | int lanes = imx_get_lanes(sd); | |
711 | u32 vt_pix_clk_div; | |
712 | u32 vt_sys_clk_div; | |
713 | u32 pre_pll_clk_div; | |
714 | u32 pll_multiplier; | |
715 | ||
716 | const int ext_clk_freq_hz = 19200000; | |
717 | struct atomisp_sensor_mode_data *buf = &info->data; | |
718 | int ret; | |
719 | u16 data[IMX_INTG_BUF_COUNT]; | |
720 | ||
721 | u32 vt_pix_clk_freq_mhz; | |
722 | u32 coarse_integration_time_min; | |
723 | u32 coarse_integration_time_max_margin; | |
724 | u32 read_mode; | |
725 | u32 div; | |
726 | ||
727 | if (info == NULL) | |
728 | return -EINVAL; | |
729 | ||
730 | memset(data, 0, IMX_INTG_BUF_COUNT * sizeof(u16)); | |
731 | ret = imx_read_reg(client, 1, IMX_VT_PIX_CLK_DIV, data); | |
732 | if (ret) | |
733 | return ret; | |
734 | vt_pix_clk_div = data[0] & IMX_MASK_5BIT; | |
735 | ||
736 | if (dev->sensor_id == IMX132_ID || dev->sensor_id == IMX208_ID) { | |
737 | static const int rgpltd[] = { 2, 4, 1, 1 }; | |
738 | ret = imx_read_reg(client, 1, IMX132_208_VT_RGPLTD, data); | |
739 | if (ret) | |
740 | return ret; | |
741 | vt_sys_clk_div = rgpltd[data[0] & IMX_MASK_2BIT]; | |
742 | } else { | |
743 | ret = imx_read_reg(client, 1, IMX_VT_SYS_CLK_DIV, data); | |
744 | if (ret) | |
745 | return ret; | |
746 | vt_sys_clk_div = data[0] & IMX_MASK_2BIT; | |
747 | } | |
748 | ret = imx_read_reg(client, 1, IMX_PRE_PLL_CLK_DIV, data); | |
749 | if (ret) | |
750 | return ret; | |
751 | pre_pll_clk_div = data[0] & IMX_MASK_4BIT; | |
752 | ||
753 | ret = imx_read_reg(client, 2, | |
754 | (dev->sensor_id == IMX132_ID || | |
755 | dev->sensor_id == IMX219_ID || | |
756 | dev->sensor_id == IMX208_ID) ? | |
757 | IMX132_208_219_PLL_MULTIPLIER : IMX_PLL_MULTIPLIER, data); | |
758 | if (ret) | |
759 | return ret; | |
760 | pll_multiplier = data[0] & IMX_MASK_11BIT; | |
761 | ||
762 | memset(data, 0, IMX_INTG_BUF_COUNT * sizeof(u16)); | |
763 | ret = imx_read_reg(client, 4, IMX_COARSE_INTG_TIME_MIN, data); | |
764 | if (ret) | |
765 | return ret; | |
766 | coarse_integration_time_min = data[0]; | |
767 | coarse_integration_time_max_margin = data[1]; | |
768 | ||
769 | /* Get the cropping and output resolution to ISP for this mode. */ | |
770 | ret = imx_read_reg(client, 2, dev->reg_addr->horizontal_start_h, data); | |
771 | if (ret) | |
772 | return ret; | |
773 | buf->crop_horizontal_start = data[0]; | |
774 | ||
775 | ret = imx_read_reg(client, 2, dev->reg_addr->vertical_start_h, data); | |
776 | if (ret) | |
777 | return ret; | |
778 | buf->crop_vertical_start = data[0]; | |
779 | ||
780 | ret = imx_read_reg(client, 2, dev->reg_addr->horizontal_end_h, data); | |
781 | if (ret) | |
782 | return ret; | |
783 | buf->crop_horizontal_end = data[0]; | |
784 | ||
785 | ret = imx_read_reg(client, 2, dev->reg_addr->vertical_end_h, data); | |
786 | if (ret) | |
787 | return ret; | |
788 | buf->crop_vertical_end = data[0]; | |
789 | ||
790 | ret = imx_read_reg(client, 2, | |
791 | dev->reg_addr->horizontal_output_size_h, data); | |
792 | if (ret) | |
793 | return ret; | |
794 | buf->output_width = data[0]; | |
795 | ||
796 | ret = imx_read_reg(client, 2, | |
797 | dev->reg_addr->vertical_output_size_h, data); | |
798 | if (ret) | |
799 | return ret; | |
800 | buf->output_height = data[0]; | |
801 | ||
802 | memset(data, 0, IMX_INTG_BUF_COUNT * sizeof(u16)); | |
803 | if (dev->sensor_id == IMX132_ID || dev->sensor_id == IMX208_ID || | |
804 | dev->sensor_id == IMX219_ID) | |
805 | read_mode = 0; | |
806 | else { | |
807 | if (dev->sensor_id == IMX227_ID) | |
808 | ret = imx_read_reg(client, 1, IMX227_READ_MODE, data); | |
809 | else | |
810 | ret = imx_read_reg(client, 1, IMX_READ_MODE, data); | |
811 | ||
812 | if (ret) | |
813 | return ret; | |
814 | read_mode = data[0] & IMX_MASK_2BIT; | |
815 | } | |
816 | ||
817 | div = pre_pll_clk_div*vt_sys_clk_div*vt_pix_clk_div; | |
818 | if (div == 0) | |
819 | return -EINVAL; | |
820 | ||
821 | if (dev->sensor_id == IMX132_ID || dev->sensor_id == IMX208_ID) | |
822 | vt_pix_clk_freq_mhz = ext_clk_freq_hz / div; | |
91b0faa5 | 823 | else if (dev->sensor_id == IMX227_ID) { |
a49d2536 AC |
824 | /* according to IMX227 datasheet: |
825 | * vt_pix_freq_mhz = * num_of_vt_lanes(4) * ivt_pix_clk_freq_mhz | |
826 | */ | |
827 | vt_pix_clk_freq_mhz = | |
91b0faa5 AC |
828 | (u64)4 * ext_clk_freq_hz * pll_multiplier; |
829 | do_div(vt_pix_clk_freq_mhz, div); | |
830 | } else | |
a49d2536 AC |
831 | vt_pix_clk_freq_mhz = 2 * ext_clk_freq_hz / div; |
832 | ||
833 | vt_pix_clk_freq_mhz *= pll_multiplier; | |
834 | if (dev->sensor_id == IMX132_ID && lanes > 0) | |
835 | vt_pix_clk_freq_mhz *= lanes; | |
836 | ||
837 | dev->vt_pix_clk_freq_mhz = vt_pix_clk_freq_mhz; | |
838 | ||
839 | buf->vt_pix_clk_freq_mhz = vt_pix_clk_freq_mhz; | |
840 | buf->coarse_integration_time_min = coarse_integration_time_min; | |
841 | buf->coarse_integration_time_max_margin = | |
842 | coarse_integration_time_max_margin; | |
843 | ||
844 | buf->fine_integration_time_min = IMX_FINE_INTG_TIME; | |
845 | buf->fine_integration_time_max_margin = IMX_FINE_INTG_TIME; | |
846 | buf->fine_integration_time_def = IMX_FINE_INTG_TIME; | |
847 | buf->frame_length_lines = dev->lines_per_frame; | |
848 | buf->line_length_pck = dev->pixels_per_line; | |
849 | buf->read_mode = read_mode; | |
850 | ||
851 | if (dev->sensor_id == IMX132_ID || dev->sensor_id == IMX208_ID || | |
852 | dev->sensor_id == IMX219_ID) { | |
853 | buf->binning_factor_x = 1; | |
854 | buf->binning_factor_y = 1; | |
855 | } else { | |
856 | if (dev->sensor_id == IMX227_ID) | |
857 | ret = imx_read_reg(client, 1, IMX227_BINNING_ENABLE, | |
858 | data); | |
859 | else | |
860 | ret = imx_read_reg(client, 1, IMX_BINNING_ENABLE, data); | |
861 | ||
862 | if (ret) | |
863 | return ret; | |
864 | /* 1:binning enabled, 0:disabled */ | |
865 | if (data[0] == 1) { | |
866 | if (dev->sensor_id == IMX227_ID) | |
867 | ret = imx_read_reg(client, 1, | |
868 | IMX227_BINNING_TYPE, data); | |
869 | else | |
870 | ret = imx_read_reg(client, 1, | |
871 | IMX_BINNING_TYPE, data); | |
872 | ||
873 | if (ret) | |
874 | return ret; | |
875 | buf->binning_factor_x = data[0] >> 4 & 0x0f; | |
876 | if (!buf->binning_factor_x) | |
877 | buf->binning_factor_x = 1; | |
878 | buf->binning_factor_y = data[0] & 0xf; | |
879 | if (!buf->binning_factor_y) | |
880 | buf->binning_factor_y = 1; | |
881 | /* WOWRKAROUND, NHD setting for IMX227 should have 4x4 | |
882 | * binning but the register setting does not reflect | |
883 | * this, I am asking vendor why this happens. this is | |
884 | * workaround for INTEL BZ 216560. | |
885 | */ | |
886 | if (dev->sensor_id == IMX227_ID) { | |
887 | if (dev->curr_res_table[dev->fmt_idx].width == | |
888 | 376 && | |
889 | dev->curr_res_table[dev->fmt_idx].height == | |
890 | 656) { | |
891 | buf->binning_factor_x = 4; | |
892 | buf->binning_factor_y = 4; | |
893 | } | |
894 | } | |
895 | } else { | |
896 | buf->binning_factor_x = 1; | |
897 | buf->binning_factor_y = 1; | |
898 | } | |
899 | } | |
900 | ||
901 | return 0; | |
902 | } | |
903 | ||
904 | /* This returns the exposure time being used. This should only be used | |
905 | for filling in EXIF data, not for actual image processing. */ | |
906 | static int imx_q_exposure(struct v4l2_subdev *sd, s32 *value) | |
907 | { | |
908 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
909 | struct imx_device *dev = to_imx_sensor(sd); | |
910 | u16 coarse; | |
911 | int ret; | |
912 | ||
913 | /* the fine integration time is currently not calculated */ | |
914 | ret = imx_read_reg(client, IMX_16BIT, | |
915 | dev->reg_addr->coarse_integration_time, &coarse); | |
916 | *value = coarse; | |
917 | ||
918 | return ret; | |
919 | } | |
920 | ||
921 | static int imx_test_pattern(struct v4l2_subdev *sd) | |
922 | { | |
923 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
924 | struct imx_device *dev = to_imx_sensor(sd); | |
925 | int ret; | |
926 | ||
927 | if (dev->power == 0) | |
928 | return 0; | |
929 | ||
930 | ret = imx_write_reg(client, IMX_16BIT, IMX_TEST_PATTERN_COLOR_R, | |
931 | (u16)(dev->tp_r->val >> 22)); | |
932 | if (ret) | |
933 | return ret; | |
934 | ||
935 | ret = imx_write_reg(client, IMX_16BIT, IMX_TEST_PATTERN_COLOR_GR, | |
936 | (u16)(dev->tp_gr->val >> 22)); | |
937 | if (ret) | |
938 | return ret; | |
939 | ||
940 | ret = imx_write_reg(client, IMX_16BIT, IMX_TEST_PATTERN_COLOR_GB, | |
941 | (u16)(dev->tp_gb->val >> 22)); | |
942 | if (ret) | |
943 | return ret; | |
944 | ||
945 | ret = imx_write_reg(client, IMX_16BIT, IMX_TEST_PATTERN_COLOR_B, | |
946 | (u16)(dev->tp_b->val >> 22)); | |
947 | if (ret) | |
948 | return ret; | |
949 | ||
950 | return imx_write_reg(client, IMX_16BIT, IMX_TEST_PATTERN_MODE, | |
951 | (u16)(dev->tp_mode->val)); | |
952 | } | |
953 | ||
954 | static u32 imx_translate_bayer_order(enum atomisp_bayer_order code) | |
955 | { | |
956 | switch (code) { | |
957 | case atomisp_bayer_order_rggb: | |
958 | return MEDIA_BUS_FMT_SRGGB10_1X10; | |
959 | case atomisp_bayer_order_grbg: | |
960 | return MEDIA_BUS_FMT_SGRBG10_1X10; | |
961 | case atomisp_bayer_order_bggr: | |
962 | return MEDIA_BUS_FMT_SBGGR10_1X10; | |
963 | case atomisp_bayer_order_gbrg: | |
964 | return MEDIA_BUS_FMT_SGBRG10_1X10; | |
965 | } | |
966 | return 0; | |
967 | } | |
968 | ||
969 | static int imx_v_flip(struct v4l2_subdev *sd, s32 value) | |
970 | { | |
971 | struct imx_device *dev = to_imx_sensor(sd); | |
972 | struct camera_mipi_info *imx_info = NULL; | |
973 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
974 | int ret; | |
975 | u16 val; | |
976 | ||
977 | if (dev->power == 0) | |
978 | return -EIO; | |
979 | ||
980 | ret = imx_write_reg_array(client, dev->param_hold); | |
981 | if (ret) | |
982 | return ret; | |
983 | ||
984 | ret = imx_read_reg(client, IMX_8BIT, | |
985 | dev->reg_addr->img_orientation, &val); | |
986 | if (ret) | |
987 | return ret; | |
988 | if (value) | |
989 | val |= IMX_VFLIP_BIT; | |
990 | else | |
991 | val &= ~IMX_VFLIP_BIT; | |
992 | ||
993 | ret = imx_write_reg(client, IMX_8BIT, | |
994 | dev->reg_addr->img_orientation, val); | |
995 | if (ret) | |
996 | return ret; | |
997 | ||
998 | imx_info = v4l2_get_subdev_hostdata(sd); | |
999 | if (imx_info) { | |
1000 | val &= (IMX_VFLIP_BIT|IMX_HFLIP_BIT); | |
1001 | imx_info->raw_bayer_order = imx_bayer_order_mapping[val]; | |
1002 | dev->format.code = imx_translate_bayer_order( | |
1003 | imx_info->raw_bayer_order); | |
1004 | } | |
1005 | ||
1006 | return imx_write_reg_array(client, dev->param_update); | |
1007 | } | |
1008 | ||
1009 | static int imx_h_flip(struct v4l2_subdev *sd, s32 value) | |
1010 | { | |
1011 | struct imx_device *dev = to_imx_sensor(sd); | |
1012 | struct camera_mipi_info *imx_info = NULL; | |
1013 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1014 | int ret; | |
1015 | u16 val; | |
1016 | ||
1017 | if (dev->power == 0) | |
1018 | return -EIO; | |
1019 | ||
1020 | ret = imx_write_reg_array(client, dev->param_hold); | |
1021 | if (ret) | |
1022 | return ret; | |
1023 | ret = imx_read_reg(client, IMX_8BIT, | |
1024 | dev->reg_addr->img_orientation, &val); | |
1025 | if (ret) | |
1026 | return ret; | |
1027 | if (value) | |
1028 | val |= IMX_HFLIP_BIT; | |
1029 | else | |
1030 | val &= ~IMX_HFLIP_BIT; | |
1031 | ret = imx_write_reg(client, IMX_8BIT, | |
1032 | dev->reg_addr->img_orientation, val); | |
1033 | if (ret) | |
1034 | return ret; | |
1035 | ||
1036 | imx_info = v4l2_get_subdev_hostdata(sd); | |
1037 | if (imx_info) { | |
1038 | val &= (IMX_VFLIP_BIT|IMX_HFLIP_BIT); | |
1039 | imx_info->raw_bayer_order = imx_bayer_order_mapping[val]; | |
1040 | dev->format.code = imx_translate_bayer_order( | |
1041 | imx_info->raw_bayer_order); | |
1042 | } | |
1043 | ||
1044 | return imx_write_reg_array(client, dev->param_update); | |
1045 | } | |
1046 | ||
1047 | static int imx_g_focal(struct v4l2_subdev *sd, s32 *val) | |
1048 | { | |
1049 | *val = (IMX_FOCAL_LENGTH_NUM << 16) | IMX_FOCAL_LENGTH_DEM; | |
1050 | return 0; | |
1051 | } | |
1052 | ||
1053 | static int imx_g_fnumber(struct v4l2_subdev *sd, s32 *val) | |
1054 | { | |
1055 | /*const f number for imx*/ | |
1056 | *val = (IMX_F_NUMBER_DEFAULT_NUM << 16) | IMX_F_NUMBER_DEM; | |
1057 | return 0; | |
1058 | } | |
1059 | ||
1060 | static int imx_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) | |
1061 | { | |
1062 | *val = (IMX_F_NUMBER_DEFAULT_NUM << 24) | | |
1063 | (IMX_F_NUMBER_DEM << 16) | | |
1064 | (IMX_F_NUMBER_DEFAULT_NUM << 8) | IMX_F_NUMBER_DEM; | |
1065 | return 0; | |
1066 | } | |
1067 | ||
1068 | static int imx_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val) | |
1069 | { | |
1070 | struct imx_device *dev = to_imx_sensor(sd); | |
1071 | ||
1072 | *val = dev->curr_res_table[dev->fmt_idx].bin_factor_x; | |
1073 | ||
1074 | return 0; | |
1075 | } | |
1076 | ||
1077 | static int imx_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val) | |
1078 | { | |
1079 | struct imx_device *dev = to_imx_sensor(sd); | |
1080 | ||
1081 | *val = dev->curr_res_table[dev->fmt_idx].bin_factor_y; | |
1082 | ||
1083 | return 0; | |
1084 | } | |
1085 | ||
2f05db96 | 1086 | static int imx_t_focus_abs(struct v4l2_subdev *sd, s32 value) |
a49d2536 AC |
1087 | { |
1088 | struct imx_device *dev = to_imx_sensor(sd); | |
1089 | if (dev->vcm_driver && dev->vcm_driver->t_focus_abs) | |
1090 | return dev->vcm_driver->t_focus_abs(sd, value); | |
1091 | return 0; | |
1092 | } | |
2f05db96 JVP |
1093 | |
1094 | static int imx_t_focus_rel(struct v4l2_subdev *sd, s32 value) | |
a49d2536 AC |
1095 | { |
1096 | struct imx_device *dev = to_imx_sensor(sd); | |
1097 | if (dev->vcm_driver && dev->vcm_driver->t_focus_rel) | |
1098 | return dev->vcm_driver->t_focus_rel(sd, value); | |
1099 | return 0; | |
1100 | } | |
1101 | ||
2f05db96 | 1102 | static int imx_q_focus_status(struct v4l2_subdev *sd, s32 *value) |
a49d2536 AC |
1103 | { |
1104 | struct imx_device *dev = to_imx_sensor(sd); | |
1105 | if (dev->vcm_driver && dev->vcm_driver->q_focus_status) | |
1106 | return dev->vcm_driver->q_focus_status(sd, value); | |
1107 | return 0; | |
1108 | } | |
1109 | ||
2f05db96 | 1110 | static int imx_q_focus_abs(struct v4l2_subdev *sd, s32 *value) |
a49d2536 AC |
1111 | { |
1112 | struct imx_device *dev = to_imx_sensor(sd); | |
1113 | if (dev->vcm_driver && dev->vcm_driver->q_focus_abs) | |
1114 | return dev->vcm_driver->q_focus_abs(sd, value); | |
1115 | return 0; | |
1116 | } | |
1117 | ||
2f05db96 | 1118 | static int imx_t_vcm_slew(struct v4l2_subdev *sd, s32 value) |
a49d2536 AC |
1119 | { |
1120 | struct imx_device *dev = to_imx_sensor(sd); | |
1121 | if (dev->vcm_driver && dev->vcm_driver->t_vcm_slew) | |
1122 | return dev->vcm_driver->t_vcm_slew(sd, value); | |
1123 | return 0; | |
1124 | } | |
1125 | ||
2f05db96 | 1126 | static int imx_t_vcm_timing(struct v4l2_subdev *sd, s32 value) |
a49d2536 AC |
1127 | { |
1128 | struct imx_device *dev = to_imx_sensor(sd); | |
1129 | if (dev->vcm_driver && dev->vcm_driver->t_vcm_timing) | |
1130 | return dev->vcm_driver->t_vcm_timing(sd, value); | |
1131 | return 0; | |
1132 | } | |
1133 | ||
1134 | static int imx_s_ctrl(struct v4l2_ctrl *ctrl) | |
1135 | { | |
1136 | struct imx_device *dev = container_of( | |
1137 | ctrl->handler, struct imx_device, ctrl_handler); | |
1138 | struct i2c_client *client = v4l2_get_subdevdata(&dev->sd); | |
1139 | int ret = 0; | |
1140 | ||
1141 | switch (ctrl->id) { | |
1142 | case V4L2_CID_TEST_PATTERN: | |
1143 | ret = imx_test_pattern(&dev->sd); | |
1144 | break; | |
1145 | case V4L2_CID_VFLIP: | |
1146 | dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n", | |
1147 | __func__, ctrl->val); | |
1148 | ret = imx_v_flip(&dev->sd, ctrl->val); | |
1149 | break; | |
1150 | case V4L2_CID_HFLIP: | |
1151 | dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n", | |
1152 | __func__, ctrl->val); | |
1153 | ret = imx_h_flip(&dev->sd, ctrl->val); | |
1154 | break; | |
1155 | case V4L2_CID_FOCUS_ABSOLUTE: | |
1156 | ret = imx_t_focus_abs(&dev->sd, ctrl->val); | |
1157 | break; | |
1158 | case V4L2_CID_FOCUS_RELATIVE: | |
1159 | ret = imx_t_focus_rel(&dev->sd, ctrl->val); | |
1160 | break; | |
1161 | case V4L2_CID_VCM_SLEW: | |
1162 | ret = imx_t_vcm_slew(&dev->sd, ctrl->val); | |
1163 | break; | |
1164 | case V4L2_CID_VCM_TIMEING: | |
1165 | ret = imx_t_vcm_timing(&dev->sd, ctrl->val); | |
1166 | break; | |
1167 | } | |
1168 | ||
1169 | return ret; | |
1170 | } | |
1171 | ||
1172 | static int imx_g_volatile_ctrl(struct v4l2_ctrl *ctrl) | |
1173 | { | |
1174 | struct imx_device *dev = container_of( | |
1175 | ctrl->handler, struct imx_device, ctrl_handler); | |
1176 | int ret = 0; | |
1177 | unsigned int val; | |
1178 | ||
1179 | switch (ctrl->id) { | |
1180 | case V4L2_CID_EXPOSURE_ABSOLUTE: | |
1181 | ret = imx_q_exposure(&dev->sd, &ctrl->val); | |
1182 | break; | |
1183 | case V4L2_CID_FOCUS_ABSOLUTE: | |
1184 | ret = imx_q_focus_abs(&dev->sd, &ctrl->val); | |
1185 | break; | |
1186 | case V4L2_CID_FOCUS_STATUS: | |
1187 | ret = imx_q_focus_status(&dev->sd, &ctrl->val); | |
1188 | break; | |
1189 | case V4L2_CID_FOCAL_ABSOLUTE: | |
1190 | ret = imx_g_focal(&dev->sd, &ctrl->val); | |
1191 | break; | |
1192 | case V4L2_CID_FNUMBER_ABSOLUTE: | |
1193 | ret = imx_g_fnumber(&dev->sd, &ctrl->val); | |
1194 | break; | |
1195 | case V4L2_CID_FNUMBER_RANGE: | |
1196 | ret = imx_g_fnumber_range(&dev->sd, &ctrl->val); | |
1197 | break; | |
1198 | case V4L2_CID_BIN_FACTOR_HORZ: | |
1199 | ret = imx_g_bin_factor_x(&dev->sd, &ctrl->val); | |
1200 | break; | |
1201 | case V4L2_CID_BIN_FACTOR_VERT: | |
1202 | ret = imx_g_bin_factor_y(&dev->sd, &ctrl->val); | |
1203 | break; | |
1204 | case V4L2_CID_VBLANK: | |
1205 | ctrl->val = dev->lines_per_frame - | |
1206 | dev->curr_res_table[dev->fmt_idx].height; | |
1207 | break; | |
1208 | case V4L2_CID_HBLANK: | |
1209 | ctrl->val = dev->pixels_per_line - | |
1210 | dev->curr_res_table[dev->fmt_idx].width; | |
1211 | break; | |
1212 | case V4L2_CID_PIXEL_RATE: | |
1213 | ctrl->val = dev->vt_pix_clk_freq_mhz; | |
1214 | break; | |
1215 | case V4L2_CID_LINK_FREQ: | |
1216 | val = dev->curr_res_table[dev->fmt_idx]. | |
1217 | fps_options[dev->fps_index].mipi_freq; | |
1218 | if (val == 0) | |
1219 | val = dev->curr_res_table[dev->fmt_idx].mipi_freq; | |
1220 | if (val == 0) | |
1221 | return -EINVAL; | |
1222 | ctrl->val = val * 1000; /* To Hz */ | |
1223 | break; | |
1224 | default: | |
1225 | return -EINVAL; | |
1226 | } | |
1227 | ||
1228 | return ret; | |
1229 | } | |
1230 | ||
1231 | static const struct v4l2_ctrl_ops ctrl_ops = { | |
1232 | .s_ctrl = imx_s_ctrl, | |
1233 | .g_volatile_ctrl = imx_g_volatile_ctrl | |
1234 | }; | |
1235 | ||
1236 | static const struct v4l2_ctrl_config imx_controls[] = { | |
1237 | { | |
1238 | .ops = &ctrl_ops, | |
1239 | .id = V4L2_CID_EXPOSURE_ABSOLUTE, | |
1240 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1241 | .name = "exposure", | |
1242 | .min = 0x0, | |
1243 | .max = 0xffff, | |
1244 | .step = 0x01, | |
1245 | .def = 0x00, | |
1246 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1247 | }, | |
1248 | { | |
1249 | .ops = &ctrl_ops, | |
1250 | .id = V4L2_CID_TEST_PATTERN, | |
1251 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1252 | .name = "Test pattern", | |
1253 | .min = 0, | |
1254 | .max = 0xffff, | |
1255 | .step = 1, | |
1256 | .def = 0, | |
1257 | }, | |
1258 | { | |
1259 | .ops = &ctrl_ops, | |
1260 | .id = V4L2_CID_TEST_PATTERN_COLOR_R, | |
1261 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1262 | .name = "Test pattern solid color R", | |
1263 | .min = INT_MIN, | |
1264 | .max = INT_MAX, | |
1265 | .step = 1, | |
1266 | .def = 0, | |
1267 | }, | |
1268 | { | |
1269 | .ops = &ctrl_ops, | |
1270 | .id = V4L2_CID_TEST_PATTERN_COLOR_GR, | |
1271 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1272 | .name = "Test pattern solid color GR", | |
1273 | .min = INT_MIN, | |
1274 | .max = INT_MAX, | |
1275 | .step = 1, | |
1276 | .def = 0, | |
1277 | }, | |
1278 | { | |
1279 | .ops = &ctrl_ops, | |
1280 | .id = V4L2_CID_TEST_PATTERN_COLOR_GB, | |
1281 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1282 | .name = "Test pattern solid color GB", | |
1283 | .min = INT_MIN, | |
1284 | .max = INT_MAX, | |
1285 | .step = 1, | |
1286 | .def = 0, | |
1287 | }, | |
1288 | { | |
1289 | .ops = &ctrl_ops, | |
1290 | .id = V4L2_CID_TEST_PATTERN_COLOR_B, | |
1291 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1292 | .name = "Test pattern solid color B", | |
1293 | .min = INT_MIN, | |
1294 | .max = INT_MAX, | |
1295 | .step = 1, | |
1296 | .def = 0, | |
1297 | }, | |
1298 | { | |
1299 | .ops = &ctrl_ops, | |
1300 | .id = V4L2_CID_VFLIP, | |
1301 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
1302 | .name = "Flip", | |
1303 | .min = 0, | |
1304 | .max = 1, | |
1305 | .step = 1, | |
1306 | .def = 0, | |
1307 | }, | |
1308 | { | |
1309 | .ops = &ctrl_ops, | |
1310 | .id = V4L2_CID_HFLIP, | |
1311 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
1312 | .name = "Mirror", | |
1313 | .min = 0, | |
1314 | .max = 1, | |
1315 | .step = 1, | |
1316 | .def = 0, | |
1317 | }, | |
1318 | { | |
1319 | .ops = &ctrl_ops, | |
1320 | .id = V4L2_CID_FOCUS_ABSOLUTE, | |
1321 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1322 | .name = "focus move absolute", | |
1323 | .min = 0, | |
1324 | .max = IMX_MAX_FOCUS_POS, | |
1325 | .step = 1, | |
1326 | .def = 0, | |
1327 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1328 | }, | |
1329 | { | |
1330 | .ops = &ctrl_ops, | |
1331 | .id = V4L2_CID_FOCUS_RELATIVE, | |
1332 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1333 | .name = "focus move relative", | |
1334 | .min = IMX_MAX_FOCUS_NEG, | |
1335 | .max = IMX_MAX_FOCUS_POS, | |
1336 | .step = 1, | |
1337 | .def = 0, | |
1338 | .flags = 0, | |
1339 | }, | |
1340 | { | |
1341 | .ops = &ctrl_ops, | |
1342 | .id = V4L2_CID_FOCUS_STATUS, | |
1343 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1344 | .name = "focus status", | |
1345 | .min = 0, | |
1346 | .max = 100, /* allow enum to grow in the future */ | |
1347 | .step = 1, | |
1348 | .def = 0, | |
1349 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1350 | }, | |
1351 | { | |
1352 | .ops = &ctrl_ops, | |
1353 | .id = V4L2_CID_VCM_SLEW, | |
1354 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1355 | .name = "vcm slew", | |
1356 | .min = 0, | |
1357 | .max = IMX_VCM_SLEW_STEP_MAX, | |
1358 | .step = 1, | |
1359 | .def = 0, | |
1360 | .flags = 0, | |
1361 | }, | |
1362 | { | |
1363 | .ops = &ctrl_ops, | |
1364 | .id = V4L2_CID_VCM_TIMEING, | |
1365 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1366 | .name = "vcm step time", | |
1367 | .min = 0, | |
1368 | .max = IMX_VCM_SLEW_TIME_MAX, | |
1369 | .step = 1, | |
1370 | .def = 0, | |
1371 | .flags = 0, | |
1372 | }, | |
1373 | { | |
1374 | .ops = &ctrl_ops, | |
1375 | .id = V4L2_CID_FOCAL_ABSOLUTE, | |
1376 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1377 | .name = "focal length", | |
1378 | .min = IMX_FOCAL_LENGTH_DEFAULT, | |
1379 | .max = IMX_FOCAL_LENGTH_DEFAULT, | |
1380 | .step = 0x01, | |
1381 | .def = IMX_FOCAL_LENGTH_DEFAULT, | |
1382 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1383 | }, | |
1384 | { | |
1385 | .ops = &ctrl_ops, | |
1386 | .id = V4L2_CID_FNUMBER_ABSOLUTE, | |
1387 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1388 | .name = "f-number", | |
1389 | .min = IMX_F_NUMBER_DEFAULT, | |
1390 | .max = IMX_F_NUMBER_DEFAULT, | |
1391 | .step = 0x01, | |
1392 | .def = IMX_F_NUMBER_DEFAULT, | |
1393 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1394 | }, | |
1395 | { | |
1396 | .ops = &ctrl_ops, | |
1397 | .id = V4L2_CID_FNUMBER_RANGE, | |
1398 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1399 | .name = "f-number range", | |
1400 | .min = IMX_F_NUMBER_RANGE, | |
1401 | .max = IMX_F_NUMBER_RANGE, | |
1402 | .step = 0x01, | |
1403 | .def = IMX_F_NUMBER_RANGE, | |
1404 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1405 | }, | |
1406 | { | |
1407 | .ops = &ctrl_ops, | |
1408 | .id = V4L2_CID_BIN_FACTOR_HORZ, | |
1409 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1410 | .name = "horizontal binning factor", | |
1411 | .min = 0, | |
1412 | .max = IMX_BIN_FACTOR_MAX, | |
1413 | .step = 1, | |
1414 | .def = 0, | |
1415 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1416 | }, | |
1417 | { | |
1418 | .ops = &ctrl_ops, | |
1419 | .id = V4L2_CID_BIN_FACTOR_VERT, | |
1420 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1421 | .name = "vertical binning factor", | |
1422 | .min = 0, | |
1423 | .max = IMX_BIN_FACTOR_MAX, | |
1424 | .step = 1, | |
1425 | .def = 0, | |
1426 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1427 | }, | |
1428 | { | |
1429 | .ops = &ctrl_ops, | |
1430 | .id = V4L2_CID_LINK_FREQ, | |
1431 | .name = "Link Frequency", | |
1432 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1433 | .min = 1, | |
1434 | .max = 1500000 * 1000, | |
1435 | .step = 1, | |
1436 | .def = 1, | |
1437 | .flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY, | |
1438 | }, | |
1439 | { | |
1440 | .ops = &ctrl_ops, | |
1441 | .id = V4L2_CID_PIXEL_RATE, | |
1442 | .name = "Pixel Rate", | |
1443 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1444 | .min = 0, | |
1445 | .max = INT_MAX, | |
1446 | .step = 1, | |
1447 | .def = 0, | |
1448 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1449 | }, | |
1450 | { | |
1451 | .ops = &ctrl_ops, | |
1452 | .id = V4L2_CID_HBLANK, | |
1453 | .name = "Horizontal Blanking", | |
1454 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1455 | .min = 0, | |
1456 | .max = SHRT_MAX, | |
1457 | .step = 1, | |
1458 | .def = 0, | |
1459 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1460 | }, | |
1461 | { | |
1462 | .ops = &ctrl_ops, | |
1463 | .id = V4L2_CID_VBLANK, | |
1464 | .name = "Vertical Blanking", | |
1465 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1466 | .min = 0, | |
1467 | .max = SHRT_MAX, | |
1468 | .step = 1, | |
1469 | .def = 0, | |
1470 | .flags = V4L2_CTRL_FLAG_VOLATILE, | |
1471 | }, | |
1472 | { | |
1473 | .ops = &ctrl_ops, | |
1474 | .id = V4L2_CID_HFLIP, | |
1475 | .name = "Horizontal Flip", | |
1476 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1477 | .min = 0, | |
1478 | .max = 1, | |
1479 | .step = 1, | |
1480 | .def = 0, | |
1481 | .flags = 0, | |
1482 | }, | |
1483 | { | |
1484 | .ops = &ctrl_ops, | |
1485 | .id = V4L2_CID_VFLIP, | |
1486 | .name = "Vertical Flip", | |
1487 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1488 | .min = 0, | |
1489 | .max = 1, | |
1490 | .step = 1, | |
1491 | .def = 0, | |
1492 | .flags = 0, | |
1493 | }, | |
1494 | }; | |
1495 | ||
1496 | /* | |
1497 | * distance - calculate the distance | |
1498 | * @res: resolution | |
1499 | * @w: width | |
1500 | * @h: height | |
1501 | * | |
1502 | * Get the gap between resolution and w/h. | |
1503 | * res->width/height smaller than w/h wouldn't be considered. | |
1504 | * Returns the value of gap or -1 if fail. | |
1505 | */ | |
1506 | #define LARGEST_ALLOWED_RATIO_MISMATCH 600 | |
1507 | static int distance(struct imx_resolution const *res, u32 w, u32 h, | |
1508 | bool keep_ratio) | |
1509 | { | |
1510 | unsigned int w_ratio; | |
1511 | unsigned int h_ratio; | |
1512 | int match; | |
1513 | unsigned int allowed_ratio_mismatch = LARGEST_ALLOWED_RATIO_MISMATCH; | |
1514 | ||
1515 | if (!keep_ratio) | |
1516 | allowed_ratio_mismatch = ~0; | |
1517 | ||
1518 | if (w == 0) | |
1519 | return -1; | |
1520 | w_ratio = (res->width << 13) / w; | |
1521 | if (h == 0) | |
1522 | return -1; | |
1523 | h_ratio = (res->height << 13) / h; | |
1524 | if (h_ratio == 0) | |
1525 | return -1; | |
1526 | match = abs(((w_ratio << 13) / h_ratio) - ((int)8192)); | |
1527 | ||
1528 | if ((w_ratio < (int)8192) || (h_ratio < (int)8192) || | |
1529 | (match > allowed_ratio_mismatch)) | |
1530 | return -1; | |
1531 | ||
1532 | return w_ratio + h_ratio; | |
1533 | } | |
1534 | ||
1535 | /* Return the nearest higher resolution index */ | |
1536 | static int nearest_resolution_index(struct v4l2_subdev *sd, int w, int h) | |
1537 | { | |
1538 | int i; | |
1539 | int idx = -1; | |
1540 | int dist; | |
1541 | int fps_diff; | |
1542 | int min_fps_diff = INT_MAX; | |
1543 | int min_dist = INT_MAX; | |
1544 | const struct imx_resolution *tmp_res = NULL; | |
1545 | struct imx_device *dev = to_imx_sensor(sd); | |
1546 | bool again = 1; | |
1547 | retry: | |
1548 | for (i = 0; i < dev->entries_curr_table; i++) { | |
1549 | tmp_res = &dev->curr_res_table[i]; | |
1550 | dist = distance(tmp_res, w, h, again); | |
1551 | if (dist == -1) | |
1552 | continue; | |
1553 | if (dist < min_dist) { | |
1554 | min_dist = dist; | |
1555 | idx = i; | |
1556 | } | |
1557 | if (dist == min_dist) { | |
1558 | fps_diff = __imx_min_fps_diff(dev->targetfps, | |
1559 | tmp_res->fps_options); | |
1560 | if (fps_diff < min_fps_diff) { | |
1561 | min_fps_diff = fps_diff; | |
1562 | idx = i; | |
1563 | } | |
1564 | } | |
1565 | } | |
1566 | ||
1567 | /* | |
1568 | * FIXME! | |
1569 | * only IMX135 for Saltbay and IMX227 use this algorithm | |
1570 | */ | |
1571 | if (idx == -1 && again == true && dev->new_res_sel_method) { | |
1572 | again = false; | |
1573 | goto retry; | |
1574 | } | |
1575 | return idx; | |
1576 | } | |
1577 | ||
1578 | /* Call with ctrl_handler.lock hold */ | |
1579 | static int __adjust_hvblank(struct v4l2_subdev *sd) | |
1580 | { | |
1581 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1582 | struct imx_device *dev = to_imx_sensor(sd); | |
1583 | u16 new_frame_length_lines, new_line_length_pck; | |
1584 | int ret; | |
1585 | ||
1586 | /* | |
1587 | * No need to adjust h/v blank if not set dbg value | |
1588 | * Note that there is no other checking on the h/v blank value, | |
1589 | * as h/v blank can be set to any value above zero for debug purpose | |
1590 | */ | |
1591 | if (!dev->v_blank->val || !dev->h_blank->val) | |
1592 | return 0; | |
1593 | ||
1594 | new_frame_length_lines = dev->curr_res_table[dev->fmt_idx].height + | |
1595 | dev->v_blank->val; | |
1596 | new_line_length_pck = dev->curr_res_table[dev->fmt_idx].width + | |
1597 | dev->h_blank->val; | |
1598 | ||
1599 | ret = imx_write_reg(client, IMX_16BIT, | |
1600 | dev->reg_addr->line_length_pixels, new_line_length_pck); | |
1601 | if (ret) | |
1602 | return ret; | |
1603 | ret = imx_write_reg(client, IMX_16BIT, | |
1604 | dev->reg_addr->frame_length_lines, new_frame_length_lines); | |
1605 | if (ret) | |
1606 | return ret; | |
1607 | ||
1608 | dev->lines_per_frame = new_frame_length_lines; | |
1609 | dev->pixels_per_line = new_line_length_pck; | |
1610 | ||
1611 | return 0; | |
1612 | } | |
1613 | ||
1614 | static int imx_set_fmt(struct v4l2_subdev *sd, | |
1615 | struct v4l2_subdev_pad_config *cfg, | |
1616 | struct v4l2_subdev_format *format) | |
1617 | { | |
1618 | struct v4l2_mbus_framefmt *fmt = &format->format; | |
1619 | struct imx_device *dev = to_imx_sensor(sd); | |
1620 | struct camera_mipi_info *imx_info = NULL; | |
1621 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1622 | const struct imx_resolution *res; | |
1623 | int lanes = imx_get_lanes(sd); | |
1624 | int ret; | |
1625 | u16 data, val; | |
1626 | int idx; | |
1627 | if (format->pad) | |
1628 | return -EINVAL; | |
1629 | if (!fmt) | |
1630 | return -EINVAL; | |
1631 | ||
1632 | imx_info = v4l2_get_subdev_hostdata(sd); | |
1633 | if (imx_info == NULL) | |
1634 | return -EINVAL; | |
1635 | if ((fmt->width > imx_max_res[dev->sensor_id].res_max_width) | |
1636 | || (fmt->height > imx_max_res[dev->sensor_id].res_max_height)) { | |
1637 | fmt->width = imx_max_res[dev->sensor_id].res_max_width; | |
1638 | fmt->height = imx_max_res[dev->sensor_id].res_max_height; | |
1639 | } else { | |
1640 | idx = nearest_resolution_index(sd, fmt->width, fmt->height); | |
1641 | ||
1642 | /* | |
1643 | * nearest_resolution_index() doesn't return smaller | |
1644 | * resolutions. If it fails, it means the requested | |
1645 | * resolution is higher than wecan support. Fallback | |
1646 | * to highest possible resolution in this case. | |
1647 | */ | |
1648 | if (idx == -1) | |
1649 | idx = dev->entries_curr_table - 1; | |
1650 | ||
1651 | fmt->width = dev->curr_res_table[idx].width; | |
1652 | fmt->height = dev->curr_res_table[idx].height; | |
1653 | } | |
1654 | ||
1655 | fmt->code = dev->format.code; | |
1656 | if(format->which == V4L2_SUBDEV_FORMAT_TRY) { | |
1657 | cfg->try_fmt = *fmt; | |
1658 | return 0; | |
1659 | } | |
1660 | mutex_lock(&dev->input_lock); | |
1661 | ||
1662 | dev->fmt_idx = nearest_resolution_index(sd, fmt->width, fmt->height); | |
1663 | if (dev->fmt_idx == -1) { | |
1664 | ret = -EINVAL; | |
1665 | goto out; | |
1666 | } | |
1667 | res = &dev->curr_res_table[dev->fmt_idx]; | |
1668 | ||
1669 | /* Adjust the FPS selection based on the resolution selected */ | |
1670 | dev->fps_index = __imx_nearest_fps_index(dev->targetfps, | |
1671 | res->fps_options); | |
1672 | dev->fps = res->fps_options[dev->fps_index].fps; | |
1673 | dev->regs = res->fps_options[dev->fps_index].regs; | |
1674 | if (!dev->regs) | |
1675 | dev->regs = res->regs; | |
1676 | ||
1677 | ret = imx_write_reg_array(client, dev->regs); | |
1678 | if (ret) | |
1679 | goto out; | |
1680 | ||
1681 | if (dev->sensor_id == IMX132_ID && lanes > 0) { | |
1682 | static const u8 imx132_rgpltd[] = { | |
1683 | 2, /* 1 lane: /1 */ | |
1684 | 0, /* 2 lanes: /2 */ | |
1685 | 0, /* undefined */ | |
1686 | 1, /* 4 lanes: /4 */ | |
1687 | }; | |
1688 | ret = imx_write_reg(client, IMX_8BIT, IMX132_208_VT_RGPLTD, | |
1689 | imx132_rgpltd[lanes - 1]); | |
1690 | if (ret) | |
1691 | goto out; | |
1692 | } | |
1693 | ||
1694 | dev->pixels_per_line = res->fps_options[dev->fps_index].pixels_per_line; | |
1695 | dev->lines_per_frame = res->fps_options[dev->fps_index].lines_per_frame; | |
1696 | ||
1697 | /* dbg h/v blank time */ | |
1698 | __adjust_hvblank(sd); | |
1699 | ||
1700 | ret = __imx_update_exposure_timing(client, dev->coarse_itg, | |
1701 | dev->pixels_per_line, dev->lines_per_frame); | |
1702 | if (ret) | |
1703 | goto out; | |
1704 | ||
1705 | ret = __imx_update_gain(sd, dev->gain); | |
1706 | if (ret) | |
1707 | goto out; | |
1708 | ||
1709 | ret = __imx_update_digital_gain(client, dev->digital_gain); | |
1710 | if (ret) | |
1711 | goto out; | |
1712 | ||
1713 | ret = imx_write_reg_array(client, dev->param_update); | |
1714 | if (ret) | |
1715 | goto out; | |
1716 | ||
1717 | ret = imx_get_intg_factor(client, imx_info, dev->regs); | |
1718 | if (ret) | |
1719 | goto out; | |
1720 | ||
1721 | ret = imx_read_reg(client, IMX_8BIT, | |
1722 | dev->reg_addr->img_orientation, &val); | |
1723 | if (ret) | |
1724 | goto out; | |
1725 | val &= (IMX_VFLIP_BIT|IMX_HFLIP_BIT); | |
1726 | imx_info->raw_bayer_order = imx_bayer_order_mapping[val]; | |
1727 | dev->format.code = imx_translate_bayer_order( | |
1728 | imx_info->raw_bayer_order); | |
1729 | ||
1730 | /* | |
1731 | * Fill meta data info. add imx135 metadata setting for RAW10 format | |
1732 | */ | |
1733 | switch (dev->sensor_id) { | |
1734 | case IMX135_ID: | |
1735 | ret = imx_read_reg(client, 2, | |
1736 | IMX135_OUTPUT_DATA_FORMAT_REG, &data); | |
1737 | if (ret) | |
1738 | goto out; | |
1739 | /* | |
1740 | * The IMX135 can support various resolutions like | |
1741 | * RAW6/8/10/12/14. | |
1742 | * 1.The data format is RAW10: | |
1743 | * matadata width = current resolution width(pixel) * 10 / 8 | |
1744 | * 2.The data format is RAW6 or RAW8: | |
1745 | * matadata width = current resolution width(pixel); | |
1746 | * 3.other data format(RAW12/14 etc): | |
1747 | * TBD. | |
1748 | */ | |
1749 | if (data == IMX135_OUTPUT_FORMAT_RAW10) | |
1750 | /* the data format is RAW10. */ | |
1751 | imx_info->metadata_width = res->width * 10 / 8; | |
1752 | else | |
1753 | /* The data format is RAW6/8/12/14/ etc. */ | |
1754 | imx_info->metadata_width = res->width; | |
1755 | ||
1756 | imx_info->metadata_height = IMX135_EMBEDDED_DATA_LINE_NUM; | |
1757 | ||
1758 | if (imx_info->metadata_effective_width == NULL) | |
1759 | imx_info->metadata_effective_width = | |
1760 | imx135_embedded_effective_size; | |
1761 | ||
1762 | break; | |
1763 | case IMX227_ID: | |
1764 | ret = imx_read_reg(client, 2, IMX227_OUTPUT_DATA_FORMAT_REG, | |
1765 | &data); | |
1766 | if (ret) | |
1767 | goto out; | |
1768 | if (data == IMX227_OUTPUT_FORMAT_RAW10) | |
1769 | /* the data format is RAW10. */ | |
1770 | imx_info->metadata_width = res->width * 10 / 8; | |
1771 | else | |
1772 | /* The data format is RAW6/8/12/14/ etc. */ | |
1773 | imx_info->metadata_width = res->width; | |
1774 | ||
1775 | imx_info->metadata_height = IMX227_EMBEDDED_DATA_LINE_NUM; | |
1776 | ||
1777 | if (imx_info->metadata_effective_width == NULL) | |
1778 | imx_info->metadata_effective_width = | |
1779 | imx227_embedded_effective_size; | |
1780 | ||
1781 | break; | |
1782 | default: | |
1783 | imx_info->metadata_width = 0; | |
1784 | imx_info->metadata_height = 0; | |
1785 | imx_info->metadata_effective_width = NULL; | |
1786 | break; | |
1787 | } | |
1788 | ||
1789 | out: | |
1790 | mutex_unlock(&dev->input_lock); | |
1791 | return ret; | |
1792 | } | |
1793 | ||
1794 | ||
1795 | static int imx_get_fmt(struct v4l2_subdev *sd, | |
1796 | struct v4l2_subdev_pad_config *cfg, | |
1797 | struct v4l2_subdev_format *format) | |
1798 | { | |
1799 | struct v4l2_mbus_framefmt *fmt = &format->format; | |
1800 | struct imx_device *dev = to_imx_sensor(sd); | |
1801 | ||
1802 | if (format->pad) | |
1803 | return -EINVAL; | |
1804 | if (!fmt) | |
1805 | return -EINVAL; | |
1806 | ||
1807 | mutex_lock(&dev->input_lock); | |
1808 | fmt->width = dev->curr_res_table[dev->fmt_idx].width; | |
1809 | fmt->height = dev->curr_res_table[dev->fmt_idx].height; | |
1810 | fmt->code = dev->format.code; | |
1811 | mutex_unlock(&dev->input_lock); | |
1812 | return 0; | |
1813 | } | |
1814 | ||
1815 | static int imx_detect(struct i2c_client *client, u16 *id, u8 *revision) | |
1816 | { | |
1817 | struct i2c_adapter *adapter = client->adapter; | |
1818 | ||
1819 | /* i2c check */ | |
1820 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) | |
1821 | return -ENODEV; | |
1822 | ||
1823 | /* check sensor chip ID */ | |
1824 | if (imx_read_reg(client, IMX_16BIT, IMX132_175_208_219_CHIP_ID, id)) { | |
1825 | v4l2_err(client, "sensor_id = 0x%x\n", *id); | |
1826 | return -ENODEV; | |
1827 | } | |
1828 | ||
1829 | if (*id == IMX132_ID || *id == IMX175_ID || | |
1830 | *id == IMX208_ID || *id == IMX219_ID) | |
1831 | goto found; | |
1832 | ||
1833 | if (imx_read_reg(client, IMX_16BIT, IMX134_135_227_CHIP_ID, id)) { | |
1834 | v4l2_err(client, "sensor_id = 0x%x\n", *id); | |
1835 | return -ENODEV; | |
1836 | } | |
1837 | if (*id != IMX134_ID && *id != IMX135_ID && *id != IMX227_ID) { | |
1838 | v4l2_err(client, "no imx sensor found\n"); | |
1839 | return -ENODEV; | |
1840 | } | |
1841 | found: | |
1842 | v4l2_info(client, "sensor_id = 0x%x\n", *id); | |
1843 | ||
1844 | /* TODO - need to be updated */ | |
1845 | *revision = 0; | |
1846 | ||
1847 | return 0; | |
1848 | } | |
1849 | ||
1850 | static void __imx_print_timing(struct v4l2_subdev *sd) | |
1851 | { | |
1852 | struct imx_device *dev = to_imx_sensor(sd); | |
1853 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1854 | u16 width = dev->curr_res_table[dev->fmt_idx].width; | |
1855 | u16 height = dev->curr_res_table[dev->fmt_idx].height; | |
1856 | ||
1857 | dev_dbg(&client->dev, "Dump imx timing in stream on:\n"); | |
1858 | dev_dbg(&client->dev, "width: %d:\n", width); | |
1859 | dev_dbg(&client->dev, "height: %d:\n", height); | |
1860 | dev_dbg(&client->dev, "pixels_per_line: %d:\n", dev->pixels_per_line); | |
1861 | dev_dbg(&client->dev, "line per frame: %d:\n", dev->lines_per_frame); | |
1862 | dev_dbg(&client->dev, "pix freq: %d:\n", dev->vt_pix_clk_freq_mhz); | |
1863 | dev_dbg(&client->dev, "init fps: %d:\n", dev->vt_pix_clk_freq_mhz / | |
1864 | dev->pixels_per_line / dev->lines_per_frame); | |
1865 | dev_dbg(&client->dev, "HBlank: %d nS:\n", | |
1866 | 1000 * (dev->pixels_per_line - width) / | |
1867 | (dev->vt_pix_clk_freq_mhz / 1000000)); | |
1868 | dev_dbg(&client->dev, "VBlank: %d uS:\n", | |
1869 | (dev->lines_per_frame - height) * dev->pixels_per_line / | |
1870 | (dev->vt_pix_clk_freq_mhz / 1000000)); | |
1871 | } | |
1872 | ||
1873 | /* | |
1874 | * imx stream on/off | |
1875 | */ | |
1876 | static int imx_s_stream(struct v4l2_subdev *sd, int enable) | |
1877 | { | |
1878 | int ret; | |
1879 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1880 | struct imx_device *dev = to_imx_sensor(sd); | |
1881 | ||
1882 | mutex_lock(&dev->input_lock); | |
1883 | if (enable) { | |
1884 | /* Noise reduction & dead pixel applied before streaming */ | |
1885 | if (dev->fw == NULL) { | |
1886 | dev_warn(&client->dev, "No MSR loaded from library"); | |
1887 | } else { | |
1888 | ret = apply_msr_data(client, dev->fw); | |
1889 | if (ret) { | |
1890 | mutex_unlock(&dev->input_lock); | |
1891 | return ret; | |
1892 | } | |
1893 | } | |
1894 | ret = imx_test_pattern(sd); | |
1895 | if (ret) { | |
1896 | v4l2_err(client, "Configure test pattern failed.\n"); | |
1897 | mutex_unlock(&dev->input_lock); | |
1898 | return ret; | |
1899 | } | |
1900 | __imx_print_timing(sd); | |
1901 | ret = imx_write_reg_array(client, imx_streaming); | |
1902 | if (ret != 0) { | |
1903 | v4l2_err(client, "write_reg_array err\n"); | |
1904 | mutex_unlock(&dev->input_lock); | |
1905 | return ret; | |
1906 | } | |
1907 | dev->streaming = 1; | |
1908 | if (dev->vcm_driver && dev->vcm_driver->t_focus_abs_init) | |
1909 | dev->vcm_driver->t_focus_abs_init(sd); | |
1910 | } else { | |
1911 | ret = imx_write_reg_array(client, imx_soft_standby); | |
1912 | if (ret != 0) { | |
1913 | v4l2_err(client, "write_reg_array err\n"); | |
1914 | mutex_unlock(&dev->input_lock); | |
1915 | return ret; | |
1916 | } | |
1917 | dev->streaming = 0; | |
1918 | dev->targetfps = 0; | |
1919 | } | |
1920 | mutex_unlock(&dev->input_lock); | |
1921 | ||
1922 | return 0; | |
1923 | } | |
1924 | ||
1925 | static int __update_imx_device_settings(struct imx_device *dev, u16 sensor_id) | |
1926 | { | |
1927 | /* IMX on other platform is not supported yet */ | |
1928 | return -EINVAL; | |
1929 | } | |
1930 | ||
1931 | static int imx_s_config(struct v4l2_subdev *sd, | |
1932 | int irq, void *pdata) | |
1933 | { | |
1934 | struct imx_device *dev = to_imx_sensor(sd); | |
1935 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1936 | u8 sensor_revision; | |
1937 | u16 sensor_id; | |
1938 | int ret; | |
1939 | if (pdata == NULL) | |
1940 | return -ENODEV; | |
1941 | ||
1942 | dev->platform_data = pdata; | |
1943 | ||
1944 | mutex_lock(&dev->input_lock); | |
1945 | ||
1946 | if (dev->platform_data->platform_init) { | |
1947 | ret = dev->platform_data->platform_init(client); | |
1948 | if (ret) { | |
1949 | mutex_unlock(&dev->input_lock); | |
1950 | dev_err(&client->dev, "imx platform init err\n"); | |
1951 | return ret; | |
1952 | } | |
1953 | } | |
1954 | /* | |
1955 | * power off the module first. | |
1956 | * | |
1957 | * As first power on by board have undecided state of power/gpio pins. | |
1958 | */ | |
1959 | ret = __imx_s_power(sd, 0); | |
1960 | if (ret) { | |
1961 | v4l2_err(client, "imx power-down err.\n"); | |
1962 | mutex_unlock(&dev->input_lock); | |
1963 | return ret; | |
1964 | } | |
1965 | ||
1966 | ret = __imx_s_power(sd, 1); | |
1967 | if (ret) { | |
1968 | v4l2_err(client, "imx power-up err.\n"); | |
1969 | mutex_unlock(&dev->input_lock); | |
1970 | return ret; | |
1971 | } | |
1972 | ||
1973 | ret = dev->platform_data->csi_cfg(sd, 1); | |
1974 | if (ret) | |
1975 | goto fail_csi_cfg; | |
1976 | ||
1977 | /* config & detect sensor */ | |
1978 | ret = imx_detect(client, &sensor_id, &sensor_revision); | |
1979 | if (ret) { | |
1980 | v4l2_err(client, "imx_detect err s_config.\n"); | |
1981 | goto fail_detect; | |
1982 | } | |
1983 | ||
1984 | dev->sensor_id = sensor_id; | |
1985 | dev->sensor_revision = sensor_revision; | |
1986 | ||
1987 | /* Resolution settings depend on sensor type and platform */ | |
1988 | ret = __update_imx_device_settings(dev, dev->sensor_id); | |
1989 | if (ret) | |
1990 | goto fail_detect; | |
1991 | /* Read sensor's OTP data */ | |
1992 | dev->otp_data = dev->otp_driver->otp_read(sd, | |
1993 | dev->otp_driver->dev_addr, dev->otp_driver->start_addr, | |
1994 | dev->otp_driver->size); | |
1995 | ||
1996 | /* power off sensor */ | |
1997 | ret = __imx_s_power(sd, 0); | |
1998 | ||
1999 | mutex_unlock(&dev->input_lock); | |
2000 | if (ret) | |
2001 | v4l2_err(client, "imx power-down err.\n"); | |
2002 | ||
2003 | return ret; | |
2004 | ||
2005 | fail_detect: | |
2006 | dev->platform_data->csi_cfg(sd, 0); | |
2007 | fail_csi_cfg: | |
2008 | __imx_s_power(sd, 0); | |
2009 | if (dev->platform_data->platform_deinit) | |
2010 | dev->platform_data->platform_deinit(); | |
2011 | mutex_unlock(&dev->input_lock); | |
2012 | dev_err(&client->dev, "sensor power-gating failed\n"); | |
2013 | return ret; | |
2014 | } | |
2015 | ||
2016 | static int | |
2017 | imx_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, | |
2018 | struct v4l2_subdev_mbus_code_enum *code) | |
2019 | { | |
2020 | struct imx_device *dev = to_imx_sensor(sd); | |
2021 | if (code->index >= MAX_FMTS) | |
2022 | return -EINVAL; | |
2023 | ||
2024 | mutex_lock(&dev->input_lock); | |
2025 | code->code = dev->format.code; | |
2026 | mutex_unlock(&dev->input_lock); | |
2027 | return 0; | |
2028 | } | |
2029 | ||
2030 | static int | |
2031 | imx_enum_frame_size(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, | |
2032 | struct v4l2_subdev_frame_size_enum *fse) | |
2033 | { | |
2034 | int index = fse->index; | |
2035 | struct imx_device *dev = to_imx_sensor(sd); | |
2036 | ||
2037 | mutex_lock(&dev->input_lock); | |
2038 | if (index >= dev->entries_curr_table) { | |
2039 | mutex_unlock(&dev->input_lock); | |
2040 | return -EINVAL; | |
2041 | } | |
2042 | ||
2043 | fse->min_width = dev->curr_res_table[index].width; | |
2044 | fse->min_height = dev->curr_res_table[index].height; | |
2045 | fse->max_width = dev->curr_res_table[index].width; | |
2046 | fse->max_height = dev->curr_res_table[index].height; | |
2047 | mutex_unlock(&dev->input_lock); | |
2048 | return 0; | |
2049 | } | |
2050 | ||
2051 | static int | |
2052 | imx_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *param) | |
2053 | { | |
2054 | struct imx_device *dev = to_imx_sensor(sd); | |
2055 | ||
2056 | mutex_lock(&dev->input_lock); | |
2057 | dev->run_mode = param->parm.capture.capturemode; | |
2058 | ||
2059 | switch (dev->run_mode) { | |
2060 | case CI_MODE_VIDEO: | |
2061 | dev->curr_res_table = dev->mode_tables->res_video; | |
2062 | dev->entries_curr_table = dev->mode_tables->n_res_video; | |
2063 | break; | |
2064 | case CI_MODE_STILL_CAPTURE: | |
2065 | dev->curr_res_table = dev->mode_tables->res_still; | |
2066 | dev->entries_curr_table = dev->mode_tables->n_res_still; | |
2067 | break; | |
2068 | default: | |
2069 | dev->curr_res_table = dev->mode_tables->res_preview; | |
2070 | dev->entries_curr_table = dev->mode_tables->n_res_preview; | |
2071 | } | |
2072 | mutex_unlock(&dev->input_lock); | |
2073 | return 0; | |
2074 | } | |
2075 | ||
2f05db96 | 2076 | static int imx_g_frame_interval(struct v4l2_subdev *sd, |
a49d2536 AC |
2077 | struct v4l2_subdev_frame_interval *interval) |
2078 | { | |
2079 | struct imx_device *dev = to_imx_sensor(sd); | |
2080 | ||
2081 | mutex_lock(&dev->input_lock); | |
2082 | interval->interval.denominator = dev->fps; | |
2083 | interval->interval.numerator = 1; | |
2084 | mutex_unlock(&dev->input_lock); | |
2085 | return 0; | |
2086 | } | |
2087 | ||
2088 | static int __imx_s_frame_interval(struct v4l2_subdev *sd, | |
2089 | struct v4l2_subdev_frame_interval *interval) | |
2090 | { | |
2091 | struct imx_device *dev = to_imx_sensor(sd); | |
2092 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
2093 | const struct imx_resolution *res = | |
2094 | &dev->curr_res_table[dev->fmt_idx]; | |
2095 | struct camera_mipi_info *imx_info = NULL; | |
2096 | unsigned short pixels_per_line; | |
2097 | unsigned short lines_per_frame; | |
2098 | unsigned int fps_index; | |
2099 | int fps; | |
2100 | int ret = 0; | |
2101 | ||
2102 | ||
2103 | imx_info = v4l2_get_subdev_hostdata(sd); | |
2104 | if (imx_info == NULL) | |
2105 | return -EINVAL; | |
2106 | ||
2107 | if (!interval->interval.numerator) | |
2108 | interval->interval.numerator = 1; | |
2109 | ||
2110 | fps = interval->interval.denominator / interval->interval.numerator; | |
2111 | ||
2112 | if (!fps) | |
2113 | return -EINVAL; | |
2114 | ||
2115 | dev->targetfps = fps; | |
2116 | /* No need to proceed further if we are not streaming */ | |
2117 | if (!dev->streaming) | |
2118 | return 0; | |
2119 | ||
2120 | /* Ignore if we are already using the required FPS. */ | |
2121 | if (fps == dev->fps) | |
2122 | return 0; | |
2123 | ||
2124 | /* | |
2125 | * Start here, sensor is already streaming, so adjust fps dynamically | |
2126 | */ | |
2127 | fps_index = __imx_above_nearest_fps_index(fps, res->fps_options); | |
2128 | if (fps > res->fps_options[fps_index].fps) { | |
2129 | /* | |
2130 | * if does not have high fps setting, not support increase fps | |
2131 | * by adjust lines per frame. | |
2132 | */ | |
2133 | dev_err(&client->dev, "Could not support fps: %d.\n", fps); | |
2134 | return -EINVAL; | |
2135 | } | |
2136 | ||
2137 | if (res->fps_options[fps_index].regs && | |
2138 | res->fps_options[fps_index].regs != dev->regs) { | |
2139 | /* | |
2140 | * if need a new setting, but the new setting has difference | |
2141 | * with current setting, not use this one, as may have | |
2142 | * unexpected result, e.g. PLL, IQ. | |
2143 | */ | |
2144 | dev_dbg(&client->dev, | |
2145 | "Sensor is streaming, not apply new sensor setting\n"); | |
2146 | if (fps > res->fps_options[dev->fps_index].fps) { | |
2147 | /* | |
2148 | * Does not support increase fps based on low fps | |
2149 | * setting, as the high fps setting could not be used, | |
2150 | * and fps requested is above current setting fps. | |
2151 | */ | |
2152 | dev_warn(&client->dev, | |
2153 | "Could not support fps: %d, keep current: %d.\n", | |
2154 | fps, dev->fps); | |
2155 | return 0; | |
2156 | } | |
2157 | } else { | |
2158 | dev->fps_index = fps_index; | |
2159 | dev->fps = res->fps_options[dev->fps_index].fps; | |
2160 | } | |
2161 | ||
2162 | /* Update the new frametimings based on FPS */ | |
2163 | pixels_per_line = res->fps_options[dev->fps_index].pixels_per_line; | |
2164 | lines_per_frame = res->fps_options[dev->fps_index].lines_per_frame; | |
2165 | ||
2166 | if (fps > res->fps_options[fps_index].fps) { | |
2167 | /* | |
2168 | * if does not have high fps setting, not support increase fps | |
2169 | * by adjust lines per frame. | |
2170 | */ | |
2171 | dev_warn(&client->dev, "Could not support fps: %d. Use:%d.\n", | |
2172 | fps, res->fps_options[fps_index].fps); | |
2173 | goto done; | |
2174 | } | |
2175 | ||
2176 | /* if the new setting does not match exactly */ | |
2177 | if (dev->fps != fps) { | |
2178 | #define MAX_LINES_PER_FRAME 0xffff | |
2179 | dev_dbg(&client->dev, "adjusting fps using lines_per_frame\n"); | |
2180 | /* | |
2181 | * FIXME! | |
2182 | * 1: check DS on max value of lines_per_frame | |
2183 | * 2: consider use pixel per line for more range? | |
2184 | */ | |
2185 | if (dev->lines_per_frame * dev->fps / fps > | |
2186 | MAX_LINES_PER_FRAME) { | |
2187 | dev_warn(&client->dev, | |
2188 | "adjust lines_per_frame out of range, try to use max value.\n"); | |
2189 | lines_per_frame = MAX_LINES_PER_FRAME; | |
2190 | } else { | |
2191 | lines_per_frame = lines_per_frame * dev->fps / fps; | |
2192 | } | |
2193 | } | |
2194 | done: | |
2195 | /* Update the new frametimings based on FPS */ | |
2196 | dev->pixels_per_line = pixels_per_line; | |
2197 | dev->lines_per_frame = lines_per_frame; | |
2198 | ||
2199 | /* Update the new values so that user side knows the current settings */ | |
2200 | ret = __imx_update_exposure_timing(client, | |
2201 | dev->coarse_itg, dev->pixels_per_line, dev->lines_per_frame); | |
2202 | if (ret) | |
2203 | return ret; | |
2204 | ||
2205 | dev->fps = fps; | |
2206 | ||
2207 | ret = imx_get_intg_factor(client, imx_info, dev->regs); | |
2208 | if (ret) | |
2209 | return ret; | |
2210 | ||
2211 | interval->interval.denominator = res->fps_options[dev->fps_index].fps; | |
2212 | interval->interval.numerator = 1; | |
2213 | __imx_print_timing(sd); | |
2214 | ||
2215 | return ret; | |
2216 | } | |
2217 | ||
2218 | static int imx_s_frame_interval(struct v4l2_subdev *sd, | |
2219 | struct v4l2_subdev_frame_interval *interval) | |
2220 | { | |
2221 | struct imx_device *dev = to_imx_sensor(sd); | |
2222 | int ret; | |
2223 | ||
2224 | mutex_lock(&dev->input_lock); | |
2225 | ret = __imx_s_frame_interval(sd, interval); | |
2226 | mutex_unlock(&dev->input_lock); | |
2227 | ||
2228 | return ret; | |
2229 | } | |
2230 | static int imx_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) | |
2231 | { | |
2232 | struct imx_device *dev = to_imx_sensor(sd); | |
2233 | ||
2234 | mutex_lock(&dev->input_lock); | |
2235 | *frames = dev->curr_res_table[dev->fmt_idx].skip_frames; | |
2236 | mutex_unlock(&dev->input_lock); | |
2237 | ||
2238 | return 0; | |
2239 | } | |
2240 | ||
2241 | static const struct v4l2_subdev_sensor_ops imx_sensor_ops = { | |
2242 | .g_skip_frames = imx_g_skip_frames, | |
2243 | }; | |
2244 | ||
2245 | static const struct v4l2_subdev_video_ops imx_video_ops = { | |
2246 | .s_stream = imx_s_stream, | |
2247 | .s_parm = imx_s_parm, | |
2248 | .g_frame_interval = imx_g_frame_interval, | |
2249 | .s_frame_interval = imx_s_frame_interval, | |
2250 | }; | |
2251 | ||
2252 | static const struct v4l2_subdev_core_ops imx_core_ops = { | |
2253 | .s_power = imx_s_power, | |
2254 | .ioctl = imx_ioctl, | |
2255 | .init = imx_init, | |
2256 | }; | |
2257 | ||
2258 | static const struct v4l2_subdev_pad_ops imx_pad_ops = { | |
2259 | .enum_mbus_code = imx_enum_mbus_code, | |
2260 | .enum_frame_size = imx_enum_frame_size, | |
2261 | .get_fmt = imx_get_fmt, | |
2262 | .set_fmt = imx_set_fmt, | |
2263 | }; | |
2264 | ||
2265 | static const struct v4l2_subdev_ops imx_ops = { | |
2266 | .core = &imx_core_ops, | |
2267 | .video = &imx_video_ops, | |
2268 | .pad = &imx_pad_ops, | |
2269 | .sensor = &imx_sensor_ops, | |
2270 | }; | |
2271 | ||
2272 | static const struct media_entity_operations imx_entity_ops = { | |
2273 | .link_setup = NULL, | |
2274 | }; | |
2275 | ||
2276 | static int imx_remove(struct i2c_client *client) | |
2277 | { | |
2278 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
2279 | struct imx_device *dev = to_imx_sensor(sd); | |
2280 | ||
2281 | if (dev->platform_data->platform_deinit) | |
2282 | dev->platform_data->platform_deinit(); | |
2283 | ||
2284 | media_entity_cleanup(&dev->sd.entity); | |
2285 | v4l2_ctrl_handler_free(&dev->ctrl_handler); | |
2286 | dev->platform_data->csi_cfg(sd, 0); | |
2287 | v4l2_device_unregister_subdev(sd); | |
2288 | release_msr_list(client, dev->fw); | |
2289 | kfree(dev); | |
2290 | ||
2291 | return 0; | |
2292 | } | |
2293 | ||
2294 | static int __imx_init_ctrl_handler(struct imx_device *dev) | |
2295 | { | |
2296 | struct v4l2_ctrl_handler *hdl; | |
2297 | int i; | |
2298 | ||
2299 | hdl = &dev->ctrl_handler; | |
2300 | ||
2301 | v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(imx_controls)); | |
2302 | ||
2303 | for (i = 0; i < ARRAY_SIZE(imx_controls); i++) | |
2304 | v4l2_ctrl_new_custom(&dev->ctrl_handler, | |
2305 | &imx_controls[i], NULL); | |
2306 | ||
2307 | dev->pixel_rate = v4l2_ctrl_find(&dev->ctrl_handler, | |
2308 | V4L2_CID_PIXEL_RATE); | |
2309 | dev->h_blank = v4l2_ctrl_find(&dev->ctrl_handler, | |
2310 | V4L2_CID_HBLANK); | |
2311 | dev->v_blank = v4l2_ctrl_find(&dev->ctrl_handler, | |
2312 | V4L2_CID_VBLANK); | |
2313 | dev->link_freq = v4l2_ctrl_find(&dev->ctrl_handler, | |
2314 | V4L2_CID_LINK_FREQ); | |
2315 | dev->h_flip = v4l2_ctrl_find(&dev->ctrl_handler, | |
2316 | V4L2_CID_HFLIP); | |
2317 | dev->v_flip = v4l2_ctrl_find(&dev->ctrl_handler, | |
2318 | V4L2_CID_VFLIP); | |
2319 | dev->tp_mode = v4l2_ctrl_find(&dev->ctrl_handler, | |
2320 | V4L2_CID_TEST_PATTERN); | |
2321 | dev->tp_r = v4l2_ctrl_find(&dev->ctrl_handler, | |
2322 | V4L2_CID_TEST_PATTERN_COLOR_R); | |
2323 | dev->tp_gr = v4l2_ctrl_find(&dev->ctrl_handler, | |
2324 | V4L2_CID_TEST_PATTERN_COLOR_GR); | |
2325 | dev->tp_gb = v4l2_ctrl_find(&dev->ctrl_handler, | |
2326 | V4L2_CID_TEST_PATTERN_COLOR_GB); | |
2327 | dev->tp_b = v4l2_ctrl_find(&dev->ctrl_handler, | |
2328 | V4L2_CID_TEST_PATTERN_COLOR_B); | |
2329 | ||
2330 | if (dev->ctrl_handler.error || dev->pixel_rate == NULL | |
2331 | || dev->h_blank == NULL || dev->v_blank == NULL | |
2332 | || dev->h_flip == NULL || dev->v_flip == NULL | |
2333 | || dev->link_freq == NULL) { | |
2334 | return dev->ctrl_handler.error; | |
2335 | } | |
2336 | ||
2337 | dev->ctrl_handler.lock = &dev->input_lock; | |
2338 | dev->sd.ctrl_handler = hdl; | |
2339 | v4l2_ctrl_handler_setup(&dev->ctrl_handler); | |
2340 | ||
2341 | return 0; | |
2342 | } | |
2343 | ||
2344 | static void imx_update_reg_info(struct imx_device *dev) | |
2345 | { | |
2346 | if (dev->sensor_id == IMX219_ID) { | |
2347 | dev->reg_addr = &imx219_addr; | |
2348 | dev->param_hold = imx219_param_hold; | |
2349 | dev->param_update = imx219_param_update; | |
2350 | } else { | |
2351 | dev->reg_addr = &imx_addr; | |
2352 | dev->param_hold = imx_param_hold; | |
2353 | dev->param_update = imx_param_update; | |
2354 | } | |
2355 | } | |
2356 | ||
2357 | static int imx_probe(struct i2c_client *client, | |
2358 | const struct i2c_device_id *id) | |
2359 | { | |
2360 | struct imx_device *dev; | |
2361 | struct camera_mipi_info *imx_info = NULL; | |
2362 | int ret; | |
2363 | char *msr_file_name = NULL; | |
2364 | ||
2365 | /* allocate sensor device & init sub device */ | |
2366 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
2367 | if (!dev) { | |
2368 | v4l2_err(client, "%s: out of memory\n", __func__); | |
2369 | return -ENOMEM; | |
2370 | } | |
2371 | ||
2372 | mutex_init(&dev->input_lock); | |
2373 | ||
2374 | dev->i2c_id = id->driver_data; | |
2375 | dev->fmt_idx = 0; | |
2376 | dev->sensor_id = IMX_ID_DEFAULT; | |
2377 | dev->vcm_driver = &imx_vcms[IMX_ID_DEFAULT]; | |
2378 | dev->digital_gain = 256; | |
2379 | ||
2380 | v4l2_i2c_subdev_init(&(dev->sd), client, &imx_ops); | |
2381 | ||
2382 | if (client->dev.platform_data) { | |
2383 | ret = imx_s_config(&dev->sd, client->irq, | |
2384 | client->dev.platform_data); | |
2385 | if (ret) | |
2386 | goto out_free; | |
2387 | } | |
2388 | imx_info = v4l2_get_subdev_hostdata(&dev->sd); | |
2389 | ||
2390 | /* | |
2391 | * sd->name is updated with sensor driver name by the v4l2. | |
2392 | * change it to sensor name in this case. | |
2393 | */ | |
2394 | imx_update_reg_info(dev); | |
2395 | snprintf(dev->sd.name, sizeof(dev->sd.name), "%s%x %d-%04x", | |
2396 | IMX_SUBDEV_PREFIX, dev->sensor_id, | |
2397 | i2c_adapter_id(client->adapter), client->addr); | |
2398 | ||
2399 | ret = __imx_init_ctrl_handler(dev); | |
2400 | if (ret) | |
2401 | goto out_ctrl_handler_free; | |
2402 | ||
2403 | dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; | |
2404 | dev->pad.flags = MEDIA_PAD_FL_SOURCE; | |
2405 | dev->format.code = imx_translate_bayer_order( | |
2406 | imx_info->raw_bayer_order); | |
2407 | dev->sd.entity.ops = &imx_entity_ops; | |
2408 | dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; | |
2409 | ||
2410 | ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); | |
2411 | if (ret) { | |
2412 | imx_remove(client); | |
2413 | return ret; | |
2414 | } | |
2415 | ||
2416 | /* Load the Noise reduction, Dead pixel registers from cpf file*/ | |
2417 | if (dev->platform_data->msr_file_name != NULL) | |
2418 | msr_file_name = dev->platform_data->msr_file_name(); | |
2419 | if (msr_file_name) { | |
2420 | ret = load_msr_list(client, msr_file_name, &dev->fw); | |
2421 | if (ret) { | |
2422 | imx_remove(client); | |
2423 | return ret; | |
2424 | } | |
2425 | } else { | |
2426 | dev_warn(&client->dev, "Drvb file not present"); | |
2427 | } | |
2428 | ||
2429 | return ret; | |
2430 | ||
2431 | out_ctrl_handler_free: | |
2432 | v4l2_ctrl_handler_free(&dev->ctrl_handler); | |
2433 | ||
2434 | out_free: | |
2435 | v4l2_device_unregister_subdev(&dev->sd); | |
2436 | kfree(dev); | |
2437 | return ret; | |
2438 | } | |
2439 | ||
2440 | static const struct i2c_device_id imx_ids[] = { | |
2441 | {IMX_NAME_175, IMX175_ID}, | |
2442 | {IMX_NAME_135, IMX135_ID}, | |
2443 | {IMX_NAME_135_FUJI, IMX135_FUJI_ID}, | |
2444 | {IMX_NAME_134, IMX134_ID}, | |
2445 | {IMX_NAME_132, IMX132_ID}, | |
2446 | {IMX_NAME_208, IMX208_ID}, | |
2447 | {IMX_NAME_219, IMX219_ID}, | |
2448 | {IMX_NAME_227, IMX227_ID}, | |
2449 | {} | |
2450 | }; | |
2451 | ||
2452 | MODULE_DEVICE_TABLE(i2c, imx_ids); | |
2453 | ||
2454 | static struct i2c_driver imx_driver = { | |
2455 | .driver = { | |
a49d2536 AC |
2456 | .name = IMX_DRIVER, |
2457 | }, | |
2458 | .probe = imx_probe, | |
2459 | .remove = imx_remove, | |
2460 | .id_table = imx_ids, | |
2461 | }; | |
2462 | ||
2463 | static __init int init_imx(void) | |
2464 | { | |
2465 | return i2c_add_driver(&imx_driver); | |
2466 | } | |
2467 | ||
2468 | static __exit void exit_imx(void) | |
2469 | { | |
2470 | i2c_del_driver(&imx_driver); | |
2471 | } | |
2472 | ||
2473 | module_init(init_imx); | |
2474 | module_exit(exit_imx); | |
2475 | ||
2476 | MODULE_DESCRIPTION("A low-level driver for Sony IMX sensors"); | |
2477 | MODULE_AUTHOR("Shenbo Huang <shenbo.huang@intel.com>"); | |
2478 | MODULE_LICENSE("GPL"); | |
2479 |