1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ZyDAS ZD1301 driver (USB interface)
5 * Copyright (C) 2015 Antti Palosaari <crope@iki.fi>
9 #include "zd1301_demod.h"
11 #include <linux/i2c.h>
12 #include <linux/platform_device.h>
14 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
18 u8 buf
[BUF_LEN
]; /* bulk USB control message */
19 struct zd1301_demod_platform_data demod_pdata
;
20 struct mt2060_platform_data mt2060_pdata
;
21 struct platform_device
*platform_device_demod
;
22 struct i2c_client
*i2c_client_tuner
;
25 static int zd1301_ctrl_msg(struct dvb_usb_device
*d
, const u8
*wbuf
,
26 unsigned int wlen
, u8
*rbuf
, unsigned int rlen
)
28 struct zd1301_dev
*dev
= d_to_priv(d
);
29 struct usb_interface
*intf
= d
->intf
;
30 int ret
, actual_length
;
32 mutex_lock(&d
->usb_mutex
);
34 memcpy(&dev
->buf
, wbuf
, wlen
);
36 dev_dbg(&intf
->dev
, ">>> %*ph\n", wlen
, dev
->buf
);
38 ret
= usb_bulk_msg(d
->udev
, usb_sndbulkpipe(d
->udev
, 0x04), dev
->buf
,
39 wlen
, &actual_length
, 1000);
41 dev_err(&intf
->dev
, "1st usb_bulk_msg() failed %d\n", ret
);
42 goto err_mutex_unlock
;
46 ret
= usb_bulk_msg(d
->udev
, usb_rcvbulkpipe(d
->udev
, 0x83),
47 dev
->buf
, rlen
, &actual_length
, 1000);
50 "2nd usb_bulk_msg() failed %d\n", ret
);
51 goto err_mutex_unlock
;
54 dev_dbg(&intf
->dev
, "<<< %*ph\n", actual_length
, dev
->buf
);
56 if (actual_length
!= rlen
) {
58 * Chip replies often with 3 byte len stub. On that case
59 * we have to query new reply.
61 dev_dbg(&intf
->dev
, "repeating reply message\n");
63 ret
= usb_bulk_msg(d
->udev
,
64 usb_rcvbulkpipe(d
->udev
, 0x83),
65 dev
->buf
, rlen
, &actual_length
,
69 "3rd usb_bulk_msg() failed %d\n", ret
);
70 goto err_mutex_unlock
;
74 "<<< %*ph\n", actual_length
, dev
->buf
);
77 memcpy(rbuf
, dev
->buf
, rlen
);
81 mutex_unlock(&d
->usb_mutex
);
85 static int zd1301_demod_wreg(void *reg_priv
, u16 reg
, u8 val
)
87 struct dvb_usb_device
*d
= reg_priv
;
88 struct usb_interface
*intf
= d
->intf
;
90 u8 buf
[7] = {0x07, 0x00, 0x03, 0x01,
91 (reg
>> 0) & 0xff, (reg
>> 8) & 0xff, val
};
93 ret
= zd1301_ctrl_msg(d
, buf
, 7, NULL
, 0);
99 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
103 static int zd1301_demod_rreg(void *reg_priv
, u16 reg
, u8
*val
)
105 struct dvb_usb_device
*d
= reg_priv
;
106 struct usb_interface
*intf
= d
->intf
;
108 u8 buf
[7] = {0x07, 0x00, 0x04, 0x01,
109 (reg
>> 0) & 0xff, (reg
>> 8) & 0xff, 0};
111 ret
= zd1301_ctrl_msg(d
, buf
, 7, buf
, 7);
119 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
123 static int zd1301_frontend_attach(struct dvb_usb_adapter
*adap
)
125 struct dvb_usb_device
*d
= adap_to_d(adap
);
126 struct zd1301_dev
*dev
= adap_to_priv(adap
);
127 struct usb_interface
*intf
= d
->intf
;
128 struct platform_device
*pdev
;
129 struct i2c_client
*client
;
130 struct i2c_board_info board_info
;
131 struct i2c_adapter
*adapter
;
132 struct dvb_frontend
*frontend
;
135 dev_dbg(&intf
->dev
, "\n");
137 /* Add platform demod */
138 dev
->demod_pdata
.reg_priv
= d
;
139 dev
->demod_pdata
.reg_read
= zd1301_demod_rreg
;
140 dev
->demod_pdata
.reg_write
= zd1301_demod_wreg
;
141 request_module("%s", "zd1301_demod");
142 pdev
= platform_device_register_data(&intf
->dev
,
146 sizeof(dev
->demod_pdata
));
151 if (!pdev
->dev
.driver
) {
155 if (!try_module_get(pdev
->dev
.driver
->owner
)) {
157 goto err_platform_device_unregister
;
160 adapter
= zd1301_demod_get_i2c_adapter(pdev
);
161 frontend
= zd1301_demod_get_dvb_frontend(pdev
);
162 if (!adapter
|| !frontend
) {
164 goto err_module_put_demod
;
168 dev
->mt2060_pdata
.i2c_write_max
= 9;
169 dev
->mt2060_pdata
.dvb_frontend
= frontend
;
170 memset(&board_info
, 0, sizeof(board_info
));
171 strscpy(board_info
.type
, "mt2060", I2C_NAME_SIZE
);
172 board_info
.addr
= 0x60;
173 board_info
.platform_data
= &dev
->mt2060_pdata
;
174 request_module("%s", "mt2060");
175 client
= i2c_new_client_device(adapter
, &board_info
);
176 if (!i2c_client_has_driver(client
)) {
178 goto err_module_put_demod
;
180 if (!try_module_get(client
->dev
.driver
->owner
)) {
182 goto err_i2c_unregister_device
;
185 dev
->platform_device_demod
= pdev
;
186 dev
->i2c_client_tuner
= client
;
187 adap
->fe
[0] = frontend
;
190 err_i2c_unregister_device
:
191 i2c_unregister_device(client
);
192 err_module_put_demod
:
193 module_put(pdev
->dev
.driver
->owner
);
194 err_platform_device_unregister
:
195 platform_device_unregister(pdev
);
197 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
201 static int zd1301_frontend_detach(struct dvb_usb_adapter
*adap
)
203 struct dvb_usb_device
*d
= adap_to_d(adap
);
204 struct zd1301_dev
*dev
= d_to_priv(d
);
205 struct usb_interface
*intf
= d
->intf
;
206 struct platform_device
*pdev
;
207 struct i2c_client
*client
;
209 dev_dbg(&intf
->dev
, "\n");
211 client
= dev
->i2c_client_tuner
;
212 pdev
= dev
->platform_device_demod
;
214 /* Remove I2C tuner */
216 module_put(client
->dev
.driver
->owner
);
217 i2c_unregister_device(client
);
220 /* Remove platform demod */
222 module_put(pdev
->dev
.driver
->owner
);
223 platform_device_unregister(pdev
);
229 static int zd1301_streaming_ctrl(struct dvb_frontend
*fe
, int onoff
)
231 struct dvb_usb_device
*d
= fe_to_d(fe
);
232 struct usb_interface
*intf
= d
->intf
;
234 u8 buf
[3] = {0x03, 0x00, onoff
? 0x07 : 0x08};
236 dev_dbg(&intf
->dev
, "onoff=%d\n", onoff
);
238 ret
= zd1301_ctrl_msg(d
, buf
, 3, NULL
, 0);
244 dev_dbg(&intf
->dev
, "failed=%d\n", ret
);
248 static const struct dvb_usb_device_properties zd1301_props
= {
249 .driver_name
= KBUILD_MODNAME
,
250 .owner
= THIS_MODULE
,
251 .adapter_nr
= adapter_nr
,
252 .size_of_priv
= sizeof(struct zd1301_dev
),
254 .frontend_attach
= zd1301_frontend_attach
,
255 .frontend_detach
= zd1301_frontend_detach
,
256 .streaming_ctrl
= zd1301_streaming_ctrl
,
261 .stream
= DVB_USB_STREAM_BULK(0x81, 6, 21 * 188),
266 static const struct usb_device_id zd1301_id_table
[] = {
267 {DVB_USB_DEVICE(USB_VID_ZYDAS
, 0x13a1, &zd1301_props
,
268 "ZyDAS ZD1301 reference design", NULL
)},
271 MODULE_DEVICE_TABLE(usb
, zd1301_id_table
);
273 /* Usb specific object needed to register this driver with the usb subsystem */
274 static struct usb_driver zd1301_usb_driver
= {
275 .name
= KBUILD_MODNAME
,
276 .id_table
= zd1301_id_table
,
277 .probe
= dvb_usbv2_probe
,
278 .disconnect
= dvb_usbv2_disconnect
,
279 .suspend
= dvb_usbv2_suspend
,
280 .resume
= dvb_usbv2_resume
,
281 .reset_resume
= dvb_usbv2_reset_resume
,
285 module_usb_driver(zd1301_usb_driver
);
287 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
288 MODULE_DESCRIPTION("ZyDAS ZD1301 driver");
289 MODULE_LICENSE("GPL");