wcd938x-slave.c 9.4 KB

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