ultra45_env.c 8.3 KB

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