Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * i2c-smbus.c - SMBus extensions to the I2C protocol
0004  *
0005  * Copyright (C) 2008 David Brownell
0006  * Copyright (C) 2010-2019 Jean Delvare <jdelvare@suse.de>
0007  */
0008 
0009 #include <linux/device.h>
0010 #include <linux/dmi.h>
0011 #include <linux/i2c.h>
0012 #include <linux/i2c-smbus.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/property.h>
0017 #include <linux/slab.h>
0018 #include <linux/workqueue.h>
0019 
0020 struct i2c_smbus_alert {
0021     struct work_struct  alert;
0022     struct i2c_client   *ara;       /* Alert response address */
0023 };
0024 
0025 struct alert_data {
0026     unsigned short      addr;
0027     enum i2c_alert_protocol type;
0028     unsigned int        data;
0029 };
0030 
0031 /* If this is the alerting device, notify its driver */
0032 static int smbus_do_alert(struct device *dev, void *addrp)
0033 {
0034     struct i2c_client *client = i2c_verify_client(dev);
0035     struct alert_data *data = addrp;
0036     struct i2c_driver *driver;
0037 
0038     if (!client || client->addr != data->addr)
0039         return 0;
0040     if (client->flags & I2C_CLIENT_TEN)
0041         return 0;
0042 
0043     /*
0044      * Drivers should either disable alerts, or provide at least
0045      * a minimal handler.  Lock so the driver won't change.
0046      */
0047     device_lock(dev);
0048     if (client->dev.driver) {
0049         driver = to_i2c_driver(client->dev.driver);
0050         if (driver->alert)
0051             driver->alert(client, data->type, data->data);
0052         else
0053             dev_warn(&client->dev, "no driver alert()!\n");
0054     } else
0055         dev_dbg(&client->dev, "alert with no driver\n");
0056     device_unlock(dev);
0057 
0058     /* Stop iterating after we find the device */
0059     return -EBUSY;
0060 }
0061 
0062 /*
0063  * The alert IRQ handler needs to hand work off to a task which can issue
0064  * SMBus calls, because those sleeping calls can't be made in IRQ context.
0065  */
0066 static irqreturn_t smbus_alert(int irq, void *d)
0067 {
0068     struct i2c_smbus_alert *alert = d;
0069     struct i2c_client *ara;
0070 
0071     ara = alert->ara;
0072 
0073     for (;;) {
0074         s32 status;
0075         struct alert_data data;
0076 
0077         /*
0078          * Devices with pending alerts reply in address order, low
0079          * to high, because of slave transmit arbitration.  After
0080          * responding, an SMBus device stops asserting SMBALERT#.
0081          *
0082          * Note that SMBus 2.0 reserves 10-bit addresses for future
0083          * use.  We neither handle them, nor try to use PEC here.
0084          */
0085         status = i2c_smbus_read_byte(ara);
0086         if (status < 0)
0087             break;
0088 
0089         data.data = status & 1;
0090         data.addr = status >> 1;
0091         data.type = I2C_PROTOCOL_SMBUS_ALERT;
0092 
0093         dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
0094             data.addr, data.data);
0095 
0096         /* Notify driver for the device which issued the alert */
0097         device_for_each_child(&ara->adapter->dev, &data,
0098                       smbus_do_alert);
0099     }
0100 
0101     return IRQ_HANDLED;
0102 }
0103 
0104 static void smbalert_work(struct work_struct *work)
0105 {
0106     struct i2c_smbus_alert *alert;
0107 
0108     alert = container_of(work, struct i2c_smbus_alert, alert);
0109 
0110     smbus_alert(0, alert);
0111 
0112 }
0113 
0114 /* Setup SMBALERT# infrastructure */
0115 static int smbalert_probe(struct i2c_client *ara,
0116               const struct i2c_device_id *id)
0117 {
0118     struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
0119     struct i2c_smbus_alert *alert;
0120     struct i2c_adapter *adapter = ara->adapter;
0121     int res, irq;
0122 
0123     alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
0124                  GFP_KERNEL);
0125     if (!alert)
0126         return -ENOMEM;
0127 
0128     if (setup) {
0129         irq = setup->irq;
0130     } else {
0131         irq = fwnode_irq_get_byname(dev_fwnode(adapter->dev.parent),
0132                         "smbus_alert");
0133         if (irq <= 0)
0134             return irq;
0135     }
0136 
0137     INIT_WORK(&alert->alert, smbalert_work);
0138     alert->ara = ara;
0139 
0140     if (irq > 0) {
0141         res = devm_request_threaded_irq(&ara->dev, irq,
0142                         NULL, smbus_alert,
0143                         IRQF_SHARED | IRQF_ONESHOT,
0144                         "smbus_alert", alert);
0145         if (res)
0146             return res;
0147     }
0148 
0149     i2c_set_clientdata(ara, alert);
0150     dev_info(&adapter->dev, "supports SMBALERT#\n");
0151 
0152     return 0;
0153 }
0154 
0155 /* IRQ and memory resources are managed so they are freed automatically */
0156 static int smbalert_remove(struct i2c_client *ara)
0157 {
0158     struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
0159 
0160     cancel_work_sync(&alert->alert);
0161     return 0;
0162 }
0163 
0164 static const struct i2c_device_id smbalert_ids[] = {
0165     { "smbus_alert", 0 },
0166     { /* LIST END */ }
0167 };
0168 MODULE_DEVICE_TABLE(i2c, smbalert_ids);
0169 
0170 static struct i2c_driver smbalert_driver = {
0171     .driver = {
0172         .name   = "smbus_alert",
0173     },
0174     .probe      = smbalert_probe,
0175     .remove     = smbalert_remove,
0176     .id_table   = smbalert_ids,
0177 };
0178 
0179 /**
0180  * i2c_handle_smbus_alert - Handle an SMBus alert
0181  * @ara: the ARA client on the relevant adapter
0182  * Context: can't sleep
0183  *
0184  * Helper function to be called from an I2C bus driver's interrupt
0185  * handler. It will schedule the alert work, in turn calling the
0186  * corresponding I2C device driver's alert function.
0187  *
0188  * It is assumed that ara is a valid i2c client previously returned by
0189  * i2c_new_smbus_alert_device().
0190  */
0191 int i2c_handle_smbus_alert(struct i2c_client *ara)
0192 {
0193     struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
0194 
0195     return schedule_work(&alert->alert);
0196 }
0197 EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
0198 
0199 module_i2c_driver(smbalert_driver);
0200 
0201 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0202 #define SMBUS_HOST_NOTIFY_LEN   3
0203 struct i2c_slave_host_notify_status {
0204     u8 index;
0205     u8 addr;
0206 };
0207 
0208 static int i2c_slave_host_notify_cb(struct i2c_client *client,
0209                     enum i2c_slave_event event, u8 *val)
0210 {
0211     struct i2c_slave_host_notify_status *status = client->dev.platform_data;
0212 
0213     switch (event) {
0214     case I2C_SLAVE_WRITE_RECEIVED:
0215         /* We only retrieve the first byte received (addr)
0216          * since there is currently no support to retrieve the data
0217          * parameter from the client.
0218          */
0219         if (status->index == 0)
0220             status->addr = *val;
0221         if (status->index < U8_MAX)
0222             status->index++;
0223         break;
0224     case I2C_SLAVE_STOP:
0225         if (status->index == SMBUS_HOST_NOTIFY_LEN)
0226             i2c_handle_smbus_host_notify(client->adapter,
0227                              status->addr);
0228         fallthrough;
0229     case I2C_SLAVE_WRITE_REQUESTED:
0230         status->index = 0;
0231         break;
0232     case I2C_SLAVE_READ_REQUESTED:
0233     case I2C_SLAVE_READ_PROCESSED:
0234         *val = 0xff;
0235         break;
0236     }
0237 
0238     return 0;
0239 }
0240 
0241 /**
0242  * i2c_new_slave_host_notify_device - get a client for SMBus host-notify support
0243  * @adapter: the target adapter
0244  * Context: can sleep
0245  *
0246  * Setup handling of the SMBus host-notify protocol on a given I2C bus segment.
0247  *
0248  * Handling is done by creating a device and its callback and handling data
0249  * received via the SMBus host-notify address (0x8)
0250  *
0251  * This returns the client, which should be ultimately freed using
0252  * i2c_free_slave_host_notify_device(); or an ERRPTR to indicate an error.
0253  */
0254 struct i2c_client *i2c_new_slave_host_notify_device(struct i2c_adapter *adapter)
0255 {
0256     struct i2c_board_info host_notify_board_info = {
0257         I2C_BOARD_INFO("smbus_host_notify", 0x08),
0258         .flags  = I2C_CLIENT_SLAVE,
0259     };
0260     struct i2c_slave_host_notify_status *status;
0261     struct i2c_client *client;
0262     int ret;
0263 
0264     status = kzalloc(sizeof(struct i2c_slave_host_notify_status),
0265              GFP_KERNEL);
0266     if (!status)
0267         return ERR_PTR(-ENOMEM);
0268 
0269     host_notify_board_info.platform_data = status;
0270 
0271     client = i2c_new_client_device(adapter, &host_notify_board_info);
0272     if (IS_ERR(client)) {
0273         kfree(status);
0274         return client;
0275     }
0276 
0277     ret = i2c_slave_register(client, i2c_slave_host_notify_cb);
0278     if (ret) {
0279         i2c_unregister_device(client);
0280         kfree(status);
0281         return ERR_PTR(ret);
0282     }
0283 
0284     return client;
0285 }
0286 EXPORT_SYMBOL_GPL(i2c_new_slave_host_notify_device);
0287 
0288 /**
0289  * i2c_free_slave_host_notify_device - free the client for SMBus host-notify
0290  * support
0291  * @client: the client to free
0292  * Context: can sleep
0293  *
0294  * Free the i2c_client allocated via i2c_new_slave_host_notify_device
0295  */
0296 void i2c_free_slave_host_notify_device(struct i2c_client *client)
0297 {
0298     if (IS_ERR_OR_NULL(client))
0299         return;
0300 
0301     i2c_slave_unregister(client);
0302     kfree(client->dev.platform_data);
0303     i2c_unregister_device(client);
0304 }
0305 EXPORT_SYMBOL_GPL(i2c_free_slave_host_notify_device);
0306 #endif
0307 
0308 /*
0309  * SPD is not part of SMBus but we include it here for convenience as the
0310  * target systems are the same.
0311  * Restrictions to automatic SPD instantiation:
0312  *  - Only works if all filled slots have the same memory type
0313  *  - Only works for DDR2, DDR3 and DDR4 for now
0314  *  - Only works on systems with 1 to 4 memory slots
0315  */
0316 #if IS_ENABLED(CONFIG_DMI)
0317 void i2c_register_spd(struct i2c_adapter *adap)
0318 {
0319     int n, slot_count = 0, dimm_count = 0;
0320     u16 handle;
0321     u8 common_mem_type = 0x0, mem_type;
0322     u64 mem_size;
0323     const char *name;
0324 
0325     while ((handle = dmi_memdev_handle(slot_count)) != 0xffff) {
0326         slot_count++;
0327 
0328         /* Skip empty slots */
0329         mem_size = dmi_memdev_size(handle);
0330         if (!mem_size)
0331             continue;
0332 
0333         /* Skip undefined memory type */
0334         mem_type = dmi_memdev_type(handle);
0335         if (mem_type <= 0x02)       /* Invalid, Other, Unknown */
0336             continue;
0337 
0338         if (!common_mem_type) {
0339             /* First filled slot */
0340             common_mem_type = mem_type;
0341         } else {
0342             /* Check that all filled slots have the same type */
0343             if (mem_type != common_mem_type) {
0344                 dev_warn(&adap->dev,
0345                      "Different memory types mixed, not instantiating SPD\n");
0346                 return;
0347             }
0348         }
0349         dimm_count++;
0350     }
0351 
0352     /* No useful DMI data, bail out */
0353     if (!dimm_count)
0354         return;
0355 
0356     dev_info(&adap->dev, "%d/%d memory slots populated (from DMI)\n",
0357          dimm_count, slot_count);
0358 
0359     if (slot_count > 4) {
0360         dev_warn(&adap->dev,
0361              "Systems with more than 4 memory slots not supported yet, not instantiating SPD\n");
0362         return;
0363     }
0364 
0365     switch (common_mem_type) {
0366     case 0x13:  /* DDR2 */
0367     case 0x18:  /* DDR3 */
0368     case 0x1C:  /* LPDDR2 */
0369     case 0x1D:  /* LPDDR3 */
0370         name = "spd";
0371         break;
0372     case 0x1A:  /* DDR4 */
0373     case 0x1E:  /* LPDDR4 */
0374         name = "ee1004";
0375         break;
0376     default:
0377         dev_info(&adap->dev,
0378              "Memory type 0x%02x not supported yet, not instantiating SPD\n",
0379              common_mem_type);
0380         return;
0381     }
0382 
0383     /*
0384      * We don't know in which slots the memory modules are. We could
0385      * try to guess from the slot names, but that would be rather complex
0386      * and unreliable, so better probe all possible addresses until we
0387      * have found all memory modules.
0388      */
0389     for (n = 0; n < slot_count && dimm_count; n++) {
0390         struct i2c_board_info info;
0391         unsigned short addr_list[2];
0392 
0393         memset(&info, 0, sizeof(struct i2c_board_info));
0394         strscpy(info.type, name, I2C_NAME_SIZE);
0395         addr_list[0] = 0x50 + n;
0396         addr_list[1] = I2C_CLIENT_END;
0397 
0398         if (!IS_ERR(i2c_new_scanned_device(adap, &info, addr_list, NULL))) {
0399             dev_info(&adap->dev,
0400                  "Successfully instantiated SPD at 0x%hx\n",
0401                  addr_list[0]);
0402             dimm_count--;
0403         }
0404     }
0405 }
0406 EXPORT_SYMBOL_GPL(i2c_register_spd);
0407 #endif
0408 
0409 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
0410 MODULE_DESCRIPTION("SMBus protocol extensions support");
0411 MODULE_LICENSE("GPL");