windfarm_max6690_sensor.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "1.0"
  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. const char *name, *loc;
  55. struct wf_6690_sensor *max;
  56. int rc;
  57. loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
  58. if (!loc) {
  59. dev_warn(&client->dev, "Missing hwsensor-location property!\n");
  60. return -ENXIO;
  61. }
  62. if (!strcmp(loc, "BACKSIDE"))
  63. name = "backside-temp";
  64. else if (!strcmp(loc, "NB Ambient"))
  65. name = "north-bridge-temp";
  66. else if (!strcmp(loc, "GPU Ambient"))
  67. name = "gpu-temp";
  68. else
  69. return -ENXIO;
  70. max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
  71. if (max == NULL) {
  72. printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "
  73. "no memory\n");
  74. return -ENOMEM;
  75. }
  76. max->i2c = client;
  77. max->sens.name = (char *)name; /* XXX fix constness in structure */
  78. max->sens.ops = &wf_max6690_ops;
  79. i2c_set_clientdata(client, max);
  80. rc = wf_register_sensor(&max->sens);
  81. if (rc)
  82. kfree(max);
  83. return rc;
  84. }
  85. static int wf_max6690_remove(struct i2c_client *client)
  86. {
  87. struct wf_6690_sensor *max = i2c_get_clientdata(client);
  88. max->i2c = NULL;
  89. wf_unregister_sensor(&max->sens);
  90. return 0;
  91. }
  92. static const struct i2c_device_id wf_max6690_id[] = {
  93. { "MAC,max6690", 0 },
  94. { }
  95. };
  96. MODULE_DEVICE_TABLE(i2c, wf_max6690_id);
  97. static struct i2c_driver wf_max6690_driver = {
  98. .driver = {
  99. .name = "wf_max6690",
  100. },
  101. .probe = wf_max6690_probe,
  102. .remove = wf_max6690_remove,
  103. .id_table = wf_max6690_id,
  104. };
  105. static int __init wf_max6690_sensor_init(void)
  106. {
  107. /* Don't register on old machines that use therm_pm72 for now */
  108. if (of_machine_is_compatible("PowerMac7,2") ||
  109. of_machine_is_compatible("PowerMac7,3") ||
  110. of_machine_is_compatible("RackMac3,1"))
  111. return -ENODEV;
  112. return i2c_add_driver(&wf_max6690_driver);
  113. }
  114. static void __exit wf_max6690_sensor_exit(void)
  115. {
  116. i2c_del_driver(&wf_max6690_driver);
  117. }
  118. module_init(wf_max6690_sensor_init);
  119. module_exit(wf_max6690_sensor_exit);
  120. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  121. MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
  122. MODULE_LICENSE("GPL");