[media] si2157: add read data support for fw cmd func
[pandora-kernel.git] / drivers / media / tuners / si2157.c
1 /*
2  * Silicon Labs Si2157 silicon tuner driver
3  *
4  * Copyright (C) 2014 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 "si2157_priv.h"
18
19 /* execute firmware command */
20 static int si2157_cmd_execute(struct si2157 *s, struct si2157_cmd *cmd)
21 {
22         int ret;
23         unsigned long timeout;
24
25         mutex_lock(&s->i2c_mutex);
26
27         if (cmd->wlen) {
28                 /* write cmd and args for firmware */
29                 ret = i2c_master_send(s->client, cmd->args, cmd->wlen);
30                 if (ret < 0) {
31                         goto err_mutex_unlock;
32                 } else if (ret != cmd->wlen) {
33                         ret = -EREMOTEIO;
34                         goto err_mutex_unlock;
35                 }
36         }
37
38         if (cmd->rlen) {
39                 /* wait cmd execution terminate */
40                 #define TIMEOUT 80
41                 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
42                 while (!time_after(jiffies, timeout)) {
43                         ret = i2c_master_recv(s->client, cmd->args, cmd->rlen);
44                         if (ret < 0) {
45                                 goto err_mutex_unlock;
46                         } else if (ret != cmd->rlen) {
47                                 ret = -EREMOTEIO;
48                                 goto err_mutex_unlock;
49                         }
50
51                         /* firmware ready? */
52                         if ((cmd->args[0] >> 7) & 0x01)
53                                 break;
54                 }
55
56                 dev_dbg(&s->client->dev, "%s: cmd execution took %d ms\n",
57                                 __func__,
58                                 jiffies_to_msecs(jiffies) -
59                                 (jiffies_to_msecs(timeout) - TIMEOUT));
60
61                 if (!((cmd->args[0] >> 7) & 0x01)) {
62                         ret = -ETIMEDOUT;
63                         goto err_mutex_unlock;
64                 }
65         }
66
67         ret = 0;
68
69 err_mutex_unlock:
70         mutex_unlock(&s->i2c_mutex);
71         if (ret)
72                 goto err;
73
74         return 0;
75 err:
76         dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
77         return ret;
78 }
79
80 static int si2157_init(struct dvb_frontend *fe)
81 {
82         struct si2157 *s = fe->tuner_priv;
83
84         dev_dbg(&s->client->dev, "%s:\n", __func__);
85
86         s->active = true;
87
88         return 0;
89 }
90
91 static int si2157_sleep(struct dvb_frontend *fe)
92 {
93         struct si2157 *s = fe->tuner_priv;
94         int ret;
95         struct si2157_cmd cmd;
96
97         dev_dbg(&s->client->dev, "%s:\n", __func__);
98
99         s->active = false;
100
101         memcpy(cmd.args, "\x13", 1);
102         cmd.wlen = 1;
103         cmd.rlen = 0;
104         ret = si2157_cmd_execute(s, &cmd);
105         if (ret)
106                 goto err;
107
108         return 0;
109 err:
110         dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
111         return ret;
112 }
113
114 static int si2157_set_params(struct dvb_frontend *fe)
115 {
116         struct si2157 *s = fe->tuner_priv;
117         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
118         int ret;
119         struct si2157_cmd cmd;
120
121         dev_dbg(&s->client->dev,
122                         "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n",
123                         __func__, c->delivery_system, c->frequency,
124                         c->bandwidth_hz);
125
126         if (!s->active) {
127                 ret = -EAGAIN;
128                 goto err;
129         }
130
131         /* configure? */
132         cmd.args[0] = 0xc0;
133         cmd.args[1] = 0x00;
134         cmd.args[2] = 0x0c;
135         cmd.args[3] = 0x00;
136         cmd.args[4] = 0x00;
137         cmd.args[5] = 0x01;
138         cmd.args[6] = 0x01;
139         cmd.args[7] = 0x01;
140         cmd.args[8] = 0x01;
141         cmd.args[9] = 0x01;
142         cmd.args[10] = 0x01;
143         cmd.args[11] = 0x02;
144         cmd.args[12] = 0x00;
145         cmd.args[13] = 0x00;
146         cmd.args[14] = 0x01;
147         cmd.wlen = 15;
148         cmd.rlen = 1;
149         ret = si2157_cmd_execute(s, &cmd);
150         if (ret)
151                 goto err;
152
153         cmd.args[0] = 0x02;
154         cmd.wlen = 1;
155         cmd.rlen = 13;
156         ret = si2157_cmd_execute(s, &cmd);
157         if (ret)
158                 goto err;
159
160         cmd.args[0] = 0x01;
161         cmd.args[1] = 0x01;
162         cmd.wlen = 2;
163         cmd.rlen = 1;
164         ret = si2157_cmd_execute(s, &cmd);
165         if (ret)
166                 goto err;
167
168         /* set frequency */
169         cmd.args[0] = 0x41;
170         cmd.args[1] = 0x00;
171         cmd.args[2] = 0x00;
172         cmd.args[3] = 0x00;
173         cmd.args[4] = (c->frequency >>  0) & 0xff;
174         cmd.args[5] = (c->frequency >>  8) & 0xff;
175         cmd.args[6] = (c->frequency >> 16) & 0xff;
176         cmd.args[7] = (c->frequency >> 24) & 0xff;
177         cmd.wlen = 8;
178         cmd.rlen = 1;
179         ret = si2157_cmd_execute(s, &cmd);
180         if (ret)
181                 goto err;
182
183         return 0;
184 err:
185         dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
186         return ret;
187 }
188
189 static const struct dvb_tuner_ops si2157_tuner_ops = {
190         .info = {
191                 .name           = "Silicon Labs Si2157",
192                 .frequency_min  = 110000000,
193                 .frequency_max  = 862000000,
194         },
195
196         .init = si2157_init,
197         .sleep = si2157_sleep,
198         .set_params = si2157_set_params,
199 };
200
201 static int si2157_probe(struct i2c_client *client,
202                 const struct i2c_device_id *id)
203 {
204         struct si2157_config *cfg = client->dev.platform_data;
205         struct dvb_frontend *fe = cfg->fe;
206         struct si2157 *s;
207         struct si2157_cmd cmd;
208         int ret;
209
210         s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
211         if (!s) {
212                 ret = -ENOMEM;
213                 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
214                 goto err;
215         }
216
217         s->client = client;
218         s->fe = cfg->fe;
219         mutex_init(&s->i2c_mutex);
220
221         /* check if the tuner is there */
222         cmd.wlen = 0;
223         cmd.rlen = 1;
224         ret = si2157_cmd_execute(s, &cmd);
225         if (ret)
226                 goto err;
227
228         fe->tuner_priv = s;
229         memcpy(&fe->ops.tuner_ops, &si2157_tuner_ops,
230                         sizeof(struct dvb_tuner_ops));
231
232         i2c_set_clientdata(client, s);
233
234         dev_info(&s->client->dev,
235                         "%s: Silicon Labs Si2157 successfully attached\n",
236                         KBUILD_MODNAME);
237         return 0;
238 err:
239         dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
240         kfree(s);
241
242         return ret;
243 }
244
245 static int si2157_remove(struct i2c_client *client)
246 {
247         struct si2157 *s = i2c_get_clientdata(client);
248         struct dvb_frontend *fe = s->fe;
249
250         dev_dbg(&client->dev, "%s:\n", __func__);
251
252         memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
253         fe->tuner_priv = NULL;
254         kfree(s);
255
256         return 0;
257 }
258
259 static const struct i2c_device_id si2157_id[] = {
260         {"si2157", 0},
261         {}
262 };
263 MODULE_DEVICE_TABLE(i2c, si2157_id);
264
265 static struct i2c_driver si2157_driver = {
266         .driver = {
267                 .owner  = THIS_MODULE,
268                 .name   = "si2157",
269         },
270         .probe          = si2157_probe,
271         .remove         = si2157_remove,
272         .id_table       = si2157_id,
273 };
274
275 module_i2c_driver(si2157_driver);
276
277 MODULE_DESCRIPTION("Silicon Labs Si2157 silicon tuner driver");
278 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
279 MODULE_LICENSE("GPL");