windfarm_ad7417_sensor.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. const struct i2c_device_id *id)
  197. {
  198. struct wf_ad7417_priv *pv;
  199. const struct mpu_data *mpu;
  200. const char *loc;
  201. int cpu_nr;
  202. loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
  203. if (!loc) {
  204. dev_warn(&client->dev, "Missing hwsensor-location property!\n");
  205. return -ENXIO;
  206. }
  207. /*
  208. * Identify which CPU we belong to by looking at the first entry
  209. * in the hwsensor-location list
  210. */
  211. if (!strncmp(loc, "CPU A", 5))
  212. cpu_nr = 0;
  213. else if (!strncmp(loc, "CPU B", 5))
  214. cpu_nr = 1;
  215. else {
  216. pr_err("wf_ad7417: Can't identify location %s\n", loc);
  217. return -ENXIO;
  218. }
  219. mpu = wf_get_mpu(cpu_nr);
  220. if (!mpu) {
  221. dev_err(&client->dev, "Failed to retrieve MPU data\n");
  222. return -ENXIO;
  223. }
  224. pv = kzalloc(sizeof(struct wf_ad7417_priv), GFP_KERNEL);
  225. if (pv == NULL)
  226. return -ENODEV;
  227. kref_init(&pv->ref);
  228. mutex_init(&pv->lock);
  229. pv->i2c = client;
  230. pv->cpu = cpu_nr;
  231. pv->mpu = mpu;
  232. dev_set_drvdata(&client->dev, pv);
  233. /* Initialize the chip */
  234. wf_ad7417_init_chip(pv);
  235. /*
  236. * We cannot rely on Apple device-tree giving us child
  237. * node with the names of the individual sensors so we
  238. * just hard code what we know about them
  239. */
  240. wf_ad7417_add_sensor(pv, 0, "cpu-amb-temp", &wf_ad7417_temp_ops);
  241. wf_ad7417_add_sensor(pv, 1, "cpu-diode-temp", &wf_ad7417_adc_ops);
  242. wf_ad7417_add_sensor(pv, 2, "cpu-12v-current", &wf_ad7417_adc_ops);
  243. wf_ad7417_add_sensor(pv, 3, "cpu-voltage", &wf_ad7417_adc_ops);
  244. wf_ad7417_add_sensor(pv, 4, "cpu-current", &wf_ad7417_adc_ops);
  245. return 0;
  246. }
  247. static void wf_ad7417_remove(struct i2c_client *client)
  248. {
  249. struct wf_ad7417_priv *pv = dev_get_drvdata(&client->dev);
  250. int i;
  251. /* Mark client detached */
  252. pv->i2c = NULL;
  253. /* Release sensor */
  254. for (i = 0; i < 5; i++)
  255. wf_unregister_sensor(&pv->sensors[i]);
  256. kref_put(&pv->ref, wf_ad7417_release);
  257. }
  258. static const struct i2c_device_id wf_ad7417_id[] = {
  259. { "MAC,ad7417", 0 },
  260. { }
  261. };
  262. MODULE_DEVICE_TABLE(i2c, wf_ad7417_id);
  263. static const struct of_device_id wf_ad7417_of_id[] = {
  264. { .compatible = "ad7417", },
  265. { }
  266. };
  267. MODULE_DEVICE_TABLE(of, wf_ad7417_of_id);
  268. static struct i2c_driver wf_ad7417_driver = {
  269. .driver = {
  270. .name = "wf_ad7417",
  271. .of_match_table = wf_ad7417_of_id,
  272. },
  273. .probe = wf_ad7417_probe,
  274. .remove = wf_ad7417_remove,
  275. .id_table = wf_ad7417_id,
  276. };
  277. static int wf_ad7417_init(void)
  278. {
  279. /* This is only supported on these machines */
  280. if (!of_machine_is_compatible("PowerMac7,2") &&
  281. !of_machine_is_compatible("PowerMac7,3") &&
  282. !of_machine_is_compatible("RackMac3,1"))
  283. return -ENODEV;
  284. return i2c_add_driver(&wf_ad7417_driver);
  285. }
  286. static void wf_ad7417_exit(void)
  287. {
  288. i2c_del_driver(&wf_ad7417_driver);
  289. }
  290. module_init(wf_ad7417_init);
  291. module_exit(wf_ad7417_exit);
  292. MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");
  293. MODULE_DESCRIPTION("ad7417 sensor driver for PowerMacs");
  294. MODULE_LICENSE("GPL");