windfarm_lm75_sensor.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "0.2"
  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;
  78. lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
  79. if (lm == NULL)
  80. return -ENODEV;
  81. lm->inited = 0;
  82. lm->ds1775 = id->driver_data;
  83. lm->i2c = client;
  84. lm->sens.name = client->dev.platform_data;
  85. lm->sens.ops = &wf_lm75_ops;
  86. i2c_set_clientdata(client, lm);
  87. rc = wf_register_sensor(&lm->sens);
  88. if (rc)
  89. kfree(lm);
  90. return rc;
  91. }
  92. static struct i2c_driver wf_lm75_driver;
  93. static struct i2c_client *wf_lm75_create(struct i2c_adapter *adapter,
  94. u8 addr, int ds1775,
  95. const char *loc)
  96. {
  97. struct i2c_board_info info;
  98. struct i2c_client *client;
  99. char *name;
  100. DBG("wf_lm75: creating %s device at address 0x%02x\n",
  101. ds1775 ? "ds1775" : "lm75", addr);
  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. name = "hd-temp";
  108. else if (!strcmp(loc, "Incoming Air Temp"))
  109. name = "incoming-air-temp";
  110. else if (!strcmp(loc, "ODD Temp"))
  111. name = "optical-drive-temp";
  112. else if (!strcmp(loc, "HD Temp"))
  113. name = "hard-drive-temp";
  114. else
  115. goto fail;
  116. memset(&info, 0, sizeof(struct i2c_board_info));
  117. info.addr = (addr >> 1) & 0x7f;
  118. info.platform_data = name;
  119. strlcpy(info.type, ds1775 ? "wf_ds1775" : "wf_lm75", I2C_NAME_SIZE);
  120. client = i2c_new_device(adapter, &info);
  121. if (client == NULL) {
  122. printk(KERN_ERR "windfarm: failed to attach %s %s to i2c\n",
  123. ds1775 ? "ds1775" : "lm75", name);
  124. goto fail;
  125. }
  126. /*
  127. * Let i2c-core delete that device on driver removal.
  128. * This is safe because i2c-core holds the core_lock mutex for us.
  129. */
  130. list_add_tail(&client->detected, &wf_lm75_driver.clients);
  131. return client;
  132. fail:
  133. return NULL;
  134. }
  135. static int wf_lm75_attach(struct i2c_adapter *adapter)
  136. {
  137. struct device_node *busnode, *dev;
  138. struct pmac_i2c_bus *bus;
  139. DBG("wf_lm75: adapter %s detected\n", adapter->name);
  140. bus = pmac_i2c_adapter_to_bus(adapter);
  141. if (bus == NULL)
  142. return -ENODEV;
  143. busnode = pmac_i2c_get_bus_node(bus);
  144. DBG("wf_lm75: bus found, looking for device...\n");
  145. /* Now look for lm75(s) in there */
  146. for (dev = NULL;
  147. (dev = of_get_next_child(busnode, dev)) != NULL;) {
  148. const char *loc =
  149. of_get_property(dev, "hwsensor-location", NULL);
  150. u8 addr;
  151. /* We must re-match the adapter in order to properly check
  152. * the channel on multibus setups
  153. */
  154. if (!pmac_i2c_match_adapter(dev, adapter))
  155. continue;
  156. addr = pmac_i2c_get_dev_addr(dev);
  157. if (loc == NULL || addr == 0)
  158. continue;
  159. /* real lm75 */
  160. if (of_device_is_compatible(dev, "lm75"))
  161. wf_lm75_create(adapter, addr, 0, loc);
  162. /* ds1775 (compatible, better resolution */
  163. else if (of_device_is_compatible(dev, "ds1775"))
  164. wf_lm75_create(adapter, addr, 1, loc);
  165. }
  166. return 0;
  167. }
  168. static int wf_lm75_remove(struct i2c_client *client)
  169. {
  170. struct wf_lm75_sensor *lm = i2c_get_clientdata(client);
  171. DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);
  172. /* Mark client detached */
  173. lm->i2c = NULL;
  174. /* release sensor */
  175. wf_unregister_sensor(&lm->sens);
  176. return 0;
  177. }
  178. static const struct i2c_device_id wf_lm75_id[] = {
  179. { "wf_lm75", 0 },
  180. { "wf_ds1775", 1 },
  181. { }
  182. };
  183. static struct i2c_driver wf_lm75_driver = {
  184. .driver = {
  185. .name = "wf_lm75",
  186. },
  187. .attach_adapter = wf_lm75_attach,
  188. .probe = wf_lm75_probe,
  189. .remove = wf_lm75_remove,
  190. .id_table = wf_lm75_id,
  191. };
  192. static int __init wf_lm75_sensor_init(void)
  193. {
  194. /* Don't register on old machines that use therm_pm72 for now */
  195. if (of_machine_is_compatible("PowerMac7,2") ||
  196. of_machine_is_compatible("PowerMac7,3") ||
  197. of_machine_is_compatible("RackMac3,1"))
  198. return -ENODEV;
  199. return i2c_add_driver(&wf_lm75_driver);
  200. }
  201. static void __exit wf_lm75_sensor_exit(void)
  202. {
  203. i2c_del_driver(&wf_lm75_driver);
  204. }
  205. module_init(wf_lm75_sensor_init);
  206. module_exit(wf_lm75_sensor_exit);
  207. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  208. MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
  209. MODULE_LICENSE("GPL");