blob: 3b6765a4ebe9a5a03ecc6bd3847bb1d575262bb4 [file] [log] [blame]
Jean Delvareb5527a72010-03-02 12:23:42 +01001/*
2 * i2c-smbus.c - SMBus extensions to the I2C protocol
3 *
4 * Copyright (C) 2008 David Brownell
Jean Delvare7c81c602014-01-29 20:40:08 +01005 * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
Jean Delvareb5527a72010-03-02 12:23:42 +01006 *
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.
Jean Delvareb5527a72010-03-02 12:23:42 +010016 */
17
Jean Delvareb5527a72010-03-02 12:23:42 +010018#include <linux/device.h>
Jean Delvareb5527a72010-03-02 12:23:42 +010019#include <linux/i2c.h>
20#include <linux/i2c-smbus.h>
Wolfram Sang62c874a2016-02-20 23:33:39 +010021#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Wolfram Sang62c874a2016-02-20 23:33:39 +010025#include <linux/workqueue.h>
Jean Delvareb5527a72010-03-02 12:23:42 +010026
27struct i2c_smbus_alert {
28 unsigned int alert_edge_triggered:1;
29 int irq;
30 struct work_struct alert;
31 struct i2c_client *ara; /* Alert response address */
32};
33
34struct alert_data {
35 unsigned short addr;
36 u8 flag:1;
37};
38
39/* If this is the alerting device, notify its driver */
40static int smbus_do_alert(struct device *dev, void *addrp)
41{
42 struct i2c_client *client = i2c_verify_client(dev);
43 struct alert_data *data = addrp;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +020044 struct i2c_driver *driver;
Jean Delvareb5527a72010-03-02 12:23:42 +010045
46 if (!client || client->addr != data->addr)
47 return 0;
48 if (client->flags & I2C_CLIENT_TEN)
49 return 0;
50
51 /*
52 * Drivers should either disable alerts, or provide at least
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +020053 * a minimal handler. Lock so the driver won't change.
Jean Delvareb5527a72010-03-02 12:23:42 +010054 */
Stephen Rothwellf635a1e2010-03-01 16:04:45 +110055 device_lock(dev);
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +020056 if (client->dev.driver) {
57 driver = to_i2c_driver(client->dev.driver);
58 if (driver->alert)
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +020059 driver->alert(client, I2C_PROTOCOL_SMBUS_ALERT,
60 data->flag);
Jean Delvareb5527a72010-03-02 12:23:42 +010061 else
62 dev_warn(&client->dev, "no driver alert()!\n");
63 } else
64 dev_dbg(&client->dev, "alert with no driver\n");
Stephen Rothwellf635a1e2010-03-01 16:04:45 +110065 device_unlock(dev);
Jean Delvareb5527a72010-03-02 12:23:42 +010066
67 /* Stop iterating after we find the device */
68 return -EBUSY;
69}
70
71/*
72 * The alert IRQ handler needs to hand work off to a task which can issue
73 * SMBus calls, because those sleeping calls can't be made in IRQ context.
74 */
75static void smbus_alert(struct work_struct *work)
76{
77 struct i2c_smbus_alert *alert;
78 struct i2c_client *ara;
79 unsigned short prev_addr = 0; /* Not a valid address */
80
81 alert = container_of(work, struct i2c_smbus_alert, alert);
82 ara = alert->ara;
83
84 for (;;) {
85 s32 status;
86 struct alert_data data;
87
88 /*
89 * Devices with pending alerts reply in address order, low
90 * to high, because of slave transmit arbitration. After
91 * responding, an SMBus device stops asserting SMBALERT#.
92 *
Shailendra Vermaedc91022015-05-19 20:09:41 +053093 * Note that SMBus 2.0 reserves 10-bit addresses for future
Jean Delvareb5527a72010-03-02 12:23:42 +010094 * use. We neither handle them, nor try to use PEC here.
95 */
96 status = i2c_smbus_read_byte(ara);
97 if (status < 0)
98 break;
99
100 data.flag = status & 1;
101 data.addr = status >> 1;
102
103 if (data.addr == prev_addr) {
104 dev_warn(&ara->dev, "Duplicate SMBALERT# from dev "
105 "0x%02x, skipping\n", data.addr);
106 break;
107 }
108 dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
109 data.addr, data.flag);
110
111 /* Notify driver for the device which issued the alert */
112 device_for_each_child(&ara->adapter->dev, &data,
113 smbus_do_alert);
114 prev_addr = data.addr;
115 }
116
117 /* We handled all alerts; re-enable level-triggered IRQs */
118 if (!alert->alert_edge_triggered)
119 enable_irq(alert->irq);
120}
121
122static irqreturn_t smbalert_irq(int irq, void *d)
123{
124 struct i2c_smbus_alert *alert = d;
125
126 /* Disable level-triggered IRQs until we handle them */
127 if (!alert->alert_edge_triggered)
128 disable_irq_nosync(irq);
129
130 schedule_work(&alert->alert);
131 return IRQ_HANDLED;
132}
133
134/* Setup SMBALERT# infrastructure */
135static int smbalert_probe(struct i2c_client *ara,
136 const struct i2c_device_id *id)
137{
Jingoo Han6d4028c2013-07-30 16:59:33 +0900138 struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
Jean Delvareb5527a72010-03-02 12:23:42 +0100139 struct i2c_smbus_alert *alert;
140 struct i2c_adapter *adapter = ara->adapter;
141 int res;
142
Julia Lawall71b57842012-10-05 22:23:52 +0200143 alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
144 GFP_KERNEL);
Jean Delvareb5527a72010-03-02 12:23:42 +0100145 if (!alert)
146 return -ENOMEM;
147
148 alert->alert_edge_triggered = setup->alert_edge_triggered;
149 alert->irq = setup->irq;
150 INIT_WORK(&alert->alert, smbus_alert);
151 alert->ara = ara;
152
153 if (setup->irq > 0) {
154 res = devm_request_irq(&ara->dev, setup->irq, smbalert_irq,
155 0, "smbus_alert", alert);
Julia Lawall71b57842012-10-05 22:23:52 +0200156 if (res)
Jean Delvareb5527a72010-03-02 12:23:42 +0100157 return res;
Jean Delvareb5527a72010-03-02 12:23:42 +0100158 }
159
160 i2c_set_clientdata(ara, alert);
161 dev_info(&adapter->dev, "supports SMBALERT#, %s trigger\n",
162 setup->alert_edge_triggered ? "edge" : "level");
163
164 return 0;
165}
166
Julia Lawall71b57842012-10-05 22:23:52 +0200167/* IRQ and memory resources are managed so they are freed automatically */
Jean Delvareb5527a72010-03-02 12:23:42 +0100168static int smbalert_remove(struct i2c_client *ara)
169{
170 struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
171
172 cancel_work_sync(&alert->alert);
Jean Delvareb5527a72010-03-02 12:23:42 +0100173 return 0;
174}
175
176static const struct i2c_device_id smbalert_ids[] = {
177 { "smbus_alert", 0 },
178 { /* LIST END */ }
179};
180MODULE_DEVICE_TABLE(i2c, smbalert_ids);
181
182static struct i2c_driver smbalert_driver = {
183 .driver = {
184 .name = "smbus_alert",
185 },
186 .probe = smbalert_probe,
187 .remove = smbalert_remove,
188 .id_table = smbalert_ids,
189};
190
191/**
192 * i2c_setup_smbus_alert - Setup SMBus alert support
193 * @adapter: the target adapter
194 * @setup: setup data for the SMBus alert handler
195 * Context: can sleep
196 *
197 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
198 *
199 * Handling can be done either through our IRQ handler, or by the
200 * adapter (from its handler, periodic polling, or whatever).
201 *
202 * NOTE that if we manage the IRQ, we *MUST* know if it's level or
203 * edge triggered in order to hand it to the workqueue correctly.
204 * If triggering the alert seems to wedge the system, you probably
205 * should have said it's level triggered.
206 *
207 * This returns the ara client, which should be saved for later use with
208 * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
209 * to indicate an error.
210 */
211struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,
212 struct i2c_smbus_alert_setup *setup)
213{
214 struct i2c_board_info ara_board_info = {
215 I2C_BOARD_INFO("smbus_alert", 0x0c),
216 .platform_data = setup,
217 };
218
219 return i2c_new_device(adapter, &ara_board_info);
220}
221EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);
222
223/**
224 * i2c_handle_smbus_alert - Handle an SMBus alert
225 * @ara: the ARA client on the relevant adapter
226 * Context: can't sleep
227 *
228 * Helper function to be called from an I2C bus driver's interrupt
229 * handler. It will schedule the alert work, in turn calling the
230 * corresponding I2C device driver's alert function.
231 *
232 * It is assumed that ara is a valid i2c client previously returned by
233 * i2c_setup_smbus_alert().
234 */
235int i2c_handle_smbus_alert(struct i2c_client *ara)
236{
237 struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
238
239 return schedule_work(&alert->alert);
240}
241EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
242
Fabio Estevamfda2f4a2012-07-24 14:13:57 +0200243module_i2c_driver(smbalert_driver);
Jean Delvareb5527a72010-03-02 12:23:42 +0100244
Jean Delvare7c81c602014-01-29 20:40:08 +0100245MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Jean Delvareb5527a72010-03-02 12:23:42 +0100246MODULE_DESCRIPTION("SMBus protocol extensions support");
247MODULE_LICENSE("GPL");