Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1 | /* |
| 2 | * drivers/media/video/smiapp/smiapp-regs.c |
| 3 | * |
| 4 | * Generic driver for SMIA/SMIA++ compliant camera modules |
| 5 | * |
| 6 | * Copyright (C) 2011--2012 Nokia Corporation |
| 7 | * Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com> |
| 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 | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame^] | 81 | int smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val) |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 82 | { |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame^] | 83 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 84 | struct i2c_msg msg; |
| 85 | unsigned char data[4]; |
| 86 | unsigned int len = (u8)(reg >> 16); |
| 87 | u16 offset = reg; |
| 88 | int r; |
| 89 | |
| 90 | if (len != SMIA_REG_8BIT && len != SMIA_REG_16BIT |
| 91 | && len != SMIA_REG_32BIT) |
| 92 | return -EINVAL; |
| 93 | |
| 94 | msg.addr = client->addr; |
| 95 | msg.flags = 0; |
| 96 | msg.len = 2; |
| 97 | msg.buf = data; |
| 98 | |
| 99 | /* high byte goes out first */ |
| 100 | data[0] = (u8) (offset >> 8); |
| 101 | data[1] = (u8) offset; |
| 102 | r = i2c_transfer(client->adapter, &msg, 1); |
| 103 | if (r != 1) { |
| 104 | if (r >= 0) |
| 105 | r = -EBUSY; |
| 106 | goto err; |
| 107 | } |
| 108 | |
| 109 | msg.len = len; |
| 110 | msg.flags = I2C_M_RD; |
| 111 | r = i2c_transfer(client->adapter, &msg, 1); |
| 112 | if (r != 1) { |
| 113 | if (r >= 0) |
| 114 | r = -EBUSY; |
| 115 | goto err; |
| 116 | } |
| 117 | |
| 118 | *val = 0; |
| 119 | /* high byte comes first */ |
| 120 | switch (len) { |
| 121 | case SMIA_REG_32BIT: |
| 122 | *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + |
| 123 | data[3]; |
| 124 | break; |
| 125 | case SMIA_REG_16BIT: |
| 126 | *val = (data[0] << 8) + data[1]; |
| 127 | break; |
| 128 | case SMIA_REG_8BIT: |
| 129 | *val = data[0]; |
| 130 | break; |
| 131 | default: |
| 132 | BUG(); |
| 133 | } |
| 134 | |
| 135 | if (reg & SMIA_REG_FLAG_FLOAT) |
| 136 | *val = float_to_u32_mul_1000000(client, *val); |
| 137 | |
| 138 | return 0; |
| 139 | |
| 140 | err: |
| 141 | dev_err(&client->dev, "read from offset 0x%x error %d\n", offset, r); |
| 142 | |
| 143 | return r; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Write to a 8/16-bit register. |
| 148 | * Returns zero if successful, or non-zero otherwise. |
| 149 | */ |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame^] | 150 | int smiapp_write(struct smiapp_sensor *sensor, u32 reg, u32 val) |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 151 | { |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame^] | 152 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 153 | struct i2c_msg msg; |
| 154 | unsigned char data[6]; |
| 155 | unsigned int retries; |
| 156 | unsigned int flags = reg >> 24; |
| 157 | unsigned int len = (u8)(reg >> 16); |
| 158 | u16 offset = reg; |
| 159 | int r; |
| 160 | |
| 161 | if ((len != SMIA_REG_8BIT && len != SMIA_REG_16BIT && |
| 162 | len != SMIA_REG_32BIT) || flags) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | msg.addr = client->addr; |
| 166 | msg.flags = 0; /* Write */ |
| 167 | msg.len = 2 + len; |
| 168 | msg.buf = data; |
| 169 | |
| 170 | /* high byte goes out first */ |
| 171 | data[0] = (u8) (reg >> 8); |
| 172 | data[1] = (u8) (reg & 0xff); |
| 173 | |
| 174 | switch (len) { |
| 175 | case SMIA_REG_8BIT: |
| 176 | data[2] = val; |
| 177 | break; |
| 178 | case SMIA_REG_16BIT: |
| 179 | data[2] = val >> 8; |
| 180 | data[3] = val; |
| 181 | break; |
| 182 | case SMIA_REG_32BIT: |
| 183 | data[2] = val >> 24; |
| 184 | data[3] = val >> 16; |
| 185 | data[4] = val >> 8; |
| 186 | data[5] = val; |
| 187 | break; |
| 188 | default: |
| 189 | BUG(); |
| 190 | } |
| 191 | |
| 192 | for (retries = 0; retries < 5; retries++) { |
| 193 | /* |
| 194 | * Due to unknown reason sensor stops responding. This |
| 195 | * loop is a temporaty solution until the root cause |
| 196 | * is found. |
| 197 | */ |
| 198 | r = i2c_transfer(client->adapter, &msg, 1); |
| 199 | if (r == 1) { |
| 200 | if (retries) |
| 201 | dev_err(&client->dev, |
| 202 | "sensor i2c stall encountered. " |
| 203 | "retries: %d\n", retries); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | usleep_range(2000, 2000); |
| 208 | } |
| 209 | |
| 210 | dev_err(&client->dev, |
| 211 | "wrote 0x%x to offset 0x%x error %d\n", val, offset, r); |
| 212 | |
| 213 | return r; |
| 214 | } |