windfarm_lm87_sensor.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. LM87 sensor
  4. *
  5. * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/delay.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/wait.h>
  14. #include <linux/i2c.h>
  15. #include <asm/machdep.h>
  16. #include <asm/io.h>
  17. #include <asm/sections.h>
  18. #include <asm/pmac_low_i2c.h>
  19. #include "windfarm.h"
  20. #define VERSION "1.0"
  21. #undef DEBUG
  22. #ifdef DEBUG
  23. #define DBG(args...) printk(args)
  24. #else
  25. #define DBG(args...) do { } while(0)
  26. #endif
  27. struct wf_lm87_sensor {
  28. struct i2c_client *i2c;
  29. struct wf_sensor sens;
  30. };
  31. #define wf_to_lm87(c) container_of(c, struct wf_lm87_sensor, sens)
  32. static int wf_lm87_read_reg(struct i2c_client *chip, int reg)
  33. {
  34. int rc, tries = 0;
  35. u8 buf;
  36. for (;;) {
  37. /* Set address */
  38. buf = (u8)reg;
  39. rc = i2c_master_send(chip, &buf, 1);
  40. if (rc <= 0)
  41. goto error;
  42. rc = i2c_master_recv(chip, &buf, 1);
  43. if (rc <= 0)
  44. goto error;
  45. return (int)buf;
  46. error:
  47. DBG("wf_lm87: Error reading LM87, retrying...\n");
  48. if (++tries > 10) {
  49. printk(KERN_ERR "wf_lm87: Error reading LM87 !\n");
  50. return -EIO;
  51. }
  52. msleep(10);
  53. }
  54. }
  55. static int wf_lm87_get(struct wf_sensor *sr, s32 *value)
  56. {
  57. struct wf_lm87_sensor *lm = sr->priv;
  58. s32 temp;
  59. if (lm->i2c == NULL)
  60. return -ENODEV;
  61. #define LM87_INT_TEMP 0x27
  62. /* Read temperature register */
  63. temp = wf_lm87_read_reg(lm->i2c, LM87_INT_TEMP);
  64. if (temp < 0)
  65. return temp;
  66. *value = temp << 16;
  67. return 0;
  68. }
  69. static void wf_lm87_release(struct wf_sensor *sr)
  70. {
  71. struct wf_lm87_sensor *lm = wf_to_lm87(sr);
  72. kfree(lm);
  73. }
  74. static const struct wf_sensor_ops wf_lm87_ops = {
  75. .get_value = wf_lm87_get,
  76. .release = wf_lm87_release,
  77. .owner = THIS_MODULE,
  78. };
  79. static int wf_lm87_probe(struct i2c_client *client)
  80. {
  81. struct wf_lm87_sensor *lm;
  82. const char *name = NULL, *loc;
  83. struct device_node *np = NULL;
  84. int rc;
  85. /*
  86. * The lm87 contains a whole pile of sensors, additionally,
  87. * the Xserve G5 has several lm87's. However, for now we only
  88. * care about the internal temperature sensor
  89. */
  90. for_each_child_of_node(client->dev.of_node, np) {
  91. if (!of_node_name_eq(np, "int-temp"))
  92. continue;
  93. loc = of_get_property(np, "location", NULL);
  94. if (!loc)
  95. continue;
  96. if (strstr(loc, "DIMM"))
  97. name = "dimms-temp";
  98. else if (strstr(loc, "Processors"))
  99. name = "between-cpus-temp";
  100. if (name) {
  101. of_node_put(np);
  102. break;
  103. }
  104. }
  105. if (!name) {
  106. pr_warn("wf_lm87: Unsupported sensor %pOF\n",
  107. client->dev.of_node);
  108. return -ENODEV;
  109. }
  110. lm = kzalloc(sizeof(struct wf_lm87_sensor), GFP_KERNEL);
  111. if (lm == NULL)
  112. return -ENODEV;
  113. lm->i2c = client;
  114. lm->sens.name = name;
  115. lm->sens.ops = &wf_lm87_ops;
  116. lm->sens.priv = lm;
  117. i2c_set_clientdata(client, lm);
  118. rc = wf_register_sensor(&lm->sens);
  119. if (rc)
  120. kfree(lm);
  121. return rc;
  122. }
  123. static void wf_lm87_remove(struct i2c_client *client)
  124. {
  125. struct wf_lm87_sensor *lm = i2c_get_clientdata(client);
  126. /* Mark client detached */
  127. lm->i2c = NULL;
  128. /* release sensor */
  129. wf_unregister_sensor(&lm->sens);
  130. }
  131. static const struct i2c_device_id wf_lm87_id[] = {
  132. { "MAC,lm87cimt" },
  133. { }
  134. };
  135. MODULE_DEVICE_TABLE(i2c, wf_lm87_id);
  136. static const struct of_device_id wf_lm87_of_id[] = {
  137. { .compatible = "lm87cimt", },
  138. { }
  139. };
  140. MODULE_DEVICE_TABLE(of, wf_lm87_of_id);
  141. static struct i2c_driver wf_lm87_driver = {
  142. .driver = {
  143. .name = "wf_lm87",
  144. .of_match_table = wf_lm87_of_id,
  145. },
  146. .probe = wf_lm87_probe,
  147. .remove = wf_lm87_remove,
  148. .id_table = wf_lm87_id,
  149. };
  150. static int __init wf_lm87_sensor_init(void)
  151. {
  152. /* We only support this on the Xserve */
  153. if (!of_machine_is_compatible("RackMac3,1"))
  154. return -ENODEV;
  155. return i2c_add_driver(&wf_lm87_driver);
  156. }
  157. static void __exit wf_lm87_sensor_exit(void)
  158. {
  159. i2c_del_driver(&wf_lm87_driver);
  160. }
  161. module_init(wf_lm87_sensor_init);
  162. module_exit(wf_lm87_sensor_exit);
  163. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  164. MODULE_DESCRIPTION("LM87 sensor objects for PowerMacs thermal control");
  165. MODULE_LICENSE("GPL");