2 * drivers/media/i2c/smiapp/smiapp-regs.c
4 * Generic driver for SMIA/SMIA++ compliant camera modules
6 * Copyright (C) 2011--2012 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/delay.h>
20 #include <linux/i2c.h>
23 #include "smiapp-regs.h"
25 static uint32_t float_to_u32_mul_1000000(struct i2c_client
*client
,
31 if (phloat
>= 0x80000000) {
32 dev_err(&client
->dev
, "this is a negative number\n");
36 if (phloat
== 0x7f800000)
39 if ((phloat
& 0x7f800000) == 0x7f800000) {
40 dev_err(&client
->dev
, "NaN or other special number\n");
44 /* Valid cases begin here */
46 return 0; /* Valid zero */
48 if (phloat
> 0x4f800000)
49 return ~0; /* larger than 4294967295 */
52 * Unbias exponent (note how phloat is now guaranteed to
53 * have 0 in the high bit)
55 exp
= ((int32_t)phloat
>> 23) - 127;
57 /* Extract mantissa, add missing '1' bit and it's in MHz */
58 man
= ((phloat
& 0x7fffff) | 0x800000) * 1000000ULL;
65 man
>>= 23; /* Remove mantissa bias */
67 return man
& 0xffffffff;
72 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
73 * Returns zero if successful, or non-zero otherwise.
75 static int ____smiapp_read(struct smiapp_sensor
*sensor
, u16 reg
,
78 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
80 unsigned char data
[4];
84 msg
.addr
= client
->addr
;
89 /* high byte goes out first */
90 data
[0] = (u8
) (offset
>> 8);
91 data
[1] = (u8
) offset
;
92 r
= i2c_transfer(client
->adapter
, &msg
, 1);
100 msg
.flags
= I2C_M_RD
;
101 r
= i2c_transfer(client
->adapter
, &msg
, 1);
109 /* high byte comes first */
111 case SMIAPP_REG_32BIT
:
112 *val
= (data
[0] << 24) + (data
[1] << 16) + (data
[2] << 8) +
115 case SMIAPP_REG_16BIT
:
116 *val
= (data
[0] << 8) + data
[1];
118 case SMIAPP_REG_8BIT
:
128 dev_err(&client
->dev
, "read from offset 0x%x error %d\n", offset
, r
);
133 /* Read a register using 8-bit access only. */
134 static int ____smiapp_read_8only(struct smiapp_sensor
*sensor
, u16 reg
,
142 for (i
= 0; i
< len
; i
++) {
145 rval
= ____smiapp_read(sensor
, reg
+ i
, 1, &val8
);
148 *val
|= val8
<< ((len
- i
- 1) << 3);
155 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
156 * Returns zero if successful, or non-zero otherwise.
158 static int __smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
,
161 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
162 u8 len
= SMIAPP_REG_WIDTH(reg
);
165 if (len
!= SMIAPP_REG_8BIT
&& len
!= SMIAPP_REG_16BIT
166 && len
!= SMIAPP_REG_32BIT
)
169 if (len
== SMIAPP_REG_8BIT
|| !only8
)
170 rval
= ____smiapp_read(sensor
, SMIAPP_REG_ADDR(reg
), len
, val
);
172 rval
= ____smiapp_read_8only(sensor
, SMIAPP_REG_ADDR(reg
), len
,
177 if (reg
& SMIAPP_REG_FLAG_FLOAT
)
178 *val
= float_to_u32_mul_1000000(client
, *val
);
183 int smiapp_read_no_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
185 return __smiapp_read(
187 smiapp_needs_quirk(sensor
,
188 SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY
));
191 int smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
196 rval
= smiapp_call_quirk(sensor
, reg_access
, false, ®
, val
);
197 if (rval
== -ENOIOCTLCMD
)
202 return smiapp_read_no_quirk(sensor
, reg
, val
);
205 int smiapp_read_8only(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
210 rval
= smiapp_call_quirk(sensor
, reg_access
, false, ®
, val
);
211 if (rval
== -ENOIOCTLCMD
)
216 return __smiapp_read(sensor
, reg
, val
, true);
219 int smiapp_write_no_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32 val
)
221 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
223 unsigned char data
[6];
224 unsigned int retries
;
225 u8 flags
= SMIAPP_REG_FLAGS(reg
);
226 u8 len
= SMIAPP_REG_WIDTH(reg
);
227 u16 offset
= SMIAPP_REG_ADDR(reg
);
230 if ((len
!= SMIAPP_REG_8BIT
&& len
!= SMIAPP_REG_16BIT
&&
231 len
!= SMIAPP_REG_32BIT
) || flags
)
234 msg
.addr
= client
->addr
;
235 msg
.flags
= 0; /* Write */
239 /* high byte goes out first */
240 data
[0] = (u8
) (reg
>> 8);
241 data
[1] = (u8
) (reg
& 0xff);
244 case SMIAPP_REG_8BIT
:
247 case SMIAPP_REG_16BIT
:
251 case SMIAPP_REG_32BIT
:
261 for (retries
= 0; retries
< 5; retries
++) {
263 * Due to unknown reason sensor stops responding. This
264 * loop is a temporaty solution until the root cause
267 r
= i2c_transfer(client
->adapter
, &msg
, 1);
270 dev_err(&client
->dev
,
271 "sensor i2c stall encountered. "
272 "retries: %d\n", retries
);
276 usleep_range(2000, 2000);
279 dev_err(&client
->dev
,
280 "wrote 0x%x to offset 0x%x error %d\n", val
, offset
, r
);
286 * Write to a 8/16-bit register.
287 * Returns zero if successful, or non-zero otherwise.
289 int smiapp_write(struct smiapp_sensor
*sensor
, u32 reg
, u32 val
)
293 rval
= smiapp_call_quirk(sensor
, reg_access
, true, ®
, &val
);
294 if (rval
== -ENOIOCTLCMD
)
299 return smiapp_write_no_quirk(sensor
, reg
, val
);