blob: 53fa9a0d96bbed8647f0597d933d0a500a4c720e [file] [log] [blame]
Rade Bozic85660f42010-01-28 12:47:07 -08001/*
2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
4 *
Jan Glauberdfcd8212016-03-18 09:46:26 +01005 * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
Rade Bozic85660f42010-01-28 12:47:07 -08006 *
7 * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
Jan Glauber4729cbe2016-04-25 16:33:35 +020014#include <linux/atomic.h>
David Daneyf353a212012-07-05 18:12:39 +020015#include <linux/platform_device.h>
16#include <linux/interrupt.h>
Rade Bozic85660f42010-01-28 12:47:07 -080017#include <linux/kernel.h>
18#include <linux/module.h>
David Daneyf353a212012-07-05 18:12:39 +020019#include <linux/delay.h>
Rade Bozic85660f42010-01-28 12:47:07 -080020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Rade Bozic85660f42010-01-28 12:47:07 -080022#include <linux/i2c.h>
David Daneyf353a212012-07-05 18:12:39 +020023#include <linux/io.h>
24#include <linux/of.h>
Rade Bozic85660f42010-01-28 12:47:07 -080025
26#include <asm/octeon/octeon.h>
Jan Glauberad836652016-08-24 23:25:43 +020027#include "i2c-octeon-core.h"
Rade Bozic85660f42010-01-28 12:47:07 -080028
29#define DRV_NAME "i2c-octeon"
30
Rade Bozic85660f42010-01-28 12:47:07 -080031/**
Jan Glauberbd7784c2016-03-07 16:10:44 +010032 * octeon_i2c_int_enable - enable the CORE interrupt
33 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -080034 *
35 * The interrupt will be asserted when there is non-STAT_IDLE state in
36 * the SW_TWSI_EOP_TWSI_STAT register.
37 */
38static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
39{
Jan Glauberdfcd8212016-03-18 09:46:26 +010040 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -080041}
42
Jan Glauberbd7784c2016-03-07 16:10:44 +010043/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -080044static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
45{
Jan Glauberdfcd8212016-03-18 09:46:26 +010046 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -080047 octeon_i2c_write_int(i2c, 0);
48}
49
Jan Glauber4729cbe2016-04-25 16:33:35 +020050/**
51 * octeon_i2c_int_enable78 - enable the CORE interrupt
52 * @i2c: The struct octeon_i2c
53 *
54 * The interrupt will be asserted when there is non-STAT_IDLE state in the
55 * SW_TWSI_EOP_TWSI_STAT register.
56 */
57static void octeon_i2c_int_enable78(struct octeon_i2c *i2c)
58{
59 atomic_inc_return(&i2c->int_enable_cnt);
60 enable_irq(i2c->irq);
61}
62
63static void __octeon_i2c_irq_disable(atomic_t *cnt, int irq)
64{
65 int count;
66
67 /*
68 * The interrupt can be disabled in two places, but we only
69 * want to make the disable_irq_nosync() call once, so keep
70 * track with the atomic variable.
71 */
72 count = atomic_dec_if_positive(cnt);
73 if (count >= 0)
74 disable_irq_nosync(irq);
75}
76
77/* disable the CORE interrupt */
78static void octeon_i2c_int_disable78(struct octeon_i2c *i2c)
79{
80 __octeon_i2c_irq_disable(&i2c->int_enable_cnt, i2c->irq);
81}
82
83/**
84 * octeon_i2c_hlc_int_enable78 - enable the ST interrupt
85 * @i2c: The struct octeon_i2c
86 *
87 * The interrupt will be asserted when there is non-STAT_IDLE state in
88 * the SW_TWSI_EOP_TWSI_STAT register.
89 */
90static void octeon_i2c_hlc_int_enable78(struct octeon_i2c *i2c)
91{
92 atomic_inc_return(&i2c->hlc_int_enable_cnt);
93 enable_irq(i2c->hlc_irq);
94}
95
96/* disable the ST interrupt */
97static void octeon_i2c_hlc_int_disable78(struct octeon_i2c *i2c)
98{
99 __octeon_i2c_irq_disable(&i2c->hlc_int_enable_cnt, i2c->hlc_irq);
100}
101
Jan Glauber4729cbe2016-04-25 16:33:35 +0200102/* HLC interrupt service routine */
103static irqreturn_t octeon_i2c_hlc_isr78(int irq, void *dev_id)
104{
105 struct octeon_i2c *i2c = dev_id;
106
107 i2c->hlc_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000108 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800109
110 return IRQ_HANDLED;
111}
112
David Daneyd1fbff82016-04-25 16:33:34 +0200113static void octeon_i2c_hlc_int_enable(struct octeon_i2c *i2c)
114{
115 octeon_i2c_write_int(i2c, TWSI_INT_ST_EN);
116}
117
Rade Bozic85660f42010-01-28 12:47:07 -0800118static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
119{
Jan Glauber392d01d2016-04-26 16:26:52 +0200120 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
David Daney886f6f82016-03-18 09:46:29 +0100121 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -0800122}
123
124static const struct i2c_algorithm octeon_i2c_algo = {
125 .master_xfer = octeon_i2c_xfer,
126 .functionality = octeon_i2c_functionality,
127};
128
129static struct i2c_adapter octeon_i2c_ops = {
130 .owner = THIS_MODULE,
131 .name = "OCTEON adapter",
132 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -0800133};
134
Bill Pemberton0b255e92012-11-27 15:59:38 -0500135static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800136{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100137 struct device_node *node = pdev->dev.of_node;
Jan Glauber4729cbe2016-04-25 16:33:35 +0200138 int irq, result = 0, hlc_irq = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800139 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100140 struct octeon_i2c *i2c;
Jan Glauber4729cbe2016-04-25 16:33:35 +0200141 bool cn78xx_style;
Rade Bozic85660f42010-01-28 12:47:07 -0800142
Jan Glauber4729cbe2016-04-25 16:33:35 +0200143 cn78xx_style = of_device_is_compatible(node, "cavium,octeon-7890-twsi");
144 if (cn78xx_style) {
145 hlc_irq = platform_get_irq(pdev, 0);
146 if (hlc_irq < 0)
147 return hlc_irq;
148
149 irq = platform_get_irq(pdev, 2);
150 if (irq < 0)
151 return irq;
152 } else {
153 /* All adaptors have an irq. */
154 irq = platform_get_irq(pdev, 0);
155 if (irq < 0)
156 return irq;
157 }
Rade Bozic85660f42010-01-28 12:47:07 -0800158
David Daneyf353a212012-07-05 18:12:39 +0200159 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -0800160 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -0800161 result = -ENOMEM;
162 goto out;
163 }
164 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -0800165
166 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +0100167 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
168 if (IS_ERR(i2c->twsi_base)) {
169 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +0200170 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800171 }
Rade Bozic85660f42010-01-28 12:47:07 -0800172
David Daneyf353a212012-07-05 18:12:39 +0200173 /*
174 * "clock-rate" is a legacy binding, the official binding is
175 * "clock-frequency". Try the official one first and then
176 * fall back if it doesn't exist.
177 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100178 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
179 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +0200180 dev_err(i2c->dev,
181 "no I2C 'clock-rate' or 'clock-frequency' property\n");
182 result = -ENXIO;
183 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800184 }
David Daneyf353a212012-07-05 18:12:39 +0200185
186 i2c->sys_freq = octeon_get_io_clock_rate();
187
Rade Bozic85660f42010-01-28 12:47:07 -0800188 init_waitqueue_head(&i2c->queue);
189
190 i2c->irq = irq;
191
Jan Glauber4729cbe2016-04-25 16:33:35 +0200192 if (cn78xx_style) {
193 i2c->hlc_irq = hlc_irq;
194
195 i2c->int_enable = octeon_i2c_int_enable78;
196 i2c->int_disable = octeon_i2c_int_disable78;
197 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable78;
198 i2c->hlc_int_disable = octeon_i2c_hlc_int_disable78;
199
200 irq_set_status_flags(i2c->irq, IRQ_NOAUTOEN);
201 irq_set_status_flags(i2c->hlc_irq, IRQ_NOAUTOEN);
202
203 result = devm_request_irq(&pdev->dev, i2c->hlc_irq,
204 octeon_i2c_hlc_isr78, 0,
205 DRV_NAME, i2c);
206 if (result < 0) {
207 dev_err(i2c->dev, "failed to attach interrupt\n");
208 goto out;
209 }
210 } else {
211 i2c->int_enable = octeon_i2c_int_enable;
212 i2c->int_disable = octeon_i2c_int_disable;
213 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable;
214 i2c->hlc_int_disable = octeon_i2c_int_disable;
215 }
216
David Daneyf353a212012-07-05 18:12:39 +0200217 result = devm_request_irq(&pdev->dev, i2c->irq,
218 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800219 if (result < 0) {
220 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +0200221 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800222 }
223
David Daneyfe600cf2016-04-27 11:44:39 +0200224 if (OCTEON_IS_MODEL(OCTEON_CN38XX))
225 i2c->broken_irq_check = true;
226
Jan Glauberdfcd8212016-03-18 09:46:26 +0100227 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800228 if (result) {
229 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +0200230 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800231 }
232
Jan Glauberdfcd8212016-03-18 09:46:26 +0100233 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800234
235 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +0200236 i2c->adap.timeout = msecs_to_jiffies(2);
237 i2c->adap.retries = 5;
Jan Glauberc981e342016-04-25 16:33:31 +0200238 i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
Rade Bozic85660f42010-01-28 12:47:07 -0800239 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100240 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -0800241 i2c_set_adapdata(&i2c->adap, i2c);
242 platform_set_drvdata(pdev, i2c);
243
David Daneyf353a212012-07-05 18:12:39 +0200244 result = i2c_add_adapter(&i2c->adap);
Wolfram Sangea734402016-08-09 13:36:17 +0200245 if (result < 0)
Doug Anderson55827f42013-02-15 13:18:35 +0000246 goto out;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100247 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +0200248 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800249
Rade Bozic85660f42010-01-28 12:47:07 -0800250out:
251 return result;
252};
253
Bill Pemberton0b255e92012-11-27 15:59:38 -0500254static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800255{
256 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
257
258 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800259 return 0;
260};
261
Jan Glauberdfcd8212016-03-18 09:46:26 +0100262static const struct of_device_id octeon_i2c_match[] = {
263 { .compatible = "cavium,octeon-3860-twsi", },
Jan Glauber4729cbe2016-04-25 16:33:35 +0200264 { .compatible = "cavium,octeon-7890-twsi", },
David Daneyf353a212012-07-05 18:12:39 +0200265 {},
266};
267MODULE_DEVICE_TABLE(of, octeon_i2c_match);
268
Rade Bozic85660f42010-01-28 12:47:07 -0800269static struct platform_driver octeon_i2c_driver = {
270 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500271 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -0800272 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -0800273 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +0200274 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -0800275 },
276};
277
Axel Lina3664b52012-01-12 20:32:04 +0100278module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -0800279
280MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
281MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
282MODULE_LICENSE("GPL");