blob: 8c7526ca912ea40c623edc6ca42567f2833bbce8 [file] [log] [blame]
Darius Augulisaa11e382009-01-30 10:32:28 +02001/*
2 * Copyright (C) 2002 Motorola GSG-China
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 *
19 * Author:
20 * Darius Augulis, Teltonika Inc.
21 *
22 * Desc.:
23 * Implementation of I2C Adapter/Algorithm Driver
24 * for I2C Bus integrated in Freescale i.MX/MXC processors
25 *
26 * Derived from Motorola GSG China I2C example driver
27 *
28 * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de
29 * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de
30 * Copyright (C) 2007 RightHand Technologies, Inc.
31 * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
32 *
33 */
34
35/** Includes *******************************************************************
36*******************************************************************************/
37
38#include <linux/init.h>
39#include <linux/kernel.h>
40#include <linux/module.h>
41#include <linux/errno.h>
42#include <linux/err.h>
43#include <linux/interrupt.h>
44#include <linux/delay.h>
45#include <linux/i2c.h>
46#include <linux/io.h>
47#include <linux/sched.h>
48#include <linux/platform_device.h>
49#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Shawn Guodfcd04b2011-09-08 15:09:35 +080051#include <linux/of.h>
52#include <linux/of_device.h>
53#include <linux/of_i2c.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020054#include <linux/platform_data/i2c-imx.h>
Darius Augulisaa11e382009-01-30 10:32:28 +020055
56/** Defines ********************************************************************
57*******************************************************************************/
58
59/* This will be the driver name the kernel reports */
60#define DRIVER_NAME "imx-i2c"
61
62/* Default value */
63#define IMX_I2C_BIT_RATE 100000 /* 100kHz */
64
65/* IMX I2C registers */
66#define IMX_I2C_IADR 0x00 /* i2c slave address */
67#define IMX_I2C_IFDR 0x04 /* i2c frequency divider */
68#define IMX_I2C_I2CR 0x08 /* i2c control */
69#define IMX_I2C_I2SR 0x0C /* i2c status */
70#define IMX_I2C_I2DR 0x10 /* i2c transfer data */
71
72/* Bits of IMX I2C registers */
73#define I2SR_RXAK 0x01
74#define I2SR_IIF 0x02
75#define I2SR_SRW 0x04
76#define I2SR_IAL 0x10
77#define I2SR_IBB 0x20
78#define I2SR_IAAS 0x40
79#define I2SR_ICF 0x80
80#define I2CR_RSTA 0x04
81#define I2CR_TXAK 0x08
82#define I2CR_MTX 0x10
83#define I2CR_MSTA 0x20
84#define I2CR_IIEN 0x40
85#define I2CR_IEN 0x80
86
87/** Variables ******************************************************************
88*******************************************************************************/
89
Darius Augulisaa11e382009-01-30 10:32:28 +020090/*
91 * sorted list of clock divider, register value pairs
92 * taken from table 26-5, p.26-9, Freescale i.MX
93 * Integrated Portable System Processor Reference Manual
94 * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
95 *
96 * Duplicated divider values removed from list
97 */
98
99static u16 __initdata i2c_clk_div[50][2] = {
100 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
101 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
102 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
103 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
104 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
105 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
106 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
107 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
108 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
109 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
110 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
111 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
112 { 3072, 0x1E }, { 3840, 0x1F }
113};
114
Shawn Guo5bdfba22012-09-14 15:19:00 +0800115enum imx_i2c_type {
116 IMX1_I2C,
117 IMX21_I2C,
118};
119
Darius Augulisaa11e382009-01-30 10:32:28 +0200120struct imx_i2c_struct {
121 struct i2c_adapter adapter;
Darius Augulisaa11e382009-01-30 10:32:28 +0200122 struct clk *clk;
123 void __iomem *base;
Darius Augulisaa11e382009-01-30 10:32:28 +0200124 wait_queue_head_t queue;
125 unsigned long i2csr;
Wolfram Sang65de3942009-04-06 16:27:45 +0200126 unsigned int disable_delay;
Richard Zhao43309f32009-10-17 17:46:22 +0800127 int stopped;
Richard Zhaodb3a3d42009-10-17 17:46:24 +0800128 unsigned int ifdr; /* IMX_I2C_IFDR */
Shawn Guo5bdfba22012-09-14 15:19:00 +0800129 enum imx_i2c_type devtype;
Darius Augulisaa11e382009-01-30 10:32:28 +0200130};
131
Shawn Guo5bdfba22012-09-14 15:19:00 +0800132static struct platform_device_id imx_i2c_devtype[] = {
133 {
134 .name = "imx1-i2c",
135 .driver_data = IMX1_I2C,
136 }, {
137 .name = "imx21-i2c",
138 .driver_data = IMX21_I2C,
139 }, {
140 /* sentinel */
141 }
142};
143MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
144
Shawn Guodfcd04b2011-09-08 15:09:35 +0800145static const struct of_device_id i2c_imx_dt_ids[] = {
Shawn Guo5bdfba22012-09-14 15:19:00 +0800146 { .compatible = "fsl,imx1-i2c", .data = &imx_i2c_devtype[IMX1_I2C], },
147 { .compatible = "fsl,imx21-i2c", .data = &imx_i2c_devtype[IMX21_I2C], },
Shawn Guodfcd04b2011-09-08 15:09:35 +0800148 { /* sentinel */ }
149};
150
Shawn Guo5bdfba22012-09-14 15:19:00 +0800151static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx)
152{
153 return i2c_imx->devtype == IMX1_I2C;
154}
155
Darius Augulisaa11e382009-01-30 10:32:28 +0200156/** Functions for IMX I2C adapter driver ***************************************
157*******************************************************************************/
158
Richard Zhao43309f32009-10-17 17:46:22 +0800159static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
Darius Augulisaa11e382009-01-30 10:32:28 +0200160{
161 unsigned long orig_jiffies = jiffies;
Richard Zhao43309f32009-10-17 17:46:22 +0800162 unsigned int temp;
Darius Augulisaa11e382009-01-30 10:32:28 +0200163
164 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
165
Richard Zhao43309f32009-10-17 17:46:22 +0800166 while (1) {
167 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
168 if (for_busy && (temp & I2SR_IBB))
169 break;
170 if (!for_busy && !(temp & I2SR_IBB))
171 break;
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100172 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200173 dev_dbg(&i2c_imx->adapter.dev,
174 "<%s> I2C bus is busy\n", __func__);
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100175 return -ETIMEDOUT;
Darius Augulisaa11e382009-01-30 10:32:28 +0200176 }
177 schedule();
178 }
179
180 return 0;
181}
182
183static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
184{
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200185 wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
Darius Augulisaa11e382009-01-30 10:32:28 +0200186
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200187 if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200188 dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
189 return -ETIMEDOUT;
190 }
191 dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
192 i2c_imx->i2csr = 0;
193 return 0;
194}
195
196static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
197{
198 if (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_RXAK) {
199 dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
200 return -EIO; /* No ACK */
201 }
202
203 dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
204 return 0;
205}
206
Richard Zhao43309f32009-10-17 17:46:22 +0800207static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
Darius Augulisaa11e382009-01-30 10:32:28 +0200208{
209 unsigned int temp = 0;
Richard Zhao43309f32009-10-17 17:46:22 +0800210 int result;
Darius Augulisaa11e382009-01-30 10:32:28 +0200211
212 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
213
Richard Zhao83914332011-11-15 14:48:08 +0800214 clk_prepare_enable(i2c_imx->clk);
Richard Zhaodb3a3d42009-10-17 17:46:24 +0800215 writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200216 /* Enable I2C controller */
Richard Zhao43309f32009-10-17 17:46:22 +0800217 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200218 writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800219
220 /* Wait controller to be stable */
221 udelay(50);
222
Darius Augulisaa11e382009-01-30 10:32:28 +0200223 /* Start I2C transaction */
224 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
225 temp |= I2CR_MSTA;
226 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800227 result = i2c_imx_bus_busy(i2c_imx, 1);
228 if (result)
229 return result;
230 i2c_imx->stopped = 0;
231
Darius Augulisaa11e382009-01-30 10:32:28 +0200232 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
233 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800234 return result;
Darius Augulisaa11e382009-01-30 10:32:28 +0200235}
236
237static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
238{
239 unsigned int temp = 0;
240
Richard Zhao43309f32009-10-17 17:46:22 +0800241 if (!i2c_imx->stopped) {
242 /* Stop I2C transaction */
243 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
244 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
245 temp &= ~(I2CR_MSTA | I2CR_MTX);
246 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800247 }
Shawn Guo5bdfba22012-09-14 15:19:00 +0800248 if (is_imx1_i2c(i2c_imx)) {
Richard Zhaoa4094a72009-10-17 17:46:23 +0800249 /*
250 * This delay caused by an i.MXL hardware bug.
251 * If no (or too short) delay, no "STOP" bit will be generated.
252 */
253 udelay(i2c_imx->disable_delay);
254 }
Richard Zhao43309f32009-10-17 17:46:22 +0800255
Valentin Longchampa1ee06b2010-01-21 18:55:32 +0100256 if (!i2c_imx->stopped) {
Richard Zhao43309f32009-10-17 17:46:22 +0800257 i2c_imx_bus_busy(i2c_imx, 0);
Valentin Longchampa1ee06b2010-01-21 18:55:32 +0100258 i2c_imx->stopped = 1;
259 }
Richard Zhao43309f32009-10-17 17:46:22 +0800260
Darius Augulisaa11e382009-01-30 10:32:28 +0200261 /* Disable I2C controller */
262 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao83914332011-11-15 14:48:08 +0800263 clk_disable_unprepare(i2c_imx->clk);
Darius Augulisaa11e382009-01-30 10:32:28 +0200264}
265
266static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
267 unsigned int rate)
268{
269 unsigned int i2c_clk_rate;
270 unsigned int div;
271 int i;
272
273 /* Divider value calculation */
274 i2c_clk_rate = clk_get_rate(i2c_imx->clk);
275 div = (i2c_clk_rate + rate - 1) / rate;
276 if (div < i2c_clk_div[0][0])
277 i = 0;
278 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
279 i = ARRAY_SIZE(i2c_clk_div) - 1;
280 else
281 for (i = 0; i2c_clk_div[i][0] < div; i++);
282
Richard Zhaodb3a3d42009-10-17 17:46:24 +0800283 /* Store divider value */
284 i2c_imx->ifdr = i2c_clk_div[i][1];
Darius Augulisaa11e382009-01-30 10:32:28 +0200285
286 /*
287 * There dummy delay is calculated.
288 * It should be about one I2C clock period long.
289 * This delay is used in I2C bus disable function
290 * to fix chip hardware bug.
291 */
Wolfram Sang65de3942009-04-06 16:27:45 +0200292 i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0]
Darius Augulisaa11e382009-01-30 10:32:28 +0200293 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
294
295 /* dev_dbg() can't be used, because adapter is not yet registered */
296#ifdef CONFIG_I2C_DEBUG_BUS
Fabio Estevam002f0022012-08-01 17:38:15 -0300297 dev_dbg(&i2c_imx->adapter.dev, "<%s> I2C_CLK=%d, REQ DIV=%d\n",
Darius Augulisaa11e382009-01-30 10:32:28 +0200298 __func__, i2c_clk_rate, div);
Fabio Estevam002f0022012-08-01 17:38:15 -0300299 dev_dbg(&i2c_imx->adapter.dev, "<%s> IFDR[IC]=0x%x, REAL DIV=%d\n",
Darius Augulisaa11e382009-01-30 10:32:28 +0200300 __func__, i2c_clk_div[i][1], i2c_clk_div[i][0]);
301#endif
302}
303
304static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
305{
306 struct imx_i2c_struct *i2c_imx = dev_id;
307 unsigned int temp;
308
309 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
310 if (temp & I2SR_IIF) {
311 /* save status register */
312 i2c_imx->i2csr = temp;
313 temp &= ~I2SR_IIF;
314 writeb(temp, i2c_imx->base + IMX_I2C_I2SR);
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200315 wake_up(&i2c_imx->queue);
Darius Augulisaa11e382009-01-30 10:32:28 +0200316 return IRQ_HANDLED;
317 }
318
319 return IRQ_NONE;
320}
321
322static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
323{
324 int i, result;
325
326 dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
327 __func__, msgs->addr << 1);
328
329 /* write slave address */
330 writeb(msgs->addr << 1, i2c_imx->base + IMX_I2C_I2DR);
331 result = i2c_imx_trx_complete(i2c_imx);
332 if (result)
333 return result;
334 result = i2c_imx_acked(i2c_imx);
335 if (result)
336 return result;
337 dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
338
339 /* write data */
340 for (i = 0; i < msgs->len; i++) {
341 dev_dbg(&i2c_imx->adapter.dev,
342 "<%s> write byte: B%d=0x%X\n",
343 __func__, i, msgs->buf[i]);
344 writeb(msgs->buf[i], i2c_imx->base + IMX_I2C_I2DR);
345 result = i2c_imx_trx_complete(i2c_imx);
346 if (result)
347 return result;
348 result = i2c_imx_acked(i2c_imx);
349 if (result)
350 return result;
351 }
352 return 0;
353}
354
355static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
356{
357 int i, result;
358 unsigned int temp;
359
360 dev_dbg(&i2c_imx->adapter.dev,
361 "<%s> write slave address: addr=0x%x\n",
362 __func__, (msgs->addr << 1) | 0x01);
363
364 /* write slave address */
365 writeb((msgs->addr << 1) | 0x01, i2c_imx->base + IMX_I2C_I2DR);
366 result = i2c_imx_trx_complete(i2c_imx);
367 if (result)
368 return result;
369 result = i2c_imx_acked(i2c_imx);
370 if (result)
371 return result;
372
373 dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
374
375 /* setup bus to read data */
376 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
377 temp &= ~I2CR_MTX;
378 if (msgs->len - 1)
379 temp &= ~I2CR_TXAK;
380 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
381 readb(i2c_imx->base + IMX_I2C_I2DR); /* dummy read */
382
383 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
384
385 /* read data */
386 for (i = 0; i < msgs->len; i++) {
387 result = i2c_imx_trx_complete(i2c_imx);
388 if (result)
389 return result;
390 if (i == (msgs->len - 1)) {
Richard Zhao43309f32009-10-17 17:46:22 +0800391 /* It must generate STOP before read I2DR to prevent
392 controller from generating another clock cycle */
Darius Augulisaa11e382009-01-30 10:32:28 +0200393 dev_dbg(&i2c_imx->adapter.dev,
394 "<%s> clear MSTA\n", __func__);
395 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800396 temp &= ~(I2CR_MSTA | I2CR_MTX);
Darius Augulisaa11e382009-01-30 10:32:28 +0200397 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800398 i2c_imx_bus_busy(i2c_imx, 0);
399 i2c_imx->stopped = 1;
Darius Augulisaa11e382009-01-30 10:32:28 +0200400 } else if (i == (msgs->len - 2)) {
401 dev_dbg(&i2c_imx->adapter.dev,
402 "<%s> set TXAK\n", __func__);
403 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
404 temp |= I2CR_TXAK;
405 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
406 }
407 msgs->buf[i] = readb(i2c_imx->base + IMX_I2C_I2DR);
408 dev_dbg(&i2c_imx->adapter.dev,
409 "<%s> read byte: B%d=0x%X\n",
410 __func__, i, msgs->buf[i]);
411 }
412 return 0;
413}
414
415static int i2c_imx_xfer(struct i2c_adapter *adapter,
416 struct i2c_msg *msgs, int num)
417{
418 unsigned int i, temp;
419 int result;
420 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
421
422 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
423
Richard Zhao43309f32009-10-17 17:46:22 +0800424 /* Start I2C transfer */
425 result = i2c_imx_start(i2c_imx);
Darius Augulisaa11e382009-01-30 10:32:28 +0200426 if (result)
427 goto fail0;
428
Darius Augulisaa11e382009-01-30 10:32:28 +0200429 /* read/write data */
430 for (i = 0; i < num; i++) {
431 if (i) {
432 dev_dbg(&i2c_imx->adapter.dev,
433 "<%s> repeated start\n", __func__);
434 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
435 temp |= I2CR_RSTA;
436 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800437 result = i2c_imx_bus_busy(i2c_imx, 1);
438 if (result)
439 goto fail0;
Darius Augulisaa11e382009-01-30 10:32:28 +0200440 }
441 dev_dbg(&i2c_imx->adapter.dev,
442 "<%s> transfer message: %d\n", __func__, i);
443 /* write/read data */
444#ifdef CONFIG_I2C_DEBUG_BUS
445 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
446 dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, "
447 "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__,
448 (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
449 (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
450 (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
451 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
452 dev_dbg(&i2c_imx->adapter.dev,
453 "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, "
454 "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__,
455 (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
456 (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
457 (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
458 (temp & I2SR_RXAK ? 1 : 0));
459#endif
460 if (msgs[i].flags & I2C_M_RD)
461 result = i2c_imx_read(i2c_imx, &msgs[i]);
462 else
463 result = i2c_imx_write(i2c_imx, &msgs[i]);
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100464 if (result)
465 goto fail0;
Darius Augulisaa11e382009-01-30 10:32:28 +0200466 }
467
468fail0:
469 /* Stop I2C transfer */
470 i2c_imx_stop(i2c_imx);
471
472 dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
473 (result < 0) ? "error" : "success msg",
474 (result < 0) ? result : num);
475 return (result < 0) ? result : num;
476}
477
478static u32 i2c_imx_func(struct i2c_adapter *adapter)
479{
480 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
481}
482
483static struct i2c_algorithm i2c_imx_algo = {
484 .master_xfer = i2c_imx_xfer,
485 .functionality = i2c_imx_func,
486};
487
488static int __init i2c_imx_probe(struct platform_device *pdev)
489{
Shawn Guo5bdfba22012-09-14 15:19:00 +0800490 const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
491 &pdev->dev);
Darius Augulisaa11e382009-01-30 10:32:28 +0200492 struct imx_i2c_struct *i2c_imx;
493 struct resource *res;
Shawn Guo593702c2011-09-08 15:09:34 +0800494 struct imxi2c_platform_data *pdata = pdev->dev.platform_data;
Darius Augulisaa11e382009-01-30 10:32:28 +0200495 void __iomem *base;
Wolfram Sang8c88ab02012-07-08 13:11:43 +0200496 int irq, ret;
497 u32 bitrate;
Darius Augulisaa11e382009-01-30 10:32:28 +0200498
499 dev_dbg(&pdev->dev, "<%s>\n", __func__);
500
501 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
502 if (!res) {
503 dev_err(&pdev->dev, "can't get device resources\n");
504 return -ENOENT;
505 }
506 irq = platform_get_irq(pdev, 0);
507 if (irq < 0) {
508 dev_err(&pdev->dev, "can't get irq number\n");
509 return -ENOENT;
510 }
511
Thierry Reding84dbf802013-01-21 11:09:03 +0100512 base = devm_ioremap_resource(&pdev->dev, res);
513 if (IS_ERR(base))
514 return PTR_ERR(base);
Uwe Kleine-König4927fbf2010-01-08 17:23:17 +0100515
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800516 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct),
517 GFP_KERNEL);
Darius Augulisaa11e382009-01-30 10:32:28 +0200518 if (!i2c_imx) {
519 dev_err(&pdev->dev, "can't allocate interface\n");
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800520 return -ENOMEM;
Darius Augulis309c18d2009-03-31 14:52:54 +0300521 }
522
Shawn Guo5bdfba22012-09-14 15:19:00 +0800523 if (of_id)
524 pdev->id_entry = of_id->data;
525 i2c_imx->devtype = pdev->id_entry->driver_data;
526
Darius Augulisaa11e382009-01-30 10:32:28 +0200527 /* Setup i2c_imx driver structure */
Wolfram Sang973c5ed2012-04-19 17:31:01 +0200528 strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
Darius Augulisaa11e382009-01-30 10:32:28 +0200529 i2c_imx->adapter.owner = THIS_MODULE;
530 i2c_imx->adapter.algo = &i2c_imx_algo;
531 i2c_imx->adapter.dev.parent = &pdev->dev;
532 i2c_imx->adapter.nr = pdev->id;
Shawn Guodfcd04b2011-09-08 15:09:35 +0800533 i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
Darius Augulisaa11e382009-01-30 10:32:28 +0200534 i2c_imx->base = base;
Darius Augulisaa11e382009-01-30 10:32:28 +0200535
536 /* Get I2C clock */
Fabio Estevam1f09c672012-07-06 15:31:32 -0300537 i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
Darius Augulisaa11e382009-01-30 10:32:28 +0200538 if (IS_ERR(i2c_imx->clk)) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200539 dev_err(&pdev->dev, "can't get I2C clock\n");
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800540 return PTR_ERR(i2c_imx->clk);
Darius Augulisaa11e382009-01-30 10:32:28 +0200541 }
Darius Augulisaa11e382009-01-30 10:32:28 +0200542
543 /* Request IRQ */
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800544 ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0,
545 pdev->name, i2c_imx);
Darius Augulisaa11e382009-01-30 10:32:28 +0200546 if (ret) {
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800547 dev_err(&pdev->dev, "can't claim irq %d\n", irq);
548 return ret;
Darius Augulisaa11e382009-01-30 10:32:28 +0200549 }
550
551 /* Init queue */
552 init_waitqueue_head(&i2c_imx->queue);
553
554 /* Set up adapter data */
555 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
556
557 /* Set up clock divider */
Shawn Guodfcd04b2011-09-08 15:09:35 +0800558 bitrate = IMX_I2C_BIT_RATE;
559 ret = of_property_read_u32(pdev->dev.of_node,
560 "clock-frequency", &bitrate);
561 if (ret < 0 && pdata && pdata->bitrate)
562 bitrate = pdata->bitrate;
563 i2c_imx_set_clk(i2c_imx, bitrate);
Darius Augulisaa11e382009-01-30 10:32:28 +0200564
565 /* Set up chip registers to defaults */
566 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
567 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
568
569 /* Add I2C adapter */
570 ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
571 if (ret < 0) {
572 dev_err(&pdev->dev, "registration failed\n");
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800573 return ret;
Darius Augulisaa11e382009-01-30 10:32:28 +0200574 }
575
Shawn Guodfcd04b2011-09-08 15:09:35 +0800576 of_i2c_register_devices(&i2c_imx->adapter);
577
Darius Augulisaa11e382009-01-30 10:32:28 +0200578 /* Set up platform driver data */
579 platform_set_drvdata(pdev, i2c_imx);
580
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800581 dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
Darius Augulisaa11e382009-01-30 10:32:28 +0200582 dev_dbg(&i2c_imx->adapter.dev, "device resources from 0x%x to 0x%x\n",
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800583 res->start, res->end);
584 dev_dbg(&i2c_imx->adapter.dev, "allocated %d bytes at 0x%x\n",
585 resource_size(res), res->start);
Darius Augulisaa11e382009-01-30 10:32:28 +0200586 dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
587 i2c_imx->adapter.name);
Fabio Estevam06d141e2012-08-01 17:38:14 -0300588 dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
Darius Augulisaa11e382009-01-30 10:32:28 +0200589
590 return 0; /* Return OK */
Darius Augulisaa11e382009-01-30 10:32:28 +0200591}
592
593static int __exit i2c_imx_remove(struct platform_device *pdev)
594{
595 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
Darius Augulisaa11e382009-01-30 10:32:28 +0200596
597 /* remove adapter */
598 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
599 i2c_del_adapter(&i2c_imx->adapter);
Darius Augulisaa11e382009-01-30 10:32:28 +0200600
Darius Augulisaa11e382009-01-30 10:32:28 +0200601 /* setup chip registers to defaults */
602 writeb(0, i2c_imx->base + IMX_I2C_IADR);
603 writeb(0, i2c_imx->base + IMX_I2C_IFDR);
604 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
605 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
606
Darius Augulisaa11e382009-01-30 10:32:28 +0200607 return 0;
608}
609
610static struct platform_driver i2c_imx_driver = {
Darius Augulisaa11e382009-01-30 10:32:28 +0200611 .remove = __exit_p(i2c_imx_remove),
612 .driver = {
613 .name = DRIVER_NAME,
614 .owner = THIS_MODULE,
Shawn Guodfcd04b2011-09-08 15:09:35 +0800615 .of_match_table = i2c_imx_dt_ids,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800616 },
617 .id_table = imx_i2c_devtype,
Darius Augulisaa11e382009-01-30 10:32:28 +0200618};
619
620static int __init i2c_adap_imx_init(void)
621{
622 return platform_driver_probe(&i2c_imx_driver, i2c_imx_probe);
623}
Wolfram Sang5d3f3332009-09-19 09:09:50 +0200624subsys_initcall(i2c_adap_imx_init);
Darius Augulisaa11e382009-01-30 10:32:28 +0200625
626static void __exit i2c_adap_imx_exit(void)
627{
628 platform_driver_unregister(&i2c_imx_driver);
629}
Darius Augulisaa11e382009-01-30 10:32:28 +0200630module_exit(i2c_adap_imx_exit);
631
632MODULE_LICENSE("GPL");
633MODULE_AUTHOR("Darius Augulis");
634MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
635MODULE_ALIAS("platform:" DRIVER_NAME);