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 | * |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 14 | * Author: |
| 15 | * Darius Augulis, Teltonika Inc. |
| 16 | * |
| 17 | * Desc.: |
| 18 | * Implementation of I2C Adapter/Algorithm Driver |
| 19 | * for I2C Bus integrated in Freescale i.MX/MXC processors |
| 20 | * |
| 21 | * Derived from Motorola GSG China I2C example driver |
| 22 | * |
| 23 | * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de |
| 24 | * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de |
| 25 | * Copyright (C) 2007 RightHand Technologies, Inc. |
| 26 | * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt> |
| 27 | * |
Jingchang Lu | d533f04 | 2013-08-07 17:05:36 +0800 | [diff] [blame] | 28 | * Copyright 2013 Freescale Semiconductor, Inc. |
| 29 | * |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | /** Includes ******************************************************************* |
| 33 | *******************************************************************************/ |
| 34 | |
Yao Yuan | 2fbed51 | 2014-11-18 18:31:05 +0800 | [diff] [blame^] | 35 | #include <linux/clk.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/err.h> |
| 38 | #include <linux/errno.h> |
| 39 | #include <linux/i2c.h> |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 40 | #include <linux/init.h> |
Yao Yuan | 2fbed51 | 2014-11-18 18:31:05 +0800 | [diff] [blame^] | 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/io.h> |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 43 | #include <linux/kernel.h> |
| 44 | #include <linux/module.h> |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 45 | #include <linux/of.h> |
| 46 | #include <linux/of_device.h> |
Arnd Bergmann | 82906b1 | 2012-08-24 15:14:29 +0200 | [diff] [blame] | 47 | #include <linux/platform_data/i2c-imx.h> |
Yao Yuan | 2fbed51 | 2014-11-18 18:31:05 +0800 | [diff] [blame^] | 48 | #include <linux/platform_device.h> |
| 49 | #include <linux/sched.h> |
| 50 | #include <linux/slab.h> |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 51 | |
| 52 | /** Defines ******************************************************************** |
| 53 | *******************************************************************************/ |
| 54 | |
| 55 | /* This will be the driver name the kernel reports */ |
| 56 | #define DRIVER_NAME "imx-i2c" |
| 57 | |
| 58 | /* Default value */ |
| 59 | #define IMX_I2C_BIT_RATE 100000 /* 100kHz */ |
| 60 | |
Jingchang Lu | 8cc7331 | 2013-08-07 17:05:40 +0800 | [diff] [blame] | 61 | /* IMX I2C registers: |
| 62 | * the I2C register offset is different between SoCs, |
| 63 | * to provid support for all these chips, split the |
| 64 | * register offset into a fixed base address and a |
| 65 | * variable shift value, then the full register offset |
| 66 | * will be calculated by |
| 67 | * reg_off = ( reg_base_addr << reg_shift) |
| 68 | */ |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 69 | #define IMX_I2C_IADR 0x00 /* i2c slave address */ |
Jingchang Lu | 8cc7331 | 2013-08-07 17:05:40 +0800 | [diff] [blame] | 70 | #define IMX_I2C_IFDR 0x01 /* i2c frequency divider */ |
| 71 | #define IMX_I2C_I2CR 0x02 /* i2c control */ |
| 72 | #define IMX_I2C_I2SR 0x03 /* i2c status */ |
| 73 | #define IMX_I2C_I2DR 0x04 /* i2c transfer data */ |
| 74 | |
| 75 | #define IMX_I2C_REGSHIFT 2 |
Jingchang Lu | ad90efa | 2013-08-07 17:05:43 +0800 | [diff] [blame] | 76 | #define VF610_I2C_REGSHIFT 0 |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 77 | |
| 78 | /* Bits of IMX I2C registers */ |
| 79 | #define I2SR_RXAK 0x01 |
| 80 | #define I2SR_IIF 0x02 |
| 81 | #define I2SR_SRW 0x04 |
| 82 | #define I2SR_IAL 0x10 |
| 83 | #define I2SR_IBB 0x20 |
| 84 | #define I2SR_IAAS 0x40 |
| 85 | #define I2SR_ICF 0x80 |
| 86 | #define I2CR_RSTA 0x04 |
| 87 | #define I2CR_TXAK 0x08 |
| 88 | #define I2CR_MTX 0x10 |
| 89 | #define I2CR_MSTA 0x20 |
| 90 | #define I2CR_IIEN 0x40 |
| 91 | #define I2CR_IEN 0x80 |
| 92 | |
Jingchang Lu | 171408c | 2013-08-07 17:05:41 +0800 | [diff] [blame] | 93 | /* register bits different operating codes definition: |
| 94 | * 1) I2SR: Interrupt flags clear operation differ between SoCs: |
| 95 | * - write zero to clear(w0c) INT flag on i.MX, |
| 96 | * - but write one to clear(w1c) INT flag on Vybrid. |
| 97 | * 2) I2CR: I2C module enable operation also differ between SoCs: |
| 98 | * - set I2CR_IEN bit enable the module on i.MX, |
| 99 | * - but clear I2CR_IEN bit enable the module on Vybrid. |
| 100 | */ |
| 101 | #define I2SR_CLR_OPCODE_W0C 0x0 |
| 102 | #define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF) |
| 103 | #define I2CR_IEN_OPCODE_0 0x0 |
| 104 | #define I2CR_IEN_OPCODE_1 I2CR_IEN |
| 105 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 106 | /** Variables ****************************************************************** |
| 107 | *******************************************************************************/ |
| 108 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 109 | /* |
| 110 | * sorted list of clock divider, register value pairs |
| 111 | * taken from table 26-5, p.26-9, Freescale i.MX |
| 112 | * Integrated Portable System Processor Reference Manual |
| 113 | * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007 |
| 114 | * |
| 115 | * Duplicated divider values removed from list |
| 116 | */ |
Jingchang Lu | d533f04 | 2013-08-07 17:05:36 +0800 | [diff] [blame] | 117 | struct imx_i2c_clk_pair { |
| 118 | u16 div; |
| 119 | u16 val; |
| 120 | }; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 121 | |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 122 | static struct imx_i2c_clk_pair imx_i2c_clk_div[] = { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 123 | { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 }, |
| 124 | { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 }, |
| 125 | { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 }, |
| 126 | { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B }, |
| 127 | { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A }, |
| 128 | { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 }, |
| 129 | { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 }, |
| 130 | { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 }, |
| 131 | { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 }, |
| 132 | { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B }, |
| 133 | { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E }, |
| 134 | { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D }, |
| 135 | { 3072, 0x1E }, { 3840, 0x1F } |
| 136 | }; |
| 137 | |
Jingchang Lu | ad90efa | 2013-08-07 17:05:43 +0800 | [diff] [blame] | 138 | /* Vybrid VF610 clock divider, register value pairs */ |
| 139 | static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = { |
| 140 | { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 }, |
| 141 | { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 }, |
| 142 | { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D }, |
| 143 | { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 }, |
| 144 | { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 }, |
| 145 | { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 }, |
| 146 | { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 }, |
| 147 | { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 }, |
| 148 | { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 }, |
| 149 | { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B }, |
| 150 | { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 }, |
| 151 | { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 }, |
| 152 | { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B }, |
| 153 | { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A }, |
| 154 | { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E }, |
| 155 | }; |
| 156 | |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 157 | enum imx_i2c_type { |
| 158 | IMX1_I2C, |
| 159 | IMX21_I2C, |
Jingchang Lu | ad90efa | 2013-08-07 17:05:43 +0800 | [diff] [blame] | 160 | VF610_I2C, |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 161 | }; |
| 162 | |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 163 | struct imx_i2c_hwdata { |
| 164 | enum imx_i2c_type devtype; |
| 165 | unsigned regshift; |
| 166 | struct imx_i2c_clk_pair *clk_div; |
| 167 | unsigned ndivs; |
| 168 | unsigned i2sr_clr_opcode; |
| 169 | unsigned i2cr_ien_opcode; |
| 170 | }; |
| 171 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 172 | struct imx_i2c_struct { |
| 173 | struct i2c_adapter adapter; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 174 | struct clk *clk; |
| 175 | void __iomem *base; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 176 | wait_queue_head_t queue; |
| 177 | unsigned long i2csr; |
Wolfram Sang | 65de394 | 2009-04-06 16:27:45 +0200 | [diff] [blame] | 178 | unsigned int disable_delay; |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 179 | int stopped; |
Richard Zhao | db3a3d4 | 2009-10-17 17:46:24 +0800 | [diff] [blame] | 180 | unsigned int ifdr; /* IMX_I2C_IFDR */ |
Fugang Duan | 9b2a6da | 2014-05-20 10:21:45 +0800 | [diff] [blame] | 181 | unsigned int cur_clk; |
| 182 | unsigned int bitrate; |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 183 | const struct imx_i2c_hwdata *hwdata; |
| 184 | }; |
| 185 | |
| 186 | static const struct imx_i2c_hwdata imx1_i2c_hwdata = { |
| 187 | .devtype = IMX1_I2C, |
| 188 | .regshift = IMX_I2C_REGSHIFT, |
| 189 | .clk_div = imx_i2c_clk_div, |
| 190 | .ndivs = ARRAY_SIZE(imx_i2c_clk_div), |
| 191 | .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C, |
| 192 | .i2cr_ien_opcode = I2CR_IEN_OPCODE_1, |
| 193 | |
| 194 | }; |
| 195 | |
| 196 | static const struct imx_i2c_hwdata imx21_i2c_hwdata = { |
| 197 | .devtype = IMX21_I2C, |
| 198 | .regshift = IMX_I2C_REGSHIFT, |
| 199 | .clk_div = imx_i2c_clk_div, |
| 200 | .ndivs = ARRAY_SIZE(imx_i2c_clk_div), |
| 201 | .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C, |
| 202 | .i2cr_ien_opcode = I2CR_IEN_OPCODE_1, |
| 203 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 204 | }; |
| 205 | |
Jingchang Lu | ad90efa | 2013-08-07 17:05:43 +0800 | [diff] [blame] | 206 | static struct imx_i2c_hwdata vf610_i2c_hwdata = { |
| 207 | .devtype = VF610_I2C, |
| 208 | .regshift = VF610_I2C_REGSHIFT, |
| 209 | .clk_div = vf610_i2c_clk_div, |
| 210 | .ndivs = ARRAY_SIZE(vf610_i2c_clk_div), |
| 211 | .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C, |
| 212 | .i2cr_ien_opcode = I2CR_IEN_OPCODE_0, |
| 213 | |
| 214 | }; |
| 215 | |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 216 | static struct platform_device_id imx_i2c_devtype[] = { |
| 217 | { |
| 218 | .name = "imx1-i2c", |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 219 | .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata, |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 220 | }, { |
| 221 | .name = "imx21-i2c", |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 222 | .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata, |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 223 | }, { |
| 224 | /* sentinel */ |
| 225 | } |
| 226 | }; |
| 227 | MODULE_DEVICE_TABLE(platform, imx_i2c_devtype); |
| 228 | |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 229 | static const struct of_device_id i2c_imx_dt_ids[] = { |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 230 | { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, }, |
| 231 | { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, }, |
Jingchang Lu | ad90efa | 2013-08-07 17:05:43 +0800 | [diff] [blame] | 232 | { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, }, |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 233 | { /* sentinel */ } |
| 234 | }; |
Arnaud Patard \(Rtp\) | 2f641a8 | 2013-06-20 23:07:06 +0200 | [diff] [blame] | 235 | MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids); |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 236 | |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 237 | static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx) |
| 238 | { |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 239 | return i2c_imx->hwdata->devtype == IMX1_I2C; |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 240 | } |
| 241 | |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 242 | static inline void imx_i2c_write_reg(unsigned int val, |
| 243 | struct imx_i2c_struct *i2c_imx, unsigned int reg) |
| 244 | { |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 245 | writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx, |
| 249 | unsigned int reg) |
| 250 | { |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 251 | return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 254 | /** Functions for IMX I2C adapter driver *************************************** |
| 255 | *******************************************************************************/ |
| 256 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 257 | 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] | 258 | { |
| 259 | unsigned long orig_jiffies = jiffies; |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 260 | unsigned int temp; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 261 | |
| 262 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
| 263 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 264 | while (1) { |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 265 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); |
Haibo Chen | 639a26c | 2014-09-03 13:52:07 +0800 | [diff] [blame] | 266 | |
| 267 | /* check for arbitration lost */ |
| 268 | if (temp & I2SR_IAL) { |
| 269 | temp &= ~I2SR_IAL; |
| 270 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR); |
| 271 | return -EAGAIN; |
| 272 | } |
| 273 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 274 | if (for_busy && (temp & I2SR_IBB)) |
| 275 | break; |
| 276 | if (!for_busy && !(temp & I2SR_IBB)) |
| 277 | break; |
Arnaud Patard | da9c99f | 2010-03-23 17:28:28 +0100 | [diff] [blame] | 278 | if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 279 | dev_dbg(&i2c_imx->adapter.dev, |
| 280 | "<%s> I2C bus is busy\n", __func__); |
Arnaud Patard | da9c99f | 2010-03-23 17:28:28 +0100 | [diff] [blame] | 281 | return -ETIMEDOUT; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 282 | } |
| 283 | schedule(); |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) |
| 290 | { |
Marc Kleine-Budde | e39428d | 2010-06-21 09:27:05 +0200 | [diff] [blame] | 291 | 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] | 292 | |
Marc Kleine-Budde | e39428d | 2010-06-21 09:27:05 +0200 | [diff] [blame] | 293 | if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 294 | dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__); |
| 295 | return -ETIMEDOUT; |
| 296 | } |
| 297 | dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__); |
| 298 | i2c_imx->i2csr = 0; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) |
| 303 | { |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 304 | if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 305 | dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__); |
| 306 | return -EIO; /* No ACK */ |
| 307 | } |
| 308 | |
| 309 | dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__); |
| 310 | return 0; |
| 311 | } |
| 312 | |
Fugang Duan | 9b2a6da | 2014-05-20 10:21:45 +0800 | [diff] [blame] | 313 | static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx) |
| 314 | { |
| 315 | struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div; |
| 316 | unsigned int i2c_clk_rate; |
| 317 | unsigned int div; |
| 318 | int i; |
| 319 | |
| 320 | /* Divider value calculation */ |
| 321 | i2c_clk_rate = clk_get_rate(i2c_imx->clk); |
| 322 | if (i2c_imx->cur_clk == i2c_clk_rate) |
| 323 | return; |
| 324 | else |
| 325 | i2c_imx->cur_clk = i2c_clk_rate; |
| 326 | |
| 327 | div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate; |
| 328 | if (div < i2c_clk_div[0].div) |
| 329 | i = 0; |
| 330 | else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div) |
| 331 | i = i2c_imx->hwdata->ndivs - 1; |
| 332 | else |
| 333 | for (i = 0; i2c_clk_div[i].div < div; i++); |
| 334 | |
| 335 | /* Store divider value */ |
| 336 | i2c_imx->ifdr = i2c_clk_div[i].val; |
| 337 | |
| 338 | /* |
| 339 | * There dummy delay is calculated. |
| 340 | * It should be about one I2C clock period long. |
| 341 | * This delay is used in I2C bus disable function |
| 342 | * to fix chip hardware bug. |
| 343 | */ |
| 344 | i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div |
| 345 | + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2); |
| 346 | |
| 347 | #ifdef CONFIG_I2C_DEBUG_BUS |
| 348 | dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n", |
| 349 | i2c_clk_rate, div); |
| 350 | dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n", |
| 351 | i2c_clk_div[i].val, i2c_clk_div[i].div); |
| 352 | #endif |
| 353 | } |
| 354 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 355 | static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 356 | { |
| 357 | unsigned int temp = 0; |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 358 | int result; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 359 | |
| 360 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
| 361 | |
Fugang Duan | 9b2a6da | 2014-05-20 10:21:45 +0800 | [diff] [blame] | 362 | i2c_imx_set_clk(i2c_imx); |
| 363 | |
Fabio Estevam | e5bf216 | 2013-12-04 20:21:37 -0200 | [diff] [blame] | 364 | result = clk_prepare_enable(i2c_imx->clk); |
| 365 | if (result) |
| 366 | return result; |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 367 | imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 368 | /* Enable I2C controller */ |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 369 | imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR); |
| 370 | imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 371 | |
| 372 | /* Wait controller to be stable */ |
| 373 | udelay(50); |
| 374 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 375 | /* Start I2C transaction */ |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 376 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 377 | temp |= I2CR_MSTA; |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 378 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 379 | result = i2c_imx_bus_busy(i2c_imx, 1); |
| 380 | if (result) |
| 381 | return result; |
| 382 | i2c_imx->stopped = 0; |
| 383 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 384 | temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK; |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 385 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 386 | return result; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx) |
| 390 | { |
| 391 | unsigned int temp = 0; |
| 392 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 393 | if (!i2c_imx->stopped) { |
| 394 | /* Stop I2C transaction */ |
| 395 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 396 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 397 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 398 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 399 | } |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 400 | if (is_imx1_i2c(i2c_imx)) { |
Richard Zhao | a4094a7 | 2009-10-17 17:46:23 +0800 | [diff] [blame] | 401 | /* |
| 402 | * This delay caused by an i.MXL hardware bug. |
| 403 | * If no (or too short) delay, no "STOP" bit will be generated. |
| 404 | */ |
| 405 | udelay(i2c_imx->disable_delay); |
| 406 | } |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 407 | |
Valentin Longchamp | a1ee06b | 2010-01-21 18:55:32 +0100 | [diff] [blame] | 408 | if (!i2c_imx->stopped) { |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 409 | i2c_imx_bus_busy(i2c_imx, 0); |
Valentin Longchamp | a1ee06b | 2010-01-21 18:55:32 +0100 | [diff] [blame] | 410 | i2c_imx->stopped = 1; |
| 411 | } |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 412 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 413 | /* Disable I2C controller */ |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 414 | temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, |
| 415 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
Richard Zhao | 8391433 | 2011-11-15 14:48:08 +0800 | [diff] [blame] | 416 | clk_disable_unprepare(i2c_imx->clk); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 419 | static irqreturn_t i2c_imx_isr(int irq, void *dev_id) |
| 420 | { |
| 421 | struct imx_i2c_struct *i2c_imx = dev_id; |
| 422 | unsigned int temp; |
| 423 | |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 424 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 425 | if (temp & I2SR_IIF) { |
| 426 | /* save status register */ |
| 427 | i2c_imx->i2csr = temp; |
| 428 | temp &= ~I2SR_IIF; |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 429 | temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 430 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR); |
Marc Kleine-Budde | e39428d | 2010-06-21 09:27:05 +0200 | [diff] [blame] | 431 | wake_up(&i2c_imx->queue); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 432 | return IRQ_HANDLED; |
| 433 | } |
| 434 | |
| 435 | return IRQ_NONE; |
| 436 | } |
| 437 | |
| 438 | static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) |
| 439 | { |
| 440 | int i, result; |
| 441 | |
| 442 | dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n", |
| 443 | __func__, msgs->addr << 1); |
| 444 | |
| 445 | /* write slave address */ |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 446 | imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 447 | result = i2c_imx_trx_complete(i2c_imx); |
| 448 | if (result) |
| 449 | return result; |
| 450 | result = i2c_imx_acked(i2c_imx); |
| 451 | if (result) |
| 452 | return result; |
| 453 | dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__); |
| 454 | |
| 455 | /* write data */ |
| 456 | for (i = 0; i < msgs->len; i++) { |
| 457 | dev_dbg(&i2c_imx->adapter.dev, |
| 458 | "<%s> write byte: B%d=0x%X\n", |
| 459 | __func__, i, msgs->buf[i]); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 460 | imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 461 | result = i2c_imx_trx_complete(i2c_imx); |
| 462 | if (result) |
| 463 | return result; |
| 464 | result = i2c_imx_acked(i2c_imx); |
| 465 | if (result) |
| 466 | return result; |
| 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
Fugang Duan | 054b62d | 2014-04-30 14:24:58 +0800 | [diff] [blame] | 471 | static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg) |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 472 | { |
| 473 | int i, result; |
| 474 | unsigned int temp; |
Kaushal Butala | 8e8782c | 2014-04-04 14:56:10 +0200 | [diff] [blame] | 475 | int block_data = msgs->flags & I2C_M_RECV_LEN; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 476 | |
| 477 | dev_dbg(&i2c_imx->adapter.dev, |
| 478 | "<%s> write slave address: addr=0x%x\n", |
| 479 | __func__, (msgs->addr << 1) | 0x01); |
| 480 | |
| 481 | /* write slave address */ |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 482 | imx_i2c_write_reg((msgs->addr << 1) | 0x01, i2c_imx, IMX_I2C_I2DR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 483 | result = i2c_imx_trx_complete(i2c_imx); |
| 484 | if (result) |
| 485 | return result; |
| 486 | result = i2c_imx_acked(i2c_imx); |
| 487 | if (result) |
| 488 | return result; |
| 489 | |
| 490 | dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__); |
| 491 | |
| 492 | /* setup bus to read data */ |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 493 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 494 | temp &= ~I2CR_MTX; |
Kaushal Butala | 8e8782c | 2014-04-04 14:56:10 +0200 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * Reset the I2CR_TXAK flag initially for SMBus block read since the |
| 498 | * length is unknown |
| 499 | */ |
| 500 | if ((msgs->len - 1) || block_data) |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 501 | temp &= ~I2CR_TXAK; |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 502 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
| 503 | imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */ |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 504 | |
| 505 | dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__); |
| 506 | |
| 507 | /* read data */ |
| 508 | for (i = 0; i < msgs->len; i++) { |
Kaushal Butala | 8e8782c | 2014-04-04 14:56:10 +0200 | [diff] [blame] | 509 | u8 len = 0; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 510 | result = i2c_imx_trx_complete(i2c_imx); |
| 511 | if (result) |
| 512 | return result; |
Kaushal Butala | 8e8782c | 2014-04-04 14:56:10 +0200 | [diff] [blame] | 513 | /* |
| 514 | * First byte is the length of remaining packet |
| 515 | * in the SMBus block data read. Add it to |
| 516 | * msgs->len. |
| 517 | */ |
| 518 | if ((!i) && block_data) { |
| 519 | len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); |
| 520 | if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) |
| 521 | return -EPROTO; |
| 522 | dev_dbg(&i2c_imx->adapter.dev, |
| 523 | "<%s> read length: 0x%X\n", |
| 524 | __func__, len); |
| 525 | msgs->len += len; |
| 526 | } |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 527 | if (i == (msgs->len - 1)) { |
Fugang Duan | 054b62d | 2014-04-30 14:24:58 +0800 | [diff] [blame] | 528 | if (is_lastmsg) { |
| 529 | /* |
| 530 | * It must generate STOP before read I2DR to prevent |
| 531 | * controller from generating another clock cycle |
| 532 | */ |
| 533 | dev_dbg(&i2c_imx->adapter.dev, |
| 534 | "<%s> clear MSTA\n", __func__); |
| 535 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
| 536 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
| 537 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
| 538 | i2c_imx_bus_busy(i2c_imx, 0); |
| 539 | i2c_imx->stopped = 1; |
| 540 | } else { |
| 541 | /* |
| 542 | * For i2c master receiver repeat restart operation like: |
| 543 | * read -> repeat MSTA -> read/write |
| 544 | * The controller must set MTX before read the last byte in |
| 545 | * the first read operation, otherwise the first read cost |
| 546 | * one extra clock cycle. |
| 547 | */ |
| 548 | temp = readb(i2c_imx->base + IMX_I2C_I2CR); |
| 549 | temp |= I2CR_MTX; |
| 550 | writeb(temp, i2c_imx->base + IMX_I2C_I2CR); |
| 551 | } |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 552 | } else if (i == (msgs->len - 2)) { |
| 553 | dev_dbg(&i2c_imx->adapter.dev, |
| 554 | "<%s> set TXAK\n", __func__); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 555 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 556 | temp |= I2CR_TXAK; |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 557 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 558 | } |
Kaushal Butala | 8e8782c | 2014-04-04 14:56:10 +0200 | [diff] [blame] | 559 | if ((!i) && block_data) |
| 560 | msgs->buf[0] = len; |
| 561 | else |
| 562 | msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 563 | dev_dbg(&i2c_imx->adapter.dev, |
| 564 | "<%s> read byte: B%d=0x%X\n", |
| 565 | __func__, i, msgs->buf[i]); |
| 566 | } |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static int i2c_imx_xfer(struct i2c_adapter *adapter, |
| 571 | struct i2c_msg *msgs, int num) |
| 572 | { |
| 573 | unsigned int i, temp; |
| 574 | int result; |
Fugang Duan | 054b62d | 2014-04-30 14:24:58 +0800 | [diff] [blame] | 575 | bool is_lastmsg = false; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 576 | struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); |
| 577 | |
| 578 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
| 579 | |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 580 | /* Start I2C transfer */ |
| 581 | result = i2c_imx_start(i2c_imx); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 582 | if (result) |
| 583 | goto fail0; |
| 584 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 585 | /* read/write data */ |
| 586 | for (i = 0; i < num; i++) { |
Fugang Duan | 054b62d | 2014-04-30 14:24:58 +0800 | [diff] [blame] | 587 | if (i == num - 1) |
| 588 | is_lastmsg = true; |
| 589 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 590 | if (i) { |
| 591 | dev_dbg(&i2c_imx->adapter.dev, |
| 592 | "<%s> repeated start\n", __func__); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 593 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 594 | temp |= I2CR_RSTA; |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 595 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
Richard Zhao | 43309f3 | 2009-10-17 17:46:22 +0800 | [diff] [blame] | 596 | result = i2c_imx_bus_busy(i2c_imx, 1); |
| 597 | if (result) |
| 598 | goto fail0; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 599 | } |
| 600 | dev_dbg(&i2c_imx->adapter.dev, |
| 601 | "<%s> transfer message: %d\n", __func__, i); |
| 602 | /* write/read data */ |
| 603 | #ifdef CONFIG_I2C_DEBUG_BUS |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 604 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 605 | dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, " |
| 606 | "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__, |
| 607 | (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0), |
| 608 | (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0), |
| 609 | (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0)); |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 610 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 611 | dev_dbg(&i2c_imx->adapter.dev, |
| 612 | "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, " |
| 613 | "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__, |
| 614 | (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0), |
| 615 | (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0), |
| 616 | (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0), |
| 617 | (temp & I2SR_RXAK ? 1 : 0)); |
| 618 | #endif |
| 619 | if (msgs[i].flags & I2C_M_RD) |
Fugang Duan | 054b62d | 2014-04-30 14:24:58 +0800 | [diff] [blame] | 620 | result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 621 | else |
| 622 | result = i2c_imx_write(i2c_imx, &msgs[i]); |
Arnaud Patard | da9c99f | 2010-03-23 17:28:28 +0100 | [diff] [blame] | 623 | if (result) |
| 624 | goto fail0; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | fail0: |
| 628 | /* Stop I2C transfer */ |
| 629 | i2c_imx_stop(i2c_imx); |
| 630 | |
| 631 | dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__, |
| 632 | (result < 0) ? "error" : "success msg", |
| 633 | (result < 0) ? result : num); |
| 634 | return (result < 0) ? result : num; |
| 635 | } |
| 636 | |
| 637 | static u32 i2c_imx_func(struct i2c_adapter *adapter) |
| 638 | { |
Kaushal Butala | 8e8782c | 2014-04-04 14:56:10 +0200 | [diff] [blame] | 639 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
| 640 | | I2C_FUNC_SMBUS_READ_BLOCK_DATA; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static struct i2c_algorithm i2c_imx_algo = { |
| 644 | .master_xfer = i2c_imx_xfer, |
| 645 | .functionality = i2c_imx_func, |
| 646 | }; |
| 647 | |
Wolfram Sang | 3611431 | 2013-10-08 22:35:34 +0200 | [diff] [blame] | 648 | static int i2c_imx_probe(struct platform_device *pdev) |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 649 | { |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 650 | const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids, |
| 651 | &pdev->dev); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 652 | struct imx_i2c_struct *i2c_imx; |
| 653 | struct resource *res; |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 654 | struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 655 | void __iomem *base; |
Wolfram Sang | 8c88ab0 | 2012-07-08 13:11:43 +0200 | [diff] [blame] | 656 | int irq, ret; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 657 | |
| 658 | dev_dbg(&pdev->dev, "<%s>\n", __func__); |
| 659 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 660 | irq = platform_get_irq(pdev, 0); |
| 661 | if (irq < 0) { |
| 662 | dev_err(&pdev->dev, "can't get irq number\n"); |
Wolfram Sang | a8763f3 | 2013-12-12 22:51:31 +0100 | [diff] [blame] | 663 | return irq; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Wolfram Sang | 3cc2d00 | 2013-05-10 10:16:54 +0200 | [diff] [blame] | 666 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 667 | base = devm_ioremap_resource(&pdev->dev, res); |
| 668 | if (IS_ERR(base)) |
| 669 | return PTR_ERR(base); |
Uwe Kleine-König | 4927fbf | 2010-01-08 17:23:17 +0100 | [diff] [blame] | 670 | |
Fabio Estevam | d4ffeec | 2014-11-07 00:44:34 -0200 | [diff] [blame] | 671 | i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL); |
Jingoo Han | 46797a2 | 2014-05-13 10:51:58 +0900 | [diff] [blame] | 672 | if (!i2c_imx) |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 673 | return -ENOMEM; |
Darius Augulis | 309c18d | 2009-03-31 14:52:54 +0300 | [diff] [blame] | 674 | |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 675 | if (of_id) |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 676 | i2c_imx->hwdata = of_id->data; |
Jingchang Lu | 0fc1347 | 2013-08-07 17:05:38 +0800 | [diff] [blame] | 677 | else |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 678 | i2c_imx->hwdata = (struct imx_i2c_hwdata *) |
| 679 | platform_get_device_id(pdev)->driver_data; |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 680 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 681 | /* Setup i2c_imx driver structure */ |
Wolfram Sang | 973c5ed | 2012-04-19 17:31:01 +0200 | [diff] [blame] | 682 | strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name)); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 683 | i2c_imx->adapter.owner = THIS_MODULE; |
| 684 | i2c_imx->adapter.algo = &i2c_imx_algo; |
| 685 | i2c_imx->adapter.dev.parent = &pdev->dev; |
| 686 | i2c_imx->adapter.nr = pdev->id; |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 687 | i2c_imx->adapter.dev.of_node = pdev->dev.of_node; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 688 | i2c_imx->base = base; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 689 | |
| 690 | /* Get I2C clock */ |
Fabio Estevam | 1f09c67 | 2012-07-06 15:31:32 -0300 | [diff] [blame] | 691 | i2c_imx->clk = devm_clk_get(&pdev->dev, NULL); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 692 | if (IS_ERR(i2c_imx->clk)) { |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 693 | dev_err(&pdev->dev, "can't get I2C clock\n"); |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 694 | return PTR_ERR(i2c_imx->clk); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 695 | } |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 696 | |
Jingchang Lu | 46f2832 | 2013-08-07 17:05:37 +0800 | [diff] [blame] | 697 | ret = clk_prepare_enable(i2c_imx->clk); |
| 698 | if (ret) { |
| 699 | dev_err(&pdev->dev, "can't enable I2C clock\n"); |
| 700 | return ret; |
| 701 | } |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 702 | /* Request IRQ */ |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 703 | ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0, |
| 704 | pdev->name, i2c_imx); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 705 | if (ret) { |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 706 | dev_err(&pdev->dev, "can't claim irq %d\n", irq); |
Fabio Estevam | a4ce47f | 2014-10-04 09:17:27 -0300 | [diff] [blame] | 707 | goto clk_disable; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* Init queue */ |
| 711 | init_waitqueue_head(&i2c_imx->queue); |
| 712 | |
| 713 | /* Set up adapter data */ |
| 714 | i2c_set_adapdata(&i2c_imx->adapter, i2c_imx); |
| 715 | |
| 716 | /* Set up clock divider */ |
Fugang Duan | 9b2a6da | 2014-05-20 10:21:45 +0800 | [diff] [blame] | 717 | i2c_imx->bitrate = IMX_I2C_BIT_RATE; |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 718 | ret = of_property_read_u32(pdev->dev.of_node, |
Fugang Duan | 9b2a6da | 2014-05-20 10:21:45 +0800 | [diff] [blame] | 719 | "clock-frequency", &i2c_imx->bitrate); |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 720 | if (ret < 0 && pdata && pdata->bitrate) |
Fugang Duan | 9b2a6da | 2014-05-20 10:21:45 +0800 | [diff] [blame] | 721 | i2c_imx->bitrate = pdata->bitrate; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 722 | |
| 723 | /* Set up chip registers to defaults */ |
Jingchang Lu | 4b77502 | 2013-08-07 17:05:42 +0800 | [diff] [blame] | 724 | imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, |
| 725 | i2c_imx, IMX_I2C_I2CR); |
| 726 | imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 727 | |
| 728 | /* Add I2C adapter */ |
| 729 | ret = i2c_add_numbered_adapter(&i2c_imx->adapter); |
| 730 | if (ret < 0) { |
| 731 | dev_err(&pdev->dev, "registration failed\n"); |
Fabio Estevam | a4ce47f | 2014-10-04 09:17:27 -0300 | [diff] [blame] | 732 | goto clk_disable; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* Set up platform driver data */ |
| 736 | platform_set_drvdata(pdev, i2c_imx); |
Jingchang Lu | 46f2832 | 2013-08-07 17:05:37 +0800 | [diff] [blame] | 737 | clk_disable_unprepare(i2c_imx->clk); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 738 | |
Richard Zhao | 9f8a3e7 | 2012-06-04 19:04:25 +0800 | [diff] [blame] | 739 | dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq); |
Xiubo Li | 64bdfbf | 2014-08-06 11:45:08 +0800 | [diff] [blame] | 740 | dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 741 | dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n", |
| 742 | i2c_imx->adapter.name); |
Fabio Estevam | 06d141e | 2012-08-01 17:38:14 -0300 | [diff] [blame] | 743 | dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n"); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 744 | |
| 745 | return 0; /* Return OK */ |
Fabio Estevam | a4ce47f | 2014-10-04 09:17:27 -0300 | [diff] [blame] | 746 | |
| 747 | clk_disable: |
| 748 | clk_disable_unprepare(i2c_imx->clk); |
| 749 | return ret; |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Wolfram Sang | 3611431 | 2013-10-08 22:35:34 +0200 | [diff] [blame] | 752 | static int i2c_imx_remove(struct platform_device *pdev) |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 753 | { |
| 754 | struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 755 | |
| 756 | /* remove adapter */ |
| 757 | dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n"); |
| 758 | i2c_del_adapter(&i2c_imx->adapter); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 759 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 760 | /* setup chip registers to defaults */ |
Jingchang Lu | 1d5ef2a | 2013-08-07 17:05:39 +0800 | [diff] [blame] | 761 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR); |
| 762 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR); |
| 763 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR); |
| 764 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 765 | |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | static struct platform_driver i2c_imx_driver = { |
Wolfram Sang | 3611431 | 2013-10-08 22:35:34 +0200 | [diff] [blame] | 770 | .probe = i2c_imx_probe, |
| 771 | .remove = i2c_imx_remove, |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 772 | .driver = { |
| 773 | .name = DRIVER_NAME, |
| 774 | .owner = THIS_MODULE, |
Shawn Guo | dfcd04b | 2011-09-08 15:09:35 +0800 | [diff] [blame] | 775 | .of_match_table = i2c_imx_dt_ids, |
Shawn Guo | 5bdfba2 | 2012-09-14 15:19:00 +0800 | [diff] [blame] | 776 | }, |
| 777 | .id_table = imx_i2c_devtype, |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 778 | }; |
| 779 | |
| 780 | static int __init i2c_adap_imx_init(void) |
| 781 | { |
Wolfram Sang | 3611431 | 2013-10-08 22:35:34 +0200 | [diff] [blame] | 782 | return platform_driver_register(&i2c_imx_driver); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 783 | } |
Wolfram Sang | 5d3f333 | 2009-09-19 09:09:50 +0200 | [diff] [blame] | 784 | subsys_initcall(i2c_adap_imx_init); |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 785 | |
| 786 | static void __exit i2c_adap_imx_exit(void) |
| 787 | { |
| 788 | platform_driver_unregister(&i2c_imx_driver); |
| 789 | } |
Darius Augulis | aa11e38 | 2009-01-30 10:32:28 +0200 | [diff] [blame] | 790 | module_exit(i2c_adap_imx_exit); |
| 791 | |
| 792 | MODULE_LICENSE("GPL"); |
| 793 | MODULE_AUTHOR("Darius Augulis"); |
| 794 | MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus"); |
| 795 | MODULE_ALIAS("platform:" DRIVER_NAME); |