magnetic_ak8963c.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2012, Samsung Electronics Co. Ltd. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include "../ssp.h"
  16. /*************************************************************************/
  17. /* factory Sysfs */
  18. /*************************************************************************/
  19. #define VENDOR "YAMAHA"
  20. #define CHIP_ID "YAS532"
  21. static ssize_t magnetic_vendor_show(struct device *dev,
  22. struct device_attribute *attr, char *buf)
  23. {
  24. return sprintf(buf, "%s\n", VENDOR);
  25. }
  26. static ssize_t magnetic_name_show(struct device *dev,
  27. struct device_attribute *attr, char *buf)
  28. {
  29. return sprintf(buf, "%s\n", CHIP_ID);
  30. }
  31. static ssize_t raw_data_read(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. struct ssp_data *data = dev_get_drvdata(dev);
  35. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  36. data->buf[GEOMAGNETIC_SENSOR].x,
  37. data->buf[GEOMAGNETIC_SENSOR].y,
  38. data->buf[GEOMAGNETIC_SENSOR].z);
  39. }
  40. static ssize_t adc_data_read(struct device *dev,
  41. struct device_attribute *attr, char *buf)
  42. {
  43. bool bSuccess = false;
  44. u8 chTempbuf[2] = {1, 20};
  45. struct ssp_data *data = dev_get_drvdata(dev);
  46. if (!(atomic_read(&data->aSensorEnable) & (1 << GEOMAGNETIC_SENSOR))) {
  47. send_instruction(data, ADD_SENSOR, GEOMAGNETIC_SENSOR,
  48. chTempbuf, 2);
  49. msleep(200);
  50. }
  51. if ((data->buf[GEOMAGNETIC_SENSOR].x == 0) &&
  52. (data->buf[GEOMAGNETIC_SENSOR].y == 0) &&
  53. (data->buf[GEOMAGNETIC_SENSOR].z == 0))
  54. bSuccess = false;
  55. else
  56. bSuccess = true;
  57. if (!(atomic_read(&data->aSensorEnable) & (1 << GEOMAGNETIC_SENSOR)))
  58. send_instruction(data, REMOVE_SENSOR, GEOMAGNETIC_SENSOR,
  59. chTempbuf, 2);
  60. pr_info("[SSP]: %s - x = %d, y = %d, z = %d\n", __func__,
  61. data->buf[GEOMAGNETIC_SENSOR].x,
  62. data->buf[GEOMAGNETIC_SENSOR].y,
  63. data->buf[GEOMAGNETIC_SENSOR].z);
  64. return sprintf(buf, "%s,%d,%d,%d\n", (bSuccess ? "OK" : "NG"),
  65. data->buf[GEOMAGNETIC_SENSOR].x,
  66. data->buf[GEOMAGNETIC_SENSOR].y,
  67. data->buf[GEOMAGNETIC_SENSOR].z);
  68. }
  69. static ssize_t magnetic_get_asa(struct device *dev,
  70. struct device_attribute *attr, char *buf)
  71. {
  72. struct ssp_data *data = dev_get_drvdata(dev);
  73. return sprintf(buf, "%d,%d,%d\n", (s16)data->uFuseRomData[0],
  74. (s16)data->uFuseRomData[1], (s16)data->uFuseRomData[2]);
  75. }
  76. static ssize_t magnetic_get_status(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. bool bSuccess;
  80. struct ssp_data *data = dev_get_drvdata(dev);
  81. if ((data->uFuseRomData[0] == 0) ||
  82. (data->uFuseRomData[0] == 0xff) ||
  83. (data->uFuseRomData[1] == 0) ||
  84. (data->uFuseRomData[1] == 0xff) ||
  85. (data->uFuseRomData[2] == 0) ||
  86. (data->uFuseRomData[2] == 0xff))
  87. bSuccess = false;
  88. else
  89. bSuccess = true;
  90. return sprintf(buf, "%s,%u\n", (bSuccess ? "OK" : "NG"), bSuccess);
  91. }
  92. static ssize_t magnetic_get_selftest(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. bool bSelftestPassed = false;
  96. s16 iSF_X = 0, iSF_Y = 0, iSF_Z = 0;
  97. int iDelayCnt = 0, iRet = 0, iReties = 0;
  98. char chTempBuf[2] = { 0, 10 };
  99. struct ssp_data *data = dev_get_drvdata(dev);
  100. reties:
  101. iDelayCnt = 0;
  102. data->uFactorydataReady = 0;
  103. memset(data->uFactorydata, 0, sizeof(char) * FACTORY_DATA_MAX);
  104. iRet = send_instruction(data, FACTORY_MODE, GEOMAGNETIC_FACTORY,
  105. chTempBuf, 2);
  106. while (!(data->uFactorydataReady & (1 << GEOMAGNETIC_FACTORY))
  107. && (iDelayCnt++ < 50)
  108. && (iRet == SUCCESS))
  109. msleep(20);
  110. if ((iDelayCnt >= 50) || (iRet != SUCCESS)) {
  111. pr_err("[SSP]: %s - Magnetic Selftest Timeout!!\n", __func__);
  112. goto exit;
  113. }
  114. iSF_X = (s16)((data->uFactorydata[0] << 8) + data->uFactorydata[1]);
  115. iSF_Y = (s16)((data->uFactorydata[2] << 8) + data->uFactorydata[3]);
  116. iSF_Z = (s16)((data->uFactorydata[4] << 8) + data->uFactorydata[5]);
  117. iSF_X = (s16)(((int)iSF_X * ((int)data->uFuseRomData[0] + 128)) >> 8);
  118. iSF_Y = (s16)(((int)iSF_Y * ((int)data->uFuseRomData[1] + 128)) >> 8);
  119. iSF_Z = (s16)(((int)iSF_Z * ((int)data->uFuseRomData[2] + 128)) >> 8);
  120. pr_info("[SSP] %s: self test x = %d, y = %d, z = %d\n",
  121. __func__, iSF_X, iSF_Y, iSF_Z);
  122. if ((iSF_X >= -200) && (iSF_X <= 200))
  123. pr_info("[SSP] x passed self test, expect -200<=x<=200\n");
  124. else
  125. pr_info("[SSP] x failed self test, expect -200<=x<=200\n");
  126. if ((iSF_Y >= -200) && (iSF_Y <= 200))
  127. pr_info("[SSP] y passed self test, expect -200<=y<=200\n");
  128. else
  129. pr_info("[SSP] y failed self test, expect -200<=y<=200\n");
  130. if ((iSF_Z >= -3200) && (iSF_Z <= -800))
  131. pr_info("[SSP] z passed self test, expect -3200<=z<=-800\n");
  132. else
  133. pr_info("[SSP] z failed self test, expect -3200<=z<=-800\n");
  134. if (((iSF_X >= -200) && (iSF_X <= 200)) &&
  135. ((iSF_Y >= -200) && (iSF_Y <= 200)) &&
  136. ((iSF_Z >= -3200) && (iSF_Z <= -800)))
  137. bSelftestPassed = true;
  138. if ((bSelftestPassed == false) && (iReties++ < 5))
  139. goto reties;
  140. exit:
  141. return sprintf(buf, "%u,%d,%d,%d\n",
  142. bSelftestPassed, iSF_X, iSF_Y, iSF_Z);
  143. }
  144. static ssize_t magnetic_check_registers(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. u8 uBuf[13] = {0,};
  148. return sprintf(buf, "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u\n",
  149. uBuf[0], uBuf[1], uBuf[2], uBuf[3], uBuf[4], uBuf[5],
  150. uBuf[6], uBuf[7], uBuf[8], uBuf[9], uBuf[10], uBuf[11],
  151. uBuf[12]);
  152. }
  153. static ssize_t magnetic_check_cntl(struct device *dev,
  154. struct device_attribute *attr, char *strbuf)
  155. {
  156. bool bSuccess = false;
  157. return sprintf(strbuf, "%s,%d,%d,%d\n",
  158. (!bSuccess ? "OK" : "NG"), 0, 0, 0);
  159. }
  160. static DEVICE_ATTR(name, S_IRUGO, magnetic_name_show, NULL);
  161. static DEVICE_ATTR(vendor, S_IRUGO, magnetic_vendor_show, NULL);
  162. static DEVICE_ATTR(raw_data, S_IRUGO, raw_data_read, NULL);
  163. static DEVICE_ATTR(status, S_IRUGO, magnetic_get_status, NULL);
  164. static DEVICE_ATTR(adc, S_IRUGO, adc_data_read, NULL);
  165. static DEVICE_ATTR(dac, S_IRUGO, magnetic_check_cntl, NULL);
  166. static DEVICE_ATTR(selftest, S_IRUGO, magnetic_get_selftest, NULL);
  167. static DEVICE_ATTR(ak8963_asa, S_IRUGO, magnetic_get_asa, NULL);
  168. static DEVICE_ATTR(ak8963_chk_registers, S_IRUGO,
  169. magnetic_check_registers, NULL);
  170. static struct device_attribute *mag_attrs[] = {
  171. &dev_attr_name,
  172. &dev_attr_vendor,
  173. &dev_attr_adc,
  174. &dev_attr_raw_data,
  175. &dev_attr_status,
  176. &dev_attr_selftest,
  177. &dev_attr_ak8963_asa,
  178. &dev_attr_ak8963_chk_registers,
  179. &dev_attr_dac,
  180. NULL,
  181. };
  182. void initialize_magnetic_factorytest(struct ssp_data *data)
  183. {
  184. struct device *mag_device = NULL;
  185. sensors_register(mag_device, data, mag_attrs, "magnetic_sensor");
  186. }