mipi_disco.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-17 Intel Corporation.
  3. /*
  4. * MIPI Discovery And Configuration (DisCo) Specification for SoundWire
  5. * specifies properties to be implemented for SoundWire Masters and Slaves.
  6. * The DisCo spec doesn't mandate these properties. However, SDW bus cannot
  7. * work without knowing these values.
  8. *
  9. * The helper functions read the Master and Slave properties. Implementers
  10. * of Master or Slave drivers can use any of the below three mechanisms:
  11. * a) Use these APIs here as .read_prop() callback for Master and Slave
  12. * b) Implement own methods and set those as .read_prop(), but invoke
  13. * APIs in this file for generic read and override the values with
  14. * platform specific data
  15. * c) Implement ones own methods which do not use anything provided
  16. * here
  17. */
  18. #include <linux/device.h>
  19. #include <linux/property.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/soundwire/sdw.h>
  22. #include "bus.h"
  23. /**
  24. * sdw_master_read_prop() - Read Master properties
  25. * @bus: SDW bus instance
  26. */
  27. int sdw_master_read_prop(struct sdw_bus *bus)
  28. {
  29. struct sdw_master_prop *prop = &bus->prop;
  30. struct fwnode_handle *link;
  31. char name[32];
  32. int nval, i;
  33. device_property_read_u32(bus->dev,
  34. "mipi-sdw-sw-interface-revision",
  35. &prop->revision);
  36. /* Find master handle */
  37. snprintf(name, sizeof(name),
  38. "mipi-sdw-link-%d-subproperties", bus->link_id);
  39. link = device_get_named_child_node(bus->dev, name);
  40. if (!link) {
  41. dev_err(bus->dev, "Master node %s not found\n", name);
  42. return -EIO;
  43. }
  44. if (fwnode_property_read_bool(link,
  45. "mipi-sdw-clock-stop-mode0-supported"))
  46. prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE0);
  47. if (fwnode_property_read_bool(link,
  48. "mipi-sdw-clock-stop-mode1-supported"))
  49. prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE1);
  50. fwnode_property_read_u32(link,
  51. "mipi-sdw-max-clock-frequency",
  52. &prop->max_clk_freq);
  53. nval = fwnode_property_count_u32(link, "mipi-sdw-clock-frequencies-supported");
  54. if (nval > 0) {
  55. prop->num_clk_freq = nval;
  56. prop->clk_freq = devm_kcalloc(bus->dev, prop->num_clk_freq,
  57. sizeof(*prop->clk_freq),
  58. GFP_KERNEL);
  59. if (!prop->clk_freq)
  60. return -ENOMEM;
  61. fwnode_property_read_u32_array(link,
  62. "mipi-sdw-clock-frequencies-supported",
  63. prop->clk_freq, prop->num_clk_freq);
  64. }
  65. /*
  66. * Check the frequencies supported. If FW doesn't provide max
  67. * freq, then populate here by checking values.
  68. */
  69. if (!prop->max_clk_freq && prop->clk_freq) {
  70. prop->max_clk_freq = prop->clk_freq[0];
  71. for (i = 1; i < prop->num_clk_freq; i++) {
  72. if (prop->clk_freq[i] > prop->max_clk_freq)
  73. prop->max_clk_freq = prop->clk_freq[i];
  74. }
  75. }
  76. nval = fwnode_property_count_u32(link, "mipi-sdw-supported-clock-gears");
  77. if (nval > 0) {
  78. prop->num_clk_gears = nval;
  79. prop->clk_gears = devm_kcalloc(bus->dev, prop->num_clk_gears,
  80. sizeof(*prop->clk_gears),
  81. GFP_KERNEL);
  82. if (!prop->clk_gears)
  83. return -ENOMEM;
  84. fwnode_property_read_u32_array(link,
  85. "mipi-sdw-supported-clock-gears",
  86. prop->clk_gears,
  87. prop->num_clk_gears);
  88. }
  89. fwnode_property_read_u32(link, "mipi-sdw-default-frame-rate",
  90. &prop->default_frame_rate);
  91. fwnode_property_read_u32(link, "mipi-sdw-default-frame-row-size",
  92. &prop->default_row);
  93. fwnode_property_read_u32(link, "mipi-sdw-default-frame-col-size",
  94. &prop->default_col);
  95. prop->dynamic_frame = fwnode_property_read_bool(link,
  96. "mipi-sdw-dynamic-frame-shape");
  97. fwnode_property_read_u32(link, "mipi-sdw-command-error-threshold",
  98. &prop->err_threshold);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(sdw_master_read_prop);
  102. static int sdw_slave_read_dp0(struct sdw_slave *slave,
  103. struct fwnode_handle *port,
  104. struct sdw_dp0_prop *dp0)
  105. {
  106. int nval;
  107. fwnode_property_read_u32(port, "mipi-sdw-port-max-wordlength",
  108. &dp0->max_word);
  109. fwnode_property_read_u32(port, "mipi-sdw-port-min-wordlength",
  110. &dp0->min_word);
  111. nval = fwnode_property_count_u32(port, "mipi-sdw-port-wordlength-configs");
  112. if (nval > 0) {
  113. dp0->num_words = nval;
  114. dp0->words = devm_kcalloc(&slave->dev,
  115. dp0->num_words, sizeof(*dp0->words),
  116. GFP_KERNEL);
  117. if (!dp0->words)
  118. return -ENOMEM;
  119. fwnode_property_read_u32_array(port,
  120. "mipi-sdw-port-wordlength-configs",
  121. dp0->words, dp0->num_words);
  122. }
  123. dp0->BRA_flow_controlled = fwnode_property_read_bool(port,
  124. "mipi-sdw-bra-flow-controlled");
  125. dp0->simple_ch_prep_sm = fwnode_property_read_bool(port,
  126. "mipi-sdw-simplified-channel-prepare-sm");
  127. dp0->imp_def_interrupts = fwnode_property_read_bool(port,
  128. "mipi-sdw-imp-def-dp0-interrupts-supported");
  129. return 0;
  130. }
  131. static int sdw_slave_read_dpn(struct sdw_slave *slave,
  132. struct sdw_dpn_prop *dpn, int count, int ports,
  133. char *type)
  134. {
  135. struct fwnode_handle *node;
  136. u32 bit, i = 0;
  137. int nval;
  138. unsigned long addr;
  139. char name[40];
  140. addr = ports;
  141. /* valid ports are 1 to 14 so apply mask */
  142. addr &= GENMASK(14, 1);
  143. for_each_set_bit(bit, &addr, 32) {
  144. snprintf(name, sizeof(name),
  145. "mipi-sdw-dp-%d-%s-subproperties", bit, type);
  146. dpn[i].num = bit;
  147. node = device_get_named_child_node(&slave->dev, name);
  148. if (!node) {
  149. dev_err(&slave->dev, "%s dpN not found\n", name);
  150. return -EIO;
  151. }
  152. fwnode_property_read_u32(node, "mipi-sdw-port-max-wordlength",
  153. &dpn[i].max_word);
  154. fwnode_property_read_u32(node, "mipi-sdw-port-min-wordlength",
  155. &dpn[i].min_word);
  156. nval = fwnode_property_count_u32(node, "mipi-sdw-port-wordlength-configs");
  157. if (nval > 0) {
  158. dpn[i].num_words = nval;
  159. dpn[i].words = devm_kcalloc(&slave->dev,
  160. dpn[i].num_words,
  161. sizeof(*dpn[i].words),
  162. GFP_KERNEL);
  163. if (!dpn[i].words)
  164. return -ENOMEM;
  165. fwnode_property_read_u32_array(node,
  166. "mipi-sdw-port-wordlength-configs",
  167. dpn[i].words, dpn[i].num_words);
  168. }
  169. fwnode_property_read_u32(node, "mipi-sdw-data-port-type",
  170. &dpn[i].type);
  171. fwnode_property_read_u32(node,
  172. "mipi-sdw-max-grouping-supported",
  173. &dpn[i].max_grouping);
  174. dpn[i].simple_ch_prep_sm = fwnode_property_read_bool(node,
  175. "mipi-sdw-simplified-channelprepare-sm");
  176. fwnode_property_read_u32(node,
  177. "mipi-sdw-port-channelprepare-timeout",
  178. &dpn[i].ch_prep_timeout);
  179. fwnode_property_read_u32(node,
  180. "mipi-sdw-imp-def-dpn-interrupts-supported",
  181. &dpn[i].imp_def_interrupts);
  182. fwnode_property_read_u32(node, "mipi-sdw-min-channel-number",
  183. &dpn[i].min_ch);
  184. fwnode_property_read_u32(node, "mipi-sdw-max-channel-number",
  185. &dpn[i].max_ch);
  186. nval = fwnode_property_count_u32(node, "mipi-sdw-channel-number-list");
  187. if (nval > 0) {
  188. dpn[i].num_channels = nval;
  189. dpn[i].channels = devm_kcalloc(&slave->dev,
  190. dpn[i].num_channels,
  191. sizeof(*dpn[i].channels),
  192. GFP_KERNEL);
  193. if (!dpn[i].channels)
  194. return -ENOMEM;
  195. fwnode_property_read_u32_array(node,
  196. "mipi-sdw-channel-number-list",
  197. dpn[i].channels, dpn[i].num_channels);
  198. }
  199. nval = fwnode_property_count_u32(node, "mipi-sdw-channel-combination-list");
  200. if (nval > 0) {
  201. dpn[i].num_ch_combinations = nval;
  202. dpn[i].ch_combinations = devm_kcalloc(&slave->dev,
  203. dpn[i].num_ch_combinations,
  204. sizeof(*dpn[i].ch_combinations),
  205. GFP_KERNEL);
  206. if (!dpn[i].ch_combinations)
  207. return -ENOMEM;
  208. fwnode_property_read_u32_array(node,
  209. "mipi-sdw-channel-combination-list",
  210. dpn[i].ch_combinations,
  211. dpn[i].num_ch_combinations);
  212. }
  213. fwnode_property_read_u32(node,
  214. "mipi-sdw-modes-supported", &dpn[i].modes);
  215. fwnode_property_read_u32(node, "mipi-sdw-max-async-buffer",
  216. &dpn[i].max_async_buffer);
  217. dpn[i].block_pack_mode = fwnode_property_read_bool(node,
  218. "mipi-sdw-block-packing-mode");
  219. fwnode_property_read_u32(node, "mipi-sdw-port-encoding-type",
  220. &dpn[i].port_encoding);
  221. /* TODO: Read audio mode */
  222. i++;
  223. }
  224. return 0;
  225. }
  226. /**
  227. * sdw_slave_read_prop() - Read Slave properties
  228. * @slave: SDW Slave
  229. */
  230. int sdw_slave_read_prop(struct sdw_slave *slave)
  231. {
  232. struct sdw_slave_prop *prop = &slave->prop;
  233. struct device *dev = &slave->dev;
  234. struct fwnode_handle *port;
  235. int nval;
  236. device_property_read_u32(dev, "mipi-sdw-sw-interface-revision",
  237. &prop->mipi_revision);
  238. prop->wake_capable = device_property_read_bool(dev,
  239. "mipi-sdw-wake-up-unavailable");
  240. prop->wake_capable = !prop->wake_capable;
  241. prop->test_mode_capable = device_property_read_bool(dev,
  242. "mipi-sdw-test-mode-supported");
  243. prop->clk_stop_mode1 = false;
  244. if (device_property_read_bool(dev,
  245. "mipi-sdw-clock-stop-mode1-supported"))
  246. prop->clk_stop_mode1 = true;
  247. prop->simple_clk_stop_capable = device_property_read_bool(dev,
  248. "mipi-sdw-simplified-clockstopprepare-sm-supported");
  249. device_property_read_u32(dev, "mipi-sdw-clockstopprepare-timeout",
  250. &prop->clk_stop_timeout);
  251. device_property_read_u32(dev, "mipi-sdw-slave-channelprepare-timeout",
  252. &prop->ch_prep_timeout);
  253. device_property_read_u32(dev,
  254. "mipi-sdw-clockstopprepare-hard-reset-behavior",
  255. &prop->reset_behave);
  256. prop->high_PHY_capable = device_property_read_bool(dev,
  257. "mipi-sdw-highPHY-capable");
  258. prop->paging_support = device_property_read_bool(dev,
  259. "mipi-sdw-paging-support");
  260. prop->bank_delay_support = device_property_read_bool(dev,
  261. "mipi-sdw-bank-delay-support");
  262. device_property_read_u32(dev,
  263. "mipi-sdw-port15-read-behavior", &prop->p15_behave);
  264. device_property_read_u32(dev, "mipi-sdw-master-count",
  265. &prop->master_count);
  266. device_property_read_u32(dev, "mipi-sdw-source-port-list",
  267. &prop->source_ports);
  268. device_property_read_u32(dev, "mipi-sdw-sink-port-list",
  269. &prop->sink_ports);
  270. /* Read dp0 properties */
  271. port = device_get_named_child_node(dev, "mipi-sdw-dp-0-subproperties");
  272. if (!port) {
  273. dev_dbg(dev, "DP0 node not found!!\n");
  274. } else {
  275. prop->dp0_prop = devm_kzalloc(&slave->dev,
  276. sizeof(*prop->dp0_prop),
  277. GFP_KERNEL);
  278. if (!prop->dp0_prop)
  279. return -ENOMEM;
  280. sdw_slave_read_dp0(slave, port, prop->dp0_prop);
  281. }
  282. /*
  283. * Based on each DPn port, get source and sink dpn properties.
  284. * Also, some ports can operate as both source or sink.
  285. */
  286. /* Allocate memory for set bits in port lists */
  287. nval = hweight32(prop->source_ports);
  288. prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
  289. sizeof(*prop->src_dpn_prop),
  290. GFP_KERNEL);
  291. if (!prop->src_dpn_prop)
  292. return -ENOMEM;
  293. /* Read dpn properties for source port(s) */
  294. sdw_slave_read_dpn(slave, prop->src_dpn_prop, nval,
  295. prop->source_ports, "source");
  296. nval = hweight32(prop->sink_ports);
  297. prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
  298. sizeof(*prop->sink_dpn_prop),
  299. GFP_KERNEL);
  300. if (!prop->sink_dpn_prop)
  301. return -ENOMEM;
  302. /* Read dpn properties for sink port(s) */
  303. sdw_slave_read_dpn(slave, prop->sink_dpn_prop, nval,
  304. prop->sink_ports, "sink");
  305. return 0;
  306. }
  307. EXPORT_SYMBOL(sdw_slave_read_prop);