windfarm_smu_sat.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "0.2"
  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. int nr;
  31. atomic_t refcnt;
  32. struct mutex mutex;
  33. unsigned long last_read; /* jiffies when cache last updated */
  34. u8 cache[16];
  35. struct i2c_client i2c;
  36. struct device_node *node;
  37. };
  38. static struct wf_sat *sats[2];
  39. struct wf_sat_sensor {
  40. int index;
  41. int index2; /* used for power sensors */
  42. int shift;
  43. struct wf_sat *sat;
  44. struct wf_sensor sens;
  45. };
  46. #define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
  47. #define i2c_to_sat(c) container_of(c, struct wf_sat, i2c)
  48. static int wf_sat_attach(struct i2c_adapter *adapter);
  49. static int wf_sat_detach(struct i2c_client *client);
  50. static struct i2c_driver wf_sat_driver = {
  51. .driver = {
  52. .name = "wf_smu_sat",
  53. },
  54. .attach_adapter = wf_sat_attach,
  55. .detach_client = wf_sat_detach,
  56. };
  57. struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
  58. unsigned int *size)
  59. {
  60. struct wf_sat *sat;
  61. int err;
  62. unsigned int i, len;
  63. u8 *buf;
  64. u8 data[4];
  65. /* TODO: Add the resulting partition to the device-tree */
  66. if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
  67. return NULL;
  68. err = i2c_smbus_write_word_data(&sat->i2c, 8, id << 8);
  69. if (err) {
  70. printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
  71. return NULL;
  72. }
  73. len = i2c_smbus_read_word_data(&sat->i2c, 9);
  74. if (len < 0) {
  75. printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
  76. return NULL;
  77. }
  78. if (len == 0) {
  79. printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
  80. return NULL;
  81. }
  82. len = le16_to_cpu(len);
  83. len = (len + 3) & ~3;
  84. buf = kmalloc(len, GFP_KERNEL);
  85. if (buf == NULL)
  86. return NULL;
  87. for (i = 0; i < len; i += 4) {
  88. err = i2c_smbus_read_i2c_block_data(&sat->i2c, 0xa, 4, data);
  89. if (err < 0) {
  90. printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
  91. err);
  92. goto fail;
  93. }
  94. buf[i] = data[1];
  95. buf[i+1] = data[0];
  96. buf[i+2] = data[3];
  97. buf[i+3] = data[2];
  98. }
  99. #ifdef DEBUG
  100. DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);
  101. for (i = 0; i < len; ++i)
  102. DBG(" %x", buf[i]);
  103. DBG("\n");
  104. #endif
  105. if (size)
  106. *size = len;
  107. return (struct smu_sdbp_header *) buf;
  108. fail:
  109. kfree(buf);
  110. return NULL;
  111. }
  112. EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
  113. /* refresh the cache */
  114. static int wf_sat_read_cache(struct wf_sat *sat)
  115. {
  116. int err;
  117. err = i2c_smbus_read_i2c_block_data(&sat->i2c, 0x3f, 16, sat->cache);
  118. if (err < 0)
  119. return err;
  120. sat->last_read = jiffies;
  121. #ifdef LOTSA_DEBUG
  122. {
  123. int i;
  124. DBG(KERN_DEBUG "wf_sat_get: data is");
  125. for (i = 0; i < 16; ++i)
  126. DBG(" %.2x", sat->cache[i]);
  127. DBG("\n");
  128. }
  129. #endif
  130. return 0;
  131. }
  132. static int wf_sat_get(struct wf_sensor *sr, s32 *value)
  133. {
  134. struct wf_sat_sensor *sens = wf_to_sat(sr);
  135. struct wf_sat *sat = sens->sat;
  136. int i, err;
  137. s32 val;
  138. if (sat->i2c.adapter == NULL)
  139. return -ENODEV;
  140. mutex_lock(&sat->mutex);
  141. if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
  142. err = wf_sat_read_cache(sat);
  143. if (err)
  144. goto fail;
  145. }
  146. i = sens->index * 2;
  147. val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
  148. if (sens->index2 >= 0) {
  149. i = sens->index2 * 2;
  150. /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
  151. val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
  152. }
  153. *value = val;
  154. err = 0;
  155. fail:
  156. mutex_unlock(&sat->mutex);
  157. return err;
  158. }
  159. static void wf_sat_release(struct wf_sensor *sr)
  160. {
  161. struct wf_sat_sensor *sens = wf_to_sat(sr);
  162. struct wf_sat *sat = sens->sat;
  163. if (atomic_dec_and_test(&sat->refcnt)) {
  164. if (sat->i2c.adapter) {
  165. i2c_detach_client(&sat->i2c);
  166. sat->i2c.adapter = NULL;
  167. }
  168. if (sat->nr >= 0)
  169. sats[sat->nr] = NULL;
  170. kfree(sat);
  171. }
  172. kfree(sens);
  173. }
  174. static struct wf_sensor_ops wf_sat_ops = {
  175. .get_value = wf_sat_get,
  176. .release = wf_sat_release,
  177. .owner = THIS_MODULE,
  178. };
  179. static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
  180. {
  181. struct wf_sat *sat;
  182. struct wf_sat_sensor *sens;
  183. const u32 *reg;
  184. const char *loc, *type;
  185. u8 addr, chip, core;
  186. struct device_node *child;
  187. int shift, cpu, index;
  188. char *name;
  189. int vsens[2], isens[2];
  190. reg = of_get_property(dev, "reg", NULL);
  191. if (reg == NULL)
  192. return;
  193. addr = *reg;
  194. DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
  195. sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
  196. if (sat == NULL)
  197. return;
  198. sat->nr = -1;
  199. sat->node = of_node_get(dev);
  200. atomic_set(&sat->refcnt, 0);
  201. mutex_init(&sat->mutex);
  202. sat->i2c.addr = (addr >> 1) & 0x7f;
  203. sat->i2c.adapter = adapter;
  204. sat->i2c.driver = &wf_sat_driver;
  205. strncpy(sat->i2c.name, "smu-sat", I2C_NAME_SIZE-1);
  206. if (i2c_attach_client(&sat->i2c)) {
  207. printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
  208. goto fail;
  209. }
  210. vsens[0] = vsens[1] = -1;
  211. isens[0] = isens[1] = -1;
  212. child = NULL;
  213. while ((child = of_get_next_child(dev, child)) != NULL) {
  214. reg = of_get_property(child, "reg", NULL);
  215. type = of_get_property(child, "device_type", NULL);
  216. loc = of_get_property(child, "location", NULL);
  217. if (reg == NULL || loc == NULL)
  218. continue;
  219. /* the cooked sensors are between 0x30 and 0x37 */
  220. if (*reg < 0x30 || *reg > 0x37)
  221. continue;
  222. index = *reg - 0x30;
  223. /* expect location to be CPU [AB][01] ... */
  224. if (strncmp(loc, "CPU ", 4) != 0)
  225. continue;
  226. chip = loc[4] - 'A';
  227. core = loc[5] - '0';
  228. if (chip > 1 || core > 1) {
  229. printk(KERN_ERR "wf_sat_create: don't understand "
  230. "location %s for %s\n", loc, child->full_name);
  231. continue;
  232. }
  233. cpu = 2 * chip + core;
  234. if (sat->nr < 0)
  235. sat->nr = chip;
  236. else if (sat->nr != chip) {
  237. printk(KERN_ERR "wf_sat_create: can't cope with "
  238. "multiple CPU chips on one SAT (%s)\n", loc);
  239. continue;
  240. }
  241. if (strcmp(type, "voltage-sensor") == 0) {
  242. name = "cpu-voltage";
  243. shift = 4;
  244. vsens[core] = index;
  245. } else if (strcmp(type, "current-sensor") == 0) {
  246. name = "cpu-current";
  247. shift = 8;
  248. isens[core] = index;
  249. } else if (strcmp(type, "temp-sensor") == 0) {
  250. name = "cpu-temp";
  251. shift = 10;
  252. } else
  253. continue; /* hmmm shouldn't happen */
  254. /* the +16 is enough for "cpu-voltage-n" */
  255. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  256. if (sens == NULL) {
  257. printk(KERN_ERR "wf_sat_create: couldn't create "
  258. "%s sensor %d (no memory)\n", name, cpu);
  259. continue;
  260. }
  261. sens->index = index;
  262. sens->index2 = -1;
  263. sens->shift = shift;
  264. sens->sat = sat;
  265. atomic_inc(&sat->refcnt);
  266. sens->sens.ops = &wf_sat_ops;
  267. sens->sens.name = (char *) (sens + 1);
  268. snprintf(sens->sens.name, 16, "%s-%d", name, cpu);
  269. if (wf_register_sensor(&sens->sens)) {
  270. atomic_dec(&sat->refcnt);
  271. kfree(sens);
  272. }
  273. }
  274. /* make the power sensors */
  275. for (core = 0; core < 2; ++core) {
  276. if (vsens[core] < 0 || isens[core] < 0)
  277. continue;
  278. cpu = 2 * sat->nr + core;
  279. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  280. if (sens == NULL) {
  281. printk(KERN_ERR "wf_sat_create: couldn't create power "
  282. "sensor %d (no memory)\n", cpu);
  283. continue;
  284. }
  285. sens->index = vsens[core];
  286. sens->index2 = isens[core];
  287. sens->shift = 0;
  288. sens->sat = sat;
  289. atomic_inc(&sat->refcnt);
  290. sens->sens.ops = &wf_sat_ops;
  291. sens->sens.name = (char *) (sens + 1);
  292. snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);
  293. if (wf_register_sensor(&sens->sens)) {
  294. atomic_dec(&sat->refcnt);
  295. kfree(sens);
  296. }
  297. }
  298. if (sat->nr >= 0)
  299. sats[sat->nr] = sat;
  300. return;
  301. fail:
  302. kfree(sat);
  303. }
  304. static int wf_sat_attach(struct i2c_adapter *adapter)
  305. {
  306. struct device_node *busnode, *dev = NULL;
  307. struct pmac_i2c_bus *bus;
  308. bus = pmac_i2c_adapter_to_bus(adapter);
  309. if (bus == NULL)
  310. return -ENODEV;
  311. busnode = pmac_i2c_get_bus_node(bus);
  312. while ((dev = of_get_next_child(busnode, dev)) != NULL)
  313. if (of_device_is_compatible(dev, "smu-sat"))
  314. wf_sat_create(adapter, dev);
  315. return 0;
  316. }
  317. static int wf_sat_detach(struct i2c_client *client)
  318. {
  319. struct wf_sat *sat = i2c_to_sat(client);
  320. /* XXX TODO */
  321. sat->i2c.adapter = NULL;
  322. return 0;
  323. }
  324. static int __init sat_sensors_init(void)
  325. {
  326. return i2c_add_driver(&wf_sat_driver);
  327. }
  328. #if 0 /* uncomment when module_exit() below is uncommented */
  329. static void __exit sat_sensors_exit(void)
  330. {
  331. i2c_del_driver(&wf_sat_driver);
  332. }
  333. #endif
  334. module_init(sat_sensors_init);
  335. /*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
  336. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  337. MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
  338. MODULE_LICENSE("GPL");