]>
Commit | Line | Data |
---|---|---|
a49d2536 AC |
1 | /* |
2 | * Support for OmniVision OV5693 1080p HD camera sensor. | |
3 | * | |
4 | * Copyright (c) 2013 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/delay.h> | |
32 | #include <linux/slab.h> | |
33 | #include <linux/i2c.h> | |
34 | #include <linux/gpio.h> | |
35 | #include <linux/moduleparam.h> | |
36 | #include <media/v4l2-device.h> | |
37 | #include <linux/io.h> | |
38 | #include <linux/acpi.h> | |
25016567 | 39 | #include "../../include/linux/atomisp_gmin_platform.h" |
a49d2536 AC |
40 | |
41 | #include "ov5693.h" | |
42 | #include "ad5823.h" | |
43 | ||
44 | #define __cci_delay(t) \ | |
45 | do { \ | |
46 | if ((t) < 10) { \ | |
47 | usleep_range((t) * 1000, ((t) + 1) * 1000); \ | |
48 | } else { \ | |
49 | msleep((t)); \ | |
50 | } \ | |
51 | } while (0) | |
52 | ||
53 | /* Value 30ms reached through experimentation on byt ecs. | |
54 | * The DS specifies a much lower value but when using a smaller value | |
55 | * the I2C bus sometimes locks up permanently when starting the camera. | |
56 | * This issue could not be reproduced on cht, so we can reduce the | |
57 | * delay value to a lower value when insmod. | |
58 | */ | |
59 | static uint up_delay = 30; | |
60 | module_param(up_delay, uint, 0644); | |
61 | MODULE_PARM_DESC(up_delay, "Delay prior to the first CCI transaction for ov5693"); | |
62 | ||
63 | static int vcm_ad_i2c_wr8(struct i2c_client *client, u8 reg, u8 val) | |
64 | { | |
65 | int err; | |
66 | struct i2c_msg msg; | |
67 | u8 buf[2]; | |
68 | ||
69 | buf[0] = reg; | |
70 | buf[1] = val; | |
71 | ||
72 | msg.addr = VCM_ADDR; | |
73 | msg.flags = 0; | |
74 | msg.len = 2; | |
75 | msg.buf = &buf[0]; | |
76 | ||
77 | err = i2c_transfer(client->adapter, &msg, 1); | |
78 | if (err != 1) { | |
79 | dev_err(&client->dev, "%s: vcm i2c fail, err code = %d\n", | |
80 | __func__, err); | |
81 | return -EIO; | |
82 | } | |
83 | return 0; | |
84 | } | |
a49d2536 | 85 | |
a49d2536 AC |
86 | static int ad5823_i2c_write(struct i2c_client *client, u8 reg, u8 val) |
87 | { | |
88 | struct i2c_msg msg; | |
89 | u8 buf[2]; | |
90 | buf[0] = reg; | |
91 | buf[1] = val; | |
92 | msg.addr = AD5823_VCM_ADDR; | |
93 | msg.flags = 0; | |
94 | msg.len = 0x02; | |
95 | msg.buf = &buf[0]; | |
96 | ||
97 | if (i2c_transfer(client->adapter, &msg, 1) != 1) | |
98 | return -EIO; | |
99 | return 0; | |
100 | } | |
101 | ||
102 | static int ad5823_i2c_read(struct i2c_client *client, u8 reg, u8 *val) | |
103 | { | |
104 | struct i2c_msg msg[2]; | |
105 | u8 buf[2]; | |
106 | buf[0] = reg; | |
107 | buf[1] = 0; | |
108 | ||
109 | msg[0].addr = AD5823_VCM_ADDR; | |
110 | msg[0].flags = 0; | |
111 | msg[0].len = 0x01; | |
112 | msg[0].buf = &buf[0]; | |
113 | ||
114 | msg[1].addr = 0x0c; | |
115 | msg[1].flags = I2C_M_RD; | |
116 | msg[1].len = 0x01; | |
117 | msg[1].buf = &buf[1]; | |
118 | *val = 0; | |
119 | if (i2c_transfer(client->adapter, msg, 2) != 2) | |
120 | return -EIO; | |
121 | *val = buf[1]; | |
122 | return 0; | |
123 | } | |
124 | ||
125 | ||
126 | static const uint32_t ov5693_embedded_effective_size = 28; | |
127 | ||
128 | /* i2c read/write stuff */ | |
129 | static int ov5693_read_reg(struct i2c_client *client, | |
130 | u16 data_length, u16 reg, u16 *val) | |
131 | { | |
132 | int err; | |
133 | struct i2c_msg msg[2]; | |
134 | unsigned char data[6]; | |
135 | ||
136 | if (!client->adapter) { | |
137 | dev_err(&client->dev, "%s error, no client->adapter\n", | |
138 | __func__); | |
139 | return -ENODEV; | |
140 | } | |
141 | ||
142 | if (data_length != OV5693_8BIT && data_length != OV5693_16BIT | |
143 | && data_length != OV5693_32BIT) { | |
144 | dev_err(&client->dev, "%s error, invalid data length\n", | |
145 | __func__); | |
146 | return -EINVAL; | |
147 | } | |
148 | ||
14c23a51 | 149 | memset(msg, 0, sizeof(msg)); |
a49d2536 AC |
150 | |
151 | msg[0].addr = client->addr; | |
152 | msg[0].flags = 0; | |
153 | msg[0].len = I2C_MSG_LENGTH; | |
154 | msg[0].buf = data; | |
155 | ||
156 | /* high byte goes out first */ | |
157 | data[0] = (u8)(reg >> 8); | |
158 | data[1] = (u8)(reg & 0xff); | |
159 | ||
160 | msg[1].addr = client->addr; | |
161 | msg[1].len = data_length; | |
162 | msg[1].flags = I2C_M_RD; | |
163 | msg[1].buf = data; | |
164 | ||
165 | err = i2c_transfer(client->adapter, msg, 2); | |
166 | if (err != 2) { | |
167 | if (err >= 0) | |
168 | err = -EIO; | |
169 | dev_err(&client->dev, | |
170 | "read from offset 0x%x error %d", reg, err); | |
171 | return err; | |
172 | } | |
173 | ||
174 | *val = 0; | |
175 | /* high byte comes first */ | |
176 | if (data_length == OV5693_8BIT) | |
177 | *val = (u8)data[0]; | |
178 | else if (data_length == OV5693_16BIT) | |
179 | *val = be16_to_cpu(*(u16 *)&data[0]); | |
180 | else | |
181 | *val = be32_to_cpu(*(u32 *)&data[0]); | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
186 | static int ov5693_i2c_write(struct i2c_client *client, u16 len, u8 *data) | |
187 | { | |
188 | struct i2c_msg msg; | |
189 | const int num_msg = 1; | |
190 | int ret; | |
191 | ||
192 | msg.addr = client->addr; | |
193 | msg.flags = 0; | |
194 | msg.len = len; | |
195 | msg.buf = data; | |
196 | ret = i2c_transfer(client->adapter, &msg, 1); | |
197 | ||
198 | return ret == num_msg ? 0 : -EIO; | |
199 | } | |
200 | ||
201 | static int vcm_dw_i2c_write(struct i2c_client *client, u16 data) | |
202 | { | |
203 | struct i2c_msg msg; | |
204 | const int num_msg = 1; | |
205 | int ret; | |
206 | u16 val; | |
207 | ||
208 | val = cpu_to_be16(data); | |
209 | msg.addr = VCM_ADDR; | |
210 | msg.flags = 0; | |
211 | msg.len = OV5693_16BIT; | |
212 | msg.buf = (u8 *)&val; | |
213 | ||
214 | ret = i2c_transfer(client->adapter, &msg, 1); | |
215 | ||
216 | return ret == num_msg ? 0 : -EIO; | |
217 | } | |
218 | ||
219 | /* Theory: per datasheet, the two VCMs both allow for a 2-byte read. | |
220 | * The DW9714 doesn't actually specify what this does (it has a | |
221 | * two-byte write-only protocol, but specifies the read sequence as | |
222 | * legal), but it returns the same data (zeroes) always, after an | |
223 | * undocumented initial NAK. The AD5823 has a one-byte address | |
224 | * register to which all writes go, and subsequent reads will cycle | |
225 | * through the 8 bytes of registers. Notably, the default values (the | |
226 | * device is always power-cycled affirmatively, so we can rely on | |
227 | * these) in AD5823 are not pairwise repetitions of the same 16 bit | |
228 | * word. So all we have to do is sequentially read two bytes at a | |
229 | * time and see if we detect a difference in any of the first four | |
230 | * pairs. */ | |
231 | static int vcm_detect(struct i2c_client *client) | |
232 | { | |
233 | int i, ret; | |
234 | struct i2c_msg msg; | |
235 | u16 data0 = 0, data; | |
236 | for (i = 0; i < 4; i++) { | |
237 | msg.addr = VCM_ADDR; | |
238 | msg.flags = I2C_M_RD; | |
239 | msg.len = sizeof(data); | |
240 | msg.buf = (u8 *)&data; | |
241 | ret = i2c_transfer(client->adapter, &msg, 1); | |
242 | ||
243 | /* DW9714 always fails the first read and returns | |
244 | * zeroes for subsequent ones */ | |
245 | if (i == 0 && ret == -EREMOTEIO) { | |
246 | data0 = 0; | |
247 | continue; | |
248 | } | |
249 | ||
250 | if (i == 0) | |
251 | data0 = data; | |
252 | ||
253 | if (data != data0) | |
254 | return VCM_AD5823; | |
255 | } | |
256 | return ret == 1 ? VCM_DW9714 : ret; | |
257 | } | |
258 | ||
259 | static int ov5693_write_reg(struct i2c_client *client, u16 data_length, | |
260 | u16 reg, u16 val) | |
261 | { | |
262 | int ret; | |
263 | unsigned char data[4] = {0}; | |
264 | u16 *wreg = (u16 *)data; | |
265 | const u16 len = data_length + sizeof(u16); /* 16-bit address + data */ | |
266 | ||
267 | if (data_length != OV5693_8BIT && data_length != OV5693_16BIT) { | |
268 | dev_err(&client->dev, | |
269 | "%s error, invalid data_length\n", __func__); | |
270 | return -EINVAL; | |
271 | } | |
272 | ||
273 | /* high byte goes out first */ | |
274 | *wreg = cpu_to_be16(reg); | |
275 | ||
276 | if (data_length == OV5693_8BIT) { | |
277 | data[2] = (u8)(val); | |
278 | } else { | |
279 | /* OV5693_16BIT */ | |
280 | u16 *wdata = (u16 *)&data[2]; | |
281 | *wdata = cpu_to_be16(val); | |
282 | } | |
283 | ||
284 | ret = ov5693_i2c_write(client, len, data); | |
285 | if (ret) | |
286 | dev_err(&client->dev, | |
287 | "write error: wrote 0x%x to offset 0x%x error %d", | |
288 | val, reg, ret); | |
289 | ||
290 | return ret; | |
291 | } | |
292 | ||
293 | /* | |
294 | * ov5693_write_reg_array - Initializes a list of OV5693 registers | |
295 | * @client: i2c driver client structure | |
296 | * @reglist: list of registers to be written | |
297 | * | |
298 | * This function initializes a list of registers. When consecutive addresses | |
299 | * are found in a row on the list, this function creates a buffer and sends | |
300 | * consecutive data in a single i2c_transfer(). | |
301 | * | |
302 | * __ov5693_flush_reg_array, __ov5693_buf_reg_array() and | |
303 | * __ov5693_write_reg_is_consecutive() are internal functions to | |
304 | * ov5693_write_reg_array_fast() and should be not used anywhere else. | |
305 | * | |
306 | */ | |
307 | ||
308 | static int __ov5693_flush_reg_array(struct i2c_client *client, | |
309 | struct ov5693_write_ctrl *ctrl) | |
310 | { | |
311 | u16 size; | |
312 | ||
313 | if (ctrl->index == 0) | |
314 | return 0; | |
315 | ||
316 | size = sizeof(u16) + ctrl->index; /* 16-bit address + data */ | |
317 | ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr); | |
318 | ctrl->index = 0; | |
319 | ||
320 | return ov5693_i2c_write(client, size, (u8 *)&ctrl->buffer); | |
321 | } | |
322 | ||
323 | static int __ov5693_buf_reg_array(struct i2c_client *client, | |
324 | struct ov5693_write_ctrl *ctrl, | |
325 | const struct ov5693_reg *next) | |
326 | { | |
327 | int size; | |
328 | u16 *data16; | |
329 | ||
330 | switch (next->type) { | |
331 | case OV5693_8BIT: | |
332 | size = 1; | |
333 | ctrl->buffer.data[ctrl->index] = (u8)next->val; | |
334 | break; | |
335 | case OV5693_16BIT: | |
336 | size = 2; | |
337 | data16 = (u16 *)&ctrl->buffer.data[ctrl->index]; | |
338 | *data16 = cpu_to_be16((u16)next->val); | |
339 | break; | |
340 | default: | |
341 | return -EINVAL; | |
342 | } | |
343 | ||
344 | /* When first item is added, we need to store its starting address */ | |
345 | if (ctrl->index == 0) | |
346 | ctrl->buffer.addr = next->reg; | |
347 | ||
348 | ctrl->index += size; | |
349 | ||
350 | /* | |
351 | * Buffer cannot guarantee free space for u32? Better flush it to avoid | |
352 | * possible lack of memory for next item. | |
353 | */ | |
354 | if (ctrl->index + sizeof(u16) >= OV5693_MAX_WRITE_BUF_SIZE) | |
355 | return __ov5693_flush_reg_array(client, ctrl); | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | static int __ov5693_write_reg_is_consecutive(struct i2c_client *client, | |
361 | struct ov5693_write_ctrl *ctrl, | |
362 | const struct ov5693_reg *next) | |
363 | { | |
364 | if (ctrl->index == 0) | |
365 | return 1; | |
366 | ||
367 | return ctrl->buffer.addr + ctrl->index == next->reg; | |
368 | } | |
369 | ||
370 | static int ov5693_write_reg_array(struct i2c_client *client, | |
371 | const struct ov5693_reg *reglist) | |
372 | { | |
373 | const struct ov5693_reg *next = reglist; | |
374 | struct ov5693_write_ctrl ctrl; | |
375 | int err; | |
376 | ||
377 | ctrl.index = 0; | |
378 | for (; next->type != OV5693_TOK_TERM; next++) { | |
379 | switch (next->type & OV5693_TOK_MASK) { | |
380 | case OV5693_TOK_DELAY: | |
381 | err = __ov5693_flush_reg_array(client, &ctrl); | |
382 | if (err) | |
383 | return err; | |
384 | msleep(next->val); | |
385 | break; | |
386 | default: | |
387 | /* | |
388 | * If next address is not consecutive, data needs to be | |
389 | * flushed before proceed. | |
390 | */ | |
391 | if (!__ov5693_write_reg_is_consecutive(client, &ctrl, | |
392 | next)) { | |
393 | err = __ov5693_flush_reg_array(client, &ctrl); | |
394 | if (err) | |
395 | return err; | |
396 | } | |
397 | err = __ov5693_buf_reg_array(client, &ctrl, next); | |
398 | if (err) { | |
399 | dev_err(&client->dev, | |
400 | "%s: write error, aborted\n", | |
401 | __func__); | |
402 | return err; | |
403 | } | |
404 | break; | |
405 | } | |
406 | } | |
407 | ||
408 | return __ov5693_flush_reg_array(client, &ctrl); | |
409 | } | |
410 | static int ov5693_g_focal(struct v4l2_subdev *sd, s32 *val) | |
411 | { | |
412 | *val = (OV5693_FOCAL_LENGTH_NUM << 16) | OV5693_FOCAL_LENGTH_DEM; | |
413 | return 0; | |
414 | } | |
415 | ||
416 | static int ov5693_g_fnumber(struct v4l2_subdev *sd, s32 *val) | |
417 | { | |
418 | /*const f number for imx*/ | |
419 | *val = (OV5693_F_NUMBER_DEFAULT_NUM << 16) | OV5693_F_NUMBER_DEM; | |
420 | return 0; | |
421 | } | |
422 | ||
423 | static int ov5693_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) | |
424 | { | |
425 | *val = (OV5693_F_NUMBER_DEFAULT_NUM << 24) | | |
426 | (OV5693_F_NUMBER_DEM << 16) | | |
427 | (OV5693_F_NUMBER_DEFAULT_NUM << 8) | OV5693_F_NUMBER_DEM; | |
428 | return 0; | |
429 | } | |
430 | ||
431 | static int ov5693_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val) | |
432 | { | |
433 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
434 | ||
435 | *val = ov5693_res[dev->fmt_idx].bin_factor_x; | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
440 | static int ov5693_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val) | |
441 | { | |
442 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
443 | ||
444 | *val = ov5693_res[dev->fmt_idx].bin_factor_y; | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
449 | static int ov5693_get_intg_factor(struct i2c_client *client, | |
450 | struct camera_mipi_info *info, | |
451 | const struct ov5693_resolution *res) | |
452 | { | |
453 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
454 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
455 | struct atomisp_sensor_mode_data *buf = &info->data; | |
456 | unsigned int pix_clk_freq_hz; | |
457 | u16 reg_val; | |
458 | int ret; | |
459 | ||
460 | if (info == NULL) | |
461 | return -EINVAL; | |
462 | ||
463 | /* pixel clock */ | |
464 | pix_clk_freq_hz = res->pix_clk_freq * 1000000; | |
465 | ||
466 | dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz; | |
467 | buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz; | |
468 | ||
469 | /* get integration time */ | |
470 | buf->coarse_integration_time_min = OV5693_COARSE_INTG_TIME_MIN; | |
471 | buf->coarse_integration_time_max_margin = | |
472 | OV5693_COARSE_INTG_TIME_MAX_MARGIN; | |
473 | ||
474 | buf->fine_integration_time_min = OV5693_FINE_INTG_TIME_MIN; | |
475 | buf->fine_integration_time_max_margin = | |
476 | OV5693_FINE_INTG_TIME_MAX_MARGIN; | |
477 | ||
478 | buf->fine_integration_time_def = OV5693_FINE_INTG_TIME_MIN; | |
479 | buf->frame_length_lines = res->lines_per_frame; | |
480 | buf->line_length_pck = res->pixels_per_line; | |
481 | buf->read_mode = res->bin_mode; | |
482 | ||
483 | /* get the cropping and output resolution to ISP for this mode. */ | |
484 | ret = ov5693_read_reg(client, OV5693_16BIT, | |
485 | OV5693_HORIZONTAL_START_H, ®_val); | |
486 | if (ret) | |
487 | return ret; | |
488 | buf->crop_horizontal_start = reg_val; | |
489 | ||
490 | ret = ov5693_read_reg(client, OV5693_16BIT, | |
491 | OV5693_VERTICAL_START_H, ®_val); | |
492 | if (ret) | |
493 | return ret; | |
494 | buf->crop_vertical_start = reg_val; | |
495 | ||
496 | ret = ov5693_read_reg(client, OV5693_16BIT, | |
497 | OV5693_HORIZONTAL_END_H, ®_val); | |
498 | if (ret) | |
499 | return ret; | |
500 | buf->crop_horizontal_end = reg_val; | |
501 | ||
502 | ret = ov5693_read_reg(client, OV5693_16BIT, | |
503 | OV5693_VERTICAL_END_H, ®_val); | |
504 | if (ret) | |
505 | return ret; | |
506 | buf->crop_vertical_end = reg_val; | |
507 | ||
508 | ret = ov5693_read_reg(client, OV5693_16BIT, | |
509 | OV5693_HORIZONTAL_OUTPUT_SIZE_H, ®_val); | |
510 | if (ret) | |
511 | return ret; | |
512 | buf->output_width = reg_val; | |
513 | ||
514 | ret = ov5693_read_reg(client, OV5693_16BIT, | |
515 | OV5693_VERTICAL_OUTPUT_SIZE_H, ®_val); | |
516 | if (ret) | |
517 | return ret; | |
518 | buf->output_height = reg_val; | |
519 | ||
520 | buf->binning_factor_x = res->bin_factor_x ? | |
521 | res->bin_factor_x : 1; | |
522 | buf->binning_factor_y = res->bin_factor_y ? | |
523 | res->bin_factor_y : 1; | |
524 | return 0; | |
525 | } | |
526 | ||
527 | static long __ov5693_set_exposure(struct v4l2_subdev *sd, int coarse_itg, | |
528 | int gain, int digitgain) | |
529 | ||
530 | { | |
531 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
532 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
533 | u16 vts, hts; | |
534 | int ret, exp_val; | |
535 | ||
536 | hts = ov5693_res[dev->fmt_idx].pixels_per_line; | |
537 | vts = ov5693_res[dev->fmt_idx].lines_per_frame; | |
538 | /*If coarse_itg is larger than 1<<15, can not write to reg directly. | |
539 | The way is to write coarse_itg/2 to the reg, meanwhile write 2*hts | |
540 | to the reg. */ | |
541 | if (coarse_itg > (1 << 15)) { | |
542 | hts = hts * 2; | |
543 | coarse_itg = (int)coarse_itg / 2; | |
544 | } | |
545 | /* group hold */ | |
546 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
547 | OV5693_GROUP_ACCESS, 0x00); | |
548 | if (ret) { | |
549 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
550 | __func__, OV5693_GROUP_ACCESS); | |
551 | return ret; | |
552 | } | |
553 | ||
554 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
555 | OV5693_TIMING_HTS_H, (hts >> 8) & 0xFF); | |
556 | if (ret) { | |
557 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
558 | __func__, OV5693_TIMING_HTS_H); | |
559 | return ret; | |
560 | } | |
561 | ||
562 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
563 | OV5693_TIMING_HTS_L, hts & 0xFF); | |
564 | if (ret) { | |
565 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
566 | __func__, OV5693_TIMING_HTS_L); | |
567 | return ret; | |
568 | } | |
569 | /* Increase the VTS to match exposure + MARGIN */ | |
570 | if (coarse_itg > vts - OV5693_INTEGRATION_TIME_MARGIN) | |
571 | vts = (u16) coarse_itg + OV5693_INTEGRATION_TIME_MARGIN; | |
572 | ||
573 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
574 | OV5693_TIMING_VTS_H, (vts >> 8) & 0xFF); | |
575 | if (ret) { | |
576 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
577 | __func__, OV5693_TIMING_VTS_H); | |
578 | return ret; | |
579 | } | |
580 | ||
581 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
582 | OV5693_TIMING_VTS_L, vts & 0xFF); | |
583 | if (ret) { | |
584 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
585 | __func__, OV5693_TIMING_VTS_L); | |
586 | return ret; | |
587 | } | |
588 | ||
589 | /* set exposure */ | |
590 | ||
591 | /* Lower four bit should be 0*/ | |
592 | exp_val = coarse_itg << 4; | |
593 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
594 | OV5693_EXPOSURE_L, exp_val & 0xFF); | |
595 | if (ret) { | |
596 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
597 | __func__, OV5693_EXPOSURE_L); | |
598 | return ret; | |
599 | } | |
600 | ||
601 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
602 | OV5693_EXPOSURE_M, (exp_val >> 8) & 0xFF); | |
603 | if (ret) { | |
604 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
605 | __func__, OV5693_EXPOSURE_M); | |
606 | return ret; | |
607 | } | |
608 | ||
609 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
610 | OV5693_EXPOSURE_H, (exp_val >> 16) & 0x0F); | |
611 | if (ret) { | |
612 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
613 | __func__, OV5693_EXPOSURE_H); | |
614 | return ret; | |
615 | } | |
616 | ||
617 | /* Analog gain */ | |
618 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
619 | OV5693_AGC_L, gain & 0xff); | |
620 | if (ret) { | |
621 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
622 | __func__, OV5693_AGC_L); | |
623 | return ret; | |
624 | } | |
625 | ||
626 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
627 | OV5693_AGC_H, (gain >> 8) & 0xff); | |
628 | if (ret) { | |
629 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
630 | __func__, OV5693_AGC_H); | |
631 | return ret; | |
632 | } | |
633 | ||
634 | /* Digital gain */ | |
635 | if (digitgain) { | |
636 | ret = ov5693_write_reg(client, OV5693_16BIT, | |
637 | OV5693_MWB_RED_GAIN_H, digitgain); | |
638 | if (ret) { | |
639 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
640 | __func__, OV5693_MWB_RED_GAIN_H); | |
641 | return ret; | |
642 | } | |
643 | ||
644 | ret = ov5693_write_reg(client, OV5693_16BIT, | |
645 | OV5693_MWB_GREEN_GAIN_H, digitgain); | |
646 | if (ret) { | |
647 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
648 | __func__, OV5693_MWB_RED_GAIN_H); | |
649 | return ret; | |
650 | } | |
651 | ||
652 | ret = ov5693_write_reg(client, OV5693_16BIT, | |
653 | OV5693_MWB_BLUE_GAIN_H, digitgain); | |
654 | if (ret) { | |
655 | dev_err(&client->dev, "%s: write %x error, aborted\n", | |
656 | __func__, OV5693_MWB_RED_GAIN_H); | |
657 | return ret; | |
658 | } | |
659 | } | |
660 | ||
661 | /* End group */ | |
662 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
663 | OV5693_GROUP_ACCESS, 0x10); | |
664 | if (ret) | |
665 | return ret; | |
666 | ||
667 | /* Delay launch group */ | |
668 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
669 | OV5693_GROUP_ACCESS, 0xa0); | |
670 | if (ret) | |
671 | return ret; | |
672 | return ret; | |
673 | } | |
674 | ||
675 | static int ov5693_set_exposure(struct v4l2_subdev *sd, int exposure, | |
676 | int gain, int digitgain) | |
677 | { | |
678 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
679 | int ret; | |
680 | ||
681 | mutex_lock(&dev->input_lock); | |
682 | ret = __ov5693_set_exposure(sd, exposure, gain, digitgain); | |
683 | mutex_unlock(&dev->input_lock); | |
684 | ||
685 | return ret; | |
686 | } | |
687 | ||
688 | static long ov5693_s_exposure(struct v4l2_subdev *sd, | |
689 | struct atomisp_exposure *exposure) | |
690 | { | |
691 | u16 coarse_itg = exposure->integration_time[0]; | |
692 | u16 analog_gain = exposure->gain[0]; | |
693 | u16 digital_gain = exposure->gain[1]; | |
694 | ||
695 | /* we should not accept the invalid value below */ | |
696 | if (analog_gain == 0) { | |
697 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
698 | v4l2_err(client, "%s: invalid value\n", __func__); | |
699 | return -EINVAL; | |
700 | } | |
701 | return ov5693_set_exposure(sd, coarse_itg, analog_gain, digital_gain); | |
702 | } | |
703 | ||
704 | static int ov5693_read_otp_reg_array(struct i2c_client *client, u16 size, | |
14c23a51 | 705 | u16 addr, u8 *buf) |
a49d2536 AC |
706 | { |
707 | u16 index; | |
708 | int ret; | |
e1b28f13 | 709 | u16 *pVal = NULL; |
a49d2536 AC |
710 | |
711 | for (index = 0; index <= size; index++) { | |
712 | pVal = (u16 *) (buf + index); | |
713 | ret = | |
714 | ov5693_read_reg(client, OV5693_8BIT, addr + index, | |
715 | pVal); | |
716 | if (ret) | |
717 | return ret; | |
718 | } | |
719 | ||
720 | return 0; | |
721 | } | |
722 | ||
14c23a51 | 723 | static int __ov5693_otp_read(struct v4l2_subdev *sd, u8 *buf) |
a49d2536 AC |
724 | { |
725 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
726 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
727 | int ret; | |
728 | int i; | |
729 | u8 *b = buf; | |
730 | dev->otp_size = 0; | |
731 | for (i = 1; i < OV5693_OTP_BANK_MAX; i++) { | |
732 | /*set bank NO and OTP read mode. */ | |
733 | ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_BANK_REG, (i | 0xc0)); //[7:6] 2'b11 [5:0] bank no | |
734 | if (ret) { | |
735 | dev_err(&client->dev, "failed to prepare OTP page\n"); | |
736 | return ret; | |
737 | } | |
738 | //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0)); | |
739 | ||
740 | /*enable read */ | |
741 | ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_READ_REG, OV5693_OTP_MODE_READ); // enable :1 | |
742 | if (ret) { | |
743 | dev_err(&client->dev, | |
744 | "failed to set OTP reading mode page"); | |
745 | return ret; | |
746 | } | |
747 | //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ); | |
748 | ||
749 | /* Reading the OTP data array */ | |
750 | ret = ov5693_read_otp_reg_array(client, OV5693_OTP_BANK_SIZE, | |
751 | OV5693_OTP_START_ADDR, | |
752 | b); | |
753 | if (ret) { | |
754 | dev_err(&client->dev, "failed to read OTP data\n"); | |
755 | return ret; | |
756 | } | |
757 | ||
758 | //pr_debug("BANK[%2d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, *b, *(b+1), *(b+2), *(b+3), *(b+4), *(b+5), *(b+6), *(b+7), *(b+8), *(b+9), *(b+10), *(b+11), *(b+12), *(b+13), *(b+14), *(b+15)); | |
759 | ||
760 | //Intel OTP map, try to read 320byts first. | |
761 | if (21 == i) { | |
762 | if ((*b) == 0) { | |
763 | dev->otp_size = 320; | |
764 | break; | |
765 | } else { | |
766 | b = buf; | |
767 | continue; | |
768 | } | |
769 | } else if (24 == i) { //if the first 320bytes data doesn't not exist, try to read the next 32bytes data. | |
770 | if ((*b) == 0) { | |
771 | dev->otp_size = 32; | |
772 | break; | |
773 | } else { | |
774 | b = buf; | |
775 | continue; | |
776 | } | |
777 | } else if (27 == i) { //if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again. | |
778 | if ((*b) == 0) { | |
779 | dev->otp_size = 32; | |
780 | break; | |
781 | } else { | |
782 | dev->otp_size = 0; // no OTP data. | |
783 | break; | |
784 | } | |
785 | } | |
786 | ||
787 | b = b + OV5693_OTP_BANK_SIZE; | |
788 | } | |
789 | return 0; | |
790 | } | |
791 | ||
792 | /* | |
793 | * Read otp data and store it into a kmalloced buffer. | |
794 | * The caller must kfree the buffer when no more needed. | |
795 | * @size: set to the size of the returned otp data. | |
796 | */ | |
797 | static void *ov5693_otp_read(struct v4l2_subdev *sd) | |
798 | { | |
799 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
800 | u8 *buf; | |
801 | int ret; | |
802 | ||
803 | buf = devm_kzalloc(&client->dev, (OV5693_OTP_DATA_SIZE + 16), GFP_KERNEL); | |
804 | if (!buf) | |
805 | return ERR_PTR(-ENOMEM); | |
806 | ||
807 | //otp valid after mipi on and sw stream on | |
808 | ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x00); | |
809 | ||
810 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
811 | OV5693_SW_STREAM, OV5693_START_STREAMING); | |
812 | ||
813 | ret = __ov5693_otp_read(sd, buf); | |
814 | ||
815 | //mipi off and sw stream off after otp read | |
816 | ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x0f); | |
817 | ||
818 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
819 | OV5693_SW_STREAM, OV5693_STOP_STREAMING); | |
820 | ||
821 | /* Driver has failed to find valid data */ | |
822 | if (ret) { | |
823 | dev_err(&client->dev, "sensor found no valid OTP data\n"); | |
824 | return ERR_PTR(ret); | |
825 | } | |
826 | ||
827 | return buf; | |
828 | } | |
829 | ||
830 | static int ov5693_g_priv_int_data(struct v4l2_subdev *sd, | |
831 | struct v4l2_private_int_data *priv) | |
832 | { | |
833 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
834 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
835 | u8 __user *to = priv->data; | |
836 | u32 read_size = priv->size; | |
837 | int ret; | |
838 | ||
839 | /* No need to copy data if size is 0 */ | |
840 | if (!read_size) | |
841 | goto out; | |
842 | ||
843 | if (IS_ERR(dev->otp_data)) { | |
844 | dev_err(&client->dev, "OTP data not available"); | |
845 | return PTR_ERR(dev->otp_data); | |
846 | } | |
847 | ||
848 | /* Correct read_size value only if bigger than maximum */ | |
849 | if (read_size > OV5693_OTP_DATA_SIZE) | |
850 | read_size = OV5693_OTP_DATA_SIZE; | |
851 | ||
852 | ret = copy_to_user(to, dev->otp_data, read_size); | |
853 | if (ret) { | |
854 | dev_err(&client->dev, "%s: failed to copy OTP data to user\n", | |
855 | __func__); | |
856 | return -EFAULT; | |
857 | } | |
858 | ||
859 | pr_debug("%s read_size:%d\n", __func__, read_size); | |
860 | ||
861 | out: | |
862 | /* Return correct size */ | |
863 | priv->size = dev->otp_size; | |
864 | ||
865 | return 0; | |
866 | ||
867 | } | |
868 | ||
869 | static long ov5693_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) | |
870 | { | |
871 | ||
872 | switch (cmd) { | |
873 | case ATOMISP_IOC_S_EXPOSURE: | |
874 | return ov5693_s_exposure(sd, arg); | |
875 | case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA: | |
876 | return ov5693_g_priv_int_data(sd, arg); | |
877 | default: | |
878 | return -EINVAL; | |
879 | } | |
880 | return 0; | |
881 | } | |
882 | ||
883 | /* This returns the exposure time being used. This should only be used | |
884 | for filling in EXIF data, not for actual image processing. */ | |
885 | static int ov5693_q_exposure(struct v4l2_subdev *sd, s32 *value) | |
886 | { | |
887 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
888 | u16 reg_v, reg_v2; | |
889 | int ret; | |
890 | ||
891 | /* get exposure */ | |
892 | ret = ov5693_read_reg(client, OV5693_8BIT, | |
893 | OV5693_EXPOSURE_L, | |
894 | ®_v); | |
895 | if (ret) | |
896 | goto err; | |
897 | ||
898 | ret = ov5693_read_reg(client, OV5693_8BIT, | |
899 | OV5693_EXPOSURE_M, | |
900 | ®_v2); | |
901 | if (ret) | |
902 | goto err; | |
903 | ||
904 | reg_v += reg_v2 << 8; | |
905 | ret = ov5693_read_reg(client, OV5693_8BIT, | |
906 | OV5693_EXPOSURE_H, | |
907 | ®_v2); | |
908 | if (ret) | |
909 | goto err; | |
910 | ||
911 | *value = reg_v + (((u32)reg_v2 << 16)); | |
912 | err: | |
913 | return ret; | |
914 | } | |
915 | ||
4a3039e2 | 916 | static int ad5823_t_focus_vcm(struct v4l2_subdev *sd, u16 val) |
a49d2536 AC |
917 | { |
918 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
919 | int ret = -EINVAL; | |
920 | u8 vcm_code; | |
921 | ||
922 | ret = ad5823_i2c_read(client, AD5823_REG_VCM_CODE_MSB, &vcm_code); | |
923 | if (ret) | |
924 | return ret; | |
925 | ||
926 | /* set reg VCM_CODE_MSB Bit[1:0] */ | |
927 | vcm_code = (vcm_code & VCM_CODE_MSB_MASK) | | |
928 | ((val >> 8) & ~VCM_CODE_MSB_MASK); | |
929 | ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, vcm_code); | |
930 | if (ret) | |
931 | return ret; | |
932 | ||
933 | /* set reg VCM_CODE_LSB Bit[7:0] */ | |
934 | ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_LSB, (val & 0xff)); | |
935 | if (ret) | |
936 | return ret; | |
937 | ||
938 | /* set required vcm move time */ | |
939 | vcm_code = AD5823_RESONANCE_PERIOD / AD5823_RESONANCE_COEF | |
940 | - AD5823_HIGH_FREQ_RANGE; | |
941 | ret = ad5823_i2c_write(client, AD5823_REG_VCM_MOVE_TIME, vcm_code); | |
942 | ||
943 | return ret; | |
944 | } | |
945 | ||
946 | int ad5823_t_focus_abs(struct v4l2_subdev *sd, s32 value) | |
947 | { | |
a49d2536 | 948 | value = min(value, AD5823_MAX_FOCUS_POS); |
34d340b7 | 949 | return ad5823_t_focus_vcm(sd, value); |
a49d2536 AC |
950 | } |
951 | ||
952 | static int ov5693_t_focus_abs(struct v4l2_subdev *sd, s32 value) | |
953 | { | |
954 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
955 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
956 | int ret = 0; | |
957 | ||
958 | dev_dbg(&client->dev, "%s: FOCUS_POS: 0x%x\n", __func__, value); | |
959 | value = clamp(value, 0, OV5693_VCM_MAX_FOCUS_POS); | |
960 | if (dev->vcm == VCM_DW9714) { | |
961 | if (dev->vcm_update) { | |
962 | ret = vcm_dw_i2c_write(client, VCM_PROTECTION_OFF); | |
963 | if (ret) | |
964 | return ret; | |
965 | ret = vcm_dw_i2c_write(client, DIRECT_VCM); | |
966 | if (ret) | |
967 | return ret; | |
968 | ret = vcm_dw_i2c_write(client, VCM_PROTECTION_ON); | |
969 | if (ret) | |
970 | return ret; | |
971 | dev->vcm_update = false; | |
972 | } | |
973 | ret = vcm_dw_i2c_write(client, | |
974 | vcm_val(value, VCM_DEFAULT_S)); | |
975 | } else if (dev->vcm == VCM_AD5823) { | |
976 | ad5823_t_focus_abs(sd, value); | |
977 | } | |
978 | if (ret == 0) { | |
979 | dev->number_of_steps = value - dev->focus; | |
980 | dev->focus = value; | |
981 | getnstimeofday(&(dev->timestamp_t_focus_abs)); | |
982 | } else | |
983 | dev_err(&client->dev, | |
984 | "%s: i2c failed. ret %d\n", __func__, ret); | |
985 | ||
986 | return ret; | |
987 | } | |
988 | ||
989 | static int ov5693_t_focus_rel(struct v4l2_subdev *sd, s32 value) | |
990 | { | |
991 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
992 | return ov5693_t_focus_abs(sd, dev->focus + value); | |
993 | } | |
994 | ||
995 | #define DELAY_PER_STEP_NS 1000000 | |
996 | #define DELAY_MAX_PER_STEP_NS (1000000 * 1023) | |
997 | static int ov5693_q_focus_status(struct v4l2_subdev *sd, s32 *value) | |
998 | { | |
999 | u32 status = 0; | |
1000 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1001 | struct timespec temptime; | |
1002 | const struct timespec timedelay = { | |
1003 | 0, | |
1004 | min((u32)abs(dev->number_of_steps) * DELAY_PER_STEP_NS, | |
1005 | (u32)DELAY_MAX_PER_STEP_NS), | |
1006 | }; | |
1007 | ||
1008 | getnstimeofday(&temptime); | |
1009 | temptime = timespec_sub(temptime, (dev->timestamp_t_focus_abs)); | |
1010 | if (timespec_compare(&temptime, &timedelay) <= 0) { | |
1011 | status |= ATOMISP_FOCUS_STATUS_MOVING; | |
1012 | status |= ATOMISP_FOCUS_HP_IN_PROGRESS; | |
1013 | } else { | |
1014 | status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE; | |
1015 | status |= ATOMISP_FOCUS_HP_COMPLETE; | |
1016 | } | |
1017 | ||
1018 | *value = status; | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | static int ov5693_q_focus_abs(struct v4l2_subdev *sd, s32 *value) | |
1024 | { | |
1025 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1026 | s32 val; | |
1027 | ||
1028 | ov5693_q_focus_status(sd, &val); | |
1029 | ||
1030 | if (val & ATOMISP_FOCUS_STATUS_MOVING) | |
1031 | *value = dev->focus - dev->number_of_steps; | |
1032 | else | |
1033 | *value = dev->focus; | |
1034 | ||
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | static int ov5693_t_vcm_slew(struct v4l2_subdev *sd, s32 value) | |
1039 | { | |
1040 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1041 | dev->number_of_steps = value; | |
1042 | dev->vcm_update = true; | |
1043 | return 0; | |
1044 | } | |
1045 | ||
1046 | static int ov5693_t_vcm_timing(struct v4l2_subdev *sd, s32 value) | |
1047 | { | |
1048 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1049 | dev->number_of_steps = value; | |
1050 | dev->vcm_update = true; | |
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl) | |
1055 | { | |
1056 | struct ov5693_device *dev = | |
1057 | container_of(ctrl->handler, struct ov5693_device, ctrl_handler); | |
1058 | struct i2c_client *client = v4l2_get_subdevdata(&dev->sd); | |
1059 | int ret = 0; | |
1060 | ||
1061 | switch (ctrl->id) { | |
1062 | case V4L2_CID_FOCUS_ABSOLUTE: | |
1063 | dev_dbg(&client->dev, "%s: CID_FOCUS_ABSOLUTE:%d.\n", | |
1064 | __func__, ctrl->val); | |
1065 | ret = ov5693_t_focus_abs(&dev->sd, ctrl->val); | |
1066 | break; | |
1067 | case V4L2_CID_FOCUS_RELATIVE: | |
1068 | dev_dbg(&client->dev, "%s: CID_FOCUS_RELATIVE:%d.\n", | |
1069 | __func__, ctrl->val); | |
1070 | ret = ov5693_t_focus_rel(&dev->sd, ctrl->val); | |
1071 | break; | |
1072 | case V4L2_CID_VCM_SLEW: | |
1073 | ret = ov5693_t_vcm_slew(&dev->sd, ctrl->val); | |
1074 | break; | |
1075 | case V4L2_CID_VCM_TIMEING: | |
1076 | ret = ov5693_t_vcm_timing(&dev->sd, ctrl->val); | |
1077 | break; | |
1078 | default: | |
1079 | ret = -EINVAL; | |
1080 | } | |
1081 | return ret; | |
1082 | } | |
1083 | ||
1084 | static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl) | |
1085 | { | |
1086 | struct ov5693_device *dev = | |
1087 | container_of(ctrl->handler, struct ov5693_device, ctrl_handler); | |
1088 | int ret = 0; | |
1089 | ||
1090 | switch (ctrl->id) { | |
1091 | case V4L2_CID_EXPOSURE_ABSOLUTE: | |
1092 | ret = ov5693_q_exposure(&dev->sd, &ctrl->val); | |
1093 | break; | |
1094 | case V4L2_CID_FOCAL_ABSOLUTE: | |
1095 | ret = ov5693_g_focal(&dev->sd, &ctrl->val); | |
1096 | break; | |
1097 | case V4L2_CID_FNUMBER_ABSOLUTE: | |
1098 | ret = ov5693_g_fnumber(&dev->sd, &ctrl->val); | |
1099 | break; | |
1100 | case V4L2_CID_FNUMBER_RANGE: | |
1101 | ret = ov5693_g_fnumber_range(&dev->sd, &ctrl->val); | |
1102 | break; | |
1103 | case V4L2_CID_FOCUS_ABSOLUTE: | |
1104 | ret = ov5693_q_focus_abs(&dev->sd, &ctrl->val); | |
1105 | break; | |
1106 | case V4L2_CID_FOCUS_STATUS: | |
1107 | ret = ov5693_q_focus_status(&dev->sd, &ctrl->val); | |
9e7b319e | 1108 | break; |
a49d2536 AC |
1109 | case V4L2_CID_BIN_FACTOR_HORZ: |
1110 | ret = ov5693_g_bin_factor_x(&dev->sd, &ctrl->val); | |
1111 | break; | |
1112 | case V4L2_CID_BIN_FACTOR_VERT: | |
1113 | ret = ov5693_g_bin_factor_y(&dev->sd, &ctrl->val); | |
1114 | break; | |
1115 | default: | |
1116 | ret = -EINVAL; | |
1117 | } | |
1118 | ||
1119 | return ret; | |
1120 | } | |
1121 | ||
1122 | static const struct v4l2_ctrl_ops ctrl_ops = { | |
1123 | .s_ctrl = ov5693_s_ctrl, | |
1124 | .g_volatile_ctrl = ov5693_g_volatile_ctrl | |
1125 | }; | |
1126 | ||
1127 | struct v4l2_ctrl_config ov5693_controls[] = { | |
1128 | { | |
1129 | .ops = &ctrl_ops, | |
1130 | .id = V4L2_CID_EXPOSURE_ABSOLUTE, | |
1131 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1132 | .name = "exposure", | |
1133 | .min = 0x0, | |
1134 | .max = 0xffff, | |
1135 | .step = 0x01, | |
1136 | .def = 0x00, | |
1137 | .flags = 0, | |
1138 | }, | |
1139 | { | |
1140 | .ops = &ctrl_ops, | |
1141 | .id = V4L2_CID_FOCAL_ABSOLUTE, | |
1142 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1143 | .name = "focal length", | |
1144 | .min = OV5693_FOCAL_LENGTH_DEFAULT, | |
1145 | .max = OV5693_FOCAL_LENGTH_DEFAULT, | |
1146 | .step = 0x01, | |
1147 | .def = OV5693_FOCAL_LENGTH_DEFAULT, | |
1148 | .flags = 0, | |
1149 | }, | |
1150 | { | |
1151 | .ops = &ctrl_ops, | |
1152 | .id = V4L2_CID_FNUMBER_ABSOLUTE, | |
1153 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1154 | .name = "f-number", | |
1155 | .min = OV5693_F_NUMBER_DEFAULT, | |
1156 | .max = OV5693_F_NUMBER_DEFAULT, | |
1157 | .step = 0x01, | |
1158 | .def = OV5693_F_NUMBER_DEFAULT, | |
1159 | .flags = 0, | |
1160 | }, | |
1161 | { | |
1162 | .ops = &ctrl_ops, | |
1163 | .id = V4L2_CID_FNUMBER_RANGE, | |
1164 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1165 | .name = "f-number range", | |
1166 | .min = OV5693_F_NUMBER_RANGE, | |
1167 | .max = OV5693_F_NUMBER_RANGE, | |
1168 | .step = 0x01, | |
1169 | .def = OV5693_F_NUMBER_RANGE, | |
1170 | .flags = 0, | |
1171 | }, | |
1172 | { | |
1173 | .ops = &ctrl_ops, | |
1174 | .id = V4L2_CID_FOCUS_ABSOLUTE, | |
1175 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1176 | .name = "focus move absolute", | |
1177 | .min = 0, | |
1178 | .max = OV5693_VCM_MAX_FOCUS_POS, | |
1179 | .step = 1, | |
1180 | .def = 0, | |
1181 | .flags = 0, | |
1182 | }, | |
1183 | { | |
1184 | .ops = &ctrl_ops, | |
1185 | .id = V4L2_CID_FOCUS_RELATIVE, | |
1186 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1187 | .name = "focus move relative", | |
1188 | .min = OV5693_VCM_MAX_FOCUS_NEG, | |
1189 | .max = OV5693_VCM_MAX_FOCUS_POS, | |
1190 | .step = 1, | |
1191 | .def = 0, | |
1192 | .flags = 0, | |
1193 | }, | |
1194 | { | |
1195 | .ops = &ctrl_ops, | |
1196 | .id = V4L2_CID_FOCUS_STATUS, | |
1197 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1198 | .name = "focus status", | |
1199 | .min = 0, | |
1200 | .max = 100, /* allow enum to grow in the future */ | |
1201 | .step = 1, | |
1202 | .def = 0, | |
1203 | .flags = 0, | |
1204 | }, | |
1205 | { | |
1206 | .ops = &ctrl_ops, | |
1207 | .id = V4L2_CID_VCM_SLEW, | |
1208 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1209 | .name = "vcm slew", | |
1210 | .min = 0, | |
1211 | .max = OV5693_VCM_SLEW_STEP_MAX, | |
1212 | .step = 1, | |
1213 | .def = 0, | |
1214 | .flags = 0, | |
1215 | }, | |
1216 | { | |
1217 | .ops = &ctrl_ops, | |
1218 | .id = V4L2_CID_VCM_TIMEING, | |
1219 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1220 | .name = "vcm step time", | |
1221 | .min = 0, | |
1222 | .max = OV5693_VCM_SLEW_TIME_MAX, | |
1223 | .step = 1, | |
1224 | .def = 0, | |
1225 | .flags = 0, | |
1226 | }, | |
1227 | { | |
1228 | .ops = &ctrl_ops, | |
1229 | .id = V4L2_CID_BIN_FACTOR_HORZ, | |
1230 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1231 | .name = "horizontal binning factor", | |
1232 | .min = 0, | |
1233 | .max = OV5693_BIN_FACTOR_MAX, | |
1234 | .step = 1, | |
1235 | .def = 0, | |
1236 | .flags = 0, | |
1237 | }, | |
1238 | { | |
1239 | .ops = &ctrl_ops, | |
1240 | .id = V4L2_CID_BIN_FACTOR_VERT, | |
1241 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1242 | .name = "vertical binning factor", | |
1243 | .min = 0, | |
1244 | .max = OV5693_BIN_FACTOR_MAX, | |
1245 | .step = 1, | |
1246 | .def = 0, | |
1247 | .flags = 0, | |
1248 | }, | |
1249 | }; | |
1250 | ||
1251 | static int ov5693_init(struct v4l2_subdev *sd) | |
1252 | { | |
1253 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1254 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1255 | int ret; | |
1256 | ||
1257 | pr_info("%s\n", __func__); | |
1258 | mutex_lock(&dev->input_lock); | |
1259 | dev->vcm_update = false; | |
1260 | ||
1261 | if (dev->vcm == VCM_AD5823) { | |
1262 | ret = vcm_ad_i2c_wr8(client, 0x01, 0x01); /* vcm init test */ | |
1263 | if (ret) | |
1264 | dev_err(&client->dev, | |
1265 | "vcm reset failed\n"); | |
1266 | /*change the mode*/ | |
1267 | ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, | |
1268 | AD5823_RING_CTRL_ENABLE); | |
1269 | if (ret) | |
1270 | dev_err(&client->dev, | |
1271 | "vcm enable ringing failed\n"); | |
1272 | ret = ad5823_i2c_write(client, AD5823_REG_MODE, | |
1273 | AD5823_ARC_RES1); | |
1274 | if (ret) | |
1275 | dev_err(&client->dev, | |
1276 | "vcm change mode failed\n"); | |
1277 | } | |
1278 | ||
1279 | /*change initial focus value for ad5823*/ | |
1280 | if (dev->vcm == VCM_AD5823) { | |
1281 | dev->focus = AD5823_INIT_FOCUS_POS; | |
1282 | ov5693_t_focus_abs(sd, AD5823_INIT_FOCUS_POS); | |
1283 | } else { | |
1284 | dev->focus = 0; | |
1285 | ov5693_t_focus_abs(sd, 0); | |
1286 | } | |
1287 | ||
1288 | mutex_unlock(&dev->input_lock); | |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | static int power_ctrl(struct v4l2_subdev *sd, bool flag) | |
1294 | { | |
1295 | int ret; | |
1296 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1297 | ||
1298 | if (!dev || !dev->platform_data) | |
1299 | return -ENODEV; | |
1300 | ||
1301 | /* Non-gmin platforms use the legacy callback */ | |
1302 | if (dev->platform_data->power_ctrl) | |
1303 | return dev->platform_data->power_ctrl(sd, flag); | |
1304 | ||
1305 | /* This driver assumes "internal DVDD, PWDNB tied to DOVDD". | |
1306 | * In this set up only gpio0 (XSHUTDN) should be available | |
1307 | * but in some products (for example ECS) gpio1 (PWDNB) is | |
1308 | * also available. If gpio1 is available we emulate it being | |
1309 | * tied to DOVDD here. */ | |
1310 | if (flag) { | |
1311 | ret = dev->platform_data->v2p8_ctrl(sd, 1); | |
1312 | dev->platform_data->gpio1_ctrl(sd, 1); | |
1313 | if (ret == 0) { | |
1314 | ret = dev->platform_data->v1p8_ctrl(sd, 1); | |
1315 | if (ret) { | |
1316 | dev->platform_data->gpio1_ctrl(sd, 0); | |
1317 | ret = dev->platform_data->v2p8_ctrl(sd, 0); | |
1318 | } | |
1319 | } | |
1320 | } else { | |
1321 | dev->platform_data->gpio1_ctrl(sd, 0); | |
1322 | ret = dev->platform_data->v1p8_ctrl(sd, 0); | |
1323 | ret |= dev->platform_data->v2p8_ctrl(sd, 0); | |
1324 | } | |
1325 | ||
1326 | return ret; | |
1327 | } | |
1328 | ||
1329 | static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) | |
1330 | { | |
a49d2536 AC |
1331 | struct ov5693_device *dev = to_ov5693_sensor(sd); |
1332 | ||
1333 | if (!dev || !dev->platform_data) | |
1334 | return -ENODEV; | |
1335 | ||
1336 | /* Non-gmin platforms use the legacy callback */ | |
1337 | if (dev->platform_data->gpio_ctrl) | |
1338 | return dev->platform_data->gpio_ctrl(sd, flag); | |
1339 | ||
34d340b7 | 1340 | return dev->platform_data->gpio0_ctrl(sd, flag); |
a49d2536 AC |
1341 | } |
1342 | ||
1343 | static int __power_up(struct v4l2_subdev *sd) | |
1344 | { | |
1345 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1346 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1347 | int ret; | |
1348 | ||
1349 | if (NULL == dev->platform_data) { | |
1350 | dev_err(&client->dev, | |
1351 | "no camera_sensor_platform_data"); | |
1352 | return -ENODEV; | |
1353 | } | |
1354 | ||
1355 | /* power control */ | |
1356 | ret = power_ctrl(sd, 1); | |
1357 | if (ret) | |
1358 | goto fail_power; | |
1359 | ||
1360 | /* according to DS, at least 5ms is needed between DOVDD and PWDN */ | |
1361 | /* add this delay time to 10~11ms*/ | |
1362 | usleep_range(10000, 11000); | |
1363 | ||
1364 | /* gpio ctrl */ | |
1365 | ret = gpio_ctrl(sd, 1); | |
1366 | if (ret) { | |
1367 | ret = gpio_ctrl(sd, 1); | |
1368 | if (ret) | |
1369 | goto fail_power; | |
1370 | } | |
1371 | ||
1372 | /* flis clock control */ | |
1373 | ret = dev->platform_data->flisclk_ctrl(sd, 1); | |
1374 | if (ret) | |
1375 | goto fail_clk; | |
1376 | ||
1377 | __cci_delay(up_delay); | |
1378 | ||
1379 | return 0; | |
1380 | ||
1381 | fail_clk: | |
1382 | gpio_ctrl(sd, 0); | |
1383 | fail_power: | |
1384 | power_ctrl(sd, 0); | |
1385 | dev_err(&client->dev, "sensor power-up failed\n"); | |
1386 | ||
1387 | return ret; | |
1388 | } | |
1389 | ||
1390 | static int power_down(struct v4l2_subdev *sd) | |
1391 | { | |
1392 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1393 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1394 | int ret = 0; | |
1395 | ||
1396 | dev->focus = OV5693_INVALID_CONFIG; | |
1397 | if (NULL == dev->platform_data) { | |
1398 | dev_err(&client->dev, | |
1399 | "no camera_sensor_platform_data"); | |
1400 | return -ENODEV; | |
1401 | } | |
1402 | ||
1403 | ret = dev->platform_data->flisclk_ctrl(sd, 0); | |
1404 | if (ret) | |
1405 | dev_err(&client->dev, "flisclk failed\n"); | |
1406 | ||
1407 | /* gpio ctrl */ | |
1408 | ret = gpio_ctrl(sd, 0); | |
1409 | if (ret) { | |
1410 | ret = gpio_ctrl(sd, 0); | |
1411 | if (ret) | |
1412 | dev_err(&client->dev, "gpio failed 2\n"); | |
1413 | } | |
1414 | ||
1415 | /* power control */ | |
1416 | ret = power_ctrl(sd, 0); | |
1417 | if (ret) | |
1418 | dev_err(&client->dev, "vprog failed.\n"); | |
1419 | ||
1420 | return ret; | |
1421 | } | |
1422 | ||
1423 | static int power_up(struct v4l2_subdev *sd) | |
1424 | { | |
1425 | static const int retry_count = 4; | |
1426 | int i, ret; | |
1427 | ||
1428 | for (i = 0; i < retry_count; i++) { | |
1429 | ret = __power_up(sd); | |
1430 | if (!ret) | |
1431 | return 0; | |
1432 | ||
1433 | power_down(sd); | |
1434 | } | |
1435 | return ret; | |
1436 | } | |
1437 | ||
1438 | static int ov5693_s_power(struct v4l2_subdev *sd, int on) | |
1439 | { | |
1440 | int ret; | |
1441 | ||
1442 | pr_info("%s: on %d\n", __func__, on); | |
1443 | if (on == 0) | |
1444 | return power_down(sd); | |
1445 | else { | |
1446 | ret = power_up(sd); | |
1447 | if (!ret) { | |
1448 | ret = ov5693_init(sd); | |
1449 | /* restore settings */ | |
1450 | ov5693_res = ov5693_res_preview; | |
1451 | N_RES = N_RES_PREVIEW; | |
1452 | } | |
1453 | } | |
1454 | return ret; | |
1455 | } | |
1456 | ||
1457 | /* | |
1458 | * distance - calculate the distance | |
1459 | * @res: resolution | |
1460 | * @w: width | |
1461 | * @h: height | |
1462 | * | |
1463 | * Get the gap between res_w/res_h and w/h. | |
1464 | * distance = (res_w/res_h - w/h) / (w/h) * 8192 | |
1465 | * res->width/height smaller than w/h wouldn't be considered. | |
1466 | * The gap of ratio larger than 1/8 wouldn't be considered. | |
1467 | * Returns the value of gap or -1 if fail. | |
1468 | */ | |
1469 | #define LARGEST_ALLOWED_RATIO_MISMATCH 1024 | |
1470 | static int distance(struct ov5693_resolution *res, u32 w, u32 h) | |
1471 | { | |
1472 | int ratio; | |
1473 | int distance; | |
1474 | ||
1475 | if (w == 0 || h == 0 || | |
1476 | res->width < w || res->height < h) | |
1477 | return -1; | |
1478 | ||
6c492211 | 1479 | ratio = res->width << 13; |
a49d2536 AC |
1480 | ratio /= w; |
1481 | ratio *= h; | |
1482 | ratio /= res->height; | |
1483 | ||
1484 | distance = abs(ratio - 8192); | |
1485 | ||
1486 | if (distance > LARGEST_ALLOWED_RATIO_MISMATCH) | |
1487 | return -1; | |
1488 | ||
1489 | return distance; | |
1490 | } | |
1491 | ||
1492 | /* Return the nearest higher resolution index | |
1493 | * Firstly try to find the approximate aspect ratio resolution | |
1494 | * If we find multiple same AR resolutions, choose the | |
1495 | * minimal size. | |
1496 | */ | |
1497 | static int nearest_resolution_index(int w, int h) | |
1498 | { | |
1499 | int i; | |
1500 | int idx = -1; | |
1501 | int dist; | |
1502 | int min_dist = INT_MAX; | |
1503 | int min_res_w = INT_MAX; | |
1504 | struct ov5693_resolution *tmp_res = NULL; | |
1505 | ||
1506 | for (i = 0; i < N_RES; i++) { | |
1507 | tmp_res = &ov5693_res[i]; | |
1508 | dist = distance(tmp_res, w, h); | |
1509 | if (dist == -1) | |
1510 | continue; | |
1511 | if (dist < min_dist) { | |
1512 | min_dist = dist; | |
1513 | idx = i; | |
1514 | min_res_w = ov5693_res[i].width; | |
1515 | continue; | |
1516 | } | |
1517 | if (dist == min_dist && ov5693_res[i].width < min_res_w) | |
1518 | idx = i; | |
1519 | } | |
1520 | ||
1521 | return idx; | |
1522 | } | |
1523 | ||
1524 | static int get_resolution_index(int w, int h) | |
1525 | { | |
1526 | int i; | |
1527 | ||
1528 | for (i = 0; i < N_RES; i++) { | |
1529 | if (w != ov5693_res[i].width) | |
1530 | continue; | |
1531 | if (h != ov5693_res[i].height) | |
1532 | continue; | |
1533 | ||
1534 | return i; | |
1535 | } | |
1536 | ||
1537 | return -1; | |
1538 | } | |
1539 | ||
1540 | /* TODO: remove it. */ | |
1541 | static int startup(struct v4l2_subdev *sd) | |
1542 | { | |
1543 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1544 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1545 | int ret = 0; | |
1546 | ||
1547 | ret = ov5693_write_reg(client, OV5693_8BIT, | |
1548 | OV5693_SW_RESET, 0x01); | |
1549 | if (ret) { | |
1550 | dev_err(&client->dev, "ov5693 reset err.\n"); | |
1551 | return ret; | |
1552 | } | |
1553 | ||
1554 | ret = ov5693_write_reg_array(client, ov5693_global_setting); | |
1555 | if (ret) { | |
1556 | dev_err(&client->dev, "ov5693 write register err.\n"); | |
1557 | return ret; | |
1558 | } | |
1559 | ||
1560 | ret = ov5693_write_reg_array(client, ov5693_res[dev->fmt_idx].regs); | |
1561 | if (ret) { | |
1562 | dev_err(&client->dev, "ov5693 write register err.\n"); | |
1563 | return ret; | |
1564 | } | |
1565 | ||
1566 | return ret; | |
1567 | } | |
1568 | ||
1569 | static int ov5693_set_fmt(struct v4l2_subdev *sd, | |
1570 | struct v4l2_subdev_pad_config *cfg, | |
1571 | struct v4l2_subdev_format *format) | |
1572 | { | |
1573 | struct v4l2_mbus_framefmt *fmt = &format->format; | |
1574 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1575 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1576 | struct camera_mipi_info *ov5693_info = NULL; | |
1577 | int ret = 0; | |
1578 | int idx; | |
1579 | if (format->pad) | |
1580 | return -EINVAL; | |
1581 | if (!fmt) | |
1582 | return -EINVAL; | |
1583 | ov5693_info = v4l2_get_subdev_hostdata(sd); | |
1584 | if (ov5693_info == NULL) | |
1585 | return -EINVAL; | |
1586 | ||
1587 | mutex_lock(&dev->input_lock); | |
1588 | idx = nearest_resolution_index(fmt->width, fmt->height); | |
1589 | if (idx == -1) { | |
1590 | /* return the largest resolution */ | |
1591 | fmt->width = ov5693_res[N_RES - 1].width; | |
1592 | fmt->height = ov5693_res[N_RES - 1].height; | |
1593 | } else { | |
1594 | fmt->width = ov5693_res[idx].width; | |
1595 | fmt->height = ov5693_res[idx].height; | |
1596 | } | |
1597 | ||
1598 | fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10; | |
1599 | if (format->which == V4L2_SUBDEV_FORMAT_TRY) { | |
1600 | cfg->try_fmt = *fmt; | |
1601 | mutex_unlock(&dev->input_lock); | |
1602 | return 0; | |
1603 | } | |
1604 | ||
1605 | dev->fmt_idx = get_resolution_index(fmt->width, fmt->height); | |
1606 | if (dev->fmt_idx == -1) { | |
1607 | dev_err(&client->dev, "get resolution fail\n"); | |
1608 | mutex_unlock(&dev->input_lock); | |
1609 | return -EINVAL; | |
1610 | } | |
1611 | ||
1612 | ret = startup(sd); | |
1613 | if (ret) { | |
1614 | int i = 0; | |
1615 | dev_err(&client->dev, "ov5693 startup err, retry to power up\n"); | |
1616 | for (i = 0; i < OV5693_POWER_UP_RETRY_NUM; i++) { | |
1617 | dev_err(&client->dev, | |
1618 | "ov5693 retry to power up %d/%d times, result: ", | |
1619 | i+1, OV5693_POWER_UP_RETRY_NUM); | |
1620 | power_down(sd); | |
1621 | ret = power_up(sd); | |
1622 | if (!ret) { | |
1623 | mutex_unlock(&dev->input_lock); | |
1624 | ov5693_init(sd); | |
1625 | mutex_lock(&dev->input_lock); | |
1626 | } else { | |
1627 | dev_err(&client->dev, "power up failed, continue\n"); | |
1628 | continue; | |
1629 | } | |
1630 | ret = startup(sd); | |
1631 | if (ret) { | |
1632 | dev_err(&client->dev, " startup FAILED!\n"); | |
1633 | } else { | |
1634 | dev_err(&client->dev, " startup SUCCESS!\n"); | |
1635 | break; | |
1636 | } | |
1637 | } | |
1638 | } | |
1639 | ||
1640 | /* | |
1641 | * After sensor settings are set to HW, sometimes stream is started. | |
1642 | * This would cause ISP timeout because ISP is not ready to receive | |
1643 | * data yet. So add stop streaming here. | |
1644 | */ | |
1645 | ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM, | |
1646 | OV5693_STOP_STREAMING); | |
1647 | if (ret) | |
1648 | dev_warn(&client->dev, "ov5693 stream off err\n"); | |
1649 | ||
1650 | ret = ov5693_get_intg_factor(client, ov5693_info, | |
1651 | &ov5693_res[dev->fmt_idx]); | |
1652 | if (ret) { | |
1653 | dev_err(&client->dev, "failed to get integration_factor\n"); | |
1654 | goto err; | |
1655 | } | |
1656 | ||
1657 | ov5693_info->metadata_width = fmt->width * 10 / 8; | |
1658 | ov5693_info->metadata_height = 1; | |
1659 | ov5693_info->metadata_effective_width = &ov5693_embedded_effective_size; | |
1660 | ||
1661 | err: | |
1662 | mutex_unlock(&dev->input_lock); | |
1663 | return ret; | |
1664 | } | |
1665 | static int ov5693_get_fmt(struct v4l2_subdev *sd, | |
1666 | struct v4l2_subdev_pad_config *cfg, | |
1667 | struct v4l2_subdev_format *format) | |
1668 | { | |
1669 | struct v4l2_mbus_framefmt *fmt = &format->format; | |
1670 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1671 | if (format->pad) | |
1672 | return -EINVAL; | |
1673 | ||
1674 | if (!fmt) | |
1675 | return -EINVAL; | |
1676 | ||
1677 | fmt->width = ov5693_res[dev->fmt_idx].width; | |
1678 | fmt->height = ov5693_res[dev->fmt_idx].height; | |
1679 | fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10; | |
1680 | ||
1681 | return 0; | |
1682 | } | |
1683 | ||
1684 | static int ov5693_detect(struct i2c_client *client) | |
1685 | { | |
1686 | struct i2c_adapter *adapter = client->adapter; | |
1687 | u16 high, low; | |
1688 | int ret; | |
1689 | u16 id; | |
1690 | u8 revision; | |
1691 | ||
1692 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) | |
1693 | return -ENODEV; | |
1694 | ||
1695 | ret = ov5693_read_reg(client, OV5693_8BIT, | |
1696 | OV5693_SC_CMMN_CHIP_ID_H, &high); | |
1697 | if (ret) { | |
1698 | dev_err(&client->dev, "sensor_id_high = 0x%x\n", high); | |
1699 | return -ENODEV; | |
1700 | } | |
1701 | ret = ov5693_read_reg(client, OV5693_8BIT, | |
1702 | OV5693_SC_CMMN_CHIP_ID_L, &low); | |
1703 | id = ((((u16) high) << 8) | (u16) low); | |
1704 | ||
1705 | if (id != OV5693_ID) { | |
1706 | dev_err(&client->dev, "sensor ID error 0x%x\n", id); | |
1707 | return -ENODEV; | |
1708 | } | |
1709 | ||
1710 | ret = ov5693_read_reg(client, OV5693_8BIT, | |
1711 | OV5693_SC_CMMN_SUB_ID, &high); | |
1712 | revision = (u8) high & 0x0f; | |
1713 | ||
1714 | dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision); | |
1715 | dev_dbg(&client->dev, "detect ov5693 success\n"); | |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | static int ov5693_s_stream(struct v4l2_subdev *sd, int enable) | |
1720 | { | |
1721 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1722 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1723 | int ret; | |
1724 | ||
1725 | mutex_lock(&dev->input_lock); | |
1726 | ||
1727 | ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM, | |
1728 | enable ? OV5693_START_STREAMING : | |
1729 | OV5693_STOP_STREAMING); | |
1730 | ||
1731 | mutex_unlock(&dev->input_lock); | |
1732 | ||
1733 | return ret; | |
1734 | } | |
1735 | ||
1736 | ||
1737 | static int ov5693_s_config(struct v4l2_subdev *sd, | |
1738 | int irq, void *platform_data) | |
1739 | { | |
1740 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1741 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1742 | int ret = 0; | |
1743 | ||
1744 | if (platform_data == NULL) | |
1745 | return -ENODEV; | |
1746 | ||
1747 | dev->platform_data = | |
1748 | (struct camera_sensor_platform_data *)platform_data; | |
1749 | ||
1750 | mutex_lock(&dev->input_lock); | |
1751 | /* power off the module, then power on it in future | |
1752 | * as first power on by board may not fulfill the | |
1753 | * power on sequqence needed by the module | |
1754 | */ | |
1755 | ret = power_down(sd); | |
1756 | if (ret) { | |
1757 | dev_err(&client->dev, "ov5693 power-off err.\n"); | |
1758 | goto fail_power_off; | |
1759 | } | |
1760 | ||
1761 | ret = power_up(sd); | |
1762 | if (ret) { | |
1763 | dev_err(&client->dev, "ov5693 power-up err.\n"); | |
1764 | goto fail_power_on; | |
1765 | } | |
1766 | ||
1767 | if (!dev->vcm) | |
1768 | dev->vcm = vcm_detect(client); | |
1769 | ||
1770 | ret = dev->platform_data->csi_cfg(sd, 1); | |
1771 | if (ret) | |
1772 | goto fail_csi_cfg; | |
1773 | ||
1774 | /* config & detect sensor */ | |
1775 | ret = ov5693_detect(client); | |
1776 | if (ret) { | |
1777 | dev_err(&client->dev, "ov5693_detect err s_config.\n"); | |
1778 | goto fail_csi_cfg; | |
1779 | } | |
1780 | ||
1781 | dev->otp_data = ov5693_otp_read(sd); | |
1782 | ||
1783 | /* turn off sensor, after probed */ | |
1784 | ret = power_down(sd); | |
1785 | if (ret) { | |
1786 | dev_err(&client->dev, "ov5693 power-off err.\n"); | |
1787 | goto fail_csi_cfg; | |
1788 | } | |
1789 | mutex_unlock(&dev->input_lock); | |
1790 | ||
1791 | return ret; | |
1792 | ||
1793 | fail_csi_cfg: | |
1794 | dev->platform_data->csi_cfg(sd, 0); | |
1795 | fail_power_on: | |
1796 | power_down(sd); | |
1797 | dev_err(&client->dev, "sensor power-gating failed\n"); | |
1798 | fail_power_off: | |
1799 | mutex_unlock(&dev->input_lock); | |
1800 | return ret; | |
1801 | } | |
1802 | ||
1803 | static int ov5693_g_parm(struct v4l2_subdev *sd, | |
1804 | struct v4l2_streamparm *param) | |
1805 | { | |
1806 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1807 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1808 | ||
1809 | if (!param) | |
1810 | return -EINVAL; | |
1811 | ||
1812 | if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { | |
1813 | dev_err(&client->dev, "unsupported buffer type.\n"); | |
1814 | return -EINVAL; | |
1815 | } | |
1816 | ||
1817 | memset(param, 0, sizeof(*param)); | |
1818 | param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1819 | ||
1820 | if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) { | |
1821 | param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; | |
1822 | param->parm.capture.timeperframe.numerator = 1; | |
1823 | param->parm.capture.capturemode = dev->run_mode; | |
1824 | param->parm.capture.timeperframe.denominator = | |
1825 | ov5693_res[dev->fmt_idx].fps; | |
1826 | } | |
1827 | return 0; | |
1828 | } | |
1829 | ||
1830 | static int ov5693_s_parm(struct v4l2_subdev *sd, | |
1831 | struct v4l2_streamparm *param) | |
1832 | { | |
1833 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1834 | dev->run_mode = param->parm.capture.capturemode; | |
1835 | ||
1836 | mutex_lock(&dev->input_lock); | |
1837 | switch (dev->run_mode) { | |
1838 | case CI_MODE_VIDEO: | |
1839 | ov5693_res = ov5693_res_video; | |
1840 | N_RES = N_RES_VIDEO; | |
1841 | break; | |
1842 | case CI_MODE_STILL_CAPTURE: | |
1843 | ov5693_res = ov5693_res_still; | |
1844 | N_RES = N_RES_STILL; | |
1845 | break; | |
1846 | default: | |
1847 | ov5693_res = ov5693_res_preview; | |
1848 | N_RES = N_RES_PREVIEW; | |
1849 | } | |
1850 | mutex_unlock(&dev->input_lock); | |
1851 | return 0; | |
1852 | } | |
1853 | ||
1854 | static int ov5693_g_frame_interval(struct v4l2_subdev *sd, | |
1855 | struct v4l2_subdev_frame_interval *interval) | |
1856 | { | |
1857 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1858 | ||
1859 | interval->interval.numerator = 1; | |
1860 | interval->interval.denominator = ov5693_res[dev->fmt_idx].fps; | |
1861 | ||
1862 | return 0; | |
1863 | } | |
1864 | ||
1865 | static int ov5693_enum_mbus_code(struct v4l2_subdev *sd, | |
1866 | struct v4l2_subdev_pad_config *cfg, | |
1867 | struct v4l2_subdev_mbus_code_enum *code) | |
1868 | { | |
1869 | if (code->index >= MAX_FMTS) | |
1870 | return -EINVAL; | |
1871 | ||
1872 | code->code = MEDIA_BUS_FMT_SBGGR10_1X10; | |
1873 | return 0; | |
1874 | } | |
1875 | ||
1876 | static int ov5693_enum_frame_size(struct v4l2_subdev *sd, | |
1877 | struct v4l2_subdev_pad_config *cfg, | |
1878 | struct v4l2_subdev_frame_size_enum *fse) | |
1879 | { | |
1880 | int index = fse->index; | |
1881 | ||
1882 | if (index >= N_RES) | |
1883 | return -EINVAL; | |
1884 | ||
1885 | fse->min_width = ov5693_res[index].width; | |
1886 | fse->min_height = ov5693_res[index].height; | |
1887 | fse->max_width = ov5693_res[index].width; | |
1888 | fse->max_height = ov5693_res[index].height; | |
1889 | ||
1890 | return 0; | |
1891 | ||
1892 | } | |
1893 | ||
1894 | static const struct v4l2_subdev_video_ops ov5693_video_ops = { | |
1895 | .s_stream = ov5693_s_stream, | |
1896 | .g_parm = ov5693_g_parm, | |
1897 | .s_parm = ov5693_s_parm, | |
1898 | .g_frame_interval = ov5693_g_frame_interval, | |
1899 | }; | |
1900 | ||
1901 | static const struct v4l2_subdev_core_ops ov5693_core_ops = { | |
1902 | .s_power = ov5693_s_power, | |
1903 | .ioctl = ov5693_ioctl, | |
1904 | }; | |
1905 | ||
1906 | static const struct v4l2_subdev_pad_ops ov5693_pad_ops = { | |
1907 | .enum_mbus_code = ov5693_enum_mbus_code, | |
1908 | .enum_frame_size = ov5693_enum_frame_size, | |
1909 | .get_fmt = ov5693_get_fmt, | |
1910 | .set_fmt = ov5693_set_fmt, | |
1911 | }; | |
1912 | ||
1913 | static const struct v4l2_subdev_ops ov5693_ops = { | |
1914 | .core = &ov5693_core_ops, | |
1915 | .video = &ov5693_video_ops, | |
1916 | .pad = &ov5693_pad_ops, | |
1917 | }; | |
1918 | ||
1919 | static int ov5693_remove(struct i2c_client *client) | |
1920 | { | |
1921 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
1922 | struct ov5693_device *dev = to_ov5693_sensor(sd); | |
1923 | dev_dbg(&client->dev, "ov5693_remove...\n"); | |
1924 | ||
1925 | dev->platform_data->csi_cfg(sd, 0); | |
1926 | ||
1927 | v4l2_device_unregister_subdev(sd); | |
1928 | ||
1929 | atomisp_gmin_remove_subdev(sd); | |
1930 | ||
1931 | media_entity_cleanup(&dev->sd.entity); | |
1932 | v4l2_ctrl_handler_free(&dev->ctrl_handler); | |
1933 | kfree(dev); | |
1934 | ||
1935 | return 0; | |
1936 | } | |
1937 | ||
e19c9205 | 1938 | static int ov5693_probe(struct i2c_client *client) |
a49d2536 AC |
1939 | { |
1940 | struct ov5693_device *dev; | |
1941 | int i2c; | |
1942 | int ret = 0; | |
1943 | void *pdata = client->dev.platform_data; | |
1944 | struct acpi_device *adev; | |
1945 | unsigned int i; | |
1946 | ||
1947 | /* Firmware workaround: Some modules use a "secondary default" | |
1948 | * address of 0x10 which doesn't appear on schematics, and | |
1949 | * some BIOS versions haven't gotten the memo. Work around | |
1950 | * via config. */ | |
1951 | i2c = gmin_get_var_int(&client->dev, "I2CAddr", -1); | |
1952 | if (i2c != -1) { | |
1953 | dev_info(&client->dev, | |
1954 | "Overriding firmware-provided I2C address (0x%x) with 0x%x\n", | |
1955 | client->addr, i2c); | |
1956 | client->addr = i2c; | |
1957 | } | |
1958 | ||
1959 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
309167b9 | 1960 | if (!dev) |
a49d2536 | 1961 | return -ENOMEM; |
a49d2536 AC |
1962 | |
1963 | mutex_init(&dev->input_lock); | |
1964 | ||
1965 | dev->fmt_idx = 0; | |
1966 | v4l2_i2c_subdev_init(&(dev->sd), client, &ov5693_ops); | |
1967 | ||
1968 | adev = ACPI_COMPANION(&client->dev); | |
1969 | if (adev) { | |
1970 | adev->power.flags.power_resources = 0; | |
1971 | pdata = gmin_camera_platform_data(&dev->sd, | |
1972 | ATOMISP_INPUT_FORMAT_RAW_10, | |
1973 | atomisp_bayer_order_bggr); | |
1974 | } | |
1975 | ||
1976 | if (!pdata) | |
1977 | goto out_free; | |
1978 | ||
1979 | ret = ov5693_s_config(&dev->sd, client->irq, pdata); | |
1980 | if (ret) | |
1981 | goto out_free; | |
1982 | ||
1983 | ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA); | |
1984 | if (ret) | |
1985 | goto out_free; | |
1986 | ||
1987 | dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; | |
1988 | dev->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1989 | dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10; | |
1990 | dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; | |
1991 | ret = | |
1992 | v4l2_ctrl_handler_init(&dev->ctrl_handler, | |
1993 | ARRAY_SIZE(ov5693_controls)); | |
1994 | if (ret) { | |
1995 | ov5693_remove(client); | |
1996 | return ret; | |
1997 | } | |
1998 | ||
1999 | for (i = 0; i < ARRAY_SIZE(ov5693_controls); i++) | |
2000 | v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov5693_controls[i], | |
2001 | NULL); | |
2002 | ||
2003 | if (dev->ctrl_handler.error) { | |
2004 | ov5693_remove(client); | |
2005 | return dev->ctrl_handler.error; | |
2006 | } | |
2007 | ||
2008 | /* Use same lock for controls as for everything else. */ | |
2009 | dev->ctrl_handler.lock = &dev->input_lock; | |
2010 | dev->sd.ctrl_handler = &dev->ctrl_handler; | |
2011 | ||
2012 | ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); | |
2013 | if (ret) | |
2014 | ov5693_remove(client); | |
2015 | ||
2016 | return ret; | |
2017 | out_free: | |
2018 | v4l2_device_unregister_subdev(&dev->sd); | |
2019 | kfree(dev); | |
2020 | return ret; | |
2021 | } | |
2022 | ||
02cc60ca | 2023 | static const struct acpi_device_id ov5693_acpi_match[] = { |
a49d2536 AC |
2024 | {"INT33BE"}, |
2025 | {}, | |
2026 | }; | |
2027 | MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match); | |
2028 | ||
2029 | static struct i2c_driver ov5693_driver = { | |
2030 | .driver = { | |
e19c9205 AS |
2031 | .name = "ov5693", |
2032 | .acpi_match_table = ov5693_acpi_match, | |
a49d2536 | 2033 | }, |
e19c9205 | 2034 | .probe_new = ov5693_probe, |
a49d2536 | 2035 | .remove = ov5693_remove, |
a49d2536 | 2036 | }; |
2cb63c4c | 2037 | module_i2c_driver(ov5693_driver); |
a49d2536 AC |
2038 | |
2039 | MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors"); | |
2040 | MODULE_LICENSE("GPL"); |