This source file includes following definitions.
- env_read
- env_write
- show_fan_speed
- set_fan_speed
- show_fan_fault
- show_temp
- show_stat_bit
- show_fwver
- show_name
- env_probe
- env_remove
1
2
3
4
5
6
7
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/io.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/err.h>
17
18 #define DRV_MODULE_VERSION "0.1"
19
20 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
21 MODULE_DESCRIPTION("Ultra45 environmental monitor driver");
22 MODULE_LICENSE("GPL");
23 MODULE_VERSION(DRV_MODULE_VERSION);
24
25
26 #define REG_CMD 0x00UL
27 #define REG_CMD_RESET 0x80
28 #define REG_CMD_ESTAR 0x01
29 #define REG_STAT 0x01UL
30 #define REG_STAT_FWVER 0xf0
31 #define REG_STAT_TGOOD 0x08
32 #define REG_STAT_STALE 0x04
33 #define REG_STAT_BUSY 0x02
34 #define REG_STAT_FAULT 0x01
35 #define REG_DATA 0x40UL
36 #define REG_ADDR 0x41UL
37 #define REG_SIZE 0x42UL
38
39
40 #define IREG_FAN0 0x00
41 #define IREG_FAN1 0x01
42 #define IREG_FAN2 0x02
43 #define IREG_FAN3 0x03
44 #define IREG_FAN4 0x04
45 #define IREG_FAN5 0x05
46 #define IREG_LCL_TEMP 0x06
47 #define IREG_RMT1_TEMP 0x07
48 #define IREG_RMT2_TEMP 0x08
49 #define IREG_RMT3_TEMP 0x09
50 #define IREG_LM95221_TEMP 0x0a
51 #define IREG_FIRE_TEMP 0x0b
52 #define IREG_LSI1064_TEMP 0x0c
53 #define IREG_FRONT_TEMP 0x0d
54 #define IREG_FAN_STAT 0x0e
55 #define IREG_VCORE0 0x0f
56 #define IREG_VCORE1 0x10
57 #define IREG_VMEM0 0x11
58 #define IREG_VMEM1 0x12
59 #define IREG_PSU_TEMP 0x13
60
61 struct env {
62 void __iomem *regs;
63 spinlock_t lock;
64
65 struct device *hwmon_dev;
66 };
67
68 static u8 env_read(struct env *p, u8 ireg)
69 {
70 u8 ret;
71
72 spin_lock(&p->lock);
73 writeb(ireg, p->regs + REG_ADDR);
74 ret = readb(p->regs + REG_DATA);
75 spin_unlock(&p->lock);
76
77 return ret;
78 }
79
80 static void env_write(struct env *p, u8 ireg, u8 val)
81 {
82 spin_lock(&p->lock);
83 writeb(ireg, p->regs + REG_ADDR);
84 writeb(val, p->regs + REG_DATA);
85 spin_unlock(&p->lock);
86 }
87
88
89
90
91
92 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
93 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
94 #define FAN_PERIOD_INVALID (0xff << 8)
95 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
96
97 static ssize_t show_fan_speed(struct device *dev, struct device_attribute *attr,
98 char *buf)
99 {
100 int fan_nr = to_sensor_dev_attr(attr)->index;
101 struct env *p = dev_get_drvdata(dev);
102 int rpm, period;
103 u8 val;
104
105 val = env_read(p, IREG_FAN0 + fan_nr);
106 period = (int) val << 8;
107 if (FAN_DATA_VALID(period))
108 rpm = FAN_PERIOD_TO_RPM(period);
109 else
110 rpm = 0;
111
112 return sprintf(buf, "%d\n", rpm);
113 }
114
115 static ssize_t set_fan_speed(struct device *dev, struct device_attribute *attr,
116 const char *buf, size_t count)
117 {
118 int fan_nr = to_sensor_dev_attr(attr)->index;
119 unsigned long rpm;
120 struct env *p = dev_get_drvdata(dev);
121 int period;
122 u8 val;
123 int err;
124
125 err = kstrtoul(buf, 10, &rpm);
126 if (err)
127 return err;
128
129 if (!rpm)
130 return -EINVAL;
131
132 period = FAN_RPM_TO_PERIOD(rpm);
133 val = period >> 8;
134 env_write(p, IREG_FAN0 + fan_nr, val);
135
136 return count;
137 }
138
139 static ssize_t show_fan_fault(struct device *dev, struct device_attribute *attr,
140 char *buf)
141 {
142 int fan_nr = to_sensor_dev_attr(attr)->index;
143 struct env *p = dev_get_drvdata(dev);
144 u8 val = env_read(p, IREG_FAN_STAT);
145 return sprintf(buf, "%d\n", (val & (1 << fan_nr)) ? 1 : 0);
146 }
147
148 #define fan(index) \
149 static SENSOR_DEVICE_ATTR(fan##index##_speed, S_IRUGO | S_IWUSR, \
150 show_fan_speed, set_fan_speed, index); \
151 static SENSOR_DEVICE_ATTR(fan##index##_fault, S_IRUGO, \
152 show_fan_fault, NULL, index)
153
154 fan(0);
155 fan(1);
156 fan(2);
157 fan(3);
158 fan(4);
159
160 static SENSOR_DEVICE_ATTR(psu_fan_fault, S_IRUGO, show_fan_fault, NULL, 6);
161
162 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
163 char *buf)
164 {
165 int temp_nr = to_sensor_dev_attr(attr)->index;
166 struct env *p = dev_get_drvdata(dev);
167 s8 val;
168
169 val = env_read(p, IREG_LCL_TEMP + temp_nr);
170 return sprintf(buf, "%d\n", ((int) val) - 64);
171 }
172
173 static SENSOR_DEVICE_ATTR(adt7462_local_temp, S_IRUGO, show_temp, NULL, 0);
174 static SENSOR_DEVICE_ATTR(cpu0_temp, S_IRUGO, show_temp, NULL, 1);
175 static SENSOR_DEVICE_ATTR(cpu1_temp, S_IRUGO, show_temp, NULL, 2);
176 static SENSOR_DEVICE_ATTR(motherboard_temp, S_IRUGO, show_temp, NULL, 3);
177 static SENSOR_DEVICE_ATTR(lm95221_local_temp, S_IRUGO, show_temp, NULL, 4);
178 static SENSOR_DEVICE_ATTR(fire_temp, S_IRUGO, show_temp, NULL, 5);
179 static SENSOR_DEVICE_ATTR(lsi1064_local_temp, S_IRUGO, show_temp, NULL, 6);
180 static SENSOR_DEVICE_ATTR(front_panel_temp, S_IRUGO, show_temp, NULL, 7);
181 static SENSOR_DEVICE_ATTR(psu_temp, S_IRUGO, show_temp, NULL, 13);
182
183 static ssize_t show_stat_bit(struct device *dev, struct device_attribute *attr,
184 char *buf)
185 {
186 int index = to_sensor_dev_attr(attr)->index;
187 struct env *p = dev_get_drvdata(dev);
188 u8 val;
189
190 val = readb(p->regs + REG_STAT);
191 return sprintf(buf, "%d\n", (val & (1 << index)) ? 1 : 0);
192 }
193
194 static SENSOR_DEVICE_ATTR(fan_failure, S_IRUGO, show_stat_bit, NULL, 0);
195 static SENSOR_DEVICE_ATTR(env_bus_busy, S_IRUGO, show_stat_bit, NULL, 1);
196 static SENSOR_DEVICE_ATTR(env_data_stale, S_IRUGO, show_stat_bit, NULL, 2);
197 static SENSOR_DEVICE_ATTR(tpm_self_test_passed, S_IRUGO, show_stat_bit, NULL,
198 3);
199
200 static ssize_t show_fwver(struct device *dev, struct device_attribute *attr,
201 char *buf)
202 {
203 struct env *p = dev_get_drvdata(dev);
204 u8 val;
205
206 val = readb(p->regs + REG_STAT);
207 return sprintf(buf, "%d\n", val >> 4);
208 }
209
210 static SENSOR_DEVICE_ATTR(firmware_version, S_IRUGO, show_fwver, NULL, 0);
211
212 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
213 char *buf)
214 {
215 return sprintf(buf, "ultra45\n");
216 }
217
218 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
219
220 static struct attribute *env_attributes[] = {
221 &sensor_dev_attr_fan0_speed.dev_attr.attr,
222 &sensor_dev_attr_fan0_fault.dev_attr.attr,
223 &sensor_dev_attr_fan1_speed.dev_attr.attr,
224 &sensor_dev_attr_fan1_fault.dev_attr.attr,
225 &sensor_dev_attr_fan2_speed.dev_attr.attr,
226 &sensor_dev_attr_fan2_fault.dev_attr.attr,
227 &sensor_dev_attr_fan3_speed.dev_attr.attr,
228 &sensor_dev_attr_fan3_fault.dev_attr.attr,
229 &sensor_dev_attr_fan4_speed.dev_attr.attr,
230 &sensor_dev_attr_fan4_fault.dev_attr.attr,
231 &sensor_dev_attr_psu_fan_fault.dev_attr.attr,
232 &sensor_dev_attr_adt7462_local_temp.dev_attr.attr,
233 &sensor_dev_attr_cpu0_temp.dev_attr.attr,
234 &sensor_dev_attr_cpu1_temp.dev_attr.attr,
235 &sensor_dev_attr_motherboard_temp.dev_attr.attr,
236 &sensor_dev_attr_lm95221_local_temp.dev_attr.attr,
237 &sensor_dev_attr_fire_temp.dev_attr.attr,
238 &sensor_dev_attr_lsi1064_local_temp.dev_attr.attr,
239 &sensor_dev_attr_front_panel_temp.dev_attr.attr,
240 &sensor_dev_attr_psu_temp.dev_attr.attr,
241 &sensor_dev_attr_fan_failure.dev_attr.attr,
242 &sensor_dev_attr_env_bus_busy.dev_attr.attr,
243 &sensor_dev_attr_env_data_stale.dev_attr.attr,
244 &sensor_dev_attr_tpm_self_test_passed.dev_attr.attr,
245 &sensor_dev_attr_firmware_version.dev_attr.attr,
246 &sensor_dev_attr_name.dev_attr.attr,
247 NULL,
248 };
249
250 static const struct attribute_group env_group = {
251 .attrs = env_attributes,
252 };
253
254 static int env_probe(struct platform_device *op)
255 {
256 struct env *p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
257 int err = -ENOMEM;
258
259 if (!p)
260 goto out;
261
262 spin_lock_init(&p->lock);
263
264 p->regs = of_ioremap(&op->resource[0], 0, REG_SIZE, "pic16f747");
265 if (!p->regs)
266 goto out;
267
268 err = sysfs_create_group(&op->dev.kobj, &env_group);
269 if (err)
270 goto out_iounmap;
271
272 p->hwmon_dev = hwmon_device_register(&op->dev);
273 if (IS_ERR(p->hwmon_dev)) {
274 err = PTR_ERR(p->hwmon_dev);
275 goto out_sysfs_remove_group;
276 }
277
278 platform_set_drvdata(op, p);
279 err = 0;
280
281 out:
282 return err;
283
284 out_sysfs_remove_group:
285 sysfs_remove_group(&op->dev.kobj, &env_group);
286
287 out_iounmap:
288 of_iounmap(&op->resource[0], p->regs, REG_SIZE);
289
290 goto out;
291 }
292
293 static int env_remove(struct platform_device *op)
294 {
295 struct env *p = platform_get_drvdata(op);
296
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 }
302
303 return 0;
304 }
305
306 static const struct of_device_id env_match[] = {
307 {
308 .name = "env-monitor",
309 .compatible = "SUNW,ebus-pic16f747-env",
310 },
311 {},
312 };
313 MODULE_DEVICE_TABLE(of, env_match);
314
315 static struct platform_driver env_driver = {
316 .driver = {
317 .name = "ultra45_env",
318 .of_match_table = env_match,
319 },
320 .probe = env_probe,
321 .remove = env_remove,
322 };
323
324 module_platform_driver(env_driver);