windfarm_lm75_sensor.c 6.0 KB

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