1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2992b3987SAntti Palosaari /*
3992b3987SAntti Palosaari * ZyDAS ZD1301 driver (USB interface)
4992b3987SAntti Palosaari *
5992b3987SAntti Palosaari * Copyright (C) 2015 Antti Palosaari <crope@iki.fi>
6992b3987SAntti Palosaari */
7992b3987SAntti Palosaari
8992b3987SAntti Palosaari #include "dvb_usb.h"
9992b3987SAntti Palosaari #include "zd1301_demod.h"
10992b3987SAntti Palosaari #include "mt2060.h"
11992b3987SAntti Palosaari #include <linux/i2c.h>
12992b3987SAntti Palosaari #include <linux/platform_device.h>
13992b3987SAntti Palosaari
14992b3987SAntti Palosaari DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
15992b3987SAntti Palosaari
16992b3987SAntti Palosaari struct zd1301_dev {
17992b3987SAntti Palosaari #define BUF_LEN 8
18992b3987SAntti Palosaari u8 buf[BUF_LEN]; /* bulk USB control message */
19992b3987SAntti Palosaari struct zd1301_demod_platform_data demod_pdata;
20992b3987SAntti Palosaari struct mt2060_platform_data mt2060_pdata;
21992b3987SAntti Palosaari struct platform_device *platform_device_demod;
22992b3987SAntti Palosaari struct i2c_client *i2c_client_tuner;
23992b3987SAntti Palosaari };
24992b3987SAntti Palosaari
zd1301_ctrl_msg(struct dvb_usb_device * d,const u8 * wbuf,unsigned int wlen,u8 * rbuf,unsigned int rlen)25992b3987SAntti Palosaari static int zd1301_ctrl_msg(struct dvb_usb_device *d, const u8 *wbuf,
26992b3987SAntti Palosaari unsigned int wlen, u8 *rbuf, unsigned int rlen)
27992b3987SAntti Palosaari {
28992b3987SAntti Palosaari struct zd1301_dev *dev = d_to_priv(d);
29992b3987SAntti Palosaari struct usb_interface *intf = d->intf;
30992b3987SAntti Palosaari int ret, actual_length;
31992b3987SAntti Palosaari
32992b3987SAntti Palosaari mutex_lock(&d->usb_mutex);
33992b3987SAntti Palosaari
34992b3987SAntti Palosaari memcpy(&dev->buf, wbuf, wlen);
35992b3987SAntti Palosaari
36992b3987SAntti Palosaari dev_dbg(&intf->dev, ">>> %*ph\n", wlen, dev->buf);
37992b3987SAntti Palosaari
38992b3987SAntti Palosaari ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev, 0x04), dev->buf,
39992b3987SAntti Palosaari wlen, &actual_length, 1000);
40992b3987SAntti Palosaari if (ret) {
41992b3987SAntti Palosaari dev_err(&intf->dev, "1st usb_bulk_msg() failed %d\n", ret);
42992b3987SAntti Palosaari goto err_mutex_unlock;
43992b3987SAntti Palosaari }
44992b3987SAntti Palosaari
45992b3987SAntti Palosaari if (rlen) {
46992b3987SAntti Palosaari ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev, 0x83),
47992b3987SAntti Palosaari dev->buf, rlen, &actual_length, 1000);
48992b3987SAntti Palosaari if (ret) {
49992b3987SAntti Palosaari dev_err(&intf->dev,
50992b3987SAntti Palosaari "2nd usb_bulk_msg() failed %d\n", ret);
51992b3987SAntti Palosaari goto err_mutex_unlock;
52992b3987SAntti Palosaari }
53992b3987SAntti Palosaari
54992b3987SAntti Palosaari dev_dbg(&intf->dev, "<<< %*ph\n", actual_length, dev->buf);
55992b3987SAntti Palosaari
56992b3987SAntti Palosaari if (actual_length != rlen) {
57992b3987SAntti Palosaari /*
58992b3987SAntti Palosaari * Chip replies often with 3 byte len stub. On that case
59992b3987SAntti Palosaari * we have to query new reply.
60992b3987SAntti Palosaari */
61992b3987SAntti Palosaari dev_dbg(&intf->dev, "repeating reply message\n");
62992b3987SAntti Palosaari
63992b3987SAntti Palosaari ret = usb_bulk_msg(d->udev,
64992b3987SAntti Palosaari usb_rcvbulkpipe(d->udev, 0x83),
65992b3987SAntti Palosaari dev->buf, rlen, &actual_length,
66992b3987SAntti Palosaari 1000);
67992b3987SAntti Palosaari if (ret) {
68992b3987SAntti Palosaari dev_err(&intf->dev,
69992b3987SAntti Palosaari "3rd usb_bulk_msg() failed %d\n", ret);
70992b3987SAntti Palosaari goto err_mutex_unlock;
71992b3987SAntti Palosaari }
72992b3987SAntti Palosaari
73992b3987SAntti Palosaari dev_dbg(&intf->dev,
74992b3987SAntti Palosaari "<<< %*ph\n", actual_length, dev->buf);
75992b3987SAntti Palosaari }
76992b3987SAntti Palosaari
77992b3987SAntti Palosaari memcpy(rbuf, dev->buf, rlen);
78992b3987SAntti Palosaari }
79992b3987SAntti Palosaari
80992b3987SAntti Palosaari err_mutex_unlock:
81992b3987SAntti Palosaari mutex_unlock(&d->usb_mutex);
82992b3987SAntti Palosaari return ret;
83992b3987SAntti Palosaari }
84992b3987SAntti Palosaari
zd1301_demod_wreg(void * reg_priv,u16 reg,u8 val)85992b3987SAntti Palosaari static int zd1301_demod_wreg(void *reg_priv, u16 reg, u8 val)
86992b3987SAntti Palosaari {
87992b3987SAntti Palosaari struct dvb_usb_device *d = reg_priv;
88992b3987SAntti Palosaari struct usb_interface *intf = d->intf;
89992b3987SAntti Palosaari int ret;
90992b3987SAntti Palosaari u8 buf[7] = {0x07, 0x00, 0x03, 0x01,
91992b3987SAntti Palosaari (reg >> 0) & 0xff, (reg >> 8) & 0xff, val};
92992b3987SAntti Palosaari
93992b3987SAntti Palosaari ret = zd1301_ctrl_msg(d, buf, 7, NULL, 0);
94992b3987SAntti Palosaari if (ret)
95992b3987SAntti Palosaari goto err;
96992b3987SAntti Palosaari
97992b3987SAntti Palosaari return 0;
98992b3987SAntti Palosaari err:
99992b3987SAntti Palosaari dev_dbg(&intf->dev, "failed=%d\n", ret);
100992b3987SAntti Palosaari return ret;
101992b3987SAntti Palosaari }
102992b3987SAntti Palosaari
zd1301_demod_rreg(void * reg_priv,u16 reg,u8 * val)103992b3987SAntti Palosaari static int zd1301_demod_rreg(void *reg_priv, u16 reg, u8 *val)
104992b3987SAntti Palosaari {
105992b3987SAntti Palosaari struct dvb_usb_device *d = reg_priv;
106992b3987SAntti Palosaari struct usb_interface *intf = d->intf;
107992b3987SAntti Palosaari int ret;
108992b3987SAntti Palosaari u8 buf[7] = {0x07, 0x00, 0x04, 0x01,
109992b3987SAntti Palosaari (reg >> 0) & 0xff, (reg >> 8) & 0xff, 0};
110992b3987SAntti Palosaari
111992b3987SAntti Palosaari ret = zd1301_ctrl_msg(d, buf, 7, buf, 7);
112992b3987SAntti Palosaari if (ret)
113992b3987SAntti Palosaari goto err;
114992b3987SAntti Palosaari
115992b3987SAntti Palosaari *val = buf[6];
116992b3987SAntti Palosaari
117992b3987SAntti Palosaari return 0;
118992b3987SAntti Palosaari err:
119992b3987SAntti Palosaari dev_dbg(&intf->dev, "failed=%d\n", ret);
120992b3987SAntti Palosaari return ret;
121992b3987SAntti Palosaari }
122992b3987SAntti Palosaari
zd1301_frontend_attach(struct dvb_usb_adapter * adap)123992b3987SAntti Palosaari static int zd1301_frontend_attach(struct dvb_usb_adapter *adap)
124992b3987SAntti Palosaari {
125992b3987SAntti Palosaari struct dvb_usb_device *d = adap_to_d(adap);
126992b3987SAntti Palosaari struct zd1301_dev *dev = adap_to_priv(adap);
127992b3987SAntti Palosaari struct usb_interface *intf = d->intf;
128992b3987SAntti Palosaari struct platform_device *pdev;
129992b3987SAntti Palosaari struct i2c_client *client;
130992b3987SAntti Palosaari struct i2c_board_info board_info;
131992b3987SAntti Palosaari struct i2c_adapter *adapter;
132992b3987SAntti Palosaari struct dvb_frontend *frontend;
133992b3987SAntti Palosaari int ret;
134992b3987SAntti Palosaari
135992b3987SAntti Palosaari dev_dbg(&intf->dev, "\n");
136992b3987SAntti Palosaari
137992b3987SAntti Palosaari /* Add platform demod */
138992b3987SAntti Palosaari dev->demod_pdata.reg_priv = d;
139992b3987SAntti Palosaari dev->demod_pdata.reg_read = zd1301_demod_rreg;
140992b3987SAntti Palosaari dev->demod_pdata.reg_write = zd1301_demod_wreg;
141992b3987SAntti Palosaari request_module("%s", "zd1301_demod");
142992b3987SAntti Palosaari pdev = platform_device_register_data(&intf->dev,
143992b3987SAntti Palosaari "zd1301_demod",
144992b3987SAntti Palosaari PLATFORM_DEVID_AUTO,
145992b3987SAntti Palosaari &dev->demod_pdata,
146992b3987SAntti Palosaari sizeof(dev->demod_pdata));
147992b3987SAntti Palosaari if (IS_ERR(pdev)) {
148992b3987SAntti Palosaari ret = PTR_ERR(pdev);
149992b3987SAntti Palosaari goto err;
150992b3987SAntti Palosaari }
151992b3987SAntti Palosaari if (!pdev->dev.driver) {
152992b3987SAntti Palosaari ret = -ENODEV;
153*ee50d6e6SQinglang Miao goto err_platform_device_unregister;
154992b3987SAntti Palosaari }
155992b3987SAntti Palosaari if (!try_module_get(pdev->dev.driver->owner)) {
156992b3987SAntti Palosaari ret = -ENODEV;
157992b3987SAntti Palosaari goto err_platform_device_unregister;
158992b3987SAntti Palosaari }
159992b3987SAntti Palosaari
160992b3987SAntti Palosaari adapter = zd1301_demod_get_i2c_adapter(pdev);
161992b3987SAntti Palosaari frontend = zd1301_demod_get_dvb_frontend(pdev);
1620d1270dfSArnd Bergmann if (!adapter || !frontend) {
1630d1270dfSArnd Bergmann ret = -ENODEV;
1640d1270dfSArnd Bergmann goto err_module_put_demod;
1650d1270dfSArnd Bergmann }
166992b3987SAntti Palosaari
167992b3987SAntti Palosaari /* Add I2C tuner */
168992b3987SAntti Palosaari dev->mt2060_pdata.i2c_write_max = 9;
169992b3987SAntti Palosaari dev->mt2060_pdata.dvb_frontend = frontend;
170992b3987SAntti Palosaari memset(&board_info, 0, sizeof(board_info));
171c0decac1SMauro Carvalho Chehab strscpy(board_info.type, "mt2060", I2C_NAME_SIZE);
172992b3987SAntti Palosaari board_info.addr = 0x60;
173992b3987SAntti Palosaari board_info.platform_data = &dev->mt2060_pdata;
174992b3987SAntti Palosaari request_module("%s", "mt2060");
1753dd01212SWolfram Sang client = i2c_new_client_device(adapter, &board_info);
1763dd01212SWolfram Sang if (!i2c_client_has_driver(client)) {
177992b3987SAntti Palosaari ret = -ENODEV;
178992b3987SAntti Palosaari goto err_module_put_demod;
179992b3987SAntti Palosaari }
180992b3987SAntti Palosaari if (!try_module_get(client->dev.driver->owner)) {
181992b3987SAntti Palosaari ret = -ENODEV;
182992b3987SAntti Palosaari goto err_i2c_unregister_device;
183992b3987SAntti Palosaari }
184992b3987SAntti Palosaari
185992b3987SAntti Palosaari dev->platform_device_demod = pdev;
186992b3987SAntti Palosaari dev->i2c_client_tuner = client;
187992b3987SAntti Palosaari adap->fe[0] = frontend;
188992b3987SAntti Palosaari
189992b3987SAntti Palosaari return 0;
190992b3987SAntti Palosaari err_i2c_unregister_device:
191992b3987SAntti Palosaari i2c_unregister_device(client);
192992b3987SAntti Palosaari err_module_put_demod:
193992b3987SAntti Palosaari module_put(pdev->dev.driver->owner);
194992b3987SAntti Palosaari err_platform_device_unregister:
195992b3987SAntti Palosaari platform_device_unregister(pdev);
196992b3987SAntti Palosaari err:
197992b3987SAntti Palosaari dev_dbg(&intf->dev, "failed=%d\n", ret);
198992b3987SAntti Palosaari return ret;
199992b3987SAntti Palosaari }
200992b3987SAntti Palosaari
zd1301_frontend_detach(struct dvb_usb_adapter * adap)201992b3987SAntti Palosaari static int zd1301_frontend_detach(struct dvb_usb_adapter *adap)
202992b3987SAntti Palosaari {
203992b3987SAntti Palosaari struct dvb_usb_device *d = adap_to_d(adap);
204992b3987SAntti Palosaari struct zd1301_dev *dev = d_to_priv(d);
205992b3987SAntti Palosaari struct usb_interface *intf = d->intf;
206992b3987SAntti Palosaari struct platform_device *pdev;
207992b3987SAntti Palosaari struct i2c_client *client;
208992b3987SAntti Palosaari
209992b3987SAntti Palosaari dev_dbg(&intf->dev, "\n");
210992b3987SAntti Palosaari
211992b3987SAntti Palosaari client = dev->i2c_client_tuner;
212992b3987SAntti Palosaari pdev = dev->platform_device_demod;
213992b3987SAntti Palosaari
214992b3987SAntti Palosaari /* Remove I2C tuner */
215992b3987SAntti Palosaari if (client) {
216992b3987SAntti Palosaari module_put(client->dev.driver->owner);
217992b3987SAntti Palosaari i2c_unregister_device(client);
218992b3987SAntti Palosaari }
219992b3987SAntti Palosaari
220992b3987SAntti Palosaari /* Remove platform demod */
221992b3987SAntti Palosaari if (pdev) {
222992b3987SAntti Palosaari module_put(pdev->dev.driver->owner);
223992b3987SAntti Palosaari platform_device_unregister(pdev);
224992b3987SAntti Palosaari }
225992b3987SAntti Palosaari
226992b3987SAntti Palosaari return 0;
227992b3987SAntti Palosaari }
228992b3987SAntti Palosaari
zd1301_streaming_ctrl(struct dvb_frontend * fe,int onoff)229992b3987SAntti Palosaari static int zd1301_streaming_ctrl(struct dvb_frontend *fe, int onoff)
230992b3987SAntti Palosaari {
231992b3987SAntti Palosaari struct dvb_usb_device *d = fe_to_d(fe);
232992b3987SAntti Palosaari struct usb_interface *intf = d->intf;
233992b3987SAntti Palosaari int ret;
234992b3987SAntti Palosaari u8 buf[3] = {0x03, 0x00, onoff ? 0x07 : 0x08};
235992b3987SAntti Palosaari
236992b3987SAntti Palosaari dev_dbg(&intf->dev, "onoff=%d\n", onoff);
237992b3987SAntti Palosaari
238992b3987SAntti Palosaari ret = zd1301_ctrl_msg(d, buf, 3, NULL, 0);
239992b3987SAntti Palosaari if (ret)
240992b3987SAntti Palosaari goto err;
241992b3987SAntti Palosaari
242992b3987SAntti Palosaari return 0;
243992b3987SAntti Palosaari err:
244992b3987SAntti Palosaari dev_dbg(&intf->dev, "failed=%d\n", ret);
245992b3987SAntti Palosaari return ret;
246992b3987SAntti Palosaari }
247992b3987SAntti Palosaari
248992b3987SAntti Palosaari static const struct dvb_usb_device_properties zd1301_props = {
249992b3987SAntti Palosaari .driver_name = KBUILD_MODNAME,
250992b3987SAntti Palosaari .owner = THIS_MODULE,
251992b3987SAntti Palosaari .adapter_nr = adapter_nr,
252992b3987SAntti Palosaari .size_of_priv = sizeof(struct zd1301_dev),
253992b3987SAntti Palosaari
254992b3987SAntti Palosaari .frontend_attach = zd1301_frontend_attach,
255992b3987SAntti Palosaari .frontend_detach = zd1301_frontend_detach,
256992b3987SAntti Palosaari .streaming_ctrl = zd1301_streaming_ctrl,
257992b3987SAntti Palosaari
258992b3987SAntti Palosaari .num_adapters = 1,
259992b3987SAntti Palosaari .adapter = {
260992b3987SAntti Palosaari {
261992b3987SAntti Palosaari .stream = DVB_USB_STREAM_BULK(0x81, 6, 21 * 188),
262992b3987SAntti Palosaari },
263992b3987SAntti Palosaari },
264992b3987SAntti Palosaari };
265992b3987SAntti Palosaari
266992b3987SAntti Palosaari static const struct usb_device_id zd1301_id_table[] = {
267992b3987SAntti Palosaari {DVB_USB_DEVICE(USB_VID_ZYDAS, 0x13a1, &zd1301_props,
268992b3987SAntti Palosaari "ZyDAS ZD1301 reference design", NULL)},
269992b3987SAntti Palosaari {}
270992b3987SAntti Palosaari };
271992b3987SAntti Palosaari MODULE_DEVICE_TABLE(usb, zd1301_id_table);
272992b3987SAntti Palosaari
273992b3987SAntti Palosaari /* Usb specific object needed to register this driver with the usb subsystem */
274992b3987SAntti Palosaari static struct usb_driver zd1301_usb_driver = {
275992b3987SAntti Palosaari .name = KBUILD_MODNAME,
276992b3987SAntti Palosaari .id_table = zd1301_id_table,
277992b3987SAntti Palosaari .probe = dvb_usbv2_probe,
278992b3987SAntti Palosaari .disconnect = dvb_usbv2_disconnect,
279992b3987SAntti Palosaari .suspend = dvb_usbv2_suspend,
280992b3987SAntti Palosaari .resume = dvb_usbv2_resume,
281992b3987SAntti Palosaari .reset_resume = dvb_usbv2_reset_resume,
282992b3987SAntti Palosaari .no_dynamic_id = 1,
283992b3987SAntti Palosaari .soft_unbind = 1,
284992b3987SAntti Palosaari };
285992b3987SAntti Palosaari module_usb_driver(zd1301_usb_driver);
286992b3987SAntti Palosaari
287992b3987SAntti Palosaari MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
288992b3987SAntti Palosaari MODULE_DESCRIPTION("ZyDAS ZD1301 driver");
289992b3987SAntti Palosaari MODULE_LICENSE("GPL");
290