i2c-powermac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 read;
  137. int addrdir;
  138. if (msgs->flags & I2C_M_TEN)
  139. return -EINVAL;
  140. read = (msgs->flags & I2C_M_RD) != 0;
  141. addrdir = (msgs->addr << 1) | read;
  142. rc = pmac_i2c_open(bus, 0);
  143. if (rc) {
  144. dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
  145. return rc;
  146. }
  147. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  148. if (rc) {
  149. dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
  150. pmac_i2c_mode_std, rc);
  151. goto bail;
  152. }
  153. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
  154. if (rc < 0) {
  155. if (rc == -ENXIO)
  156. dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
  157. addrdir & 1 ? "read from" : "write to",
  158. addrdir >> 1, rc);
  159. else
  160. dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
  161. addrdir & 1 ? "read from" : "write to",
  162. addrdir >> 1, rc);
  163. }
  164. bail:
  165. pmac_i2c_close(bus);
  166. return rc < 0 ? rc : 1;
  167. }
  168. static u32 i2c_powermac_func(struct i2c_adapter * adapter)
  169. {
  170. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  171. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  172. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
  173. }
  174. /* For now, we only handle smbus */
  175. static const struct i2c_algorithm i2c_powermac_algorithm = {
  176. .smbus_xfer = i2c_powermac_smbus_xfer,
  177. .master_xfer = i2c_powermac_master_xfer,
  178. .functionality = i2c_powermac_func,
  179. };
  180. static struct i2c_adapter_quirks i2c_powermac_quirks = {
  181. .max_num_msgs = 1,
  182. };
  183. static int i2c_powermac_remove(struct platform_device *dev)
  184. {
  185. struct i2c_adapter *adapter = platform_get_drvdata(dev);
  186. i2c_del_adapter(adapter);
  187. memset(adapter, 0, sizeof(*adapter));
  188. return 0;
  189. }
  190. static u32 i2c_powermac_get_addr(struct i2c_adapter *adap,
  191. struct pmac_i2c_bus *bus,
  192. struct device_node *node)
  193. {
  194. const __be32 *prop;
  195. int len;
  196. /* First check for valid "reg" */
  197. prop = of_get_property(node, "reg", &len);
  198. if (prop && (len >= sizeof(int)))
  199. return (be32_to_cpup(prop) & 0xff) >> 1;
  200. /* Then check old-style "i2c-address" */
  201. prop = of_get_property(node, "i2c-address", &len);
  202. if (prop && (len >= sizeof(int)))
  203. return (be32_to_cpup(prop) & 0xff) >> 1;
  204. /* Now handle some devices with missing "reg" properties */
  205. if (!strcmp(node->name, "cereal"))
  206. return 0x60;
  207. else if (!strcmp(node->name, "deq"))
  208. return 0x34;
  209. dev_warn(&adap->dev, "No i2c address for %s\n", node->full_name);
  210. return 0xffffffff;
  211. }
  212. static void i2c_powermac_create_one(struct i2c_adapter *adap,
  213. const char *type,
  214. u32 addr)
  215. {
  216. struct i2c_board_info info = {};
  217. struct i2c_client *newdev;
  218. strncpy(info.type, type, sizeof(info.type));
  219. info.addr = addr;
  220. newdev = i2c_new_device(adap, &info);
  221. if (!newdev)
  222. dev_err(&adap->dev,
  223. "i2c-powermac: Failure to register missing %s\n",
  224. type);
  225. }
  226. static void i2c_powermac_add_missing(struct i2c_adapter *adap,
  227. struct pmac_i2c_bus *bus,
  228. bool found_onyx)
  229. {
  230. struct device_node *busnode = pmac_i2c_get_bus_node(bus);
  231. int rc;
  232. /* Check for the onyx audio codec */
  233. #define ONYX_REG_CONTROL 67
  234. if (of_device_is_compatible(busnode, "k2-i2c") && !found_onyx) {
  235. union i2c_smbus_data data;
  236. rc = i2c_smbus_xfer(adap, 0x46, 0, I2C_SMBUS_READ,
  237. ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
  238. &data);
  239. if (rc >= 0)
  240. i2c_powermac_create_one(adap, "MAC,pcm3052", 0x46);
  241. rc = i2c_smbus_xfer(adap, 0x47, 0, I2C_SMBUS_READ,
  242. ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
  243. &data);
  244. if (rc >= 0)
  245. i2c_powermac_create_one(adap, "MAC,pcm3052", 0x47);
  246. }
  247. }
  248. static bool i2c_powermac_get_type(struct i2c_adapter *adap,
  249. struct device_node *node,
  250. u32 addr, char *type, int type_size)
  251. {
  252. char tmp[16];
  253. /* Note: we to _NOT_ want the standard
  254. * i2c drivers to match with any of our powermac stuff
  255. * unless they have been specifically modified to handle
  256. * it on a case by case basis. For example, for thermal
  257. * control, things like lm75 etc... shall match with their
  258. * corresponding windfarm drivers, _NOT_ the generic ones,
  259. * so we force a prefix of AAPL, onto the modalias to
  260. * make that happen
  261. */
  262. /* First try proper modalias */
  263. if (of_modalias_node(node, tmp, sizeof(tmp)) >= 0) {
  264. snprintf(type, type_size, "MAC,%s", tmp);
  265. return true;
  266. }
  267. /* Now look for known workarounds */
  268. if (!strcmp(node->name, "deq")) {
  269. /* Apple uses address 0x34 for TAS3001 and 0x35 for TAS3004 */
  270. if (addr == 0x34) {
  271. snprintf(type, type_size, "MAC,tas3001");
  272. return true;
  273. } else if (addr == 0x35) {
  274. snprintf(type, type_size, "MAC,tas3004");
  275. return true;
  276. }
  277. }
  278. dev_err(&adap->dev, "i2c-powermac: modalias failure"
  279. " on %s\n", node->full_name);
  280. return false;
  281. }
  282. static void i2c_powermac_register_devices(struct i2c_adapter *adap,
  283. struct pmac_i2c_bus *bus)
  284. {
  285. struct i2c_client *newdev;
  286. struct device_node *node;
  287. bool found_onyx = 0;
  288. /*
  289. * In some cases we end up with the via-pmu node itself, in this
  290. * case we skip this function completely as the device-tree will
  291. * not contain anything useful.
  292. */
  293. if (!strcmp(adap->dev.of_node->name, "via-pmu"))
  294. return;
  295. for_each_child_of_node(adap->dev.of_node, node) {
  296. struct i2c_board_info info = {};
  297. u32 addr;
  298. /* Get address & channel */
  299. addr = i2c_powermac_get_addr(adap, bus, node);
  300. if (addr == 0xffffffff)
  301. continue;
  302. /* Multibus setup, check channel */
  303. if (!pmac_i2c_match_adapter(node, adap))
  304. continue;
  305. dev_dbg(&adap->dev, "i2c-powermac: register %s\n",
  306. node->full_name);
  307. /*
  308. * Keep track of some device existence to handle
  309. * workarounds later.
  310. */
  311. if (of_device_is_compatible(node, "pcm3052"))
  312. found_onyx = true;
  313. /* Make up a modalias */
  314. if (!i2c_powermac_get_type(adap, node, addr,
  315. info.type, sizeof(info.type))) {
  316. continue;
  317. }
  318. /* Fill out the rest of the info structure */
  319. info.addr = addr;
  320. info.irq = irq_of_parse_and_map(node, 0);
  321. info.of_node = of_node_get(node);
  322. newdev = i2c_new_device(adap, &info);
  323. if (!newdev) {
  324. dev_err(&adap->dev, "i2c-powermac: Failure to register"
  325. " %s\n", node->full_name);
  326. of_node_put(node);
  327. /* We do not dispose of the interrupt mapping on
  328. * purpose. It's not necessary (interrupt cannot be
  329. * re-used) and somebody else might have grabbed it
  330. * via direct DT lookup so let's not bother
  331. */
  332. continue;
  333. }
  334. }
  335. /* Additional workarounds */
  336. i2c_powermac_add_missing(adap, bus, found_onyx);
  337. }
  338. static int i2c_powermac_probe(struct platform_device *dev)
  339. {
  340. struct pmac_i2c_bus *bus = dev_get_platdata(&dev->dev);
  341. struct device_node *parent = NULL;
  342. struct i2c_adapter *adapter;
  343. const char *basename;
  344. int rc;
  345. if (bus == NULL)
  346. return -EINVAL;
  347. adapter = pmac_i2c_get_adapter(bus);
  348. /* Ok, now we need to make up a name for the interface that will
  349. * match what we used to do in the past, that is basically the
  350. * controller's parent device node for keywest. PMU didn't have a
  351. * naming convention and SMU has a different one
  352. */
  353. switch(pmac_i2c_get_type(bus)) {
  354. case pmac_i2c_bus_keywest:
  355. parent = of_get_parent(pmac_i2c_get_controller(bus));
  356. if (parent == NULL)
  357. return -EINVAL;
  358. basename = parent->name;
  359. break;
  360. case pmac_i2c_bus_pmu:
  361. basename = "pmu";
  362. break;
  363. case pmac_i2c_bus_smu:
  364. /* This is not what we used to do but I'm fixing drivers at
  365. * the same time as this change
  366. */
  367. basename = "smu";
  368. break;
  369. default:
  370. return -EINVAL;
  371. }
  372. snprintf(adapter->name, sizeof(adapter->name), "%s %d", basename,
  373. pmac_i2c_get_channel(bus));
  374. of_node_put(parent);
  375. platform_set_drvdata(dev, adapter);
  376. adapter->algo = &i2c_powermac_algorithm;
  377. adapter->quirks = &i2c_powermac_quirks;
  378. i2c_set_adapdata(adapter, bus);
  379. adapter->dev.parent = &dev->dev;
  380. /* Clear of_node to skip automatic registration of i2c child nodes */
  381. adapter->dev.of_node = NULL;
  382. rc = i2c_add_adapter(adapter);
  383. if (rc) {
  384. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  385. "failed\n", adapter->name);
  386. memset(adapter, 0, sizeof(*adapter));
  387. return rc;
  388. }
  389. printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);
  390. /* Use custom child registration due to Apple device-tree funkyness */
  391. adapter->dev.of_node = dev->dev.of_node;
  392. i2c_powermac_register_devices(adapter, bus);
  393. return 0;
  394. }
  395. static struct platform_driver i2c_powermac_driver = {
  396. .probe = i2c_powermac_probe,
  397. .remove = i2c_powermac_remove,
  398. .driver = {
  399. .name = "i2c-powermac",
  400. .bus = &platform_bus_type,
  401. },
  402. };
  403. module_platform_driver(i2c_powermac_driver);
  404. MODULE_ALIAS("platform:i2c-powermac");