blob: 43913c1d50f5b5d0da535f1c9f788a98712befc2 [file] [log] [blame]
Vladimir Barinov95a7f102007-10-13 23:56:30 +02001/*
2 * TI DAVINCI I2C adapter driver.
3 *
4 * Copyright (C) 2006 Texas Instruments.
5 * Copyright (C) 2007 MontaVista Software Inc.
6 *
7 * Updated by Vinod & Sudhakar Feb 2005
8 *
9 * ----------------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * ----------------------------------------------------------------------------
25 *
26 */
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/delay.h>
30#include <linux/i2c.h>
31#include <linux/clk.h>
32#include <linux/errno.h>
33#include <linux/sched.h>
34#include <linux/err.h>
35#include <linux/interrupt.h>
36#include <linux/platform_device.h>
37#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Vladimir Barinov95a7f102007-10-13 23:56:30 +020039
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/hardware.h>
Vladimir Barinov95a7f102007-10-13 23:56:30 +020041
Russell Kinga09e64f2008-08-05 16:14:15 +010042#include <mach/i2c.h>
Vladimir Barinov95a7f102007-10-13 23:56:30 +020043
44/* ----- global defines ----------------------------------------------- */
45
46#define DAVINCI_I2C_TIMEOUT (1*HZ)
47#define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
48 DAVINCI_I2C_IMR_SCD | \
49 DAVINCI_I2C_IMR_ARDY | \
50 DAVINCI_I2C_IMR_NACK | \
51 DAVINCI_I2C_IMR_AL)
52
53#define DAVINCI_I2C_OAR_REG 0x00
54#define DAVINCI_I2C_IMR_REG 0x04
55#define DAVINCI_I2C_STR_REG 0x08
56#define DAVINCI_I2C_CLKL_REG 0x0c
57#define DAVINCI_I2C_CLKH_REG 0x10
58#define DAVINCI_I2C_CNT_REG 0x14
59#define DAVINCI_I2C_DRR_REG 0x18
60#define DAVINCI_I2C_SAR_REG 0x1c
61#define DAVINCI_I2C_DXR_REG 0x20
62#define DAVINCI_I2C_MDR_REG 0x24
63#define DAVINCI_I2C_IVR_REG 0x28
64#define DAVINCI_I2C_EMDR_REG 0x2c
65#define DAVINCI_I2C_PSC_REG 0x30
66
67#define DAVINCI_I2C_IVR_AAS 0x07
68#define DAVINCI_I2C_IVR_SCD 0x06
69#define DAVINCI_I2C_IVR_XRDY 0x05
70#define DAVINCI_I2C_IVR_RDR 0x04
71#define DAVINCI_I2C_IVR_ARDY 0x03
72#define DAVINCI_I2C_IVR_NACK 0x02
73#define DAVINCI_I2C_IVR_AL 0x01
74
75#define DAVINCI_I2C_STR_BB (1 << 12)
76#define DAVINCI_I2C_STR_RSFULL (1 << 11)
77#define DAVINCI_I2C_STR_SCD (1 << 5)
78#define DAVINCI_I2C_STR_ARDY (1 << 2)
79#define DAVINCI_I2C_STR_NACK (1 << 1)
80#define DAVINCI_I2C_STR_AL (1 << 0)
81
82#define DAVINCI_I2C_MDR_NACK (1 << 15)
83#define DAVINCI_I2C_MDR_STT (1 << 13)
84#define DAVINCI_I2C_MDR_STP (1 << 11)
85#define DAVINCI_I2C_MDR_MST (1 << 10)
86#define DAVINCI_I2C_MDR_TRX (1 << 9)
87#define DAVINCI_I2C_MDR_XA (1 << 8)
Troy Kisky5a0d5f52008-07-14 22:38:21 +020088#define DAVINCI_I2C_MDR_RM (1 << 7)
Vladimir Barinov95a7f102007-10-13 23:56:30 +020089#define DAVINCI_I2C_MDR_IRS (1 << 5)
90
91#define DAVINCI_I2C_IMR_AAS (1 << 6)
92#define DAVINCI_I2C_IMR_SCD (1 << 5)
93#define DAVINCI_I2C_IMR_XRDY (1 << 4)
94#define DAVINCI_I2C_IMR_RRDY (1 << 3)
95#define DAVINCI_I2C_IMR_ARDY (1 << 2)
96#define DAVINCI_I2C_IMR_NACK (1 << 1)
97#define DAVINCI_I2C_IMR_AL (1 << 0)
98
99#define MOD_REG_BIT(val, mask, set) do { \
100 if (set) { \
101 val |= mask; \
102 } else { \
103 val &= ~mask; \
104 } \
105} while (0)
106
107struct davinci_i2c_dev {
108 struct device *dev;
109 void __iomem *base;
110 struct completion cmd_complete;
111 struct clk *clk;
112 int cmd_err;
113 u8 *buf;
114 size_t buf_len;
115 int irq;
Dirk Behmec6c7c722008-03-28 06:16:12 +0100116 int stop;
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200117 u8 terminate;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200118 struct i2c_adapter adapter;
119};
120
121/* default platform data to use if not supplied in the platform_device */
122static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
123 .bus_freq = 100,
124 .bus_delay = 0,
125};
126
127static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
128 int reg, u16 val)
129{
130 __raw_writew(val, i2c_dev->base + reg);
131}
132
133static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
134{
135 return __raw_readw(i2c_dev->base + reg);
136}
137
138/*
139 * This functions configures I2C and brings I2C out of reset.
140 * This function is called during I2C init function. This function
141 * also gets called if I2C encounters any errors.
142 */
143static int i2c_davinci_init(struct davinci_i2c_dev *dev)
144{
145 struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
146 u16 psc;
147 u32 clk;
Troy Kiskycc99ff72008-07-14 22:38:20 +0200148 u32 d;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200149 u32 clkh;
150 u32 clkl;
151 u32 input_clock = clk_get_rate(dev->clk);
152 u16 w;
153
154 if (!pdata)
155 pdata = &davinci_i2c_platform_data_default;
156
157 /* put I2C into reset */
158 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
159 MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 0);
160 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
161
162 /* NOTE: I2C Clock divider programming info
163 * As per I2C specs the following formulas provide prescaler
164 * and low/high divider values
165 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
166 * module clk
167 *
168 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
169 *
170 * Thus,
171 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
172 *
173 * where if PSC == 0, d = 7,
174 * if PSC == 1, d = 6
175 * if PSC > 1 , d = 5
176 */
177
Troy Kiskycc99ff72008-07-14 22:38:20 +0200178 /* get minimum of 7 MHz clock, but max of 12 MHz */
179 psc = (input_clock / 7000000) - 1;
180 if ((input_clock / (psc + 1)) > 12000000)
181 psc++; /* better to run under spec than over */
182 d = (psc >= 2) ? 5 : 7 - psc;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200183
Troy Kiskycc99ff72008-07-14 22:38:20 +0200184 clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
185 clkh = clk >> 1;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200186 clkl = clk - clkh;
187
188 davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
189 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
190 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
191
David Brownell7605fa32009-07-06 15:48:36 -0700192 /* Respond at reserved "SMBus Host" slave address" (and zero);
193 * we seem to have no option to not respond...
194 */
195 davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
196
Troy Kiskycc99ff72008-07-14 22:38:20 +0200197 dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200198 dev_dbg(dev->dev, "PSC = %d\n",
199 davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
200 dev_dbg(dev->dev, "CLKL = %d\n",
201 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
202 dev_dbg(dev->dev, "CLKH = %d\n",
203 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
Troy Kiskycc99ff72008-07-14 22:38:20 +0200204 dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
205 pdata->bus_freq, pdata->bus_delay);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200206
207 /* Take the I2C module out of reset: */
208 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
209 MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 1);
210 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
211
212 /* Enable interrupts */
213 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
214
215 return 0;
216}
217
218/*
219 * Waiting for bus not busy
220 */
221static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
222 char allow_sleep)
223{
224 unsigned long timeout;
225
Jean Delvare98a679c2009-03-28 21:34:43 +0100226 timeout = jiffies + dev->adapter.timeout;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200227 while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
228 & DAVINCI_I2C_STR_BB) {
229 if (time_after(jiffies, timeout)) {
230 dev_warn(dev->dev,
231 "timeout waiting for bus ready\n");
232 return -ETIMEDOUT;
233 }
234 if (allow_sleep)
235 schedule_timeout(1);
236 }
237
238 return 0;
239}
240
241/*
242 * Low level master read/write transaction. This function is called
243 * from i2c_davinci_xfer.
244 */
245static int
246i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
247{
248 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
249 struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
250 u32 flag;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200251 u16 w;
252 int r;
253
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200254 if (!pdata)
255 pdata = &davinci_i2c_platform_data_default;
256 /* Introduce a delay, required for some boards (e.g Davinci EVM) */
257 if (pdata->bus_delay)
258 udelay(pdata->bus_delay);
259
260 /* set the slave address */
261 davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
262
263 dev->buf = msg->buf;
264 dev->buf_len = msg->len;
Dirk Behmec6c7c722008-03-28 06:16:12 +0100265 dev->stop = stop;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200266
267 davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
268
Troy Kisky2e743782008-07-14 22:38:21 +0200269 INIT_COMPLETION(dev->cmd_complete);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200270 dev->cmd_err = 0;
271
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200272 /* Take I2C out of reset, configure it as master and set the
273 * start bit */
274 flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST | DAVINCI_I2C_MDR_STT;
275
276 /* if the slave address is ten bit address, enable XA bit */
277 if (msg->flags & I2C_M_TEN)
278 flag |= DAVINCI_I2C_MDR_XA;
279 if (!(msg->flags & I2C_M_RD))
280 flag |= DAVINCI_I2C_MDR_TRX;
281 if (stop)
282 flag |= DAVINCI_I2C_MDR_STP;
Dirk Behmec6c7c722008-03-28 06:16:12 +0100283 if (msg->len == 0) {
284 flag |= DAVINCI_I2C_MDR_RM;
285 flag &= ~DAVINCI_I2C_MDR_STP;
286 }
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200287
288 /* Enable receive or transmit interrupts */
289 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
290 if (msg->flags & I2C_M_RD)
291 MOD_REG_BIT(w, DAVINCI_I2C_IMR_RRDY, 1);
292 else
293 MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 1);
294 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
295
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200296 dev->terminate = 0;
Dirk Behmec6c7c722008-03-28 06:16:12 +0100297
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200298 /* write the data into mode register */
299 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
300
Dirk Behmec6c7c722008-03-28 06:16:12 +0100301 /*
302 * First byte should be set here, not after interrupt,
303 * because transmit-data-ready interrupt can come before
304 * NACK-interrupt during sending of previous message and
305 * ICDXR may have wrong data
306 */
307 if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
308 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
309 dev->buf_len--;
310 }
311
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200312 r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
Jean Delvare98a679c2009-03-28 21:34:43 +0100313 dev->adapter.timeout);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200314 if (r == 0) {
315 dev_err(dev->dev, "controller timed out\n");
316 i2c_davinci_init(dev);
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200317 dev->buf_len = 0;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200318 return -ETIMEDOUT;
319 }
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200320 if (dev->buf_len) {
321 /* This should be 0 if all bytes were transferred
322 * or dev->cmd_err denotes an error.
323 * A signal may have aborted the transfer.
324 */
325 if (r >= 0) {
326 dev_err(dev->dev, "abnormal termination buf_len=%i\n",
327 dev->buf_len);
328 r = -EREMOTEIO;
329 }
330 dev->terminate = 1;
331 wmb();
332 dev->buf_len = 0;
333 }
334 if (r < 0)
335 return r;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200336
337 /* no error */
338 if (likely(!dev->cmd_err))
339 return msg->len;
340
341 /* We have an error */
342 if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
343 i2c_davinci_init(dev);
344 return -EIO;
345 }
346
347 if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
348 if (msg->flags & I2C_M_IGNORE_NAK)
349 return msg->len;
350 if (stop) {
351 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
352 MOD_REG_BIT(w, DAVINCI_I2C_MDR_STP, 1);
353 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
354 }
355 return -EREMOTEIO;
356 }
357 return -EIO;
358}
359
360/*
361 * Prepare controller for a transaction and call i2c_davinci_xfer_msg
362 */
363static int
364i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
365{
366 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
367 int i;
368 int ret;
369
Harvey Harrison08882d22008-04-22 22:16:47 +0200370 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200371
372 ret = i2c_davinci_wait_bus_not_busy(dev, 1);
373 if (ret < 0) {
374 dev_warn(dev->dev, "timeout waiting for bus ready\n");
375 return ret;
376 }
377
378 for (i = 0; i < num; i++) {
379 ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
Troy Kiskyd868caa2008-07-14 22:38:20 +0200380 dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
381 ret);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200382 if (ret < 0)
383 return ret;
384 }
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200385 return num;
386}
387
388static u32 i2c_davinci_func(struct i2c_adapter *adap)
389{
Dirk Behmec6c7c722008-03-28 06:16:12 +0100390 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200391}
392
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200393static void terminate_read(struct davinci_i2c_dev *dev)
394{
395 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
396 w |= DAVINCI_I2C_MDR_NACK;
397 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
398
399 /* Throw away data */
400 davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
401 if (!dev->terminate)
402 dev_err(dev->dev, "RDR IRQ while no data requested\n");
403}
404static void terminate_write(struct davinci_i2c_dev *dev)
405{
406 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
407 w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
408 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
409
410 if (!dev->terminate)
David Brownell7605fa32009-07-06 15:48:36 -0700411 dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200412}
413
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200414/*
415 * Interrupt service routine. This gets called whenever an I2C interrupt
416 * occurs.
417 */
418static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
419{
420 struct davinci_i2c_dev *dev = dev_id;
421 u32 stat;
422 int count = 0;
423 u16 w;
424
425 while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
Harvey Harrison08882d22008-04-22 22:16:47 +0200426 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200427 if (count++ == 100) {
428 dev_warn(dev->dev, "Too much work in one IRQ\n");
429 break;
430 }
431
432 switch (stat) {
433 case DAVINCI_I2C_IVR_AL:
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200434 /* Arbitration lost, must retry */
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200435 dev->cmd_err |= DAVINCI_I2C_STR_AL;
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200436 dev->buf_len = 0;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200437 complete(&dev->cmd_complete);
438 break;
439
440 case DAVINCI_I2C_IVR_NACK:
441 dev->cmd_err |= DAVINCI_I2C_STR_NACK;
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200442 dev->buf_len = 0;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200443 complete(&dev->cmd_complete);
444 break;
445
446 case DAVINCI_I2C_IVR_ARDY:
Troy Kiskyb73a9ae2008-04-11 12:07:05 +0200447 davinci_i2c_write_reg(dev,
448 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
Dirk Behmec6c7c722008-03-28 06:16:12 +0100449 if (((dev->buf_len == 0) && (dev->stop != 0)) ||
450 (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
451 w = davinci_i2c_read_reg(dev,
452 DAVINCI_I2C_MDR_REG);
453 w |= DAVINCI_I2C_MDR_STP;
454 davinci_i2c_write_reg(dev,
455 DAVINCI_I2C_MDR_REG, w);
456 }
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200457 complete(&dev->cmd_complete);
458 break;
459
460 case DAVINCI_I2C_IVR_RDR:
461 if (dev->buf_len) {
462 *dev->buf++ =
463 davinci_i2c_read_reg(dev,
464 DAVINCI_I2C_DRR_REG);
465 dev->buf_len--;
466 if (dev->buf_len)
467 continue;
468
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200469 davinci_i2c_write_reg(dev,
Troy Kiskyb73a9ae2008-04-11 12:07:05 +0200470 DAVINCI_I2C_STR_REG,
471 DAVINCI_I2C_IMR_RRDY);
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200472 } else {
473 /* signal can terminate transfer */
474 terminate_read(dev);
475 }
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200476 break;
477
478 case DAVINCI_I2C_IVR_XRDY:
479 if (dev->buf_len) {
480 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
481 *dev->buf++);
482 dev->buf_len--;
483 if (dev->buf_len)
484 continue;
485
486 w = davinci_i2c_read_reg(dev,
487 DAVINCI_I2C_IMR_REG);
488 MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 0);
489 davinci_i2c_write_reg(dev,
490 DAVINCI_I2C_IMR_REG,
491 w);
Troy Kisky5a0d5f52008-07-14 22:38:21 +0200492 } else {
493 /* signal can terminate transfer */
494 terminate_write(dev);
495 }
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200496 break;
497
498 case DAVINCI_I2C_IVR_SCD:
Troy Kiskyb73a9ae2008-04-11 12:07:05 +0200499 davinci_i2c_write_reg(dev,
500 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200501 complete(&dev->cmd_complete);
502 break;
503
504 case DAVINCI_I2C_IVR_AAS:
David Brownell7605fa32009-07-06 15:48:36 -0700505 dev_dbg(dev->dev, "Address as slave interrupt\n");
506 break;
507
508 default:
509 dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
510 break;
511 }
512 }
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200513
514 return count ? IRQ_HANDLED : IRQ_NONE;
515}
516
517static struct i2c_algorithm i2c_davinci_algo = {
518 .master_xfer = i2c_davinci_xfer,
519 .functionality = i2c_davinci_func,
520};
521
522static int davinci_i2c_probe(struct platform_device *pdev)
523{
524 struct davinci_i2c_dev *dev;
525 struct i2c_adapter *adap;
526 struct resource *mem, *irq, *ioarea;
527 int r;
528
529 /* NOTE: driver uses the static register mapping */
530 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
531 if (!mem) {
532 dev_err(&pdev->dev, "no mem resource?\n");
533 return -ENODEV;
534 }
535
536 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
537 if (!irq) {
538 dev_err(&pdev->dev, "no irq resource?\n");
539 return -ENODEV;
540 }
541
Julia Lawall59330822009-07-05 08:37:50 +0200542 ioarea = request_mem_region(mem->start, resource_size(mem),
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200543 pdev->name);
544 if (!ioarea) {
545 dev_err(&pdev->dev, "I2C region already claimed\n");
546 return -EBUSY;
547 }
548
549 dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
550 if (!dev) {
551 r = -ENOMEM;
552 goto err_release_region;
553 }
554
Troy Kisky2e743782008-07-14 22:38:21 +0200555 init_completion(&dev->cmd_complete);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200556 dev->dev = get_device(&pdev->dev);
557 dev->irq = irq->start;
558 platform_set_drvdata(pdev, dev);
559
Kevin Hilmane164dde2009-07-06 15:48:35 -0700560 dev->clk = clk_get(&pdev->dev, NULL);
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200561 if (IS_ERR(dev->clk)) {
562 r = -ENODEV;
563 goto err_free_mem;
564 }
565 clk_enable(dev->clk);
566
567 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
568 i2c_davinci_init(dev);
569
570 r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
571 if (r) {
572 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
573 goto err_unuse_clocks;
574 }
575
576 adap = &dev->adapter;
577 i2c_set_adapdata(adap, dev);
578 adap->owner = THIS_MODULE;
579 adap->class = I2C_CLASS_HWMON;
580 strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
581 adap->algo = &i2c_davinci_algo;
582 adap->dev.parent = &pdev->dev;
Jean Delvare98a679c2009-03-28 21:34:43 +0100583 adap->timeout = DAVINCI_I2C_TIMEOUT;
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200584
585 adap->nr = pdev->id;
586 r = i2c_add_numbered_adapter(adap);
587 if (r) {
588 dev_err(&pdev->dev, "failure adding adapter\n");
589 goto err_free_irq;
590 }
591
592 return 0;
593
594err_free_irq:
595 free_irq(dev->irq, dev);
596err_unuse_clocks:
597 clk_disable(dev->clk);
598 clk_put(dev->clk);
599 dev->clk = NULL;
600err_free_mem:
601 platform_set_drvdata(pdev, NULL);
602 put_device(&pdev->dev);
603 kfree(dev);
604err_release_region:
Julia Lawall59330822009-07-05 08:37:50 +0200605 release_mem_region(mem->start, resource_size(mem));
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200606
607 return r;
608}
609
610static int davinci_i2c_remove(struct platform_device *pdev)
611{
612 struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
613 struct resource *mem;
614
615 platform_set_drvdata(pdev, NULL);
616 i2c_del_adapter(&dev->adapter);
617 put_device(&pdev->dev);
618
619 clk_disable(dev->clk);
620 clk_put(dev->clk);
621 dev->clk = NULL;
622
623 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
624 free_irq(IRQ_I2C, dev);
625 kfree(dev);
626
627 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall59330822009-07-05 08:37:50 +0200628 release_mem_region(mem->start, resource_size(mem));
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200629 return 0;
630}
631
Kay Sieversadd8eda2008-04-22 22:16:49 +0200632/* work with hotplug and coldplug */
633MODULE_ALIAS("platform:i2c_davinci");
634
Vladimir Barinov95a7f102007-10-13 23:56:30 +0200635static struct platform_driver davinci_i2c_driver = {
636 .probe = davinci_i2c_probe,
637 .remove = davinci_i2c_remove,
638 .driver = {
639 .name = "i2c_davinci",
640 .owner = THIS_MODULE,
641 },
642};
643
644/* I2C may be needed to bring up other drivers */
645static int __init davinci_i2c_init_driver(void)
646{
647 return platform_driver_register(&davinci_i2c_driver);
648}
649subsys_initcall(davinci_i2c_init_driver);
650
651static void __exit davinci_i2c_exit_driver(void)
652{
653 platform_driver_unregister(&davinci_i2c_driver);
654}
655module_exit(davinci_i2c_exit_driver);
656
657MODULE_AUTHOR("Texas Instruments India");
658MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
659MODULE_LICENSE("GPL");