windfarm_smu_sat.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Windfarm PowerMac thermal control. SMU "satellite" controller sensors.
  3. *
  4. * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
  5. *
  6. * Released under the terms of the GNU GPL v2.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/wait.h>
  14. #include <linux/i2c.h>
  15. #include <linux/mutex.h>
  16. #include <asm/prom.h>
  17. #include <asm/smu.h>
  18. #include <asm/pmac_low_i2c.h>
  19. #include "windfarm.h"
  20. #define VERSION "1.0"
  21. #define DEBUG
  22. #ifdef DEBUG
  23. #define DBG(args...) printk(args)
  24. #else
  25. #define DBG(args...) do { } while(0)
  26. #endif
  27. /* If the cache is older than 800ms we'll refetch it */
  28. #define MAX_AGE msecs_to_jiffies(800)
  29. struct wf_sat {
  30. struct kref ref;
  31. int nr;
  32. struct mutex mutex;
  33. unsigned long last_read; /* jiffies when cache last updated */
  34. u8 cache[16];
  35. struct list_head sensors;
  36. struct i2c_client *i2c;
  37. struct device_node *node;
  38. };
  39. static struct wf_sat *sats[2];
  40. struct wf_sat_sensor {
  41. struct list_head link;
  42. int index;
  43. int index2; /* used for power sensors */
  44. int shift;
  45. struct wf_sat *sat;
  46. struct wf_sensor sens;
  47. };
  48. #define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
  49. struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
  50. unsigned int *size)
  51. {
  52. struct wf_sat *sat;
  53. int err;
  54. unsigned int i, len;
  55. u8 *buf;
  56. u8 data[4];
  57. /* TODO: Add the resulting partition to the device-tree */
  58. if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
  59. return NULL;
  60. err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
  61. if (err) {
  62. printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
  63. return NULL;
  64. }
  65. err = i2c_smbus_read_word_data(sat->i2c, 9);
  66. if (err < 0) {
  67. printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
  68. return NULL;
  69. }
  70. len = err;
  71. if (len == 0) {
  72. printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
  73. return NULL;
  74. }
  75. len = le16_to_cpu(len);
  76. len = (len + 3) & ~3;
  77. buf = kmalloc(len, GFP_KERNEL);
  78. if (buf == NULL)
  79. return NULL;
  80. for (i = 0; i < len; i += 4) {
  81. err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
  82. if (err < 0) {
  83. printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
  84. err);
  85. goto fail;
  86. }
  87. buf[i] = data[1];
  88. buf[i+1] = data[0];
  89. buf[i+2] = data[3];
  90. buf[i+3] = data[2];
  91. }
  92. #ifdef DEBUG
  93. DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);
  94. for (i = 0; i < len; ++i)
  95. DBG(" %x", buf[i]);
  96. DBG("\n");
  97. #endif
  98. if (size)
  99. *size = len;
  100. return (struct smu_sdbp_header *) buf;
  101. fail:
  102. kfree(buf);
  103. return NULL;
  104. }
  105. EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
  106. /* refresh the cache */
  107. static int wf_sat_read_cache(struct wf_sat *sat)
  108. {
  109. int err;
  110. err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
  111. if (err < 0)
  112. return err;
  113. sat->last_read = jiffies;
  114. #ifdef LOTSA_DEBUG
  115. {
  116. int i;
  117. DBG(KERN_DEBUG "wf_sat_get: data is");
  118. for (i = 0; i < 16; ++i)
  119. DBG(" %.2x", sat->cache[i]);
  120. DBG("\n");
  121. }
  122. #endif
  123. return 0;
  124. }
  125. static int wf_sat_sensor_get(struct wf_sensor *sr, s32 *value)
  126. {
  127. struct wf_sat_sensor *sens = wf_to_sat(sr);
  128. struct wf_sat *sat = sens->sat;
  129. int i, err;
  130. s32 val;
  131. if (sat->i2c == NULL)
  132. return -ENODEV;
  133. mutex_lock(&sat->mutex);
  134. if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
  135. err = wf_sat_read_cache(sat);
  136. if (err)
  137. goto fail;
  138. }
  139. i = sens->index * 2;
  140. val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
  141. if (sens->index2 >= 0) {
  142. i = sens->index2 * 2;
  143. /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
  144. val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
  145. }
  146. *value = val;
  147. err = 0;
  148. fail:
  149. mutex_unlock(&sat->mutex);
  150. return err;
  151. }
  152. static void wf_sat_release(struct kref *ref)
  153. {
  154. struct wf_sat *sat = container_of(ref, struct wf_sat, ref);
  155. if (sat->nr >= 0)
  156. sats[sat->nr] = NULL;
  157. kfree(sat);
  158. }
  159. static void wf_sat_sensor_release(struct wf_sensor *sr)
  160. {
  161. struct wf_sat_sensor *sens = wf_to_sat(sr);
  162. struct wf_sat *sat = sens->sat;
  163. kfree(sens);
  164. kref_put(&sat->ref, wf_sat_release);
  165. }
  166. static struct wf_sensor_ops wf_sat_ops = {
  167. .get_value = wf_sat_sensor_get,
  168. .release = wf_sat_sensor_release,
  169. .owner = THIS_MODULE,
  170. };
  171. static int wf_sat_probe(struct i2c_client *client,
  172. const struct i2c_device_id *id)
  173. {
  174. struct device_node *dev = client->dev.of_node;
  175. struct wf_sat *sat;
  176. struct wf_sat_sensor *sens;
  177. const u32 *reg;
  178. const char *loc, *type;
  179. u8 chip, core;
  180. struct device_node *child;
  181. int shift, cpu, index;
  182. char *name;
  183. int vsens[2], isens[2];
  184. sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
  185. if (sat == NULL)
  186. return -ENOMEM;
  187. sat->nr = -1;
  188. sat->node = of_node_get(dev);
  189. kref_init(&sat->ref);
  190. mutex_init(&sat->mutex);
  191. sat->i2c = client;
  192. INIT_LIST_HEAD(&sat->sensors);
  193. i2c_set_clientdata(client, sat);
  194. vsens[0] = vsens[1] = -1;
  195. isens[0] = isens[1] = -1;
  196. child = NULL;
  197. while ((child = of_get_next_child(dev, child)) != NULL) {
  198. reg = of_get_property(child, "reg", NULL);
  199. type = of_get_property(child, "device_type", NULL);
  200. loc = of_get_property(child, "location", NULL);
  201. if (reg == NULL || loc == NULL)
  202. continue;
  203. /* the cooked sensors are between 0x30 and 0x37 */
  204. if (*reg < 0x30 || *reg > 0x37)
  205. continue;
  206. index = *reg - 0x30;
  207. /* expect location to be CPU [AB][01] ... */
  208. if (strncmp(loc, "CPU ", 4) != 0)
  209. continue;
  210. chip = loc[4] - 'A';
  211. core = loc[5] - '0';
  212. if (chip > 1 || core > 1) {
  213. printk(KERN_ERR "wf_sat_create: don't understand "
  214. "location %s for %s\n", loc, child->full_name);
  215. continue;
  216. }
  217. cpu = 2 * chip + core;
  218. if (sat->nr < 0)
  219. sat->nr = chip;
  220. else if (sat->nr != chip) {
  221. printk(KERN_ERR "wf_sat_create: can't cope with "
  222. "multiple CPU chips on one SAT (%s)\n", loc);
  223. continue;
  224. }
  225. if (strcmp(type, "voltage-sensor") == 0) {
  226. name = "cpu-voltage";
  227. shift = 4;
  228. vsens[core] = index;
  229. } else if (strcmp(type, "current-sensor") == 0) {
  230. name = "cpu-current";
  231. shift = 8;
  232. isens[core] = index;
  233. } else if (strcmp(type, "temp-sensor") == 0) {
  234. name = "cpu-temp";
  235. shift = 10;
  236. } else
  237. continue; /* hmmm shouldn't happen */
  238. /* the +16 is enough for "cpu-voltage-n" */
  239. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  240. if (sens == NULL) {
  241. printk(KERN_ERR "wf_sat_create: couldn't create "
  242. "%s sensor %d (no memory)\n", name, cpu);
  243. continue;
  244. }
  245. sens->index = index;
  246. sens->index2 = -1;
  247. sens->shift = shift;
  248. sens->sat = sat;
  249. sens->sens.ops = &wf_sat_ops;
  250. sens->sens.name = (char *) (sens + 1);
  251. snprintf((char *)sens->sens.name, 16, "%s-%d", name, cpu);
  252. if (wf_register_sensor(&sens->sens))
  253. kfree(sens);
  254. else {
  255. list_add(&sens->link, &sat->sensors);
  256. kref_get(&sat->ref);
  257. }
  258. }
  259. /* make the power sensors */
  260. for (core = 0; core < 2; ++core) {
  261. if (vsens[core] < 0 || isens[core] < 0)
  262. continue;
  263. cpu = 2 * sat->nr + core;
  264. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  265. if (sens == NULL) {
  266. printk(KERN_ERR "wf_sat_create: couldn't create power "
  267. "sensor %d (no memory)\n", cpu);
  268. continue;
  269. }
  270. sens->index = vsens[core];
  271. sens->index2 = isens[core];
  272. sens->shift = 0;
  273. sens->sat = sat;
  274. sens->sens.ops = &wf_sat_ops;
  275. sens->sens.name = (char *) (sens + 1);
  276. snprintf((char *)sens->sens.name, 16, "cpu-power-%d", cpu);
  277. if (wf_register_sensor(&sens->sens))
  278. kfree(sens);
  279. else {
  280. list_add(&sens->link, &sat->sensors);
  281. kref_get(&sat->ref);
  282. }
  283. }
  284. if (sat->nr >= 0)
  285. sats[sat->nr] = sat;
  286. return 0;
  287. }
  288. static int wf_sat_remove(struct i2c_client *client)
  289. {
  290. struct wf_sat *sat = i2c_get_clientdata(client);
  291. struct wf_sat_sensor *sens;
  292. /* release sensors */
  293. while(!list_empty(&sat->sensors)) {
  294. sens = list_first_entry(&sat->sensors,
  295. struct wf_sat_sensor, link);
  296. list_del(&sens->link);
  297. wf_unregister_sensor(&sens->sens);
  298. }
  299. sat->i2c = NULL;
  300. kref_put(&sat->ref, wf_sat_release);
  301. return 0;
  302. }
  303. static const struct i2c_device_id wf_sat_id[] = {
  304. { "MAC,smu-sat", 0 },
  305. { }
  306. };
  307. MODULE_DEVICE_TABLE(i2c, wf_sat_id);
  308. static struct i2c_driver wf_sat_driver = {
  309. .driver = {
  310. .name = "wf_smu_sat",
  311. },
  312. .probe = wf_sat_probe,
  313. .remove = wf_sat_remove,
  314. .id_table = wf_sat_id,
  315. };
  316. module_i2c_driver(wf_sat_driver);
  317. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  318. MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
  319. MODULE_LICENSE("GPL");