sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / media / i2c / smiapp / smiapp-regs.c
bloba2098007fb70cf1a8a0a872cadfe4c7aa6d5e1d5
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.
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
21 * 02110-1301 USA
25 #include <linux/delay.h>
26 #include <linux/i2c.h>
28 #include "smiapp.h"
29 #include "smiapp-regs.h"
31 static uint32_t float_to_u32_mul_1000000(struct i2c_client *client,
32 uint32_t phloat)
34 int32_t exp;
35 uint64_t man;
37 if (phloat >= 0x80000000) {
38 dev_err(&client->dev, "this is a negative number\n");
39 return 0;
42 if (phloat == 0x7f800000)
43 return ~0; /* Inf. */
45 if ((phloat & 0x7f800000) == 0x7f800000) {
46 dev_err(&client->dev, "NaN or other special number\n");
47 return 0;
50 /* Valid cases begin here */
51 if (phloat == 0)
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;
66 if (exp < 0)
67 man >>= -exp;
68 else
69 man <<= exp;
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,
82 u16 len, u32 *val)
84 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
85 struct i2c_msg msg;
86 unsigned char data[4];
87 u16 offset = reg;
88 int r;
90 msg.addr = client->addr;
91 msg.flags = 0;
92 msg.len = 2;
93 msg.buf = data;
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);
99 if (r != 1) {
100 if (r >= 0)
101 r = -EBUSY;
102 goto err;
105 msg.len = len;
106 msg.flags = I2C_M_RD;
107 r = i2c_transfer(client->adapter, &msg, 1);
108 if (r != 1) {
109 if (r >= 0)
110 r = -EBUSY;
111 goto err;
114 *val = 0;
115 /* high byte comes first */
116 switch (len) {
117 case SMIAPP_REG_32BIT:
118 *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) +
119 data[3];
120 break;
121 case SMIAPP_REG_16BIT:
122 *val = (data[0] << 8) + data[1];
123 break;
124 case SMIAPP_REG_8BIT:
125 *val = data[0];
126 break;
127 default:
128 BUG();
131 return 0;
133 err:
134 dev_err(&client->dev, "read from offset 0x%x error %d\n", offset, r);
136 return r;
139 /* Read a register using 8-bit access only. */
140 static int ____smiapp_read_8only(struct smiapp_sensor *sensor, u16 reg,
141 u16 len, u32 *val)
143 unsigned int i;
144 int rval;
146 *val = 0;
148 for (i = 0; i < len; i++) {
149 u32 val8;
151 rval = ____smiapp_read(sensor, reg + i, 1, &val8);
152 if (rval < 0)
153 return rval;
154 *val |= val8 << ((len - i - 1) << 3);
157 return 0;
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,
165 bool only8)
167 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
168 u8 len = SMIAPP_REG_WIDTH(reg);
169 int rval;
171 if (len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT
172 && len != SMIAPP_REG_32BIT)
173 return -EINVAL;
175 if (len == SMIAPP_REG_8BIT || !only8)
176 rval = ____smiapp_read(sensor, SMIAPP_REG_ADDR(reg), len, val);
177 else
178 rval = ____smiapp_read_8only(sensor, SMIAPP_REG_ADDR(reg), len,
179 val);
180 if (rval < 0)
181 return rval;
183 if (reg & SMIAPP_REG_FLAG_FLOAT)
184 *val = float_to_u32_mul_1000000(client, *val);
186 return 0;
189 int smiapp_read_no_quirk(struct smiapp_sensor *sensor, u32 reg, u32 *val)
191 return __smiapp_read(
192 sensor, reg, val,
193 smiapp_needs_quirk(sensor,
194 SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY));
197 int smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val)
199 int rval;
201 *val = 0;
202 rval = smiapp_call_quirk(sensor, reg_access, false, &reg, val);
203 if (rval == -ENOIOCTLCMD)
204 return 0;
205 if (rval < 0)
206 return rval;
208 return smiapp_read_no_quirk(sensor, reg, val);
211 int smiapp_read_8only(struct smiapp_sensor *sensor, u32 reg, u32 *val)
213 int rval;
215 *val = 0;
216 rval = smiapp_call_quirk(sensor, reg_access, false, &reg, val);
217 if (rval == -ENOIOCTLCMD)
218 return 0;
219 if (rval < 0)
220 return rval;
222 return __smiapp_read(sensor, reg, val, true);
225 int smiapp_write_no_quirk(struct smiapp_sensor *sensor, u32 reg, u32 val)
227 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
228 struct i2c_msg msg;
229 unsigned char data[6];
230 unsigned int retries;
231 u8 flags = SMIAPP_REG_FLAGS(reg);
232 u8 len = SMIAPP_REG_WIDTH(reg);
233 u16 offset = SMIAPP_REG_ADDR(reg);
234 int r;
236 if ((len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT &&
237 len != SMIAPP_REG_32BIT) || flags)
238 return -EINVAL;
240 msg.addr = client->addr;
241 msg.flags = 0; /* Write */
242 msg.len = 2 + len;
243 msg.buf = data;
245 /* high byte goes out first */
246 data[0] = (u8) (reg >> 8);
247 data[1] = (u8) (reg & 0xff);
249 switch (len) {
250 case SMIAPP_REG_8BIT:
251 data[2] = val;
252 break;
253 case SMIAPP_REG_16BIT:
254 data[2] = val >> 8;
255 data[3] = val;
256 break;
257 case SMIAPP_REG_32BIT:
258 data[2] = val >> 24;
259 data[3] = val >> 16;
260 data[4] = val >> 8;
261 data[5] = val;
262 break;
263 default:
264 BUG();
267 for (retries = 0; retries < 5; retries++) {
269 * Due to unknown reason sensor stops responding. This
270 * loop is a temporaty solution until the root cause
271 * is found.
273 r = i2c_transfer(client->adapter, &msg, 1);
274 if (r == 1) {
275 if (retries)
276 dev_err(&client->dev,
277 "sensor i2c stall encountered. "
278 "retries: %d\n", retries);
279 return 0;
282 usleep_range(2000, 2000);
285 dev_err(&client->dev,
286 "wrote 0x%x to offset 0x%x error %d\n", val, offset, r);
288 return r;
292 * Write to a 8/16-bit register.
293 * Returns zero if successful, or non-zero otherwise.
295 int smiapp_write(struct smiapp_sensor *sensor, u32 reg, u32 val)
297 int rval;
299 rval = smiapp_call_quirk(sensor, reg_access, true, &reg, &val);
300 if (rval == -ENOIOCTLCMD)
301 return 0;
302 if (rval < 0)
303 return rval;
305 return smiapp_write_no_quirk(sensor, reg, val);