sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / media / tuners / si2157.c
blobfa4cc7b880aa4774f4fdf6d7c6cf3ddbb010dbd8
1 /*
2 * Silicon Labs Si2157 silicon tuner driver
4 * Copyright (C) 2014 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.
17 #include "si2157_priv.h"
19 /* execute firmware command */
20 static int si2157_cmd_execute(struct si2157 *s, struct si2157_cmd *cmd)
22 int ret;
23 u8 buf[1];
24 unsigned long timeout;
26 mutex_lock(&s->i2c_mutex);
28 if (cmd->len) {
29 /* write cmd and args for firmware */
30 ret = i2c_master_send(s->client, cmd->args, cmd->len);
31 if (ret < 0) {
32 goto err_mutex_unlock;
33 } else if (ret != cmd->len) {
34 ret = -EREMOTEIO;
35 goto err_mutex_unlock;
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, buf, 1);
44 if (ret < 0) {
45 goto err_mutex_unlock;
46 } else if (ret != 1) {
47 ret = -EREMOTEIO;
48 goto err_mutex_unlock;
51 /* firmware ready? */
52 if ((buf[0] >> 7) & 0x01)
53 break;
56 dev_dbg(&s->client->dev, "%s: cmd execution took %d ms\n", __func__,
57 jiffies_to_msecs(jiffies) -
58 (jiffies_to_msecs(timeout) - TIMEOUT));
60 if (!((buf[0] >> 7) & 0x01)) {
61 ret = -ETIMEDOUT;
62 goto err_mutex_unlock;
63 } else {
64 ret = 0;
67 err_mutex_unlock:
68 mutex_unlock(&s->i2c_mutex);
69 if (ret)
70 goto err;
72 return 0;
73 err:
74 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
75 return ret;
78 static int si2157_init(struct dvb_frontend *fe)
80 struct si2157 *s = fe->tuner_priv;
82 dev_dbg(&s->client->dev, "%s:\n", __func__);
84 s->active = true;
86 return 0;
89 static int si2157_sleep(struct dvb_frontend *fe)
91 struct si2157 *s = fe->tuner_priv;
93 dev_dbg(&s->client->dev, "%s:\n", __func__);
95 s->active = false;
97 return 0;
100 static int si2157_set_params(struct dvb_frontend *fe)
102 struct si2157 *s = fe->tuner_priv;
103 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
104 int ret;
105 struct si2157_cmd cmd;
107 dev_dbg(&s->client->dev,
108 "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n",
109 __func__, c->delivery_system, c->frequency,
110 c->bandwidth_hz);
112 if (!s->active) {
113 ret = -EAGAIN;
114 goto err;
117 /* configure? */
118 cmd.args[0] = 0xc0;
119 cmd.args[1] = 0x00;
120 cmd.args[2] = 0x0c;
121 cmd.args[3] = 0x00;
122 cmd.args[4] = 0x00;
123 cmd.args[5] = 0x01;
124 cmd.args[6] = 0x01;
125 cmd.args[7] = 0x01;
126 cmd.args[8] = 0x01;
127 cmd.args[9] = 0x01;
128 cmd.args[10] = 0x01;
129 cmd.args[11] = 0x02;
130 cmd.args[12] = 0x00;
131 cmd.args[13] = 0x00;
132 cmd.args[14] = 0x01;
133 cmd.len = 15;
134 ret = si2157_cmd_execute(s, &cmd);
135 if (ret)
136 goto err;
138 cmd.args[0] = 0x02;
139 cmd.len = 1;
140 ret = si2157_cmd_execute(s, &cmd);
141 if (ret)
142 goto err;
144 cmd.args[0] = 0x01;
145 cmd.args[1] = 0x01;
146 cmd.len = 2;
147 ret = si2157_cmd_execute(s, &cmd);
148 if (ret)
149 goto err;
151 /* set frequency */
152 cmd.args[0] = 0x41;
153 cmd.args[1] = 0x00;
154 cmd.args[2] = 0x00;
155 cmd.args[3] = 0x00;
156 cmd.args[4] = (c->frequency >> 0) & 0xff;
157 cmd.args[5] = (c->frequency >> 8) & 0xff;
158 cmd.args[6] = (c->frequency >> 16) & 0xff;
159 cmd.args[7] = (c->frequency >> 24) & 0xff;
160 cmd.len = 8;
161 ret = si2157_cmd_execute(s, &cmd);
162 if (ret)
163 goto err;
165 return 0;
166 err:
167 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
168 return ret;
171 static const struct dvb_tuner_ops si2157_tuner_ops = {
172 .info = {
173 .name = "Silicon Labs Si2157",
174 .frequency_min = 110000000,
175 .frequency_max = 862000000,
178 .init = si2157_init,
179 .sleep = si2157_sleep,
180 .set_params = si2157_set_params,
183 static int si2157_probe(struct i2c_client *client,
184 const struct i2c_device_id *id)
186 struct si2157_config *cfg = client->dev.platform_data;
187 struct dvb_frontend *fe = cfg->fe;
188 struct si2157 *s;
189 struct si2157_cmd cmd;
190 int ret;
192 s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
193 if (!s) {
194 ret = -ENOMEM;
195 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
196 goto err;
199 s->client = client;
200 s->fe = cfg->fe;
201 mutex_init(&s->i2c_mutex);
203 /* check if the tuner is there */
204 cmd.len = 0;
205 ret = si2157_cmd_execute(s, &cmd);
206 if (ret)
207 goto err;
209 fe->tuner_priv = s;
210 memcpy(&fe->ops.tuner_ops, &si2157_tuner_ops,
211 sizeof(struct dvb_tuner_ops));
213 i2c_set_clientdata(client, s);
215 dev_info(&s->client->dev,
216 "%s: Silicon Labs Si2157 successfully attached\n",
217 KBUILD_MODNAME);
218 return 0;
219 err:
220 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
221 kfree(s);
223 return ret;
226 static int si2157_remove(struct i2c_client *client)
228 struct si2157 *s = i2c_get_clientdata(client);
229 struct dvb_frontend *fe = s->fe;
231 dev_dbg(&client->dev, "%s:\n", __func__);
233 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
234 fe->tuner_priv = NULL;
235 kfree(s);
237 return 0;
240 static const struct i2c_device_id si2157_id[] = {
241 {"si2157", 0},
244 MODULE_DEVICE_TABLE(i2c, si2157_id);
246 static struct i2c_driver si2157_driver = {
247 .driver = {
248 .owner = THIS_MODULE,
249 .name = "si2157",
251 .probe = si2157_probe,
252 .remove = si2157_remove,
253 .id_table = si2157_id,
256 module_i2c_driver(si2157_driver);
258 MODULE_DESCRIPTION("Silicon Labs Si2157 silicon tuner driver");
259 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
260 MODULE_LICENSE("GPL");