Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 51 | #include <linux/of.h> |
| 52 | #include <linux/of_device.h> |
| 53 | #include <linux/of_i2c.h> |
Arnd Bergmann | 82906b1 | 2012-08-24 15:14:29 +0200 | [diff] [blame] | 54 | #include <linux/platform_data/i2c-imx.h> |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 55 | |
| 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 Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 90 | /* |
| 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 | |
| 99 | static 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 Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 115 | enum imx_i2c_type { |
| 116 | IMX1_I2C, |
| 117 | IMX21_I2C, |
| 118 | }; |
| 119 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 120 | struct imx_i2c_struct { |
| 121 | struct i2c_adapter adapter; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 122 | struct clk *clk; |
| 123 | void __iomem *base; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 124 | wait_queue_head_t queue; |
| 125 | unsigned long i2csr; |
Wolfram Sang | 65de394 | 2009-04-06 16:27:45 +0200 | [diff] [blame] | 126 | unsigned int disable_delay; |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 127 | int stopped; |
Richard Zhao | db3a3d4 | 2009-10-17 17:46:24 +0800 | [diff] [blame] | 128 | unsigned int ifdr; /* IMX_I2C_IFDR */ |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 129 | enum imx_i2c_type devtype; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 132 | static 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 | }; |
| 143 | MODULE_DEVICE_TABLE(platform, imx_i2c_devtype); |
| 144 | |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 145 | static const struct of_device_id i2c_imx_dt_ids[] = { |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 146 | { .compatible = "fsl,imx1-i2c", .data = &imx_i2c_devtype[IMX1_I2C], }, |
| 147 | { .compatible = "fsl,imx21-i2c", .data = &imx_i2c_devtype[IMX21_I2C], }, |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 148 | { /* sentinel */ } |
| 149 | }; |
| 150 | |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 151 | static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx) |
| 152 | { |
| 153 | return i2c_imx->devtype == IMX1_I2C; |
| 154 | } |
| 155 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 156 | /** Functions for IMX I2C adapter driver *************************************** |
| 157 | *******************************************************************************/ |
| 158 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 159 | static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy) |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 160 | { |
| 161 | unsigned long orig_jiffies = jiffies; |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 162 | unsigned int temp; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 163 | |
| 164 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
| 165 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 166 | 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 Patard | da9c99f | 2010-03-23 17:28:28 +0100 | [diff] [blame] | 172 | if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 173 | dev_dbg(&i2c_imx->adapter.dev, |
| 174 | "<%s> I2C bus is busy\n", __func__); |
Arnaud Patard | da9c99f | 2010-03-23 17:28:28 +0100 | [diff] [blame] | 175 | return -ETIMEDOUT; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 176 | } |
| 177 | schedule(); |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) |
| 184 | { |
Marc Kleine-Budde | e39428d | 2010-06-21 09:27:05 +0200 | [diff] [blame] | 185 | wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 186 | |
Marc Kleine-Budde | e39428d | 2010-06-21 09:27:05 +0200 | [diff] [blame] | 187 | if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 188 | 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 | |
| 196 | static 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 Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 207 | static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 208 | { |
| 209 | unsigned int temp = 0; |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 210 | int result; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 211 | |
| 212 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
| 213 | |
Richard Zhao | 8391433 | 2011-11-15 14:48:08 +0800 | [diff] [blame] | 214 | clk_prepare_enable(i2c_imx->clk); |
Richard Zhao | db3a3d4 | 2009-10-17 17:46:24 +0800 | [diff] [blame] | 215 | writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 216 | /* Enable I2C controller */ |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 217 | writeb(0, i2c_imx->base + IMX_I2C_I2SR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 218 | writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 219 | |
| 220 | /* Wait controller to be stable */ |
| 221 | udelay(50); |
| 222 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 223 | /* 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 Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 227 | result = i2c_imx_bus_busy(i2c_imx, 1); |
| 228 | if (result) |
| 229 | return result; |
| 230 | i2c_imx->stopped = 0; |
| 231 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 232 | temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK; |
| 233 | writeb(temp, i2c_imx->base + IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 234 | return result; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx) |
| 238 | { |
| 239 | unsigned int temp = 0; |
| 240 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 241 | 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 Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 247 | } |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 248 | if (is_imx1_i2c(i2c_imx)) { |
Richard Zhao | a4094a7 | 2009-10-17 17:46:23 +0800 | [diff] [blame] | 249 | /* |
| 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 Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 255 | |
Valentin Longchamp | a1ee06b | 2010-01-21 18:55:32 +0100 | [diff] [blame] | 256 | if (!i2c_imx->stopped) { |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 257 | i2c_imx_bus_busy(i2c_imx, 0); |
Valentin Longchamp | a1ee06b | 2010-01-21 18:55:32 +0100 | [diff] [blame] | 258 | i2c_imx->stopped = 1; |
| 259 | } |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 260 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 261 | /* Disable I2C controller */ |
| 262 | writeb(0, i2c_imx->base + IMX_I2C_I2CR); |
Richard Zhao | 8391433 | 2011-11-15 14:48:08 +0800 | [diff] [blame] | 263 | clk_disable_unprepare(i2c_imx->clk); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static 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 Zhao | db3a3d4 | 2009-10-17 17:46:24 +0800 | [diff] [blame] | 283 | /* Store divider value */ |
| 284 | i2c_imx->ifdr = i2c_clk_div[i][1]; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 285 | |
| 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 Sang | 65de394 | 2009-04-06 16:27:45 +0200 | [diff] [blame] | 292 | i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0] |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 293 | + (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 Estevam | 002f002 | 2012-08-01 17:38:15 -0300 | [diff] [blame] | 297 | dev_dbg(&i2c_imx->adapter.dev, "<%s> I2C_CLK=%d, REQ DIV=%d\n", |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 298 | __func__, i2c_clk_rate, div); |
Fabio Estevam | 002f002 | 2012-08-01 17:38:15 -0300 | [diff] [blame] | 299 | dev_dbg(&i2c_imx->adapter.dev, "<%s> IFDR[IC]=0x%x, REAL DIV=%d\n", |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 300 | __func__, i2c_clk_div[i][1], i2c_clk_div[i][0]); |
| 301 | #endif |
| 302 | } |
| 303 | |
| 304 | static 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-Budde | e39428d | 2010-06-21 09:27:05 +0200 | [diff] [blame] | 315 | wake_up(&i2c_imx->queue); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 316 | return IRQ_HANDLED; |
| 317 | } |
| 318 | |
| 319 | return IRQ_NONE; |
| 320 | } |
| 321 | |
| 322 | static 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 | |
| 355 | static 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 Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 391 | /* It must generate STOP before read I2DR to prevent |
| 392 | controller from generating another clock cycle */ |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 393 | dev_dbg(&i2c_imx->adapter.dev, |
| 394 | "<%s> clear MSTA\n", __func__); |
| 395 | temp = readb(i2c_imx->base + IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 396 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 397 | writeb(temp, i2c_imx->base + IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 398 | i2c_imx_bus_busy(i2c_imx, 0); |
| 399 | i2c_imx->stopped = 1; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 400 | } 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 | |
| 415 | static 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 Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 424 | /* Start I2C transfer */ |
| 425 | result = i2c_imx_start(i2c_imx); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 426 | if (result) |
| 427 | goto fail0; |
| 428 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 429 | /* 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 Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 437 | result = i2c_imx_bus_busy(i2c_imx, 1); |
| 438 | if (result) |
| 439 | goto fail0; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 440 | } |
| 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 Patard | da9c99f | 2010-03-23 17:28:28 +0100 | [diff] [blame] | 464 | if (result) |
| 465 | goto fail0; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | fail0: |
| 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 | |
| 478 | static u32 i2c_imx_func(struct i2c_adapter *adapter) |
| 479 | { |
| 480 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 481 | } |
| 482 | |
| 483 | static struct i2c_algorithm i2c_imx_algo = { |
| 484 | .master_xfer = i2c_imx_xfer, |
| 485 | .functionality = i2c_imx_func, |
| 486 | }; |
| 487 | |
| 488 | static int __init i2c_imx_probe(struct platform_device *pdev) |
| 489 | { |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 490 | const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids, |
| 491 | &pdev->dev); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 492 | struct imx_i2c_struct *i2c_imx; |
| 493 | struct resource *res; |
Shawn Guo | 593702c | 2011-09-08 15:09:34 +0800 | [diff] [blame] | 494 | struct imxi2c_platform_data *pdata = pdev->dev.platform_data; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 495 | void __iomem *base; |
Wolfram Sang | 8c88ab0 | 2012-07-08 13:11:43 +0200 | [diff] [blame] | 496 | int irq, ret; |
| 497 | u32 bitrate; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 498 | |
| 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 Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 512 | base = devm_ioremap_resource(&pdev->dev, res); |
| 513 | if (IS_ERR(base)) |
| 514 | return PTR_ERR(base); |
Uwe Kleine-König | 4927fbf | 2010-01-08 17:23:17 +0100 | [diff] [blame] | 515 | |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 516 | i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct), |
| 517 | GFP_KERNEL); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 518 | if (!i2c_imx) { |
| 519 | dev_err(&pdev->dev, "can't allocate interface\n"); |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 520 | return -ENOMEM; |
Darius Augulis | 309c18d | 2009-03-31 14:52:54 +0300 | [diff] [blame] | 521 | } |
| 522 | |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 523 | if (of_id) |
| 524 | pdev->id_entry = of_id->data; |
| 525 | i2c_imx->devtype = pdev->id_entry->driver_data; |
| 526 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 527 | /* Setup i2c_imx driver structure */ |
Wolfram Sang | 973c5ed | 2012-04-19 17:31:01 +0200 | [diff] [blame] | 528 | strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name)); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 529 | 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 Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 533 | i2c_imx->adapter.dev.of_node = pdev->dev.of_node; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 534 | i2c_imx->base = base; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 535 | |
| 536 | /* Get I2C clock */ |
Fabio Estevam | 1f09c67 | 2012-07-06 15:31:32 -0300 | [diff] [blame] | 537 | i2c_imx->clk = devm_clk_get(&pdev->dev, NULL); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 538 | if (IS_ERR(i2c_imx->clk)) { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 539 | dev_err(&pdev->dev, "can't get I2C clock\n"); |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 540 | return PTR_ERR(i2c_imx->clk); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 541 | } |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 542 | |
| 543 | /* Request IRQ */ |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 544 | ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0, |
| 545 | pdev->name, i2c_imx); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 546 | if (ret) { |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 547 | dev_err(&pdev->dev, "can't claim irq %d\n", irq); |
| 548 | return ret; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 549 | } |
| 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 Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 558 | 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 Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 564 | |
| 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 Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 573 | return ret; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 576 | of_i2c_register_devices(&i2c_imx->adapter); |
| 577 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 578 | /* Set up platform driver data */ |
| 579 | platform_set_drvdata(pdev, i2c_imx); |
| 580 | |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 581 | dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 582 | dev_dbg(&i2c_imx->adapter.dev, "device resources from 0x%x to 0x%x\n", |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 583 | 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 Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 586 | dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n", |
| 587 | i2c_imx->adapter.name); |
Fabio Estevam | 06d141e | 2012-08-01 17:38:14 -0300 | [diff] [blame] | 588 | dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n"); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 589 | |
| 590 | return 0; /* Return OK */ |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static int __exit i2c_imx_remove(struct platform_device *pdev) |
| 594 | { |
| 595 | struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 596 | |
| 597 | /* remove adapter */ |
| 598 | dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n"); |
| 599 | i2c_del_adapter(&i2c_imx->adapter); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 600 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 601 | /* 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 Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static struct platform_driver i2c_imx_driver = { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 611 | .remove = __exit_p(i2c_imx_remove), |
| 612 | .driver = { |
| 613 | .name = DRIVER_NAME, |
| 614 | .owner = THIS_MODULE, |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 615 | .of_match_table = i2c_imx_dt_ids, |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 616 | }, |
| 617 | .id_table = imx_i2c_devtype, |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | static int __init i2c_adap_imx_init(void) |
| 621 | { |
| 622 | return platform_driver_probe(&i2c_imx_driver, i2c_imx_probe); |
| 623 | } |
Wolfram Sang | 5d3f333 | 2009-09-19 09:09:50 +0200 | [diff] [blame] | 624 | subsys_initcall(i2c_adap_imx_init); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 625 | |
| 626 | static void __exit i2c_adap_imx_exit(void) |
| 627 | { |
| 628 | platform_driver_unregister(&i2c_imx_driver); |
| 629 | } |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 630 | module_exit(i2c_adap_imx_exit); |
| 631 | |
| 632 | MODULE_LICENSE("GPL"); |
| 633 | MODULE_AUTHOR("Darius Augulis"); |
| 634 | MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus"); |
| 635 | MODULE_ALIAS("platform:" DRIVER_NAME); |