windfarm_ad7417_sensor.c 7.6 KB

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