windfarm_max6690_sensor.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <linux/i2c-dev.h>
  15. #include <asm/prom.h>
  16. #include <asm/pmac_low_i2c.h>
  17. #include "windfarm.h"
  18. #define VERSION "0.1"
  19. /* This currently only exports the external temperature sensor,
  20. since that's all the control loops need. */
  21. /* Some MAX6690 register numbers */
  22. #define MAX6690_INTERNAL_TEMP 0
  23. #define MAX6690_EXTERNAL_TEMP 1
  24. struct wf_6690_sensor {
  25. struct i2c_client i2c;
  26. struct wf_sensor sens;
  27. };
  28. #define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)
  29. #define i2c_to_6690(x) container_of((x), struct wf_6690_sensor, i2c)
  30. static int wf_max6690_attach(struct i2c_adapter *adapter);
  31. static int wf_max6690_detach(struct i2c_client *client);
  32. static struct i2c_driver wf_max6690_driver = {
  33. .driver = {
  34. .name = "wf_max6690",
  35. },
  36. .attach_adapter = wf_max6690_attach,
  37. .detach_client = wf_max6690_detach,
  38. };
  39. static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
  40. {
  41. struct wf_6690_sensor *max = wf_to_6690(sr);
  42. s32 data;
  43. if (max->i2c.adapter == NULL)
  44. return -ENODEV;
  45. /* chip gets initialized by firmware */
  46. data = i2c_smbus_read_byte_data(&max->i2c, MAX6690_EXTERNAL_TEMP);
  47. if (data < 0)
  48. return data;
  49. *value = data << 16;
  50. return 0;
  51. }
  52. static void wf_max6690_release(struct wf_sensor *sr)
  53. {
  54. struct wf_6690_sensor *max = wf_to_6690(sr);
  55. if (max->i2c.adapter) {
  56. i2c_detach_client(&max->i2c);
  57. max->i2c.adapter = NULL;
  58. }
  59. kfree(max);
  60. }
  61. static struct wf_sensor_ops wf_max6690_ops = {
  62. .get_value = wf_max6690_get,
  63. .release = wf_max6690_release,
  64. .owner = THIS_MODULE,
  65. };
  66. static void wf_max6690_create(struct i2c_adapter *adapter, u8 addr)
  67. {
  68. struct wf_6690_sensor *max;
  69. char *name = "u4-temp";
  70. max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
  71. if (max == NULL) {
  72. printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor %s: "
  73. "no memory\n", name);
  74. return;
  75. }
  76. max->sens.ops = &wf_max6690_ops;
  77. max->sens.name = name;
  78. max->i2c.addr = addr >> 1;
  79. max->i2c.adapter = adapter;
  80. max->i2c.driver = &wf_max6690_driver;
  81. strncpy(max->i2c.name, name, I2C_NAME_SIZE-1);
  82. if (i2c_attach_client(&max->i2c)) {
  83. printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
  84. goto fail;
  85. }
  86. if (wf_register_sensor(&max->sens)) {
  87. i2c_detach_client(&max->i2c);
  88. goto fail;
  89. }
  90. return;
  91. fail:
  92. kfree(max);
  93. }
  94. static int wf_max6690_attach(struct i2c_adapter *adapter)
  95. {
  96. struct device_node *busnode, *dev = NULL;
  97. struct pmac_i2c_bus *bus;
  98. const char *loc;
  99. u32 *reg;
  100. bus = pmac_i2c_adapter_to_bus(adapter);
  101. if (bus == NULL)
  102. return -ENODEV;
  103. busnode = pmac_i2c_get_bus_node(bus);
  104. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  105. if (!device_is_compatible(dev, "max6690"))
  106. continue;
  107. loc = get_property(dev, "hwsensor-location", NULL);
  108. reg = (u32 *) get_property(dev, "reg", NULL);
  109. if (!loc || !reg)
  110. continue;
  111. printk("found max6690, loc=%s reg=%x\n", loc, *reg);
  112. if (strcmp(loc, "BACKSIDE"))
  113. continue;
  114. wf_max6690_create(adapter, *reg);
  115. }
  116. return 0;
  117. }
  118. static int wf_max6690_detach(struct i2c_client *client)
  119. {
  120. struct wf_6690_sensor *max = i2c_to_6690(client);
  121. max->i2c.adapter = NULL;
  122. wf_unregister_sensor(&max->sens);
  123. return 0;
  124. }
  125. static int __init wf_max6690_sensor_init(void)
  126. {
  127. return i2c_add_driver(&wf_max6690_driver);
  128. }
  129. static void __exit wf_max6690_sensor_exit(void)
  130. {
  131. i2c_del_driver(&wf_max6690_driver);
  132. }
  133. module_init(wf_max6690_sensor_init);
  134. module_exit(wf_max6690_sensor_exit);
  135. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  136. MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
  137. MODULE_LICENSE("GPL");