Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
0002 /*
0003  * AMD MP2 platform driver
0004  *
0005  * Setup the I2C adapters enumerated in the ACPI namespace.
0006  * MP2 controllers have 2 separate busses, up to 2 I2C adapters may be listed.
0007  *
0008  * Authors: Nehal Bakulchandra Shah <Nehal-bakulchandra.shah@amd.com>
0009  *          Elie Morisse <syniurge@gmail.com>
0010  */
0011 
0012 #include <linux/acpi.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/slab.h>
0017 #include <linux/types.h>
0018 
0019 #include "i2c-amd-mp2.h"
0020 
0021 #define AMD_MP2_I2C_MAX_RW_LENGTH ((1 << 12) - 1)
0022 #define AMD_I2C_TIMEOUT (msecs_to_jiffies(250))
0023 
0024 /**
0025  * struct amd_i2c_dev - MP2 bus/i2c adapter context
0026  * @common: shared context with the MP2 PCI driver
0027  * @pdev: platform driver node
0028  * @adap: i2c adapter
0029  * @cmd_complete: xfer completion object
0030  */
0031 struct amd_i2c_dev {
0032     struct amd_i2c_common common;
0033     struct platform_device *pdev;
0034     struct i2c_adapter adap;
0035     struct completion cmd_complete;
0036 };
0037 
0038 #define amd_i2c_dev_common(__common) \
0039     container_of(__common, struct amd_i2c_dev, common)
0040 
0041 static int i2c_amd_dma_map(struct amd_i2c_common *i2c_common)
0042 {
0043     struct device *dev_pci = &i2c_common->mp2_dev->pci_dev->dev;
0044     struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
0045     enum dma_data_direction dma_direction =
0046             i2c_common->msg->flags & I2C_M_RD ?
0047             DMA_FROM_DEVICE : DMA_TO_DEVICE;
0048 
0049     i2c_common->dma_buf = i2c_get_dma_safe_msg_buf(i2c_common->msg, 0);
0050     i2c_common->dma_addr = dma_map_single(dev_pci, i2c_common->dma_buf,
0051                           i2c_common->msg->len,
0052                           dma_direction);
0053 
0054     if (unlikely(dma_mapping_error(dev_pci, i2c_common->dma_addr))) {
0055         dev_err(&i2c_dev->pdev->dev,
0056             "Error while mapping dma buffer %p\n",
0057             i2c_common->dma_buf);
0058         return -EIO;
0059     }
0060 
0061     return 0;
0062 }
0063 
0064 static void i2c_amd_dma_unmap(struct amd_i2c_common *i2c_common)
0065 {
0066     struct device *dev_pci = &i2c_common->mp2_dev->pci_dev->dev;
0067     enum dma_data_direction dma_direction =
0068             i2c_common->msg->flags & I2C_M_RD ?
0069             DMA_FROM_DEVICE : DMA_TO_DEVICE;
0070 
0071     dma_unmap_single(dev_pci, i2c_common->dma_addr,
0072              i2c_common->msg->len, dma_direction);
0073 
0074     i2c_put_dma_safe_msg_buf(i2c_common->dma_buf, i2c_common->msg, true);
0075 }
0076 
0077 static void i2c_amd_start_cmd(struct amd_i2c_dev *i2c_dev)
0078 {
0079     struct amd_i2c_common *i2c_common = &i2c_dev->common;
0080 
0081     reinit_completion(&i2c_dev->cmd_complete);
0082     i2c_common->cmd_success = false;
0083 }
0084 
0085 static void i2c_amd_cmd_completion(struct amd_i2c_common *i2c_common)
0086 {
0087     struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
0088     union i2c_event *event = &i2c_common->eventval;
0089 
0090     if (event->r.status == i2c_readcomplete_event)
0091         dev_dbg(&i2c_dev->pdev->dev, "readdata:%*ph\n", event->r.length,
0092             i2c_common->msg->buf);
0093 
0094     complete(&i2c_dev->cmd_complete);
0095 }
0096 
0097 static int i2c_amd_check_cmd_completion(struct amd_i2c_dev *i2c_dev)
0098 {
0099     struct amd_i2c_common *i2c_common = &i2c_dev->common;
0100     unsigned long timeout;
0101 
0102     timeout = wait_for_completion_timeout(&i2c_dev->cmd_complete,
0103                           i2c_dev->adap.timeout);
0104 
0105     if ((i2c_common->reqcmd == i2c_read ||
0106          i2c_common->reqcmd == i2c_write) &&
0107         i2c_common->msg->len > 32)
0108         i2c_amd_dma_unmap(i2c_common);
0109 
0110     if (timeout == 0) {
0111         amd_mp2_rw_timeout(i2c_common);
0112         return -ETIMEDOUT;
0113     }
0114 
0115     amd_mp2_process_event(i2c_common);
0116 
0117     if (!i2c_common->cmd_success)
0118         return -EIO;
0119 
0120     return 0;
0121 }
0122 
0123 static int i2c_amd_enable_set(struct amd_i2c_dev *i2c_dev, bool enable)
0124 {
0125     struct amd_i2c_common *i2c_common = &i2c_dev->common;
0126 
0127     i2c_amd_start_cmd(i2c_dev);
0128     amd_mp2_bus_enable_set(i2c_common, enable);
0129 
0130     return i2c_amd_check_cmd_completion(i2c_dev);
0131 }
0132 
0133 static int i2c_amd_xfer_msg(struct amd_i2c_dev *i2c_dev, struct i2c_msg *pmsg)
0134 {
0135     struct amd_i2c_common *i2c_common = &i2c_dev->common;
0136 
0137     i2c_amd_start_cmd(i2c_dev);
0138     i2c_common->msg = pmsg;
0139 
0140     if (pmsg->len > 32)
0141         if (i2c_amd_dma_map(i2c_common))
0142             return -EIO;
0143 
0144     if (pmsg->flags & I2C_M_RD)
0145         amd_mp2_rw(i2c_common, i2c_read);
0146     else
0147         amd_mp2_rw(i2c_common, i2c_write);
0148 
0149     return i2c_amd_check_cmd_completion(i2c_dev);
0150 }
0151 
0152 static int i2c_amd_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
0153 {
0154     struct amd_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
0155     int i;
0156     struct i2c_msg *pmsg;
0157     int err = 0;
0158 
0159     /* the adapter might have been deleted while waiting for the bus lock */
0160     if (unlikely(!i2c_dev->common.mp2_dev))
0161         return -EINVAL;
0162 
0163     amd_mp2_pm_runtime_get(i2c_dev->common.mp2_dev);
0164 
0165     for (i = 0; i < num; i++) {
0166         pmsg = &msgs[i];
0167         err = i2c_amd_xfer_msg(i2c_dev, pmsg);
0168         if (err)
0169             break;
0170     }
0171 
0172     amd_mp2_pm_runtime_put(i2c_dev->common.mp2_dev);
0173     return err ? err : num;
0174 }
0175 
0176 static u32 i2c_amd_func(struct i2c_adapter *a)
0177 {
0178     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0179 }
0180 
0181 static const struct i2c_algorithm i2c_amd_algorithm = {
0182     .master_xfer = i2c_amd_xfer,
0183     .functionality = i2c_amd_func,
0184 };
0185 
0186 #ifdef CONFIG_PM
0187 static int i2c_amd_suspend(struct amd_i2c_common *i2c_common)
0188 {
0189     struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
0190 
0191     i2c_amd_enable_set(i2c_dev, false);
0192     return 0;
0193 }
0194 
0195 static int i2c_amd_resume(struct amd_i2c_common *i2c_common)
0196 {
0197     struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
0198 
0199     return i2c_amd_enable_set(i2c_dev, true);
0200 }
0201 #endif
0202 
0203 static const u32 supported_speeds[] = {
0204     I2C_MAX_HIGH_SPEED_MODE_FREQ,
0205     I2C_MAX_TURBO_MODE_FREQ,
0206     I2C_MAX_FAST_MODE_PLUS_FREQ,
0207     I2C_MAX_FAST_MODE_FREQ,
0208     I2C_MAX_STANDARD_MODE_FREQ,
0209 };
0210 
0211 static enum speed_enum i2c_amd_get_bus_speed(struct platform_device *pdev)
0212 {
0213     u32 acpi_speed;
0214     int i;
0215 
0216     acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev);
0217     /* round down to the lowest standard speed */
0218     for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) {
0219         if (acpi_speed >= supported_speeds[i])
0220             break;
0221     }
0222     acpi_speed = i < ARRAY_SIZE(supported_speeds) ? supported_speeds[i] : 0;
0223 
0224     switch (acpi_speed) {
0225     case I2C_MAX_STANDARD_MODE_FREQ:
0226         return speed100k;
0227     case I2C_MAX_FAST_MODE_FREQ:
0228         return speed400k;
0229     case I2C_MAX_FAST_MODE_PLUS_FREQ:
0230         return speed1000k;
0231     case I2C_MAX_TURBO_MODE_FREQ:
0232         return speed1400k;
0233     case I2C_MAX_HIGH_SPEED_MODE_FREQ:
0234         return speed3400k;
0235     default:
0236         return speed400k;
0237     }
0238 }
0239 
0240 static const struct i2c_adapter_quirks amd_i2c_dev_quirks = {
0241     .max_read_len = AMD_MP2_I2C_MAX_RW_LENGTH,
0242     .max_write_len = AMD_MP2_I2C_MAX_RW_LENGTH,
0243 };
0244 
0245 static int i2c_amd_probe(struct platform_device *pdev)
0246 {
0247     int ret;
0248     struct amd_i2c_dev *i2c_dev;
0249     struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
0250     struct amd_mp2_dev *mp2_dev;
0251     const char *uid;
0252 
0253     if (!adev)
0254         return -ENODEV;
0255 
0256     /* The ACPI namespace doesn't contain information about which MP2 PCI
0257      * device an AMDI0011 ACPI device is related to, so assume that there's
0258      * only one MP2 PCI device per system.
0259      */
0260     mp2_dev = amd_mp2_find_device();
0261     if (!mp2_dev || !mp2_dev->probed)
0262         /* The MP2 PCI device should get probed later */
0263         return -EPROBE_DEFER;
0264 
0265     i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
0266     if (!i2c_dev)
0267         return -ENOMEM;
0268 
0269     i2c_dev->common.mp2_dev = mp2_dev;
0270     i2c_dev->pdev = pdev;
0271     platform_set_drvdata(pdev, i2c_dev);
0272 
0273     i2c_dev->common.cmd_completion = &i2c_amd_cmd_completion;
0274 #ifdef CONFIG_PM
0275     i2c_dev->common.suspend = &i2c_amd_suspend;
0276     i2c_dev->common.resume = &i2c_amd_resume;
0277 #endif
0278 
0279     uid = adev->pnp.unique_id;
0280     if (!uid) {
0281         dev_err(&pdev->dev, "missing UID/bus id!\n");
0282         return -EINVAL;
0283     } else if (strcmp(uid, "0") == 0) {
0284         i2c_dev->common.bus_id = 0;
0285     } else if (strcmp(uid, "1") == 0) {
0286         i2c_dev->common.bus_id = 1;
0287     } else {
0288         dev_err(&pdev->dev, "incorrect UID/bus id \"%s\"!\n", uid);
0289         return -EINVAL;
0290     }
0291     dev_dbg(&pdev->dev, "bus id is %u\n", i2c_dev->common.bus_id);
0292 
0293     /* Register the adapter */
0294     amd_mp2_pm_runtime_get(mp2_dev);
0295 
0296     i2c_dev->common.reqcmd = i2c_none;
0297     if (amd_mp2_register_cb(&i2c_dev->common))
0298         return -EINVAL;
0299     device_link_add(&i2c_dev->pdev->dev, &mp2_dev->pci_dev->dev,
0300             DL_FLAG_AUTOREMOVE_CONSUMER);
0301 
0302     i2c_dev->common.i2c_speed = i2c_amd_get_bus_speed(pdev);
0303 
0304     /* Setup i2c adapter description */
0305     i2c_dev->adap.owner = THIS_MODULE;
0306     i2c_dev->adap.algo = &i2c_amd_algorithm;
0307     i2c_dev->adap.quirks = &amd_i2c_dev_quirks;
0308     i2c_dev->adap.dev.parent = &pdev->dev;
0309     i2c_dev->adap.algo_data = i2c_dev;
0310     i2c_dev->adap.timeout = AMD_I2C_TIMEOUT;
0311     ACPI_COMPANION_SET(&i2c_dev->adap.dev, ACPI_COMPANION(&pdev->dev));
0312     i2c_dev->adap.dev.of_node = pdev->dev.of_node;
0313     snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
0314          "AMD MP2 i2c bus %u", i2c_dev->common.bus_id);
0315     i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
0316 
0317     init_completion(&i2c_dev->cmd_complete);
0318 
0319     /* Enable the bus */
0320     if (i2c_amd_enable_set(i2c_dev, true))
0321         dev_err(&pdev->dev, "initial bus enable failed\n");
0322 
0323     /* Attach to the i2c layer */
0324     ret = i2c_add_adapter(&i2c_dev->adap);
0325 
0326     amd_mp2_pm_runtime_put(mp2_dev);
0327 
0328     if (ret < 0)
0329         dev_err(&pdev->dev, "i2c add adapter failed = %d\n", ret);
0330 
0331     return ret;
0332 }
0333 
0334 static int i2c_amd_remove(struct platform_device *pdev)
0335 {
0336     struct amd_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
0337     struct amd_i2c_common *i2c_common = &i2c_dev->common;
0338 
0339     i2c_lock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
0340 
0341     i2c_amd_enable_set(i2c_dev, false);
0342     amd_mp2_unregister_cb(i2c_common);
0343     i2c_common->mp2_dev = NULL;
0344 
0345     i2c_unlock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
0346 
0347     i2c_del_adapter(&i2c_dev->adap);
0348     return 0;
0349 }
0350 
0351 static const struct acpi_device_id i2c_amd_acpi_match[] = {
0352     { "AMDI0011" },
0353     { },
0354 };
0355 MODULE_DEVICE_TABLE(acpi, i2c_amd_acpi_match);
0356 
0357 static struct platform_driver i2c_amd_plat_driver = {
0358     .probe = i2c_amd_probe,
0359     .remove = i2c_amd_remove,
0360     .driver = {
0361         .name = "i2c_amd_mp2",
0362         .acpi_match_table = ACPI_PTR(i2c_amd_acpi_match),
0363     },
0364 };
0365 module_platform_driver(i2c_amd_plat_driver);
0366 
0367 MODULE_DESCRIPTION("AMD(R) MP2 I2C Platform Driver");
0368 MODULE_AUTHOR("Nehal Shah <nehal-bakulchandra.shah@amd.com>");
0369 MODULE_AUTHOR("Elie Morisse <syniurge@gmail.com>");
0370 MODULE_LICENSE("Dual BSD/GPL");