xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2014 Broadcom Corporation
4  */
5 #include <linux/init.h>
6 #include <linux/of.h>
7 #include <linux/of_irq.h>
8 #include <linux/of_net.h>
9 #include <linux/clk.h>
10 
11 #include <defs.h>
12 #include "debug.h"
13 #include "core.h"
14 #include "common.h"
15 #include "of.h"
16 
17 static int brcmf_of_get_country_codes(struct device *dev,
18 				      struct brcmf_mp_device *settings)
19 {
20 	struct device_node *np = dev->of_node;
21 	struct brcmfmac_pd_cc_entry *cce;
22 	struct brcmfmac_pd_cc *cc;
23 	int count;
24 	int i;
25 
26 	count = of_property_count_strings(np, "brcm,ccode-map");
27 	if (count < 0) {
28 		/* If no explicit country code map is specified, check whether
29 		 * the trivial map should be used.
30 		 */
31 		settings->trivial_ccode_map =
32 			of_property_read_bool(np, "brcm,ccode-map-trivial");
33 
34 		/* The property is optional, so return success if it doesn't
35 		 * exist. Otherwise propagate the error code.
36 		 */
37 		return (count == -EINVAL) ? 0 : count;
38 	}
39 
40 	cc = devm_kzalloc(dev, struct_size(cc, table, count), GFP_KERNEL);
41 	if (!cc)
42 		return -ENOMEM;
43 
44 	cc->table_size = count;
45 
46 	for (i = 0; i < count; i++) {
47 		const char *map;
48 
49 		cce = &cc->table[i];
50 
51 		if (of_property_read_string_index(np, "brcm,ccode-map",
52 						  i, &map))
53 			continue;
54 
55 		/* String format e.g. US-Q2-86 */
56 		if (sscanf(map, "%2c-%2c-%d", cce->iso3166, cce->cc,
57 			   &cce->rev) != 3)
58 			brcmf_err("failed to read country map %s\n", map);
59 		else
60 			brcmf_dbg(INFO, "%s-%s-%d\n", cce->iso3166, cce->cc,
61 				  cce->rev);
62 	}
63 
64 	settings->country_codes = cc;
65 
66 	return 0;
67 }
68 
69 int brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
70 		   struct brcmf_mp_device *settings)
71 {
72 	struct brcmfmac_sdio_pd *sdio = &settings->bus.sdio;
73 	struct device_node *root, *np = dev->of_node;
74 	struct of_phandle_args oirq;
75 	struct clk *clk;
76 	const char *prop;
77 	int irq;
78 	int err;
79 	u32 irqf;
80 	u32 val;
81 
82 	/* Apple ARM64 platforms have their own idea of board type, passed in
83 	 * via the device tree. They also have an antenna SKU parameter
84 	 */
85 	err = of_property_read_string(np, "brcm,board-type", &prop);
86 	if (!err)
87 		settings->board_type = prop;
88 
89 	if (!of_property_read_string(np, "apple,antenna-sku", &prop))
90 		settings->antenna_sku = prop;
91 
92 	/* The WLAN calibration blob is normally stored in SROM, but Apple
93 	 * ARM64 platforms pass it via the DT instead.
94 	 */
95 	prop = of_get_property(np, "brcm,cal-blob", &settings->cal_size);
96 	if (prop && settings->cal_size)
97 		settings->cal_blob = prop;
98 
99 	/* Set board-type to the first string of the machine compatible prop */
100 	root = of_find_node_by_path("/");
101 	if (root && err) {
102 		char *board_type;
103 		const char *tmp;
104 
105 		of_property_read_string_index(root, "compatible", 0, &tmp);
106 
107 		/* get rid of '/' in the compatible string to be able to find the FW */
108 		board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
109 		if (!board_type) {
110 			of_node_put(root);
111 			return 0;
112 		}
113 		strreplace(board_type, '/', '-');
114 		settings->board_type = board_type;
115 	}
116 	of_node_put(root);
117 
118 	clk = devm_clk_get_optional_enabled_with_rate(dev, "lpo", 32768);
119 	if (IS_ERR(clk))
120 		return PTR_ERR(clk);
121 
122 	brcmf_dbg(INFO, "%s LPO clock\n", clk ? "enable" : "no");
123 
124 	if (!np || !of_device_is_compatible(np, "brcm,bcm4329-fmac"))
125 		return 0;
126 
127 	err = brcmf_of_get_country_codes(dev, settings);
128 	if (err)
129 		brcmf_err("failed to get OF country code map (err=%d)\n", err);
130 
131 	of_get_mac_address(np, settings->mac);
132 
133 	if (bus_type != BRCMF_BUSTYPE_SDIO)
134 		return 0;
135 
136 	if (of_property_read_u32(np, "brcm,drive-strength", &val) == 0)
137 		sdio->drive_strength = val;
138 
139 	/* make sure there are interrupts defined in the node */
140 	if (of_irq_parse_one(np, 0, &oirq))
141 		return 0;
142 
143 	irq = irq_create_of_mapping(&oirq);
144 	if (!irq) {
145 		brcmf_err("interrupt could not be mapped\n");
146 		return 0;
147 	}
148 	irqf = irq_get_trigger_type(irq);
149 
150 	sdio->oob_irq_supported = true;
151 	sdio->oob_irq_nr = irq;
152 	sdio->oob_irq_flags = irqf;
153 
154 	return 0;
155 }
156