Path: blob/master/drivers/i2c/busses/i2c-elektor.c
15111 views
/* ------------------------------------------------------------------------- */1/* i2c-elektor.c i2c-hw access for PCF8584 style isa bus adaptes */2/* ------------------------------------------------------------------------- */3/* Copyright (C) 1995-97 Simon G. Vogl41998-99 Hans Berglund56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation; either version 2 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program; if not, write to the Free Software18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */19/* ------------------------------------------------------------------------- */2021/* With some changes from Kyösti Mälkki <[email protected]> and even22Frodo Looijaard <[email protected]> */2324/* Partially rewriten by Oleg I. Vdovikin for mmapped support of25for Alpha Processor Inc. UP-2000(+) boards */2627#include <linux/kernel.h>28#include <linux/ioport.h>29#include <linux/module.h>30#include <linux/delay.h>31#include <linux/init.h>32#include <linux/interrupt.h>33#include <linux/pci.h>34#include <linux/wait.h>3536#include <linux/isa.h>37#include <linux/i2c.h>38#include <linux/i2c-algo-pcf.h>39#include <linux/io.h>4041#include <asm/irq.h>4243#include "../algos/i2c-algo-pcf.h"4445#define DEFAULT_BASE 0x3304647static int base;48static u8 __iomem *base_iomem;4950static int irq;51static int clock = 0x1c;52static int own = 0x55;53static int mmapped;5455/* vdovikin: removed static struct i2c_pcf_isa gpi; code -56this module in real supports only one device, due to missing arguments57in some functions, called from the algo-pcf module. Sometimes it's58need to be rewriten - but for now just remove this for simpler reading */5960static wait_queue_head_t pcf_wait;61static int pcf_pending;62static spinlock_t lock;6364static struct i2c_adapter pcf_isa_ops;6566/* ----- local functions ---------------------------------------------- */6768static void pcf_isa_setbyte(void *data, int ctl, int val)69{70u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;7172/* enable irq if any specified for serial operation */73if (ctl && irq && (val & I2C_PCF_ESO)) {74val |= I2C_PCF_ENI;75}7677pr_debug("%s: Write %p 0x%02X\n", pcf_isa_ops.name, address, val);78iowrite8(val, address);79#ifdef __alpha__80/* API UP2000 needs some hardware fudging to make the write stick */81iowrite8(val, address);82#endif83}8485static int pcf_isa_getbyte(void *data, int ctl)86{87u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;88int val = ioread8(address);8990pr_debug("%s: Read %p 0x%02X\n", pcf_isa_ops.name, address, val);91return (val);92}9394static int pcf_isa_getown(void *data)95{96return (own);97}9899100static int pcf_isa_getclock(void *data)101{102return (clock);103}104105static void pcf_isa_waitforpin(void *data)106{107DEFINE_WAIT(wait);108int timeout = 2;109unsigned long flags;110111if (irq > 0) {112spin_lock_irqsave(&lock, flags);113if (pcf_pending == 0) {114spin_unlock_irqrestore(&lock, flags);115prepare_to_wait(&pcf_wait, &wait, TASK_INTERRUPTIBLE);116if (schedule_timeout(timeout*HZ)) {117spin_lock_irqsave(&lock, flags);118if (pcf_pending == 1) {119pcf_pending = 0;120}121spin_unlock_irqrestore(&lock, flags);122}123finish_wait(&pcf_wait, &wait);124} else {125pcf_pending = 0;126spin_unlock_irqrestore(&lock, flags);127}128} else {129udelay(100);130}131}132133134static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id) {135spin_lock(&lock);136pcf_pending = 1;137spin_unlock(&lock);138wake_up_interruptible(&pcf_wait);139return IRQ_HANDLED;140}141142143static int pcf_isa_init(void)144{145spin_lock_init(&lock);146if (!mmapped) {147if (!request_region(base, 2, pcf_isa_ops.name)) {148printk(KERN_ERR "%s: requested I/O region (%#x:2) is "149"in use\n", pcf_isa_ops.name, base);150return -ENODEV;151}152base_iomem = ioport_map(base, 2);153if (!base_iomem) {154printk(KERN_ERR "%s: remap of I/O region %#x failed\n",155pcf_isa_ops.name, base);156release_region(base, 2);157return -ENODEV;158}159} else {160if (!request_mem_region(base, 2, pcf_isa_ops.name)) {161printk(KERN_ERR "%s: requested memory region (%#x:2) "162"is in use\n", pcf_isa_ops.name, base);163return -ENODEV;164}165base_iomem = ioremap(base, 2);166if (base_iomem == NULL) {167printk(KERN_ERR "%s: remap of memory region %#x "168"failed\n", pcf_isa_ops.name, base);169release_mem_region(base, 2);170return -ENODEV;171}172}173pr_debug("%s: registers %#x remapped to %p\n", pcf_isa_ops.name, base,174base_iomem);175176if (irq > 0) {177if (request_irq(irq, pcf_isa_handler, 0, pcf_isa_ops.name,178NULL) < 0) {179printk(KERN_ERR "%s: Request irq%d failed\n",180pcf_isa_ops.name, irq);181irq = 0;182} else183enable_irq(irq);184}185return 0;186}187188/* ------------------------------------------------------------------------189* Encapsulate the above functions in the correct operations structure.190* This is only done when more than one hardware adapter is supported.191*/192static struct i2c_algo_pcf_data pcf_isa_data = {193.setpcf = pcf_isa_setbyte,194.getpcf = pcf_isa_getbyte,195.getown = pcf_isa_getown,196.getclock = pcf_isa_getclock,197.waitforpin = pcf_isa_waitforpin,198};199200static struct i2c_adapter pcf_isa_ops = {201.owner = THIS_MODULE,202.class = I2C_CLASS_HWMON | I2C_CLASS_SPD,203.algo_data = &pcf_isa_data,204.name = "i2c-elektor",205};206207static int __devinit elektor_match(struct device *dev, unsigned int id)208{209#ifdef __alpha__210/* check to see we have memory mapped PCF8584 connected to the211Cypress cy82c693 PCI-ISA bridge as on UP2000 board */212if (base == 0) {213struct pci_dev *cy693_dev;214215cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ,216PCI_DEVICE_ID_CONTAQ_82C693, NULL);217if (cy693_dev) {218unsigned char config;219/* yeap, we've found cypress, let's check config */220if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {221222dev_dbg(dev, "found cy82c693, config "223"register 0x47 = 0x%02x\n", config);224225/* UP2000 board has this register set to 0xe1,226but the most significant bit as seems can be227reset during the proper initialisation228sequence if guys from API decides to do that229(so, we can even enable Tsunami Pchip230window for the upper 1 Gb) */231232/* so just check for ROMCS at 0xe0000,233ROMCS enabled for writes234and external XD Bus buffer in use. */235if ((config & 0x7f) == 0x61) {236/* seems to be UP2000 like board */237base = 0xe0000;238mmapped = 1;239/* UP2000 drives ISA with2408.25 MHz (PCI/4) clock241(this can be read from cypress) */242clock = I2C_PCF_CLK | I2C_PCF_TRNS90;243dev_info(dev, "found API UP2000 like "244"board, will probe PCF8584 "245"later\n");246}247}248pci_dev_put(cy693_dev);249}250}251#endif252253/* sanity checks for mmapped I/O */254if (mmapped && base < 0xc8000) {255dev_err(dev, "incorrect base address (%#x) specified "256"for mmapped I/O\n", base);257return 0;258}259260if (base == 0) {261base = DEFAULT_BASE;262}263return 1;264}265266static int __devinit elektor_probe(struct device *dev, unsigned int id)267{268init_waitqueue_head(&pcf_wait);269if (pcf_isa_init())270return -ENODEV;271pcf_isa_ops.dev.parent = dev;272if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)273goto fail;274275dev_info(dev, "found device at %#x\n", base);276277return 0;278279fail:280if (irq > 0) {281disable_irq(irq);282free_irq(irq, NULL);283}284285if (!mmapped) {286ioport_unmap(base_iomem);287release_region(base, 2);288} else {289iounmap(base_iomem);290release_mem_region(base, 2);291}292return -ENODEV;293}294295static int __devexit elektor_remove(struct device *dev, unsigned int id)296{297i2c_del_adapter(&pcf_isa_ops);298299if (irq > 0) {300disable_irq(irq);301free_irq(irq, NULL);302}303304if (!mmapped) {305ioport_unmap(base_iomem);306release_region(base, 2);307} else {308iounmap(base_iomem);309release_mem_region(base, 2);310}311312return 0;313}314315static struct isa_driver i2c_elektor_driver = {316.match = elektor_match,317.probe = elektor_probe,318.remove = __devexit_p(elektor_remove),319.driver = {320.owner = THIS_MODULE,321.name = "i2c-elektor",322},323};324325static int __init i2c_pcfisa_init(void)326{327return isa_register_driver(&i2c_elektor_driver, 1);328}329330static void __exit i2c_pcfisa_exit(void)331{332isa_unregister_driver(&i2c_elektor_driver);333}334335MODULE_AUTHOR("Hans Berglund <[email protected]>");336MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");337MODULE_LICENSE("GPL");338339module_param(base, int, 0);340module_param(irq, int, 0);341module_param(clock, int, 0);342module_param(own, int, 0);343module_param(mmapped, int, 0);344345module_init(i2c_pcfisa_init);346module_exit(i2c_pcfisa_exit);347348349