dw-dphy-sysfs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018-2019 Synopsys, Inc. and/or its affiliates.
  4. *
  5. * Synopsys DesignWare MIPI D-PHY controller driver.
  6. * SysFS components for the platform driver
  7. *
  8. * Author: Luis Oliveira <luis.oliveira@synopsys.com>
  9. */
  10. //#define DEBUG 1
  11. #include "dw-dphy-rx.h"
  12. #include "bm_csi_hw.h"
  13. static ssize_t dphy_reset_show(struct device *dev,
  14. struct device_attribute *attr, char *buf)
  15. {
  16. struct platform_device *pdev = to_platform_device(dev);
  17. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  18. struct dw_dphy_rx *dphy = drvdata->dphy;
  19. char buffer[15];
  20. dw_dphy_write(dphy, R_CSI2_DPHY_RSTZ, 0);
  21. usleep_range(100, 200);
  22. dw_dphy_write(dphy, R_CSI2_DPHY_RSTZ, 1);
  23. return strlcpy(buf, buffer, PAGE_SIZE);
  24. }
  25. static ssize_t dphy_freq_store(struct device *dev,
  26. struct device_attribute *attr,
  27. const char *buf,
  28. size_t count)
  29. {
  30. int ret;
  31. unsigned long freq;
  32. struct platform_device *pdev = to_platform_device(dev);
  33. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  34. struct dw_dphy_rx *dphy = drvdata->dphy;
  35. ret = kstrtoul(buf, 10, &freq);
  36. if (ret < 0)
  37. return ret;
  38. if (freq > 2500) {
  39. dev_info(dev, "Freq must be under 2500 Mhz\n");
  40. return count;
  41. }
  42. if (freq < 80) {
  43. dev_info(dev, "Freq must be over 80 Mhz\n");
  44. return count;
  45. }
  46. dev_vdbg(dev, "Data Rate %lu Mbps\n", freq);
  47. dphy->dphy_freq = freq*1000;
  48. return count;
  49. }
  50. static ssize_t dphy_freq_show(struct device *dev, struct device_attribute *attr,
  51. char *buf)
  52. {
  53. struct platform_device *pdev = to_platform_device(dev);
  54. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  55. struct dw_dphy_rx *dphy = drvdata->dphy;
  56. char buffer[15];
  57. snprintf(buffer,
  58. sizeof(buffer),
  59. "Freq %d\n", dphy->dphy_freq);
  60. return strlcpy(buf, buffer, PAGE_SIZE);
  61. }
  62. static ssize_t dphy_addr_show(struct device *dev, struct device_attribute *attr,
  63. char *buf)
  64. {
  65. struct platform_device *pdev = to_platform_device(dev);
  66. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  67. struct dw_dphy_rx *dphy = drvdata->dphy;
  68. char buffer[100];
  69. unsigned long addr;
  70. unsigned long data;
  71. int ret;
  72. ret = kstrtoul(buf, 16, &addr);
  73. if (ret < 0) {
  74. return ret;
  75. }
  76. data = dw_dphy_read(dphy, addr);
  77. snprintf(buffer,
  78. sizeof(buffer),
  79. "reg_0x%x = 0x%x\n", addr, data);
  80. return strlcpy(buf, buffer, PAGE_SIZE);
  81. }
  82. static ssize_t dphy_addr_store(struct device *dev,
  83. struct device_attribute *attr,
  84. const char *buf,
  85. size_t count)
  86. {
  87. struct platform_device *pdev = to_platform_device(dev);
  88. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  89. struct dw_dphy_rx *dphy = drvdata->dphy;
  90. unsigned long addr;
  91. unsigned long data;
  92. int ret;
  93. ret = kstrtoul(buf, 16, &addr);
  94. if (ret < 0) {
  95. return ret;
  96. }
  97. data = addr & 0xff;
  98. addr &= 0xfff00;
  99. addr = addr >> 8;
  100. dw_dphy_write(dphy, addr, data);
  101. printk("dphy write reg_0x%x = 0x%x", addr, data);
  102. return count;
  103. }
  104. #if IS_ENABLED(CONFIG_DWC_MIPI_TC_DPHY_GEN3)
  105. #if 0
  106. static ssize_t idelay_show(struct device *dev, struct device_attribute *attr,
  107. char *buf)
  108. {
  109. struct platform_device *pdev = to_platform_device(dev);
  110. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  111. struct dw_dphy_rx *dphy = drvdata->dphy;
  112. char buffer[15];
  113. snprintf(buffer,
  114. sizeof(buffer), "idelay %d\n", dw_dphy_if_get_idelay(dphy));
  115. return strlcpy(buf, buffer, PAGE_SIZE);
  116. }
  117. static ssize_t idelay_store(struct device *dev, struct device_attribute *attr,
  118. const char *buf, size_t count)
  119. {
  120. struct platform_device *pdev = to_platform_device(dev);
  121. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  122. struct dw_dphy_rx *dphy = drvdata->dphy;
  123. unsigned long val;
  124. u8 lane, delay;
  125. int ret;
  126. ret = kstrtoul(buf, 16, &val);
  127. if (ret < 0)
  128. return ret;
  129. lane = (u8)val;
  130. delay = (u8)(val >> 8);
  131. dev_vdbg(dev, "Lanes %u\n", lane);
  132. dev_vdbg(dev, "Delay %u\n", delay);
  133. dw_dphy_if_set_idelay_lane(dphy, delay, lane);
  134. return count;
  135. }
  136. #endif
  137. #endif
  138. static ssize_t len_config_store(struct device *dev,
  139. struct device_attribute *attr,
  140. const char *buf,
  141. size_t count)
  142. {
  143. struct platform_device *pdev = to_platform_device(dev);
  144. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  145. struct dw_dphy_rx *dphy = drvdata->dphy;
  146. unsigned long length;
  147. int ret;
  148. ret = kstrtoul(buf, 10, &length);
  149. if (ret < 0)
  150. return ret;
  151. if (length == BIT8)
  152. dev_vdbg(dev, "Configured for 8-bit interface\n");
  153. else if (length == BIT12)
  154. dev_vdbg(dev, "Configured for 12-bit interface\n");
  155. else
  156. return count;
  157. dphy->dphy_te_len = length;
  158. return count;
  159. }
  160. static ssize_t len_config_show(struct device *dev,
  161. struct device_attribute *attr, char *buf)
  162. {
  163. struct platform_device *pdev = to_platform_device(dev);
  164. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  165. struct dw_dphy_rx *dphy = drvdata->dphy;
  166. char buffer[20];
  167. snprintf(buffer, sizeof(buffer), "Length %d\n", dphy->dphy_te_len);
  168. return strlcpy(buf, buffer, PAGE_SIZE);
  169. }
  170. static ssize_t dw_dphy_g118_settle_store(struct device *dev,
  171. struct device_attribute *attr,
  172. const char *buf, size_t count)
  173. {
  174. struct platform_device *pdev = to_platform_device(dev);
  175. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  176. struct dw_dphy_rx *dphy = drvdata->dphy;
  177. unsigned long lp_time;
  178. int ret;
  179. ret = kstrtoul(buf, 10, &lp_time);
  180. if (ret < 0)
  181. return ret;
  182. if (lp_time > 1 && lp_time < 10000) {
  183. dphy->lp_time = lp_time;
  184. } else {
  185. dev_vdbg(dev, "Invalid Value configuring for 1000 ns\n");
  186. dphy->lp_time = 1000;
  187. }
  188. dphy->lp_time = lp_time;
  189. return count;
  190. }
  191. static ssize_t dw_dphy_g118_settle_show(struct device *dev,
  192. struct device_attribute *attr,
  193. char *buf)
  194. {
  195. struct platform_device *pdev = to_platform_device(dev);
  196. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  197. struct dw_dphy_rx *dphy = drvdata->dphy;
  198. char buffer[10];
  199. snprintf(buffer, sizeof(buffer), "Settle %d ns\n", dphy->lp_time);
  200. return strlcpy(buf, buffer, PAGE_SIZE);
  201. }
  202. static DEVICE_ATTR_RO(dphy_reset);
  203. static DEVICE_ATTR_RW(dphy_freq);
  204. static DEVICE_ATTR_RW(dphy_addr);
  205. #if IS_ENABLED(CONFIG_DWC_MIPI_TC_DPHY_GEN3)
  206. //static DEVICE_ATTR_RW(idelay);
  207. #endif
  208. static DEVICE_ATTR_RW(len_config);
  209. static DEVICE_ATTR_RW(dw_dphy_g118_settle);
  210. int dw_dphy_create_capabilities_sysfs(struct platform_device *pdev)
  211. {
  212. device_create_file(&pdev->dev, &dev_attr_dphy_reset);
  213. device_create_file(&pdev->dev, &dev_attr_dphy_freq);
  214. device_create_file(&pdev->dev, &dev_attr_dphy_addr);
  215. #if IS_ENABLED(CONFIG_DWC_MIPI_TC_DPHY_GEN3)
  216. // device_create_file(&pdev->dev, &dev_attr_idelay);
  217. #endif
  218. device_create_file(&pdev->dev, &dev_attr_len_config);
  219. device_create_file(&pdev->dev, &dev_attr_dw_dphy_g118_settle);
  220. return 0;
  221. }
  222. int dw_dphy_remove_capabilities_sysfs(struct platform_device *pdev)
  223. {
  224. device_remove_file(&pdev->dev, &dev_attr_dphy_reset);
  225. device_remove_file(&pdev->dev, &dev_attr_dphy_freq);
  226. device_remove_file(&pdev->dev, &dev_attr_dphy_addr);
  227. #if IS_ENABLED(CONFIG_DWC_MIPI_TC_DPHY_GEN3)
  228. // device_create_file(&pdev->dev, &dev_attr_idelay);
  229. #endif
  230. device_remove_file(&pdev->dev, &dev_attr_len_config);
  231. device_remove_file(&pdev->dev, &dev_attr_dw_dphy_g118_settle);
  232. return 0;
  233. }