mpu6515_gyro.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "INVENSENSE"
  19. #define CHIP_ID "MPU6515"
  20. #define GYRO_SELFTEST_TRY_CNT 7
  21. #define RAWDATA_TIMER_MS 200
  22. #define RAWDATA_TIMER_MARGIN_MS 20
  23. extern unsigned int raw_data_stream;
  24. extern struct mutex raw_stream_lock;
  25. static struct work_struct timer_stop_data_work;
  26. s64 get_time_nanossec(void)
  27. {
  28. struct timespec ts;
  29. ktime_get_ts(&ts);
  30. return timespec_to_ns(&ts);
  31. }
  32. static ssize_t gyro_vendor_show(struct device *dev,
  33. struct device_attribute *attr, char *buf)
  34. {
  35. return sprintf(buf, "%s\n", VENDOR);
  36. }
  37. static ssize_t gyro_name_show(struct device *dev,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. return sprintf(buf, "%s\n", CHIP_ID);
  41. }
  42. static ssize_t gyro_raw_data_read(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct msg_data message;
  46. unsigned long timeout = jiffies + (2 * HZ);
  47. struct adsp_data *data = dev_get_drvdata(dev);
  48. if (!(raw_data_stream & ADSP_RAW_GYRO)) {
  49. pr_info("[FACTORY] %s: Start\n", __func__);
  50. mutex_lock(&raw_stream_lock);
  51. raw_data_stream |= ADSP_RAW_GYRO;
  52. message.sensor_type = ADSP_FACTORY_MODE_GYRO;
  53. adsp_unicast(message, NETLINK_MESSAGE_GET_RAW_DATA, 0, 0);
  54. mutex_unlock(&raw_stream_lock);
  55. }
  56. while(!(data->data_ready_flag & 1 << ADSP_FACTORY_MODE_GYRO)) {
  57. msleep(20);
  58. if (time_after(jiffies, timeout))
  59. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", 0, 0, 0);
  60. }
  61. adsp_factory_start_timer(RAWDATA_TIMER_MS);
  62. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  63. data->sensor_data[ADSP_FACTORY_MODE_GYRO].x,
  64. data->sensor_data[ADSP_FACTORY_MODE_GYRO].y,
  65. data->sensor_data[ADSP_FACTORY_MODE_GYRO].z);
  66. }
  67. static ssize_t gyro_power_show(struct device *dev,
  68. struct device_attribute *attr, char *buf)
  69. {
  70. return snprintf(buf, PAGE_SIZE, "%d\n", 1);
  71. }
  72. static ssize_t gyro_temp_show(struct device *dev,
  73. struct device_attribute *attr, char *buf)
  74. {
  75. struct msg_data message;
  76. unsigned long timeout = jiffies + (2 * HZ);
  77. struct adsp_data *data = dev_get_drvdata(dev);
  78. /* power up */
  79. if( !(raw_data_stream & ADSP_RAW_GYRO) ){
  80. mutex_lock(&raw_stream_lock);
  81. raw_data_stream |= ADSP_RAW_GYRO;
  82. message.sensor_type = ADSP_FACTORY_MODE_GYRO;
  83. mutex_unlock(&raw_stream_lock);
  84. adsp_unicast(message,NETLINK_MESSAGE_GET_RAW_DATA,0,0);
  85. }
  86. while( !(data->data_ready_flag & 1 << ADSP_FACTORY_MODE_GYRO) ){
  87. usleep_range(1000, 2000);
  88. if (time_after(jiffies, timeout)) {
  89. pr_err("[Factory] gyro power up fail\n");
  90. break;
  91. }
  92. }
  93. /* get temperature */
  94. if (!(data->data_ready & ADSP_DATA_GYRO_TEMP)) {
  95. message.sensor_type = ADSP_FACTORY_MODE_GYRO;
  96. adsp_unicast(message, NETLINK_MESSAGE_GYRO_TEMP, 0, 0);
  97. }
  98. while( !(data->data_ready & ADSP_DATA_GYRO_TEMP) ){
  99. usleep_range(1000, 2000);
  100. if (time_after(jiffies, timeout)) {
  101. return sprintf(buf, "%d \n", 0);
  102. }
  103. }
  104. data->data_ready &= ~ADSP_DATA_GYRO_TEMP;
  105. /* power down */
  106. mutex_lock(&raw_stream_lock);
  107. message.sensor_type = ADSP_FACTORY_MODE_GYRO;
  108. adsp_unicast(message,NETLINK_MESSAGE_STOP_RAW_DATA,0,0);
  109. /* because we don't call adsp_factory_start_timer, ADSP_RAW_GYRO should be unmasked manually */
  110. if((raw_data_stream & ADSP_RAW_GYRO) )
  111. raw_data_stream &= ~ADSP_RAW_GYRO;
  112. data->data_ready_flag &= 0 << ADSP_FACTORY_MODE_GYRO; // GYRO_RAW data is used only here
  113. mutex_unlock(&raw_stream_lock);
  114. return sprintf(buf, "%d\n", data->gyro_temp);
  115. }
  116. static void gyro_stop_raw_data_worker(struct work_struct *work)
  117. {
  118. struct msg_data message;
  119. if (raw_data_stream & ADSP_RAW_GYRO) {
  120. mutex_lock(&raw_stream_lock);
  121. raw_data_stream &= ~ADSP_RAW_GYRO;
  122. message.sensor_type = ADSP_FACTORY_MODE_GYRO;
  123. adsp_unicast(message, NETLINK_MESSAGE_STOP_RAW_DATA, 0, 0);
  124. mutex_unlock(&raw_stream_lock);
  125. pr_info("[FACTORY] %s: raw_data_stream flag = %d\n",
  126. __func__, raw_data_stream);
  127. }
  128. return;
  129. }
  130. static ssize_t gyro_selftest_show(struct device *dev,
  131. struct device_attribute *attr, char *buf)
  132. {
  133. struct adsp_data *data = dev_get_drvdata(dev);
  134. int result1 = 0;
  135. int result2 = 0;
  136. unsigned long timeout;
  137. struct msg_data message;
  138. int retry = 0;
  139. retry_gyro_selftest:
  140. message.sensor_type = ADSP_FACTORY_MODE_GYRO;
  141. msleep(RAWDATA_TIMER_MS + RAWDATA_TIMER_MARGIN_MS);
  142. adsp_unicast(message, NETLINK_MESSAGE_SELFTEST_SHOW_DATA, 0, 0);
  143. timeout = jiffies + (10 * HZ);
  144. while (!(data->selftest_ready_flag & 1 << ADSP_FACTORY_MODE_GYRO)) {
  145. msleep(20);
  146. if (time_after(jiffies, timeout)) {
  147. pr_info("[FACTORY] %s: Timeout!!!\n", __func__);
  148. return snprintf(buf, PAGE_SIZE, "%d,"
  149. "%d.%03d,%d.%03d,%d.%03d,"
  150. "%d.%03d,%d.%03d,%d.%03d,"
  151. "%d.%01d,%d.%01d,%d.%01d,"
  152. "%d.%03d,%d.%03d,%d.%03d\n",
  153. 1,0,0,0,0,
  154. 0,0,0,0,0,
  155. 0,0,0,0,0,
  156. 0,0,0,0,0,0,0,0,0,0);
  157. }
  158. }
  159. data->selftest_ready_flag &= 0 << ADSP_FACTORY_MODE_GYRO;
  160. result1 = data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].nSelftestresult1;
  161. result2 = data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].nSelftestresult2;
  162. pr_info("[FACTORY] %s: result = %d \n", __func__, result1);
  163. pr_info("[FACTORY] %d.%03d,%d.%03d,%d.%03d,"
  164. "%d.%03d,%d.%03d,%d.%03d,"
  165. "%d.%01d,%d.%01d,%d.%01d,"
  166. "%d.%03d,%d.%03d,%d.%03d\n",
  167. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_x / 1000),
  168. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_x) % 1000,
  169. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_y / 1000),
  170. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_y) % 1000,
  171. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_z / 1000),
  172. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_z) % 1000,
  173. data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_x / 1000,
  174. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_x) % 1000,
  175. data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_y /1000,
  176. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_y) % 1000,
  177. data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_z / 1000,
  178. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_z) % 1000,
  179. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_x),
  180. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_x_dec),
  181. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_y),
  182. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_y_dec),
  183. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_z),
  184. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_z_dec),
  185. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_x / 100),
  186. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_x) % 100,
  187. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_y / 100),
  188. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_y) % 100,
  189. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_z / 100),
  190. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_z) % 100);
  191. if (result1) {
  192. if (retry < GYRO_SELFTEST_TRY_CNT && data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_x == 0) {
  193. retry++;
  194. msleep(RAWDATA_TIMER_MS * 2);
  195. goto retry_gyro_selftest;
  196. }
  197. }
  198. return snprintf(buf, PAGE_SIZE, "%d,"
  199. "%d.%03d,%d.%03d,%d.%03d,"
  200. "%d.%03d,%d.%03d,%d.%03d,"
  201. "%d.%01d,%d.%01d,%d.%01d,"
  202. "%d.%03d,%d.%03d,%d.%03d\n",
  203. result1 | result2,
  204. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_x / 1000),
  205. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_x) % 1000,
  206. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_y / 1000),
  207. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_y) % 1000,
  208. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_z / 1000),
  209. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].bias_z) % 1000,
  210. data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_x / 1000,
  211. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_x) % 1000,
  212. data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_y /1000,
  213. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_y) % 1000,
  214. data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_z / 1000,
  215. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].rms_z) % 1000,
  216. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_x),
  217. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_x_dec),
  218. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_y),
  219. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_y_dec),
  220. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_z),
  221. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].ratio_z_dec),
  222. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_x / 100),
  223. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_x) % 100,
  224. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_y / 100),
  225. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_y) % 100,
  226. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_z / 100),
  227. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_GYRO].st_z) % 100);
  228. }
  229. static DEVICE_ATTR(name, S_IRUGO, gyro_name_show, NULL);
  230. static DEVICE_ATTR(vendor, S_IRUGO, gyro_vendor_show, NULL);
  231. static DEVICE_ATTR(raw_data, S_IRUGO, gyro_raw_data_read, NULL);
  232. static DEVICE_ATTR(selftest, S_IRUSR | S_IRGRP,
  233. gyro_selftest_show, NULL);
  234. static DEVICE_ATTR(temperature, S_IRUSR | S_IRGRP,
  235. gyro_temp_show, NULL);
  236. static DEVICE_ATTR(power_on, S_IRUSR | S_IRGRP,
  237. gyro_power_show, NULL);
  238. static struct device_attribute *gyro_attrs[] = {
  239. &dev_attr_name,
  240. &dev_attr_vendor,
  241. &dev_attr_raw_data,
  242. &dev_attr_selftest,
  243. &dev_attr_temperature,
  244. &dev_attr_power_on,
  245. NULL,
  246. };
  247. int gyro_factory_init(struct adsp_data *data)
  248. {
  249. return 0;
  250. }
  251. int gyro_factory_exit(struct adsp_data *data)
  252. {
  253. return 0;
  254. }
  255. int gyro_factory_receive_data(struct adsp_data *data, int cmd)
  256. {
  257. switch (cmd) {
  258. case CALLBACK_REGISTER_SUCCESS:
  259. pr_info("[FACTORY] %s: mpu6515 registration success\n",
  260. __func__);
  261. break;
  262. case CALLBACK_TIMER_EXPIRED:
  263. pr_info("[FACTORY] %s: raw_data_stream flag = %d\n",
  264. __func__, raw_data_stream);
  265. schedule_work(&timer_stop_data_work);
  266. break;
  267. default:
  268. break;
  269. }
  270. return 0;
  271. }
  272. static struct adsp_fac_ctl adsp_fact_cb = {
  273. .init_fnc = gyro_factory_init,
  274. .exit_fnc = gyro_factory_exit,
  275. .receive_data_fnc = gyro_factory_receive_data
  276. };
  277. static int __devinit mpu6515gyro_factory_init(void)
  278. {
  279. adsp_factory_register("gyro_sensor",
  280. ADSP_FACTORY_MODE_GYRO, gyro_attrs, &adsp_fact_cb);
  281. INIT_WORK(&timer_stop_data_work, gyro_stop_raw_data_worker );
  282. pr_info("[FACTORY] %s\n", __func__);
  283. return 0;
  284. }
  285. static void __devexit mpu6515gyro_factory_exit(void)
  286. {
  287. pr_info("[FACTORY] %s\n", __func__);
  288. }
  289. module_init(mpu6515gyro_factory_init);
  290. module_exit(mpu6515gyro_factory_exit);