wcd939x-slave.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, 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 wcd939x_slave_priv {
  26. struct swr_device *swr_slave;
  27. #ifdef CONFIG_DEBUG_FS
  28. struct dentry *debugfs_wcd939x_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 > 0x438) && (reg < 0x2000)))
  82. ret = false;
  83. return ret;
  84. }
  85. static ssize_t wcd939x_swrslave_reg_show(struct swr_device *pdev,
  86. char __user *ubuf,
  87. size_t count, loff_t *ppos)
  88. {
  89. int i, reg_val, len;
  90. ssize_t total = 0;
  91. char tmp_buf[SWR_SLV_MAX_BUF_LEN];
  92. if (!ubuf || !ppos)
  93. return 0;
  94. for (i = (((int) *ppos/BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
  95. i <= SWR_SLV_MAX_REG_ADDR; i++) {
  96. if (!is_swr_slv_reg_readable(i))
  97. continue;
  98. swr_read(pdev, pdev->dev_num, i, &reg_val, 1);
  99. len = snprintf(tmp_buf, sizeof(tmp_buf), "0x%.3x: 0x%.2x\n", i,
  100. (reg_val & 0xFF));
  101. if (((total + len) >= count - 1) || (len < 0))
  102. break;
  103. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  104. pr_err_ratelimited("%s: fail to copy reg dump\n", __func__);
  105. total = -EFAULT;
  106. goto copy_err;
  107. }
  108. total += len;
  109. *ppos += len;
  110. }
  111. copy_err:
  112. *ppos = SWR_SLV_MAX_REG_ADDR * BYTES_PER_LINE;
  113. return total;
  114. }
  115. static ssize_t codec_debug_dump(struct file *file, char __user *ubuf,
  116. size_t count, loff_t *ppos)
  117. {
  118. struct swr_device *pdev;
  119. if (!count || !file || !ppos || !ubuf)
  120. return -EINVAL;
  121. pdev = file->private_data;
  122. if (!pdev)
  123. return -EINVAL;
  124. if (*ppos < 0)
  125. return -EINVAL;
  126. return wcd939x_swrslave_reg_show(pdev, ubuf, count, ppos);
  127. }
  128. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  129. size_t count, loff_t *ppos)
  130. {
  131. char lbuf[SWR_SLV_RD_BUF_LEN];
  132. struct swr_device *pdev = NULL;
  133. struct wcd939x_slave_priv *wcd939x_slave = NULL;
  134. if (!count || !file || !ppos || !ubuf)
  135. return -EINVAL;
  136. pdev = file->private_data;
  137. if (!pdev)
  138. return -EINVAL;
  139. wcd939x_slave = swr_get_dev_data(pdev);
  140. if (!wcd939x_slave)
  141. return -EINVAL;
  142. if (*ppos < 0)
  143. return -EINVAL;
  144. snprintf(lbuf, sizeof(lbuf), "0x%x\n",
  145. (wcd939x_slave->read_data & 0xFF));
  146. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  147. strnlen(lbuf, 7));
  148. }
  149. static ssize_t codec_debug_peek_write(struct file *file,
  150. const char __user *ubuf, size_t cnt, loff_t *ppos)
  151. {
  152. char lbuf[SWR_SLV_WR_BUF_LEN];
  153. int rc = 0;
  154. u32 param[5];
  155. struct swr_device *pdev = NULL;
  156. struct wcd939x_slave_priv *wcd939x_slave = NULL;
  157. if (!cnt || !file || !ppos || !ubuf)
  158. return -EINVAL;
  159. pdev = file->private_data;
  160. if (!pdev)
  161. return -EINVAL;
  162. wcd939x_slave = swr_get_dev_data(pdev);
  163. if (!wcd939x_slave)
  164. return -EINVAL;
  165. if (*ppos < 0)
  166. return -EINVAL;
  167. if (cnt > sizeof(lbuf) - 1)
  168. return -EINVAL;
  169. rc = copy_from_user(lbuf, ubuf, cnt);
  170. if (rc)
  171. return -EFAULT;
  172. lbuf[cnt] = '\0';
  173. rc = get_parameters(lbuf, param, 1);
  174. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0)))
  175. return -EINVAL;
  176. swr_read(pdev, pdev->dev_num, param[0], &wcd939x_slave->read_data, 1);
  177. if (rc == 0)
  178. rc = cnt;
  179. else
  180. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  181. return rc;
  182. }
  183. static ssize_t codec_debug_write(struct file *file,
  184. const char __user *ubuf, size_t cnt, loff_t *ppos)
  185. {
  186. char lbuf[SWR_SLV_WR_BUF_LEN];
  187. int rc = 0;
  188. u32 param[5];
  189. struct swr_device *pdev;
  190. if (!file || !ppos || !ubuf)
  191. return -EINVAL;
  192. pdev = file->private_data;
  193. if (!pdev)
  194. return -EINVAL;
  195. if (cnt > sizeof(lbuf) - 1)
  196. return -EINVAL;
  197. rc = copy_from_user(lbuf, ubuf, cnt);
  198. if (rc)
  199. return -EFAULT;
  200. lbuf[cnt] = '\0';
  201. rc = get_parameters(lbuf, param, 2);
  202. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) &&
  203. (param[1] <= 0xFF) && (rc == 0)))
  204. return -EINVAL;
  205. swr_write(pdev, pdev->dev_num, param[0], &param[1]);
  206. if (rc == 0)
  207. rc = cnt;
  208. else
  209. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  210. return rc;
  211. }
  212. static const struct file_operations codec_debug_write_ops = {
  213. .open = codec_debug_open,
  214. .write = codec_debug_write,
  215. };
  216. static const struct file_operations codec_debug_read_ops = {
  217. .open = codec_debug_open,
  218. .read = codec_debug_read,
  219. .write = codec_debug_peek_write,
  220. };
  221. static const struct file_operations codec_debug_dump_ops = {
  222. .open = codec_debug_open,
  223. .read = codec_debug_dump,
  224. };
  225. #endif
  226. static int wcd939x_slave_bind(struct device *dev,
  227. struct device *master, void *data)
  228. {
  229. int ret = 0;
  230. uint8_t devnum = 0;
  231. struct swr_device *pdev = to_swr_device(dev);
  232. int retry = SWR_MAX_RETRY;
  233. if (!pdev) {
  234. pr_err_ratelimited("%s: invalid swr device handle\n", __func__);
  235. return -EINVAL;
  236. }
  237. do {
  238. /* Add delay for soundwire enumeration */
  239. usleep_range(100, 110);
  240. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  241. } while (ret && --retry);
  242. if (ret) {
  243. dev_dbg(&pdev->dev,
  244. "%s get devnum %d for dev addr %llx failed\n",
  245. __func__, devnum, pdev->addr);
  246. ret = -EPROBE_DEFER;
  247. return ret;
  248. }
  249. pdev->dev_num = devnum;
  250. return ret;
  251. }
  252. static void wcd939x_slave_unbind(struct device *dev,
  253. struct device *master, void *data)
  254. {
  255. struct wcd939x_slave_priv *wcd939x_slave = NULL;
  256. struct swr_device *pdev = to_swr_device(dev);
  257. wcd939x_slave = swr_get_dev_data(pdev);
  258. if (!wcd939x_slave) {
  259. dev_err_ratelimited(&pdev->dev, "%s: wcd939x_slave is NULL\n", __func__);
  260. return;
  261. }
  262. }
  263. static const struct swr_device_id wcd939x_swr_id[] = {
  264. {"wcd939x-slave", 0},
  265. {}
  266. };
  267. static const struct of_device_id wcd939x_swr_dt_match[] = {
  268. {
  269. .compatible = "qcom,wcd939x-slave",
  270. },
  271. {}
  272. };
  273. static const struct component_ops wcd939x_slave_comp_ops = {
  274. .bind = wcd939x_slave_bind,
  275. .unbind = wcd939x_slave_unbind,
  276. };
  277. static int wcd939x_swr_probe(struct swr_device *pdev)
  278. {
  279. struct wcd939x_slave_priv *wcd939x_slave = NULL;
  280. wcd939x_slave = devm_kzalloc(&pdev->dev,
  281. sizeof(struct wcd939x_slave_priv), GFP_KERNEL);
  282. if (!wcd939x_slave)
  283. return -ENOMEM;
  284. swr_set_dev_data(pdev, wcd939x_slave);
  285. wcd939x_slave->swr_slave = pdev;
  286. #ifdef CONFIG_DEBUG_FS
  287. if (!wcd939x_slave->debugfs_wcd939x_dent) {
  288. wcd939x_slave->debugfs_wcd939x_dent = debugfs_create_dir(
  289. dev_name(&pdev->dev), 0);
  290. if (!IS_ERR(wcd939x_slave->debugfs_wcd939x_dent)) {
  291. wcd939x_slave->debugfs_peek =
  292. debugfs_create_file("swrslave_peek",
  293. S_IFREG | 0444,
  294. wcd939x_slave->debugfs_wcd939x_dent,
  295. (void *) pdev,
  296. &codec_debug_read_ops);
  297. wcd939x_slave->debugfs_poke =
  298. debugfs_create_file("swrslave_poke",
  299. S_IFREG | 0444,
  300. wcd939x_slave->debugfs_wcd939x_dent,
  301. (void *) pdev,
  302. &codec_debug_write_ops);
  303. wcd939x_slave->debugfs_reg_dump =
  304. debugfs_create_file(
  305. "swrslave_reg_dump",
  306. S_IFREG | 0444,
  307. wcd939x_slave->debugfs_wcd939x_dent,
  308. (void *) pdev,
  309. &codec_debug_dump_ops);
  310. }
  311. }
  312. #endif
  313. return component_add(&pdev->dev, &wcd939x_slave_comp_ops);
  314. }
  315. static int wcd939x_swr_remove(struct swr_device *pdev)
  316. {
  317. #ifdef CONFIG_DEBUG_FS
  318. struct wcd939x_slave_priv *wcd939x_slave = swr_get_dev_data(pdev);
  319. if (wcd939x_slave) {
  320. debugfs_remove_recursive(wcd939x_slave->debugfs_wcd939x_dent);
  321. wcd939x_slave->debugfs_wcd939x_dent = NULL;
  322. }
  323. #endif
  324. component_del(&pdev->dev, &wcd939x_slave_comp_ops);
  325. swr_set_dev_data(pdev, NULL);
  326. swr_remove_device(pdev);
  327. return 0;
  328. }
  329. static struct swr_driver wcd939x_slave_driver = {
  330. .driver = {
  331. .name = "wcd939x-slave",
  332. .owner = THIS_MODULE,
  333. .of_match_table = wcd939x_swr_dt_match,
  334. },
  335. .probe = wcd939x_swr_probe,
  336. .remove = wcd939x_swr_remove,
  337. .id_table = wcd939x_swr_id,
  338. };
  339. static int __init wcd939x_slave_init(void)
  340. {
  341. return swr_driver_register(&wcd939x_slave_driver);
  342. }
  343. static void __exit wcd939x_slave_exit(void)
  344. {
  345. swr_driver_unregister(&wcd939x_slave_driver);
  346. }
  347. module_init(wcd939x_slave_init);
  348. module_exit(wcd939x_slave_exit);
  349. MODULE_DESCRIPTION("WCD939X Swr Slave driver");
  350. MODULE_LICENSE("GPL v2");