wcd938x-slave.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/component.h>
  12. #include <soc/soundwire.h>
  13. #ifdef CONFIG_DEBUG_FS
  14. #include <linux/debugfs.h>
  15. #include <linux/uaccess.h>
  16. #define SWR_SLV_MAX_REG_ADDR 0x2009
  17. #define SWR_SLV_START_REG_ADDR 0x40
  18. #define SWR_SLV_MAX_BUF_LEN 20
  19. #define BYTES_PER_LINE 12
  20. #define SWR_SLV_RD_BUF_LEN 8
  21. #define SWR_SLV_WR_BUF_LEN 32
  22. #define SWR_SLV_MAX_DEVICES 2
  23. #endif /* CONFIG_DEBUG_FS */
  24. #define SWR_MAX_RETRY 5
  25. struct wcd938x_slave_priv {
  26. struct swr_device *swr_slave;
  27. #ifdef CONFIG_DEBUG_FS
  28. struct dentry *debugfs_wcd938x_dent;
  29. struct dentry *debugfs_peek;
  30. struct dentry *debugfs_poke;
  31. struct dentry *debugfs_reg_dump;
  32. unsigned int read_data;
  33. #endif
  34. };
  35. #ifdef CONFIG_DEBUG_FS
  36. static int codec_debug_open(struct inode *inode, struct file *file)
  37. {
  38. file->private_data = inode->i_private;
  39. return 0;
  40. }
  41. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  42. {
  43. char *token = NULL;
  44. int base = 0, cnt = 0;
  45. token = strsep(&buf, " ");
  46. for (cnt = 0; cnt < num_of_par; cnt++) {
  47. if (token) {
  48. if ((token[1] == 'x') || (token[1] == 'X'))
  49. base = 16;
  50. else
  51. base = 10;
  52. if (kstrtou32(token, base, &param1[cnt]) != 0)
  53. return -EINVAL;
  54. token = strsep(&buf, " ");
  55. } else {
  56. return -EINVAL;
  57. }
  58. }
  59. return 0;
  60. }
  61. static bool is_swr_slv_reg_readable(int reg)
  62. {
  63. int ret = true;
  64. if (((reg > 0x46) && (reg < 0x4A)) ||
  65. ((reg > 0x4A) && (reg < 0x50)) ||
  66. ((reg > 0x55) && (reg < 0xD0)) ||
  67. ((reg > 0xD0) && (reg < 0xE0)) ||
  68. ((reg > 0xE0) && (reg < 0xF0)) ||
  69. ((reg > 0xF0) && (reg < 0x100)) ||
  70. ((reg > 0x105) && (reg < 0x120)) ||
  71. ((reg > 0x205) && (reg < 0x220)) ||
  72. ((reg > 0x305) && (reg < 0x320)) ||
  73. ((reg > 0x405) && (reg < 0x420)) ||
  74. ((reg > 0x128) && (reg < 0x130)) ||
  75. ((reg > 0x228) && (reg < 0x230)) ||
  76. ((reg > 0x328) && (reg < 0x330)) ||
  77. ((reg > 0x428) && (reg < 0x430)) ||
  78. ((reg > 0x138) && (reg < 0x205)) ||
  79. ((reg > 0x238) && (reg < 0x305)) ||
  80. ((reg > 0x338) && (reg < 0x405)) ||
  81. ((reg > 0x405) && (reg < 0xF00)) ||
  82. ((reg > 0xF05) && (reg < 0xF20)) ||
  83. ((reg > 0xF25) && (reg < 0xF30)) ||
  84. ((reg > 0xF35) && (reg < 0x2000)))
  85. ret = false;
  86. return ret;
  87. }
  88. static ssize_t wcd938x_swrslave_reg_show(struct swr_device *pdev,
  89. char __user *ubuf,
  90. size_t count, loff_t *ppos)
  91. {
  92. int i, reg_val, len;
  93. ssize_t total = 0;
  94. char tmp_buf[SWR_SLV_MAX_BUF_LEN];
  95. if (!ubuf || !ppos)
  96. return 0;
  97. for (i = (((int) *ppos/BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
  98. i <= SWR_SLV_MAX_REG_ADDR; i++) {
  99. if (!is_swr_slv_reg_readable(i))
  100. continue;
  101. swr_read(pdev, pdev->dev_num, i, &reg_val, 1);
  102. len = snprintf(tmp_buf, sizeof(tmp_buf), "0x%.3x: 0x%.2x\n", i,
  103. (reg_val & 0xFF));
  104. if (((total + len) >= count - 1) || (len < 0))
  105. break;
  106. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  107. pr_err_ratelimited("%s: fail to copy reg dump\n", __func__);
  108. total = -EFAULT;
  109. goto copy_err;
  110. }
  111. total += len;
  112. *ppos += len;
  113. }
  114. copy_err:
  115. *ppos = SWR_SLV_MAX_REG_ADDR * BYTES_PER_LINE;
  116. return total;
  117. }
  118. static ssize_t codec_debug_dump(struct file *file, char __user *ubuf,
  119. size_t count, loff_t *ppos)
  120. {
  121. struct swr_device *pdev;
  122. if (!count || !file || !ppos || !ubuf)
  123. return -EINVAL;
  124. pdev = file->private_data;
  125. if (!pdev)
  126. return -EINVAL;
  127. if (*ppos < 0)
  128. return -EINVAL;
  129. return wcd938x_swrslave_reg_show(pdev, ubuf, count, ppos);
  130. }
  131. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  132. size_t count, loff_t *ppos)
  133. {
  134. char lbuf[SWR_SLV_RD_BUF_LEN];
  135. struct swr_device *pdev = NULL;
  136. struct wcd938x_slave_priv *wcd938x_slave = NULL;
  137. if (!count || !file || !ppos || !ubuf)
  138. return -EINVAL;
  139. pdev = file->private_data;
  140. if (!pdev)
  141. return -EINVAL;
  142. wcd938x_slave = swr_get_dev_data(pdev);
  143. if (!wcd938x_slave)
  144. return -EINVAL;
  145. if (*ppos < 0)
  146. return -EINVAL;
  147. snprintf(lbuf, sizeof(lbuf), "0x%x\n",
  148. (wcd938x_slave->read_data & 0xFF));
  149. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  150. strnlen(lbuf, 7));
  151. }
  152. static ssize_t codec_debug_peek_write(struct file *file,
  153. const char __user *ubuf, size_t cnt, loff_t *ppos)
  154. {
  155. char lbuf[SWR_SLV_WR_BUF_LEN];
  156. int rc = 0;
  157. u32 param[5];
  158. struct swr_device *pdev = NULL;
  159. struct wcd938x_slave_priv *wcd938x_slave = NULL;
  160. if (!cnt || !file || !ppos || !ubuf)
  161. return -EINVAL;
  162. pdev = file->private_data;
  163. if (!pdev)
  164. return -EINVAL;
  165. wcd938x_slave = swr_get_dev_data(pdev);
  166. if (!wcd938x_slave)
  167. return -EINVAL;
  168. if (*ppos < 0)
  169. return -EINVAL;
  170. if (cnt > sizeof(lbuf) - 1)
  171. return -EINVAL;
  172. rc = copy_from_user(lbuf, ubuf, cnt);
  173. if (rc)
  174. return -EFAULT;
  175. lbuf[cnt] = '\0';
  176. rc = get_parameters(lbuf, param, 1);
  177. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0)))
  178. return -EINVAL;
  179. swr_read(pdev, pdev->dev_num, param[0], &wcd938x_slave->read_data, 1);
  180. if (rc == 0)
  181. rc = cnt;
  182. else
  183. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  184. return rc;
  185. }
  186. static ssize_t codec_debug_write(struct file *file,
  187. const char __user *ubuf, size_t cnt, loff_t *ppos)
  188. {
  189. char lbuf[SWR_SLV_WR_BUF_LEN];
  190. int rc = 0;
  191. u32 param[5];
  192. struct swr_device *pdev;
  193. if (!file || !ppos || !ubuf)
  194. return -EINVAL;
  195. pdev = file->private_data;
  196. if (!pdev)
  197. return -EINVAL;
  198. if (cnt > sizeof(lbuf) - 1)
  199. return -EINVAL;
  200. rc = copy_from_user(lbuf, ubuf, cnt);
  201. if (rc)
  202. return -EFAULT;
  203. lbuf[cnt] = '\0';
  204. rc = get_parameters(lbuf, param, 2);
  205. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) &&
  206. (param[1] <= 0xFF) && (rc == 0)))
  207. return -EINVAL;
  208. swr_write(pdev, pdev->dev_num, param[0], &param[1]);
  209. if (rc == 0)
  210. rc = cnt;
  211. else
  212. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  213. return rc;
  214. }
  215. static const struct file_operations codec_debug_write_ops = {
  216. .open = codec_debug_open,
  217. .write = codec_debug_write,
  218. };
  219. static const struct file_operations codec_debug_read_ops = {
  220. .open = codec_debug_open,
  221. .read = codec_debug_read,
  222. .write = codec_debug_peek_write,
  223. };
  224. static const struct file_operations codec_debug_dump_ops = {
  225. .open = codec_debug_open,
  226. .read = codec_debug_dump,
  227. };
  228. #endif
  229. static int wcd938x_slave_bind(struct device *dev,
  230. struct device *master, void *data)
  231. {
  232. int ret = 0;
  233. uint8_t devnum = 0;
  234. struct swr_device *pdev = to_swr_device(dev);
  235. int retry = SWR_MAX_RETRY;
  236. if (!pdev) {
  237. pr_err_ratelimited("%s: invalid swr device handle\n", __func__);
  238. return -EINVAL;
  239. }
  240. do {
  241. /* Add delay for soundwire enumeration */
  242. usleep_range(100, 110);
  243. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  244. } while (ret && --retry);
  245. if (ret) {
  246. dev_dbg(&pdev->dev,
  247. "%s get devnum %d for dev addr %llx failed\n",
  248. __func__, devnum, pdev->addr);
  249. ret = -EPROBE_DEFER;
  250. return ret;
  251. }
  252. pdev->dev_num = devnum;
  253. return ret;
  254. }
  255. static void wcd938x_slave_unbind(struct device *dev,
  256. struct device *master, void *data)
  257. {
  258. struct wcd938x_slave_priv *wcd938x_slave = NULL;
  259. struct swr_device *pdev = to_swr_device(dev);
  260. wcd938x_slave = swr_get_dev_data(pdev);
  261. if (!wcd938x_slave) {
  262. dev_err_ratelimited(&pdev->dev, "%s: wcd938x_slave is NULL\n", __func__);
  263. return;
  264. }
  265. }
  266. static const struct swr_device_id wcd938x_swr_id[] = {
  267. {"wcd938x-slave", 0},
  268. {}
  269. };
  270. static const struct of_device_id wcd938x_swr_dt_match[] = {
  271. {
  272. .compatible = "qcom,wcd938x-slave",
  273. },
  274. {}
  275. };
  276. static const struct component_ops wcd938x_slave_comp_ops = {
  277. .bind = wcd938x_slave_bind,
  278. .unbind = wcd938x_slave_unbind,
  279. };
  280. static int wcd938x_swr_probe(struct swr_device *pdev)
  281. {
  282. struct wcd938x_slave_priv *wcd938x_slave = NULL;
  283. wcd938x_slave = devm_kzalloc(&pdev->dev,
  284. sizeof(struct wcd938x_slave_priv), GFP_KERNEL);
  285. if (!wcd938x_slave)
  286. return -ENOMEM;
  287. swr_set_dev_data(pdev, wcd938x_slave);
  288. wcd938x_slave->swr_slave = pdev;
  289. #ifdef CONFIG_DEBUG_FS
  290. if (!wcd938x_slave->debugfs_wcd938x_dent) {
  291. wcd938x_slave->debugfs_wcd938x_dent = debugfs_create_dir(
  292. dev_name(&pdev->dev), 0);
  293. if (!IS_ERR(wcd938x_slave->debugfs_wcd938x_dent)) {
  294. wcd938x_slave->debugfs_peek =
  295. debugfs_create_file("swrslave_peek",
  296. S_IFREG | 0444,
  297. wcd938x_slave->debugfs_wcd938x_dent,
  298. (void *) pdev,
  299. &codec_debug_read_ops);
  300. wcd938x_slave->debugfs_poke =
  301. debugfs_create_file("swrslave_poke",
  302. S_IFREG | 0444,
  303. wcd938x_slave->debugfs_wcd938x_dent,
  304. (void *) pdev,
  305. &codec_debug_write_ops);
  306. wcd938x_slave->debugfs_reg_dump =
  307. debugfs_create_file(
  308. "swrslave_reg_dump",
  309. S_IFREG | 0444,
  310. wcd938x_slave->debugfs_wcd938x_dent,
  311. (void *) pdev,
  312. &codec_debug_dump_ops);
  313. }
  314. }
  315. #endif
  316. return component_add(&pdev->dev, &wcd938x_slave_comp_ops);
  317. }
  318. static int wcd938x_swr_remove(struct swr_device *pdev)
  319. {
  320. #ifdef CONFIG_DEBUG_FS
  321. struct wcd938x_slave_priv *wcd938x_slave = swr_get_dev_data(pdev);
  322. if (wcd938x_slave) {
  323. debugfs_remove_recursive(wcd938x_slave->debugfs_wcd938x_dent);
  324. wcd938x_slave->debugfs_wcd938x_dent = NULL;
  325. }
  326. #endif
  327. component_del(&pdev->dev, &wcd938x_slave_comp_ops);
  328. swr_set_dev_data(pdev, NULL);
  329. swr_remove_device(pdev);
  330. return 0;
  331. }
  332. static struct swr_driver wcd938x_slave_driver = {
  333. .driver = {
  334. .name = "wcd938x-slave",
  335. .owner = THIS_MODULE,
  336. .of_match_table = wcd938x_swr_dt_match,
  337. },
  338. .probe = wcd938x_swr_probe,
  339. .remove = wcd938x_swr_remove,
  340. .id_table = wcd938x_swr_id,
  341. };
  342. static int __init wcd938x_slave_init(void)
  343. {
  344. return swr_driver_register(&wcd938x_slave_driver);
  345. }
  346. static void __exit wcd938x_slave_exit(void)
  347. {
  348. swr_driver_unregister(&wcd938x_slave_driver);
  349. }
  350. module_init(wcd938x_slave_init);
  351. module_exit(wcd938x_slave_exit);
  352. MODULE_DESCRIPTION("WCD938X Swr Slave driver");
  353. MODULE_LICENSE("GPL v2");