1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/slab.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
21#include <linux/mfd/dln2.h>
22#include <linux/acpi.h>
23
24#define DLN2_I2C_MODULE_ID 0x03
25#define DLN2_I2C_CMD(cmd) DLN2_CMD(cmd, DLN2_I2C_MODULE_ID)
26
27
28#define DLN2_I2C_GET_PORT_COUNT DLN2_I2C_CMD(0x00)
29#define DLN2_I2C_ENABLE DLN2_I2C_CMD(0x01)
30#define DLN2_I2C_DISABLE DLN2_I2C_CMD(0x02)
31#define DLN2_I2C_IS_ENABLED DLN2_I2C_CMD(0x03)
32#define DLN2_I2C_WRITE DLN2_I2C_CMD(0x06)
33#define DLN2_I2C_READ DLN2_I2C_CMD(0x07)
34#define DLN2_I2C_SCAN_DEVICES DLN2_I2C_CMD(0x08)
35#define DLN2_I2C_PULLUP_ENABLE DLN2_I2C_CMD(0x09)
36#define DLN2_I2C_PULLUP_DISABLE DLN2_I2C_CMD(0x0A)
37#define DLN2_I2C_PULLUP_IS_ENABLED DLN2_I2C_CMD(0x0B)
38#define DLN2_I2C_TRANSFER DLN2_I2C_CMD(0x0C)
39#define DLN2_I2C_SET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0D)
40#define DLN2_I2C_GET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0E)
41
42#define DLN2_I2C_MAX_XFER_SIZE 256
43#define DLN2_I2C_BUF_SIZE (DLN2_I2C_MAX_XFER_SIZE + 16)
44
45struct dln2_i2c {
46 struct platform_device *pdev;
47 struct i2c_adapter adapter;
48 u8 port;
49
50
51
52
53 void *buf;
54};
55
56static int dln2_i2c_enable(struct dln2_i2c *dln2, bool enable)
57{
58 u16 cmd;
59 struct {
60 u8 port;
61 } tx;
62
63 tx.port = dln2->port;
64
65 if (enable)
66 cmd = DLN2_I2C_ENABLE;
67 else
68 cmd = DLN2_I2C_DISABLE;
69
70 return dln2_transfer_tx(dln2->pdev, cmd, &tx, sizeof(tx));
71}
72
73static int dln2_i2c_write(struct dln2_i2c *dln2, u8 addr,
74 u8 *data, u16 data_len)
75{
76 int ret;
77 struct {
78 u8 port;
79 u8 addr;
80 u8 mem_addr_len;
81 __le32 mem_addr;
82 __le16 buf_len;
83 u8 buf[DLN2_I2C_MAX_XFER_SIZE];
84 } __packed *tx = dln2->buf;
85 unsigned len;
86
87 BUILD_BUG_ON(sizeof(*tx) > DLN2_I2C_BUF_SIZE);
88
89 tx->port = dln2->port;
90 tx->addr = addr;
91 tx->mem_addr_len = 0;
92 tx->mem_addr = 0;
93 tx->buf_len = cpu_to_le16(data_len);
94 memcpy(tx->buf, data, data_len);
95
96 len = sizeof(*tx) + data_len - DLN2_I2C_MAX_XFER_SIZE;
97 ret = dln2_transfer_tx(dln2->pdev, DLN2_I2C_WRITE, tx, len);
98 if (ret < 0)
99 return ret;
100
101 return data_len;
102}
103
104static int dln2_i2c_read(struct dln2_i2c *dln2, u16 addr, u8 *data,
105 u16 data_len)
106{
107 int ret;
108 struct {
109 u8 port;
110 u8 addr;
111 u8 mem_addr_len;
112 __le32 mem_addr;
113 __le16 buf_len;
114 } __packed tx;
115 struct {
116 __le16 buf_len;
117 u8 buf[DLN2_I2C_MAX_XFER_SIZE];
118 } __packed *rx = dln2->buf;
119 unsigned rx_len = sizeof(*rx);
120
121 BUILD_BUG_ON(sizeof(*rx) > DLN2_I2C_BUF_SIZE);
122
123 tx.port = dln2->port;
124 tx.addr = addr;
125 tx.mem_addr_len = 0;
126 tx.mem_addr = 0;
127 tx.buf_len = cpu_to_le16(data_len);
128
129 ret = dln2_transfer(dln2->pdev, DLN2_I2C_READ, &tx, sizeof(tx),
130 rx, &rx_len);
131 if (ret < 0)
132 return ret;
133 if (rx_len < sizeof(rx->buf_len) + data_len)
134 return -EPROTO;
135 if (le16_to_cpu(rx->buf_len) != data_len)
136 return -EPROTO;
137
138 memcpy(data, rx->buf, data_len);
139
140 return data_len;
141}
142
143static int dln2_i2c_xfer(struct i2c_adapter *adapter,
144 struct i2c_msg *msgs, int num)
145{
146 struct dln2_i2c *dln2 = i2c_get_adapdata(adapter);
147 struct i2c_msg *pmsg;
148 int i;
149
150 for (i = 0; i < num; i++) {
151 int ret;
152
153 pmsg = &msgs[i];
154
155 if (pmsg->flags & I2C_M_RD) {
156 ret = dln2_i2c_read(dln2, pmsg->addr, pmsg->buf,
157 pmsg->len);
158 if (ret < 0)
159 return ret;
160
161 pmsg->len = ret;
162 } else {
163 ret = dln2_i2c_write(dln2, pmsg->addr, pmsg->buf,
164 pmsg->len);
165 if (ret != pmsg->len)
166 return -EPROTO;
167 }
168 }
169
170 return num;
171}
172
173static u32 dln2_i2c_func(struct i2c_adapter *a)
174{
175 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
176 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
177 I2C_FUNC_SMBUS_I2C_BLOCK;
178}
179
180static const struct i2c_algorithm dln2_i2c_usb_algorithm = {
181 .master_xfer = dln2_i2c_xfer,
182 .functionality = dln2_i2c_func,
183};
184
185static const struct i2c_adapter_quirks dln2_i2c_quirks = {
186 .max_read_len = DLN2_I2C_MAX_XFER_SIZE,
187 .max_write_len = DLN2_I2C_MAX_XFER_SIZE,
188};
189
190static int dln2_i2c_probe(struct platform_device *pdev)
191{
192 int ret;
193 struct dln2_i2c *dln2;
194 struct device *dev = &pdev->dev;
195 struct dln2_platform_data *pdata = dev_get_platdata(&pdev->dev);
196
197 dln2 = devm_kzalloc(dev, sizeof(*dln2), GFP_KERNEL);
198 if (!dln2)
199 return -ENOMEM;
200
201 dln2->buf = devm_kmalloc(dev, DLN2_I2C_BUF_SIZE, GFP_KERNEL);
202 if (!dln2->buf)
203 return -ENOMEM;
204
205 dln2->pdev = pdev;
206 dln2->port = pdata->port;
207
208
209 dln2->adapter.owner = THIS_MODULE;
210 dln2->adapter.class = I2C_CLASS_HWMON;
211 dln2->adapter.algo = &dln2_i2c_usb_algorithm;
212 dln2->adapter.quirks = &dln2_i2c_quirks;
213 dln2->adapter.dev.parent = dev;
214 ACPI_COMPANION_SET(&dln2->adapter.dev, ACPI_COMPANION(&pdev->dev));
215 dln2->adapter.dev.of_node = dev->of_node;
216 i2c_set_adapdata(&dln2->adapter, dln2);
217 snprintf(dln2->adapter.name, sizeof(dln2->adapter.name), "%s-%s-%d",
218 "dln2-i2c", dev_name(pdev->dev.parent), dln2->port);
219
220 platform_set_drvdata(pdev, dln2);
221
222
223 ret = dln2_i2c_enable(dln2, true);
224 if (ret < 0) {
225 dev_err(dev, "failed to initialize adapter: %d\n", ret);
226 return ret;
227 }
228
229
230 ret = i2c_add_adapter(&dln2->adapter);
231 if (ret < 0)
232 goto out_disable;
233
234 return 0;
235
236out_disable:
237 dln2_i2c_enable(dln2, false);
238
239 return ret;
240}
241
242static int dln2_i2c_remove(struct platform_device *pdev)
243{
244 struct dln2_i2c *dln2 = platform_get_drvdata(pdev);
245
246 i2c_del_adapter(&dln2->adapter);
247 dln2_i2c_enable(dln2, false);
248
249 return 0;
250}
251
252static struct platform_driver dln2_i2c_driver = {
253 .driver.name = "dln2-i2c",
254 .probe = dln2_i2c_probe,
255 .remove = dln2_i2c_remove,
256};
257
258module_platform_driver(dln2_i2c_driver);
259
260MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
261MODULE_DESCRIPTION("Driver for the Diolan DLN2 I2C master interface");
262MODULE_LICENSE("GPL v2");
263MODULE_ALIAS("platform:dln2-i2c");
264