i2c-smbus.c 5.51 KB
Newer Older
Jean Delvare's avatar
Jean Delvare committed
1 2 3 4
/*
 * i2c-smbus.c - SMBus extensions to the I2C protocol
 *
 * Copyright (C) 2008 David Brownell
5
 * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare's avatar
Jean Delvare committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/i2c-smbus.h>
Wolfram Sang's avatar
Wolfram Sang committed
21 22 23
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
24
#include <linux/of_irq.h>
25
#include <linux/slab.h>
Wolfram Sang's avatar
Wolfram Sang committed
26
#include <linux/workqueue.h>
Jean Delvare's avatar
Jean Delvare committed
27 28 29 30 31 32 33 34

struct i2c_smbus_alert {
	struct work_struct	alert;
	struct i2c_client	*ara;		/* Alert response address */
};

struct alert_data {
	unsigned short		addr;
35 36
	enum i2c_alert_protocol	type;
	unsigned int		data;
Jean Delvare's avatar
Jean Delvare committed
37 38 39 40 41 42 43
};

/* If this is the alerting device, notify its driver */
static int smbus_do_alert(struct device *dev, void *addrp)
{
	struct i2c_client *client = i2c_verify_client(dev);
	struct alert_data *data = addrp;
44
	struct i2c_driver *driver;
Jean Delvare's avatar
Jean Delvare committed
45 46 47 48 49 50 51 52

	if (!client || client->addr != data->addr)
		return 0;
	if (client->flags & I2C_CLIENT_TEN)
		return 0;

	/*
	 * Drivers should either disable alerts, or provide at least
53
	 * a minimal handler.  Lock so the driver won't change.
Jean Delvare's avatar
Jean Delvare committed
54
	 */
55
	device_lock(dev);
56 57 58
	if (client->dev.driver) {
		driver = to_i2c_driver(client->dev.driver);
		if (driver->alert)
59
			driver->alert(client, data->type, data->data);
Jean Delvare's avatar
Jean Delvare committed
60 61 62 63
		else
			dev_warn(&client->dev, "no driver alert()!\n");
	} else
		dev_dbg(&client->dev, "alert with no driver\n");
64
	device_unlock(dev);
Jean Delvare's avatar
Jean Delvare committed
65 66 67 68 69 70 71 72 73

	/* Stop iterating after we find the device */
	return -EBUSY;
}

/*
 * The alert IRQ handler needs to hand work off to a task which can issue
 * SMBus calls, because those sleeping calls can't be made in IRQ context.
 */
74
static irqreturn_t smbus_alert(int irq, void *d)
Jean Delvare's avatar
Jean Delvare committed
75
{
76
	struct i2c_smbus_alert *alert = d;
Jean Delvare's avatar
Jean Delvare committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90
	struct i2c_client *ara;
	unsigned short prev_addr = 0;	/* Not a valid address */

	ara = alert->ara;

	for (;;) {
		s32 status;
		struct alert_data data;

		/*
		 * Devices with pending alerts reply in address order, low
		 * to high, because of slave transmit arbitration.  After
		 * responding, an SMBus device stops asserting SMBALERT#.
		 *
91
		 * Note that SMBus 2.0 reserves 10-bit addresses for future
Jean Delvare's avatar
Jean Delvare committed
92 93 94 95 96 97
		 * use.  We neither handle them, nor try to use PEC here.
		 */
		status = i2c_smbus_read_byte(ara);
		if (status < 0)
			break;

98
		data.data = status & 1;
Jean Delvare's avatar
Jean Delvare committed
99
		data.addr = status >> 1;
100
		data.type = I2C_PROTOCOL_SMBUS_ALERT;
Jean Delvare's avatar
Jean Delvare committed
101 102 103 104 105 106 107

		if (data.addr == prev_addr) {
			dev_warn(&ara->dev, "Duplicate SMBALERT# from dev "
				"0x%02x, skipping\n", data.addr);
			break;
		}
		dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
108
			data.addr, data.data);
Jean Delvare's avatar
Jean Delvare committed
109 110 111 112 113 114 115

		/* Notify driver for the device which issued the alert */
		device_for_each_child(&ara->adapter->dev, &data,
				      smbus_do_alert);
		prev_addr = data.addr;
	}

116
	return IRQ_HANDLED;
Jean Delvare's avatar
Jean Delvare committed
117 118
}

119
static void smbalert_work(struct work_struct *work)
Jean Delvare's avatar
Jean Delvare committed
120
{
121 122 123
	struct i2c_smbus_alert *alert;

	alert = container_of(work, struct i2c_smbus_alert, alert);
Jean Delvare's avatar
Jean Delvare committed
124

125
	smbus_alert(0, alert);
Jean Delvare's avatar
Jean Delvare committed
126 127 128 129 130 131 132

}

/* Setup SMBALERT# infrastructure */
static int smbalert_probe(struct i2c_client *ara,
			  const struct i2c_device_id *id)
{
Jingoo Han's avatar
Jingoo Han committed
133
	struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
Jean Delvare's avatar
Jean Delvare committed
134 135
	struct i2c_smbus_alert *alert;
	struct i2c_adapter *adapter = ara->adapter;
136
	int res, irq;
Jean Delvare's avatar
Jean Delvare committed
137

138 139
	alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
			     GFP_KERNEL);
Jean Delvare's avatar
Jean Delvare committed
140 141 142
	if (!alert)
		return -ENOMEM;

143 144 145 146 147 148 149 150
	if (setup) {
		irq = setup->irq;
	} else {
		irq = of_irq_get_byname(adapter->dev.of_node, "smbus_alert");
		if (irq <= 0)
			return irq;
	}

151
	INIT_WORK(&alert->alert, smbalert_work);
Jean Delvare's avatar
Jean Delvare committed
152 153
	alert->ara = ara;

154 155 156 157 158
	if (irq > 0) {
		res = devm_request_threaded_irq(&ara->dev, irq,
						NULL, smbus_alert,
						IRQF_SHARED | IRQF_ONESHOT,
						"smbus_alert", alert);
159
		if (res)
Jean Delvare's avatar
Jean Delvare committed
160 161 162 163
			return res;
	}

	i2c_set_clientdata(ara, alert);
164
	dev_info(&adapter->dev, "supports SMBALERT#\n");
Jean Delvare's avatar
Jean Delvare committed
165 166 167 168

	return 0;
}

169
/* IRQ and memory resources are managed so they are freed automatically */
Jean Delvare's avatar
Jean Delvare committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static int smbalert_remove(struct i2c_client *ara)
{
	struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);

	cancel_work_sync(&alert->alert);
	return 0;
}

static const struct i2c_device_id smbalert_ids[] = {
	{ "smbus_alert", 0 },
	{ /* LIST END */ }
};
MODULE_DEVICE_TABLE(i2c, smbalert_ids);

static struct i2c_driver smbalert_driver = {
	.driver = {
		.name	= "smbus_alert",
	},
	.probe		= smbalert_probe,
	.remove		= smbalert_remove,
	.id_table	= smbalert_ids,
};

/**
 * i2c_handle_smbus_alert - Handle an SMBus alert
 * @ara: the ARA client on the relevant adapter
 * Context: can't sleep
 *
 * Helper function to be called from an I2C bus driver's interrupt
 * handler. It will schedule the alert work, in turn calling the
 * corresponding I2C device driver's alert function.
 *
 * It is assumed that ara is a valid i2c client previously returned by
 * i2c_setup_smbus_alert().
 */
int i2c_handle_smbus_alert(struct i2c_client *ara)
{
	struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);

	return schedule_work(&alert->alert);
}
EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);

213
module_i2c_driver(smbalert_driver);
Jean Delvare's avatar
Jean Delvare committed
214

215
MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Jean Delvare's avatar
Jean Delvare committed
216 217
MODULE_DESCRIPTION("SMBus protocol extensions support");
MODULE_LICENSE("GPL");