windfarm_max6690_sensor.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
  29. {
  30. struct wf_6690_sensor *max = wf_to_6690(sr);
  31. s32 data;
  32. if (max->i2c == NULL)
  33. return -ENODEV;
  34. /* chip gets initialized by firmware */
  35. data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP);
  36. if (data < 0)
  37. return data;
  38. *value = data << 16;
  39. return 0;
  40. }
  41. static void wf_max6690_release(struct wf_sensor *sr)
  42. {
  43. struct wf_6690_sensor *max = wf_to_6690(sr);
  44. kfree(max);
  45. }
  46. static struct wf_sensor_ops wf_max6690_ops = {
  47. .get_value = wf_max6690_get,
  48. .release = wf_max6690_release,
  49. .owner = THIS_MODULE,
  50. };
  51. static int wf_max6690_probe(struct i2c_client *client,
  52. const struct i2c_device_id *id)
  53. {
  54. struct wf_6690_sensor *max;
  55. int rc;
  56. max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
  57. if (max == NULL) {
  58. printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "
  59. "no memory\n");
  60. return -ENOMEM;
  61. }
  62. max->i2c = client;
  63. max->sens.name = client->dev.platform_data;
  64. max->sens.ops = &wf_max6690_ops;
  65. i2c_set_clientdata(client, max);
  66. rc = wf_register_sensor(&max->sens);
  67. if (rc) {
  68. i2c_set_clientdata(client, NULL);
  69. kfree(max);
  70. }
  71. return rc;
  72. }
  73. static struct i2c_driver wf_max6690_driver;
  74. static struct i2c_client *wf_max6690_create(struct i2c_adapter *adapter,
  75. u8 addr, const char *loc)
  76. {
  77. struct i2c_board_info info;
  78. struct i2c_client *client;
  79. char *name;
  80. if (!strcmp(loc, "BACKSIDE"))
  81. name = "backside-temp";
  82. else if (!strcmp(loc, "NB Ambient"))
  83. name = "north-bridge-temp";
  84. else if (!strcmp(loc, "GPU Ambient"))
  85. name = "gpu-temp";
  86. else
  87. goto fail;
  88. memset(&info, 0, sizeof(struct i2c_board_info));
  89. info.addr = addr >> 1;
  90. info.platform_data = name;
  91. strlcpy(info.type, "wf_max6690", I2C_NAME_SIZE);
  92. client = i2c_new_device(adapter, &info);
  93. if (client == NULL) {
  94. printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
  95. goto fail;
  96. }
  97. /*
  98. * Let i2c-core delete that device on driver removal.
  99. * This is safe because i2c-core holds the core_lock mutex for us.
  100. */
  101. list_add_tail(&client->detected, &wf_max6690_driver.clients);
  102. return client;
  103. fail:
  104. return NULL;
  105. }
  106. static int wf_max6690_attach(struct i2c_adapter *adapter)
  107. {
  108. struct device_node *busnode, *dev = NULL;
  109. struct pmac_i2c_bus *bus;
  110. const char *loc;
  111. bus = pmac_i2c_adapter_to_bus(adapter);
  112. if (bus == NULL)
  113. return -ENODEV;
  114. busnode = pmac_i2c_get_bus_node(bus);
  115. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  116. u8 addr;
  117. /* We must re-match the adapter in order to properly check
  118. * the channel on multibus setups
  119. */
  120. if (!pmac_i2c_match_adapter(dev, adapter))
  121. continue;
  122. if (!of_device_is_compatible(dev, "max6690"))
  123. continue;
  124. addr = pmac_i2c_get_dev_addr(dev);
  125. loc = of_get_property(dev, "hwsensor-location", NULL);
  126. if (loc == NULL || addr == 0)
  127. continue;
  128. printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);
  129. wf_max6690_create(adapter, addr, loc);
  130. }
  131. return 0;
  132. }
  133. static int wf_max6690_remove(struct i2c_client *client)
  134. {
  135. struct wf_6690_sensor *max = i2c_get_clientdata(client);
  136. max->i2c = NULL;
  137. wf_unregister_sensor(&max->sens);
  138. return 0;
  139. }
  140. static const struct i2c_device_id wf_max6690_id[] = {
  141. { "wf_max6690", 0 },
  142. { }
  143. };
  144. static struct i2c_driver wf_max6690_driver = {
  145. .driver = {
  146. .name = "wf_max6690",
  147. },
  148. .attach_adapter = wf_max6690_attach,
  149. .probe = wf_max6690_probe,
  150. .remove = wf_max6690_remove,
  151. .id_table = wf_max6690_id,
  152. };
  153. static int __init wf_max6690_sensor_init(void)
  154. {
  155. /* Don't register on old machines that use therm_pm72 for now */
  156. if (machine_is_compatible("PowerMac7,2") ||
  157. machine_is_compatible("PowerMac7,3") ||
  158. machine_is_compatible("RackMac3,1"))
  159. return -ENODEV;
  160. return i2c_add_driver(&wf_max6690_driver);
  161. }
  162. static void __exit wf_max6690_sensor_exit(void)
  163. {
  164. i2c_del_driver(&wf_max6690_driver);
  165. }
  166. module_init(wf_max6690_sensor_init);
  167. module_exit(wf_max6690_sensor_exit);
  168. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  169. MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
  170. MODULE_LICENSE("GPL");