mpu6515_accel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 RAWDATA_TIMER_MS 200
  21. #define RAWDATA_TIMER_MARGIN_MS 20
  22. #define ACCEL_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 accel_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 accel_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 sensor_type_show(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. return sprintf(buf, "%s\n", "ADSP");
  40. }
  41. static ssize_t accel_calibration_show(struct device *dev,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. struct adsp_data *data = dev_get_drvdata(dev);
  45. int iCount = 0;
  46. struct msg_data message;
  47. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  48. adsp_unicast(message, NETLINK_MESSAGE_GET_CALIB_DATA, 0, 0);
  49. while (!(data->calib_ready_flag & 1 << ADSP_FACTORY_MODE_ACCEL))
  50. msleep(20);
  51. data->calib_ready_flag &= 0 << ADSP_FACTORY_MODE_ACCEL;
  52. pr_info("[FACTORY] %s: %d,%d,%d,%d\n", __func__,
  53. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].result,
  54. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].x,
  55. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].y,
  56. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].z);
  57. iCount = snprintf(buf, PAGE_SIZE, "%d,%d,%d,%d\n",
  58. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].result,
  59. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].x,
  60. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].y,
  61. data->sensor_calib_data[ADSP_FACTORY_MODE_ACCEL].z);
  62. return iCount;
  63. }
  64. static ssize_t accel_calibration_store(struct device *dev,
  65. struct device_attribute *attr, const char *buf, size_t size)
  66. {
  67. #ifdef CONFIG_ARCH_MSM8974PRO
  68. struct msg_data message;
  69. unsigned long enable = 0;
  70. struct adsp_data *data = dev_get_drvdata(dev);
  71. if (strict_strtoul(buf, 10, &enable)) {
  72. pr_err("[FACTORY] %s: strict_strtoul fail\n", __func__);
  73. return -EINVAL;
  74. }
  75. if (enable > 0)
  76. enable = 1;
  77. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  78. message.param1 = enable;
  79. msleep(RAWDATA_TIMER_MS + RAWDATA_TIMER_MARGIN_MS);
  80. adsp_unicast(message, NETLINK_MESSAGE_CALIB_STORE_DATA, 0, 0);
  81. while (!(data->calib_store_ready_flag & 1 << ADSP_FACTORY_MODE_ACCEL))
  82. msleep(20);
  83. if (data->sensor_calib_result[ADSP_FACTORY_MODE_ACCEL].nCalibstoreresult < 0)
  84. pr_err("[FACTORY] %s: accel_do_calibrate() failed\n", __func__);
  85. data->calib_store_ready_flag |= 0 << ADSP_FACTORY_MODE_ACCEL;
  86. pr_info("[FACTORY] %s: result(%d)\n", __func__,
  87. data->sensor_calib_result[ADSP_FACTORY_MODE_ACCEL].nCalibstoreresult);
  88. #else
  89. struct adsp_data *data = dev_get_drvdata(dev);
  90. struct msg_data message;
  91. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  92. adsp_unicast(message, NETLINK_MESSAGE_CALIB_STORE_DATA, 0, 0);
  93. while (!(data->calib_store_ready_flag & 1 << ADSP_FACTORY_MODE_ACCEL))
  94. msleep(20);
  95. if (data->sensor_calib_result[ADSP_FACTORY_MODE_ACCEL].nCalibstoreresult < 0)
  96. pr_err("[FACTORY] %s: accel_do_calibrate() failed\n", __func__);
  97. data->calib_store_ready_flag |= 0 << ADSP_FACTORY_MODE_ACCEL;
  98. pr_info("[FACTORY] %s: result(%d)\n", __func__,
  99. data->sensor_calib_result[ADSP_FACTORY_MODE_ACCEL].nCalibstoreresult);
  100. #endif
  101. return size;
  102. }
  103. static ssize_t accel_selftest_show(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. struct adsp_data *data = dev_get_drvdata(dev);
  107. int result1 = 0;
  108. int result2 = 0;
  109. unsigned long timeout;
  110. struct msg_data message;
  111. int retry = 0;
  112. retry_accel_selftest:
  113. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  114. msleep(RAWDATA_TIMER_MS + RAWDATA_TIMER_MARGIN_MS);
  115. adsp_unicast(message, NETLINK_MESSAGE_SELFTEST_SHOW_DATA, 0, 0);
  116. timeout = jiffies + (20 * HZ);
  117. while (!(data->selftest_ready_flag & 1 << ADSP_FACTORY_MODE_ACCEL)) {
  118. msleep(20);
  119. if (time_after(jiffies, timeout)) {
  120. pr_info("[FACTORY] %s: Timeout!!!\n", __func__);
  121. return sprintf(buf, "%d,%d.%01d,%d.%01d,%d.%01d\n",
  122. 1, 0, 0, 0, 0, 0, 0);
  123. }
  124. }
  125. if (data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].nSelftestresult1 < 0)
  126. pr_err("[FACTORY] %s: accel_do_calibrate() failed\n", __func__);
  127. data->selftest_ready_flag &= 0 << ADSP_FACTORY_MODE_ACCEL;
  128. result1 = data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].nSelftestresult1 + 1;
  129. result2 = data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].nSelftestresult2 + 1;
  130. pr_info("[FACTORY] %s : result = %d\n", __func__, result1);
  131. pr_info("[FACTORY] %d.%03d,%d.%03d,%d.%03d\n",
  132. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_x),
  133. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_x_dec),
  134. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_y),
  135. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_y_dec),
  136. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_z),
  137. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_z_dec));
  138. if (result1 != 1) {
  139. if (retry < ACCEL_SELFTEST_TRY_CNT && data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_x_dec == 0) {
  140. retry++;
  141. msleep(RAWDATA_TIMER_MS * 2);
  142. goto retry_accel_selftest;
  143. }
  144. }
  145. return sprintf(buf, "%d,%d.%03d,%d.%03d,%d.%03d\n", result1,
  146. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_x),
  147. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_x_dec),
  148. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_y),
  149. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_y_dec),
  150. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_z),
  151. (int)abs(data->sensor_selftest_result[ADSP_FACTORY_MODE_ACCEL].ratio_z_dec));
  152. }
  153. static ssize_t accel_raw_data_read(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct msg_data message;
  157. unsigned long timeout = jiffies + (2 * HZ);
  158. struct adsp_data *data = dev_get_drvdata(dev);
  159. if (!(raw_data_stream & ADSP_RAW_ACCEL)) {
  160. pr_info("[FACTORY] %s: Start\n", __func__);
  161. mutex_lock(&raw_stream_lock);
  162. raw_data_stream |= ADSP_RAW_ACCEL;
  163. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  164. adsp_unicast(message, NETLINK_MESSAGE_GET_RAW_DATA, 0, 0);
  165. mutex_unlock(&raw_stream_lock);
  166. }
  167. while (!(data->data_ready_flag & 1 << ADSP_FACTORY_MODE_ACCEL)) {
  168. msleep(20);
  169. if (time_after(jiffies, timeout)) {
  170. pr_info("[FACTORY] %s: Timeout!!!\n", __func__);
  171. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",0, 0, 0);
  172. }
  173. }
  174. adsp_factory_start_timer(RAWDATA_TIMER_MS);
  175. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  176. data->sensor_data[ADSP_FACTORY_MODE_ACCEL].x,
  177. data->sensor_data[ADSP_FACTORY_MODE_ACCEL].y,
  178. data->sensor_data[ADSP_FACTORY_MODE_ACCEL].z);
  179. }
  180. static ssize_t accel_reactive_show(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. int alert_val = 0;
  184. struct adsp_data *data = dev_get_drvdata(dev);
  185. data->alert_ready_flag &= 0 << ADSP_FACTORY_MODE_ACCEL;
  186. if (data->reactive_alert == 2) {
  187. struct msg_data message;
  188. alert_val = 1;
  189. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  190. message.param1 = 0;
  191. adsp_unicast(message, NETLINK_MESSAGE_REACTIVE_ALERT_DATA, 0, 0);
  192. } else if (data->reactive_alert == 1) {
  193. alert_val = 0;
  194. } else if (data->reactive_alert == 3) {
  195. struct msg_data message;
  196. alert_val = 1;
  197. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  198. message.param1 = 3;
  199. adsp_unicast(message, NETLINK_MESSAGE_REACTIVE_ALERT_DATA, 0, 0);
  200. }
  201. pr_info("[FACTORY] %s: data->alert_ready_flag = %u, data->reactive_alert = %d, return %d\n",
  202. __func__, data->alert_ready_flag, data->reactive_alert, alert_val);
  203. return snprintf(buf, PAGE_SIZE, "%d\n", alert_val);
  204. }
  205. static ssize_t accel_reactive_store(struct device *dev,
  206. struct device_attribute *attr, const char *buf, size_t size)
  207. {
  208. unsigned long enable = 0;
  209. struct adsp_data *data = dev_get_drvdata(dev);
  210. if (strict_strtoul(buf, 10, &enable)) {
  211. pr_err("[FACTORY] %s: strict_strtoul fail\n", __func__);
  212. return -EINVAL;
  213. }
  214. pr_info("[FACTORY] %s: enable = %ld, data->alert_ready_flag = %u\n",
  215. __func__, enable, data->alert_ready_flag);
  216. if (!(data->alert_ready_flag & 1 << ADSP_FACTORY_MODE_ACCEL)) {
  217. struct msg_data message;
  218. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  219. message.param1 = enable;
  220. adsp_unicast(message, NETLINK_MESSAGE_REACTIVE_ALERT_DATA, 0, 0);
  221. }
  222. return size;
  223. }
  224. static void accel_stop_raw_data_worker(struct work_struct *work)
  225. {
  226. struct msg_data message;
  227. if ((raw_data_stream & ADSP_RAW_ACCEL)) {
  228. mutex_lock(&raw_stream_lock);
  229. raw_data_stream &= ~ADSP_RAW_ACCEL;
  230. message.sensor_type = ADSP_FACTORY_MODE_ACCEL;
  231. adsp_unicast(message, NETLINK_MESSAGE_STOP_RAW_DATA, 0, 0);
  232. mutex_unlock(&raw_stream_lock);
  233. pr_info("[FACTORY] %s: raw_data_stream flag = %d\n",
  234. __func__, raw_data_stream);
  235. }
  236. return;
  237. }
  238. static DEVICE_ATTR(name, S_IRUGO, accel_name_show, NULL);
  239. static DEVICE_ATTR(vendor, S_IRUGO, accel_vendor_show, NULL);
  240. static DEVICE_ATTR(type, S_IRUGO, sensor_type_show, NULL);
  241. static DEVICE_ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  242. accel_calibration_show, accel_calibration_store);
  243. static DEVICE_ATTR(selftest, S_IRUSR | S_IRGRP,
  244. accel_selftest_show, NULL);
  245. static DEVICE_ATTR(raw_data, S_IRUGO, accel_raw_data_read, NULL);
  246. static DEVICE_ATTR(reactive_alert, S_IRUGO | S_IWUSR | S_IWGRP,
  247. accel_reactive_show, accel_reactive_store);
  248. static struct device_attribute *acc_attrs[] = {
  249. &dev_attr_name,
  250. &dev_attr_vendor,
  251. &dev_attr_type,
  252. &dev_attr_calibration,
  253. &dev_attr_selftest,
  254. &dev_attr_raw_data,
  255. &dev_attr_reactive_alert,
  256. NULL,
  257. };
  258. int accel_factory_init(struct adsp_data *data)
  259. {
  260. return 0;
  261. }
  262. int accel_factory_exit(struct adsp_data *data)
  263. {
  264. return 0;
  265. }
  266. int accel_factory_receive_data(struct adsp_data *data, int cmd )
  267. {
  268. switch (cmd) {
  269. case CALLBACK_REGISTER_SUCCESS:
  270. pr_info("[FACTORY] %s: mpu6515 registration success \n",
  271. __func__);
  272. break;
  273. case CALLBACK_TIMER_EXPIRED:
  274. pr_info("[FACTORY] %s: raw_data_stream flag = %d\n",
  275. __func__, raw_data_stream);
  276. schedule_work(&timer_stop_data_work);
  277. break;
  278. default:
  279. break;
  280. }
  281. return 0;
  282. }
  283. static struct adsp_fac_ctl adsp_fact_cb = {
  284. .init_fnc = accel_factory_init,
  285. .exit_fnc = accel_factory_exit,
  286. .receive_data_fnc = accel_factory_receive_data
  287. };
  288. static int __devinit mpu6515accel_factory_init(void)
  289. {
  290. adsp_factory_register("accelerometer_sensor",
  291. ADSP_FACTORY_MODE_ACCEL, acc_attrs, &adsp_fact_cb);
  292. INIT_WORK(&timer_stop_data_work, accel_stop_raw_data_worker);
  293. pr_err("[FACTORY] %s\n", __func__);
  294. return 0;
  295. }
  296. static void __devexit mpu6515accel_factory_exit(void)
  297. {
  298. pr_err("[FACTORY] %s\n", __func__);
  299. }
  300. module_init(mpu6515accel_factory_init);
  301. module_exit(mpu6515accel_factory_exit);