wcd937x_slave.c 9.8 KB

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