i2c-powermac.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. return rc;
  100. rc = pmac_i2c_setmode(bus, mode);
  101. if (rc)
  102. goto bail;
  103. rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);
  104. if (rc)
  105. goto bail;
  106. if (size == I2C_SMBUS_WORD_DATA && read) {
  107. data->word = ((u16)local[1]) << 8;
  108. data->word |= local[0];
  109. }
  110. bail:
  111. pmac_i2c_close(bus);
  112. return rc;
  113. }
  114. /*
  115. * Generic i2c master transfer entrypoint. This driver only support single
  116. * messages (for "lame i2c" transfers). Anything else should use the smbus
  117. * entry point
  118. */
  119. static int i2c_powermac_master_xfer( struct i2c_adapter *adap,
  120. struct i2c_msg *msgs,
  121. int num)
  122. {
  123. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  124. int rc = 0;
  125. int read;
  126. int addrdir;
  127. if (num != 1) {
  128. dev_err(&adap->dev,
  129. "Multi-message I2C transactions not supported\n");
  130. return -EOPNOTSUPP;
  131. }
  132. if (msgs->flags & I2C_M_TEN)
  133. return -EINVAL;
  134. read = (msgs->flags & I2C_M_RD) != 0;
  135. addrdir = (msgs->addr << 1) | read;
  136. if (msgs->flags & I2C_M_REV_DIR_ADDR)
  137. addrdir ^= 1;
  138. rc = pmac_i2c_open(bus, 0);
  139. if (rc)
  140. return rc;
  141. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  142. if (rc)
  143. goto bail;
  144. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
  145. bail:
  146. pmac_i2c_close(bus);
  147. return rc < 0 ? rc : 1;
  148. }
  149. static u32 i2c_powermac_func(struct i2c_adapter * adapter)
  150. {
  151. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  152. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  153. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
  154. }
  155. /* For now, we only handle smbus */
  156. static const struct i2c_algorithm i2c_powermac_algorithm = {
  157. .smbus_xfer = i2c_powermac_smbus_xfer,
  158. .master_xfer = i2c_powermac_master_xfer,
  159. .functionality = i2c_powermac_func,
  160. };
  161. static int __devexit i2c_powermac_remove(struct platform_device *dev)
  162. {
  163. struct i2c_adapter *adapter = platform_get_drvdata(dev);
  164. struct pmac_i2c_bus *bus = i2c_get_adapdata(adapter);
  165. int rc;
  166. rc = i2c_del_adapter(adapter);
  167. pmac_i2c_detach_adapter(bus, adapter);
  168. i2c_set_adapdata(adapter, NULL);
  169. /* We aren't that prepared to deal with this... */
  170. if (rc)
  171. printk(KERN_WARNING
  172. "i2c-powermac.c: Failed to remove bus %s !\n",
  173. adapter->name);
  174. platform_set_drvdata(dev, NULL);
  175. kfree(adapter);
  176. return 0;
  177. }
  178. static int __devinit i2c_powermac_probe(struct platform_device *dev)
  179. {
  180. struct pmac_i2c_bus *bus = dev->dev.platform_data;
  181. struct device_node *parent = NULL;
  182. struct i2c_adapter *adapter;
  183. char name[32];
  184. const char *basename;
  185. int rc;
  186. if (bus == NULL)
  187. return -EINVAL;
  188. /* Ok, now we need to make up a name for the interface that will
  189. * match what we used to do in the past, that is basically the
  190. * controller's parent device node for keywest. PMU didn't have a
  191. * naming convention and SMU has a different one
  192. */
  193. switch(pmac_i2c_get_type(bus)) {
  194. case pmac_i2c_bus_keywest:
  195. parent = of_get_parent(pmac_i2c_get_controller(bus));
  196. if (parent == NULL)
  197. return -EINVAL;
  198. basename = parent->name;
  199. break;
  200. case pmac_i2c_bus_pmu:
  201. basename = "pmu";
  202. break;
  203. case pmac_i2c_bus_smu:
  204. /* This is not what we used to do but I'm fixing drivers at
  205. * the same time as this change
  206. */
  207. basename = "smu";
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. snprintf(name, 32, "%s %d", basename, pmac_i2c_get_channel(bus));
  213. of_node_put(parent);
  214. adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  215. if (adapter == NULL) {
  216. printk(KERN_ERR "i2c-powermac: can't allocate inteface !\n");
  217. return -ENOMEM;
  218. }
  219. platform_set_drvdata(dev, adapter);
  220. strcpy(adapter->name, name);
  221. adapter->algo = &i2c_powermac_algorithm;
  222. i2c_set_adapdata(adapter, bus);
  223. adapter->dev.parent = &dev->dev;
  224. pmac_i2c_attach_adapter(bus, adapter);
  225. rc = i2c_add_adapter(adapter);
  226. if (rc) {
  227. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  228. "failed\n", name);
  229. i2c_set_adapdata(adapter, NULL);
  230. pmac_i2c_detach_adapter(bus, adapter);
  231. }
  232. printk(KERN_INFO "PowerMac i2c bus %s registered\n", name);
  233. if (!strncmp(basename, "uni-n", 5)) {
  234. struct device_node *np;
  235. const u32 *prop;
  236. struct i2c_board_info info;
  237. /* Instantiate I2C motion sensor if present */
  238. np = of_find_node_by_name(NULL, "accelerometer");
  239. if (np && of_device_is_compatible(np, "AAPL,accelerometer_1") &&
  240. (prop = of_get_property(np, "reg", NULL))) {
  241. int i2c_bus;
  242. const char *tmp_bus;
  243. /* look for bus either using "reg" or by path */
  244. tmp_bus = strstr(np->full_name, "/i2c-bus@");
  245. if (tmp_bus)
  246. i2c_bus = *(tmp_bus + 9) - '0';
  247. else
  248. i2c_bus = ((*prop) >> 8) & 0x0f;
  249. if (pmac_i2c_get_channel(bus) == i2c_bus) {
  250. memset(&info, 0, sizeof(struct i2c_board_info));
  251. info.addr = ((*prop) & 0xff) >> 1;
  252. strlcpy(info.type, "ams", I2C_NAME_SIZE);
  253. i2c_new_device(adapter, &info);
  254. }
  255. }
  256. }
  257. return rc;
  258. }
  259. /* work with hotplug and coldplug */
  260. MODULE_ALIAS("platform:i2c-powermac");
  261. static struct platform_driver i2c_powermac_driver = {
  262. .probe = i2c_powermac_probe,
  263. .remove = __devexit_p(i2c_powermac_remove),
  264. .driver = {
  265. .name = "i2c-powermac",
  266. .bus = &platform_bus_type,
  267. },
  268. };
  269. static int __init i2c_powermac_init(void)
  270. {
  271. platform_driver_register(&i2c_powermac_driver);
  272. return 0;
  273. }
  274. static void __exit i2c_powermac_cleanup(void)
  275. {
  276. platform_driver_unregister(&i2c_powermac_driver);
  277. }
  278. module_init(i2c_powermac_init);
  279. module_exit(i2c_powermac_cleanup);