Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * IBM OPAL I2C driver
0004  * Copyright (C) 2014 IBM
0005  */
0006 
0007 #include <linux/device.h>
0008 #include <linux/i2c.h>
0009 #include <linux/kernel.h>
0010 #include <linux/mm.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/slab.h>
0015 
0016 #include <asm/firmware.h>
0017 #include <asm/opal.h>
0018 
0019 static int i2c_opal_translate_error(int rc)
0020 {
0021     switch (rc) {
0022     case OPAL_NO_MEM:
0023         return -ENOMEM;
0024     case OPAL_PARAMETER:
0025         return -EINVAL;
0026     case OPAL_I2C_ARBT_LOST:
0027         return -EAGAIN;
0028     case OPAL_I2C_TIMEOUT:
0029         return -ETIMEDOUT;
0030     case OPAL_I2C_NACK_RCVD:
0031         return -ENXIO;
0032     case OPAL_I2C_STOP_ERR:
0033         return -EBUSY;
0034     default:
0035         return -EIO;
0036     }
0037 }
0038 
0039 static int i2c_opal_send_request(u32 bus_id, struct opal_i2c_request *req)
0040 {
0041     struct opal_msg msg;
0042     int token, rc;
0043 
0044     token = opal_async_get_token_interruptible();
0045     if (token < 0) {
0046         if (token != -ERESTARTSYS)
0047             pr_err("Failed to get the async token\n");
0048 
0049         return token;
0050     }
0051 
0052     rc = opal_i2c_request(token, bus_id, req);
0053     if (rc != OPAL_ASYNC_COMPLETION) {
0054         rc = i2c_opal_translate_error(rc);
0055         goto exit;
0056     }
0057 
0058     rc = opal_async_wait_response(token, &msg);
0059     if (rc)
0060         goto exit;
0061 
0062     rc = opal_get_async_rc(msg);
0063     if (rc != OPAL_SUCCESS) {
0064         rc = i2c_opal_translate_error(rc);
0065         goto exit;
0066     }
0067 
0068 exit:
0069     opal_async_release_token(token);
0070     return rc;
0071 }
0072 
0073 static int i2c_opal_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
0074                 int num)
0075 {
0076     unsigned long opal_id = (unsigned long)adap->algo_data;
0077     struct opal_i2c_request req;
0078     int rc, i;
0079 
0080     /* We only support fairly simple combinations here of one
0081      * or two messages
0082      */
0083     memset(&req, 0, sizeof(req));
0084     switch(num) {
0085     case 1:
0086         req.type = (msgs[0].flags & I2C_M_RD) ?
0087             OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
0088         req.addr = cpu_to_be16(msgs[0].addr);
0089         req.size = cpu_to_be32(msgs[0].len);
0090         req.buffer_ra = cpu_to_be64(__pa(msgs[0].buf));
0091         break;
0092     case 2:
0093         req.type = (msgs[1].flags & I2C_M_RD) ?
0094             OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
0095         req.addr = cpu_to_be16(msgs[0].addr);
0096         req.subaddr_sz = msgs[0].len;
0097         for (i = 0; i < msgs[0].len; i++)
0098             req.subaddr = (req.subaddr << 8) | msgs[0].buf[i];
0099         req.subaddr = cpu_to_be32(req.subaddr);
0100         req.size = cpu_to_be32(msgs[1].len);
0101         req.buffer_ra = cpu_to_be64(__pa(msgs[1].buf));
0102         break;
0103     }
0104 
0105     rc = i2c_opal_send_request(opal_id, &req);
0106     if (rc)
0107         return rc;
0108 
0109     return num;
0110 }
0111 
0112 static int i2c_opal_smbus_xfer(struct i2c_adapter *adap, u16 addr,
0113                    unsigned short flags, char read_write,
0114                    u8 command, int size, union i2c_smbus_data *data)
0115 {
0116     unsigned long opal_id = (unsigned long)adap->algo_data;
0117     struct opal_i2c_request req;
0118     u8 local[2];
0119     int rc;
0120 
0121     memset(&req, 0, sizeof(req));
0122 
0123     req.addr = cpu_to_be16(addr);
0124     switch (size) {
0125     case I2C_SMBUS_BYTE:
0126         req.buffer_ra = cpu_to_be64(__pa(&data->byte));
0127         req.size = cpu_to_be32(1);
0128         fallthrough;
0129     case I2C_SMBUS_QUICK:
0130         req.type = (read_write == I2C_SMBUS_READ) ?
0131             OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
0132         break;
0133     case I2C_SMBUS_BYTE_DATA:
0134         req.buffer_ra = cpu_to_be64(__pa(&data->byte));
0135         req.size = cpu_to_be32(1);
0136         req.subaddr = cpu_to_be32(command);
0137         req.subaddr_sz = 1;
0138         req.type = (read_write == I2C_SMBUS_READ) ?
0139             OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
0140         break;
0141     case I2C_SMBUS_WORD_DATA:
0142         if (!read_write) {
0143             local[0] = data->word & 0xff;
0144             local[1] = (data->word >> 8) & 0xff;
0145         }
0146         req.buffer_ra = cpu_to_be64(__pa(local));
0147         req.size = cpu_to_be32(2);
0148         req.subaddr = cpu_to_be32(command);
0149         req.subaddr_sz = 1;
0150         req.type = (read_write == I2C_SMBUS_READ) ?
0151             OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
0152         break;
0153     case I2C_SMBUS_I2C_BLOCK_DATA:
0154         req.buffer_ra = cpu_to_be64(__pa(&data->block[1]));
0155         req.size = cpu_to_be32(data->block[0]);
0156         req.subaddr = cpu_to_be32(command);
0157         req.subaddr_sz = 1;
0158         req.type = (read_write == I2C_SMBUS_READ) ?
0159             OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
0160         break;
0161     default:
0162         return -EINVAL;
0163     }
0164 
0165     rc = i2c_opal_send_request(opal_id, &req);
0166     if (!rc && read_write && size == I2C_SMBUS_WORD_DATA) {
0167         data->word = ((u16)local[1]) << 8;
0168         data->word |= local[0];
0169     }
0170 
0171     return rc;
0172 }
0173 
0174 static u32 i2c_opal_func(struct i2c_adapter *adapter)
0175 {
0176     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
0177            I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
0178            I2C_FUNC_SMBUS_I2C_BLOCK;
0179 }
0180 
0181 static const struct i2c_algorithm i2c_opal_algo = {
0182     .master_xfer    = i2c_opal_master_xfer,
0183     .smbus_xfer = i2c_opal_smbus_xfer,
0184     .functionality  = i2c_opal_func,
0185 };
0186 
0187 /*
0188  * For two messages, we basically support simple smbus transactions of a
0189  * write-then-anything.
0190  */
0191 static const struct i2c_adapter_quirks i2c_opal_quirks = {
0192     .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
0193     .max_comb_1st_msg_len = 4,
0194 };
0195 
0196 static int i2c_opal_probe(struct platform_device *pdev)
0197 {
0198     struct i2c_adapter  *adapter;
0199     const char      *pname;
0200     u32         opal_id;
0201     int         rc;
0202 
0203     if (!pdev->dev.of_node)
0204         return -ENODEV;
0205 
0206     rc = of_property_read_u32(pdev->dev.of_node, "ibm,opal-id", &opal_id);
0207     if (rc) {
0208         dev_err(&pdev->dev, "Missing ibm,opal-id property !\n");
0209         return -EIO;
0210     }
0211 
0212     adapter = devm_kzalloc(&pdev->dev, sizeof(*adapter), GFP_KERNEL);
0213     if (!adapter)
0214         return -ENOMEM;
0215 
0216     adapter->algo = &i2c_opal_algo;
0217     adapter->algo_data = (void *)(unsigned long)opal_id;
0218     adapter->quirks = &i2c_opal_quirks;
0219     adapter->dev.parent = &pdev->dev;
0220     adapter->dev.of_node = of_node_get(pdev->dev.of_node);
0221     pname = of_get_property(pdev->dev.of_node, "ibm,port-name", NULL);
0222     if (pname)
0223         strscpy(adapter->name, pname, sizeof(adapter->name));
0224     else
0225         strscpy(adapter->name, "opal", sizeof(adapter->name));
0226 
0227     platform_set_drvdata(pdev, adapter);
0228     rc = i2c_add_adapter(adapter);
0229     if (rc)
0230         dev_err(&pdev->dev, "Failed to register the i2c adapter\n");
0231 
0232     return rc;
0233 }
0234 
0235 static int i2c_opal_remove(struct platform_device *pdev)
0236 {
0237     struct i2c_adapter *adapter = platform_get_drvdata(pdev);
0238 
0239     i2c_del_adapter(adapter);
0240 
0241     return 0;
0242 }
0243 
0244 static const struct of_device_id i2c_opal_of_match[] = {
0245     {
0246         .compatible = "ibm,opal-i2c",
0247     },
0248     { }
0249 };
0250 MODULE_DEVICE_TABLE(of, i2c_opal_of_match);
0251 
0252 static struct platform_driver i2c_opal_driver = {
0253     .probe  = i2c_opal_probe,
0254     .remove = i2c_opal_remove,
0255     .driver = {
0256         .name       = "i2c-opal",
0257         .of_match_table = i2c_opal_of_match,
0258     },
0259 };
0260 
0261 static int __init i2c_opal_init(void)
0262 {
0263     if (!firmware_has_feature(FW_FEATURE_OPAL))
0264         return -ENODEV;
0265 
0266     return platform_driver_register(&i2c_opal_driver);
0267 }
0268 module_init(i2c_opal_init);
0269 
0270 static void __exit i2c_opal_exit(void)
0271 {
0272     return platform_driver_unregister(&i2c_opal_driver);
0273 }
0274 module_exit(i2c_opal_exit);
0275 
0276 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
0277 MODULE_DESCRIPTION("IBM OPAL I2C driver");
0278 MODULE_LICENSE("GPL");