i2c-powermac.c 12 KB

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