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