windfarm_max6690_sensor.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Windfarm PowerMac thermal control. MAX6690 sensor.
  3. *
  4. * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
  5. *
  6. * Use and redistribute under the terms of the GNU GPL v2.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <asm/prom.h>
  15. #include <asm/pmac_low_i2c.h>
  16. #include "windfarm.h"
  17. #define VERSION "0.2"
  18. /* This currently only exports the external temperature sensor,
  19. since that's all the control loops need. */
  20. /* Some MAX6690 register numbers */
  21. #define MAX6690_INTERNAL_TEMP 0
  22. #define MAX6690_EXTERNAL_TEMP 1
  23. struct wf_6690_sensor {
  24. struct i2c_client i2c;
  25. struct wf_sensor sens;
  26. };
  27. #define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)
  28. #define i2c_to_6690(x) container_of((x), struct wf_6690_sensor, i2c)
  29. static int wf_max6690_attach(struct i2c_adapter *adapter);
  30. static int wf_max6690_detach(struct i2c_client *client);
  31. static struct i2c_driver wf_max6690_driver = {
  32. .driver = {
  33. .name = "wf_max6690",
  34. },
  35. .attach_adapter = wf_max6690_attach,
  36. .detach_client = wf_max6690_detach,
  37. };
  38. static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
  39. {
  40. struct wf_6690_sensor *max = wf_to_6690(sr);
  41. s32 data;
  42. if (max->i2c.adapter == NULL)
  43. return -ENODEV;
  44. /* chip gets initialized by firmware */
  45. data = i2c_smbus_read_byte_data(&max->i2c, MAX6690_EXTERNAL_TEMP);
  46. if (data < 0)
  47. return data;
  48. *value = data << 16;
  49. return 0;
  50. }
  51. static void wf_max6690_release(struct wf_sensor *sr)
  52. {
  53. struct wf_6690_sensor *max = wf_to_6690(sr);
  54. if (max->i2c.adapter) {
  55. i2c_detach_client(&max->i2c);
  56. max->i2c.adapter = NULL;
  57. }
  58. kfree(max);
  59. }
  60. static struct wf_sensor_ops wf_max6690_ops = {
  61. .get_value = wf_max6690_get,
  62. .release = wf_max6690_release,
  63. .owner = THIS_MODULE,
  64. };
  65. static void wf_max6690_create(struct i2c_adapter *adapter, u8 addr)
  66. {
  67. struct wf_6690_sensor *max;
  68. char *name = "backside-temp";
  69. max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
  70. if (max == NULL) {
  71. printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor %s: "
  72. "no memory\n", name);
  73. return;
  74. }
  75. max->sens.ops = &wf_max6690_ops;
  76. max->sens.name = name;
  77. max->i2c.addr = addr >> 1;
  78. max->i2c.adapter = adapter;
  79. max->i2c.driver = &wf_max6690_driver;
  80. strncpy(max->i2c.name, name, I2C_NAME_SIZE-1);
  81. if (i2c_attach_client(&max->i2c)) {
  82. printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
  83. goto fail;
  84. }
  85. if (wf_register_sensor(&max->sens)) {
  86. i2c_detach_client(&max->i2c);
  87. goto fail;
  88. }
  89. return;
  90. fail:
  91. kfree(max);
  92. }
  93. static int wf_max6690_attach(struct i2c_adapter *adapter)
  94. {
  95. struct device_node *busnode, *dev = NULL;
  96. struct pmac_i2c_bus *bus;
  97. const char *loc;
  98. bus = pmac_i2c_adapter_to_bus(adapter);
  99. if (bus == NULL)
  100. return -ENODEV;
  101. busnode = pmac_i2c_get_bus_node(bus);
  102. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  103. u8 addr;
  104. /* We must re-match the adapter in order to properly check
  105. * the channel on multibus setups
  106. */
  107. if (!pmac_i2c_match_adapter(dev, adapter))
  108. continue;
  109. if (!device_is_compatible(dev, "max6690"))
  110. continue;
  111. addr = pmac_i2c_get_dev_addr(dev);
  112. loc = get_property(dev, "hwsensor-location", NULL);
  113. if (loc == NULL || addr == 0)
  114. continue;
  115. printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);
  116. if (strcmp(loc, "BACKSIDE"))
  117. continue;
  118. wf_max6690_create(adapter, addr);
  119. }
  120. return 0;
  121. }
  122. static int wf_max6690_detach(struct i2c_client *client)
  123. {
  124. struct wf_6690_sensor *max = i2c_to_6690(client);
  125. max->i2c.adapter = NULL;
  126. wf_unregister_sensor(&max->sens);
  127. return 0;
  128. }
  129. static int __init wf_max6690_sensor_init(void)
  130. {
  131. /* Don't register on old machines that use therm_pm72 for now */
  132. if (machine_is_compatible("PowerMac7,2") ||
  133. machine_is_compatible("PowerMac7,3") ||
  134. machine_is_compatible("RackMac3,1"))
  135. return -ENODEV;
  136. return i2c_add_driver(&wf_max6690_driver);
  137. }
  138. static void __exit wf_max6690_sensor_exit(void)
  139. {
  140. i2c_del_driver(&wf_max6690_driver);
  141. }
  142. module_init(wf_max6690_sensor_init);
  143. module_exit(wf_max6690_sensor_exit);
  144. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  145. MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
  146. MODULE_LICENSE("GPL");