xref: /openbmc/linux/drivers/i2c/busses/i2c-powermac.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a28d3af2SBenjamin Herrenschmidt /*
3a28d3af2SBenjamin Herrenschmidt     i2c Support for Apple SMU Controller
4a28d3af2SBenjamin Herrenschmidt 
5a28d3af2SBenjamin Herrenschmidt     Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp.
6a28d3af2SBenjamin Herrenschmidt                        <benh@kernel.crashing.org>
7a28d3af2SBenjamin Herrenschmidt 
8a28d3af2SBenjamin Herrenschmidt 
9a28d3af2SBenjamin Herrenschmidt */
10a28d3af2SBenjamin Herrenschmidt 
11a28d3af2SBenjamin Herrenschmidt #include <linux/module.h>
12a28d3af2SBenjamin Herrenschmidt #include <linux/kernel.h>
13a28d3af2SBenjamin Herrenschmidt #include <linux/types.h>
14a28d3af2SBenjamin Herrenschmidt #include <linux/i2c.h>
15a28d3af2SBenjamin Herrenschmidt #include <linux/device.h>
16a28d3af2SBenjamin Herrenschmidt #include <linux/platform_device.h>
175af50730SRob Herring #include <linux/of_irq.h>
1843bf42ffSChristophe Leroy 
19a28d3af2SBenjamin Herrenschmidt #include <asm/pmac_low_i2c.h>
20a28d3af2SBenjamin Herrenschmidt 
21a28d3af2SBenjamin Herrenschmidt MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
22a28d3af2SBenjamin Herrenschmidt MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
23a28d3af2SBenjamin Herrenschmidt MODULE_LICENSE("GPL");
24a28d3af2SBenjamin Herrenschmidt 
25a28d3af2SBenjamin Herrenschmidt /*
26a28d3af2SBenjamin Herrenschmidt  * SMBUS-type transfer entrypoint
27a28d3af2SBenjamin Herrenschmidt  */
i2c_powermac_smbus_xfer(struct i2c_adapter * adap,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)28a28d3af2SBenjamin Herrenschmidt static s32 i2c_powermac_smbus_xfer(	struct i2c_adapter*	adap,
29a28d3af2SBenjamin Herrenschmidt 					u16			addr,
30a28d3af2SBenjamin Herrenschmidt 					unsigned short		flags,
31a28d3af2SBenjamin Herrenschmidt 					char			read_write,
32a28d3af2SBenjamin Herrenschmidt 					u8			command,
33a28d3af2SBenjamin Herrenschmidt 					int			size,
34a28d3af2SBenjamin Herrenschmidt 					union i2c_smbus_data*	data)
35a28d3af2SBenjamin Herrenschmidt {
36a28d3af2SBenjamin Herrenschmidt 	struct pmac_i2c_bus	*bus = i2c_get_adapdata(adap);
37a28d3af2SBenjamin Herrenschmidt 	int			rc = 0;
38a28d3af2SBenjamin Herrenschmidt 	int			read = (read_write == I2C_SMBUS_READ);
39a28d3af2SBenjamin Herrenschmidt 	int			addrdir = (addr << 1) | read;
4002864d58SJean Delvare 	int			mode, subsize, len;
4102864d58SJean Delvare 	u32			subaddr;
4202864d58SJean Delvare 	u8			*buf;
43a28d3af2SBenjamin Herrenschmidt 	u8			local[2];
44a28d3af2SBenjamin Herrenschmidt 
4502864d58SJean Delvare 	if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) {
4602864d58SJean Delvare 		mode = pmac_i2c_mode_std;
4702864d58SJean Delvare 		subsize = 0;
4802864d58SJean Delvare 		subaddr = 0;
4902864d58SJean Delvare 	} else {
5002864d58SJean Delvare 		mode = read ? pmac_i2c_mode_combined : pmac_i2c_mode_stdsub;
5102864d58SJean Delvare 		subsize = 1;
5202864d58SJean Delvare 		subaddr = command;
5302864d58SJean Delvare 	}
54a28d3af2SBenjamin Herrenschmidt 
55a28d3af2SBenjamin Herrenschmidt 	switch (size) {
56a28d3af2SBenjamin Herrenschmidt         case I2C_SMBUS_QUICK:
5702864d58SJean Delvare 		buf = NULL;
5802864d58SJean Delvare 		len = 0;
59a28d3af2SBenjamin Herrenschmidt 	    	break;
60a28d3af2SBenjamin Herrenschmidt         case I2C_SMBUS_BYTE:
61a28d3af2SBenjamin Herrenschmidt         case I2C_SMBUS_BYTE_DATA:
6202864d58SJean Delvare 		buf = &data->byte;
6302864d58SJean Delvare 		len = 1;
64a28d3af2SBenjamin Herrenschmidt 	    	break;
65a28d3af2SBenjamin Herrenschmidt         case I2C_SMBUS_WORD_DATA:
66a28d3af2SBenjamin Herrenschmidt 		if (!read) {
67a28d3af2SBenjamin Herrenschmidt 			local[0] = data->word & 0xff;
68a28d3af2SBenjamin Herrenschmidt 			local[1] = (data->word >> 8) & 0xff;
69a28d3af2SBenjamin Herrenschmidt 		}
7002864d58SJean Delvare 		buf = local;
7102864d58SJean Delvare 		len = 2;
72a28d3af2SBenjamin Herrenschmidt 	    	break;
73a28d3af2SBenjamin Herrenschmidt 
74a28d3af2SBenjamin Herrenschmidt 	/* Note that these are broken vs. the expected smbus API where
7596acafe0SJoe Perches 	 * on reads, the length is actually returned from the function,
76a28d3af2SBenjamin Herrenschmidt 	 * but I think the current API makes no sense and I don't want
77a28d3af2SBenjamin Herrenschmidt 	 * any driver that I haven't verified for correctness to go
78a28d3af2SBenjamin Herrenschmidt 	 * anywhere near a pmac i2c bus anyway ...
79a28d3af2SBenjamin Herrenschmidt 	 */
80a28d3af2SBenjamin Herrenschmidt         case I2C_SMBUS_BLOCK_DATA:
8102864d58SJean Delvare 		buf = data->block;
8202864d58SJean Delvare 		len = data->block[0] + 1;
83a28d3af2SBenjamin Herrenschmidt 		break;
84a28d3af2SBenjamin Herrenschmidt 	case I2C_SMBUS_I2C_BLOCK_DATA:
8502864d58SJean Delvare 		buf = &data->block[1];
8602864d58SJean Delvare 		len = data->block[0];
87a28d3af2SBenjamin Herrenschmidt 		break;
88a28d3af2SBenjamin Herrenschmidt 
89a28d3af2SBenjamin Herrenschmidt         default:
9002864d58SJean Delvare 		return -EINVAL;
91a28d3af2SBenjamin Herrenschmidt 	}
9202864d58SJean Delvare 
9302864d58SJean Delvare 	rc = pmac_i2c_open(bus, 0);
94d7d838a6SJean Delvare 	if (rc) {
95d7d838a6SJean Delvare 		dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
9602864d58SJean Delvare 		return rc;
97d7d838a6SJean Delvare 	}
9802864d58SJean Delvare 
9902864d58SJean Delvare 	rc = pmac_i2c_setmode(bus, mode);
100d7d838a6SJean Delvare 	if (rc) {
101d7d838a6SJean Delvare 		dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
102d7d838a6SJean Delvare 			mode, rc);
10302864d58SJean Delvare 		goto bail;
104d7d838a6SJean Delvare 	}
10502864d58SJean Delvare 
10602864d58SJean Delvare 	rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);
107d7d838a6SJean Delvare 	if (rc) {
1088e4b980cSJean Delvare 		if (rc == -ENXIO)
1098e4b980cSJean Delvare 			dev_dbg(&adap->dev,
1108e4b980cSJean Delvare 				"I2C transfer at 0x%02x failed, size %d, "
1118e4b980cSJean Delvare 				"err %d\n", addrdir >> 1, size, rc);
1128e4b980cSJean Delvare 		else
113d7d838a6SJean Delvare 			dev_err(&adap->dev,
1148e4b980cSJean Delvare 				"I2C transfer at 0x%02x failed, size %d, "
1158e4b980cSJean Delvare 				"err %d\n", addrdir >> 1, size, rc);
11602864d58SJean Delvare 		goto bail;
117d7d838a6SJean Delvare 	}
11802864d58SJean Delvare 
11902864d58SJean Delvare 	if (size == I2C_SMBUS_WORD_DATA && read) {
12002864d58SJean Delvare 		data->word = ((u16)local[1]) << 8;
12102864d58SJean Delvare 		data->word |= local[0];
12202864d58SJean Delvare 	}
12302864d58SJean Delvare 
124a28d3af2SBenjamin Herrenschmidt  bail:
125a28d3af2SBenjamin Herrenschmidt 	pmac_i2c_close(bus);
126a28d3af2SBenjamin Herrenschmidt 	return rc;
127a28d3af2SBenjamin Herrenschmidt }
128a28d3af2SBenjamin Herrenschmidt 
129a28d3af2SBenjamin Herrenschmidt /*
130a28d3af2SBenjamin Herrenschmidt  * Generic i2c master transfer entrypoint. This driver only support single
131a28d3af2SBenjamin Herrenschmidt  * messages (for "lame i2c" transfers). Anything else should use the smbus
132a28d3af2SBenjamin Herrenschmidt  * entry point
133a28d3af2SBenjamin Herrenschmidt  */
i2c_powermac_master_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)134a28d3af2SBenjamin Herrenschmidt static int i2c_powermac_master_xfer(	struct i2c_adapter *adap,
135a28d3af2SBenjamin Herrenschmidt 					struct i2c_msg *msgs,
136a28d3af2SBenjamin Herrenschmidt 					int num)
137a28d3af2SBenjamin Herrenschmidt {
138a28d3af2SBenjamin Herrenschmidt 	struct pmac_i2c_bus	*bus = i2c_get_adapdata(adap);
139a28d3af2SBenjamin Herrenschmidt 	int			rc = 0;
140a28d3af2SBenjamin Herrenschmidt 	int			addrdir;
141a28d3af2SBenjamin Herrenschmidt 
142a28d3af2SBenjamin Herrenschmidt 	if (msgs->flags & I2C_M_TEN)
143a28d3af2SBenjamin Herrenschmidt 		return -EINVAL;
1447756e1eeSWolfram Sang 	addrdir = i2c_8bit_addr_from_msg(msgs);
145a28d3af2SBenjamin Herrenschmidt 
146a28d3af2SBenjamin Herrenschmidt 	rc = pmac_i2c_open(bus, 0);
147d7d838a6SJean Delvare 	if (rc) {
148d7d838a6SJean Delvare 		dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
149a28d3af2SBenjamin Herrenschmidt 		return rc;
150d7d838a6SJean Delvare 	}
151a28d3af2SBenjamin Herrenschmidt 	rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
152d7d838a6SJean Delvare 	if (rc) {
153d7d838a6SJean Delvare 		dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
154d7d838a6SJean Delvare 			pmac_i2c_mode_std, rc);
155a28d3af2SBenjamin Herrenschmidt 		goto bail;
156d7d838a6SJean Delvare 	}
157a28d3af2SBenjamin Herrenschmidt 	rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
1588e4b980cSJean Delvare 	if (rc < 0) {
1598e4b980cSJean Delvare 		if (rc == -ENXIO)
1608e4b980cSJean Delvare 			dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
1618e4b980cSJean Delvare 				addrdir & 1 ? "read from" : "write to",
1628e4b980cSJean Delvare 				addrdir >> 1, rc);
1638e4b980cSJean Delvare 		else
164d7d838a6SJean Delvare 			dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
1658e4b980cSJean Delvare 				addrdir & 1 ? "read from" : "write to",
1668e4b980cSJean Delvare 				addrdir >> 1, rc);
1678e4b980cSJean Delvare 	}
168a28d3af2SBenjamin Herrenschmidt  bail:
169a28d3af2SBenjamin Herrenschmidt 	pmac_i2c_close(bus);
1708ced8eeeSJean Delvare 	return rc < 0 ? rc : 1;
171a28d3af2SBenjamin Herrenschmidt }
172a28d3af2SBenjamin Herrenschmidt 
i2c_powermac_func(struct i2c_adapter * adapter)173a28d3af2SBenjamin Herrenschmidt static u32 i2c_powermac_func(struct i2c_adapter * adapter)
174a28d3af2SBenjamin Herrenschmidt {
175a28d3af2SBenjamin Herrenschmidt 	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
176a28d3af2SBenjamin Herrenschmidt 		I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
177a28d3af2SBenjamin Herrenschmidt 		I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
178a28d3af2SBenjamin Herrenschmidt }
179a28d3af2SBenjamin Herrenschmidt 
180a28d3af2SBenjamin Herrenschmidt /* For now, we only handle smbus */
1818f9082c5SJean Delvare static const struct i2c_algorithm i2c_powermac_algorithm = {
182a28d3af2SBenjamin Herrenschmidt 	.smbus_xfer	= i2c_powermac_smbus_xfer,
183a28d3af2SBenjamin Herrenschmidt 	.master_xfer	= i2c_powermac_master_xfer,
184a28d3af2SBenjamin Herrenschmidt 	.functionality	= i2c_powermac_func,
185a28d3af2SBenjamin Herrenschmidt };
186a28d3af2SBenjamin Herrenschmidt 
187ae3923a2SBhumika Goyal static const struct i2c_adapter_quirks i2c_powermac_quirks = {
1887ee405eaSWolfram Sang 	.max_num_msgs = 1,
1897ee405eaSWolfram Sang };
190a28d3af2SBenjamin Herrenschmidt 
i2c_powermac_remove(struct platform_device * dev)1910b255e92SBill Pemberton static void i2c_powermac_remove(struct platform_device *dev)
192a28d3af2SBenjamin Herrenschmidt {
1939f2545c1SBenjamin Herrenschmidt 	struct i2c_adapter	*adapter = platform_get_drvdata(dev);
194a28d3af2SBenjamin Herrenschmidt 
195bf51a8c5SLars-Peter Clausen 	i2c_del_adapter(adapter);
1966dfa5ca3SJean Delvare 	memset(adapter, 0, sizeof(*adapter));
197a28d3af2SBenjamin Herrenschmidt }
198a28d3af2SBenjamin Herrenschmidt 
i2c_powermac_get_addr(struct i2c_adapter * adap,struct pmac_i2c_bus * bus,struct device_node * node)199a28d3af2SBenjamin Herrenschmidt static u32 i2c_powermac_get_addr(struct i2c_adapter *adap,
200a28d3af2SBenjamin Herrenschmidt 					   struct pmac_i2c_bus *bus,
2010b255e92SBill Pemberton 					   struct device_node *node)
2023a3dd018SBenjamin Herrenschmidt {
2033a3dd018SBenjamin Herrenschmidt 	u32 prop;
20481e5d864SBenjamin Herrenschmidt 	int ret;
2056b98bf01SAishwarya R 
2066b98bf01SAishwarya R 	/* First check for valid "reg" */
20781e5d864SBenjamin Herrenschmidt 	ret = of_property_read_u32(node, "reg", &prop);
2083a3dd018SBenjamin Herrenschmidt 	if (ret == 0)
2096b98bf01SAishwarya R 		return (prop & 0xff) >> 1;
2106b98bf01SAishwarya R 
2116b98bf01SAishwarya R 	/* Then check old-style "i2c-address" */
2123a3dd018SBenjamin Herrenschmidt 	ret = of_property_read_u32(node, "i2c-address", &prop);
2133a3dd018SBenjamin Herrenschmidt 	if (ret == 0)
2146b98bf01SAishwarya R 		return (prop & 0xff) >> 1;
2156b98bf01SAishwarya R 
2166b98bf01SAishwarya R 	/* Now handle some devices with missing "reg" properties */
2173a3dd018SBenjamin Herrenschmidt 	if (of_node_name_eq(node, "cereal"))
2183a3dd018SBenjamin Herrenschmidt 		return 0x60;
21948f9c33cSRob Herring 	else if (of_node_name_eq(node, "deq"))
2203a3dd018SBenjamin Herrenschmidt 		return 0x34;
22148f9c33cSRob Herring 
2223a3dd018SBenjamin Herrenschmidt 	dev_warn(&adap->dev, "No i2c address for %pOF\n", node);
2233a3dd018SBenjamin Herrenschmidt 
224453a237cSRob Herring 	return 0xffffffff;
2253a3dd018SBenjamin Herrenschmidt }
2263a3dd018SBenjamin Herrenschmidt 
i2c_powermac_create_one(struct i2c_adapter * adap,const char * type,u32 addr)22781e5d864SBenjamin Herrenschmidt static void i2c_powermac_create_one(struct i2c_adapter *adap,
22881e5d864SBenjamin Herrenschmidt 					      const char *type,
2290b255e92SBill Pemberton 					      u32 addr)
2303a3dd018SBenjamin Herrenschmidt {
2313a3dd018SBenjamin Herrenschmidt 	struct i2c_board_info info = {};
2323a3dd018SBenjamin Herrenschmidt 	struct i2c_client *newdev;
2333a3dd018SBenjamin Herrenschmidt 
2343a3dd018SBenjamin Herrenschmidt 	strncpy(info.type, type, sizeof(info.type));
23581e5d864SBenjamin Herrenschmidt 	info.addr = addr;
2363a3dd018SBenjamin Herrenschmidt 	newdev = i2c_new_client_device(adap, &info);
2373a3dd018SBenjamin Herrenschmidt 	if (IS_ERR(newdev))
238f12c5290SWolfram Sang 		dev_err(&adap->dev,
239f12c5290SWolfram Sang 			"i2c-powermac: Failure to register missing %s\n",
2403a3dd018SBenjamin Herrenschmidt 			type);
2413a3dd018SBenjamin Herrenschmidt }
2423a3dd018SBenjamin Herrenschmidt 
i2c_powermac_add_missing(struct i2c_adapter * adap,struct pmac_i2c_bus * bus,bool found_onyx)2433a3dd018SBenjamin Herrenschmidt static void i2c_powermac_add_missing(struct i2c_adapter *adap,
24481e5d864SBenjamin Herrenschmidt 					       struct pmac_i2c_bus *bus,
2450b255e92SBill Pemberton 					       bool found_onyx)
2463a3dd018SBenjamin Herrenschmidt {
2473a3dd018SBenjamin Herrenschmidt 	struct device_node *busnode = pmac_i2c_get_bus_node(bus);
2483a3dd018SBenjamin Herrenschmidt 	int rc;
2493a3dd018SBenjamin Herrenschmidt 
2503a3dd018SBenjamin Herrenschmidt 	/* Check for the onyx audio codec */
2513a3dd018SBenjamin Herrenschmidt #define ONYX_REG_CONTROL		67
2523a3dd018SBenjamin Herrenschmidt 	if (of_device_is_compatible(busnode, "k2-i2c") && !found_onyx) {
2533a3dd018SBenjamin Herrenschmidt 		union i2c_smbus_data data;
2543a3dd018SBenjamin Herrenschmidt 
2553a3dd018SBenjamin Herrenschmidt 		rc = i2c_smbus_xfer(adap, 0x46, 0, I2C_SMBUS_READ,
2563a3dd018SBenjamin Herrenschmidt 				    ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
2573a3dd018SBenjamin Herrenschmidt 				    &data);
2583a3dd018SBenjamin Herrenschmidt 		if (rc >= 0)
2593a3dd018SBenjamin Herrenschmidt 			i2c_powermac_create_one(adap, "MAC,pcm3052", 0x46);
2603a3dd018SBenjamin Herrenschmidt 
2613a3dd018SBenjamin Herrenschmidt 		rc = i2c_smbus_xfer(adap, 0x47, 0, I2C_SMBUS_READ,
2623a3dd018SBenjamin Herrenschmidt 				    ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
2633a3dd018SBenjamin Herrenschmidt 				    &data);
2643a3dd018SBenjamin Herrenschmidt 		if (rc >= 0)
2653a3dd018SBenjamin Herrenschmidt 			i2c_powermac_create_one(adap, "MAC,pcm3052", 0x47);
2663a3dd018SBenjamin Herrenschmidt 	}
2673a3dd018SBenjamin Herrenschmidt }
2683a3dd018SBenjamin Herrenschmidt 
i2c_powermac_get_type(struct i2c_adapter * adap,struct device_node * node,u32 addr,char * type,int type_size)2693a3dd018SBenjamin Herrenschmidt static bool i2c_powermac_get_type(struct i2c_adapter *adap,
2703a3dd018SBenjamin Herrenschmidt 					    struct device_node *node,
2710b255e92SBill Pemberton 					    u32 addr, char *type, int type_size)
2723a3dd018SBenjamin Herrenschmidt {
2733a3dd018SBenjamin Herrenschmidt 	char tmp[16];
2743a3dd018SBenjamin Herrenschmidt 
2753a3dd018SBenjamin Herrenschmidt 	/*
2763a3dd018SBenjamin Herrenschmidt 	 * Note: we do _NOT_ want the standard i2c drivers to match with any of
277a786b80cSWolfram Sang 	 * our powermac stuff unless they have been specifically modified to
278a786b80cSWolfram Sang 	 * handle it on a case by case basis. For example, for thermal control,
279a786b80cSWolfram Sang 	 * things like lm75 etc... shall match with their corresponding
280a786b80cSWolfram Sang 	 * windfarm drivers, _NOT_ the generic ones, so we force a prefix of
281a786b80cSWolfram Sang 	 * 'MAC', onto the modalias to make that happen
282a786b80cSWolfram Sang 	 */
283a786b80cSWolfram Sang 
28481e5d864SBenjamin Herrenschmidt 	/* First try proper modalias */
2853a3dd018SBenjamin Herrenschmidt 	if (of_alias_from_compatible(node, tmp, sizeof(tmp)) >= 0) {
2863a3dd018SBenjamin Herrenschmidt 		snprintf(type, type_size, "MAC,%s", tmp);
287*673aa1edSMiquel Raynal 		return true;
2883a3dd018SBenjamin Herrenschmidt 	}
2893a3dd018SBenjamin Herrenschmidt 
2903a3dd018SBenjamin Herrenschmidt 	/* Now look for known workarounds */
2913a3dd018SBenjamin Herrenschmidt 	if (of_node_name_eq(node, "deq")) {
2923a3dd018SBenjamin Herrenschmidt 		/* Apple uses address 0x34 for TAS3001 and 0x35 for TAS3004 */
29348f9c33cSRob Herring 		if (addr == 0x34) {
2943a3dd018SBenjamin Herrenschmidt 			snprintf(type, type_size, "MAC,tas3001");
2953a3dd018SBenjamin Herrenschmidt 			return true;
2963a3dd018SBenjamin Herrenschmidt 		} else if (addr == 0x35) {
2973a3dd018SBenjamin Herrenschmidt 			snprintf(type, type_size, "MAC,tas3004");
2983a3dd018SBenjamin Herrenschmidt 			return true;
2993a3dd018SBenjamin Herrenschmidt 		}
3003a3dd018SBenjamin Herrenschmidt 	}
3013a3dd018SBenjamin Herrenschmidt 
3023a3dd018SBenjamin Herrenschmidt 	dev_err(&adap->dev, "i2c-powermac: modalias failure on %pOF\n", node);
3033a3dd018SBenjamin Herrenschmidt 	return false;
304453a237cSRob Herring }
3053a3dd018SBenjamin Herrenschmidt 
i2c_powermac_register_devices(struct i2c_adapter * adap,struct pmac_i2c_bus * bus)3063a3dd018SBenjamin Herrenschmidt static void i2c_powermac_register_devices(struct i2c_adapter *adap,
3073a3dd018SBenjamin Herrenschmidt 						    struct pmac_i2c_bus *bus)
3080b255e92SBill Pemberton {
3093a3dd018SBenjamin Herrenschmidt 	struct i2c_client *newdev;
3103a3dd018SBenjamin Herrenschmidt 	struct device_node *node;
3113a3dd018SBenjamin Herrenschmidt 	bool found_onyx = false;
3123a3dd018SBenjamin Herrenschmidt 
313de51696cSJason Yan 	/*
3143a3dd018SBenjamin Herrenschmidt 	 * In some cases we end up with the via-pmu node itself, in this
3153a3dd018SBenjamin Herrenschmidt 	 * case we skip this function completely as the device-tree will
3163a3dd018SBenjamin Herrenschmidt 	 * not contain anything useful.
3173a3dd018SBenjamin Herrenschmidt 	 */
3183a3dd018SBenjamin Herrenschmidt 	if (of_node_name_eq(adap->dev.of_node, "via-pmu"))
3193a3dd018SBenjamin Herrenschmidt 		return;
32048f9c33cSRob Herring 
3213a3dd018SBenjamin Herrenschmidt 	for_each_child_of_node(adap->dev.of_node, node) {
3223a3dd018SBenjamin Herrenschmidt 		struct i2c_board_info info = {};
3233a3dd018SBenjamin Herrenschmidt 		u32 addr;
3243a3dd018SBenjamin Herrenschmidt 
3253a3dd018SBenjamin Herrenschmidt 		/* Get address & channel */
3263a3dd018SBenjamin Herrenschmidt 		addr = i2c_powermac_get_addr(adap, bus, node);
3273a3dd018SBenjamin Herrenschmidt 		if (addr == 0xffffffff)
3283a3dd018SBenjamin Herrenschmidt 			continue;
3293a3dd018SBenjamin Herrenschmidt 
3303a3dd018SBenjamin Herrenschmidt 		/* Multibus setup, check channel */
3313a3dd018SBenjamin Herrenschmidt 		if (!pmac_i2c_match_adapter(node, adap))
3323a3dd018SBenjamin Herrenschmidt 			continue;
3333a3dd018SBenjamin Herrenschmidt 
3343a3dd018SBenjamin Herrenschmidt 		dev_dbg(&adap->dev, "i2c-powermac: register %pOF\n", node);
3353a3dd018SBenjamin Herrenschmidt 
336453a237cSRob Herring 		/*
3373a3dd018SBenjamin Herrenschmidt 		 * Keep track of some device existence to handle
3383a3dd018SBenjamin Herrenschmidt 		 * workarounds later.
3393a3dd018SBenjamin Herrenschmidt 		 */
3403a3dd018SBenjamin Herrenschmidt 		if (of_device_is_compatible(node, "pcm3052"))
3413a3dd018SBenjamin Herrenschmidt 			found_onyx = true;
3423a3dd018SBenjamin Herrenschmidt 
3433a3dd018SBenjamin Herrenschmidt 		/* Make up a modalias */
3443a3dd018SBenjamin Herrenschmidt 		if (!i2c_powermac_get_type(adap, node, addr,
3453a3dd018SBenjamin Herrenschmidt 					   info.type, sizeof(info.type))) {
3463a3dd018SBenjamin Herrenschmidt 			continue;
3473a3dd018SBenjamin Herrenschmidt 		}
34881e5d864SBenjamin Herrenschmidt 
34981e5d864SBenjamin Herrenschmidt 		/* Fill out the rest of the info structure */
35081e5d864SBenjamin Herrenschmidt 		info.addr = addr;
35181e5d864SBenjamin Herrenschmidt 		info.irq = irq_of_parse_and_map(node, 0);
3523a3dd018SBenjamin Herrenschmidt 		info.of_node = of_node_get(node);
35381e5d864SBenjamin Herrenschmidt 
35481e5d864SBenjamin Herrenschmidt 		newdev = i2c_new_client_device(adap, &info);
35581e5d864SBenjamin Herrenschmidt 		if (IS_ERR(newdev)) {
356f12c5290SWolfram Sang 			dev_err(&adap->dev, "i2c-powermac: Failure to register"
357f12c5290SWolfram Sang 				" %pOF\n", node);
35881e5d864SBenjamin Herrenschmidt 			of_node_put(node);
359453a237cSRob Herring 			/* We do not dispose of the interrupt mapping on
36081e5d864SBenjamin Herrenschmidt 			 * purpose. It's not necessary (interrupt cannot be
36181e5d864SBenjamin Herrenschmidt 			 * re-used) and somebody else might have grabbed it
36281e5d864SBenjamin Herrenschmidt 			 * via direct DT lookup so let's not bother
36381e5d864SBenjamin Herrenschmidt 			 */
36481e5d864SBenjamin Herrenschmidt 			continue;
36581e5d864SBenjamin Herrenschmidt 		}
36681e5d864SBenjamin Herrenschmidt 	}
36781e5d864SBenjamin Herrenschmidt 
36881e5d864SBenjamin Herrenschmidt 	/* Additional workarounds */
3693a3dd018SBenjamin Herrenschmidt 	i2c_powermac_add_missing(adap, bus, found_onyx);
3703a3dd018SBenjamin Herrenschmidt }
3713a3dd018SBenjamin Herrenschmidt 
i2c_powermac_probe(struct platform_device * dev)37281e5d864SBenjamin Herrenschmidt static int i2c_powermac_probe(struct platform_device *dev)
373a28d3af2SBenjamin Herrenschmidt {
3740b255e92SBill Pemberton 	struct pmac_i2c_bus *bus = dev_get_platdata(&dev->dev);
375a28d3af2SBenjamin Herrenschmidt 	struct device_node *parent;
3766d4028c6SJingoo Han 	struct i2c_adapter *adapter;
37749d54abeSRob Herring 	int rc;
378a28d3af2SBenjamin Herrenschmidt 
379a28d3af2SBenjamin Herrenschmidt 	if (bus == NULL)
380a28d3af2SBenjamin Herrenschmidt 		return -EINVAL;
381a28d3af2SBenjamin Herrenschmidt 	adapter = pmac_i2c_get_adapter(bus);
382a28d3af2SBenjamin Herrenschmidt 
383bc6286e5SJean Delvare 	/* Ok, now we need to make up a name for the interface that will
384a28d3af2SBenjamin Herrenschmidt 	 * match what we used to do in the past, that is basically the
385a28d3af2SBenjamin Herrenschmidt 	 * controller's parent device node for keywest. PMU didn't have a
386a28d3af2SBenjamin Herrenschmidt 	 * naming convention and SMU has a different one
387a28d3af2SBenjamin Herrenschmidt 	 */
388a28d3af2SBenjamin Herrenschmidt 	switch(pmac_i2c_get_type(bus)) {
389a28d3af2SBenjamin Herrenschmidt 	case pmac_i2c_bus_keywest:
390a28d3af2SBenjamin Herrenschmidt 		parent = of_get_parent(pmac_i2c_get_controller(bus));
391a28d3af2SBenjamin Herrenschmidt 		if (parent == NULL)
392a28d3af2SBenjamin Herrenschmidt 			return -EINVAL;
393a28d3af2SBenjamin Herrenschmidt 		snprintf(adapter->name, sizeof(adapter->name), "%pOFn %d",
394a28d3af2SBenjamin Herrenschmidt 			 parent,
39549d54abeSRob Herring 			 pmac_i2c_get_channel(bus));
39649d54abeSRob Herring 		of_node_put(parent);
39749d54abeSRob Herring 		break;
39849d54abeSRob Herring 	case pmac_i2c_bus_pmu:
399a28d3af2SBenjamin Herrenschmidt 		snprintf(adapter->name, sizeof(adapter->name), "pmu %d",
400a28d3af2SBenjamin Herrenschmidt 			 pmac_i2c_get_channel(bus));
40149d54abeSRob Herring 		break;
40249d54abeSRob Herring 	case pmac_i2c_bus_smu:
403a28d3af2SBenjamin Herrenschmidt 		/* This is not what we used to do but I'm fixing drivers at
404a28d3af2SBenjamin Herrenschmidt 		 * the same time as this change
405a28d3af2SBenjamin Herrenschmidt 		 */
406a28d3af2SBenjamin Herrenschmidt 		snprintf(adapter->name, sizeof(adapter->name), "smu %d",
407a28d3af2SBenjamin Herrenschmidt 			 pmac_i2c_get_channel(bus));
40849d54abeSRob Herring 		break;
40949d54abeSRob Herring 	default:
410a28d3af2SBenjamin Herrenschmidt 		return -EINVAL;
411a28d3af2SBenjamin Herrenschmidt 	}
412a28d3af2SBenjamin Herrenschmidt 
413a28d3af2SBenjamin Herrenschmidt 	platform_set_drvdata(dev, adapter);
414a28d3af2SBenjamin Herrenschmidt 	adapter->algo = &i2c_powermac_algorithm;
4159f2545c1SBenjamin Herrenschmidt 	adapter->quirks = &i2c_powermac_quirks;
416a28d3af2SBenjamin Herrenschmidt 	i2c_set_adapdata(adapter, bus);
4177ee405eaSWolfram Sang 	adapter->dev.parent = &dev->dev;
418a28d3af2SBenjamin Herrenschmidt 
4199f2545c1SBenjamin Herrenschmidt 	/* Clear of_node to skip automatic registration of i2c child nodes */
420687b81d0SWolfram Sang 	adapter->dev.of_node = NULL;
421687b81d0SWolfram Sang 	rc = i2c_add_adapter(adapter);
422687b81d0SWolfram Sang 	if (rc) {
423a28d3af2SBenjamin Herrenschmidt 		printk(KERN_ERR "i2c-powermac: Adapter %s registration "
424a28d3af2SBenjamin Herrenschmidt 		       "failed\n", adapter->name);
425a28d3af2SBenjamin Herrenschmidt 		memset(adapter, 0, sizeof(*adapter));
426bc6286e5SJean Delvare 		return rc;
4276dfa5ca3SJean Delvare 	}
428874e955bSWolfram Sang 
429a28d3af2SBenjamin Herrenschmidt 	printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);
430a28d3af2SBenjamin Herrenschmidt 
431bc6286e5SJean Delvare 	/* Use custom child registration due to Apple device-tree funkyness */
432810ad7b6SJean Delvare 	adapter->dev.of_node = dev->dev.of_node;
433687b81d0SWolfram Sang 	i2c_powermac_register_devices(adapter, bus);
434687b81d0SWolfram Sang 
43581e5d864SBenjamin Herrenschmidt 	return 0;
436810ad7b6SJean Delvare }
437874e955bSWolfram Sang 
438a28d3af2SBenjamin Herrenschmidt static struct platform_driver i2c_powermac_driver = {
439a28d3af2SBenjamin Herrenschmidt 	.probe = i2c_powermac_probe,
4409f2545c1SBenjamin Herrenschmidt 	.remove_new = i2c_powermac_remove,
4419f2545c1SBenjamin Herrenschmidt 	.driver = {
4420b255e92SBill Pemberton 		.name = "i2c-powermac",
4439f2545c1SBenjamin Herrenschmidt 		.bus = &platform_bus_type,
444a28d3af2SBenjamin Herrenschmidt 	},
445a28d3af2SBenjamin Herrenschmidt };
4469f2545c1SBenjamin Herrenschmidt 
447a28d3af2SBenjamin Herrenschmidt module_platform_driver(i2c_powermac_driver);
448a28d3af2SBenjamin Herrenschmidt 
449a3664b51SAxel Lin MODULE_ALIAS("platform:i2c-powermac");
450a28d3af2SBenjamin Herrenschmidt