avtimer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (c) 2012-2015, 2017 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/fs.h>
  17. #include <linux/cdev.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/device.h>
  20. #include <linux/io.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/avtimer.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/wait.h>
  26. #include <linux/sched.h>
  27. #include <ipc/apr.h>
  28. #include <dsp/q6core.h>
  29. #define DEVICE_NAME "avtimer"
  30. #define TIMEOUT_MS 1000
  31. #define CORE_CLIENT 1
  32. #define TEMP_PORT ((CORE_CLIENT << 8) | 0x0001)
  33. #define SSR_WAKETIME 1000
  34. #define Q6_READY_RETRY 250
  35. #define Q6_READY_MAX_RETRIES 40
  36. #define AVCS_CMD_REMOTE_AVTIMER_VOTE_REQUEST 0x00012914
  37. #define AVCS_CMD_RSP_REMOTE_AVTIMER_VOTE_REQUEST 0x00012915
  38. #define AVCS_CMD_REMOTE_AVTIMER_RELEASE_REQUEST 0x00012916
  39. #define AVTIMER_REG_CNT 2
  40. struct adsp_avt_timer {
  41. struct apr_hdr hdr;
  42. union {
  43. char client_name[8];
  44. u32 avtimer_handle;
  45. };
  46. } __packed;
  47. static int major;
  48. struct avtimer_t {
  49. struct apr_svc *core_handle_q;
  50. struct cdev myc;
  51. struct class *avtimer_class;
  52. struct mutex avtimer_lock;
  53. int avtimer_open_cnt;
  54. struct delayed_work ssr_dwork;
  55. wait_queue_head_t adsp_resp_wait;
  56. int enable_timer_resp_received;
  57. int timer_handle;
  58. void __iomem *p_avtimer_msw;
  59. void __iomem *p_avtimer_lsw;
  60. uint32_t clk_div;
  61. uint32_t clk_mult;
  62. atomic_t adsp_ready;
  63. int num_retries;
  64. };
  65. static struct avtimer_t avtimer;
  66. static int32_t aprv2_core_fn_q(struct apr_client_data *data, void *priv)
  67. {
  68. uint32_t *payload1;
  69. if (!data) {
  70. pr_err("%s: Invalid params\n", __func__);
  71. return -EINVAL;
  72. }
  73. pr_debug("%s: core msg: payload len = %u, apr resp opcode = 0x%X\n",
  74. __func__, data->payload_size, data->opcode);
  75. switch (data->opcode) {
  76. case APR_BASIC_RSP_RESULT:{
  77. if (!data->payload_size) {
  78. pr_err("%s: APR_BASIC_RSP_RESULT No Payload ",
  79. __func__);
  80. return 0;
  81. }
  82. payload1 = data->payload;
  83. switch (payload1[0]) {
  84. case AVCS_CMD_REMOTE_AVTIMER_RELEASE_REQUEST:
  85. pr_debug("%s: Cmd = TIMER RELEASE status[0x%x]\n",
  86. __func__, payload1[1]);
  87. break;
  88. default:
  89. pr_err("Invalid cmd rsp[0x%x][0x%x]\n",
  90. payload1[0], payload1[1]);
  91. break;
  92. }
  93. break;
  94. }
  95. case RESET_EVENTS:{
  96. pr_debug("%s: Reset event received in AV timer\n", __func__);
  97. apr_reset(avtimer.core_handle_q);
  98. avtimer.core_handle_q = NULL;
  99. avtimer.avtimer_open_cnt = 0;
  100. atomic_set(&avtimer.adsp_ready, 0);
  101. schedule_delayed_work(&avtimer.ssr_dwork,
  102. msecs_to_jiffies(SSR_WAKETIME));
  103. break;
  104. }
  105. case AVCS_CMD_RSP_REMOTE_AVTIMER_VOTE_REQUEST:
  106. payload1 = data->payload;
  107. pr_debug("%s: RSP_REMOTE_AVTIMER_VOTE_REQUEST handle %x\n",
  108. __func__, payload1[0]);
  109. avtimer.timer_handle = payload1[0];
  110. avtimer.enable_timer_resp_received = 1;
  111. wake_up(&avtimer.adsp_resp_wait);
  112. break;
  113. default:
  114. pr_err("%s: Message adspcore svc: %d\n",
  115. __func__, data->opcode);
  116. break;
  117. }
  118. return 0;
  119. }
  120. int avcs_core_open(void)
  121. {
  122. if (!avtimer.core_handle_q)
  123. avtimer.core_handle_q = apr_register("ADSP", "CORE",
  124. aprv2_core_fn_q, TEMP_PORT, NULL);
  125. pr_debug("%s: Open_q %p\n", __func__, avtimer.core_handle_q);
  126. if (!avtimer.core_handle_q) {
  127. pr_err("%s: Unable to register CORE\n", __func__);
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(avcs_core_open);
  133. static int avcs_core_disable_avtimer(int timerhandle)
  134. {
  135. int rc = -EINVAL;
  136. struct adsp_avt_timer payload;
  137. if (!timerhandle) {
  138. pr_err("%s: Invalid timer handle\n", __func__);
  139. return -EINVAL;
  140. }
  141. memset(&payload, 0, sizeof(payload));
  142. rc = avcs_core_open();
  143. if (!rc && avtimer.core_handle_q) {
  144. payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  145. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  146. payload.hdr.pkt_size =
  147. sizeof(struct adsp_avt_timer);
  148. payload.hdr.src_svc = avtimer.core_handle_q->id;
  149. payload.hdr.src_domain = APR_DOMAIN_APPS;
  150. payload.hdr.dest_domain = APR_DOMAIN_ADSP;
  151. payload.hdr.dest_svc = APR_SVC_ADSP_CORE;
  152. payload.hdr.src_port = TEMP_PORT;
  153. payload.hdr.dest_port = TEMP_PORT;
  154. payload.hdr.token = CORE_CLIENT;
  155. payload.hdr.opcode = AVCS_CMD_REMOTE_AVTIMER_RELEASE_REQUEST;
  156. payload.avtimer_handle = timerhandle;
  157. pr_debug("%s: disable avtimer opcode %x handle %x\n",
  158. __func__, payload.hdr.opcode, payload.avtimer_handle);
  159. rc = apr_send_pkt(avtimer.core_handle_q,
  160. (uint32_t *)&payload);
  161. if (rc < 0)
  162. pr_err("%s: Enable AVtimer failed op[0x%x]rc[%d]\n",
  163. __func__, payload.hdr.opcode, rc);
  164. else
  165. rc = 0;
  166. }
  167. return rc;
  168. }
  169. static int avcs_core_enable_avtimer(char *client_name)
  170. {
  171. int rc = -EINVAL, ret = -EINVAL;
  172. struct adsp_avt_timer payload;
  173. if (!client_name) {
  174. pr_err("%s: Invalid params\n", __func__);
  175. return -EINVAL;
  176. }
  177. memset(&payload, 0, sizeof(payload));
  178. rc = avcs_core_open();
  179. if (!rc && avtimer.core_handle_q) {
  180. avtimer.enable_timer_resp_received = 0;
  181. payload.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_EVENT,
  182. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  183. payload.hdr.pkt_size =
  184. sizeof(struct adsp_avt_timer);
  185. payload.hdr.src_svc = avtimer.core_handle_q->id;
  186. payload.hdr.src_domain = APR_DOMAIN_APPS;
  187. payload.hdr.dest_domain = APR_DOMAIN_ADSP;
  188. payload.hdr.dest_svc = APR_SVC_ADSP_CORE;
  189. payload.hdr.src_port = TEMP_PORT;
  190. payload.hdr.dest_port = TEMP_PORT;
  191. payload.hdr.token = CORE_CLIENT;
  192. payload.hdr.opcode = AVCS_CMD_REMOTE_AVTIMER_VOTE_REQUEST;
  193. strlcpy(payload.client_name, client_name,
  194. sizeof(payload.client_name));
  195. pr_debug("%s: enable avtimer opcode %x client name %s\n",
  196. __func__, payload.hdr.opcode, payload.client_name);
  197. rc = apr_send_pkt(avtimer.core_handle_q,
  198. (uint32_t *)&payload);
  199. if (rc < 0) {
  200. pr_err("%s: Enable AVtimer failed op[0x%x]rc[%d]\n",
  201. __func__, payload.hdr.opcode, rc);
  202. goto bail;
  203. } else
  204. rc = 0;
  205. ret = wait_event_timeout(avtimer.adsp_resp_wait,
  206. (avtimer.enable_timer_resp_received == 1),
  207. msecs_to_jiffies(TIMEOUT_MS));
  208. if (!ret) {
  209. pr_err("%s: wait_event timeout for Enable timer\n",
  210. __func__);
  211. rc = -ETIMEDOUT;
  212. }
  213. if (rc)
  214. avtimer.timer_handle = 0;
  215. }
  216. bail:
  217. return rc;
  218. }
  219. int avcs_core_disable_power_collapse(int enable)
  220. {
  221. int rc = 0;
  222. mutex_lock(&avtimer.avtimer_lock);
  223. if (enable) {
  224. if (avtimer.avtimer_open_cnt) {
  225. avtimer.avtimer_open_cnt++;
  226. pr_debug("%s: opened avtimer open count=%d\n",
  227. __func__, avtimer.avtimer_open_cnt);
  228. rc = 0;
  229. goto done;
  230. }
  231. rc = avcs_core_enable_avtimer("timer");
  232. if (!rc) {
  233. avtimer.avtimer_open_cnt++;
  234. atomic_set(&avtimer.adsp_ready, 1);
  235. }
  236. } else {
  237. if (avtimer.avtimer_open_cnt > 0) {
  238. avtimer.avtimer_open_cnt--;
  239. if (!avtimer.avtimer_open_cnt) {
  240. rc = avcs_core_disable_avtimer(
  241. avtimer.timer_handle);
  242. avtimer.timer_handle = 0;
  243. }
  244. }
  245. }
  246. done:
  247. mutex_unlock(&avtimer.avtimer_lock);
  248. return rc;
  249. }
  250. EXPORT_SYMBOL(avcs_core_disable_power_collapse);
  251. static void reset_work(struct work_struct *work)
  252. {
  253. if (q6core_is_adsp_ready()) {
  254. avcs_core_disable_power_collapse(1);
  255. avtimer.num_retries = Q6_READY_MAX_RETRIES;
  256. return;
  257. }
  258. pr_debug("%s:Q6 not ready-retry after sometime\n", __func__);
  259. if (--avtimer.num_retries > 0) {
  260. schedule_delayed_work(&avtimer.ssr_dwork,
  261. msecs_to_jiffies(Q6_READY_RETRY));
  262. } else {
  263. pr_err("%s: Q6 failed responding after multiple retries\n",
  264. __func__);
  265. avtimer.num_retries = Q6_READY_MAX_RETRIES;
  266. }
  267. }
  268. int avcs_core_query_timer(uint64_t *avtimer_tick)
  269. {
  270. uint32_t avtimer_msw = 0, avtimer_lsw = 0;
  271. uint64_t avtimer_tick_temp;
  272. if (!atomic_read(&avtimer.adsp_ready)) {
  273. pr_debug("%s:In SSR, return\n", __func__);
  274. return -ENETRESET;
  275. }
  276. avtimer_lsw = ioread32(avtimer.p_avtimer_lsw);
  277. avtimer_msw = ioread32(avtimer.p_avtimer_msw);
  278. avtimer_tick_temp = (uint64_t)((uint64_t)avtimer_msw << 32)
  279. | avtimer_lsw;
  280. *avtimer_tick = mul_u64_u32_div(avtimer_tick_temp, avtimer.clk_mult,
  281. avtimer.clk_div);
  282. pr_debug_ratelimited("%s:Avtimer: msw: %u, lsw: %u, tick: %llu\n",
  283. __func__,
  284. avtimer_msw, avtimer_lsw, *avtimer_tick);
  285. return 0;
  286. }
  287. EXPORT_SYMBOL(avcs_core_query_timer);
  288. static int avtimer_open(struct inode *inode, struct file *file)
  289. {
  290. return avcs_core_disable_power_collapse(1);
  291. }
  292. static int avtimer_release(struct inode *inode, struct file *file)
  293. {
  294. return avcs_core_disable_power_collapse(0);
  295. }
  296. /*
  297. * ioctl call provides GET_AVTIMER
  298. */
  299. static long avtimer_ioctl(struct file *file, unsigned int ioctl_num,
  300. unsigned long ioctl_param)
  301. {
  302. switch (ioctl_num) {
  303. case IOCTL_GET_AVTIMER_TICK:
  304. {
  305. uint64_t avtimer_tick = 0;
  306. int rc;
  307. rc = avcs_core_query_timer(&avtimer_tick);
  308. if (rc) {
  309. pr_err("%s: Error: Invalid AV Timer tick, rc = %d\n",
  310. __func__, rc);
  311. return rc;
  312. }
  313. pr_debug_ratelimited("%s: AV Timer tick: time %llx\n",
  314. __func__, avtimer_tick);
  315. if (copy_to_user((void __user *)ioctl_param, &avtimer_tick,
  316. sizeof(avtimer_tick))) {
  317. pr_err("%s: copy_to_user failed\n", __func__);
  318. return -EFAULT;
  319. }
  320. }
  321. break;
  322. default:
  323. pr_err("%s: invalid cmd\n", __func__);
  324. return -EINVAL;
  325. }
  326. return 0;
  327. }
  328. static const struct file_operations avtimer_fops = {
  329. .unlocked_ioctl = avtimer_ioctl,
  330. .compat_ioctl = avtimer_ioctl,
  331. .open = avtimer_open,
  332. .release = avtimer_release
  333. };
  334. static int dev_avtimer_probe(struct platform_device *pdev)
  335. {
  336. int result = 0;
  337. dev_t dev = MKDEV(major, 0);
  338. struct device *device_handle;
  339. struct resource *reg_lsb = NULL, *reg_msb = NULL;
  340. uint32_t clk_div_val;
  341. uint32_t clk_mult_val;
  342. if (!pdev) {
  343. pr_err("%s: Invalid params\n", __func__);
  344. return -EINVAL;
  345. }
  346. reg_lsb = platform_get_resource_byname(pdev,
  347. IORESOURCE_MEM, "avtimer_lsb_addr");
  348. if (!reg_lsb) {
  349. dev_err(&pdev->dev, "%s: Looking up %s property",
  350. "avtimer_lsb_addr", __func__);
  351. return -EINVAL;
  352. }
  353. reg_msb = platform_get_resource_byname(pdev,
  354. IORESOURCE_MEM, "avtimer_msb_addr");
  355. if (!reg_msb) {
  356. dev_err(&pdev->dev, "%s: Looking up %s property",
  357. "avtimer_msb_addr", __func__);
  358. return -EINVAL;
  359. }
  360. INIT_DELAYED_WORK(&avtimer.ssr_dwork, reset_work);
  361. avtimer.p_avtimer_lsw = devm_ioremap_nocache(&pdev->dev,
  362. reg_lsb->start, resource_size(reg_lsb));
  363. if (!avtimer.p_avtimer_lsw) {
  364. dev_err(&pdev->dev, "%s: ioremap failed for lsb avtimer register",
  365. __func__);
  366. return -ENOMEM;
  367. }
  368. avtimer.p_avtimer_msw = devm_ioremap_nocache(&pdev->dev,
  369. reg_msb->start, resource_size(reg_msb));
  370. if (!avtimer.p_avtimer_msw) {
  371. dev_err(&pdev->dev, "%s: ioremap failed for msb avtimer register",
  372. __func__);
  373. goto unmap;
  374. }
  375. avtimer.num_retries = Q6_READY_MAX_RETRIES;
  376. /* get the device number */
  377. if (major)
  378. result = register_chrdev_region(dev, 1, DEVICE_NAME);
  379. else {
  380. result = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
  381. major = MAJOR(dev);
  382. }
  383. if (result < 0) {
  384. dev_err(&pdev->dev, "%s: Registering avtimer device failed\n",
  385. __func__);
  386. goto unmap;
  387. }
  388. avtimer.avtimer_class = class_create(THIS_MODULE, "avtimer");
  389. if (IS_ERR(avtimer.avtimer_class)) {
  390. result = PTR_ERR(avtimer.avtimer_class);
  391. dev_err(&pdev->dev, "%s: Error creating avtimer class: %d\n",
  392. __func__, result);
  393. goto unregister_chrdev_region;
  394. }
  395. cdev_init(&avtimer.myc, &avtimer_fops);
  396. result = cdev_add(&avtimer.myc, dev, 1);
  397. if (result < 0) {
  398. dev_err(&pdev->dev, "%s: Registering file operations failed\n",
  399. __func__);
  400. goto class_destroy;
  401. }
  402. device_handle = device_create(avtimer.avtimer_class,
  403. NULL, avtimer.myc.dev, NULL, "avtimer");
  404. if (IS_ERR(device_handle)) {
  405. result = PTR_ERR(device_handle);
  406. pr_err("%s: device_create failed: %d\n", __func__, result);
  407. goto class_destroy;
  408. }
  409. init_waitqueue_head(&avtimer.adsp_resp_wait);
  410. mutex_init(&avtimer.avtimer_lock);
  411. avtimer.avtimer_open_cnt = 0;
  412. pr_debug("%s: Device create done for avtimer major=%d\n",
  413. __func__, major);
  414. if (of_property_read_u32(pdev->dev.of_node,
  415. "qcom,clk-div", &clk_div_val))
  416. avtimer.clk_div = 1;
  417. else
  418. avtimer.clk_div = clk_div_val;
  419. if (of_property_read_u32(pdev->dev.of_node,
  420. "qcom,clk-mult", &clk_mult_val))
  421. avtimer.clk_mult = 1;
  422. else
  423. avtimer.clk_mult = clk_mult_val;
  424. pr_debug("%s: avtimer.clk_div = %d, avtimer.clk_mult = %d\n",
  425. __func__, avtimer.clk_div, avtimer.clk_mult);
  426. return 0;
  427. class_destroy:
  428. class_destroy(avtimer.avtimer_class);
  429. unregister_chrdev_region:
  430. unregister_chrdev_region(MKDEV(major, 0), 1);
  431. unmap:
  432. if (avtimer.p_avtimer_lsw)
  433. devm_iounmap(&pdev->dev, avtimer.p_avtimer_lsw);
  434. if (avtimer.p_avtimer_msw)
  435. devm_iounmap(&pdev->dev, avtimer.p_avtimer_msw);
  436. avtimer.p_avtimer_lsw = NULL;
  437. avtimer.p_avtimer_msw = NULL;
  438. return result;
  439. }
  440. static int dev_avtimer_remove(struct platform_device *pdev)
  441. {
  442. pr_debug("%s: dev_avtimer_remove\n", __func__);
  443. if (avtimer.p_avtimer_lsw)
  444. devm_iounmap(&pdev->dev, avtimer.p_avtimer_lsw);
  445. if (avtimer.p_avtimer_msw)
  446. devm_iounmap(&pdev->dev, avtimer.p_avtimer_msw);
  447. device_destroy(avtimer.avtimer_class, avtimer.myc.dev);
  448. cdev_del(&avtimer.myc);
  449. class_destroy(avtimer.avtimer_class);
  450. unregister_chrdev_region(MKDEV(major, 0), 1);
  451. return 0;
  452. }
  453. static const struct of_device_id avtimer_machine_of_match[] = {
  454. { .compatible = "qcom,avtimer", },
  455. {},
  456. };
  457. static struct platform_driver dev_avtimer_driver = {
  458. .probe = dev_avtimer_probe,
  459. .remove = dev_avtimer_remove,
  460. .driver = {
  461. .name = "dev_avtimer",
  462. .of_match_table = avtimer_machine_of_match,
  463. },
  464. };
  465. int __init avtimer_init(void)
  466. {
  467. s32 rc;
  468. rc = platform_driver_register(&dev_avtimer_driver);
  469. if (rc < 0) {
  470. pr_err("%s: platform_driver_register failed\n", __func__);
  471. goto error_platform_driver;
  472. }
  473. pr_debug("%s: dev_avtimer_init : done\n", __func__);
  474. return 0;
  475. error_platform_driver:
  476. pr_err("%s: encounterd error\n", __func__);
  477. return rc;
  478. }
  479. void avtimer_exit(void)
  480. {
  481. platform_driver_unregister(&dev_avtimer_driver);
  482. }
  483. MODULE_DESCRIPTION("avtimer driver");
  484. MODULE_LICENSE("GPL v2");