blob: 466e4f681d7a4eefd4f1316d21bcb39b7ea680be [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Russell Kingb652b432005-06-15 12:38:14 +01002/*
3 * i2c_adap_pxa.c
4 *
5 * I2C adapter for the PXA I2C bus access.
6 *
7 * Copyright (C) 2002 Intrinsyc Software Inc.
8 * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
9 *
Russell Kingb652b432005-06-15 12:38:14 +010010 * History:
11 * Apr 2002: Initial version [CS]
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +080012 * Jun 2002: Properly separated algo/adap [FB]
Russell Kingb652b432005-06-15 12:38:14 +010013 * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
14 * Jan 2003: added limited signal handling [Kai-Uwe Bloem]
15 * Sep 2004: Major rework to ensure efficient bus handling [RMK]
16 * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
17 * Feb 2005: Rework slave mode handling [RMK]
18 */
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/i2c.h>
Russell Kingb652b432005-06-15 12:38:14 +010022#include <linux/init.h>
23#include <linux/time.h>
24#include <linux/sched.h>
25#include <linux/delay.h>
26#include <linux/errno.h>
27#include <linux/interrupt.h>
Haojian Zhuang63fe1222012-03-01 13:04:44 +080028#include <linux/of.h>
29#include <linux/of_device.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010030#include <linux/platform_device.h>
Russell Kingc3cef3f2007-08-20 10:19:10 +010031#include <linux/err.h>
32#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020034#include <linux/io.h>
Wolfram Sangf15fc9b2017-11-13 18:27:39 +010035#include <linux/platform_data/i2c-pxa.h>
Russell Kingb652b432005-06-15 12:38:14 +010036
Russell Kingb652b432005-06-15 12:38:14 +010037#include <asm/irq.h>
Eric Miao283afa02008-09-08 14:15:08 +080038
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010039struct pxa_reg_layout {
40 u32 ibmr;
41 u32 idbr;
42 u32 icr;
43 u32 isr;
44 u32 isar;
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +053045 u32 ilcr;
46 u32 iwcr;
Romain Perier6c14bda2016-12-01 12:04:37 +010047 u32 fm;
48 u32 hs;
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010049};
50
51enum pxa_i2c_types {
52 REGS_PXA2XX,
53 REGS_PXA3XX,
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +010054 REGS_CE4100,
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +053055 REGS_PXA910,
Romain Perier294be032016-12-01 12:04:38 +010056 REGS_A3700,
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010057};
58
Romain Perier294be032016-12-01 12:04:38 +010059#define ICR_BUSMODE_FM (1 << 16) /* shifted fast mode for armada-3700 */
60#define ICR_BUSMODE_HS (1 << 17) /* shifted high speed mode for armada-3700 */
61
Eric Miao283afa02008-09-08 14:15:08 +080062/*
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010063 * I2C registers definitions
Eric Miaof23d4912009-04-13 14:43:25 +080064 */
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010065static struct pxa_reg_layout pxa_reg_layout[] = {
66 [REGS_PXA2XX] = {
67 .ibmr = 0x00,
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010068 .idbr = 0x08,
69 .icr = 0x10,
70 .isr = 0x18,
71 .isar = 0x20,
72 },
Vasily Khoruzhick23e74a82011-03-13 15:53:28 +020073 [REGS_PXA3XX] = {
74 .ibmr = 0x00,
75 .idbr = 0x04,
76 .icr = 0x08,
77 .isr = 0x0c,
78 .isar = 0x10,
79 },
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +010080 [REGS_CE4100] = {
81 .ibmr = 0x14,
82 .idbr = 0x0c,
83 .icr = 0x00,
84 .isr = 0x04,
85 /* no isar register */
86 },
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +053087 [REGS_PXA910] = {
88 .ibmr = 0x00,
89 .idbr = 0x08,
90 .icr = 0x10,
91 .isr = 0x18,
92 .isar = 0x20,
93 .ilcr = 0x28,
94 .iwcr = 0x30,
95 },
Romain Perier294be032016-12-01 12:04:38 +010096 [REGS_A3700] = {
97 .ibmr = 0x00,
98 .idbr = 0x04,
99 .icr = 0x08,
100 .isr = 0x0c,
101 .isar = 0x10,
102 .fm = ICR_BUSMODE_FM,
103 .hs = ICR_BUSMODE_HS,
104 },
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100105};
Eric Miaof23d4912009-04-13 14:43:25 +0800106
107static const struct platform_device_id i2c_pxa_id_table[] = {
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100108 { "pxa2xx-i2c", REGS_PXA2XX },
109 { "pxa3xx-pwri2c", REGS_PXA3XX },
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100110 { "ce4100-i2c", REGS_CE4100 },
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530111 { "pxa910-i2c", REGS_PXA910 },
Romain Perier294be032016-12-01 12:04:38 +0100112 { "armada-3700-i2c", REGS_A3700 },
Eric Miaof23d4912009-04-13 14:43:25 +0800113 { },
114};
115MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table);
116
117/*
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100118 * I2C bit definitions
Eric Miao283afa02008-09-08 14:15:08 +0800119 */
Eric Miao283afa02008-09-08 14:15:08 +0800120
121#define ICR_START (1 << 0) /* start bit */
122#define ICR_STOP (1 << 1) /* stop bit */
123#define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */
124#define ICR_TB (1 << 3) /* transfer byte bit */
125#define ICR_MA (1 << 4) /* master abort */
126#define ICR_SCLE (1 << 5) /* master clock enable */
127#define ICR_IUE (1 << 6) /* unit enable */
128#define ICR_GCD (1 << 7) /* general call disable */
129#define ICR_ITEIE (1 << 8) /* enable tx interrupts */
130#define ICR_IRFIE (1 << 9) /* enable rx interrupts */
131#define ICR_BEIE (1 << 10) /* enable bus error ints */
132#define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */
133#define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */
134#define ICR_SADIE (1 << 13) /* slave address detected int enable */
135#define ICR_UR (1 << 14) /* unit reset */
136#define ICR_FM (1 << 15) /* fast mode */
Leilei Shang9d3dda52013-06-07 14:38:17 +0800137#define ICR_HS (1 << 16) /* High Speed mode */
138#define ICR_GPIOEN (1 << 19) /* enable GPIO mode for SCL in HS */
Eric Miao283afa02008-09-08 14:15:08 +0800139
140#define ISR_RWM (1 << 0) /* read/write mode */
141#define ISR_ACKNAK (1 << 1) /* ack/nak status */
142#define ISR_UB (1 << 2) /* unit busy */
143#define ISR_IBB (1 << 3) /* bus busy */
144#define ISR_SSD (1 << 4) /* slave stop detected */
145#define ISR_ALD (1 << 5) /* arbitration loss detected */
146#define ISR_ITE (1 << 6) /* tx buffer empty */
147#define ISR_IRF (1 << 7) /* rx buffer full */
148#define ISR_GCAD (1 << 8) /* general call address detected */
149#define ISR_SAD (1 << 9) /* slave address detected */
150#define ISR_BED (1 << 10) /* bus error no ACK/NAK */
Russell Kingb652b432005-06-15 12:38:14 +0100151
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530152/* bit field shift & mask */
153#define ILCR_SLV_SHIFT 0
154#define ILCR_SLV_MASK (0x1FF << ILCR_SLV_SHIFT)
155#define ILCR_FLV_SHIFT 9
156#define ILCR_FLV_MASK (0x1FF << ILCR_FLV_SHIFT)
157#define ILCR_HLVL_SHIFT 18
158#define ILCR_HLVL_MASK (0x1FF << ILCR_HLVL_SHIFT)
159#define ILCR_HLVH_SHIFT 27
160#define ILCR_HLVH_MASK (0x1F << ILCR_HLVH_SHIFT)
161
162#define IWCR_CNT_SHIFT 0
163#define IWCR_CNT_MASK (0x1F << IWCR_CNT_SHIFT)
164#define IWCR_HS_CNT1_SHIFT 5
165#define IWCR_HS_CNT1_MASK (0x1F << IWCR_HS_CNT1_SHIFT)
166#define IWCR_HS_CNT2_SHIFT 10
167#define IWCR_HS_CNT2_MASK (0x1F << IWCR_HS_CNT2_SHIFT)
168
Russell Kingb652b432005-06-15 12:38:14 +0100169struct pxa_i2c {
170 spinlock_t lock;
171 wait_queue_head_t wait;
172 struct i2c_msg *msg;
173 unsigned int msg_num;
174 unsigned int msg_idx;
175 unsigned int msg_ptr;
176 unsigned int slave_addr;
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530177 unsigned int req_slave_addr;
Russell Kingb652b432005-06-15 12:38:14 +0100178
179 struct i2c_adapter adap;
Russell Kingc3cef3f2007-08-20 10:19:10 +0100180 struct clk *clk;
Russell Kingb652b432005-06-15 12:38:14 +0100181#ifdef CONFIG_I2C_PXA_SLAVE
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500182 struct i2c_client *slave;
Russell Kingb652b432005-06-15 12:38:14 +0100183#endif
184
185 unsigned int irqlogidx;
186 u32 isrlog[32];
187 u32 icrlog[32];
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100188
189 void __iomem *reg_base;
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100190 void __iomem *reg_ibmr;
191 void __iomem *reg_idbr;
192 void __iomem *reg_icr;
193 void __iomem *reg_isr;
194 void __iomem *reg_isar;
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530195 void __iomem *reg_ilcr;
196 void __iomem *reg_iwcr;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100197
198 unsigned long iobase;
199 unsigned long iosize;
200
201 int irq;
Jonathan Cameronc46c9482008-10-03 15:07:36 +0100202 unsigned int use_pio :1;
203 unsigned int fast_mode :1;
Leilei Shang9d3dda52013-06-07 14:38:17 +0800204 unsigned int high_mode:1;
205 unsigned char master_code;
206 unsigned long rate;
207 bool highmode_enter;
Romain Perier6c14bda2016-12-01 12:04:37 +0100208 u32 fm_mask;
209 u32 hs_mask;
Russell Kingb652b432005-06-15 12:38:14 +0100210};
211
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100212#define _IBMR(i2c) ((i2c)->reg_ibmr)
213#define _IDBR(i2c) ((i2c)->reg_idbr)
214#define _ICR(i2c) ((i2c)->reg_icr)
215#define _ISR(i2c) ((i2c)->reg_isr)
216#define _ISAR(i2c) ((i2c)->reg_isar)
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530217#define _ILCR(i2c) ((i2c)->reg_ilcr)
218#define _IWCR(i2c) ((i2c)->reg_iwcr)
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100219
Russell Kingb652b432005-06-15 12:38:14 +0100220/*
221 * I2C Slave mode address
222 */
223#define I2C_PXA_SLAVE_ADDR 0x1
224
Russell Kingb652b432005-06-15 12:38:14 +0100225#ifdef DEBUG
226
227struct bits {
228 u32 mask;
229 const char *set;
230 const char *unset;
231};
Jiri Slabyed113992007-10-18 23:40:28 -0700232#define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
Russell Kingb652b432005-06-15 12:38:14 +0100233
234static inline void
235decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
236{
237 printk("%s %08x: ", prefix, val);
238 while (num--) {
239 const char *str = val & bits->mask ? bits->set : bits->unset;
240 if (str)
241 printk("%s ", str);
242 bits++;
243 }
244}
245
246static const struct bits isr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700247 PXA_BIT(ISR_RWM, "RX", "TX"),
248 PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
249 PXA_BIT(ISR_UB, "Bsy", "Rdy"),
250 PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
251 PXA_BIT(ISR_SSD, "SlaveStop", NULL),
252 PXA_BIT(ISR_ALD, "ALD", NULL),
253 PXA_BIT(ISR_ITE, "TxEmpty", NULL),
254 PXA_BIT(ISR_IRF, "RxFull", NULL),
255 PXA_BIT(ISR_GCAD, "GenCall", NULL),
256 PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
257 PXA_BIT(ISR_BED, "BusErr", NULL),
Russell Kingb652b432005-06-15 12:38:14 +0100258};
259
260static void decode_ISR(unsigned int val)
261{
Russell King6fd60fa2005-09-08 21:04:58 +0100262 decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100263 printk("\n");
264}
265
266static const struct bits icr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700267 PXA_BIT(ICR_START, "START", NULL),
268 PXA_BIT(ICR_STOP, "STOP", NULL),
269 PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
270 PXA_BIT(ICR_TB, "TB", NULL),
271 PXA_BIT(ICR_MA, "MA", NULL),
272 PXA_BIT(ICR_SCLE, "SCLE", "scle"),
273 PXA_BIT(ICR_IUE, "IUE", "iue"),
274 PXA_BIT(ICR_GCD, "GCD", NULL),
275 PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
276 PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
277 PXA_BIT(ICR_BEIE, "BEIE", NULL),
278 PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
279 PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
280 PXA_BIT(ICR_SADIE, "SADIE", NULL),
281 PXA_BIT(ICR_UR, "UR", "ur"),
Russell Kingb652b432005-06-15 12:38:14 +0100282};
283
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100284#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +0100285static void decode_ICR(unsigned int val)
286{
Russell King6fd60fa2005-09-08 21:04:58 +0100287 decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100288 printk("\n");
289}
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100290#endif
Russell Kingb652b432005-06-15 12:38:14 +0100291
292static unsigned int i2c_debug = DEBUG;
293
294static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
295{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100296 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
297 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100298}
299
Harvey Harrison08882d22008-04-22 22:16:47 +0200300#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
Russell Kingb652b432005-06-15 12:38:14 +0100301
302static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
303{
304 unsigned int i;
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530305 struct device *dev = &i2c->adap.dev;
306
307 dev_err(dev, "slave_0x%x error: %s\n",
308 i2c->req_slave_addr >> 1, why);
309 dev_err(dev, "msg_num: %d msg_idx: %d msg_ptr: %d\n",
Russell Kingb652b432005-06-15 12:38:14 +0100310 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530311 dev_err(dev, "IBMR: %08x IDBR: %08x ICR: %08x ISR: %08x\n",
312 readl(_IBMR(i2c)), readl(_IDBR(i2c)), readl(_ICR(i2c)),
313 readl(_ISR(i2c)));
314 dev_dbg(dev, "log: ");
Russell Kingb652b432005-06-15 12:38:14 +0100315 for (i = 0; i < i2c->irqlogidx; i++)
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530316 pr_debug("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
317
318 pr_debug("\n");
Russell Kingb652b432005-06-15 12:38:14 +0100319}
320
Wolfram Sang0d813d92009-11-03 12:53:41 +0100321#else /* ifdef DEBUG */
322
323#define i2c_debug 0
324
325#define show_state(i2c) do { } while (0)
326#define decode_ISR(val) do { } while (0)
327#define decode_ICR(val) do { } while (0)
328#define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
329
330#endif /* ifdef DEBUG / else */
331
332static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
333static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
334
Russell Kingb652b432005-06-15 12:38:14 +0100335static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
336{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100337 return !(readl(_ICR(i2c)) & ICR_SCLE);
Russell Kingb652b432005-06-15 12:38:14 +0100338}
339
340static void i2c_pxa_abort(struct pxa_i2c *i2c)
341{
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100342 int i = 250;
Russell Kingb652b432005-06-15 12:38:14 +0100343
344 if (i2c_pxa_is_slavemode(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100345 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100346 return;
347 }
348
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100349 while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100350 unsigned long icr = readl(_ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100351
352 icr &= ~ICR_START;
353 icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
354
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100355 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100356
357 show_state(i2c);
358
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100359 mdelay(1);
360 i --;
Russell Kingb652b432005-06-15 12:38:14 +0100361 }
362
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100363 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
364 _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100365}
366
367static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
368{
369 int timeout = DEF_TIMEOUT;
370
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100371 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
372 if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
Russell Kingb652b432005-06-15 12:38:14 +0100373 timeout += 4;
374
375 msleep(2);
376 show_state(i2c);
377 }
378
Roel Kluind10db3a2009-04-23 16:27:39 +0200379 if (timeout < 0)
Russell Kingb652b432005-06-15 12:38:14 +0100380 show_state(i2c);
381
Roel Kluind10db3a2009-04-23 16:27:39 +0200382 return timeout < 0 ? I2C_RETRY : 0;
Russell Kingb652b432005-06-15 12:38:14 +0100383}
384
385static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
386{
387 unsigned long timeout = jiffies + HZ*4;
388
389 while (time_before(jiffies, timeout)) {
390 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100391 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100392 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100393
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100394 if (readl(_ISR(i2c)) & ISR_SAD) {
Russell Kingb652b432005-06-15 12:38:14 +0100395 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100396 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100397 goto out;
398 }
399
400 /* wait for unit and bus being not busy, and we also do a
401 * quick check of the i2c lines themselves to ensure they've
402 * gone high...
403 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100404 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
Russell Kingb652b432005-06-15 12:38:14 +0100405 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100406 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100407 return 1;
408 }
409
410 msleep(1);
411 }
412
413 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100414 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100415 out:
416 return 0;
417}
418
419static int i2c_pxa_set_master(struct pxa_i2c *i2c)
420{
421 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +0100422 dev_dbg(&i2c->adap.dev, "setting to bus master\n");
Russell Kingb652b432005-06-15 12:38:14 +0100423
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100424 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100425 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100426 if (!i2c_pxa_wait_master(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100427 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100428 return I2C_RETRY;
429 }
430 }
431
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100432 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100433 return 0;
434}
435
436#ifdef CONFIG_I2C_PXA_SLAVE
437static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
438{
439 unsigned long timeout = jiffies + HZ*1;
440
441 /* wait for stop */
442
443 show_state(i2c);
444
445 while (time_before(jiffies, timeout)) {
446 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100447 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100448 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100449
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100450 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
451 (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
452 (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
Russell Kingb652b432005-06-15 12:38:14 +0100453 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100454 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100455 return 1;
456 }
457
458 msleep(1);
459 }
460
461 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100462 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100463 return 0;
464}
465
466/*
467 * clear the hold on the bus, and take of anything else
468 * that has been configured
469 */
470static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
471{
472 show_state(i2c);
473
474 if (errcode < 0) {
475 udelay(100); /* simple delay */
476 } else {
477 /* we need to wait for the stop condition to end */
478
479 /* if we where in stop, then clear... */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100480 if (readl(_ICR(i2c)) & ICR_STOP) {
Russell Kingb652b432005-06-15 12:38:14 +0100481 udelay(100);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100482 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100483 }
484
485 if (!i2c_pxa_wait_slave(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100486 dev_err(&i2c->adap.dev, "%s: wait timedout\n",
487 __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100488 return;
489 }
490 }
491
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100492 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
493 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100494
495 if (i2c_debug) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100496 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
497 decode_ICR(readl(_ICR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100498 }
499}
500#else
501#define i2c_pxa_set_slave(i2c, err) do { } while (0)
502#endif
503
504static void i2c_pxa_reset(struct pxa_i2c *i2c)
505{
506 pr_debug("Resetting I2C Controller Unit\n");
507
508 /* abort any transfer currently under way */
509 i2c_pxa_abort(i2c);
510
511 /* reset according to 9.8 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100512 writel(ICR_UR, _ICR(i2c));
513 writel(I2C_ISR_INIT, _ISR(i2c));
514 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100515
Vaibhav Hiremathe087b422015-07-14 13:06:41 +0530516 if (i2c->reg_isar && IS_ENABLED(CONFIG_I2C_PXA_SLAVE))
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100517 writel(i2c->slave_addr, _ISAR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100518
519 /* set control register values */
Romain Perier6c14bda2016-12-01 12:04:37 +0100520 writel(I2C_ICR_INIT | (i2c->fast_mode ? i2c->fm_mask : 0), _ICR(i2c));
521 writel(readl(_ICR(i2c)) | (i2c->high_mode ? i2c->hs_mask : 0), _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100522
523#ifdef CONFIG_I2C_PXA_SLAVE
Russell King6fd60fa2005-09-08 21:04:58 +0100524 dev_info(&i2c->adap.dev, "Enabling slave mode\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100525 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100526#endif
527
528 i2c_pxa_set_slave(i2c, 0);
529
530 /* enable unit */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100531 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100532 udelay(100);
533}
534
535
536#ifdef CONFIG_I2C_PXA_SLAVE
537/*
Russell Kingb652b432005-06-15 12:38:14 +0100538 * PXA I2C Slave mode
539 */
540
541static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
542{
543 if (isr & ISR_BED) {
544 /* what should we do here? */
545 } else {
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500546 u8 byte = 0;
Russell King84b5abe2006-10-28 22:30:17 +0100547
548 if (i2c->slave != NULL)
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500549 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_PROCESSED,
550 &byte);
Russell Kingb652b432005-06-15 12:38:14 +0100551
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500552 writel(byte, _IDBR(i2c));
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100553 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
Russell Kingb652b432005-06-15 12:38:14 +0100554 }
555}
556
557static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
558{
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500559 u8 byte = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100560
561 if (i2c->slave != NULL)
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500562 i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_RECEIVED, &byte);
Russell Kingb652b432005-06-15 12:38:14 +0100563
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100564 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100565}
566
567static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
568{
569 int timeout;
570
571 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100572 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
Russell Kingb652b432005-06-15 12:38:14 +0100573 (isr & ISR_RWM) ? 'r' : 't');
574
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500575 if (i2c->slave != NULL) {
576 if (isr & ISR_RWM) {
577 u8 byte = 0;
578
579 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_REQUESTED,
580 &byte);
581 writel(byte, _IDBR(i2c));
582 } else {
583 i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_REQUESTED,
584 NULL);
585 }
586 }
Russell Kingb652b432005-06-15 12:38:14 +0100587
588 /*
589 * slave could interrupt in the middle of us generating a
590 * start condition... if this happens, we'd better back off
591 * and stop holding the poor thing up
592 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100593 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
594 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100595
596 timeout = 0x10000;
597
598 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100599 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100600 break;
601
602 timeout--;
603
604 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100605 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100606 break;
607 }
608 }
609
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100610 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100611}
612
613static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
614{
615 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100616 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
Russell Kingb652b432005-06-15 12:38:14 +0100617
618 if (i2c->slave != NULL)
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500619 i2c_slave_event(i2c->slave, I2C_SLAVE_STOP, NULL);
Russell Kingb652b432005-06-15 12:38:14 +0100620
621 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100622 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
Russell Kingb652b432005-06-15 12:38:14 +0100623
624 /*
625 * If we have a master-mode message waiting,
626 * kick it off now that the slave has completed.
627 */
628 if (i2c->msg)
629 i2c_pxa_master_complete(i2c, I2C_RETRY);
630}
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500631
632static int i2c_pxa_slave_reg(struct i2c_client *slave)
633{
634 struct pxa_i2c *i2c = slave->adapter->algo_data;
635
636 if (i2c->slave)
637 return -EBUSY;
638
639 if (!i2c->reg_isar)
640 return -EAFNOSUPPORT;
641
642 i2c->slave = slave;
643 i2c->slave_addr = slave->addr;
644
645 writel(i2c->slave_addr, _ISAR(i2c));
646
647 return 0;
648}
649
650static int i2c_pxa_slave_unreg(struct i2c_client *slave)
651{
652 struct pxa_i2c *i2c = slave->adapter->algo_data;
653
654 WARN_ON(!i2c->slave);
655
656 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
657 writel(i2c->slave_addr, _ISAR(i2c));
658
659 i2c->slave = NULL;
660
661 return 0;
662}
Russell Kingb652b432005-06-15 12:38:14 +0100663#else
664static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
665{
666 if (isr & ISR_BED) {
667 /* what should we do here? */
668 } else {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100669 writel(0, _IDBR(i2c));
670 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100671 }
672}
673
674static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
675{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100676 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100677}
678
679static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
680{
681 int timeout;
682
683 /*
684 * slave could interrupt in the middle of us generating a
685 * start condition... if this happens, we'd better back off
686 * and stop holding the poor thing up
687 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100688 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
689 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100690
691 timeout = 0x10000;
692
693 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100694 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100695 break;
696
697 timeout--;
698
699 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100700 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100701 break;
702 }
703 }
704
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100705 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100706}
707
708static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
709{
710 if (i2c->msg)
711 i2c_pxa_master_complete(i2c, I2C_RETRY);
712}
713#endif
714
715/*
716 * PXA I2C Master mode
717 */
718
719static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
720{
721 unsigned int addr = (msg->addr & 0x7f) << 1;
722
723 if (msg->flags & I2C_M_RD)
724 addr |= 1;
725
726 return addr;
727}
728
729static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
730{
731 u32 icr;
732
733 /*
734 * Step 1: target slave address into IDBR
735 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100736 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530737 i2c->req_slave_addr = i2c_pxa_addr_byte(i2c->msg);
Russell Kingb652b432005-06-15 12:38:14 +0100738
739 /*
740 * Step 2: initiate the write.
741 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100742 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
743 writel(icr | ICR_START | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100744}
745
Jean Delvare7d054812007-05-01 23:26:33 +0200746static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
747{
748 u32 icr;
749
750 /*
751 * Clear the STOP and ACK flags
752 */
753 icr = readl(_ICR(i2c));
754 icr &= ~(ICR_STOP | ICR_ACKNAK);
Russell King0cfe61e2007-05-10 03:15:32 -0700755 writel(icr, _ICR(i2c));
Jean Delvare7d054812007-05-01 23:26:33 +0200756}
757
Mike Rapoportb7a36702008-01-27 18:14:50 +0100758static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
759{
760 /* make timeout the same as for interrupt based functions */
761 long timeout = 2 * DEF_TIMEOUT;
762
763 /*
764 * Wait for the bus to become free.
765 */
766 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
767 udelay(1000);
768 show_state(i2c);
769 }
770
Roel Kluind10db3a2009-04-23 16:27:39 +0200771 if (timeout < 0) {
Mike Rapoportb7a36702008-01-27 18:14:50 +0100772 show_state(i2c);
773 dev_err(&i2c->adap.dev,
774 "i2c_pxa: timeout waiting for bus free\n");
775 return I2C_RETRY;
776 }
777
778 /*
779 * Set master mode.
780 */
781 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
782
783 return 0;
784}
785
Leilei Shang9d3dda52013-06-07 14:38:17 +0800786/*
787 * PXA I2C send master code
788 * 1. Load master code to IDBR and send it.
789 * Note for HS mode, set ICR [GPIOEN].
790 * 2. Wait until win arbitration.
791 */
792static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c)
793{
794 u32 icr;
795 long timeout;
796
797 spin_lock_irq(&i2c->lock);
798 i2c->highmode_enter = true;
799 writel(i2c->master_code, _IDBR(i2c));
800
801 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
802 icr |= ICR_GPIOEN | ICR_START | ICR_TB | ICR_ITEIE;
803 writel(icr, _ICR(i2c));
804
805 spin_unlock_irq(&i2c->lock);
806 timeout = wait_event_timeout(i2c->wait,
807 i2c->highmode_enter == false, HZ * 1);
808
809 i2c->highmode_enter = false;
810
811 return (timeout == 0) ? I2C_RETRY : 0;
812}
813
Mike Rapoportb7a36702008-01-27 18:14:50 +0100814static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
815 struct i2c_msg *msg, int num)
816{
817 unsigned long timeout = 500000; /* 5 seconds */
818 int ret = 0;
819
820 ret = i2c_pxa_pio_set_master(i2c);
821 if (ret)
822 goto out;
823
824 i2c->msg = msg;
825 i2c->msg_num = num;
826 i2c->msg_idx = 0;
827 i2c->msg_ptr = 0;
828 i2c->irqlogidx = 0;
829
830 i2c_pxa_start_message(i2c);
831
Roel Kluina746b572009-02-24 19:19:48 +0100832 while (i2c->msg_num > 0 && --timeout) {
Mike Rapoportb7a36702008-01-27 18:14:50 +0100833 i2c_pxa_handler(0, i2c);
834 udelay(10);
835 }
836
837 i2c_pxa_stop_message(i2c);
838
839 /*
840 * We place the return code in i2c->msg_idx.
841 */
842 ret = i2c->msg_idx;
843
844out:
Shouming Wang8bd75bd2015-07-14 13:06:42 +0530845 if (timeout == 0) {
Mike Rapoportb7a36702008-01-27 18:14:50 +0100846 i2c_pxa_scream_blue_murder(i2c, "timeout");
Shouming Wang8bd75bd2015-07-14 13:06:42 +0530847 ret = I2C_RETRY;
848 }
Mike Rapoportb7a36702008-01-27 18:14:50 +0100849
850 return ret;
851}
852
Russell Kingb652b432005-06-15 12:38:14 +0100853/*
Jean Delvare3fb9a652006-01-18 23:17:01 +0100854 * We are protected by the adapter bus mutex.
Russell Kingb652b432005-06-15 12:38:14 +0100855 */
856static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
857{
858 long timeout;
859 int ret;
860
861 /*
862 * Wait for the bus to become free.
863 */
864 ret = i2c_pxa_wait_bus_not_busy(i2c);
865 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100866 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
Russell Kingb652b432005-06-15 12:38:14 +0100867 goto out;
868 }
869
870 /*
871 * Set master mode.
872 */
873 ret = i2c_pxa_set_master(i2c);
874 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100875 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
Russell Kingb652b432005-06-15 12:38:14 +0100876 goto out;
877 }
878
Leilei Shang9d3dda52013-06-07 14:38:17 +0800879 if (i2c->high_mode) {
880 ret = i2c_pxa_send_mastercode(i2c);
881 if (ret) {
882 dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
883 goto out;
884 }
885 }
886
Russell Kingb652b432005-06-15 12:38:14 +0100887 spin_lock_irq(&i2c->lock);
888
889 i2c->msg = msg;
890 i2c->msg_num = num;
891 i2c->msg_idx = 0;
892 i2c->msg_ptr = 0;
893 i2c->irqlogidx = 0;
894
895 i2c_pxa_start_message(i2c);
896
897 spin_unlock_irq(&i2c->lock);
898
899 /*
900 * The rest of the processing occurs in the interrupt handler.
901 */
902 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
Jean Delvare7d054812007-05-01 23:26:33 +0200903 i2c_pxa_stop_message(i2c);
Russell Kingb652b432005-06-15 12:38:14 +0100904
905 /*
906 * We place the return code in i2c->msg_idx.
907 */
908 ret = i2c->msg_idx;
909
Sebastian Andrzej Siewior93c92cf2011-02-23 12:38:19 +0100910 if (!timeout && i2c->msg_num) {
Russell Kingb652b432005-06-15 12:38:14 +0100911 i2c_pxa_scream_blue_murder(i2c, "timeout");
Sebastian Andrzej Siewior93c92cf2011-02-23 12:38:19 +0100912 ret = I2C_RETRY;
913 }
Russell Kingb652b432005-06-15 12:38:14 +0100914
915 out:
916 return ret;
917}
918
Mike Rapoportb7a36702008-01-27 18:14:50 +0100919static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
920 struct i2c_msg msgs[], int num)
921{
922 struct pxa_i2c *i2c = adap->algo_data;
923 int ret, i;
924
925 /* If the I2C controller is disabled we need to reset it
926 (probably due to a suspend/resume destroying state). We do
927 this here as we can then avoid worrying about resuming the
928 controller before its users. */
929 if (!(readl(_ICR(i2c)) & ICR_IUE))
930 i2c_pxa_reset(i2c);
931
932 for (i = adap->retries; i >= 0; i--) {
933 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
934 if (ret != I2C_RETRY)
935 goto out;
936
937 if (i2c_debug)
938 dev_dbg(&adap->dev, "Retrying transmission\n");
939 udelay(100);
940 }
941 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
942 ret = -EREMOTEIO;
943 out:
944 i2c_pxa_set_slave(i2c, ret);
945 return ret;
946}
947
Russell Kingb652b432005-06-15 12:38:14 +0100948/*
949 * i2c_pxa_master_complete - complete the message and wake up.
950 */
951static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
952{
953 i2c->msg_ptr = 0;
954 i2c->msg = NULL;
955 i2c->msg_idx ++;
956 i2c->msg_num = 0;
957 if (ret)
958 i2c->msg_idx = ret;
Mike Rapoportb7a36702008-01-27 18:14:50 +0100959 if (!i2c->use_pio)
960 wake_up(&i2c->wait);
Russell Kingb652b432005-06-15 12:38:14 +0100961}
962
963static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
964{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100965 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100966
967 again:
968 /*
969 * If ISR_ALD is set, we lost arbitration.
970 */
971 if (isr & ISR_ALD) {
972 /*
973 * Do we need to do anything here? The PXA docs
974 * are vague about what happens.
975 */
976 i2c_pxa_scream_blue_murder(i2c, "ALD set");
977
978 /*
979 * We ignore this error. We seem to see spurious ALDs
980 * for seemingly no reason. If we handle them as I think
981 * they should, we end up causing an I2C error, which
982 * is painful for some systems.
983 */
984 return; /* ignore */
985 }
986
Petr Cvek86261fd2014-11-25 06:05:33 +0100987 if ((isr & ISR_BED) &&
988 (!((i2c->msg->flags & I2C_M_IGNORE_NAK) &&
989 (isr & ISR_ACKNAK)))) {
Russell Kingb652b432005-06-15 12:38:14 +0100990 int ret = BUS_ERROR;
991
992 /*
993 * I2C bus error - either the device NAK'd us, or
994 * something more serious happened. If we were NAK'd
995 * on the initial address phase, we can retry.
996 */
997 if (isr & ISR_ACKNAK) {
998 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
999 ret = I2C_RETRY;
1000 else
1001 ret = XFER_NAKED;
1002 }
1003 i2c_pxa_master_complete(i2c, ret);
1004 } else if (isr & ISR_RWM) {
1005 /*
1006 * Read mode. We have just sent the address byte, and
1007 * now we must initiate the transfer.
1008 */
1009 if (i2c->msg_ptr == i2c->msg->len - 1 &&
1010 i2c->msg_idx == i2c->msg_num - 1)
1011 icr |= ICR_STOP | ICR_ACKNAK;
1012
1013 icr |= ICR_ALDIE | ICR_TB;
1014 } else if (i2c->msg_ptr < i2c->msg->len) {
1015 /*
1016 * Write mode. Write the next data byte.
1017 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001018 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +01001019
1020 icr |= ICR_ALDIE | ICR_TB;
1021
1022 /*
Petr Cvek86261fd2014-11-25 06:05:33 +01001023 * If this is the last byte of the last message or last byte
1024 * of any message with I2C_M_STOP (e.g. SCCB), send a STOP.
Russell Kingb652b432005-06-15 12:38:14 +01001025 */
Petr Cvek86261fd2014-11-25 06:05:33 +01001026 if ((i2c->msg_ptr == i2c->msg->len) &&
1027 ((i2c->msg->flags & I2C_M_STOP) ||
1028 (i2c->msg_idx == i2c->msg_num - 1)))
1029 icr |= ICR_STOP;
1030
Russell Kingb652b432005-06-15 12:38:14 +01001031 } else if (i2c->msg_idx < i2c->msg_num - 1) {
1032 /*
1033 * Next segment of the message.
1034 */
1035 i2c->msg_ptr = 0;
1036 i2c->msg_idx ++;
1037 i2c->msg++;
1038
1039 /*
1040 * If we aren't doing a repeated start and address,
1041 * go back and try to send the next byte. Note that
1042 * we do not support switching the R/W direction here.
1043 */
1044 if (i2c->msg->flags & I2C_M_NOSTART)
1045 goto again;
1046
1047 /*
1048 * Write the next address.
1049 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001050 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +05301051 i2c->req_slave_addr = i2c_pxa_addr_byte(i2c->msg);
Russell Kingb652b432005-06-15 12:38:14 +01001052
1053 /*
1054 * And trigger a repeated start, and send the byte.
1055 */
1056 icr &= ~ICR_ALDIE;
1057 icr |= ICR_START | ICR_TB;
1058 } else {
1059 if (i2c->msg->len == 0) {
1060 /*
1061 * Device probes have a message length of zero
1062 * and need the bus to be reset before it can
1063 * be used again.
1064 */
1065 i2c_pxa_reset(i2c);
1066 }
1067 i2c_pxa_master_complete(i2c, 0);
1068 }
1069
1070 i2c->icrlog[i2c->irqlogidx-1] = icr;
1071
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001072 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +01001073 show_state(i2c);
1074}
1075
1076static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
1077{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001078 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +01001079
1080 /*
1081 * Read the byte.
1082 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001083 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +01001084
1085 if (i2c->msg_ptr < i2c->msg->len) {
1086 /*
1087 * If this is the last byte of the last
1088 * message, send a STOP.
1089 */
1090 if (i2c->msg_ptr == i2c->msg->len - 1)
1091 icr |= ICR_STOP | ICR_ACKNAK;
1092
1093 icr |= ICR_ALDIE | ICR_TB;
1094 } else {
1095 i2c_pxa_master_complete(i2c, 0);
1096 }
1097
1098 i2c->icrlog[i2c->irqlogidx-1] = icr;
1099
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001100 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +01001101}
1102
Sebastian Andrzej Siewiorc66dc522011-02-23 12:38:18 +01001103#define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
1104 ISR_SAD | ISR_BED)
David Howells7d12e782006-10-05 14:55:46 +01001105static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
Russell Kingb652b432005-06-15 12:38:14 +01001106{
1107 struct pxa_i2c *i2c = dev_id;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001108 u32 isr = readl(_ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +01001109
Vasily Khoruzhick97491ba2011-03-13 15:53:29 +02001110 if (!(isr & VALID_INT_SOURCE))
Sebastian Andrzej Siewiorc66dc522011-02-23 12:38:18 +01001111 return IRQ_NONE;
1112
Russell Kingb652b432005-06-15 12:38:14 +01001113 if (i2c_debug > 2 && 0) {
Russell King6fd60fa2005-09-08 21:04:58 +01001114 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001115 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +01001116 decode_ISR(isr);
1117 }
1118
Tobias Klauser7e3d7db2006-01-09 23:19:51 +01001119 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
Russell Kingb652b432005-06-15 12:38:14 +01001120 i2c->isrlog[i2c->irqlogidx++] = isr;
1121
1122 show_state(i2c);
1123
1124 /*
1125 * Always clear all pending IRQs.
1126 */
Vasily Khoruzhick97491ba2011-03-13 15:53:29 +02001127 writel(isr & VALID_INT_SOURCE, _ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +01001128
1129 if (isr & ISR_SAD)
1130 i2c_pxa_slave_start(i2c, isr);
1131 if (isr & ISR_SSD)
1132 i2c_pxa_slave_stop(i2c);
1133
1134 if (i2c_pxa_is_slavemode(i2c)) {
1135 if (isr & ISR_ITE)
1136 i2c_pxa_slave_txempty(i2c, isr);
1137 if (isr & ISR_IRF)
1138 i2c_pxa_slave_rxfull(i2c, isr);
Leilei Shang9d3dda52013-06-07 14:38:17 +08001139 } else if (i2c->msg && (!i2c->highmode_enter)) {
Russell Kingb652b432005-06-15 12:38:14 +01001140 if (isr & ISR_ITE)
1141 i2c_pxa_irq_txempty(i2c, isr);
1142 if (isr & ISR_IRF)
1143 i2c_pxa_irq_rxfull(i2c, isr);
Leilei Shang9d3dda52013-06-07 14:38:17 +08001144 } else if ((isr & ISR_ITE) && i2c->highmode_enter) {
1145 i2c->highmode_enter = false;
1146 wake_up(&i2c->wait);
Russell Kingb652b432005-06-15 12:38:14 +01001147 } else {
1148 i2c_pxa_scream_blue_murder(i2c, "spurious irq");
1149 }
1150
1151 return IRQ_HANDLED;
1152}
1153
1154
1155static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
1156{
1157 struct pxa_i2c *i2c = adap->algo_data;
1158 int ret, i;
1159
1160 for (i = adap->retries; i >= 0; i--) {
1161 ret = i2c_pxa_do_xfer(i2c, msgs, num);
1162 if (ret != I2C_RETRY)
1163 goto out;
1164
1165 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +01001166 dev_dbg(&adap->dev, "Retrying transmission\n");
Russell Kingb652b432005-06-15 12:38:14 +01001167 udelay(100);
1168 }
1169 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
1170 ret = -EREMOTEIO;
1171 out:
1172 i2c_pxa_set_slave(i2c, ret);
1173 return ret;
1174}
1175
Russell Kingda16e322005-09-14 22:54:45 +01001176static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
1177{
Petr Cvek86261fd2014-11-25 06:05:33 +01001178 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1179 I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
Russell Kingda16e322005-09-14 22:54:45 +01001180}
1181
Jean Delvare8f9082c2006-09-03 22:39:46 +02001182static const struct i2c_algorithm i2c_pxa_algorithm = {
Russell Kingb652b432005-06-15 12:38:14 +01001183 .master_xfer = i2c_pxa_xfer,
Russell Kingda16e322005-09-14 22:54:45 +01001184 .functionality = i2c_pxa_functionality,
Patrick Williams4d51b4c2019-10-01 10:59:59 -05001185#ifdef CONFIG_I2C_PXA_SLAVE
1186 .reg_slave = i2c_pxa_slave_reg,
1187 .unreg_slave = i2c_pxa_slave_unreg,
1188#endif
Russell Kingb652b432005-06-15 12:38:14 +01001189};
1190
Mike Rapoportb7a36702008-01-27 18:14:50 +01001191static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
1192 .master_xfer = i2c_pxa_pio_xfer,
1193 .functionality = i2c_pxa_functionality,
Patrick Williams4d51b4c2019-10-01 10:59:59 -05001194#ifdef CONFIG_I2C_PXA_SLAVE
1195 .reg_slave = i2c_pxa_slave_reg,
1196 .unreg_slave = i2c_pxa_slave_unreg,
1197#endif
Mike Rapoportb7a36702008-01-27 18:14:50 +01001198};
1199
Jingoo Haneae45e52014-05-15 15:46:11 +09001200static const struct of_device_id i2c_pxa_dt_ids[] = {
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001201 { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
1202 { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +05301203 { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA910 },
Romain Perier294be032016-12-01 12:04:38 +01001204 { .compatible = "marvell,armada-3700-i2c", .data = (void *)REGS_A3700 },
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001205 {}
1206};
1207MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
1208
1209static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
1210 enum pxa_i2c_types *i2c_types)
1211{
1212 struct device_node *np = pdev->dev.of_node;
1213 const struct of_device_id *of_id =
1214 of_match_device(i2c_pxa_dt_ids, &pdev->dev);
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001215
1216 if (!of_id)
1217 return 1;
Doug Andersonfe69c552013-03-01 06:57:32 +00001218
1219 /* For device tree we always use the dynamic or alias-assigned ID */
1220 i2c->adap.nr = -1;
1221
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001222 if (of_get_property(np, "mrvl,i2c-polling", NULL))
1223 i2c->use_pio = 1;
1224 if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
1225 i2c->fast_mode = 1;
Yipeng Yaoe2b498f2015-07-14 13:06:43 +05301226
1227 *i2c_types = (enum pxa_i2c_types)(of_id->data);
1228
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001229 return 0;
1230}
1231
1232static int i2c_pxa_probe_pdata(struct platform_device *pdev,
1233 struct pxa_i2c *i2c,
1234 enum pxa_i2c_types *i2c_types)
1235{
Jingoo Han6d4028c2013-07-30 16:59:33 +09001236 struct i2c_pxa_platform_data *plat = dev_get_platdata(&pdev->dev);
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001237 const struct platform_device_id *id = platform_get_device_id(pdev);
1238
1239 *i2c_types = id->driver_data;
1240 if (plat) {
1241 i2c->use_pio = plat->use_pio;
1242 i2c->fast_mode = plat->fast_mode;
Leilei Shang9d3dda52013-06-07 14:38:17 +08001243 i2c->high_mode = plat->high_mode;
1244 i2c->master_code = plat->master_code;
1245 if (!i2c->master_code)
1246 i2c->master_code = 0xe;
1247 i2c->rate = plat->rate;
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001248 }
1249 return 0;
1250}
1251
Russell King3ae5eae2005-11-09 22:32:44 +00001252static int i2c_pxa_probe(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001253{
Jingoo Han6d4028c2013-07-30 16:59:33 +09001254 struct i2c_pxa_platform_data *plat = dev_get_platdata(&dev->dev);
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001255 enum pxa_i2c_types i2c_type;
1256 struct pxa_i2c *i2c;
1257 struct resource *res = NULL;
1258 int ret, irq;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001259
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301260 i2c = devm_kzalloc(&dev->dev, sizeof(struct pxa_i2c), GFP_KERNEL);
1261 if (!i2c)
1262 return -ENOMEM;
1263
1264 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1265 i2c->reg_base = devm_ioremap_resource(&dev->dev, res);
1266 if (IS_ERR(i2c->reg_base))
1267 return PTR_ERR(i2c->reg_base);
1268
1269 irq = platform_get_irq(dev, 0);
1270 if (irq < 0) {
1271 dev_err(&dev->dev, "no irq resource: %d\n", irq);
1272 return irq;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001273 }
1274
Doug Andersonfe69c552013-03-01 06:57:32 +00001275 /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
1276 i2c->adap.nr = dev->id;
1277
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001278 ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
1279 if (ret > 0)
1280 ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
1281 if (ret < 0)
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301282 return ret;
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001283
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001284 i2c->adap.owner = THIS_MODULE;
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001285 i2c->adap.retries = 5;
1286
1287 spin_lock_init(&i2c->lock);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001288 init_waitqueue_head(&i2c->wait);
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001289
Doug Andersonfe69c552013-03-01 06:57:32 +00001290 strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001291
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301292 i2c->clk = devm_clk_get(&dev->dev, NULL);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001293 if (IS_ERR(i2c->clk)) {
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301294 dev_err(&dev->dev, "failed to get the clk: %ld\n", PTR_ERR(i2c->clk));
1295 return PTR_ERR(i2c->clk);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001296 }
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +01001297
1298 i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
1299 i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
1300 i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
1301 i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
Romain Perier6c14bda2016-12-01 12:04:37 +01001302 i2c->fm_mask = pxa_reg_layout[i2c_type].fm ? : ICR_FM;
1303 i2c->hs_mask = pxa_reg_layout[i2c_type].hs ? : ICR_HS;
1304
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +01001305 if (i2c_type != REGS_CE4100)
1306 i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001307
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +05301308 if (i2c_type == REGS_PXA910) {
1309 i2c->reg_ilcr = i2c->reg_base + pxa_reg_layout[i2c_type].ilcr;
1310 i2c->reg_iwcr = i2c->reg_base + pxa_reg_layout[i2c_type].iwcr;
1311 }
1312
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001313 i2c->iobase = res->start;
Linus Walleijc6ffdde2009-06-14 00:20:36 +02001314 i2c->iosize = resource_size(res);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001315
1316 i2c->irq = irq;
Russell Kingb652b432005-06-15 12:38:14 +01001317
1318 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
Leilei Shang9d3dda52013-06-07 14:38:17 +08001319 i2c->highmode_enter = false;
Russell Kingb652b432005-06-15 12:38:14 +01001320
Russell Kingb652b432005-06-15 12:38:14 +01001321 if (plat) {
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001322 i2c->adap.class = plat->class;
1323 }
Russell Kingb652b432005-06-15 12:38:14 +01001324
Leilei Shang9d3dda52013-06-07 14:38:17 +08001325 if (i2c->high_mode) {
1326 if (i2c->rate) {
1327 clk_set_rate(i2c->clk, i2c->rate);
1328 pr_info("i2c: <%s> set rate to %ld\n",
1329 i2c->adap.name, clk_get_rate(i2c->clk));
1330 } else
1331 pr_warn("i2c: <%s> clock rate not set\n",
1332 i2c->adap.name);
1333 }
1334
Daniel Drake7a10f472013-06-17 11:30:36 -04001335 clk_prepare_enable(i2c->clk);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001336
Mike Rapoportb7a36702008-01-27 18:14:50 +01001337 if (i2c->use_pio) {
1338 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1339 } else {
1340 i2c->adap.algo = &i2c_pxa_algorithm;
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301341 ret = devm_request_irq(&dev->dev, irq, i2c_pxa_handler,
Leilei Shangabf8a1f2015-07-14 13:06:40 +05301342 IRQF_SHARED | IRQF_NO_SUSPEND,
1343 dev_name(&dev->dev), i2c);
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301344 if (ret) {
1345 dev_err(&dev->dev, "failed to request irq: %d\n", ret);
Mike Rapoportb7a36702008-01-27 18:14:50 +01001346 goto ereqirq;
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301347 }
Mike Rapoportb7a36702008-01-27 18:14:50 +01001348 }
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001349
Russell Kingb652b432005-06-15 12:38:14 +01001350 i2c_pxa_reset(i2c);
1351
1352 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001353 i2c->adap.dev.parent = &dev->dev;
Sebastian Andrzej Siewiorbaa8cab2011-02-23 12:38:20 +01001354#ifdef CONFIG_OF
1355 i2c->adap.dev.of_node = dev->dev.of_node;
1356#endif
Russell Kingb652b432005-06-15 12:38:14 +01001357
Grant Likely488bf312011-07-25 17:49:43 +02001358 ret = i2c_add_numbered_adapter(&i2c->adap);
Wolfram Sangea734402016-08-09 13:36:17 +02001359 if (ret < 0)
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301360 goto ereqirq;
Russell Kingb652b432005-06-15 12:38:14 +01001361
Russell King3ae5eae2005-11-09 22:32:44 +00001362 platform_set_drvdata(dev, i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001363
1364#ifdef CONFIG_I2C_PXA_SLAVE
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301365 dev_info(&i2c->adap.dev, " PXA I2C adapter, slave address %d\n",
1366 i2c->slave_addr);
Russell Kingb652b432005-06-15 12:38:14 +01001367#else
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301368 dev_info(&i2c->adap.dev, " PXA I2C adapter\n");
Russell Kingb652b432005-06-15 12:38:14 +01001369#endif
1370 return 0;
1371
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001372ereqirq:
Daniel Drake7a10f472013-06-17 11:30:36 -04001373 clk_disable_unprepare(i2c->clk);
Russell Kingb652b432005-06-15 12:38:14 +01001374 return ret;
1375}
1376
Dmitry Torokhov0a6d2242013-02-19 22:50:10 +00001377static int i2c_pxa_remove(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001378{
Russell King3ae5eae2005-11-09 22:32:44 +00001379 struct pxa_i2c *i2c = platform_get_drvdata(dev);
Russell Kingb652b432005-06-15 12:38:14 +01001380
Russell Kingb652b432005-06-15 12:38:14 +01001381 i2c_del_adapter(&i2c->adap);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001382
Daniel Drake7a10f472013-06-17 11:30:36 -04001383 clk_disable_unprepare(i2c->clk);
Russell Kingb652b432005-06-15 12:38:14 +01001384
1385 return 0;
1386}
1387
Russell Kinge7d48fa2008-08-26 10:40:50 +01001388#ifdef CONFIG_PM
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001389static int i2c_pxa_suspend_noirq(struct device *dev)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001390{
Masahiro Yamada9242e722017-07-28 01:16:24 +09001391 struct pxa_i2c *i2c = dev_get_drvdata(dev);
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001392
Russell Kinge7d48fa2008-08-26 10:40:50 +01001393 clk_disable(i2c->clk);
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001394
Russell Kinge7d48fa2008-08-26 10:40:50 +01001395 return 0;
1396}
1397
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001398static int i2c_pxa_resume_noirq(struct device *dev)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001399{
Masahiro Yamada9242e722017-07-28 01:16:24 +09001400 struct pxa_i2c *i2c = dev_get_drvdata(dev);
Russell Kinge7d48fa2008-08-26 10:40:50 +01001401
1402 clk_enable(i2c->clk);
1403 i2c_pxa_reset(i2c);
1404
1405 return 0;
1406}
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001407
Alexey Dobriyan47145212009-12-14 18:00:08 -08001408static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001409 .suspend_noirq = i2c_pxa_suspend_noirq,
1410 .resume_noirq = i2c_pxa_resume_noirq,
1411};
1412
1413#define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001414#else
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001415#define I2C_PXA_DEV_PM_OPS NULL
Russell Kinge7d48fa2008-08-26 10:40:50 +01001416#endif
1417
Russell King3ae5eae2005-11-09 22:32:44 +00001418static struct platform_driver i2c_pxa_driver = {
Russell Kingb652b432005-06-15 12:38:14 +01001419 .probe = i2c_pxa_probe,
Dmitry Torokhov0a6d2242013-02-19 22:50:10 +00001420 .remove = i2c_pxa_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001421 .driver = {
1422 .name = "pxa2xx-i2c",
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001423 .pm = I2C_PXA_DEV_PM_OPS,
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001424 .of_match_table = i2c_pxa_dt_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001425 },
Eric Miaof23d4912009-04-13 14:43:25 +08001426 .id_table = i2c_pxa_id_table,
Russell Kingb652b432005-06-15 12:38:14 +01001427};
1428
1429static int __init i2c_adap_pxa_init(void)
1430{
Russell King3ae5eae2005-11-09 22:32:44 +00001431 return platform_driver_register(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001432}
1433
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001434static void __exit i2c_adap_pxa_exit(void)
Russell Kingb652b432005-06-15 12:38:14 +01001435{
Holger Schurigd6a7b5f2008-02-11 16:51:41 +01001436 platform_driver_unregister(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001437}
1438
Richard Purdieece5f7b2006-01-12 16:30:23 +00001439MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +02001440MODULE_ALIAS("platform:pxa2xx-i2c");
Richard Purdieece5f7b2006-01-12 16:30:23 +00001441
Uli Luckas47a9b132008-07-14 22:38:30 +02001442subsys_initcall(i2c_adap_pxa_init);
Russell Kingb652b432005-06-15 12:38:14 +01001443module_exit(i2c_adap_pxa_exit);