summaryrefslogtreecommitdiffstats
path: root/drivers/misc/smpro-misc.c
blob: 6c427141e51b4121267c03818d1df5d5a299a768 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Ampere Computing SoC's SMpro Misc Driver
 *
 * Copyright (c) 2022, Ampere Computing LLC
 */
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>

/* Boot Stage/Progress Registers */
#define BOOTSTAGE	0xB0
#define BOOTSTAGE_LO	0xB1
#define CUR_BOOTSTAGE	0xB2
#define BOOTSTAGE_HI	0xB3

/* SOC State Registers */
#define SOC_POWER_LIMIT		0xE5

struct smpro_misc {
	struct regmap *regmap;
};

static ssize_t boot_progress_show(struct device *dev, struct device_attribute *da, char *buf)
{
	struct smpro_misc *misc = dev_get_drvdata(dev);
	u16 boot_progress[3] = { 0 };
	u32 bootstage;
	u8 boot_stage;
	u8 cur_stage;
	u32 reg_lo;
	u32 reg;
	int ret;

	/* Read current boot stage */
	ret = regmap_read(misc->regmap, CUR_BOOTSTAGE, &reg);
	if (ret)
		return ret;

	cur_stage = reg & 0xff;

	ret = regmap_read(misc->regmap, BOOTSTAGE, &bootstage);
	if (ret)
		return ret;

	boot_stage = (bootstage >> 8) & 0xff;

	if (boot_stage > cur_stage)
		return -EINVAL;

	ret = regmap_read(misc->regmap,	BOOTSTAGE_LO, &reg_lo);
	if (!ret)
		ret = regmap_read(misc->regmap, BOOTSTAGE_HI, &reg);
	if (ret)
		return ret;

	/* Firmware to report new boot stage next time */
	if (boot_stage < cur_stage) {
		ret = regmap_write(misc->regmap, BOOTSTAGE, ((bootstage & 0xff00) | 0x1));
		if (ret)
			return ret;
	}

	boot_progress[0] = bootstage;
	boot_progress[1] = swab16(reg);
	boot_progress[2] = swab16(reg_lo);

	return sysfs_emit(buf, "%*phN\n", (int)sizeof(boot_progress), boot_progress);
}

static DEVICE_ATTR_RO(boot_progress);

static ssize_t soc_power_limit_show(struct device *dev, struct device_attribute *da, char *buf)
{
	struct smpro_misc *misc = dev_get_drvdata(dev);
	unsigned int value;
	int ret;

	ret = regmap_read(misc->regmap, SOC_POWER_LIMIT, &value);
	if (ret)
		return ret;

	return sysfs_emit(buf, "%d\n", value);
}

static ssize_t soc_power_limit_store(struct device *dev, struct device_attribute *da,
				     const char *buf, size_t count)
{
	struct smpro_misc *misc = dev_get_drvdata(dev);
	unsigned long val;
	s32 ret;

	ret = kstrtoul(buf, 0, &val);
	if (ret)
		return ret;

	ret = regmap_write(misc->regmap, SOC_POWER_LIMIT, (unsigned int)val);
	if (ret)
		return -EPROTO;

	return count;
}

static DEVICE_ATTR_RW(soc_power_limit);

static struct attribute *smpro_misc_attrs[] = {
	&dev_attr_boot_progress.attr,
	&dev_attr_soc_power_limit.attr,
	NULL
};

ATTRIBUTE_GROUPS(smpro_misc);

static int smpro_misc_probe(struct platform_device *pdev)
{
	struct smpro_misc *misc;

	misc = devm_kzalloc(&pdev->dev, sizeof(struct smpro_misc), GFP_KERNEL);
	if (!misc)
		return -ENOMEM;

	platform_set_drvdata(pdev, misc);

	misc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!misc->regmap)
		return -ENODEV;

	return 0;
}

static struct platform_driver smpro_misc_driver = {
	.probe		= smpro_misc_probe,
	.driver = {
		.name	= "smpro-misc",
		.dev_groups = smpro_misc_groups,
	},
};

module_platform_driver(smpro_misc_driver);

MODULE_AUTHOR("Tung Nguyen <tungnguyen@os.amperecomputing.com>");
MODULE_AUTHOR("Quan Nguyen <quan@os.amperecomputing.com>");
MODULE_DESCRIPTION("Ampere Altra SMpro Misc driver");
MODULE_LICENSE("GPL");