b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * i2c-smbus.c - SMBus extensions to the I2C protocol |
| 4 | * |
| 5 | * Copyright (C) 2008 David Brownell |
| 6 | * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/i2c-smbus.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of_irq.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | |
| 19 | struct i2c_smbus_alert { |
| 20 | struct work_struct alert; |
| 21 | struct i2c_client *ara; /* Alert response address */ |
| 22 | }; |
| 23 | |
| 24 | struct alert_data { |
| 25 | unsigned short addr; |
| 26 | enum i2c_alert_protocol type; |
| 27 | unsigned int data; |
| 28 | }; |
| 29 | |
| 30 | /* If this is the alerting device, notify its driver */ |
| 31 | static int smbus_do_alert(struct device *dev, void *addrp) |
| 32 | { |
| 33 | struct i2c_client *client = i2c_verify_client(dev); |
| 34 | struct alert_data *data = addrp; |
| 35 | struct i2c_driver *driver; |
| 36 | int ret; |
| 37 | |
| 38 | if (!client || client->addr != data->addr) |
| 39 | return 0; |
| 40 | if (client->flags & I2C_CLIENT_TEN) |
| 41 | return 0; |
| 42 | |
| 43 | /* |
| 44 | * Drivers should either disable alerts, or provide at least |
| 45 | * a minimal handler. Lock so the driver won't change. |
| 46 | */ |
| 47 | device_lock(dev); |
| 48 | if (client->dev.driver) { |
| 49 | driver = to_i2c_driver(client->dev.driver); |
| 50 | if (driver->alert) { |
| 51 | /* Stop iterating after we find the device */ |
| 52 | driver->alert(client, data->type, data->data); |
| 53 | ret = -EBUSY; |
| 54 | } else { |
| 55 | dev_warn(&client->dev, "no driver alert()!\n"); |
| 56 | ret = -EOPNOTSUPP; |
| 57 | } |
| 58 | } else { |
| 59 | dev_dbg(&client->dev, "alert with no driver\n"); |
| 60 | ret = -ENODEV; |
| 61 | } |
| 62 | device_unlock(dev); |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | /* Same as above, but call back all drivers with alert handler */ |
| 68 | |
| 69 | static int smbus_do_alert_force(struct device *dev, void *addrp) |
| 70 | { |
| 71 | struct i2c_client *client = i2c_verify_client(dev); |
| 72 | struct alert_data *data = addrp; |
| 73 | struct i2c_driver *driver; |
| 74 | |
| 75 | if (!client || (client->flags & I2C_CLIENT_TEN)) |
| 76 | return 0; |
| 77 | |
| 78 | /* |
| 79 | * Drivers should either disable alerts, or provide at least |
| 80 | * a minimal handler. Lock so the driver won't change. |
| 81 | */ |
| 82 | device_lock(dev); |
| 83 | if (client->dev.driver) { |
| 84 | driver = to_i2c_driver(client->dev.driver); |
| 85 | if (driver->alert) |
| 86 | driver->alert(client, data->type, data->data); |
| 87 | } |
| 88 | device_unlock(dev); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * The alert IRQ handler needs to hand work off to a task which can issue |
| 95 | * SMBus calls, because those sleeping calls can't be made in IRQ context. |
| 96 | */ |
| 97 | static irqreturn_t smbus_alert(int irq, void *d) |
| 98 | { |
| 99 | struct i2c_smbus_alert *alert = d; |
| 100 | struct i2c_client *ara; |
| 101 | unsigned short prev_addr = I2C_CLIENT_END; /* Not a valid address */ |
| 102 | |
| 103 | ara = alert->ara; |
| 104 | |
| 105 | for (;;) { |
| 106 | s32 status; |
| 107 | struct alert_data data; |
| 108 | |
| 109 | /* |
| 110 | * Devices with pending alerts reply in address order, low |
| 111 | * to high, because of slave transmit arbitration. After |
| 112 | * responding, an SMBus device stops asserting SMBALERT#. |
| 113 | * |
| 114 | * Note that SMBus 2.0 reserves 10-bit addresses for future |
| 115 | * use. We neither handle them, nor try to use PEC here. |
| 116 | */ |
| 117 | status = i2c_smbus_read_byte(ara); |
| 118 | if (status < 0) |
| 119 | break; |
| 120 | |
| 121 | data.data = status & 1; |
| 122 | data.addr = status >> 1; |
| 123 | data.type = I2C_PROTOCOL_SMBUS_ALERT; |
| 124 | |
| 125 | dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n", |
| 126 | data.addr, data.data); |
| 127 | |
| 128 | /* Notify driver for the device which issued the alert */ |
| 129 | status = device_for_each_child(&ara->adapter->dev, &data, |
| 130 | smbus_do_alert); |
| 131 | /* |
| 132 | * If we read the same address more than once, and the alert |
| 133 | * was not handled by a driver, it won't do any good to repeat |
| 134 | * the loop because it will never terminate. Try again, this |
| 135 | * time calling the alert handlers of all devices connected to |
| 136 | * the bus, and abort the loop afterwards. If this helps, we |
| 137 | * are all set. If it doesn't, there is nothing else we can do, |
| 138 | * so we might as well abort the loop. |
| 139 | * Note: This assumes that a driver with alert handler handles |
| 140 | * the alert properly and clears it if necessary. |
| 141 | */ |
| 142 | if (data.addr == prev_addr && status != -EBUSY) { |
| 143 | device_for_each_child(&ara->adapter->dev, &data, |
| 144 | smbus_do_alert_force); |
| 145 | break; |
| 146 | } |
| 147 | prev_addr = data.addr; |
| 148 | } |
| 149 | |
| 150 | return IRQ_HANDLED; |
| 151 | } |
| 152 | |
| 153 | static void smbalert_work(struct work_struct *work) |
| 154 | { |
| 155 | struct i2c_smbus_alert *alert; |
| 156 | |
| 157 | alert = container_of(work, struct i2c_smbus_alert, alert); |
| 158 | |
| 159 | smbus_alert(0, alert); |
| 160 | |
| 161 | } |
| 162 | |
| 163 | /* Setup SMBALERT# infrastructure */ |
| 164 | static int smbalert_probe(struct i2c_client *ara, |
| 165 | const struct i2c_device_id *id) |
| 166 | { |
| 167 | struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev); |
| 168 | struct i2c_smbus_alert *alert; |
| 169 | struct i2c_adapter *adapter = ara->adapter; |
| 170 | int res, irq; |
| 171 | |
| 172 | alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert), |
| 173 | GFP_KERNEL); |
| 174 | if (!alert) |
| 175 | return -ENOMEM; |
| 176 | |
| 177 | if (setup) { |
| 178 | irq = setup->irq; |
| 179 | } else { |
| 180 | irq = of_irq_get_byname(adapter->dev.of_node, "smbus_alert"); |
| 181 | if (irq <= 0) |
| 182 | return irq; |
| 183 | } |
| 184 | |
| 185 | INIT_WORK(&alert->alert, smbalert_work); |
| 186 | alert->ara = ara; |
| 187 | |
| 188 | if (irq > 0) { |
| 189 | res = devm_request_threaded_irq(&ara->dev, irq, |
| 190 | NULL, smbus_alert, |
| 191 | IRQF_SHARED | IRQF_ONESHOT, |
| 192 | "smbus_alert", alert); |
| 193 | if (res) |
| 194 | return res; |
| 195 | } |
| 196 | |
| 197 | i2c_set_clientdata(ara, alert); |
| 198 | dev_info(&adapter->dev, "supports SMBALERT#\n"); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* IRQ and memory resources are managed so they are freed automatically */ |
| 204 | static int smbalert_remove(struct i2c_client *ara) |
| 205 | { |
| 206 | struct i2c_smbus_alert *alert = i2c_get_clientdata(ara); |
| 207 | |
| 208 | cancel_work_sync(&alert->alert); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static const struct i2c_device_id smbalert_ids[] = { |
| 213 | { "smbus_alert", 0 }, |
| 214 | { /* LIST END */ } |
| 215 | }; |
| 216 | MODULE_DEVICE_TABLE(i2c, smbalert_ids); |
| 217 | |
| 218 | static struct i2c_driver smbalert_driver = { |
| 219 | .driver = { |
| 220 | .name = "smbus_alert", |
| 221 | }, |
| 222 | .probe = smbalert_probe, |
| 223 | .remove = smbalert_remove, |
| 224 | .id_table = smbalert_ids, |
| 225 | }; |
| 226 | |
| 227 | /** |
| 228 | * i2c_handle_smbus_alert - Handle an SMBus alert |
| 229 | * @ara: the ARA client on the relevant adapter |
| 230 | * Context: can't sleep |
| 231 | * |
| 232 | * Helper function to be called from an I2C bus driver's interrupt |
| 233 | * handler. It will schedule the alert work, in turn calling the |
| 234 | * corresponding I2C device driver's alert function. |
| 235 | * |
| 236 | * It is assumed that ara is a valid i2c client previously returned by |
| 237 | * i2c_setup_smbus_alert(). |
| 238 | */ |
| 239 | int i2c_handle_smbus_alert(struct i2c_client *ara) |
| 240 | { |
| 241 | struct i2c_smbus_alert *alert = i2c_get_clientdata(ara); |
| 242 | |
| 243 | return schedule_work(&alert->alert); |
| 244 | } |
| 245 | EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert); |
| 246 | |
| 247 | module_i2c_driver(smbalert_driver); |
| 248 | |
| 249 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
| 250 | MODULE_DESCRIPTION("SMBus protocol extensions support"); |
| 251 | MODULE_LICENSE("GPL"); |