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