1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Diolan DLN-2 USB-I2C adapter
5 * Copyright (c) 2014 Intel Corporation
9 * Copyright (c) 2010-2011 Ericsson AB
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/platform_device.h>
18 #include <linux/mfd/dln2.h>
19 #include <linux/acpi.h>
21 #define DLN2_I2C_MODULE_ID 0x03
22 #define DLN2_I2C_CMD(cmd) DLN2_CMD(cmd, DLN2_I2C_MODULE_ID)
25 #define DLN2_I2C_GET_PORT_COUNT DLN2_I2C_CMD(0x00)
26 #define DLN2_I2C_ENABLE DLN2_I2C_CMD(0x01)
27 #define DLN2_I2C_DISABLE DLN2_I2C_CMD(0x02)
28 #define DLN2_I2C_IS_ENABLED DLN2_I2C_CMD(0x03)
29 #define DLN2_I2C_WRITE DLN2_I2C_CMD(0x06)
30 #define DLN2_I2C_READ DLN2_I2C_CMD(0x07)
31 #define DLN2_I2C_SCAN_DEVICES DLN2_I2C_CMD(0x08)
32 #define DLN2_I2C_PULLUP_ENABLE DLN2_I2C_CMD(0x09)
33 #define DLN2_I2C_PULLUP_DISABLE DLN2_I2C_CMD(0x0A)
34 #define DLN2_I2C_PULLUP_IS_ENABLED DLN2_I2C_CMD(0x0B)
35 #define DLN2_I2C_TRANSFER DLN2_I2C_CMD(0x0C)
36 #define DLN2_I2C_SET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0D)
37 #define DLN2_I2C_GET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0E)
39 #define DLN2_I2C_MAX_XFER_SIZE 256
40 #define DLN2_I2C_BUF_SIZE (DLN2_I2C_MAX_XFER_SIZE + 16)
43 struct platform_device
*pdev
;
44 struct i2c_adapter adapter
;
47 * Buffer to hold the packet for read or write transfers. One is enough
48 * since we can't have multiple transfers in parallel on the i2c bus.
53 static int dln2_i2c_enable(struct dln2_i2c
*dln2
, bool enable
)
63 cmd
= DLN2_I2C_ENABLE
;
65 cmd
= DLN2_I2C_DISABLE
;
67 return dln2_transfer_tx(dln2
->pdev
, cmd
, &tx
, sizeof(tx
));
70 static int dln2_i2c_write(struct dln2_i2c
*dln2
, u8 addr
,
71 u8
*data
, u16 data_len
)
80 u8 buf
[DLN2_I2C_MAX_XFER_SIZE
];
81 } __packed
*tx
= dln2
->buf
;
84 BUILD_BUG_ON(sizeof(*tx
) > DLN2_I2C_BUF_SIZE
);
86 tx
->port
= dln2
->port
;
90 tx
->buf_len
= cpu_to_le16(data_len
);
91 memcpy(tx
->buf
, data
, data_len
);
93 len
= sizeof(*tx
) + data_len
- DLN2_I2C_MAX_XFER_SIZE
;
94 ret
= dln2_transfer_tx(dln2
->pdev
, DLN2_I2C_WRITE
, tx
, len
);
101 static int dln2_i2c_read(struct dln2_i2c
*dln2
, u16 addr
, u8
*data
,
114 u8 buf
[DLN2_I2C_MAX_XFER_SIZE
];
115 } __packed
*rx
= dln2
->buf
;
116 unsigned rx_len
= sizeof(*rx
);
118 BUILD_BUG_ON(sizeof(*rx
) > DLN2_I2C_BUF_SIZE
);
120 tx
.port
= dln2
->port
;
124 tx
.buf_len
= cpu_to_le16(data_len
);
126 ret
= dln2_transfer(dln2
->pdev
, DLN2_I2C_READ
, &tx
, sizeof(tx
),
130 if (rx_len
< sizeof(rx
->buf_len
) + data_len
)
132 if (le16_to_cpu(rx
->buf_len
) != data_len
)
135 memcpy(data
, rx
->buf
, data_len
);
140 static int dln2_i2c_xfer(struct i2c_adapter
*adapter
,
141 struct i2c_msg
*msgs
, int num
)
143 struct dln2_i2c
*dln2
= i2c_get_adapdata(adapter
);
144 struct i2c_msg
*pmsg
;
147 for (i
= 0; i
< num
; i
++) {
152 if (pmsg
->flags
& I2C_M_RD
) {
153 ret
= dln2_i2c_read(dln2
, pmsg
->addr
, pmsg
->buf
,
160 ret
= dln2_i2c_write(dln2
, pmsg
->addr
, pmsg
->buf
,
162 if (ret
!= pmsg
->len
)
170 static u32
dln2_i2c_func(struct i2c_adapter
*a
)
172 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_SMBUS_BYTE_DATA
|
173 I2C_FUNC_SMBUS_WORD_DATA
| I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
174 I2C_FUNC_SMBUS_I2C_BLOCK
;
177 static const struct i2c_algorithm dln2_i2c_usb_algorithm
= {
178 .xfer
= dln2_i2c_xfer
,
179 .functionality
= dln2_i2c_func
,
182 static const struct i2c_adapter_quirks dln2_i2c_quirks
= {
183 .max_read_len
= DLN2_I2C_MAX_XFER_SIZE
,
184 .max_write_len
= DLN2_I2C_MAX_XFER_SIZE
,
187 static int dln2_i2c_probe(struct platform_device
*pdev
)
190 struct dln2_i2c
*dln2
;
191 struct device
*dev
= &pdev
->dev
;
192 struct dln2_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
194 dln2
= devm_kzalloc(dev
, sizeof(*dln2
), GFP_KERNEL
);
198 dln2
->buf
= devm_kmalloc(dev
, DLN2_I2C_BUF_SIZE
, GFP_KERNEL
);
203 dln2
->port
= pdata
->port
;
205 /* setup i2c adapter description */
206 dln2
->adapter
.owner
= THIS_MODULE
;
207 dln2
->adapter
.class = I2C_CLASS_HWMON
;
208 dln2
->adapter
.algo
= &dln2_i2c_usb_algorithm
;
209 dln2
->adapter
.quirks
= &dln2_i2c_quirks
;
210 dln2
->adapter
.dev
.parent
= dev
;
211 ACPI_COMPANION_SET(&dln2
->adapter
.dev
, ACPI_COMPANION(&pdev
->dev
));
212 dln2
->adapter
.dev
.of_node
= dev
->of_node
;
213 i2c_set_adapdata(&dln2
->adapter
, dln2
);
214 snprintf(dln2
->adapter
.name
, sizeof(dln2
->adapter
.name
), "%s-%s-%d",
215 "dln2-i2c", dev_name(pdev
->dev
.parent
), dln2
->port
);
217 platform_set_drvdata(pdev
, dln2
);
219 /* initialize the i2c interface */
220 ret
= dln2_i2c_enable(dln2
, true);
222 return dev_err_probe(dev
, ret
, "failed to initialize adapter\n");
224 /* and finally attach to i2c layer */
225 ret
= i2c_add_adapter(&dln2
->adapter
);
232 dln2_i2c_enable(dln2
, false);
237 static void dln2_i2c_remove(struct platform_device
*pdev
)
239 struct dln2_i2c
*dln2
= platform_get_drvdata(pdev
);
241 i2c_del_adapter(&dln2
->adapter
);
242 dln2_i2c_enable(dln2
, false);
245 static struct platform_driver dln2_i2c_driver
= {
246 .driver
.name
= "dln2-i2c",
247 .probe
= dln2_i2c_probe
,
248 .remove
= dln2_i2c_remove
,
251 module_platform_driver(dln2_i2c_driver
);
253 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
254 MODULE_DESCRIPTION("Driver for the Diolan DLN2 I2C controller interface");
255 MODULE_LICENSE("GPL v2");
256 MODULE_ALIAS("platform:dln2-i2c");