···144144 This driver can also be built as a module. If so, the module145145 will be called i2c-i810.146146147147+config I2C_PXA148148+ tristate "Intel PXA2XX I2C adapter (EXPERIMENTAL)"149149+ depends on I2C && EXPERIMENTAL && ARCH_PXA150150+ help151151+ If you have devices in the PXA I2C bus, say yes to this option.152152+ This driver can also be built as a module. If so, the module153153+ will be called i2c-pxa.154154+155155+config I2C_PXA_SLAVE156156+ bool "Intel PXA2XX I2C Slave comms support"157157+ depends on I2C_PXA158158+ help159159+ Support I2C slave mode communications on the PXA I2C bus. This160160+ is necessary for systems where the PXA may be a target on the161161+ I2C bus.162162+147163config I2C_PIIX4148164 tristate "Intel PIIX4"149165 depends on I2C && PCI
···11+/*22+ * i2c_adap_pxa.c33+ *44+ * I2C adapter for the PXA I2C bus access.55+ *66+ * Copyright (C) 2002 Intrinsyc Software Inc.77+ * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.88+ *99+ * This program is free software; you can redistribute it and/or modify1010+ * it under the terms of the GNU General Public License version 2 as1111+ * published by the Free Software Foundation.1212+ *1313+ * History:1414+ * Apr 2002: Initial version [CS]1515+ * Jun 2002: Properly seperated algo/adap [FB]1616+ * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]1717+ * Jan 2003: added limited signal handling [Kai-Uwe Bloem]1818+ * Sep 2004: Major rework to ensure efficient bus handling [RMK]1919+ * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]2020+ * Feb 2005: Rework slave mode handling [RMK]2121+ */2222+#include <linux/kernel.h>2323+#include <linux/module.h>2424+#include <linux/i2c.h>2525+#include <linux/i2c-id.h>2626+#include <linux/init.h>2727+#include <linux/time.h>2828+#include <linux/sched.h>2929+#include <linux/delay.h>3030+#include <linux/errno.h>3131+#include <linux/interrupt.h>3232+#include <linux/i2c-pxa.h>3333+3434+#include <asm/hardware.h>3535+#include <asm/irq.h>3636+#include <asm/arch/i2c.h>3737+#include <asm/arch/pxa-regs.h>3838+3939+struct pxa_i2c {4040+ spinlock_t lock;4141+ wait_queue_head_t wait;4242+ struct i2c_msg *msg;4343+ unsigned int msg_num;4444+ unsigned int msg_idx;4545+ unsigned int msg_ptr;4646+ unsigned int slave_addr;4747+4848+ struct i2c_adapter adap;4949+#ifdef CONFIG_I2C_PXA_SLAVE5050+ struct i2c_slave_client *slave;5151+#endif5252+5353+ unsigned int irqlogidx;5454+ u32 isrlog[32];5555+ u32 icrlog[32];5656+};5757+5858+/*5959+ * I2C Slave mode address6060+ */6161+#define I2C_PXA_SLAVE_ADDR 0x16262+6363+/*6464+ * Set this to zero to remove all debug statements via dead code elimination.6565+ */6666+#undef DEBUG6767+6868+#if 06969+#define DBGLVL KERN_INFO7070+#else7171+#define DBGLVL KERN_DEBUG7272+#endif7373+7474+#ifdef DEBUG7575+7676+struct bits {7777+ u32 mask;7878+ const char *set;7979+ const char *unset;8080+};8181+#define BIT(m, s, u) { .mask = m, .set = s, .unset = u }8282+8383+static inline void8484+decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)8585+{8686+ printk("%s %08x: ", prefix, val);8787+ while (num--) {8888+ const char *str = val & bits->mask ? bits->set : bits->unset;8989+ if (str)9090+ printk("%s ", str);9191+ bits++;9292+ }9393+}9494+9595+static const struct bits isr_bits[] = {9696+ BIT(ISR_RWM, "RX", "TX"),9797+ BIT(ISR_ACKNAK, "NAK", "ACK"),9898+ BIT(ISR_UB, "Bsy", "Rdy"),9999+ BIT(ISR_IBB, "BusBsy", "BusRdy"),100100+ BIT(ISR_SSD, "SlaveStop", NULL),101101+ BIT(ISR_ALD, "ALD", NULL),102102+ BIT(ISR_ITE, "TxEmpty", NULL),103103+ BIT(ISR_IRF, "RxFull", NULL),104104+ BIT(ISR_GCAD, "GenCall", NULL),105105+ BIT(ISR_SAD, "SlaveAddr", NULL),106106+ BIT(ISR_BED, "BusErr", NULL),107107+};108108+109109+static void decode_ISR(unsigned int val)110110+{111111+ decode_bits(DBGLVL "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);112112+ printk("\n");113113+}114114+115115+static const struct bits icr_bits[] = {116116+ BIT(ICR_START, "START", NULL),117117+ BIT(ICR_STOP, "STOP", NULL),118118+ BIT(ICR_ACKNAK, "ACKNAK", NULL),119119+ BIT(ICR_TB, "TB", NULL),120120+ BIT(ICR_MA, "MA", NULL),121121+ BIT(ICR_SCLE, "SCLE", "scle"),122122+ BIT(ICR_IUE, "IUE", "iue"),123123+ BIT(ICR_GCD, "GCD", NULL),124124+ BIT(ICR_ITEIE, "ITEIE", NULL),125125+ BIT(ICR_IRFIE, "IRFIE", NULL),126126+ BIT(ICR_BEIE, "BEIE", NULL),127127+ BIT(ICR_SSDIE, "SSDIE", NULL),128128+ BIT(ICR_ALDIE, "ALDIE", NULL),129129+ BIT(ICR_SADIE, "SADIE", NULL),130130+ BIT(ICR_UR, "UR", "ur"),131131+};132132+133133+static void decode_ICR(unsigned int val)134134+{135135+ decode_bits(DBGLVL "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);136136+ printk("\n");137137+}138138+139139+static unsigned int i2c_debug = DEBUG;140140+141141+static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)142142+{143143+ printk(DBGLVL "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno, ISR, ICR, IBMR);144144+}145145+146146+#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __FUNCTION__)147147+#else148148+#define i2c_debug 0149149+150150+#define show_state(i2c) do { } while (0)151151+#define decode_ISR(val) do { } while (0)152152+#define decode_ICR(val) do { } while (0)153153+#endif154154+155155+#define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(DBGLVL "" x); } } while(0)156156+157157+static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);158158+159159+static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)160160+{161161+ unsigned int i;162162+ printk("i2c: error: %s\n", why);163163+ printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",164164+ i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);165165+ printk("i2c: ICR: %08x ISR: %08x\ni2c: log: ", ICR, ISR);166166+ for (i = 0; i < i2c->irqlogidx; i++)167167+ printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);168168+ printk("\n");169169+}170170+171171+static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)172172+{173173+ return !(ICR & ICR_SCLE);174174+}175175+176176+static void i2c_pxa_abort(struct pxa_i2c *i2c)177177+{178178+ unsigned long timeout = jiffies + HZ/4;179179+180180+ if (i2c_pxa_is_slavemode(i2c)) {181181+ printk(DBGLVL "i2c_pxa_transfer: called in slave mode\n");182182+ return;183183+ }184184+185185+ while (time_before(jiffies, timeout) && (IBMR & 0x1) == 0) {186186+ unsigned long icr = ICR;187187+188188+ icr &= ~ICR_START;189189+ icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;190190+191191+ ICR = icr;192192+193193+ show_state(i2c);194194+195195+ msleep(1);196196+ }197197+198198+ ICR &= ~(ICR_MA | ICR_START | ICR_STOP);199199+}200200+201201+static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)202202+{203203+ int timeout = DEF_TIMEOUT;204204+205205+ while (timeout-- && ISR & (ISR_IBB | ISR_UB)) {206206+ if ((ISR & ISR_SAD) != 0)207207+ timeout += 4;208208+209209+ msleep(2);210210+ show_state(i2c);211211+ }212212+213213+ if (timeout <= 0)214214+ show_state(i2c);215215+216216+ return timeout <= 0 ? I2C_RETRY : 0;217217+}218218+219219+static int i2c_pxa_wait_master(struct pxa_i2c *i2c)220220+{221221+ unsigned long timeout = jiffies + HZ*4;222222+223223+ while (time_before(jiffies, timeout)) {224224+ if (i2c_debug > 1)225225+ printk(DBGLVL "i2c_pxa_wait_master: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",226226+ (long)jiffies, ISR, ICR, IBMR);227227+228228+ if (ISR & ISR_SAD) {229229+ if (i2c_debug > 0)230230+ printk(DBGLVL "i2c_pxa_wait_master: Slave detected\n");231231+ goto out;232232+ }233233+234234+ /* wait for unit and bus being not busy, and we also do a235235+ * quick check of the i2c lines themselves to ensure they've236236+ * gone high...237237+ */238238+ if ((ISR & (ISR_UB | ISR_IBB)) == 0 && IBMR == 3) {239239+ if (i2c_debug > 0)240240+ printk(DBGLVL "i2c_pxa_wait_master: done\n");241241+ return 1;242242+ }243243+244244+ msleep(1);245245+ }246246+247247+ if (i2c_debug > 0)248248+ printk(DBGLVL "i2c_pxa_wait_master: did not free\n");249249+ out:250250+ return 0;251251+}252252+253253+static int i2c_pxa_set_master(struct pxa_i2c *i2c)254254+{255255+ if (i2c_debug)256256+ printk(DBGLVL "I2C: setting to bus master\n");257257+258258+ if ((ISR & (ISR_UB | ISR_IBB)) != 0) {259259+ printk(DBGLVL "set_master: unit is busy\n");260260+ if (!i2c_pxa_wait_master(i2c)) {261261+ printk(DBGLVL "set_master: error: unit busy\n");262262+ return I2C_RETRY;263263+ }264264+ }265265+266266+ ICR |= ICR_SCLE;267267+ return 0;268268+}269269+270270+#ifdef CONFIG_I2C_PXA_SLAVE271271+static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)272272+{273273+ unsigned long timeout = jiffies + HZ*1;274274+275275+ /* wait for stop */276276+277277+ show_state(i2c);278278+279279+ while (time_before(jiffies, timeout)) {280280+ if (i2c_debug > 1)281281+ printk(DBGLVL "i2c_pxa_wait_slave: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",282282+ (long)jiffies, ISR, ICR, IBMR);283283+284284+ if ((ISR & (ISR_UB|ISR_IBB|ISR_SAD)) == ISR_SAD ||285285+ (ICR & ICR_SCLE) == 0) {286286+ if (i2c_debug > 1)287287+ printk(DBGLVL "i2c_pxa_wait_slave: done\n");288288+ return 1;289289+ }290290+291291+ msleep(1);292292+ }293293+294294+ if (i2c_debug > 0)295295+ printk(DBGLVL "i2c_pxa_wait_slave: did not free\n");296296+ return 0;297297+}298298+299299+/*300300+ * clear the hold on the bus, and take of anything else301301+ * that has been configured302302+ */303303+static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)304304+{305305+ show_state(i2c);306306+307307+ if (errcode < 0) {308308+ udelay(100); /* simple delay */309309+ } else {310310+ /* we need to wait for the stop condition to end */311311+312312+ /* if we where in stop, then clear... */313313+ if (ICR & ICR_STOP) {314314+ udelay(100);315315+ ICR &= ~ICR_STOP;316316+ }317317+318318+ if (!i2c_pxa_wait_slave(i2c)) {319319+ printk(KERN_ERR "i2c_pxa_set_slave: wait timedout\n");320320+ return;321321+ }322322+ }323323+324324+ ICR &= ~(ICR_STOP|ICR_ACKNAK|ICR_MA);325325+ ICR &= ~ICR_SCLE;326326+327327+ if (i2c_debug) {328328+ printk(DBGLVL "ICR now %08x, ISR %08x\n", ICR, ISR);329329+ decode_ICR(ICR);330330+ }331331+}332332+#else333333+#define i2c_pxa_set_slave(i2c, err) do { } while (0)334334+#endif335335+336336+static void i2c_pxa_reset(struct pxa_i2c *i2c)337337+{338338+ pr_debug("Resetting I2C Controller Unit\n");339339+340340+ /* abort any transfer currently under way */341341+ i2c_pxa_abort(i2c);342342+343343+ /* reset according to 9.8 */344344+ ICR = ICR_UR;345345+ ISR = I2C_ISR_INIT;346346+ ICR &= ~ICR_UR;347347+348348+ ISAR = i2c->slave_addr;349349+350350+ /* set control register values */351351+ ICR = I2C_ICR_INIT;352352+353353+#ifdef CONFIG_I2C_PXA_SLAVE354354+ printk(KERN_INFO "I2C: Enabling slave mode\n");355355+ ICR |= ICR_SADIE | ICR_ALDIE | ICR_SSDIE;356356+#endif357357+358358+ i2c_pxa_set_slave(i2c, 0);359359+360360+ /* enable unit */361361+ ICR |= ICR_IUE;362362+ udelay(100);363363+}364364+365365+366366+#ifdef CONFIG_I2C_PXA_SLAVE367367+/*368368+ * I2C EEPROM emulation.369369+ */370370+static struct i2c_eeprom_emu eeprom = {371371+ .size = I2C_EEPROM_EMU_SIZE,372372+ .watch = LIST_HEAD_INIT(eeprom.watch),373373+};374374+375375+struct i2c_eeprom_emu *i2c_pxa_get_eeprom(void)376376+{377377+ return &eeprom;378378+}379379+380380+int i2c_eeprom_emu_addwatcher(struct i2c_eeprom_emu *emu, void *data,381381+ unsigned int addr, unsigned int size,382382+ struct i2c_eeprom_emu_watcher *watcher)383383+{384384+ struct i2c_eeprom_emu_watch *watch;385385+ unsigned long flags;386386+387387+ if (addr + size > emu->size)388388+ return -EINVAL;389389+390390+ watch = kmalloc(sizeof(struct i2c_eeprom_emu_watch), GFP_KERNEL);391391+ if (watch) {392392+ watch->start = addr;393393+ watch->end = addr + size - 1;394394+ watch->ops = watcher;395395+ watch->data = data;396396+397397+ local_irq_save(flags);398398+ list_add(&watch->node, &emu->watch);399399+ local_irq_restore(flags);400400+ }401401+402402+ return watch ? 0 : -ENOMEM;403403+}404404+405405+void i2c_eeprom_emu_delwatcher(struct i2c_eeprom_emu *emu, void *data,406406+ struct i2c_eeprom_emu_watcher *watcher)407407+{408408+ struct i2c_eeprom_emu_watch *watch, *n;409409+ unsigned long flags;410410+411411+ list_for_each_entry_safe(watch, n, &emu->watch, node) {412412+ if (watch->ops == watcher && watch->data == data) {413413+ local_irq_save(flags);414414+ list_del(&watch->node);415415+ local_irq_restore(flags);416416+ kfree(watch);417417+ }418418+ }419419+}420420+421421+static void i2c_eeprom_emu_event(void *ptr, i2c_slave_event_t event)422422+{423423+ struct i2c_eeprom_emu *emu = ptr;424424+425425+ eedbg(3, "i2c_eeprom_emu_event: %d\n", event);426426+427427+ switch (event) {428428+ case I2C_SLAVE_EVENT_START_WRITE:429429+ emu->seen_start = 1;430430+ eedbg(2, "i2c_eeprom: write initiated\n");431431+ break;432432+433433+ case I2C_SLAVE_EVENT_START_READ:434434+ emu->seen_start = 0;435435+ eedbg(2, "i2c_eeprom: read initiated\n");436436+ break;437437+438438+ case I2C_SLAVE_EVENT_STOP:439439+ emu->seen_start = 0;440440+ eedbg(2, "i2c_eeprom: received stop\n");441441+ break;442442+443443+ default:444444+ eedbg(0, "i2c_eeprom: unhandled event\n");445445+ break;446446+ }447447+}448448+449449+static int i2c_eeprom_emu_read(void *ptr)450450+{451451+ struct i2c_eeprom_emu *emu = ptr;452452+ int ret;453453+454454+ ret = emu->bytes[emu->ptr];455455+ emu->ptr = (emu->ptr + 1) % emu->size;456456+457457+ return ret;458458+}459459+460460+static void i2c_eeprom_emu_write(void *ptr, unsigned int val)461461+{462462+ struct i2c_eeprom_emu *emu = ptr;463463+ struct i2c_eeprom_emu_watch *watch;464464+465465+ if (emu->seen_start != 0) {466466+ eedbg(2, "i2c_eeprom_emu_write: setting ptr %02x\n", val);467467+ emu->ptr = val;468468+ emu->seen_start = 0;469469+ return;470470+ }471471+472472+ emu->bytes[emu->ptr] = val;473473+474474+ eedbg(1, "i2c_eeprom_emu_write: ptr=0x%02x, val=0x%02x\n",475475+ emu->ptr, val);476476+477477+ list_for_each_entry(watch, &emu->watch, node) {478478+ if (!watch->ops || !watch->ops->write)479479+ continue;480480+ if (watch->start <= emu->ptr && watch->end >= emu->ptr)481481+ watch->ops->write(watch->data, emu->ptr, val);482482+ }483483+484484+ emu->ptr = (emu->ptr + 1) % emu->size;485485+}486486+487487+struct i2c_slave_client eeprom_client = {488488+ .data = &eeprom,489489+ .event = i2c_eeprom_emu_event,490490+ .read = i2c_eeprom_emu_read,491491+ .write = i2c_eeprom_emu_write492492+};493493+494494+/*495495+ * PXA I2C Slave mode496496+ */497497+498498+static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)499499+{500500+ if (isr & ISR_BED) {501501+ /* what should we do here? */502502+ } else {503503+ int ret = i2c->slave->read(i2c->slave->data);504504+505505+ IDBR = ret;506506+ ICR |= ICR_TB; /* allow next byte */507507+ }508508+}509509+510510+static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)511511+{512512+ unsigned int byte = IDBR;513513+514514+ if (i2c->slave != NULL)515515+ i2c->slave->write(i2c->slave->data, byte);516516+517517+ ICR |= ICR_TB;518518+}519519+520520+static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)521521+{522522+ int timeout;523523+524524+ if (i2c_debug > 0)525525+ printk(DBGLVL "I2C: SAD, mode is slave-%cx\n",526526+ (isr & ISR_RWM) ? 'r' : 't');527527+528528+ if (i2c->slave != NULL)529529+ i2c->slave->event(i2c->slave->data,530530+ (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);531531+532532+ /*533533+ * slave could interrupt in the middle of us generating a534534+ * start condition... if this happens, we'd better back off535535+ * and stop holding the poor thing up536536+ */537537+ ICR &= ~(ICR_START|ICR_STOP);538538+ ICR |= ICR_TB;539539+540540+ timeout = 0x10000;541541+542542+ while (1) {543543+ if ((IBMR & 2) == 2)544544+ break;545545+546546+ timeout--;547547+548548+ if (timeout <= 0) {549549+ printk(KERN_ERR "timeout waiting for SCL high\n");550550+ break;551551+ }552552+ }553553+554554+ ICR &= ~ICR_SCLE;555555+}556556+557557+static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)558558+{559559+ if (i2c_debug > 2)560560+ printk(DBGLVL "ISR: SSD (Slave Stop)\n");561561+562562+ if (i2c->slave != NULL)563563+ i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);564564+565565+ if (i2c_debug > 2)566566+ printk(DBGLVL "ISR: SSD (Slave Stop) acked\n");567567+568568+ /*569569+ * If we have a master-mode message waiting,570570+ * kick it off now that the slave has completed.571571+ */572572+ if (i2c->msg)573573+ i2c_pxa_master_complete(i2c, I2C_RETRY);574574+}575575+#else576576+static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)577577+{578578+ if (isr & ISR_BED) {579579+ /* what should we do here? */580580+ } else {581581+ IDBR = 0;582582+ ICR |= ICR_TB;583583+ }584584+}585585+586586+static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)587587+{588588+ ICR |= ICR_TB | ICR_ACKNAK;589589+}590590+591591+static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)592592+{593593+ int timeout;594594+595595+ /*596596+ * slave could interrupt in the middle of us generating a597597+ * start condition... if this happens, we'd better back off598598+ * and stop holding the poor thing up599599+ */600600+ ICR &= ~(ICR_START|ICR_STOP);601601+ ICR |= ICR_TB | ICR_ACKNAK;602602+603603+ timeout = 0x10000;604604+605605+ while (1) {606606+ if ((IBMR & 2) == 2)607607+ break;608608+609609+ timeout--;610610+611611+ if (timeout <= 0) {612612+ printk(KERN_ERR "timeout waiting for SCL high\n");613613+ break;614614+ }615615+ }616616+617617+ ICR &= ~ICR_SCLE;618618+}619619+620620+static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)621621+{622622+ if (i2c->msg)623623+ i2c_pxa_master_complete(i2c, I2C_RETRY);624624+}625625+#endif626626+627627+/*628628+ * PXA I2C Master mode629629+ */630630+631631+static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)632632+{633633+ unsigned int addr = (msg->addr & 0x7f) << 1;634634+635635+ if (msg->flags & I2C_M_RD)636636+ addr |= 1;637637+638638+ return addr;639639+}640640+641641+static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)642642+{643643+ u32 icr;644644+645645+ /*646646+ * Step 1: target slave address into IDBR647647+ */648648+ IDBR = i2c_pxa_addr_byte(i2c->msg);649649+650650+ /*651651+ * Step 2: initiate the write.652652+ */653653+ icr = ICR & ~(ICR_STOP | ICR_ALDIE);654654+ ICR = icr | ICR_START | ICR_TB;655655+}656656+657657+/*658658+ * We are protected by the adapter bus semaphore.659659+ */660660+static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)661661+{662662+ long timeout;663663+ int ret;664664+665665+ /*666666+ * Wait for the bus to become free.667667+ */668668+ ret = i2c_pxa_wait_bus_not_busy(i2c);669669+ if (ret) {670670+ printk(KERN_INFO "i2c_pxa: timeout waiting for bus free\n");671671+ goto out;672672+ }673673+674674+ /*675675+ * Set master mode.676676+ */677677+ ret = i2c_pxa_set_master(i2c);678678+ if (ret) {679679+ printk(KERN_INFO "i2c_pxa_set_master: error %d\n", ret);680680+ goto out;681681+ }682682+683683+ spin_lock_irq(&i2c->lock);684684+685685+ i2c->msg = msg;686686+ i2c->msg_num = num;687687+ i2c->msg_idx = 0;688688+ i2c->msg_ptr = 0;689689+ i2c->irqlogidx = 0;690690+691691+ i2c_pxa_start_message(i2c);692692+693693+ spin_unlock_irq(&i2c->lock);694694+695695+ /*696696+ * The rest of the processing occurs in the interrupt handler.697697+ */698698+ timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);699699+700700+ /*701701+ * We place the return code in i2c->msg_idx.702702+ */703703+ ret = i2c->msg_idx;704704+705705+ if (timeout == 0)706706+ i2c_pxa_scream_blue_murder(i2c, "timeout");707707+708708+ out:709709+ return ret;710710+}711711+712712+/*713713+ * i2c_pxa_master_complete - complete the message and wake up.714714+ */715715+static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)716716+{717717+ i2c->msg_ptr = 0;718718+ i2c->msg = NULL;719719+ i2c->msg_idx ++;720720+ i2c->msg_num = 0;721721+ if (ret)722722+ i2c->msg_idx = ret;723723+ wake_up(&i2c->wait);724724+}725725+726726+static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)727727+{728728+ u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);729729+730730+ again:731731+ /*732732+ * If ISR_ALD is set, we lost arbitration.733733+ */734734+ if (isr & ISR_ALD) {735735+ /*736736+ * Do we need to do anything here? The PXA docs737737+ * are vague about what happens.738738+ */739739+ i2c_pxa_scream_blue_murder(i2c, "ALD set");740740+741741+ /*742742+ * We ignore this error. We seem to see spurious ALDs743743+ * for seemingly no reason. If we handle them as I think744744+ * they should, we end up causing an I2C error, which745745+ * is painful for some systems.746746+ */747747+ return; /* ignore */748748+ }749749+750750+ if (isr & ISR_BED) {751751+ int ret = BUS_ERROR;752752+753753+ /*754754+ * I2C bus error - either the device NAK'd us, or755755+ * something more serious happened. If we were NAK'd756756+ * on the initial address phase, we can retry.757757+ */758758+ if (isr & ISR_ACKNAK) {759759+ if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)760760+ ret = I2C_RETRY;761761+ else762762+ ret = XFER_NAKED;763763+ }764764+ i2c_pxa_master_complete(i2c, ret);765765+ } else if (isr & ISR_RWM) {766766+ /*767767+ * Read mode. We have just sent the address byte, and768768+ * now we must initiate the transfer.769769+ */770770+ if (i2c->msg_ptr == i2c->msg->len - 1 &&771771+ i2c->msg_idx == i2c->msg_num - 1)772772+ icr |= ICR_STOP | ICR_ACKNAK;773773+774774+ icr |= ICR_ALDIE | ICR_TB;775775+ } else if (i2c->msg_ptr < i2c->msg->len) {776776+ /*777777+ * Write mode. Write the next data byte.778778+ */779779+ IDBR = i2c->msg->buf[i2c->msg_ptr++];780780+781781+ icr |= ICR_ALDIE | ICR_TB;782782+783783+ /*784784+ * If this is the last byte of the last message, send785785+ * a STOP.786786+ */787787+ if (i2c->msg_ptr == i2c->msg->len &&788788+ i2c->msg_idx == i2c->msg_num - 1)789789+ icr |= ICR_STOP;790790+ } else if (i2c->msg_idx < i2c->msg_num - 1) {791791+ /*792792+ * Next segment of the message.793793+ */794794+ i2c->msg_ptr = 0;795795+ i2c->msg_idx ++;796796+ i2c->msg++;797797+798798+ /*799799+ * If we aren't doing a repeated start and address,800800+ * go back and try to send the next byte. Note that801801+ * we do not support switching the R/W direction here.802802+ */803803+ if (i2c->msg->flags & I2C_M_NOSTART)804804+ goto again;805805+806806+ /*807807+ * Write the next address.808808+ */809809+ IDBR = i2c_pxa_addr_byte(i2c->msg);810810+811811+ /*812812+ * And trigger a repeated start, and send the byte.813813+ */814814+ icr &= ~ICR_ALDIE;815815+ icr |= ICR_START | ICR_TB;816816+ } else {817817+ if (i2c->msg->len == 0) {818818+ /*819819+ * Device probes have a message length of zero820820+ * and need the bus to be reset before it can821821+ * be used again.822822+ */823823+ i2c_pxa_reset(i2c);824824+ }825825+ i2c_pxa_master_complete(i2c, 0);826826+ }827827+828828+ i2c->icrlog[i2c->irqlogidx-1] = icr;829829+830830+ ICR = icr;831831+ show_state(i2c);832832+}833833+834834+static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)835835+{836836+ u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);837837+838838+ /*839839+ * Read the byte.840840+ */841841+ i2c->msg->buf[i2c->msg_ptr++] = IDBR;842842+843843+ if (i2c->msg_ptr < i2c->msg->len) {844844+ /*845845+ * If this is the last byte of the last846846+ * message, send a STOP.847847+ */848848+ if (i2c->msg_ptr == i2c->msg->len - 1)849849+ icr |= ICR_STOP | ICR_ACKNAK;850850+851851+ icr |= ICR_ALDIE | ICR_TB;852852+ } else {853853+ i2c_pxa_master_complete(i2c, 0);854854+ }855855+856856+ i2c->icrlog[i2c->irqlogidx-1] = icr;857857+858858+ ICR = icr;859859+}860860+861861+static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id, struct pt_regs *regs)862862+{863863+ struct pxa_i2c *i2c = dev_id;864864+ u32 isr = ISR;865865+866866+ if (i2c_debug > 2 && 0) {867867+ printk(DBGLVL "i2c_pxa_handler: ISR=%08x, ICR=%08x, IBMR=%02x\n",868868+ isr, ICR, IBMR);869869+ decode_ISR(isr);870870+ }871871+872872+ if (i2c->irqlogidx < sizeof(i2c->isrlog)/sizeof(u32))873873+ i2c->isrlog[i2c->irqlogidx++] = isr;874874+875875+ show_state(i2c);876876+877877+ /*878878+ * Always clear all pending IRQs.879879+ */880880+ ISR = isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED);881881+882882+ if (isr & ISR_SAD)883883+ i2c_pxa_slave_start(i2c, isr);884884+ if (isr & ISR_SSD)885885+ i2c_pxa_slave_stop(i2c);886886+887887+ if (i2c_pxa_is_slavemode(i2c)) {888888+ if (isr & ISR_ITE)889889+ i2c_pxa_slave_txempty(i2c, isr);890890+ if (isr & ISR_IRF)891891+ i2c_pxa_slave_rxfull(i2c, isr);892892+ } else if (i2c->msg) {893893+ if (isr & ISR_ITE)894894+ i2c_pxa_irq_txempty(i2c, isr);895895+ if (isr & ISR_IRF)896896+ i2c_pxa_irq_rxfull(i2c, isr);897897+ } else {898898+ i2c_pxa_scream_blue_murder(i2c, "spurious irq");899899+ }900900+901901+ return IRQ_HANDLED;902902+}903903+904904+905905+static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)906906+{907907+ struct pxa_i2c *i2c = adap->algo_data;908908+ int ret, i;909909+910910+ for (i = adap->retries; i >= 0; i--) {911911+ ret = i2c_pxa_do_xfer(i2c, msgs, num);912912+ if (ret != I2C_RETRY)913913+ goto out;914914+915915+ if (i2c_debug)916916+ printk(KERN_INFO "Retrying transmission\n");917917+ udelay(100);918918+ }919919+ i2c_pxa_scream_blue_murder(i2c, "exhausted retries");920920+ ret = -EREMOTEIO;921921+ out:922922+ i2c_pxa_set_slave(i2c, ret);923923+ return ret;924924+}925925+926926+static struct i2c_algorithm i2c_pxa_algorithm = {927927+ .name = "PXA-I2C-Algorithm",928928+ .id = I2C_ALGO_PXA,929929+ .master_xfer = i2c_pxa_xfer,930930+};931931+932932+static struct pxa_i2c i2c_pxa = {933933+ .lock = SPIN_LOCK_UNLOCKED,934934+ .wait = __WAIT_QUEUE_HEAD_INITIALIZER(i2c_pxa.wait),935935+ .adap = {936936+ .name = "pxa2xx-i2c",937937+ .id = I2C_ALGO_PXA,938938+ .algo = &i2c_pxa_algorithm,939939+ .retries = 5,940940+ },941941+};942942+943943+static int i2c_pxa_probe(struct device *dev)944944+{945945+ struct pxa_i2c *i2c = &i2c_pxa;946946+ struct i2c_pxa_platform_data *plat = dev->platform_data;947947+ int ret;948948+949949+#ifdef CONFIG_PXA27x950950+ pxa_gpio_mode(GPIO117_I2CSCL_MD);951951+ pxa_gpio_mode(GPIO118_I2CSDA_MD);952952+ udelay(100);953953+#endif954954+955955+ i2c->slave_addr = I2C_PXA_SLAVE_ADDR;956956+957957+#ifdef CONFIG_I2C_PXA_SLAVE958958+ i2c->slave = &eeprom_client;959959+ if (plat) {960960+ i2c->slave_addr = plat->slave_addr;961961+ if (plat->slave)962962+ i2c->slave = plat->slave;963963+ }964964+#endif965965+966966+ pxa_set_cken(CKEN14_I2C, 1);967967+ ret = request_irq(IRQ_I2C, i2c_pxa_handler, SA_INTERRUPT,968968+ "pxa2xx-i2c", i2c);969969+ if (ret)970970+ goto out;971971+972972+ i2c_pxa_reset(i2c);973973+974974+ i2c->adap.algo_data = i2c;975975+ i2c->adap.dev.parent = dev;976976+977977+ ret = i2c_add_adapter(&i2c->adap);978978+ if (ret < 0) {979979+ printk(KERN_INFO "I2C: Failed to add bus\n");980980+ goto err_irq;981981+ }982982+983983+ dev_set_drvdata(dev, i2c);984984+985985+#ifdef CONFIG_I2C_PXA_SLAVE986986+ printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",987987+ i2c->adap.dev.bus_id, i2c->slave_addr);988988+#else989989+ printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",990990+ i2c->adap.dev.bus_id);991991+#endif992992+ return 0;993993+994994+ err_irq:995995+ free_irq(IRQ_I2C, i2c);996996+ out:997997+ return ret;998998+}999999+10001000+static int i2c_pxa_remove(struct device *dev)10011001+{10021002+ struct pxa_i2c *i2c = dev_get_drvdata(dev);10031003+10041004+ dev_set_drvdata(dev, NULL);10051005+10061006+ i2c_del_adapter(&i2c->adap);10071007+ free_irq(IRQ_I2C, i2c);10081008+ pxa_set_cken(CKEN14_I2C, 0);10091009+10101010+ return 0;10111011+}10121012+10131013+static struct device_driver i2c_pxa_driver = {10141014+ .name = "pxa2xx-i2c",10151015+ .bus = &platform_bus_type,10161016+ .probe = i2c_pxa_probe,10171017+ .remove = i2c_pxa_remove,10181018+};10191019+10201020+static int __init i2c_adap_pxa_init(void)10211021+{10221022+ return driver_register(&i2c_pxa_driver);10231023+}10241024+10251025+static void i2c_adap_pxa_exit(void)10261026+{10271027+ return driver_unregister(&i2c_pxa_driver);10281028+}10291029+10301030+module_init(i2c_adap_pxa_init);10311031+module_exit(i2c_adap_pxa_exit);
+70
include/asm-arm/arch-pxa/i2c.h
···11+/*22+ * i2c_pxa.h33+ *44+ * Copyright (C) 2002 Intrinsyc Software Inc.55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 as88+ * published by the Free Software Foundation.99+ *1010+ */1111+#ifndef _I2C_PXA_H_1212+#define _I2C_PXA_H_1313+1414+#if 01515+#define DEF_TIMEOUT 31616+#else1717+/* need a longer timeout if we're dealing with the fact we may well be1818+ * looking at a multi-master environment1919+*/2020+#define DEF_TIMEOUT 322121+#endif2222+2323+#define BUS_ERROR (-EREMOTEIO)2424+#define XFER_NAKED (-ECONNREFUSED)2525+#define I2C_RETRY (-2000) /* an error has occurred retry transmit */2626+2727+/* ICR initialize bit values2828+*2929+* 15. FM 0 (100 Khz operation)3030+* 14. UR 0 (No unit reset)3131+* 13. SADIE 0 (Disables the unit from interrupting on slave addresses3232+* matching its slave address)3333+* 12. ALDIE 0 (Disables the unit from interrupt when it loses arbitration3434+* in master mode)3535+* 11. SSDIE 0 (Disables interrupts from a slave stop detected, in slave mode)3636+* 10. BEIE 1 (Enable interrupts from detected bus errors, no ACK sent)3737+* 9. IRFIE 1 (Enable interrupts from full buffer received)3838+* 8. ITEIE 1 (Enables the I2C unit to interrupt when transmit buffer empty)3939+* 7. GCD 1 (Disables i2c unit response to general call messages as a slave)4040+* 6. IUE 0 (Disable unit until we change settings)4141+* 5. SCLE 1 (Enables the i2c clock output for master mode (drives SCL)4242+* 4. MA 0 (Only send stop with the ICR stop bit)4343+* 3. TB 0 (We are not transmitting a byte initially)4444+* 2. ACKNAK 0 (Send an ACK after the unit receives a byte)4545+* 1. STOP 0 (Do not send a STOP)4646+* 0. START 0 (Do not send a START)4747+*4848+*/4949+#define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)5050+5151+/* I2C status register init values5252+ *5353+ * 10. BED 1 (Clear bus error detected)5454+ * 9. SAD 1 (Clear slave address detected)5555+ * 7. IRF 1 (Clear IDBR Receive Full)5656+ * 6. ITE 1 (Clear IDBR Transmit Empty)5757+ * 5. ALD 1 (Clear Arbitration Loss Detected)5858+ * 4. SSD 1 (Clear Slave Stop Detected)5959+ */6060+#define I2C_ISR_INIT 0x7FF /* status register init */6161+6262+struct i2c_slave_client;6363+6464+struct i2c_pxa_platform_data {6565+ unsigned int slave_addr;6666+ struct i2c_slave_client *slave;6767+};6868+6969+extern void pxa_set_i2c_info(struct i2c_pxa_platform_data *info);7070+#endif