yas532_mag.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <linux/init.h>
  16. #include <linux/module.h>
  17. #include "adsp.h"
  18. #define VENDOR "YAMAHA"
  19. #define CHIP_ID "YAS532B"
  20. #define RAWDATA_TIMER_MS 200
  21. #define RAWDATA_TIMER_MARGIN_MS 20
  22. #define MAG_SELFTEST_TRY_CNT 7
  23. static struct work_struct timer_stop_data_work;
  24. extern unsigned int raw_data_stream;
  25. extern struct mutex raw_stream_lock;
  26. static ssize_t mag_vendor_show(struct device *dev,
  27. struct device_attribute *attr, char *buf)
  28. {
  29. return sprintf(buf, "%s\n", VENDOR);
  30. }
  31. static ssize_t mag_name_show(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. return sprintf(buf, "%s\n", CHIP_ID);
  35. }
  36. static ssize_t mag_raw_data_read(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct msg_data message;
  40. unsigned long timeout = jiffies + (2 * HZ);
  41. struct adsp_data *data = dev_get_drvdata(dev);
  42. if (!(raw_data_stream & ADSP_RAW_MAG)) {
  43. msleep(RAWDATA_TIMER_MS * 4);
  44. pr_info("[FACTORY] %s: Start!!!!!!\n", __func__);
  45. mutex_lock(&raw_stream_lock);
  46. raw_data_stream |= ADSP_RAW_MAG;
  47. message.sensor_type = ADSP_FACTORY_MODE_MAG;
  48. adsp_unicast(message, NETLINK_MESSAGE_GET_RAW_DATA, 0, 0);
  49. mutex_unlock(&raw_stream_lock);
  50. }
  51. while (!(data->data_ready_flag & 1 << ADSP_FACTORY_MODE_MAG)) {
  52. msleep(20);
  53. if (time_after(jiffies, timeout)) {
  54. pr_info("[FACTORY] %s: Timeout!!!\n", __func__);
  55. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", 0, 0, 0);
  56. }
  57. }
  58. adsp_factory_start_timer(RAWDATA_TIMER_MS);
  59. pr_info("[FACTORY] %s: %d,%d,%d\n", __func__,
  60. data->sensor_data[ADSP_FACTORY_MODE_MAG].x,
  61. data->sensor_data[ADSP_FACTORY_MODE_MAG].y,
  62. data->sensor_data[ADSP_FACTORY_MODE_MAG].z);
  63. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  64. data->sensor_data[ADSP_FACTORY_MODE_MAG].x,
  65. data->sensor_data[ADSP_FACTORY_MODE_MAG].y,
  66. data->sensor_data[ADSP_FACTORY_MODE_MAG].z);
  67. }
  68. static ssize_t mag_raw_data_store(struct device *dev,
  69. struct device_attribute *attr, const char *buf, size_t size)
  70. {
  71. int iRet;
  72. int64_t dEnable;
  73. struct msg_data message;
  74. iRet = kstrtoll(buf, 10, &dEnable);
  75. if (iRet < 0)
  76. return iRet;
  77. if (dEnable) {
  78. message.sensor_type = ADSP_FACTORY_MODE_MAG_POWERNOISE_ON;
  79. msleep(RAWDATA_TIMER_MS + RAWDATA_TIMER_MARGIN_MS);
  80. adsp_unicast(message, NETLINK_MESSAGE_MAG_POWERNOISE_ON, 0, 0);
  81. } else {
  82. if ((raw_data_stream & ADSP_RAW_MAG)) {
  83. mutex_lock(&raw_stream_lock);
  84. raw_data_stream &= ~ADSP_RAW_MAG;
  85. message.sensor_type = ADSP_FACTORY_MODE_MAG;
  86. adsp_unicast(message, NETLINK_MESSAGE_STOP_RAW_DATA, 0, 0);
  87. mutex_unlock(&raw_stream_lock);
  88. pr_info("[FACTORY] %s: raw_data_stream flag = %d\n",
  89. __func__, raw_data_stream);
  90. }
  91. message.sensor_type = ADSP_FACTORY_MODE_MAG_POWERNOISE_OFF;
  92. msleep(RAWDATA_TIMER_MS * 2);
  93. adsp_unicast(message, NETLINK_MESSAGE_MAG_POWERNOISE_OFF, 0, 0);
  94. }
  95. pr_info("[FACTORY] %s: %lld\n", __func__, dEnable);
  96. return size;
  97. }
  98. static void mag_stop_raw_data_worker(struct work_struct *work)
  99. {
  100. struct msg_data message;
  101. if ((raw_data_stream & ADSP_RAW_MAG)) {
  102. mutex_lock(&raw_stream_lock);
  103. raw_data_stream &= ~ADSP_RAW_MAG;
  104. message.sensor_type = ADSP_FACTORY_MODE_MAG;
  105. adsp_unicast(message, NETLINK_MESSAGE_STOP_RAW_DATA, 0, 0);
  106. mutex_unlock(&raw_stream_lock);
  107. pr_info("[FACTORY] %s: raw_data_stream flag = %d\n",
  108. __func__, raw_data_stream);
  109. }
  110. return;
  111. }
  112. static ssize_t selttest_show(struct device *dev,
  113. struct device_attribute *attr, char *buf)
  114. {
  115. struct adsp_data *data = dev_get_drvdata(dev);
  116. int result1 = 0;
  117. int result2 = 0;
  118. struct msg_data message;
  119. s8 err[7] = { 0, };
  120. int retry = 0;
  121. int i;
  122. retry_mag_selftest:
  123. message.sensor_type = ADSP_FACTORY_MODE_MAG;
  124. msleep(RAWDATA_TIMER_MS + RAWDATA_TIMER_MARGIN_MS);
  125. adsp_unicast(message, NETLINK_MESSAGE_SELFTEST_SHOW_DATA, 0, 0);
  126. while (!(data->selftest_ready_flag & 1 << ADSP_FACTORY_MODE_MAG))
  127. msleep(20);
  128. data->selftest_ready_flag &= 0 << ADSP_FACTORY_MODE_MAG;
  129. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].nSelftestresult1 == 0)
  130. result1 = 1;
  131. else
  132. result1 = 0;
  133. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].nSelftestresult2 == 0)
  134. result2 = 1;
  135. else
  136. result2 = 0;
  137. pr_info("[FACTORY] %s: result = %d, %d\n", __func__, result1, result2);
  138. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].id != 0x2)
  139. err[0] = -1;
  140. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_x < -30 ||
  141. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_x > 30)
  142. err[3] = -1;
  143. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_y < -30 ||
  144. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_y > 30)
  145. err[3] = -1;
  146. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_z < -30 ||
  147. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_z > 30)
  148. err[3] = -1;
  149. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].sx < 17 ||
  150. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].sy < 22)
  151. err[5] = -1;
  152. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohx < -600 ||
  153. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohx > 600)
  154. err[6] = -1;
  155. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohy < -600 ||
  156. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohy > 600)
  157. err[6] = -1;
  158. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohz < -600 ||
  159. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohz > 600)
  160. err[6] = -1;
  161. pr_info("[FACTORY] Test1: err = %d, id = %d\n"
  162. "[FACTORY] Test3: err = %d\n"
  163. "[FACTORY] Test4: err = %d, offset = %d,%d,%d\n"
  164. "[FACTORY] Test5: err = %d, direction = %d\n"
  165. "[FACTORY] Test6: err = %d, sensitivity = %d,%d\n"
  166. "[FACTORY] Test7: err = %d, offset = %d,%d,%d\n"
  167. "[FACTORY] Test2: err = %d\n",
  168. err[0], data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].id,
  169. err[2], err[3],
  170. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_x,
  171. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_y,
  172. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_z,
  173. err[4], data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].dir,
  174. err[5],
  175. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].sx,
  176. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].sy,
  177. err[6],
  178. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohx,
  179. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohy,
  180. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohz,
  181. err[1]);
  182. if (!result1) {
  183. if (retry < MAG_SELFTEST_TRY_CNT && data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].id == 0) {
  184. retry++;
  185. msleep(RAWDATA_TIMER_MS * 2);
  186. for (i = 0; i < 7; i++)
  187. err[i] = 0;
  188. goto retry_mag_selftest;
  189. }
  190. }
  191. return sprintf(buf,
  192. "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  193. err[0], data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].id,
  194. err[2], err[3],
  195. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_x,
  196. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_y,
  197. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].offset_z,
  198. err[4], data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].dir,
  199. err[5],
  200. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].sx,
  201. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].sy,
  202. err[6],
  203. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohx,
  204. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohy,
  205. data->sensor_selftest_result[ADSP_FACTORY_MODE_MAG].ohz,
  206. err[1]);
  207. }
  208. static DEVICE_ATTR(selftest, 0664, selttest_show, NULL);
  209. static DEVICE_ATTR(name, 0440, mag_name_show, NULL);
  210. static DEVICE_ATTR(vendor, 0440, mag_vendor_show, NULL);
  211. static DEVICE_ATTR(raw_data, 0664, mag_raw_data_read, mag_raw_data_store);
  212. static struct device_attribute *mag_attrs[] = {
  213. &dev_attr_selftest,
  214. &dev_attr_name,
  215. &dev_attr_vendor,
  216. &dev_attr_raw_data,
  217. NULL,
  218. };
  219. int mag_factory_init(struct adsp_data *data)
  220. {
  221. return 0;
  222. }
  223. int mag_factory_exit(struct adsp_data *data)
  224. {
  225. return 0;
  226. }
  227. int mag_factory_receive_data(struct adsp_data *data, int cmd)
  228. {
  229. switch (cmd) {
  230. case CALLBACK_REGISTER_SUCCESS:
  231. pr_info("[FACTORY] %s: yas532 registration success\n",
  232. __func__);
  233. break;
  234. case CALLBACK_TIMER_EXPIRED:
  235. pr_info("[FACTORY] %s: raw_data_stream flag = %d\n",
  236. __func__, raw_data_stream);
  237. schedule_work(&timer_stop_data_work);
  238. break;
  239. default:
  240. break;
  241. }
  242. return 0;
  243. }
  244. static struct adsp_fac_ctl adsp_fact_cb = {
  245. .init_fnc = mag_factory_init,
  246. .exit_fnc = mag_factory_exit,
  247. .receive_data_fnc = mag_factory_receive_data
  248. };
  249. static int __devinit yas532_factory_init(void)
  250. {
  251. adsp_factory_register("magnetic_sensor",
  252. ADSP_FACTORY_MODE_MAG, mag_attrs, &adsp_fact_cb);
  253. pr_info("[FACTORY] %s\n", __func__);
  254. INIT_WORK(&timer_stop_data_work, mag_stop_raw_data_worker);
  255. return 0;
  256. }
  257. static void __devexit yas532_factory_exit(void)
  258. {
  259. pr_info("[FACTORY] %s\n", __func__);
  260. }
  261. module_init(yas532_factory_init);
  262. module_exit(yas532_factory_exit);