windfarm_ad7417_sensor.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. AD7417 sensors
  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 "windfarm.h"
  19. #include "windfarm_mpu.h"
  20. #define VERSION "1.0"
  21. struct wf_ad7417_priv {
  22. struct kref ref;
  23. struct i2c_client *i2c;
  24. u8 config;
  25. u8 cpu;
  26. const struct mpu_data *mpu;
  27. struct wf_sensor sensors[5];
  28. struct mutex lock;
  29. };
  30. static int wf_ad7417_temp_get(struct wf_sensor *sr, s32 *value)
  31. {
  32. struct wf_ad7417_priv *pv = sr->priv;
  33. u8 buf[2];
  34. s16 raw;
  35. int rc;
  36. *value = 0;
  37. mutex_lock(&pv->lock);
  38. /* Read temp register */
  39. buf[0] = 0;
  40. rc = i2c_master_send(pv->i2c, buf, 1);
  41. if (rc < 0)
  42. goto error;
  43. rc = i2c_master_recv(pv->i2c, buf, 2);
  44. if (rc < 0)
  45. goto error;
  46. /* Read a a 16-bit signed value */
  47. raw = be16_to_cpup((__le16 *)buf);
  48. /* Convert 8.8-bit to 16.16 fixed point */
  49. *value = ((s32)raw) << 8;
  50. mutex_unlock(&pv->lock);
  51. return 0;
  52. error:
  53. mutex_unlock(&pv->lock);
  54. return -1;
  55. }
  56. /*
  57. * Scaling factors for the AD7417 ADC converters (except
  58. * for the CPU diode which is obtained from the EEPROM).
  59. * Those values are obtained from the property list of
  60. * the darwin driver
  61. */
  62. #define ADC_12V_CURRENT_SCALE 0x0320 /* _AD2 */
  63. #define ADC_CPU_VOLTAGE_SCALE 0x00a0 /* _AD3 */
  64. #define ADC_CPU_CURRENT_SCALE 0x1f40 /* _AD4 */
  65. static void wf_ad7417_adc_convert(struct wf_ad7417_priv *pv,
  66. int chan, s32 raw, s32 *value)
  67. {
  68. switch(chan) {
  69. case 1: /* Diode */
  70. *value = (raw * (s32)pv->mpu->mdiode +
  71. ((s32)pv->mpu->bdiode << 12)) >> 2;
  72. break;
  73. case 2: /* 12v current */
  74. *value = raw * ADC_12V_CURRENT_SCALE;
  75. break;
  76. case 3: /* core voltage */
  77. *value = raw * ADC_CPU_VOLTAGE_SCALE;
  78. break;
  79. case 4: /* core current */
  80. *value = raw * ADC_CPU_CURRENT_SCALE;
  81. break;
  82. }
  83. }
  84. static int wf_ad7417_adc_get(struct wf_sensor *sr, s32 *value)
  85. {
  86. struct wf_ad7417_priv *pv = sr->priv;
  87. int chan = sr - pv->sensors;
  88. int i, rc;
  89. u8 buf[2];
  90. u16 raw;
  91. *value = 0;
  92. mutex_lock(&pv->lock);
  93. for (i = 0; i < 10; i++) {
  94. /* Set channel */
  95. buf[0] = 1;
  96. buf[1] = (pv->config & 0x1f) | (chan << 5);
  97. rc = i2c_master_send(pv->i2c, buf, 2);
  98. if (rc < 0)
  99. goto error;
  100. /* Wait for conversion */
  101. msleep(1);
  102. /* Switch to data register */
  103. buf[0] = 4;
  104. rc = i2c_master_send(pv->i2c, buf, 1);
  105. if (rc < 0)
  106. goto error;
  107. /* Read result */
  108. rc = i2c_master_recv(pv->i2c, buf, 2);
  109. if (rc < 0)
  110. goto error;
  111. /* Read a a 16-bit signed value */
  112. raw = be16_to_cpup((__le16 *)buf) >> 6;
  113. wf_ad7417_adc_convert(pv, chan, raw, value);
  114. dev_vdbg(&pv->i2c->dev, "ADC chan %d [%s]"
  115. " raw value: 0x%x, conv to: 0x%08x\n",
  116. chan, sr->name, raw, *value);
  117. mutex_unlock(&pv->lock);
  118. return 0;
  119. error:
  120. dev_dbg(&pv->i2c->dev,
  121. "Error reading ADC, try %d...\n", i);
  122. if (i < 9)
  123. msleep(10);
  124. }
  125. mutex_unlock(&pv->lock);
  126. return -1;
  127. }
  128. static void wf_ad7417_release(struct kref *ref)
  129. {
  130. struct wf_ad7417_priv *pv = container_of(ref,
  131. struct wf_ad7417_priv, ref);
  132. kfree(pv);
  133. }
  134. static void wf_ad7417_sensor_release(struct wf_sensor *sr)
  135. {
  136. struct wf_ad7417_priv *pv = sr->priv;
  137. kfree(sr->name);
  138. kref_put(&pv->ref, wf_ad7417_release);
  139. }
  140. static const struct wf_sensor_ops wf_ad7417_temp_ops = {
  141. .get_value = wf_ad7417_temp_get,
  142. .release = wf_ad7417_sensor_release,
  143. .owner = THIS_MODULE,
  144. };
  145. static const struct wf_sensor_ops wf_ad7417_adc_ops = {
  146. .get_value = wf_ad7417_adc_get,
  147. .release = wf_ad7417_sensor_release,
  148. .owner = THIS_MODULE,
  149. };
  150. static void wf_ad7417_add_sensor(struct wf_ad7417_priv *pv,
  151. int index, const char *name,
  152. const struct wf_sensor_ops *ops)
  153. {
  154. pv->sensors[index].name = kasprintf(GFP_KERNEL, "%s-%d", name, pv->cpu);
  155. pv->sensors[index].priv = pv;
  156. pv->sensors[index].ops = ops;
  157. if (!wf_register_sensor(&pv->sensors[index]))
  158. kref_get(&pv->ref);
  159. }
  160. static void wf_ad7417_init_chip(struct wf_ad7417_priv *pv)
  161. {
  162. int rc;
  163. u8 buf[2];
  164. u8 config = 0;
  165. /*
  166. * Read ADC the configuration register and cache it. We
  167. * also make sure Config2 contains proper values, I've seen
  168. * cases where we got stale grabage in there, thus preventing
  169. * proper reading of conv. values
  170. */
  171. /* Clear Config2 */
  172. buf[0] = 5;
  173. buf[1] = 0;
  174. i2c_master_send(pv->i2c, buf, 2);
  175. /* Read & cache Config1 */
  176. buf[0] = 1;
  177. rc = i2c_master_send(pv->i2c, buf, 1);
  178. if (rc > 0) {
  179. rc = i2c_master_recv(pv->i2c, buf, 1);
  180. if (rc > 0) {
  181. config = buf[0];
  182. dev_dbg(&pv->i2c->dev, "ADC config reg: %02x\n",
  183. config);
  184. /* Disable shutdown mode */
  185. config &= 0xfe;
  186. buf[0] = 1;
  187. buf[1] = config;
  188. rc = i2c_master_send(pv->i2c, buf, 2);
  189. }
  190. }
  191. if (rc <= 0)
  192. dev_err(&pv->i2c->dev, "Error reading ADC config\n");
  193. pv->config = config;
  194. }
  195. static int wf_ad7417_probe(struct i2c_client *client)
  196. {
  197. struct wf_ad7417_priv *pv;
  198. const struct mpu_data *mpu;
  199. const char *loc;
  200. int cpu_nr;
  201. loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
  202. if (!loc) {
  203. dev_warn(&client->dev, "Missing hwsensor-location property!\n");
  204. return -ENXIO;
  205. }
  206. /*
  207. * Identify which CPU we belong to by looking at the first entry
  208. * in the hwsensor-location list
  209. */
  210. if (!strncmp(loc, "CPU A", 5))
  211. cpu_nr = 0;
  212. else if (!strncmp(loc, "CPU B", 5))
  213. cpu_nr = 1;
  214. else {
  215. pr_err("wf_ad7417: Can't identify location %s\n", loc);
  216. return -ENXIO;
  217. }
  218. mpu = wf_get_mpu(cpu_nr);
  219. if (!mpu) {
  220. dev_err(&client->dev, "Failed to retrieve MPU data\n");
  221. return -ENXIO;
  222. }
  223. pv = kzalloc_obj(struct wf_ad7417_priv);
  224. if (pv == NULL)
  225. return -ENODEV;
  226. kref_init(&pv->ref);
  227. mutex_init(&pv->lock);
  228. pv->i2c = client;
  229. pv->cpu = cpu_nr;
  230. pv->mpu = mpu;
  231. dev_set_drvdata(&client->dev, pv);
  232. /* Initialize the chip */
  233. wf_ad7417_init_chip(pv);
  234. /*
  235. * We cannot rely on Apple device-tree giving us child
  236. * node with the names of the individual sensors so we
  237. * just hard code what we know about them
  238. */
  239. wf_ad7417_add_sensor(pv, 0, "cpu-amb-temp", &wf_ad7417_temp_ops);
  240. wf_ad7417_add_sensor(pv, 1, "cpu-diode-temp", &wf_ad7417_adc_ops);
  241. wf_ad7417_add_sensor(pv, 2, "cpu-12v-current", &wf_ad7417_adc_ops);
  242. wf_ad7417_add_sensor(pv, 3, "cpu-voltage", &wf_ad7417_adc_ops);
  243. wf_ad7417_add_sensor(pv, 4, "cpu-current", &wf_ad7417_adc_ops);
  244. return 0;
  245. }
  246. static void wf_ad7417_remove(struct i2c_client *client)
  247. {
  248. struct wf_ad7417_priv *pv = dev_get_drvdata(&client->dev);
  249. int i;
  250. /* Mark client detached */
  251. pv->i2c = NULL;
  252. /* Release sensor */
  253. for (i = 0; i < 5; i++)
  254. wf_unregister_sensor(&pv->sensors[i]);
  255. kref_put(&pv->ref, wf_ad7417_release);
  256. }
  257. static const struct i2c_device_id wf_ad7417_id[] = {
  258. { "MAC,ad7417" },
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(i2c, wf_ad7417_id);
  262. static const struct of_device_id wf_ad7417_of_id[] = {
  263. { .compatible = "ad7417", },
  264. { }
  265. };
  266. MODULE_DEVICE_TABLE(of, wf_ad7417_of_id);
  267. static struct i2c_driver wf_ad7417_driver = {
  268. .driver = {
  269. .name = "wf_ad7417",
  270. .of_match_table = wf_ad7417_of_id,
  271. },
  272. .probe = wf_ad7417_probe,
  273. .remove = wf_ad7417_remove,
  274. .id_table = wf_ad7417_id,
  275. };
  276. static int wf_ad7417_init(void)
  277. {
  278. /* This is only supported on these machines */
  279. if (!of_machine_is_compatible("PowerMac7,2") &&
  280. !of_machine_is_compatible("PowerMac7,3") &&
  281. !of_machine_is_compatible("RackMac3,1"))
  282. return -ENODEV;
  283. return i2c_add_driver(&wf_ad7417_driver);
  284. }
  285. static void wf_ad7417_exit(void)
  286. {
  287. i2c_del_driver(&wf_ad7417_driver);
  288. }
  289. module_init(wf_ad7417_init);
  290. module_exit(wf_ad7417_exit);
  291. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  292. MODULE_DESCRIPTION("ad7417 sensor driver for PowerMacs");
  293. MODULE_LICENSE("GPL");