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