windfarm_lm75_sensor.c 4.3 KB

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