Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * BCM2835 master mode driver |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/completion.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/slab.h> |
| 23 | |
| 24 | #define BCM2835_I2C_C 0x0 |
| 25 | #define BCM2835_I2C_S 0x4 |
| 26 | #define BCM2835_I2C_DLEN 0x8 |
| 27 | #define BCM2835_I2C_A 0xc |
| 28 | #define BCM2835_I2C_FIFO 0x10 |
| 29 | #define BCM2835_I2C_DIV 0x14 |
| 30 | #define BCM2835_I2C_DEL 0x18 |
| 31 | #define BCM2835_I2C_CLKT 0x1c |
| 32 | |
| 33 | #define BCM2835_I2C_C_READ BIT(0) |
| 34 | #define BCM2835_I2C_C_CLEAR BIT(4) /* bits 4 and 5 both clear */ |
| 35 | #define BCM2835_I2C_C_ST BIT(7) |
| 36 | #define BCM2835_I2C_C_INTD BIT(8) |
| 37 | #define BCM2835_I2C_C_INTT BIT(9) |
| 38 | #define BCM2835_I2C_C_INTR BIT(10) |
| 39 | #define BCM2835_I2C_C_I2CEN BIT(15) |
| 40 | |
| 41 | #define BCM2835_I2C_S_TA BIT(0) |
| 42 | #define BCM2835_I2C_S_DONE BIT(1) |
| 43 | #define BCM2835_I2C_S_TXW BIT(2) |
| 44 | #define BCM2835_I2C_S_RXR BIT(3) |
| 45 | #define BCM2835_I2C_S_TXD BIT(4) |
| 46 | #define BCM2835_I2C_S_RXD BIT(5) |
| 47 | #define BCM2835_I2C_S_TXE BIT(6) |
| 48 | #define BCM2835_I2C_S_RXF BIT(7) |
| 49 | #define BCM2835_I2C_S_ERR BIT(8) |
| 50 | #define BCM2835_I2C_S_CLKT BIT(9) |
| 51 | #define BCM2835_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */ |
| 52 | |
Silvan Wicki | 7b61863 | 2015-06-16 17:40:59 +0200 | [diff] [blame^] | 53 | #define BCM2835_I2C_BITMSK_S 0x03FF |
| 54 | |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 55 | #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000)) |
| 56 | |
| 57 | struct bcm2835_i2c_dev { |
| 58 | struct device *dev; |
| 59 | void __iomem *regs; |
| 60 | struct clk *clk; |
| 61 | int irq; |
| 62 | struct i2c_adapter adapter; |
| 63 | struct completion completion; |
| 64 | u32 msg_err; |
| 65 | u8 *msg_buf; |
| 66 | size_t msg_buf_remaining; |
| 67 | }; |
| 68 | |
| 69 | static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev, |
| 70 | u32 reg, u32 val) |
| 71 | { |
| 72 | writel(val, i2c_dev->regs + reg); |
| 73 | } |
| 74 | |
| 75 | static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg) |
| 76 | { |
| 77 | return readl(i2c_dev->regs + reg); |
| 78 | } |
| 79 | |
| 80 | static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev) |
| 81 | { |
| 82 | u32 val; |
| 83 | |
| 84 | while (i2c_dev->msg_buf_remaining) { |
| 85 | val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S); |
| 86 | if (!(val & BCM2835_I2C_S_TXD)) |
| 87 | break; |
| 88 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO, |
| 89 | *i2c_dev->msg_buf); |
| 90 | i2c_dev->msg_buf++; |
| 91 | i2c_dev->msg_buf_remaining--; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev) |
| 96 | { |
| 97 | u32 val; |
| 98 | |
| 99 | while (i2c_dev->msg_buf_remaining) { |
| 100 | val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S); |
| 101 | if (!(val & BCM2835_I2C_S_RXD)) |
| 102 | break; |
| 103 | *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev, |
| 104 | BCM2835_I2C_FIFO); |
| 105 | i2c_dev->msg_buf++; |
| 106 | i2c_dev->msg_buf_remaining--; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data) |
| 111 | { |
| 112 | struct bcm2835_i2c_dev *i2c_dev = data; |
| 113 | u32 val, err; |
| 114 | |
| 115 | val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S); |
Silvan Wicki | 7b61863 | 2015-06-16 17:40:59 +0200 | [diff] [blame^] | 116 | val &= BCM2835_I2C_BITMSK_S; |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 117 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val); |
| 118 | |
| 119 | err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR); |
| 120 | if (err) { |
| 121 | i2c_dev->msg_err = err; |
| 122 | complete(&i2c_dev->completion); |
| 123 | return IRQ_HANDLED; |
| 124 | } |
| 125 | |
| 126 | if (val & BCM2835_I2C_S_RXD) { |
| 127 | bcm2835_drain_rxfifo(i2c_dev); |
| 128 | if (!(val & BCM2835_I2C_S_DONE)) |
| 129 | return IRQ_HANDLED; |
| 130 | } |
| 131 | |
| 132 | if (val & BCM2835_I2C_S_DONE) { |
| 133 | if (i2c_dev->msg_buf_remaining) |
| 134 | i2c_dev->msg_err = BCM2835_I2C_S_LEN; |
| 135 | else |
| 136 | i2c_dev->msg_err = 0; |
| 137 | complete(&i2c_dev->completion); |
| 138 | return IRQ_HANDLED; |
| 139 | } |
| 140 | |
| 141 | if (val & BCM2835_I2C_S_TXD) { |
| 142 | bcm2835_fill_txfifo(i2c_dev); |
| 143 | return IRQ_HANDLED; |
| 144 | } |
| 145 | |
| 146 | return IRQ_NONE; |
| 147 | } |
| 148 | |
| 149 | static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev, |
| 150 | struct i2c_msg *msg) |
| 151 | { |
| 152 | u32 c; |
Nicholas Mc Guire | c4dc121 | 2015-02-08 06:04:18 -0500 | [diff] [blame] | 153 | unsigned long time_left; |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 154 | |
| 155 | i2c_dev->msg_buf = msg->buf; |
| 156 | i2c_dev->msg_buf_remaining = msg->len; |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 157 | reinit_completion(&i2c_dev->completion); |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 158 | |
| 159 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR); |
| 160 | |
| 161 | if (msg->flags & I2C_M_RD) { |
| 162 | c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR; |
| 163 | } else { |
| 164 | c = BCM2835_I2C_C_INTT; |
| 165 | bcm2835_fill_txfifo(i2c_dev); |
| 166 | } |
| 167 | c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN; |
| 168 | |
| 169 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr); |
| 170 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len); |
| 171 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c); |
| 172 | |
| 173 | time_left = wait_for_completion_timeout(&i2c_dev->completion, |
| 174 | BCM2835_I2C_TIMEOUT); |
| 175 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR); |
| 176 | if (!time_left) { |
| 177 | dev_err(i2c_dev->dev, "i2c transfer timed out\n"); |
| 178 | return -ETIMEDOUT; |
| 179 | } |
| 180 | |
| 181 | if (likely(!i2c_dev->msg_err)) |
| 182 | return 0; |
| 183 | |
| 184 | if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) && |
| 185 | (msg->flags & I2C_M_IGNORE_NAK)) |
| 186 | return 0; |
| 187 | |
| 188 | dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err); |
| 189 | |
| 190 | if (i2c_dev->msg_err & BCM2835_I2C_S_ERR) |
| 191 | return -EREMOTEIO; |
| 192 | else |
| 193 | return -EIO; |
| 194 | } |
| 195 | |
| 196 | static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], |
| 197 | int num) |
| 198 | { |
| 199 | struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
| 200 | int i; |
| 201 | int ret = 0; |
| 202 | |
| 203 | for (i = 0; i < num; i++) { |
| 204 | ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]); |
| 205 | if (ret) |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | return ret ?: i; |
| 210 | } |
| 211 | |
| 212 | static u32 bcm2835_i2c_func(struct i2c_adapter *adap) |
| 213 | { |
| 214 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 215 | } |
| 216 | |
| 217 | static const struct i2c_algorithm bcm2835_i2c_algo = { |
| 218 | .master_xfer = bcm2835_i2c_xfer, |
| 219 | .functionality = bcm2835_i2c_func, |
| 220 | }; |
| 221 | |
| 222 | static int bcm2835_i2c_probe(struct platform_device *pdev) |
| 223 | { |
| 224 | struct bcm2835_i2c_dev *i2c_dev; |
Jingoo Han | ae50b1d | 2014-02-11 22:02:36 +0900 | [diff] [blame] | 225 | struct resource *mem, *irq; |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 226 | u32 bus_clk_rate, divider; |
| 227 | int ret; |
| 228 | struct i2c_adapter *adap; |
| 229 | |
| 230 | i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); |
Jingoo Han | 46797a2 | 2014-05-13 10:51:58 +0900 | [diff] [blame] | 231 | if (!i2c_dev) |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 232 | return -ENOMEM; |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 233 | platform_set_drvdata(pdev, i2c_dev); |
| 234 | i2c_dev->dev = &pdev->dev; |
| 235 | init_completion(&i2c_dev->completion); |
| 236 | |
| 237 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Jingoo Han | ae50b1d | 2014-02-11 22:02:36 +0900 | [diff] [blame] | 238 | i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem); |
| 239 | if (IS_ERR(i2c_dev->regs)) |
| 240 | return PTR_ERR(i2c_dev->regs); |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 241 | |
| 242 | i2c_dev->clk = devm_clk_get(&pdev->dev, NULL); |
| 243 | if (IS_ERR(i2c_dev->clk)) { |
| 244 | dev_err(&pdev->dev, "Could not get clock\n"); |
| 245 | return PTR_ERR(i2c_dev->clk); |
| 246 | } |
| 247 | |
| 248 | ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency", |
| 249 | &bus_clk_rate); |
| 250 | if (ret < 0) { |
| 251 | dev_warn(&pdev->dev, |
| 252 | "Could not read clock-frequency property\n"); |
| 253 | bus_clk_rate = 100000; |
| 254 | } |
| 255 | |
| 256 | divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate); |
| 257 | /* |
| 258 | * Per the datasheet, the register is always interpreted as an even |
| 259 | * number, by rounding down. In other words, the LSB is ignored. So, |
| 260 | * if the LSB is set, increment the divider to avoid any issue. |
| 261 | */ |
| 262 | if (divider & 1) |
| 263 | divider++; |
| 264 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider); |
| 265 | |
| 266 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 267 | if (!irq) { |
| 268 | dev_err(&pdev->dev, "No IRQ resource\n"); |
| 269 | return -ENODEV; |
| 270 | } |
| 271 | i2c_dev->irq = irq->start; |
| 272 | |
| 273 | ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED, |
| 274 | dev_name(&pdev->dev), i2c_dev); |
| 275 | if (ret) { |
| 276 | dev_err(&pdev->dev, "Could not request IRQ\n"); |
| 277 | return -ENODEV; |
| 278 | } |
| 279 | |
| 280 | adap = &i2c_dev->adapter; |
| 281 | i2c_set_adapdata(adap, i2c_dev); |
| 282 | adap->owner = THIS_MODULE; |
Wolfram Sang | 37e4f91 | 2014-07-10 13:46:23 +0200 | [diff] [blame] | 283 | adap->class = I2C_CLASS_DEPRECATED; |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 284 | strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name)); |
| 285 | adap->algo = &bcm2835_i2c_algo; |
| 286 | adap->dev.parent = &pdev->dev; |
Florian Meier | 07a27a0 | 2013-11-25 09:01:50 +0100 | [diff] [blame] | 287 | adap->dev.of_node = pdev->dev.of_node; |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 288 | |
| 289 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0); |
| 290 | |
| 291 | ret = i2c_add_adapter(adap); |
| 292 | if (ret) |
| 293 | free_irq(i2c_dev->irq, i2c_dev); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static int bcm2835_i2c_remove(struct platform_device *pdev) |
| 299 | { |
| 300 | struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev); |
| 301 | |
| 302 | free_irq(i2c_dev->irq, i2c_dev); |
| 303 | i2c_del_adapter(&i2c_dev->adapter); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static const struct of_device_id bcm2835_i2c_of_match[] = { |
| 309 | { .compatible = "brcm,bcm2835-i2c" }, |
| 310 | {}, |
| 311 | }; |
| 312 | MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match); |
| 313 | |
| 314 | static struct platform_driver bcm2835_i2c_driver = { |
| 315 | .probe = bcm2835_i2c_probe, |
| 316 | .remove = bcm2835_i2c_remove, |
| 317 | .driver = { |
| 318 | .name = "i2c-bcm2835", |
Stephen Warren | f3b54b9 | 2013-02-11 19:47:56 -0700 | [diff] [blame] | 319 | .of_match_table = bcm2835_i2c_of_match, |
| 320 | }, |
| 321 | }; |
| 322 | module_platform_driver(bcm2835_i2c_driver); |
| 323 | |
| 324 | MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>"); |
| 325 | MODULE_DESCRIPTION("BCM2835 I2C bus adapter"); |
| 326 | MODULE_LICENSE("GPL v2"); |
| 327 | MODULE_ALIAS("platform:i2c-bcm2835"); |