^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * i2c-smbus.c - SMBus extensions to the I2C protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008 David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2010-2019 Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c-smbus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct i2c_smbus_alert {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct work_struct alert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct i2c_client *ara; /* Alert response address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct alert_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned short addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) enum i2c_alert_protocol type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* If this is the alerting device, notify its driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int smbus_do_alert(struct device *dev, void *addrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct i2c_client *client = i2c_verify_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct alert_data *data = addrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct i2c_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!client || client->addr != data->addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (client->flags & I2C_CLIENT_TEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Drivers should either disable alerts, or provide at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * a minimal handler. Lock so the driver won't change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (client->dev.driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) driver = to_i2c_driver(client->dev.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (driver->alert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) driver->alert(client, data->type, data->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) dev_warn(&client->dev, "no driver alert()!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) dev_dbg(&client->dev, "alert with no driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Stop iterating after we find the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * The alert IRQ handler needs to hand work off to a task which can issue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * SMBus calls, because those sleeping calls can't be made in IRQ context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static irqreturn_t smbus_alert(int irq, void *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct i2c_smbus_alert *alert = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct i2c_client *ara;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ara = alert->ara;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) s32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct alert_data data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * Devices with pending alerts reply in address order, low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * to high, because of slave transmit arbitration. After
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * responding, an SMBus device stops asserting SMBALERT#.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Note that SMBus 2.0 reserves 10-bit addresses for future
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * use. We neither handle them, nor try to use PEC here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) status = i2c_smbus_read_byte(ara);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) data.data = status & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) data.addr = status >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) data.type = I2C_PROTOCOL_SMBUS_ALERT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) data.addr, data.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Notify driver for the device which issued the alert */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) device_for_each_child(&ara->adapter->dev, &data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) smbus_do_alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void smbalert_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct i2c_smbus_alert *alert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) alert = container_of(work, struct i2c_smbus_alert, alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) smbus_alert(0, alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Setup SMBALERT# infrastructure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int smbalert_probe(struct i2c_client *ara,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct i2c_smbus_alert *alert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct i2c_adapter *adapter = ara->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int res, irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!alert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (setup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) irq = setup->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) irq = of_irq_get_byname(adapter->dev.of_node, "smbus_alert");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) INIT_WORK(&alert->alert, smbalert_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) alert->ara = ara;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) res = devm_request_threaded_irq(&ara->dev, irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) NULL, smbus_alert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) IRQF_SHARED | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) "smbus_alert", alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) i2c_set_clientdata(ara, alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dev_info(&adapter->dev, "supports SMBALERT#\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* IRQ and memory resources are managed so they are freed automatically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int smbalert_remove(struct i2c_client *ara)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) cancel_work_sync(&alert->alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static const struct i2c_device_id smbalert_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) { "smbus_alert", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) { /* LIST END */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODULE_DEVICE_TABLE(i2c, smbalert_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static struct i2c_driver smbalert_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .name = "smbus_alert",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .probe = smbalert_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .remove = smbalert_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .id_table = smbalert_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * i2c_handle_smbus_alert - Handle an SMBus alert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @ara: the ARA client on the relevant adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * Context: can't sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * Helper function to be called from an I2C bus driver's interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * handler. It will schedule the alert work, in turn calling the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * corresponding I2C device driver's alert function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * It is assumed that ara is a valid i2c client previously returned by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * i2c_new_smbus_alert_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int i2c_handle_smbus_alert(struct i2c_client *ara)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return schedule_work(&alert->alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) module_i2c_driver(smbalert_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #if IS_ENABLED(CONFIG_I2C_SLAVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #define SMBUS_HOST_NOTIFY_LEN 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct i2c_slave_host_notify_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) u8 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) u8 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int i2c_slave_host_notify_cb(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) enum i2c_slave_event event, u8 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct i2c_slave_host_notify_status *status = client->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) case I2C_SLAVE_WRITE_RECEIVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* We only retrieve the first byte received (addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * since there is currently no support to retrieve the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * parameter from the client.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (status->index == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) status->addr = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (status->index < U8_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) status->index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) case I2C_SLAVE_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (status->index == SMBUS_HOST_NOTIFY_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) i2c_handle_smbus_host_notify(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) status->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) case I2C_SLAVE_WRITE_REQUESTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) status->index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case I2C_SLAVE_READ_REQUESTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) case I2C_SLAVE_READ_PROCESSED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *val = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * i2c_new_slave_host_notify_device - get a client for SMBus host-notify support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * @adapter: the target adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * Context: can sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * Setup handling of the SMBus host-notify protocol on a given I2C bus segment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Handling is done by creating a device and its callback and handling data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * received via the SMBus host-notify address (0x8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * This returns the client, which should be ultimately freed using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * i2c_free_slave_host_notify_device(); or an ERRPTR to indicate an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct i2c_client *i2c_new_slave_host_notify_device(struct i2c_adapter *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct i2c_board_info host_notify_board_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) I2C_BOARD_INFO("smbus_host_notify", 0x08),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) .flags = I2C_CLIENT_SLAVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct i2c_slave_host_notify_status *status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) status = kzalloc(sizeof(struct i2c_slave_host_notify_status),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) host_notify_board_info.platform_data = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) client = i2c_new_client_device(adapter, &host_notify_board_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (IS_ERR(client)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kfree(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ret = i2c_slave_register(client, i2c_slave_host_notify_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) i2c_unregister_device(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) kfree(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) EXPORT_SYMBOL_GPL(i2c_new_slave_host_notify_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * i2c_free_slave_host_notify_device - free the client for SMBus host-notify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * @client: the client to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Context: can sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * Free the i2c_client allocated via i2c_new_slave_host_notify_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) void i2c_free_slave_host_notify_device(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (IS_ERR_OR_NULL(client))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) i2c_slave_unregister(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) kfree(client->dev.platform_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) i2c_unregister_device(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) EXPORT_SYMBOL_GPL(i2c_free_slave_host_notify_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * SPD is not part of SMBus but we include it here for convenience as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * target systems are the same.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Restrictions to automatic SPD instantiation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * - Only works if all filled slots have the same memory type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * - Only works for DDR2, DDR3 and DDR4 for now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * - Only works on systems with 1 to 4 memory slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #if IS_ENABLED(CONFIG_DMI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) void i2c_register_spd(struct i2c_adapter *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int n, slot_count = 0, dimm_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) u16 handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) u8 common_mem_type = 0x0, mem_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) u64 mem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) while ((handle = dmi_memdev_handle(slot_count)) != 0xffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) slot_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /* Skip empty slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) mem_size = dmi_memdev_size(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (!mem_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* Skip undefined memory type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) mem_type = dmi_memdev_type(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (mem_type <= 0x02) /* Invalid, Other, Unknown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (!common_mem_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /* First filled slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) common_mem_type = mem_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /* Check that all filled slots have the same type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (mem_type != common_mem_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) dev_warn(&adap->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) "Different memory types mixed, not instantiating SPD\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) dimm_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* No useful DMI data, bail out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!dimm_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dev_info(&adap->dev, "%d/%d memory slots populated (from DMI)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) dimm_count, slot_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (slot_count > 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) dev_warn(&adap->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) "Systems with more than 4 memory slots not supported yet, not instantiating SPD\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) switch (common_mem_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) case 0x13: /* DDR2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) case 0x18: /* DDR3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) case 0x1C: /* LPDDR2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) case 0x1D: /* LPDDR3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) name = "spd";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) case 0x1A: /* DDR4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) case 0x1E: /* LPDDR4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) name = "ee1004";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) dev_info(&adap->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) "Memory type 0x%02x not supported yet, not instantiating SPD\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) common_mem_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * We don't know in which slots the memory modules are. We could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * try to guess from the slot names, but that would be rather complex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * and unreliable, so better probe all possible addresses until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * have found all memory modules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) for (n = 0; n < slot_count && dimm_count; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) struct i2c_board_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) unsigned short addr_list[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) memset(&info, 0, sizeof(struct i2c_board_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) strlcpy(info.type, name, I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) addr_list[0] = 0x50 + n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) addr_list[1] = I2C_CLIENT_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (!IS_ERR(i2c_new_scanned_device(adap, &info, addr_list, NULL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) dev_info(&adap->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) "Successfully instantiated SPD at 0x%hx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) addr_list[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) dimm_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) EXPORT_SYMBOL_GPL(i2c_register_spd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) MODULE_DESCRIPTION("SMBus protocol extensions support");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) MODULE_LICENSE("GPL");