windfarm_lm75_sensor.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Windfarm PowerMac thermal control. LM75 sensor
  3. *
  4. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  5. * <benh@kernel.crashing.org>
  6. *
  7. * Released under the term of the GNU GPL v2.
  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 <linux/i2c-dev.h>
  18. #include <asm/prom.h>
  19. #include <asm/machdep.h>
  20. #include <asm/io.h>
  21. #include <asm/system.h>
  22. #include <asm/sections.h>
  23. #include <asm/pmac_low_i2c.h>
  24. #include "windfarm.h"
  25. #define VERSION "0.2"
  26. #undef DEBUG
  27. #ifdef DEBUG
  28. #define DBG(args...) printk(args)
  29. #else
  30. #define DBG(args...) do { } while(0)
  31. #endif
  32. struct wf_lm75_sensor {
  33. int ds1775 : 1;
  34. int inited : 1;
  35. struct i2c_client i2c;
  36. struct wf_sensor sens;
  37. };
  38. #define wf_to_lm75(c) container_of(c, struct wf_lm75_sensor, sens)
  39. #define i2c_to_lm75(c) container_of(c, struct wf_lm75_sensor, i2c)
  40. static int wf_lm75_attach(struct i2c_adapter *adapter);
  41. static int wf_lm75_detach(struct i2c_client *client);
  42. static struct i2c_driver wf_lm75_driver = {
  43. .driver = {
  44. .name = "wf_lm75",
  45. },
  46. .attach_adapter = wf_lm75_attach,
  47. .detach_client = wf_lm75_detach,
  48. };
  49. static int wf_lm75_get(struct wf_sensor *sr, s32 *value)
  50. {
  51. struct wf_lm75_sensor *lm = wf_to_lm75(sr);
  52. s32 data;
  53. if (lm->i2c.adapter == NULL)
  54. return -ENODEV;
  55. /* Init chip if necessary */
  56. if (!lm->inited) {
  57. u8 cfg_new, cfg = (u8)i2c_smbus_read_byte_data(&lm->i2c, 1);
  58. DBG("wf_lm75: Initializing %s, cfg was: %02x\n",
  59. sr->name, cfg);
  60. /* clear shutdown bit, keep other settings as left by
  61. * the firmware for now
  62. */
  63. cfg_new = cfg & ~0x01;
  64. i2c_smbus_write_byte_data(&lm->i2c, 1, cfg_new);
  65. lm->inited = 1;
  66. /* If we just powered it up, let's wait 200 ms */
  67. msleep(200);
  68. }
  69. /* Read temperature register */
  70. data = (s32)le16_to_cpu(i2c_smbus_read_word_data(&lm->i2c, 0));
  71. data <<= 8;
  72. *value = data;
  73. return 0;
  74. }
  75. static void wf_lm75_release(struct wf_sensor *sr)
  76. {
  77. struct wf_lm75_sensor *lm = wf_to_lm75(sr);
  78. /* check if client is registered and detach from i2c */
  79. if (lm->i2c.adapter) {
  80. i2c_detach_client(&lm->i2c);
  81. lm->i2c.adapter = NULL;
  82. }
  83. kfree(lm);
  84. }
  85. static struct wf_sensor_ops wf_lm75_ops = {
  86. .get_value = wf_lm75_get,
  87. .release = wf_lm75_release,
  88. .owner = THIS_MODULE,
  89. };
  90. static struct wf_lm75_sensor *wf_lm75_create(struct i2c_adapter *adapter,
  91. u8 addr, int ds1775,
  92. const char *loc)
  93. {
  94. struct wf_lm75_sensor *lm;
  95. int rc;
  96. DBG("wf_lm75: creating %s device at address 0x%02x\n",
  97. ds1775 ? "ds1775" : "lm75", addr);
  98. lm = kmalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
  99. if (lm == NULL)
  100. return NULL;
  101. memset(lm, 0, sizeof(struct wf_lm75_sensor));
  102. /* Usual rant about sensor names not beeing very consistent in
  103. * the device-tree, oh well ...
  104. * Add more entries below as you deal with more setups
  105. */
  106. if (!strcmp(loc, "Hard drive") || !strcmp(loc, "DRIVE BAY"))
  107. lm->sens.name = "hd-temp";
  108. else
  109. goto fail;
  110. lm->inited = 0;
  111. lm->sens.ops = &wf_lm75_ops;
  112. lm->ds1775 = ds1775;
  113. lm->i2c.addr = (addr >> 1) & 0x7f;
  114. lm->i2c.adapter = adapter;
  115. lm->i2c.driver = &wf_lm75_driver;
  116. strncpy(lm->i2c.name, lm->sens.name, I2C_NAME_SIZE-1);
  117. rc = i2c_attach_client(&lm->i2c);
  118. if (rc) {
  119. printk(KERN_ERR "windfarm: failed to attach %s %s to i2c,"
  120. " err %d\n", ds1775 ? "ds1775" : "lm75",
  121. lm->i2c.name, rc);
  122. goto fail;
  123. }
  124. if (wf_register_sensor(&lm->sens)) {
  125. i2c_detach_client(&lm->i2c);
  126. goto fail;
  127. }
  128. return lm;
  129. fail:
  130. kfree(lm);
  131. return NULL;
  132. }
  133. static int wf_lm75_attach(struct i2c_adapter *adapter)
  134. {
  135. struct device_node *busnode, *dev;
  136. struct pmac_i2c_bus *bus;
  137. DBG("wf_lm75: adapter %s detected\n", adapter->name);
  138. bus = pmac_i2c_adapter_to_bus(adapter);
  139. if (bus == NULL)
  140. return -ENODEV;
  141. busnode = pmac_i2c_get_bus_node(bus);
  142. DBG("wf_lm75: bus found, looking for device...\n");
  143. /* Now look for lm75(s) in there */
  144. for (dev = NULL;
  145. (dev = of_get_next_child(busnode, dev)) != NULL;) {
  146. const char *loc =
  147. get_property(dev, "hwsensor-location", NULL);
  148. u8 addr;
  149. /* We must re-match the adapter in order to properly check
  150. * the channel on multibus setups
  151. */
  152. if (!pmac_i2c_match_adapter(dev, adapter))
  153. continue;
  154. addr = pmac_i2c_get_dev_addr(dev);
  155. if (loc == NULL || addr == 0)
  156. continue;
  157. /* real lm75 */
  158. if (device_is_compatible(dev, "lm75"))
  159. wf_lm75_create(adapter, addr, 0, loc);
  160. /* ds1775 (compatible, better resolution */
  161. else if (device_is_compatible(dev, "ds1775"))
  162. wf_lm75_create(adapter, addr, 1, loc);
  163. }
  164. return 0;
  165. }
  166. static int wf_lm75_detach(struct i2c_client *client)
  167. {
  168. struct wf_lm75_sensor *lm = i2c_to_lm75(client);
  169. DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);
  170. /* Mark client detached */
  171. lm->i2c.adapter = NULL;
  172. /* release sensor */
  173. wf_unregister_sensor(&lm->sens);
  174. return 0;
  175. }
  176. static int __init wf_lm75_sensor_init(void)
  177. {
  178. /* Don't register on old machines that use therm_pm72 for now */
  179. if (machine_is_compatible("PowerMac7,2") ||
  180. machine_is_compatible("PowerMac7,3") ||
  181. machine_is_compatible("RackMac3,1"))
  182. return -ENODEV;
  183. return i2c_add_driver(&wf_lm75_driver);
  184. }
  185. static void __exit wf_lm75_sensor_exit(void)
  186. {
  187. i2c_del_driver(&wf_lm75_driver);
  188. }
  189. module_init(wf_lm75_sensor_init);
  190. module_exit(wf_lm75_sensor_exit);
  191. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  192. MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
  193. MODULE_LICENSE("GPL");