dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / media / i2c / smiapp / smiapp-regs.c
blob6b6c20b6139788353112aab5a6cd0c1dc17ce6ad
1 /*
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>
22 #include "smiapp.h"
23 #include "smiapp-regs.h"
25 static uint32_t float_to_u32_mul_1000000(struct i2c_client *client,
26 uint32_t phloat)
28 int32_t exp;
29 uint64_t man;
31 if (phloat >= 0x80000000) {
32 dev_err(&client->dev, "this is a negative number\n");
33 return 0;
36 if (phloat == 0x7f800000)
37 return ~0; /* Inf. */
39 if ((phloat & 0x7f800000) == 0x7f800000) {
40 dev_err(&client->dev, "NaN or other special number\n");
41 return 0;
44 /* Valid cases begin here */
45 if (phloat == 0)
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;
60 if (exp < 0)
61 man >>= -exp;
62 else
63 man <<= exp;
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,
76 u16 len, u32 *val)
78 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
79 struct i2c_msg msg;
80 unsigned char data[4];
81 u16 offset = reg;
82 int r;
84 msg.addr = client->addr;
85 msg.flags = 0;
86 msg.len = 2;
87 msg.buf = data;
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);
93 if (r != 1) {
94 if (r >= 0)
95 r = -EBUSY;
96 goto err;
99 msg.len = len;
100 msg.flags = I2C_M_RD;
101 r = i2c_transfer(client->adapter, &msg, 1);
102 if (r != 1) {
103 if (r >= 0)
104 r = -EBUSY;
105 goto err;
108 *val = 0;
109 /* high byte comes first */
110 switch (len) {
111 case SMIAPP_REG_32BIT:
112 *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) +
113 data[3];
114 break;
115 case SMIAPP_REG_16BIT:
116 *val = (data[0] << 8) + data[1];
117 break;
118 case SMIAPP_REG_8BIT:
119 *val = data[0];
120 break;
121 default:
122 BUG();
125 return 0;
127 err:
128 dev_err(&client->dev, "read from offset 0x%x error %d\n", offset, r);
130 return r;
133 /* Read a register using 8-bit access only. */
134 static int ____smiapp_read_8only(struct smiapp_sensor *sensor, u16 reg,
135 u16 len, u32 *val)
137 unsigned int i;
138 int rval;
140 *val = 0;
142 for (i = 0; i < len; i++) {
143 u32 val8;
145 rval = ____smiapp_read(sensor, reg + i, 1, &val8);
146 if (rval < 0)
147 return rval;
148 *val |= val8 << ((len - i - 1) << 3);
151 return 0;
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,
159 bool only8)
161 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
162 u8 len = SMIAPP_REG_WIDTH(reg);
163 int rval;
165 if (len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT
166 && len != SMIAPP_REG_32BIT)
167 return -EINVAL;
169 if (len == SMIAPP_REG_8BIT || !only8)
170 rval = ____smiapp_read(sensor, SMIAPP_REG_ADDR(reg), len, val);
171 else
172 rval = ____smiapp_read_8only(sensor, SMIAPP_REG_ADDR(reg), len,
173 val);
174 if (rval < 0)
175 return rval;
177 if (reg & SMIAPP_REG_FLAG_FLOAT)
178 *val = float_to_u32_mul_1000000(client, *val);
180 return 0;
183 int smiapp_read_no_quirk(struct smiapp_sensor *sensor, u32 reg, u32 *val)
185 return __smiapp_read(
186 sensor, reg, val,
187 smiapp_needs_quirk(sensor,
188 SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY));
191 int smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val)
193 int rval;
195 *val = 0;
196 rval = smiapp_call_quirk(sensor, reg_access, false, &reg, val);
197 if (rval == -ENOIOCTLCMD)
198 return 0;
199 if (rval < 0)
200 return rval;
202 return smiapp_read_no_quirk(sensor, reg, val);
205 int smiapp_read_8only(struct smiapp_sensor *sensor, u32 reg, u32 *val)
207 int rval;
209 *val = 0;
210 rval = smiapp_call_quirk(sensor, reg_access, false, &reg, val);
211 if (rval == -ENOIOCTLCMD)
212 return 0;
213 if (rval < 0)
214 return rval;
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);
222 struct i2c_msg msg;
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);
228 int r;
230 if ((len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT &&
231 len != SMIAPP_REG_32BIT) || flags)
232 return -EINVAL;
234 msg.addr = client->addr;
235 msg.flags = 0; /* Write */
236 msg.len = 2 + len;
237 msg.buf = data;
239 /* high byte goes out first */
240 data[0] = (u8) (reg >> 8);
241 data[1] = (u8) (reg & 0xff);
243 switch (len) {
244 case SMIAPP_REG_8BIT:
245 data[2] = val;
246 break;
247 case SMIAPP_REG_16BIT:
248 data[2] = val >> 8;
249 data[3] = val;
250 break;
251 case SMIAPP_REG_32BIT:
252 data[2] = val >> 24;
253 data[3] = val >> 16;
254 data[4] = val >> 8;
255 data[5] = val;
256 break;
257 default:
258 BUG();
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
265 * is found.
267 r = i2c_transfer(client->adapter, &msg, 1);
268 if (r == 1) {
269 if (retries)
270 dev_err(&client->dev,
271 "sensor i2c stall encountered. "
272 "retries: %d\n", retries);
273 return 0;
276 usleep_range(2000, 2000);
279 dev_err(&client->dev,
280 "wrote 0x%x to offset 0x%x error %d\n", val, offset, r);
282 return 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)
291 int rval;
293 rval = smiapp_call_quirk(sensor, reg_access, true, &reg, &val);
294 if (rval == -ENOIOCTLCMD)
295 return 0;
296 if (rval < 0)
297 return rval;
299 return smiapp_write_no_quirk(sensor, reg, val);