windfarm_max6690_sensor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. MAX6690 sensor.
  4. *
  5. * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
  6. */
  7. #include <linux/types.h>
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <asm/pmac_low_i2c.h>
  14. #include "windfarm.h"
  15. #define VERSION "1.0"
  16. /* This currently only exports the external temperature sensor,
  17. since that's all the control loops need. */
  18. /* Some MAX6690 register numbers */
  19. #define MAX6690_INTERNAL_TEMP 0
  20. #define MAX6690_EXTERNAL_TEMP 1
  21. struct wf_6690_sensor {
  22. struct i2c_client *i2c;
  23. struct wf_sensor sens;
  24. };
  25. #define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)
  26. static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
  27. {
  28. struct wf_6690_sensor *max = wf_to_6690(sr);
  29. s32 data;
  30. if (max->i2c == NULL)
  31. return -ENODEV;
  32. /* chip gets initialized by firmware */
  33. data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP);
  34. if (data < 0)
  35. return data;
  36. *value = data << 16;
  37. return 0;
  38. }
  39. static void wf_max6690_release(struct wf_sensor *sr)
  40. {
  41. struct wf_6690_sensor *max = wf_to_6690(sr);
  42. kfree(max);
  43. }
  44. static const struct wf_sensor_ops wf_max6690_ops = {
  45. .get_value = wf_max6690_get,
  46. .release = wf_max6690_release,
  47. .owner = THIS_MODULE,
  48. };
  49. static int wf_max6690_probe(struct i2c_client *client,
  50. const struct i2c_device_id *id)
  51. {
  52. const char *name, *loc;
  53. struct wf_6690_sensor *max;
  54. int rc;
  55. loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
  56. if (!loc) {
  57. dev_warn(&client->dev, "Missing hwsensor-location property!\n");
  58. return -ENXIO;
  59. }
  60. /*
  61. * We only expose the external temperature register for
  62. * now as this is all we need for our control loops
  63. */
  64. if (!strcmp(loc, "BACKSIDE") || !strcmp(loc, "SYS CTRLR AMBIENT"))
  65. name = "backside-temp";
  66. else if (!strcmp(loc, "NB Ambient"))
  67. name = "north-bridge-temp";
  68. else if (!strcmp(loc, "GPU Ambient"))
  69. name = "gpu-temp";
  70. else
  71. return -ENXIO;
  72. max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
  73. if (max == NULL) {
  74. printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "
  75. "no memory\n");
  76. return -ENOMEM;
  77. }
  78. max->i2c = client;
  79. max->sens.name = name;
  80. max->sens.ops = &wf_max6690_ops;
  81. i2c_set_clientdata(client, max);
  82. rc = wf_register_sensor(&max->sens);
  83. if (rc)
  84. kfree(max);
  85. return rc;
  86. }
  87. static void wf_max6690_remove(struct i2c_client *client)
  88. {
  89. struct wf_6690_sensor *max = i2c_get_clientdata(client);
  90. max->i2c = NULL;
  91. wf_unregister_sensor(&max->sens);
  92. }
  93. static const struct i2c_device_id wf_max6690_id[] = {
  94. { "MAC,max6690", 0 },
  95. { }
  96. };
  97. MODULE_DEVICE_TABLE(i2c, wf_max6690_id);
  98. static const struct of_device_id wf_max6690_of_id[] = {
  99. { .compatible = "max6690", },
  100. { }
  101. };
  102. MODULE_DEVICE_TABLE(of, wf_max6690_of_id);
  103. static struct i2c_driver wf_max6690_driver = {
  104. .driver = {
  105. .name = "wf_max6690",
  106. .of_match_table = wf_max6690_of_id,
  107. },
  108. .probe = wf_max6690_probe,
  109. .remove = wf_max6690_remove,
  110. .id_table = wf_max6690_id,
  111. };
  112. module_i2c_driver(wf_max6690_driver);
  113. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  114. MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
  115. MODULE_LICENSE("GPL");