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 */ in idt_smb_write_byte()
266 for (idx = 0; idx < seq->bytecnt; idx++) { in idt_smb_write_byte()
268 ccode = seq->ccode | CCODE_BYTE; in idt_smb_write_byte()
271 if (idx == seq->bytecnt - 1) in idt_smb_write_byte()
275 sts = idt_smb_safe(write_byte, pdev->client, ccode, in idt_smb_write_byte()
276 seq->data[idx]); in idt_smb_write_byte()
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 */ in idt_smb_read_byte()
298 for (idx = 0; idx < seq->bytecnt; idx++) { in idt_smb_read_byte()
300 ccode = seq->ccode | CCODE_BYTE; in idt_smb_read_byte()
303 if (idx == seq->bytecnt - 1) in idt_smb_read_byte()
307 sts = idt_smb_safe(read_byte, pdev->client, ccode); in idt_smb_read_byte()
311 seq->data[idx] = (u8)sts; in idt_smb_read_byte()
318 * idt_smb_write_word() - SMBus write method when I2C_SMBUS_BYTE_DATA and
330 /* Calculate the even count of data to send */ in idt_smb_write_word()
331 evencnt = seq->bytecnt - (seq->bytecnt % 2); in idt_smb_write_word()
336 ccode = seq->ccode | CCODE_WORD; in idt_smb_write_word()
339 if (idx == evencnt - 2) in idt_smb_write_word()
343 sts = idt_smb_safe(write_word, pdev->client, ccode, in idt_smb_write_word()
344 *(u16 *)&seq->data[idx]); in idt_smb_write_word()
350 if (seq->bytecnt != evencnt) { in idt_smb_write_word()
352 ccode = seq->ccode | CCODE_BYTE | CCODE_END; in idt_smb_write_word()
357 sts = idt_smb_safe(write_byte, pdev->client, ccode, in idt_smb_write_word()
358 seq->data[idx]); in idt_smb_write_word()
367 * idt_smb_read_word() - SMBus read method when I2C_SMBUS_BYTE_DATA and
379 /* Calculate the even count of data to send */ in idt_smb_read_word()
380 evencnt = seq->bytecnt - (seq->bytecnt % 2); in idt_smb_read_word()
385 ccode = seq->ccode | CCODE_WORD; in idt_smb_read_word()
388 if (idx == evencnt - 2) in idt_smb_read_word()
392 sts = idt_smb_safe(read_word, pdev->client, ccode); in idt_smb_read_word()
396 *(u16 *)&seq->data[idx] = (u16)sts; in idt_smb_read_word()
400 if (seq->bytecnt != evencnt) { in idt_smb_read_word()
402 ccode = seq->ccode | CCODE_BYTE | CCODE_END; in idt_smb_read_word()
407 sts = idt_smb_safe(read_byte, pdev->client, ccode); in idt_smb_read_word()
411 seq->data[idx] = (u8)sts; in idt_smb_read_word()
418 * idt_smb_write_block() - SMBus write method when I2C_SMBUS_BLOCK_DATA
429 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX) in idt_smb_write_block()
430 return -EINVAL; in idt_smb_write_block()
433 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END; in idt_smb_write_block()
436 return idt_smb_safe(write_block, pdev->client, ccode, seq->bytecnt, in idt_smb_write_block()
437 seq->data); in idt_smb_write_block()
441 * idt_smb_read_block() - SMBus read method when I2C_SMBUS_BLOCK_DATA
453 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX) in idt_smb_read_block()
454 return -EINVAL; in idt_smb_read_block()
457 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END; in idt_smb_read_block()
460 sts = idt_smb_safe(read_block, pdev->client, ccode, seq->data); in idt_smb_read_block()
461 if (sts != seq->bytecnt) in idt_smb_read_block()
462 return (sts < 0 ? sts : -ENODATA); in idt_smb_read_block()
468 * idt_smb_write_i2c_block() - SMBus write method when I2C_SMBUS_I2C_BLOCK_DATA
482 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX) in idt_smb_write_i2c_block()
483 return -EINVAL; in idt_smb_write_i2c_block()
486 buf[0] = seq->bytecnt; in idt_smb_write_i2c_block()
487 memcpy(&buf[1], seq->data, seq->bytecnt); in idt_smb_write_i2c_block()
490 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END; in idt_smb_write_i2c_block()
493 return idt_smb_safe(write_i2c_block, pdev->client, ccode, in idt_smb_write_i2c_block()
494 seq->bytecnt + 1, buf); in idt_smb_write_i2c_block()
498 * idt_smb_read_i2c_block() - SMBus read method when I2C_SMBUS_I2C_BLOCK_DATA
513 if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX) in idt_smb_read_i2c_block()
514 return -EINVAL; in idt_smb_read_i2c_block()
517 ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END; in idt_smb_read_i2c_block()
520 sts = idt_smb_safe(read_i2c_block, pdev->client, ccode, in idt_smb_read_i2c_block()
521 seq->bytecnt + 1, buf); in idt_smb_read_i2c_block()
522 if (sts != seq->bytecnt + 1) in idt_smb_read_i2c_block()
523 return (sts < 0 ? sts : -ENODATA); in idt_smb_read_i2c_block()
524 if (buf[0] != seq->bytecnt) in idt_smb_read_i2c_block()
525 return -ENODATA; in idt_smb_read_i2c_block()
528 memcpy(seq->data, &buf[1], seq->bytecnt); in idt_smb_read_i2c_block()
534 * EEPROM IO-operations
539 * idt_eeprom_read_byte() - read just one byte from EEPROM
547 struct device *dev = &pdev->client->dev; in idt_eeprom_read_byte()
550 int ret, retry; in idt_eeprom_read_byte() local
553 smbseq.ccode = pdev->iniccode | CCODE_EEPROM; in idt_eeprom_read_byte()
560 retry = RETRY_CNT; in idt_eeprom_read_byte()
564 eeseq.cmd = pdev->inieecmd | EEPROM_OP_READ; in idt_eeprom_read_byte()
565 eeseq.eeaddr = pdev->eeaddr; in idt_eeprom_read_byte()
567 ret = pdev->smb_write(pdev, &smbseq); in idt_eeprom_read_byte()
576 ret = pdev->smb_read(pdev, &smbseq); in idt_eeprom_read_byte()
584 if (retry && (eeseq.cmd & EEPROM_NAERR)) { in idt_eeprom_read_byte()
585 dev_dbg(dev, "EEPROM busy, retry reading after %d ms", in idt_eeprom_read_byte()
596 ret = -EREMOTEIO; in idt_eeprom_read_byte()
603 } while (retry--); in idt_eeprom_read_byte()
610 * idt_eeprom_write() - EEPROM write operation
619 struct device *dev = &pdev->client->dev; in idt_eeprom_write()
626 smbseq.ccode = pdev->iniccode | CCODE_EEPROM; in idt_eeprom_write()
629 /* Send data byte-by-byte, checking if it is successfully written */ in idt_eeprom_write()
632 mutex_lock(&pdev->smb_mtx); in idt_eeprom_write()
636 eeseq.cmd = pdev->inieecmd | EEPROM_OP_WRITE; in idt_eeprom_write()
637 eeseq.eeaddr = pdev->eeaddr; in idt_eeprom_write()
640 ret = pdev->smb_write(pdev, &smbseq); in idt_eeprom_write()
661 ret = -EREMOTEIO; in idt_eeprom_write()
667 mutex_unlock(&pdev->smb_mtx); in idt_eeprom_write()
676 * idt_eeprom_read() - EEPROM read operation
688 /* Read data byte-by-byte, retrying if it wasn't successful */ in idt_eeprom_read()
691 mutex_lock(&pdev->smb_mtx); in idt_eeprom_read()
697 mutex_unlock(&pdev->smb_mtx); in idt_eeprom_read()
708 * CSR IO-operations
713 * idt_csr_write() - CSR write operation
721 struct device *dev = &pdev->client->dev; in idt_csr_write()
727 smbseq.ccode = pdev->iniccode | CCODE_CSR; in idt_csr_write()
731 mutex_lock(&pdev->smb_mtx); in idt_csr_write()
735 csrseq.cmd = pdev->inicsrcmd | CSR_OP_WRITE; in idt_csr_write()
738 ret = pdev->smb_write(pdev, &smbseq); in idt_csr_write()
747 csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ; in idt_csr_write()
748 ret = pdev->smb_write(pdev, &smbseq); in idt_csr_write()
757 ret = pdev->smb_read(pdev, &smbseq); in idt_csr_write()
767 ret = -EREMOTEIO; in idt_csr_write()
773 mutex_unlock(&pdev->smb_mtx); in idt_csr_write()
779 * idt_csr_read() - CSR read operation
786 struct device *dev = &pdev->client->dev; in idt_csr_read()
792 smbseq.ccode = pdev->iniccode | CCODE_CSR; in idt_csr_read()
796 mutex_lock(&pdev->smb_mtx); in idt_csr_read()
800 csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ; in idt_csr_read()
802 ret = pdev->smb_write(pdev, &smbseq); in idt_csr_read()
811 ret = pdev->smb_read(pdev, &smbseq); in idt_csr_read()
821 ret = -EREMOTEIO; in idt_csr_read()
830 mutex_unlock(&pdev->smb_mtx); in idt_csr_read()
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) in eeprom_write() argument
860 ret = idt_eeprom_write(pdev, (u16)off, (u16)count, (u8 *)buf); in eeprom_write()
861 return (ret != 0 ? ret : count); in eeprom_write()
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) in eeprom_read() argument
884 ret = idt_eeprom_read(pdev, (u16)off, (u16)count, (u8 *)buf); in eeprom_read()
885 return (ret != 0 ? ret : count); in eeprom_read()
889 * idt_dbgfs_csr_write() - CSR debugfs-node write callback
892 * @count: Size of the buffer
904 size_t count, loff_t *offp) in idt_dbgfs_csr_write() argument
906 struct idt_89hpesx_dev *pdev = filep->private_data; in idt_dbgfs_csr_write()
915 /* Copy data from User-space */ in idt_dbgfs_csr_write()
916 buf = memdup_user_nul(ubuf, count); in idt_dbgfs_csr_write()
921 colon_ch = strnchr(buf, count, ':'); in idt_dbgfs_csr_write()
931 csraddr_str = kmemdup_nul(buf, colon_ch - buf, GFP_KERNEL); in idt_dbgfs_csr_write()
933 ret = -ENOMEM; in idt_dbgfs_csr_write()
950 ret = -EINVAL; in idt_dbgfs_csr_write()
955 pdev->csr = (csraddr >> 2); in idt_dbgfs_csr_write()
963 ret = idt_csr_write(pdev, pdev->csr, csrval); in idt_dbgfs_csr_write()
973 /* Free buffer allocated for data retrieved from User-space */ in idt_dbgfs_csr_write()
977 return (ret != 0 ? ret : count); in idt_dbgfs_csr_write()
981 * idt_dbgfs_csr_read() - CSR debugfs-node read callback
984 * @count: Size of the buffer
991 size_t count, loff_t *offp) in idt_dbgfs_csr_read() argument
993 struct idt_89hpesx_dev *pdev = filep->private_data; in idt_dbgfs_csr_read()
999 ret = idt_csr_read(pdev, pdev->csr, &csrval); in idt_dbgfs_csr_read()
1004 csraddr = ((u32)pdev->csr << 2); in idt_dbgfs_csr_read()
1010 /* Copy data to User-space */ in idt_dbgfs_csr_read()
1011 return simple_read_from_buffer(ubuf, count, offp, buf, size); in idt_dbgfs_csr_read()
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; in idt_set_defval()
1045 pdev->eero = true; in idt_set_defval()
1046 pdev->inieecmd = 0; in idt_set_defval()
1047 pdev->eeaddr = 0; in idt_set_defval()
1053 * idt_ee_match_id() - check whether the node belongs to compatible EEPROMs
1069 while (id->name[0]) { in idt_ee_match_id()
1070 if (strcmp(devname, id->name) == 0) in idt_ee_match_id()
1078 * idt_get_fw_data() - get IDT i2c-device parameters from device tree
1083 struct device *dev = &pdev->client->dev; in idt_get_fw_data()
1105 pdev->eesize = (u32)ee_id->driver_data; in idt_get_fw_data()
1112 pdev->inieecmd = 0; in idt_get_fw_data()
1113 pdev->eeaddr = EEPROM_DEF_ADDR << 1; in idt_get_fw_data()
1115 pdev->inieecmd = EEPROM_USA; in idt_get_fw_data()
1116 pdev->eeaddr = eeprom_addr << 1; in idt_get_fw_data()
1119 /* Check EEPROM 'read-only' flag */ in idt_get_fw_data()
1120 if (fwnode_property_read_bool(fwnode, "read-only")) in idt_get_fw_data()
1121 pdev->eero = true; in idt_get_fw_data()
1122 else /* if (!fwnode_property_read_bool(node, "read-only")) */ in idt_get_fw_data()
1123 pdev->eero = false; in idt_get_fw_data()
1127 pdev->eesize, pdev->eeaddr); in idt_get_fw_data()
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), in idt_create_pdev()
1142 return ERR_PTR(-ENOMEM); in idt_create_pdev()
1145 pdev->client = client; in idt_create_pdev()
1151 /* Initialize basic CSR CMD field - use full DWORD-sized r/w ops */ in idt_create_pdev()
1152 pdev->inicsrcmd = CSR_DWE; in idt_create_pdev()
1153 pdev->csr = CSR_DEF; in idt_create_pdev()
1156 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) { in idt_create_pdev()
1157 pdev->iniccode = CCODE_PEC; in idt_create_pdev()
1158 client->flags |= I2C_CLIENT_PEC; in idt_create_pdev()
1160 pdev->iniccode = 0; in idt_create_pdev()
1167 * idt_free_pdev() - free data structure of the driver
1173 i2c_set_clientdata(pdev->client, NULL); in idt_free_pdev()
1177 * idt_set_smbus_ops() - set supported SMBus operations
1183 struct i2c_adapter *adapter = pdev->client->adapter; in idt_set_smbus_ops()
1184 struct device *dev = &pdev->client->dev; in idt_set_smbus_ops()
1186 /* Check i2c adapter read functionality */ in idt_set_smbus_ops()
1189 pdev->smb_read = idt_smb_read_block; in idt_set_smbus_ops()
1190 dev_dbg(dev, "SMBus block-read op chosen"); in idt_set_smbus_ops()
1193 pdev->smb_read = idt_smb_read_i2c_block; in idt_set_smbus_ops()
1194 dev_dbg(dev, "SMBus i2c-block-read op chosen"); in idt_set_smbus_ops()
1199 pdev->smb_read = idt_smb_read_word; in idt_set_smbus_ops()
1203 pdev->smb_read = idt_smb_read_byte; in idt_set_smbus_ops()
1207 return -EPFNOSUPPORT; in idt_set_smbus_ops()
1210 /* Check i2c adapter write functionality */ in idt_set_smbus_ops()
1213 pdev->smb_write = idt_smb_write_block; in idt_set_smbus_ops()
1214 dev_dbg(dev, "SMBus block-write op chosen"); in idt_set_smbus_ops()
1217 pdev->smb_write = idt_smb_write_i2c_block; in idt_set_smbus_ops()
1218 dev_dbg(dev, "SMBus i2c-block-write op chosen"); in idt_set_smbus_ops()
1223 pdev->smb_write = idt_smb_write_word; in idt_set_smbus_ops()
1227 pdev->smb_write = idt_smb_write_byte; in idt_set_smbus_ops()
1231 return -EPFNOSUPPORT; in idt_set_smbus_ops()
1235 mutex_init(&pdev->smb_mtx); in idt_set_smbus_ops()
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; in idt_check_dev()
1261 return -ENODEV; in idt_check_dev()
1271 * idt_create_sysfs_files() - create sysfs attribute files
1277 struct device *dev = &pdev->client->dev; in idt_create_sysfs_files()
1281 if (pdev->eesize == 0) { in idt_create_sysfs_files()
1282 dev_dbg(dev, "Skip creating sysfs-files"); in idt_create_sysfs_files()
1290 pdev->ee_file = devm_kmemdup(dev, &bin_attr_eeprom, in idt_create_sysfs_files()
1291 sizeof(*pdev->ee_file), GFP_KERNEL); in idt_create_sysfs_files()
1292 if (!pdev->ee_file) in idt_create_sysfs_files()
1293 return -ENOMEM; in idt_create_sysfs_files()
1295 /* In case of read-only EEPROM get rid of write ability */ in idt_create_sysfs_files()
1296 if (pdev->eero) { in idt_create_sysfs_files()
1297 pdev->ee_file->attr.mode &= ~0200; in idt_create_sysfs_files()
1298 pdev->ee_file->write = NULL; in idt_create_sysfs_files()
1301 pdev->ee_file->size = pdev->eesize; in idt_create_sysfs_files()
1302 ret = sysfs_create_bin_file(&dev->kobj, pdev->ee_file); in idt_create_sysfs_files()
1304 dev_err(dev, "Failed to create EEPROM sysfs-node"); in idt_create_sysfs_files()
1312 * idt_remove_sysfs_files() - remove sysfs attribute files
1317 struct device *dev = &pdev->client->dev; in idt_remove_sysfs_files()
1320 if (pdev->eesize == 0) in idt_remove_sysfs_files()
1324 sysfs_remove_bin_file(&dev->kobj, pdev->ee_file); in idt_remove_sysfs_files()
1328 * idt_create_dbgfs_files() - create debugfs files
1334 struct i2c_client *cli = pdev->client; in idt_create_dbgfs_files()
1338 snprintf(fname, CSRNAME_LEN, "%d-%04hx", cli->adapter->nr, cli->addr); in idt_create_dbgfs_files()
1339 pdev->csr_dir = debugfs_create_dir(fname, csr_dbgdir); in idt_create_dbgfs_files()
1342 debugfs_create_file(cli->name, 0600, pdev->csr_dir, pdev, in idt_create_dbgfs_files()
1347 * idt_remove_dbgfs_files() - remove debugfs files
1352 /* Remove CSR directory and it sysfs-node */ in idt_remove_dbgfs_files()
1353 debugfs_remove_recursive(pdev->csr_dir); in idt_remove_dbgfs_files()
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 */ in idt_init()
1577 * idt_exit() - IDT 89HPESx driver exit() callback method
1584 /* Unregister i2c-device driver */ in idt_exit()