xref: /linux/drivers/media/dvb-frontends/a8293.c (revision 005438a8eef063495ac059d128eea71b58de50e5)
1 /*
2  * Allegro A8293 SEC driver
3  *
4  * Copyright (C) 2011 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  *    You should have received a copy of the GNU General Public License along
17  *    with this program; if not, write to the Free Software Foundation, Inc.,
18  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include "dvb_frontend.h"
22 #include "a8293.h"
23 
24 struct a8293_priv {
25 	u8 i2c_addr;
26 	struct i2c_adapter *i2c;
27 	struct i2c_client *client;
28 	u8 reg[2];
29 };
30 
31 static int a8293_i2c(struct a8293_priv *priv, u8 *val, int len, bool rd)
32 {
33 	int ret;
34 	struct i2c_msg msg[1] = {
35 		{
36 			.addr = priv->i2c_addr,
37 			.len = len,
38 			.buf = val,
39 		}
40 	};
41 
42 	if (rd)
43 		msg[0].flags = I2C_M_RD;
44 	else
45 		msg[0].flags = 0;
46 
47 	ret = i2c_transfer(priv->i2c, msg, 1);
48 	if (ret == 1) {
49 		ret = 0;
50 	} else {
51 		dev_warn(&priv->i2c->dev, "%s: i2c failed=%d rd=%d\n",
52 				KBUILD_MODNAME, ret, rd);
53 		ret = -EREMOTEIO;
54 	}
55 
56 	return ret;
57 }
58 
59 static int a8293_wr(struct a8293_priv *priv, u8 *val, int len)
60 {
61 	return a8293_i2c(priv, val, len, 0);
62 }
63 
64 static int a8293_rd(struct a8293_priv *priv, u8 *val, int len)
65 {
66 	return a8293_i2c(priv, val, len, 1);
67 }
68 
69 static int a8293_set_voltage(struct dvb_frontend *fe,
70 	enum fe_sec_voltage fe_sec_voltage)
71 {
72 	struct a8293_priv *priv = fe->sec_priv;
73 	int ret;
74 
75 	dev_dbg(&priv->i2c->dev, "%s: fe_sec_voltage=%d\n", __func__,
76 			fe_sec_voltage);
77 
78 	switch (fe_sec_voltage) {
79 	case SEC_VOLTAGE_OFF:
80 		/* ENB=0 */
81 		priv->reg[0] = 0x10;
82 		break;
83 	case SEC_VOLTAGE_13:
84 		/* VSEL0=1, VSEL1=0, VSEL2=0, VSEL3=0, ENB=1*/
85 		priv->reg[0] = 0x31;
86 		break;
87 	case SEC_VOLTAGE_18:
88 		/* VSEL0=0, VSEL1=0, VSEL2=0, VSEL3=1, ENB=1*/
89 		priv->reg[0] = 0x38;
90 		break;
91 	default:
92 		ret = -EINVAL;
93 		goto err;
94 	}
95 
96 	ret = a8293_wr(priv, &priv->reg[0], 1);
97 	if (ret)
98 		goto err;
99 
100 	usleep_range(1500, 50000);
101 
102 	return ret;
103 err:
104 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
105 	return ret;
106 }
107 
108 static void a8293_release_sec(struct dvb_frontend *fe)
109 {
110 	a8293_set_voltage(fe, SEC_VOLTAGE_OFF);
111 
112 	kfree(fe->sec_priv);
113 	fe->sec_priv = NULL;
114 }
115 
116 struct dvb_frontend *a8293_attach(struct dvb_frontend *fe,
117 	struct i2c_adapter *i2c, const struct a8293_config *cfg)
118 {
119 	int ret;
120 	struct a8293_priv *priv = NULL;
121 	u8 buf[2];
122 
123 	/* allocate memory for the internal priv */
124 	priv = kzalloc(sizeof(struct a8293_priv), GFP_KERNEL);
125 	if (priv == NULL) {
126 		ret = -ENOMEM;
127 		goto err;
128 	}
129 
130 	/* setup the priv */
131 	priv->i2c = i2c;
132 	priv->i2c_addr = cfg->i2c_addr;
133 	fe->sec_priv = priv;
134 
135 	/* check if the SEC is there */
136 	ret = a8293_rd(priv, buf, 2);
137 	if (ret)
138 		goto err;
139 
140 	/* ENB=0 */
141 	priv->reg[0] = 0x10;
142 	ret = a8293_wr(priv, &priv->reg[0], 1);
143 	if (ret)
144 		goto err;
145 
146 	/* TMODE=0, TGATE=1 */
147 	priv->reg[1] = 0x82;
148 	ret = a8293_wr(priv, &priv->reg[1], 1);
149 	if (ret)
150 		goto err;
151 
152 	fe->ops.release_sec = a8293_release_sec;
153 
154 	/* override frontend ops */
155 	fe->ops.set_voltage = a8293_set_voltage;
156 
157 	dev_info(&priv->i2c->dev, "%s: Allegro A8293 SEC attached\n",
158 			KBUILD_MODNAME);
159 
160 	return fe;
161 err:
162 	dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
163 	kfree(priv);
164 	return NULL;
165 }
166 EXPORT_SYMBOL(a8293_attach);
167 
168 static int a8293_probe(struct i2c_client *client,
169 			const struct i2c_device_id *id)
170 {
171 	struct a8293_priv *dev;
172 	struct a8293_platform_data *pdata = client->dev.platform_data;
173 	struct dvb_frontend *fe = pdata->dvb_frontend;
174 	int ret;
175 	u8 buf[2];
176 
177 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
178 	if (!dev) {
179 		ret = -ENOMEM;
180 		goto err;
181 	}
182 
183 	dev->client = client;
184 	dev->i2c = client->adapter;
185 	dev->i2c_addr = client->addr;
186 
187 	/* check if the SEC is there */
188 	ret = a8293_rd(dev, buf, 2);
189 	if (ret)
190 		goto err_kfree;
191 
192 	/* ENB=0 */
193 	dev->reg[0] = 0x10;
194 	ret = a8293_wr(dev, &dev->reg[0], 1);
195 	if (ret)
196 		goto err_kfree;
197 
198 	/* TMODE=0, TGATE=1 */
199 	dev->reg[1] = 0x82;
200 	ret = a8293_wr(dev, &dev->reg[1], 1);
201 	if (ret)
202 		goto err_kfree;
203 
204 	/* override frontend ops */
205 	fe->ops.set_voltage = a8293_set_voltage;
206 
207 	fe->sec_priv = dev;
208 	i2c_set_clientdata(client, dev);
209 
210 	dev_info(&client->dev, "Allegro A8293 SEC successfully attached\n");
211 	return 0;
212 err_kfree:
213 	kfree(dev);
214 err:
215 	dev_dbg(&client->dev, "failed=%d\n", ret);
216 	return ret;
217 }
218 
219 static int a8293_remove(struct i2c_client *client)
220 {
221 	struct a8293_dev *dev = i2c_get_clientdata(client);
222 
223 	dev_dbg(&client->dev, "\n");
224 
225 	kfree(dev);
226 	return 0;
227 }
228 
229 static const struct i2c_device_id a8293_id_table[] = {
230 	{"a8293", 0},
231 	{}
232 };
233 MODULE_DEVICE_TABLE(i2c, a8293_id_table);
234 
235 static struct i2c_driver a8293_driver = {
236 	.driver = {
237 		.owner	= THIS_MODULE,
238 		.name	= "a8293",
239 		.suppress_bind_attrs = true,
240 	},
241 	.probe		= a8293_probe,
242 	.remove		= a8293_remove,
243 	.id_table	= a8293_id_table,
244 };
245 
246 module_i2c_driver(a8293_driver);
247 
248 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
249 MODULE_DESCRIPTION("Allegro A8293 SEC driver");
250 MODULE_LICENSE("GPL");
251