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