smiapp-regs.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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-regs.h"
  27. static uint32_t float_to_u32_mul_1000000(struct i2c_client *client,
  28. uint32_t phloat)
  29. {
  30. int32_t exp;
  31. uint64_t man;
  32. if (phloat >= 0x80000000) {
  33. dev_err(&client->dev, "this is a negative number\n");
  34. return 0;
  35. }
  36. if (phloat == 0x7f800000)
  37. return ~0; /* Inf. */
  38. if ((phloat & 0x7f800000) == 0x7f800000) {
  39. dev_err(&client->dev, "NaN or other special number\n");
  40. return 0;
  41. }
  42. /* Valid cases begin here */
  43. if (phloat == 0)
  44. return 0; /* Valid zero */
  45. if (phloat > 0x4f800000)
  46. return ~0; /* larger than 4294967295 */
  47. /*
  48. * Unbias exponent (note how phloat is now guaranteed to
  49. * have 0 in the high bit)
  50. */
  51. exp = ((int32_t)phloat >> 23) - 127;
  52. /* Extract mantissa, add missing '1' bit and it's in MHz */
  53. man = ((phloat & 0x7fffff) | 0x800000) * 1000000ULL;
  54. if (exp < 0)
  55. man >>= -exp;
  56. else
  57. man <<= exp;
  58. man >>= 23; /* Remove mantissa bias */
  59. return man & 0xffffffff;
  60. }
  61. /*
  62. * Read a 8/16/32-bit i2c register. The value is returned in 'val'.
  63. * Returns zero if successful, or non-zero otherwise.
  64. */
  65. int smiapp_read(struct i2c_client *client, u32 reg, u32 *val)
  66. {
  67. struct i2c_msg msg;
  68. unsigned char data[4];
  69. unsigned int len = (u8)(reg >> 16);
  70. u16 offset = reg;
  71. int r;
  72. if (len != SMIA_REG_8BIT && len != SMIA_REG_16BIT
  73. && len != SMIA_REG_32BIT)
  74. return -EINVAL;
  75. msg.addr = client->addr;
  76. msg.flags = 0;
  77. msg.len = 2;
  78. msg.buf = data;
  79. /* high byte goes out first */
  80. data[0] = (u8) (offset >> 8);
  81. data[1] = (u8) offset;
  82. r = i2c_transfer(client->adapter, &msg, 1);
  83. if (r != 1) {
  84. if (r >= 0)
  85. r = -EBUSY;
  86. goto err;
  87. }
  88. msg.len = len;
  89. msg.flags = I2C_M_RD;
  90. r = i2c_transfer(client->adapter, &msg, 1);
  91. if (r != 1) {
  92. if (r >= 0)
  93. r = -EBUSY;
  94. goto err;
  95. }
  96. *val = 0;
  97. /* high byte comes first */
  98. switch (len) {
  99. case SMIA_REG_32BIT:
  100. *val = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) +
  101. data[3];
  102. break;
  103. case SMIA_REG_16BIT:
  104. *val = (data[0] << 8) + data[1];
  105. break;
  106. case SMIA_REG_8BIT:
  107. *val = data[0];
  108. break;
  109. default:
  110. BUG();
  111. }
  112. if (reg & SMIA_REG_FLAG_FLOAT)
  113. *val = float_to_u32_mul_1000000(client, *val);
  114. return 0;
  115. err:
  116. dev_err(&client->dev, "read from offset 0x%x error %d\n", offset, r);
  117. return r;
  118. }
  119. /*
  120. * Write to a 8/16-bit register.
  121. * Returns zero if successful, or non-zero otherwise.
  122. */
  123. int smiapp_write(struct i2c_client *client, u32 reg, u32 val)
  124. {
  125. struct i2c_msg msg;
  126. unsigned char data[6];
  127. unsigned int retries;
  128. unsigned int flags = reg >> 24;
  129. unsigned int len = (u8)(reg >> 16);
  130. u16 offset = reg;
  131. int r;
  132. if ((len != SMIA_REG_8BIT && len != SMIA_REG_16BIT &&
  133. len != SMIA_REG_32BIT) || flags)
  134. return -EINVAL;
  135. msg.addr = client->addr;
  136. msg.flags = 0; /* Write */
  137. msg.len = 2 + len;
  138. msg.buf = data;
  139. /* high byte goes out first */
  140. data[0] = (u8) (reg >> 8);
  141. data[1] = (u8) (reg & 0xff);
  142. switch (len) {
  143. case SMIA_REG_8BIT:
  144. data[2] = val;
  145. break;
  146. case SMIA_REG_16BIT:
  147. data[2] = val >> 8;
  148. data[3] = val;
  149. break;
  150. case SMIA_REG_32BIT:
  151. data[2] = val >> 24;
  152. data[3] = val >> 16;
  153. data[4] = val >> 8;
  154. data[5] = val;
  155. break;
  156. default:
  157. BUG();
  158. }
  159. for (retries = 0; retries < 5; retries++) {
  160. /*
  161. * Due to unknown reason sensor stops responding. This
  162. * loop is a temporaty solution until the root cause
  163. * is found.
  164. */
  165. r = i2c_transfer(client->adapter, &msg, 1);
  166. if (r == 1) {
  167. if (retries)
  168. dev_err(&client->dev,
  169. "sensor i2c stall encountered. "
  170. "retries: %d\n", retries);
  171. return 0;
  172. }
  173. usleep_range(2000, 2000);
  174. }
  175. dev_err(&client->dev,
  176. "wrote 0x%x to offset 0x%x error %d\n", val, offset, r);
  177. return r;
  178. }