windfarm_lm87_sensor.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Windfarm PowerMac thermal control. LM87 sensor
  3. *
  4. * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
  5. *
  6. * Released under the term of the GNU GPL v2.
  7. *
  8. */
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/wait.h>
  16. #include <linux/i2c.h>
  17. #include <asm/prom.h>
  18. #include <asm/machdep.h>
  19. #include <asm/io.h>
  20. #include <asm/sections.h>
  21. #include <asm/pmac_low_i2c.h>
  22. #include "windfarm.h"
  23. #define VERSION "1.0"
  24. #undef DEBUG
  25. #ifdef DEBUG
  26. #define DBG(args...) printk(args)
  27. #else
  28. #define DBG(args...) do { } while(0)
  29. #endif
  30. struct wf_lm87_sensor {
  31. struct i2c_client *i2c;
  32. struct wf_sensor sens;
  33. };
  34. #define wf_to_lm87(c) container_of(c, struct wf_lm87_sensor, sens)
  35. static int wf_lm87_read_reg(struct i2c_client *chip, int reg)
  36. {
  37. int rc, tries = 0;
  38. u8 buf;
  39. for (;;) {
  40. /* Set address */
  41. buf = (u8)reg;
  42. rc = i2c_master_send(chip, &buf, 1);
  43. if (rc <= 0)
  44. goto error;
  45. rc = i2c_master_recv(chip, &buf, 1);
  46. if (rc <= 0)
  47. goto error;
  48. return (int)buf;
  49. error:
  50. DBG("wf_lm87: Error reading LM87, retrying...\n");
  51. if (++tries > 10) {
  52. printk(KERN_ERR "wf_lm87: Error reading LM87 !\n");
  53. return -EIO;
  54. }
  55. msleep(10);
  56. }
  57. }
  58. static int wf_lm87_get(struct wf_sensor *sr, s32 *value)
  59. {
  60. struct wf_lm87_sensor *lm = sr->priv;
  61. s32 temp;
  62. if (lm->i2c == NULL)
  63. return -ENODEV;
  64. #define LM87_INT_TEMP 0x27
  65. /* Read temperature register */
  66. temp = wf_lm87_read_reg(lm->i2c, LM87_INT_TEMP);
  67. if (temp < 0)
  68. return temp;
  69. *value = temp << 16;
  70. return 0;
  71. }
  72. static void wf_lm87_release(struct wf_sensor *sr)
  73. {
  74. struct wf_lm87_sensor *lm = wf_to_lm87(sr);
  75. kfree(lm);
  76. }
  77. static struct wf_sensor_ops wf_lm87_ops = {
  78. .get_value = wf_lm87_get,
  79. .release = wf_lm87_release,
  80. .owner = THIS_MODULE,
  81. };
  82. static int wf_lm87_probe(struct i2c_client *client,
  83. const struct i2c_device_id *id)
  84. {
  85. struct wf_lm87_sensor *lm;
  86. const char *name = NULL, *loc;
  87. struct device_node *np = NULL;
  88. int rc;
  89. /*
  90. * The lm87 contains a whole pile of sensors, additionally,
  91. * the Xserve G5 has several lm87's. However, for now we only
  92. * care about the internal temperature sensor
  93. */
  94. while ((np = of_get_next_child(client->dev.of_node, np)) != NULL) {
  95. if (strcmp(np->name, "int-temp"))
  96. continue;
  97. loc = of_get_property(np, "location", NULL);
  98. if (!loc)
  99. continue;
  100. if (strstr(loc, "DIMM"))
  101. name = "dimms-temp";
  102. else if (strstr(loc, "Processors"))
  103. name = "between-cpus-temp";
  104. if (name) {
  105. of_node_put(np);
  106. break;
  107. }
  108. }
  109. if (!name) {
  110. pr_warning("wf_lm87: Unsupported sensor %s\n",
  111. client->dev.of_node->full_name);
  112. return -ENODEV;
  113. }
  114. lm = kzalloc(sizeof(struct wf_lm87_sensor), GFP_KERNEL);
  115. if (lm == NULL)
  116. return -ENODEV;
  117. lm->i2c = client;
  118. lm->sens.name = name;
  119. lm->sens.ops = &wf_lm87_ops;
  120. lm->sens.priv = lm;
  121. i2c_set_clientdata(client, lm);
  122. rc = wf_register_sensor(&lm->sens);
  123. if (rc)
  124. kfree(lm);
  125. return rc;
  126. }
  127. static int wf_lm87_remove(struct i2c_client *client)
  128. {
  129. struct wf_lm87_sensor *lm = i2c_get_clientdata(client);
  130. DBG("wf_lm87: i2c detatch called for %s\n", lm->sens.name);
  131. /* Mark client detached */
  132. lm->i2c = NULL;
  133. /* release sensor */
  134. wf_unregister_sensor(&lm->sens);
  135. return 0;
  136. }
  137. static const struct i2c_device_id wf_lm87_id[] = {
  138. { "MAC,lm87cimt", 0 },
  139. { }
  140. };
  141. MODULE_DEVICE_TABLE(i2c, wf_lm87_id);
  142. static struct i2c_driver wf_lm87_driver = {
  143. .driver = {
  144. .name = "wf_lm87",
  145. },
  146. .probe = wf_lm87_probe,
  147. .remove = wf_lm87_remove,
  148. .id_table = wf_lm87_id,
  149. };
  150. static int __init wf_lm87_sensor_init(void)
  151. {
  152. /* We only support this on the Xserve */
  153. if (!of_machine_is_compatible("RackMac3,1"))
  154. return -ENODEV;
  155. return i2c_add_driver(&wf_lm87_driver);
  156. }
  157. static void __exit wf_lm87_sensor_exit(void)
  158. {
  159. i2c_del_driver(&wf_lm87_driver);
  160. }
  161. module_init(wf_lm87_sensor_init);
  162. module_exit(wf_lm87_sensor_exit);
  163. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  164. MODULE_DESCRIPTION("LM87 sensor objects for PowerMacs thermal control");
  165. MODULE_LICENSE("GPL");