i2c-powermac.c 11 KB

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