summaryrefslogtreecommitdiff
path: root/drivers/hwmon/mc34vr500.c
blob: 0c8fd3fce83ef0822d7952797b97fddb17cc9365 (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
258
259
260
261
262
263
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * An hwmon driver for the NXP MC34VR500 PMIC
 *
 * Author: Mario Kicherer <dev@kicherer.org>
 */

#include <linux/bits.h>
#include <linux/dev_printk.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/hwmon.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/irqreturn.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regmap.h>

#define MC34VR500_I2C_ADDR		0x08
#define MC34VR500_DEVICEID_VALUE	0x14

/* INTSENSE0 */
#define ENS_BIT		BIT(0)
#define LOWVINS_BIT	BIT(1)
#define THERM110S_BIT	BIT(2)
#define THERM120S_BIT	BIT(3)
#define THERM125S_BIT	BIT(4)
#define THERM130S_BIT	BIT(5)

#define MC34VR500_DEVICEID	0x00

#define MC34VR500_SILICONREVID	0x03
#define MC34VR500_FABID		0x04
#define MC34VR500_INTSTAT0	0x05
#define MC34VR500_INTMASK0	0x06
#define MC34VR500_INTSENSE0	0x07

struct mc34vr500_data {
	struct device *hwmon_dev;
	struct regmap *regmap;
};

static irqreturn_t mc34vr500_process_interrupt(int irq, void *userdata)
{
	struct mc34vr500_data *data = (struct mc34vr500_data *)userdata;
	unsigned int reg;
	int ret;

	ret = regmap_read(data->regmap, MC34VR500_INTSTAT0, &reg);
	if (ret < 0)
		return IRQ_HANDLED;

	if (reg) {
		if (reg & LOWVINS_BIT)
			hwmon_notify_event(data->hwmon_dev, hwmon_in,
					   hwmon_in_min_alarm, 0);

		if (reg & THERM110S_BIT)
			hwmon_notify_event(data->hwmon_dev, hwmon_temp,
					   hwmon_temp_max_alarm, 0);

		if (reg & THERM120S_BIT)
			hwmon_notify_event(data->hwmon_dev, hwmon_temp,
					   hwmon_temp_crit_alarm, 0);

		if (reg & THERM130S_BIT)
			hwmon_notify_event(data->hwmon_dev, hwmon_temp,
					   hwmon_temp_emergency_alarm, 0);

		/* write 1 to clear */
		regmap_write(data->regmap, MC34VR500_INTSTAT0, LOWVINS_BIT |
			     THERM110S_BIT | THERM120S_BIT | THERM130S_BIT);
	}

	return IRQ_HANDLED;
}

static umode_t mc34vr500_is_visible(const void *data,
				    enum hwmon_sensor_types type,
				    u32 attr, int channel)
{
	switch (attr) {
	case hwmon_in_min_alarm:
	case hwmon_temp_max_alarm:
	case hwmon_temp_crit_alarm:
	case hwmon_temp_emergency_alarm:
		return 0444;
	default:
		break;
	}

	return 0;
}

static int mc34vr500_alarm_read(struct mc34vr500_data *data, int index,
				long *val)
{
	unsigned int reg;
	int ret;

	ret = regmap_read(data->regmap, MC34VR500_INTSENSE0, &reg);
	if (ret < 0)
		return ret;

	*val = !!(reg & index);

	return 0;
}

static int mc34vr500_read(struct device *dev, enum hwmon_sensor_types type,
			  u32 attr, int channel, long *val)
{
	struct mc34vr500_data *data = dev_get_drvdata(dev);

	switch (type) {
	case hwmon_in:
		switch (attr) {
		case hwmon_in_min_alarm:
			return mc34vr500_alarm_read(data, LOWVINS_BIT, val);
		default:
			return -EOPNOTSUPP;
		}
	case hwmon_temp:
		switch (attr) {
		case hwmon_temp_max_alarm:
			return mc34vr500_alarm_read(data, THERM110S_BIT, val);
		case hwmon_temp_crit_alarm:
			return mc34vr500_alarm_read(data, THERM120S_BIT, val);
		case hwmon_temp_emergency_alarm:
			return mc34vr500_alarm_read(data, THERM130S_BIT, val);
		default:
			return -EOPNOTSUPP;
		}
	default:
		return -EOPNOTSUPP;
	}
}

static const struct hwmon_channel_info * const mc34vr500_info[] = {
	HWMON_CHANNEL_INFO(in, HWMON_I_MIN_ALARM),
	HWMON_CHANNEL_INFO(temp, HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM
			   | HWMON_T_EMERGENCY_ALARM),
	NULL,
};

static const struct hwmon_ops mc34vr500_hwmon_ops = {
	.is_visible = mc34vr500_is_visible,
	.read = mc34vr500_read,
};

static const struct hwmon_chip_info mc34vr500_chip_info = {
	.ops = &mc34vr500_hwmon_ops,
	.info = mc34vr500_info,
};

static const struct regmap_config mc34vr500_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = MC34VR500_INTSENSE0,
};

static int mc34vr500_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	struct mc34vr500_data *data;
	struct device *hwmon_dev;
	int ret;
	unsigned int reg, revid, fabid;
	struct regmap *regmap;

	regmap = devm_regmap_init_i2c(client, &mc34vr500_regmap_config);
	if (IS_ERR(regmap))
		return PTR_ERR(regmap);

	data = devm_kzalloc(dev, sizeof(struct mc34vr500_data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	data->regmap = regmap;

	ret = regmap_read(regmap, MC34VR500_DEVICEID, &reg);
	if (ret < 0)
		return ret;

	if (reg != MC34VR500_DEVICEID_VALUE)
		return -ENODEV;

	ret = regmap_read(regmap, MC34VR500_SILICONREVID, &revid);
	if (ret < 0)
		return ret;

	ret = regmap_read(regmap, MC34VR500_FABID, &fabid);
	if (ret < 0)
		return ret;

	dev_dbg(dev, "mc34vr500: revid 0x%x fabid 0x%x\n", revid, fabid);

	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
							 data,
							 &mc34vr500_chip_info,
							 NULL);
	if (IS_ERR(hwmon_dev))
		return PTR_ERR(hwmon_dev);

	data->hwmon_dev = hwmon_dev;

	if (client->irq) {
		ret = devm_request_threaded_irq(dev, client->irq, NULL,
						mc34vr500_process_interrupt,
						IRQF_TRIGGER_RISING |
						IRQF_ONESHOT |
						IRQF_SHARED,
						dev_name(dev), data);
		if (ret)
			return ret;

		/* write 1 to clear interrupts */
		ret = regmap_write(regmap, MC34VR500_INTSTAT0, LOWVINS_BIT |
				   THERM110S_BIT | THERM120S_BIT |
				   THERM130S_BIT);
		if (ret)
			return ret;

		/* unmask interrupts */
		ret = regmap_write(regmap, MC34VR500_INTMASK0,
				   (unsigned int) ~(LOWVINS_BIT | THERM110S_BIT |
						    THERM120S_BIT | THERM130S_BIT));
		if (ret)
			return ret;
	}

	return 0;
}

static const struct i2c_device_id mc34vr500_id[] = {
	{ "mc34vr500", 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, mc34vr500_id);

static const struct of_device_id __maybe_unused mc34vr500_of_match[] = {
	{ .compatible = "nxp,mc34vr500" },
	{ },
};
MODULE_DEVICE_TABLE(of, mc34vr500_of_match);

static struct i2c_driver mc34vr500_driver = {
	.driver = {
		   .name = "mc34vr500",
		   .of_match_table = of_match_ptr(mc34vr500_of_match),
		    },
	.probe = mc34vr500_probe,
	.id_table = mc34vr500_id,
};

module_i2c_driver(mc34vr500_driver);

MODULE_AUTHOR("Mario Kicherer <dev@kicherer.org>");

MODULE_DESCRIPTION("MC34VR500 driver");
MODULE_LICENSE("GPL");