blob: 224763925a0f379e083871ff4fa2746bb710dbc1 [file] [log] [blame]
Raja Mallikff6c75b2019-01-29 16:52:37 +05301/* Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
Raja Mallikc7e256f2018-12-06 17:36:28 +05302 *
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/module.h>
14#include <cam_sensor_cmn_header.h>
15#include "cam_actuator_core.h"
16#include "cam_sensor_util.h"
17#include "cam_trace.h"
18#include "cam_res_mgr_api.h"
19#include "cam_common_util.h"
Raja Mallikff6c75b2019-01-29 16:52:37 +053020#include "cam_packet_util.h"
Raja Mallikc7e256f2018-12-06 17:36:28 +053021
22int32_t cam_actuator_construct_default_power_setting(
23 struct cam_sensor_power_ctrl_t *power_info)
24{
25 int rc = 0;
26
27 power_info->power_setting_size = 1;
28 power_info->power_setting =
29 (struct cam_sensor_power_setting *)
30 kzalloc(sizeof(struct cam_sensor_power_setting),
31 GFP_KERNEL);
32 if (!power_info->power_setting)
33 return -ENOMEM;
34
35 power_info->power_setting[0].seq_type = SENSOR_VAF;
36 power_info->power_setting[0].seq_val = CAM_VAF;
37 power_info->power_setting[0].config_val = 1;
38 power_info->power_setting[0].delay = 2;
39
40 power_info->power_down_setting_size = 1;
41 power_info->power_down_setting =
42 (struct cam_sensor_power_setting *)
43 kzalloc(sizeof(struct cam_sensor_power_setting),
44 GFP_KERNEL);
45 if (!power_info->power_down_setting) {
46 rc = -ENOMEM;
47 goto free_power_settings;
48 }
49
50 power_info->power_down_setting[0].seq_type = SENSOR_VAF;
51 power_info->power_down_setting[0].seq_val = CAM_VAF;
52 power_info->power_down_setting[0].config_val = 0;
53
54 return rc;
55
56free_power_settings:
57 kfree(power_info->power_setting);
58 power_info->power_setting = NULL;
59 power_info->power_setting_size = 0;
60 return rc;
61}
62
63static int32_t cam_actuator_power_up(struct cam_actuator_ctrl_t *a_ctrl)
64{
65 int rc = 0;
66 struct cam_hw_soc_info *soc_info =
67 &a_ctrl->soc_info;
68 struct cam_actuator_soc_private *soc_private;
69 struct cam_sensor_power_ctrl_t *power_info;
70
71 soc_private =
72 (struct cam_actuator_soc_private *)a_ctrl->soc_info.soc_private;
73 power_info = &soc_private->power_info;
74
75 if ((power_info->power_setting == NULL) &&
76 (power_info->power_down_setting == NULL)) {
77 CAM_INFO(CAM_ACTUATOR,
78 "Using default power settings");
79 rc = cam_actuator_construct_default_power_setting(power_info);
80 if (rc < 0) {
81 CAM_ERR(CAM_ACTUATOR,
82 "Construct default actuator power setting failed.");
83 return rc;
84 }
85 }
86
87 /* Parse and fill vreg params for power up settings */
88 rc = msm_camera_fill_vreg_params(
89 &a_ctrl->soc_info,
90 power_info->power_setting,
91 power_info->power_setting_size);
92 if (rc) {
93 CAM_ERR(CAM_ACTUATOR,
94 "failed to fill vreg params for power up rc:%d", rc);
95 return rc;
96 }
97
98 /* Parse and fill vreg params for power down settings*/
99 rc = msm_camera_fill_vreg_params(
100 &a_ctrl->soc_info,
101 power_info->power_down_setting,
102 power_info->power_down_setting_size);
103 if (rc) {
104 CAM_ERR(CAM_ACTUATOR,
105 "failed to fill vreg params power down rc:%d", rc);
106 return rc;
107 }
108
109 power_info->dev = soc_info->dev;
110
111 rc = cam_sensor_core_power_up(power_info, soc_info);
112 if (rc) {
113 CAM_ERR(CAM_ACTUATOR,
114 "failed in actuator power up rc %d", rc);
115 return rc;
116 }
117
118 rc = camera_io_init(&a_ctrl->io_master_info);
119 if (rc < 0)
120 CAM_ERR(CAM_ACTUATOR, "cci init failed: rc: %d", rc);
121
122 return rc;
123}
124
125static int32_t cam_actuator_power_down(struct cam_actuator_ctrl_t *a_ctrl)
126{
127 int32_t rc = 0;
128 struct cam_sensor_power_ctrl_t *power_info;
129 struct cam_hw_soc_info *soc_info = &a_ctrl->soc_info;
130 struct cam_actuator_soc_private *soc_private;
131
132 if (!a_ctrl) {
133 CAM_ERR(CAM_ACTUATOR, "failed: a_ctrl %pK", a_ctrl);
134 return -EINVAL;
135 }
136
137 soc_private =
138 (struct cam_actuator_soc_private *)a_ctrl->soc_info.soc_private;
139 power_info = &soc_private->power_info;
140 soc_info = &a_ctrl->soc_info;
141
142 if (!power_info) {
143 CAM_ERR(CAM_ACTUATOR, "failed: power_info %pK", power_info);
144 return -EINVAL;
145 }
146 rc = cam_sensor_util_power_down(power_info, soc_info);
147 if (rc) {
148 CAM_ERR(CAM_ACTUATOR, "power down the core is failed:%d", rc);
149 return rc;
150 }
151
152 camera_io_release(&a_ctrl->io_master_info);
153
154 return rc;
155}
156
157static int32_t cam_actuator_i2c_modes_util(
158 struct camera_io_master *io_master_info,
159 struct i2c_settings_list *i2c_list)
160{
161 int32_t rc = 0;
162 uint32_t i, size;
163
164 if (i2c_list->op_code == CAM_SENSOR_I2C_WRITE_RANDOM) {
165 rc = camera_io_dev_write(io_master_info,
166 &(i2c_list->i2c_settings));
167 if (rc < 0) {
168 CAM_ERR(CAM_ACTUATOR,
169 "Failed to random write I2C settings: %d",
170 rc);
171 return rc;
172 }
173 } else if (i2c_list->op_code == CAM_SENSOR_I2C_WRITE_SEQ) {
174 rc = camera_io_dev_write_continuous(
175 io_master_info,
176 &(i2c_list->i2c_settings),
177 0);
178 if (rc < 0) {
179 CAM_ERR(CAM_ACTUATOR,
180 "Failed to seq write I2C settings: %d",
181 rc);
182 return rc;
183 }
184 } else if (i2c_list->op_code == CAM_SENSOR_I2C_WRITE_BURST) {
185 rc = camera_io_dev_write_continuous(
186 io_master_info,
187 &(i2c_list->i2c_settings),
188 1);
189 if (rc < 0) {
190 CAM_ERR(CAM_ACTUATOR,
191 "Failed to burst write I2C settings: %d",
192 rc);
193 return rc;
194 }
195 } else if (i2c_list->op_code == CAM_SENSOR_I2C_POLL) {
196 size = i2c_list->i2c_settings.size;
197 for (i = 0; i < size; i++) {
198 rc = camera_io_dev_poll(
199 io_master_info,
200 i2c_list->i2c_settings.reg_setting[i].reg_addr,
201 i2c_list->i2c_settings.reg_setting[i].reg_data,
202 i2c_list->i2c_settings.reg_setting[i].data_mask,
203 i2c_list->i2c_settings.addr_type,
204 i2c_list->i2c_settings.data_type,
205 i2c_list->i2c_settings.reg_setting[i].delay);
206 if (rc < 0) {
207 CAM_ERR(CAM_ACTUATOR,
208 "i2c poll apply setting Fail: %d", rc);
209 return rc;
210 }
211 }
212 }
213
214 return rc;
215}
216
217int32_t cam_actuator_slaveInfo_pkt_parser(struct cam_actuator_ctrl_t *a_ctrl,
Raja Mallikff6c75b2019-01-29 16:52:37 +0530218 uint32_t *cmd_buf, size_t len)
Raja Mallikc7e256f2018-12-06 17:36:28 +0530219{
220 int32_t rc = 0;
221 struct cam_cmd_i2c_info *i2c_info;
222
Raja Mallikff6c75b2019-01-29 16:52:37 +0530223 if (!a_ctrl || !cmd_buf || (len < sizeof(struct cam_cmd_i2c_info))) {
Raja Mallikc7e256f2018-12-06 17:36:28 +0530224 CAM_ERR(CAM_ACTUATOR, "Invalid Args");
225 return -EINVAL;
226 }
227
228 i2c_info = (struct cam_cmd_i2c_info *)cmd_buf;
229 if (a_ctrl->io_master_info.master_type == CCI_MASTER) {
230 a_ctrl->io_master_info.cci_client->cci_i2c_master =
231 a_ctrl->cci_i2c_master;
232 a_ctrl->io_master_info.cci_client->i2c_freq_mode =
233 i2c_info->i2c_freq_mode;
234 a_ctrl->io_master_info.cci_client->sid =
235 i2c_info->slave_addr >> 1;
236 CAM_DBG(CAM_ACTUATOR, "Slave addr: 0x%x Freq Mode: %d",
237 i2c_info->slave_addr, i2c_info->i2c_freq_mode);
238 } else if (a_ctrl->io_master_info.master_type == I2C_MASTER) {
239 a_ctrl->io_master_info.client->addr = i2c_info->slave_addr;
240 CAM_DBG(CAM_ACTUATOR, "Slave addr: 0x%x", i2c_info->slave_addr);
241 } else {
242 CAM_ERR(CAM_ACTUATOR, "Invalid Master type: %d",
243 a_ctrl->io_master_info.master_type);
244 rc = -EINVAL;
245 }
246
247 return rc;
248}
249
250int32_t cam_actuator_apply_settings(struct cam_actuator_ctrl_t *a_ctrl,
251 struct i2c_settings_array *i2c_set)
252{
253 struct i2c_settings_list *i2c_list;
254 int32_t rc = 0;
255
256 if (a_ctrl == NULL || i2c_set == NULL) {
257 CAM_ERR(CAM_ACTUATOR, "Invalid Args");
258 return -EINVAL;
259 }
260
261 if (i2c_set->is_settings_valid != 1) {
262 CAM_ERR(CAM_ACTUATOR, " Invalid settings");
263 return -EINVAL;
264 }
265
266 list_for_each_entry(i2c_list,
267 &(i2c_set->list_head), list) {
268 rc = cam_actuator_i2c_modes_util(
269 &(a_ctrl->io_master_info),
270 i2c_list);
271 if (rc < 0) {
272 CAM_ERR(CAM_ACTUATOR,
273 "Failed to apply settings: %d",
274 rc);
275 } else {
276 CAM_DBG(CAM_ACTUATOR,
277 "Success:request ID: %d",
278 i2c_set->request_id);
279 }
280 }
281
282 return rc;
283}
284
285int32_t cam_actuator_apply_request(struct cam_req_mgr_apply_request *apply)
286{
287 int32_t rc = 0, request_id, del_req_id;
288 struct cam_actuator_ctrl_t *a_ctrl = NULL;
289
290 if (!apply) {
291 CAM_ERR(CAM_ACTUATOR, "Invalid Input Args");
292 return -EINVAL;
293 }
294
295 a_ctrl = (struct cam_actuator_ctrl_t *)
296 cam_get_device_priv(apply->dev_hdl);
297 if (!a_ctrl) {
298 CAM_ERR(CAM_ACTUATOR, "Device data is NULL");
299 return -EINVAL;
300 }
301 request_id = apply->request_id % MAX_PER_FRAME_ARRAY;
302
303 trace_cam_apply_req("Actuator", apply->request_id);
304
305 CAM_DBG(CAM_ACTUATOR, "Request Id: %lld", apply->request_id);
306 mutex_lock(&(a_ctrl->actuator_mutex));
307 if ((apply->request_id ==
308 a_ctrl->i2c_data.per_frame[request_id].request_id) &&
309 (a_ctrl->i2c_data.per_frame[request_id].is_settings_valid)
310 == 1) {
311 rc = cam_actuator_apply_settings(a_ctrl,
312 &a_ctrl->i2c_data.per_frame[request_id]);
313 if (rc < 0) {
314 CAM_ERR(CAM_ACTUATOR,
315 "Failed in applying the request: %lld\n",
316 apply->request_id);
317 goto release_mutex;
318 }
319 }
320 del_req_id = (request_id +
321 MAX_PER_FRAME_ARRAY - MAX_SYSTEM_PIPELINE_DELAY) %
322 MAX_PER_FRAME_ARRAY;
323
324 if (apply->request_id >
325 a_ctrl->i2c_data.per_frame[del_req_id].request_id) {
326 a_ctrl->i2c_data.per_frame[del_req_id].request_id = 0;
327 rc = delete_request(&a_ctrl->i2c_data.per_frame[del_req_id]);
328 if (rc < 0) {
329 CAM_ERR(CAM_ACTUATOR,
330 "Fail deleting the req: %d err: %d\n",
331 del_req_id, rc);
332 goto release_mutex;
333 }
334 } else {
335 CAM_DBG(CAM_ACTUATOR, "No Valid Req to clean Up");
336 }
337
338release_mutex:
339 mutex_unlock(&(a_ctrl->actuator_mutex));
340 return rc;
341}
342
343int32_t cam_actuator_establish_link(
344 struct cam_req_mgr_core_dev_link_setup *link)
345{
346 struct cam_actuator_ctrl_t *a_ctrl = NULL;
347
348 if (!link) {
349 CAM_ERR(CAM_ACTUATOR, "Invalid Args");
350 return -EINVAL;
351 }
352
353 a_ctrl = (struct cam_actuator_ctrl_t *)
354 cam_get_device_priv(link->dev_hdl);
355 if (!a_ctrl) {
356 CAM_ERR(CAM_ACTUATOR, "Device data is NULL");
357 return -EINVAL;
358 }
359
360 mutex_lock(&(a_ctrl->actuator_mutex));
361 if (link->link_enable) {
362 a_ctrl->bridge_intf.link_hdl = link->link_hdl;
363 a_ctrl->bridge_intf.crm_cb = link->crm_cb;
364 } else {
365 a_ctrl->bridge_intf.link_hdl = -1;
366 a_ctrl->bridge_intf.crm_cb = NULL;
367 }
368 mutex_unlock(&(a_ctrl->actuator_mutex));
369
370 return 0;
371}
372
373static void cam_actuator_update_req_mgr(
374 struct cam_actuator_ctrl_t *a_ctrl,
375 struct cam_packet *csl_packet)
376{
377 struct cam_req_mgr_add_request add_req;
378
379 add_req.link_hdl = a_ctrl->bridge_intf.link_hdl;
380 add_req.req_id = csl_packet->header.request_id;
381 add_req.dev_hdl = a_ctrl->bridge_intf.device_hdl;
382 add_req.skip_before_applying = 0;
383
384 if (a_ctrl->bridge_intf.crm_cb &&
385 a_ctrl->bridge_intf.crm_cb->add_req) {
386 a_ctrl->bridge_intf.crm_cb->add_req(&add_req);
387 CAM_DBG(CAM_ACTUATOR, "Request Id: %lld added to CRM",
388 add_req.req_id);
389 } else {
390 CAM_ERR(CAM_ACTUATOR, "Can't add Request ID: %lld to CRM",
391 csl_packet->header.request_id);
392 }
393}
394
395int32_t cam_actuator_publish_dev_info(struct cam_req_mgr_device_info *info)
396{
397 if (!info) {
398 CAM_ERR(CAM_ACTUATOR, "Invalid Args");
399 return -EINVAL;
400 }
401
402 info->dev_id = CAM_REQ_MGR_DEVICE_ACTUATOR;
403 strlcpy(info->name, CAM_ACTUATOR_NAME, sizeof(info->name));
404 info->p_delay = 1;
405 info->trigger = CAM_TRIGGER_POINT_SOF;
406
407 return 0;
408}
409
410int32_t cam_actuator_i2c_pkt_parse(struct cam_actuator_ctrl_t *a_ctrl,
411 void *arg)
412{
413 int32_t rc = 0;
414 int32_t i = 0;
415 uint32_t total_cmd_buf_in_bytes = 0;
416 size_t len_of_buff = 0;
Raja Mallikff6c75b2019-01-29 16:52:37 +0530417 size_t remain_len = 0;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530418 uint32_t *offset = NULL;
419 uint32_t *cmd_buf = NULL;
420 uintptr_t generic_ptr;
Raja Mallikff6c75b2019-01-29 16:52:37 +0530421 uintptr_t generic_pkt_ptr;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530422 struct common_header *cmm_hdr = NULL;
423 struct cam_control *ioctl_ctrl = NULL;
424 struct cam_packet *csl_packet = NULL;
425 struct cam_config_dev_cmd config;
426 struct i2c_data_settings *i2c_data = NULL;
427 struct i2c_settings_array *i2c_reg_settings = NULL;
428 struct cam_cmd_buf_desc *cmd_desc = NULL;
429 struct cam_actuator_soc_private *soc_private = NULL;
430 struct cam_sensor_power_ctrl_t *power_info = NULL;
431
432 if (!a_ctrl || !arg) {
433 CAM_ERR(CAM_ACTUATOR, "Invalid Args");
434 return -EINVAL;
435 }
436
437 soc_private =
438 (struct cam_actuator_soc_private *)a_ctrl->soc_info.soc_private;
439
440 power_info = &soc_private->power_info;
441
442 ioctl_ctrl = (struct cam_control *)arg;
443 if (copy_from_user(&config,
444 u64_to_user_ptr(ioctl_ctrl->handle),
445 sizeof(config)))
446 return -EFAULT;
447 rc = cam_mem_get_cpu_buf(config.packet_handle,
Raja Mallikff6c75b2019-01-29 16:52:37 +0530448 &generic_pkt_ptr, &len_of_buff);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530449 if (rc < 0) {
450 CAM_ERR(CAM_ACTUATOR, "Error in converting command Handle %d",
451 rc);
452 return rc;
453 }
454
Raja Mallikff6c75b2019-01-29 16:52:37 +0530455 remain_len = len_of_buff;
456 if ((sizeof(struct cam_packet) > len_of_buff) ||
457 ((size_t)config.offset >= len_of_buff -
458 sizeof(struct cam_packet))) {
Raja Mallikc7e256f2018-12-06 17:36:28 +0530459 CAM_ERR(CAM_ACTUATOR,
Raja Mallikff6c75b2019-01-29 16:52:37 +0530460 "Inval cam_packet strut size: %zu, len_of_buff: %zu",
461 sizeof(struct cam_packet), len_of_buff);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530462 return -EINVAL;
463 }
464
Raja Mallikff6c75b2019-01-29 16:52:37 +0530465 remain_len -= (size_t)config.offset;
466 csl_packet = (struct cam_packet *)
467 (generic_pkt_ptr + (uint32_t)config.offset);
468
469 if (cam_packet_util_validate_packet(csl_packet,
470 remain_len)) {
471 CAM_ERR(CAM_ACTUATOR, "Invalid packet params");
472 return -EINVAL;
473 }
474
475 CAM_DBG(CAM_ACTUATOR, "Pkt opcode: %d", csl_packet->header.op_code);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530476
477 if ((csl_packet->header.op_code & 0xFFFFFF) !=
478 CAM_ACTUATOR_PACKET_OPCODE_INIT &&
479 csl_packet->header.request_id <= a_ctrl->last_flush_req
480 && a_ctrl->last_flush_req != 0) {
481 CAM_DBG(CAM_ACTUATOR,
482 "reject request %lld, last request to flush %lld",
483 csl_packet->header.request_id, a_ctrl->last_flush_req);
Raja Mallikff6c75b2019-01-29 16:52:37 +0530484 rc = -EINVAL;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530485 }
486
487 if (csl_packet->header.request_id > a_ctrl->last_flush_req)
488 a_ctrl->last_flush_req = 0;
489
490 switch (csl_packet->header.op_code & 0xFFFFFF) {
491 case CAM_ACTUATOR_PACKET_OPCODE_INIT:
492 offset = (uint32_t *)&csl_packet->payload;
493 offset += (csl_packet->cmd_buf_offset / sizeof(uint32_t));
494 cmd_desc = (struct cam_cmd_buf_desc *)(offset);
495
496 /* Loop through multiple command buffers */
497 for (i = 0; i < csl_packet->num_cmd_buf; i++) {
498 total_cmd_buf_in_bytes = cmd_desc[i].length;
499 if (!total_cmd_buf_in_bytes)
500 continue;
501 rc = cam_mem_get_cpu_buf(cmd_desc[i].mem_handle,
502 &generic_ptr, &len_of_buff);
503 if (rc < 0) {
504 CAM_ERR(CAM_ACTUATOR, "Failed to get cpu buf");
505 return rc;
506 }
507 cmd_buf = (uint32_t *)generic_ptr;
508 if (!cmd_buf) {
509 CAM_ERR(CAM_ACTUATOR, "invalid cmd buf");
510 return -EINVAL;
511 }
Raja Mallikff6c75b2019-01-29 16:52:37 +0530512 if ((len_of_buff < sizeof(struct common_header)) ||
513 (cmd_desc[i].offset > (len_of_buff -
514 sizeof(struct common_header)))) {
515 CAM_ERR(CAM_ACTUATOR,
516 "Invalid length for sensor cmd");
517 return -EINVAL;
518 }
519 remain_len = len_of_buff - cmd_desc[i].offset;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530520 cmd_buf += cmd_desc[i].offset / sizeof(uint32_t);
521 cmm_hdr = (struct common_header *)cmd_buf;
522
523 switch (cmm_hdr->cmd_type) {
524 case CAMERA_SENSOR_CMD_TYPE_I2C_INFO:
525 CAM_DBG(CAM_ACTUATOR,
526 "Received slave info buffer");
527 rc = cam_actuator_slaveInfo_pkt_parser(
Raja Mallikff6c75b2019-01-29 16:52:37 +0530528 a_ctrl, cmd_buf, remain_len);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530529 if (rc < 0) {
530 CAM_ERR(CAM_ACTUATOR,
531 "Failed to parse slave info: %d", rc);
532 return rc;
533 }
534 break;
535 case CAMERA_SENSOR_CMD_TYPE_PWR_UP:
536 case CAMERA_SENSOR_CMD_TYPE_PWR_DOWN:
537 CAM_DBG(CAM_ACTUATOR,
538 "Received power settings buffer");
539 rc = cam_sensor_update_power_settings(
540 cmd_buf,
541 total_cmd_buf_in_bytes,
Raja Mallikff6c75b2019-01-29 16:52:37 +0530542 power_info, remain_len);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530543 if (rc) {
544 CAM_ERR(CAM_ACTUATOR,
545 "Failed:parse power settings: %d",
546 rc);
547 return rc;
548 }
549 break;
550 default:
551 CAM_DBG(CAM_ACTUATOR,
552 "Received initSettings buffer");
553 i2c_data = &(a_ctrl->i2c_data);
554 i2c_reg_settings =
555 &i2c_data->init_settings;
556
557 i2c_reg_settings->request_id = 0;
558 i2c_reg_settings->is_settings_valid = 1;
559 rc = cam_sensor_i2c_command_parser(
560 &a_ctrl->io_master_info,
561 i2c_reg_settings,
562 &cmd_desc[i], 1);
563 if (rc < 0) {
564 CAM_ERR(CAM_ACTUATOR,
565 "Failed:parse init settings: %d",
566 rc);
567 return rc;
568 }
569 break;
570 }
571 }
572
573 if (a_ctrl->cam_act_state == CAM_ACTUATOR_ACQUIRE) {
574 rc = cam_actuator_power_up(a_ctrl);
575 if (rc < 0) {
576 CAM_ERR(CAM_ACTUATOR,
577 " Actuator Power up failed");
578 return rc;
579 }
580 a_ctrl->cam_act_state = CAM_ACTUATOR_CONFIG;
581 }
582
583 rc = cam_actuator_apply_settings(a_ctrl,
584 &a_ctrl->i2c_data.init_settings);
585 if (rc < 0) {
586 CAM_ERR(CAM_ACTUATOR, "Cannot apply Init settings");
587 return rc;
588 }
589
590 /* Delete the request even if the apply is failed */
591 rc = delete_request(&a_ctrl->i2c_data.init_settings);
592 if (rc < 0) {
593 CAM_WARN(CAM_ACTUATOR,
594 "Fail in deleting the Init settings");
595 rc = 0;
596 }
597 break;
598 case CAM_ACTUATOR_PACKET_AUTO_MOVE_LENS:
599 if (a_ctrl->cam_act_state < CAM_ACTUATOR_CONFIG) {
600 rc = -EINVAL;
601 CAM_WARN(CAM_ACTUATOR,
602 "Not in right state to move lens: %d",
603 a_ctrl->cam_act_state);
604 return rc;
605 }
606 a_ctrl->setting_apply_state = ACT_APPLY_SETTINGS_NOW;
607
608 i2c_data = &(a_ctrl->i2c_data);
609 i2c_reg_settings = &i2c_data->init_settings;
610
611 i2c_data->init_settings.request_id =
612 csl_packet->header.request_id;
613 i2c_reg_settings->is_settings_valid = 1;
614 offset = (uint32_t *)&csl_packet->payload;
615 offset += csl_packet->cmd_buf_offset / sizeof(uint32_t);
616 cmd_desc = (struct cam_cmd_buf_desc *)(offset);
617 rc = cam_sensor_i2c_command_parser(
618 &a_ctrl->io_master_info,
619 i2c_reg_settings,
620 cmd_desc, 1);
621 if (rc < 0) {
622 CAM_ERR(CAM_ACTUATOR,
623 "Auto move lens parsing failed: %d", rc);
624 return rc;
625 }
626 cam_actuator_update_req_mgr(a_ctrl, csl_packet);
627 break;
628 case CAM_ACTUATOR_PACKET_MANUAL_MOVE_LENS:
629 if (a_ctrl->cam_act_state < CAM_ACTUATOR_CONFIG) {
630 rc = -EINVAL;
631 CAM_WARN(CAM_ACTUATOR,
632 "Not in right state to move lens: %d",
633 a_ctrl->cam_act_state);
634 return rc;
635 }
636
637 a_ctrl->setting_apply_state = ACT_APPLY_SETTINGS_LATER;
638 i2c_data = &(a_ctrl->i2c_data);
639 i2c_reg_settings = &i2c_data->per_frame[
640 csl_packet->header.request_id % MAX_PER_FRAME_ARRAY];
641
642 i2c_reg_settings->request_id =
643 csl_packet->header.request_id;
644 i2c_reg_settings->is_settings_valid = 1;
645 offset = (uint32_t *)&csl_packet->payload;
646 offset += csl_packet->cmd_buf_offset / sizeof(uint32_t);
647 cmd_desc = (struct cam_cmd_buf_desc *)(offset);
648 rc = cam_sensor_i2c_command_parser(
649 &a_ctrl->io_master_info,
650 i2c_reg_settings,
651 cmd_desc, 1);
652 if (rc < 0) {
653 CAM_ERR(CAM_ACTUATOR,
654 "Manual move lens parsing failed: %d", rc);
655 return rc;
656 }
657
658 cam_actuator_update_req_mgr(a_ctrl, csl_packet);
659 break;
660 case CAM_PKT_NOP_OPCODE:
661 if (a_ctrl->cam_act_state < CAM_ACTUATOR_CONFIG) {
662 CAM_WARN(CAM_ACTUATOR,
663 "Received NOP packets in invalid state: %d",
664 a_ctrl->cam_act_state);
665 return -EINVAL;
666 }
Raja Mallikc7e256f2018-12-06 17:36:28 +0530667 cam_actuator_update_req_mgr(a_ctrl, csl_packet);
668 break;
Raja Mallikff6c75b2019-01-29 16:52:37 +0530669 default:
670 CAM_ERR(CAM_ACTUATOR, "Wrong Opcode: %d",
671 csl_packet->header.op_code & 0xFFFFFF);
672 return -EINVAL;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530673 }
674
675 return rc;
676}
677
678void cam_actuator_shutdown(struct cam_actuator_ctrl_t *a_ctrl)
679{
680 int rc = 0;
681 struct cam_actuator_soc_private *soc_private =
682 (struct cam_actuator_soc_private *)a_ctrl->soc_info.soc_private;
683 struct cam_sensor_power_ctrl_t *power_info =
684 &soc_private->power_info;
685
686 if (a_ctrl->cam_act_state == CAM_ACTUATOR_INIT)
687 return;
688
689 if (a_ctrl->cam_act_state >= CAM_ACTUATOR_CONFIG) {
690 rc = cam_actuator_power_down(a_ctrl);
691 if (rc < 0)
692 CAM_ERR(CAM_ACTUATOR, "Actuator Power down failed");
693 a_ctrl->cam_act_state = CAM_ACTUATOR_ACQUIRE;
694 }
695
696 if (a_ctrl->cam_act_state >= CAM_ACTUATOR_ACQUIRE) {
697 rc = cam_destroy_device_hdl(a_ctrl->bridge_intf.device_hdl);
698 if (rc < 0)
699 CAM_ERR(CAM_ACTUATOR, "destroying dhdl failed");
700 a_ctrl->bridge_intf.device_hdl = -1;
701 a_ctrl->bridge_intf.link_hdl = -1;
702 a_ctrl->bridge_intf.session_hdl = -1;
703 }
704
705 kfree(power_info->power_setting);
706 kfree(power_info->power_down_setting);
707 power_info->power_setting = NULL;
708 power_info->power_down_setting = NULL;
709 power_info->power_setting_size = 0;
710 power_info->power_down_setting_size = 0;
711
712 a_ctrl->cam_act_state = CAM_ACTUATOR_INIT;
713}
714
715int32_t cam_actuator_driver_cmd(struct cam_actuator_ctrl_t *a_ctrl,
716 void *arg)
717{
718 int rc = 0;
719 struct cam_control *cmd = (struct cam_control *)arg;
720 struct cam_actuator_soc_private *soc_private = NULL;
721 struct cam_sensor_power_ctrl_t *power_info = NULL;
722
723 if (!a_ctrl || !cmd) {
724 CAM_ERR(CAM_ACTUATOR, "Invalid Args");
725 return -EINVAL;
726 }
727
728 soc_private =
729 (struct cam_actuator_soc_private *)a_ctrl->soc_info.soc_private;
730
731 power_info = &soc_private->power_info;
732
733 if (cmd->handle_type != CAM_HANDLE_USER_POINTER) {
734 CAM_ERR(CAM_ACTUATOR, "Invalid handle type: %d",
735 cmd->handle_type);
736 return -EINVAL;
737 }
738
739 CAM_DBG(CAM_ACTUATOR, "Opcode to Actuator: %d", cmd->op_code);
740
741 mutex_lock(&(a_ctrl->actuator_mutex));
742 switch (cmd->op_code) {
743 case CAM_ACQUIRE_DEV: {
744 struct cam_sensor_acquire_dev actuator_acq_dev;
745 struct cam_create_dev_hdl bridge_params;
746
747 if (a_ctrl->bridge_intf.device_hdl != -1) {
748 CAM_ERR(CAM_ACTUATOR, "Device is already acquired");
749 rc = -EINVAL;
750 goto release_mutex;
751 }
752 rc = copy_from_user(&actuator_acq_dev,
753 u64_to_user_ptr(cmd->handle),
754 sizeof(actuator_acq_dev));
755 if (rc < 0) {
756 CAM_ERR(CAM_ACTUATOR, "Failed Copying from user\n");
757 goto release_mutex;
758 }
759
760 bridge_params.session_hdl = actuator_acq_dev.session_handle;
761 bridge_params.ops = &a_ctrl->bridge_intf.ops;
762 bridge_params.v4l2_sub_dev_flag = 0;
763 bridge_params.media_entity_flag = 0;
764 bridge_params.priv = a_ctrl;
765
766 actuator_acq_dev.device_handle =
767 cam_create_device_hdl(&bridge_params);
768 a_ctrl->bridge_intf.device_hdl = actuator_acq_dev.device_handle;
769 a_ctrl->bridge_intf.session_hdl =
770 actuator_acq_dev.session_handle;
771
772 CAM_DBG(CAM_ACTUATOR, "Device Handle: %d",
773 actuator_acq_dev.device_handle);
774 if (copy_to_user(u64_to_user_ptr(cmd->handle),
775 &actuator_acq_dev,
776 sizeof(struct cam_sensor_acquire_dev))) {
777 CAM_ERR(CAM_ACTUATOR, "Failed Copy to User");
778 rc = -EFAULT;
779 goto release_mutex;
780 }
781
782 a_ctrl->cam_act_state = CAM_ACTUATOR_ACQUIRE;
783 }
784 break;
785 case CAM_RELEASE_DEV: {
786 if (a_ctrl->cam_act_state == CAM_ACTUATOR_START) {
787 rc = -EINVAL;
788 CAM_WARN(CAM_ACTUATOR,
789 "Cant release actuator: in start state");
790 goto release_mutex;
791 }
792
793 if (a_ctrl->cam_act_state == CAM_ACTUATOR_CONFIG) {
794 rc = cam_actuator_power_down(a_ctrl);
795 if (rc < 0) {
796 CAM_ERR(CAM_ACTUATOR,
797 "Actuator Power down failed");
798 goto release_mutex;
799 }
800 }
801
802 if (a_ctrl->bridge_intf.device_hdl == -1) {
803 CAM_ERR(CAM_ACTUATOR, "link hdl: %d device hdl: %d",
804 a_ctrl->bridge_intf.device_hdl,
805 a_ctrl->bridge_intf.link_hdl);
806 rc = -EINVAL;
807 goto release_mutex;
808 }
809
810 if (a_ctrl->bridge_intf.link_hdl != -1) {
811 CAM_ERR(CAM_ACTUATOR,
812 "Device [%d] still active on link 0x%x",
813 a_ctrl->cam_act_state,
814 a_ctrl->bridge_intf.link_hdl);
815 rc = -EAGAIN;
816 goto release_mutex;
817 }
818
819 rc = cam_destroy_device_hdl(a_ctrl->bridge_intf.device_hdl);
820 if (rc < 0)
821 CAM_ERR(CAM_ACTUATOR, "destroying the device hdl");
822 a_ctrl->bridge_intf.device_hdl = -1;
823 a_ctrl->bridge_intf.link_hdl = -1;
824 a_ctrl->bridge_intf.session_hdl = -1;
825 a_ctrl->cam_act_state = CAM_ACTUATOR_INIT;
826 a_ctrl->last_flush_req = 0;
827 kfree(power_info->power_setting);
828 kfree(power_info->power_down_setting);
829 power_info->power_setting = NULL;
830 power_info->power_down_setting = NULL;
831 power_info->power_down_setting_size = 0;
832 power_info->power_setting_size = 0;
833 }
834 break;
835 case CAM_QUERY_CAP: {
836 struct cam_actuator_query_cap actuator_cap = {0};
837
838 actuator_cap.slot_info = a_ctrl->soc_info.index;
839 if (copy_to_user(u64_to_user_ptr(cmd->handle),
840 &actuator_cap,
841 sizeof(struct cam_actuator_query_cap))) {
842 CAM_ERR(CAM_ACTUATOR, "Failed Copy to User");
843 rc = -EFAULT;
844 goto release_mutex;
845 }
846 }
847 break;
848 case CAM_START_DEV: {
849 if (a_ctrl->cam_act_state != CAM_ACTUATOR_CONFIG) {
850 rc = -EINVAL;
851 CAM_WARN(CAM_ACTUATOR,
852 "Not in right state to start : %d",
853 a_ctrl->cam_act_state);
854 goto release_mutex;
855 }
856 a_ctrl->cam_act_state = CAM_ACTUATOR_START;
857 a_ctrl->last_flush_req = 0;
858 }
859 break;
860 case CAM_STOP_DEV: {
861 struct i2c_settings_array *i2c_set = NULL;
862 int i;
863
864 if (a_ctrl->cam_act_state != CAM_ACTUATOR_START) {
865 rc = -EINVAL;
866 CAM_WARN(CAM_ACTUATOR,
867 "Not in right state to stop : %d",
868 a_ctrl->cam_act_state);
869 goto release_mutex;
870 }
871
872 for (i = 0; i < MAX_PER_FRAME_ARRAY; i++) {
873 i2c_set = &(a_ctrl->i2c_data.per_frame[i]);
874
875 if (i2c_set->is_settings_valid == 1) {
876 rc = delete_request(i2c_set);
877 if (rc < 0)
878 CAM_ERR(CAM_SENSOR,
879 "delete request: %lld rc: %d",
880 i2c_set->request_id, rc);
881 }
882 }
883 a_ctrl->last_flush_req = 0;
884 a_ctrl->cam_act_state = CAM_ACTUATOR_CONFIG;
885 }
886 break;
887 case CAM_CONFIG_DEV: {
888 a_ctrl->setting_apply_state =
889 ACT_APPLY_SETTINGS_LATER;
890 rc = cam_actuator_i2c_pkt_parse(a_ctrl, arg);
891 if (rc < 0) {
892 CAM_ERR(CAM_ACTUATOR, "Failed in actuator Parsing");
893 goto release_mutex;
894 }
895
896 if (a_ctrl->setting_apply_state ==
897 ACT_APPLY_SETTINGS_NOW) {
898 rc = cam_actuator_apply_settings(a_ctrl,
899 &a_ctrl->i2c_data.init_settings);
900 if (rc < 0)
901 CAM_ERR(CAM_ACTUATOR,
902 "Cannot apply Update settings");
903
904 /* Delete the request even if the apply is failed */
905 rc = delete_request(&a_ctrl->i2c_data.init_settings);
906 if (rc < 0) {
907 CAM_ERR(CAM_ACTUATOR,
908 "Failed in Deleting the Init Pkt: %d",
909 rc);
910 goto release_mutex;
911 }
912 }
913 }
914 break;
915 default:
916 CAM_ERR(CAM_ACTUATOR, "Invalid Opcode %d", cmd->op_code);
917 }
918
919release_mutex:
920 mutex_unlock(&(a_ctrl->actuator_mutex));
921
922 return rc;
923}
924
925int32_t cam_actuator_flush_request(struct cam_req_mgr_flush_request *flush_req)
926{
927 int32_t rc = 0, i;
928 uint32_t cancel_req_id_found = 0;
929 struct cam_actuator_ctrl_t *a_ctrl = NULL;
930 struct i2c_settings_array *i2c_set = NULL;
931
932 if (!flush_req)
933 return -EINVAL;
934
935 a_ctrl = (struct cam_actuator_ctrl_t *)
936 cam_get_device_priv(flush_req->dev_hdl);
937 if (!a_ctrl) {
938 CAM_ERR(CAM_ACTUATOR, "Device data is NULL");
939 return -EINVAL;
940 }
941
942 if (a_ctrl->i2c_data.per_frame == NULL) {
943 CAM_ERR(CAM_ACTUATOR, "i2c frame data is NULL");
944 return -EINVAL;
945 }
946
947 mutex_lock(&(a_ctrl->actuator_mutex));
948 if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
949 a_ctrl->last_flush_req = flush_req->req_id;
950 CAM_DBG(CAM_ACTUATOR, "last reqest to flush is %lld",
951 flush_req->req_id);
952 }
953
954 for (i = 0; i < MAX_PER_FRAME_ARRAY; i++) {
955 i2c_set = &(a_ctrl->i2c_data.per_frame[i]);
956
957 if ((flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ)
958 && (i2c_set->request_id != flush_req->req_id))
959 continue;
960
961 if (i2c_set->is_settings_valid == 1) {
962 rc = delete_request(i2c_set);
963 if (rc < 0)
964 CAM_ERR(CAM_ACTUATOR,
965 "delete request: %lld rc: %d",
966 i2c_set->request_id, rc);
967
968 if (flush_req->type ==
969 CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
970 cancel_req_id_found = 1;
971 break;
972 }
973 }
974 }
975
976 if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
977 !cancel_req_id_found)
978 CAM_DBG(CAM_ACTUATOR,
979 "Flush request id:%lld not found in the pending list",
980 flush_req->req_id);
981 mutex_unlock(&(a_ctrl->actuator_mutex));
982 return rc;
983}