blob: c315c59f61fe3fa86b982b19eec3315de0686ec6 [file] [log] [blame]
Guenter Roecka80b10c2012-01-14 22:42:41 -08001/*
2 * ultra45_env.c: Driver for Ultra45 PIC16F747 environmental monitor.
David S. Millere0418082008-08-24 20:59:49 -07003 *
4 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
5 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/slab.h>
Paul Gortmaker31a1a152011-07-18 16:05:10 -040010#include <linux/module.h>
David S. Millere0418082008-08-24 20:59:49 -070011#include <linux/of_device.h>
12#include <linux/io.h>
13#include <linux/hwmon.h>
14#include <linux/hwmon-sysfs.h>
15
16#define DRV_MODULE_VERSION "0.1"
17
18MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
19MODULE_DESCRIPTION("Ultra45 environmental monitor driver");
20MODULE_LICENSE("GPL");
21MODULE_VERSION(DRV_MODULE_VERSION);
22
23/* PIC device registers */
24#define REG_CMD 0x00UL
25#define REG_CMD_RESET 0x80
26#define REG_CMD_ESTAR 0x01
27#define REG_STAT 0x01UL
28#define REG_STAT_FWVER 0xf0
29#define REG_STAT_TGOOD 0x08
30#define REG_STAT_STALE 0x04
31#define REG_STAT_BUSY 0x02
32#define REG_STAT_FAULT 0x01
33#define REG_DATA 0x40UL
34#define REG_ADDR 0x41UL
35#define REG_SIZE 0x42UL
36
37/* Registers accessed indirectly via REG_DATA/REG_ADDR */
38#define IREG_FAN0 0x00
39#define IREG_FAN1 0x01
40#define IREG_FAN2 0x02
41#define IREG_FAN3 0x03
42#define IREG_FAN4 0x04
43#define IREG_FAN5 0x05
44#define IREG_LCL_TEMP 0x06
45#define IREG_RMT1_TEMP 0x07
46#define IREG_RMT2_TEMP 0x08
47#define IREG_RMT3_TEMP 0x09
48#define IREG_LM95221_TEMP 0x0a
49#define IREG_FIRE_TEMP 0x0b
50#define IREG_LSI1064_TEMP 0x0c
51#define IREG_FRONT_TEMP 0x0d
52#define IREG_FAN_STAT 0x0e
53#define IREG_VCORE0 0x0f
54#define IREG_VCORE1 0x10
55#define IREG_VMEM0 0x11
56#define IREG_VMEM1 0x12
57#define IREG_PSU_TEMP 0x13
58
59struct env {
60 void __iomem *regs;
61 spinlock_t lock;
62
63 struct device *hwmon_dev;
64};
65
66static u8 env_read(struct env *p, u8 ireg)
67{
68 u8 ret;
69
70 spin_lock(&p->lock);
71 writeb(ireg, p->regs + REG_ADDR);
72 ret = readb(p->regs + REG_DATA);
73 spin_unlock(&p->lock);
74
75 return ret;
76}
77
78static void env_write(struct env *p, u8 ireg, u8 val)
79{
80 spin_lock(&p->lock);
81 writeb(ireg, p->regs + REG_ADDR);
82 writeb(val, p->regs + REG_DATA);
83 spin_unlock(&p->lock);
84}
85
Guenter Roecka80b10c2012-01-14 22:42:41 -080086/*
87 * There seems to be a adr7462 providing these values, thus a lot
David S. Millere0418082008-08-24 20:59:49 -070088 * of these calculations are borrowed from the adt7470 driver.
89 */
90#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
91#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
92#define FAN_PERIOD_INVALID (0xff << 8)
93#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
94
Guenter Roecka80b10c2012-01-14 22:42:41 -080095static ssize_t show_fan_speed(struct device *dev, struct device_attribute *attr,
96 char *buf)
David S. Millere0418082008-08-24 20:59:49 -070097{
98 int fan_nr = to_sensor_dev_attr(attr)->index;
99 struct env *p = dev_get_drvdata(dev);
100 int rpm, period;
101 u8 val;
102
103 val = env_read(p, IREG_FAN0 + fan_nr);
104 period = (int) val << 8;
105 if (FAN_DATA_VALID(period))
106 rpm = FAN_PERIOD_TO_RPM(period);
107 else
108 rpm = 0;
109
110 return sprintf(buf, "%d\n", rpm);
111}
112
113static ssize_t set_fan_speed(struct device *dev, struct device_attribute *attr,
114 const char *buf, size_t count)
115{
116 int fan_nr = to_sensor_dev_attr(attr)->index;
Guenter Roecka80b10c2012-01-14 22:42:41 -0800117 unsigned long rpm;
David S. Millere0418082008-08-24 20:59:49 -0700118 struct env *p = dev_get_drvdata(dev);
119 int period;
120 u8 val;
Guenter Roecka80b10c2012-01-14 22:42:41 -0800121 int err;
122
123 err = kstrtoul(buf, 10, &rpm);
124 if (err)
125 return err;
David S. Millere0418082008-08-24 20:59:49 -0700126
127 if (!rpm)
128 return -EINVAL;
129
130 period = FAN_RPM_TO_PERIOD(rpm);
131 val = period >> 8;
132 env_write(p, IREG_FAN0 + fan_nr, val);
133
134 return count;
135}
136
Guenter Roecka80b10c2012-01-14 22:42:41 -0800137static ssize_t show_fan_fault(struct device *dev, struct device_attribute *attr,
138 char *buf)
David S. Millere0418082008-08-24 20:59:49 -0700139{
140 int fan_nr = to_sensor_dev_attr(attr)->index;
141 struct env *p = dev_get_drvdata(dev);
142 u8 val = env_read(p, IREG_FAN_STAT);
143 return sprintf(buf, "%d\n", (val & (1 << fan_nr)) ? 1 : 0);
144}
145
146#define fan(index) \
147static SENSOR_DEVICE_ATTR(fan##index##_speed, S_IRUGO | S_IWUSR, \
148 show_fan_speed, set_fan_speed, index); \
149static SENSOR_DEVICE_ATTR(fan##index##_fault, S_IRUGO, \
150 show_fan_fault, NULL, index)
151
152fan(0);
153fan(1);
154fan(2);
155fan(3);
156fan(4);
157
158static SENSOR_DEVICE_ATTR(psu_fan_fault, S_IRUGO, show_fan_fault, NULL, 6);
159
Guenter Roecka80b10c2012-01-14 22:42:41 -0800160static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
161 char *buf)
David S. Millere0418082008-08-24 20:59:49 -0700162{
163 int temp_nr = to_sensor_dev_attr(attr)->index;
164 struct env *p = dev_get_drvdata(dev);
165 s8 val;
166
167 val = env_read(p, IREG_LCL_TEMP + temp_nr);
168 return sprintf(buf, "%d\n", ((int) val) - 64);
169}
170
171static SENSOR_DEVICE_ATTR(adt7462_local_temp, S_IRUGO, show_temp, NULL, 0);
172static SENSOR_DEVICE_ATTR(cpu0_temp, S_IRUGO, show_temp, NULL, 1);
173static SENSOR_DEVICE_ATTR(cpu1_temp, S_IRUGO, show_temp, NULL, 2);
174static SENSOR_DEVICE_ATTR(motherboard_temp, S_IRUGO, show_temp, NULL, 3);
175static SENSOR_DEVICE_ATTR(lm95221_local_temp, S_IRUGO, show_temp, NULL, 4);
176static SENSOR_DEVICE_ATTR(fire_temp, S_IRUGO, show_temp, NULL, 5);
177static SENSOR_DEVICE_ATTR(lsi1064_local_temp, S_IRUGO, show_temp, NULL, 6);
178static SENSOR_DEVICE_ATTR(front_panel_temp, S_IRUGO, show_temp, NULL, 7);
179static SENSOR_DEVICE_ATTR(psu_temp, S_IRUGO, show_temp, NULL, 13);
180
Guenter Roecka80b10c2012-01-14 22:42:41 -0800181static ssize_t show_stat_bit(struct device *dev, struct device_attribute *attr,
182 char *buf)
David S. Millere0418082008-08-24 20:59:49 -0700183{
184 int index = to_sensor_dev_attr(attr)->index;
185 struct env *p = dev_get_drvdata(dev);
186 u8 val;
187
188 val = readb(p->regs + REG_STAT);
189 return sprintf(buf, "%d\n", (val & (1 << index)) ? 1 : 0);
190}
191
192static SENSOR_DEVICE_ATTR(fan_failure, S_IRUGO, show_stat_bit, NULL, 0);
193static SENSOR_DEVICE_ATTR(env_bus_busy, S_IRUGO, show_stat_bit, NULL, 1);
194static SENSOR_DEVICE_ATTR(env_data_stale, S_IRUGO, show_stat_bit, NULL, 2);
Guenter Roecka80b10c2012-01-14 22:42:41 -0800195static SENSOR_DEVICE_ATTR(tpm_self_test_passed, S_IRUGO, show_stat_bit, NULL,
196 3);
David S. Millere0418082008-08-24 20:59:49 -0700197
Guenter Roecka80b10c2012-01-14 22:42:41 -0800198static ssize_t show_fwver(struct device *dev, struct device_attribute *attr,
199 char *buf)
David S. Millere0418082008-08-24 20:59:49 -0700200{
201 struct env *p = dev_get_drvdata(dev);
202 u8 val;
203
204 val = readb(p->regs + REG_STAT);
205 return sprintf(buf, "%d\n", val >> 4);
206}
207
208static SENSOR_DEVICE_ATTR(firmware_version, S_IRUGO, show_fwver, NULL, 0);
209
Guenter Roecka80b10c2012-01-14 22:42:41 -0800210static ssize_t show_name(struct device *dev, struct device_attribute *attr,
211 char *buf)
David S. Millere0418082008-08-24 20:59:49 -0700212{
213 return sprintf(buf, "ultra45\n");
214}
215
216static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
217
218static struct attribute *env_attributes[] = {
219 &sensor_dev_attr_fan0_speed.dev_attr.attr,
220 &sensor_dev_attr_fan0_fault.dev_attr.attr,
221 &sensor_dev_attr_fan1_speed.dev_attr.attr,
222 &sensor_dev_attr_fan1_fault.dev_attr.attr,
223 &sensor_dev_attr_fan2_speed.dev_attr.attr,
224 &sensor_dev_attr_fan2_fault.dev_attr.attr,
225 &sensor_dev_attr_fan3_speed.dev_attr.attr,
226 &sensor_dev_attr_fan3_fault.dev_attr.attr,
227 &sensor_dev_attr_fan4_speed.dev_attr.attr,
228 &sensor_dev_attr_fan4_fault.dev_attr.attr,
229 &sensor_dev_attr_psu_fan_fault.dev_attr.attr,
230 &sensor_dev_attr_adt7462_local_temp.dev_attr.attr,
231 &sensor_dev_attr_cpu0_temp.dev_attr.attr,
232 &sensor_dev_attr_cpu1_temp.dev_attr.attr,
233 &sensor_dev_attr_motherboard_temp.dev_attr.attr,
234 &sensor_dev_attr_lm95221_local_temp.dev_attr.attr,
235 &sensor_dev_attr_fire_temp.dev_attr.attr,
236 &sensor_dev_attr_lsi1064_local_temp.dev_attr.attr,
237 &sensor_dev_attr_front_panel_temp.dev_attr.attr,
238 &sensor_dev_attr_psu_temp.dev_attr.attr,
239 &sensor_dev_attr_fan_failure.dev_attr.attr,
240 &sensor_dev_attr_env_bus_busy.dev_attr.attr,
241 &sensor_dev_attr_env_data_stale.dev_attr.attr,
242 &sensor_dev_attr_tpm_self_test_passed.dev_attr.attr,
243 &sensor_dev_attr_firmware_version.dev_attr.attr,
244 &sensor_dev_attr_name.dev_attr.attr,
245 NULL,
246};
247
248static const struct attribute_group env_group = {
249 .attrs = env_attributes,
250};
251
Grant Likely4ebb24f2011-02-22 20:01:33 -0700252static int __devinit env_probe(struct platform_device *op)
David S. Millere0418082008-08-24 20:59:49 -0700253{
254 struct env *p = kzalloc(sizeof(*p), GFP_KERNEL);
255 int err = -ENOMEM;
256
257 if (!p)
258 goto out;
259
260 spin_lock_init(&p->lock);
261
262 p->regs = of_ioremap(&op->resource[0], 0, REG_SIZE, "pic16f747");
263 if (!p->regs)
264 goto out_free;
265
266 err = sysfs_create_group(&op->dev.kobj, &env_group);
267 if (err)
268 goto out_iounmap;
269
270 p->hwmon_dev = hwmon_device_register(&op->dev);
271 if (IS_ERR(p->hwmon_dev)) {
272 err = PTR_ERR(p->hwmon_dev);
273 goto out_sysfs_remove_group;
274 }
275
Jean Delvare95de3b22011-05-25 20:43:31 +0200276 platform_set_drvdata(op, p);
David S. Millere0418082008-08-24 20:59:49 -0700277 err = 0;
278
279out:
280 return err;
281
282out_sysfs_remove_group:
283 sysfs_remove_group(&op->dev.kobj, &env_group);
284
285out_iounmap:
286 of_iounmap(&op->resource[0], p->regs, REG_SIZE);
287
288out_free:
289 kfree(p);
290 goto out;
291}
292
Grant Likely2dc11582010-08-06 09:25:50 -0600293static int __devexit env_remove(struct platform_device *op)
David S. Millere0418082008-08-24 20:59:49 -0700294{
Jean Delvare95de3b22011-05-25 20:43:31 +0200295 struct env *p = platform_get_drvdata(op);
David S. Millere0418082008-08-24 20:59:49 -0700296
297 if (p) {
298 sysfs_remove_group(&op->dev.kobj, &env_group);
299 hwmon_device_unregister(p->hwmon_dev);
300 of_iounmap(&op->resource[0], p->regs, REG_SIZE);
301 kfree(p);
302 }
303
304 return 0;
305}
306
David S. Millerfd098312008-08-31 01:23:17 -0700307static const struct of_device_id env_match[] = {
David S. Millere0418082008-08-24 20:59:49 -0700308 {
309 .name = "env-monitor",
310 .compatible = "SUNW,ebus-pic16f747-env",
311 },
312 {},
313};
314MODULE_DEVICE_TABLE(of, env_match);
315
Grant Likely4ebb24f2011-02-22 20:01:33 -0700316static struct platform_driver env_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700317 .driver = {
318 .name = "ultra45_env",
319 .owner = THIS_MODULE,
320 .of_match_table = env_match,
321 },
David S. Millere0418082008-08-24 20:59:49 -0700322 .probe = env_probe,
323 .remove = __devexit_p(env_remove),
324};
325
Axel Lin25a236a2011-11-25 02:31:00 -0500326module_platform_driver(env_driver);