windfarm_lm75_sensor.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <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_lm75_sensor {
  31. int ds1775 : 1;
  32. int inited : 1;
  33. struct i2c_client *i2c;
  34. struct wf_sensor sens;
  35. };
  36. #define wf_to_lm75(c) container_of(c, struct wf_lm75_sensor, sens)
  37. static int wf_lm75_get(struct wf_sensor *sr, s32 *value)
  38. {
  39. struct wf_lm75_sensor *lm = wf_to_lm75(sr);
  40. s32 data;
  41. if (lm->i2c == NULL)
  42. return -ENODEV;
  43. /* Init chip if necessary */
  44. if (!lm->inited) {
  45. u8 cfg_new, cfg = (u8)i2c_smbus_read_byte_data(lm->i2c, 1);
  46. DBG("wf_lm75: Initializing %s, cfg was: %02x\n",
  47. sr->name, cfg);
  48. /* clear shutdown bit, keep other settings as left by
  49. * the firmware for now
  50. */
  51. cfg_new = cfg & ~0x01;
  52. i2c_smbus_write_byte_data(lm->i2c, 1, cfg_new);
  53. lm->inited = 1;
  54. /* If we just powered it up, let's wait 200 ms */
  55. msleep(200);
  56. }
  57. /* Read temperature register */
  58. data = (s32)le16_to_cpu(i2c_smbus_read_word_data(lm->i2c, 0));
  59. data <<= 8;
  60. *value = data;
  61. return 0;
  62. }
  63. static void wf_lm75_release(struct wf_sensor *sr)
  64. {
  65. struct wf_lm75_sensor *lm = wf_to_lm75(sr);
  66. kfree(lm);
  67. }
  68. static struct wf_sensor_ops wf_lm75_ops = {
  69. .get_value = wf_lm75_get,
  70. .release = wf_lm75_release,
  71. .owner = THIS_MODULE,
  72. };
  73. static int wf_lm75_probe(struct i2c_client *client,
  74. const struct i2c_device_id *id)
  75. {
  76. struct wf_lm75_sensor *lm;
  77. int rc, ds1775 = id->driver_data;
  78. const char *name, *loc;
  79. DBG("wf_lm75: creating %s device at address 0x%02x\n",
  80. ds1775 ? "ds1775" : "lm75", client->addr);
  81. loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
  82. if (!loc) {
  83. dev_warn(&client->dev, "Missing hwsensor-location property!\n");
  84. return -ENXIO;
  85. }
  86. /* Usual rant about sensor names not beeing very consistent in
  87. * the device-tree, oh well ...
  88. * Add more entries below as you deal with more setups
  89. */
  90. if (!strcmp(loc, "Hard drive") || !strcmp(loc, "DRIVE BAY"))
  91. name = "hd-temp";
  92. else if (!strcmp(loc, "Incoming Air Temp"))
  93. name = "incoming-air-temp";
  94. else if (!strcmp(loc, "ODD Temp"))
  95. name = "optical-drive-temp";
  96. else if (!strcmp(loc, "HD Temp"))
  97. name = "hard-drive-temp";
  98. else if (!strcmp(loc, "PCI SLOTS"))
  99. name = "slots-temp";
  100. else if (!strcmp(loc, "CPU A INLET"))
  101. name = "cpu-inlet-temp-0";
  102. else if (!strcmp(loc, "CPU B INLET"))
  103. name = "cpu-inlet-temp-1";
  104. else
  105. return -ENXIO;
  106. lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
  107. if (lm == NULL)
  108. return -ENODEV;
  109. lm->inited = 0;
  110. lm->ds1775 = ds1775;
  111. lm->i2c = client;
  112. lm->sens.name = name;
  113. lm->sens.ops = &wf_lm75_ops;
  114. i2c_set_clientdata(client, lm);
  115. rc = wf_register_sensor(&lm->sens);
  116. if (rc)
  117. kfree(lm);
  118. return rc;
  119. }
  120. static int wf_lm75_remove(struct i2c_client *client)
  121. {
  122. struct wf_lm75_sensor *lm = i2c_get_clientdata(client);
  123. DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);
  124. /* Mark client detached */
  125. lm->i2c = NULL;
  126. /* release sensor */
  127. wf_unregister_sensor(&lm->sens);
  128. return 0;
  129. }
  130. static const struct i2c_device_id wf_lm75_id[] = {
  131. { "MAC,lm75", 0 },
  132. { "MAC,ds1775", 1 },
  133. { }
  134. };
  135. MODULE_DEVICE_TABLE(i2c, wf_lm75_id);
  136. static struct i2c_driver wf_lm75_driver = {
  137. .driver = {
  138. .name = "wf_lm75",
  139. },
  140. .probe = wf_lm75_probe,
  141. .remove = wf_lm75_remove,
  142. .id_table = wf_lm75_id,
  143. };
  144. module_i2c_driver(wf_lm75_driver);
  145. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  146. MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
  147. MODULE_LICENSE("GPL");