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>
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...) \
37 printk(KERN_DEBUG fmt, ##args); \
40 static int stk1160_i2c_busy_wait(struct stk1160 *dev, u8 wait_bit_mask)
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)
54 usleep_range(10 * USEC_PER_MSEC, 20 * USEC_PER_MSEC);
63 static int stk1160_i2c_write_reg(struct stk1160 *dev, u8 addr,
68 /* Set serial device address */
69 rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
73 /* Set i2c device register sub-address */
74 rc = stk1160_write_reg(dev, STK1160_SBUSW_WA, reg);
78 /* Set i2c device register value */
79 rc = stk1160_write_reg(dev, STK1160_SBUSW_WD, value);
84 rc = stk1160_write_reg(dev, STK1160_SICTL, 0x01);
88 rc = stk1160_i2c_busy_wait(dev, 0x04);
95 static int stk1160_i2c_read_reg(struct stk1160 *dev, u8 addr,
100 /* Set serial device address */
101 rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
105 /* Set i2c device register sub-address */
106 rc = stk1160_write_reg(dev, STK1160_SBUSR_RA, reg);
111 rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20);
115 rc = stk1160_i2c_busy_wait(dev, 0x01);
119 rc = stk1160_read_reg(dev, STK1160_SBUSR_RD, value);
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,
135 /* Set serial device address */
136 rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr);
140 /* Set device sub-address, we'll chip version reg */
141 rc = stk1160_write_reg(dev, STK1160_SBUSR_RA, 0x00);
146 rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20);
150 rc = stk1160_i2c_busy_wait(dev, 0x01);
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;
167 for (i = 0; i < num; i++) {
168 addr = msgs[i].addr << 1;
169 dprintk_i2c("%s: addr=%x", __func__, addr);
172 /* no len: check only for device presence */
173 rc = stk1160_i2c_check_for_device(dev, addr);
175 dprintk_i2c(" no device\n");
179 } else if (msgs[i].flags & I2C_M_RD) {
180 /* read request without preceding register selection */
181 dprintk_i2c(" subaddr not selected");
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");
195 dprintk_i2c(" subaddr=%x", msgs[i].buf[0]);
197 rc = stk1160_i2c_read_reg(dev, addr, msgs[i].buf[0],
200 dprintk_i2c(" read=%x", *msgs[i + 1].buf);
202 /* consumed two msgs, so we skip one of them */
206 if (msgs[i].len != 2) {
207 dprintk_i2c(" len not supported");
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],
221 dprintk_i2c(" OK\n");
226 dprintk_i2c(" ERROR: %d\n", rc);
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,
249 static struct i2c_client client_template = {
250 .name = "stk1160 internal",
254 * stk1160_i2c_register()
257 int stk1160_i2c_register(struct stk1160 *dev)
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);
270 stk1160_err("cannot add i2c adapter (%d)\n", 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);
281 stk1160_write_reg(dev, STK1160_ASIC + 3, 0x00);
287 * stk1160_i2c_unregister()
290 int stk1160_i2c_unregister(struct stk1160 *dev)
292 i2c_del_adapter(&dev->i2c_adap);