windfarm_lm75_sensor.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. LM75 sensor
  4. *
  5. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  6. * <[email protected]>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/wait.h>
  15. #include <linux/i2c.h>
  16. #include <linux/of_device.h>
  17. #include <asm/machdep.h>
  18. #include <asm/io.h>
  19. #include <asm/sections.h>
  20. #include <asm/pmac_low_i2c.h>
  21. #include "windfarm.h"
  22. #define VERSION "1.0"
  23. #undef DEBUG
  24. #ifdef DEBUG
  25. #define DBG(args...) printk(args)
  26. #else
  27. #define DBG(args...) do { } while(0)
  28. #endif
  29. struct wf_lm75_sensor {
  30. unsigned int ds1775 : 1;
  31. unsigned int inited : 1;
  32. struct i2c_client *i2c;
  33. struct wf_sensor sens;
  34. };
  35. #define wf_to_lm75(c) container_of(c, struct wf_lm75_sensor, sens)
  36. static int wf_lm75_get(struct wf_sensor *sr, s32 *value)
  37. {
  38. struct wf_lm75_sensor *lm = wf_to_lm75(sr);
  39. s32 data;
  40. if (lm->i2c == NULL)
  41. return -ENODEV;
  42. /* Init chip if necessary */
  43. if (!lm->inited) {
  44. u8 cfg_new, cfg = (u8)i2c_smbus_read_byte_data(lm->i2c, 1);
  45. DBG("wf_lm75: Initializing %s, cfg was: %02x\n",
  46. sr->name, cfg);
  47. /* clear shutdown bit, keep other settings as left by
  48. * the firmware for now
  49. */
  50. cfg_new = cfg & ~0x01;
  51. i2c_smbus_write_byte_data(lm->i2c, 1, cfg_new);
  52. lm->inited = 1;
  53. /* If we just powered it up, let's wait 200 ms */
  54. msleep(200);
  55. }
  56. /* Read temperature register */
  57. data = (s32)le16_to_cpu(i2c_smbus_read_word_data(lm->i2c, 0));
  58. data <<= 8;
  59. *value = data;
  60. return 0;
  61. }
  62. static void wf_lm75_release(struct wf_sensor *sr)
  63. {
  64. struct wf_lm75_sensor *lm = wf_to_lm75(sr);
  65. kfree(lm);
  66. }
  67. static const struct wf_sensor_ops wf_lm75_ops = {
  68. .get_value = wf_lm75_get,
  69. .release = wf_lm75_release,
  70. .owner = THIS_MODULE,
  71. };
  72. static int wf_lm75_probe(struct i2c_client *client,
  73. const struct i2c_device_id *id)
  74. {
  75. struct wf_lm75_sensor *lm;
  76. int rc, ds1775;
  77. const char *name, *loc;
  78. if (id)
  79. ds1775 = id->driver_data;
  80. else
  81. ds1775 = !!of_device_get_match_data(&client->dev);
  82. DBG("wf_lm75: creating %s device at address 0x%02x\n",
  83. ds1775 ? "ds1775" : "lm75", client->addr);
  84. loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
  85. if (!loc) {
  86. dev_warn(&client->dev, "Missing hwsensor-location property!\n");
  87. return -ENXIO;
  88. }
  89. /* Usual rant about sensor names not beeing very consistent in
  90. * the device-tree, oh well ...
  91. * Add more entries below as you deal with more setups
  92. */
  93. if (!strcmp(loc, "Hard drive") || !strcmp(loc, "DRIVE BAY"))
  94. name = "hd-temp";
  95. else if (!strcmp(loc, "Incoming Air Temp"))
  96. name = "incoming-air-temp";
  97. else if (!strcmp(loc, "ODD Temp"))
  98. name = "optical-drive-temp";
  99. else if (!strcmp(loc, "HD Temp"))
  100. name = "hard-drive-temp";
  101. else if (!strcmp(loc, "PCI SLOTS"))
  102. name = "slots-temp";
  103. else if (!strcmp(loc, "CPU A INLET"))
  104. name = "cpu-inlet-temp-0";
  105. else if (!strcmp(loc, "CPU B INLET"))
  106. name = "cpu-inlet-temp-1";
  107. else
  108. return -ENXIO;
  109. lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
  110. if (lm == NULL)
  111. return -ENODEV;
  112. lm->inited = 0;
  113. lm->ds1775 = ds1775;
  114. lm->i2c = client;
  115. lm->sens.name = name;
  116. lm->sens.ops = &wf_lm75_ops;
  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_lm75_remove(struct i2c_client *client)
  124. {
  125. struct wf_lm75_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_lm75_id[] = {
  132. { "MAC,lm75", 0 },
  133. { "MAC,ds1775", 1 },
  134. { }
  135. };
  136. MODULE_DEVICE_TABLE(i2c, wf_lm75_id);
  137. static const struct of_device_id wf_lm75_of_id[] = {
  138. { .compatible = "lm75", .data = (void *)0},
  139. { .compatible = "ds1775", .data = (void *)1 },
  140. { }
  141. };
  142. MODULE_DEVICE_TABLE(of, wf_lm75_of_id);
  143. static struct i2c_driver wf_lm75_driver = {
  144. .driver = {
  145. .name = "wf_lm75",
  146. .of_match_table = wf_lm75_of_id,
  147. },
  148. .probe = wf_lm75_probe,
  149. .remove = wf_lm75_remove,
  150. .id_table = wf_lm75_id,
  151. };
  152. module_i2c_driver(wf_lm75_driver);
  153. MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");
  154. MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
  155. MODULE_LICENSE("GPL");