ultra45_env.c 8.4 KB

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