aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/ath/wil6210/wil_platform_msm.c
blob: b354a743240d0d0e658f55bb77bfc416edb94478 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (c) 2014 Qualcomm Atheros, Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/of.h>
#include <linux/slab.h>
#include <linux/msm-bus.h>

#include "wil_platform.h"
#include "wil_platform_msm.h"

/**
 * struct wil_platform_msm - wil6210 msm platform module info
 *
 * @dev: device object
 * @msm_bus_handle: handle for using msm_bus API
 * @pdata: bus scale info retrieved from DT
 */
struct wil_platform_msm {
	struct device *dev;
	uint32_t msm_bus_handle;
	struct msm_bus_scale_pdata *pdata;
};

#define KBTOB(a) (a * 1000ULL)

/**
 * wil_platform_get_pdata() - Generate bus client data from device tree
 * provided by clients.
 *
 * dev: device object
 * of_node: Device tree node to extract information from
 *
 * The function returns a valid pointer to the allocated bus-scale-pdata
 * if the vectors were correctly read from the client's device node.
 * Any error in reading or parsing the device node will return NULL
 * to the caller.
 */
static struct msm_bus_scale_pdata *wil_platform_get_pdata(
		struct device *dev,
		struct device_node *of_node)
{
	struct msm_bus_scale_pdata *pdata;
	struct msm_bus_paths *usecase;
	int i, j, ret, len;
	unsigned int num_usecases, num_paths, mem_size;
	const uint32_t *vec_arr;
	struct msm_bus_vectors *vectors;

	/* first read num_usecases and num_paths so we can calculate
	 * amount of memory to allocate
	 */
	ret = of_property_read_u32(of_node, "qcom,msm-bus,num-cases",
				   &num_usecases);
	if (ret) {
		dev_err(dev, "Error: num-usecases not found\n");
		return NULL;
	}

	ret = of_property_read_u32(of_node, "qcom,msm-bus,num-paths",
				   &num_paths);
	if (ret) {
		dev_err(dev, "Error: num_paths not found\n");
		return NULL;
	}

	/* pdata memory layout:
	 *   msm_bus_scale_pdata
	 *   msm_bus_paths[num_usecases]
	 *   msm_bus_vectors[num_usecases][num_paths]
	 */
	mem_size = sizeof(struct msm_bus_scale_pdata) +
		   sizeof(struct msm_bus_paths) * num_usecases +
		   sizeof(struct msm_bus_vectors) * num_usecases * num_paths;

	pdata = kzalloc(mem_size, GFP_KERNEL);
	if (!pdata)
		return NULL;

	ret = of_property_read_string(of_node, "qcom,msm-bus,name",
				      &pdata->name);
	if (ret) {
		dev_err(dev, "Error: Client name not found\n");
		goto err;
	}

	if (of_property_read_bool(of_node, "qcom,msm-bus,active-only")) {
		pdata->active_only = 1;
	} else {
		dev_info(dev, "active_only flag absent.\n");
		dev_info(dev, "Using dual context by default\n");
	}

	pdata->num_usecases = num_usecases;
	pdata->usecase = (struct msm_bus_paths *)(pdata + 1);

	vec_arr = of_get_property(of_node, "qcom,msm-bus,vectors-KBps", &len);
	if (vec_arr == NULL) {
		dev_err(dev, "Error: Vector array not found\n");
		goto err;
	}

	if (len != num_usecases * num_paths * sizeof(uint32_t) * 4) {
		dev_err(dev, "Error: Length-error on getting vectors\n");
		goto err;
	}

	vectors = (struct msm_bus_vectors *)(pdata->usecase + num_usecases);
	for (i = 0; i < num_usecases; i++) {
		usecase = &pdata->usecase[i];
		usecase->num_paths = num_paths;
		usecase->vectors = &vectors[i];

		for (j = 0; j < num_paths; j++) {
			int index = ((i * num_paths) + j) * 4;

			usecase->vectors[j].src = be32_to_cpu(vec_arr[index]);
			usecase->vectors[j].dst =
				be32_to_cpu(vec_arr[index + 1]);
			usecase->vectors[j].ab = (uint64_t)
				KBTOB(be32_to_cpu(vec_arr[index + 2]));
			usecase->vectors[j].ib = (uint64_t)
				KBTOB(be32_to_cpu(vec_arr[index + 3]));
		}
	}

	return pdata;

err:
	kfree(pdata);

	return NULL;
}

/* wil_platform API (callbacks) */

static int wil_platform_bus_request(void *handle,
				    uint32_t kbps /* KBytes/Sec */)
{
	int rc, i;
	struct wil_platform_msm *msm = (struct wil_platform_msm *)handle;
	int vote = 0; /* vote 0 in case requested kbps cannot be satisfied */
	struct msm_bus_paths *usecase;
	uint32_t usecase_kbps;
	uint32_t min_kbps = ~0;

	/* find the lowest usecase that is bigger than requested kbps */
	for (i = 0; i < msm->pdata->num_usecases; i++) {
		usecase = &msm->pdata->usecase[i];
		/* assume we have single path (vectors[0]). If we ever
		 * have multiple paths, need to define the behavior */
		usecase_kbps = div64_u64(usecase->vectors[0].ib, 1000);
		if (usecase_kbps >= kbps && usecase_kbps < min_kbps) {
			min_kbps = usecase_kbps;
			vote = i;
		}
	}

	rc = msm_bus_scale_client_update_request(msm->msm_bus_handle, vote);
	if (rc)
		dev_err(msm->dev, "Failed msm_bus voting. kbps=%d vote=%d, rc=%d\n",
			kbps, vote, rc);
	else
		/* TOOD: remove */
		dev_info(msm->dev, "msm_bus_scale_client_update_request succeeded. kbps=%d vote=%d\n",
			 kbps, vote);

	return rc;
}

static void wil_platform_uninit(void *handle)
{
	struct wil_platform_msm *msm = (struct wil_platform_msm *)handle;

	dev_info(msm->dev, "wil_platform_uninit\n");

	if (msm->msm_bus_handle)
		msm_bus_scale_unregister_client(msm->msm_bus_handle);

	kfree(msm->pdata);
	kfree(msm);
}

static int wil_platform_msm_bus_register(struct wil_platform_msm *msm,
					 struct device_node *node)
{
	msm->pdata = wil_platform_get_pdata(msm->dev, node);
	if (!msm->pdata) {
		dev_err(msm->dev, "Failed getting DT info\n");
		return -EINVAL;
	}

	msm->msm_bus_handle = msm_bus_scale_register_client(msm->pdata);
	if (!msm->msm_bus_handle) {
		dev_err(msm->dev, "Failed msm_bus registration\n");
		return -EINVAL;
	}

	dev_info(msm->dev, "msm_bus registration succeeded! handle 0x%x\n",
		 msm->msm_bus_handle);

	return 0;
}

/**
 * wil_platform_msm_init() - wil6210 msm platform module init
 *
 * The function must be called before all other functions in this module.
 * It returns a handle which is used with the rest of the API
 *
 */
void *wil_platform_msm_init(struct device *dev, struct wil_platform_ops *ops)
{
	struct device_node *of_node;
	struct wil_platform_msm *msm;
	int rc;

	of_node = of_find_compatible_node(NULL, NULL, "qcom,wil6210");
	if (!of_node) {
		/* this could mean non-msm platform */
		dev_err(dev, "DT node not found\n");
		return NULL;
	}

	msm = kzalloc(sizeof(*msm), GFP_KERNEL);
	if (!msm)
		return NULL;

	msm->dev = dev;

	/* register with msm_bus module for scaling requests */
	rc = wil_platform_msm_bus_register(msm, of_node);
	if (rc)
		goto cleanup;

	memset(ops, 0, sizeof(*ops));
	ops->bus_request = wil_platform_bus_request;
	ops->uninit = wil_platform_uninit;

	return (void *)msm;

cleanup:
	kfree(msm);
	return NULL;
}