wcd9378-slave.c 9.4 KB

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