mipi_disco.c 11 KB

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