i2c-powermac.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. i2c Support for Apple SMU Controller
  3. Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp.
  4. <benh@kernel.crashing.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <asm/prom.h>
  25. #include <asm/pmac_low_i2c.h>
  26. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  27. MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
  28. MODULE_LICENSE("GPL");
  29. /*
  30. * SMBUS-type transfer entrypoint
  31. */
  32. static s32 i2c_powermac_smbus_xfer( struct i2c_adapter* adap,
  33. u16 addr,
  34. unsigned short flags,
  35. char read_write,
  36. u8 command,
  37. int size,
  38. union i2c_smbus_data* data)
  39. {
  40. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  41. int rc = 0;
  42. int read = (read_write == I2C_SMBUS_READ);
  43. int addrdir = (addr << 1) | read;
  44. int mode, subsize, len;
  45. u32 subaddr;
  46. u8 *buf;
  47. u8 local[2];
  48. if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) {
  49. mode = pmac_i2c_mode_std;
  50. subsize = 0;
  51. subaddr = 0;
  52. } else {
  53. mode = read ? pmac_i2c_mode_combined : pmac_i2c_mode_stdsub;
  54. subsize = 1;
  55. subaddr = command;
  56. }
  57. switch (size) {
  58. case I2C_SMBUS_QUICK:
  59. buf = NULL;
  60. len = 0;
  61. break;
  62. case I2C_SMBUS_BYTE:
  63. case I2C_SMBUS_BYTE_DATA:
  64. buf = &data->byte;
  65. len = 1;
  66. break;
  67. case I2C_SMBUS_WORD_DATA:
  68. if (!read) {
  69. local[0] = data->word & 0xff;
  70. local[1] = (data->word >> 8) & 0xff;
  71. }
  72. buf = local;
  73. len = 2;
  74. break;
  75. /* Note that these are broken vs. the expected smbus API where
  76. * on reads, the length is actually returned from the function,
  77. * but I think the current API makes no sense and I don't want
  78. * any driver that I haven't verified for correctness to go
  79. * anywhere near a pmac i2c bus anyway ...
  80. *
  81. * I'm also not completely sure what kind of phases to do between
  82. * the actual command and the data (what I am _supposed_ to do that
  83. * is). For now, I assume writes are a single stream and reads have
  84. * a repeat start/addr phase (but not stop in between)
  85. */
  86. case I2C_SMBUS_BLOCK_DATA:
  87. buf = data->block;
  88. len = data->block[0] + 1;
  89. break;
  90. case I2C_SMBUS_I2C_BLOCK_DATA:
  91. buf = &data->block[1];
  92. len = data->block[0];
  93. break;
  94. default:
  95. return -EINVAL;
  96. }
  97. rc = pmac_i2c_open(bus, 0);
  98. if (rc) {
  99. dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
  100. return rc;
  101. }
  102. rc = pmac_i2c_setmode(bus, mode);
  103. if (rc) {
  104. dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
  105. mode, rc);
  106. goto bail;
  107. }
  108. rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);
  109. if (rc) {
  110. if (rc == -ENXIO)
  111. dev_dbg(&adap->dev,
  112. "I2C transfer at 0x%02x failed, size %d, "
  113. "err %d\n", addrdir >> 1, size, rc);
  114. else
  115. dev_err(&adap->dev,
  116. "I2C transfer at 0x%02x failed, size %d, "
  117. "err %d\n", addrdir >> 1, size, rc);
  118. goto bail;
  119. }
  120. if (size == I2C_SMBUS_WORD_DATA && read) {
  121. data->word = ((u16)local[1]) << 8;
  122. data->word |= local[0];
  123. }
  124. bail:
  125. pmac_i2c_close(bus);
  126. return rc;
  127. }
  128. /*
  129. * Generic i2c master transfer entrypoint. This driver only support single
  130. * messages (for "lame i2c" transfers). Anything else should use the smbus
  131. * entry point
  132. */
  133. static int i2c_powermac_master_xfer( struct i2c_adapter *adap,
  134. struct i2c_msg *msgs,
  135. int num)
  136. {
  137. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  138. int rc = 0;
  139. int read;
  140. int addrdir;
  141. if (num != 1) {
  142. dev_err(&adap->dev,
  143. "Multi-message I2C transactions not supported\n");
  144. return -EOPNOTSUPP;
  145. }
  146. if (msgs->flags & I2C_M_TEN)
  147. return -EINVAL;
  148. read = (msgs->flags & I2C_M_RD) != 0;
  149. addrdir = (msgs->addr << 1) | read;
  150. rc = pmac_i2c_open(bus, 0);
  151. if (rc) {
  152. dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
  153. return rc;
  154. }
  155. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  156. if (rc) {
  157. dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
  158. pmac_i2c_mode_std, rc);
  159. goto bail;
  160. }
  161. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
  162. if (rc < 0) {
  163. if (rc == -ENXIO)
  164. dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
  165. addrdir & 1 ? "read from" : "write to",
  166. addrdir >> 1, rc);
  167. else
  168. dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
  169. addrdir & 1 ? "read from" : "write to",
  170. addrdir >> 1, rc);
  171. }
  172. bail:
  173. pmac_i2c_close(bus);
  174. return rc < 0 ? rc : 1;
  175. }
  176. static u32 i2c_powermac_func(struct i2c_adapter * adapter)
  177. {
  178. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  179. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  180. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
  181. }
  182. /* For now, we only handle smbus */
  183. static const struct i2c_algorithm i2c_powermac_algorithm = {
  184. .smbus_xfer = i2c_powermac_smbus_xfer,
  185. .master_xfer = i2c_powermac_master_xfer,
  186. .functionality = i2c_powermac_func,
  187. };
  188. static int __devexit i2c_powermac_remove(struct platform_device *dev)
  189. {
  190. struct i2c_adapter *adapter = platform_get_drvdata(dev);
  191. int rc;
  192. rc = i2c_del_adapter(adapter);
  193. /* We aren't that prepared to deal with this... */
  194. if (rc)
  195. printk(KERN_WARNING
  196. "i2c-powermac.c: Failed to remove bus %s !\n",
  197. adapter->name);
  198. platform_set_drvdata(dev, NULL);
  199. memset(adapter, 0, sizeof(*adapter));
  200. return 0;
  201. }
  202. static void __devinit i2c_powermac_register_devices(struct i2c_adapter *adap,
  203. struct pmac_i2c_bus *bus)
  204. {
  205. struct i2c_client *newdev;
  206. struct device_node *node;
  207. for_each_child_of_node(adap->dev.of_node, node) {
  208. struct i2c_board_info info = {};
  209. struct dev_archdata dev_ad = {};
  210. const __be32 *reg;
  211. char tmp[16];
  212. u32 addr;
  213. int len;
  214. /* Get address & channel */
  215. reg = of_get_property(node, "reg", &len);
  216. if (!reg || (len < sizeof(int))) {
  217. dev_err(&adap->dev, "i2c-powermac: invalid reg on %s\n",
  218. node->full_name);
  219. continue;
  220. }
  221. addr = be32_to_cpup(reg);
  222. /* Multibus setup, check channel */
  223. if (!pmac_i2c_match_adapter(node, adap))
  224. continue;
  225. dev_dbg(&adap->dev, "i2c-powermac: register %s\n",
  226. node->full_name);
  227. /* Make up a modalias. Note: we to _NOT_ want the standard
  228. * i2c drivers to match with any of our powermac stuff
  229. * unless they have been specifically modified to handle
  230. * it on a case by case basis. For example, for thermal
  231. * control, things like lm75 etc... shall match with their
  232. * corresponding windfarm drivers, _NOT_ the generic ones,
  233. * so we force a prefix of AAPL, onto the modalias to
  234. * make that happen
  235. */
  236. if (of_modalias_node(node, tmp, sizeof(tmp)) < 0) {
  237. dev_err(&adap->dev, "i2c-powermac: modalias failure"
  238. " on %s\n", node->full_name);
  239. continue;
  240. }
  241. snprintf(info.type, sizeof(info.type), "MAC,%s", tmp);
  242. /* Fill out the rest of the info structure */
  243. info.addr = (addr & 0xff) >> 1;
  244. info.irq = irq_of_parse_and_map(node, 0);
  245. info.of_node = of_node_get(node);
  246. info.archdata = &dev_ad;
  247. newdev = i2c_new_device(adap, &info);
  248. if (!newdev) {
  249. dev_err(&adap->dev, "i2c-powermac: Failure to register"
  250. " %s\n", node->full_name);
  251. of_node_put(node);
  252. /* We do not dispose of the interrupt mapping on
  253. * purpose. It's not necessary (interrupt cannot be
  254. * re-used) and somebody else might have grabbed it
  255. * via direct DT lookup so let's not bother
  256. */
  257. continue;
  258. }
  259. }
  260. }
  261. static int __devinit i2c_powermac_probe(struct platform_device *dev)
  262. {
  263. struct pmac_i2c_bus *bus = dev->dev.platform_data;
  264. struct device_node *parent = NULL;
  265. struct i2c_adapter *adapter;
  266. const char *basename;
  267. int rc;
  268. if (bus == NULL)
  269. return -EINVAL;
  270. adapter = pmac_i2c_get_adapter(bus);
  271. /* Ok, now we need to make up a name for the interface that will
  272. * match what we used to do in the past, that is basically the
  273. * controller's parent device node for keywest. PMU didn't have a
  274. * naming convention and SMU has a different one
  275. */
  276. switch(pmac_i2c_get_type(bus)) {
  277. case pmac_i2c_bus_keywest:
  278. parent = of_get_parent(pmac_i2c_get_controller(bus));
  279. if (parent == NULL)
  280. return -EINVAL;
  281. basename = parent->name;
  282. break;
  283. case pmac_i2c_bus_pmu:
  284. basename = "pmu";
  285. break;
  286. case pmac_i2c_bus_smu:
  287. /* This is not what we used to do but I'm fixing drivers at
  288. * the same time as this change
  289. */
  290. basename = "smu";
  291. break;
  292. default:
  293. return -EINVAL;
  294. }
  295. snprintf(adapter->name, sizeof(adapter->name), "%s %d", basename,
  296. pmac_i2c_get_channel(bus));
  297. of_node_put(parent);
  298. platform_set_drvdata(dev, adapter);
  299. adapter->algo = &i2c_powermac_algorithm;
  300. i2c_set_adapdata(adapter, bus);
  301. adapter->dev.parent = &dev->dev;
  302. adapter->dev.of_node = dev->dev.of_node;
  303. rc = i2c_add_adapter(adapter);
  304. if (rc) {
  305. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  306. "failed\n", adapter->name);
  307. memset(adapter, 0, sizeof(*adapter));
  308. }
  309. printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);
  310. /* Cannot use of_i2c_register_devices() due to Apple device-tree
  311. * funkyness
  312. */
  313. i2c_powermac_register_devices(adapter, bus);
  314. return rc;
  315. }
  316. static struct platform_driver i2c_powermac_driver = {
  317. .probe = i2c_powermac_probe,
  318. .remove = __devexit_p(i2c_powermac_remove),
  319. .driver = {
  320. .name = "i2c-powermac",
  321. .bus = &platform_bus_type,
  322. },
  323. };
  324. module_platform_driver(i2c_powermac_driver);
  325. MODULE_ALIAS("platform:i2c-powermac");