windfarm_smu_sat.c 9.3 KB

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