windfarm_lm87_sensor.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. const struct i2c_device_id *id)
  81. {
  82. struct wf_lm87_sensor *lm;
  83. const char *name = NULL, *loc;
  84. struct device_node *np = NULL;
  85. int rc;
  86. /*
  87. * The lm87 contains a whole pile of sensors, additionally,
  88. * the Xserve G5 has several lm87's. However, for now we only
  89. * care about the internal temperature sensor
  90. */
  91. for_each_child_of_node(client->dev.of_node, np) {
  92. if (!of_node_name_eq(np, "int-temp"))
  93. continue;
  94. loc = of_get_property(np, "location", NULL);
  95. if (!loc)
  96. continue;
  97. if (strstr(loc, "DIMM"))
  98. name = "dimms-temp";
  99. else if (strstr(loc, "Processors"))
  100. name = "between-cpus-temp";
  101. if (name) {
  102. of_node_put(np);
  103. break;
  104. }
  105. }
  106. if (!name) {
  107. pr_warn("wf_lm87: Unsupported sensor %pOF\n",
  108. client->dev.of_node);
  109. return -ENODEV;
  110. }
  111. lm = kzalloc(sizeof(struct wf_lm87_sensor), GFP_KERNEL);
  112. if (lm == NULL)
  113. return -ENODEV;
  114. lm->i2c = client;
  115. lm->sens.name = name;
  116. lm->sens.ops = &wf_lm87_ops;
  117. lm->sens.priv = lm;
  118. i2c_set_clientdata(client, lm);
  119. rc = wf_register_sensor(&lm->sens);
  120. if (rc)
  121. kfree(lm);
  122. return rc;
  123. }
  124. static void wf_lm87_remove(struct i2c_client *client)
  125. {
  126. struct wf_lm87_sensor *lm = i2c_get_clientdata(client);
  127. /* Mark client detached */
  128. lm->i2c = NULL;
  129. /* release sensor */
  130. wf_unregister_sensor(&lm->sens);
  131. }
  132. static const struct i2c_device_id wf_lm87_id[] = {
  133. { "MAC,lm87cimt", 0 },
  134. { }
  135. };
  136. MODULE_DEVICE_TABLE(i2c, wf_lm87_id);
  137. static const struct of_device_id wf_lm87_of_id[] = {
  138. { .compatible = "lm87cimt", },
  139. { }
  140. };
  141. MODULE_DEVICE_TABLE(of, wf_lm87_of_id);
  142. static struct i2c_driver wf_lm87_driver = {
  143. .driver = {
  144. .name = "wf_lm87",
  145. .of_match_table = wf_lm87_of_id,
  146. },
  147. .probe = wf_lm87_probe,
  148. .remove = wf_lm87_remove,
  149. .id_table = wf_lm87_id,
  150. };
  151. static int __init wf_lm87_sensor_init(void)
  152. {
  153. /* We only support this on the Xserve */
  154. if (!of_machine_is_compatible("RackMac3,1"))
  155. return -ENODEV;
  156. return i2c_add_driver(&wf_lm87_driver);
  157. }
  158. static void __exit wf_lm87_sensor_exit(void)
  159. {
  160. i2c_del_driver(&wf_lm87_driver);
  161. }
  162. module_init(wf_lm87_sensor_init);
  163. module_exit(wf_lm87_sensor_exit);
  164. MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");
  165. MODULE_DESCRIPTION("LM87 sensor objects for PowerMacs thermal control");
  166. MODULE_LICENSE("GPL");