Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / of.c
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
9 #include <defs.h>
10 #include "debug.h"
11 #include "core.h"
12 #include "common.h"
13 #include "firmware.h"
14 #include "of.h"
15
16 static int brcmf_of_get_country_codes(struct device *dev,
17                                       struct brcmf_mp_device *settings)
18 {
19         struct device_node *np = dev->of_node;
20         struct brcmfmac_pd_cc_entry *cce;
21         struct brcmfmac_pd_cc *cc;
22         int count;
23         int i;
24
25         count = of_property_count_strings(np, "brcm,ccode-map");
26         if (count < 0) {
27                 /* The property is optional, so return success if it doesn't
28                  * exist. Otherwise propagate the error code.
29                  */
30                 return (count == -EINVAL) ? 0 : count;
31         }
32
33         cc = devm_kzalloc(dev, sizeof(*cc) + count * sizeof(*cce), GFP_KERNEL);
34         if (!cc)
35                 return -ENOMEM;
36
37         cc->table_size = count;
38
39         for (i = 0; i < count; i++) {
40                 const char *map;
41
42                 cce = &cc->table[i];
43
44                 if (of_property_read_string_index(np, "brcm,ccode-map",
45                                                   i, &map))
46                         continue;
47
48                 /* String format e.g. US-Q2-86 */
49                 if (sscanf(map, "%2c-%2c-%d", cce->iso3166, cce->cc,
50                            &cce->rev) != 3)
51                         brcmf_err("failed to read country map %s\n", map);
52                 else
53                         brcmf_dbg(INFO, "%s-%s-%d\n", cce->iso3166, cce->cc,
54                                   cce->rev);
55         }
56
57         settings->country_codes = cc;
58
59         return 0;
60 }
61
62 void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
63                     struct brcmf_mp_device *settings)
64 {
65         struct brcmfmac_sdio_pd *sdio = &settings->bus.sdio;
66         struct device_node *root, *np = dev->of_node;
67         int irq;
68         int err;
69         u32 irqf;
70         u32 val;
71
72         /* Set board-type to the first string of the machine compatible prop */
73         root = of_find_node_by_path("/");
74         if (root) {
75                 int i, len;
76                 char *board_type;
77                 const char *tmp;
78
79                 of_property_read_string_index(root, "compatible", 0, &tmp);
80
81                 /* get rid of '/' in the compatible string to be able to find the FW */
82                 len = strlen(tmp) + 1;
83                 board_type = devm_kzalloc(dev, len, GFP_KERNEL);
84                 strscpy(board_type, tmp, len);
85                 for (i = 0; i < board_type[i]; i++) {
86                         if (board_type[i] == '/')
87                                 board_type[i] = '-';
88                 }
89                 settings->board_type = board_type;
90
91                 of_node_put(root);
92         }
93
94         if (!np || !of_device_is_compatible(np, "brcm,bcm4329-fmac"))
95                 return;
96
97         err = brcmf_of_get_country_codes(dev, settings);
98         if (err)
99                 brcmf_err("failed to get OF country code map (err=%d)\n", err);
100
101         if (bus_type != BRCMF_BUSTYPE_SDIO)
102                 return;
103
104         if (of_property_read_u32(np, "brcm,drive-strength", &val) == 0)
105                 sdio->drive_strength = val;
106
107         /* make sure there are interrupts defined in the node */
108         if (!of_find_property(np, "interrupts", NULL))
109                 return;
110
111         irq = irq_of_parse_and_map(np, 0);
112         if (!irq) {
113                 brcmf_err("interrupt could not be mapped\n");
114                 return;
115         }
116         irqf = irqd_get_trigger_type(irq_get_irq_data(irq));
117
118         sdio->oob_irq_supported = true;
119         sdio->oob_irq_nr = irq;
120         sdio->oob_irq_flags = irqf;
121 }
122
123 struct brcmf_firmware_mapping *
124 brcmf_of_fwnames(struct device *dev, u32 *fwname_count)
125 {
126         struct device_node *np = dev->of_node;
127         struct brcmf_firmware_mapping *fwnames;
128         struct device_node *map_np, *fw_np;
129         int of_count;
130         int count = 0;
131
132         map_np = of_get_child_by_name(np, "firmwares");
133         of_count = of_get_child_count(map_np);
134         if (!of_count)
135                 return NULL;
136
137         fwnames = devm_kcalloc(dev, of_count,
138                                sizeof(struct brcmf_firmware_mapping),
139                                GFP_KERNEL);
140
141         for_each_child_of_node(map_np, fw_np)
142         {
143                 struct brcmf_firmware_mapping *cur = &fwnames[count];
144
145                 if (of_property_read_u32(fw_np, "chipid", &cur->chipid) ||
146                     of_property_read_u32(fw_np, "revmask", &cur->revmask))
147                         continue;
148                 cur->fw_base = of_get_property(fw_np, "fw_base", NULL);
149                 if (cur->fw_base)
150                         count++;
151         }
152
153         *fwname_count = count;
154
155         return count ? fwnames : NULL;
156 }