windfarm_smu_sat.c 8.3 KB

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