sysfs_slave.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright(c) 2015-2020 Intel Corporation.
  3. #include <linux/device.h>
  4. #include <linux/mod_devicetable.h>
  5. #include <linux/slab.h>
  6. #include <linux/sysfs.h>
  7. #include <linux/soundwire/sdw.h>
  8. #include <linux/soundwire/sdw_type.h>
  9. #include "bus.h"
  10. #include "sysfs_local.h"
  11. /*
  12. * Slave sysfs
  13. */
  14. /*
  15. * The sysfs for Slave reflects the MIPI description as given
  16. * in the MIPI DisCo spec.
  17. * status and device_number come directly from the MIPI SoundWire
  18. * 1.x specification.
  19. *
  20. * Base file is device
  21. * |---- status
  22. * |---- device_number
  23. * |---- modalias
  24. * |---- dev-properties
  25. * |---- mipi_revision
  26. * |---- wake_capable
  27. * |---- test_mode_capable
  28. * |---- clk_stop_mode1
  29. * |---- simple_clk_stop_capable
  30. * |---- clk_stop_timeout
  31. * |---- ch_prep_timeout
  32. * |---- reset_behave
  33. * |---- high_PHY_capable
  34. * |---- paging_support
  35. * |---- bank_delay_support
  36. * |---- p15_behave
  37. * |---- master_count
  38. * |---- source_ports
  39. * |---- sink_ports
  40. * |---- dp0
  41. * |---- max_word
  42. * |---- min_word
  43. * |---- words
  44. * |---- BRA_flow_controlled
  45. * |---- simple_ch_prep_sm
  46. * |---- imp_def_interrupts
  47. * |---- dpN_<sink/src>
  48. * |---- max_word
  49. * |---- min_word
  50. * |---- words
  51. * |---- type
  52. * |---- max_grouping
  53. * |---- simple_ch_prep_sm
  54. * |---- ch_prep_timeout
  55. * |---- imp_def_interrupts
  56. * |---- min_ch
  57. * |---- max_ch
  58. * |---- channels
  59. * |---- ch_combinations
  60. * |---- max_async_buffer
  61. * |---- block_pack_mode
  62. * |---- port_encoding
  63. *
  64. */
  65. #define sdw_slave_attr(field, format_string) \
  66. static ssize_t field##_show(struct device *dev, \
  67. struct device_attribute *attr, \
  68. char *buf) \
  69. { \
  70. struct sdw_slave *slave = dev_to_sdw_dev(dev); \
  71. return sprintf(buf, format_string, slave->prop.field); \
  72. } \
  73. static DEVICE_ATTR_RO(field)
  74. sdw_slave_attr(mipi_revision, "0x%x\n");
  75. sdw_slave_attr(wake_capable, "%d\n");
  76. sdw_slave_attr(test_mode_capable, "%d\n");
  77. sdw_slave_attr(clk_stop_mode1, "%d\n");
  78. sdw_slave_attr(simple_clk_stop_capable, "%d\n");
  79. sdw_slave_attr(clk_stop_timeout, "%d\n");
  80. sdw_slave_attr(ch_prep_timeout, "%d\n");
  81. sdw_slave_attr(reset_behave, "%d\n");
  82. sdw_slave_attr(high_PHY_capable, "%d\n");
  83. sdw_slave_attr(paging_support, "%d\n");
  84. sdw_slave_attr(bank_delay_support, "%d\n");
  85. sdw_slave_attr(p15_behave, "%d\n");
  86. sdw_slave_attr(master_count, "%d\n");
  87. sdw_slave_attr(source_ports, "0x%x\n");
  88. sdw_slave_attr(sink_ports, "0x%x\n");
  89. static ssize_t modalias_show(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  93. return sdw_slave_modalias(slave, buf, 256);
  94. }
  95. static DEVICE_ATTR_RO(modalias);
  96. static struct attribute *slave_attrs[] = {
  97. &dev_attr_modalias.attr,
  98. NULL,
  99. };
  100. static const struct attribute_group slave_attr_group = {
  101. .attrs = slave_attrs,
  102. };
  103. static struct attribute *slave_dev_attrs[] = {
  104. &dev_attr_mipi_revision.attr,
  105. &dev_attr_wake_capable.attr,
  106. &dev_attr_test_mode_capable.attr,
  107. &dev_attr_clk_stop_mode1.attr,
  108. &dev_attr_simple_clk_stop_capable.attr,
  109. &dev_attr_clk_stop_timeout.attr,
  110. &dev_attr_ch_prep_timeout.attr,
  111. &dev_attr_reset_behave.attr,
  112. &dev_attr_high_PHY_capable.attr,
  113. &dev_attr_paging_support.attr,
  114. &dev_attr_bank_delay_support.attr,
  115. &dev_attr_p15_behave.attr,
  116. &dev_attr_master_count.attr,
  117. &dev_attr_source_ports.attr,
  118. &dev_attr_sink_ports.attr,
  119. NULL,
  120. };
  121. static const struct attribute_group sdw_slave_dev_attr_group = {
  122. .attrs = slave_dev_attrs,
  123. .name = "dev-properties",
  124. };
  125. /*
  126. * DP0 sysfs
  127. */
  128. #define sdw_dp0_attr(field, format_string) \
  129. static ssize_t field##_show(struct device *dev, \
  130. struct device_attribute *attr, \
  131. char *buf) \
  132. { \
  133. struct sdw_slave *slave = dev_to_sdw_dev(dev); \
  134. return sprintf(buf, format_string, slave->prop.dp0_prop->field);\
  135. } \
  136. static DEVICE_ATTR_RO(field)
  137. sdw_dp0_attr(max_word, "%d\n");
  138. sdw_dp0_attr(min_word, "%d\n");
  139. sdw_dp0_attr(BRA_flow_controlled, "%d\n");
  140. sdw_dp0_attr(simple_ch_prep_sm, "%d\n");
  141. sdw_dp0_attr(imp_def_interrupts, "0x%x\n");
  142. static ssize_t words_show(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  146. ssize_t size = 0;
  147. int i;
  148. for (i = 0; i < slave->prop.dp0_prop->num_words; i++)
  149. size += sprintf(buf + size, "%d ",
  150. slave->prop.dp0_prop->words[i]);
  151. size += sprintf(buf + size, "\n");
  152. return size;
  153. }
  154. static DEVICE_ATTR_RO(words);
  155. static struct attribute *dp0_attrs[] = {
  156. &dev_attr_max_word.attr,
  157. &dev_attr_min_word.attr,
  158. &dev_attr_words.attr,
  159. &dev_attr_BRA_flow_controlled.attr,
  160. &dev_attr_simple_ch_prep_sm.attr,
  161. &dev_attr_imp_def_interrupts.attr,
  162. NULL,
  163. };
  164. static umode_t dp0_attr_visible(struct kobject *kobj, struct attribute *attr,
  165. int n)
  166. {
  167. struct sdw_slave *slave = dev_to_sdw_dev(kobj_to_dev(kobj));
  168. if (slave->prop.dp0_prop)
  169. return attr->mode;
  170. return 0;
  171. }
  172. static bool dp0_group_visible(struct kobject *kobj)
  173. {
  174. struct sdw_slave *slave = dev_to_sdw_dev(kobj_to_dev(kobj));
  175. if (slave->prop.dp0_prop)
  176. return true;
  177. return false;
  178. }
  179. DEFINE_SYSFS_GROUP_VISIBLE(dp0);
  180. static const struct attribute_group dp0_group = {
  181. .attrs = dp0_attrs,
  182. .is_visible = SYSFS_GROUP_VISIBLE(dp0),
  183. .name = "dp0",
  184. };
  185. const struct attribute_group *sdw_attr_groups[] = {
  186. &slave_attr_group,
  187. &sdw_slave_dev_attr_group,
  188. &dp0_group,
  189. NULL,
  190. };
  191. /*
  192. * the status is shown in capital letters for UNATTACHED and RESERVED
  193. * on purpose, to highligh users to the fact that these status values
  194. * are not expected.
  195. */
  196. static const char *const slave_status[] = {
  197. [SDW_SLAVE_UNATTACHED] = "UNATTACHED",
  198. [SDW_SLAVE_ATTACHED] = "Attached",
  199. [SDW_SLAVE_ALERT] = "Alert",
  200. [SDW_SLAVE_RESERVED] = "RESERVED",
  201. };
  202. static ssize_t status_show(struct device *dev,
  203. struct device_attribute *attr, char *buf)
  204. {
  205. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  206. return sprintf(buf, "%s\n", slave_status[slave->status]);
  207. }
  208. static DEVICE_ATTR_RO(status);
  209. static ssize_t device_number_show(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  213. if (slave->status == SDW_SLAVE_UNATTACHED)
  214. return sprintf(buf, "%s", "N/A");
  215. else
  216. return sprintf(buf, "%d", slave->dev_num);
  217. }
  218. static DEVICE_ATTR_RO(device_number);
  219. static struct attribute *slave_status_attrs[] = {
  220. &dev_attr_status.attr,
  221. &dev_attr_device_number.attr,
  222. NULL,
  223. };
  224. /*
  225. * we don't use ATTRIBUTES_GROUP here since the group is used in a
  226. * separate file and can't be handled as a static.
  227. */
  228. static const struct attribute_group sdw_slave_status_attr_group = {
  229. .attrs = slave_status_attrs,
  230. };
  231. const struct attribute_group *sdw_slave_status_attr_groups[] = {
  232. &sdw_slave_status_attr_group,
  233. NULL
  234. };