2 * ZyDAS ZD1301 driver (USB interface)
4 * Copyright (C) 2015 Antti Palosaari <crope@iki.fi>
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.
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.
18 #include "zd1301_demod.h"
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
23 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
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
;
34 static int zd1301_ctrl_msg(struct dvb_usb_device
*d
, const u8
*wbuf
,
35 unsigned int wlen
, u8
*rbuf
, unsigned int rlen
)
37 struct zd1301_dev
*dev
= d_to_priv(d
);
38 struct usb_interface
*intf
= d
->intf
;
39 int ret
, actual_length
;
41 mutex_lock(&d
->usb_mutex
);
43 memcpy(&dev
->buf
, wbuf
, wlen
);
45 dev_dbg(&intf
->dev
, ">>> %*ph\n", wlen
, dev
->buf
);
47 ret
= usb_bulk_msg(d
->udev
, usb_sndbulkpipe(d
->udev
, 0x04), dev
->buf
,
48 wlen
, &actual_length
, 1000);
50 dev_err(&intf
->dev
, "1st usb_bulk_msg() failed %d\n", ret
);
51 goto err_mutex_unlock
;
55 ret
= usb_bulk_msg(d
->udev
, usb_rcvbulkpipe(d
->udev
, 0x83),
56 dev
->buf
, rlen
, &actual_length
, 1000);
59 "2nd usb_bulk_msg() failed %d\n", ret
);
60 goto err_mutex_unlock
;
63 dev_dbg(&intf
->dev
, "<<< %*ph\n", actual_length
, dev
->buf
);
65 if (actual_length
!= rlen
) {
67 * Chip replies often with 3 byte len stub. On that case
68 * we have to query new reply.
70 dev_dbg(&intf
->dev
, "repeating reply message\n");
72 ret
= usb_bulk_msg(d
->udev
,
73 usb_rcvbulkpipe(d
->udev
, 0x83),
74 dev
->buf
, rlen
, &actual_length
,
78 "3rd usb_bulk_msg() failed %d\n", ret
);
79 goto err_mutex_unlock
;
83 "<<< %*ph\n", actual_length
, dev
->buf
);
86 memcpy(rbuf
, dev
->buf
, rlen
);
90 mutex_unlock(&d
->usb_mutex
);
94 static int zd1301_demod_wreg(void *reg_priv
, u16 reg
, u8 val
)
96 struct dvb_usb_device
*d
= reg_priv
;
97 struct usb_interface
*intf
= d
->intf
;
99 u8 buf
[7] = {0x07, 0x00, 0x03, 0x01,
100 (reg
>> 0) & 0xff, (reg
>> 8) & 0xff, val
};
102 ret
= zd1301_ctrl_msg(d
, buf
, 7, NULL
, 0);
108 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
112 static int zd1301_demod_rreg(void *reg_priv
, u16 reg
, u8
*val
)
114 struct dvb_usb_device
*d
= reg_priv
;
115 struct usb_interface
*intf
= d
->intf
;
117 u8 buf
[7] = {0x07, 0x00, 0x04, 0x01,
118 (reg
>> 0) & 0xff, (reg
>> 8) & 0xff, 0};
120 ret
= zd1301_ctrl_msg(d
, buf
, 7, buf
, 7);
128 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
132 static int zd1301_frontend_attach(struct dvb_usb_adapter
*adap
)
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
;
144 dev_dbg(&intf
->dev
, "\n");
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
,
155 sizeof(dev
->demod_pdata
));
160 if (!pdev
->dev
.driver
) {
164 if (!try_module_get(pdev
->dev
.driver
->owner
)) {
166 goto err_platform_device_unregister
;
169 adapter
= zd1301_demod_get_i2c_adapter(pdev
);
170 frontend
= zd1301_demod_get_dvb_frontend(pdev
);
171 if (!adapter
|| !frontend
) {
173 goto err_module_put_demod
;
177 dev
->mt2060_pdata
.i2c_write_max
= 9;
178 dev
->mt2060_pdata
.dvb_frontend
= frontend
;
179 memset(&board_info
, 0, sizeof(board_info
));
180 strlcpy(board_info
.type
, "mt2060", I2C_NAME_SIZE
);
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
) {
187 goto err_module_put_demod
;
189 if (!try_module_get(client
->dev
.driver
->owner
)) {
191 goto err_i2c_unregister_device
;
194 dev
->platform_device_demod
= pdev
;
195 dev
->i2c_client_tuner
= client
;
196 adap
->fe
[0] = frontend
;
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
);
206 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
210 static int zd1301_frontend_detach(struct dvb_usb_adapter
*adap
)
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
;
218 dev_dbg(&intf
->dev
, "\n");
220 client
= dev
->i2c_client_tuner
;
221 pdev
= dev
->platform_device_demod
;
223 /* Remove I2C tuner */
225 module_put(client
->dev
.driver
->owner
);
226 i2c_unregister_device(client
);
229 /* Remove platform demod */
231 module_put(pdev
->dev
.driver
->owner
);
232 platform_device_unregister(pdev
);
238 static int zd1301_streaming_ctrl(struct dvb_frontend
*fe
, int onoff
)
240 struct dvb_usb_device
*d
= fe_to_d(fe
);
241 struct usb_interface
*intf
= d
->intf
;
243 u8 buf
[3] = {0x03, 0x00, onoff
? 0x07 : 0x08};
245 dev_dbg(&intf
->dev
, "onoff=%d\n", onoff
);
247 ret
= zd1301_ctrl_msg(d
, buf
, 3, NULL
, 0);
253 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
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
),
263 .frontend_attach
= zd1301_frontend_attach
,
264 .frontend_detach
= zd1301_frontend_detach
,
265 .streaming_ctrl
= zd1301_streaming_ctrl
,
270 .stream
= DVB_USB_STREAM_BULK(0x81, 6, 21 * 188),
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
)},
280 MODULE_DEVICE_TABLE(usb
, zd1301_id_table
);
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
,
294 module_usb_driver(zd1301_usb_driver
);
296 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
297 MODULE_DESCRIPTION("ZyDAS ZD1301 driver");
298 MODULE_LICENSE("GPL");