blob: 3837aac6a2e57d87e3d20dd584aa841f9605b62d [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 Mallik96ab3482019-02-19 15:21:59 +0530462 rc = -EINVAL;
463 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530464 }
465
Raja Mallikff6c75b2019-01-29 16:52:37 +0530466 remain_len -= (size_t)config.offset;
467 csl_packet = (struct cam_packet *)
468 (generic_pkt_ptr + (uint32_t)config.offset);
469
470 if (cam_packet_util_validate_packet(csl_packet,
471 remain_len)) {
472 CAM_ERR(CAM_ACTUATOR, "Invalid packet params");
Raja Mallik96ab3482019-02-19 15:21:59 +0530473 rc = -EINVAL;
474 goto rel_pkt_buf;
Raja Mallikff6c75b2019-01-29 16:52:37 +0530475 }
476
477 CAM_DBG(CAM_ACTUATOR, "Pkt opcode: %d", csl_packet->header.op_code);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530478
479 if ((csl_packet->header.op_code & 0xFFFFFF) !=
480 CAM_ACTUATOR_PACKET_OPCODE_INIT &&
481 csl_packet->header.request_id <= a_ctrl->last_flush_req
482 && a_ctrl->last_flush_req != 0) {
483 CAM_DBG(CAM_ACTUATOR,
484 "reject request %lld, last request to flush %lld",
485 csl_packet->header.request_id, a_ctrl->last_flush_req);
Raja Mallikff6c75b2019-01-29 16:52:37 +0530486 rc = -EINVAL;
Raja Mallik96ab3482019-02-19 15:21:59 +0530487 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530488 }
489
490 if (csl_packet->header.request_id > a_ctrl->last_flush_req)
491 a_ctrl->last_flush_req = 0;
492
493 switch (csl_packet->header.op_code & 0xFFFFFF) {
494 case CAM_ACTUATOR_PACKET_OPCODE_INIT:
495 offset = (uint32_t *)&csl_packet->payload;
496 offset += (csl_packet->cmd_buf_offset / sizeof(uint32_t));
497 cmd_desc = (struct cam_cmd_buf_desc *)(offset);
498
499 /* Loop through multiple command buffers */
500 for (i = 0; i < csl_packet->num_cmd_buf; i++) {
501 total_cmd_buf_in_bytes = cmd_desc[i].length;
502 if (!total_cmd_buf_in_bytes)
503 continue;
504 rc = cam_mem_get_cpu_buf(cmd_desc[i].mem_handle,
505 &generic_ptr, &len_of_buff);
506 if (rc < 0) {
507 CAM_ERR(CAM_ACTUATOR, "Failed to get cpu buf");
Raja Mallik96ab3482019-02-19 15:21:59 +0530508 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530509 }
510 cmd_buf = (uint32_t *)generic_ptr;
511 if (!cmd_buf) {
512 CAM_ERR(CAM_ACTUATOR, "invalid cmd buf");
Raja Mallik96ab3482019-02-19 15:21:59 +0530513 goto rel_cmd_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530514 }
Raja Mallikff6c75b2019-01-29 16:52:37 +0530515 if ((len_of_buff < sizeof(struct common_header)) ||
516 (cmd_desc[i].offset > (len_of_buff -
517 sizeof(struct common_header)))) {
518 CAM_ERR(CAM_ACTUATOR,
519 "Invalid length for sensor cmd");
Raja Mallik96ab3482019-02-19 15:21:59 +0530520 rc = -EINVAL;
521 goto rel_cmd_buf;
Raja Mallikff6c75b2019-01-29 16:52:37 +0530522 }
523 remain_len = len_of_buff - cmd_desc[i].offset;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530524 cmd_buf += cmd_desc[i].offset / sizeof(uint32_t);
525 cmm_hdr = (struct common_header *)cmd_buf;
526
527 switch (cmm_hdr->cmd_type) {
528 case CAMERA_SENSOR_CMD_TYPE_I2C_INFO:
529 CAM_DBG(CAM_ACTUATOR,
530 "Received slave info buffer");
531 rc = cam_actuator_slaveInfo_pkt_parser(
Raja Mallikff6c75b2019-01-29 16:52:37 +0530532 a_ctrl, cmd_buf, remain_len);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530533 if (rc < 0) {
534 CAM_ERR(CAM_ACTUATOR,
535 "Failed to parse slave info: %d", rc);
Raja Mallik96ab3482019-02-19 15:21:59 +0530536 goto rel_cmd_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530537 }
538 break;
539 case CAMERA_SENSOR_CMD_TYPE_PWR_UP:
540 case CAMERA_SENSOR_CMD_TYPE_PWR_DOWN:
541 CAM_DBG(CAM_ACTUATOR,
542 "Received power settings buffer");
543 rc = cam_sensor_update_power_settings(
544 cmd_buf,
545 total_cmd_buf_in_bytes,
Raja Mallikff6c75b2019-01-29 16:52:37 +0530546 power_info, remain_len);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530547 if (rc) {
548 CAM_ERR(CAM_ACTUATOR,
549 "Failed:parse power settings: %d",
550 rc);
Raja Mallik96ab3482019-02-19 15:21:59 +0530551 goto rel_cmd_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530552 }
553 break;
554 default:
555 CAM_DBG(CAM_ACTUATOR,
556 "Received initSettings buffer");
557 i2c_data = &(a_ctrl->i2c_data);
558 i2c_reg_settings =
559 &i2c_data->init_settings;
560
561 i2c_reg_settings->request_id = 0;
562 i2c_reg_settings->is_settings_valid = 1;
563 rc = cam_sensor_i2c_command_parser(
564 &a_ctrl->io_master_info,
565 i2c_reg_settings,
566 &cmd_desc[i], 1);
567 if (rc < 0) {
568 CAM_ERR(CAM_ACTUATOR,
569 "Failed:parse init settings: %d",
570 rc);
Raja Mallik96ab3482019-02-19 15:21:59 +0530571 goto rel_cmd_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530572 }
573 break;
574 }
Raja Mallik96ab3482019-02-19 15:21:59 +0530575 if (cam_mem_put_cpu_buf(cmd_desc[i].mem_handle))
576 CAM_WARN(CAM_ACTUATOR,
577 "Failed to put cpu buf: %d",
578 cmd_desc[i].mem_handle);
Raja Mallikc7e256f2018-12-06 17:36:28 +0530579 }
580
581 if (a_ctrl->cam_act_state == CAM_ACTUATOR_ACQUIRE) {
582 rc = cam_actuator_power_up(a_ctrl);
583 if (rc < 0) {
584 CAM_ERR(CAM_ACTUATOR,
585 " Actuator Power up failed");
Raja Mallik96ab3482019-02-19 15:21:59 +0530586 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530587 }
588 a_ctrl->cam_act_state = CAM_ACTUATOR_CONFIG;
589 }
590
591 rc = cam_actuator_apply_settings(a_ctrl,
592 &a_ctrl->i2c_data.init_settings);
593 if (rc < 0) {
594 CAM_ERR(CAM_ACTUATOR, "Cannot apply Init settings");
Raja Mallik96ab3482019-02-19 15:21:59 +0530595 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530596 }
597
598 /* Delete the request even if the apply is failed */
599 rc = delete_request(&a_ctrl->i2c_data.init_settings);
600 if (rc < 0) {
601 CAM_WARN(CAM_ACTUATOR,
602 "Fail in deleting the Init settings");
603 rc = 0;
604 }
605 break;
606 case CAM_ACTUATOR_PACKET_AUTO_MOVE_LENS:
607 if (a_ctrl->cam_act_state < CAM_ACTUATOR_CONFIG) {
608 rc = -EINVAL;
609 CAM_WARN(CAM_ACTUATOR,
610 "Not in right state to move lens: %d",
611 a_ctrl->cam_act_state);
Raja Mallik96ab3482019-02-19 15:21:59 +0530612 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530613 }
614 a_ctrl->setting_apply_state = ACT_APPLY_SETTINGS_NOW;
615
616 i2c_data = &(a_ctrl->i2c_data);
617 i2c_reg_settings = &i2c_data->init_settings;
618
619 i2c_data->init_settings.request_id =
620 csl_packet->header.request_id;
621 i2c_reg_settings->is_settings_valid = 1;
622 offset = (uint32_t *)&csl_packet->payload;
623 offset += csl_packet->cmd_buf_offset / sizeof(uint32_t);
624 cmd_desc = (struct cam_cmd_buf_desc *)(offset);
625 rc = cam_sensor_i2c_command_parser(
626 &a_ctrl->io_master_info,
627 i2c_reg_settings,
628 cmd_desc, 1);
629 if (rc < 0) {
630 CAM_ERR(CAM_ACTUATOR,
631 "Auto move lens parsing failed: %d", rc);
Raja Mallik96ab3482019-02-19 15:21:59 +0530632 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530633 }
634 cam_actuator_update_req_mgr(a_ctrl, csl_packet);
635 break;
636 case CAM_ACTUATOR_PACKET_MANUAL_MOVE_LENS:
637 if (a_ctrl->cam_act_state < CAM_ACTUATOR_CONFIG) {
638 rc = -EINVAL;
639 CAM_WARN(CAM_ACTUATOR,
640 "Not in right state to move lens: %d",
641 a_ctrl->cam_act_state);
Raja Mallik96ab3482019-02-19 15:21:59 +0530642 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530643 }
644
645 a_ctrl->setting_apply_state = ACT_APPLY_SETTINGS_LATER;
646 i2c_data = &(a_ctrl->i2c_data);
647 i2c_reg_settings = &i2c_data->per_frame[
648 csl_packet->header.request_id % MAX_PER_FRAME_ARRAY];
649
650 i2c_reg_settings->request_id =
651 csl_packet->header.request_id;
652 i2c_reg_settings->is_settings_valid = 1;
653 offset = (uint32_t *)&csl_packet->payload;
654 offset += csl_packet->cmd_buf_offset / sizeof(uint32_t);
655 cmd_desc = (struct cam_cmd_buf_desc *)(offset);
656 rc = cam_sensor_i2c_command_parser(
657 &a_ctrl->io_master_info,
658 i2c_reg_settings,
659 cmd_desc, 1);
660 if (rc < 0) {
661 CAM_ERR(CAM_ACTUATOR,
662 "Manual move lens parsing failed: %d", rc);
Raja Mallik96ab3482019-02-19 15:21:59 +0530663 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530664 }
665
666 cam_actuator_update_req_mgr(a_ctrl, csl_packet);
667 break;
668 case CAM_PKT_NOP_OPCODE:
669 if (a_ctrl->cam_act_state < CAM_ACTUATOR_CONFIG) {
670 CAM_WARN(CAM_ACTUATOR,
671 "Received NOP packets in invalid state: %d",
672 a_ctrl->cam_act_state);
Raja Mallik96ab3482019-02-19 15:21:59 +0530673 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530674 }
Raja Mallikc7e256f2018-12-06 17:36:28 +0530675 cam_actuator_update_req_mgr(a_ctrl, csl_packet);
676 break;
Raja Mallikff6c75b2019-01-29 16:52:37 +0530677 default:
678 CAM_ERR(CAM_ACTUATOR, "Wrong Opcode: %d",
679 csl_packet->header.op_code & 0xFFFFFF);
Raja Mallik96ab3482019-02-19 15:21:59 +0530680 rc = -EINVAL;
681 goto rel_pkt_buf;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530682 }
683
Raja Mallik96ab3482019-02-19 15:21:59 +0530684 if (cam_mem_put_cpu_buf(config.packet_handle))
685 CAM_WARN(CAM_ACTUATOR, "Fail to put cmd buffer: %llu",
686 config.packet_handle);
687
688 return rc;
689
690rel_cmd_buf:
691 if (cam_mem_put_cpu_buf(cmd_desc[i].mem_handle))
692 CAM_WARN(CAM_ACTUATOR, "Fail to put cmd buffer: %d",
693 cmd_desc[i].mem_handle);
694rel_pkt_buf:
695 if (cam_mem_put_cpu_buf(config.packet_handle))
696 CAM_WARN(CAM_ACTUATOR, "Fail to put cmd buffer: %llu",
697 config.packet_handle);
698
Raja Mallikc7e256f2018-12-06 17:36:28 +0530699 return rc;
700}
701
702void cam_actuator_shutdown(struct cam_actuator_ctrl_t *a_ctrl)
703{
704 int rc = 0;
705 struct cam_actuator_soc_private *soc_private =
706 (struct cam_actuator_soc_private *)a_ctrl->soc_info.soc_private;
707 struct cam_sensor_power_ctrl_t *power_info =
708 &soc_private->power_info;
709
710 if (a_ctrl->cam_act_state == CAM_ACTUATOR_INIT)
711 return;
712
713 if (a_ctrl->cam_act_state >= CAM_ACTUATOR_CONFIG) {
714 rc = cam_actuator_power_down(a_ctrl);
715 if (rc < 0)
716 CAM_ERR(CAM_ACTUATOR, "Actuator Power down failed");
717 a_ctrl->cam_act_state = CAM_ACTUATOR_ACQUIRE;
718 }
719
720 if (a_ctrl->cam_act_state >= CAM_ACTUATOR_ACQUIRE) {
721 rc = cam_destroy_device_hdl(a_ctrl->bridge_intf.device_hdl);
722 if (rc < 0)
723 CAM_ERR(CAM_ACTUATOR, "destroying dhdl failed");
724 a_ctrl->bridge_intf.device_hdl = -1;
725 a_ctrl->bridge_intf.link_hdl = -1;
726 a_ctrl->bridge_intf.session_hdl = -1;
727 }
728
729 kfree(power_info->power_setting);
730 kfree(power_info->power_down_setting);
731 power_info->power_setting = NULL;
732 power_info->power_down_setting = NULL;
733 power_info->power_setting_size = 0;
734 power_info->power_down_setting_size = 0;
735
736 a_ctrl->cam_act_state = CAM_ACTUATOR_INIT;
737}
738
739int32_t cam_actuator_driver_cmd(struct cam_actuator_ctrl_t *a_ctrl,
740 void *arg)
741{
742 int rc = 0;
743 struct cam_control *cmd = (struct cam_control *)arg;
744 struct cam_actuator_soc_private *soc_private = NULL;
745 struct cam_sensor_power_ctrl_t *power_info = NULL;
746
747 if (!a_ctrl || !cmd) {
748 CAM_ERR(CAM_ACTUATOR, "Invalid Args");
749 return -EINVAL;
750 }
751
752 soc_private =
753 (struct cam_actuator_soc_private *)a_ctrl->soc_info.soc_private;
754
755 power_info = &soc_private->power_info;
756
757 if (cmd->handle_type != CAM_HANDLE_USER_POINTER) {
758 CAM_ERR(CAM_ACTUATOR, "Invalid handle type: %d",
759 cmd->handle_type);
760 return -EINVAL;
761 }
762
763 CAM_DBG(CAM_ACTUATOR, "Opcode to Actuator: %d", cmd->op_code);
764
765 mutex_lock(&(a_ctrl->actuator_mutex));
766 switch (cmd->op_code) {
767 case CAM_ACQUIRE_DEV: {
768 struct cam_sensor_acquire_dev actuator_acq_dev;
769 struct cam_create_dev_hdl bridge_params;
770
771 if (a_ctrl->bridge_intf.device_hdl != -1) {
772 CAM_ERR(CAM_ACTUATOR, "Device is already acquired");
773 rc = -EINVAL;
774 goto release_mutex;
775 }
776 rc = copy_from_user(&actuator_acq_dev,
777 u64_to_user_ptr(cmd->handle),
778 sizeof(actuator_acq_dev));
779 if (rc < 0) {
780 CAM_ERR(CAM_ACTUATOR, "Failed Copying from user\n");
781 goto release_mutex;
782 }
783
784 bridge_params.session_hdl = actuator_acq_dev.session_handle;
785 bridge_params.ops = &a_ctrl->bridge_intf.ops;
786 bridge_params.v4l2_sub_dev_flag = 0;
787 bridge_params.media_entity_flag = 0;
788 bridge_params.priv = a_ctrl;
Raja Mallik477ca242019-06-21 14:23:32 +0530789 bridge_params.dev_id = CAM_ACTUATOR;
Raja Mallikc7e256f2018-12-06 17:36:28 +0530790 actuator_acq_dev.device_handle =
791 cam_create_device_hdl(&bridge_params);
792 a_ctrl->bridge_intf.device_hdl = actuator_acq_dev.device_handle;
793 a_ctrl->bridge_intf.session_hdl =
794 actuator_acq_dev.session_handle;
795
796 CAM_DBG(CAM_ACTUATOR, "Device Handle: %d",
797 actuator_acq_dev.device_handle);
798 if (copy_to_user(u64_to_user_ptr(cmd->handle),
799 &actuator_acq_dev,
800 sizeof(struct cam_sensor_acquire_dev))) {
801 CAM_ERR(CAM_ACTUATOR, "Failed Copy to User");
802 rc = -EFAULT;
803 goto release_mutex;
804 }
805
806 a_ctrl->cam_act_state = CAM_ACTUATOR_ACQUIRE;
807 }
808 break;
809 case CAM_RELEASE_DEV: {
810 if (a_ctrl->cam_act_state == CAM_ACTUATOR_START) {
811 rc = -EINVAL;
812 CAM_WARN(CAM_ACTUATOR,
813 "Cant release actuator: in start state");
814 goto release_mutex;
815 }
816
817 if (a_ctrl->cam_act_state == CAM_ACTUATOR_CONFIG) {
818 rc = cam_actuator_power_down(a_ctrl);
819 if (rc < 0) {
820 CAM_ERR(CAM_ACTUATOR,
821 "Actuator Power down failed");
822 goto release_mutex;
823 }
824 }
825
826 if (a_ctrl->bridge_intf.device_hdl == -1) {
827 CAM_ERR(CAM_ACTUATOR, "link hdl: %d device hdl: %d",
828 a_ctrl->bridge_intf.device_hdl,
829 a_ctrl->bridge_intf.link_hdl);
830 rc = -EINVAL;
831 goto release_mutex;
832 }
833
834 if (a_ctrl->bridge_intf.link_hdl != -1) {
835 CAM_ERR(CAM_ACTUATOR,
836 "Device [%d] still active on link 0x%x",
837 a_ctrl->cam_act_state,
838 a_ctrl->bridge_intf.link_hdl);
839 rc = -EAGAIN;
840 goto release_mutex;
841 }
842
843 rc = cam_destroy_device_hdl(a_ctrl->bridge_intf.device_hdl);
844 if (rc < 0)
845 CAM_ERR(CAM_ACTUATOR, "destroying the device hdl");
846 a_ctrl->bridge_intf.device_hdl = -1;
847 a_ctrl->bridge_intf.link_hdl = -1;
848 a_ctrl->bridge_intf.session_hdl = -1;
849 a_ctrl->cam_act_state = CAM_ACTUATOR_INIT;
850 a_ctrl->last_flush_req = 0;
851 kfree(power_info->power_setting);
852 kfree(power_info->power_down_setting);
853 power_info->power_setting = NULL;
854 power_info->power_down_setting = NULL;
855 power_info->power_down_setting_size = 0;
856 power_info->power_setting_size = 0;
857 }
858 break;
859 case CAM_QUERY_CAP: {
860 struct cam_actuator_query_cap actuator_cap = {0};
861
862 actuator_cap.slot_info = a_ctrl->soc_info.index;
863 if (copy_to_user(u64_to_user_ptr(cmd->handle),
864 &actuator_cap,
865 sizeof(struct cam_actuator_query_cap))) {
866 CAM_ERR(CAM_ACTUATOR, "Failed Copy to User");
867 rc = -EFAULT;
868 goto release_mutex;
869 }
870 }
871 break;
872 case CAM_START_DEV: {
873 if (a_ctrl->cam_act_state != CAM_ACTUATOR_CONFIG) {
874 rc = -EINVAL;
875 CAM_WARN(CAM_ACTUATOR,
876 "Not in right state to start : %d",
877 a_ctrl->cam_act_state);
878 goto release_mutex;
879 }
880 a_ctrl->cam_act_state = CAM_ACTUATOR_START;
881 a_ctrl->last_flush_req = 0;
882 }
883 break;
884 case CAM_STOP_DEV: {
885 struct i2c_settings_array *i2c_set = NULL;
886 int i;
887
888 if (a_ctrl->cam_act_state != CAM_ACTUATOR_START) {
889 rc = -EINVAL;
890 CAM_WARN(CAM_ACTUATOR,
891 "Not in right state to stop : %d",
892 a_ctrl->cam_act_state);
893 goto release_mutex;
894 }
895
896 for (i = 0; i < MAX_PER_FRAME_ARRAY; i++) {
897 i2c_set = &(a_ctrl->i2c_data.per_frame[i]);
898
899 if (i2c_set->is_settings_valid == 1) {
900 rc = delete_request(i2c_set);
901 if (rc < 0)
902 CAM_ERR(CAM_SENSOR,
903 "delete request: %lld rc: %d",
904 i2c_set->request_id, rc);
905 }
906 }
907 a_ctrl->last_flush_req = 0;
908 a_ctrl->cam_act_state = CAM_ACTUATOR_CONFIG;
909 }
910 break;
911 case CAM_CONFIG_DEV: {
912 a_ctrl->setting_apply_state =
913 ACT_APPLY_SETTINGS_LATER;
914 rc = cam_actuator_i2c_pkt_parse(a_ctrl, arg);
915 if (rc < 0) {
916 CAM_ERR(CAM_ACTUATOR, "Failed in actuator Parsing");
917 goto release_mutex;
918 }
919
920 if (a_ctrl->setting_apply_state ==
921 ACT_APPLY_SETTINGS_NOW) {
922 rc = cam_actuator_apply_settings(a_ctrl,
923 &a_ctrl->i2c_data.init_settings);
924 if (rc < 0)
925 CAM_ERR(CAM_ACTUATOR,
926 "Cannot apply Update settings");
927
928 /* Delete the request even if the apply is failed */
929 rc = delete_request(&a_ctrl->i2c_data.init_settings);
930 if (rc < 0) {
931 CAM_ERR(CAM_ACTUATOR,
932 "Failed in Deleting the Init Pkt: %d",
933 rc);
934 goto release_mutex;
935 }
936 }
937 }
938 break;
939 default:
940 CAM_ERR(CAM_ACTUATOR, "Invalid Opcode %d", cmd->op_code);
941 }
942
943release_mutex:
944 mutex_unlock(&(a_ctrl->actuator_mutex));
945
946 return rc;
947}
948
949int32_t cam_actuator_flush_request(struct cam_req_mgr_flush_request *flush_req)
950{
951 int32_t rc = 0, i;
952 uint32_t cancel_req_id_found = 0;
953 struct cam_actuator_ctrl_t *a_ctrl = NULL;
954 struct i2c_settings_array *i2c_set = NULL;
955
956 if (!flush_req)
957 return -EINVAL;
958
959 a_ctrl = (struct cam_actuator_ctrl_t *)
960 cam_get_device_priv(flush_req->dev_hdl);
961 if (!a_ctrl) {
962 CAM_ERR(CAM_ACTUATOR, "Device data is NULL");
963 return -EINVAL;
964 }
965
966 if (a_ctrl->i2c_data.per_frame == NULL) {
967 CAM_ERR(CAM_ACTUATOR, "i2c frame data is NULL");
968 return -EINVAL;
969 }
970
971 mutex_lock(&(a_ctrl->actuator_mutex));
972 if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
973 a_ctrl->last_flush_req = flush_req->req_id;
974 CAM_DBG(CAM_ACTUATOR, "last reqest to flush is %lld",
975 flush_req->req_id);
976 }
977
978 for (i = 0; i < MAX_PER_FRAME_ARRAY; i++) {
979 i2c_set = &(a_ctrl->i2c_data.per_frame[i]);
980
981 if ((flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ)
982 && (i2c_set->request_id != flush_req->req_id))
983 continue;
984
985 if (i2c_set->is_settings_valid == 1) {
986 rc = delete_request(i2c_set);
987 if (rc < 0)
988 CAM_ERR(CAM_ACTUATOR,
989 "delete request: %lld rc: %d",
990 i2c_set->request_id, rc);
991
992 if (flush_req->type ==
993 CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
994 cancel_req_id_found = 1;
995 break;
996 }
997 }
998 }
999
1000 if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
1001 !cancel_req_id_found)
1002 CAM_DBG(CAM_ACTUATOR,
1003 "Flush request id:%lld not found in the pending list",
1004 flush_req->req_id);
1005 mutex_unlock(&(a_ctrl->actuator_mutex));
1006 return rc;
1007}