Antti Palosaari | 992b398 | 2015-07-23 18:53:41 -0300 | [diff] [blame] | 1 | /* |
| 2 | * ZyDAS ZD1301 driver (USB interface) |
| 3 | * |
| 4 | * Copyright (C) 2015 Antti Palosaari <crope@iki.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include "dvb_usb.h" |
| 18 | #include "zd1301_demod.h" |
| 19 | #include "mt2060.h" |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | |
| 23 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 24 | |
| 25 | struct zd1301_dev { |
| 26 | #define BUF_LEN 8 |
| 27 | u8 buf[BUF_LEN]; /* bulk USB control message */ |
| 28 | struct zd1301_demod_platform_data demod_pdata; |
| 29 | struct mt2060_platform_data mt2060_pdata; |
| 30 | struct platform_device *platform_device_demod; |
| 31 | struct i2c_client *i2c_client_tuner; |
| 32 | }; |
| 33 | |
| 34 | static int zd1301_ctrl_msg(struct dvb_usb_device *d, const u8 *wbuf, |
| 35 | unsigned int wlen, u8 *rbuf, unsigned int rlen) |
| 36 | { |
| 37 | struct zd1301_dev *dev = d_to_priv(d); |
| 38 | struct usb_interface *intf = d->intf; |
| 39 | int ret, actual_length; |
| 40 | |
| 41 | mutex_lock(&d->usb_mutex); |
| 42 | |
| 43 | memcpy(&dev->buf, wbuf, wlen); |
| 44 | |
| 45 | dev_dbg(&intf->dev, ">>> %*ph\n", wlen, dev->buf); |
| 46 | |
| 47 | ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev, 0x04), dev->buf, |
| 48 | wlen, &actual_length, 1000); |
| 49 | if (ret) { |
| 50 | dev_err(&intf->dev, "1st usb_bulk_msg() failed %d\n", ret); |
| 51 | goto err_mutex_unlock; |
| 52 | } |
| 53 | |
| 54 | if (rlen) { |
| 55 | ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev, 0x83), |
| 56 | dev->buf, rlen, &actual_length, 1000); |
| 57 | if (ret) { |
| 58 | dev_err(&intf->dev, |
| 59 | "2nd usb_bulk_msg() failed %d\n", ret); |
| 60 | goto err_mutex_unlock; |
| 61 | } |
| 62 | |
| 63 | dev_dbg(&intf->dev, "<<< %*ph\n", actual_length, dev->buf); |
| 64 | |
| 65 | if (actual_length != rlen) { |
| 66 | /* |
| 67 | * Chip replies often with 3 byte len stub. On that case |
| 68 | * we have to query new reply. |
| 69 | */ |
| 70 | dev_dbg(&intf->dev, "repeating reply message\n"); |
| 71 | |
| 72 | ret = usb_bulk_msg(d->udev, |
| 73 | usb_rcvbulkpipe(d->udev, 0x83), |
| 74 | dev->buf, rlen, &actual_length, |
| 75 | 1000); |
| 76 | if (ret) { |
| 77 | dev_err(&intf->dev, |
| 78 | "3rd usb_bulk_msg() failed %d\n", ret); |
| 79 | goto err_mutex_unlock; |
| 80 | } |
| 81 | |
| 82 | dev_dbg(&intf->dev, |
| 83 | "<<< %*ph\n", actual_length, dev->buf); |
| 84 | } |
| 85 | |
| 86 | memcpy(rbuf, dev->buf, rlen); |
| 87 | } |
| 88 | |
| 89 | err_mutex_unlock: |
| 90 | mutex_unlock(&d->usb_mutex); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static int zd1301_demod_wreg(void *reg_priv, u16 reg, u8 val) |
| 95 | { |
| 96 | struct dvb_usb_device *d = reg_priv; |
| 97 | struct usb_interface *intf = d->intf; |
| 98 | int ret; |
| 99 | u8 buf[7] = {0x07, 0x00, 0x03, 0x01, |
| 100 | (reg >> 0) & 0xff, (reg >> 8) & 0xff, val}; |
| 101 | |
| 102 | ret = zd1301_ctrl_msg(d, buf, 7, NULL, 0); |
| 103 | if (ret) |
| 104 | goto err; |
| 105 | |
| 106 | return 0; |
| 107 | err: |
| 108 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static int zd1301_demod_rreg(void *reg_priv, u16 reg, u8 *val) |
| 113 | { |
| 114 | struct dvb_usb_device *d = reg_priv; |
| 115 | struct usb_interface *intf = d->intf; |
| 116 | int ret; |
| 117 | u8 buf[7] = {0x07, 0x00, 0x04, 0x01, |
| 118 | (reg >> 0) & 0xff, (reg >> 8) & 0xff, 0}; |
| 119 | |
| 120 | ret = zd1301_ctrl_msg(d, buf, 7, buf, 7); |
| 121 | if (ret) |
| 122 | goto err; |
| 123 | |
| 124 | *val = buf[6]; |
| 125 | |
| 126 | return 0; |
| 127 | err: |
| 128 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static int zd1301_frontend_attach(struct dvb_usb_adapter *adap) |
| 133 | { |
| 134 | struct dvb_usb_device *d = adap_to_d(adap); |
| 135 | struct zd1301_dev *dev = adap_to_priv(adap); |
| 136 | struct usb_interface *intf = d->intf; |
| 137 | struct platform_device *pdev; |
| 138 | struct i2c_client *client; |
| 139 | struct i2c_board_info board_info; |
| 140 | struct i2c_adapter *adapter; |
| 141 | struct dvb_frontend *frontend; |
| 142 | int ret; |
| 143 | |
| 144 | dev_dbg(&intf->dev, "\n"); |
| 145 | |
| 146 | /* Add platform demod */ |
| 147 | dev->demod_pdata.reg_priv = d; |
| 148 | dev->demod_pdata.reg_read = zd1301_demod_rreg; |
| 149 | dev->demod_pdata.reg_write = zd1301_demod_wreg; |
| 150 | request_module("%s", "zd1301_demod"); |
| 151 | pdev = platform_device_register_data(&intf->dev, |
| 152 | "zd1301_demod", |
| 153 | PLATFORM_DEVID_AUTO, |
| 154 | &dev->demod_pdata, |
| 155 | sizeof(dev->demod_pdata)); |
| 156 | if (IS_ERR(pdev)) { |
| 157 | ret = PTR_ERR(pdev); |
| 158 | goto err; |
| 159 | } |
| 160 | if (!pdev->dev.driver) { |
| 161 | ret = -ENODEV; |
| 162 | goto err; |
| 163 | } |
| 164 | if (!try_module_get(pdev->dev.driver->owner)) { |
| 165 | ret = -ENODEV; |
| 166 | goto err_platform_device_unregister; |
| 167 | } |
| 168 | |
| 169 | adapter = zd1301_demod_get_i2c_adapter(pdev); |
| 170 | frontend = zd1301_demod_get_dvb_frontend(pdev); |
Arnd Bergmann | 0d1270d | 2017-02-07 11:01:41 -0200 | [diff] [blame] | 171 | if (!adapter || !frontend) { |
| 172 | ret = -ENODEV; |
| 173 | goto err_module_put_demod; |
| 174 | } |
Antti Palosaari | 992b398 | 2015-07-23 18:53:41 -0300 | [diff] [blame] | 175 | |
| 176 | /* Add I2C tuner */ |
| 177 | dev->mt2060_pdata.i2c_write_max = 9; |
| 178 | dev->mt2060_pdata.dvb_frontend = frontend; |
| 179 | memset(&board_info, 0, sizeof(board_info)); |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 180 | strscpy(board_info.type, "mt2060", I2C_NAME_SIZE); |
Antti Palosaari | 992b398 | 2015-07-23 18:53:41 -0300 | [diff] [blame] | 181 | board_info.addr = 0x60; |
| 182 | board_info.platform_data = &dev->mt2060_pdata; |
| 183 | request_module("%s", "mt2060"); |
| 184 | client = i2c_new_device(adapter, &board_info); |
| 185 | if (!client || !client->dev.driver) { |
| 186 | ret = -ENODEV; |
| 187 | goto err_module_put_demod; |
| 188 | } |
| 189 | if (!try_module_get(client->dev.driver->owner)) { |
| 190 | ret = -ENODEV; |
| 191 | goto err_i2c_unregister_device; |
| 192 | } |
| 193 | |
| 194 | dev->platform_device_demod = pdev; |
| 195 | dev->i2c_client_tuner = client; |
| 196 | adap->fe[0] = frontend; |
| 197 | |
| 198 | return 0; |
| 199 | err_i2c_unregister_device: |
| 200 | i2c_unregister_device(client); |
| 201 | err_module_put_demod: |
| 202 | module_put(pdev->dev.driver->owner); |
| 203 | err_platform_device_unregister: |
| 204 | platform_device_unregister(pdev); |
| 205 | err: |
| 206 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static int zd1301_frontend_detach(struct dvb_usb_adapter *adap) |
| 211 | { |
| 212 | struct dvb_usb_device *d = adap_to_d(adap); |
| 213 | struct zd1301_dev *dev = d_to_priv(d); |
| 214 | struct usb_interface *intf = d->intf; |
| 215 | struct platform_device *pdev; |
| 216 | struct i2c_client *client; |
| 217 | |
| 218 | dev_dbg(&intf->dev, "\n"); |
| 219 | |
| 220 | client = dev->i2c_client_tuner; |
| 221 | pdev = dev->platform_device_demod; |
| 222 | |
| 223 | /* Remove I2C tuner */ |
| 224 | if (client) { |
| 225 | module_put(client->dev.driver->owner); |
| 226 | i2c_unregister_device(client); |
| 227 | } |
| 228 | |
| 229 | /* Remove platform demod */ |
| 230 | if (pdev) { |
| 231 | module_put(pdev->dev.driver->owner); |
| 232 | platform_device_unregister(pdev); |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int zd1301_streaming_ctrl(struct dvb_frontend *fe, int onoff) |
| 239 | { |
| 240 | struct dvb_usb_device *d = fe_to_d(fe); |
| 241 | struct usb_interface *intf = d->intf; |
| 242 | int ret; |
| 243 | u8 buf[3] = {0x03, 0x00, onoff ? 0x07 : 0x08}; |
| 244 | |
| 245 | dev_dbg(&intf->dev, "onoff=%d\n", onoff); |
| 246 | |
| 247 | ret = zd1301_ctrl_msg(d, buf, 3, NULL, 0); |
| 248 | if (ret) |
| 249 | goto err; |
| 250 | |
| 251 | return 0; |
| 252 | err: |
| 253 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static const struct dvb_usb_device_properties zd1301_props = { |
| 258 | .driver_name = KBUILD_MODNAME, |
| 259 | .owner = THIS_MODULE, |
| 260 | .adapter_nr = adapter_nr, |
| 261 | .size_of_priv = sizeof(struct zd1301_dev), |
| 262 | |
| 263 | .frontend_attach = zd1301_frontend_attach, |
| 264 | .frontend_detach = zd1301_frontend_detach, |
| 265 | .streaming_ctrl = zd1301_streaming_ctrl, |
| 266 | |
| 267 | .num_adapters = 1, |
| 268 | .adapter = { |
| 269 | { |
| 270 | .stream = DVB_USB_STREAM_BULK(0x81, 6, 21 * 188), |
| 271 | }, |
| 272 | }, |
| 273 | }; |
| 274 | |
| 275 | static const struct usb_device_id zd1301_id_table[] = { |
| 276 | {DVB_USB_DEVICE(USB_VID_ZYDAS, 0x13a1, &zd1301_props, |
| 277 | "ZyDAS ZD1301 reference design", NULL)}, |
| 278 | {} |
| 279 | }; |
| 280 | MODULE_DEVICE_TABLE(usb, zd1301_id_table); |
| 281 | |
| 282 | /* Usb specific object needed to register this driver with the usb subsystem */ |
| 283 | static struct usb_driver zd1301_usb_driver = { |
| 284 | .name = KBUILD_MODNAME, |
| 285 | .id_table = zd1301_id_table, |
| 286 | .probe = dvb_usbv2_probe, |
| 287 | .disconnect = dvb_usbv2_disconnect, |
| 288 | .suspend = dvb_usbv2_suspend, |
| 289 | .resume = dvb_usbv2_resume, |
| 290 | .reset_resume = dvb_usbv2_reset_resume, |
| 291 | .no_dynamic_id = 1, |
| 292 | .soft_unbind = 1, |
| 293 | }; |
| 294 | module_usb_driver(zd1301_usb_driver); |
| 295 | |
| 296 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 297 | MODULE_DESCRIPTION("ZyDAS ZD1301 driver"); |
| 298 | MODULE_LICENSE("GPL"); |