summaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/ultra45_env.c
blob: 27a62711e0a6170c4d32fa1c608660af761b9a48 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* ultra45_env.c: Driver for Ultra45 PIC16F747 environmental monitor.
 *
 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/of_device.h>
#include <linux/io.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>

#define DRV_MODULE_VERSION	"0.1"

MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
MODULE_DESCRIPTION("Ultra45 environmental monitor driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_MODULE_VERSION);

/* PIC device registers */
#define REG_CMD		0x00UL
#define  REG_CMD_RESET	0x80
#define  REG_CMD_ESTAR	0x01
#define REG_STAT	0x01UL
#define  REG_STAT_FWVER	0xf0
#define  REG_STAT_TGOOD	0x08
#define  REG_STAT_STALE	0x04
#define  REG_STAT_BUSY	0x02
#define  REG_STAT_FAULT	0x01
#define REG_DATA	0x40UL
#define REG_ADDR	0x41UL
#define REG_SIZE	0x42UL

/* Registers accessed indirectly via REG_DATA/REG_ADDR */
#define IREG_FAN0		0x00
#define IREG_FAN1		0x01
#define IREG_FAN2		0x02
#define IREG_FAN3		0x03
#define IREG_FAN4		0x04
#define IREG_FAN5		0x05
#define IREG_LCL_TEMP		0x06
#define IREG_RMT1_TEMP		0x07
#define IREG_RMT2_TEMP		0x08
#define IREG_RMT3_TEMP		0x09
#define IREG_LM95221_TEMP	0x0a
#define IREG_FIRE_TEMP		0x0b
#define IREG_LSI1064_TEMP	0x0c
#define IREG_FRONT_TEMP		0x0d
#define IREG_FAN_STAT		0x0e
#define IREG_VCORE0		0x0f
#define IREG_VCORE1		0x10
#define IREG_VMEM0		0x11
#define IREG_VMEM1		0x12
#define IREG_PSU_TEMP		0x13

struct env {
	void __iomem	*regs;
	spinlock_t	lock;

	struct device	*hwmon_dev;
};

static u8 env_read(struct env *p, u8 ireg)
{
	u8 ret;

	spin_lock(&p->lock);
	writeb(ireg, p->regs + REG_ADDR);
	ret = readb(p->regs + REG_DATA);
	spin_unlock(&p->lock);

	return ret;
}

static void env_write(struct env *p, u8 ireg, u8 val)
{
	spin_lock(&p->lock);
	writeb(ireg, p->regs + REG_ADDR);
	writeb(val, p->regs + REG_DATA);
	spin_unlock(&p->lock);
}

/* There seems to be a adr7462 providing these values, thus a lot
 * of these calculations are borrowed from the adt7470 driver.
 */
#define FAN_PERIOD_TO_RPM(x)	((90000 * 60) / (x))
#define FAN_RPM_TO_PERIOD	FAN_PERIOD_TO_RPM
#define FAN_PERIOD_INVALID	(0xff << 8)
#define FAN_DATA_VALID(x)	((x) && (x) != FAN_PERIOD_INVALID)

static ssize_t show_fan_speed(struct device *dev, struct device_attribute *attr, char *buf)
{
	int fan_nr = to_sensor_dev_attr(attr)->index;
	struct env *p = dev_get_drvdata(dev);
	int rpm, period;
	u8 val;

	val = env_read(p, IREG_FAN0 + fan_nr);
	period = (int) val << 8;
	if (FAN_DATA_VALID(period))
		rpm = FAN_PERIOD_TO_RPM(period);
	else
		rpm = 0;

	return sprintf(buf, "%d\n", rpm);
}

static ssize_t set_fan_speed(struct device *dev, struct device_attribute *attr,
			     const char *buf, size_t count)
{
	int fan_nr = to_sensor_dev_attr(attr)->index;
	int rpm = simple_strtol(buf, NULL, 10);
	struct env *p = dev_get_drvdata(dev);
	int period;
	u8 val;

	if (!rpm)
		return -EINVAL;

	period = FAN_RPM_TO_PERIOD(rpm);
	val = period >> 8;
	env_write(p, IREG_FAN0 + fan_nr, val);

	return count;
}

static ssize_t show_fan_fault(struct device *dev, struct device_attribute *attr, char *buf)
{
	int fan_nr = to_sensor_dev_attr(attr)->index;
	struct env *p = dev_get_drvdata(dev);
	u8 val = env_read(p, IREG_FAN_STAT);
	return sprintf(buf, "%d\n", (val & (1 << fan_nr)) ? 1 : 0);
}

#define fan(index)							\
static SENSOR_DEVICE_ATTR(fan##index##_speed, S_IRUGO | S_IWUSR,	\
		show_fan_speed, set_fan_speed, index);			\
static SENSOR_DEVICE_ATTR(fan##index##_fault, S_IRUGO,			\
		show_fan_fault, NULL, index)

fan(0);
fan(1);
fan(2);
fan(3);
fan(4);

static SENSOR_DEVICE_ATTR(psu_fan_fault, S_IRUGO, show_fan_fault, NULL, 6);

static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
{
	int temp_nr = to_sensor_dev_attr(attr)->index;
	struct env *p = dev_get_drvdata(dev);
	s8 val;

	val = env_read(p, IREG_LCL_TEMP + temp_nr);
	return sprintf(buf, "%d\n", ((int) val) - 64);
}

static SENSOR_DEVICE_ATTR(adt7462_local_temp, S_IRUGO, show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(cpu0_temp, S_IRUGO, show_temp, NULL, 1);
static SENSOR_DEVICE_ATTR(cpu1_temp, S_IRUGO, show_temp, NULL, 2);
static SENSOR_DEVICE_ATTR(motherboard_temp, S_IRUGO, show_temp, NULL, 3);
static SENSOR_DEVICE_ATTR(lm95221_local_temp, S_IRUGO, show_temp, NULL, 4);
static SENSOR_DEVICE_ATTR(fire_temp, S_IRUGO, show_temp, NULL, 5);
static SENSOR_DEVICE_ATTR(lsi1064_local_temp, S_IRUGO, show_temp, NULL, 6);
static SENSOR_DEVICE_ATTR(front_panel_temp, S_IRUGO, show_temp, NULL, 7);
static SENSOR_DEVICE_ATTR(psu_temp, S_IRUGO, show_temp, NULL, 13);

static ssize_t show_stat_bit(struct device *dev, struct device_attribute *attr, char *buf)
{
	int index = to_sensor_dev_attr(attr)->index;
	struct env *p = dev_get_drvdata(dev);
	u8 val;

	val = readb(p->regs + REG_STAT);
	return sprintf(buf, "%d\n", (val & (1 << index)) ? 1 : 0);
}

static SENSOR_DEVICE_ATTR(fan_failure, S_IRUGO, show_stat_bit, NULL, 0);
static SENSOR_DEVICE_ATTR(env_bus_busy, S_IRUGO, show_stat_bit, NULL, 1);
static SENSOR_DEVICE_ATTR(env_data_stale, S_IRUGO, show_stat_bit, NULL, 2);
static SENSOR_DEVICE_ATTR(tpm_self_test_passed, S_IRUGO, show_stat_bit, NULL, 3);

static ssize_t show_fwver(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct env *p = dev_get_drvdata(dev);
	u8 val;

	val = readb(p->regs + REG_STAT);
	return sprintf(buf, "%d\n", val >> 4);
}

static SENSOR_DEVICE_ATTR(firmware_version, S_IRUGO, show_fwver, NULL, 0);

static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "ultra45\n");
}

static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);

static struct attribute *env_attributes[] = {
	&sensor_dev_attr_fan0_speed.dev_attr.attr,
	&sensor_dev_attr_fan0_fault.dev_attr.attr,
	&sensor_dev_attr_fan1_speed.dev_attr.attr,
	&sensor_dev_attr_fan1_fault.dev_attr.attr,
	&sensor_dev_attr_fan2_speed.dev_attr.attr,
	&sensor_dev_attr_fan2_fault.dev_attr.attr,
	&sensor_dev_attr_fan3_speed.dev_attr.attr,
	&sensor_dev_attr_fan3_fault.dev_attr.attr,
	&sensor_dev_attr_fan4_speed.dev_attr.attr,
	&sensor_dev_attr_fan4_fault.dev_attr.attr,
	&sensor_dev_attr_psu_fan_fault.dev_attr.attr,
	&sensor_dev_attr_adt7462_local_temp.dev_attr.attr,
	&sensor_dev_attr_cpu0_temp.dev_attr.attr,
	&sensor_dev_attr_cpu1_temp.dev_attr.attr,
	&sensor_dev_attr_motherboard_temp.dev_attr.attr,
	&sensor_dev_attr_lm95221_local_temp.dev_attr.attr,
	&sensor_dev_attr_fire_temp.dev_attr.attr,
	&sensor_dev_attr_lsi1064_local_temp.dev_attr.attr,
	&sensor_dev_attr_front_panel_temp.dev_attr.attr,
	&sensor_dev_attr_psu_temp.dev_attr.attr,
	&sensor_dev_attr_fan_failure.dev_attr.attr,
	&sensor_dev_attr_env_bus_busy.dev_attr.attr,
	&sensor_dev_attr_env_data_stale.dev_attr.attr,
	&sensor_dev_attr_tpm_self_test_passed.dev_attr.attr,
	&sensor_dev_attr_firmware_version.dev_attr.attr,
	&sensor_dev_attr_name.dev_attr.attr,
	NULL,
};

static const struct attribute_group env_group = {
	.attrs = env_attributes,
};

static int __devinit env_probe(struct platform_device *op)
{
	struct env *p = kzalloc(sizeof(*p), GFP_KERNEL);
	int err = -ENOMEM;

	if (!p)
		goto out;

	spin_lock_init(&p->lock);

	p->regs = of_ioremap(&op->resource[0], 0, REG_SIZE, "pic16f747");
	if (!p->regs)
		goto out_free;

	err = sysfs_create_group(&op->dev.kobj, &env_group);
	if (err)
		goto out_iounmap;

	p->hwmon_dev = hwmon_device_register(&op->dev);
	if (IS_ERR(p->hwmon_dev)) {
		err = PTR_ERR(p->hwmon_dev);
		goto out_sysfs_remove_group;
	}

	platform_set_drvdata(op, p);
	err = 0;

out:
	return err;

out_sysfs_remove_group:
	sysfs_remove_group(&op->dev.kobj, &env_group);

out_iounmap:
	of_iounmap(&op->resource[0], p->regs, REG_SIZE);

out_free:
	kfree(p);
	goto out;
}

static int __devexit env_remove(struct platform_device *op)
{
	struct env *p = platform_get_drvdata(op);

	if (p) {
		sysfs_remove_group(&op->dev.kobj, &env_group);
		hwmon_device_unregister(p->hwmon_dev);
		of_iounmap(&op->resource[0], p->regs, REG_SIZE);
		kfree(p);
	}

	return 0;
}

static const struct of_device_id env_match[] = {
	{
		.name = "env-monitor",
		.compatible = "SUNW,ebus-pic16f747-env",
	},
	{},
};
MODULE_DEVICE_TABLE(of, env_match);

static struct platform_driver env_driver = {
	.driver = {
		.name = "ultra45_env",
		.owner = THIS_MODULE,
		.of_match_table = env_match,
	},
	.probe		= env_probe,
	.remove		= __devexit_p(env_remove),
};

static int __init env_init(void)
{
	return platform_driver_register(&env_driver);
}

static void __exit env_exit(void)
{
	platform_driver_unregister(&env_driver);
}

module_init(env_init);
module_exit(env_exit);