blob: c1e50c0b9756befa3cdfae04f0b9d55fca344c4a [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 */
Russell King8de32da2020-04-27 19:48:46 +010019#include <linux/clk.h>
20#include <linux/delay.h>
21#include <linux/err.h>
22#include <linux/errno.h>
Russell Kingb652b432005-06-15 12:38:14 +010023#include <linux/i2c.h>
Russell Kingb652b432005-06-15 12:38:14 +010024#include <linux/init.h>
Russell Kingb652b432005-06-15 12:38:14 +010025#include <linux/interrupt.h>
Russell King8de32da2020-04-27 19:48:46 +010026#include <linux/io.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
Haojian Zhuang63fe1222012-03-01 13:04:44 +080029#include <linux/of.h>
30#include <linux/of_device.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010031#include <linux/platform_device.h>
Wolfram Sangf15fc9b2017-11-13 18:27:39 +010032#include <linux/platform_data/i2c-pxa.h>
Russell King8de32da2020-04-27 19:48:46 +010033#include <linux/slab.h>
Russell Kingb652b432005-06-15 12:38:14 +010034
Russell King940695a2020-04-27 19:48:56 +010035/* I2C register field definitions */
Russell Kingf8e5d3c2020-04-27 19:49:01 +010036#define IBMR_SDAS (1 << 0)
37#define IBMR_SCLS (1 << 1)
38
Russell King940695a2020-04-27 19:48:56 +010039#define ICR_START (1 << 0) /* start bit */
40#define ICR_STOP (1 << 1) /* stop bit */
41#define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */
42#define ICR_TB (1 << 3) /* transfer byte bit */
43#define ICR_MA (1 << 4) /* master abort */
44#define ICR_SCLE (1 << 5) /* master clock enable */
45#define ICR_IUE (1 << 6) /* unit enable */
46#define ICR_GCD (1 << 7) /* general call disable */
47#define ICR_ITEIE (1 << 8) /* enable tx interrupts */
48#define ICR_IRFIE (1 << 9) /* enable rx interrupts */
49#define ICR_BEIE (1 << 10) /* enable bus error ints */
50#define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */
51#define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */
52#define ICR_SADIE (1 << 13) /* slave address detected int enable */
53#define ICR_UR (1 << 14) /* unit reset */
54#define ICR_FM (1 << 15) /* fast mode */
55#define ICR_HS (1 << 16) /* High Speed mode */
56#define ICR_A3700_FM (1 << 16) /* fast mode for armada-3700 */
57#define ICR_A3700_HS (1 << 17) /* high speed mode for armada-3700 */
58#define ICR_GPIOEN (1 << 19) /* enable GPIO mode for SCL in HS */
59
60#define ISR_RWM (1 << 0) /* read/write mode */
61#define ISR_ACKNAK (1 << 1) /* ack/nak status */
62#define ISR_UB (1 << 2) /* unit busy */
63#define ISR_IBB (1 << 3) /* bus busy */
64#define ISR_SSD (1 << 4) /* slave stop detected */
65#define ISR_ALD (1 << 5) /* arbitration loss detected */
66#define ISR_ITE (1 << 6) /* tx buffer empty */
67#define ISR_IRF (1 << 7) /* rx buffer full */
68#define ISR_GCAD (1 << 8) /* general call address detected */
69#define ISR_SAD (1 << 9) /* slave address detected */
70#define ISR_BED (1 << 10) /* bus error no ACK/NAK */
71
72#define ILCR_SLV_SHIFT 0
73#define ILCR_SLV_MASK (0x1FF << ILCR_SLV_SHIFT)
74#define ILCR_FLV_SHIFT 9
75#define ILCR_FLV_MASK (0x1FF << ILCR_FLV_SHIFT)
76#define ILCR_HLVL_SHIFT 18
77#define ILCR_HLVL_MASK (0x1FF << ILCR_HLVL_SHIFT)
78#define ILCR_HLVH_SHIFT 27
79#define ILCR_HLVH_MASK (0x1F << ILCR_HLVH_SHIFT)
80
81#define IWCR_CNT_SHIFT 0
82#define IWCR_CNT_MASK (0x1F << IWCR_CNT_SHIFT)
83#define IWCR_HS_CNT1_SHIFT 5
84#define IWCR_HS_CNT1_MASK (0x1F << IWCR_HS_CNT1_SHIFT)
85#define IWCR_HS_CNT2_SHIFT 10
86#define IWCR_HS_CNT2_MASK (0x1F << IWCR_HS_CNT2_SHIFT)
87
Russell King79622f32020-04-27 19:49:12 +010088/* need a longer timeout if we're dealing with the fact we may well be
89 * looking at a multi-master environment
90 */
91#define DEF_TIMEOUT 32
92
93#define BUS_ERROR (-EREMOTEIO)
94#define XFER_NAKED (-ECONNREFUSED)
95#define I2C_RETRY (-2000) /* an error has occurred retry transmit */
96
97/* ICR initialize bit values
98 *
99 * 15 FM 0 (100 kHz operation)
100 * 14 UR 0 (No unit reset)
101 * 13 SADIE 0 (Disables the unit from interrupting on slave addresses
102 * matching its slave address)
103 * 12 ALDIE 0 (Disables the unit from interrupt when it loses arbitration
104 * in master mode)
105 * 11 SSDIE 0 (Disables interrupts from a slave stop detected, in slave mode)
106 * 10 BEIE 1 (Enable interrupts from detected bus errors, no ACK sent)
107 * 9 IRFIE 1 (Enable interrupts from full buffer received)
108 * 8 ITEIE 1 (Enables the I2C unit to interrupt when transmit buffer empty)
109 * 7 GCD 1 (Disables i2c unit response to general call messages as a slave)
110 * 6 IUE 0 (Disable unit until we change settings)
111 * 5 SCLE 1 (Enables the i2c clock output for master mode (drives SCL)
112 * 4 MA 0 (Only send stop with the ICR stop bit)
113 * 3 TB 0 (We are not transmitting a byte initially)
114 * 2 ACKNAK 0 (Send an ACK after the unit receives a byte)
115 * 1 STOP 0 (Do not send a STOP)
116 * 0 START 0 (Do not send a START)
117 */
118#define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
119
120/* I2C status register init values
121 *
122 * 10 BED 1 (Clear bus error detected)
123 * 9 SAD 1 (Clear slave address detected)
124 * 7 IRF 1 (Clear IDBR Receive Full)
125 * 6 ITE 1 (Clear IDBR Transmit Empty)
126 * 5 ALD 1 (Clear Arbitration Loss Detected)
127 * 4 SSD 1 (Clear Slave Stop Detected)
128 */
129#define I2C_ISR_INIT 0x7FF /* status register init */
130
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100131struct pxa_reg_layout {
132 u32 ibmr;
133 u32 idbr;
134 u32 icr;
135 u32 isr;
136 u32 isar;
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530137 u32 ilcr;
138 u32 iwcr;
Romain Perier6c14bda2016-12-01 12:04:37 +0100139 u32 fm;
140 u32 hs;
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100141};
142
143enum pxa_i2c_types {
144 REGS_PXA2XX,
145 REGS_PXA3XX,
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100146 REGS_CE4100,
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530147 REGS_PXA910,
Romain Perier294be032016-12-01 12:04:38 +0100148 REGS_A3700,
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100149};
150
Russell King940695a2020-04-27 19:48:56 +0100151/* I2C register layout definitions */
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100152static struct pxa_reg_layout pxa_reg_layout[] = {
153 [REGS_PXA2XX] = {
154 .ibmr = 0x00,
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100155 .idbr = 0x08,
156 .icr = 0x10,
157 .isr = 0x18,
158 .isar = 0x20,
Russell Kingee478932020-04-27 19:49:07 +0100159 .fm = ICR_FM,
160 .hs = ICR_HS,
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100161 },
Vasily Khoruzhick23e74a82011-03-13 15:53:28 +0200162 [REGS_PXA3XX] = {
163 .ibmr = 0x00,
164 .idbr = 0x04,
165 .icr = 0x08,
166 .isr = 0x0c,
167 .isar = 0x10,
Russell Kingee478932020-04-27 19:49:07 +0100168 .fm = ICR_FM,
169 .hs = ICR_HS,
Vasily Khoruzhick23e74a82011-03-13 15:53:28 +0200170 },
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100171 [REGS_CE4100] = {
172 .ibmr = 0x14,
173 .idbr = 0x0c,
174 .icr = 0x00,
175 .isr = 0x04,
176 /* no isar register */
Russell Kingee478932020-04-27 19:49:07 +0100177 .fm = ICR_FM,
178 .hs = ICR_HS,
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100179 },
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530180 [REGS_PXA910] = {
181 .ibmr = 0x00,
182 .idbr = 0x08,
183 .icr = 0x10,
184 .isr = 0x18,
185 .isar = 0x20,
186 .ilcr = 0x28,
187 .iwcr = 0x30,
Russell Kingee478932020-04-27 19:49:07 +0100188 .fm = ICR_FM,
189 .hs = ICR_HS,
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530190 },
Romain Perier294be032016-12-01 12:04:38 +0100191 [REGS_A3700] = {
192 .ibmr = 0x00,
193 .idbr = 0x04,
194 .icr = 0x08,
195 .isr = 0x0c,
196 .isar = 0x10,
Russell King940695a2020-04-27 19:48:56 +0100197 .fm = ICR_A3700_FM,
198 .hs = ICR_A3700_HS,
Romain Perier294be032016-12-01 12:04:38 +0100199 },
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100200};
Eric Miaof23d4912009-04-13 14:43:25 +0800201
Russell King70aee282020-04-27 19:49:17 +0100202static const struct of_device_id i2c_pxa_dt_ids[] = {
203 { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
204 { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
205 { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA910 },
206 { .compatible = "marvell,armada-3700-i2c", .data = (void *)REGS_A3700 },
207 {}
208};
209MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
210
Eric Miaof23d4912009-04-13 14:43:25 +0800211static const struct platform_device_id i2c_pxa_id_table[] = {
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100212 { "pxa2xx-i2c", REGS_PXA2XX },
213 { "pxa3xx-pwri2c", REGS_PXA3XX },
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100214 { "ce4100-i2c", REGS_CE4100 },
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530215 { "pxa910-i2c", REGS_PXA910 },
Romain Perier294be032016-12-01 12:04:38 +0100216 { "armada-3700-i2c", REGS_A3700 },
Eric Miaof23d4912009-04-13 14:43:25 +0800217 { },
218};
219MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table);
220
Russell Kingb652b432005-06-15 12:38:14 +0100221struct pxa_i2c {
222 spinlock_t lock;
223 wait_queue_head_t wait;
224 struct i2c_msg *msg;
225 unsigned int msg_num;
226 unsigned int msg_idx;
227 unsigned int msg_ptr;
228 unsigned int slave_addr;
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530229 unsigned int req_slave_addr;
Russell Kingb652b432005-06-15 12:38:14 +0100230
231 struct i2c_adapter adap;
Russell Kingc3cef3f2007-08-20 10:19:10 +0100232 struct clk *clk;
Russell Kingb652b432005-06-15 12:38:14 +0100233#ifdef CONFIG_I2C_PXA_SLAVE
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500234 struct i2c_client *slave;
Russell Kingb652b432005-06-15 12:38:14 +0100235#endif
236
237 unsigned int irqlogidx;
238 u32 isrlog[32];
239 u32 icrlog[32];
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100240
241 void __iomem *reg_base;
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100242 void __iomem *reg_ibmr;
243 void __iomem *reg_idbr;
244 void __iomem *reg_icr;
245 void __iomem *reg_isr;
246 void __iomem *reg_isar;
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530247 void __iomem *reg_ilcr;
248 void __iomem *reg_iwcr;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100249
250 unsigned long iobase;
251 unsigned long iosize;
252
253 int irq;
Jonathan Cameronc46c9482008-10-03 15:07:36 +0100254 unsigned int use_pio :1;
255 unsigned int fast_mode :1;
Leilei Shang9d3dda52013-06-07 14:38:17 +0800256 unsigned int high_mode:1;
257 unsigned char master_code;
258 unsigned long rate;
259 bool highmode_enter;
Romain Perier6c14bda2016-12-01 12:04:37 +0100260 u32 fm_mask;
261 u32 hs_mask;
Russell Kingb652b432005-06-15 12:38:14 +0100262};
263
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100264#define _IBMR(i2c) ((i2c)->reg_ibmr)
265#define _IDBR(i2c) ((i2c)->reg_idbr)
266#define _ICR(i2c) ((i2c)->reg_icr)
267#define _ISR(i2c) ((i2c)->reg_isr)
268#define _ISAR(i2c) ((i2c)->reg_isar)
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +0530269#define _ILCR(i2c) ((i2c)->reg_ilcr)
270#define _IWCR(i2c) ((i2c)->reg_iwcr)
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100271
Russell Kingb652b432005-06-15 12:38:14 +0100272/*
273 * I2C Slave mode address
274 */
275#define I2C_PXA_SLAVE_ADDR 0x1
276
Russell Kingb652b432005-06-15 12:38:14 +0100277#ifdef DEBUG
278
279struct bits {
280 u32 mask;
281 const char *set;
282 const char *unset;
283};
Jiri Slabyed113992007-10-18 23:40:28 -0700284#define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
Russell Kingb652b432005-06-15 12:38:14 +0100285
286static inline void
287decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
288{
Russell Kingbb82ba62020-04-27 19:49:27 +0100289 printk("%s %08x:", prefix, val);
Russell Kingb652b432005-06-15 12:38:14 +0100290 while (num--) {
291 const char *str = val & bits->mask ? bits->set : bits->unset;
292 if (str)
Russell Kingbb82ba62020-04-27 19:49:27 +0100293 pr_cont(" %s", str);
Russell Kingb652b432005-06-15 12:38:14 +0100294 bits++;
295 }
Russell Kingbb82ba62020-04-27 19:49:27 +0100296 pr_cont("\n");
Russell Kingb652b432005-06-15 12:38:14 +0100297}
298
299static const struct bits isr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700300 PXA_BIT(ISR_RWM, "RX", "TX"),
301 PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
302 PXA_BIT(ISR_UB, "Bsy", "Rdy"),
303 PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
304 PXA_BIT(ISR_SSD, "SlaveStop", NULL),
305 PXA_BIT(ISR_ALD, "ALD", NULL),
306 PXA_BIT(ISR_ITE, "TxEmpty", NULL),
307 PXA_BIT(ISR_IRF, "RxFull", NULL),
308 PXA_BIT(ISR_GCAD, "GenCall", NULL),
309 PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
310 PXA_BIT(ISR_BED, "BusErr", NULL),
Russell Kingb652b432005-06-15 12:38:14 +0100311};
312
313static void decode_ISR(unsigned int val)
314{
Russell King6fd60fa2005-09-08 21:04:58 +0100315 decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100316}
317
318static const struct bits icr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700319 PXA_BIT(ICR_START, "START", NULL),
320 PXA_BIT(ICR_STOP, "STOP", NULL),
321 PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
322 PXA_BIT(ICR_TB, "TB", NULL),
323 PXA_BIT(ICR_MA, "MA", NULL),
324 PXA_BIT(ICR_SCLE, "SCLE", "scle"),
325 PXA_BIT(ICR_IUE, "IUE", "iue"),
326 PXA_BIT(ICR_GCD, "GCD", NULL),
327 PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
328 PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
329 PXA_BIT(ICR_BEIE, "BEIE", NULL),
330 PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
331 PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
332 PXA_BIT(ICR_SADIE, "SADIE", NULL),
333 PXA_BIT(ICR_UR, "UR", "ur"),
Russell Kingb652b432005-06-15 12:38:14 +0100334};
335
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100336#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +0100337static void decode_ICR(unsigned int val)
338{
Russell King6fd60fa2005-09-08 21:04:58 +0100339 decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100340}
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100341#endif
Russell Kingb652b432005-06-15 12:38:14 +0100342
343static unsigned int i2c_debug = DEBUG;
344
345static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
346{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100347 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
348 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100349}
350
Harvey Harrison08882d22008-04-22 22:16:47 +0200351#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
Russell Kingb652b432005-06-15 12:38:14 +0100352
353static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
354{
355 unsigned int i;
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530356 struct device *dev = &i2c->adap.dev;
357
358 dev_err(dev, "slave_0x%x error: %s\n",
359 i2c->req_slave_addr >> 1, why);
360 dev_err(dev, "msg_num: %d msg_idx: %d msg_ptr: %d\n",
Russell Kingb652b432005-06-15 12:38:14 +0100361 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
Vaibhav Hiremath3a2dc162015-07-14 13:06:44 +0530362 dev_err(dev, "IBMR: %08x IDBR: %08x ICR: %08x ISR: %08x\n",
363 readl(_IBMR(i2c)), readl(_IDBR(i2c)), readl(_ICR(i2c)),
364 readl(_ISR(i2c)));
Russell King88b73ee2020-04-27 19:49:22 +0100365 dev_err(dev, "log:");
Russell Kingb652b432005-06-15 12:38:14 +0100366 for (i = 0; i < i2c->irqlogidx; i++)
Russell King88b73ee2020-04-27 19:49:22 +0100367 pr_cont(" [%03x:%05x]", i2c->isrlog[i], i2c->icrlog[i]);
368 pr_cont("\n");
Russell Kingb652b432005-06-15 12:38:14 +0100369}
370
Wolfram Sang0d813d92009-11-03 12:53:41 +0100371#else /* ifdef DEBUG */
372
373#define i2c_debug 0
374
375#define show_state(i2c) do { } while (0)
376#define decode_ISR(val) do { } while (0)
377#define decode_ICR(val) do { } while (0)
378#define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
379
380#endif /* ifdef DEBUG / else */
381
382static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
Wolfram Sang0d813d92009-11-03 12:53:41 +0100383
Russell Kingb652b432005-06-15 12:38:14 +0100384static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
385{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100386 return !(readl(_ICR(i2c)) & ICR_SCLE);
Russell Kingb652b432005-06-15 12:38:14 +0100387}
388
389static void i2c_pxa_abort(struct pxa_i2c *i2c)
390{
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100391 int i = 250;
Russell Kingb652b432005-06-15 12:38:14 +0100392
393 if (i2c_pxa_is_slavemode(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100394 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100395 return;
396 }
397
Russell Kingf8e5d3c2020-04-27 19:49:01 +0100398 while ((i > 0) && (readl(_IBMR(i2c)) & IBMR_SDAS) == 0) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100399 unsigned long icr = readl(_ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100400
401 icr &= ~ICR_START;
402 icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
403
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100404 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100405
406 show_state(i2c);
407
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100408 mdelay(1);
409 i --;
Russell Kingb652b432005-06-15 12:38:14 +0100410 }
411
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100412 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
413 _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100414}
415
416static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
417{
418 int timeout = DEF_TIMEOUT;
Russell Kinge896be52020-04-27 19:49:32 +0100419 u32 isr;
Russell Kingb652b432005-06-15 12:38:14 +0100420
Russell Kinge896be52020-04-27 19:49:32 +0100421 while (1) {
422 isr = readl(_ISR(i2c));
423 if (!(isr & (ISR_IBB | ISR_UB)))
424 return 0;
425
426 if (isr & ISR_SAD)
Russell Kingb652b432005-06-15 12:38:14 +0100427 timeout += 4;
428
Russell Kinge896be52020-04-27 19:49:32 +0100429 if (!timeout--)
430 break;
431
Russell Kingb652b432005-06-15 12:38:14 +0100432 msleep(2);
433 show_state(i2c);
434 }
435
Russell Kinge896be52020-04-27 19:49:32 +0100436 show_state(i2c);
Russell Kingb652b432005-06-15 12:38:14 +0100437
Russell Kinge896be52020-04-27 19:49:32 +0100438 return I2C_RETRY;
Russell Kingb652b432005-06-15 12:38:14 +0100439}
440
441static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
442{
443 unsigned long timeout = jiffies + HZ*4;
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_SAD) {
Russell Kingb652b432005-06-15 12:38:14 +0100451 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100452 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100453 goto out;
454 }
455
456 /* wait for unit and bus being not busy, and we also do a
457 * quick check of the i2c lines themselves to ensure they've
458 * gone high...
459 */
Russell Kingf8e5d3c2020-04-27 19:49:01 +0100460 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 &&
461 readl(_IBMR(i2c)) == (IBMR_SCLS | IBMR_SDAS)) {
Russell Kingb652b432005-06-15 12:38:14 +0100462 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100463 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100464 return 1;
465 }
466
467 msleep(1);
468 }
469
470 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100471 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100472 out:
473 return 0;
474}
475
476static int i2c_pxa_set_master(struct pxa_i2c *i2c)
477{
478 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +0100479 dev_dbg(&i2c->adap.dev, "setting to bus master\n");
Russell Kingb652b432005-06-15 12:38:14 +0100480
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100481 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100482 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100483 if (!i2c_pxa_wait_master(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100484 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100485 return I2C_RETRY;
486 }
487 }
488
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100489 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100490 return 0;
491}
492
493#ifdef CONFIG_I2C_PXA_SLAVE
494static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
495{
496 unsigned long timeout = jiffies + HZ*1;
497
498 /* wait for stop */
499
500 show_state(i2c);
501
502 while (time_before(jiffies, timeout)) {
503 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100504 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100505 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100506
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100507 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
508 (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
509 (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
Russell Kingb652b432005-06-15 12:38:14 +0100510 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100511 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100512 return 1;
513 }
514
515 msleep(1);
516 }
517
518 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100519 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100520 return 0;
521}
522
523/*
524 * clear the hold on the bus, and take of anything else
525 * that has been configured
526 */
527static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
528{
529 show_state(i2c);
530
531 if (errcode < 0) {
532 udelay(100); /* simple delay */
533 } else {
534 /* we need to wait for the stop condition to end */
535
536 /* if we where in stop, then clear... */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100537 if (readl(_ICR(i2c)) & ICR_STOP) {
Russell Kingb652b432005-06-15 12:38:14 +0100538 udelay(100);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100539 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100540 }
541
542 if (!i2c_pxa_wait_slave(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100543 dev_err(&i2c->adap.dev, "%s: wait timedout\n",
544 __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100545 return;
546 }
547 }
548
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100549 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
550 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100551
552 if (i2c_debug) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100553 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
554 decode_ICR(readl(_ICR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100555 }
556}
557#else
558#define i2c_pxa_set_slave(i2c, err) do { } while (0)
559#endif
560
561static void i2c_pxa_reset(struct pxa_i2c *i2c)
562{
563 pr_debug("Resetting I2C Controller Unit\n");
564
565 /* abort any transfer currently under way */
566 i2c_pxa_abort(i2c);
567
568 /* reset according to 9.8 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100569 writel(ICR_UR, _ICR(i2c));
570 writel(I2C_ISR_INIT, _ISR(i2c));
571 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100572
Vaibhav Hiremathe087b422015-07-14 13:06:41 +0530573 if (i2c->reg_isar && IS_ENABLED(CONFIG_I2C_PXA_SLAVE))
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100574 writel(i2c->slave_addr, _ISAR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100575
576 /* set control register values */
Romain Perier6c14bda2016-12-01 12:04:37 +0100577 writel(I2C_ICR_INIT | (i2c->fast_mode ? i2c->fm_mask : 0), _ICR(i2c));
578 writel(readl(_ICR(i2c)) | (i2c->high_mode ? i2c->hs_mask : 0), _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100579
580#ifdef CONFIG_I2C_PXA_SLAVE
Russell King6fd60fa2005-09-08 21:04:58 +0100581 dev_info(&i2c->adap.dev, "Enabling slave mode\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100582 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100583#endif
584
585 i2c_pxa_set_slave(i2c, 0);
586
587 /* enable unit */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100588 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100589 udelay(100);
590}
591
592
593#ifdef CONFIG_I2C_PXA_SLAVE
594/*
Russell Kingb652b432005-06-15 12:38:14 +0100595 * PXA I2C Slave mode
596 */
597
598static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
599{
600 if (isr & ISR_BED) {
601 /* what should we do here? */
602 } else {
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500603 u8 byte = 0;
Russell King84b5abe2006-10-28 22:30:17 +0100604
605 if (i2c->slave != NULL)
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500606 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_PROCESSED,
607 &byte);
Russell Kingb652b432005-06-15 12:38:14 +0100608
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500609 writel(byte, _IDBR(i2c));
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100610 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
Russell Kingb652b432005-06-15 12:38:14 +0100611 }
612}
613
614static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
615{
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500616 u8 byte = readl(_IDBR(i2c));
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_WRITE_RECEIVED, &byte);
Russell Kingb652b432005-06-15 12:38:14 +0100620
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100621 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100622}
623
624static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
625{
626 int timeout;
627
628 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100629 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
Russell Kingb652b432005-06-15 12:38:14 +0100630 (isr & ISR_RWM) ? 'r' : 't');
631
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500632 if (i2c->slave != NULL) {
633 if (isr & ISR_RWM) {
634 u8 byte = 0;
635
636 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_REQUESTED,
637 &byte);
638 writel(byte, _IDBR(i2c));
639 } else {
640 i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_REQUESTED,
641 NULL);
642 }
643 }
Russell Kingb652b432005-06-15 12:38:14 +0100644
645 /*
646 * slave could interrupt in the middle of us generating a
647 * start condition... if this happens, we'd better back off
648 * and stop holding the poor thing up
649 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100650 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
651 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100652
653 timeout = 0x10000;
654
655 while (1) {
Russell Kingf8e5d3c2020-04-27 19:49:01 +0100656 if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS)
Russell Kingb652b432005-06-15 12:38:14 +0100657 break;
658
659 timeout--;
660
661 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100662 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100663 break;
664 }
665 }
666
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100667 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100668}
669
670static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
671{
672 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100673 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
Russell Kingb652b432005-06-15 12:38:14 +0100674
675 if (i2c->slave != NULL)
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500676 i2c_slave_event(i2c->slave, I2C_SLAVE_STOP, NULL);
Russell Kingb652b432005-06-15 12:38:14 +0100677
678 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100679 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
Russell Kingb652b432005-06-15 12:38:14 +0100680
681 /*
682 * If we have a master-mode message waiting,
683 * kick it off now that the slave has completed.
684 */
685 if (i2c->msg)
686 i2c_pxa_master_complete(i2c, I2C_RETRY);
687}
Patrick Williams4d51b4c2019-10-01 10:59:59 -0500688
689static int i2c_pxa_slave_reg(struct i2c_client *slave)
690{
691 struct pxa_i2c *i2c = slave->adapter->algo_data;
692
693 if (i2c->slave)
694 return -EBUSY;
695
696 if (!i2c->reg_isar)
697 return -EAFNOSUPPORT;
698
699 i2c->slave = slave;
700 i2c->slave_addr = slave->addr;
701
702 writel(i2c->slave_addr, _ISAR(i2c));
703
704 return 0;
705}
706
707static int i2c_pxa_slave_unreg(struct i2c_client *slave)
708{
709 struct pxa_i2c *i2c = slave->adapter->algo_data;
710
711 WARN_ON(!i2c->slave);
712
713 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
714 writel(i2c->slave_addr, _ISAR(i2c));
715
716 i2c->slave = NULL;
717
718 return 0;
719}
Russell Kingb652b432005-06-15 12:38:14 +0100720#else
721static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
722{
723 if (isr & ISR_BED) {
724 /* what should we do here? */
725 } else {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100726 writel(0, _IDBR(i2c));
727 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100728 }
729}
730
731static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
732{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100733 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100734}
735
736static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
737{
738 int timeout;
739
740 /*
741 * slave could interrupt in the middle of us generating a
742 * start condition... if this happens, we'd better back off
743 * and stop holding the poor thing up
744 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100745 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
746 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100747
748 timeout = 0x10000;
749
750 while (1) {
Russell Kingf8e5d3c2020-04-27 19:49:01 +0100751 if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS)
Russell Kingb652b432005-06-15 12:38:14 +0100752 break;
753
754 timeout--;
755
756 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100757 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100758 break;
759 }
760 }
761
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100762 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100763}
764
765static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
766{
767 if (i2c->msg)
768 i2c_pxa_master_complete(i2c, I2C_RETRY);
769}
770#endif
771
772/*
773 * PXA I2C Master mode
774 */
775
Russell Kingb652b432005-06-15 12:38:14 +0100776static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
777{
778 u32 icr;
779
780 /*
781 * Step 1: target slave address into IDBR
782 */
Russell King868d4d32020-04-27 19:48:36 +0100783 i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg);
784 writel(i2c->req_slave_addr, _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100785
786 /*
787 * Step 2: initiate the write.
788 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100789 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
790 writel(icr | ICR_START | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100791}
792
Jean Delvare7d054812007-05-01 23:26:33 +0200793static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
794{
795 u32 icr;
796
797 /*
798 * Clear the STOP and ACK flags
799 */
800 icr = readl(_ICR(i2c));
801 icr &= ~(ICR_STOP | ICR_ACKNAK);
Russell King0cfe61e2007-05-10 03:15:32 -0700802 writel(icr, _ICR(i2c));
Jean Delvare7d054812007-05-01 23:26:33 +0200803}
804
Leilei Shang9d3dda52013-06-07 14:38:17 +0800805/*
806 * PXA I2C send master code
807 * 1. Load master code to IDBR and send it.
808 * Note for HS mode, set ICR [GPIOEN].
809 * 2. Wait until win arbitration.
810 */
811static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c)
812{
813 u32 icr;
814 long timeout;
815
816 spin_lock_irq(&i2c->lock);
817 i2c->highmode_enter = true;
818 writel(i2c->master_code, _IDBR(i2c));
819
820 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
821 icr |= ICR_GPIOEN | ICR_START | ICR_TB | ICR_ITEIE;
822 writel(icr, _ICR(i2c));
823
824 spin_unlock_irq(&i2c->lock);
825 timeout = wait_event_timeout(i2c->wait,
826 i2c->highmode_enter == false, HZ * 1);
827
828 i2c->highmode_enter = false;
829
830 return (timeout == 0) ? I2C_RETRY : 0;
831}
832
Russell Kingb652b432005-06-15 12:38:14 +0100833/*
834 * i2c_pxa_master_complete - complete the message and wake up.
835 */
836static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
837{
838 i2c->msg_ptr = 0;
839 i2c->msg = NULL;
840 i2c->msg_idx ++;
841 i2c->msg_num = 0;
842 if (ret)
843 i2c->msg_idx = ret;
Mike Rapoportb7a36702008-01-27 18:14:50 +0100844 if (!i2c->use_pio)
845 wake_up(&i2c->wait);
Russell Kingb652b432005-06-15 12:38:14 +0100846}
847
848static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
849{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100850 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100851
852 again:
853 /*
854 * If ISR_ALD is set, we lost arbitration.
855 */
856 if (isr & ISR_ALD) {
857 /*
858 * Do we need to do anything here? The PXA docs
859 * are vague about what happens.
860 */
861 i2c_pxa_scream_blue_murder(i2c, "ALD set");
862
863 /*
864 * We ignore this error. We seem to see spurious ALDs
865 * for seemingly no reason. If we handle them as I think
866 * they should, we end up causing an I2C error, which
867 * is painful for some systems.
868 */
869 return; /* ignore */
870 }
871
Petr Cvek86261fd2014-11-25 06:05:33 +0100872 if ((isr & ISR_BED) &&
873 (!((i2c->msg->flags & I2C_M_IGNORE_NAK) &&
874 (isr & ISR_ACKNAK)))) {
Russell Kingb652b432005-06-15 12:38:14 +0100875 int ret = BUS_ERROR;
876
877 /*
878 * I2C bus error - either the device NAK'd us, or
879 * something more serious happened. If we were NAK'd
880 * on the initial address phase, we can retry.
881 */
882 if (isr & ISR_ACKNAK) {
883 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
884 ret = I2C_RETRY;
885 else
886 ret = XFER_NAKED;
887 }
888 i2c_pxa_master_complete(i2c, ret);
889 } else if (isr & ISR_RWM) {
890 /*
891 * Read mode. We have just sent the address byte, and
892 * now we must initiate the transfer.
893 */
894 if (i2c->msg_ptr == i2c->msg->len - 1 &&
895 i2c->msg_idx == i2c->msg_num - 1)
896 icr |= ICR_STOP | ICR_ACKNAK;
897
898 icr |= ICR_ALDIE | ICR_TB;
899 } else if (i2c->msg_ptr < i2c->msg->len) {
900 /*
901 * Write mode. Write the next data byte.
902 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100903 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100904
905 icr |= ICR_ALDIE | ICR_TB;
906
907 /*
Petr Cvek86261fd2014-11-25 06:05:33 +0100908 * If this is the last byte of the last message or last byte
909 * of any message with I2C_M_STOP (e.g. SCCB), send a STOP.
Russell Kingb652b432005-06-15 12:38:14 +0100910 */
Petr Cvek86261fd2014-11-25 06:05:33 +0100911 if ((i2c->msg_ptr == i2c->msg->len) &&
912 ((i2c->msg->flags & I2C_M_STOP) ||
913 (i2c->msg_idx == i2c->msg_num - 1)))
914 icr |= ICR_STOP;
915
Russell Kingb652b432005-06-15 12:38:14 +0100916 } else if (i2c->msg_idx < i2c->msg_num - 1) {
917 /*
918 * Next segment of the message.
919 */
920 i2c->msg_ptr = 0;
921 i2c->msg_idx ++;
922 i2c->msg++;
923
924 /*
925 * If we aren't doing a repeated start and address,
926 * go back and try to send the next byte. Note that
927 * we do not support switching the R/W direction here.
928 */
929 if (i2c->msg->flags & I2C_M_NOSTART)
930 goto again;
931
932 /*
933 * Write the next address.
934 */
Russell King868d4d32020-04-27 19:48:36 +0100935 i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg);
936 writel(i2c->req_slave_addr, _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100937
938 /*
939 * And trigger a repeated start, and send the byte.
940 */
941 icr &= ~ICR_ALDIE;
942 icr |= ICR_START | ICR_TB;
943 } else {
944 if (i2c->msg->len == 0) {
945 /*
946 * Device probes have a message length of zero
947 * and need the bus to be reset before it can
948 * be used again.
949 */
950 i2c_pxa_reset(i2c);
951 }
952 i2c_pxa_master_complete(i2c, 0);
953 }
954
955 i2c->icrlog[i2c->irqlogidx-1] = icr;
956
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100957 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100958 show_state(i2c);
959}
960
961static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
962{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100963 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100964
965 /*
966 * Read the byte.
967 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100968 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100969
970 if (i2c->msg_ptr < i2c->msg->len) {
971 /*
972 * If this is the last byte of the last
973 * message, send a STOP.
974 */
975 if (i2c->msg_ptr == i2c->msg->len - 1)
976 icr |= ICR_STOP | ICR_ACKNAK;
977
978 icr |= ICR_ALDIE | ICR_TB;
979 } else {
980 i2c_pxa_master_complete(i2c, 0);
981 }
982
983 i2c->icrlog[i2c->irqlogidx-1] = icr;
984
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100985 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100986}
987
Sebastian Andrzej Siewiorc66dc522011-02-23 12:38:18 +0100988#define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
989 ISR_SAD | ISR_BED)
David Howells7d12e782006-10-05 14:55:46 +0100990static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
Russell Kingb652b432005-06-15 12:38:14 +0100991{
992 struct pxa_i2c *i2c = dev_id;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100993 u32 isr = readl(_ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100994
Vasily Khoruzhick97491ba2011-03-13 15:53:29 +0200995 if (!(isr & VALID_INT_SOURCE))
Sebastian Andrzej Siewiorc66dc522011-02-23 12:38:18 +0100996 return IRQ_NONE;
997
Russell Kingb652b432005-06-15 12:38:14 +0100998 if (i2c_debug > 2 && 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100999 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001000 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +01001001 decode_ISR(isr);
1002 }
1003
Tobias Klauser7e3d7db2006-01-09 23:19:51 +01001004 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
Russell Kingb652b432005-06-15 12:38:14 +01001005 i2c->isrlog[i2c->irqlogidx++] = isr;
1006
1007 show_state(i2c);
1008
1009 /*
1010 * Always clear all pending IRQs.
1011 */
Vasily Khoruzhick97491ba2011-03-13 15:53:29 +02001012 writel(isr & VALID_INT_SOURCE, _ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +01001013
1014 if (isr & ISR_SAD)
1015 i2c_pxa_slave_start(i2c, isr);
1016 if (isr & ISR_SSD)
1017 i2c_pxa_slave_stop(i2c);
1018
1019 if (i2c_pxa_is_slavemode(i2c)) {
1020 if (isr & ISR_ITE)
1021 i2c_pxa_slave_txempty(i2c, isr);
1022 if (isr & ISR_IRF)
1023 i2c_pxa_slave_rxfull(i2c, isr);
Leilei Shang9d3dda52013-06-07 14:38:17 +08001024 } else if (i2c->msg && (!i2c->highmode_enter)) {
Russell Kingb652b432005-06-15 12:38:14 +01001025 if (isr & ISR_ITE)
1026 i2c_pxa_irq_txempty(i2c, isr);
1027 if (isr & ISR_IRF)
1028 i2c_pxa_irq_rxfull(i2c, isr);
Leilei Shang9d3dda52013-06-07 14:38:17 +08001029 } else if ((isr & ISR_ITE) && i2c->highmode_enter) {
1030 i2c->highmode_enter = false;
1031 wake_up(&i2c->wait);
Russell Kingb652b432005-06-15 12:38:14 +01001032 } else {
1033 i2c_pxa_scream_blue_murder(i2c, "spurious irq");
1034 }
1035
1036 return IRQ_HANDLED;
1037}
1038
Russell King1ae49a12020-04-27 19:48:51 +01001039/*
1040 * We are protected by the adapter bus mutex.
1041 */
1042static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
1043{
1044 long timeout;
1045 int ret;
1046
1047 /*
1048 * Wait for the bus to become free.
1049 */
1050 ret = i2c_pxa_wait_bus_not_busy(i2c);
1051 if (ret) {
1052 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
1053 goto out;
1054 }
1055
1056 /*
1057 * Set master mode.
1058 */
1059 ret = i2c_pxa_set_master(i2c);
1060 if (ret) {
1061 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
1062 goto out;
1063 }
1064
1065 if (i2c->high_mode) {
1066 ret = i2c_pxa_send_mastercode(i2c);
1067 if (ret) {
1068 dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
1069 goto out;
1070 }
1071 }
1072
1073 spin_lock_irq(&i2c->lock);
1074
1075 i2c->msg = msg;
1076 i2c->msg_num = num;
1077 i2c->msg_idx = 0;
1078 i2c->msg_ptr = 0;
1079 i2c->irqlogidx = 0;
1080
1081 i2c_pxa_start_message(i2c);
1082
1083 spin_unlock_irq(&i2c->lock);
1084
1085 /*
1086 * The rest of the processing occurs in the interrupt handler.
1087 */
1088 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
1089 i2c_pxa_stop_message(i2c);
1090
1091 /*
1092 * We place the return code in i2c->msg_idx.
1093 */
1094 ret = i2c->msg_idx;
1095
1096 if (!timeout && i2c->msg_num) {
1097 i2c_pxa_scream_blue_murder(i2c, "timeout");
1098 ret = I2C_RETRY;
1099 }
1100
1101 out:
1102 return ret;
1103}
Russell Kingb652b432005-06-15 12:38:14 +01001104
1105static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
1106{
1107 struct pxa_i2c *i2c = adap->algo_data;
1108 int ret, i;
1109
1110 for (i = adap->retries; i >= 0; i--) {
1111 ret = i2c_pxa_do_xfer(i2c, msgs, num);
1112 if (ret != I2C_RETRY)
1113 goto out;
1114
1115 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +01001116 dev_dbg(&adap->dev, "Retrying transmission\n");
Russell Kingb652b432005-06-15 12:38:14 +01001117 udelay(100);
1118 }
1119 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
1120 ret = -EREMOTEIO;
1121 out:
1122 i2c_pxa_set_slave(i2c, ret);
1123 return ret;
1124}
1125
Russell Kingda16e322005-09-14 22:54:45 +01001126static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
1127{
Petr Cvek86261fd2014-11-25 06:05:33 +01001128 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1129 I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
Russell Kingda16e322005-09-14 22:54:45 +01001130}
1131
Jean Delvare8f9082c2006-09-03 22:39:46 +02001132static const struct i2c_algorithm i2c_pxa_algorithm = {
Russell Kingb652b432005-06-15 12:38:14 +01001133 .master_xfer = i2c_pxa_xfer,
Russell Kingda16e322005-09-14 22:54:45 +01001134 .functionality = i2c_pxa_functionality,
Patrick Williams4d51b4c2019-10-01 10:59:59 -05001135#ifdef CONFIG_I2C_PXA_SLAVE
1136 .reg_slave = i2c_pxa_slave_reg,
1137 .unreg_slave = i2c_pxa_slave_unreg,
1138#endif
Russell Kingb652b432005-06-15 12:38:14 +01001139};
1140
Russell King1ae49a12020-04-27 19:48:51 +01001141/* Non-interrupt mode support */
1142static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
1143{
1144 /* make timeout the same as for interrupt based functions */
1145 long timeout = 2 * DEF_TIMEOUT;
1146
1147 /*
1148 * Wait for the bus to become free.
1149 */
1150 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
1151 udelay(1000);
1152 show_state(i2c);
1153 }
1154
1155 if (timeout < 0) {
1156 show_state(i2c);
1157 dev_err(&i2c->adap.dev,
1158 "i2c_pxa: timeout waiting for bus free\n");
1159 return I2C_RETRY;
1160 }
1161
1162 /*
1163 * Set master mode.
1164 */
1165 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
1166
1167 return 0;
1168}
1169
1170static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
1171 struct i2c_msg *msg, int num)
1172{
1173 unsigned long timeout = 500000; /* 5 seconds */
1174 int ret = 0;
1175
1176 ret = i2c_pxa_pio_set_master(i2c);
1177 if (ret)
1178 goto out;
1179
1180 i2c->msg = msg;
1181 i2c->msg_num = num;
1182 i2c->msg_idx = 0;
1183 i2c->msg_ptr = 0;
1184 i2c->irqlogidx = 0;
1185
1186 i2c_pxa_start_message(i2c);
1187
1188 while (i2c->msg_num > 0 && --timeout) {
1189 i2c_pxa_handler(0, i2c);
1190 udelay(10);
1191 }
1192
1193 i2c_pxa_stop_message(i2c);
1194
1195 /*
1196 * We place the return code in i2c->msg_idx.
1197 */
1198 ret = i2c->msg_idx;
1199
1200out:
1201 if (timeout == 0) {
1202 i2c_pxa_scream_blue_murder(i2c, "timeout");
1203 ret = I2C_RETRY;
1204 }
1205
1206 return ret;
1207}
1208
1209static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
1210 struct i2c_msg msgs[], int num)
1211{
1212 struct pxa_i2c *i2c = adap->algo_data;
1213 int ret, i;
1214
1215 /* If the I2C controller is disabled we need to reset it
1216 (probably due to a suspend/resume destroying state). We do
1217 this here as we can then avoid worrying about resuming the
1218 controller before its users. */
1219 if (!(readl(_ICR(i2c)) & ICR_IUE))
1220 i2c_pxa_reset(i2c);
1221
1222 for (i = adap->retries; i >= 0; i--) {
1223 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
1224 if (ret != I2C_RETRY)
1225 goto out;
1226
1227 if (i2c_debug)
1228 dev_dbg(&adap->dev, "Retrying transmission\n");
1229 udelay(100);
1230 }
1231 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
1232 ret = -EREMOTEIO;
1233 out:
1234 i2c_pxa_set_slave(i2c, ret);
1235 return ret;
1236}
1237
Mike Rapoportb7a36702008-01-27 18:14:50 +01001238static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
1239 .master_xfer = i2c_pxa_pio_xfer,
1240 .functionality = i2c_pxa_functionality,
Patrick Williams4d51b4c2019-10-01 10:59:59 -05001241#ifdef CONFIG_I2C_PXA_SLAVE
1242 .reg_slave = i2c_pxa_slave_reg,
1243 .unreg_slave = i2c_pxa_slave_unreg,
1244#endif
Mike Rapoportb7a36702008-01-27 18:14:50 +01001245};
1246
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001247static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
1248 enum pxa_i2c_types *i2c_types)
1249{
1250 struct device_node *np = pdev->dev.of_node;
1251 const struct of_device_id *of_id =
1252 of_match_device(i2c_pxa_dt_ids, &pdev->dev);
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001253
1254 if (!of_id)
1255 return 1;
Doug Andersonfe69c552013-03-01 06:57:32 +00001256
1257 /* For device tree we always use the dynamic or alias-assigned ID */
1258 i2c->adap.nr = -1;
1259
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001260 if (of_get_property(np, "mrvl,i2c-polling", NULL))
1261 i2c->use_pio = 1;
1262 if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
1263 i2c->fast_mode = 1;
Yipeng Yaoe2b498f2015-07-14 13:06:43 +05301264
1265 *i2c_types = (enum pxa_i2c_types)(of_id->data);
1266
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001267 return 0;
1268}
1269
1270static int i2c_pxa_probe_pdata(struct platform_device *pdev,
1271 struct pxa_i2c *i2c,
1272 enum pxa_i2c_types *i2c_types)
1273{
Jingoo Han6d4028c2013-07-30 16:59:33 +09001274 struct i2c_pxa_platform_data *plat = dev_get_platdata(&pdev->dev);
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001275 const struct platform_device_id *id = platform_get_device_id(pdev);
1276
1277 *i2c_types = id->driver_data;
1278 if (plat) {
1279 i2c->use_pio = plat->use_pio;
1280 i2c->fast_mode = plat->fast_mode;
Leilei Shang9d3dda52013-06-07 14:38:17 +08001281 i2c->high_mode = plat->high_mode;
1282 i2c->master_code = plat->master_code;
1283 if (!i2c->master_code)
1284 i2c->master_code = 0xe;
1285 i2c->rate = plat->rate;
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001286 }
1287 return 0;
1288}
1289
Russell King3ae5eae2005-11-09 22:32:44 +00001290static int i2c_pxa_probe(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001291{
Jingoo Han6d4028c2013-07-30 16:59:33 +09001292 struct i2c_pxa_platform_data *plat = dev_get_platdata(&dev->dev);
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001293 enum pxa_i2c_types i2c_type;
1294 struct pxa_i2c *i2c;
1295 struct resource *res = NULL;
1296 int ret, irq;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001297
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301298 i2c = devm_kzalloc(&dev->dev, sizeof(struct pxa_i2c), GFP_KERNEL);
1299 if (!i2c)
1300 return -ENOMEM;
1301
1302 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1303 i2c->reg_base = devm_ioremap_resource(&dev->dev, res);
1304 if (IS_ERR(i2c->reg_base))
1305 return PTR_ERR(i2c->reg_base);
1306
1307 irq = platform_get_irq(dev, 0);
Dejin Zhenge42688e2020-04-16 23:23:45 +08001308 if (irq < 0)
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301309 return irq;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001310
Doug Andersonfe69c552013-03-01 06:57:32 +00001311 /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
1312 i2c->adap.nr = dev->id;
1313
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001314 ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
1315 if (ret > 0)
1316 ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
1317 if (ret < 0)
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301318 return ret;
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001319
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001320 i2c->adap.owner = THIS_MODULE;
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001321 i2c->adap.retries = 5;
1322
1323 spin_lock_init(&i2c->lock);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001324 init_waitqueue_head(&i2c->wait);
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001325
Doug Andersonfe69c552013-03-01 06:57:32 +00001326 strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001327
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301328 i2c->clk = devm_clk_get(&dev->dev, NULL);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001329 if (IS_ERR(i2c->clk)) {
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301330 dev_err(&dev->dev, "failed to get the clk: %ld\n", PTR_ERR(i2c->clk));
1331 return PTR_ERR(i2c->clk);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001332 }
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +01001333
1334 i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
1335 i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
1336 i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
1337 i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
Russell Kingee478932020-04-27 19:49:07 +01001338 i2c->fm_mask = pxa_reg_layout[i2c_type].fm;
1339 i2c->hs_mask = pxa_reg_layout[i2c_type].hs;
Romain Perier6c14bda2016-12-01 12:04:37 +01001340
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +01001341 if (i2c_type != REGS_CE4100)
1342 i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001343
Vaibhav Hiremathc5fa6fc2015-08-24 11:29:36 +05301344 if (i2c_type == REGS_PXA910) {
1345 i2c->reg_ilcr = i2c->reg_base + pxa_reg_layout[i2c_type].ilcr;
1346 i2c->reg_iwcr = i2c->reg_base + pxa_reg_layout[i2c_type].iwcr;
1347 }
1348
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001349 i2c->iobase = res->start;
Linus Walleijc6ffdde2009-06-14 00:20:36 +02001350 i2c->iosize = resource_size(res);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001351
1352 i2c->irq = irq;
Russell Kingb652b432005-06-15 12:38:14 +01001353
1354 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
Leilei Shang9d3dda52013-06-07 14:38:17 +08001355 i2c->highmode_enter = false;
Russell Kingb652b432005-06-15 12:38:14 +01001356
Russell Kingb652b432005-06-15 12:38:14 +01001357 if (plat) {
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001358 i2c->adap.class = plat->class;
1359 }
Russell Kingb652b432005-06-15 12:38:14 +01001360
Leilei Shang9d3dda52013-06-07 14:38:17 +08001361 if (i2c->high_mode) {
1362 if (i2c->rate) {
1363 clk_set_rate(i2c->clk, i2c->rate);
1364 pr_info("i2c: <%s> set rate to %ld\n",
1365 i2c->adap.name, clk_get_rate(i2c->clk));
1366 } else
1367 pr_warn("i2c: <%s> clock rate not set\n",
1368 i2c->adap.name);
1369 }
1370
Daniel Drake7a10f472013-06-17 11:30:36 -04001371 clk_prepare_enable(i2c->clk);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001372
Mike Rapoportb7a36702008-01-27 18:14:50 +01001373 if (i2c->use_pio) {
1374 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1375 } else {
1376 i2c->adap.algo = &i2c_pxa_algorithm;
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301377 ret = devm_request_irq(&dev->dev, irq, i2c_pxa_handler,
Leilei Shangabf8a1f2015-07-14 13:06:40 +05301378 IRQF_SHARED | IRQF_NO_SUSPEND,
1379 dev_name(&dev->dev), i2c);
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301380 if (ret) {
1381 dev_err(&dev->dev, "failed to request irq: %d\n", ret);
Mike Rapoportb7a36702008-01-27 18:14:50 +01001382 goto ereqirq;
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301383 }
Mike Rapoportb7a36702008-01-27 18:14:50 +01001384 }
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001385
Russell Kingb652b432005-06-15 12:38:14 +01001386 i2c_pxa_reset(i2c);
1387
1388 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001389 i2c->adap.dev.parent = &dev->dev;
Sebastian Andrzej Siewiorbaa8cab2011-02-23 12:38:20 +01001390#ifdef CONFIG_OF
1391 i2c->adap.dev.of_node = dev->dev.of_node;
1392#endif
Russell Kingb652b432005-06-15 12:38:14 +01001393
Grant Likely488bf312011-07-25 17:49:43 +02001394 ret = i2c_add_numbered_adapter(&i2c->adap);
Wolfram Sangea734402016-08-09 13:36:17 +02001395 if (ret < 0)
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301396 goto ereqirq;
Russell Kingb652b432005-06-15 12:38:14 +01001397
Russell King3ae5eae2005-11-09 22:32:44 +00001398 platform_set_drvdata(dev, i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001399
1400#ifdef CONFIG_I2C_PXA_SLAVE
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301401 dev_info(&i2c->adap.dev, " PXA I2C adapter, slave address %d\n",
1402 i2c->slave_addr);
Russell Kingb652b432005-06-15 12:38:14 +01001403#else
Vaibhav Hiremath51fcce82015-07-14 13:06:45 +05301404 dev_info(&i2c->adap.dev, " PXA I2C adapter\n");
Russell Kingb652b432005-06-15 12:38:14 +01001405#endif
1406 return 0;
1407
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001408ereqirq:
Daniel Drake7a10f472013-06-17 11:30:36 -04001409 clk_disable_unprepare(i2c->clk);
Russell Kingb652b432005-06-15 12:38:14 +01001410 return ret;
1411}
1412
Dmitry Torokhov0a6d2242013-02-19 22:50:10 +00001413static int i2c_pxa_remove(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001414{
Russell King3ae5eae2005-11-09 22:32:44 +00001415 struct pxa_i2c *i2c = platform_get_drvdata(dev);
Russell Kingb652b432005-06-15 12:38:14 +01001416
Russell Kingb652b432005-06-15 12:38:14 +01001417 i2c_del_adapter(&i2c->adap);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001418
Daniel Drake7a10f472013-06-17 11:30:36 -04001419 clk_disable_unprepare(i2c->clk);
Russell Kingb652b432005-06-15 12:38:14 +01001420
1421 return 0;
1422}
1423
Russell Kinge7d48fa2008-08-26 10:40:50 +01001424#ifdef CONFIG_PM
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001425static int i2c_pxa_suspend_noirq(struct device *dev)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001426{
Masahiro Yamada9242e722017-07-28 01:16:24 +09001427 struct pxa_i2c *i2c = dev_get_drvdata(dev);
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001428
Russell Kinge7d48fa2008-08-26 10:40:50 +01001429 clk_disable(i2c->clk);
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001430
Russell Kinge7d48fa2008-08-26 10:40:50 +01001431 return 0;
1432}
1433
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001434static int i2c_pxa_resume_noirq(struct device *dev)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001435{
Masahiro Yamada9242e722017-07-28 01:16:24 +09001436 struct pxa_i2c *i2c = dev_get_drvdata(dev);
Russell Kinge7d48fa2008-08-26 10:40:50 +01001437
1438 clk_enable(i2c->clk);
1439 i2c_pxa_reset(i2c);
1440
1441 return 0;
1442}
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001443
Alexey Dobriyan47145212009-12-14 18:00:08 -08001444static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001445 .suspend_noirq = i2c_pxa_suspend_noirq,
1446 .resume_noirq = i2c_pxa_resume_noirq,
1447};
1448
1449#define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001450#else
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001451#define I2C_PXA_DEV_PM_OPS NULL
Russell Kinge7d48fa2008-08-26 10:40:50 +01001452#endif
1453
Russell King3ae5eae2005-11-09 22:32:44 +00001454static struct platform_driver i2c_pxa_driver = {
Russell Kingb652b432005-06-15 12:38:14 +01001455 .probe = i2c_pxa_probe,
Dmitry Torokhov0a6d2242013-02-19 22:50:10 +00001456 .remove = i2c_pxa_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001457 .driver = {
1458 .name = "pxa2xx-i2c",
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001459 .pm = I2C_PXA_DEV_PM_OPS,
Haojian Zhuang63fe1222012-03-01 13:04:44 +08001460 .of_match_table = i2c_pxa_dt_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001461 },
Eric Miaof23d4912009-04-13 14:43:25 +08001462 .id_table = i2c_pxa_id_table,
Russell Kingb652b432005-06-15 12:38:14 +01001463};
1464
1465static int __init i2c_adap_pxa_init(void)
1466{
Russell King3ae5eae2005-11-09 22:32:44 +00001467 return platform_driver_register(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001468}
1469
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001470static void __exit i2c_adap_pxa_exit(void)
Russell Kingb652b432005-06-15 12:38:14 +01001471{
Holger Schurigd6a7b5f2008-02-11 16:51:41 +01001472 platform_driver_unregister(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001473}
1474
Richard Purdieece5f7b2006-01-12 16:30:23 +00001475MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +02001476MODULE_ALIAS("platform:pxa2xx-i2c");
Richard Purdieece5f7b2006-01-12 16:30:23 +00001477
Uli Luckas47a9b132008-07-14 22:38:30 +02001478subsys_initcall(i2c_adap_pxa_init);
Russell Kingb652b432005-06-15 12:38:14 +01001479module_exit(i2c_adap_pxa_exit);