2 * drivers/media/video/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@maxwell.research.nokia.com>
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 "smiapp-debug.h"
27 #include <linux/delay.h>
28 #include <linux/i2c.h>
30 #include "smiapp-regs.h"
32 static uint32_t float_to_u32_mul_1000000(struct i2c_client
*client
,
38 if (phloat
>= 0x80000000) {
39 dev_err(&client
->dev
, "this is a negative number\n");
43 if (phloat
== 0x7f800000)
46 if ((phloat
& 0x7f800000) == 0x7f800000) {
47 dev_err(&client
->dev
, "NaN or other special number\n");
51 /* Valid cases begin here */
53 return 0; /* Valid zero */
55 if (phloat
> 0x4f800000)
56 return ~0; /* larger than 4294967295 */
59 * Unbias exponent (note how phloat is now guaranteed to
60 * have 0 in the high bit)
62 exp
= ((int32_t)phloat
>> 23) - 127;
64 /* Extract mantissa, add missing '1' bit and it's in MHz */
65 man
= ((phloat
& 0x7fffff) | 0x800000) * 1000000ULL;
72 man
>>= 23; /* Remove mantissa bias */
74 return man
& 0xffffffff;
79 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
80 * Returns zero if successful, or non-zero otherwise.
82 int smiapp_read(struct i2c_client
*client
, u32 reg
, u32
*val
)
85 unsigned char data
[4];
86 unsigned int len
= (u8
)(reg
>> 16);
90 if (len
!= SMIA_REG_8BIT
&& len
!= SMIA_REG_16BIT
91 && len
!= SMIA_REG_32BIT
)
94 msg
.addr
= client
->addr
;
99 /* high byte goes out first */
100 data
[0] = (u8
) (offset
>> 8);
101 data
[1] = (u8
) offset
;
102 r
= i2c_transfer(client
->adapter
, &msg
, 1);
110 msg
.flags
= I2C_M_RD
;
111 r
= i2c_transfer(client
->adapter
, &msg
, 1);
119 /* high byte comes first */
122 *val
= (data
[0] << 24) + (data
[1] << 16) + (data
[2] << 8) +
126 *val
= (data
[0] << 8) + data
[1];
135 if (reg
& SMIA_REG_FLAG_FLOAT
)
136 *val
= float_to_u32_mul_1000000(client
, *val
);
141 dev_err(&client
->dev
, "read from offset 0x%x error %d\n", offset
, r
);
147 * Write to a 8/16-bit register.
148 * Returns zero if successful, or non-zero otherwise.
150 int smiapp_write(struct i2c_client
*client
, u32 reg
, u32 val
)
153 unsigned char data
[6];
154 unsigned int retries
;
155 unsigned int flags
= reg
>> 24;
156 unsigned int len
= (u8
)(reg
>> 16);
160 if ((len
!= SMIA_REG_8BIT
&& len
!= SMIA_REG_16BIT
&&
161 len
!= SMIA_REG_32BIT
) || flags
)
164 msg
.addr
= client
->addr
;
165 msg
.flags
= 0; /* Write */
169 /* high byte goes out first */
170 data
[0] = (u8
) (reg
>> 8);
171 data
[1] = (u8
) (reg
& 0xff);
191 for (retries
= 0; retries
< 5; retries
++) {
193 * Due to unknown reason sensor stops responding. This
194 * loop is a temporaty solution until the root cause
197 r
= i2c_transfer(client
->adapter
, &msg
, 1);
200 dev_err(&client
->dev
,
201 "sensor i2c stall encountered. "
202 "retries: %d\n", retries
);
206 usleep_range(2000, 2000);
209 dev_err(&client
->dev
,
210 "wrote 0x%x to offset 0x%x error %d\n", val
, offset
, r
);
This page took 0.0647219999999999 seconds and 5 git commands to generate.