2 * Cavium ThunderX i2c driver.
4 * Copyright (C) 2015,2016 Cavium Inc.
5 * Authors: Fred Martin <fmartin@caviumnetworks.com>
6 * Jan Glauber <jglauber@cavium.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #include <linux/acpi.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/i2c-smbus.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_irq.h>
22 #include <linux/pci.h>
24 #include "i2c-octeon-core.h"
26 #define DRV_NAME "i2c-thunderx"
28 #define PCI_DEVICE_ID_THUNDER_TWSI 0xa012
30 #define SYS_FREQ_DEFAULT 800000000
31 #define OTX2_REF_FREQ_DEFAULT 100000000
33 #define TWSI_INT_ENA_W1C 0x1028
34 #define TWSI_INT_ENA_W1S 0x1030
37 * Enable the CORE interrupt.
38 * The interrupt will be asserted when there is non-STAT_IDLE state in the
39 * SW_TWSI_EOP_TWSI_STAT register.
41 static void thunder_i2c_int_enable(struct octeon_i2c
*i2c
)
43 octeon_i2c_writeq_flush(TWSI_INT_CORE_INT
,
44 i2c
->twsi_base
+ TWSI_INT_ENA_W1S
);
48 * Disable the CORE interrupt.
50 static void thunder_i2c_int_disable(struct octeon_i2c
*i2c
)
52 octeon_i2c_writeq_flush(TWSI_INT_CORE_INT
,
53 i2c
->twsi_base
+ TWSI_INT_ENA_W1C
);
56 static void thunder_i2c_hlc_int_enable(struct octeon_i2c
*i2c
)
58 octeon_i2c_writeq_flush(TWSI_INT_ST_INT
| TWSI_INT_TS_INT
,
59 i2c
->twsi_base
+ TWSI_INT_ENA_W1S
);
62 static void thunder_i2c_hlc_int_disable(struct octeon_i2c
*i2c
)
64 octeon_i2c_writeq_flush(TWSI_INT_ST_INT
| TWSI_INT_TS_INT
,
65 i2c
->twsi_base
+ TWSI_INT_ENA_W1C
);
68 static u32
thunderx_i2c_functionality(struct i2c_adapter
*adap
)
70 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
) |
71 I2C_FUNC_SMBUS_READ_BLOCK_DATA
| I2C_SMBUS_BLOCK_PROC_CALL
;
74 static const struct i2c_algorithm thunderx_i2c_algo
= {
75 .xfer
= octeon_i2c_xfer
,
76 .functionality
= thunderx_i2c_functionality
,
79 static const struct i2c_adapter thunderx_i2c_ops
= {
81 .name
= "ThunderX adapter",
82 .algo
= &thunderx_i2c_algo
,
85 static void thunder_i2c_clock_enable(struct device
*dev
, struct octeon_i2c
*i2c
)
91 i2c
->clk
= clk_get(dev
, NULL
);
92 if (IS_ERR(i2c
->clk
)) {
97 ret
= clk_prepare_enable(i2c
->clk
);
100 i2c
->sys_freq
= clk_get_rate(i2c
->clk
);
103 if (device_property_read_u32(dev
, "sclk", &i2c
->sys_freq
))
104 device_property_read_u32(dev
, "ioclk", &i2c
->sys_freq
);
109 i2c
->sys_freq
= SYS_FREQ_DEFAULT
;
112 static void thunder_i2c_clock_disable(struct device
*dev
, struct clk
*clk
)
116 clk_disable_unprepare(clk
);
120 static int thunder_i2c_smbus_setup_of(struct octeon_i2c
*i2c
,
121 struct device_node
*node
)
123 struct i2c_client
*ara
;
128 i2c
->alert_data
.irq
= irq_of_parse_and_map(node
, 0);
129 if (!i2c
->alert_data
.irq
)
132 ara
= i2c_new_smbus_alert_device(&i2c
->adap
, &i2c
->alert_data
);
141 static int thunder_i2c_smbus_setup(struct octeon_i2c
*i2c
,
142 struct device_node
*node
)
144 /* TODO: ACPI support */
148 return thunder_i2c_smbus_setup_of(i2c
, node
);
151 static void thunder_i2c_smbus_remove(struct octeon_i2c
*i2c
)
153 i2c_unregister_device(i2c
->ara
);
156 static int thunder_i2c_probe_pci(struct pci_dev
*pdev
,
157 const struct pci_device_id
*ent
)
159 struct device
*dev
= &pdev
->dev
;
160 struct octeon_i2c
*i2c
;
163 i2c
= devm_kzalloc(dev
, sizeof(*i2c
), GFP_KERNEL
);
167 i2c
->roff
.sw_twsi
= 0x1000;
168 i2c
->roff
.twsi_int
= 0x1010;
169 i2c
->roff
.sw_twsi_ext
= 0x1018;
170 i2c
->roff
.mode
= 0x1038;
173 pci_set_drvdata(pdev
, i2c
);
174 ret
= pcim_enable_device(pdev
);
178 ret
= pci_request_regions(pdev
, DRV_NAME
);
182 i2c
->twsi_base
= pcim_iomap(pdev
, 0, pci_resource_len(pdev
, 0));
186 thunder_i2c_clock_enable(dev
, i2c
);
187 ret
= device_property_read_u32(dev
, "clock-frequency", &i2c
->twsi_freq
);
189 i2c
->twsi_freq
= I2C_MAX_STANDARD_MODE_FREQ
;
191 init_waitqueue_head(&i2c
->queue
);
193 i2c
->int_enable
= thunder_i2c_int_enable
;
194 i2c
->int_disable
= thunder_i2c_int_disable
;
195 i2c
->hlc_int_enable
= thunder_i2c_hlc_int_enable
;
196 i2c
->hlc_int_disable
= thunder_i2c_hlc_int_disable
;
198 ret
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_MSIX
);
202 ret
= devm_request_irq(dev
, pci_irq_vector(pdev
, 0), octeon_i2c_isr
, 0,
207 ret
= octeon_i2c_init_lowlevel(i2c
);
212 * For OcteonTX2 chips, set reference frequency to 100MHz
213 * as refclk_src in TWSI_MODE register defaults to 100MHz.
215 if (octeon_i2c_is_otx2(pdev
) && IS_LS_FREQ(i2c
->twsi_freq
))
216 i2c
->sys_freq
= OTX2_REF_FREQ_DEFAULT
;
217 octeon_i2c_set_clock(i2c
);
219 i2c
->adap
= thunderx_i2c_ops
;
220 i2c
->adap
.retries
= 5;
221 i2c
->adap
.class = I2C_CLASS_HWMON
;
222 i2c
->adap
.bus_recovery_info
= &octeon_i2c_recovery_info
;
223 i2c
->adap
.dev
.parent
= dev
;
224 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
225 i2c
->adap
.dev
.fwnode
= dev
->fwnode
;
226 snprintf(i2c
->adap
.name
, sizeof(i2c
->adap
.name
),
227 "Cavium ThunderX i2c adapter at %s", dev_name(dev
));
228 i2c_set_adapdata(&i2c
->adap
, i2c
);
230 ret
= i2c_add_adapter(&i2c
->adap
);
234 dev_info(i2c
->dev
, "Probed. Set system clock to %u\n", i2c
->sys_freq
);
236 ret
= thunder_i2c_smbus_setup(i2c
, pdev
->dev
.of_node
);
238 dev_info(dev
, "SMBUS alert not active on this bus\n");
243 thunder_i2c_clock_disable(dev
, i2c
->clk
);
247 static void thunder_i2c_remove_pci(struct pci_dev
*pdev
)
249 struct octeon_i2c
*i2c
= pci_get_drvdata(pdev
);
251 thunder_i2c_smbus_remove(i2c
);
252 thunder_i2c_clock_disable(&pdev
->dev
, i2c
->clk
);
253 i2c_del_adapter(&i2c
->adap
);
256 static const struct pci_device_id thunder_i2c_pci_id_table
[] = {
257 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM
, PCI_DEVICE_ID_THUNDER_TWSI
) },
261 MODULE_DEVICE_TABLE(pci
, thunder_i2c_pci_id_table
);
263 static struct pci_driver thunder_i2c_pci_driver
= {
265 .id_table
= thunder_i2c_pci_id_table
,
266 .probe
= thunder_i2c_probe_pci
,
267 .remove
= thunder_i2c_remove_pci
,
270 module_pci_driver(thunder_i2c_pci_driver
);
272 MODULE_LICENSE("GPL");
273 MODULE_AUTHOR("Fred Martin <fmartin@caviumnetworks.com>");
274 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium ThunderX SOC");