smiapp-regs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 SMIA_REG_32BIT:
  99. *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) +
  100. data[3];
  101. break;
  102. case SMIA_REG_16BIT:
  103. *val = (data[0] << 8) + data[1];
  104. break;
  105. case SMIA_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. unsigned int len = (u8)(reg >> 16);
  141. int rval;
  142. if (len != SMIA_REG_8BIT && len != SMIA_REG_16BIT
  143. && len != SMIA_REG_32BIT)
  144. return -EINVAL;
  145. if (smiapp_quirk_reg(sensor, reg, val))
  146. goto found_quirk;
  147. if (len == SMIA_REG_8BIT && !only8)
  148. rval = ____smiapp_read(sensor, (u16)reg, len, val);
  149. else
  150. rval = ____smiapp_read_8only(sensor, (u16)reg, len, val);
  151. if (rval < 0)
  152. return rval;
  153. found_quirk:
  154. if (reg & SMIA_REG_FLAG_FLOAT)
  155. *val = float_to_u32_mul_1000000(client, *val);
  156. return 0;
  157. }
  158. int smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val)
  159. {
  160. return __smiapp_read(
  161. sensor, reg, val,
  162. smiapp_needs_quirk(sensor,
  163. SMIAPP_QUIRK_FLAG_8BIT_READ_ONLY));
  164. }
  165. int smiapp_read_8only(struct smiapp_sensor *sensor, u32 reg, u32 *val)
  166. {
  167. return __smiapp_read(sensor, reg, val, true);
  168. }
  169. /*
  170. * Write to a 8/16-bit register.
  171. * Returns zero if successful, or non-zero otherwise.
  172. */
  173. int smiapp_write(struct smiapp_sensor *sensor, u32 reg, u32 val)
  174. {
  175. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  176. struct i2c_msg msg;
  177. unsigned char data[6];
  178. unsigned int retries;
  179. unsigned int flags = reg >> 24;
  180. unsigned int len = (u8)(reg >> 16);
  181. u16 offset = reg;
  182. int r;
  183. if ((len != SMIA_REG_8BIT && len != SMIA_REG_16BIT &&
  184. len != SMIA_REG_32BIT) || flags)
  185. return -EINVAL;
  186. msg.addr = client->addr;
  187. msg.flags = 0; /* Write */
  188. msg.len = 2 + len;
  189. msg.buf = data;
  190. /* high byte goes out first */
  191. data[0] = (u8) (reg >> 8);
  192. data[1] = (u8) (reg & 0xff);
  193. switch (len) {
  194. case SMIA_REG_8BIT:
  195. data[2] = val;
  196. break;
  197. case SMIA_REG_16BIT:
  198. data[2] = val >> 8;
  199. data[3] = val;
  200. break;
  201. case SMIA_REG_32BIT:
  202. data[2] = val >> 24;
  203. data[3] = val >> 16;
  204. data[4] = val >> 8;
  205. data[5] = val;
  206. break;
  207. default:
  208. BUG();
  209. }
  210. for (retries = 0; retries < 5; retries++) {
  211. /*
  212. * Due to unknown reason sensor stops responding. This
  213. * loop is a temporaty solution until the root cause
  214. * is found.
  215. */
  216. r = i2c_transfer(client->adapter, &msg, 1);
  217. if (r == 1) {
  218. if (retries)
  219. dev_err(&client->dev,
  220. "sensor i2c stall encountered. "
  221. "retries: %d\n", retries);
  222. return 0;
  223. }
  224. usleep_range(2000, 2000);
  225. }
  226. dev_err(&client->dev,
  227. "wrote 0x%x to offset 0x%x error %d\n", val, offset, r);
  228. return r;
  229. }