1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
3
4 /*
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.
9 *
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
17 * here
18 */
19
20 #include <linux/device.h>
21 #include <linux/property.h>
22 #include <linux/soundwire/sdw.h>
23 #include "bus.h"
24
mipi_fwnode_property_read_bool(const struct fwnode_handle * fwnode,const char * propname)25 static bool mipi_fwnode_property_read_bool(const struct fwnode_handle *fwnode,
26 const char *propname)
27 {
28 int ret;
29 u8 val;
30
31 if (!fwnode_property_present(fwnode, propname))
32 return false;
33 ret = fwnode_property_read_u8_array(fwnode, propname, &val, 1);
34 if (ret < 0)
35 return false;
36 return !!val;
37 }
38
mipi_device_property_read_bool(const struct device * dev,const char * propname)39 static bool mipi_device_property_read_bool(const struct device *dev,
40 const char *propname)
41 {
42 return mipi_fwnode_property_read_bool(dev_fwnode(dev), propname);
43 }
44
45 /**
46 * sdw_master_read_prop() - Read Master properties
47 * @bus: SDW bus instance
48 */
sdw_master_read_prop(struct sdw_bus * bus)49 int sdw_master_read_prop(struct sdw_bus *bus)
50 {
51 struct sdw_master_prop *prop = &bus->prop;
52 struct fwnode_handle *link;
53 const char *scales_prop;
54 char name[32];
55 int nval;
56 int ret;
57 int i;
58
59 device_property_read_u32(bus->dev,
60 "mipi-sdw-sw-interface-revision",
61 &prop->revision);
62
63 /* Find master handle */
64 snprintf(name, sizeof(name),
65 "mipi-sdw-link-%d-subproperties", bus->link_id);
66
67 link = device_get_named_child_node(bus->dev, name);
68 if (!link) {
69 dev_err(bus->dev, "Master node %s not found\n", name);
70 return -EIO;
71 }
72
73 if (mipi_fwnode_property_read_bool(link,
74 "mipi-sdw-clock-stop-mode0-supported"))
75 prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE0);
76
77 if (mipi_fwnode_property_read_bool(link,
78 "mipi-sdw-clock-stop-mode1-supported"))
79 prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE1);
80
81 fwnode_property_read_u32(link,
82 "mipi-sdw-max-clock-frequency",
83 &prop->max_clk_freq);
84
85 nval = fwnode_property_count_u32(link, "mipi-sdw-clock-frequencies-supported");
86 if (nval > 0) {
87 prop->num_clk_freq = nval;
88 prop->clk_freq = devm_kcalloc(bus->dev, prop->num_clk_freq,
89 sizeof(*prop->clk_freq),
90 GFP_KERNEL);
91 if (!prop->clk_freq) {
92 fwnode_handle_put(link);
93 return -ENOMEM;
94 }
95
96 ret = fwnode_property_read_u32_array(link,
97 "mipi-sdw-clock-frequencies-supported",
98 prop->clk_freq, prop->num_clk_freq);
99 if (ret < 0)
100 return ret;
101 }
102
103 /*
104 * Check the frequencies supported. If FW doesn't provide max
105 * freq, then populate here by checking values.
106 */
107 if (!prop->max_clk_freq && prop->clk_freq) {
108 prop->max_clk_freq = prop->clk_freq[0];
109 for (i = 1; i < prop->num_clk_freq; i++) {
110 if (prop->clk_freq[i] > prop->max_clk_freq)
111 prop->max_clk_freq = prop->clk_freq[i];
112 }
113 }
114
115 scales_prop = "mipi-sdw-supported-clock-scales";
116 nval = fwnode_property_count_u32(link, scales_prop);
117 if (nval == 0) {
118 scales_prop = "mipi-sdw-supported-clock-gears";
119 nval = fwnode_property_count_u32(link, scales_prop);
120 }
121 if (nval > 0) {
122 prop->num_clk_gears = nval;
123 prop->clk_gears = devm_kcalloc(bus->dev, prop->num_clk_gears,
124 sizeof(*prop->clk_gears),
125 GFP_KERNEL);
126 if (!prop->clk_gears) {
127 fwnode_handle_put(link);
128 return -ENOMEM;
129 }
130
131 ret = fwnode_property_read_u32_array(link,
132 scales_prop,
133 prop->clk_gears,
134 prop->num_clk_gears);
135 if (ret < 0)
136 return ret;
137 }
138
139 fwnode_property_read_u32(link, "mipi-sdw-default-frame-rate",
140 &prop->default_frame_rate);
141
142 fwnode_property_read_u32(link, "mipi-sdw-default-frame-row-size",
143 &prop->default_row);
144
145 fwnode_property_read_u32(link, "mipi-sdw-default-frame-col-size",
146 &prop->default_col);
147
148 prop->dynamic_frame = mipi_fwnode_property_read_bool(link,
149 "mipi-sdw-dynamic-frame-shape");
150
151 fwnode_property_read_u32(link, "mipi-sdw-command-error-threshold",
152 &prop->err_threshold);
153
154 fwnode_handle_put(link);
155
156 return 0;
157 }
158 EXPORT_SYMBOL(sdw_master_read_prop);
159
sdw_slave_read_dp0(struct sdw_slave * slave,struct fwnode_handle * port,struct sdw_dp0_prop * dp0)160 static int sdw_slave_read_dp0(struct sdw_slave *slave,
161 struct fwnode_handle *port,
162 struct sdw_dp0_prop *dp0)
163 {
164 int nval;
165 int ret;
166
167 fwnode_property_read_u32(port, "mipi-sdw-port-max-wordlength",
168 &dp0->max_word);
169
170 fwnode_property_read_u32(port, "mipi-sdw-port-min-wordlength",
171 &dp0->min_word);
172
173 nval = fwnode_property_count_u32(port, "mipi-sdw-port-wordlength-configs");
174 if (nval > 0) {
175
176 dp0->num_words = nval;
177 dp0->words = devm_kcalloc(&slave->dev,
178 dp0->num_words, sizeof(*dp0->words),
179 GFP_KERNEL);
180 if (!dp0->words)
181 return -ENOMEM;
182
183 ret = fwnode_property_read_u32_array(port,
184 "mipi-sdw-port-wordlength-configs",
185 dp0->words, dp0->num_words);
186 if (ret < 0)
187 return ret;
188 }
189
190 dp0->BRA_flow_controlled = mipi_fwnode_property_read_bool(port,
191 "mipi-sdw-bra-flow-controlled");
192
193 dp0->simple_ch_prep_sm = mipi_fwnode_property_read_bool(port,
194 "mipi-sdw-simplified-channel-prepare-sm");
195
196 dp0->imp_def_interrupts = mipi_fwnode_property_read_bool(port,
197 "mipi-sdw-imp-def-dp0-interrupts-supported");
198
199 nval = fwnode_property_count_u32(port, "mipi-sdw-lane-list");
200 if (nval > 0) {
201 dp0->num_lanes = nval;
202 dp0->lane_list = devm_kcalloc(&slave->dev,
203 dp0->num_lanes, sizeof(*dp0->lane_list),
204 GFP_KERNEL);
205 if (!dp0->lane_list)
206 return -ENOMEM;
207
208 ret = fwnode_property_read_u32_array(port,
209 "mipi-sdw-lane-list",
210 dp0->lane_list, dp0->num_lanes);
211 if (ret < 0)
212 return ret;
213 }
214
215 return 0;
216 }
217
sdw_slave_read_dpn(struct sdw_slave * slave,struct sdw_dpn_prop * dpn,int count,int ports,char * type)218 static int sdw_slave_read_dpn(struct sdw_slave *slave,
219 struct sdw_dpn_prop *dpn, int count, int ports,
220 char *type)
221 {
222 struct fwnode_handle *node;
223 u32 bit, i = 0;
224 unsigned long addr;
225 char name[40];
226 int nval;
227 int ret;
228
229 addr = ports;
230 /* valid ports are 1 to 14 so apply mask */
231 addr &= GENMASK(14, 1);
232
233 for_each_set_bit(bit, &addr, 32) {
234 snprintf(name, sizeof(name),
235 "mipi-sdw-dp-%d-%s-subproperties", bit, type);
236
237 dpn[i].num = bit;
238
239 node = device_get_named_child_node(&slave->dev, name);
240 if (!node) {
241 dev_err(&slave->dev, "%s dpN not found\n", name);
242 return -EIO;
243 }
244
245 fwnode_property_read_u32(node, "mipi-sdw-port-max-wordlength",
246 &dpn[i].max_word);
247 fwnode_property_read_u32(node, "mipi-sdw-port-min-wordlength",
248 &dpn[i].min_word);
249
250 nval = fwnode_property_count_u32(node, "mipi-sdw-port-wordlength-configs");
251 if (nval > 0) {
252 dpn[i].num_words = nval;
253 dpn[i].words = devm_kcalloc(&slave->dev,
254 dpn[i].num_words,
255 sizeof(*dpn[i].words),
256 GFP_KERNEL);
257 if (!dpn[i].words) {
258 fwnode_handle_put(node);
259 return -ENOMEM;
260 }
261
262 ret = fwnode_property_read_u32_array(node,
263 "mipi-sdw-port-wordlength-configs",
264 dpn[i].words, dpn[i].num_words);
265 if (ret < 0)
266 return ret;
267 }
268
269 fwnode_property_read_u32(node, "mipi-sdw-data-port-type",
270 &dpn[i].type);
271
272 fwnode_property_read_u32(node,
273 "mipi-sdw-max-grouping-supported",
274 &dpn[i].max_grouping);
275
276 dpn[i].simple_ch_prep_sm = mipi_fwnode_property_read_bool(node,
277 "mipi-sdw-simplified-channelprepare-sm");
278
279 fwnode_property_read_u32(node,
280 "mipi-sdw-port-channelprepare-timeout",
281 &dpn[i].ch_prep_timeout);
282
283 fwnode_property_read_u32(node,
284 "mipi-sdw-imp-def-dpn-interrupts-supported",
285 &dpn[i].imp_def_interrupts);
286
287 fwnode_property_read_u32(node, "mipi-sdw-min-channel-number",
288 &dpn[i].min_ch);
289
290 fwnode_property_read_u32(node, "mipi-sdw-max-channel-number",
291 &dpn[i].max_ch);
292
293 nval = fwnode_property_count_u32(node, "mipi-sdw-channel-number-list");
294 if (nval > 0) {
295 dpn[i].num_channels = nval;
296 dpn[i].channels = devm_kcalloc(&slave->dev,
297 dpn[i].num_channels,
298 sizeof(*dpn[i].channels),
299 GFP_KERNEL);
300 if (!dpn[i].channels) {
301 fwnode_handle_put(node);
302 return -ENOMEM;
303 }
304
305 ret = fwnode_property_read_u32_array(node,
306 "mipi-sdw-channel-number-list",
307 dpn[i].channels, dpn[i].num_channels);
308 if (ret < 0)
309 return ret;
310 }
311
312 nval = fwnode_property_count_u32(node, "mipi-sdw-channel-combination-list");
313 if (nval > 0) {
314 dpn[i].num_ch_combinations = nval;
315 dpn[i].ch_combinations = devm_kcalloc(&slave->dev,
316 dpn[i].num_ch_combinations,
317 sizeof(*dpn[i].ch_combinations),
318 GFP_KERNEL);
319 if (!dpn[i].ch_combinations) {
320 fwnode_handle_put(node);
321 return -ENOMEM;
322 }
323
324 ret = fwnode_property_read_u32_array(node,
325 "mipi-sdw-channel-combination-list",
326 dpn[i].ch_combinations,
327 dpn[i].num_ch_combinations);
328 if (ret < 0)
329 return ret;
330 }
331
332 fwnode_property_read_u32(node,
333 "mipi-sdw-modes-supported", &dpn[i].modes);
334
335 fwnode_property_read_u32(node, "mipi-sdw-max-async-buffer",
336 &dpn[i].max_async_buffer);
337
338 dpn[i].block_pack_mode = mipi_fwnode_property_read_bool(node,
339 "mipi-sdw-block-packing-mode");
340
341 fwnode_property_read_u32(node, "mipi-sdw-port-encoding-type",
342 &dpn[i].port_encoding);
343
344 nval = fwnode_property_count_u32(node, "mipi-sdw-lane-list");
345 if (nval > 0) {
346 dpn[i].num_lanes = nval;
347 dpn[i].lane_list = devm_kcalloc(&slave->dev,
348 dpn[i].num_lanes, sizeof(*dpn[i].lane_list),
349 GFP_KERNEL);
350 if (!dpn[i].lane_list)
351 return -ENOMEM;
352
353 ret = fwnode_property_read_u32_array(node,
354 "mipi-sdw-lane-list",
355 dpn[i].lane_list, dpn[i].num_lanes);
356 if (ret < 0)
357 return ret;
358 }
359
360 fwnode_handle_put(node);
361
362 i++;
363 }
364
365 return 0;
366 }
367
368 /*
369 * In MIPI DisCo spec for SoundWire, lane mapping for a slave device is done with
370 * mipi-sdw-lane-x-mapping properties, where x is 1..7, and the values for those
371 * properties are mipi-sdw-manager-lane-x or mipi-sdw-peripheral-link-y, where x
372 * is an integer between 1 to 7 if the lane is connected to a manager lane, y is a
373 * character between A to E if the lane is connected to another peripheral lane.
374 */
sdw_slave_read_lane_mapping(struct sdw_slave * slave)375 int sdw_slave_read_lane_mapping(struct sdw_slave *slave)
376 {
377 struct sdw_slave_prop *prop = &slave->prop;
378 struct device *dev = &slave->dev;
379 char prop_name[30];
380 const char *prop_val;
381 size_t len;
382 int ret, i;
383 u8 lane;
384
385 for (i = 0; i < SDW_MAX_LANES; i++) {
386 snprintf(prop_name, sizeof(prop_name), "mipi-sdw-lane-%d-mapping", i);
387 ret = device_property_read_string(dev, prop_name, &prop_val);
388 if (ret)
389 continue;
390
391 len = strlen(prop_val);
392 if (len < 1)
393 return -EINVAL;
394
395 /* The last character is enough to identify the connection */
396 ret = kstrtou8(&prop_val[len - 1], 10, &lane);
397 if (ret)
398 return ret;
399 if (in_range(lane, 1, SDW_MAX_LANES - 1))
400 prop->lane_maps[i] = lane;
401 }
402 return 0;
403 }
404 EXPORT_SYMBOL(sdw_slave_read_lane_mapping);
405
406 /**
407 * sdw_slave_read_prop() - Read Slave properties
408 * @slave: SDW Slave
409 */
sdw_slave_read_prop(struct sdw_slave * slave)410 int sdw_slave_read_prop(struct sdw_slave *slave)
411 {
412 struct sdw_slave_prop *prop = &slave->prop;
413 struct device *dev = &slave->dev;
414 struct fwnode_handle *port;
415 int nval;
416 int ret;
417
418 device_property_read_u32(dev, "mipi-sdw-sw-interface-revision",
419 &prop->mipi_revision);
420
421 prop->wake_capable = mipi_device_property_read_bool(dev,
422 "mipi-sdw-wake-up-unavailable");
423 prop->wake_capable = !prop->wake_capable;
424
425 prop->test_mode_capable = mipi_device_property_read_bool(dev,
426 "mipi-sdw-test-mode-supported");
427
428 prop->clk_stop_mode1 = false;
429 if (mipi_device_property_read_bool(dev,
430 "mipi-sdw-clock-stop-mode1-supported"))
431 prop->clk_stop_mode1 = true;
432
433 prop->simple_clk_stop_capable = mipi_device_property_read_bool(dev,
434 "mipi-sdw-simplified-clockstopprepare-sm-supported");
435
436 device_property_read_u32(dev, "mipi-sdw-clockstopprepare-timeout",
437 &prop->clk_stop_timeout);
438
439 ret = device_property_read_u32(dev, "mipi-sdw-peripheral-channelprepare-timeout",
440 &prop->ch_prep_timeout);
441 if (ret < 0)
442 device_property_read_u32(dev, "mipi-sdw-slave-channelprepare-timeout",
443 &prop->ch_prep_timeout);
444
445 device_property_read_u32(dev,
446 "mipi-sdw-clockstopprepare-hard-reset-behavior",
447 &prop->reset_behave);
448
449 prop->high_PHY_capable = mipi_device_property_read_bool(dev,
450 "mipi-sdw-highPHY-capable");
451
452 prop->paging_support = mipi_device_property_read_bool(dev,
453 "mipi-sdw-paging-supported");
454
455 prop->bank_delay_support = mipi_device_property_read_bool(dev,
456 "mipi-sdw-bank-delay-supported");
457
458 device_property_read_u32(dev,
459 "mipi-sdw-port15-read-behavior", &prop->p15_behave);
460
461 device_property_read_u32(dev, "mipi-sdw-master-count",
462 &prop->master_count);
463
464 device_property_read_u32(dev, "mipi-sdw-source-port-list",
465 &prop->source_ports);
466
467 device_property_read_u32(dev, "mipi-sdw-sink-port-list",
468 &prop->sink_ports);
469
470 device_property_read_u32(dev, "mipi-sdw-sdca-interrupt-register-list",
471 &prop->sdca_interrupt_register_list);
472
473 device_property_read_u32(dev, "mipi-sdw-bra-mode-block-alignment",
474 &prop->bra_block_alignment);
475
476 device_property_read_u32(dev, "mipi-sdw-bra-mode-max-data-per-frame",
477 &prop->bra_max_data_per_frame);
478
479 prop->commit_register_supported = mipi_device_property_read_bool(dev,
480 "mipi-sdw-commit-register-supported");
481
482 /*
483 * Read dp0 properties - we don't rely on the 'mipi-sdw-dp-0-supported'
484 * property since the 'mipi-sdw-dp0-subproperties' property is logically
485 * equivalent.
486 */
487 port = device_get_named_child_node(dev, "mipi-sdw-dp-0-subproperties");
488 if (!port) {
489 dev_dbg(dev, "DP0 node not found!!\n");
490 } else {
491 prop->dp0_prop = devm_kzalloc(&slave->dev,
492 sizeof(*prop->dp0_prop),
493 GFP_KERNEL);
494 if (!prop->dp0_prop) {
495 fwnode_handle_put(port);
496 return -ENOMEM;
497 }
498
499 sdw_slave_read_dp0(slave, port, prop->dp0_prop);
500
501 fwnode_handle_put(port);
502 }
503
504 /*
505 * Based on each DPn port, get source and sink dpn properties.
506 * Also, some ports can operate as both source or sink.
507 */
508
509 /* Allocate memory for set bits in port lists */
510 nval = hweight32(prop->source_ports);
511 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
512 sizeof(*prop->src_dpn_prop),
513 GFP_KERNEL);
514 if (!prop->src_dpn_prop)
515 return -ENOMEM;
516
517 /* Read dpn properties for source port(s) */
518 sdw_slave_read_dpn(slave, prop->src_dpn_prop, nval,
519 prop->source_ports, "source");
520
521 nval = hweight32(prop->sink_ports);
522 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
523 sizeof(*prop->sink_dpn_prop),
524 GFP_KERNEL);
525 if (!prop->sink_dpn_prop)
526 return -ENOMEM;
527
528 /* Read dpn properties for sink port(s) */
529 sdw_slave_read_dpn(slave, prop->sink_dpn_prop, nval,
530 prop->sink_ports, "sink");
531
532 return sdw_slave_read_lane_mapping(slave);
533 }
534 EXPORT_SYMBOL(sdw_slave_read_prop);
535