Lines Matching +full:always +full:- +full:wait +full:- +full:for +full:- +full:ack

1 // SPDX-License-Identifier: GPL-2.0+
3 // em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
20 #include <media/v4l2-common.h>
23 /* ----------------------------------------------------------- */
35 dev_printk(KERN_DEBUG, &dev->intf->dev, \
40 * Time in msecs to wait for i2c xfers to finish.
41 * 35ms is the maximum time a SMBUS device could wait when
54 switch (dev->i2c_speed & 0x03) { in em28xx_i2c_timeout()
56 time += 4; /* Assume 4 ms for transfers */ in em28xx_i2c_timeout()
60 time += 1; /* Assume 1 ms for transfers */ in em28xx_i2c_timeout()
80 return -EOPNOTSUPP; in em2800_i2c_send_bytes()
82 b2[5] = 0x80 + len - 1; in em2800_i2c_send_bytes()
93 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len); in em2800_i2c_send_bytes()
95 dev_warn(&dev->intf->dev, in em2800_i2c_send_bytes()
98 return (ret < 0) ? ret : -EIO; in em2800_i2c_send_bytes()
100 /* wait for completion */ in em2800_i2c_send_bytes()
102 ret = dev->em28xx_read_reg(dev, 0x05); in em2800_i2c_send_bytes()
103 if (ret == 0x80 + len - 1) in em2800_i2c_send_bytes()
105 if (ret == 0x94 + len - 1) { in em2800_i2c_send_bytes()
106 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret); in em2800_i2c_send_bytes()
107 return -ENXIO; in em2800_i2c_send_bytes()
110 dev_warn(&dev->intf->dev, in em2800_i2c_send_bytes()
118 return -ETIMEDOUT; in em2800_i2c_send_bytes()
133 return -EOPNOTSUPP; in em2800_i2c_recv_bytes()
136 buf2[1] = 0x84 + len - 1; in em2800_i2c_recv_bytes()
138 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2); in em2800_i2c_recv_bytes()
140 dev_warn(&dev->intf->dev, in em2800_i2c_recv_bytes()
143 return (ret < 0) ? ret : -EIO; in em2800_i2c_recv_bytes()
146 /* wait for completion */ in em2800_i2c_recv_bytes()
148 ret = dev->em28xx_read_reg(dev, 0x05); in em2800_i2c_recv_bytes()
149 if (ret == 0x84 + len - 1) in em2800_i2c_recv_bytes()
151 if (ret == 0x94 + len - 1) { in em2800_i2c_recv_bytes()
152 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", in em2800_i2c_recv_bytes()
154 return -ENXIO; in em2800_i2c_recv_bytes()
157 dev_warn(&dev->intf->dev, in em2800_i2c_recv_bytes()
164 if (ret != 0x84 + len - 1) in em2800_i2c_recv_bytes()
168 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4 - len, buf2, len); in em2800_i2c_recv_bytes()
170 dev_warn(&dev->intf->dev, in em2800_i2c_recv_bytes()
173 return (ret < 0) ? ret : -EIO; in em2800_i2c_recv_bytes()
175 for (i = 0; i < len; i++) in em2800_i2c_recv_bytes()
176 buf[i] = buf2[len - 1 - i]; in em2800_i2c_recv_bytes()
193 return (ret < 0) ? ret : -EIO; in em2800_i2c_check_for_device()
206 return -EOPNOTSUPP; in em28xx_i2c_send_bytes()
209 * Zero length reads always succeed, even if no device is connected in em28xx_i2c_send_bytes()
213 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len); in em28xx_i2c_send_bytes()
216 dev_warn(&dev->intf->dev, in em28xx_i2c_send_bytes()
221 dev_warn(&dev->intf->dev, in em28xx_i2c_send_bytes()
224 return -EIO; in em28xx_i2c_send_bytes()
227 /* wait for completion */ in em28xx_i2c_send_bytes()
229 ret = dev->em28xx_read_reg(dev, 0x05); in em28xx_i2c_send_bytes()
233 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n", in em28xx_i2c_send_bytes()
235 return -ENXIO; in em28xx_i2c_send_bytes()
238 dev_warn(&dev->intf->dev, in em28xx_i2c_send_bytes()
245 * NOTE: do we really have to wait for success ? in em28xx_i2c_send_bytes()
256 return -ETIMEDOUT; in em28xx_i2c_send_bytes()
259 dev_warn(&dev->intf->dev, in em28xx_i2c_send_bytes()
262 return -EIO; in em28xx_i2c_send_bytes()
274 return -EOPNOTSUPP; in em28xx_i2c_recv_bytes()
277 * Zero length reads always succeed, even if no device is connected in em28xx_i2c_recv_bytes()
281 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len); in em28xx_i2c_recv_bytes()
283 dev_warn(&dev->intf->dev, in em28xx_i2c_recv_bytes()
288 dev_dbg(&dev->intf->dev, in em28xx_i2c_recv_bytes()
297 * Anyway, the next check will fail with -ENXIO in this case, so avoid in em28xx_i2c_recv_bytes()
302 ret = dev->em28xx_read_reg(dev, 0x05); in em28xx_i2c_recv_bytes()
306 dev_warn(&dev->intf->dev, in em28xx_i2c_recv_bytes()
312 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n", in em28xx_i2c_recv_bytes()
314 return -ENXIO; in em28xx_i2c_recv_bytes()
322 return -ETIMEDOUT; in em28xx_i2c_recv_bytes()
325 dev_warn(&dev->intf->dev, in em28xx_i2c_recv_bytes()
328 return -EIO; in em28xx_i2c_recv_bytes()
343 return (ret < 0) ? ret : -EIO; in em28xx_i2c_check_for_device()
356 return -EOPNOTSUPP; in em25xx_bus_B_send_bytes()
359 * Zero length reads always succeed, even if no device is connected in em25xx_bus_B_send_bytes()
363 ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len); in em25xx_bus_B_send_bytes()
366 dev_warn(&dev->intf->dev, in em25xx_bus_B_send_bytes()
372 dev_warn(&dev->intf->dev, in em25xx_bus_B_send_bytes()
375 return -EIO; in em25xx_bus_B_send_bytes()
378 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000); in em25xx_bus_B_send_bytes()
387 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret); in em25xx_bus_B_send_bytes()
388 return -ENXIO; in em25xx_bus_B_send_bytes()
394 * this operation, it seems to succeed ALWAYS ! (even if there is no in em25xx_bus_B_send_bytes()
409 return -EOPNOTSUPP; in em25xx_bus_B_recv_bytes()
412 * Zero length reads always succeed, even if no device is connected in em25xx_bus_B_recv_bytes()
416 ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len); in em25xx_bus_B_recv_bytes()
418 dev_warn(&dev->intf->dev, in em25xx_bus_B_recv_bytes()
428 * Anyway, the next check will fail with -ENXIO in this case, so avoid in em25xx_bus_B_recv_bytes()
433 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000); in em25xx_bus_B_recv_bytes()
442 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret); in em25xx_bus_B_recv_bytes()
443 return -ENXIO; in em25xx_bus_B_recv_bytes()
449 * this operation, it seems to succeed ALWAYS ! (even if there is no in em25xx_bus_B_recv_bytes()
470 * it seems to succeed ALWAYS ! (even if no device connected) in em25xx_bus_B_check_for_device()
476 struct em28xx *dev = i2c_bus->dev; in i2c_check_for_device()
477 int rc = -EOPNOTSUPP; in i2c_check_for_device()
479 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) in i2c_check_for_device()
481 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) in i2c_check_for_device()
483 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) in i2c_check_for_device()
491 struct em28xx *dev = i2c_bus->dev; in i2c_recv_bytes()
493 int rc = -EOPNOTSUPP; in i2c_recv_bytes()
495 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) in i2c_recv_bytes()
497 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) in i2c_recv_bytes()
499 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) in i2c_recv_bytes()
507 struct em28xx *dev = i2c_bus->dev; in i2c_send_bytes()
509 int rc = -EOPNOTSUPP; in i2c_send_bytes()
511 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) in i2c_send_bytes()
513 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) in i2c_send_bytes()
515 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) in i2c_send_bytes()
527 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data; in em28xx_i2c_xfer()
528 struct em28xx *dev = i2c_bus->dev; in em28xx_i2c_xfer()
529 unsigned int bus = i2c_bus->bus; in em28xx_i2c_xfer()
538 if (dev->disconnected) in em28xx_i2c_xfer()
539 return -ENODEV; in em28xx_i2c_xfer()
541 if (!rt_mutex_trylock(&dev->i2c_bus_lock)) in em28xx_i2c_xfer()
542 return -EAGAIN; in em28xx_i2c_xfer()
545 if (bus != dev->cur_i2c_bus && in em28xx_i2c_xfer()
546 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) { in em28xx_i2c_xfer()
553 dev->cur_i2c_bus = bus; in em28xx_i2c_xfer()
556 for (i = 0; i < num; i++) { in em28xx_i2c_xfer()
560 * no len: check only for device presence in em28xx_i2c_xfer()
565 if (rc == -ENXIO) in em28xx_i2c_xfer()
566 rc = -ENODEV; in em28xx_i2c_xfer()
572 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1); in em28xx_i2c_xfer()
580 i == num - 1 ? "stop" : "nonstop", in em28xx_i2c_xfer()
585 rt_mutex_unlock(&dev->i2c_bus_lock); in em28xx_i2c_xfer()
591 i == num - 1 ? "stop" : "nonstop", in em28xx_i2c_xfer()
593 (rc == -ENODEV) ? "no device " : "", in em28xx_i2c_xfer()
596 rt_mutex_unlock(&dev->i2c_bus_lock); in em28xx_i2c_xfer()
615 len = -1; in em28xx_hash_mem()
621 if ((len & (32 / 8 - 1)) == 0) in em28xx_hash_mem()
625 return (hash >> (32 - bits)) & 0xffffffffUL; in em28xx_hash_mem()
640 return -EINVAL; in em28xx_i2c_read_block()
644 ret = i2c_master_send(&dev->i2c_client[bus], in em28xx_i2c_read_block()
649 if (dev->board.is_em2800) in em28xx_i2c_read_block()
659 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize); in em28xx_i2c_read_block()
663 remain -= rsize; in em28xx_i2c_read_block()
675 * FIXME common length/size for bytes to read, to display, hash in em28xx_i2c_eeprom()
686 /* EEPROM is always on i2c bus 0 on all known devices. */ in em28xx_i2c_eeprom()
688 dev->i2c_client[bus].addr = 0xa0 >> 1; in em28xx_i2c_eeprom()
691 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0); in em28xx_i2c_eeprom()
693 dev_info(&dev->intf->dev, "board has no eeprom\n"); in em28xx_i2c_eeprom()
694 return -ENODEV; in em28xx_i2c_eeprom()
699 return -ENOMEM; in em28xx_i2c_eeprom()
703 dev->eeprom_addrwidth_16bit, in em28xx_i2c_eeprom()
706 dev_err(&dev->intf->dev, in em28xx_i2c_eeprom()
716 if (dev->eeprom_addrwidth_16bit) in em28xx_i2c_eeprom()
717 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
721 if (dev->eeprom_addrwidth_16bit && in em28xx_i2c_eeprom()
723 /* new eeprom format; size 4-64kb */ in em28xx_i2c_eeprom()
727 dev->hash = em28xx_hash_mem(data, len, 32); in em28xx_i2c_eeprom()
730 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
732 data, dev->hash); in em28xx_i2c_eeprom()
733 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
735 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
741 * [1] always selects 12 kb RAM in em28xx_i2c_eeprom()
743 * [4] 1 = force fast mode and no suspend for device testing in em28xx_i2c_eeprom()
755 dev_err(&dev->intf->dev, in em28xx_i2c_eeprom()
774 dev_err(&dev->intf->dev, in em28xx_i2c_eeprom()
784 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
791 * TODO: decrypt eeprom data for camera bridges in em28xx_i2c_eeprom()
795 } else if (!dev->eeprom_addrwidth_16bit && in em28xx_i2c_eeprom()
798 dev->hash = em28xx_hash_mem(data, len, 32); in em28xx_i2c_eeprom()
799 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
801 data, dev->hash); in em28xx_i2c_eeprom()
802 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
805 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
807 err = -ENODEV; in em28xx_i2c_eeprom()
815 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) { in em28xx_i2c_eeprom()
817 dev_info(&dev->intf->dev, "\tNo audio on board.\n"); in em28xx_i2c_eeprom()
820 dev_info(&dev->intf->dev, "\tAC97 audio (5 sample rates)\n"); in em28xx_i2c_eeprom()
823 if (dev->chip_id < CHIP_ID_EM2860) in em28xx_i2c_eeprom()
824 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
827 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
831 if (dev->chip_id < CHIP_ID_EM2860) in em28xx_i2c_eeprom()
832 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
835 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
840 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3) in em28xx_i2c_eeprom()
841 dev_info(&dev->intf->dev, "\tUSB Remote wakeup capable\n"); in em28xx_i2c_eeprom()
843 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2) in em28xx_i2c_eeprom()
844 dev_info(&dev->intf->dev, "\tUSB Self power capable\n"); in em28xx_i2c_eeprom()
846 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) { in em28xx_i2c_eeprom()
848 dev_info(&dev->intf->dev, "\t500mA max power\n"); in em28xx_i2c_eeprom()
851 dev_info(&dev->intf->dev, "\t400mA max power\n"); in em28xx_i2c_eeprom()
854 dev_info(&dev->intf->dev, "\t300mA max power\n"); in em28xx_i2c_eeprom()
857 dev_info(&dev->intf->dev, "\t200mA max power\n"); in em28xx_i2c_eeprom()
860 dev_info(&dev->intf->dev, in em28xx_i2c_eeprom()
862 dev_config->string_idx_table, in em28xx_i2c_eeprom()
863 le16_to_cpu(dev_config->string1), in em28xx_i2c_eeprom()
864 le16_to_cpu(dev_config->string2), in em28xx_i2c_eeprom()
865 le16_to_cpu(dev_config->string3)); in em28xx_i2c_eeprom()
874 /* ----------------------------------------------------------- */
881 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data; in functionality()
883 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX || in functionality()
884 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) { in functionality()
886 } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) { in functionality()
910 /* ----------------------------------------------------------- */
938 * check i2c address range for devices
948 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { in em28xx_do_i2c_scan()
949 dev->i2c_client[bus].addr = i; in em28xx_do_i2c_scan()
950 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0); in em28xx_do_i2c_scan()
954 dev_info(&dev->intf->dev, in em28xx_do_i2c_scan()
959 if (bus == dev->def_i2c_bus) in em28xx_do_i2c_scan()
960 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist, in em28xx_do_i2c_scan()
973 if (WARN_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg || in em28xx_i2c_register()
974 !dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req)) in em28xx_i2c_register()
975 return -ENODEV; in em28xx_i2c_register()
978 return -ENODEV; in em28xx_i2c_register()
980 dev->i2c_adap[bus] = em28xx_adap_template; in em28xx_i2c_register()
981 dev->i2c_adap[bus].dev.parent = &dev->intf->dev; in em28xx_i2c_register()
982 strscpy(dev->i2c_adap[bus].name, dev_name(&dev->intf->dev), in em28xx_i2c_register()
983 sizeof(dev->i2c_adap[bus].name)); in em28xx_i2c_register()
985 dev->i2c_bus[bus].bus = bus; in em28xx_i2c_register()
986 dev->i2c_bus[bus].algo_type = algo_type; in em28xx_i2c_register()
987 dev->i2c_bus[bus].dev = dev; in em28xx_i2c_register()
988 dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus]; in em28xx_i2c_register()
990 retval = i2c_add_adapter(&dev->i2c_adap[bus]); in em28xx_i2c_register()
992 dev_err(&dev->intf->dev, in em28xx_i2c_register()
998 dev->i2c_client[bus] = em28xx_client_template; in em28xx_i2c_register()
999 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus]; in em28xx_i2c_register()
1004 &dev->eedata, &dev->eedata_len); in em28xx_i2c_register()
1005 if (retval < 0 && retval != -ENODEV) { in em28xx_i2c_register()
1006 dev_err(&dev->intf->dev, in em28xx_i2c_register()
1025 return -ENODEV; in em28xx_i2c_unregister()
1027 i2c_del_adapter(&dev->i2c_adap[bus]); in em28xx_i2c_unregister()