Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Thierry MERLE | f2242ee | 2006-12-04 08:31:14 -0300 | [diff] [blame] | 2 | /* |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 3 | * usbvision_i2c.c |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 4 | * i2c algorithm for USB-I2C Bridges |
| 5 | * |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 6 | * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de> |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 7 | * Dwaine Garden <dwainegarden@rogers.com> |
| 8 | * |
| 9 | * This module is part of usbvision driver project. |
| 10 | * Updates to driver completed by Dwaine P. Garden |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/delay.h> |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 17 | #include <linux/init.h> |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 19 | #include <linux/ioport.h> |
| 20 | #include <linux/errno.h> |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 21 | #include <linux/usb.h> |
| 22 | #include <linux/i2c.h> |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 23 | #include "usbvision.h" |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 24 | |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 25 | #define DBG_I2C (1 << 0) |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 26 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 27 | static int i2c_debug; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 28 | |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 29 | module_param(i2c_debug, int, 0644); /* debug_i2c_usb mode of the device driver */ |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 30 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); |
| 31 | |
Thierry MERLE | df18c31 | 2008-04-04 21:00:57 -0300 | [diff] [blame] | 32 | #define PDEBUG(level, fmt, args...) { \ |
| 33 | if (i2c_debug & (level)) \ |
Greg Kroah-Hartman | a482f32 | 2008-10-10 05:08:23 -0300 | [diff] [blame] | 34 | printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \ |
| 35 | __func__, __LINE__ , ## args); \ |
Thierry MERLE | df18c31 | 2008-04-04 21:00:57 -0300 | [diff] [blame] | 36 | } |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 37 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 38 | static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Mauro Carvalho Chehab | 2a7e9a2 | 2006-12-11 05:37:14 -0300 | [diff] [blame] | 39 | short len); |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 40 | static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Mauro Carvalho Chehab | 2a7e9a2 | 2006-12-11 05:37:14 -0300 | [diff] [blame] | 41 | short len); |
| 42 | |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 43 | static inline int try_write_address(struct i2c_adapter *i2c_adap, |
| 44 | unsigned char addr, int retries) |
| 45 | { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 46 | struct usb_usbvision *usbvision; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 47 | int i, ret = -1; |
| 48 | char buf[4]; |
| 49 | |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 50 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 51 | buf[0] = 0x00; |
| 52 | for (i = 0; i <= retries; i++) { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 53 | ret = (usbvision_i2c_write(usbvision, addr, buf, 1)); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 54 | if (ret == 1) |
| 55 | break; /* success! */ |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 56 | udelay(5); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 57 | if (i == retries) /* no success */ |
| 58 | break; |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 59 | udelay(10); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 60 | } |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 61 | if (i) { |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 62 | PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr); |
| 63 | PDEBUG(DBG_I2C, "Maybe there's no device at this address"); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 64 | } |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | static inline int try_read_address(struct i2c_adapter *i2c_adap, |
| 69 | unsigned char addr, int retries) |
| 70 | { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 71 | struct usb_usbvision *usbvision; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 72 | int i, ret = -1; |
| 73 | char buf[4]; |
| 74 | |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 75 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 76 | for (i = 0; i <= retries; i++) { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 77 | ret = (usbvision_i2c_read(usbvision, addr, buf, 1)); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 78 | if (ret == 1) |
| 79 | break; /* success! */ |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 80 | udelay(5); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 81 | if (i == retries) /* no success */ |
| 82 | break; |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 83 | udelay(10); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 84 | } |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 85 | if (i) { |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 86 | PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr); |
| 87 | PDEBUG(DBG_I2C, "Maybe there's no device at this address"); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 88 | } |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static inline int usb_find_address(struct i2c_adapter *i2c_adap, |
| 93 | struct i2c_msg *msg, int retries, |
| 94 | unsigned char *add) |
| 95 | { |
| 96 | unsigned short flags = msg->flags; |
Thierry MERLE | f2242ee | 2006-12-04 08:31:14 -0300 | [diff] [blame] | 97 | |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 98 | unsigned char addr; |
| 99 | int ret; |
Jean Delvare | 819cd82 | 2011-11-07 05:24:49 -0300 | [diff] [blame] | 100 | |
| 101 | addr = (msg->addr << 1); |
| 102 | if (flags & I2C_M_RD) |
| 103 | addr |= 1; |
| 104 | |
| 105 | add[0] = addr; |
| 106 | if (flags & I2C_M_RD) |
| 107 | ret = try_read_address(i2c_adap, addr, retries); |
| 108 | else |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 109 | ret = try_write_address(i2c_adap, addr, retries); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 110 | |
Jean Delvare | 819cd82 | 2011-11-07 05:24:49 -0300 | [diff] [blame] | 111 | if (ret != 1) |
| 112 | return -EREMOTEIO; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 113 | |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 118 | usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 119 | { |
| 120 | struct i2c_msg *pmsg; |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 121 | struct usb_usbvision *usbvision; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 122 | int i, ret; |
Hans Verkuil | cf8e193 | 2009-01-17 13:09:11 -0300 | [diff] [blame] | 123 | unsigned char addr = 0; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 124 | |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 125 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
| 126 | |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 127 | for (i = 0; i < num; i++) { |
| 128 | pmsg = &msgs[i]; |
| 129 | ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr); |
| 130 | if (ret != 0) { |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 131 | PDEBUG(DBG_I2C, "got NAK from device, message #%d", i); |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 132 | return (ret < 0) ? ret : -EREMOTEIO; |
| 133 | } |
| 134 | |
| 135 | if (pmsg->flags & I2C_M_RD) { |
| 136 | /* read bytes into buffer */ |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 137 | ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len)); |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 138 | if (ret < pmsg->len) |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 139 | return (ret < 0) ? ret : -EREMOTEIO; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 140 | } else { |
| 141 | /* write bytes from buffer */ |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 142 | ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len)); |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 143 | if (ret < pmsg->len) |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 144 | return (ret < 0) ? ret : -EREMOTEIO; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | return num; |
| 148 | } |
| 149 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 150 | static u32 functionality(struct i2c_adapter *adap) |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 151 | { |
Jean Delvare | 819cd82 | 2011-11-07 05:24:49 -0300 | [diff] [blame] | 152 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 153 | } |
| 154 | |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 155 | /* -----exported algorithm data: ------------------------------------- */ |
| 156 | |
Gustavo A. R. Silva | 830a45a | 2017-07-09 18:04:24 -0400 | [diff] [blame] | 157 | static const struct i2c_algorithm usbvision_algo = { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 158 | .master_xfer = usbvision_i2c_xfer, |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 159 | .smbus_xfer = NULL, |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 160 | .functionality = functionality, |
Mauro Carvalho Chehab | 781aa1d1a | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 164 | /* ----------------------------------------------------------------------- */ |
| 165 | /* usbvision specific I2C functions */ |
| 166 | /* ----------------------------------------------------------------------- */ |
Bhumika Goyal | 6843868 | 2017-08-19 06:34:15 -0400 | [diff] [blame] | 167 | static const struct i2c_adapter i2c_adap_template; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 168 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 169 | int usbvision_i2c_register(struct usb_usbvision *usbvision) |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 170 | { |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 171 | static unsigned short saa711x_addrs[] = { |
| 172 | 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */ |
| 173 | 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */ |
| 174 | I2C_CLIENT_END }; |
| 175 | |
Hans Verkuil | 8403472 | 2010-09-17 15:07:28 -0300 | [diff] [blame] | 176 | if (usbvision->registered_i2c) |
| 177 | return 0; |
| 178 | |
Ezequiel Garcia | 8fe392b | 2012-10-23 15:57:06 -0300 | [diff] [blame] | 179 | usbvision->i2c_adap = i2c_adap_template; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 180 | |
Arnd Bergmann | d82cf24 | 2017-07-17 10:29:58 -0400 | [diff] [blame] | 181 | snprintf(usbvision->i2c_adap.name, sizeof(usbvision->i2c_adap.name), |
| 182 | "usbvision-%d-%s", |
| 183 | usbvision->dev->bus->busnum, usbvision->dev->devpath); |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 184 | PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name); |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 185 | usbvision->i2c_adap.dev.parent = &usbvision->dev->dev; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 186 | |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 187 | i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 188 | |
| 189 | if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) { |
Ondrej Zary | 240d57b | 2011-04-27 17:36:05 -0300 | [diff] [blame] | 190 | printk(KERN_ERR "usbvision_i2c_register: can't write reg\n"); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 191 | return -EBUSY; |
| 192 | } |
| 193 | |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 194 | PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]"); |
| 195 | PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]"); |
| 196 | |
| 197 | /* register new adapter to i2c module... */ |
| 198 | |
| 199 | usbvision->i2c_adap.algo = &usbvision_algo; |
| 200 | |
| 201 | usbvision->i2c_adap.timeout = 100; /* default values, should */ |
| 202 | usbvision->i2c_adap.retries = 3; /* be replaced by defines */ |
| 203 | |
| 204 | i2c_add_adapter(&usbvision->i2c_adap); |
| 205 | |
| 206 | PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name); |
| 207 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 208 | /* Request the load of the i2c modules we need */ |
Hans Verkuil | 5490a7c | 2010-12-19 20:21:36 -0300 | [diff] [blame] | 209 | switch (usbvision_device_data[usbvision->dev_model].codec) { |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 210 | case CODEC_SAA7113: |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 211 | case CODEC_SAA7111: |
Hans Verkuil | 4d6c871 | 2010-05-02 09:29:27 -0300 | [diff] [blame] | 212 | /* Without this delay the detection of the saa711x is |
| 213 | hit-and-miss. */ |
| 214 | mdelay(10); |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 215 | v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 216 | &usbvision->i2c_adap, |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 217 | "saa7115_auto", 0, saa711x_addrs); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 218 | break; |
| 219 | } |
Hans Verkuil | 5490a7c | 2010-12-19 20:21:36 -0300 | [diff] [blame] | 220 | if (usbvision_device_data[usbvision->dev_model].tuner == 1) { |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 221 | struct v4l2_subdev *sd; |
| 222 | enum v4l2_i2c_tuner_type type; |
| 223 | struct tuner_setup tun_setup; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 224 | |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 225 | sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 226 | &usbvision->i2c_adap, |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 227 | "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD)); |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 228 | /* depending on whether we found a demod or not, select |
| 229 | the tuner type. */ |
| 230 | type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV; |
| 231 | |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 232 | sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 233 | &usbvision->i2c_adap, |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 234 | "tuner", 0, v4l2_i2c_tuner_addrs(type)); |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 235 | |
Hans Verkuil | 8403472 | 2010-09-17 15:07:28 -0300 | [diff] [blame] | 236 | if (sd == NULL) |
| 237 | return -ENODEV; |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 238 | if (usbvision->tuner_type != -1) { |
| 239 | tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; |
| 240 | tun_setup.type = usbvision->tuner_type; |
| 241 | tun_setup.addr = v4l2_i2c_subdev_addr(sd); |
| 242 | call_all(usbvision, tuner, s_type_addr, &tun_setup); |
| 243 | } |
| 244 | } |
Hans Verkuil | 8403472 | 2010-09-17 15:07:28 -0300 | [diff] [blame] | 245 | usbvision->registered_i2c = 1; |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 246 | |
| 247 | return 0; |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | int usbvision_i2c_unregister(struct usb_usbvision *usbvision) |
| 251 | { |
Hans Verkuil | 8403472 | 2010-09-17 15:07:28 -0300 | [diff] [blame] | 252 | if (!usbvision->registered_i2c) |
| 253 | return 0; |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 254 | |
| 255 | i2c_del_adapter(&(usbvision->i2c_adap)); |
Hans Verkuil | 8403472 | 2010-09-17 15:07:28 -0300 | [diff] [blame] | 256 | usbvision->registered_i2c = 0; |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 257 | |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 258 | PDEBUG(DBG_I2C, "i2c bus for %s unregistered", usbvision->i2c_adap.name); |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 259 | |
| 260 | return 0; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 261 | } |
| 262 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 263 | static int |
| 264 | usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr, |
| 265 | char *buf, short len) |
| 266 | { |
| 267 | int rc, retries; |
| 268 | |
| 269 | for (retries = 5;;) { |
| 270 | rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr); |
| 271 | if (rc < 0) |
| 272 | return rc; |
| 273 | |
| 274 | /* Initiate byte read cycle */ |
| 275 | /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */ |
| 276 | /* d3 0=Wr 1=Rd */ |
| 277 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, |
| 278 | (len & 0x07) | 0x18); |
| 279 | if (rc < 0) |
| 280 | return rc; |
| 281 | |
| 282 | /* Test for Busy and ACK */ |
| 283 | do { |
| 284 | /* USBVISION_SER_CONT -> d4 == 0 busy */ |
| 285 | rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); |
| 286 | } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ |
| 287 | if (rc < 0) |
| 288 | return rc; |
| 289 | |
| 290 | /* USBVISION_SER_CONT -> d5 == 1 Not ack */ |
| 291 | if ((rc & 0x20) == 0) /* Ack? */ |
| 292 | break; |
| 293 | |
| 294 | /* I2C abort */ |
| 295 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); |
| 296 | if (rc < 0) |
| 297 | return rc; |
| 298 | |
| 299 | if (--retries < 0) |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | switch (len) { |
| 304 | case 4: |
| 305 | buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4); |
Mauro Carvalho Chehab | 06eeefe | 2017-05-18 08:13:28 -0300 | [diff] [blame] | 306 | /* fall through */ |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 307 | case 3: |
| 308 | buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3); |
Mauro Carvalho Chehab | 06eeefe | 2017-05-18 08:13:28 -0300 | [diff] [blame] | 309 | /* fall through */ |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 310 | case 2: |
| 311 | buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2); |
Mauro Carvalho Chehab | 06eeefe | 2017-05-18 08:13:28 -0300 | [diff] [blame] | 312 | /* fall through */ |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 313 | case 1: |
| 314 | buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1); |
| 315 | break; |
| 316 | default: |
| 317 | printk(KERN_ERR |
| 318 | "usbvision_i2c_read_max4: buffer length > 4\n"); |
| 319 | } |
| 320 | |
| 321 | if (i2c_debug & DBG_I2C) { |
| 322 | int idx; |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 323 | |
| 324 | for (idx = 0; idx < len; idx++) |
| 325 | PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 326 | } |
| 327 | return len; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision, |
| 332 | unsigned char addr, const char *buf, |
| 333 | short len) |
| 334 | { |
| 335 | int rc, retries; |
| 336 | int i; |
Hans Verkuil | 8926e84 | 2015-07-20 09:59:36 -0300 | [diff] [blame] | 337 | unsigned char *value = usbvision->ctrl_urb_buffer; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 338 | unsigned char ser_cont; |
| 339 | |
| 340 | ser_cont = (len & 0x07) | 0x10; |
| 341 | |
| 342 | value[0] = addr; |
| 343 | value[1] = ser_cont; |
| 344 | for (i = 0; i < len; i++) |
| 345 | value[i + 2] = buf[i]; |
| 346 | |
| 347 | for (retries = 5;;) { |
| 348 | rc = usb_control_msg(usbvision->dev, |
| 349 | usb_sndctrlpipe(usbvision->dev, 1), |
| 350 | USBVISION_OP_CODE, |
| 351 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 352 | USB_RECIP_ENDPOINT, 0, |
| 353 | (__u16) USBVISION_SER_ADRS, value, |
| 354 | len + 2, HZ); |
| 355 | |
| 356 | if (rc < 0) |
| 357 | return rc; |
| 358 | |
| 359 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, |
| 360 | (len & 0x07) | 0x10); |
| 361 | if (rc < 0) |
| 362 | return rc; |
| 363 | |
| 364 | /* Test for Busy and ACK */ |
| 365 | do { |
| 366 | rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); |
| 367 | } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ |
| 368 | if (rc < 0) |
| 369 | return rc; |
| 370 | |
| 371 | if ((rc & 0x20) == 0) /* Ack? */ |
| 372 | break; |
| 373 | |
| 374 | /* I2C abort */ |
| 375 | usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); |
| 376 | |
| 377 | if (--retries < 0) |
| 378 | return -1; |
| 379 | |
| 380 | } |
| 381 | |
| 382 | if (i2c_debug & DBG_I2C) { |
| 383 | int idx; |
Hans Verkuil | 6d6a48e | 2010-12-29 13:53:21 -0300 | [diff] [blame] | 384 | |
| 385 | for (idx = 0; idx < len; idx++) |
| 386 | PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 387 | } |
| 388 | return len; |
| 389 | } |
| 390 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 391 | static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 392 | short len) |
| 393 | { |
Hans Verkuil | 5490a7c | 2010-12-19 20:21:36 -0300 | [diff] [blame] | 394 | char *buf_ptr = buf; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 395 | int retval; |
| 396 | int wrcount = 0; |
| 397 | int count; |
Hans Verkuil | 5490a7c | 2010-12-19 20:21:36 -0300 | [diff] [blame] | 398 | int max_len = 4; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 399 | |
| 400 | while (len > 0) { |
Hans Verkuil | 5490a7c | 2010-12-19 20:21:36 -0300 | [diff] [blame] | 401 | count = (len > max_len) ? max_len : len; |
| 402 | retval = usbvision_i2c_write_max4(usbvision, addr, buf_ptr, count); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 403 | if (retval > 0) { |
| 404 | len -= count; |
Hans Verkuil | 5490a7c | 2010-12-19 20:21:36 -0300 | [diff] [blame] | 405 | buf_ptr += count; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 406 | wrcount += count; |
| 407 | } else |
| 408 | return (retval < 0) ? retval : -EFAULT; |
| 409 | } |
| 410 | return wrcount; |
| 411 | } |
| 412 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 413 | static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 414 | short len) |
| 415 | { |
| 416 | char temp[4]; |
| 417 | int retval, i; |
| 418 | int rdcount = 0; |
| 419 | int count; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 420 | |
| 421 | while (len > 0) { |
| 422 | count = (len > 3) ? 4 : len; |
| 423 | retval = usbvision_i2c_read_max4(usbvision, addr, temp, count); |
| 424 | if (retval > 0) { |
| 425 | for (i = 0; i < len; i++) |
| 426 | buf[rdcount + i] = temp[i]; |
| 427 | len -= count; |
| 428 | rdcount += count; |
| 429 | } else |
| 430 | return (retval < 0) ? retval : -EFAULT; |
| 431 | } |
| 432 | return rdcount; |
| 433 | } |
| 434 | |
Bhumika Goyal | 6843868 | 2017-08-19 06:34:15 -0400 | [diff] [blame] | 435 | static const struct i2c_adapter i2c_adap_template = { |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 436 | .owner = THIS_MODULE, |
| 437 | .name = "usbvision", |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 438 | }; |