smiapp-regs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #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. int smiapp_read(struct smiapp_sensor *sensor, u32 reg, u32 *val)
  67. {
  68. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  69. struct i2c_msg msg;
  70. unsigned char data[4];
  71. unsigned int len = (u8)(reg >> 16);
  72. u16 offset = reg;
  73. int r;
  74. if (len != SMIA_REG_8BIT && len != SMIA_REG_16BIT
  75. && len != SMIA_REG_32BIT)
  76. return -EINVAL;
  77. msg.addr = client->addr;
  78. msg.flags = 0;
  79. msg.len = 2;
  80. msg.buf = data;
  81. /* high byte goes out first */
  82. data[0] = (u8) (offset >> 8);
  83. data[1] = (u8) offset;
  84. r = i2c_transfer(client->adapter, &msg, 1);
  85. if (r != 1) {
  86. if (r >= 0)
  87. r = -EBUSY;
  88. goto err;
  89. }
  90. msg.len = len;
  91. msg.flags = I2C_M_RD;
  92. r = i2c_transfer(client->adapter, &msg, 1);
  93. if (r != 1) {
  94. if (r >= 0)
  95. r = -EBUSY;
  96. goto err;
  97. }
  98. *val = 0;
  99. /* high byte comes first */
  100. switch (len) {
  101. case SMIA_REG_32BIT:
  102. *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) +
  103. data[3];
  104. break;
  105. case SMIA_REG_16BIT:
  106. *val = (data[0] << 8) + data[1];
  107. break;
  108. case SMIA_REG_8BIT:
  109. *val = data[0];
  110. break;
  111. default:
  112. BUG();
  113. }
  114. if (reg & SMIA_REG_FLAG_FLOAT)
  115. *val = float_to_u32_mul_1000000(client, *val);
  116. return 0;
  117. err:
  118. dev_err(&client->dev, "read from offset 0x%x error %d\n", offset, r);
  119. return r;
  120. }
  121. /*
  122. * Write to a 8/16-bit register.
  123. * Returns zero if successful, or non-zero otherwise.
  124. */
  125. int smiapp_write(struct smiapp_sensor *sensor, u32 reg, u32 val)
  126. {
  127. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  128. struct i2c_msg msg;
  129. unsigned char data[6];
  130. unsigned int retries;
  131. unsigned int flags = reg >> 24;
  132. unsigned int len = (u8)(reg >> 16);
  133. u16 offset = reg;
  134. int r;
  135. if ((len != SMIA_REG_8BIT && len != SMIA_REG_16BIT &&
  136. len != SMIA_REG_32BIT) || flags)
  137. return -EINVAL;
  138. msg.addr = client->addr;
  139. msg.flags = 0; /* Write */
  140. msg.len = 2 + len;
  141. msg.buf = data;
  142. /* high byte goes out first */
  143. data[0] = (u8) (reg >> 8);
  144. data[1] = (u8) (reg & 0xff);
  145. switch (len) {
  146. case SMIA_REG_8BIT:
  147. data[2] = val;
  148. break;
  149. case SMIA_REG_16BIT:
  150. data[2] = val >> 8;
  151. data[3] = val;
  152. break;
  153. case SMIA_REG_32BIT:
  154. data[2] = val >> 24;
  155. data[3] = val >> 16;
  156. data[4] = val >> 8;
  157. data[5] = val;
  158. break;
  159. default:
  160. BUG();
  161. }
  162. for (retries = 0; retries < 5; retries++) {
  163. /*
  164. * Due to unknown reason sensor stops responding. This
  165. * loop is a temporaty solution until the root cause
  166. * is found.
  167. */
  168. r = i2c_transfer(client->adapter, &msg, 1);
  169. if (r == 1) {
  170. if (retries)
  171. dev_err(&client->dev,
  172. "sensor i2c stall encountered. "
  173. "retries: %d\n", retries);
  174. return 0;
  175. }
  176. usleep_range(2000, 2000);
  177. }
  178. dev_err(&client->dev,
  179. "wrote 0x%x to offset 0x%x error %d\n", val, offset, r);
  180. return r;
  181. }