1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
5 * MIPI Discovery And Configuration (DisCo) Specification for SoundWire
6 * specifies properties to be implemented for SoundWire Masters and Slaves.
7 * The DisCo spec doesn't mandate these properties. However, SDW bus cannot
8 * work without knowing these values.
10 * The helper functions read the Master and Slave properties. Implementers
11 * of Master or Slave drivers can use any of the below three mechanisms:
12 * a) Use these APIs here as .read_prop() callback for Master and Slave
13 * b) Implement own methods and set those as .read_prop(), but invoke
14 * APIs in this file for generic read and override the values with
15 * platform specific data
16 * c) Implement ones own methods which do not use anything provided
20 #include <linux/device.h>
21 #include <linux/property.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/soundwire/sdw.h>
27 * sdw_master_read_prop() - Read Master properties
28 * @bus: SDW bus instance
30 int sdw_master_read_prop(struct sdw_bus *bus)
32 struct sdw_master_prop *prop = &bus->prop;
33 struct fwnode_handle *link;
37 device_property_read_u32(bus->dev,
38 "mipi-sdw-sw-interface-revision",
41 /* Find master handle */
42 snprintf(name, sizeof(name),
43 "mipi-sdw-link-%d-subproperties", bus->link_id);
45 link = device_get_named_child_node(bus->dev, name);
47 dev_err(bus->dev, "Master node %s not found\n", name);
51 if (fwnode_property_read_bool(link,
52 "mipi-sdw-clock-stop-mode0-supported"))
53 prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE0);
55 if (fwnode_property_read_bool(link,
56 "mipi-sdw-clock-stop-mode1-supported"))
57 prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE1);
59 fwnode_property_read_u32(link,
60 "mipi-sdw-max-clock-frequency",
63 nval = fwnode_property_read_u32_array(link,
64 "mipi-sdw-clock-frequencies-supported", NULL, 0);
66 prop->num_clk_freq = nval;
67 prop->clk_freq = devm_kcalloc(bus->dev, prop->num_clk_freq,
68 sizeof(*prop->clk_freq),
73 fwnode_property_read_u32_array(link,
74 "mipi-sdw-clock-frequencies-supported",
75 prop->clk_freq, prop->num_clk_freq);
79 * Check the frequencies supported. If FW doesn't provide max
80 * freq, then populate here by checking values.
82 if (!prop->max_clk_freq && prop->clk_freq) {
83 prop->max_clk_freq = prop->clk_freq[0];
84 for (i = 1; i < prop->num_clk_freq; i++) {
85 if (prop->clk_freq[i] > prop->max_clk_freq)
86 prop->max_clk_freq = prop->clk_freq[i];
90 nval = fwnode_property_read_u32_array(link,
91 "mipi-sdw-supported-clock-gears", NULL, 0);
93 prop->num_clk_gears = nval;
94 prop->clk_gears = devm_kcalloc(bus->dev, prop->num_clk_gears,
95 sizeof(*prop->clk_gears),
100 fwnode_property_read_u32_array(link,
101 "mipi-sdw-supported-clock-gears",
103 prop->num_clk_gears);
106 fwnode_property_read_u32(link, "mipi-sdw-default-frame-rate",
107 &prop->default_frame_rate);
109 fwnode_property_read_u32(link, "mipi-sdw-default-frame-row-size",
112 fwnode_property_read_u32(link, "mipi-sdw-default-frame-col-size",
115 prop->dynamic_frame = fwnode_property_read_bool(link,
116 "mipi-sdw-dynamic-frame-shape");
118 fwnode_property_read_u32(link, "mipi-sdw-command-error-threshold",
119 &prop->err_threshold);
123 EXPORT_SYMBOL(sdw_master_read_prop);
125 static int sdw_slave_read_dp0(struct sdw_slave *slave,
126 struct fwnode_handle *port,
127 struct sdw_dp0_prop *dp0)
131 fwnode_property_read_u32(port, "mipi-sdw-port-max-wordlength",
134 fwnode_property_read_u32(port, "mipi-sdw-port-min-wordlength",
137 nval = fwnode_property_read_u32_array(port,
138 "mipi-sdw-port-wordlength-configs", NULL, 0);
141 dp0->num_words = nval;
142 dp0->words = devm_kcalloc(&slave->dev,
143 dp0->num_words, sizeof(*dp0->words),
148 fwnode_property_read_u32_array(port,
149 "mipi-sdw-port-wordlength-configs",
150 dp0->words, dp0->num_words);
153 dp0->BRA_flow_controlled = fwnode_property_read_bool(port,
154 "mipi-sdw-bra-flow-controlled");
156 dp0->simple_ch_prep_sm = fwnode_property_read_bool(port,
157 "mipi-sdw-simplified-channel-prepare-sm");
159 dp0->imp_def_interrupts = fwnode_property_read_bool(port,
160 "mipi-sdw-imp-def-dp0-interrupts-supported");
165 static int sdw_slave_read_dpn(struct sdw_slave *slave,
166 struct sdw_dpn_prop *dpn, int count, int ports,
169 struct fwnode_handle *node;
176 /* valid ports are 1 to 14 so apply mask */
177 addr &= GENMASK(14, 1);
179 for_each_set_bit(bit, &addr, 32) {
180 snprintf(name, sizeof(name),
181 "mipi-sdw-dp-%d-%s-subproperties", bit, type);
185 node = device_get_named_child_node(&slave->dev, name);
187 dev_err(&slave->dev, "%s dpN not found\n", name);
191 fwnode_property_read_u32(node, "mipi-sdw-port-max-wordlength",
193 fwnode_property_read_u32(node, "mipi-sdw-port-min-wordlength",
196 nval = fwnode_property_read_u32_array(node,
197 "mipi-sdw-port-wordlength-configs", NULL, 0);
199 dpn[i].num_words = nval;
200 dpn[i].words = devm_kcalloc(&slave->dev,
202 sizeof(*dpn[i].words),
207 fwnode_property_read_u32_array(node,
208 "mipi-sdw-port-wordlength-configs",
209 dpn[i].words, dpn[i].num_words);
212 fwnode_property_read_u32(node, "mipi-sdw-data-port-type",
215 fwnode_property_read_u32(node,
216 "mipi-sdw-max-grouping-supported",
217 &dpn[i].max_grouping);
219 dpn[i].simple_ch_prep_sm = fwnode_property_read_bool(node,
220 "mipi-sdw-simplified-channelprepare-sm");
222 fwnode_property_read_u32(node,
223 "mipi-sdw-port-channelprepare-timeout",
224 &dpn[i].ch_prep_timeout);
226 fwnode_property_read_u32(node,
227 "mipi-sdw-imp-def-dpn-interrupts-supported",
228 &dpn[i].imp_def_interrupts);
230 fwnode_property_read_u32(node, "mipi-sdw-min-channel-number",
233 fwnode_property_read_u32(node, "mipi-sdw-max-channel-number",
236 nval = fwnode_property_read_u32_array(node,
237 "mipi-sdw-channel-number-list", NULL, 0);
239 dpn[i].num_ch = nval;
240 dpn[i].ch = devm_kcalloc(&slave->dev, dpn[i].num_ch,
246 fwnode_property_read_u32_array(node,
247 "mipi-sdw-channel-number-list",
248 dpn[i].ch, dpn[i].num_ch);
251 nval = fwnode_property_read_u32_array(node,
252 "mipi-sdw-channel-combination-list", NULL, 0);
254 dpn[i].num_ch_combinations = nval;
255 dpn[i].ch_combinations = devm_kcalloc(&slave->dev,
256 dpn[i].num_ch_combinations,
257 sizeof(*dpn[i].ch_combinations),
259 if (!dpn[i].ch_combinations)
262 fwnode_property_read_u32_array(node,
263 "mipi-sdw-channel-combination-list",
264 dpn[i].ch_combinations,
265 dpn[i].num_ch_combinations);
268 fwnode_property_read_u32(node,
269 "mipi-sdw-modes-supported", &dpn[i].modes);
271 fwnode_property_read_u32(node, "mipi-sdw-max-async-buffer",
272 &dpn[i].max_async_buffer);
274 dpn[i].block_pack_mode = fwnode_property_read_bool(node,
275 "mipi-sdw-block-packing-mode");
277 fwnode_property_read_u32(node, "mipi-sdw-port-encoding-type",
278 &dpn[i].port_encoding);
280 /* TODO: Read audio mode */
289 * sdw_slave_read_prop() - Read Slave properties
292 int sdw_slave_read_prop(struct sdw_slave *slave)
294 struct sdw_slave_prop *prop = &slave->prop;
295 struct device *dev = &slave->dev;
296 struct fwnode_handle *port;
297 int num_of_ports, nval, i, dp0 = 0;
299 device_property_read_u32(dev, "mipi-sdw-sw-interface-revision",
300 &prop->mipi_revision);
302 prop->wake_capable = device_property_read_bool(dev,
303 "mipi-sdw-wake-up-unavailable");
304 prop->wake_capable = !prop->wake_capable;
306 prop->test_mode_capable = device_property_read_bool(dev,
307 "mipi-sdw-test-mode-supported");
309 prop->clk_stop_mode1 = false;
310 if (device_property_read_bool(dev,
311 "mipi-sdw-clock-stop-mode1-supported"))
312 prop->clk_stop_mode1 = true;
314 prop->simple_clk_stop_capable = device_property_read_bool(dev,
315 "mipi-sdw-simplified-clockstopprepare-sm-supported");
317 device_property_read_u32(dev, "mipi-sdw-clockstopprepare-timeout",
318 &prop->clk_stop_timeout);
320 device_property_read_u32(dev, "mipi-sdw-slave-channelprepare-timeout",
321 &prop->ch_prep_timeout);
323 device_property_read_u32(dev,
324 "mipi-sdw-clockstopprepare-hard-reset-behavior",
325 &prop->reset_behave);
327 prop->high_PHY_capable = device_property_read_bool(dev,
328 "mipi-sdw-highPHY-capable");
330 prop->paging_support = device_property_read_bool(dev,
331 "mipi-sdw-paging-support");
333 prop->bank_delay_support = device_property_read_bool(dev,
334 "mipi-sdw-bank-delay-support");
336 device_property_read_u32(dev,
337 "mipi-sdw-port15-read-behavior", &prop->p15_behave);
339 device_property_read_u32(dev, "mipi-sdw-master-count",
340 &prop->master_count);
342 device_property_read_u32(dev, "mipi-sdw-source-port-list",
343 &prop->source_ports);
345 device_property_read_u32(dev, "mipi-sdw-sink-port-list",
348 /* Read dp0 properties */
349 port = device_get_named_child_node(dev, "mipi-sdw-dp-0-subproperties");
351 dev_dbg(dev, "DP0 node not found!!\n");
353 prop->dp0_prop = devm_kzalloc(&slave->dev,
354 sizeof(*prop->dp0_prop),
359 sdw_slave_read_dp0(slave, port, prop->dp0_prop);
364 * Based on each DPn port, get source and sink dpn properties.
365 * Also, some ports can operate as both source or sink.
368 /* Allocate memory for set bits in port lists */
369 nval = hweight32(prop->source_ports);
370 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
371 sizeof(*prop->src_dpn_prop),
373 if (!prop->src_dpn_prop)
376 /* Read dpn properties for source port(s) */
377 sdw_slave_read_dpn(slave, prop->src_dpn_prop, nval,
378 prop->source_ports, "source");
380 nval = hweight32(prop->sink_ports);
381 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
382 sizeof(*prop->sink_dpn_prop),
384 if (!prop->sink_dpn_prop)
387 /* Read dpn properties for sink port(s) */
388 sdw_slave_read_dpn(slave, prop->sink_dpn_prop, nval,
389 prop->sink_ports, "sink");
391 /* some ports are bidirectional so check total ports by ORing */
392 nval = prop->source_ports | prop->sink_ports;
393 num_of_ports = hweight32(nval) + dp0; /* add DP0 */
395 /* Allocate port_ready based on num_of_ports */
396 slave->port_ready = devm_kcalloc(&slave->dev, num_of_ports,
397 sizeof(*slave->port_ready),
399 if (!slave->port_ready)
402 /* Initialize completion */
403 for (i = 0; i < num_of_ports; i++)
404 init_completion(&slave->port_ready[i]);
408 EXPORT_SYMBOL(sdw_slave_read_prop);