]>
Commit | Line | Data |
---|---|---|
b5527a77 JD |
1 | /* |
2 | * i2c-smbus.c - SMBus extensions to the I2C protocol | |
3 | * | |
4 | * Copyright (C) 2008 David Brownell | |
7c81c60f | 5 | * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de> |
b5527a77 JD |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
b5527a77 JD |
16 | */ |
17 | ||
b5527a77 | 18 | #include <linux/device.h> |
b5527a77 JD |
19 | #include <linux/i2c.h> |
20 | #include <linux/i2c-smbus.h> | |
62c874ab WS |
21 | #include <linux/interrupt.h> |
22 | #include <linux/kernel.h> | |
23 | #include <linux/module.h> | |
5a0e3ad6 | 24 | #include <linux/slab.h> |
62c874ab | 25 | #include <linux/workqueue.h> |
b5527a77 JD |
26 | |
27 | struct i2c_smbus_alert { | |
b5527a77 JD |
28 | struct work_struct alert; |
29 | struct i2c_client *ara; /* Alert response address */ | |
30 | }; | |
31 | ||
32 | struct alert_data { | |
33 | unsigned short addr; | |
e456cd37 BT |
34 | enum i2c_alert_protocol type; |
35 | unsigned int data; | |
b5527a77 JD |
36 | }; |
37 | ||
38 | /* If this is the alerting device, notify its driver */ | |
39 | static int smbus_do_alert(struct device *dev, void *addrp) | |
40 | { | |
41 | struct i2c_client *client = i2c_verify_client(dev); | |
42 | struct alert_data *data = addrp; | |
0acc2b32 | 43 | struct i2c_driver *driver; |
b5527a77 JD |
44 | |
45 | if (!client || client->addr != data->addr) | |
46 | return 0; | |
47 | if (client->flags & I2C_CLIENT_TEN) | |
48 | return 0; | |
49 | ||
50 | /* | |
51 | * Drivers should either disable alerts, or provide at least | |
0acc2b32 | 52 | * a minimal handler. Lock so the driver won't change. |
b5527a77 | 53 | */ |
f635a1e7 | 54 | device_lock(dev); |
0acc2b32 LPC |
55 | if (client->dev.driver) { |
56 | driver = to_i2c_driver(client->dev.driver); | |
57 | if (driver->alert) | |
e456cd37 | 58 | driver->alert(client, data->type, data->data); |
b5527a77 JD |
59 | else |
60 | dev_warn(&client->dev, "no driver alert()!\n"); | |
61 | } else | |
62 | dev_dbg(&client->dev, "alert with no driver\n"); | |
f635a1e7 | 63 | device_unlock(dev); |
b5527a77 JD |
64 | |
65 | /* Stop iterating after we find the device */ | |
66 | return -EBUSY; | |
67 | } | |
68 | ||
69 | /* | |
70 | * The alert IRQ handler needs to hand work off to a task which can issue | |
71 | * SMBus calls, because those sleeping calls can't be made in IRQ context. | |
72 | */ | |
9b9f2b8b | 73 | static irqreturn_t smbus_alert(int irq, void *d) |
b5527a77 | 74 | { |
9b9f2b8b | 75 | struct i2c_smbus_alert *alert = d; |
b5527a77 JD |
76 | struct i2c_client *ara; |
77 | unsigned short prev_addr = 0; /* Not a valid address */ | |
78 | ||
b5527a77 JD |
79 | ara = alert->ara; |
80 | ||
81 | for (;;) { | |
82 | s32 status; | |
83 | struct alert_data data; | |
84 | ||
85 | /* | |
86 | * Devices with pending alerts reply in address order, low | |
87 | * to high, because of slave transmit arbitration. After | |
88 | * responding, an SMBus device stops asserting SMBALERT#. | |
89 | * | |
edc9102a | 90 | * Note that SMBus 2.0 reserves 10-bit addresses for future |
b5527a77 JD |
91 | * use. We neither handle them, nor try to use PEC here. |
92 | */ | |
93 | status = i2c_smbus_read_byte(ara); | |
94 | if (status < 0) | |
95 | break; | |
96 | ||
e456cd37 | 97 | data.data = status & 1; |
b5527a77 | 98 | data.addr = status >> 1; |
e456cd37 | 99 | data.type = I2C_PROTOCOL_SMBUS_ALERT; |
b5527a77 JD |
100 | |
101 | if (data.addr == prev_addr) { | |
102 | dev_warn(&ara->dev, "Duplicate SMBALERT# from dev " | |
103 | "0x%02x, skipping\n", data.addr); | |
104 | break; | |
105 | } | |
106 | dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n", | |
e456cd37 | 107 | data.addr, data.data); |
b5527a77 JD |
108 | |
109 | /* Notify driver for the device which issued the alert */ | |
110 | device_for_each_child(&ara->adapter->dev, &data, | |
111 | smbus_do_alert); | |
112 | prev_addr = data.addr; | |
113 | } | |
114 | ||
9b9f2b8b | 115 | return IRQ_HANDLED; |
b5527a77 JD |
116 | } |
117 | ||
9b9f2b8b | 118 | static void smbalert_work(struct work_struct *work) |
b5527a77 | 119 | { |
9b9f2b8b PR |
120 | struct i2c_smbus_alert *alert; |
121 | ||
122 | alert = container_of(work, struct i2c_smbus_alert, alert); | |
b5527a77 | 123 | |
9b9f2b8b | 124 | smbus_alert(0, alert); |
b5527a77 | 125 | |
b5527a77 JD |
126 | } |
127 | ||
128 | /* Setup SMBALERT# infrastructure */ | |
129 | static int smbalert_probe(struct i2c_client *ara, | |
130 | const struct i2c_device_id *id) | |
131 | { | |
6d4028c6 | 132 | struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev); |
b5527a77 JD |
133 | struct i2c_smbus_alert *alert; |
134 | struct i2c_adapter *adapter = ara->adapter; | |
9b9f2b8b | 135 | int res, irq; |
b5527a77 | 136 | |
71b57845 JL |
137 | alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert), |
138 | GFP_KERNEL); | |
b5527a77 JD |
139 | if (!alert) |
140 | return -ENOMEM; | |
141 | ||
9b9f2b8b PR |
142 | irq = setup->irq; |
143 | INIT_WORK(&alert->alert, smbalert_work); | |
b5527a77 JD |
144 | alert->ara = ara; |
145 | ||
9b9f2b8b PR |
146 | if (irq > 0) { |
147 | res = devm_request_threaded_irq(&ara->dev, irq, | |
148 | NULL, smbus_alert, | |
149 | IRQF_SHARED | IRQF_ONESHOT, | |
150 | "smbus_alert", alert); | |
71b57845 | 151 | if (res) |
b5527a77 | 152 | return res; |
b5527a77 JD |
153 | } |
154 | ||
155 | i2c_set_clientdata(ara, alert); | |
9b9f2b8b | 156 | dev_info(&adapter->dev, "supports SMBALERT#\n"); |
b5527a77 JD |
157 | |
158 | return 0; | |
159 | } | |
160 | ||
71b57845 | 161 | /* IRQ and memory resources are managed so they are freed automatically */ |
b5527a77 JD |
162 | static int smbalert_remove(struct i2c_client *ara) |
163 | { | |
164 | struct i2c_smbus_alert *alert = i2c_get_clientdata(ara); | |
165 | ||
166 | cancel_work_sync(&alert->alert); | |
b5527a77 JD |
167 | return 0; |
168 | } | |
169 | ||
170 | static const struct i2c_device_id smbalert_ids[] = { | |
171 | { "smbus_alert", 0 }, | |
172 | { /* LIST END */ } | |
173 | }; | |
174 | MODULE_DEVICE_TABLE(i2c, smbalert_ids); | |
175 | ||
176 | static struct i2c_driver smbalert_driver = { | |
177 | .driver = { | |
178 | .name = "smbus_alert", | |
179 | }, | |
180 | .probe = smbalert_probe, | |
181 | .remove = smbalert_remove, | |
182 | .id_table = smbalert_ids, | |
183 | }; | |
184 | ||
b5527a77 JD |
185 | /** |
186 | * i2c_handle_smbus_alert - Handle an SMBus alert | |
187 | * @ara: the ARA client on the relevant adapter | |
188 | * Context: can't sleep | |
189 | * | |
190 | * Helper function to be called from an I2C bus driver's interrupt | |
191 | * handler. It will schedule the alert work, in turn calling the | |
192 | * corresponding I2C device driver's alert function. | |
193 | * | |
194 | * It is assumed that ara is a valid i2c client previously returned by | |
195 | * i2c_setup_smbus_alert(). | |
196 | */ | |
197 | int i2c_handle_smbus_alert(struct i2c_client *ara) | |
198 | { | |
199 | struct i2c_smbus_alert *alert = i2c_get_clientdata(ara); | |
200 | ||
201 | return schedule_work(&alert->alert); | |
202 | } | |
203 | EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert); | |
204 | ||
fda2f4af | 205 | module_i2c_driver(smbalert_driver); |
b5527a77 | 206 | |
7c81c60f | 207 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
b5527a77 JD |
208 | MODULE_DESCRIPTION("SMBus protocol extensions support"); |
209 | MODULE_LICENSE("GPL"); |