Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Diolan DLN-2 USB-I2C adapter |
| 3 | * |
| 4 | * Copyright (c) 2014 Intel Corporation |
| 5 | * |
| 6 | * Derived from: |
| 7 | * i2c-diolan-u2c.c |
| 8 | * Copyright (c) 2010-2011 Ericsson AB |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation, version 2. |
| 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 | |
| 23 | #define DLN2_I2C_MODULE_ID 0x03 |
| 24 | #define DLN2_I2C_CMD(cmd) DLN2_CMD(cmd, DLN2_I2C_MODULE_ID) |
| 25 | |
| 26 | /* I2C commands */ |
| 27 | #define DLN2_I2C_GET_PORT_COUNT DLN2_I2C_CMD(0x00) |
| 28 | #define DLN2_I2C_ENABLE DLN2_I2C_CMD(0x01) |
| 29 | #define DLN2_I2C_DISABLE DLN2_I2C_CMD(0x02) |
| 30 | #define DLN2_I2C_IS_ENABLED DLN2_I2C_CMD(0x03) |
| 31 | #define DLN2_I2C_WRITE DLN2_I2C_CMD(0x06) |
| 32 | #define DLN2_I2C_READ DLN2_I2C_CMD(0x07) |
| 33 | #define DLN2_I2C_SCAN_DEVICES DLN2_I2C_CMD(0x08) |
| 34 | #define DLN2_I2C_PULLUP_ENABLE DLN2_I2C_CMD(0x09) |
| 35 | #define DLN2_I2C_PULLUP_DISABLE DLN2_I2C_CMD(0x0A) |
| 36 | #define DLN2_I2C_PULLUP_IS_ENABLED DLN2_I2C_CMD(0x0B) |
| 37 | #define DLN2_I2C_TRANSFER DLN2_I2C_CMD(0x0C) |
| 38 | #define DLN2_I2C_SET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0D) |
| 39 | #define DLN2_I2C_GET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0E) |
| 40 | |
| 41 | #define DLN2_I2C_MAX_XFER_SIZE 256 |
| 42 | #define DLN2_I2C_BUF_SIZE (DLN2_I2C_MAX_XFER_SIZE + 16) |
| 43 | |
| 44 | struct dln2_i2c { |
| 45 | struct platform_device *pdev; |
| 46 | struct i2c_adapter adapter; |
| 47 | u8 port; |
| 48 | /* |
| 49 | * Buffer to hold the packet for read or write transfers. One is enough |
| 50 | * since we can't have multiple transfers in parallel on the i2c bus. |
| 51 | */ |
| 52 | void *buf; |
| 53 | }; |
| 54 | |
| 55 | static int dln2_i2c_enable(struct dln2_i2c *dln2, bool enable) |
| 56 | { |
Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 57 | u16 cmd; |
| 58 | struct { |
| 59 | u8 port; |
| 60 | } tx; |
| 61 | |
| 62 | tx.port = dln2->port; |
| 63 | |
| 64 | if (enable) |
| 65 | cmd = DLN2_I2C_ENABLE; |
| 66 | else |
| 67 | cmd = DLN2_I2C_DISABLE; |
| 68 | |
Octavian Purdila | 9331642 | 2014-11-18 14:57:58 +0200 | [diff] [blame] | 69 | return dln2_transfer_tx(dln2->pdev, cmd, &tx, sizeof(tx)); |
Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int dln2_i2c_write(struct dln2_i2c *dln2, u8 addr, |
| 73 | u8 *data, u16 data_len) |
| 74 | { |
| 75 | int ret; |
| 76 | struct { |
| 77 | u8 port; |
| 78 | u8 addr; |
| 79 | u8 mem_addr_len; |
| 80 | __le32 mem_addr; |
| 81 | __le16 buf_len; |
| 82 | u8 buf[DLN2_I2C_MAX_XFER_SIZE]; |
| 83 | } __packed *tx = dln2->buf; |
| 84 | unsigned len; |
| 85 | |
| 86 | BUILD_BUG_ON(sizeof(*tx) > DLN2_I2C_BUF_SIZE); |
| 87 | |
| 88 | tx->port = dln2->port; |
| 89 | tx->addr = addr; |
| 90 | tx->mem_addr_len = 0; |
| 91 | tx->mem_addr = 0; |
| 92 | tx->buf_len = cpu_to_le16(data_len); |
| 93 | memcpy(tx->buf, data, data_len); |
| 94 | |
| 95 | len = sizeof(*tx) + data_len - DLN2_I2C_MAX_XFER_SIZE; |
| 96 | ret = dln2_transfer_tx(dln2->pdev, DLN2_I2C_WRITE, tx, len); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
| 100 | return data_len; |
| 101 | } |
| 102 | |
| 103 | static int dln2_i2c_read(struct dln2_i2c *dln2, u16 addr, u8 *data, |
| 104 | u16 data_len) |
| 105 | { |
| 106 | int ret; |
| 107 | struct { |
| 108 | u8 port; |
| 109 | u8 addr; |
| 110 | u8 mem_addr_len; |
| 111 | __le32 mem_addr; |
| 112 | __le16 buf_len; |
| 113 | } __packed tx; |
| 114 | struct { |
| 115 | __le16 buf_len; |
| 116 | u8 buf[DLN2_I2C_MAX_XFER_SIZE]; |
| 117 | } __packed *rx = dln2->buf; |
| 118 | unsigned rx_len = sizeof(*rx); |
| 119 | |
| 120 | BUILD_BUG_ON(sizeof(*rx) > DLN2_I2C_BUF_SIZE); |
| 121 | |
| 122 | tx.port = dln2->port; |
| 123 | tx.addr = addr; |
| 124 | tx.mem_addr_len = 0; |
| 125 | tx.mem_addr = 0; |
| 126 | tx.buf_len = cpu_to_le16(data_len); |
| 127 | |
| 128 | ret = dln2_transfer(dln2->pdev, DLN2_I2C_READ, &tx, sizeof(tx), |
| 129 | rx, &rx_len); |
| 130 | if (ret < 0) |
| 131 | return ret; |
| 132 | if (rx_len < sizeof(rx->buf_len) + data_len) |
| 133 | return -EPROTO; |
| 134 | if (le16_to_cpu(rx->buf_len) != data_len) |
| 135 | return -EPROTO; |
| 136 | |
| 137 | memcpy(data, rx->buf, data_len); |
| 138 | |
| 139 | return data_len; |
| 140 | } |
| 141 | |
| 142 | static int dln2_i2c_xfer(struct i2c_adapter *adapter, |
| 143 | struct i2c_msg *msgs, int num) |
| 144 | { |
| 145 | struct dln2_i2c *dln2 = i2c_get_adapdata(adapter); |
| 146 | struct i2c_msg *pmsg; |
Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 147 | int i; |
| 148 | |
| 149 | for (i = 0; i < num; i++) { |
| 150 | int ret; |
| 151 | |
| 152 | pmsg = &msgs[i]; |
| 153 | |
Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 154 | if (pmsg->flags & I2C_M_RD) { |
| 155 | ret = dln2_i2c_read(dln2, pmsg->addr, pmsg->buf, |
| 156 | pmsg->len); |
| 157 | if (ret < 0) |
| 158 | return ret; |
| 159 | |
| 160 | pmsg->len = ret; |
| 161 | } else { |
| 162 | ret = dln2_i2c_write(dln2, pmsg->addr, pmsg->buf, |
| 163 | pmsg->len); |
| 164 | if (ret != pmsg->len) |
| 165 | return -EPROTO; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return num; |
| 170 | } |
| 171 | |
| 172 | static u32 dln2_i2c_func(struct i2c_adapter *a) |
| 173 | { |
| 174 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA | |
| 175 | I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL | |
| 176 | I2C_FUNC_SMBUS_I2C_BLOCK; |
| 177 | } |
| 178 | |
| 179 | static const struct i2c_algorithm dln2_i2c_usb_algorithm = { |
| 180 | .master_xfer = dln2_i2c_xfer, |
| 181 | .functionality = dln2_i2c_func, |
| 182 | }; |
| 183 | |
Wolfram Sang | afe9020 | 2015-01-07 12:24:10 +0100 | [diff] [blame] | 184 | static struct i2c_adapter_quirks dln2_i2c_quirks = { |
| 185 | .max_read_len = DLN2_I2C_MAX_XFER_SIZE, |
| 186 | .max_write_len = DLN2_I2C_MAX_XFER_SIZE, |
| 187 | }; |
| 188 | |
Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 189 | static int dln2_i2c_probe(struct platform_device *pdev) |
| 190 | { |
| 191 | int ret; |
| 192 | struct dln2_i2c *dln2; |
| 193 | struct device *dev = &pdev->dev; |
| 194 | struct dln2_platform_data *pdata = dev_get_platdata(&pdev->dev); |
| 195 | |
| 196 | dln2 = devm_kzalloc(dev, sizeof(*dln2), GFP_KERNEL); |
| 197 | if (!dln2) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | dln2->buf = devm_kmalloc(dev, DLN2_I2C_BUF_SIZE, GFP_KERNEL); |
| 201 | if (!dln2->buf) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | dln2->pdev = pdev; |
| 205 | dln2->port = pdata->port; |
| 206 | |
| 207 | /* setup i2c adapter description */ |
| 208 | dln2->adapter.owner = THIS_MODULE; |
| 209 | dln2->adapter.class = I2C_CLASS_HWMON; |
| 210 | dln2->adapter.algo = &dln2_i2c_usb_algorithm; |
Wolfram Sang | afe9020 | 2015-01-07 12:24:10 +0100 | [diff] [blame] | 211 | dln2->adapter.quirks = &dln2_i2c_quirks; |
Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 212 | dln2->adapter.dev.parent = dev; |
Octavian Purdila | 3b10db2 | 2015-03-27 17:37:10 +0200 | [diff] [blame] | 213 | dln2->adapter.dev.of_node = dev->of_node; |
Laurentiu Palcu | db23e50 | 2014-11-06 15:48:04 +0200 | [diff] [blame] | 214 | i2c_set_adapdata(&dln2->adapter, dln2); |
| 215 | snprintf(dln2->adapter.name, sizeof(dln2->adapter.name), "%s-%s-%d", |
| 216 | "dln2-i2c", dev_name(pdev->dev.parent), dln2->port); |
| 217 | |
| 218 | platform_set_drvdata(pdev, dln2); |
| 219 | |
| 220 | /* initialize the i2c interface */ |
| 221 | ret = dln2_i2c_enable(dln2, true); |
| 222 | if (ret < 0) { |
| 223 | dev_err(dev, "failed to initialize adapter: %d\n", ret); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | /* and finally attach to i2c layer */ |
| 228 | ret = i2c_add_adapter(&dln2->adapter); |
| 229 | if (ret < 0) { |
| 230 | dev_err(dev, "failed to add I2C adapter: %d\n", ret); |
| 231 | goto out_disable; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | |
| 236 | out_disable: |
| 237 | dln2_i2c_enable(dln2, false); |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static 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 | |
| 252 | static struct platform_driver dln2_i2c_driver = { |
| 253 | .driver.name = "dln2-i2c", |
| 254 | .probe = dln2_i2c_probe, |
| 255 | .remove = dln2_i2c_remove, |
| 256 | }; |
| 257 | |
| 258 | module_platform_driver(dln2_i2c_driver); |
| 259 | |
| 260 | MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>"); |
| 261 | MODULE_DESCRIPTION("Driver for the Diolan DLN2 I2C master interface"); |
| 262 | MODULE_LICENSE("GPL v2"); |
| 263 | MODULE_ALIAS("platform:dln2-i2c"); |