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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/delay.h>
26 #include <linux/i2c.h>
29 #include "smiapp-regs.h"
31 static uint32_t float_to_u32_mul_1000000(struct i2c_client
*client
,
37 if (phloat
>= 0x80000000) {
38 dev_err(&client
->dev
, "this is a negative number\n");
42 if (phloat
== 0x7f800000)
45 if ((phloat
& 0x7f800000) == 0x7f800000) {
46 dev_err(&client
->dev
, "NaN or other special number\n");
50 /* Valid cases begin here */
52 return 0; /* Valid zero */
54 if (phloat
> 0x4f800000)
55 return ~0; /* larger than 4294967295 */
58 * Unbias exponent (note how phloat is now guaranteed to
59 * have 0 in the high bit)
61 exp
= ((int32_t)phloat
>> 23) - 127;
63 /* Extract mantissa, add missing '1' bit and it's in MHz */
64 man
= ((phloat
& 0x7fffff) | 0x800000) * 1000000ULL;
71 man
>>= 23; /* Remove mantissa bias */
73 return man
& 0xffffffff;
78 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
79 * Returns zero if successful, or non-zero otherwise.
81 static int ____smiapp_read(struct smiapp_sensor
*sensor
, u16 reg
,
84 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
86 unsigned char data
[4];
90 msg
.addr
= client
->addr
;
95 /* high byte goes out first */
96 data
[0] = (u8
) (offset
>> 8);
97 data
[1] = (u8
) offset
;
98 r
= i2c_transfer(client
->adapter
, &msg
, 1);
106 msg
.flags
= I2C_M_RD
;
107 r
= i2c_transfer(client
->adapter
, &msg
, 1);
115 /* high byte comes first */
118 *val
= (data
[0] << 24) + (data
[1] << 16) + (data
[2] << 8) +
122 *val
= (data
[0] << 8) + data
[1];
134 dev_err(&client
->dev
, "read from offset 0x%x error %d\n", offset
, r
);
139 /* Read a register using 8-bit access only. */
140 static int ____smiapp_read_8only(struct smiapp_sensor
*sensor
, u16 reg
,
148 for (i
= 0; i
< len
; i
++) {
151 rval
= ____smiapp_read(sensor
, reg
+ i
, 1, &val8
);
154 *val
|= val8
<< ((len
- i
- 1) << 3);
161 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
162 * Returns zero if successful, or non-zero otherwise.
164 static int __smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
,
167 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
168 unsigned int len
= (u8
)(reg
>> 16);
171 if (len
!= SMIA_REG_8BIT
&& len
!= SMIA_REG_16BIT
172 && len
!= SMIA_REG_32BIT
)
175 if (smiapp_quirk_reg(sensor
, reg
, val
))
178 if (len
== SMIA_REG_8BIT
&& !only8
)
179 rval
= ____smiapp_read(sensor
, (u16
)reg
, len
, val
);
181 rval
= ____smiapp_read_8only(sensor
, (u16
)reg
, len
, val
);
186 if (reg
& SMIA_REG_FLAG_FLOAT
)
187 *val
= float_to_u32_mul_1000000(client
, *val
);
192 int smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
194 return __smiapp_read(
196 smiapp_needs_quirk(sensor
,
197 SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY
));
200 int smiapp_read_8only(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
202 return __smiapp_read(sensor
, reg
, val
, true);
206 * Write to a 8/16-bit register.
207 * Returns zero if successful, or non-zero otherwise.
209 int smiapp_write(struct smiapp_sensor
*sensor
, u32 reg
, u32 val
)
211 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
213 unsigned char data
[6];
214 unsigned int retries
;
215 unsigned int flags
= reg
>> 24;
216 unsigned int len
= (u8
)(reg
>> 16);
220 if ((len
!= SMIA_REG_8BIT
&& len
!= SMIA_REG_16BIT
&&
221 len
!= SMIA_REG_32BIT
) || flags
)
224 msg
.addr
= client
->addr
;
225 msg
.flags
= 0; /* Write */
229 /* high byte goes out first */
230 data
[0] = (u8
) (reg
>> 8);
231 data
[1] = (u8
) (reg
& 0xff);
251 for (retries
= 0; retries
< 5; retries
++) {
253 * Due to unknown reason sensor stops responding. This
254 * loop is a temporaty solution until the root cause
257 r
= i2c_transfer(client
->adapter
, &msg
, 1);
260 dev_err(&client
->dev
,
261 "sensor i2c stall encountered. "
262 "retries: %d\n", retries
);
266 usleep_range(2000, 2000);
269 dev_err(&client
->dev
,
270 "wrote 0x%x to offset 0x%x error %d\n", val
, offset
, r
);