blob: 79fee1b21ab6e62a8a4a47dd21ba0041a4379508 [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 Bossarteadc0042019-05-22 14:47:18 -050043 "mipi-sdw-link-%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,
Pierre-Louis Bossart00910f32019-05-01 10:57:34 -050052 "mipi-sdw-clock-stop-mode0-supported"))
Pierre-Louis Bossart53d2e9c32019-05-22 14:47:23 -050053 prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE0);
Vinod Koul56d4fe32017-12-14 11:19:35 +053054
55 if (fwnode_property_read_bool(link,
Pierre-Louis Bossart00910f32019-05-01 10:57:34 -050056 "mipi-sdw-clock-stop-mode1-supported"))
Pierre-Louis Bossart53d2e9c32019-05-22 14:47:23 -050057 prop->clk_stop_modes |= BIT(SDW_CLK_STOP_MODE1);
Vinod Koul56d4fe32017-12-14 11:19:35 +053058
59 fwnode_property_read_u32(link,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -050060 "mipi-sdw-max-clock-frequency",
Pierre-Louis Bossart34243052019-05-22 14:47:22 -050061 &prop->max_clk_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) {
Pierre-Louis Bossart34243052019-05-22 14:47:22 -050066 prop->num_clk_freq = nval;
67 prop->clk_freq = devm_kcalloc(bus->dev, prop->num_clk_freq,
68 sizeof(*prop->clk_freq),
69 GFP_KERNEL);
70 if (!prop->clk_freq)
Vinod Koul56d4fe32017-12-14 11:19:35 +053071 return -ENOMEM;
72
73 fwnode_property_read_u32_array(link,
74 "mipi-sdw-clock-frequencies-supported",
Pierre-Louis Bossart34243052019-05-22 14:47:22 -050075 prop->clk_freq, prop->num_clk_freq);
Vinod Koul56d4fe32017-12-14 11:19:35 +053076 }
77
78 /*
79 * Check the frequencies supported. If FW doesn't provide max
80 * freq, then populate here by checking values.
81 */
Pierre-Louis Bossart34243052019-05-22 14:47:22 -050082 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];
Vinod Koul56d4fe32017-12-14 11:19:35 +053087 }
88 }
89
90 nval = fwnode_property_read_u32_array(link,
91 "mipi-sdw-supported-clock-gears", NULL, 0);
92 if (nval > 0) {
Vinod Koul56d4fe32017-12-14 11:19:35 +053093 prop->num_clk_gears = nval;
94 prop->clk_gears = devm_kcalloc(bus->dev, prop->num_clk_gears,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -050095 sizeof(*prop->clk_gears),
96 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +053097 if (!prop->clk_gears)
98 return -ENOMEM;
99
100 fwnode_property_read_u32_array(link,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500101 "mipi-sdw-supported-clock-gears",
102 prop->clk_gears,
103 prop->num_clk_gears);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530104 }
105
106 fwnode_property_read_u32(link, "mipi-sdw-default-frame-rate",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500107 &prop->default_frame_rate);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530108
109 fwnode_property_read_u32(link, "mipi-sdw-default-frame-row-size",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500110 &prop->default_row);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530111
112 fwnode_property_read_u32(link, "mipi-sdw-default-frame-col-size",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500113 &prop->default_col);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530114
115 prop->dynamic_frame = fwnode_property_read_bool(link,
116 "mipi-sdw-dynamic-frame-shape");
117
118 fwnode_property_read_u32(link, "mipi-sdw-command-error-threshold",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500119 &prop->err_threshold);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530120
121 return 0;
122}
123EXPORT_SYMBOL(sdw_master_read_prop);
124
125static int sdw_slave_read_dp0(struct sdw_slave *slave,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500126 struct fwnode_handle *port,
127 struct sdw_dp0_prop *dp0)
Vinod Koul56d4fe32017-12-14 11:19:35 +0530128{
129 int nval;
130
131 fwnode_property_read_u32(port, "mipi-sdw-port-max-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500132 &dp0->max_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530133
134 fwnode_property_read_u32(port, "mipi-sdw-port-min-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500135 &dp0->min_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530136
137 nval = fwnode_property_read_u32_array(port,
138 "mipi-sdw-port-wordlength-configs", NULL, 0);
139 if (nval > 0) {
140
141 dp0->num_words = nval;
142 dp0->words = devm_kcalloc(&slave->dev,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500143 dp0->num_words, sizeof(*dp0->words),
144 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530145 if (!dp0->words)
146 return -ENOMEM;
147
148 fwnode_property_read_u32_array(port,
149 "mipi-sdw-port-wordlength-configs",
150 dp0->words, dp0->num_words);
151 }
152
Pierre-Louis Bossart8acbbfe2019-05-22 14:47:25 -0500153 dp0->BRA_flow_controlled = fwnode_property_read_bool(port,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500154 "mipi-sdw-bra-flow-controlled");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530155
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500156 dp0->simple_ch_prep_sm = fwnode_property_read_bool(port,
157 "mipi-sdw-simplified-channel-prepare-sm");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530158
Pierre-Louis Bossart8acbbfe2019-05-22 14:47:25 -0500159 dp0->imp_def_interrupts = fwnode_property_read_bool(port,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500160 "mipi-sdw-imp-def-dp0-interrupts-supported");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530161
162 return 0;
163}
164
165static int sdw_slave_read_dpn(struct sdw_slave *slave,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500166 struct sdw_dpn_prop *dpn, int count, int ports,
167 char *type)
Vinod Koul56d4fe32017-12-14 11:19:35 +0530168{
169 struct fwnode_handle *node;
170 u32 bit, i = 0;
171 int nval;
172 unsigned long addr;
173 char name[40];
174
175 addr = ports;
176 /* valid ports are 1 to 14 so apply mask */
177 addr &= GENMASK(14, 1);
178
179 for_each_set_bit(bit, &addr, 32) {
180 snprintf(name, sizeof(name),
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500181 "mipi-sdw-dp-%d-%s-subproperties", bit, type);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530182
183 dpn[i].num = bit;
184
185 node = device_get_named_child_node(&slave->dev, name);
186 if (!node) {
187 dev_err(&slave->dev, "%s dpN not found\n", name);
188 return -EIO;
189 }
190
191 fwnode_property_read_u32(node, "mipi-sdw-port-max-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500192 &dpn[i].max_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530193 fwnode_property_read_u32(node, "mipi-sdw-port-min-wordlength",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500194 &dpn[i].min_word);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530195
196 nval = fwnode_property_read_u32_array(node,
197 "mipi-sdw-port-wordlength-configs", NULL, 0);
198 if (nval > 0) {
Vinod Koul56d4fe32017-12-14 11:19:35 +0530199 dpn[i].num_words = nval;
200 dpn[i].words = devm_kcalloc(&slave->dev,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500201 dpn[i].num_words,
202 sizeof(*dpn[i].words),
203 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530204 if (!dpn[i].words)
205 return -ENOMEM;
206
207 fwnode_property_read_u32_array(node,
208 "mipi-sdw-port-wordlength-configs",
209 dpn[i].words, dpn[i].num_words);
210 }
211
212 fwnode_property_read_u32(node, "mipi-sdw-data-port-type",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500213 &dpn[i].type);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530214
215 fwnode_property_read_u32(node,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500216 "mipi-sdw-max-grouping-supported",
217 &dpn[i].max_grouping);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530218
219 dpn[i].simple_ch_prep_sm = fwnode_property_read_bool(node,
220 "mipi-sdw-simplified-channelprepare-sm");
221
222 fwnode_property_read_u32(node,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500223 "mipi-sdw-port-channelprepare-timeout",
224 &dpn[i].ch_prep_timeout);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530225
226 fwnode_property_read_u32(node,
227 "mipi-sdw-imp-def-dpn-interrupts-supported",
Pierre-Louis Bossart8acbbfe2019-05-22 14:47:25 -0500228 &dpn[i].imp_def_interrupts);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530229
230 fwnode_property_read_u32(node, "mipi-sdw-min-channel-number",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500231 &dpn[i].min_ch);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530232
233 fwnode_property_read_u32(node, "mipi-sdw-max-channel-number",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500234 &dpn[i].max_ch);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530235
236 nval = fwnode_property_read_u32_array(node,
237 "mipi-sdw-channel-number-list", NULL, 0);
238 if (nval > 0) {
Vinod Koul56d4fe32017-12-14 11:19:35 +0530239 dpn[i].num_ch = nval;
240 dpn[i].ch = devm_kcalloc(&slave->dev, dpn[i].num_ch,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500241 sizeof(*dpn[i].ch),
242 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530243 if (!dpn[i].ch)
244 return -ENOMEM;
245
246 fwnode_property_read_u32_array(node,
247 "mipi-sdw-channel-number-list",
248 dpn[i].ch, dpn[i].num_ch);
249 }
250
251 nval = fwnode_property_read_u32_array(node,
252 "mipi-sdw-channel-combination-list", NULL, 0);
253 if (nval > 0) {
Vinod Koul56d4fe32017-12-14 11:19:35 +0530254 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),
258 GFP_KERNEL);
259 if (!dpn[i].ch_combinations)
260 return -ENOMEM;
261
262 fwnode_property_read_u32_array(node,
263 "mipi-sdw-channel-combination-list",
264 dpn[i].ch_combinations,
265 dpn[i].num_ch_combinations);
266 }
267
268 fwnode_property_read_u32(node,
269 "mipi-sdw-modes-supported", &dpn[i].modes);
270
271 fwnode_property_read_u32(node, "mipi-sdw-max-async-buffer",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500272 &dpn[i].max_async_buffer);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530273
274 dpn[i].block_pack_mode = fwnode_property_read_bool(node,
275 "mipi-sdw-block-packing-mode");
276
277 fwnode_property_read_u32(node, "mipi-sdw-port-encoding-type",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500278 &dpn[i].port_encoding);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530279
280 /* TODO: Read audio mode */
281
282 i++;
283 }
284
285 return 0;
286}
287
288/**
289 * sdw_slave_read_prop() - Read Slave properties
290 * @slave: SDW Slave
291 */
292int sdw_slave_read_prop(struct sdw_slave *slave)
293{
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;
298
299 device_property_read_u32(dev, "mipi-sdw-sw-interface-revision",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500300 &prop->mipi_revision);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530301
302 prop->wake_capable = device_property_read_bool(dev,
303 "mipi-sdw-wake-up-unavailable");
304 prop->wake_capable = !prop->wake_capable;
305
306 prop->test_mode_capable = device_property_read_bool(dev,
307 "mipi-sdw-test-mode-supported");
308
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;
313
314 prop->simple_clk_stop_capable = device_property_read_bool(dev,
315 "mipi-sdw-simplified-clockstopprepare-sm-supported");
316
317 device_property_read_u32(dev, "mipi-sdw-clockstopprepare-timeout",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500318 &prop->clk_stop_timeout);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530319
320 device_property_read_u32(dev, "mipi-sdw-slave-channelprepare-timeout",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500321 &prop->ch_prep_timeout);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530322
323 device_property_read_u32(dev,
324 "mipi-sdw-clockstopprepare-hard-reset-behavior",
325 &prop->reset_behave);
326
327 prop->high_PHY_capable = device_property_read_bool(dev,
328 "mipi-sdw-highPHY-capable");
329
330 prop->paging_support = device_property_read_bool(dev,
331 "mipi-sdw-paging-support");
332
333 prop->bank_delay_support = device_property_read_bool(dev,
334 "mipi-sdw-bank-delay-support");
335
336 device_property_read_u32(dev,
337 "mipi-sdw-port15-read-behavior", &prop->p15_behave);
338
339 device_property_read_u32(dev, "mipi-sdw-master-count",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500340 &prop->master_count);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530341
342 device_property_read_u32(dev, "mipi-sdw-source-port-list",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500343 &prop->source_ports);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530344
345 device_property_read_u32(dev, "mipi-sdw-sink-port-list",
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500346 &prop->sink_ports);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530347
348 /* Read dp0 properties */
349 port = device_get_named_child_node(dev, "mipi-sdw-dp-0-subproperties");
350 if (!port) {
351 dev_dbg(dev, "DP0 node not found!!\n");
352 } else {
Vinod Koul56d4fe32017-12-14 11:19:35 +0530353 prop->dp0_prop = devm_kzalloc(&slave->dev,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500354 sizeof(*prop->dp0_prop),
355 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530356 if (!prop->dp0_prop)
357 return -ENOMEM;
358
359 sdw_slave_read_dp0(slave, port, prop->dp0_prop);
360 dp0 = 1;
361 }
362
363 /*
364 * Based on each DPn port, get source and sink dpn properties.
365 * Also, some ports can operate as both source or sink.
366 */
367
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,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500371 sizeof(*prop->src_dpn_prop),
372 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530373 if (!prop->src_dpn_prop)
374 return -ENOMEM;
375
376 /* Read dpn properties for source port(s) */
377 sdw_slave_read_dpn(slave, prop->src_dpn_prop, nval,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500378 prop->source_ports, "source");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530379
380 nval = hweight32(prop->sink_ports);
381 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500382 sizeof(*prop->sink_dpn_prop),
383 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530384 if (!prop->sink_dpn_prop)
385 return -ENOMEM;
386
387 /* Read dpn properties for sink port(s) */
388 sdw_slave_read_dpn(slave, prop->sink_dpn_prop, nval,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500389 prop->sink_ports, "sink");
Vinod Koul56d4fe32017-12-14 11:19:35 +0530390
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 */
394
395 /* Allocate port_ready based on num_of_ports */
396 slave->port_ready = devm_kcalloc(&slave->dev, num_of_ports,
Pierre-Louis Bossart31dba312019-05-01 10:57:33 -0500397 sizeof(*slave->port_ready),
398 GFP_KERNEL);
Vinod Koul56d4fe32017-12-14 11:19:35 +0530399 if (!slave->port_ready)
400 return -ENOMEM;
401
402 /* Initialize completion */
403 for (i = 0; i < num_of_ports; i++)
404 init_completion(&slave->port_ready[i]);
405
406 return 0;
407}
408EXPORT_SYMBOL(sdw_slave_read_prop);