windfarm_max6690_sensor.c 4.7 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. kfree(max);
  69. }
  70. return rc;
  71. }
  72. static struct i2c_driver wf_max6690_driver;
  73. static struct i2c_client *wf_max6690_create(struct i2c_adapter *adapter,
  74. u8 addr, const char *loc)
  75. {
  76. struct i2c_board_info info;
  77. struct i2c_client *client;
  78. char *name;
  79. if (!strcmp(loc, "BACKSIDE"))
  80. name = "backside-temp";
  81. else if (!strcmp(loc, "NB Ambient"))
  82. name = "north-bridge-temp";
  83. else if (!strcmp(loc, "GPU Ambient"))
  84. name = "gpu-temp";
  85. else
  86. goto fail;
  87. memset(&info, 0, sizeof(struct i2c_board_info));
  88. info.addr = addr >> 1;
  89. info.platform_data = name;
  90. strlcpy(info.type, "wf_max6690", I2C_NAME_SIZE);
  91. client = i2c_new_device(adapter, &info);
  92. if (client == NULL) {
  93. printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
  94. goto fail;
  95. }
  96. /*
  97. * Let i2c-core delete that device on driver removal.
  98. * This is safe because i2c-core holds the core_lock mutex for us.
  99. */
  100. list_add_tail(&client->detected, &wf_max6690_driver.clients);
  101. return client;
  102. fail:
  103. return NULL;
  104. }
  105. static int wf_max6690_attach(struct i2c_adapter *adapter)
  106. {
  107. struct device_node *busnode, *dev = NULL;
  108. struct pmac_i2c_bus *bus;
  109. const char *loc;
  110. bus = pmac_i2c_adapter_to_bus(adapter);
  111. if (bus == NULL)
  112. return -ENODEV;
  113. busnode = pmac_i2c_get_bus_node(bus);
  114. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  115. u8 addr;
  116. /* We must re-match the adapter in order to properly check
  117. * the channel on multibus setups
  118. */
  119. if (!pmac_i2c_match_adapter(dev, adapter))
  120. continue;
  121. if (!of_device_is_compatible(dev, "max6690"))
  122. continue;
  123. addr = pmac_i2c_get_dev_addr(dev);
  124. loc = of_get_property(dev, "hwsensor-location", NULL);
  125. if (loc == NULL || addr == 0)
  126. continue;
  127. printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);
  128. wf_max6690_create(adapter, addr, loc);
  129. }
  130. return 0;
  131. }
  132. static int wf_max6690_remove(struct i2c_client *client)
  133. {
  134. struct wf_6690_sensor *max = i2c_get_clientdata(client);
  135. max->i2c = NULL;
  136. wf_unregister_sensor(&max->sens);
  137. return 0;
  138. }
  139. static const struct i2c_device_id wf_max6690_id[] = {
  140. { "wf_max6690", 0 },
  141. { }
  142. };
  143. static struct i2c_driver wf_max6690_driver = {
  144. .driver = {
  145. .name = "wf_max6690",
  146. },
  147. .attach_adapter = wf_max6690_attach,
  148. .probe = wf_max6690_probe,
  149. .remove = wf_max6690_remove,
  150. .id_table = wf_max6690_id,
  151. };
  152. static int __init wf_max6690_sensor_init(void)
  153. {
  154. /* Don't register on old machines that use therm_pm72 for now */
  155. if (of_machine_is_compatible("PowerMac7,2") ||
  156. of_machine_is_compatible("PowerMac7,3") ||
  157. of_machine_is_compatible("RackMac3,1"))
  158. return -ENODEV;
  159. return i2c_add_driver(&wf_max6690_driver);
  160. }
  161. static void __exit wf_max6690_sensor_exit(void)
  162. {
  163. i2c_del_driver(&wf_max6690_driver);
  164. }
  165. module_init(wf_max6690_sensor_init);
  166. module_exit(wf_max6690_sensor_exit);
  167. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  168. MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
  169. MODULE_LICENSE("GPL");