blob: 44294e8dceececf5ec80cac44b9f491a8541f1c2 [file] [log] [blame]
Viswanadha Raju Thotakura426ec662017-03-15 17:00:29 -07001/* Copyright (c) 2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include "cam_sensor_util.h"
15#include "cam_sensor_soc_api.h"
16
17#define CAM_SENSOR_PINCTRL_STATE_SLEEP "cam_suspend"
18#define CAM_SENSOR_PINCTRL_STATE_DEFAULT "cam_default"
19
20#define VALIDATE_VOLTAGE(min, max, config_val) ((config_val) && \
21 (config_val >= min) && (config_val <= max))
22
23#undef CDBG
24#define CDBG(fmt, args...) pr_debug(fmt, ##args)
25
26static struct i2c_settings_list*
27 cam_sensor_get_i2c_ptr(struct i2c_settings_array *i2c_reg_settings,
28 uint32_t size)
29{
30 struct i2c_settings_list *tmp;
31
32 tmp = (struct i2c_settings_list *)
33 kzalloc(sizeof(struct i2c_settings_list), GFP_KERNEL);
34
35 if (tmp != NULL)
36 list_add_tail(&(tmp->list),
37 &(i2c_reg_settings->list_head));
38 else
39 return NULL;
40
41 tmp->i2c_settings.reg_setting = (struct cam_sensor_i2c_reg_array *)
42 kzalloc(sizeof(struct cam_sensor_i2c_reg_array) *
43 size, GFP_KERNEL);
44 if (tmp->i2c_settings.reg_setting == NULL) {
45 list_del(&(tmp->list));
46 kfree(tmp);
47 return NULL;
48 }
49 tmp->i2c_settings.size = size;
50
51 return tmp;
52}
53
54int32_t delete_request(struct i2c_settings_array *i2c_array)
55{
56 struct i2c_settings_list *i2c_list = NULL, *i2c_next = NULL;
57 int32_t rc = 0;
58
59 if (i2c_array == NULL) {
60 pr_err("%s:%d ::FATAL:: Invalid argument\n",
61 __func__, __LINE__);
62 return -EINVAL;
63 }
64
65 list_for_each_entry_safe(i2c_list, i2c_next,
66 &(i2c_array->list_head), list) {
67 kfree(i2c_list->i2c_settings.reg_setting);
68 list_del(&(i2c_list->list));
69 kfree(i2c_list);
70 }
71 INIT_LIST_HEAD(&(i2c_array->list_head));
72 i2c_array->is_settings_valid = 0;
73
74 return rc;
75}
76
77int32_t cam_sensor_handle_delay(
78 uint32_t **cmd_buf,
79 uint16_t generic_op_code,
80 struct i2c_settings_array *i2c_reg_settings,
81 uint32_t offset, uint32_t *byte_cnt,
82 struct list_head *list_ptr)
83{
84 int32_t rc = 0;
85 struct cam_cmd_unconditional_wait *cmd_uncond_wait =
86 (struct cam_cmd_unconditional_wait *) *cmd_buf;
87 struct i2c_settings_list *i2c_list = NULL;
88
89 if (i2c_list == NULL) {
90 pr_err("%s:%d Invalid list ptr\n",
91 __func__, __LINE__);
92 return -EINVAL;
93 }
94
95 if (offset > 0) {
96 i2c_list =
97 list_entry(list_ptr, struct i2c_settings_list, list);
98 if (generic_op_code ==
99 CAMERA_SENSOR_WAIT_OP_HW_UCND)
100 i2c_list->i2c_settings.
101 reg_setting[offset - 1].delay =
102 cmd_uncond_wait->delay;
103 else
104 i2c_list->i2c_settings.delay =
105 cmd_uncond_wait->delay;
106 (*cmd_buf) +=
107 sizeof(
108 struct cam_cmd_unconditional_wait) / sizeof(uint32_t);
109 (*byte_cnt) +=
110 sizeof(
111 struct cam_cmd_unconditional_wait);
112 } else {
113 pr_err("%s: %d Error: Delay Rxed Before any buffer: %d\n",
114 __func__, __LINE__, offset);
115 return -EINVAL;
116 }
117
118 return rc;
119}
120
121int32_t cam_sensor_handle_poll(
122 uint32_t **cmd_buf,
123 struct i2c_settings_array *i2c_reg_settings,
124 uint32_t *byte_cnt, int32_t *offset,
125 struct list_head **list_ptr)
126{
127 struct i2c_settings_list *i2c_list;
128 int32_t rc = 0;
129 struct cam_cmd_conditional_wait *cond_wait
130 = (struct cam_cmd_conditional_wait *) *cmd_buf;
131
132 i2c_list =
133 cam_sensor_get_i2c_ptr(i2c_reg_settings, 1);
134 if (!i2c_list || !i2c_list->i2c_settings.reg_setting) {
135 pr_err("%s: %d Failed in allocating mem for list\n",
136 __func__, __LINE__);
137 return -ENOMEM;
138 }
139
140 i2c_list->op_code = CAM_SENSOR_I2C_POLL;
141 i2c_list->i2c_settings.data_type =
142 cond_wait->data_type;
143 i2c_list->i2c_settings.addr_type =
144 cond_wait->addr_type;
145 i2c_list->i2c_settings.reg_setting->reg_addr =
146 cond_wait->reg_addr;
147 i2c_list->i2c_settings.reg_setting->reg_data =
148 cond_wait->reg_data;
149 i2c_list->i2c_settings.reg_setting->delay =
150 cond_wait->timeout;
151
152 (*cmd_buf) += sizeof(struct cam_cmd_conditional_wait) /
153 sizeof(uint32_t);
154 (*byte_cnt) += sizeof(struct cam_cmd_conditional_wait);
155
156 (*offset) += 1;
157 *list_ptr = &(i2c_list->list);
158
159 return rc;
160}
161
162int32_t cam_sensor_handle_random_write(
163 struct cam_cmd_i2c_random_wr *cam_cmd_i2c_random_wr,
164 struct i2c_settings_array *i2c_reg_settings,
165 uint16_t *cmd_length_in_bytes, int32_t *offset,
166 struct list_head **list)
167{
168 struct i2c_settings_list *i2c_list;
169 int32_t rc = 0, cnt;
170
171 i2c_list = cam_sensor_get_i2c_ptr(i2c_reg_settings,
172 cam_cmd_i2c_random_wr->header.count);
173 if (i2c_list == NULL ||
174 i2c_list->i2c_settings.reg_setting == NULL) {
175 pr_err("%s: %d Failed in allocating i2c_list\n",
176 __func__, __LINE__);
177 return -ENOMEM;
178 }
179
180 *cmd_length_in_bytes = (sizeof(struct i2c_rdwr_header) +
181 sizeof(struct i2c_random_wr_payload) *
182 (cam_cmd_i2c_random_wr->header.count));
183 i2c_list->op_code = CAM_SENSOR_I2C_WRITE_RANDOM;
184 i2c_list->i2c_settings.addr_type =
185 cam_cmd_i2c_random_wr->header.addr_type;
186 i2c_list->i2c_settings.data_type =
187 cam_cmd_i2c_random_wr->header.data_type;
188
189 for (cnt = 0; cnt < (cam_cmd_i2c_random_wr->header.count);
190 cnt++) {
191 i2c_list->i2c_settings.reg_setting[cnt].reg_addr =
192 cam_cmd_i2c_random_wr->
193 random_wr_payload[cnt].reg_addr;
194 i2c_list->i2c_settings.
195 reg_setting[cnt].reg_data =
196 cam_cmd_i2c_random_wr->
197 random_wr_payload[cnt].reg_data;
198 i2c_list->i2c_settings.
199 reg_setting[cnt].data_mask = 0;
200 }
201 (*offset) += cnt;
202 *list = &(i2c_list->list);
203
204 return rc;
205}
206
207/**
208 * Name : cam_sensor_i2c_pkt_parser
209 * Description : Parse CSL CCI packet and apply register settings
210 * Parameters : s_ctrl input/output sub_device
211 * arg input cam_control
212 * Description :
213 * Handle multiple I2C RD/WR and WAIT cmd formats in one command
214 * buffer, for example, a command buffer of m x RND_WR + 1 x HW_
215 * WAIT + n x RND_WR with num_cmd_buf = 1. Do not exepect RD/WR
216 * with different cmd_type and op_code in one command buffer.
217 */
218int cam_sensor_i2c_pkt_parser(struct i2c_settings_array *i2c_reg_settings,
219 struct cam_cmd_buf_desc *cmd_desc, int32_t num_cmd_buffers)
220{
221 int16_t rc = 0, i = 0;
222 size_t len_of_buff = 0;
223 uint64_t generic_ptr;
224
225 for (i = 0; i < num_cmd_buffers; i++) {
226 uint32_t *cmd_buf = NULL;
227 struct common_header *cmm_hdr;
228 uint16_t generic_op_code;
229 uint32_t byte_cnt = 0;
230 uint32_t j = 0;
231 struct list_head *list = NULL;
232
233 /*
234 * It is not expected the same settings to
235 * be spread across multiple cmd buffers
236 */
237
238 CDBG("%s:%d Total cmd Buf in Bytes: %d\n", __func__,
239 __LINE__, cmd_desc[i].length);
240
241 if (!cmd_desc[i].length)
242 continue;
243
244 rc = cam_mem_get_cpu_buf(cmd_desc[i].mem_handle,
245 (uint64_t *)&generic_ptr, &len_of_buff);
246 cmd_buf = (uint32_t *)generic_ptr;
247 if (rc < 0) {
248 pr_err("%s:%d Failed in getting cmd hdl: %d Err: %d Buffer Len: %ld\n",
249 __func__, __LINE__,
250 cmd_desc[i].mem_handle, rc,
251 len_of_buff);
252 return rc;
253 }
254 cmd_buf += cmd_desc[i].offset / sizeof(uint32_t);
255
256 while (byte_cnt < cmd_desc[i].length) {
257 cmm_hdr = (struct common_header *)cmd_buf;
258 generic_op_code = cmm_hdr->third_byte;
259 switch (cmm_hdr->cmd_type) {
260 case CAMERA_SENSOR_CMD_TYPE_I2C_RNDM_WR: {
261 uint16_t cmd_length_in_bytes = 0;
262 struct cam_cmd_i2c_random_wr
263 *cam_cmd_i2c_random_wr =
264 (struct cam_cmd_i2c_random_wr *)cmd_buf;
265
266 rc = cam_sensor_handle_random_write(
267 cam_cmd_i2c_random_wr,
268 i2c_reg_settings,
269 &cmd_length_in_bytes, &j, &list);
270 if (rc < 0) {
271 pr_err("%s:%d :Error: Failed in random read %d\n",
272 __func__, __LINE__, rc);
273 return rc;
274 }
275
276 cmd_buf += cmd_length_in_bytes /
277 sizeof(uint32_t);
278 byte_cnt += cmd_length_in_bytes;
279 break;
280 }
281 case CAMERA_SENSOR_CMD_TYPE_WAIT: {
282 if (generic_op_code ==
283 CAMERA_SENSOR_WAIT_OP_HW_UCND ||
284 generic_op_code ==
285 CAMERA_SENSOR_WAIT_OP_SW_UCND) {
286
287 rc = cam_sensor_handle_delay(
288 &cmd_buf, generic_op_code,
289 i2c_reg_settings, j, &byte_cnt,
290 list);
291 if (rc < 0) {
292 pr_err("%s:%d :Error: Failed in handling delay %d\n",
293 __func__, __LINE__, rc);
294 return rc;
295 }
296
297 } else if (generic_op_code ==
298 CAMERA_SENSOR_WAIT_OP_COND) {
299 rc = cam_sensor_handle_poll(
300 &cmd_buf, i2c_reg_settings,
301 &byte_cnt, &j, &list);
302 if (rc < 0) {
303 pr_err("%s:%d :Error: Failed in random read %d\n",
304 __func__, __LINE__, rc);
305 return rc;
306 }
307 } else {
308 pr_err("%s: %d Wrong Wait Command: %d\n",
309 __func__, __LINE__,
310 generic_op_code);
311 return -EINVAL;
312 }
313 break;
314 }
315 default:
316 pr_err("%s:%d Invalid Command Type:%d\n",
317 __func__, __LINE__, cmm_hdr->cmd_type);
318 return -EINVAL;
319 }
320 }
321 i2c_reg_settings->is_settings_valid = 1;
322 }
323
324 return rc;
325}
326
327int32_t msm_camera_fill_vreg_params(struct camera_vreg_t *cam_vreg,
328 int num_vreg, struct cam_sensor_power_setting *power_setting,
329 uint16_t power_setting_size)
330{
331 int32_t rc = 0, j = 0, i = 0;
332
333 /* Validate input parameters */
334 if (!cam_vreg || !power_setting) {
335 pr_err("%s:%d failed: cam_vreg %pK power_setting %pK", __func__,
336 __LINE__, cam_vreg, power_setting);
337 return -EINVAL;
338 }
339
340 /* Validate size of num_vreg */
341 if (num_vreg <= 0) {
342 pr_err("failed: num_vreg %d", num_vreg);
343 return -EINVAL;
344 }
345
346 for (i = 0; i < power_setting_size; i++) {
347 switch (power_setting[i].seq_type) {
348 case SENSOR_VDIG:
349 for (j = 0; j < num_vreg; j++) {
350 if (!strcmp(cam_vreg[j].reg_name, "cam_vdig")) {
351 CDBG("%s:%d i %d j %d cam_vdig\n",
352 __func__, __LINE__, i, j);
353 power_setting[i].seq_val = j;
354 if (VALIDATE_VOLTAGE(
355 cam_vreg[j].min_voltage,
356 cam_vreg[j].max_voltage,
357 power_setting[i].config_val)) {
358 cam_vreg[j].min_voltage =
359 cam_vreg[j].max_voltage =
360 power_setting[i].config_val;
361 }
362 break;
363 }
364 }
365 if (j == num_vreg)
366 power_setting[i].seq_val = INVALID_VREG;
367 break;
368
369 case SENSOR_VIO:
370 for (j = 0; j < num_vreg; j++) {
371 if (!strcmp(cam_vreg[j].reg_name, "cam_vio")) {
372 CDBG("%s:%d i %d j %d cam_vio\n",
373 __func__, __LINE__, i, j);
374 power_setting[i].seq_val = j;
375 if (VALIDATE_VOLTAGE(
376 cam_vreg[j].min_voltage,
377 cam_vreg[j].max_voltage,
378 power_setting[i].config_val)) {
379 cam_vreg[j].min_voltage =
380 cam_vreg[j].max_voltage =
381 power_setting[i].config_val;
382 }
383 break;
384 }
385 }
386 if (j == num_vreg)
387 power_setting[i].seq_val = INVALID_VREG;
388 break;
389
390 case SENSOR_VANA:
391 for (j = 0; j < num_vreg; j++) {
392 if (!strcmp(cam_vreg[j].reg_name, "cam_vana")) {
393 CDBG("%s:%d i %d j %d cam_vana\n",
394 __func__, __LINE__, i, j);
395 power_setting[i].seq_val = j;
396 if (VALIDATE_VOLTAGE(
397 cam_vreg[j].min_voltage,
398 cam_vreg[j].max_voltage,
399 power_setting[i].config_val)) {
400 cam_vreg[j].min_voltage =
401 cam_vreg[j].max_voltage =
402 power_setting[i].config_val;
403 }
404 break;
405 }
406 }
407 if (j == num_vreg)
408 power_setting[i].seq_val = INVALID_VREG;
409 break;
410
411 case SENSOR_VAF:
412 for (j = 0; j < num_vreg; j++) {
413 if (!strcmp(cam_vreg[j].reg_name, "cam_vaf")) {
414 CDBG("%s:%d i %d j %d cam_vaf\n",
415 __func__, __LINE__, i, j);
416 power_setting[i].seq_val = j;
417 if (VALIDATE_VOLTAGE(
418 cam_vreg[j].min_voltage,
419 cam_vreg[j].max_voltage,
420 power_setting[i].config_val)) {
421 cam_vreg[j].min_voltage =
422 cam_vreg[j].max_voltage =
423 power_setting[i].config_val;
424 }
425 break;
426 }
427 }
428 if (j == num_vreg)
429 power_setting[i].seq_val = INVALID_VREG;
430 break;
431
432 case SENSOR_CUSTOM_REG1:
433 for (j = 0; j < num_vreg; j++) {
434 if (!strcmp(cam_vreg[j].reg_name,
435 "cam_v_custom1")) {
436 CDBG("%s:%d i %d j %d cam_vcustom1\n",
437 __func__, __LINE__, i, j);
438 power_setting[i].seq_val = j;
439 if (VALIDATE_VOLTAGE(
440 cam_vreg[j].min_voltage,
441 cam_vreg[j].max_voltage,
442 power_setting[i].config_val)) {
443 cam_vreg[j].min_voltage =
444 cam_vreg[j].max_voltage =
445 power_setting[i].config_val;
446 }
447 break;
448 }
449 }
450 if (j == num_vreg)
451 power_setting[i].seq_val = INVALID_VREG;
452 break;
453 case SENSOR_CUSTOM_REG2:
454 for (j = 0; j < num_vreg; j++) {
455 if (!strcmp(cam_vreg[j].reg_name,
456 "cam_v_custom2")) {
457 CDBG("%s:%d i %d j %d cam_vcustom2\n",
458 __func__, __LINE__, i, j);
459 power_setting[i].seq_val = j;
460 if (VALIDATE_VOLTAGE(
461 cam_vreg[j].min_voltage,
462 cam_vreg[j].max_voltage,
463 power_setting[i].config_val)) {
464 cam_vreg[j].min_voltage =
465 cam_vreg[j].max_voltage =
466 power_setting[i].config_val;
467 }
468 break;
469 }
470 }
471 if (j == num_vreg)
472 power_setting[i].seq_val = INVALID_VREG;
473 break;
474
475 default: {
476 pr_err("%s:%d invalid seq_val %d\n", __func__,
477 __LINE__, power_setting[i].seq_val);
478 break;
479 }
480 }
481 }
482
483 return rc;
484}
485
486int32_t msm_camera_get_dt_gpio_req_tbl(struct device_node *of_node,
487 struct msm_camera_gpio_conf *gconf, uint16_t *gpio_array,
488 uint16_t gpio_array_size)
489{
490 int32_t rc = 0, i = 0;
491 uint32_t count = 0, *val_array = NULL;
492
493 if (!of_get_property(of_node, "qcom,gpio-req-tbl-num", &count))
494 return 0;
495
496 count /= sizeof(uint32_t);
497 if (!count) {
498 pr_err("%s qcom,gpio-req-tbl-num 0\n", __func__);
499 return 0;
500 }
501
502 val_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
503 if (!val_array)
504 return -ENOMEM;
505
506 gconf->cam_gpio_req_tbl = kcalloc(count, sizeof(struct gpio),
507 GFP_KERNEL);
508 if (!gconf->cam_gpio_req_tbl) {
509 rc = -ENOMEM;
510 goto free_val_array;
511 }
512 gconf->cam_gpio_req_tbl_size = count;
513
514 rc = of_property_read_u32_array(of_node, "qcom,gpio-req-tbl-num",
515 val_array, count);
516 if (rc < 0) {
517 pr_err("%s failed %d\n", __func__, __LINE__);
518 goto free_gpio_req_tbl;
519 }
520
521 for (i = 0; i < count; i++) {
522 if (val_array[i] >= gpio_array_size) {
523 pr_err("%s gpio req tbl index %d invalid\n",
524 __func__, val_array[i]);
525 return -EINVAL;
526 }
527 gconf->cam_gpio_req_tbl[i].gpio = gpio_array[val_array[i]];
528 CDBG("%s cam_gpio_req_tbl[%d].gpio = %d\n", __func__, i,
529 gconf->cam_gpio_req_tbl[i].gpio);
530 }
531
532 rc = of_property_read_u32_array(of_node, "qcom,gpio-req-tbl-flags",
533 val_array, count);
534 if (rc < 0) {
535 pr_err("%s failed %d\n", __func__, __LINE__);
536 goto free_gpio_req_tbl;
537 }
538
539 for (i = 0; i < count; i++) {
540 gconf->cam_gpio_req_tbl[i].flags = val_array[i];
541 CDBG("%s cam_gpio_req_tbl[%d].flags = %ld\n", __func__, i,
542 gconf->cam_gpio_req_tbl[i].flags);
543 }
544
545 for (i = 0; i < count; i++) {
546 rc = of_property_read_string_index(of_node,
547 "qcom,gpio-req-tbl-label", i,
548 &gconf->cam_gpio_req_tbl[i].label);
549 CDBG("%s cam_gpio_req_tbl[%d].label = %s\n", __func__, i,
550 gconf->cam_gpio_req_tbl[i].label);
551 if (rc < 0) {
552 pr_err("%s failed %d\n", __func__, __LINE__);
553 goto free_gpio_req_tbl;
554 }
555 }
556
557 kfree(val_array);
558
559 return rc;
560
561free_gpio_req_tbl:
562 kfree(gconf->cam_gpio_req_tbl);
563free_val_array:
564 kfree(val_array);
565 gconf->cam_gpio_req_tbl_size = 0;
566
567 return rc;
568}
569
570int msm_camera_init_gpio_pin_tbl(struct device_node *of_node,
571 struct msm_camera_gpio_conf *gconf, uint16_t *gpio_array,
572 uint16_t gpio_array_size)
573{
574 int rc = 0, val = 0;
575
576 gconf->gpio_num_info = kzalloc(sizeof(struct msm_camera_gpio_num_info),
577 GFP_KERNEL);
578 if (!gconf->gpio_num_info)
579 return -ENOMEM;
580
581 rc = of_property_read_u32(of_node, "qcom,gpio-vana", &val);
582 if (rc != -EINVAL) {
583 if (rc < 0) {
584 pr_err("%s:%d read qcom,gpio-vana failed rc %d\n",
585 __func__, __LINE__, rc);
586 goto free_gpio_info;
587 } else if (val >= gpio_array_size) {
588 pr_err("%s:%d qcom,gpio-vana invalid %d\n",
589 __func__, __LINE__, val);
590 rc = -EINVAL;
591 goto free_gpio_info;
592 }
593 gconf->gpio_num_info->gpio_num[SENSOR_VANA] =
594 gpio_array[val];
595 gconf->gpio_num_info->valid[SENSOR_VANA] = 1;
596 CDBG("%s qcom,gpio-vana %d\n", __func__,
597 gconf->gpio_num_info->gpio_num[SENSOR_VANA]);
598 }
599
600 rc = of_property_read_u32(of_node, "qcom,gpio-vio", &val);
601 if (rc != -EINVAL) {
602 if (rc < 0) {
603 pr_err("%s:%d read qcom,gpio-vio failed rc %d\n",
604 __func__, __LINE__, rc);
605 goto free_gpio_info;
606 } else if (val >= gpio_array_size) {
607 pr_err("%s:%d qcom,gpio-vio invalid %d\n",
608 __func__, __LINE__, val);
609 goto free_gpio_info;
610 }
611 gconf->gpio_num_info->gpio_num[SENSOR_VIO] =
612 gpio_array[val];
613 gconf->gpio_num_info->valid[SENSOR_VIO] = 1;
614 CDBG("%s qcom,gpio-vio %d\n", __func__,
615 gconf->gpio_num_info->gpio_num[SENSOR_VIO]);
616 }
617
618 rc = of_property_read_u32(of_node, "qcom,gpio-vaf", &val);
619 if (rc != -EINVAL) {
620 if (rc < 0) {
621 pr_err("%s:%d read qcom,gpio-vaf failed rc %d\n",
622 __func__, __LINE__, rc);
623 goto free_gpio_info;
624 } else if (val >= gpio_array_size) {
625 pr_err("%s:%d qcom,gpio-vaf invalid %d\n",
626 __func__, __LINE__, val);
627 rc = -EINVAL;
628 goto free_gpio_info;
629 }
630 gconf->gpio_num_info->gpio_num[SENSOR_VAF] =
631 gpio_array[val];
632 gconf->gpio_num_info->valid[SENSOR_VAF] = 1;
633 CDBG("%s qcom,gpio-vaf %d\n", __func__,
634 gconf->gpio_num_info->gpio_num[SENSOR_VAF]);
635 }
636
637 rc = of_property_read_u32(of_node, "qcom,gpio-vdig", &val);
638 if (rc != -EINVAL) {
639 if (rc < 0) {
640 pr_err("%s:%d read qcom,gpio-vdig failed rc %d\n",
641 __func__, __LINE__, rc);
642 goto free_gpio_info;
643 } else if (val >= gpio_array_size) {
644 pr_err("%s:%d qcom,gpio-vdig invalid %d\n",
645 __func__, __LINE__, val);
646 rc = -EINVAL;
647 goto free_gpio_info;
648 }
649 gconf->gpio_num_info->gpio_num[SENSOR_VDIG] =
650 gpio_array[val];
651 gconf->gpio_num_info->valid[SENSOR_VDIG] = 1;
652 CDBG("%s qcom,gpio-vdig %d\n", __func__,
653 gconf->gpio_num_info->gpio_num[SENSOR_VDIG]);
654 }
655
656 rc = of_property_read_u32(of_node, "qcom,gpio-reset", &val);
657 if (rc != -EINVAL) {
658 if (rc < 0) {
659 pr_err("%s:%d read qcom,gpio-reset failed rc %d\n",
660 __func__, __LINE__, rc);
661 goto free_gpio_info;
662 } else if (val >= gpio_array_size) {
663 pr_err("%s:%d qcom,gpio-reset invalid %d\n",
664 __func__, __LINE__, val);
665 rc = -EINVAL;
666 goto free_gpio_info;
667 }
668 gconf->gpio_num_info->gpio_num[SENSOR_RESET] =
669 gpio_array[val];
670 gconf->gpio_num_info->valid[SENSOR_RESET] = 1;
671 CDBG("%s qcom,gpio-reset %d\n", __func__,
672 gconf->gpio_num_info->gpio_num[SENSOR_RESET]);
673 }
674
675 rc = of_property_read_u32(of_node, "qcom,gpio-standby", &val);
676 if (rc != -EINVAL) {
677 if (rc < 0) {
678 pr_err("%s:%d read qcom,gpio-standby failed rc %d\n",
679 __func__, __LINE__, rc);
680 goto free_gpio_info;
681 } else if (val >= gpio_array_size) {
682 pr_err("%s:%d qcom,gpio-standby invalid %d\n",
683 __func__, __LINE__, val);
684 rc = -EINVAL;
685 goto free_gpio_info;
686 }
687 gconf->gpio_num_info->gpio_num[SENSOR_STANDBY] =
688 gpio_array[val];
689 gconf->gpio_num_info->valid[SENSOR_STANDBY] = 1;
690 CDBG("%s qcom,gpio-standby %d\n", __func__,
691 gconf->gpio_num_info->gpio_num[SENSOR_STANDBY]);
692 }
693
694 rc = of_property_read_u32(of_node, "qcom,gpio-af-pwdm", &val);
695 if (rc != -EINVAL) {
696 if (rc < 0) {
697 pr_err("%s:%d read qcom,gpio-af-pwdm failed rc %d\n",
698 __func__, __LINE__, rc);
699 goto free_gpio_info;
700 } else if (val >= gpio_array_size) {
701 pr_err("%s:%d qcom,gpio-af-pwdm invalid %d\n",
702 __func__, __LINE__, val);
703 rc = -EINVAL;
704 goto free_gpio_info;
705 }
706 gconf->gpio_num_info->gpio_num[SENSOR_VAF_PWDM] =
707 gpio_array[val];
708 gconf->gpio_num_info->valid[SENSOR_VAF_PWDM] = 1;
709 CDBG("%s qcom,gpio-af-pwdm %d\n", __func__,
710 gconf->gpio_num_info->gpio_num[SENSOR_VAF_PWDM]);
711 }
712
713 rc = of_property_read_u32(of_node, "qcom,gpio-custom1", &val);
714 if (rc != -EINVAL) {
715 if (rc < 0) {
716 pr_err("%s:%d read qcom,gpio-custom1 failed rc %d\n",
717 __func__, __LINE__, rc);
718 goto free_gpio_info;
719 } else if (val >= gpio_array_size) {
720 pr_err("%s:%d qcom,gpio-custom1 invalid %d\n",
721 __func__, __LINE__, val);
722 rc = -EINVAL;
723 goto free_gpio_info;
724 }
725 gconf->gpio_num_info->gpio_num[SENSOR_CUSTOM_GPIO1] =
726 gpio_array[val];
727 gconf->gpio_num_info->valid[SENSOR_CUSTOM_GPIO1] = 1;
728 CDBG("%s qcom,gpio-custom1 %d\n", __func__,
729 gconf->gpio_num_info->gpio_num[SENSOR_CUSTOM_GPIO1]);
730 }
731
732 rc = of_property_read_u32(of_node, "qcom,gpio-custom2", &val);
733 if (rc != -EINVAL) {
734 if (rc < 0) {
735 pr_err("%s:%d read qcom,gpio-custom2 failed rc %d\n",
736 __func__, __LINE__, rc);
737 goto free_gpio_info;
738 } else if (val >= gpio_array_size) {
739 pr_err("%s:%d qcom,gpio-custom2 invalid %d\n",
740 __func__, __LINE__, val);
741 rc = -EINVAL;
742 goto free_gpio_info;
743 }
744 gconf->gpio_num_info->gpio_num[SENSOR_CUSTOM_GPIO2] =
745 gpio_array[val];
746 gconf->gpio_num_info->valid[SENSOR_CUSTOM_GPIO2] = 1;
747 CDBG("%s qcom,gpio-custom2 %d\n", __func__,
748 gconf->gpio_num_info->gpio_num[SENSOR_CUSTOM_GPIO2]);
749 } else {
750 rc = 0;
751 }
752
753 return rc;
754
755free_gpio_info:
756 kfree(gconf->gpio_num_info);
757 gconf->gpio_num_info = NULL;
758 return rc;
759}
760
761int cam_sensor_get_dt_vreg_data(struct device_node *of_node,
762 struct camera_vreg_t **cam_vreg, int *num_vreg)
763{
764 int rc = 0, i = 0;
765 int32_t count = 0;
766 uint32_t *vreg_array = NULL;
767 struct camera_vreg_t *vreg = NULL;
768
769 count = of_property_count_strings(of_node, "qcom,cam-vreg-name");
770 CDBG("%s qcom,cam-vreg-name count %d\n", __func__, count);
771
772 if (!count || (count == -EINVAL)) {
773 pr_err("%s:%d number of entries is 0 or not present in dts\n",
774 __func__, __LINE__);
775 *num_vreg = 0;
776 return 0;
777 }
778
779 vreg = kcalloc(count, sizeof(*vreg), GFP_KERNEL);
780 if (!vreg)
781 return -ENOMEM;
782
783 *cam_vreg = vreg;
784 *num_vreg = count;
785 for (i = 0; i < count; i++) {
786 rc = of_property_read_string_index(of_node,
787 "qcom,cam-vreg-name", i,
788 &vreg[i].reg_name);
789 CDBG("%s reg_name[%d] = %s\n", __func__, i,
790 vreg[i].reg_name);
791 if (rc < 0) {
792 pr_err("%s failed %d\n", __func__, __LINE__);
793 goto free_vreg;
794 }
795 }
796
797 vreg_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
798 if (!vreg_array) {
799 rc = -ENOMEM;
800 goto free_vreg;
801 }
802
803 for (i = 0; i < count; i++)
804 vreg[i].type = VREG_TYPE_DEFAULT;
805
806 rc = of_property_read_u32_array(of_node, "qcom,cam-vreg-type",
807 vreg_array, count);
808 if (rc != -EINVAL) {
809 if (rc < 0) {
810 pr_err("%s failed %d\n", __func__, __LINE__);
811 goto free_vreg_array;
812 } else {
813 for (i = 0; i < count; i++) {
814 vreg[i].type = vreg_array[i];
815 CDBG("%s cam_vreg[%d].type = %d\n",
816 __func__, i, vreg[i].type);
817 }
818 }
819 } else {
820 CDBG("%s:%d no qcom,cam-vreg-type entries in dts\n",
821 __func__, __LINE__);
822 rc = 0;
823 }
824
825 rc = of_property_read_u32_array(of_node, "qcom,cam-vreg-min-voltage",
826 vreg_array, count);
827 if (rc != -EINVAL) {
828 if (rc < 0) {
829 pr_err("%s failed %d\n", __func__, __LINE__);
830 goto free_vreg_array;
831 } else {
832 for (i = 0; i < count; i++) {
833 vreg[i].min_voltage = vreg_array[i];
834 CDBG("%s cam_vreg[%d].min_voltage = %d\n",
835 __func__, i, vreg[i].min_voltage);
836 }
837 }
838 } else {
839 CDBG("%s:%d no qcom,cam-vreg-min-voltage entries in dts\n",
840 __func__, __LINE__);
841 rc = 0;
842 }
843
844 rc = of_property_read_u32_array(of_node, "qcom,cam-vreg-max-voltage",
845 vreg_array, count);
846 if (rc != -EINVAL) {
847 if (rc < 0) {
848 pr_err("%s failed %d\n", __func__, __LINE__);
849 goto free_vreg_array;
850 } else {
851 for (i = 0; i < count; i++) {
852 vreg[i].max_voltage = vreg_array[i];
853 CDBG("%s cam_vreg[%d].max_voltage = %d\n",
854 __func__, i, vreg[i].max_voltage);
855 }
856 }
857 } else {
858 CDBG("%s:%d no qcom,cam-vreg-max-voltage entries in dts\n",
859 __func__, __LINE__);
860 rc = 0;
861 }
862
863 rc = of_property_read_u32_array(of_node, "qcom,cam-vreg-op-mode",
864 vreg_array, count);
865 if (rc != -EINVAL) {
866 if (rc < 0) {
867 pr_err("%s failed %d\n", __func__, __LINE__);
868 goto free_vreg_array;
869 } else {
870 for (i = 0; i < count; i++) {
871 vreg[i].op_mode = vreg_array[i];
872 CDBG("%s cam_vreg[%d].op_mode = %d\n",
873 __func__, i, vreg[i].op_mode);
874 }
875 }
876 } else {
877 CDBG("%s:%d no qcom,cam-vreg-op-mode entries in dts\n",
878 __func__, __LINE__);
879 rc = 0;
880 }
881
882 kfree(vreg_array);
883
884 return rc;
885
886free_vreg_array:
887 kfree(vreg_array);
888free_vreg:
889 kfree(vreg);
890 *num_vreg = 0;
891
892 return rc;
893}
894
895int msm_camera_pinctrl_init(
896 struct msm_pinctrl_info *sensor_pctrl, struct device *dev) {
897
898 sensor_pctrl->pinctrl = devm_pinctrl_get(dev);
899 if (IS_ERR_OR_NULL(sensor_pctrl->pinctrl)) {
900 pr_err("%s:%d Getting pinctrl handle failed\n",
901 __func__, __LINE__);
902 return -EINVAL;
903 }
904 sensor_pctrl->gpio_state_active =
905 pinctrl_lookup_state(sensor_pctrl->pinctrl,
906 CAM_SENSOR_PINCTRL_STATE_DEFAULT);
907 if (IS_ERR_OR_NULL(sensor_pctrl->gpio_state_active)) {
908 pr_err("%s:%d Failed to get the active state pinctrl handle\n",
909 __func__, __LINE__);
910 return -EINVAL;
911 }
912 sensor_pctrl->gpio_state_suspend
913 = pinctrl_lookup_state(sensor_pctrl->pinctrl,
914 CAM_SENSOR_PINCTRL_STATE_SLEEP);
915 if (IS_ERR_OR_NULL(sensor_pctrl->gpio_state_suspend)) {
916 pr_err("%s:%d Failed to get the suspend state pinctrl handle\n",
917 __func__, __LINE__);
918 return -EINVAL;
919 }
920 return 0;
921}
922
923int msm_cam_sensor_handle_reg_gpio(int seq_type,
924 struct msm_camera_gpio_conf *gconf, int val)
925{
926
927 int gpio_offset = -1;
928
929 if (!gconf) {
930 pr_err("ERR:%s: Input Parameters are not proper\n", __func__);
931 return -EINVAL;
932 }
933 CDBG("%s: %d Seq type: %d, config: %d", __func__, __LINE__,
934 seq_type, val);
935
936 gpio_offset = seq_type;
937
938 if ((gconf->gpio_num_info->valid[gpio_offset] == 1)) {
939 CDBG("%s: %d VALID GPIO offset: %d, seqtype: %d\n",
940 __func__, __LINE__, gpio_offset, seq_type);
941 gpio_set_value_cansleep(
942 gconf->gpio_num_info->gpio_num
943 [gpio_offset], val);
944 }
945
946 return 0;
947}
948
949int32_t msm_sensor_driver_get_gpio_data(
950 struct msm_camera_gpio_conf **gpio_conf,
951 struct device_node *of_node)
952{
953 int32_t rc = 0, i = 0;
954 uint16_t *gpio_array = NULL;
955 int16_t gpio_array_size = 0;
956 struct msm_camera_gpio_conf *gconf = NULL;
957
958 /* Validate input parameters */
959 if (!of_node) {
960 pr_err("failed: invalid param of_node %pK", of_node);
961 return -EINVAL;
962 }
963
964 gpio_array_size = of_gpio_count(of_node);
965 CDBG("gpio count %d\n", gpio_array_size);
966 if (gpio_array_size <= 0)
967 return 0;
968
969 gconf = kzalloc(sizeof(*gconf), GFP_KERNEL);
970 if (!gconf)
971 return -ENOMEM;
972
973 *gpio_conf = gconf;
974
975 gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), GFP_KERNEL);
976 if (!gpio_array)
977 goto free_gpio_conf;
978
979 for (i = 0; i < gpio_array_size; i++) {
980 gpio_array[i] = of_get_gpio(of_node, i);
981 CDBG("gpio_array[%d] = %d", i, gpio_array[i]);
982 }
983 rc = msm_camera_get_dt_gpio_req_tbl(of_node, gconf, gpio_array,
984 gpio_array_size);
985 if (rc < 0) {
986 pr_err("failed in msm_camera_get_dt_gpio_req_tbl\n");
987 goto free_gpio_array;
988 }
989
990 rc = msm_camera_init_gpio_pin_tbl(of_node, gconf, gpio_array,
991 gpio_array_size);
992 if (rc < 0) {
993 pr_err("failed in msm_camera_init_gpio_pin_tbl\n");
994 goto free_gpio_req_tbl;
995 }
996 kfree(gpio_array);
997
998 return rc;
999
1000free_gpio_req_tbl:
1001 kfree(gconf->cam_gpio_req_tbl);
1002free_gpio_array:
1003 kfree(gpio_array);
1004free_gpio_conf:
1005 kfree(gconf);
1006 *gpio_conf = NULL;
1007
1008 return rc;
1009}
1010
1011int cam_sensor_core_power_up(struct cam_sensor_power_ctrl_t *ctrl)
1012{
1013 int rc = 0, index = 0, no_gpio = 0, ret = 0, num_vreg, j = 0;
1014 struct cam_sensor_power_setting *power_setting = NULL;
1015 struct camera_vreg_t *cam_vreg;
1016
1017 CDBG("%s:%d\n", __func__, __LINE__);
1018 if (!ctrl) {
1019 pr_err("failed ctrl %pK\n", ctrl);
1020 return -EINVAL;
1021 }
1022
1023 cam_vreg = ctrl->cam_vreg;
1024 num_vreg = ctrl->num_vreg;
1025
1026 if (ctrl->gpio_conf->cam_gpiomux_conf_tbl != NULL)
1027 CDBG("%s:%d mux install\n", __func__, __LINE__);
1028
1029 ret = msm_camera_pinctrl_init(&(ctrl->pinctrl_info), ctrl->dev);
1030 if (ret < 0) {
1031 pr_err("%s:%d Initialization of pinctrl failed\n",
1032 __func__, __LINE__);
1033 ctrl->cam_pinctrl_status = 0;
1034 } else {
1035 ctrl->cam_pinctrl_status = 1;
1036 }
1037 rc = msm_camera_request_gpio_table(
1038 ctrl->gpio_conf->cam_gpio_req_tbl,
1039 ctrl->gpio_conf->cam_gpio_req_tbl_size, 1);
1040 if (rc < 0)
1041 no_gpio = rc;
1042 if (ctrl->cam_pinctrl_status) {
1043 ret = pinctrl_select_state(ctrl->pinctrl_info.pinctrl,
1044 ctrl->pinctrl_info.gpio_state_active);
1045 if (ret)
1046 pr_err("%s:%d cannot set pin to active state",
1047 __func__, __LINE__);
1048 }
1049
1050 for (index = 0; index < ctrl->power_setting_size; index++) {
1051 CDBG("%s index %d\n", __func__, index);
1052 power_setting = &ctrl->power_setting[index];
1053
1054 switch (power_setting->seq_type) {
1055 case SENSOR_MCLK:
1056 if (power_setting->seq_val >= ctrl->clk_info_size) {
1057 pr_err("%s:%d :Error: clk index %d >= max %zu\n",
1058 __func__, __LINE__,
1059 power_setting->seq_val,
1060 ctrl->clk_info_size);
1061 goto power_up_failed;
1062 }
1063 for (j = 0; j < num_vreg; j++) {
1064 if (!strcmp(cam_vreg[j].reg_name,
1065 "cam_clk")) {
1066 CDBG("%s:%d Enable cam_clk: %d\n",
1067 __func__, __LINE__, j);
1068 msm_camera_config_single_vreg(ctrl->dev,
1069 &cam_vreg[j],
1070 (struct regulator **)
1071 &power_setting->data[0],
1072 1);
1073 }
1074 }
1075 if (power_setting->config_val)
1076 ctrl->clk_info[power_setting->seq_val].
1077 clk_rate = power_setting->config_val;
1078 rc = msm_camera_clk_enable(ctrl->dev,
1079 ctrl->clk_info, ctrl->clk_ptr,
1080 ctrl->clk_info_size, true);
1081 if (rc < 0) {
1082 pr_err("%s: clk enable failed\n", __func__);
1083 goto power_up_failed;
1084 }
1085 break;
1086 case SENSOR_RESET:
1087 case SENSOR_STANDBY:
1088 case SENSOR_CUSTOM_GPIO1:
1089 case SENSOR_CUSTOM_GPIO2:
1090 if (no_gpio) {
1091 pr_err("%s: request gpio failed\n", __func__);
1092 return no_gpio;
1093 }
1094 if (power_setting->seq_val >= CAM_VREG_MAX ||
1095 !ctrl->gpio_conf->gpio_num_info) {
1096 pr_err("%s gpio index %d >= max %d\n", __func__,
1097 power_setting->seq_val,
1098 CAM_VREG_MAX);
1099 goto power_up_failed;
1100 }
1101 CDBG("%s:%d gpio set val %d\n",
1102 __func__, __LINE__,
1103 ctrl->gpio_conf->gpio_num_info->gpio_num
1104 [power_setting->seq_val]);
1105
1106 rc = msm_cam_sensor_handle_reg_gpio(
1107 power_setting->seq_type,
1108 ctrl->gpio_conf, 1);
1109 if (rc < 0) {
1110 pr_err("ERR:%s Error in handling VREG GPIO\n",
1111 __func__);
1112 goto power_up_failed;
1113 }
1114 break;
1115 case SENSOR_VANA:
1116 case SENSOR_VDIG:
1117 case SENSOR_VIO:
1118 case SENSOR_VAF:
1119 case SENSOR_VAF_PWDM:
1120 case SENSOR_CUSTOM_REG1:
1121 case SENSOR_CUSTOM_REG2:
1122 if (power_setting->seq_val == INVALID_VREG)
1123 break;
1124
1125 if (power_setting->seq_val >= CAM_VREG_MAX) {
1126 pr_err("%s vreg index %d >= max %d\n", __func__,
1127 power_setting->seq_val,
1128 CAM_VREG_MAX);
1129 goto power_up_failed;
1130 }
1131 if (power_setting->seq_val < ctrl->num_vreg)
1132 msm_camera_config_single_vreg(ctrl->dev,
1133 &ctrl->cam_vreg
1134 [power_setting->seq_val],
1135 (struct regulator **)
1136 &power_setting->data[0],
1137 1);
1138 else
1139 pr_err("%s: %d usr_idx:%d dts_idx:%d\n",
1140 __func__, __LINE__,
1141 power_setting->seq_val, ctrl->num_vreg);
1142
1143 rc = msm_cam_sensor_handle_reg_gpio(
1144 power_setting->seq_type,
1145 ctrl->gpio_conf, 1);
1146 if (rc < 0) {
1147 pr_err("ERR:%s Error in handling VREG GPIO\n",
1148 __func__);
1149 goto power_up_failed;
1150 }
1151 break;
1152 default:
1153 pr_err("%s error power seq type %d\n", __func__,
1154 power_setting->seq_type);
1155 break;
1156 }
1157 if (power_setting->delay > 20)
1158 msleep(power_setting->delay);
1159 else if (power_setting->delay)
1160 usleep_range(power_setting->delay * 1000,
1161 (power_setting->delay * 1000) + 1000);
1162 }
1163
1164 return 0;
1165power_up_failed:
1166 pr_err("%s:%d failed\n", __func__, __LINE__);
1167 for (index--; index >= 0; index--) {
1168 CDBG("%s index %d\n", __func__, index);
1169 power_setting = &ctrl->power_setting[index];
1170 CDBG("%s type %d\n", __func__, power_setting->seq_type);
1171 switch (power_setting->seq_type) {
1172 case SENSOR_RESET:
1173 case SENSOR_STANDBY:
1174 case SENSOR_CUSTOM_GPIO1:
1175 case SENSOR_CUSTOM_GPIO2:
1176 if (!ctrl->gpio_conf->gpio_num_info)
1177 continue;
1178 if (!ctrl->gpio_conf->gpio_num_info->valid
1179 [power_setting->seq_val])
1180 continue;
1181 gpio_set_value_cansleep(
1182 ctrl->gpio_conf->gpio_num_info->gpio_num
1183 [power_setting->seq_val], GPIOF_OUT_INIT_LOW);
1184 break;
1185 case SENSOR_VANA:
1186 case SENSOR_VDIG:
1187 case SENSOR_VIO:
1188 case SENSOR_VAF:
1189 case SENSOR_VAF_PWDM:
1190 case SENSOR_CUSTOM_REG1:
1191 case SENSOR_CUSTOM_REG2:
1192 if (power_setting->seq_val < ctrl->num_vreg)
1193 msm_camera_config_single_vreg(ctrl->dev,
1194 &ctrl->cam_vreg
1195 [power_setting->seq_val],
1196 (struct regulator **)
1197 &power_setting->data[0],
1198 0);
1199 else
1200 pr_err("%s:%d:seq_val: %d > num_vreg: %d\n",
1201 __func__, __LINE__,
1202 power_setting->seq_val, ctrl->num_vreg);
1203
1204 msm_cam_sensor_handle_reg_gpio(power_setting->seq_type,
1205 ctrl->gpio_conf, GPIOF_OUT_INIT_LOW);
1206 break;
1207 default:
1208 pr_err("%s error power seq type %d\n", __func__,
1209 power_setting->seq_type);
1210 break;
1211 }
1212 if (power_setting->delay > 20) {
1213 msleep(power_setting->delay);
1214 } else if (power_setting->delay) {
1215 usleep_range(power_setting->delay * 1000,
1216 (power_setting->delay * 1000) + 1000);
1217 }
1218 }
1219 if (ctrl->cam_pinctrl_status) {
1220 ret = pinctrl_select_state(ctrl->pinctrl_info.pinctrl,
1221 ctrl->pinctrl_info.gpio_state_suspend);
1222 if (ret)
1223 pr_err("%s:%d cannot set pin to suspend state\n",
1224 __func__, __LINE__);
1225 devm_pinctrl_put(ctrl->pinctrl_info.pinctrl);
1226 }
1227 ctrl->cam_pinctrl_status = 0;
1228 msm_camera_request_gpio_table(
1229 ctrl->gpio_conf->cam_gpio_req_tbl,
1230 ctrl->gpio_conf->cam_gpio_req_tbl_size, 0);
1231
1232 return rc;
1233}
1234
1235static struct cam_sensor_power_setting*
1236msm_camera_get_power_settings(struct cam_sensor_power_ctrl_t *ctrl,
1237 enum msm_camera_power_seq_type seq_type,
1238 uint16_t seq_val)
1239{
1240 struct cam_sensor_power_setting *power_setting, *ps = NULL;
1241 int idx;
1242
1243 for (idx = 0; idx < ctrl->power_setting_size; idx++) {
1244 power_setting = &ctrl->power_setting[idx];
1245 if (power_setting->seq_type == seq_type &&
1246 power_setting->seq_val == seq_val) {
1247 ps = power_setting;
1248 return ps;
1249 }
1250
1251 }
1252
1253 return ps;
1254}
1255
1256static int cam_config_mclk_reg(struct cam_sensor_power_ctrl_t *ctrl,
1257 int32_t index)
1258{
1259 struct camera_vreg_t *cam_vreg;
1260 int32_t num_vreg = 0, j = 0, rc = 0, idx = 0;
1261 struct cam_sensor_power_setting *ps = NULL;
1262 struct cam_sensor_power_setting *pd = NULL;
1263
1264 cam_vreg = ctrl->cam_vreg;
1265 num_vreg = ctrl->num_vreg;
1266 pd = &ctrl->power_down_setting[index];
1267
1268 for (j = 0; j < num_vreg; j++) {
1269 if (!strcmp(cam_vreg[j].reg_name, "cam_clk")) {
1270
1271 ps = NULL;
1272 for (idx = 0; idx <
1273 ctrl->power_setting_size; idx++) {
1274 if (ctrl->power_setting[idx].
1275 seq_type == pd->seq_type) {
1276 ps = &ctrl->power_setting[idx];
1277 break;
1278 }
1279 }
1280
1281 if (ps != NULL)
1282 msm_camera_config_single_vreg(
1283 ctrl->dev,
1284 &cam_vreg[j],
1285 (struct regulator **)
1286 &ps->data[0], 0);
1287 }
1288 }
1289
1290 return rc;
1291}
1292
1293int msm_camera_power_down(struct cam_sensor_power_ctrl_t *ctrl)
1294{
1295 int index = 0, ret = 0, num_vreg = 0;
1296 struct cam_sensor_power_setting *pd = NULL;
1297 struct cam_sensor_power_setting *ps;
1298 struct camera_vreg_t *cam_vreg;
1299
1300 CDBG("%s:%d\n", __func__, __LINE__);
1301 if (!ctrl) {
1302 pr_err("failed ctrl %pK\n", ctrl);
1303 return -EINVAL;
1304 }
1305
1306 cam_vreg = ctrl->cam_vreg;
1307 num_vreg = ctrl->num_vreg;
1308
1309 for (index = 0; index < ctrl->power_down_setting_size; index++) {
1310 CDBG("%s index %d\n", __func__, index);
1311 pd = &ctrl->power_down_setting[index];
1312 ps = NULL;
1313 CDBG("%s type %d\n", __func__, pd->seq_type);
1314 switch (pd->seq_type) {
1315 case SENSOR_MCLK:
1316 ret = cam_config_mclk_reg(ctrl, index);
1317 if (ret < 0) {
1318 pr_err("%s:%d :Error: in config clk reg\n",
1319 __func__, __LINE__);
1320 return ret;
1321 }
1322 msm_camera_clk_enable(ctrl->dev,
1323 ctrl->clk_info, ctrl->clk_ptr,
1324 ctrl->clk_info_size, false);
1325 break;
1326 case SENSOR_RESET:
1327 case SENSOR_STANDBY:
1328 case SENSOR_CUSTOM_GPIO1:
1329 case SENSOR_CUSTOM_GPIO2:
1330 if (!ctrl->gpio_conf->gpio_num_info->valid
1331 [pd->seq_val])
1332 continue;
1333 gpio_set_value_cansleep(
1334 ctrl->gpio_conf->gpio_num_info->gpio_num
1335 [pd->seq_val],
1336 (int) pd->config_val);
1337 break;
1338 case SENSOR_VANA:
1339 case SENSOR_VDIG:
1340 case SENSOR_VIO:
1341 case SENSOR_VAF:
1342 case SENSOR_VAF_PWDM:
1343 case SENSOR_CUSTOM_REG1:
1344 case SENSOR_CUSTOM_REG2:
1345 if (pd->seq_val == INVALID_VREG)
1346 break;
1347 ps = msm_camera_get_power_settings(
1348 ctrl, pd->seq_type,
1349 pd->seq_val);
1350 if (ps) {
1351 if (pd->seq_val < ctrl->num_vreg)
1352 msm_camera_config_single_vreg(ctrl->dev,
1353 &ctrl->cam_vreg
1354 [pd->seq_val],
1355 (struct regulator **)
1356 &ps->data[0],
1357 0);
1358 else
1359 pr_err("%s:%d:seq_val:%d > num_vreg: %d\n",
1360 __func__, __LINE__, pd->seq_val,
1361 ctrl->num_vreg);
1362 } else
1363 pr_err("%s error in power up/down seq data\n",
1364 __func__);
1365 ret = msm_cam_sensor_handle_reg_gpio(pd->seq_type,
1366 ctrl->gpio_conf, GPIOF_OUT_INIT_LOW);
1367 if (ret < 0)
1368 pr_err("ERR:%s Error while disabling VREG GPIO\n",
1369 __func__);
1370 break;
1371 default:
1372 pr_err("%s error power seq type %d\n", __func__,
1373 pd->seq_type);
1374 break;
1375 }
1376 if (pd->delay > 20)
1377 msleep(pd->delay);
1378 else if (pd->delay)
1379 usleep_range(pd->delay * 1000,
1380 (pd->delay * 1000) + 1000);
1381 }
1382
1383 if (ctrl->cam_pinctrl_status) {
1384 ret = pinctrl_select_state(ctrl->pinctrl_info.pinctrl,
1385 ctrl->pinctrl_info.gpio_state_suspend);
1386 if (ret)
1387 pr_err("%s:%d cannot set pin to suspend state",
1388 __func__, __LINE__);
1389 devm_pinctrl_put(ctrl->pinctrl_info.pinctrl);
1390 }
1391
1392 ctrl->cam_pinctrl_status = 0;
1393 msm_camera_request_gpio_table(
1394 ctrl->gpio_conf->cam_gpio_req_tbl,
1395 ctrl->gpio_conf->cam_gpio_req_tbl_size, 0);
1396
1397 return 0;
1398}
1399