smiapp-regs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * drivers/media/i2c/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@iki.fi>
  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. #include <linux/delay.h>
  25. #include <linux/i2c.h>
  26. #include "smiapp.h"
  27. #include "smiapp-regs.h"
  28. static uint32_t float_to_u32_mul_1000000(struct i2c_client *client,
  29. uint32_t phloat)
  30. {
  31. int32_t exp;
  32. uint64_t man;
  33. if (phloat >= 0x80000000) {
  34. dev_err(&client->dev, "this is a negative number\n");
  35. return 0;
  36. }
  37. if (phloat == 0x7f800000)
  38. return ~0; /* Inf. */
  39. if ((phloat & 0x7f800000) == 0x7f800000) {
  40. dev_err(&client->dev, "NaN or other special number\n");
  41. return 0;
  42. }
  43. /* Valid cases begin here */
  44. if (phloat == 0)
  45. return 0; /* Valid zero */
  46. if (phloat > 0x4f800000)
  47. return ~0; /* larger than 4294967295 */
  48. /*
  49. * Unbias exponent (note how phloat is now guaranteed to
  50. * have 0 in the high bit)
  51. */
  52. exp = ((int32_t)phloat >> 23) - 127;
  53. /* Extract mantissa, add missing '1' bit and it's in MHz */
  54. man = ((phloat & 0x7fffff) | 0x800000) * 1000000ULL;
  55. if (exp < 0)
  56. man >>= -exp;
  57. else
  58. man <<= exp;
  59. man >>= 23; /* Remove mantissa bias */
  60. return man & 0xffffffff;
  61. }
  62. /*
  63. * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
  64. * Returns zero if successful, or non-zero otherwise.
  65. */
  66. static int ____smiapp_read(struct smiapp_sensor *sensor, u16 reg,
  67. u16 len, u32 *val)
  68. {
  69. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  70. struct i2c_msg msg;
  71. unsigned char data[4];
  72. u16 offset = reg;
  73. int r;
  74. msg.addr = client->addr;
  75. msg.flags = 0;
  76. msg.len = 2;
  77. msg.buf = data;
  78. /* high byte goes out first */
  79. data[0] = (u8) (offset >> 8);
  80. data[1] = (u8) offset;
  81. r = i2c_transfer(client->adapter, &msg, 1);
  82. if (r != 1) {
  83. if (r >= 0)
  84. r = -EBUSY;
  85. goto err;
  86. }
  87. msg.len = len;
  88. msg.flags = I2C_M_RD;
  89. r = i2c_transfer(client->adapter, &msg, 1);
  90. if (r != 1) {
  91. if (r >= 0)
  92. r = -EBUSY;
  93. goto err;
  94. }
  95. *val = 0;
  96. /* high byte comes first */
  97. switch (len) {
  98. case SMIAPP_REG_32BIT:
  99. *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) +
  100. data[3];
  101. break;
  102. case SMIAPP_REG_16BIT:
  103. *val = (data[0] << 8) + data[1];
  104. break;
  105. case SMIAPP_REG_8BIT:
  106. *val = data[0];
  107. break;
  108. default:
  109. BUG();
  110. }
  111. return 0;
  112. err:
  113. dev_err(&client->dev, "read from offset 0x%x error %d\n", offset, r);
  114. return r;
  115. }
  116. /* Read a register using 8-bit access only. */
  117. static int ____smiapp_read_8only(struct smiapp_sensor *sensor, u16 reg,
  118. u16 len, u32 *val)
  119. {
  120. unsigned int i;
  121. int rval;
  122. *val = 0;
  123. for (i = 0; i < len; i++) {
  124. u32 val8;
  125. rval = ____smiapp_read(sensor, reg + i, 1, &val8);
  126. if (rval < 0)
  127. return rval;
  128. *val |= val8 << ((len - i - 1) << 3);
  129. }
  130. return 0;
  131. }
  132. /*
  133. * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
  134. * Returns zero if successful, or non-zero otherwise.
  135. */
  136. static int __smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val,
  137. bool only8)
  138. {
  139. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  140. u8 len = SMIAPP_REG_WIDTH(reg);
  141. int rval;
  142. if (len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT
  143. && len != SMIAPP_REG_32BIT)
  144. return -EINVAL;
  145. if (len == SMIAPP_REG_8BIT || !only8)
  146. rval = ____smiapp_read(sensor, SMIAPP_REG_ADDR(reg), len, val);
  147. else
  148. rval = ____smiapp_read_8only(sensor, SMIAPP_REG_ADDR(reg), len,
  149. val);
  150. if (rval < 0)
  151. return rval;
  152. if (reg & SMIAPP_REG_FLAG_FLOAT)
  153. *val = float_to_u32_mul_1000000(client, *val);
  154. return 0;
  155. }
  156. int smiapp_read_no_quirk(struct smiapp_sensor *sensor, u32 reg, u32 *val)
  157. {
  158. return __smiapp_read(
  159. sensor, reg, val,
  160. smiapp_needs_quirk(sensor,
  161. SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY));
  162. }
  163. int smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val)
  164. {
  165. int rval;
  166. *val = 0;
  167. rval = smiapp_call_quirk(sensor, reg_access, false, &reg, val);
  168. if (rval == -ENOIOCTLCMD)
  169. return 0;
  170. if (rval < 0)
  171. return rval;
  172. return smiapp_read_no_quirk(sensor, reg, val);
  173. }
  174. int smiapp_read_8only(struct smiapp_sensor *sensor, u32 reg, u32 *val)
  175. {
  176. int rval;
  177. *val = 0;
  178. rval = smiapp_call_quirk(sensor, reg_access, false, &reg, val);
  179. if (rval == -ENOIOCTLCMD)
  180. return 0;
  181. if (rval < 0)
  182. return rval;
  183. return __smiapp_read(sensor, reg, val, true);
  184. }
  185. int smiapp_write_no_quirk(struct smiapp_sensor *sensor, u32 reg, u32 val)
  186. {
  187. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  188. struct i2c_msg msg;
  189. unsigned char data[6];
  190. unsigned int retries;
  191. u8 flags = SMIAPP_REG_FLAGS(reg);
  192. u8 len = SMIAPP_REG_WIDTH(reg);
  193. u16 offset = SMIAPP_REG_ADDR(reg);
  194. int r;
  195. if ((len != SMIAPP_REG_8BIT && len != SMIAPP_REG_16BIT &&
  196. len != SMIAPP_REG_32BIT) || flags)
  197. return -EINVAL;
  198. msg.addr = client->addr;
  199. msg.flags = 0; /* Write */
  200. msg.len = 2 + len;
  201. msg.buf = data;
  202. /* high byte goes out first */
  203. data[0] = (u8) (reg >> 8);
  204. data[1] = (u8) (reg & 0xff);
  205. switch (len) {
  206. case SMIAPP_REG_8BIT:
  207. data[2] = val;
  208. break;
  209. case SMIAPP_REG_16BIT:
  210. data[2] = val >> 8;
  211. data[3] = val;
  212. break;
  213. case SMIAPP_REG_32BIT:
  214. data[2] = val >> 24;
  215. data[3] = val >> 16;
  216. data[4] = val >> 8;
  217. data[5] = val;
  218. break;
  219. default:
  220. BUG();
  221. }
  222. for (retries = 0; retries < 5; retries++) {
  223. /*
  224. * Due to unknown reason sensor stops responding. This
  225. * loop is a temporaty solution until the root cause
  226. * is found.
  227. */
  228. r = i2c_transfer(client->adapter, &msg, 1);
  229. if (r == 1) {
  230. if (retries)
  231. dev_err(&client->dev,
  232. "sensor i2c stall encountered. "
  233. "retries: %d\n", retries);
  234. return 0;
  235. }
  236. usleep_range(2000, 2000);
  237. }
  238. dev_err(&client->dev,
  239. "wrote 0x%x to offset 0x%x error %d\n", val, offset, r);
  240. return r;
  241. }
  242. /*
  243. * Write to a 8/16-bit register.
  244. * Returns zero if successful, or non-zero otherwise.
  245. */
  246. int smiapp_write(struct smiapp_sensor *sensor, u32 reg, u32 val)
  247. {
  248. int rval;
  249. rval = smiapp_call_quirk(sensor, reg_access, true, &reg, &val);
  250. if (rval == -ENOIOCTLCMD)
  251. return 0;
  252. if (rval < 0)
  253. return rval;
  254. return smiapp_write_no_quirk(sensor, reg, val);
  255. }