smiapp-regs.c 6.3 KB

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