i2c-powermac.c 12 KB

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