Lines Matching +full:i2c +full:- +full:retry +full:- +full:count
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
5 * IDT PCIe-switch NTB Linux driver
8 * Serge Semin <fancer.lancer@gmail.com>, <Sergey.Semin@t-platforms.ru>
11 * NOTE of the IDT 89HPESx SMBus-slave interface driver
13 * IDT PCIe-switches. IDT provides a simple SMBus interface to perform IO-
16 * binary sysfs-file in the device directory:
17 * /sys/bus/i2c/devices/<bus>-<devaddr>/eeprom
18 * In case if read-only flag is specified in the dts-node of device desription,
19 * User-space applications won't be able to write to the EEPROM sysfs-node.
21 * data of device CSRs. This driver exposes debugf-file to perform simple IO
23 * next file is created in the specific debugfs-directory:
25 * Format of the debugfs-node is:
26 * $ cat /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>;
29 * If User-space application wishes to change current CSR address,
30 * it can just write a proper value to the sysfs-file:
31 * $ echo "<CSR address>" > /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>
35 * /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>;
50 #include <linux/i2c.h>
55 #define IDT_89HPESX_DESC "IDT 89HPESx SMBus-slave interface driver"
61 MODULE_AUTHOR("T-platforms");
64 * csr_dbgdir - CSR read/write operations Debugfs directory
69 * struct idt_89hpesx_dev - IDT 89HPESx device data structure
71 * @eero: EEPROM Read-only flag
76 * @iniccode: Initialial command code value for IO-operations
84 * @client: i2c client used to perform IO operations
86 * @ee_file: EEPROM read/write sysfs-file
111 * struct idt_smb_seq - sequence of data to be read/written from/to IDT 89HPESx
113 * @bytecnt: Byte count of operation
123 * struct idt_eeprom_seq - sequence of data to be read/written from/to EEPROM
137 * struct idt_csr_seq - sequence of data to be read/written from/to CSR
176 * @EEPROM_WR_CNT: Bytes count to perform write operation
177 * @EEPROM_WRRD_CNT: Bytes count to write before reading
178 * @EEPROM_RD_CNT: Bytes count to perform read operation
181 * @EEPROM_TOUT: Timeout before retry read operation if eeprom is busy
203 * @CSR_WR_CNT: Bytes count to perform write operation
204 * @CSR_WRRD_CNT: Bytes count to write before reading
205 * @CSR_RD_CNT: Bytes count to perform read operation
235 * @idt_smb_safe: Generate a retry loop on corresponding SMBus method
243 } while (__retry-- && __sts < 0); \
248 * i2c bus level IO-operations
253 * idt_smb_write_byte() - SMBus write method when I2C_SMBUS_BYTE_DATA operation
265 /* Loop over the supplied data sending byte one-by-one */
266 for (idx = 0; idx < seq->bytecnt; idx++) {
268 ccode = seq->ccode | CCODE_BYTE;
271 if (idx == seq->bytecnt - 1)
275 sts = idt_smb_safe(write_byte, pdev->client, ccode,
276 seq->data[idx]);
285 * idt_smb_read_byte() - SMBus read method when I2C_SMBUS_BYTE_DATA operation
297 /* Loop over the supplied buffer receiving byte one-by-one */
298 for (idx = 0; idx < seq->bytecnt; idx++) {
300 ccode = seq->ccode | CCODE_BYTE;
303 if (idx == seq->bytecnt - 1)
307 sts = idt_smb_safe(read_byte, pdev->client, ccode);
311 seq->data[idx] = (u8)sts;
318 * idt_smb_write_word() - SMBus write method when I2C_SMBUS_BYTE_DATA and
330 /* Calculate the even count of data to send */
331 evencnt = seq->bytecnt - (seq->bytecnt % 2);
336 ccode = seq->ccode | CCODE_WORD;
339 if (idx == evencnt - 2)
343 sts = idt_smb_safe(write_word, pdev->client, ccode,
344 *(u16 *)&seq->data[idx]);
350 if (seq->bytecnt != evencnt) {
352 ccode = seq->ccode | CCODE_BYTE | CCODE_END;
357 sts = idt_smb_safe(write_byte, pdev->client, ccode,
358 seq->data[idx]);
367 * idt_smb_read_word() - SMBus read method when I2C_SMBUS_BYTE_DATA and
379 /* Calculate the even count of data to send */
380 evencnt = seq->bytecnt - (seq->bytecnt % 2);
385 ccode = seq->ccode | CCODE_WORD;
388 if (idx == evencnt - 2)
392 sts = idt_smb_safe(read_word, pdev->client, ccode);
396 *(u16 *)&seq->data[idx] = (u16)sts;
400 if (seq->bytecnt != evencnt) {
402 ccode = seq->ccode | CCODE_BYTE | CCODE_END;
407 sts = idt_smb_safe(read_byte, pdev->client, ccode);
411 seq->data[idx] = (u8)sts;
418 * idt_smb_write_block() - SMBus write method when I2C_SMBUS_BLOCK_DATA
429 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
430 return -EINVAL;
433 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
436 return idt_smb_safe(write_block, pdev->client, ccode, seq->bytecnt,
437 seq->data);
441 * idt_smb_read_block() - SMBus read method when I2C_SMBUS_BLOCK_DATA
453 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
454 return -EINVAL;
457 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
460 sts = idt_smb_safe(read_block, pdev->client, ccode, seq->data);
461 if (sts != seq->bytecnt)
462 return (sts < 0 ? sts : -ENODATA);
468 * idt_smb_write_i2c_block() - SMBus write method when I2C_SMBUS_I2C_BLOCK_DATA
482 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
483 return -EINVAL;
486 buf[0] = seq->bytecnt;
487 memcpy(&buf[1], seq->data, seq->bytecnt);
490 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
493 return idt_smb_safe(write_i2c_block, pdev->client, ccode,
494 seq->bytecnt + 1, buf);
498 * idt_smb_read_i2c_block() - SMBus read method when I2C_SMBUS_I2C_BLOCK_DATA
513 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
514 return -EINVAL;
517 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
520 sts = idt_smb_safe(read_i2c_block, pdev->client, ccode,
521 seq->bytecnt + 1, buf);
522 if (sts != seq->bytecnt + 1)
523 return (sts < 0 ? sts : -ENODATA);
524 if (buf[0] != seq->bytecnt)
525 return -ENODATA;
528 memcpy(seq->data, &buf[1], seq->bytecnt);
534 * EEPROM IO-operations
539 * idt_eeprom_read_byte() - read just one byte from EEPROM
547 struct device *dev = &pdev->client->dev;
550 int ret, retry;
553 smbseq.ccode = pdev->iniccode | CCODE_EEPROM;
560 retry = RETRY_CNT;
564 eeseq.cmd = pdev->inieecmd | EEPROM_OP_READ;
565 eeseq.eeaddr = pdev->eeaddr;
567 ret = pdev->smb_write(pdev, &smbseq);
576 ret = pdev->smb_read(pdev, &smbseq);
584 if (retry && (eeseq.cmd & EEPROM_NAERR)) {
585 dev_dbg(dev, "EEPROM busy, retry reading after %d ms",
596 ret = -EREMOTEIO;
603 } while (retry--);
610 * idt_eeprom_write() - EEPROM write operation
619 struct device *dev = &pdev->client->dev;
626 smbseq.ccode = pdev->iniccode | CCODE_EEPROM;
629 /* Send data byte-by-byte, checking if it is successfully written */
632 mutex_lock(&pdev->smb_mtx);
636 eeseq.cmd = pdev->inieecmd | EEPROM_OP_WRITE;
637 eeseq.eeaddr = pdev->eeaddr;
640 ret = pdev->smb_write(pdev, &smbseq);
661 ret = -EREMOTEIO;
667 mutex_unlock(&pdev->smb_mtx);
676 * idt_eeprom_read() - EEPROM read operation
688 /* Read data byte-by-byte, retrying if it wasn't successful */
691 mutex_lock(&pdev->smb_mtx);
697 mutex_unlock(&pdev->smb_mtx);
708 * CSR IO-operations
713 * idt_csr_write() - CSR write operation
721 struct device *dev = &pdev->client->dev;
727 smbseq.ccode = pdev->iniccode | CCODE_CSR;
731 mutex_lock(&pdev->smb_mtx);
735 csrseq.cmd = pdev->inicsrcmd | CSR_OP_WRITE;
738 ret = pdev->smb_write(pdev, &smbseq);
747 csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ;
748 ret = pdev->smb_write(pdev, &smbseq);
757 ret = pdev->smb_read(pdev, &smbseq);
767 ret = -EREMOTEIO;
773 mutex_unlock(&pdev->smb_mtx);
779 * idt_csr_read() - CSR read operation
786 struct device *dev = &pdev->client->dev;
792 smbseq.ccode = pdev->iniccode | CCODE_CSR;
796 mutex_lock(&pdev->smb_mtx);
800 csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ;
802 ret = pdev->smb_write(pdev, &smbseq);
811 ret = pdev->smb_read(pdev, &smbseq);
821 ret = -EREMOTEIO;
830 mutex_unlock(&pdev->smb_mtx);
836 * Sysfs/debugfs-nodes IO-operations
841 * eeprom_write() - EEPROM sysfs-node write callback
843 * @kobj: Pointer to the kernel object related to the sysfs-node
847 * @count: Number of bytes to write
851 char *buf, loff_t off, size_t count)
860 ret = idt_eeprom_write(pdev, (u16)off, (u16)count, (u8 *)buf);
861 return (ret != 0 ? ret : count);
865 * eeprom_read() - EEPROM sysfs-node read callback
867 * @kobj: Pointer to the kernel object related to the sysfs-node
871 * @count: Number of bytes to write
875 char *buf, loff_t off, size_t count)
884 ret = idt_eeprom_read(pdev, (u16)off, (u16)count, (u8 *)buf);
885 return (ret != 0 ? ret : count);
889 * idt_dbgfs_csr_write() - CSR debugfs-node write callback
892 * @count: Size of the buffer
904 size_t count, loff_t *offp)
906 struct idt_89hpesx_dev *pdev = filep->private_data;
915 /* Copy data from User-space */
916 buf = memdup_user_nul(ubuf, count);
921 colon_ch = strnchr(buf, count, ':');
931 csraddr_str = kmemdup_nul(buf, colon_ch - buf, GFP_KERNEL);
933 ret = -ENOMEM;
950 ret = -EINVAL;
955 pdev->csr = (csraddr >> 2);
963 ret = idt_csr_write(pdev, pdev->csr, csrval);
973 /* Free buffer allocated for data retrieved from User-space */
977 return (ret != 0 ? ret : count);
981 * idt_dbgfs_csr_read() - CSR debugfs-node read callback
984 * @count: Size of the buffer
991 size_t count, loff_t *offp)
993 struct idt_89hpesx_dev *pdev = filep->private_data;
999 ret = idt_csr_read(pdev, pdev->csr, &csrval);
1004 csraddr = ((u32)pdev->csr << 2);
1010 /* Copy data to User-space */
1011 return simple_read_from_buffer(ubuf, count, offp, buf, size);
1015 * eeprom_attribute - EEPROM sysfs-node attributes
1018 * be read-only as well if the corresponding flag is specified in OF node.
1023 * csr_dbgfs_ops - CSR debugfs-node read/write operations
1038 * idt_set_defval() - disable EEPROM access by default
1044 pdev->eesize = 0;
1045 pdev->eero = true;
1046 pdev->inieecmd = 0;
1047 pdev->eeaddr = 0;
1053 * idt_ee_match_id() - check whether the node belongs to compatible EEPROMs
1069 while (id->name[0]) {
1070 if (strcmp(devname, id->name) == 0)
1078 * idt_get_fw_data() - get IDT i2c-device parameters from device tree
1083 struct device *dev = &pdev->client->dev;
1105 pdev->eesize = (u32)ee_id->driver_data;
1112 pdev->inieecmd = 0;
1113 pdev->eeaddr = EEPROM_DEF_ADDR << 1;
1115 pdev->inieecmd = EEPROM_USA;
1116 pdev->eeaddr = eeprom_addr << 1;
1119 /* Check EEPROM 'read-only' flag */
1120 if (fwnode_property_read_bool(fwnode, "read-only"))
1121 pdev->eero = true;
1122 else /* if (!fwnode_property_read_bool(node, "read-only")) */
1123 pdev->eero = false;
1127 pdev->eesize, pdev->eeaddr);
1131 * idt_create_pdev() - create and init data structure of the driver
1132 * @client: i2c client of IDT PCIe-switch device
1139 pdev = devm_kmalloc(&client->dev, sizeof(struct idt_89hpesx_dev),
1142 return ERR_PTR(-ENOMEM);
1145 pdev->client = client;
1151 /* Initialize basic CSR CMD field - use full DWORD-sized r/w ops */
1152 pdev->inicsrcmd = CSR_DWE;
1153 pdev->csr = CSR_DEF;
1156 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) {
1157 pdev->iniccode = CCODE_PEC;
1158 client->flags |= I2C_CLIENT_PEC;
1160 pdev->iniccode = 0;
1167 * idt_free_pdev() - free data structure of the driver
1173 i2c_set_clientdata(pdev->client, NULL);
1177 * idt_set_smbus_ops() - set supported SMBus operations
1183 struct i2c_adapter *adapter = pdev->client->adapter;
1184 struct device *dev = &pdev->client->dev;
1186 /* Check i2c adapter read functionality */
1189 pdev->smb_read = idt_smb_read_block;
1190 dev_dbg(dev, "SMBus block-read op chosen");
1193 pdev->smb_read = idt_smb_read_i2c_block;
1194 dev_dbg(dev, "SMBus i2c-block-read op chosen");
1199 pdev->smb_read = idt_smb_read_word;
1203 pdev->smb_read = idt_smb_read_byte;
1207 return -EPFNOSUPPORT;
1210 /* Check i2c adapter write functionality */
1213 pdev->smb_write = idt_smb_write_block;
1214 dev_dbg(dev, "SMBus block-write op chosen");
1217 pdev->smb_write = idt_smb_write_i2c_block;
1218 dev_dbg(dev, "SMBus i2c-block-write op chosen");
1223 pdev->smb_write = idt_smb_write_word;
1227 pdev->smb_write = idt_smb_write_byte;
1231 return -EPFNOSUPPORT;
1235 mutex_init(&pdev->smb_mtx);
1241 * idt_check_dev() - check whether it's really IDT 89HPESx device
1243 * Return status of i2c adapter check operation
1247 struct device *dev = &pdev->client->dev;
1261 return -ENODEV;
1271 * idt_create_sysfs_files() - create sysfs attribute files
1277 struct device *dev = &pdev->client->dev;
1281 if (pdev->eesize == 0) {
1282 dev_dbg(dev, "Skip creating sysfs-files");
1290 pdev->ee_file = devm_kmemdup(dev, &bin_attr_eeprom,
1291 sizeof(*pdev->ee_file), GFP_KERNEL);
1292 if (!pdev->ee_file)
1293 return -ENOMEM;
1295 /* In case of read-only EEPROM get rid of write ability */
1296 if (pdev->eero) {
1297 pdev->ee_file->attr.mode &= ~0200;
1298 pdev->ee_file->write = NULL;
1301 pdev->ee_file->size = pdev->eesize;
1302 ret = sysfs_create_bin_file(&dev->kobj, pdev->ee_file);
1304 dev_err(dev, "Failed to create EEPROM sysfs-node");
1312 * idt_remove_sysfs_files() - remove sysfs attribute files
1317 struct device *dev = &pdev->client->dev;
1320 if (pdev->eesize == 0)
1324 sysfs_remove_bin_file(&dev->kobj, pdev->ee_file);
1328 * idt_create_dbgfs_files() - create debugfs files
1334 struct i2c_client *cli = pdev->client;
1338 snprintf(fname, CSRNAME_LEN, "%d-%04hx", cli->adapter->nr, cli->addr);
1339 pdev->csr_dir = debugfs_create_dir(fname, csr_dbgdir);
1342 debugfs_create_file(cli->name, 0600, pdev->csr_dir, pdev,
1347 * idt_remove_dbgfs_files() - remove debugfs files
1352 /* Remove CSR directory and it sysfs-node */
1353 debugfs_remove_recursive(pdev->csr_dir);
1357 * idt_probe() - IDT 89HPESx driver probe() callback method
1396 * idt_remove() - IDT 89HPESx driver remove() callback method
1413 * ee_ids - array of supported EEPROMs
1423 MODULE_DEVICE_TABLE(i2c, ee_ids);
1426 * idt_ids - supported IDT 89HPESx devices
1460 /* { "89hpes3t3" }, // No SMBus-slave iface */
1463 /* { "89hpes4t4" }, // No SMBus-slave iface */
1483 MODULE_DEVICE_TABLE(i2c, idt_ids);
1542 * idt_driver - IDT 89HPESx driver structure
1555 * idt_init() - IDT 89HPESx driver init() callback method
1565 /* Add new i2c-device driver */
1577 * idt_exit() - IDT 89HPESx driver exit() callback method
1584 /* Unregister i2c-device driver */