/*1* i2c-smbus.c - SMBus extensions to the I2C protocol2*3* Copyright (C) 2008 David Brownell4* Copyright (C) 2010 Jean Delvare <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.19*/2021#include <linux/kernel.h>22#include <linux/module.h>23#include <linux/device.h>24#include <linux/interrupt.h>25#include <linux/workqueue.h>26#include <linux/i2c.h>27#include <linux/i2c-smbus.h>28#include <linux/slab.h>2930struct i2c_smbus_alert {31unsigned int alert_edge_triggered:1;32int irq;33struct work_struct alert;34struct i2c_client *ara; /* Alert response address */35};3637struct alert_data {38unsigned short addr;39u8 flag:1;40};4142/* If this is the alerting device, notify its driver */43static int smbus_do_alert(struct device *dev, void *addrp)44{45struct i2c_client *client = i2c_verify_client(dev);46struct alert_data *data = addrp;4748if (!client || client->addr != data->addr)49return 0;50if (client->flags & I2C_CLIENT_TEN)51return 0;5253/*54* Drivers should either disable alerts, or provide at least55* a minimal handler. Lock so client->driver won't change.56*/57device_lock(dev);58if (client->driver) {59if (client->driver->alert)60client->driver->alert(client, data->flag);61else62dev_warn(&client->dev, "no driver alert()!\n");63} else64dev_dbg(&client->dev, "alert with no driver\n");65device_unlock(dev);6667/* Stop iterating after we find the device */68return -EBUSY;69}7071/*72* The alert IRQ handler needs to hand work off to a task which can issue73* SMBus calls, because those sleeping calls can't be made in IRQ context.74*/75static void smbus_alert(struct work_struct *work)76{77struct i2c_smbus_alert *alert;78struct i2c_client *ara;79unsigned short prev_addr = 0; /* Not a valid address */8081alert = container_of(work, struct i2c_smbus_alert, alert);82ara = alert->ara;8384for (;;) {85s32 status;86struct alert_data data;8788/*89* Devices with pending alerts reply in address order, low90* to high, because of slave transmit arbitration. After91* responding, an SMBus device stops asserting SMBALERT#.92*93* Note that SMBus 2.0 reserves 10-bit addresess for future94* use. We neither handle them, nor try to use PEC here.95*/96status = i2c_smbus_read_byte(ara);97if (status < 0)98break;99100data.flag = status & 1;101data.addr = status >> 1;102103if (data.addr == prev_addr) {104dev_warn(&ara->dev, "Duplicate SMBALERT# from dev "105"0x%02x, skipping\n", data.addr);106break;107}108dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",109data.addr, data.flag);110111/* Notify driver for the device which issued the alert */112device_for_each_child(&ara->adapter->dev, &data,113smbus_do_alert);114prev_addr = data.addr;115}116117/* We handled all alerts; re-enable level-triggered IRQs */118if (!alert->alert_edge_triggered)119enable_irq(alert->irq);120}121122static irqreturn_t smbalert_irq(int irq, void *d)123{124struct i2c_smbus_alert *alert = d;125126/* Disable level-triggered IRQs until we handle them */127if (!alert->alert_edge_triggered)128disable_irq_nosync(irq);129130schedule_work(&alert->alert);131return IRQ_HANDLED;132}133134/* Setup SMBALERT# infrastructure */135static int smbalert_probe(struct i2c_client *ara,136const struct i2c_device_id *id)137{138struct i2c_smbus_alert_setup *setup = ara->dev.platform_data;139struct i2c_smbus_alert *alert;140struct i2c_adapter *adapter = ara->adapter;141int res;142143alert = kzalloc(sizeof(struct i2c_smbus_alert), GFP_KERNEL);144if (!alert)145return -ENOMEM;146147alert->alert_edge_triggered = setup->alert_edge_triggered;148alert->irq = setup->irq;149INIT_WORK(&alert->alert, smbus_alert);150alert->ara = ara;151152if (setup->irq > 0) {153res = devm_request_irq(&ara->dev, setup->irq, smbalert_irq,1540, "smbus_alert", alert);155if (res) {156kfree(alert);157return res;158}159}160161i2c_set_clientdata(ara, alert);162dev_info(&adapter->dev, "supports SMBALERT#, %s trigger\n",163setup->alert_edge_triggered ? "edge" : "level");164165return 0;166}167168/* IRQ resource is managed so it is freed automatically */169static int smbalert_remove(struct i2c_client *ara)170{171struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);172173cancel_work_sync(&alert->alert);174175kfree(alert);176return 0;177}178179static const struct i2c_device_id smbalert_ids[] = {180{ "smbus_alert", 0 },181{ /* LIST END */ }182};183MODULE_DEVICE_TABLE(i2c, smbalert_ids);184185static struct i2c_driver smbalert_driver = {186.driver = {187.name = "smbus_alert",188},189.probe = smbalert_probe,190.remove = smbalert_remove,191.id_table = smbalert_ids,192};193194/**195* i2c_setup_smbus_alert - Setup SMBus alert support196* @adapter: the target adapter197* @setup: setup data for the SMBus alert handler198* Context: can sleep199*200* Setup handling of the SMBus alert protocol on a given I2C bus segment.201*202* Handling can be done either through our IRQ handler, or by the203* adapter (from its handler, periodic polling, or whatever).204*205* NOTE that if we manage the IRQ, we *MUST* know if it's level or206* edge triggered in order to hand it to the workqueue correctly.207* If triggering the alert seems to wedge the system, you probably208* should have said it's level triggered.209*210* This returns the ara client, which should be saved for later use with211* i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL212* to indicate an error.213*/214struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,215struct i2c_smbus_alert_setup *setup)216{217struct i2c_board_info ara_board_info = {218I2C_BOARD_INFO("smbus_alert", 0x0c),219.platform_data = setup,220};221222return i2c_new_device(adapter, &ara_board_info);223}224EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);225226/**227* i2c_handle_smbus_alert - Handle an SMBus alert228* @ara: the ARA client on the relevant adapter229* Context: can't sleep230*231* Helper function to be called from an I2C bus driver's interrupt232* handler. It will schedule the alert work, in turn calling the233* corresponding I2C device driver's alert function.234*235* It is assumed that ara is a valid i2c client previously returned by236* i2c_setup_smbus_alert().237*/238int i2c_handle_smbus_alert(struct i2c_client *ara)239{240struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);241242return schedule_work(&alert->alert);243}244EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);245246static int __init i2c_smbus_init(void)247{248return i2c_add_driver(&smbalert_driver);249}250251static void __exit i2c_smbus_exit(void)252{253i2c_del_driver(&smbalert_driver);254}255256module_init(i2c_smbus_init);257module_exit(i2c_smbus_exit);258259MODULE_AUTHOR("Jean Delvare <[email protected]>");260MODULE_DESCRIPTION("SMBus protocol extensions support");261MODULE_LICENSE("GPL");262263264