e8c3ce0eaf4d38d58769a1c0f6a1e883145aa054
[cascardo/linux.git] / drivers / i2c / busses / i2c-thunderx-pcidrv.c
1 /*
2  * Cavium ThunderX i2c driver.
3  *
4  * Copyright (C) 2015,2016 Cavium Inc.
5  * Authors: Fred Martin <fmartin@caviumnetworks.com>
6  *          Jan Glauber <jglauber@cavium.com>
7  *
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.
11  */
12
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>
23
24 #include "i2c-octeon-core.h"
25
26 #define DRV_NAME "i2c-thunderx"
27
28 #define PCI_DEVICE_ID_THUNDER_TWSI      0xa012
29
30 #define SYS_FREQ_DEFAULT                700000000
31
32 #define TWSI_INT_ENA_W1C                0x1028
33 #define TWSI_INT_ENA_W1S                0x1030
34
35 /*
36  * Enable the CORE interrupt.
37  * The interrupt will be asserted when there is non-STAT_IDLE state in the
38  * SW_TWSI_EOP_TWSI_STAT register.
39  */
40 static void thunder_i2c_int_enable(struct octeon_i2c *i2c)
41 {
42         octeon_i2c_writeq_flush(TWSI_INT_CORE_INT,
43                                 i2c->twsi_base + TWSI_INT_ENA_W1S);
44 }
45
46 /*
47  * Disable the CORE interrupt.
48  */
49 static void thunder_i2c_int_disable(struct octeon_i2c *i2c)
50 {
51         octeon_i2c_writeq_flush(TWSI_INT_CORE_INT,
52                                 i2c->twsi_base + TWSI_INT_ENA_W1C);
53 }
54
55 static void thunder_i2c_hlc_int_enable(struct octeon_i2c *i2c)
56 {
57         octeon_i2c_writeq_flush(TWSI_INT_ST_INT | TWSI_INT_TS_INT,
58                                 i2c->twsi_base + TWSI_INT_ENA_W1S);
59 }
60
61 static void thunder_i2c_hlc_int_disable(struct octeon_i2c *i2c)
62 {
63         octeon_i2c_writeq_flush(TWSI_INT_ST_INT | TWSI_INT_TS_INT,
64                                 i2c->twsi_base + TWSI_INT_ENA_W1C);
65 }
66
67 static u32 thunderx_i2c_functionality(struct i2c_adapter *adap)
68 {
69         return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
70                I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
71 }
72
73 static const struct i2c_algorithm thunderx_i2c_algo = {
74         .master_xfer = octeon_i2c_xfer,
75         .functionality = thunderx_i2c_functionality,
76 };
77
78 static struct i2c_adapter thunderx_i2c_ops = {
79         .owner  = THIS_MODULE,
80         .name   = "ThunderX adapter",
81         .algo   = &thunderx_i2c_algo,
82 };
83
84 static void thunder_i2c_clock_enable(struct device *dev, struct octeon_i2c *i2c)
85 {
86         int ret;
87
88         i2c->clk = clk_get(dev, NULL);
89         if (IS_ERR(i2c->clk)) {
90                 i2c->clk = NULL;
91                 goto skip;
92         }
93
94         ret = clk_prepare_enable(i2c->clk);
95         if (ret)
96                 goto skip;
97         i2c->sys_freq = clk_get_rate(i2c->clk);
98
99 skip:
100         if (!i2c->sys_freq)
101                 i2c->sys_freq = SYS_FREQ_DEFAULT;
102 }
103
104 static void thunder_i2c_clock_disable(struct device *dev, struct clk *clk)
105 {
106         if (!clk)
107                 return;
108         clk_disable_unprepare(clk);
109         clk_put(clk);
110 }
111
112 static int thunder_i2c_smbus_setup_of(struct octeon_i2c *i2c,
113                                       struct device_node *node)
114 {
115         u32 type;
116
117         if (!node)
118                 return -EINVAL;
119
120         i2c->alert_data.irq = irq_of_parse_and_map(node, 0);
121         if (!i2c->alert_data.irq)
122                 return -EINVAL;
123
124         type = irqd_get_trigger_type(irq_get_irq_data(i2c->alert_data.irq));
125         i2c->alert_data.alert_edge_triggered =
126                 (type & IRQ_TYPE_LEVEL_MASK) ? 1 : 0;
127
128         i2c->ara = i2c_setup_smbus_alert(&i2c->adap, &i2c->alert_data);
129         if (!i2c->ara)
130                 return -ENODEV;
131         return 0;
132 }
133
134 static int thunder_i2c_smbus_setup(struct octeon_i2c *i2c,
135                                    struct device_node *node)
136 {
137         /* TODO: ACPI support */
138         if (!acpi_disabled)
139                 return -EOPNOTSUPP;
140
141         return thunder_i2c_smbus_setup_of(i2c, node);
142 }
143
144 static void thunder_i2c_smbus_remove(struct octeon_i2c *i2c)
145 {
146         if (i2c->ara)
147                 i2c_unregister_device(i2c->ara);
148 }
149
150 static int thunder_i2c_probe_pci(struct pci_dev *pdev,
151                                  const struct pci_device_id *ent)
152 {
153         struct device *dev = &pdev->dev;
154         struct octeon_i2c *i2c;
155         int ret;
156
157         i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
158         if (!i2c)
159                 return -ENOMEM;
160
161         i2c->dev = dev;
162         pci_set_drvdata(pdev, i2c);
163         ret = pcim_enable_device(pdev);
164         if (ret)
165                 return ret;
166
167         ret = pci_request_regions(pdev, DRV_NAME);
168         if (ret)
169                 return ret;
170
171         i2c->twsi_base = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
172         if (!i2c->twsi_base)
173                 return -EINVAL;
174
175         thunder_i2c_clock_enable(dev, i2c);
176         ret = device_property_read_u32(dev, "clock-frequency", &i2c->twsi_freq);
177         if (ret)
178                 i2c->twsi_freq = 100000;
179
180         init_waitqueue_head(&i2c->queue);
181
182         i2c->int_enable = thunder_i2c_int_enable;
183         i2c->int_disable = thunder_i2c_int_disable;
184         i2c->hlc_int_enable = thunder_i2c_hlc_int_enable;
185         i2c->hlc_int_disable = thunder_i2c_hlc_int_disable;
186
187         ret = pci_enable_msix(pdev, &i2c->i2c_msix, 1);
188         if (ret)
189                 goto error;
190
191         ret = devm_request_irq(dev, i2c->i2c_msix.vector, octeon_i2c_isr, 0,
192                                DRV_NAME, i2c);
193         if (ret)
194                 goto error;
195
196         ret = octeon_i2c_init_lowlevel(i2c);
197         if (ret)
198                 goto error;
199
200         octeon_i2c_set_clock(i2c);
201
202         i2c->adap = thunderx_i2c_ops;
203         i2c->adap.retries = 5;
204         i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
205         i2c->adap.dev.parent = dev;
206         i2c->adap.dev.of_node = pdev->dev.of_node;
207         snprintf(i2c->adap.name, sizeof(i2c->adap.name),
208                  "Cavium ThunderX i2c adapter at %s", dev_name(dev));
209         i2c_set_adapdata(&i2c->adap, i2c);
210
211         ret = i2c_add_adapter(&i2c->adap);
212         if (ret)
213                 goto error;
214
215         dev_info(i2c->dev, "Probed. Set system clock to %u\n", i2c->sys_freq);
216
217         ret = thunder_i2c_smbus_setup(i2c, pdev->dev.of_node);
218         if (ret)
219                 dev_info(dev, "SMBUS alert not active on this bus\n");
220
221         return 0;
222
223 error:
224         thunder_i2c_clock_disable(dev, i2c->clk);
225         return ret;
226 }
227
228 static void thunder_i2c_remove_pci(struct pci_dev *pdev)
229 {
230         struct octeon_i2c *i2c = pci_get_drvdata(pdev);
231
232         thunder_i2c_smbus_remove(i2c);
233         thunder_i2c_clock_disable(&pdev->dev, i2c->clk);
234         i2c_del_adapter(&i2c->adap);
235 }
236
237 static const struct pci_device_id thunder_i2c_pci_id_table[] = {
238         { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_TWSI) },
239         { 0, }
240 };
241
242 MODULE_DEVICE_TABLE(pci, thunder_i2c_pci_id_table);
243
244 static struct pci_driver thunder_i2c_pci_driver = {
245         .name           = DRV_NAME,
246         .id_table       = thunder_i2c_pci_id_table,
247         .probe          = thunder_i2c_probe_pci,
248         .remove         = thunder_i2c_remove_pci,
249 };
250
251 module_pci_driver(thunder_i2c_pci_driver);
252
253 MODULE_LICENSE("GPL");
254 MODULE_AUTHOR("Fred Martin <fmartin@caviumnetworks.com>");
255 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium ThunderX SOC");