i2c-powermac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/i2c.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/of_irq.h>
  21. #include <asm/prom.h>
  22. #include <asm/pmac_low_i2c.h>
  23. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  24. MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
  25. MODULE_LICENSE("GPL");
  26. /*
  27. * SMBUS-type transfer entrypoint
  28. */
  29. static s32 i2c_powermac_smbus_xfer( struct i2c_adapter* adap,
  30. u16 addr,
  31. unsigned short flags,
  32. char read_write,
  33. u8 command,
  34. int size,
  35. union i2c_smbus_data* data)
  36. {
  37. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  38. int rc = 0;
  39. int read = (read_write == I2C_SMBUS_READ);
  40. int addrdir = (addr << 1) | read;
  41. int mode, subsize, len;
  42. u32 subaddr;
  43. u8 *buf;
  44. u8 local[2];
  45. if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) {
  46. mode = pmac_i2c_mode_std;
  47. subsize = 0;
  48. subaddr = 0;
  49. } else {
  50. mode = read ? pmac_i2c_mode_combined : pmac_i2c_mode_stdsub;
  51. subsize = 1;
  52. subaddr = command;
  53. }
  54. switch (size) {
  55. case I2C_SMBUS_QUICK:
  56. buf = NULL;
  57. len = 0;
  58. break;
  59. case I2C_SMBUS_BYTE:
  60. case I2C_SMBUS_BYTE_DATA:
  61. buf = &data->byte;
  62. len = 1;
  63. break;
  64. case I2C_SMBUS_WORD_DATA:
  65. if (!read) {
  66. local[0] = data->word & 0xff;
  67. local[1] = (data->word >> 8) & 0xff;
  68. }
  69. buf = local;
  70. len = 2;
  71. break;
  72. /* Note that these are broken vs. the expected smbus API where
  73. * on reads, the length is actually returned from the function,
  74. * but I think the current API makes no sense and I don't want
  75. * any driver that I haven't verified for correctness to go
  76. * anywhere near a pmac i2c bus anyway ...
  77. *
  78. * I'm also not completely sure what kind of phases to do between
  79. * the actual command and the data (what I am _supposed_ to do that
  80. * is). For now, I assume writes are a single stream and reads have
  81. * a repeat start/addr phase (but not stop in between)
  82. */
  83. case I2C_SMBUS_BLOCK_DATA:
  84. buf = data->block;
  85. len = data->block[0] + 1;
  86. break;
  87. case I2C_SMBUS_I2C_BLOCK_DATA:
  88. buf = &data->block[1];
  89. len = data->block[0];
  90. break;
  91. default:
  92. return -EINVAL;
  93. }
  94. rc = pmac_i2c_open(bus, 0);
  95. if (rc) {
  96. dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
  97. return rc;
  98. }
  99. rc = pmac_i2c_setmode(bus, mode);
  100. if (rc) {
  101. dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
  102. mode, rc);
  103. goto bail;
  104. }
  105. rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);
  106. if (rc) {
  107. if (rc == -ENXIO)
  108. dev_dbg(&adap->dev,
  109. "I2C transfer at 0x%02x failed, size %d, "
  110. "err %d\n", addrdir >> 1, size, rc);
  111. else
  112. dev_err(&adap->dev,
  113. "I2C transfer at 0x%02x failed, size %d, "
  114. "err %d\n", addrdir >> 1, size, rc);
  115. goto bail;
  116. }
  117. if (size == I2C_SMBUS_WORD_DATA && read) {
  118. data->word = ((u16)local[1]) << 8;
  119. data->word |= local[0];
  120. }
  121. bail:
  122. pmac_i2c_close(bus);
  123. return rc;
  124. }
  125. /*
  126. * Generic i2c master transfer entrypoint. This driver only support single
  127. * messages (for "lame i2c" transfers). Anything else should use the smbus
  128. * entry point
  129. */
  130. static int i2c_powermac_master_xfer( struct i2c_adapter *adap,
  131. struct i2c_msg *msgs,
  132. int num)
  133. {
  134. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  135. int rc = 0;
  136. int addrdir;
  137. if (msgs->flags & I2C_M_TEN)
  138. return -EINVAL;
  139. addrdir = i2c_8bit_addr_from_msg(msgs);
  140. rc = pmac_i2c_open(bus, 0);
  141. if (rc) {
  142. dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
  143. return rc;
  144. }
  145. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  146. if (rc) {
  147. dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
  148. pmac_i2c_mode_std, rc);
  149. goto bail;
  150. }
  151. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
  152. if (rc < 0) {
  153. if (rc == -ENXIO)
  154. dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
  155. addrdir & 1 ? "read from" : "write to",
  156. addrdir >> 1, rc);
  157. else
  158. dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
  159. addrdir & 1 ? "read from" : "write to",
  160. addrdir >> 1, rc);
  161. }
  162. bail:
  163. pmac_i2c_close(bus);
  164. return rc < 0 ? rc : 1;
  165. }
  166. static u32 i2c_powermac_func(struct i2c_adapter * adapter)
  167. {
  168. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  169. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  170. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
  171. }
  172. /* For now, we only handle smbus */
  173. static const struct i2c_algorithm i2c_powermac_algorithm = {
  174. .smbus_xfer = i2c_powermac_smbus_xfer,
  175. .master_xfer = i2c_powermac_master_xfer,
  176. .functionality = i2c_powermac_func,
  177. };
  178. static struct i2c_adapter_quirks i2c_powermac_quirks = {
  179. .max_num_msgs = 1,
  180. };
  181. static int i2c_powermac_remove(struct platform_device *dev)
  182. {
  183. struct i2c_adapter *adapter = platform_get_drvdata(dev);
  184. i2c_del_adapter(adapter);
  185. memset(adapter, 0, sizeof(*adapter));
  186. return 0;
  187. }
  188. static u32 i2c_powermac_get_addr(struct i2c_adapter *adap,
  189. struct pmac_i2c_bus *bus,
  190. struct device_node *node)
  191. {
  192. const __be32 *prop;
  193. int len;
  194. /* First check for valid "reg" */
  195. prop = of_get_property(node, "reg", &len);
  196. if (prop && (len >= sizeof(int)))
  197. return (be32_to_cpup(prop) & 0xff) >> 1;
  198. /* Then check old-style "i2c-address" */
  199. prop = of_get_property(node, "i2c-address", &len);
  200. if (prop && (len >= sizeof(int)))
  201. return (be32_to_cpup(prop) & 0xff) >> 1;
  202. /* Now handle some devices with missing "reg" properties */
  203. if (!strcmp(node->name, "cereal"))
  204. return 0x60;
  205. else if (!strcmp(node->name, "deq"))
  206. return 0x34;
  207. dev_warn(&adap->dev, "No i2c address for %s\n", node->full_name);
  208. return 0xffffffff;
  209. }
  210. static void i2c_powermac_create_one(struct i2c_adapter *adap,
  211. const char *type,
  212. u32 addr)
  213. {
  214. struct i2c_board_info info = {};
  215. struct i2c_client *newdev;
  216. strncpy(info.type, type, sizeof(info.type));
  217. info.addr = addr;
  218. newdev = i2c_new_device(adap, &info);
  219. if (!newdev)
  220. dev_err(&adap->dev,
  221. "i2c-powermac: Failure to register missing %s\n",
  222. type);
  223. }
  224. static void i2c_powermac_add_missing(struct i2c_adapter *adap,
  225. struct pmac_i2c_bus *bus,
  226. bool found_onyx)
  227. {
  228. struct device_node *busnode = pmac_i2c_get_bus_node(bus);
  229. int rc;
  230. /* Check for the onyx audio codec */
  231. #define ONYX_REG_CONTROL 67
  232. if (of_device_is_compatible(busnode, "k2-i2c") && !found_onyx) {
  233. union i2c_smbus_data data;
  234. rc = i2c_smbus_xfer(adap, 0x46, 0, I2C_SMBUS_READ,
  235. ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
  236. &data);
  237. if (rc >= 0)
  238. i2c_powermac_create_one(adap, "MAC,pcm3052", 0x46);
  239. rc = i2c_smbus_xfer(adap, 0x47, 0, I2C_SMBUS_READ,
  240. ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
  241. &data);
  242. if (rc >= 0)
  243. i2c_powermac_create_one(adap, "MAC,pcm3052", 0x47);
  244. }
  245. }
  246. static bool i2c_powermac_get_type(struct i2c_adapter *adap,
  247. struct device_node *node,
  248. u32 addr, char *type, int type_size)
  249. {
  250. char tmp[16];
  251. /* Note: we to _NOT_ want the standard
  252. * i2c drivers to match with any of our powermac stuff
  253. * unless they have been specifically modified to handle
  254. * it on a case by case basis. For example, for thermal
  255. * control, things like lm75 etc... shall match with their
  256. * corresponding windfarm drivers, _NOT_ the generic ones,
  257. * so we force a prefix of AAPL, onto the modalias to
  258. * make that happen
  259. */
  260. /* First try proper modalias */
  261. if (of_modalias_node(node, tmp, sizeof(tmp)) >= 0) {
  262. snprintf(type, type_size, "MAC,%s", tmp);
  263. return true;
  264. }
  265. /* Now look for known workarounds */
  266. if (!strcmp(node->name, "deq")) {
  267. /* Apple uses address 0x34 for TAS3001 and 0x35 for TAS3004 */
  268. if (addr == 0x34) {
  269. snprintf(type, type_size, "MAC,tas3001");
  270. return true;
  271. } else if (addr == 0x35) {
  272. snprintf(type, type_size, "MAC,tas3004");
  273. return true;
  274. }
  275. }
  276. dev_err(&adap->dev, "i2c-powermac: modalias failure"
  277. " on %s\n", node->full_name);
  278. return false;
  279. }
  280. static void i2c_powermac_register_devices(struct i2c_adapter *adap,
  281. struct pmac_i2c_bus *bus)
  282. {
  283. struct i2c_client *newdev;
  284. struct device_node *node;
  285. bool found_onyx = 0;
  286. /*
  287. * In some cases we end up with the via-pmu node itself, in this
  288. * case we skip this function completely as the device-tree will
  289. * not contain anything useful.
  290. */
  291. if (!strcmp(adap->dev.of_node->name, "via-pmu"))
  292. return;
  293. for_each_child_of_node(adap->dev.of_node, node) {
  294. struct i2c_board_info info = {};
  295. u32 addr;
  296. /* Get address & channel */
  297. addr = i2c_powermac_get_addr(adap, bus, node);
  298. if (addr == 0xffffffff)
  299. continue;
  300. /* Multibus setup, check channel */
  301. if (!pmac_i2c_match_adapter(node, adap))
  302. continue;
  303. dev_dbg(&adap->dev, "i2c-powermac: register %s\n",
  304. node->full_name);
  305. /*
  306. * Keep track of some device existence to handle
  307. * workarounds later.
  308. */
  309. if (of_device_is_compatible(node, "pcm3052"))
  310. found_onyx = true;
  311. /* Make up a modalias */
  312. if (!i2c_powermac_get_type(adap, node, addr,
  313. info.type, sizeof(info.type))) {
  314. continue;
  315. }
  316. /* Fill out the rest of the info structure */
  317. info.addr = addr;
  318. info.irq = irq_of_parse_and_map(node, 0);
  319. info.of_node = of_node_get(node);
  320. newdev = i2c_new_device(adap, &info);
  321. if (!newdev) {
  322. dev_err(&adap->dev, "i2c-powermac: Failure to register"
  323. " %s\n", node->full_name);
  324. of_node_put(node);
  325. /* We do not dispose of the interrupt mapping on
  326. * purpose. It's not necessary (interrupt cannot be
  327. * re-used) and somebody else might have grabbed it
  328. * via direct DT lookup so let's not bother
  329. */
  330. continue;
  331. }
  332. }
  333. /* Additional workarounds */
  334. i2c_powermac_add_missing(adap, bus, found_onyx);
  335. }
  336. static int i2c_powermac_probe(struct platform_device *dev)
  337. {
  338. struct pmac_i2c_bus *bus = dev_get_platdata(&dev->dev);
  339. struct device_node *parent = NULL;
  340. struct i2c_adapter *adapter;
  341. const char *basename;
  342. int rc;
  343. if (bus == NULL)
  344. return -EINVAL;
  345. adapter = pmac_i2c_get_adapter(bus);
  346. /* Ok, now we need to make up a name for the interface that will
  347. * match what we used to do in the past, that is basically the
  348. * controller's parent device node for keywest. PMU didn't have a
  349. * naming convention and SMU has a different one
  350. */
  351. switch(pmac_i2c_get_type(bus)) {
  352. case pmac_i2c_bus_keywest:
  353. parent = of_get_parent(pmac_i2c_get_controller(bus));
  354. if (parent == NULL)
  355. return -EINVAL;
  356. basename = parent->name;
  357. break;
  358. case pmac_i2c_bus_pmu:
  359. basename = "pmu";
  360. break;
  361. case pmac_i2c_bus_smu:
  362. /* This is not what we used to do but I'm fixing drivers at
  363. * the same time as this change
  364. */
  365. basename = "smu";
  366. break;
  367. default:
  368. return -EINVAL;
  369. }
  370. snprintf(adapter->name, sizeof(adapter->name), "%s %d", basename,
  371. pmac_i2c_get_channel(bus));
  372. of_node_put(parent);
  373. platform_set_drvdata(dev, adapter);
  374. adapter->algo = &i2c_powermac_algorithm;
  375. adapter->quirks = &i2c_powermac_quirks;
  376. i2c_set_adapdata(adapter, bus);
  377. adapter->dev.parent = &dev->dev;
  378. /* Clear of_node to skip automatic registration of i2c child nodes */
  379. adapter->dev.of_node = NULL;
  380. rc = i2c_add_adapter(adapter);
  381. if (rc) {
  382. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  383. "failed\n", adapter->name);
  384. memset(adapter, 0, sizeof(*adapter));
  385. return rc;
  386. }
  387. printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);
  388. /* Use custom child registration due to Apple device-tree funkyness */
  389. adapter->dev.of_node = dev->dev.of_node;
  390. i2c_powermac_register_devices(adapter, bus);
  391. return 0;
  392. }
  393. static struct platform_driver i2c_powermac_driver = {
  394. .probe = i2c_powermac_probe,
  395. .remove = i2c_powermac_remove,
  396. .driver = {
  397. .name = "i2c-powermac",
  398. .bus = &platform_bus_type,
  399. },
  400. };
  401. module_platform_driver(i2c_powermac_driver);
  402. MODULE_ALIAS("platform:i2c-powermac");