2006-01-06 17:35:26 -07:00
|
|
|
/*
|
|
|
|
i2c Support for Apple SMU Controller
|
|
|
|
|
|
|
|
Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp.
|
|
|
|
<benh@kernel.crashing.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <asm/prom.h>
|
|
|
|
#include <asm/pmac_low_i2c.h>
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
|
|
|
|
MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SMBUS-type transfer entrypoint
|
|
|
|
*/
|
|
|
|
static s32 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)
|
|
|
|
{
|
|
|
|
struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
|
|
|
|
int rc = 0;
|
|
|
|
int read = (read_write == I2C_SMBUS_READ);
|
|
|
|
int addrdir = (addr << 1) | read;
|
2009-12-06 09:06:17 -07:00
|
|
|
int mode, subsize, len;
|
|
|
|
u32 subaddr;
|
|
|
|
u8 *buf;
|
2006-01-06 17:35:26 -07:00
|
|
|
u8 local[2];
|
|
|
|
|
2009-12-06 09:06:17 -07:00
|
|
|
if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) {
|
|
|
|
mode = pmac_i2c_mode_std;
|
|
|
|
subsize = 0;
|
|
|
|
subaddr = 0;
|
|
|
|
} else {
|
|
|
|
mode = read ? pmac_i2c_mode_combined : pmac_i2c_mode_stdsub;
|
|
|
|
subsize = 1;
|
|
|
|
subaddr = command;
|
|
|
|
}
|
2006-01-06 17:35:26 -07:00
|
|
|
|
|
|
|
switch (size) {
|
|
|
|
case I2C_SMBUS_QUICK:
|
2009-12-06 09:06:17 -07:00
|
|
|
buf = NULL;
|
|
|
|
len = 0;
|
2006-01-06 17:35:26 -07:00
|
|
|
break;
|
|
|
|
case I2C_SMBUS_BYTE:
|
|
|
|
case I2C_SMBUS_BYTE_DATA:
|
2009-12-06 09:06:17 -07:00
|
|
|
buf = &data->byte;
|
|
|
|
len = 1;
|
2006-01-06 17:35:26 -07:00
|
|
|
break;
|
|
|
|
case I2C_SMBUS_WORD_DATA:
|
|
|
|
if (!read) {
|
|
|
|
local[0] = data->word & 0xff;
|
|
|
|
local[1] = (data->word >> 8) & 0xff;
|
|
|
|
}
|
2009-12-06 09:06:17 -07:00
|
|
|
buf = local;
|
|
|
|
len = 2;
|
2006-01-06 17:35:26 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Note that these are broken vs. the expected smbus API where
|
2008-01-14 13:53:30 -07:00
|
|
|
* on reads, the length is actually returned from the function,
|
2006-01-06 17:35:26 -07:00
|
|
|
* but I think the current API makes no sense and I don't want
|
|
|
|
* any driver that I haven't verified for correctness to go
|
|
|
|
* anywhere near a pmac i2c bus anyway ...
|
|
|
|
*
|
|
|
|
* I'm also not completely sure what kind of phases to do between
|
|
|
|
* the actual command and the data (what I am _supposed_ to do that
|
|
|
|
* is). For now, I assume writes are a single stream and reads have
|
|
|
|
* a repeat start/addr phase (but not stop in between)
|
|
|
|
*/
|
|
|
|
case I2C_SMBUS_BLOCK_DATA:
|
2009-12-06 09:06:17 -07:00
|
|
|
buf = data->block;
|
|
|
|
len = data->block[0] + 1;
|
2006-01-06 17:35:26 -07:00
|
|
|
break;
|
|
|
|
case I2C_SMBUS_I2C_BLOCK_DATA:
|
2009-12-06 09:06:17 -07:00
|
|
|
buf = &data->block[1];
|
|
|
|
len = data->block[0];
|
2006-01-06 17:35:26 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-12-06 09:06:17 -07:00
|
|
|
return -EINVAL;
|
2006-01-06 17:35:26 -07:00
|
|
|
}
|
2009-12-06 09:06:17 -07:00
|
|
|
|
|
|
|
rc = pmac_i2c_open(bus, 0);
|
2009-12-06 09:06:18 -07:00
|
|
|
if (rc) {
|
|
|
|
dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
|
2009-12-06 09:06:17 -07:00
|
|
|
return rc;
|
2009-12-06 09:06:18 -07:00
|
|
|
}
|
2009-12-06 09:06:17 -07:00
|
|
|
|
|
|
|
rc = pmac_i2c_setmode(bus, mode);
|
2009-12-06 09:06:18 -07:00
|
|
|
if (rc) {
|
|
|
|
dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
|
|
|
|
mode, rc);
|
2009-12-06 09:06:17 -07:00
|
|
|
goto bail;
|
2009-12-06 09:06:18 -07:00
|
|
|
}
|
2009-12-06 09:06:17 -07:00
|
|
|
|
|
|
|
rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);
|
2009-12-06 09:06:18 -07:00
|
|
|
if (rc) {
|
2010-03-13 12:56:52 -07:00
|
|
|
if (rc == -ENXIO)
|
|
|
|
dev_dbg(&adap->dev,
|
|
|
|
"I2C transfer at 0x%02x failed, size %d, "
|
|
|
|
"err %d\n", addrdir >> 1, size, rc);
|
|
|
|
else
|
|
|
|
dev_err(&adap->dev,
|
|
|
|
"I2C transfer at 0x%02x failed, size %d, "
|
|
|
|
"err %d\n", addrdir >> 1, size, rc);
|
2009-12-06 09:06:17 -07:00
|
|
|
goto bail;
|
2009-12-06 09:06:18 -07:00
|
|
|
}
|
2009-12-06 09:06:17 -07:00
|
|
|
|
|
|
|
if (size == I2C_SMBUS_WORD_DATA && read) {
|
|
|
|
data->word = ((u16)local[1]) << 8;
|
|
|
|
data->word |= local[0];
|
|
|
|
}
|
|
|
|
|
2006-01-06 17:35:26 -07:00
|
|
|
bail:
|
|
|
|
pmac_i2c_close(bus);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic i2c master transfer entrypoint. This driver only support single
|
|
|
|
* messages (for "lame i2c" transfers). Anything else should use the smbus
|
|
|
|
* entry point
|
|
|
|
*/
|
|
|
|
static int i2c_powermac_master_xfer( struct i2c_adapter *adap,
|
|
|
|
struct i2c_msg *msgs,
|
|
|
|
int num)
|
|
|
|
{
|
|
|
|
struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
|
|
|
|
int rc = 0;
|
|
|
|
int read;
|
|
|
|
int addrdir;
|
|
|
|
|
2009-12-06 09:06:17 -07:00
|
|
|
if (num != 1) {
|
|
|
|
dev_err(&adap->dev,
|
|
|
|
"Multi-message I2C transactions not supported\n");
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2006-01-06 17:35:26 -07:00
|
|
|
if (msgs->flags & I2C_M_TEN)
|
|
|
|
return -EINVAL;
|
|
|
|
read = (msgs->flags & I2C_M_RD) != 0;
|
|
|
|
addrdir = (msgs->addr << 1) | read;
|
|
|
|
|
|
|
|
rc = pmac_i2c_open(bus, 0);
|
2009-12-06 09:06:18 -07:00
|
|
|
if (rc) {
|
|
|
|
dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
|
2006-01-06 17:35:26 -07:00
|
|
|
return rc;
|
2009-12-06 09:06:18 -07:00
|
|
|
}
|
2006-01-06 17:35:26 -07:00
|
|
|
rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
|
2009-12-06 09:06:18 -07:00
|
|
|
if (rc) {
|
|
|
|
dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
|
|
|
|
pmac_i2c_mode_std, rc);
|
2006-01-06 17:35:26 -07:00
|
|
|
goto bail;
|
2009-12-06 09:06:18 -07:00
|
|
|
}
|
2006-01-06 17:35:26 -07:00
|
|
|
rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
|
2010-03-13 12:56:52 -07:00
|
|
|
if (rc < 0) {
|
|
|
|
if (rc == -ENXIO)
|
|
|
|
dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
|
|
|
|
addrdir & 1 ? "read from" : "write to",
|
|
|
|
addrdir >> 1, rc);
|
|
|
|
else
|
|
|
|
dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
|
|
|
|
addrdir & 1 ? "read from" : "write to",
|
|
|
|
addrdir >> 1, rc);
|
|
|
|
}
|
2006-01-06 17:35:26 -07:00
|
|
|
bail:
|
|
|
|
pmac_i2c_close(bus);
|
2006-07-01 08:12:53 -07:00
|
|
|
return rc < 0 ? rc : 1;
|
2006-01-06 17:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static u32 i2c_powermac_func(struct i2c_adapter * adapter)
|
|
|
|
{
|
|
|
|
return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
|
|
|
|
I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
|
|
|
|
I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For now, we only handle smbus */
|
2006-09-03 13:39:46 -07:00
|
|
|
static const struct i2c_algorithm i2c_powermac_algorithm = {
|
2006-01-06 17:35:26 -07:00
|
|
|
.smbus_xfer = i2c_powermac_smbus_xfer,
|
|
|
|
.master_xfer = i2c_powermac_master_xfer,
|
|
|
|
.functionality = i2c_powermac_func,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-09-24 04:39:21 -07:00
|
|
|
static int __devexit i2c_powermac_remove(struct platform_device *dev)
|
2006-01-06 17:35:26 -07:00
|
|
|
{
|
2006-10-09 18:45:45 -07:00
|
|
|
struct i2c_adapter *adapter = platform_get_drvdata(dev);
|
2006-01-06 17:35:26 -07:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = i2c_del_adapter(adapter);
|
|
|
|
/* We aren't that prepared to deal with this... */
|
|
|
|
if (rc)
|
2009-03-28 13:34:42 -07:00
|
|
|
printk(KERN_WARNING
|
|
|
|
"i2c-powermac.c: Failed to remove bus %s !\n",
|
2006-01-06 17:35:26 -07:00
|
|
|
adapter->name);
|
2006-10-09 18:45:45 -07:00
|
|
|
platform_set_drvdata(dev, NULL);
|
2009-12-06 09:06:19 -07:00
|
|
|
memset(adapter, 0, sizeof(*adapter));
|
2006-01-06 17:35:26 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 04:39:21 -07:00
|
|
|
static int __devinit i2c_powermac_probe(struct platform_device *dev)
|
2006-01-06 17:35:26 -07:00
|
|
|
{
|
2006-10-09 18:45:45 -07:00
|
|
|
struct pmac_i2c_bus *bus = dev->dev.platform_data;
|
2006-01-06 17:35:26 -07:00
|
|
|
struct device_node *parent = NULL;
|
|
|
|
struct i2c_adapter *adapter;
|
2006-07-11 22:40:29 -07:00
|
|
|
const char *basename;
|
2006-01-06 17:35:26 -07:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (bus == NULL)
|
|
|
|
return -EINVAL;
|
2009-12-06 09:06:20 -07:00
|
|
|
adapter = pmac_i2c_get_adapter(bus);
|
2006-01-06 17:35:26 -07:00
|
|
|
|
|
|
|
/* Ok, now we need to make up a name for the interface that will
|
|
|
|
* match what we used to do in the past, that is basically the
|
|
|
|
* controller's parent device node for keywest. PMU didn't have a
|
|
|
|
* naming convention and SMU has a different one
|
|
|
|
*/
|
|
|
|
switch(pmac_i2c_get_type(bus)) {
|
|
|
|
case pmac_i2c_bus_keywest:
|
|
|
|
parent = of_get_parent(pmac_i2c_get_controller(bus));
|
|
|
|
if (parent == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
basename = parent->name;
|
|
|
|
break;
|
|
|
|
case pmac_i2c_bus_pmu:
|
|
|
|
basename = "pmu";
|
|
|
|
break;
|
|
|
|
case pmac_i2c_bus_smu:
|
|
|
|
/* This is not what we used to do but I'm fixing drivers at
|
|
|
|
* the same time as this change
|
|
|
|
*/
|
|
|
|
basename = "smu";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2009-12-06 09:06:20 -07:00
|
|
|
snprintf(adapter->name, sizeof(adapter->name), "%s %d", basename,
|
|
|
|
pmac_i2c_get_channel(bus));
|
2006-01-06 17:35:26 -07:00
|
|
|
of_node_put(parent);
|
|
|
|
|
2006-10-09 18:45:45 -07:00
|
|
|
platform_set_drvdata(dev, adapter);
|
2006-01-06 17:35:26 -07:00
|
|
|
adapter->algo = &i2c_powermac_algorithm;
|
|
|
|
i2c_set_adapdata(adapter, bus);
|
2006-10-09 18:45:45 -07:00
|
|
|
adapter->dev.parent = &dev->dev;
|
2006-01-06 17:35:26 -07:00
|
|
|
rc = i2c_add_adapter(adapter);
|
|
|
|
if (rc) {
|
|
|
|
printk(KERN_ERR "i2c-powermac: Adapter %s registration "
|
2009-12-06 09:06:20 -07:00
|
|
|
"failed\n", adapter->name);
|
2009-12-06 09:06:19 -07:00
|
|
|
memset(adapter, 0, sizeof(*adapter));
|
2006-01-06 17:35:26 -07:00
|
|
|
}
|
|
|
|
|
2009-12-06 09:06:20 -07:00
|
|
|
printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);
|
2008-10-17 08:51:12 -07:00
|
|
|
|
|
|
|
if (!strncmp(basename, "uni-n", 5)) {
|
|
|
|
struct device_node *np;
|
|
|
|
const u32 *prop;
|
|
|
|
struct i2c_board_info info;
|
|
|
|
|
|
|
|
/* Instantiate I2C motion sensor if present */
|
|
|
|
np = of_find_node_by_name(NULL, "accelerometer");
|
|
|
|
if (np && of_device_is_compatible(np, "AAPL,accelerometer_1") &&
|
|
|
|
(prop = of_get_property(np, "reg", NULL))) {
|
|
|
|
int i2c_bus;
|
|
|
|
const char *tmp_bus;
|
|
|
|
|
|
|
|
/* look for bus either using "reg" or by path */
|
|
|
|
tmp_bus = strstr(np->full_name, "/i2c-bus@");
|
|
|
|
if (tmp_bus)
|
|
|
|
i2c_bus = *(tmp_bus + 9) - '0';
|
|
|
|
else
|
|
|
|
i2c_bus = ((*prop) >> 8) & 0x0f;
|
|
|
|
|
|
|
|
if (pmac_i2c_get_channel(bus) == i2c_bus) {
|
|
|
|
memset(&info, 0, sizeof(struct i2c_board_info));
|
|
|
|
info.addr = ((*prop) & 0xff) >> 1;
|
|
|
|
strlcpy(info.type, "ams", I2C_NAME_SIZE);
|
|
|
|
i2c_new_device(adapter, &info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-06 17:35:26 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-22 13:16:49 -07:00
|
|
|
/* work with hotplug and coldplug */
|
|
|
|
MODULE_ALIAS("platform:i2c-powermac");
|
|
|
|
|
2006-10-09 18:45:45 -07:00
|
|
|
static struct platform_driver i2c_powermac_driver = {
|
2006-01-06 17:35:26 -07:00
|
|
|
.probe = i2c_powermac_probe,
|
2006-10-09 18:45:45 -07:00
|
|
|
.remove = __devexit_p(i2c_powermac_remove),
|
|
|
|
.driver = {
|
|
|
|
.name = "i2c-powermac",
|
|
|
|
.bus = &platform_bus_type,
|
|
|
|
},
|
2006-01-06 17:35:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init i2c_powermac_init(void)
|
|
|
|
{
|
2006-10-09 18:45:45 -07:00
|
|
|
platform_driver_register(&i2c_powermac_driver);
|
2006-01-06 17:35:26 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void __exit i2c_powermac_cleanup(void)
|
|
|
|
{
|
2006-10-09 18:45:45 -07:00
|
|
|
platform_driver_unregister(&i2c_powermac_driver);
|
2006-01-06 17:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(i2c_powermac_init);
|
|
|
|
module_exit(i2c_powermac_cleanup);
|