1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2012 Ezequiel Garcia
6 * <elezegarcia--a.t--gmail.com>
8 * Based on Easycap driver by R.M. Thomas
9 * Copyright (C) 2010 R.M. Thomas
10 * <rmthomas--a.t--sciolus.org>
13 #include <linux/module.h>
14 #include <linux/usb.h>
15 #include <linux/i2c.h>
18 #include "stk1160-reg.h"
20 static unsigned int i2c_debug
;
21 module_param(i2c_debug
, int, 0644);
22 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
24 #define dprintk_i2c(fmt, args...) \
27 printk(KERN_DEBUG fmt, ##args); \
30 static int stk1160_i2c_busy_wait(struct stk1160
*dev
, u8 wait_bit_mask
)
35 /* Wait until read/write finish bit is set */
36 end
= jiffies
+ msecs_to_jiffies(STK1160_I2C_TIMEOUT
);
37 while (time_is_after_jiffies(end
)) {
39 stk1160_read_reg(dev
, STK1160_SICTL
+1, &flag
);
40 /* read/write done? */
41 if (flag
& wait_bit_mask
)
44 usleep_range(10 * USEC_PER_MSEC
, 20 * USEC_PER_MSEC
);
53 static int stk1160_i2c_write_reg(struct stk1160
*dev
, u8 addr
,
58 /* Set serial device address */
59 rc
= stk1160_write_reg(dev
, STK1160_SICTL_SDA
, addr
);
63 /* Set i2c device register sub-address */
64 rc
= stk1160_write_reg(dev
, STK1160_SBUSW_WA
, reg
);
68 /* Set i2c device register value */
69 rc
= stk1160_write_reg(dev
, STK1160_SBUSW_WD
, value
);
74 rc
= stk1160_write_reg(dev
, STK1160_SICTL
, 0x01);
78 rc
= stk1160_i2c_busy_wait(dev
, 0x04);
85 static int stk1160_i2c_read_reg(struct stk1160
*dev
, u8 addr
,
90 /* Set serial device address */
91 rc
= stk1160_write_reg(dev
, STK1160_SICTL_SDA
, addr
);
95 /* Set i2c device register sub-address */
96 rc
= stk1160_write_reg(dev
, STK1160_SBUSR_RA
, reg
);
101 rc
= stk1160_write_reg(dev
, STK1160_SICTL
, 0x20);
105 rc
= stk1160_i2c_busy_wait(dev
, 0x01);
109 rc
= stk1160_read_reg(dev
, STK1160_SBUSR_RD
, value
);
117 * stk1160_i2c_check_for_device()
118 * check if there is a i2c_device at the supplied address
120 static int stk1160_i2c_check_for_device(struct stk1160
*dev
,
125 /* Set serial device address */
126 rc
= stk1160_write_reg(dev
, STK1160_SICTL_SDA
, addr
);
130 /* Set device sub-address, we'll chip version reg */
131 rc
= stk1160_write_reg(dev
, STK1160_SBUSR_RA
, 0x00);
136 rc
= stk1160_write_reg(dev
, STK1160_SICTL
, 0x20);
140 rc
= stk1160_i2c_busy_wait(dev
, 0x01);
149 * the main i2c transfer function
151 static int stk1160_i2c_xfer(struct i2c_adapter
*i2c_adap
,
152 struct i2c_msg msgs
[], int num
)
154 struct stk1160
*dev
= i2c_adap
->algo_data
;
157 for (i
= 0; i
< num
; i
++) {
158 addr
= msgs
[i
].addr
<< 1;
159 dprintk_i2c("%s: addr=%x", __func__
, addr
);
162 /* no len: check only for device presence */
163 rc
= stk1160_i2c_check_for_device(dev
, addr
);
165 dprintk_i2c(" no device\n");
169 } else if (msgs
[i
].flags
& I2C_M_RD
) {
170 /* read request without preceding register selection */
171 dprintk_i2c(" subaddr not selected");
175 } else if (i
+ 1 < num
&& msgs
[i
].len
<= 2 &&
176 (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
177 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
179 if (msgs
[i
].len
!= 1 || msgs
[i
+ 1].len
!= 1) {
180 dprintk_i2c(" len not supported");
185 dprintk_i2c(" subaddr=%x", msgs
[i
].buf
[0]);
187 rc
= stk1160_i2c_read_reg(dev
, addr
, msgs
[i
].buf
[0],
190 dprintk_i2c(" read=%x", *msgs
[i
+ 1].buf
);
192 /* consumed two msgs, so we skip one of them */
196 if (msgs
[i
].len
!= 2) {
197 dprintk_i2c(" len not supported");
202 dprintk_i2c(" subaddr=%x write=%x",
203 msgs
[i
].buf
[0], msgs
[i
].buf
[1]);
205 rc
= stk1160_i2c_write_reg(dev
, addr
, msgs
[i
].buf
[0],
211 dprintk_i2c(" OK\n");
216 dprintk_i2c(" ERROR: %d\n", rc
);
221 * functionality(), what da heck is this?
223 static u32
functionality(struct i2c_adapter
*adap
)
225 return I2C_FUNC_SMBUS_EMUL
;
228 static const struct i2c_algorithm algo
= {
229 .master_xfer
= stk1160_i2c_xfer
,
230 .functionality
= functionality
,
233 static const struct i2c_adapter adap_template
= {
234 .owner
= THIS_MODULE
,
239 static const struct i2c_client client_template
= {
240 .name
= "stk1160 internal",
244 * stk1160_i2c_register()
247 int stk1160_i2c_register(struct stk1160
*dev
)
251 dev
->i2c_adap
= adap_template
;
252 dev
->i2c_adap
.dev
.parent
= dev
->dev
;
253 strscpy(dev
->i2c_adap
.name
, "stk1160", sizeof(dev
->i2c_adap
.name
));
254 dev
->i2c_adap
.algo_data
= dev
;
256 i2c_set_adapdata(&dev
->i2c_adap
, &dev
->v4l2_dev
);
258 rc
= i2c_add_adapter(&dev
->i2c_adap
);
260 stk1160_err("cannot add i2c adapter (%d)\n", rc
);
264 dev
->i2c_client
= client_template
;
265 dev
->i2c_client
.adapter
= &dev
->i2c_adap
;
267 /* Set i2c clock divider device address */
268 stk1160_write_reg(dev
, STK1160_SICTL_CD
, 0x0f);
271 stk1160_write_reg(dev
, STK1160_ASIC
+ 3, 0x00);
277 * stk1160_i2c_unregister()
280 int stk1160_i2c_unregister(struct stk1160
*dev
)
282 i2c_del_adapter(&dev
->i2c_adap
);