summaryrefslogtreecommitdiff
path: root/modemsmem/modemsmem.c
blob: ac2b15a7ce9624852c1846f1da4f24267a9601e0 (plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2018 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/of.h>
#include <linux/ctype.h>
#include <linux/soc/qcom/smem.h>
#include <soc/qcom/socinfo.h>
#include <linux/io.h>
#include "modemsmem.h"

#define BOOTMODE_LENGTH			20

#define DEVICE_TREE_CONFIG_PATH		"/chosen/config"
#define DEVICE_TREE_PLAT_PATH		"/chosen/plat"
#define FTM_ON				"ftm_on"
#define FTM_OFF				"ftm_off"

static char bootmode[BOOTMODE_LENGTH];
static const char * const factory_bootmodes[] = {
	"factory",
	"ffbm-00",
	"ffbm-01"
};

static struct modem_smem_type *modem_smem;

static void get_bootmode(void)
{
	unsigned int size;
	unsigned char *dt_bootmode = NULL;
	struct device_node *dtnp = NULL;

	dtnp = of_find_node_by_path(DEVICE_TREE_CONFIG_PATH);
	if (!dtnp)
		pr_err("[SMEM]: Invalid CDT DT node\n");
	else {
		dt_bootmode = (unsigned char *)
				of_get_property(dtnp, "bootmode", &size);
	}
	if (dt_bootmode)
		strlcpy(bootmode, dt_bootmode, BOOTMODE_LENGTH);
}

static bool is_factory_bootmode(void)
{
	int i = 0;

	get_bootmode();
	for (; i < ARRAY_SIZE(factory_bootmodes); i++)
		if (!strncmp(factory_bootmodes[i], bootmode, sizeof(bootmode)))
			return true;
	return false;
}

static ssize_t modem_smem_show(struct device *d,
			struct device_attribute *attr,
			char *buf)
{
	if (IS_ERR(modem_smem)) {
		dev_err(d, "Get invalid modem smem pointer\n");
		return snprintf(buf, PAGE_SIZE, "Get invalid modem smem\n");
	}

	return snprintf(buf, PAGE_SIZE,
			"version:0x%x\n"
			"modem_flag:0x%x\n"
			"platform:0x%x\n"
			"product:0x%x\n"
			"stage:0x%x\n"
			"major:0x%x\n"
			"minor:0x%x\n"
			"variant:0x%x\n"
			"modem_sku:0x%x\n"
			"efs_magic:0x%x\n"
			"ftm_magic:0x%x\n"
			"dsds_magic:0x%x\n",
			modem_smem->version,
			modem_smem->modem_flag,
			modem_smem->platform,
			modem_smem->product,
			modem_smem->stage,
			modem_smem->major,
			modem_smem->minor,
			modem_smem->variant,
			modem_smem->modem_sku,
			modem_smem->efs_magic,
			modem_smem->ftm_magic,
			modem_smem->dsds_magic);
}

static ssize_t modem_smem_store(struct device *d,
			struct device_attribute *attr,
			const char *buf,
			size_t count)
{
	if (IS_ERR(modem_smem)) {
		dev_err(d, "Get invalid modem smem pointer failed\n");
		return count;
	}

	if (!is_factory_bootmode()) {
		dev_err(d, "The action is not allowed in normal bootmode\n");
		return count;
	}

	if (!strncmp(buf, FTM_ON, sizeof(FTM_ON) - 1))
		modem_smem_set_u32(modem_smem, ftm_magic, MODEM_FTM_MAGIC);
	else if (!strncmp(buf, FTM_OFF, sizeof(FTM_OFF) - 1))
		modem_smem_set_u32(modem_smem, ftm_magic, 0);
	else {
		dev_err(d, "Unsupport action %s\n", buf);
		return count;
	}
	dev_info(d, "Set %s mode via sysfs\n", buf);

	return count;
}

static DEVICE_ATTR_RW(modem_smem);
static struct attribute *modem_smem_attributes[] = {
	&dev_attr_modem_smem.attr,
	NULL
};

static const struct attribute_group modem_smem_group = {
	.name  = "modemsmem",
	.attrs = modem_smem_attributes,
};

static int modem_smem_probe(struct platform_device *pdev)
{
	int ret = -1;
	size_t size;
	struct device_node *np = NULL;
	struct device *dev = NULL;
	struct device_node *dtnp = NULL;
	int len = 0;
	u8 buff[8 + 1];
	u32 val = 0;
	char *prop = NULL;
	int prop_size;

	pr_debug("[SMEM] %s: Enter probe\n", __func__);

	if (!pdev) {
		pr_err("[SMEM] %s: Invalid pdev\n", __func__);
		return -ENODEV;
	}

	np = pdev->dev.of_node;
	if (!np) {
		pr_err("[SMEM] %s: Invalid DT node\n", __func__);
		return -EINVAL;
	}

	dev = &pdev->dev;
	if (dev == NULL) {
		pr_err("[SMEM] %s: Invalid dev\n", __func__);
		return -EINVAL;
	}
	platform_set_drvdata(pdev, dev);

	/* Allocate with SMEM channel */
	ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_ID_VENDOR0,
			sizeof(struct modem_smem_type));
	if (ret && ret != -EEXIST) {
		dev_err(dev, "smem alloc fail\n");
		return -ENOMEM;
	}

	modem_smem = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_ID_VENDOR0, &size);
	if (IS_ERR(modem_smem) || size != sizeof(struct modem_smem_type)) {
		dev_err(dev, "Get modem_smem pointer failed\n");
		return -ENOMEM;
	}

	/* Initialize smem with zeros */
	memset_io(modem_smem, 0, sizeof(*modem_smem));

	/* Setup modem SMEM parameters */
	modem_smem_set_u32(modem_smem, version, MODEM_SMEM_VERSION);

	dtnp = of_find_node_by_path(DEVICE_TREE_PLAT_PATH);

	if (!dtnp)
		pr_err("[SMEM]: Invalid CDT DT node\n");
	else {
		ret = 0;
		ret += modem_smem_set_by_property(dtnp, modem_smem, platform);
		ret += modem_smem_set_by_property(dtnp, modem_smem, product);
		ret += modem_smem_set_by_property(dtnp, modem_smem, stage);
		ret += modem_smem_set_by_property(dtnp, modem_smem, major);
		ret += modem_smem_set_by_property(dtnp, modem_smem, minor);
		ret += modem_smem_set_by_property(dtnp, modem_smem, variant);
		ret += modem_smem_set_by_property(dtnp, modem_smem, modem_sku);

		if(ret) {
			dev_err(dev, "Invalid CDT DT value\n");
			return -EINVAL;
		}
	}

	do {
		dtnp = of_find_node_by_path(DEVICE_TREE_CONFIG_PATH);
		if (dtnp && !of_find_property(dtnp, "modem_flag", &len)) {
			dev_info(dev, "Get modem_flag node failed\n");
			break;
		}
		if (len > ARRAY_SIZE(buff) || len < 2) {
			dev_err(dev, "Invalid modem_flag length %d", len);
			break;
		}
		ret = of_property_read_u8_array(dtnp, "modem_flag", buff, len);
		if (ret) {
			dev_err(dev, "Get modem_flag failed %d", ret);
			break;
		}
		buff[len - 1] = '\0';
		ret = kstrtou32(buff, 16, &val);
		if (ret) {
			dev_err(dev, "Set modem_flag failed %d\n", ret);
			break;
		}
		modem_smem_set_u32(modem_smem, modem_flag, val);
	} while (0);

	if (is_factory_bootmode()) {
		modem_smem_set_u32(modem_smem, ftm_magic, MODEM_FTM_MAGIC);
		dev_info(dev, "Set FTM mode due to %s\n", bootmode);
	}

	if (dtnp) {
		prop = (char *) of_get_property(dtnp, "dsds", &prop_size);
		if (prop && !strncmp(prop, "1", sizeof("1"))) {
			modem_smem_set_u32(modem_smem, dsds_magic,
					MODEM_DSDS_MAGIC);
			dev_info(dev, "Set DSDS mode\n");
		}
	}

	/* Create sysfs */
	ret = sysfs_create_group(&pdev->dev.kobj, &modem_smem_group);
	if (ret)
		dev_err(dev, "Failed to create sysfs\n");

	dev_dbg(dev, "End probe\n");
	return 0;
}

static const struct of_device_id modem_smem_of_match[] = {
	{.compatible = "modemsmem",},
    {}
};
MODULE_DEVICE_TABLE(of, modem_smem_of_match);

static struct platform_driver modem_smem_driver = {
	.probe = modem_smem_probe,
	.driver = {
		.name = "modemsmem",
		.owner = THIS_MODULE,
		.of_match_table = modem_smem_of_match,
	}
};

static int __init modem_smem_init(void)
{
	int ret = -1;

	pr_debug("[SMEM] %s: Enter\n", __func__);

	ret = platform_driver_register(&modem_smem_driver);
	if (ret < 0)
		pr_err("[SMEM] %s: platform_driver register fail. ret:%d\n",
			__func__, ret);

	return ret;
}

static void __exit modem_smem_exit(void)
{
	platform_driver_unregister(&modem_smem_driver);
}

module_init(modem_smem_init);
module_exit(modem_smem_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Modem SMEM Driver");