Linux 3.11-rc3
[cris-mirror.git] / drivers / media / usb / stk1160 / stk1160-i2c.c
blob850cf285ada8d97e2f2348da39f139d041d6404b
1 /*
2 * STK1160 driver
4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
7 * Based on Easycap driver by R.M. Thomas
8 * Copyright (C) 2010 R.M. Thomas
9 * <rmthomas--a.t--sciolus.org>
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.
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.
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/i2c.h>
27 #include "stk1160.h"
28 #include "stk1160-reg.h"
30 static unsigned int i2c_debug;
31 module_param(i2c_debug, int, 0644);
32 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
34 #define dprintk_i2c(fmt, args...) \
35 do { \
36 if (i2c_debug) \
37 printk(KERN_DEBUG fmt, ##args); \
38 } while (0)
40 static int stk1160_i2c_busy_wait(struct stk1160 *dev, u8 wait_bit_mask)
42 unsigned long end;
43 u8 flag;
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)) {
49 stk1160_read_reg(dev, STK1160_SICTL+1, &flag);
50 /* read/write done? */
51 if (flag & wait_bit_mask)
52 goto done;
54 usleep_range(10 * USEC_PER_MSEC, 20 * USEC_PER_MSEC);
57 return -ETIMEDOUT;
59 done:
60 return 0;
63 static int stk1160_i2c_write_reg(struct stk1160 *dev, u8 addr,
64 u8 reg, u8 value)
66 int rc;
68 /* Set serial device address */
69 rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
70 if (rc < 0)
71 return rc;
73 /* Set i2c device register sub-address */
74 rc = stk1160_write_reg(dev, STK1160_SBUSW_WA, reg);
75 if (rc < 0)
76 return rc;
78 /* Set i2c device register value */
79 rc = stk1160_write_reg(dev, STK1160_SBUSW_WD, value);
80 if (rc < 0)
81 return rc;
83 /* Start write now */
84 rc = stk1160_write_reg(dev, STK1160_SICTL, 0x01);
85 if (rc < 0)
86 return rc;
88 rc = stk1160_i2c_busy_wait(dev, 0x04);
89 if (rc < 0)
90 return rc;
92 return 0;
95 static int stk1160_i2c_read_reg(struct stk1160 *dev, u8 addr,
96 u8 reg, u8 *value)
98 int rc;
100 /* Set serial device address */
101 rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
102 if (rc < 0)
103 return rc;
105 /* Set i2c device register sub-address */
106 rc = stk1160_write_reg(dev, STK1160_SBUSR_RA, reg);
107 if (rc < 0)
108 return rc;
110 /* Start read now */
111 rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20);
112 if (rc < 0)
113 return rc;
115 rc = stk1160_i2c_busy_wait(dev, 0x01);
116 if (rc < 0)
117 return rc;
119 rc = stk1160_read_reg(dev, STK1160_SBUSR_RD, value);
120 if (rc < 0)
121 return rc;
123 return 0;
127 * stk1160_i2c_check_for_device()
128 * check if there is a i2c_device at the supplied address
130 static int stk1160_i2c_check_for_device(struct stk1160 *dev,
131 unsigned char addr)
133 int rc;
135 /* Set serial device address */
136 rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
137 if (rc < 0)
138 return rc;
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;
145 /* Start read now */
146 rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20);
147 if (rc < 0)
148 return rc;
150 rc = stk1160_i2c_busy_wait(dev, 0x01);
151 if (rc < 0)
152 return -ENODEV;
154 return 0;
158 * stk1160_i2c_xfer()
159 * the main i2c transfer function
161 static int stk1160_i2c_xfer(struct i2c_adapter *i2c_adap,
162 struct i2c_msg msgs[], int num)
164 struct stk1160 *dev = i2c_adap->algo_data;
165 int addr, rc, i;
167 for (i = 0; i < num; i++) {
168 addr = msgs[i].addr << 1;
169 dprintk_i2c("%s: addr=%x", __func__, addr);
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;
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;
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) {
189 if (msgs[i].len != 1 || msgs[i + 1].len != 1) {
190 dprintk_i2c(" len not supported");
191 rc = -EOPNOTSUPP;
192 goto err;
195 dprintk_i2c(" subaddr=%x", msgs[i].buf[0]);
197 rc = stk1160_i2c_read_reg(dev, addr, msgs[i].buf[0],
198 msgs[i + 1].buf);
200 dprintk_i2c(" read=%x", *msgs[i + 1].buf);
202 /* consumed two msgs, so we skip one of them */
203 i++;
205 } else {
206 if (msgs[i].len != 2) {
207 dprintk_i2c(" len not supported");
208 rc = -EOPNOTSUPP;
209 goto err;
212 dprintk_i2c(" subaddr=%x write=%x",
213 msgs[i].buf[0], msgs[i].buf[1]);
215 rc = stk1160_i2c_write_reg(dev, addr, msgs[i].buf[0],
216 msgs[i].buf[1]);
219 if (rc < 0)
220 goto err;
221 dprintk_i2c(" OK\n");
224 return num;
225 err:
226 dprintk_i2c(" ERROR: %d\n", rc);
227 return num;
231 * functionality(), what da heck is this?
233 static u32 functionality(struct i2c_adapter *adap)
235 return I2C_FUNC_SMBUS_EMUL;
238 static struct i2c_algorithm algo = {
239 .master_xfer = stk1160_i2c_xfer,
240 .functionality = functionality,
243 static struct i2c_adapter adap_template = {
244 .owner = THIS_MODULE,
245 .name = "stk1160",
246 .algo = &algo,
249 static struct i2c_client client_template = {
250 .name = "stk1160 internal",
254 * stk1160_i2c_register()
255 * register i2c bus
257 int stk1160_i2c_register(struct stk1160 *dev)
259 int rc;
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;
266 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
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;
274 dev->i2c_client = client_template;
275 dev->i2c_client.adapter = &dev->i2c_adap;
277 /* Set i2c clock divider device address */
278 stk1160_write_reg(dev, STK1160_SICTL_CD, 0x0f);
280 /* ??? */
281 stk1160_write_reg(dev, STK1160_ASIC + 3, 0x00);
283 return 0;
287 * stk1160_i2c_unregister()
288 * unregister i2c_bus
290 int stk1160_i2c_unregister(struct stk1160 *dev)
292 i2c_del_adapter(&dev->i2c_adap);
293 return 0;