1 /*
2  * STK1160 driver
3  *
4  * Copyright (C) 2012 Ezequiel Garcia
5  * <elezegarcia--a.t--gmail.com>
6  *
7  * Based on Easycap driver by R.M. Thomas
8  *	Copyright (C) 2010 R.M. Thomas
9  *	<rmthomas--a.t--sciolus.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  */
22 
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/i2c.h>
26 
27 #include "stk1160.h"
28 #include "stk1160-reg.h"
29 
30 static unsigned int i2c_debug;
31 module_param(i2c_debug, int, 0644);
32 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
33 
34 #define dprintk_i2c(fmt, args...)				\
35 do {								\
36 	if (i2c_debug)						\
37 		printk(KERN_DEBUG fmt, ##args);			\
38 } while (0)
39 
40 static int stk1160_i2c_busy_wait(struct stk1160 *dev, u8 wait_bit_mask)
41 {
42 	unsigned long end;
43 	u8 flag;
44 
45 	/* Wait until read/write finish bit is set */
46 	end = jiffies + msecs_to_jiffies(STK1160_I2C_TIMEOUT);
47 	while (time_is_after_jiffies(end)) {
48 
49 		stk1160_read_reg(dev, STK1160_SICTL+1, &flag);
50 		/* read/write done? */
51 		if (flag & wait_bit_mask)
52 			goto done;
53 
54 		usleep_range(10 * USEC_PER_MSEC, 20 * USEC_PER_MSEC);
55 	}
56 
57 	return -ETIMEDOUT;
58 
59 done:
60 	return 0;
61 }
62 
63 static int stk1160_i2c_write_reg(struct stk1160 *dev, u8 addr,
64 		u8 reg, u8 value)
65 {
66 	int rc;
67 
68 	/* Set serial device address */
69 	rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
70 	if (rc < 0)
71 		return rc;
72 
73 	/* Set i2c device register sub-address */
74 	rc = stk1160_write_reg(dev, STK1160_SBUSW_WA, reg);
75 	if (rc < 0)
76 		return rc;
77 
78 	/* Set i2c device register value */
79 	rc = stk1160_write_reg(dev, STK1160_SBUSW_WD, value);
80 	if (rc < 0)
81 		return rc;
82 
83 	/* Start write now */
84 	rc = stk1160_write_reg(dev, STK1160_SICTL, 0x01);
85 	if (rc < 0)
86 		return rc;
87 
88 	rc = stk1160_i2c_busy_wait(dev, 0x04);
89 	if (rc < 0)
90 		return rc;
91 
92 	return 0;
93 }
94 
95 static int stk1160_i2c_read_reg(struct stk1160 *dev, u8 addr,
96 		u8 reg, u8 *value)
97 {
98 	int rc;
99 
100 	/* Set serial device address */
101 	rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
102 	if (rc < 0)
103 		return rc;
104 
105 	/* Set i2c device register sub-address */
106 	rc = stk1160_write_reg(dev, STK1160_SBUSR_RA, reg);
107 	if (rc < 0)
108 		return rc;
109 
110 	/* Start read now */
111 	rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20);
112 	if (rc < 0)
113 		return rc;
114 
115 	rc = stk1160_i2c_busy_wait(dev, 0x01);
116 	if (rc < 0)
117 		return rc;
118 
119 	rc = stk1160_read_reg(dev, STK1160_SBUSR_RD, value);
120 	if (rc < 0)
121 		return rc;
122 
123 	return 0;
124 }
125 
126 /*
127  * stk1160_i2c_check_for_device()
128  * check if there is a i2c_device at the supplied address
129  */
130 static int stk1160_i2c_check_for_device(struct stk1160 *dev,
131 		unsigned char addr)
132 {
133 	int rc;
134 
135 	/* Set serial device address */
136 	rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
137 	if (rc < 0)
138 		return rc;
139 
140 	/* Set device sub-address, we'll chip version reg */
141 	rc = stk1160_write_reg(dev, STK1160_SBUSR_RA, 0x00);
142 	if (rc < 0)
143 		return rc;
144 
145 	/* Start read now */
146 	rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20);
147 	if (rc < 0)
148 		return rc;
149 
150 	rc = stk1160_i2c_busy_wait(dev, 0x01);
151 	if (rc < 0)
152 		return -ENODEV;
153 
154 	return 0;
155 }
156 
157 /*
158  * stk1160_i2c_xfer()
159  * the main i2c transfer function
160  */
161 static int stk1160_i2c_xfer(struct i2c_adapter *i2c_adap,
162 			   struct i2c_msg msgs[], int num)
163 {
164 	struct stk1160 *dev = i2c_adap->algo_data;
165 	int addr, rc, i;
166 
167 	for (i = 0; i < num; i++) {
168 		addr = msgs[i].addr << 1;
169 		dprintk_i2c("%s: addr=%x", __func__, addr);
170 
171 		if (!msgs[i].len) {
172 			/* no len: check only for device presence */
173 			rc = stk1160_i2c_check_for_device(dev, addr);
174 			if (rc < 0) {
175 				dprintk_i2c(" no device\n");
176 				return rc;
177 			}
178 
179 		} else if (msgs[i].flags & I2C_M_RD) {
180 			/* read request without preceding register selection */
181 			dprintk_i2c(" subaddr not selected");
182 			rc = -EOPNOTSUPP;
183 			goto err;
184 
185 		} else if (i + 1 < num && msgs[i].len <= 2 &&
186 			   (msgs[i + 1].flags & I2C_M_RD) &&
187 			   msgs[i].addr == msgs[i + 1].addr) {
188 
189 			if (msgs[i].len != 1 || msgs[i + 1].len != 1) {
190 				dprintk_i2c(" len not supported");
191 				rc = -EOPNOTSUPP;
192 				goto err;
193 			}
194 
195 			dprintk_i2c(" subaddr=%x", msgs[i].buf[0]);
196 
197 			rc = stk1160_i2c_read_reg(dev, addr, msgs[i].buf[0],
198 				msgs[i + 1].buf);
199 
200 			dprintk_i2c(" read=%x", *msgs[i + 1].buf);
201 
202 			/* consumed two msgs, so we skip one of them */
203 			i++;
204 
205 		} else {
206 			if (msgs[i].len != 2) {
207 				dprintk_i2c(" len not supported");
208 				rc = -EOPNOTSUPP;
209 				goto err;
210 			}
211 
212 			dprintk_i2c(" subaddr=%x write=%x",
213 				msgs[i].buf[0],  msgs[i].buf[1]);
214 
215 			rc = stk1160_i2c_write_reg(dev, addr, msgs[i].buf[0],
216 				msgs[i].buf[1]);
217 		}
218 
219 		if (rc < 0)
220 			goto err;
221 		dprintk_i2c(" OK\n");
222 	}
223 
224 	return num;
225 err:
226 	dprintk_i2c(" ERROR: %d\n", rc);
227 	return num;
228 }
229 
230 /*
231  * functionality(), what da heck is this?
232  */
233 static u32 functionality(struct i2c_adapter *adap)
234 {
235 	return I2C_FUNC_SMBUS_EMUL;
236 }
237 
238 static const struct i2c_algorithm algo = {
239 	.master_xfer   = stk1160_i2c_xfer,
240 	.functionality = functionality,
241 };
242 
243 static const struct i2c_adapter adap_template = {
244 	.owner = THIS_MODULE,
245 	.name = "stk1160",
246 	.algo = &algo,
247 };
248 
249 static const struct i2c_client client_template = {
250 	.name = "stk1160 internal",
251 };
252 
253 /*
254  * stk1160_i2c_register()
255  * register i2c bus
256  */
257 int stk1160_i2c_register(struct stk1160 *dev)
258 {
259 	int rc;
260 
261 	dev->i2c_adap = adap_template;
262 	dev->i2c_adap.dev.parent = dev->dev;
263 	strcpy(dev->i2c_adap.name, "stk1160");
264 	dev->i2c_adap.algo_data = dev;
265 
266 	i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
267 
268 	rc = i2c_add_adapter(&dev->i2c_adap);
269 	if (rc < 0) {
270 		stk1160_err("cannot add i2c adapter (%d)\n", rc);
271 		return rc;
272 	}
273 
274 	dev->i2c_client = client_template;
275 	dev->i2c_client.adapter = &dev->i2c_adap;
276 
277 	/* Set i2c clock divider device address */
278 	stk1160_write_reg(dev, STK1160_SICTL_CD,  0x0f);
279 
280 	/* ??? */
281 	stk1160_write_reg(dev, STK1160_ASIC + 3,  0x00);
282 
283 	return 0;
284 }
285 
286 /*
287  * stk1160_i2c_unregister()
288  * unregister i2c_bus
289  */
290 int stk1160_i2c_unregister(struct stk1160 *dev)
291 {
292 	i2c_del_adapter(&dev->i2c_adap);
293 	return 0;
294 }
295