yas.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (c) 2010 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 <linux/platform_device.h>
  23. #include "yas_cfg.h"
  24. #define YAS_VERSION "4.1.0"
  25. #define __LINUX_KERNEL_DRIVER__
  26. /* -------------------------------------------------------------------------- */
  27. /* Typedef definition */
  28. /* -------------------------------------------------------------------------- */
  29. #if defined(__LINUX_KERNEL_DRIVER__)
  30. #include <linux/types.h>
  31. #elif defined(__ANDROID__)
  32. #include <stdint.h>
  33. #else
  34. typedef signed char int8_t;
  35. typedef unsigned char uint8_t;
  36. typedef signed short int16_t;
  37. typedef unsigned short uint16_t;
  38. typedef signed int int32_t;
  39. typedef unsigned int uint32_t;
  40. #endif
  41. /* -------------------------------------------------------------------------- */
  42. /* Macro definition */
  43. /* -------------------------------------------------------------------------- */
  44. /* Debugging */
  45. #define DEBUG (0)
  46. #define MEDIATEK_CODE
  47. #if DEBUG
  48. #ifdef __LINUX_KERNEL_DRIVER__
  49. #include <linux/kernel.h>
  50. #define YLOGD(args) (pr_debug args)
  51. #define YLOGI(args) (pr_debug args)
  52. #define YLOGE(args) (pr_err args)
  53. #define YLOGW(args) (pr_debug args)
  54. #elif defined __ANDROID__
  55. #include <cutils/log.h>
  56. #ifdef LOG_TAG
  57. #undef LOG_TAG
  58. #endif
  59. #define LOG_TAG "yas"
  60. #define YLOGD(args) (LOGD args)
  61. #define YLOGI(args) (LOGI args)
  62. #define YLOGE(args) (LOGE args)
  63. #define YLOGW(args) (LOGW args)
  64. #else /* __ANDROID__ */
  65. #include <stdio.h>
  66. #define YLOGD(args) (printf args)
  67. #define YLOGI(args) (printf args)
  68. #define YLOGE(args) (printf args)
  69. #define YLOGW(args) (printf args)
  70. #endif /* __ANDROID__ */
  71. #else /* DEBUG */
  72. #define YLOGD(args)
  73. #define YLOGI(args)
  74. #define YLOGW(args)
  75. #define YLOGE(args)
  76. #endif /* DEBUG */
  77. #define YAS_REPORT_DATA (0x01)
  78. #define YAS_REPORT_CALIB (0x02)
  79. #define YAS_REPORT_OVERFLOW_OCCURED (0x04)
  80. #define YAS_REPORT_HARD_OFFSET_CHANGED (0x08)
  81. #define YAS_REPORT_CALIB_OFFSET_CHANGED (0x10)
  82. #define YAS_HARD_OFFSET_UNKNOWN (0x7f)
  83. #define YAS_CALIB_OFFSET_UNKNOWN (0x7fffffff)
  84. #define YAS_NO_ERROR (0)
  85. #define YAS_ERROR_ARG (-1)
  86. #define YAS_ERROR_NOT_INITIALIZED (-2)
  87. #define YAS_ERROR_BUSY (-3)
  88. #define YAS_ERROR_DEVICE_COMMUNICATION (-4)
  89. #define YAS_ERROR_CHIP_ID (-5)
  90. #define YAS_ERROR_NOT_ACTIVE (-6)
  91. #define YAS_ERROR_RESTARTSYS (-7)
  92. #define YAS_ERROR_HARDOFFSET_NOT_WRITTEN (-8)
  93. #define YAS_ERROR_INTERRUPT (-9)
  94. #define YAS_ERROR_ERROR (-128)
  95. #ifndef NULL
  96. #define NULL ((void *)(0))
  97. #endif
  98. #ifndef FALSE
  99. #define FALSE (0)
  100. #endif
  101. #ifndef TRUE
  102. #define TRUE (!(0))
  103. #endif
  104. #ifndef NELEMS
  105. #define NELEMS(a) ((int)(sizeof(a)/sizeof(a[0])))
  106. #endif
  107. #ifndef ABS
  108. #define ABS(a) ((a) > 0 ? (a) : -(a))
  109. #endif
  110. #ifndef M_PI
  111. #define M_PI (3.14159265358979323846)
  112. #endif
  113. /* -------------------------------------------------------------------------- */
  114. /* Structure definition */
  115. /* -------------------------------------------------------------------------- */
  116. struct yas_mag_filter {
  117. int len;
  118. int noise[3];
  119. int threshold; /* nT */
  120. };
  121. struct yas_vector {
  122. int32_t v[3];
  123. };
  124. struct yas_matrix {
  125. int32_t matrix[9];
  126. };
  127. struct yas_acc_data {
  128. struct yas_vector xyz;
  129. struct yas_vector raw;
  130. };
  131. struct yas_gyro_data {
  132. struct yas_vector xyz;
  133. struct yas_vector raw;
  134. int overrun;
  135. int num;
  136. };
  137. struct yas_mag_data {
  138. struct yas_vector xyz; /* without offset, filtered */
  139. struct yas_vector raw; /* with offset, not filtered */
  140. struct yas_vector xy1y2;
  141. int16_t temperature;
  142. };
  143. struct yas_mag_offset {
  144. int8_t hard_offset[3];
  145. struct yas_vector calib_offset;
  146. };
  147. struct yas_mag_status {
  148. struct yas_mag_offset offset;
  149. int accuracy;
  150. struct yas_matrix dynamic_matrix;
  151. };
  152. struct yas_offset {
  153. struct yas_mag_status mag[YAS_MAGCALIB_SHAPE_NUM];
  154. };
  155. struct yas_mag_driver_callback {
  156. int (*lock)(void);
  157. int (*unlock)(void);
  158. int (*device_open)(void);
  159. int (*device_close)(void);
  160. #if YAS_MAG_DRIVER == YAS_MAG_DRIVER_YAS529
  161. int (*device_write)(uint8_t slave, const uint8_t *buf, int len);
  162. int (*device_read)(uint8_t slave, uint8_t *buf, int len);
  163. #else
  164. int (*device_write)(uint8_t slave, uint8_t addr, const uint8_t *buf, int len);
  165. int (*device_read)(uint8_t slave, uint8_t addr, uint8_t *buf, int len);
  166. #endif
  167. void (*msleep)(int msec);
  168. void (*current_time)(int32_t *sec, int32_t *msec);
  169. };
  170. struct yas_mag_driver {
  171. int (*init)(void);
  172. int (*term)(void);
  173. int (*get_delay)(void);
  174. int (*set_delay)(int msec);
  175. int (*get_offset)(struct yas_mag_offset *offset);
  176. int (*set_offset)(struct yas_mag_offset *offset);
  177. #ifdef YAS_MAG_MANUAL_OFFSET
  178. int (*get_manual_offset)(struct yas_vector *offset);
  179. int (*set_manual_offset)(struct yas_vector *offset);
  180. #endif
  181. int (*get_static_matrix)(struct yas_matrix *static_matrix);
  182. int (*set_static_matrix)(struct yas_matrix *static_matrix);
  183. int (*get_dynamic_matrix)(struct yas_matrix *dynamic_matrix);
  184. int (*set_dynamic_matrix)(struct yas_matrix *dynamic_matrix);
  185. int (*get_enable)(void);
  186. int (*set_enable)(int enable);
  187. int (*get_filter)(struct yas_mag_filter *filter);
  188. int (*set_filter)(struct yas_mag_filter *filter);
  189. int (*get_filter_enable)(void);
  190. int (*set_filter_enable)(int enable);
  191. int (*get_position)(void);
  192. int (*set_position)(int position);
  193. #if YAS_MAG_DRIVER == YAS_MAG_DRIVER_YAS529
  194. int (*read_reg)(uint8_t *buf, int len);
  195. int (*write_reg)(const uint8_t *buf, int len);
  196. #else
  197. int (*read_reg)(uint8_t addr, uint8_t *buf, int len);
  198. int (*write_reg)(uint8_t addr, const uint8_t *buf, int len);
  199. #endif
  200. int (*measure)(struct yas_mag_data *data, int *time_delay_ms);
  201. struct yas_mag_driver_callback callback;
  202. };
  203. struct yas_mag_calibration_result {
  204. int32_t spread;
  205. int32_t variation;
  206. int32_t radius;
  207. int8_t axis;
  208. int8_t level;
  209. int8_t accuracy;
  210. struct yas_matrix dynamic_matrix;
  211. };
  212. struct yas_mag_calibration_threshold {
  213. int32_t spread;
  214. int32_t variation[3];
  215. };
  216. struct yas_mag_calibration_callback {
  217. int (*lock)(void);
  218. int (*unlock)(void);
  219. };
  220. #define YAS_MAGCALIB_MODE_SPHERE (0)
  221. #define YAS_MAGCALIB_MODE_ELLIPSOID (1)
  222. struct yas_mag_calibration {
  223. int (*init)(void);
  224. int (*term)(void);
  225. int (*update)(struct yas_vector *mag,
  226. struct yas_mag_calibration_result *result);
  227. int (*get_accuracy)(void);
  228. int (*set_accuracy)(int accuracy);
  229. int (*get_offset)(struct yas_vector *offset);
  230. int (*set_offset)(struct yas_vector *offset);
  231. int (*get_shape)(void);
  232. int (*set_shape)(int shape);
  233. int (*get_threshold)(struct yas_mag_calibration_threshold *threshold);
  234. int (*set_threshold)(struct yas_mag_calibration_threshold *threshold);
  235. int (*get_mode)(void);
  236. int (*set_mode)(int mode);
  237. int (*get_max_sample)(void);
  238. int (*set_max_sample)(int num_samples);
  239. int (*get_dynamic_matrix)(struct yas_matrix *dynamic_matrix);
  240. struct yas_mag_calibration_callback callback;
  241. };
  242. struct yas_quaternion {
  243. int32_t q[4];
  244. };
  245. #if YAS_SUPPORT_FUSION_DRIVER
  246. struct yas_fusion_callback {
  247. int (*lock)(void);
  248. int (*unlock)(void);
  249. void (*current_time)(int32_t *sec, int32_t *msec);
  250. };
  251. struct yas_fusion {
  252. int (*init)(void);
  253. int (*term)(void);
  254. int (*update)(struct yas_vector *acc, struct yas_vector *mag,
  255. struct yas_vector *gyro);
  256. int (*get_quaternion)(struct yas_quaternion *quaternion);
  257. int (*get_fusion)(struct yas_quaternion *quaternion,
  258. struct yas_vector *acc,
  259. struct yas_vector *gravity,
  260. struct yas_vector *linear_acceleration,
  261. struct yas_vector *rotation_vector);
  262. struct yas_fusion_callback callback;
  263. };
  264. #endif
  265. #if YAS_SUPPORT_SOFTWARE_GYROSCOPE
  266. struct yas_swgyro_callback {
  267. int (*lock)(void);
  268. int (*unlock)(void);
  269. };
  270. struct yas_swgyro {
  271. int (*init)(void);
  272. int (*term)(void);
  273. int (*get_delay)(void);
  274. int (*set_delay)(int msec);
  275. int (*update)(struct yas_vector *acc, struct yas_vector *mag,
  276. struct yas_vector *gyro);
  277. struct yas_swgyro_callback callback;
  278. };
  279. #endif
  280. struct yas_acc_filter {
  281. int threshold; /* um/s^2 */
  282. };
  283. struct yas_acc_driver_callback {
  284. int (*lock)(void);
  285. int (*unlock)(void);
  286. int (*device_open)(void);
  287. int (*device_close)(void);
  288. int (*device_write)(uint8_t slave, uint8_t adr, const uint8_t *buf, int len);
  289. int (*device_read)(uint8_t slave, uint8_t adr, uint8_t *buf, int len);
  290. void (*msleep)(int msec);
  291. };
  292. struct yas_acc_driver {
  293. int (*init)(void);
  294. int (*term)(void);
  295. int (*get_delay)(void);
  296. int (*set_delay)(int delay);
  297. int (*get_offset)(struct yas_vector *offset);
  298. int (*set_offset)(struct yas_vector *offset);
  299. int (*get_enable)(void);
  300. int (*set_enable)(int enable);
  301. int (*get_filter)(struct yas_acc_filter *filter);
  302. int (*set_filter)(struct yas_acc_filter *filter);
  303. int (*get_filter_enable)(void);
  304. int (*set_filter_enable)(int enable);
  305. int (*get_position)(void);
  306. int (*set_position)(int position);
  307. int (*measure)(struct yas_acc_data *data);
  308. #if DEBUG
  309. int (*get_register)(uint8_t adr, uint8_t *val);
  310. #endif
  311. struct yas_acc_driver_callback callback;
  312. };
  313. struct yas_acc_calibration_threshold {
  314. int32_t variation;
  315. };
  316. struct yas_acc_calibration_callback {
  317. int (*lock)(void);
  318. int (*unlock)(void);
  319. };
  320. struct yas_acc_calibration {
  321. int (*init)(void);
  322. int (*term)(void);
  323. int (*update)(struct yas_vector *acc);
  324. int (*get_offset)(struct yas_vector *offset);
  325. int (*get_threshold)(struct yas_acc_calibration_threshold *threshold);
  326. int (*set_threshold)(struct yas_acc_calibration_threshold *threshold);
  327. struct yas_acc_calibration_callback callback;
  328. };
  329. struct yas_gyro_filter {
  330. int threshold; /* mdegree/s */
  331. };
  332. struct yas_gyro_driver_callback {
  333. int (*lock)(void);
  334. int (*unlock)(void);
  335. int (*device_open)(void);
  336. int (*device_close)(void);
  337. int (*device_write)(uint8_t adr, const uint8_t *buf, int len);
  338. int (*device_read)(uint8_t adr, uint8_t *buf, int len);
  339. int (*interrupt_enable)(void);
  340. int (*interrupt_disable)(void);
  341. void (*interrupt_notify)(int num);
  342. void (*msleep)(int msec);
  343. };
  344. struct yas_gyro_driver {
  345. int (*init)(void);
  346. int (*term)(void);
  347. int (*get_delay)(void);
  348. int (*set_delay)(int delay);
  349. int (*get_offset)(struct yas_vector *offset);
  350. int (*set_offset)(struct yas_vector *offset);
  351. int (*get_enable)(void);
  352. int (*set_enable)(int enable);
  353. int (*get_filter)(struct yas_gyro_filter *filter);
  354. int (*set_filter)(struct yas_gyro_filter *filter);
  355. int (*get_filter_enable)(void);
  356. int (*set_filter_enable)(int enable);
  357. int (*get_position)(void);
  358. int (*set_position)(int position);
  359. int (*get_interrupt)(void);
  360. int (*set_interrupt)(int interrupt);
  361. int (*measure)(struct yas_gyro_data *data, int num);
  362. void (*interrupt_handler)(void);
  363. #if DEBUG
  364. int (*get_register)(uint8_t adr, uint8_t *val);
  365. int (*set_register)(uint8_t adr, uint8_t val);
  366. #endif
  367. struct yas_gyro_driver_callback callback;
  368. };
  369. struct yas_gyro_calibration_threshold {
  370. int32_t variation;
  371. };
  372. struct yas_gyro_calibration_callback {
  373. int (*lock)(void);
  374. int (*unlock)(void);
  375. };
  376. struct yas_gyro_calibration {
  377. int (*init)(void);
  378. int (*term)(void);
  379. int (*update)(struct yas_vector *gyro);
  380. int (*get_offset)(struct yas_vector *offset);
  381. int (*get_threshold)(struct yas_gyro_calibration_threshold *threshold);
  382. int (*set_threshold)(struct yas_gyro_calibration_threshold *threshold);
  383. struct yas_gyro_calibration_callback callback;
  384. };
  385. struct yas_utility {
  386. int (*get_rotation_matrix)(struct yas_vector *acc,
  387. struct yas_vector *mag,
  388. struct yas_matrix *rotation_matrix);
  389. int (*get_euler)(struct yas_matrix *rotation_matrix,
  390. struct yas_vector *euler);
  391. };
  392. /* ----------------------------------------------------------------------------
  393. * Global function definition
  394. *--------------------------------------------------------------------------- */
  395. extern int is_valid_calib_offset(const int32_t *p);
  396. int yas_mag_driver_init(struct yas_mag_driver *f);
  397. #ifdef YAS_MAGCALIB_INSTANTIATE
  398. int yas_mag_calibration_get_memsize(void);
  399. void yas_mag_calibration_set_memregion(void *p);
  400. #endif
  401. int yas_mag_calibration_init(struct yas_mag_calibration *f);
  402. int yas_acc_driver_init(struct yas_acc_driver *f);
  403. int yas_acc_calibration_init(struct yas_acc_calibration *f);
  404. int yas_gyro_driver_init(struct yas_gyro_driver *f, int interrupt);
  405. int yas_gyro_calibration_init(struct yas_gyro_calibration *f);
  406. int yas_utility_init(struct yas_utility *f);
  407. #if YAS_SUPPORT_FUSION_DRIVER
  408. int yas_fusion_init(struct yas_fusion *f);
  409. #endif
  410. #if YAS_SUPPORT_SOFTWARE_GYROSCOPE
  411. int yas_swgyro_init(struct yas_swgyro *f);
  412. #endif
  413. int yamaha532_o_probe(struct platform_device *pdev);
  414. int yamaha532_o_remove(struct platform_device *pdev);
  415. int yamaha532_o_suspend(struct platform_device *pdev, pm_message_t state);
  416. int yamaha532_o_resume(struct platform_device *pdev);
  417. int yamaha532_o_enable(int en);
  418. int yamaha532_o_set_delay(u64 ns);
  419. int yamaha532_o_open_report_data(int open);
  420. int yamaha532_o_get_data(int *x , int *y, int *z, int *status);
  421. #endif /* __YAS_H__ */