2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
15 #include <linux/clk.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
22 #define UNIPHIER_I2C_DTRM 0x00 /* TX register */
23 #define UNIPHIER_I2C_DTRM_IRQEN BIT(11) /* enable interrupt */
24 #define UNIPHIER_I2C_DTRM_STA BIT(10) /* start condition */
25 #define UNIPHIER_I2C_DTRM_STO BIT(9) /* stop condition */
26 #define UNIPHIER_I2C_DTRM_NACK BIT(8) /* do not return ACK */
27 #define UNIPHIER_I2C_DTRM_RD BIT(0) /* read transaction */
28 #define UNIPHIER_I2C_DREC 0x04 /* RX register */
29 #define UNIPHIER_I2C_DREC_MST BIT(14) /* 1 = master, 0 = slave */
30 #define UNIPHIER_I2C_DREC_TX BIT(13) /* 1 = transmit, 0 = receive */
31 #define UNIPHIER_I2C_DREC_STS BIT(12) /* stop condition detected */
32 #define UNIPHIER_I2C_DREC_LRB BIT(11) /* no ACK */
33 #define UNIPHIER_I2C_DREC_LAB BIT(9) /* arbitration lost */
34 #define UNIPHIER_I2C_DREC_BBN BIT(8) /* bus not busy */
35 #define UNIPHIER_I2C_MYAD 0x08 /* slave address */
36 #define UNIPHIER_I2C_CLK 0x0c /* clock frequency control */
37 #define UNIPHIER_I2C_BRST 0x10 /* bus reset */
38 #define UNIPHIER_I2C_BRST_FOEN BIT(1) /* normal operation */
39 #define UNIPHIER_I2C_BRST_RSCL BIT(0) /* release SCL */
40 #define UNIPHIER_I2C_HOLD 0x14 /* hold time control */
41 #define UNIPHIER_I2C_BSTS 0x18 /* bus status monitor */
42 #define UNIPHIER_I2C_BSTS_SDA BIT(1) /* readback of SDA line */
43 #define UNIPHIER_I2C_BSTS_SCL BIT(0) /* readback of SCL line */
44 #define UNIPHIER_I2C_NOISE 0x1c /* noise filter control */
45 #define UNIPHIER_I2C_SETUP 0x20 /* setup time control */
47 #define UNIPHIER_I2C_DEFAULT_SPEED 100000
48 #define UNIPHIER_I2C_MAX_SPEED 400000
50 struct uniphier_i2c_priv {
51 struct completion comp;
52 struct i2c_adapter adap;
53 void __iomem *membase;
55 unsigned int busy_cnt;
58 static irqreturn_t uniphier_i2c_interrupt(int irq, void *dev_id)
60 struct uniphier_i2c_priv *priv = dev_id;
63 * This hardware uses edge triggered interrupt. Do not touch the
64 * hardware registers in this handler to make sure to catch the next
65 * interrupt edge. Just send a complete signal and return.
67 complete(&priv->comp);
72 static int uniphier_i2c_xfer_byte(struct i2c_adapter *adap, u32 txdata,
75 struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
76 unsigned long time_left;
79 reinit_completion(&priv->comp);
81 txdata |= UNIPHIER_I2C_DTRM_IRQEN;
82 dev_dbg(&adap->dev, "write data: 0x%04x\n", txdata);
83 writel(txdata, priv->membase + UNIPHIER_I2C_DTRM);
85 time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
86 if (unlikely(!time_left)) {
87 dev_err(&adap->dev, "transaction timeout\n");
91 rxdata = readl(priv->membase + UNIPHIER_I2C_DREC);
92 dev_dbg(&adap->dev, "read data: 0x%04x\n", rxdata);
100 static int uniphier_i2c_send_byte(struct i2c_adapter *adap, u32 txdata)
105 ret = uniphier_i2c_xfer_byte(adap, txdata, &rxdata);
109 if (unlikely(rxdata & UNIPHIER_I2C_DREC_LAB)) {
110 dev_dbg(&adap->dev, "arbitration lost\n");
113 if (unlikely(rxdata & UNIPHIER_I2C_DREC_LRB)) {
114 dev_dbg(&adap->dev, "could not get ACK\n");
121 static int uniphier_i2c_tx(struct i2c_adapter *adap, u16 addr, u16 len,
126 dev_dbg(&adap->dev, "start condition\n");
127 ret = uniphier_i2c_send_byte(adap, addr << 1 |
128 UNIPHIER_I2C_DTRM_STA |
129 UNIPHIER_I2C_DTRM_NACK);
134 ret = uniphier_i2c_send_byte(adap,
135 UNIPHIER_I2C_DTRM_NACK | *buf++);
143 static int uniphier_i2c_rx(struct i2c_adapter *adap, u16 addr, u16 len,
148 dev_dbg(&adap->dev, "start condition\n");
149 ret = uniphier_i2c_send_byte(adap, addr << 1 |
150 UNIPHIER_I2C_DTRM_STA |
151 UNIPHIER_I2C_DTRM_NACK |
152 UNIPHIER_I2C_DTRM_RD);
159 ret = uniphier_i2c_xfer_byte(adap,
160 len ? 0 : UNIPHIER_I2C_DTRM_NACK,
170 static int uniphier_i2c_stop(struct i2c_adapter *adap)
172 dev_dbg(&adap->dev, "stop condition\n");
173 return uniphier_i2c_send_byte(adap, UNIPHIER_I2C_DTRM_STO |
174 UNIPHIER_I2C_DTRM_NACK);
177 static int uniphier_i2c_master_xfer_one(struct i2c_adapter *adap,
178 struct i2c_msg *msg, bool stop)
180 bool is_read = msg->flags & I2C_M_RD;
181 bool recovery = false;
184 dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n",
185 is_read ? "receive" : "transmit", msg->addr, msg->len, stop);
188 ret = uniphier_i2c_rx(adap, msg->addr, msg->len, msg->buf);
190 ret = uniphier_i2c_tx(adap, msg->addr, msg->len, msg->buf);
192 if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */
195 if (ret == -ETIMEDOUT) {
196 /* This error is fatal. Needs recovery. */
202 int ret2 = uniphier_i2c_stop(adap);
205 /* Failed to issue STOP. The bus needs recovery. */
212 i2c_recover_bus(adap);
217 static int uniphier_i2c_check_bus_busy(struct i2c_adapter *adap)
219 struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
221 if (!(readl(priv->membase + UNIPHIER_I2C_DREC) &
222 UNIPHIER_I2C_DREC_BBN)) {
223 if (priv->busy_cnt++ > 3) {
225 * If bus busy continues too long, it is probably
226 * in a wrong state. Try bus recovery.
228 i2c_recover_bus(adap);
239 static int uniphier_i2c_master_xfer(struct i2c_adapter *adap,
240 struct i2c_msg *msgs, int num)
242 struct i2c_msg *msg, *emsg = msgs + num;
245 ret = uniphier_i2c_check_bus_busy(adap);
249 for (msg = msgs; msg < emsg; msg++) {
250 /* Emit STOP if it is the last message or I2C_M_STOP is set. */
251 bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
253 ret = uniphier_i2c_master_xfer_one(adap, msg, stop);
261 static u32 uniphier_i2c_functionality(struct i2c_adapter *adap)
263 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
266 static const struct i2c_algorithm uniphier_i2c_algo = {
267 .master_xfer = uniphier_i2c_master_xfer,
268 .functionality = uniphier_i2c_functionality,
271 static void uniphier_i2c_reset(struct uniphier_i2c_priv *priv, bool reset_on)
273 u32 val = UNIPHIER_I2C_BRST_RSCL;
275 val |= reset_on ? 0 : UNIPHIER_I2C_BRST_FOEN;
276 writel(val, priv->membase + UNIPHIER_I2C_BRST);
279 static int uniphier_i2c_get_scl(struct i2c_adapter *adap)
281 struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
283 return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
284 UNIPHIER_I2C_BSTS_SCL);
287 static void uniphier_i2c_set_scl(struct i2c_adapter *adap, int val)
289 struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
291 writel(val ? UNIPHIER_I2C_BRST_RSCL : 0,
292 priv->membase + UNIPHIER_I2C_BRST);
295 static int uniphier_i2c_get_sda(struct i2c_adapter *adap)
297 struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
299 return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
300 UNIPHIER_I2C_BSTS_SDA);
303 static void uniphier_i2c_unprepare_recovery(struct i2c_adapter *adap)
305 uniphier_i2c_reset(i2c_get_adapdata(adap), false);
308 static struct i2c_bus_recovery_info uniphier_i2c_bus_recovery_info = {
309 .recover_bus = i2c_generic_scl_recovery,
310 .get_scl = uniphier_i2c_get_scl,
311 .set_scl = uniphier_i2c_set_scl,
312 .get_sda = uniphier_i2c_get_sda,
313 .unprepare_recovery = uniphier_i2c_unprepare_recovery,
316 static void uniphier_i2c_hw_init(struct uniphier_i2c_priv *priv,
317 u32 bus_speed, unsigned long clk_rate)
319 uniphier_i2c_reset(priv, true);
321 writel((clk_rate / bus_speed / 2 << 16) | (clk_rate / bus_speed),
322 priv->membase + UNIPHIER_I2C_CLK);
324 uniphier_i2c_reset(priv, false);
327 static int uniphier_i2c_probe(struct platform_device *pdev)
329 struct device *dev = &pdev->dev;
330 struct uniphier_i2c_priv *priv;
331 struct resource *regs;
333 unsigned long clk_rate;
336 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
340 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
341 priv->membase = devm_ioremap_resource(dev, regs);
342 if (IS_ERR(priv->membase))
343 return PTR_ERR(priv->membase);
345 irq = platform_get_irq(pdev, 0);
347 dev_err(dev, "failed to get IRQ number\n");
351 if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
352 bus_speed = UNIPHIER_I2C_DEFAULT_SPEED;
354 if (!bus_speed || bus_speed > UNIPHIER_I2C_MAX_SPEED) {
355 dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
359 priv->clk = devm_clk_get(dev, NULL);
360 if (IS_ERR(priv->clk)) {
361 dev_err(dev, "failed to get clock\n");
362 return PTR_ERR(priv->clk);
365 ret = clk_prepare_enable(priv->clk);
369 clk_rate = clk_get_rate(priv->clk);
371 dev_err(dev, "input clock rate should not be zero\n");
376 init_completion(&priv->comp);
377 priv->adap.owner = THIS_MODULE;
378 priv->adap.algo = &uniphier_i2c_algo;
379 priv->adap.dev.parent = dev;
380 priv->adap.dev.of_node = dev->of_node;
381 strlcpy(priv->adap.name, "UniPhier I2C", sizeof(priv->adap.name));
382 priv->adap.bus_recovery_info = &uniphier_i2c_bus_recovery_info;
383 i2c_set_adapdata(&priv->adap, priv);
384 platform_set_drvdata(pdev, priv);
386 uniphier_i2c_hw_init(priv, bus_speed, clk_rate);
388 ret = devm_request_irq(dev, irq, uniphier_i2c_interrupt, 0, pdev->name,
391 dev_err(dev, "failed to request irq %d\n", irq);
395 ret = i2c_add_adapter(&priv->adap);
398 clk_disable_unprepare(priv->clk);
403 static int uniphier_i2c_remove(struct platform_device *pdev)
405 struct uniphier_i2c_priv *priv = platform_get_drvdata(pdev);
407 i2c_del_adapter(&priv->adap);
408 clk_disable_unprepare(priv->clk);
413 static const struct of_device_id uniphier_i2c_match[] = {
414 { .compatible = "socionext,uniphier-i2c" },
417 MODULE_DEVICE_TABLE(of, uniphier_i2c_match);
419 static struct platform_driver uniphier_i2c_drv = {
420 .probe = uniphier_i2c_probe,
421 .remove = uniphier_i2c_remove,
423 .name = "uniphier-i2c",
424 .of_match_table = uniphier_i2c_match,
427 module_platform_driver(uniphier_i2c_drv);
429 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
430 MODULE_DESCRIPTION("UniPhier I2C bus driver");
431 MODULE_LICENSE("GPL");