blob: 95efa82dc73b4a157788fa3eb72cca52ca5f4327 [file] [log] [blame]
Vinod Koul56d4fe32017-12-14 11:19:35 +05301// 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/mod_devicetable.h>
23#include <linux/soundwire/sdw.h>
24#include "bus.h"
25
26/**
27 * sdw_master_read_prop() - Read Master properties
28 * @bus: SDW bus instance
29 */
30int sdw_master_read_prop(struct sdw_bus *bus)
31{
32 struct sdw_master_prop *prop = &bus->prop;
33 struct fwnode_handle *link;
34 char name[32];
35 int nval, i;
36
37 device_property_read_u32(bus->dev,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -050038 "mipi-sdw-sw-interface-revision",
39 &prop->revision);
Vinod Koul56d4fe32017-12-14 11:19:35 +053040
41 /* Find master handle */
42 snprintf(name, sizeof(name),
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -050043 "mipi-sdw-master-%d-subproperties", bus->link_id);
Vinod Koul56d4fe32017-12-14 11:19:35 +053044
45 link = device_get_named_child_node(bus->dev, name);
46 if (!link) {
47 dev_err(bus->dev, "Master node %s not found\n", name);
48 return -EIO;
49 }
50
51 if (fwnode_property_read_bool(link,
52 "mipi-sdw-clock-stop-mode0-supported") == true)
53 prop->clk_stop_mode = SDW_CLK_STOP_MODE0;
54
55 if (fwnode_property_read_bool(link,
56 "mipi-sdw-clock-stop-mode1-supported") == true)
57 prop->clk_stop_mode |= SDW_CLK_STOP_MODE1;
58
59 fwnode_property_read_u32(link,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -050060 "mipi-sdw-max-clock-frequency",
61 &prop->max_freq);
Vinod Koul56d4fe32017-12-14 11:19:35 +053062
63 nval = fwnode_property_read_u32_array(link,
64 "mipi-sdw-clock-frequencies-supported", NULL, 0);
65 if (nval > 0) {
66
67 prop->num_freq = nval;
68 prop->freq = devm_kcalloc(bus->dev, prop->num_freq,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -050069 sizeof(*prop->freq), GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +053070 if (!prop->freq)
71 return -ENOMEM;
72
73 fwnode_property_read_u32_array(link,
74 "mipi-sdw-clock-frequencies-supported",
75 prop->freq, prop->num_freq);
76 }
77
78 /*
79 * Check the frequencies supported. If FW doesn't provide max
80 * freq, then populate here by checking values.
81 */
82 if (!prop->max_freq && prop->freq) {
83 prop->max_freq = prop->freq[0];
84 for (i = 1; i < prop->num_freq; i++) {
85 if (prop->freq[i] > prop->max_freq)
86 prop->max_freq = prop->freq[i];
87 }
88 }
89
90 nval = fwnode_property_read_u32_array(link,
91 "mipi-sdw-supported-clock-gears", NULL, 0);
92 if (nval > 0) {
93
94 prop->num_clk_gears = nval;
95 prop->clk_gears = devm_kcalloc(bus->dev, prop->num_clk_gears,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -050096 sizeof(*prop->clk_gears),
97 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +053098 if (!prop->clk_gears)
99 return -ENOMEM;
100
101 fwnode_property_read_u32_array(link,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500102 "mipi-sdw-supported-clock-gears",
103 prop->clk_gears,
104 prop->num_clk_gears);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530105 }
106
107 fwnode_property_read_u32(link, "mipi-sdw-default-frame-rate",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500108 &prop->default_frame_rate);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530109
110 fwnode_property_read_u32(link, "mipi-sdw-default-frame-row-size",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500111 &prop->default_row);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530112
113 fwnode_property_read_u32(link, "mipi-sdw-default-frame-col-size",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500114 &prop->default_col);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530115
116 prop->dynamic_frame = fwnode_property_read_bool(link,
117 "mipi-sdw-dynamic-frame-shape");
118
119 fwnode_property_read_u32(link, "mipi-sdw-command-error-threshold",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500120 &prop->err_threshold);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530121
122 return 0;
123}
124EXPORT_SYMBOL(sdw_master_read_prop);
125
126static int sdw_slave_read_dp0(struct sdw_slave *slave,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500127 struct fwnode_handle *port,
128 struct sdw_dp0_prop *dp0)
Vinod Koul56d4fe32017-12-14 11:19:35 +0530129{
130 int nval;
131
132 fwnode_property_read_u32(port, "mipi-sdw-port-max-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500133 &dp0->max_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530134
135 fwnode_property_read_u32(port, "mipi-sdw-port-min-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500136 &dp0->min_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530137
138 nval = fwnode_property_read_u32_array(port,
139 "mipi-sdw-port-wordlength-configs", NULL, 0);
140 if (nval > 0) {
141
142 dp0->num_words = nval;
143 dp0->words = devm_kcalloc(&slave->dev,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500144 dp0->num_words, sizeof(*dp0->words),
145 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530146 if (!dp0->words)
147 return -ENOMEM;
148
149 fwnode_property_read_u32_array(port,
150 "mipi-sdw-port-wordlength-configs",
151 dp0->words, dp0->num_words);
152 }
153
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500154 dp0->flow_controlled = fwnode_property_read_bool(port,
155 "mipi-sdw-bra-flow-controlled");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530156
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500157 dp0->simple_ch_prep_sm = fwnode_property_read_bool(port,
158 "mipi-sdw-simplified-channel-prepare-sm");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530159
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500160 dp0->device_interrupts = fwnode_property_read_bool(port,
161 "mipi-sdw-imp-def-dp0-interrupts-supported");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530162
163 return 0;
164}
165
166static int sdw_slave_read_dpn(struct sdw_slave *slave,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500167 struct sdw_dpn_prop *dpn, int count, int ports,
168 char *type)
Vinod Koul56d4fe32017-12-14 11:19:35 +0530169{
170 struct fwnode_handle *node;
171 u32 bit, i = 0;
172 int nval;
173 unsigned long addr;
174 char name[40];
175
176 addr = ports;
177 /* valid ports are 1 to 14 so apply mask */
178 addr &= GENMASK(14, 1);
179
180 for_each_set_bit(bit, &addr, 32) {
181 snprintf(name, sizeof(name),
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500182 "mipi-sdw-dp-%d-%s-subproperties", bit, type);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530183
184 dpn[i].num = bit;
185
186 node = device_get_named_child_node(&slave->dev, name);
187 if (!node) {
188 dev_err(&slave->dev, "%s dpN not found\n", name);
189 return -EIO;
190 }
191
192 fwnode_property_read_u32(node, "mipi-sdw-port-max-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500193 &dpn[i].max_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530194 fwnode_property_read_u32(node, "mipi-sdw-port-min-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500195 &dpn[i].min_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530196
197 nval = fwnode_property_read_u32_array(node,
198 "mipi-sdw-port-wordlength-configs", NULL, 0);
199 if (nval > 0) {
200
201 dpn[i].num_words = nval;
202 dpn[i].words = devm_kcalloc(&slave->dev,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500203 dpn[i].num_words,
204 sizeof(*dpn[i].words),
205 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530206 if (!dpn[i].words)
207 return -ENOMEM;
208
209 fwnode_property_read_u32_array(node,
210 "mipi-sdw-port-wordlength-configs",
211 dpn[i].words, dpn[i].num_words);
212 }
213
214 fwnode_property_read_u32(node, "mipi-sdw-data-port-type",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500215 &dpn[i].type);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530216
217 fwnode_property_read_u32(node,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500218 "mipi-sdw-max-grouping-supported",
219 &dpn[i].max_grouping);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530220
221 dpn[i].simple_ch_prep_sm = fwnode_property_read_bool(node,
222 "mipi-sdw-simplified-channelprepare-sm");
223
224 fwnode_property_read_u32(node,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500225 "mipi-sdw-port-channelprepare-timeout",
226 &dpn[i].ch_prep_timeout);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530227
228 fwnode_property_read_u32(node,
229 "mipi-sdw-imp-def-dpn-interrupts-supported",
230 &dpn[i].device_interrupts);
231
232 fwnode_property_read_u32(node, "mipi-sdw-min-channel-number",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500233 &dpn[i].min_ch);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530234
235 fwnode_property_read_u32(node, "mipi-sdw-max-channel-number",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500236 &dpn[i].max_ch);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530237
238 nval = fwnode_property_read_u32_array(node,
239 "mipi-sdw-channel-number-list", NULL, 0);
240 if (nval > 0) {
241
242 dpn[i].num_ch = nval;
243 dpn[i].ch = devm_kcalloc(&slave->dev, dpn[i].num_ch,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500244 sizeof(*dpn[i].ch),
245 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530246 if (!dpn[i].ch)
247 return -ENOMEM;
248
249 fwnode_property_read_u32_array(node,
250 "mipi-sdw-channel-number-list",
251 dpn[i].ch, dpn[i].num_ch);
252 }
253
254 nval = fwnode_property_read_u32_array(node,
255 "mipi-sdw-channel-combination-list", NULL, 0);
256 if (nval > 0) {
257
258 dpn[i].num_ch_combinations = nval;
259 dpn[i].ch_combinations = devm_kcalloc(&slave->dev,
260 dpn[i].num_ch_combinations,
261 sizeof(*dpn[i].ch_combinations),
262 GFP_KERNEL);
263 if (!dpn[i].ch_combinations)
264 return -ENOMEM;
265
266 fwnode_property_read_u32_array(node,
267 "mipi-sdw-channel-combination-list",
268 dpn[i].ch_combinations,
269 dpn[i].num_ch_combinations);
270 }
271
272 fwnode_property_read_u32(node,
273 "mipi-sdw-modes-supported", &dpn[i].modes);
274
275 fwnode_property_read_u32(node, "mipi-sdw-max-async-buffer",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500276 &dpn[i].max_async_buffer);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530277
278 dpn[i].block_pack_mode = fwnode_property_read_bool(node,
279 "mipi-sdw-block-packing-mode");
280
281 fwnode_property_read_u32(node, "mipi-sdw-port-encoding-type",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500282 &dpn[i].port_encoding);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530283
284 /* TODO: Read audio mode */
285
286 i++;
287 }
288
289 return 0;
290}
291
292/**
293 * sdw_slave_read_prop() - Read Slave properties
294 * @slave: SDW Slave
295 */
296int sdw_slave_read_prop(struct sdw_slave *slave)
297{
298 struct sdw_slave_prop *prop = &slave->prop;
299 struct device *dev = &slave->dev;
300 struct fwnode_handle *port;
301 int num_of_ports, nval, i, dp0 = 0;
302
303 device_property_read_u32(dev, "mipi-sdw-sw-interface-revision",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500304 &prop->mipi_revision);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530305
306 prop->wake_capable = device_property_read_bool(dev,
307 "mipi-sdw-wake-up-unavailable");
308 prop->wake_capable = !prop->wake_capable;
309
310 prop->test_mode_capable = device_property_read_bool(dev,
311 "mipi-sdw-test-mode-supported");
312
313 prop->clk_stop_mode1 = false;
314 if (device_property_read_bool(dev,
315 "mipi-sdw-clock-stop-mode1-supported"))
316 prop->clk_stop_mode1 = true;
317
318 prop->simple_clk_stop_capable = device_property_read_bool(dev,
319 "mipi-sdw-simplified-clockstopprepare-sm-supported");
320
321 device_property_read_u32(dev, "mipi-sdw-clockstopprepare-timeout",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500322 &prop->clk_stop_timeout);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530323
324 device_property_read_u32(dev, "mipi-sdw-slave-channelprepare-timeout",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500325 &prop->ch_prep_timeout);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530326
327 device_property_read_u32(dev,
328 "mipi-sdw-clockstopprepare-hard-reset-behavior",
329 &prop->reset_behave);
330
331 prop->high_PHY_capable = device_property_read_bool(dev,
332 "mipi-sdw-highPHY-capable");
333
334 prop->paging_support = device_property_read_bool(dev,
335 "mipi-sdw-paging-support");
336
337 prop->bank_delay_support = device_property_read_bool(dev,
338 "mipi-sdw-bank-delay-support");
339
340 device_property_read_u32(dev,
341 "mipi-sdw-port15-read-behavior", &prop->p15_behave);
342
343 device_property_read_u32(dev, "mipi-sdw-master-count",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500344 &prop->master_count);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530345
346 device_property_read_u32(dev, "mipi-sdw-source-port-list",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500347 &prop->source_ports);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530348
349 device_property_read_u32(dev, "mipi-sdw-sink-port-list",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500350 &prop->sink_ports);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530351
352 /* Read dp0 properties */
353 port = device_get_named_child_node(dev, "mipi-sdw-dp-0-subproperties");
354 if (!port) {
355 dev_dbg(dev, "DP0 node not found!!\n");
356 } else {
357
358 prop->dp0_prop = devm_kzalloc(&slave->dev,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500359 sizeof(*prop->dp0_prop),
360 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530361 if (!prop->dp0_prop)
362 return -ENOMEM;
363
364 sdw_slave_read_dp0(slave, port, prop->dp0_prop);
365 dp0 = 1;
366 }
367
368 /*
369 * Based on each DPn port, get source and sink dpn properties.
370 * Also, some ports can operate as both source or sink.
371 */
372
373 /* Allocate memory for set bits in port lists */
374 nval = hweight32(prop->source_ports);
375 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500376 sizeof(*prop->src_dpn_prop),
377 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530378 if (!prop->src_dpn_prop)
379 return -ENOMEM;
380
381 /* Read dpn properties for source port(s) */
382 sdw_slave_read_dpn(slave, prop->src_dpn_prop, nval,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500383 prop->source_ports, "source");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530384
385 nval = hweight32(prop->sink_ports);
386 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500387 sizeof(*prop->sink_dpn_prop),
388 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530389 if (!prop->sink_dpn_prop)
390 return -ENOMEM;
391
392 /* Read dpn properties for sink port(s) */
393 sdw_slave_read_dpn(slave, prop->sink_dpn_prop, nval,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500394 prop->sink_ports, "sink");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530395
396 /* some ports are bidirectional so check total ports by ORing */
397 nval = prop->source_ports | prop->sink_ports;
398 num_of_ports = hweight32(nval) + dp0; /* add DP0 */
399
400 /* Allocate port_ready based on num_of_ports */
401 slave->port_ready = devm_kcalloc(&slave->dev, num_of_ports,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500402 sizeof(*slave->port_ready),
403 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530404 if (!slave->port_ready)
405 return -ENOMEM;
406
407 /* Initialize completion */
408 for (i = 0; i < num_of_ports; i++)
409 init_completion(&slave->port_ready[i]);
410
411 return 0;
412}
413EXPORT_SYMBOL(sdw_slave_read_prop);