Path: blob/master/drivers/i2c/busses/i2c-powermac.c
15111 views
/*1i2c Support for Apple SMU Controller23Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp.4<[email protected]>56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation; either version 2 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program; if not, write to the Free Software18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.1920*/2122#include <linux/module.h>23#include <linux/kernel.h>24#include <linux/types.h>25#include <linux/i2c.h>26#include <linux/init.h>27#include <linux/device.h>28#include <linux/platform_device.h>29#include <asm/prom.h>30#include <asm/pmac_low_i2c.h>3132MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");33MODULE_DESCRIPTION("I2C driver for Apple PowerMac");34MODULE_LICENSE("GPL");3536/*37* SMBUS-type transfer entrypoint38*/39static s32 i2c_powermac_smbus_xfer( struct i2c_adapter* adap,40u16 addr,41unsigned short flags,42char read_write,43u8 command,44int size,45union i2c_smbus_data* data)46{47struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);48int rc = 0;49int read = (read_write == I2C_SMBUS_READ);50int addrdir = (addr << 1) | read;51int mode, subsize, len;52u32 subaddr;53u8 *buf;54u8 local[2];5556if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) {57mode = pmac_i2c_mode_std;58subsize = 0;59subaddr = 0;60} else {61mode = read ? pmac_i2c_mode_combined : pmac_i2c_mode_stdsub;62subsize = 1;63subaddr = command;64}6566switch (size) {67case I2C_SMBUS_QUICK:68buf = NULL;69len = 0;70break;71case I2C_SMBUS_BYTE:72case I2C_SMBUS_BYTE_DATA:73buf = &data->byte;74len = 1;75break;76case I2C_SMBUS_WORD_DATA:77if (!read) {78local[0] = data->word & 0xff;79local[1] = (data->word >> 8) & 0xff;80}81buf = local;82len = 2;83break;8485/* Note that these are broken vs. the expected smbus API where86* on reads, the length is actually returned from the function,87* but I think the current API makes no sense and I don't want88* any driver that I haven't verified for correctness to go89* anywhere near a pmac i2c bus anyway ...90*91* I'm also not completely sure what kind of phases to do between92* the actual command and the data (what I am _supposed_ to do that93* is). For now, I assume writes are a single stream and reads have94* a repeat start/addr phase (but not stop in between)95*/96case I2C_SMBUS_BLOCK_DATA:97buf = data->block;98len = data->block[0] + 1;99break;100case I2C_SMBUS_I2C_BLOCK_DATA:101buf = &data->block[1];102len = data->block[0];103break;104105default:106return -EINVAL;107}108109rc = pmac_i2c_open(bus, 0);110if (rc) {111dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);112return rc;113}114115rc = pmac_i2c_setmode(bus, mode);116if (rc) {117dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",118mode, rc);119goto bail;120}121122rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);123if (rc) {124if (rc == -ENXIO)125dev_dbg(&adap->dev,126"I2C transfer at 0x%02x failed, size %d, "127"err %d\n", addrdir >> 1, size, rc);128else129dev_err(&adap->dev,130"I2C transfer at 0x%02x failed, size %d, "131"err %d\n", addrdir >> 1, size, rc);132goto bail;133}134135if (size == I2C_SMBUS_WORD_DATA && read) {136data->word = ((u16)local[1]) << 8;137data->word |= local[0];138}139140bail:141pmac_i2c_close(bus);142return rc;143}144145/*146* Generic i2c master transfer entrypoint. This driver only support single147* messages (for "lame i2c" transfers). Anything else should use the smbus148* entry point149*/150static int i2c_powermac_master_xfer( struct i2c_adapter *adap,151struct i2c_msg *msgs,152int num)153{154struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);155int rc = 0;156int read;157int addrdir;158159if (num != 1) {160dev_err(&adap->dev,161"Multi-message I2C transactions not supported\n");162return -EOPNOTSUPP;163}164165if (msgs->flags & I2C_M_TEN)166return -EINVAL;167read = (msgs->flags & I2C_M_RD) != 0;168addrdir = (msgs->addr << 1) | read;169170rc = pmac_i2c_open(bus, 0);171if (rc) {172dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);173return rc;174}175rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);176if (rc) {177dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",178pmac_i2c_mode_std, rc);179goto bail;180}181rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);182if (rc < 0) {183if (rc == -ENXIO)184dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",185addrdir & 1 ? "read from" : "write to",186addrdir >> 1, rc);187else188dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",189addrdir & 1 ? "read from" : "write to",190addrdir >> 1, rc);191}192bail:193pmac_i2c_close(bus);194return rc < 0 ? rc : 1;195}196197static u32 i2c_powermac_func(struct i2c_adapter * adapter)198{199return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |200I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |201I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;202}203204/* For now, we only handle smbus */205static const struct i2c_algorithm i2c_powermac_algorithm = {206.smbus_xfer = i2c_powermac_smbus_xfer,207.master_xfer = i2c_powermac_master_xfer,208.functionality = i2c_powermac_func,209};210211212static int __devexit i2c_powermac_remove(struct platform_device *dev)213{214struct i2c_adapter *adapter = platform_get_drvdata(dev);215int rc;216217rc = i2c_del_adapter(adapter);218/* We aren't that prepared to deal with this... */219if (rc)220printk(KERN_WARNING221"i2c-powermac.c: Failed to remove bus %s !\n",222adapter->name);223platform_set_drvdata(dev, NULL);224memset(adapter, 0, sizeof(*adapter));225226return 0;227}228229230static int __devinit i2c_powermac_probe(struct platform_device *dev)231{232struct pmac_i2c_bus *bus = dev->dev.platform_data;233struct device_node *parent = NULL;234struct i2c_adapter *adapter;235const char *basename;236int rc;237238if (bus == NULL)239return -EINVAL;240adapter = pmac_i2c_get_adapter(bus);241242/* Ok, now we need to make up a name for the interface that will243* match what we used to do in the past, that is basically the244* controller's parent device node for keywest. PMU didn't have a245* naming convention and SMU has a different one246*/247switch(pmac_i2c_get_type(bus)) {248case pmac_i2c_bus_keywest:249parent = of_get_parent(pmac_i2c_get_controller(bus));250if (parent == NULL)251return -EINVAL;252basename = parent->name;253break;254case pmac_i2c_bus_pmu:255basename = "pmu";256break;257case pmac_i2c_bus_smu:258/* This is not what we used to do but I'm fixing drivers at259* the same time as this change260*/261basename = "smu";262break;263default:264return -EINVAL;265}266snprintf(adapter->name, sizeof(adapter->name), "%s %d", basename,267pmac_i2c_get_channel(bus));268of_node_put(parent);269270platform_set_drvdata(dev, adapter);271adapter->algo = &i2c_powermac_algorithm;272i2c_set_adapdata(adapter, bus);273adapter->dev.parent = &dev->dev;274rc = i2c_add_adapter(adapter);275if (rc) {276printk(KERN_ERR "i2c-powermac: Adapter %s registration "277"failed\n", adapter->name);278memset(adapter, 0, sizeof(*adapter));279}280281printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);282283if (!strncmp(basename, "uni-n", 5)) {284struct device_node *np;285const u32 *prop;286struct i2c_board_info info;287288/* Instantiate I2C motion sensor if present */289np = of_find_node_by_name(NULL, "accelerometer");290if (np && of_device_is_compatible(np, "AAPL,accelerometer_1") &&291(prop = of_get_property(np, "reg", NULL))) {292int i2c_bus;293const char *tmp_bus;294295/* look for bus either using "reg" or by path */296tmp_bus = strstr(np->full_name, "/i2c-bus@");297if (tmp_bus)298i2c_bus = *(tmp_bus + 9) - '0';299else300i2c_bus = ((*prop) >> 8) & 0x0f;301302if (pmac_i2c_get_channel(bus) == i2c_bus) {303memset(&info, 0, sizeof(struct i2c_board_info));304info.addr = ((*prop) & 0xff) >> 1;305strlcpy(info.type, "ams", I2C_NAME_SIZE);306i2c_new_device(adapter, &info);307}308}309}310311return rc;312}313314315/* work with hotplug and coldplug */316MODULE_ALIAS("platform:i2c-powermac");317318static struct platform_driver i2c_powermac_driver = {319.probe = i2c_powermac_probe,320.remove = __devexit_p(i2c_powermac_remove),321.driver = {322.name = "i2c-powermac",323.bus = &platform_bus_type,324},325};326327static int __init i2c_powermac_init(void)328{329platform_driver_register(&i2c_powermac_driver);330return 0;331}332333334static void __exit i2c_powermac_cleanup(void)335{336platform_driver_unregister(&i2c_powermac_driver);337}338339module_init(i2c_powermac_init);340module_exit(i2c_powermac_cleanup);341342343