Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | cb7a01a | 2012-08-14 16:23:43 -0300 | [diff] [blame] | 2 | * drivers/media/i2c/smiapp/smiapp-regs.c |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 3 | * |
| 4 | * Generic driver for SMIA/SMIA++ compliant camera modules |
| 5 | * |
| 6 | * Copyright (C) 2011--2012 Nokia Corporation |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 7 | * Contact: Sakari Ailus <sakari.ailus@iki.fi> |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 8 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 |
| 22 | * |
| 23 | */ |
| 24 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 25 | #include <linux/delay.h> |
| 26 | #include <linux/i2c.h> |
| 27 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 28 | #include "smiapp.h" |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 29 | #include "smiapp-regs.h" |
| 30 | |
| 31 | static uint32_t float_to_u32_mul_1000000(struct i2c_client *client, |
| 32 | uint32_t phloat) |
| 33 | { |
| 34 | int32_t exp; |
| 35 | uint64_t man; |
| 36 | |
| 37 | if (phloat >= 0x80000000) { |
| 38 | dev_err(&client->dev, "this is a negative number\n"); |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | if (phloat == 0x7f800000) |
| 43 | return ~0; /* Inf. */ |
| 44 | |
| 45 | if ((phloat & 0x7f800000) == 0x7f800000) { |
| 46 | dev_err(&client->dev, "NaN or other special number\n"); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* Valid cases begin here */ |
| 51 | if (phloat == 0) |
| 52 | return 0; /* Valid zero */ |
| 53 | |
| 54 | if (phloat > 0x4f800000) |
| 55 | return ~0; /* larger than 4294967295 */ |
| 56 | |
| 57 | /* |
| 58 | * Unbias exponent (note how phloat is now guaranteed to |
| 59 | * have 0 in the high bit) |
| 60 | */ |
| 61 | exp = ((int32_t)phloat >> 23) - 127; |
| 62 | |
| 63 | /* Extract mantissa, add missing '1' bit and it's in MHz */ |
| 64 | man = ((phloat & 0x7fffff) | 0x800000) * 1000000ULL; |
| 65 | |
| 66 | if (exp < 0) |
| 67 | man >>= -exp; |
| 68 | else |
| 69 | man <<= exp; |
| 70 | |
| 71 | man >>= 23; /* Remove mantissa bias */ |
| 72 | |
| 73 | return man & 0xffffffff; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * Read a 8/16/32-bit i2c register. The value is returned in 'val'. |
| 79 | * Returns zero if successful, or non-zero otherwise. |
| 80 | */ |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 81 | static int ____smiapp_read(struct smiapp_sensor *sensor, u16 reg, |
| 82 | u16 len, u32 *val) |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 83 | { |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 84 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 85 | struct i2c_msg msg; |
| 86 | unsigned char data[4]; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 87 | u16 offset = reg; |
| 88 | int r; |
| 89 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 90 | msg.addr = client->addr; |
| 91 | msg.flags = 0; |
| 92 | msg.len = 2; |
| 93 | msg.buf = data; |
| 94 | |
| 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; |
| 103 | } |
| 104 | |
| 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; |
| 112 | } |
| 113 | |
| 114 | *val = 0; |
| 115 | /* high byte comes first */ |
| 116 | switch (len) { |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 117 | case SMIAPP_REG_32BIT: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 118 | *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + |
| 119 | data[3]; |
| 120 | break; |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 121 | case SMIAPP_REG_16BIT: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 122 | *val = (data[0] << 8) + data[1]; |
| 123 | break; |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 124 | case SMIAPP_REG_8BIT: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 125 | *val = data[0]; |
| 126 | break; |
| 127 | default: |
| 128 | BUG(); |
| 129 | } |
| 130 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 131 | return 0; |
| 132 | |
| 133 | err: |
| 134 | dev_err(&client->dev, "read from offset 0x%x error %d\n", offset, r); |
| 135 | |
| 136 | return r; |
| 137 | } |
| 138 | |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 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) |
| 142 | { |
| 143 | unsigned int i; |
| 144 | int rval; |
| 145 | |
| 146 | *val = 0; |
| 147 | |
| 148 | for (i = 0; i < len; i++) { |
| 149 | u32 val8; |
| 150 | |
| 151 | rval = ____smiapp_read(sensor, reg + i, 1, &val8); |
| 152 | if (rval < 0) |
| 153 | return rval; |
| 154 | *val |= val8 << ((len - i - 1) << 3); |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Read a 8/16/32-bit i2c register. The value is returned in 'val'. |
| 162 | * Returns zero if successful, or non-zero otherwise. |
| 163 | */ |
| 164 | static int __smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val, |
| 165 | bool only8) |
| 166 | { |
| 167 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 168 | unsigned int len = (u8)(reg >> 16); |
| 169 | int rval; |
| 170 | |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 171 | if (len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT |
| 172 | && len != SMIAPP_REG_32BIT) |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 173 | return -EINVAL; |
| 174 | |
Sakari Ailus | b05cd21 | 2014-04-12 16:41:12 -0300 | [diff] [blame] | 175 | if (len == SMIAPP_REG_8BIT || !only8) |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 176 | rval = ____smiapp_read(sensor, (u16)reg, len, val); |
| 177 | else |
| 178 | rval = ____smiapp_read_8only(sensor, (u16)reg, len, val); |
| 179 | if (rval < 0) |
| 180 | return rval; |
| 181 | |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 182 | if (reg & SMIAPP_REG_FLAG_FLOAT) |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 183 | *val = float_to_u32_mul_1000000(client, *val); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Sakari Ailus | 6f7481b | 2014-04-01 08:37:38 -0300 | [diff] [blame^] | 188 | int smiapp_read_no_quirk(struct smiapp_sensor *sensor, u32 reg, u32 *val) |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 189 | { |
| 190 | return __smiapp_read( |
| 191 | sensor, reg, val, |
| 192 | smiapp_needs_quirk(sensor, |
| 193 | SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY)); |
| 194 | } |
| 195 | |
Sakari Ailus | 6f7481b | 2014-04-01 08:37:38 -0300 | [diff] [blame^] | 196 | int smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val) |
| 197 | { |
| 198 | int rval; |
| 199 | |
| 200 | *val = 0; |
| 201 | rval = smiapp_call_quirk(sensor, reg_access, false, ®, val); |
| 202 | if (rval == -ENOIOCTLCMD) |
| 203 | return 0; |
| 204 | if (rval < 0) |
| 205 | return rval; |
| 206 | |
| 207 | return smiapp_read_no_quirk(sensor, reg, val); |
| 208 | } |
| 209 | |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 210 | int smiapp_read_8only(struct smiapp_sensor *sensor, u32 reg, u32 *val) |
| 211 | { |
Sakari Ailus | 6f7481b | 2014-04-01 08:37:38 -0300 | [diff] [blame^] | 212 | int rval; |
| 213 | |
| 214 | *val = 0; |
| 215 | rval = smiapp_call_quirk(sensor, reg_access, false, ®, val); |
| 216 | if (rval == -ENOIOCTLCMD) |
| 217 | return 0; |
| 218 | if (rval < 0) |
| 219 | return rval; |
| 220 | |
Sakari Ailus | ceb9e30 | 2012-04-22 09:11:26 -0300 | [diff] [blame] | 221 | return __smiapp_read(sensor, reg, val, true); |
| 222 | } |
| 223 | |
Sakari Ailus | 6f7481b | 2014-04-01 08:37:38 -0300 | [diff] [blame^] | 224 | int smiapp_write_no_quirk(struct smiapp_sensor *sensor, u32 reg, u32 val) |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 225 | { |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 226 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 227 | struct i2c_msg msg; |
| 228 | unsigned char data[6]; |
| 229 | unsigned int retries; |
| 230 | unsigned int flags = reg >> 24; |
| 231 | unsigned int len = (u8)(reg >> 16); |
| 232 | u16 offset = reg; |
| 233 | int r; |
| 234 | |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 235 | if ((len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT && |
| 236 | len != SMIAPP_REG_32BIT) || flags) |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 237 | return -EINVAL; |
| 238 | |
| 239 | msg.addr = client->addr; |
| 240 | msg.flags = 0; /* Write */ |
| 241 | msg.len = 2 + len; |
| 242 | msg.buf = data; |
| 243 | |
| 244 | /* high byte goes out first */ |
| 245 | data[0] = (u8) (reg >> 8); |
| 246 | data[1] = (u8) (reg & 0xff); |
| 247 | |
| 248 | switch (len) { |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 249 | case SMIAPP_REG_8BIT: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 250 | data[2] = val; |
| 251 | break; |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 252 | case SMIAPP_REG_16BIT: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 253 | data[2] = val >> 8; |
| 254 | data[3] = val; |
| 255 | break; |
Sakari Ailus | 6fcc7a5 | 2014-04-11 05:47:21 -0300 | [diff] [blame] | 256 | case SMIAPP_REG_32BIT: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 257 | data[2] = val >> 24; |
| 258 | data[3] = val >> 16; |
| 259 | data[4] = val >> 8; |
| 260 | data[5] = val; |
| 261 | break; |
| 262 | default: |
| 263 | BUG(); |
| 264 | } |
| 265 | |
| 266 | for (retries = 0; retries < 5; retries++) { |
| 267 | /* |
| 268 | * Due to unknown reason sensor stops responding. This |
| 269 | * loop is a temporaty solution until the root cause |
| 270 | * is found. |
| 271 | */ |
| 272 | r = i2c_transfer(client->adapter, &msg, 1); |
| 273 | if (r == 1) { |
| 274 | if (retries) |
| 275 | dev_err(&client->dev, |
| 276 | "sensor i2c stall encountered. " |
| 277 | "retries: %d\n", retries); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | usleep_range(2000, 2000); |
| 282 | } |
| 283 | |
| 284 | dev_err(&client->dev, |
| 285 | "wrote 0x%x to offset 0x%x error %d\n", val, offset, r); |
| 286 | |
| 287 | return r; |
| 288 | } |
Sakari Ailus | 6f7481b | 2014-04-01 08:37:38 -0300 | [diff] [blame^] | 289 | |
| 290 | /* |
| 291 | * Write to a 8/16-bit register. |
| 292 | * Returns zero if successful, or non-zero otherwise. |
| 293 | */ |
| 294 | int smiapp_write(struct smiapp_sensor *sensor, u32 reg, u32 val) |
| 295 | { |
| 296 | int rval; |
| 297 | |
| 298 | rval = smiapp_call_quirk(sensor, reg_access, true, ®, &val); |
| 299 | if (rval == -ENOIOCTLCMD) |
| 300 | return 0; |
| 301 | if (rval < 0) |
| 302 | return rval; |
| 303 | |
| 304 | return smiapp_write_no_quirk(sensor, reg, val); |
| 305 | } |