windfarm_smu_sat.c 9.2 KB

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