windfarm_smu_sat.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
  167. {
  168. struct i2c_board_info info;
  169. struct i2c_client *client;
  170. const u32 *reg;
  171. u8 addr;
  172. reg = of_get_property(dev, "reg", NULL);
  173. if (reg == NULL)
  174. return;
  175. addr = *reg;
  176. DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
  177. memset(&info, 0, sizeof(struct i2c_board_info));
  178. info.addr = (addr >> 1) & 0x7f;
  179. info.platform_data = dev;
  180. strlcpy(info.type, "wf_sat", I2C_NAME_SIZE);
  181. client = i2c_new_device(adapter, &info);
  182. if (client == NULL) {
  183. printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
  184. return;
  185. }
  186. /*
  187. * Let i2c-core delete that device on driver removal.
  188. * This is safe because i2c-core holds the core_lock mutex for us.
  189. */
  190. list_add_tail(&client->detected, &client->driver->clients);
  191. }
  192. static int wf_sat_probe(struct i2c_client *client,
  193. const struct i2c_device_id *id)
  194. {
  195. struct device_node *dev = client->dev.platform_data;
  196. struct wf_sat *sat;
  197. struct wf_sat_sensor *sens;
  198. const u32 *reg;
  199. const char *loc, *type;
  200. u8 chip, core;
  201. struct device_node *child;
  202. int shift, cpu, index;
  203. char *name;
  204. int vsens[2], isens[2];
  205. sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
  206. if (sat == NULL)
  207. return -ENOMEM;
  208. sat->nr = -1;
  209. sat->node = of_node_get(dev);
  210. atomic_set(&sat->refcnt, 0);
  211. mutex_init(&sat->mutex);
  212. sat->i2c = client;
  213. i2c_set_clientdata(client, sat);
  214. vsens[0] = vsens[1] = -1;
  215. isens[0] = isens[1] = -1;
  216. child = NULL;
  217. while ((child = of_get_next_child(dev, child)) != NULL) {
  218. reg = of_get_property(child, "reg", NULL);
  219. type = of_get_property(child, "device_type", NULL);
  220. loc = of_get_property(child, "location", NULL);
  221. if (reg == NULL || loc == NULL)
  222. continue;
  223. /* the cooked sensors are between 0x30 and 0x37 */
  224. if (*reg < 0x30 || *reg > 0x37)
  225. continue;
  226. index = *reg - 0x30;
  227. /* expect location to be CPU [AB][01] ... */
  228. if (strncmp(loc, "CPU ", 4) != 0)
  229. continue;
  230. chip = loc[4] - 'A';
  231. core = loc[5] - '0';
  232. if (chip > 1 || core > 1) {
  233. printk(KERN_ERR "wf_sat_create: don't understand "
  234. "location %s for %s\n", loc, child->full_name);
  235. continue;
  236. }
  237. cpu = 2 * chip + core;
  238. if (sat->nr < 0)
  239. sat->nr = chip;
  240. else if (sat->nr != chip) {
  241. printk(KERN_ERR "wf_sat_create: can't cope with "
  242. "multiple CPU chips on one SAT (%s)\n", loc);
  243. continue;
  244. }
  245. if (strcmp(type, "voltage-sensor") == 0) {
  246. name = "cpu-voltage";
  247. shift = 4;
  248. vsens[core] = index;
  249. } else if (strcmp(type, "current-sensor") == 0) {
  250. name = "cpu-current";
  251. shift = 8;
  252. isens[core] = index;
  253. } else if (strcmp(type, "temp-sensor") == 0) {
  254. name = "cpu-temp";
  255. shift = 10;
  256. } else
  257. continue; /* hmmm shouldn't happen */
  258. /* the +16 is enough for "cpu-voltage-n" */
  259. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  260. if (sens == NULL) {
  261. printk(KERN_ERR "wf_sat_create: couldn't create "
  262. "%s sensor %d (no memory)\n", name, cpu);
  263. continue;
  264. }
  265. sens->index = index;
  266. sens->index2 = -1;
  267. sens->shift = shift;
  268. sens->sat = sat;
  269. atomic_inc(&sat->refcnt);
  270. sens->sens.ops = &wf_sat_ops;
  271. sens->sens.name = (char *) (sens + 1);
  272. snprintf(sens->sens.name, 16, "%s-%d", name, cpu);
  273. if (wf_register_sensor(&sens->sens)) {
  274. atomic_dec(&sat->refcnt);
  275. kfree(sens);
  276. }
  277. }
  278. /* make the power sensors */
  279. for (core = 0; core < 2; ++core) {
  280. if (vsens[core] < 0 || isens[core] < 0)
  281. continue;
  282. cpu = 2 * sat->nr + core;
  283. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  284. if (sens == NULL) {
  285. printk(KERN_ERR "wf_sat_create: couldn't create power "
  286. "sensor %d (no memory)\n", cpu);
  287. continue;
  288. }
  289. sens->index = vsens[core];
  290. sens->index2 = isens[core];
  291. sens->shift = 0;
  292. sens->sat = sat;
  293. atomic_inc(&sat->refcnt);
  294. sens->sens.ops = &wf_sat_ops;
  295. sens->sens.name = (char *) (sens + 1);
  296. snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);
  297. if (wf_register_sensor(&sens->sens)) {
  298. atomic_dec(&sat->refcnt);
  299. kfree(sens);
  300. }
  301. }
  302. if (sat->nr >= 0)
  303. sats[sat->nr] = sat;
  304. return 0;
  305. }
  306. static int wf_sat_attach(struct i2c_adapter *adapter)
  307. {
  308. struct device_node *busnode, *dev = NULL;
  309. struct pmac_i2c_bus *bus;
  310. bus = pmac_i2c_adapter_to_bus(adapter);
  311. if (bus == NULL)
  312. return -ENODEV;
  313. busnode = pmac_i2c_get_bus_node(bus);
  314. while ((dev = of_get_next_child(busnode, dev)) != NULL)
  315. if (of_device_is_compatible(dev, "smu-sat"))
  316. wf_sat_create(adapter, dev);
  317. return 0;
  318. }
  319. static int wf_sat_remove(struct i2c_client *client)
  320. {
  321. struct wf_sat *sat = i2c_get_clientdata(client);
  322. /* XXX TODO */
  323. sat->i2c = NULL;
  324. i2c_set_clientdata(client, 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");