yas.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (c) 2010-2011 Yamaha Corporation
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #ifndef __YAS_H__
  21. #define __YAS_H__
  22. #include "yas_cfg.h"
  23. #define YAS_VERSION "6.0.2"
  24. /* ------------------ */
  25. /* Typedef definition */
  26. /* ------------------- */
  27. #include <linux/types.h>
  28. /* -------------------------- */
  29. /* Macro definition */
  30. /* ------------------------- */
  31. /* Debugging */
  32. #define DEBUG (0)
  33. #if DEBUG
  34. #include <linux/kernel.h>
  35. #define YLOGD(args) (printk args)
  36. #define YLOGI(args) (printk args)
  37. #define YLOGE(args) (printk args)
  38. #define YLOGW(args) (printk args)
  39. #else /* DEBUG */
  40. #define YLOGD(args)
  41. #define YLOGI(args)
  42. #define YLOGW(args)
  43. #define YLOGE(args)
  44. #endif /* DEBUG */
  45. #define YAS_REPORT_DATA (0x01)
  46. #define YAS_REPORT_CALIB (0x02)
  47. #define YAS_REPORT_OVERFLOW_OCCURED (0x04)
  48. #define YAS_REPORT_HARD_OFFSET_CHANGED (0x08)
  49. #define YAS_REPORT_CALIB_OFFSET_CHANGED (0x10)
  50. #define YAS_HARD_OFFSET_UNKNOWN (0x7f)
  51. #define YAS_CALIB_OFFSET_UNKNOWN (0x7fffffff)
  52. #define YAS_NO_ERROR (0)
  53. #define YAS_ERROR_ARG (-1)
  54. #define YAS_ERROR_NOT_INITIALIZED (-2)
  55. #define YAS_ERROR_BUSY (-3)
  56. #define YAS_ERROR_DEVICE_COMMUNICATION (-4)
  57. #define YAS_ERROR_CHIP_ID (-5)
  58. #define YAS_ERROR_NOT_ACTIVE (-6)
  59. #define YAS_ERROR_RESTARTSYS (-7)
  60. #define YAS_ERROR_HARDOFFSET_NOT_WRITTEN (-8)
  61. #define YAS_ERROR_INTERRUPT (-9)
  62. #define YAS_ERROR_ERROR (-128)
  63. #ifndef NULL
  64. #define NULL ((void *)(0))
  65. #endif
  66. #ifndef FALSE
  67. #define FALSE (0)
  68. #endif
  69. #ifndef TRUE
  70. #define TRUE (!(0))
  71. #endif
  72. #ifndef NELEMS
  73. #define NELEMS(a) ((int)(sizeof(a)/sizeof(a[0])))
  74. #endif
  75. #ifndef ABS
  76. #define ABS(a) ((a) > 0 ? (a) : -(a))
  77. #endif
  78. #ifndef M_PI
  79. #define M_PI (3.14159265358979323846)
  80. #endif
  81. /* -------------------------------------------------------------------------- */
  82. /* Structure definition */
  83. /* -------------------------------------------------------------------------- */
  84. struct mag_platform_data {
  85. int position;
  86. void (*init_gpio)(void);
  87. };
  88. struct yas_mag_filter {
  89. int len;
  90. int noise[3];
  91. int threshold; /* nT */
  92. };
  93. struct yas_vector {
  94. int32_t v[3];
  95. };
  96. struct yas_matrix {
  97. int32_t matrix[9];
  98. };
  99. struct yas_acc_data {
  100. struct yas_vector xyz;
  101. struct yas_vector raw;
  102. };
  103. struct yas_gyro_data {
  104. struct yas_vector xyz;
  105. struct yas_vector raw;
  106. int overrun;
  107. int num;
  108. };
  109. struct yas_mag_data {
  110. struct yas_vector xyz; /* without offset, filtered */
  111. struct yas_vector raw; /* with offset, not filtered */
  112. struct yas_vector xy1y2;
  113. int16_t temperature;
  114. };
  115. struct yas_mag_offset {
  116. int8_t hard_offset[3];
  117. struct yas_vector calib_offset;
  118. };
  119. struct yas_mag_status {
  120. struct yas_mag_offset offset;
  121. int accuracy;
  122. struct yas_matrix dynamic_matrix;
  123. };
  124. struct yas_offset {
  125. struct yas_mag_status mag[YAS_MAGCALIB_SHAPE_NUM];
  126. };
  127. struct yas_mag_driver_callback {
  128. int (*lock) (void);
  129. int (*unlock) (void);
  130. int (*device_open) (void);
  131. int (*device_close) (void);
  132. #if YAS_MAG_DRIVER == YAS_MAG_DRIVER_YAS529
  133. int (*device_write) (const uint8_t *buf, int len);
  134. int (*device_read) (uint8_t *buf, int len);
  135. #else
  136. int (*device_write) (uint8_t addr, const uint8_t *buf, int len);
  137. int (*device_read) (uint8_t addr, uint8_t *buf, int len);
  138. #endif
  139. void (*msleep) (int msec);
  140. void (*current_time) (int32_t *sec, int32_t *msec);
  141. };
  142. struct yas_mag_driver {
  143. int (*init) (void);
  144. int (*term) (void);
  145. int (*get_delay) (void);
  146. int (*set_delay) (int msec);
  147. int (*get_offset) (struct yas_mag_offset *offset);
  148. int (*set_offset) (struct yas_mag_offset *offset);
  149. #ifdef YAS_MAG_MANUAL_OFFSET
  150. int (*get_manual_offset) (struct yas_vector *offset);
  151. int (*set_manual_offset) (struct yas_vector *offset);
  152. #endif
  153. int (*get_static_matrix) (struct yas_matrix *static_matrix);
  154. int (*set_static_matrix) (struct yas_matrix *static_matrix);
  155. int (*get_dynamic_matrix) (struct yas_matrix *dynamic_matrix);
  156. int (*set_dynamic_matrix) (struct yas_matrix *dynamic_matrix);
  157. int (*get_enable) (void);
  158. int (*set_enable) (int enable);
  159. int (*get_filter) (struct yas_mag_filter *filter);
  160. int (*set_filter) (struct yas_mag_filter *filter);
  161. int (*get_filter_enable) (void);
  162. int (*set_filter_enable) (int enable);
  163. int (*get_position) (void);
  164. int (*set_position) (int position);
  165. #if YAS_MAG_DRIVER == YAS_MAG_DRIVER_YAS529
  166. int (*read_reg) (uint8_t *buf, int len);
  167. int (*write_reg) (const uint8_t *buf, int len);
  168. #else
  169. int (*read_reg) (uint8_t addr, uint8_t *buf, int len);
  170. int (*write_reg) (uint8_t addr, const uint8_t *buf, int len);
  171. #endif
  172. int (*measure) (struct yas_mag_data *data, int *time_delay_ms);
  173. struct yas_mag_driver_callback callback;
  174. };
  175. struct yas_mag_calibration_result {
  176. int32_t spread;
  177. int32_t variation;
  178. int32_t radius;
  179. int8_t axis;
  180. int8_t level;
  181. int8_t accuracy;
  182. struct yas_matrix dynamic_matrix;
  183. };
  184. struct yas_mag_calibration_threshold {
  185. int32_t spread;
  186. int32_t variation[3];
  187. };
  188. struct yas_mag_calibration_callback {
  189. int (*lock) (void);
  190. int (*unlock) (void);
  191. };
  192. #define YAS_MAGCALIB_MODE_SPHERE (1)
  193. #define YAS_MAGCALIB_MODE_ELLIPSOID (1)
  194. struct yas_mag_calibration {
  195. int (*init) (void);
  196. int (*term) (void);
  197. int (*update) (struct yas_vector *mag,
  198. struct yas_mag_calibration_result *result);
  199. int (*get_accuracy) (void);
  200. int (*set_accuracy) (int accuracy);
  201. int (*get_offset) (struct yas_vector *offset);
  202. int (*set_offset) (struct yas_vector *offset);
  203. int (*get_shape) (void);
  204. int (*set_shape) (int shape);
  205. int (*get_threshold) (struct yas_mag_calibration_threshold *threshold);
  206. int (*set_threshold) (struct yas_mag_calibration_threshold *threshold);
  207. int (*get_mode) (void);
  208. int (*set_mode) (int mode);
  209. int (*get_max_sample) (void);
  210. int (*set_max_sample) (int num_samples);
  211. int (*get_dynamic_matrix) (struct yas_matrix *dynamic_matrix);
  212. struct yas_mag_calibration_callback callback;
  213. };
  214. #if YAS_SUPPORT_FUSION_DRIVER
  215. struct yas_quaternion {
  216. int32_t q[4];
  217. };
  218. struct yas_fusion_callback {
  219. int (*lock) (void);
  220. int (*unlock) (void);
  221. void (*current_time) (int32_t *sec, int32_t *msec);
  222. };
  223. struct yas_fusion {
  224. int (*init) (void);
  225. int (*term) (void);
  226. int (*update) (struct yas_vector *acc, struct yas_vector *mag,
  227. struct yas_vector *gyro);
  228. int (*get_quaternion) (struct yas_quaternion *quaternion);
  229. int (*get_fusion) (struct yas_quaternion *quaternion,
  230. struct yas_vector *acc, struct yas_vector *gravity,
  231. struct yas_vector *linear_acceleration,
  232. struct yas_vector *rotation_vector);
  233. struct yas_fusion_callback callback;
  234. };
  235. #endif
  236. #if YAS_SUPPORT_SOFTWARE_GYROSCOPE
  237. struct yas_swgyro_callback {
  238. int (*lock) (void);
  239. int (*unlock) (void);
  240. };
  241. struct yas_swgyro {
  242. int (*init) (void);
  243. int (*term) (void);
  244. int (*get_delay) (void);
  245. int (*set_delay) (int msec);
  246. int (*update) (struct yas_vector *acc, struct yas_vector *mag,
  247. struct yas_vector *gyro);
  248. struct yas_swgyro_callback callback;
  249. };
  250. #endif
  251. struct yas_acc_filter {
  252. int threshold; /* um/s^2 */
  253. };
  254. struct yas_acc_driver_callback {
  255. int (*lock) (void);
  256. int (*unlock) (void);
  257. int (*device_open) (void);
  258. int (*device_close) (void);
  259. int (*device_write) (uint8_t adr, const uint8_t *buf, int len);
  260. int (*device_read) (uint8_t adr, uint8_t *buf, int len);
  261. void (*msleep) (int msec);
  262. };
  263. struct yas_acc_driver {
  264. int (*init) (void);
  265. int (*term) (void);
  266. int (*get_delay) (void);
  267. int (*set_delay) (int delay);
  268. int (*get_offset) (struct yas_vector *offset);
  269. int (*set_offset) (struct yas_vector *offset);
  270. int (*get_enable) (void);
  271. int (*set_enable) (int enable);
  272. int (*get_filter) (struct yas_acc_filter *filter);
  273. int (*set_filter) (struct yas_acc_filter *filter);
  274. int (*get_filter_enable) (void);
  275. int (*set_filter_enable) (int enable);
  276. int (*get_position) (void);
  277. int (*set_position) (int position);
  278. int (*measure) (struct yas_acc_data *data);
  279. #if DEBUG
  280. int (*get_register) (uint8_t adr, uint8_t *val);
  281. #endif
  282. struct yas_acc_driver_callback callback;
  283. };
  284. struct yas_acc_calibration_threshold {
  285. int32_t variation;
  286. };
  287. struct yas_acc_calibration_callback {
  288. int (*lock) (void);
  289. int (*unlock) (void);
  290. };
  291. struct yas_acc_calibration {
  292. int (*init) (void);
  293. int (*term) (void);
  294. int (*update) (struct yas_vector *acc);
  295. int (*get_offset) (struct yas_vector *offset);
  296. int (*get_threshold) (struct yas_acc_calibration_threshold *threshold);
  297. int (*set_threshold) (struct yas_acc_calibration_threshold *threshold);
  298. struct yas_acc_calibration_callback callback;
  299. };
  300. struct yas_gyro_filter {
  301. int threshold; /*mdegree/s */
  302. };
  303. struct yas_gyro_driver_callback {
  304. int (*lock) (void);
  305. int (*unlock) (void);
  306. int (*device_open) (void);
  307. int (*device_close) (void);
  308. int (*device_write) (uint8_t adr, const uint8_t *buf, int len);
  309. int (*device_read) (uint8_t adr, uint8_t *buf, int len);
  310. int (*interrupt_enable) (void);
  311. int (*interrupt_disable) (void);
  312. void (*interrupt_notify) (int num);
  313. void (*msleep) (int msec);
  314. };
  315. struct yas_gyro_driver {
  316. int (*init) (void);
  317. int (*term) (void);
  318. int (*get_delay) (void);
  319. int (*set_delay) (int delay);
  320. int (*get_offset) (struct yas_vector *offset);
  321. int (*set_offset) (struct yas_vector *offset);
  322. int (*get_enable) (void);
  323. int (*set_enable) (int enable);
  324. int (*get_filter) (struct yas_gyro_filter *filter);
  325. int (*set_filter) (struct yas_gyro_filter *filter);
  326. int (*get_filter_enable) (void);
  327. int (*set_filter_enable) (int enable);
  328. int (*get_position) (void);
  329. int (*set_position) (int position);
  330. int (*get_interrupt) (void);
  331. int (*set_interrupt) (int interrupt);
  332. int (*measure) (struct yas_gyro_data *data, int num);
  333. void (*interrupt_handler) (void);
  334. #if DEBUG
  335. int (*get_register) (uint8_t adr, uint8_t *val);
  336. int (*set_register) (uint8_t adr, uint8_t val);
  337. #endif
  338. struct yas_gyro_driver_callback callback;
  339. };
  340. struct yas_gyro_calibration_threshold {
  341. int32_t variation;
  342. };
  343. struct yas_gyro_calibration_callback {
  344. int (*lock) (void);
  345. int (*unlock) (void);
  346. };
  347. struct yas_gyro_calibration {
  348. int (*init) (void);
  349. int (*term) (void);
  350. int (*update) (struct yas_vector *gyro);
  351. int (*get_offset) (struct yas_vector *offset);
  352. int (*get_threshold) (struct yas_gyro_calibration_threshold *
  353. threshold);
  354. int (*set_threshold) (struct yas_gyro_calibration_threshold *
  355. threshold);
  356. struct yas_gyro_calibration_callback callback;
  357. };
  358. struct yas_utility {
  359. int (*get_rotation_matrix) (struct yas_vector *acc,
  360. struct yas_vector *mag,
  361. struct yas_matrix *rotation_matrix);
  362. int (*get_euler) (struct yas_matrix *rotation_matrix,
  363. struct yas_vector *euler);
  364. };
  365. struct yas_platform_data {
  366. void (*poweron) (int);
  367. };
  368. /*-------------------------------------------------------------------------- */
  369. /* Global function definition */
  370. /*-------------------------------------------------------------------------- */
  371. int yas_mag_driver_init(struct yas_mag_driver *f);
  372. int yas_mag_calibration_init(struct yas_mag_calibration *f);
  373. int yas_acc_driver_init(struct yas_acc_driver *f);
  374. int yas_acc_calibration_init(struct yas_acc_calibration *f);
  375. int yas_gyro_driver_init(struct yas_gyro_driver *f, int interrupt);
  376. int yas_gyro_calibration_init(struct yas_gyro_calibration *f);
  377. int yas_utility_init(struct yas_utility *f);
  378. #if YAS_SUPPORT_FUSION_DRIVER
  379. int yas_fusion_init(struct yas_fusion *f);
  380. #endif
  381. #if YAS_SUPPORT_SOFTWARE_GYROSCOPE
  382. int yas_swgyro_init(struct yas_swgyro *f);
  383. #endif
  384. #endif /*__YAS_H__ */