1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/media/i2c/smiapp/smiapp-regs.c
5 * Generic driver for SMIA/SMIA++ compliant camera modules
7 * Copyright (C) 2011--2012 Nokia Corporation
8 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
15 #include "smiapp-regs.h"
17 static uint32_t float_to_u32_mul_1000000(struct i2c_client
*client
,
23 if (phloat
>= 0x80000000) {
24 dev_err(&client
->dev
, "this is a negative number\n");
28 if (phloat
== 0x7f800000)
31 if ((phloat
& 0x7f800000) == 0x7f800000) {
32 dev_err(&client
->dev
, "NaN or other special number\n");
36 /* Valid cases begin here */
38 return 0; /* Valid zero */
40 if (phloat
> 0x4f800000)
41 return ~0; /* larger than 4294967295 */
44 * Unbias exponent (note how phloat is now guaranteed to
45 * have 0 in the high bit)
47 exp
= ((int32_t)phloat
>> 23) - 127;
49 /* Extract mantissa, add missing '1' bit and it's in MHz */
50 man
= ((phloat
& 0x7fffff) | 0x800000) * 1000000ULL;
57 man
>>= 23; /* Remove mantissa bias */
59 return man
& 0xffffffff;
64 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
65 * Returns zero if successful, or non-zero otherwise.
67 static int ____smiapp_read(struct smiapp_sensor
*sensor
, u16 reg
,
70 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
72 unsigned char data
[4];
76 msg
.addr
= client
->addr
;
81 /* high byte goes out first */
82 data
[0] = (u8
) (offset
>> 8);
83 data
[1] = (u8
) offset
;
84 r
= i2c_transfer(client
->adapter
, &msg
, 1);
93 r
= i2c_transfer(client
->adapter
, &msg
, 1);
101 /* high byte comes first */
103 case SMIAPP_REG_32BIT
:
104 *val
= (data
[0] << 24) + (data
[1] << 16) + (data
[2] << 8) +
107 case SMIAPP_REG_16BIT
:
108 *val
= (data
[0] << 8) + data
[1];
110 case SMIAPP_REG_8BIT
:
120 dev_err(&client
->dev
, "read from offset 0x%x error %d\n", offset
, r
);
125 /* Read a register using 8-bit access only. */
126 static int ____smiapp_read_8only(struct smiapp_sensor
*sensor
, u16 reg
,
134 for (i
= 0; i
< len
; i
++) {
137 rval
= ____smiapp_read(sensor
, reg
+ i
, 1, &val8
);
140 *val
|= val8
<< ((len
- i
- 1) << 3);
147 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
148 * Returns zero if successful, or non-zero otherwise.
150 static int __smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
,
153 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
154 u8 len
= SMIAPP_REG_WIDTH(reg
);
157 if (len
!= SMIAPP_REG_8BIT
&& len
!= SMIAPP_REG_16BIT
158 && len
!= SMIAPP_REG_32BIT
)
161 if (len
== SMIAPP_REG_8BIT
|| !only8
)
162 rval
= ____smiapp_read(sensor
, SMIAPP_REG_ADDR(reg
), len
, val
);
164 rval
= ____smiapp_read_8only(sensor
, SMIAPP_REG_ADDR(reg
), len
,
169 if (reg
& SMIAPP_REG_FLAG_FLOAT
)
170 *val
= float_to_u32_mul_1000000(client
, *val
);
175 int smiapp_read_no_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
177 return __smiapp_read(
179 smiapp_needs_quirk(sensor
,
180 SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY
));
183 static int smiapp_read_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
,
189 rval
= smiapp_call_quirk(sensor
, reg_access
, false, ®
, val
);
190 if (rval
== -ENOIOCTLCMD
)
196 return __smiapp_read(sensor
, reg
, val
, true);
198 return smiapp_read_no_quirk(sensor
, reg
, val
);
201 int smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
203 return smiapp_read_quirk(sensor
, reg
, val
, false);
206 int smiapp_read_8only(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
208 return smiapp_read_quirk(sensor
, reg
, val
, true);
211 int smiapp_write_no_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32 val
)
213 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
215 unsigned char data
[6];
216 unsigned int retries
;
217 u8 flags
= SMIAPP_REG_FLAGS(reg
);
218 u8 len
= SMIAPP_REG_WIDTH(reg
);
219 u16 offset
= SMIAPP_REG_ADDR(reg
);
222 if ((len
!= SMIAPP_REG_8BIT
&& len
!= SMIAPP_REG_16BIT
&&
223 len
!= SMIAPP_REG_32BIT
) || flags
)
229 msg
.addr
= client
->addr
;
230 msg
.flags
= 0; /* Write */
234 /* high byte goes out first */
235 data
[0] = (u8
) (reg
>> 8);
236 data
[1] = (u8
) (reg
& 0xff);
239 case SMIAPP_REG_8BIT
:
242 case SMIAPP_REG_16BIT
:
246 case SMIAPP_REG_32BIT
:
256 for (retries
= 0; retries
< 5; retries
++) {
258 * Due to unknown reason sensor stops responding. This
259 * loop is a temporaty solution until the root cause
262 r
= i2c_transfer(client
->adapter
, &msg
, 1);
265 dev_err(&client
->dev
,
266 "sensor i2c stall encountered. retries: %d\n",
271 usleep_range(2000, 2000);
274 dev_err(&client
->dev
,
275 "wrote 0x%x to offset 0x%x error %d\n", val
, offset
, r
);
281 * Write to a 8/16-bit register.
282 * Returns zero if successful, or non-zero otherwise.
284 int smiapp_write(struct smiapp_sensor
*sensor
, u32 reg
, u32 val
)
288 rval
= smiapp_call_quirk(sensor
, reg_access
, true, ®
, &val
);
289 if (rval
== -ENOIOCTLCMD
)
294 return smiapp_write_no_quirk(sensor
, reg
, val
);