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 "of.h"
14 
15 static int brcmf_of_get_country_codes(struct device *dev,
16 				      struct brcmf_mp_device *settings)
17 {
18 	struct device_node *np = dev->of_node;
19 	struct brcmfmac_pd_cc_entry *cce;
20 	struct brcmfmac_pd_cc *cc;
21 	int count;
22 	int i;
23 
24 	count = of_property_count_strings(np, "brcm,ccode-map");
25 	if (count < 0) {
26 		/* The property is optional, so return success if it doesn't
27 		 * exist. Otherwise propagate the error code.
28 		 */
29 		return (count == -EINVAL) ? 0 : count;
30 	}
31 
32 	cc = devm_kzalloc(dev, struct_size(cc, table, count), GFP_KERNEL);
33 	if (!cc)
34 		return -ENOMEM;
35 
36 	cc->table_size = count;
37 
38 	for (i = 0; i < count; i++) {
39 		const char *map;
40 
41 		cce = &cc->table[i];
42 
43 		if (of_property_read_string_index(np, "brcm,ccode-map",
44 						  i, &map))
45 			continue;
46 
47 		/* String format e.g. US-Q2-86 */
48 		if (sscanf(map, "%2c-%2c-%d", cce->iso3166, cce->cc,
49 			   &cce->rev) != 3)
50 			brcmf_err("failed to read country map %s\n", map);
51 		else
52 			brcmf_dbg(INFO, "%s-%s-%d\n", cce->iso3166, cce->cc,
53 				  cce->rev);
54 	}
55 
56 	settings->country_codes = cc;
57 
58 	return 0;
59 }
60 
61 void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
62 		    struct brcmf_mp_device *settings)
63 {
64 	struct brcmfmac_sdio_pd *sdio = &settings->bus.sdio;
65 	struct device_node *root, *np = dev->of_node;
66 	int irq;
67 	int err;
68 	u32 irqf;
69 	u32 val;
70 
71 	/* Set board-type to the first string of the machine compatible prop */
72 	root = of_find_node_by_path("/");
73 	if (root) {
74 		int i;
75 		char *board_type;
76 		const char *tmp;
77 
78 		of_property_read_string_index(root, "compatible", 0, &tmp);
79 
80 		/* get rid of '/' in the compatible string to be able to find the FW */
81 		board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
82 		if (!board_type) {
83 			of_node_put(root);
84 			return;
85 		}
86 		for (i = 0; i < board_type[i]; i++) {
87 			if (board_type[i] == '/')
88 				board_type[i] = '-';
89 		}
90 		settings->board_type = board_type;
91 
92 		of_node_put(root);
93 	}
94 
95 	if (!np || !of_device_is_compatible(np, "brcm,bcm4329-fmac"))
96 		return;
97 
98 	err = brcmf_of_get_country_codes(dev, settings);
99 	if (err)
100 		brcmf_err("failed to get OF country code map (err=%d)\n", err);
101 
102 	if (bus_type != BRCMF_BUSTYPE_SDIO)
103 		return;
104 
105 	if (of_property_read_u32(np, "brcm,drive-strength", &val) == 0)
106 		sdio->drive_strength = val;
107 
108 	/* make sure there are interrupts defined in the node */
109 	if (!of_find_property(np, "interrupts", NULL))
110 		return;
111 
112 	irq = irq_of_parse_and_map(np, 0);
113 	if (!irq) {
114 		brcmf_err("interrupt could not be mapped\n");
115 		return;
116 	}
117 	irqf = irqd_get_trigger_type(irq_get_irq_data(irq));
118 
119 	sdio->oob_irq_supported = true;
120 	sdio->oob_irq_nr = irq;
121 	sdio->oob_irq_flags = irqf;
122 }
123