Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     i2c Support for Apple SMU Controller
0004 
0005     Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp.
0006                        <benh@kernel.crashing.org>
0007 
0008 
0009 */
0010 
0011 #include <linux/module.h>
0012 #include <linux/kernel.h>
0013 #include <linux/types.h>
0014 #include <linux/i2c.h>
0015 #include <linux/device.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/of_irq.h>
0018 
0019 #include <asm/pmac_low_i2c.h>
0020 
0021 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
0022 MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
0023 MODULE_LICENSE("GPL");
0024 
0025 /*
0026  * SMBUS-type transfer entrypoint
0027  */
0028 static s32 i2c_powermac_smbus_xfer( struct i2c_adapter* adap,
0029                     u16         addr,
0030                     unsigned short      flags,
0031                     char            read_write,
0032                     u8          command,
0033                     int         size,
0034                     union i2c_smbus_data*   data)
0035 {
0036     struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
0037     int         rc = 0;
0038     int         read = (read_write == I2C_SMBUS_READ);
0039     int         addrdir = (addr << 1) | read;
0040     int         mode, subsize, len;
0041     u32         subaddr;
0042     u8          *buf;
0043     u8          local[2];
0044 
0045     if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) {
0046         mode = pmac_i2c_mode_std;
0047         subsize = 0;
0048         subaddr = 0;
0049     } else {
0050         mode = read ? pmac_i2c_mode_combined : pmac_i2c_mode_stdsub;
0051         subsize = 1;
0052         subaddr = command;
0053     }
0054 
0055     switch (size) {
0056         case I2C_SMBUS_QUICK:
0057         buf = NULL;
0058         len = 0;
0059             break;
0060         case I2C_SMBUS_BYTE:
0061         case I2C_SMBUS_BYTE_DATA:
0062         buf = &data->byte;
0063         len = 1;
0064             break;
0065         case I2C_SMBUS_WORD_DATA:
0066         if (!read) {
0067             local[0] = data->word & 0xff;
0068             local[1] = (data->word >> 8) & 0xff;
0069         }
0070         buf = local;
0071         len = 2;
0072             break;
0073 
0074     /* Note that these are broken vs. the expected smbus API where
0075      * on reads, the length is actually returned from the function,
0076      * but I think the current API makes no sense and I don't want
0077      * any driver that I haven't verified for correctness to go
0078      * anywhere near a pmac i2c bus anyway ...
0079      */
0080         case I2C_SMBUS_BLOCK_DATA:
0081         buf = data->block;
0082         len = data->block[0] + 1;
0083         break;
0084     case I2C_SMBUS_I2C_BLOCK_DATA:
0085         buf = &data->block[1];
0086         len = data->block[0];
0087         break;
0088 
0089         default:
0090         return -EINVAL;
0091     }
0092 
0093     rc = pmac_i2c_open(bus, 0);
0094     if (rc) {
0095         dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
0096         return rc;
0097     }
0098 
0099     rc = pmac_i2c_setmode(bus, mode);
0100     if (rc) {
0101         dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
0102             mode, rc);
0103         goto bail;
0104     }
0105 
0106     rc = pmac_i2c_xfer(bus, addrdir, subsize, subaddr, buf, len);
0107     if (rc) {
0108         if (rc == -ENXIO)
0109             dev_dbg(&adap->dev,
0110                 "I2C transfer at 0x%02x failed, size %d, "
0111                 "err %d\n", addrdir >> 1, size, rc);
0112         else
0113             dev_err(&adap->dev,
0114                 "I2C transfer at 0x%02x failed, size %d, "
0115                 "err %d\n", addrdir >> 1, size, rc);
0116         goto bail;
0117     }
0118 
0119     if (size == I2C_SMBUS_WORD_DATA && read) {
0120         data->word = ((u16)local[1]) << 8;
0121         data->word |= local[0];
0122     }
0123 
0124  bail:
0125     pmac_i2c_close(bus);
0126     return rc;
0127 }
0128 
0129 /*
0130  * Generic i2c master transfer entrypoint. This driver only support single
0131  * messages (for "lame i2c" transfers). Anything else should use the smbus
0132  * entry point
0133  */
0134 static int i2c_powermac_master_xfer(    struct i2c_adapter *adap,
0135                     struct i2c_msg *msgs,
0136                     int num)
0137 {
0138     struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
0139     int         rc = 0;
0140     int         addrdir;
0141 
0142     if (msgs->flags & I2C_M_TEN)
0143         return -EINVAL;
0144     addrdir = i2c_8bit_addr_from_msg(msgs);
0145 
0146     rc = pmac_i2c_open(bus, 0);
0147     if (rc) {
0148         dev_err(&adap->dev, "Failed to open I2C, err %d\n", rc);
0149         return rc;
0150     }
0151     rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
0152     if (rc) {
0153         dev_err(&adap->dev, "Failed to set I2C mode %d, err %d\n",
0154             pmac_i2c_mode_std, rc);
0155         goto bail;
0156     }
0157     rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
0158     if (rc < 0) {
0159         if (rc == -ENXIO)
0160             dev_dbg(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
0161                 addrdir & 1 ? "read from" : "write to",
0162                 addrdir >> 1, rc);
0163         else
0164             dev_err(&adap->dev, "I2C %s 0x%02x failed, err %d\n",
0165                 addrdir & 1 ? "read from" : "write to",
0166                 addrdir >> 1, rc);
0167     }
0168  bail:
0169     pmac_i2c_close(bus);
0170     return rc < 0 ? rc : 1;
0171 }
0172 
0173 static u32 i2c_powermac_func(struct i2c_adapter * adapter)
0174 {
0175     return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
0176         I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
0177         I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
0178 }
0179 
0180 /* For now, we only handle smbus */
0181 static const struct i2c_algorithm i2c_powermac_algorithm = {
0182     .smbus_xfer = i2c_powermac_smbus_xfer,
0183     .master_xfer    = i2c_powermac_master_xfer,
0184     .functionality  = i2c_powermac_func,
0185 };
0186 
0187 static const struct i2c_adapter_quirks i2c_powermac_quirks = {
0188     .max_num_msgs = 1,
0189 };
0190 
0191 static int i2c_powermac_remove(struct platform_device *dev)
0192 {
0193     struct i2c_adapter  *adapter = platform_get_drvdata(dev);
0194 
0195     i2c_del_adapter(adapter);
0196     memset(adapter, 0, sizeof(*adapter));
0197 
0198     return 0;
0199 }
0200 
0201 static u32 i2c_powermac_get_addr(struct i2c_adapter *adap,
0202                        struct pmac_i2c_bus *bus,
0203                        struct device_node *node)
0204 {
0205     u32 prop;
0206     int ret;
0207 
0208     /* First check for valid "reg" */
0209     ret = of_property_read_u32(node, "reg", &prop);
0210     if (ret == 0)
0211         return (prop & 0xff) >> 1;
0212 
0213     /* Then check old-style "i2c-address" */
0214     ret = of_property_read_u32(node, "i2c-address", &prop);
0215     if (ret == 0)
0216         return (prop & 0xff) >> 1;
0217 
0218     /* Now handle some devices with missing "reg" properties */
0219     if (of_node_name_eq(node, "cereal"))
0220         return 0x60;
0221     else if (of_node_name_eq(node, "deq"))
0222         return 0x34;
0223 
0224     dev_warn(&adap->dev, "No i2c address for %pOF\n", node);
0225 
0226     return 0xffffffff;
0227 }
0228 
0229 static void i2c_powermac_create_one(struct i2c_adapter *adap,
0230                           const char *type,
0231                           u32 addr)
0232 {
0233     struct i2c_board_info info = {};
0234     struct i2c_client *newdev;
0235 
0236     strncpy(info.type, type, sizeof(info.type));
0237     info.addr = addr;
0238     newdev = i2c_new_client_device(adap, &info);
0239     if (IS_ERR(newdev))
0240         dev_err(&adap->dev,
0241             "i2c-powermac: Failure to register missing %s\n",
0242             type);
0243 }
0244 
0245 static void i2c_powermac_add_missing(struct i2c_adapter *adap,
0246                            struct pmac_i2c_bus *bus,
0247                            bool found_onyx)
0248 {
0249     struct device_node *busnode = pmac_i2c_get_bus_node(bus);
0250     int rc;
0251 
0252     /* Check for the onyx audio codec */
0253 #define ONYX_REG_CONTROL        67
0254     if (of_device_is_compatible(busnode, "k2-i2c") && !found_onyx) {
0255         union i2c_smbus_data data;
0256 
0257         rc = i2c_smbus_xfer(adap, 0x46, 0, I2C_SMBUS_READ,
0258                     ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
0259                     &data);
0260         if (rc >= 0)
0261             i2c_powermac_create_one(adap, "MAC,pcm3052", 0x46);
0262 
0263         rc = i2c_smbus_xfer(adap, 0x47, 0, I2C_SMBUS_READ,
0264                     ONYX_REG_CONTROL, I2C_SMBUS_BYTE_DATA,
0265                     &data);
0266         if (rc >= 0)
0267             i2c_powermac_create_one(adap, "MAC,pcm3052", 0x47);
0268     }
0269 }
0270 
0271 static bool i2c_powermac_get_type(struct i2c_adapter *adap,
0272                         struct device_node *node,
0273                         u32 addr, char *type, int type_size)
0274 {
0275     char tmp[16];
0276 
0277     /*
0278      * Note: we do _NOT_ want the standard i2c drivers to match with any of
0279      * our powermac stuff unless they have been specifically modified to
0280      * handle it on a case by case basis. For example, for thermal control,
0281      * things like lm75 etc... shall match with their corresponding
0282      * windfarm drivers, _NOT_ the generic ones, so we force a prefix of
0283      * 'MAC', onto the modalias to make that happen
0284      */
0285 
0286     /* First try proper modalias */
0287     if (of_modalias_node(node, tmp, sizeof(tmp)) >= 0) {
0288         snprintf(type, type_size, "MAC,%s", tmp);
0289         return true;
0290     }
0291 
0292     /* Now look for known workarounds */
0293     if (of_node_name_eq(node, "deq")) {
0294         /* Apple uses address 0x34 for TAS3001 and 0x35 for TAS3004 */
0295         if (addr == 0x34) {
0296             snprintf(type, type_size, "MAC,tas3001");
0297             return true;
0298         } else if (addr == 0x35) {
0299             snprintf(type, type_size, "MAC,tas3004");
0300             return true;
0301         }
0302     }
0303 
0304     dev_err(&adap->dev, "i2c-powermac: modalias failure on %pOF\n", node);
0305     return false;
0306 }
0307 
0308 static void i2c_powermac_register_devices(struct i2c_adapter *adap,
0309                             struct pmac_i2c_bus *bus)
0310 {
0311     struct i2c_client *newdev;
0312     struct device_node *node;
0313     bool found_onyx = false;
0314 
0315     /*
0316      * In some cases we end up with the via-pmu node itself, in this
0317      * case we skip this function completely as the device-tree will
0318      * not contain anything useful.
0319      */
0320     if (of_node_name_eq(adap->dev.of_node, "via-pmu"))
0321         return;
0322 
0323     for_each_child_of_node(adap->dev.of_node, node) {
0324         struct i2c_board_info info = {};
0325         u32 addr;
0326 
0327         /* Get address & channel */
0328         addr = i2c_powermac_get_addr(adap, bus, node);
0329         if (addr == 0xffffffff)
0330             continue;
0331 
0332         /* Multibus setup, check channel */
0333         if (!pmac_i2c_match_adapter(node, adap))
0334             continue;
0335 
0336         dev_dbg(&adap->dev, "i2c-powermac: register %pOF\n", node);
0337 
0338         /*
0339          * Keep track of some device existence to handle
0340          * workarounds later.
0341          */
0342         if (of_device_is_compatible(node, "pcm3052"))
0343             found_onyx = true;
0344 
0345         /* Make up a modalias */
0346         if (!i2c_powermac_get_type(adap, node, addr,
0347                        info.type, sizeof(info.type))) {
0348             continue;
0349         }
0350 
0351         /* Fill out the rest of the info structure */
0352         info.addr = addr;
0353         info.irq = irq_of_parse_and_map(node, 0);
0354         info.of_node = of_node_get(node);
0355 
0356         newdev = i2c_new_client_device(adap, &info);
0357         if (IS_ERR(newdev)) {
0358             dev_err(&adap->dev, "i2c-powermac: Failure to register"
0359                 " %pOF\n", node);
0360             of_node_put(node);
0361             /* We do not dispose of the interrupt mapping on
0362              * purpose. It's not necessary (interrupt cannot be
0363              * re-used) and somebody else might have grabbed it
0364              * via direct DT lookup so let's not bother
0365              */
0366             continue;
0367         }
0368     }
0369 
0370     /* Additional workarounds */
0371     i2c_powermac_add_missing(adap, bus, found_onyx);
0372 }
0373 
0374 static int i2c_powermac_probe(struct platform_device *dev)
0375 {
0376     struct pmac_i2c_bus *bus = dev_get_platdata(&dev->dev);
0377     struct device_node *parent;
0378     struct i2c_adapter *adapter;
0379     int rc;
0380 
0381     if (bus == NULL)
0382         return -EINVAL;
0383     adapter = pmac_i2c_get_adapter(bus);
0384 
0385     /* Ok, now we need to make up a name for the interface that will
0386      * match what we used to do in the past, that is basically the
0387      * controller's parent device node for keywest. PMU didn't have a
0388      * naming convention and SMU has a different one
0389      */
0390     switch(pmac_i2c_get_type(bus)) {
0391     case pmac_i2c_bus_keywest:
0392         parent = of_get_parent(pmac_i2c_get_controller(bus));
0393         if (parent == NULL)
0394             return -EINVAL;
0395         snprintf(adapter->name, sizeof(adapter->name), "%pOFn %d",
0396              parent,
0397              pmac_i2c_get_channel(bus));
0398         of_node_put(parent);
0399         break;
0400     case pmac_i2c_bus_pmu:
0401         snprintf(adapter->name, sizeof(adapter->name), "pmu %d",
0402              pmac_i2c_get_channel(bus));
0403         break;
0404     case pmac_i2c_bus_smu:
0405         /* This is not what we used to do but I'm fixing drivers at
0406          * the same time as this change
0407          */
0408         snprintf(adapter->name, sizeof(adapter->name), "smu %d",
0409              pmac_i2c_get_channel(bus));
0410         break;
0411     default:
0412         return -EINVAL;
0413     }
0414 
0415     platform_set_drvdata(dev, adapter);
0416     adapter->algo = &i2c_powermac_algorithm;
0417     adapter->quirks = &i2c_powermac_quirks;
0418     i2c_set_adapdata(adapter, bus);
0419     adapter->dev.parent = &dev->dev;
0420 
0421     /* Clear of_node to skip automatic registration of i2c child nodes */
0422     adapter->dev.of_node = NULL;
0423     rc = i2c_add_adapter(adapter);
0424     if (rc) {
0425         printk(KERN_ERR "i2c-powermac: Adapter %s registration "
0426                "failed\n", adapter->name);
0427         memset(adapter, 0, sizeof(*adapter));
0428         return rc;
0429     }
0430 
0431     printk(KERN_INFO "PowerMac i2c bus %s registered\n", adapter->name);
0432 
0433     /* Use custom child registration due to Apple device-tree funkyness */
0434     adapter->dev.of_node = dev->dev.of_node;
0435     i2c_powermac_register_devices(adapter, bus);
0436 
0437     return 0;
0438 }
0439 
0440 static struct platform_driver i2c_powermac_driver = {
0441     .probe = i2c_powermac_probe,
0442     .remove = i2c_powermac_remove,
0443     .driver = {
0444         .name = "i2c-powermac",
0445         .bus = &platform_bus_type,
0446     },
0447 };
0448 
0449 module_platform_driver(i2c_powermac_driver);
0450 
0451 MODULE_ALIAS("platform:i2c-powermac");