mipi_disco.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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", &prop->revision);
  35. /* Find master handle */
  36. snprintf(name, sizeof(name),
  37. "mipi-sdw-master-%d-subproperties", bus->link_id);
  38. link = device_get_named_child_node(bus->dev, name);
  39. if (!link) {
  40. dev_err(bus->dev, "Master node %s not found\n", name);
  41. return -EIO;
  42. }
  43. if (fwnode_property_read_bool(link,
  44. "mipi-sdw-clock-stop-mode0-supported") == true)
  45. prop->clk_stop_mode = SDW_CLK_STOP_MODE0;
  46. if (fwnode_property_read_bool(link,
  47. "mipi-sdw-clock-stop-mode1-supported") == true)
  48. prop->clk_stop_mode |= SDW_CLK_STOP_MODE1;
  49. fwnode_property_read_u32(link,
  50. "mipi-sdw-max-clock-frequency", &prop->max_freq);
  51. nval = fwnode_property_read_u32_array(link,
  52. "mipi-sdw-clock-frequencies-supported", NULL, 0);
  53. if (nval > 0) {
  54. prop->num_freq = nval;
  55. prop->freq = devm_kcalloc(bus->dev, prop->num_freq,
  56. sizeof(*prop->freq), GFP_KERNEL);
  57. if (!prop->freq)
  58. return -ENOMEM;
  59. fwnode_property_read_u32_array(link,
  60. "mipi-sdw-clock-frequencies-supported",
  61. prop->freq, prop->num_freq);
  62. }
  63. /*
  64. * Check the frequencies supported. If FW doesn't provide max
  65. * freq, then populate here by checking values.
  66. */
  67. if (!prop->max_freq && prop->freq) {
  68. prop->max_freq = prop->freq[0];
  69. for (i = 1; i < prop->num_freq; i++) {
  70. if (prop->freq[i] > prop->max_freq)
  71. prop->max_freq = prop->freq[i];
  72. }
  73. }
  74. nval = fwnode_property_read_u32_array(link,
  75. "mipi-sdw-supported-clock-gears", NULL, 0);
  76. if (nval > 0) {
  77. prop->num_clk_gears = nval;
  78. prop->clk_gears = devm_kcalloc(bus->dev, prop->num_clk_gears,
  79. sizeof(*prop->clk_gears), GFP_KERNEL);
  80. if (!prop->clk_gears)
  81. return -ENOMEM;
  82. fwnode_property_read_u32_array(link,
  83. "mipi-sdw-supported-clock-gears",
  84. prop->clk_gears, prop->num_clk_gears);
  85. }
  86. fwnode_property_read_u32(link, "mipi-sdw-default-frame-rate",
  87. &prop->default_frame_rate);
  88. fwnode_property_read_u32(link, "mipi-sdw-default-frame-row-size",
  89. &prop->default_row);
  90. fwnode_property_read_u32(link, "mipi-sdw-default-frame-col-size",
  91. &prop->default_col);
  92. prop->dynamic_frame = fwnode_property_read_bool(link,
  93. "mipi-sdw-dynamic-frame-shape");
  94. fwnode_property_read_u32(link, "mipi-sdw-command-error-threshold",
  95. &prop->err_threshold);
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(sdw_master_read_prop);
  99. static int sdw_slave_read_dp0(struct sdw_slave *slave,
  100. struct fwnode_handle *port, struct sdw_dp0_prop *dp0)
  101. {
  102. int nval;
  103. fwnode_property_read_u32(port, "mipi-sdw-port-max-wordlength",
  104. &dp0->max_word);
  105. fwnode_property_read_u32(port, "mipi-sdw-port-min-wordlength",
  106. &dp0->min_word);
  107. nval = fwnode_property_read_u32_array(port,
  108. "mipi-sdw-port-wordlength-configs", NULL, 0);
  109. if (nval > 0) {
  110. dp0->num_words = nval;
  111. dp0->words = devm_kcalloc(&slave->dev,
  112. dp0->num_words, sizeof(*dp0->words),
  113. GFP_KERNEL);
  114. if (!dp0->words)
  115. return -ENOMEM;
  116. fwnode_property_read_u32_array(port,
  117. "mipi-sdw-port-wordlength-configs",
  118. dp0->words, dp0->num_words);
  119. }
  120. dp0->flow_controlled = fwnode_property_read_bool(
  121. port, "mipi-sdw-bra-flow-controlled");
  122. dp0->simple_ch_prep_sm = fwnode_property_read_bool(
  123. port, "mipi-sdw-simplified-channel-prepare-sm");
  124. dp0->device_interrupts = fwnode_property_read_bool(
  125. port, "mipi-sdw-imp-def-dp0-interrupts-supported");
  126. return 0;
  127. }
  128. static int sdw_slave_read_dpn(struct sdw_slave *slave,
  129. struct sdw_dpn_prop *dpn, int count, int ports, char *type)
  130. {
  131. struct fwnode_handle *node;
  132. u32 bit, i = 0;
  133. int nval;
  134. unsigned long addr;
  135. char name[40];
  136. addr = ports;
  137. /* valid ports are 1 to 14 so apply mask */
  138. addr &= GENMASK(14, 1);
  139. for_each_set_bit(bit, &addr, 32) {
  140. snprintf(name, sizeof(name),
  141. "mipi-sdw-dp-%d-%s-subproperties", bit, type);
  142. dpn[i].num = bit;
  143. node = device_get_named_child_node(&slave->dev, name);
  144. if (!node) {
  145. dev_err(&slave->dev, "%s dpN not found\n", name);
  146. return -EIO;
  147. }
  148. fwnode_property_read_u32(node, "mipi-sdw-port-max-wordlength",
  149. &dpn[i].max_word);
  150. fwnode_property_read_u32(node, "mipi-sdw-port-min-wordlength",
  151. &dpn[i].min_word);
  152. nval = fwnode_property_read_u32_array(node,
  153. "mipi-sdw-port-wordlength-configs", NULL, 0);
  154. if (nval > 0) {
  155. dpn[i].num_words = nval;
  156. dpn[i].words = devm_kcalloc(&slave->dev,
  157. dpn[i].num_words,
  158. sizeof(*dpn[i].words), GFP_KERNEL);
  159. if (!dpn[i].words)
  160. return -ENOMEM;
  161. fwnode_property_read_u32_array(node,
  162. "mipi-sdw-port-wordlength-configs",
  163. dpn[i].words, dpn[i].num_words);
  164. }
  165. fwnode_property_read_u32(node, "mipi-sdw-data-port-type",
  166. &dpn[i].type);
  167. fwnode_property_read_u32(node,
  168. "mipi-sdw-max-grouping-supported",
  169. &dpn[i].max_grouping);
  170. dpn[i].simple_ch_prep_sm = fwnode_property_read_bool(node,
  171. "mipi-sdw-simplified-channelprepare-sm");
  172. fwnode_property_read_u32(node,
  173. "mipi-sdw-port-channelprepare-timeout",
  174. &dpn[i].ch_prep_timeout);
  175. fwnode_property_read_u32(node,
  176. "mipi-sdw-imp-def-dpn-interrupts-supported",
  177. &dpn[i].device_interrupts);
  178. fwnode_property_read_u32(node, "mipi-sdw-min-channel-number",
  179. &dpn[i].min_ch);
  180. fwnode_property_read_u32(node, "mipi-sdw-max-channel-number",
  181. &dpn[i].max_ch);
  182. nval = fwnode_property_read_u32_array(node,
  183. "mipi-sdw-channel-number-list", NULL, 0);
  184. if (nval > 0) {
  185. dpn[i].num_ch = nval;
  186. dpn[i].ch = devm_kcalloc(&slave->dev, dpn[i].num_ch,
  187. sizeof(*dpn[i].ch), GFP_KERNEL);
  188. if (!dpn[i].ch)
  189. return -ENOMEM;
  190. fwnode_property_read_u32_array(node,
  191. "mipi-sdw-channel-number-list",
  192. dpn[i].ch, dpn[i].num_ch);
  193. }
  194. nval = fwnode_property_read_u32_array(node,
  195. "mipi-sdw-channel-combination-list", NULL, 0);
  196. if (nval > 0) {
  197. dpn[i].num_ch_combinations = nval;
  198. dpn[i].ch_combinations = devm_kcalloc(&slave->dev,
  199. dpn[i].num_ch_combinations,
  200. sizeof(*dpn[i].ch_combinations),
  201. GFP_KERNEL);
  202. if (!dpn[i].ch_combinations)
  203. return -ENOMEM;
  204. fwnode_property_read_u32_array(node,
  205. "mipi-sdw-channel-combination-list",
  206. dpn[i].ch_combinations,
  207. dpn[i].num_ch_combinations);
  208. }
  209. fwnode_property_read_u32(node,
  210. "mipi-sdw-modes-supported", &dpn[i].modes);
  211. fwnode_property_read_u32(node, "mipi-sdw-max-async-buffer",
  212. &dpn[i].max_async_buffer);
  213. dpn[i].block_pack_mode = fwnode_property_read_bool(node,
  214. "mipi-sdw-block-packing-mode");
  215. fwnode_property_read_u32(node, "mipi-sdw-port-encoding-type",
  216. &dpn[i].port_encoding);
  217. /* TODO: Read audio mode */
  218. i++;
  219. }
  220. return 0;
  221. }
  222. /**
  223. * sdw_slave_read_prop() - Read Slave properties
  224. * @slave: SDW Slave
  225. */
  226. int sdw_slave_read_prop(struct sdw_slave *slave)
  227. {
  228. struct sdw_slave_prop *prop = &slave->prop;
  229. struct device *dev = &slave->dev;
  230. struct fwnode_handle *port;
  231. int num_of_ports, nval, i, dp0 = 0;
  232. device_property_read_u32(dev, "mipi-sdw-sw-interface-revision",
  233. &prop->mipi_revision);
  234. prop->wake_capable = device_property_read_bool(dev,
  235. "mipi-sdw-wake-up-unavailable");
  236. prop->wake_capable = !prop->wake_capable;
  237. prop->test_mode_capable = device_property_read_bool(dev,
  238. "mipi-sdw-test-mode-supported");
  239. prop->clk_stop_mode1 = false;
  240. if (device_property_read_bool(dev,
  241. "mipi-sdw-clock-stop-mode1-supported"))
  242. prop->clk_stop_mode1 = true;
  243. prop->simple_clk_stop_capable = device_property_read_bool(dev,
  244. "mipi-sdw-simplified-clockstopprepare-sm-supported");
  245. device_property_read_u32(dev, "mipi-sdw-clockstopprepare-timeout",
  246. &prop->clk_stop_timeout);
  247. device_property_read_u32(dev, "mipi-sdw-slave-channelprepare-timeout",
  248. &prop->ch_prep_timeout);
  249. device_property_read_u32(dev,
  250. "mipi-sdw-clockstopprepare-hard-reset-behavior",
  251. &prop->reset_behave);
  252. prop->high_PHY_capable = device_property_read_bool(dev,
  253. "mipi-sdw-highPHY-capable");
  254. prop->paging_support = device_property_read_bool(dev,
  255. "mipi-sdw-paging-support");
  256. prop->bank_delay_support = device_property_read_bool(dev,
  257. "mipi-sdw-bank-delay-support");
  258. device_property_read_u32(dev,
  259. "mipi-sdw-port15-read-behavior", &prop->p15_behave);
  260. device_property_read_u32(dev, "mipi-sdw-master-count",
  261. &prop->master_count);
  262. device_property_read_u32(dev, "mipi-sdw-source-port-list",
  263. &prop->source_ports);
  264. device_property_read_u32(dev, "mipi-sdw-sink-port-list",
  265. &prop->sink_ports);
  266. /* Read dp0 properties */
  267. port = device_get_named_child_node(dev, "mipi-sdw-dp-0-subproperties");
  268. if (!port) {
  269. dev_dbg(dev, "DP0 node not found!!\n");
  270. } else {
  271. prop->dp0_prop = devm_kzalloc(&slave->dev,
  272. sizeof(*prop->dp0_prop), GFP_KERNEL);
  273. if (!prop->dp0_prop)
  274. return -ENOMEM;
  275. sdw_slave_read_dp0(slave, port, prop->dp0_prop);
  276. dp0 = 1;
  277. }
  278. /*
  279. * Based on each DPn port, get source and sink dpn properties.
  280. * Also, some ports can operate as both source or sink.
  281. */
  282. /* Allocate memory for set bits in port lists */
  283. nval = hweight32(prop->source_ports);
  284. prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
  285. sizeof(*prop->src_dpn_prop), GFP_KERNEL);
  286. if (!prop->src_dpn_prop)
  287. return -ENOMEM;
  288. /* Read dpn properties for source port(s) */
  289. sdw_slave_read_dpn(slave, prop->src_dpn_prop, nval,
  290. prop->source_ports, "source");
  291. nval = hweight32(prop->sink_ports);
  292. prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
  293. sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
  294. if (!prop->sink_dpn_prop)
  295. return -ENOMEM;
  296. /* Read dpn properties for sink port(s) */
  297. sdw_slave_read_dpn(slave, prop->sink_dpn_prop, nval,
  298. prop->sink_ports, "sink");
  299. /* some ports are bidirectional so check total ports by ORing */
  300. nval = prop->source_ports | prop->sink_ports;
  301. num_of_ports = hweight32(nval) + dp0; /* add DP0 */
  302. /* Allocate port_ready based on num_of_ports */
  303. slave->port_ready = devm_kcalloc(&slave->dev, num_of_ports,
  304. sizeof(*slave->port_ready), GFP_KERNEL);
  305. if (!slave->port_ready)
  306. return -ENOMEM;
  307. /* Initialize completion */
  308. for (i = 0; i < num_of_ports; i++)
  309. init_completion(&slave->port_ready[i]);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL(sdw_slave_read_prop);