windfarm_lm75_sensor.c 5.9 KB

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