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 <asm/unaligned.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
17 #include "smiapp-regs.h"
19 static uint32_t float_to_u32_mul_1000000(struct i2c_client
*client
,
25 if (phloat
>= 0x80000000) {
26 dev_err(&client
->dev
, "this is a negative number\n");
30 if (phloat
== 0x7f800000)
33 if ((phloat
& 0x7f800000) == 0x7f800000) {
34 dev_err(&client
->dev
, "NaN or other special number\n");
38 /* Valid cases begin here */
40 return 0; /* Valid zero */
42 if (phloat
> 0x4f800000)
43 return ~0; /* larger than 4294967295 */
46 * Unbias exponent (note how phloat is now guaranteed to
47 * have 0 in the high bit)
49 exp
= ((int32_t)phloat
>> 23) - 127;
51 /* Extract mantissa, add missing '1' bit and it's in MHz */
52 man
= ((phloat
& 0x7fffff) | 0x800000) * 1000000ULL;
59 man
>>= 23; /* Remove mantissa bias */
61 return man
& 0xffffffff;
66 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
67 * Returns zero if successful, or non-zero otherwise.
69 static int ____smiapp_read(struct smiapp_sensor
*sensor
, u16 reg
,
72 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
74 unsigned char data_buf
[sizeof(u32
)] = { 0 };
75 unsigned char offset_buf
[sizeof(u16
)];
78 if (len
> sizeof(data_buf
))
81 msg
.addr
= client
->addr
;
83 msg
.len
= sizeof(offset_buf
);
85 put_unaligned_be16(reg
, offset_buf
);
87 r
= i2c_transfer(client
->adapter
, &msg
, 1);
96 msg
.buf
= &data_buf
[sizeof(data_buf
) - len
];
98 r
= i2c_transfer(client
->adapter
, &msg
, 1);
105 *val
= get_unaligned_be32(data_buf
);
110 dev_err(&client
->dev
, "read from offset 0x%x error %d\n", reg
, r
);
115 /* Read a register using 8-bit access only. */
116 static int ____smiapp_read_8only(struct smiapp_sensor
*sensor
, u16 reg
,
124 for (i
= 0; i
< len
; i
++) {
127 rval
= ____smiapp_read(sensor
, reg
+ i
, 1, &val8
);
130 *val
|= val8
<< ((len
- i
- 1) << 3);
137 * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
138 * Returns zero if successful, or non-zero otherwise.
140 static int __smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
,
143 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
144 u8 len
= SMIAPP_REG_WIDTH(reg
);
147 if (len
!= SMIAPP_REG_8BIT
&& len
!= SMIAPP_REG_16BIT
148 && len
!= SMIAPP_REG_32BIT
)
152 rval
= ____smiapp_read(sensor
, SMIAPP_REG_ADDR(reg
), len
, val
);
154 rval
= ____smiapp_read_8only(sensor
, SMIAPP_REG_ADDR(reg
), len
,
159 if (reg
& SMIAPP_REG_FLAG_FLOAT
)
160 *val
= float_to_u32_mul_1000000(client
, *val
);
165 int smiapp_read_no_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
167 return __smiapp_read(
169 smiapp_needs_quirk(sensor
,
170 SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY
));
173 static int smiapp_read_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
,
179 rval
= smiapp_call_quirk(sensor
, reg_access
, false, ®
, val
);
180 if (rval
== -ENOIOCTLCMD
)
186 return __smiapp_read(sensor
, reg
, val
, true);
188 return smiapp_read_no_quirk(sensor
, reg
, val
);
191 int smiapp_read(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
193 return smiapp_read_quirk(sensor
, reg
, val
, false);
196 int smiapp_read_8only(struct smiapp_sensor
*sensor
, u32 reg
, u32
*val
)
198 return smiapp_read_quirk(sensor
, reg
, val
, true);
201 int smiapp_write_no_quirk(struct smiapp_sensor
*sensor
, u32 reg
, u32 val
)
203 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
205 unsigned char data
[6];
206 unsigned int retries
;
207 u8 len
= SMIAPP_REG_WIDTH(reg
);
210 if (len
> sizeof(data
) - 2)
213 msg
.addr
= client
->addr
;
214 msg
.flags
= 0; /* Write */
218 put_unaligned_be16(SMIAPP_REG_ADDR(reg
), data
);
219 put_unaligned_be32(val
<< (8 * (sizeof(val
) - len
)), data
+ 2);
221 for (retries
= 0; retries
< 5; retries
++) {
223 * Due to unknown reason sensor stops responding. This
224 * loop is a temporaty solution until the root cause
227 r
= i2c_transfer(client
->adapter
, &msg
, 1);
230 dev_err(&client
->dev
,
231 "sensor i2c stall encountered. retries: %d\n",
236 usleep_range(2000, 2000);
239 dev_err(&client
->dev
,
240 "wrote 0x%x to offset 0x%x error %d\n", val
,
241 SMIAPP_REG_ADDR(reg
), r
);
247 * Write to a 8/16-bit register.
248 * Returns zero if successful, or non-zero otherwise.
250 int smiapp_write(struct smiapp_sensor
*sensor
, u32 reg
, u32 val
)
254 rval
= smiapp_call_quirk(sensor
, reg_access
, true, ®
, &val
);
255 if (rval
== -ENOIOCTLCMD
)
260 return smiapp_write_no_quirk(sensor
, reg
, val
);