windfarm_smu_sat.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. SMU "satellite" controller sensors.
  4. *
  5. * Copyright (C) 2005 Paul Mackerras, IBM Corp. <[email protected]>
  6. */
  7. #include <linux/types.h>
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/wait.h>
  13. #include <linux/i2c.h>
  14. #include <linux/mutex.h>
  15. #include <asm/smu.h>
  16. #include <asm/pmac_low_i2c.h>
  17. #include "windfarm.h"
  18. #define VERSION "1.0"
  19. /* If the cache is older than 800ms we'll refetch it */
  20. #define MAX_AGE msecs_to_jiffies(800)
  21. struct wf_sat {
  22. struct kref ref;
  23. int nr;
  24. struct mutex mutex;
  25. unsigned long last_read; /* jiffies when cache last updated */
  26. u8 cache[16];
  27. struct list_head sensors;
  28. struct i2c_client *i2c;
  29. struct device_node *node;
  30. };
  31. static struct wf_sat *sats[2];
  32. struct wf_sat_sensor {
  33. struct list_head link;
  34. int index;
  35. int index2; /* used for power sensors */
  36. int shift;
  37. struct wf_sat *sat;
  38. struct wf_sensor sens;
  39. };
  40. #define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)
  41. struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
  42. unsigned int *size)
  43. {
  44. struct wf_sat *sat;
  45. int err;
  46. unsigned int i, len;
  47. u8 *buf;
  48. u8 data[4];
  49. /* TODO: Add the resulting partition to the device-tree */
  50. if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
  51. return NULL;
  52. err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
  53. if (err) {
  54. printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
  55. return NULL;
  56. }
  57. err = i2c_smbus_read_word_data(sat->i2c, 9);
  58. if (err < 0) {
  59. printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
  60. return NULL;
  61. }
  62. len = err;
  63. if (len == 0) {
  64. printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
  65. return NULL;
  66. }
  67. len = le16_to_cpu(len);
  68. len = (len + 3) & ~3;
  69. buf = kmalloc(len, GFP_KERNEL);
  70. if (buf == NULL)
  71. return NULL;
  72. for (i = 0; i < len; i += 4) {
  73. err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
  74. if (err < 0) {
  75. printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
  76. err);
  77. goto fail;
  78. }
  79. buf[i] = data[1];
  80. buf[i+1] = data[0];
  81. buf[i+2] = data[3];
  82. buf[i+3] = data[2];
  83. }
  84. printk(KERN_DEBUG "sat %d partition %x:", sat_id, id);
  85. print_hex_dump(KERN_DEBUG, " ", DUMP_PREFIX_OFFSET,
  86. 16, 1, buf, len, false);
  87. if (size)
  88. *size = len;
  89. return (struct smu_sdbp_header *) buf;
  90. fail:
  91. kfree(buf);
  92. return NULL;
  93. }
  94. EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
  95. /* refresh the cache */
  96. static int wf_sat_read_cache(struct wf_sat *sat)
  97. {
  98. int err;
  99. err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
  100. if (err < 0)
  101. return err;
  102. sat->last_read = jiffies;
  103. #ifdef LOTSA_DEBUG
  104. {
  105. int i;
  106. printk(KERN_DEBUG "wf_sat_get: data is");
  107. print_hex_dump(KERN_DEBUG, " ", DUMP_PREFIX_OFFSET,
  108. 16, 1, sat->cache, 16, false);
  109. }
  110. #endif
  111. return 0;
  112. }
  113. static int wf_sat_sensor_get(struct wf_sensor *sr, s32 *value)
  114. {
  115. struct wf_sat_sensor *sens = wf_to_sat(sr);
  116. struct wf_sat *sat = sens->sat;
  117. int i, err;
  118. s32 val;
  119. if (sat->i2c == NULL)
  120. return -ENODEV;
  121. mutex_lock(&sat->mutex);
  122. if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
  123. err = wf_sat_read_cache(sat);
  124. if (err)
  125. goto fail;
  126. }
  127. i = sens->index * 2;
  128. val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
  129. if (sens->index2 >= 0) {
  130. i = sens->index2 * 2;
  131. /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
  132. val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
  133. }
  134. *value = val;
  135. err = 0;
  136. fail:
  137. mutex_unlock(&sat->mutex);
  138. return err;
  139. }
  140. static void wf_sat_release(struct kref *ref)
  141. {
  142. struct wf_sat *sat = container_of(ref, struct wf_sat, ref);
  143. if (sat->nr >= 0)
  144. sats[sat->nr] = NULL;
  145. of_node_put(sat->node);
  146. kfree(sat);
  147. }
  148. static void wf_sat_sensor_release(struct wf_sensor *sr)
  149. {
  150. struct wf_sat_sensor *sens = wf_to_sat(sr);
  151. struct wf_sat *sat = sens->sat;
  152. kfree(sens);
  153. kref_put(&sat->ref, wf_sat_release);
  154. }
  155. static const struct wf_sensor_ops wf_sat_ops = {
  156. .get_value = wf_sat_sensor_get,
  157. .release = wf_sat_sensor_release,
  158. .owner = THIS_MODULE,
  159. };
  160. static int wf_sat_probe(struct i2c_client *client,
  161. const struct i2c_device_id *id)
  162. {
  163. struct device_node *dev = client->dev.of_node;
  164. struct wf_sat *sat;
  165. struct wf_sat_sensor *sens;
  166. const u32 *reg;
  167. const char *loc;
  168. u8 chip, core;
  169. struct device_node *child;
  170. int shift, cpu, index;
  171. char *name;
  172. int vsens[2], isens[2];
  173. sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
  174. if (sat == NULL)
  175. return -ENOMEM;
  176. sat->nr = -1;
  177. sat->node = of_node_get(dev);
  178. kref_init(&sat->ref);
  179. mutex_init(&sat->mutex);
  180. sat->i2c = client;
  181. INIT_LIST_HEAD(&sat->sensors);
  182. i2c_set_clientdata(client, sat);
  183. vsens[0] = vsens[1] = -1;
  184. isens[0] = isens[1] = -1;
  185. for_each_child_of_node(dev, child) {
  186. reg = of_get_property(child, "reg", NULL);
  187. loc = of_get_property(child, "location", NULL);
  188. if (reg == NULL || loc == NULL)
  189. continue;
  190. /* the cooked sensors are between 0x30 and 0x37 */
  191. if (*reg < 0x30 || *reg > 0x37)
  192. continue;
  193. index = *reg - 0x30;
  194. /* expect location to be CPU [AB][01] ... */
  195. if (strncmp(loc, "CPU ", 4) != 0)
  196. continue;
  197. chip = loc[4] - 'A';
  198. core = loc[5] - '0';
  199. if (chip > 1 || core > 1) {
  200. printk(KERN_ERR "wf_sat_create: don't understand "
  201. "location %s for %pOF\n", loc, child);
  202. continue;
  203. }
  204. cpu = 2 * chip + core;
  205. if (sat->nr < 0)
  206. sat->nr = chip;
  207. else if (sat->nr != chip) {
  208. printk(KERN_ERR "wf_sat_create: can't cope with "
  209. "multiple CPU chips on one SAT (%s)\n", loc);
  210. continue;
  211. }
  212. if (of_node_is_type(child, "voltage-sensor")) {
  213. name = "cpu-voltage";
  214. shift = 4;
  215. vsens[core] = index;
  216. } else if (of_node_is_type(child, "current-sensor")) {
  217. name = "cpu-current";
  218. shift = 8;
  219. isens[core] = index;
  220. } else if (of_node_is_type(child, "temp-sensor")) {
  221. name = "cpu-temp";
  222. shift = 10;
  223. } else
  224. continue; /* hmmm shouldn't happen */
  225. /* the +16 is enough for "cpu-voltage-n" */
  226. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  227. if (sens == NULL) {
  228. printk(KERN_ERR "wf_sat_create: couldn't create "
  229. "%s sensor %d (no memory)\n", name, cpu);
  230. continue;
  231. }
  232. sens->index = index;
  233. sens->index2 = -1;
  234. sens->shift = shift;
  235. sens->sat = sat;
  236. sens->sens.ops = &wf_sat_ops;
  237. sens->sens.name = (char *) (sens + 1);
  238. snprintf((char *)sens->sens.name, 16, "%s-%d", name, cpu);
  239. if (wf_register_sensor(&sens->sens))
  240. kfree(sens);
  241. else {
  242. list_add(&sens->link, &sat->sensors);
  243. kref_get(&sat->ref);
  244. }
  245. }
  246. /* make the power sensors */
  247. for (core = 0; core < 2; ++core) {
  248. if (vsens[core] < 0 || isens[core] < 0)
  249. continue;
  250. cpu = 2 * sat->nr + core;
  251. sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  252. if (sens == NULL) {
  253. printk(KERN_ERR "wf_sat_create: couldn't create power "
  254. "sensor %d (no memory)\n", cpu);
  255. continue;
  256. }
  257. sens->index = vsens[core];
  258. sens->index2 = isens[core];
  259. sens->shift = 0;
  260. sens->sat = sat;
  261. sens->sens.ops = &wf_sat_ops;
  262. sens->sens.name = (char *) (sens + 1);
  263. snprintf((char *)sens->sens.name, 16, "cpu-power-%d", cpu);
  264. if (wf_register_sensor(&sens->sens))
  265. kfree(sens);
  266. else {
  267. list_add(&sens->link, &sat->sensors);
  268. kref_get(&sat->ref);
  269. }
  270. }
  271. if (sat->nr >= 0)
  272. sats[sat->nr] = sat;
  273. return 0;
  274. }
  275. static void wf_sat_remove(struct i2c_client *client)
  276. {
  277. struct wf_sat *sat = i2c_get_clientdata(client);
  278. struct wf_sat_sensor *sens;
  279. /* release sensors */
  280. while(!list_empty(&sat->sensors)) {
  281. sens = list_first_entry(&sat->sensors,
  282. struct wf_sat_sensor, link);
  283. list_del(&sens->link);
  284. wf_unregister_sensor(&sens->sens);
  285. }
  286. sat->i2c = NULL;
  287. kref_put(&sat->ref, wf_sat_release);
  288. }
  289. static const struct i2c_device_id wf_sat_id[] = {
  290. { "MAC,smu-sat", 0 },
  291. { }
  292. };
  293. MODULE_DEVICE_TABLE(i2c, wf_sat_id);
  294. static const struct of_device_id wf_sat_of_id[] = {
  295. { .compatible = "smu-sat", },
  296. { }
  297. };
  298. MODULE_DEVICE_TABLE(of, wf_sat_of_id);
  299. static struct i2c_driver wf_sat_driver = {
  300. .driver = {
  301. .name = "wf_smu_sat",
  302. .of_match_table = wf_sat_of_id,
  303. },
  304. .probe = wf_sat_probe,
  305. .remove = wf_sat_remove,
  306. .id_table = wf_sat_id,
  307. };
  308. module_i2c_driver(wf_sat_driver);
  309. MODULE_AUTHOR("Paul Mackerras <[email protected]>");
  310. MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
  311. MODULE_LICENSE("GPL");