windfarm_smu_sat.c 9.3 KB

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