blob: ccf46656bdad9182e2d75d6c1f3dbf42ea0c32c6 [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 *
Jingchang Lud533f042013-08-07 17:05:36 +080033 * Copyright 2013 Freescale Semiconductor, Inc.
34 *
Darius Augulisaa11e382009-01-30 10:32:28 +020035 */
36
37/** Includes *******************************************************************
38*******************************************************************************/
39
40#include <linux/init.h>
41#include <linux/kernel.h>
42#include <linux/module.h>
43#include <linux/errno.h>
44#include <linux/err.h>
45#include <linux/interrupt.h>
46#include <linux/delay.h>
47#include <linux/i2c.h>
48#include <linux/io.h>
49#include <linux/sched.h>
50#include <linux/platform_device.h>
51#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Shawn Guodfcd04b2011-09-08 15:09:35 +080053#include <linux/of.h>
54#include <linux/of_device.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020055#include <linux/platform_data/i2c-imx.h>
Darius Augulisaa11e382009-01-30 10:32:28 +020056
57/** Defines ********************************************************************
58*******************************************************************************/
59
60/* This will be the driver name the kernel reports */
61#define DRIVER_NAME "imx-i2c"
62
63/* Default value */
64#define IMX_I2C_BIT_RATE 100000 /* 100kHz */
65
Jingchang Lu8cc73312013-08-07 17:05:40 +080066/* IMX I2C registers:
67 * the I2C register offset is different between SoCs,
68 * to provid support for all these chips, split the
69 * register offset into a fixed base address and a
70 * variable shift value, then the full register offset
71 * will be calculated by
72 * reg_off = ( reg_base_addr << reg_shift)
73 */
Darius Augulisaa11e382009-01-30 10:32:28 +020074#define IMX_I2C_IADR 0x00 /* i2c slave address */
Jingchang Lu8cc73312013-08-07 17:05:40 +080075#define IMX_I2C_IFDR 0x01 /* i2c frequency divider */
76#define IMX_I2C_I2CR 0x02 /* i2c control */
77#define IMX_I2C_I2SR 0x03 /* i2c status */
78#define IMX_I2C_I2DR 0x04 /* i2c transfer data */
79
80#define IMX_I2C_REGSHIFT 2
Jingchang Luad90efa2013-08-07 17:05:43 +080081#define VF610_I2C_REGSHIFT 0
Darius Augulisaa11e382009-01-30 10:32:28 +020082
83/* Bits of IMX I2C registers */
84#define I2SR_RXAK 0x01
85#define I2SR_IIF 0x02
86#define I2SR_SRW 0x04
87#define I2SR_IAL 0x10
88#define I2SR_IBB 0x20
89#define I2SR_IAAS 0x40
90#define I2SR_ICF 0x80
91#define I2CR_RSTA 0x04
92#define I2CR_TXAK 0x08
93#define I2CR_MTX 0x10
94#define I2CR_MSTA 0x20
95#define I2CR_IIEN 0x40
96#define I2CR_IEN 0x80
97
Jingchang Lu171408c2013-08-07 17:05:41 +080098/* register bits different operating codes definition:
99 * 1) I2SR: Interrupt flags clear operation differ between SoCs:
100 * - write zero to clear(w0c) INT flag on i.MX,
101 * - but write one to clear(w1c) INT flag on Vybrid.
102 * 2) I2CR: I2C module enable operation also differ between SoCs:
103 * - set I2CR_IEN bit enable the module on i.MX,
104 * - but clear I2CR_IEN bit enable the module on Vybrid.
105 */
106#define I2SR_CLR_OPCODE_W0C 0x0
107#define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF)
108#define I2CR_IEN_OPCODE_0 0x0
109#define I2CR_IEN_OPCODE_1 I2CR_IEN
110
Darius Augulisaa11e382009-01-30 10:32:28 +0200111/** Variables ******************************************************************
112*******************************************************************************/
113
Darius Augulisaa11e382009-01-30 10:32:28 +0200114/*
115 * sorted list of clock divider, register value pairs
116 * taken from table 26-5, p.26-9, Freescale i.MX
117 * Integrated Portable System Processor Reference Manual
118 * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
119 *
120 * Duplicated divider values removed from list
121 */
Jingchang Lud533f042013-08-07 17:05:36 +0800122struct imx_i2c_clk_pair {
123 u16 div;
124 u16 val;
125};
Darius Augulisaa11e382009-01-30 10:32:28 +0200126
Jingchang Lu4b775022013-08-07 17:05:42 +0800127static struct imx_i2c_clk_pair imx_i2c_clk_div[] = {
Darius Augulisaa11e382009-01-30 10:32:28 +0200128 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
129 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
130 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
131 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
132 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
133 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
134 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
135 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
136 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
137 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
138 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
139 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
140 { 3072, 0x1E }, { 3840, 0x1F }
141};
142
Jingchang Luad90efa2013-08-07 17:05:43 +0800143/* Vybrid VF610 clock divider, register value pairs */
144static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = {
145 { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
146 { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
147 { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
148 { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
149 { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
150 { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
151 { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
152 { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
153 { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
154 { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
155 { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
156 { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
157 { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
158 { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
159 { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
160};
161
Shawn Guo5bdfba22012-09-14 15:19:00 +0800162enum imx_i2c_type {
163 IMX1_I2C,
164 IMX21_I2C,
Jingchang Luad90efa2013-08-07 17:05:43 +0800165 VF610_I2C,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800166};
167
Jingchang Lu4b775022013-08-07 17:05:42 +0800168struct imx_i2c_hwdata {
169 enum imx_i2c_type devtype;
170 unsigned regshift;
171 struct imx_i2c_clk_pair *clk_div;
172 unsigned ndivs;
173 unsigned i2sr_clr_opcode;
174 unsigned i2cr_ien_opcode;
175};
176
Darius Augulisaa11e382009-01-30 10:32:28 +0200177struct imx_i2c_struct {
178 struct i2c_adapter adapter;
Darius Augulisaa11e382009-01-30 10:32:28 +0200179 struct clk *clk;
180 void __iomem *base;
Darius Augulisaa11e382009-01-30 10:32:28 +0200181 wait_queue_head_t queue;
182 unsigned long i2csr;
Wolfram Sang65de3942009-04-06 16:27:45 +0200183 unsigned int disable_delay;
Richard Zhao43309f32009-10-17 17:46:22 +0800184 int stopped;
Richard Zhaodb3a3d42009-10-17 17:46:24 +0800185 unsigned int ifdr; /* IMX_I2C_IFDR */
Jingchang Lu4b775022013-08-07 17:05:42 +0800186 const struct imx_i2c_hwdata *hwdata;
187};
188
189static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
190 .devtype = IMX1_I2C,
191 .regshift = IMX_I2C_REGSHIFT,
192 .clk_div = imx_i2c_clk_div,
193 .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
194 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
195 .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
196
197};
198
199static const struct imx_i2c_hwdata imx21_i2c_hwdata = {
200 .devtype = IMX21_I2C,
201 .regshift = IMX_I2C_REGSHIFT,
202 .clk_div = imx_i2c_clk_div,
203 .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
204 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
205 .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
206
Darius Augulisaa11e382009-01-30 10:32:28 +0200207};
208
Jingchang Luad90efa2013-08-07 17:05:43 +0800209static struct imx_i2c_hwdata vf610_i2c_hwdata = {
210 .devtype = VF610_I2C,
211 .regshift = VF610_I2C_REGSHIFT,
212 .clk_div = vf610_i2c_clk_div,
213 .ndivs = ARRAY_SIZE(vf610_i2c_clk_div),
214 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C,
215 .i2cr_ien_opcode = I2CR_IEN_OPCODE_0,
216
217};
218
Shawn Guo5bdfba22012-09-14 15:19:00 +0800219static struct platform_device_id imx_i2c_devtype[] = {
220 {
221 .name = "imx1-i2c",
Jingchang Lu4b775022013-08-07 17:05:42 +0800222 .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800223 }, {
224 .name = "imx21-i2c",
Jingchang Lu4b775022013-08-07 17:05:42 +0800225 .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800226 }, {
227 /* sentinel */
228 }
229};
230MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
231
Shawn Guodfcd04b2011-09-08 15:09:35 +0800232static const struct of_device_id i2c_imx_dt_ids[] = {
Jingchang Lu4b775022013-08-07 17:05:42 +0800233 { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, },
234 { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, },
Jingchang Luad90efa2013-08-07 17:05:43 +0800235 { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, },
Shawn Guodfcd04b2011-09-08 15:09:35 +0800236 { /* sentinel */ }
237};
Arnaud Patard \(Rtp\)2f641a82013-06-20 23:07:06 +0200238MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids);
Shawn Guodfcd04b2011-09-08 15:09:35 +0800239
Shawn Guo5bdfba22012-09-14 15:19:00 +0800240static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx)
241{
Jingchang Lu4b775022013-08-07 17:05:42 +0800242 return i2c_imx->hwdata->devtype == IMX1_I2C;
Shawn Guo5bdfba22012-09-14 15:19:00 +0800243}
244
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800245static inline void imx_i2c_write_reg(unsigned int val,
246 struct imx_i2c_struct *i2c_imx, unsigned int reg)
247{
Jingchang Lu4b775022013-08-07 17:05:42 +0800248 writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800249}
250
251static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx,
252 unsigned int reg)
253{
Jingchang Lu4b775022013-08-07 17:05:42 +0800254 return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800255}
256
Darius Augulisaa11e382009-01-30 10:32:28 +0200257/** Functions for IMX I2C adapter driver ***************************************
258*******************************************************************************/
259
Richard Zhao43309f32009-10-17 17:46:22 +0800260static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
Darius Augulisaa11e382009-01-30 10:32:28 +0200261{
262 unsigned long orig_jiffies = jiffies;
Richard Zhao43309f32009-10-17 17:46:22 +0800263 unsigned int temp;
Darius Augulisaa11e382009-01-30 10:32:28 +0200264
265 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
266
Richard Zhao43309f32009-10-17 17:46:22 +0800267 while (1) {
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800268 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
Richard Zhao43309f32009-10-17 17:46:22 +0800269 if (for_busy && (temp & I2SR_IBB))
270 break;
271 if (!for_busy && !(temp & I2SR_IBB))
272 break;
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100273 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200274 dev_dbg(&i2c_imx->adapter.dev,
275 "<%s> I2C bus is busy\n", __func__);
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100276 return -ETIMEDOUT;
Darius Augulisaa11e382009-01-30 10:32:28 +0200277 }
278 schedule();
279 }
280
281 return 0;
282}
283
284static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
285{
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200286 wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
Darius Augulisaa11e382009-01-30 10:32:28 +0200287
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200288 if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200289 dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
290 return -ETIMEDOUT;
291 }
292 dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
293 i2c_imx->i2csr = 0;
294 return 0;
295}
296
297static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
298{
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800299 if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200300 dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
301 return -EIO; /* No ACK */
302 }
303
304 dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
305 return 0;
306}
307
Richard Zhao43309f32009-10-17 17:46:22 +0800308static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
Darius Augulisaa11e382009-01-30 10:32:28 +0200309{
310 unsigned int temp = 0;
Richard Zhao43309f32009-10-17 17:46:22 +0800311 int result;
Darius Augulisaa11e382009-01-30 10:32:28 +0200312
313 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
314
Richard Zhao83914332011-11-15 14:48:08 +0800315 clk_prepare_enable(i2c_imx->clk);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800316 imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200317 /* Enable I2C controller */
Jingchang Lu4b775022013-08-07 17:05:42 +0800318 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
319 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800320
321 /* Wait controller to be stable */
322 udelay(50);
323
Darius Augulisaa11e382009-01-30 10:32:28 +0200324 /* Start I2C transaction */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800325 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200326 temp |= I2CR_MSTA;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800327 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800328 result = i2c_imx_bus_busy(i2c_imx, 1);
329 if (result)
330 return result;
331 i2c_imx->stopped = 0;
332
Darius Augulisaa11e382009-01-30 10:32:28 +0200333 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800334 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800335 return result;
Darius Augulisaa11e382009-01-30 10:32:28 +0200336}
337
338static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
339{
340 unsigned int temp = 0;
341
Richard Zhao43309f32009-10-17 17:46:22 +0800342 if (!i2c_imx->stopped) {
343 /* Stop I2C transaction */
344 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800345 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800346 temp &= ~(I2CR_MSTA | I2CR_MTX);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800347 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800348 }
Shawn Guo5bdfba22012-09-14 15:19:00 +0800349 if (is_imx1_i2c(i2c_imx)) {
Richard Zhaoa4094a72009-10-17 17:46:23 +0800350 /*
351 * This delay caused by an i.MXL hardware bug.
352 * If no (or too short) delay, no "STOP" bit will be generated.
353 */
354 udelay(i2c_imx->disable_delay);
355 }
Richard Zhao43309f32009-10-17 17:46:22 +0800356
Valentin Longchampa1ee06b2010-01-21 18:55:32 +0100357 if (!i2c_imx->stopped) {
Richard Zhao43309f32009-10-17 17:46:22 +0800358 i2c_imx_bus_busy(i2c_imx, 0);
Valentin Longchampa1ee06b2010-01-21 18:55:32 +0100359 i2c_imx->stopped = 1;
360 }
Richard Zhao43309f32009-10-17 17:46:22 +0800361
Darius Augulisaa11e382009-01-30 10:32:28 +0200362 /* Disable I2C controller */
Jingchang Lu4b775022013-08-07 17:05:42 +0800363 temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
364 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao83914332011-11-15 14:48:08 +0800365 clk_disable_unprepare(i2c_imx->clk);
Darius Augulisaa11e382009-01-30 10:32:28 +0200366}
367
368static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
369 unsigned int rate)
370{
Jingchang Lu4b775022013-08-07 17:05:42 +0800371 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
Darius Augulisaa11e382009-01-30 10:32:28 +0200372 unsigned int i2c_clk_rate;
373 unsigned int div;
374 int i;
375
376 /* Divider value calculation */
377 i2c_clk_rate = clk_get_rate(i2c_imx->clk);
378 div = (i2c_clk_rate + rate - 1) / rate;
Jingchang Lud533f042013-08-07 17:05:36 +0800379 if (div < i2c_clk_div[0].div)
Darius Augulisaa11e382009-01-30 10:32:28 +0200380 i = 0;
Jingchang Lu4b775022013-08-07 17:05:42 +0800381 else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
382 i = i2c_imx->hwdata->ndivs - 1;
Darius Augulisaa11e382009-01-30 10:32:28 +0200383 else
Jingchang Lud533f042013-08-07 17:05:36 +0800384 for (i = 0; i2c_clk_div[i].div < div; i++);
Darius Augulisaa11e382009-01-30 10:32:28 +0200385
Richard Zhaodb3a3d42009-10-17 17:46:24 +0800386 /* Store divider value */
Jingchang Lud533f042013-08-07 17:05:36 +0800387 i2c_imx->ifdr = i2c_clk_div[i].val;
Darius Augulisaa11e382009-01-30 10:32:28 +0200388
389 /*
390 * There dummy delay is calculated.
391 * It should be about one I2C clock period long.
392 * This delay is used in I2C bus disable function
393 * to fix chip hardware bug.
394 */
Jingchang Lud533f042013-08-07 17:05:36 +0800395 i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
Darius Augulisaa11e382009-01-30 10:32:28 +0200396 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
397
398 /* dev_dbg() can't be used, because adapter is not yet registered */
399#ifdef CONFIG_I2C_DEBUG_BUS
Fabio Estevam002f0022012-08-01 17:38:15 -0300400 dev_dbg(&i2c_imx->adapter.dev, "<%s> I2C_CLK=%d, REQ DIV=%d\n",
Darius Augulisaa11e382009-01-30 10:32:28 +0200401 __func__, i2c_clk_rate, div);
Fabio Estevam002f0022012-08-01 17:38:15 -0300402 dev_dbg(&i2c_imx->adapter.dev, "<%s> IFDR[IC]=0x%x, REAL DIV=%d\n",
Jingchang Lud533f042013-08-07 17:05:36 +0800403 __func__, i2c_clk_div[i].val, i2c_clk_div[i].div);
Darius Augulisaa11e382009-01-30 10:32:28 +0200404#endif
405}
406
407static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
408{
409 struct imx_i2c_struct *i2c_imx = dev_id;
410 unsigned int temp;
411
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800412 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200413 if (temp & I2SR_IIF) {
414 /* save status register */
415 i2c_imx->i2csr = temp;
416 temp &= ~I2SR_IIF;
Jingchang Lu4b775022013-08-07 17:05:42 +0800417 temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800418 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200419 wake_up(&i2c_imx->queue);
Darius Augulisaa11e382009-01-30 10:32:28 +0200420 return IRQ_HANDLED;
421 }
422
423 return IRQ_NONE;
424}
425
426static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
427{
428 int i, result;
429
430 dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
431 __func__, msgs->addr << 1);
432
433 /* write slave address */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800434 imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200435 result = i2c_imx_trx_complete(i2c_imx);
436 if (result)
437 return result;
438 result = i2c_imx_acked(i2c_imx);
439 if (result)
440 return result;
441 dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
442
443 /* write data */
444 for (i = 0; i < msgs->len; i++) {
445 dev_dbg(&i2c_imx->adapter.dev,
446 "<%s> write byte: B%d=0x%X\n",
447 __func__, i, msgs->buf[i]);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800448 imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200449 result = i2c_imx_trx_complete(i2c_imx);
450 if (result)
451 return result;
452 result = i2c_imx_acked(i2c_imx);
453 if (result)
454 return result;
455 }
456 return 0;
457}
458
459static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
460{
461 int i, result;
462 unsigned int temp;
463
464 dev_dbg(&i2c_imx->adapter.dev,
465 "<%s> write slave address: addr=0x%x\n",
466 __func__, (msgs->addr << 1) | 0x01);
467
468 /* write slave address */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800469 imx_i2c_write_reg((msgs->addr << 1) | 0x01, i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200470 result = i2c_imx_trx_complete(i2c_imx);
471 if (result)
472 return result;
473 result = i2c_imx_acked(i2c_imx);
474 if (result)
475 return result;
476
477 dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
478
479 /* setup bus to read data */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800480 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200481 temp &= ~I2CR_MTX;
482 if (msgs->len - 1)
483 temp &= ~I2CR_TXAK;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800484 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
485 imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
Darius Augulisaa11e382009-01-30 10:32:28 +0200486
487 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
488
489 /* read data */
490 for (i = 0; i < msgs->len; i++) {
491 result = i2c_imx_trx_complete(i2c_imx);
492 if (result)
493 return result;
494 if (i == (msgs->len - 1)) {
Richard Zhao43309f32009-10-17 17:46:22 +0800495 /* It must generate STOP before read I2DR to prevent
496 controller from generating another clock cycle */
Darius Augulisaa11e382009-01-30 10:32:28 +0200497 dev_dbg(&i2c_imx->adapter.dev,
498 "<%s> clear MSTA\n", __func__);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800499 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800500 temp &= ~(I2CR_MSTA | I2CR_MTX);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800501 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800502 i2c_imx_bus_busy(i2c_imx, 0);
503 i2c_imx->stopped = 1;
Darius Augulisaa11e382009-01-30 10:32:28 +0200504 } else if (i == (msgs->len - 2)) {
505 dev_dbg(&i2c_imx->adapter.dev,
506 "<%s> set TXAK\n", __func__);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800507 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200508 temp |= I2CR_TXAK;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800509 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200510 }
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800511 msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200512 dev_dbg(&i2c_imx->adapter.dev,
513 "<%s> read byte: B%d=0x%X\n",
514 __func__, i, msgs->buf[i]);
515 }
516 return 0;
517}
518
519static int i2c_imx_xfer(struct i2c_adapter *adapter,
520 struct i2c_msg *msgs, int num)
521{
522 unsigned int i, temp;
523 int result;
524 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
525
526 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
527
Richard Zhao43309f32009-10-17 17:46:22 +0800528 /* Start I2C transfer */
529 result = i2c_imx_start(i2c_imx);
Darius Augulisaa11e382009-01-30 10:32:28 +0200530 if (result)
531 goto fail0;
532
Darius Augulisaa11e382009-01-30 10:32:28 +0200533 /* read/write data */
534 for (i = 0; i < num; i++) {
535 if (i) {
536 dev_dbg(&i2c_imx->adapter.dev,
537 "<%s> repeated start\n", __func__);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800538 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200539 temp |= I2CR_RSTA;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800540 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800541 result = i2c_imx_bus_busy(i2c_imx, 1);
542 if (result)
543 goto fail0;
Darius Augulisaa11e382009-01-30 10:32:28 +0200544 }
545 dev_dbg(&i2c_imx->adapter.dev,
546 "<%s> transfer message: %d\n", __func__, i);
547 /* write/read data */
548#ifdef CONFIG_I2C_DEBUG_BUS
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800549 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200550 dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, "
551 "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__,
552 (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
553 (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
554 (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800555 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200556 dev_dbg(&i2c_imx->adapter.dev,
557 "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, "
558 "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__,
559 (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
560 (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
561 (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
562 (temp & I2SR_RXAK ? 1 : 0));
563#endif
564 if (msgs[i].flags & I2C_M_RD)
565 result = i2c_imx_read(i2c_imx, &msgs[i]);
566 else
567 result = i2c_imx_write(i2c_imx, &msgs[i]);
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100568 if (result)
569 goto fail0;
Darius Augulisaa11e382009-01-30 10:32:28 +0200570 }
571
572fail0:
573 /* Stop I2C transfer */
574 i2c_imx_stop(i2c_imx);
575
576 dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
577 (result < 0) ? "error" : "success msg",
578 (result < 0) ? result : num);
579 return (result < 0) ? result : num;
580}
581
582static u32 i2c_imx_func(struct i2c_adapter *adapter)
583{
584 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
585}
586
587static struct i2c_algorithm i2c_imx_algo = {
588 .master_xfer = i2c_imx_xfer,
589 .functionality = i2c_imx_func,
590};
591
592static int __init i2c_imx_probe(struct platform_device *pdev)
593{
Shawn Guo5bdfba22012-09-14 15:19:00 +0800594 const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
595 &pdev->dev);
Darius Augulisaa11e382009-01-30 10:32:28 +0200596 struct imx_i2c_struct *i2c_imx;
597 struct resource *res;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900598 struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
Darius Augulisaa11e382009-01-30 10:32:28 +0200599 void __iomem *base;
Wolfram Sang8c88ab02012-07-08 13:11:43 +0200600 int irq, ret;
601 u32 bitrate;
Darius Augulisaa11e382009-01-30 10:32:28 +0200602
603 dev_dbg(&pdev->dev, "<%s>\n", __func__);
604
Darius Augulisaa11e382009-01-30 10:32:28 +0200605 irq = platform_get_irq(pdev, 0);
606 if (irq < 0) {
607 dev_err(&pdev->dev, "can't get irq number\n");
608 return -ENOENT;
609 }
610
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200611 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100612 base = devm_ioremap_resource(&pdev->dev, res);
613 if (IS_ERR(base))
614 return PTR_ERR(base);
Uwe Kleine-König4927fbf2010-01-08 17:23:17 +0100615
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800616 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct),
617 GFP_KERNEL);
Darius Augulisaa11e382009-01-30 10:32:28 +0200618 if (!i2c_imx) {
619 dev_err(&pdev->dev, "can't allocate interface\n");
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800620 return -ENOMEM;
Darius Augulis309c18d2009-03-31 14:52:54 +0300621 }
622
Shawn Guo5bdfba22012-09-14 15:19:00 +0800623 if (of_id)
Jingchang Lu4b775022013-08-07 17:05:42 +0800624 i2c_imx->hwdata = of_id->data;
Jingchang Lu0fc13472013-08-07 17:05:38 +0800625 else
Jingchang Lu4b775022013-08-07 17:05:42 +0800626 i2c_imx->hwdata = (struct imx_i2c_hwdata *)
627 platform_get_device_id(pdev)->driver_data;
Shawn Guo5bdfba22012-09-14 15:19:00 +0800628
Darius Augulisaa11e382009-01-30 10:32:28 +0200629 /* Setup i2c_imx driver structure */
Wolfram Sang973c5ed2012-04-19 17:31:01 +0200630 strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
Darius Augulisaa11e382009-01-30 10:32:28 +0200631 i2c_imx->adapter.owner = THIS_MODULE;
632 i2c_imx->adapter.algo = &i2c_imx_algo;
633 i2c_imx->adapter.dev.parent = &pdev->dev;
634 i2c_imx->adapter.nr = pdev->id;
Shawn Guodfcd04b2011-09-08 15:09:35 +0800635 i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
Darius Augulisaa11e382009-01-30 10:32:28 +0200636 i2c_imx->base = base;
Darius Augulisaa11e382009-01-30 10:32:28 +0200637
638 /* Get I2C clock */
Fabio Estevam1f09c672012-07-06 15:31:32 -0300639 i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
Darius Augulisaa11e382009-01-30 10:32:28 +0200640 if (IS_ERR(i2c_imx->clk)) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200641 dev_err(&pdev->dev, "can't get I2C clock\n");
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800642 return PTR_ERR(i2c_imx->clk);
Darius Augulisaa11e382009-01-30 10:32:28 +0200643 }
Darius Augulisaa11e382009-01-30 10:32:28 +0200644
Jingchang Lu46f28322013-08-07 17:05:37 +0800645 ret = clk_prepare_enable(i2c_imx->clk);
646 if (ret) {
647 dev_err(&pdev->dev, "can't enable I2C clock\n");
648 return ret;
649 }
Darius Augulisaa11e382009-01-30 10:32:28 +0200650 /* Request IRQ */
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800651 ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0,
652 pdev->name, i2c_imx);
Darius Augulisaa11e382009-01-30 10:32:28 +0200653 if (ret) {
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800654 dev_err(&pdev->dev, "can't claim irq %d\n", irq);
655 return ret;
Darius Augulisaa11e382009-01-30 10:32:28 +0200656 }
657
658 /* Init queue */
659 init_waitqueue_head(&i2c_imx->queue);
660
661 /* Set up adapter data */
662 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
663
664 /* Set up clock divider */
Shawn Guodfcd04b2011-09-08 15:09:35 +0800665 bitrate = IMX_I2C_BIT_RATE;
666 ret = of_property_read_u32(pdev->dev.of_node,
667 "clock-frequency", &bitrate);
668 if (ret < 0 && pdata && pdata->bitrate)
669 bitrate = pdata->bitrate;
670 i2c_imx_set_clk(i2c_imx, bitrate);
Darius Augulisaa11e382009-01-30 10:32:28 +0200671
672 /* Set up chip registers to defaults */
Jingchang Lu4b775022013-08-07 17:05:42 +0800673 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
674 i2c_imx, IMX_I2C_I2CR);
675 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200676
677 /* Add I2C adapter */
678 ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
679 if (ret < 0) {
680 dev_err(&pdev->dev, "registration failed\n");
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800681 return ret;
Darius Augulisaa11e382009-01-30 10:32:28 +0200682 }
683
684 /* Set up platform driver data */
685 platform_set_drvdata(pdev, i2c_imx);
Jingchang Lu46f28322013-08-07 17:05:37 +0800686 clk_disable_unprepare(i2c_imx->clk);
Darius Augulisaa11e382009-01-30 10:32:28 +0200687
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800688 dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
Darius Augulisaa11e382009-01-30 10:32:28 +0200689 dev_dbg(&i2c_imx->adapter.dev, "device resources from 0x%x to 0x%x\n",
Richard Zhao9f8a3e72012-06-04 19:04:25 +0800690 res->start, res->end);
691 dev_dbg(&i2c_imx->adapter.dev, "allocated %d bytes at 0x%x\n",
692 resource_size(res), res->start);
Darius Augulisaa11e382009-01-30 10:32:28 +0200693 dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
694 i2c_imx->adapter.name);
Fabio Estevam06d141e2012-08-01 17:38:14 -0300695 dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
Darius Augulisaa11e382009-01-30 10:32:28 +0200696
697 return 0; /* Return OK */
Darius Augulisaa11e382009-01-30 10:32:28 +0200698}
699
700static int __exit i2c_imx_remove(struct platform_device *pdev)
701{
702 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
Darius Augulisaa11e382009-01-30 10:32:28 +0200703
704 /* remove adapter */
705 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
706 i2c_del_adapter(&i2c_imx->adapter);
Darius Augulisaa11e382009-01-30 10:32:28 +0200707
Darius Augulisaa11e382009-01-30 10:32:28 +0200708 /* setup chip registers to defaults */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800709 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
710 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);
711 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR);
712 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200713
Darius Augulisaa11e382009-01-30 10:32:28 +0200714 return 0;
715}
716
717static struct platform_driver i2c_imx_driver = {
Darius Augulisaa11e382009-01-30 10:32:28 +0200718 .remove = __exit_p(i2c_imx_remove),
719 .driver = {
720 .name = DRIVER_NAME,
721 .owner = THIS_MODULE,
Shawn Guodfcd04b2011-09-08 15:09:35 +0800722 .of_match_table = i2c_imx_dt_ids,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800723 },
724 .id_table = imx_i2c_devtype,
Darius Augulisaa11e382009-01-30 10:32:28 +0200725};
726
727static int __init i2c_adap_imx_init(void)
728{
729 return platform_driver_probe(&i2c_imx_driver, i2c_imx_probe);
730}
Wolfram Sang5d3f3332009-09-19 09:09:50 +0200731subsys_initcall(i2c_adap_imx_init);
Darius Augulisaa11e382009-01-30 10:32:28 +0200732
733static void __exit i2c_adap_imx_exit(void)
734{
735 platform_driver_unregister(&i2c_imx_driver);
736}
Darius Augulisaa11e382009-01-30 10:32:28 +0200737module_exit(i2c_adap_imx_exit);
738
739MODULE_LICENSE("GPL");
740MODULE_AUTHOR("Darius Augulis");
741MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
742MODULE_ALIAS("platform:" DRIVER_NAME);