windfarm_lm75_sensor.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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
  99. return -ENXIO;
  100. lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
  101. if (lm == NULL)
  102. return -ENODEV;
  103. lm->inited = 0;
  104. lm->ds1775 = ds1775;
  105. lm->i2c = client;
  106. lm->sens.name = (char *)name; /* XXX fix constness in structure */
  107. lm->sens.ops = &wf_lm75_ops;
  108. i2c_set_clientdata(client, lm);
  109. rc = wf_register_sensor(&lm->sens);
  110. if (rc)
  111. kfree(lm);
  112. return rc;
  113. }
  114. static int wf_lm75_remove(struct i2c_client *client)
  115. {
  116. struct wf_lm75_sensor *lm = i2c_get_clientdata(client);
  117. DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);
  118. /* Mark client detached */
  119. lm->i2c = NULL;
  120. /* release sensor */
  121. wf_unregister_sensor(&lm->sens);
  122. return 0;
  123. }
  124. static const struct i2c_device_id wf_lm75_id[] = {
  125. { "MAC,lm75", 0 },
  126. { "MAC,ds1775", 1 },
  127. { }
  128. };
  129. MODULE_DEVICE_TABLE(i2c, wf_lm75_id);
  130. static struct i2c_driver wf_lm75_driver = {
  131. .driver = {
  132. .name = "wf_lm75",
  133. },
  134. .probe = wf_lm75_probe,
  135. .remove = wf_lm75_remove,
  136. .id_table = wf_lm75_id,
  137. };
  138. static int __init wf_lm75_sensor_init(void)
  139. {
  140. /* Don't register on old machines that use therm_pm72 for now */
  141. if (of_machine_is_compatible("PowerMac7,2") ||
  142. of_machine_is_compatible("PowerMac7,3") ||
  143. of_machine_is_compatible("RackMac3,1"))
  144. return -ENODEV;
  145. return i2c_add_driver(&wf_lm75_driver);
  146. }
  147. static void __exit wf_lm75_sensor_exit(void)
  148. {
  149. i2c_del_driver(&wf_lm75_driver);
  150. }
  151. module_init(wf_lm75_sensor_init);
  152. module_exit(wf_lm75_sensor_exit);
  153. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  154. MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
  155. MODULE_LICENSE("GPL");