lsm6dso.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /* ST Microelectronics LSM6DSO 6-axis IMU sensor driver
  2. *
  3. * Copyright (c) 2019 STMicroelectronics
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Datasheet:
  8. * https://www.st.com/resource/en/datasheet/lsm6dso.pdf
  9. */
  10. #define DT_DRV_COMPAT st_lsm6dso
  11. #include <drivers/sensor.h>
  12. #include <kernel.h>
  13. #include <device.h>
  14. #include <init.h>
  15. #include <string.h>
  16. #include <sys/byteorder.h>
  17. #include <sys/__assert.h>
  18. #include <logging/log.h>
  19. #include "lsm6dso.h"
  20. LOG_MODULE_REGISTER(LSM6DSO, CONFIG_SENSOR_LOG_LEVEL);
  21. static const uint16_t lsm6dso_odr_map[] = {0, 12, 26, 52, 104, 208, 416, 833,
  22. 1660, 3330, 6660};
  23. #if defined(LSM6DSO_ACCEL_ODR_RUNTIME) || defined(LSM6DSO_GYRO_ODR_RUNTIME)
  24. static int lsm6dso_freq_to_odr_val(uint16_t freq)
  25. {
  26. size_t i;
  27. for (i = 0; i < ARRAY_SIZE(lsm6dso_odr_map); i++) {
  28. if (freq == lsm6dso_odr_map[i]) {
  29. return i;
  30. }
  31. }
  32. return -EINVAL;
  33. }
  34. #endif
  35. static int lsm6dso_odr_to_freq_val(uint16_t odr)
  36. {
  37. /* for valid index, return value from map */
  38. if (odr < ARRAY_SIZE(lsm6dso_odr_map)) {
  39. return lsm6dso_odr_map[odr];
  40. }
  41. /* invalid index, return last entry */
  42. return lsm6dso_odr_map[ARRAY_SIZE(lsm6dso_odr_map) - 1];
  43. }
  44. #ifdef LSM6DSO_ACCEL_FS_RUNTIME
  45. static const uint16_t lsm6dso_accel_fs_map[] = {2, 16, 4, 8};
  46. static const uint16_t lsm6dso_accel_fs_sens[] = {1, 8, 2, 4};
  47. static int lsm6dso_accel_range_to_fs_val(int32_t range)
  48. {
  49. size_t i;
  50. for (i = 0; i < ARRAY_SIZE(lsm6dso_accel_fs_map); i++) {
  51. if (range == lsm6dso_accel_fs_map[i]) {
  52. return i;
  53. }
  54. }
  55. return -EINVAL;
  56. }
  57. #endif
  58. #ifdef LSM6DSO_GYRO_FS_RUNTIME
  59. static const uint16_t lsm6dso_gyro_fs_map[] = {250, 500, 1000, 2000, 125};
  60. static const uint16_t lsm6dso_gyro_fs_sens[] = {2, 4, 8, 16, 1};
  61. static int lsm6dso_gyro_range_to_fs_val(int32_t range)
  62. {
  63. size_t i;
  64. for (i = 0; i < ARRAY_SIZE(lsm6dso_gyro_fs_map); i++) {
  65. if (range == lsm6dso_gyro_fs_map[i]) {
  66. return i;
  67. }
  68. }
  69. return -EINVAL;
  70. }
  71. #endif
  72. static inline int lsm6dso_reboot(const struct device *dev)
  73. {
  74. const struct lsm6dso_config *cfg = dev->config;
  75. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  76. if (lsm6dso_boot_set(ctx, 1) < 0) {
  77. return -EIO;
  78. }
  79. /* Wait sensor turn-on time as per datasheet */
  80. k_busy_wait(35 * USEC_PER_MSEC);
  81. return 0;
  82. }
  83. static int lsm6dso_accel_set_fs_raw(const struct device *dev, uint8_t fs)
  84. {
  85. const struct lsm6dso_config *cfg = dev->config;
  86. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  87. struct lsm6dso_data *data = dev->data;
  88. if (lsm6dso_xl_full_scale_set(ctx, fs) < 0) {
  89. return -EIO;
  90. }
  91. data->accel_fs = fs;
  92. return 0;
  93. }
  94. static int lsm6dso_accel_set_odr_raw(const struct device *dev, uint8_t odr)
  95. {
  96. const struct lsm6dso_config *cfg = dev->config;
  97. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  98. struct lsm6dso_data *data = dev->data;
  99. if (lsm6dso_xl_data_rate_set(ctx, odr) < 0) {
  100. return -EIO;
  101. }
  102. data->accel_freq = lsm6dso_odr_to_freq_val(odr);
  103. return 0;
  104. }
  105. static int lsm6dso_gyro_set_fs_raw(const struct device *dev, uint8_t fs)
  106. {
  107. const struct lsm6dso_config *cfg = dev->config;
  108. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  109. if (lsm6dso_gy_full_scale_set(ctx, fs) < 0) {
  110. return -EIO;
  111. }
  112. return 0;
  113. }
  114. static int lsm6dso_gyro_set_odr_raw(const struct device *dev, uint8_t odr)
  115. {
  116. const struct lsm6dso_config *cfg = dev->config;
  117. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  118. if (lsm6dso_gy_data_rate_set(ctx, odr) < 0) {
  119. return -EIO;
  120. }
  121. return 0;
  122. }
  123. #ifdef LSM6DSO_ACCEL_ODR_RUNTIME
  124. static int lsm6dso_accel_odr_set(const struct device *dev, uint16_t freq)
  125. {
  126. int odr;
  127. odr = lsm6dso_freq_to_odr_val(freq);
  128. if (odr < 0) {
  129. return odr;
  130. }
  131. if (lsm6dso_accel_set_odr_raw(dev, odr) < 0) {
  132. LOG_DBG("failed to set accelerometer sampling rate");
  133. return -EIO;
  134. }
  135. return 0;
  136. }
  137. #endif
  138. #ifdef LSM6DSO_ACCEL_FS_RUNTIME
  139. static int lsm6dso_accel_range_set(const struct device *dev, int32_t range)
  140. {
  141. int fs;
  142. struct lsm6dso_data *data = dev->data;
  143. fs = lsm6dso_accel_range_to_fs_val(range);
  144. if (fs < 0) {
  145. return fs;
  146. }
  147. if (lsm6dso_accel_set_fs_raw(dev, fs) < 0) {
  148. LOG_DBG("failed to set accelerometer full-scale");
  149. return -EIO;
  150. }
  151. data->acc_gain = (lsm6dso_accel_fs_sens[fs] * GAIN_UNIT_XL);
  152. return 0;
  153. }
  154. #endif
  155. static int lsm6dso_accel_config(const struct device *dev,
  156. enum sensor_channel chan,
  157. enum sensor_attribute attr,
  158. const struct sensor_value *val)
  159. {
  160. switch (attr) {
  161. #ifdef LSM6DSO_ACCEL_FS_RUNTIME
  162. case SENSOR_ATTR_FULL_SCALE:
  163. return lsm6dso_accel_range_set(dev, sensor_ms2_to_g(val));
  164. #endif
  165. #ifdef LSM6DSO_ACCEL_ODR_RUNTIME
  166. case SENSOR_ATTR_SAMPLING_FREQUENCY:
  167. return lsm6dso_accel_odr_set(dev, val->val1);
  168. #endif
  169. default:
  170. LOG_DBG("Accel attribute not supported.");
  171. return -ENOTSUP;
  172. }
  173. return 0;
  174. }
  175. #ifdef LSM6DSO_GYRO_ODR_RUNTIME
  176. static int lsm6dso_gyro_odr_set(const struct device *dev, uint16_t freq)
  177. {
  178. int odr;
  179. odr = lsm6dso_freq_to_odr_val(freq);
  180. if (odr < 0) {
  181. return odr;
  182. }
  183. if (lsm6dso_gyro_set_odr_raw(dev, odr) < 0) {
  184. LOG_DBG("failed to set gyroscope sampling rate");
  185. return -EIO;
  186. }
  187. return 0;
  188. }
  189. #endif
  190. #ifdef LSM6DSO_GYRO_FS_RUNTIME
  191. static int lsm6dso_gyro_range_set(const struct device *dev, int32_t range)
  192. {
  193. int fs;
  194. struct lsm6dso_data *data = dev->data;
  195. fs = lsm6dso_gyro_range_to_fs_val(range);
  196. if (fs < 0) {
  197. return fs;
  198. }
  199. if (lsm6dso_gyro_set_fs_raw(dev, fs) < 0) {
  200. LOG_DBG("failed to set gyroscope full-scale");
  201. return -EIO;
  202. }
  203. data->gyro_gain = (lsm6dso_gyro_fs_sens[fs] * GAIN_UNIT_G);
  204. return 0;
  205. }
  206. #endif
  207. static int lsm6dso_gyro_config(const struct device *dev,
  208. enum sensor_channel chan,
  209. enum sensor_attribute attr,
  210. const struct sensor_value *val)
  211. {
  212. switch (attr) {
  213. #ifdef LSM6DSO_GYRO_FS_RUNTIME
  214. case SENSOR_ATTR_FULL_SCALE:
  215. return lsm6dso_gyro_range_set(dev, sensor_rad_to_degrees(val));
  216. #endif
  217. #ifdef LSM6DSO_GYRO_ODR_RUNTIME
  218. case SENSOR_ATTR_SAMPLING_FREQUENCY:
  219. return lsm6dso_gyro_odr_set(dev, val->val1);
  220. #endif
  221. default:
  222. LOG_DBG("Gyro attribute not supported.");
  223. return -ENOTSUP;
  224. }
  225. return 0;
  226. }
  227. static int lsm6dso_attr_set(const struct device *dev,
  228. enum sensor_channel chan,
  229. enum sensor_attribute attr,
  230. const struct sensor_value *val)
  231. {
  232. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  233. struct lsm6dso_data *data = dev->data;
  234. #endif /* CONFIG_LSM6DSO_SENSORHUB */
  235. switch (chan) {
  236. case SENSOR_CHAN_ACCEL_XYZ:
  237. return lsm6dso_accel_config(dev, chan, attr, val);
  238. case SENSOR_CHAN_GYRO_XYZ:
  239. return lsm6dso_gyro_config(dev, chan, attr, val);
  240. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  241. case SENSOR_CHAN_MAGN_XYZ:
  242. case SENSOR_CHAN_PRESS:
  243. case SENSOR_CHAN_HUMIDITY:
  244. if (!data->shub_inited) {
  245. LOG_ERR("shub not inited.");
  246. return -ENOTSUP;
  247. }
  248. return lsm6dso_shub_config(dev, chan, attr, val);
  249. #endif /* CONFIG_LSM6DSO_SENSORHUB */
  250. default:
  251. LOG_WRN("attr_set() not supported on this channel.");
  252. return -ENOTSUP;
  253. }
  254. return 0;
  255. }
  256. static int lsm6dso_sample_fetch_accel(const struct device *dev)
  257. {
  258. const struct lsm6dso_config *cfg = dev->config;
  259. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  260. struct lsm6dso_data *data = dev->data;
  261. int16_t buf[3];
  262. if (lsm6dso_acceleration_raw_get(ctx, buf) < 0) {
  263. LOG_DBG("Failed to read sample");
  264. return -EIO;
  265. }
  266. data->acc[0] = sys_le16_to_cpu(buf[0]);
  267. data->acc[1] = sys_le16_to_cpu(buf[1]);
  268. data->acc[2] = sys_le16_to_cpu(buf[2]);
  269. return 0;
  270. }
  271. static int lsm6dso_sample_fetch_gyro(const struct device *dev)
  272. {
  273. const struct lsm6dso_config *cfg = dev->config;
  274. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  275. struct lsm6dso_data *data = dev->data;
  276. int16_t buf[3];
  277. if (lsm6dso_angular_rate_raw_get(ctx, buf) < 0) {
  278. LOG_DBG("Failed to read sample");
  279. return -EIO;
  280. }
  281. data->gyro[0] = sys_le16_to_cpu(buf[0]);
  282. data->gyro[1] = sys_le16_to_cpu(buf[1]);
  283. data->gyro[2] = sys_le16_to_cpu(buf[2]);
  284. return 0;
  285. }
  286. #if defined(CONFIG_LSM6DSO_ENABLE_TEMP)
  287. static int lsm6dso_sample_fetch_temp(const struct device *dev)
  288. {
  289. const struct lsm6dso_config *cfg = dev->config;
  290. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  291. struct lsm6dso_data *data = dev->data;
  292. int16_t buf;
  293. if (lsm6dso_temperature_raw_get(ctx, &buf) < 0) {
  294. LOG_DBG("Failed to read sample");
  295. return -EIO;
  296. }
  297. data->temp_sample = sys_le16_to_cpu(buf);
  298. return 0;
  299. }
  300. #endif
  301. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  302. static int lsm6dso_sample_fetch_shub(const struct device *dev)
  303. {
  304. if (lsm6dso_shub_fetch_external_devs(dev) < 0) {
  305. LOG_DBG("failed to read ext shub devices");
  306. return -EIO;
  307. }
  308. return 0;
  309. }
  310. #endif /* CONFIG_LSM6DSO_SENSORHUB */
  311. static int lsm6dso_sample_fetch(const struct device *dev,
  312. enum sensor_channel chan)
  313. {
  314. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  315. struct lsm6dso_data *data = dev->data;
  316. #endif /* CONFIG_LSM6DSO_SENSORHUB */
  317. switch (chan) {
  318. case SENSOR_CHAN_ACCEL_XYZ:
  319. lsm6dso_sample_fetch_accel(dev);
  320. break;
  321. case SENSOR_CHAN_GYRO_XYZ:
  322. lsm6dso_sample_fetch_gyro(dev);
  323. break;
  324. #if defined(CONFIG_LSM6DSO_ENABLE_TEMP)
  325. case SENSOR_CHAN_DIE_TEMP:
  326. lsm6dso_sample_fetch_temp(dev);
  327. break;
  328. #endif
  329. case SENSOR_CHAN_ALL:
  330. lsm6dso_sample_fetch_accel(dev);
  331. lsm6dso_sample_fetch_gyro(dev);
  332. #if defined(CONFIG_LSM6DSO_ENABLE_TEMP)
  333. lsm6dso_sample_fetch_temp(dev);
  334. #endif
  335. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  336. if (data->shub_inited) {
  337. lsm6dso_sample_fetch_shub(dev);
  338. }
  339. #endif
  340. break;
  341. default:
  342. return -ENOTSUP;
  343. }
  344. return 0;
  345. }
  346. static inline void lsm6dso_accel_convert(struct sensor_value *val, int raw_val,
  347. uint32_t sensitivity)
  348. {
  349. int64_t dval;
  350. /* Sensitivity is exposed in ug/LSB */
  351. /* Convert to m/s^2 */
  352. dval = (int64_t)(raw_val) * sensitivity * SENSOR_G_DOUBLE;
  353. val->val1 = (int32_t)(dval / 1000000);
  354. val->val2 = (int32_t)(dval % 1000000);
  355. }
  356. static inline int lsm6dso_accel_get_channel(enum sensor_channel chan,
  357. struct sensor_value *val,
  358. struct lsm6dso_data *data,
  359. uint32_t sensitivity)
  360. {
  361. uint8_t i;
  362. switch (chan) {
  363. case SENSOR_CHAN_ACCEL_X:
  364. lsm6dso_accel_convert(val, data->acc[0], sensitivity);
  365. break;
  366. case SENSOR_CHAN_ACCEL_Y:
  367. lsm6dso_accel_convert(val, data->acc[1], sensitivity);
  368. break;
  369. case SENSOR_CHAN_ACCEL_Z:
  370. lsm6dso_accel_convert(val, data->acc[2], sensitivity);
  371. break;
  372. case SENSOR_CHAN_ACCEL_XYZ:
  373. for (i = 0; i < 3; i++) {
  374. lsm6dso_accel_convert(val++, data->acc[i], sensitivity);
  375. }
  376. break;
  377. default:
  378. return -ENOTSUP;
  379. }
  380. return 0;
  381. }
  382. static int lsm6dso_accel_channel_get(enum sensor_channel chan,
  383. struct sensor_value *val,
  384. struct lsm6dso_data *data)
  385. {
  386. return lsm6dso_accel_get_channel(chan, val, data, data->acc_gain);
  387. }
  388. static inline void lsm6dso_gyro_convert(struct sensor_value *val, int raw_val,
  389. uint32_t sensitivity)
  390. {
  391. int64_t dval;
  392. /* Sensitivity is exposed in udps/LSB */
  393. /* Convert to rad/s */
  394. dval = (int64_t)(raw_val) * sensitivity * SENSOR_DEG2RAD_DOUBLE;
  395. val->val1 = (int32_t)(dval / 1000000);
  396. val->val2 = (int32_t)(dval % 1000000);
  397. }
  398. static inline int lsm6dso_gyro_get_channel(enum sensor_channel chan,
  399. struct sensor_value *val,
  400. struct lsm6dso_data *data,
  401. uint32_t sensitivity)
  402. {
  403. uint8_t i;
  404. switch (chan) {
  405. case SENSOR_CHAN_GYRO_X:
  406. lsm6dso_gyro_convert(val, data->gyro[0], sensitivity);
  407. break;
  408. case SENSOR_CHAN_GYRO_Y:
  409. lsm6dso_gyro_convert(val, data->gyro[1], sensitivity);
  410. break;
  411. case SENSOR_CHAN_GYRO_Z:
  412. lsm6dso_gyro_convert(val, data->gyro[2], sensitivity);
  413. break;
  414. case SENSOR_CHAN_GYRO_XYZ:
  415. for (i = 0; i < 3; i++) {
  416. lsm6dso_gyro_convert(val++, data->gyro[i], sensitivity);
  417. }
  418. break;
  419. default:
  420. return -ENOTSUP;
  421. }
  422. return 0;
  423. }
  424. static int lsm6dso_gyro_channel_get(enum sensor_channel chan,
  425. struct sensor_value *val,
  426. struct lsm6dso_data *data)
  427. {
  428. return lsm6dso_gyro_get_channel(chan, val, data,
  429. LSM6DSO_DEFAULT_GYRO_SENSITIVITY);
  430. }
  431. #if defined(CONFIG_LSM6DSO_ENABLE_TEMP)
  432. static void lsm6dso_gyro_channel_get_temp(struct sensor_value *val,
  433. struct lsm6dso_data *data)
  434. {
  435. /* val = temp_sample / 256 + 25 */
  436. val->val1 = data->temp_sample / 256 + 25;
  437. val->val2 = (data->temp_sample % 256) * (1000000 / 256);
  438. }
  439. #endif
  440. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  441. static inline void lsm6dso_magn_convert(struct sensor_value *val, int raw_val,
  442. uint16_t sensitivity)
  443. {
  444. double dval;
  445. /* Sensitivity is exposed in mgauss/LSB */
  446. dval = (double)(raw_val * sensitivity);
  447. val->val1 = (int32_t)dval / 1000000;
  448. val->val2 = (int32_t)dval % 1000000;
  449. }
  450. static inline int lsm6dso_magn_get_channel(enum sensor_channel chan,
  451. struct sensor_value *val,
  452. struct lsm6dso_data *data)
  453. {
  454. int16_t sample[3];
  455. int idx;
  456. idx = lsm6dso_shub_get_idx(data->dev, SENSOR_CHAN_MAGN_XYZ);
  457. if (idx < 0) {
  458. LOG_DBG("external magn not supported");
  459. return -ENOTSUP;
  460. }
  461. sample[0] = sys_le16_to_cpu((int16_t)(data->ext_data[idx][0] |
  462. (data->ext_data[idx][1] << 8)));
  463. sample[1] = sys_le16_to_cpu((int16_t)(data->ext_data[idx][2] |
  464. (data->ext_data[idx][3] << 8)));
  465. sample[2] = sys_le16_to_cpu((int16_t)(data->ext_data[idx][4] |
  466. (data->ext_data[idx][5] << 8)));
  467. switch (chan) {
  468. case SENSOR_CHAN_MAGN_X:
  469. lsm6dso_magn_convert(val, sample[0], data->magn_gain);
  470. break;
  471. case SENSOR_CHAN_MAGN_Y:
  472. lsm6dso_magn_convert(val, sample[1], data->magn_gain);
  473. break;
  474. case SENSOR_CHAN_MAGN_Z:
  475. lsm6dso_magn_convert(val, sample[2], data->magn_gain);
  476. break;
  477. case SENSOR_CHAN_MAGN_XYZ:
  478. lsm6dso_magn_convert(val, sample[0], data->magn_gain);
  479. lsm6dso_magn_convert(val + 1, sample[1], data->magn_gain);
  480. lsm6dso_magn_convert(val + 2, sample[2], data->magn_gain);
  481. break;
  482. default:
  483. return -ENOTSUP;
  484. }
  485. return 0;
  486. }
  487. static inline void lsm6dso_hum_convert(struct sensor_value *val,
  488. struct lsm6dso_data *data)
  489. {
  490. float rh;
  491. int16_t raw_val;
  492. struct hts221_data *ht = &data->hts221;
  493. int idx;
  494. idx = lsm6dso_shub_get_idx(data->dev, SENSOR_CHAN_HUMIDITY);
  495. if (idx < 0) {
  496. LOG_DBG("external press/temp not supported");
  497. return;
  498. }
  499. raw_val = sys_le16_to_cpu((int16_t)(data->ext_data[idx][0] |
  500. (data->ext_data[idx][1] << 8)));
  501. /* find relative humidty by linear interpolation */
  502. rh = (ht->y1 - ht->y0) * raw_val + ht->x1 * ht->y0 - ht->x0 * ht->y1;
  503. rh /= (ht->x1 - ht->x0);
  504. /* convert humidity to integer and fractional part */
  505. val->val1 = rh;
  506. val->val2 = rh * 1000000;
  507. }
  508. static inline void lsm6dso_press_convert(struct sensor_value *val,
  509. struct lsm6dso_data *data)
  510. {
  511. int32_t raw_val;
  512. int idx;
  513. idx = lsm6dso_shub_get_idx(data->dev, SENSOR_CHAN_PRESS);
  514. if (idx < 0) {
  515. LOG_DBG("external press/temp not supported");
  516. return;
  517. }
  518. raw_val = sys_le32_to_cpu((int32_t)(data->ext_data[idx][0] |
  519. (data->ext_data[idx][1] << 8) |
  520. (data->ext_data[idx][2] << 16)));
  521. /* Pressure sensitivity is 4096 LSB/hPa */
  522. /* Convert raw_val to val in kPa */
  523. val->val1 = (raw_val >> 12) / 10;
  524. val->val2 = (raw_val >> 12) % 10 * 100000 +
  525. (((int32_t)((raw_val) & 0x0FFF) * 100000L) >> 12);
  526. }
  527. static inline void lsm6dso_temp_convert(struct sensor_value *val,
  528. struct lsm6dso_data *data)
  529. {
  530. int16_t raw_val;
  531. int idx;
  532. idx = lsm6dso_shub_get_idx(data->dev, SENSOR_CHAN_PRESS);
  533. if (idx < 0) {
  534. LOG_DBG("external press/temp not supported");
  535. return;
  536. }
  537. raw_val = sys_le16_to_cpu((int16_t)(data->ext_data[idx][3] |
  538. (data->ext_data[idx][4] << 8)));
  539. /* Temperature sensitivity is 100 LSB/deg C */
  540. val->val1 = raw_val / 100;
  541. val->val2 = (int32_t)raw_val % 100 * (10000);
  542. }
  543. #endif
  544. static int lsm6dso_channel_get(const struct device *dev,
  545. enum sensor_channel chan,
  546. struct sensor_value *val)
  547. {
  548. struct lsm6dso_data *data = dev->data;
  549. switch (chan) {
  550. case SENSOR_CHAN_ACCEL_X:
  551. case SENSOR_CHAN_ACCEL_Y:
  552. case SENSOR_CHAN_ACCEL_Z:
  553. case SENSOR_CHAN_ACCEL_XYZ:
  554. lsm6dso_accel_channel_get(chan, val, data);
  555. break;
  556. case SENSOR_CHAN_GYRO_X:
  557. case SENSOR_CHAN_GYRO_Y:
  558. case SENSOR_CHAN_GYRO_Z:
  559. case SENSOR_CHAN_GYRO_XYZ:
  560. lsm6dso_gyro_channel_get(chan, val, data);
  561. break;
  562. #if defined(CONFIG_LSM6DSO_ENABLE_TEMP)
  563. case SENSOR_CHAN_DIE_TEMP:
  564. lsm6dso_gyro_channel_get_temp(val, data);
  565. break;
  566. #endif
  567. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  568. case SENSOR_CHAN_MAGN_X:
  569. case SENSOR_CHAN_MAGN_Y:
  570. case SENSOR_CHAN_MAGN_Z:
  571. case SENSOR_CHAN_MAGN_XYZ:
  572. if (!data->shub_inited) {
  573. LOG_ERR("attr_set() shub not inited.");
  574. return -ENOTSUP;
  575. }
  576. lsm6dso_magn_get_channel(chan, val, data);
  577. break;
  578. case SENSOR_CHAN_HUMIDITY:
  579. if (!data->shub_inited) {
  580. LOG_ERR("attr_set() shub not inited.");
  581. return -ENOTSUP;
  582. }
  583. lsm6dso_hum_convert(val, data);
  584. break;
  585. case SENSOR_CHAN_PRESS:
  586. if (!data->shub_inited) {
  587. LOG_ERR("attr_set() shub not inited.");
  588. return -ENOTSUP;
  589. }
  590. lsm6dso_press_convert(val, data);
  591. break;
  592. case SENSOR_CHAN_AMBIENT_TEMP:
  593. if (!data->shub_inited) {
  594. LOG_ERR("attr_set() shub not inited.");
  595. return -ENOTSUP;
  596. }
  597. lsm6dso_temp_convert(val, data);
  598. break;
  599. #endif
  600. default:
  601. return -ENOTSUP;
  602. }
  603. return 0;
  604. }
  605. static const struct sensor_driver_api lsm6dso_driver_api = {
  606. .attr_set = lsm6dso_attr_set,
  607. #if CONFIG_LSM6DSO_TRIGGER
  608. .trigger_set = lsm6dso_trigger_set,
  609. #endif
  610. .sample_fetch = lsm6dso_sample_fetch,
  611. .channel_get = lsm6dso_channel_get,
  612. };
  613. static int lsm6dso_init_chip(const struct device *dev)
  614. {
  615. const struct lsm6dso_config *cfg = dev->config;
  616. stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
  617. struct lsm6dso_data *lsm6dso = dev->data;
  618. uint8_t chip_id;
  619. if (lsm6dso_device_id_get(ctx, &chip_id) < 0) {
  620. LOG_DBG("Failed reading chip id");
  621. return -EIO;
  622. }
  623. LOG_INF("chip id 0x%x", chip_id);
  624. if (chip_id != LSM6DSO_ID) {
  625. LOG_DBG("Invalid chip id 0x%x", chip_id);
  626. return -EIO;
  627. }
  628. /* reset device */
  629. if (lsm6dso_reset_set(ctx, 1) < 0) {
  630. return -EIO;
  631. }
  632. k_busy_wait(100);
  633. if (lsm6dso_accel_set_fs_raw(dev,
  634. LSM6DSO_DEFAULT_ACCEL_FULLSCALE) < 0) {
  635. LOG_DBG("failed to set accelerometer full-scale");
  636. return -EIO;
  637. }
  638. lsm6dso->acc_gain = LSM6DSO_DEFAULT_ACCEL_SENSITIVITY;
  639. lsm6dso->accel_freq = lsm6dso_odr_to_freq_val(CONFIG_LSM6DSO_ACCEL_ODR);
  640. if (lsm6dso_accel_set_odr_raw(dev, CONFIG_LSM6DSO_ACCEL_ODR) < 0) {
  641. LOG_DBG("failed to set accelerometer sampling rate");
  642. return -EIO;
  643. }
  644. if (lsm6dso_gyro_set_fs_raw(dev, LSM6DSO_DEFAULT_GYRO_FULLSCALE) < 0) {
  645. LOG_DBG("failed to set gyroscope full-scale");
  646. return -EIO;
  647. }
  648. lsm6dso->gyro_gain = LSM6DSO_DEFAULT_GYRO_SENSITIVITY;
  649. lsm6dso->gyro_freq = lsm6dso_odr_to_freq_val(CONFIG_LSM6DSO_GYRO_ODR);
  650. if (lsm6dso_gyro_set_odr_raw(dev, CONFIG_LSM6DSO_GYRO_ODR) < 0) {
  651. LOG_DBG("failed to set gyroscope sampling rate");
  652. return -EIO;
  653. }
  654. /* Set FIFO bypass mode */
  655. if (lsm6dso_fifo_mode_set(ctx, LSM6DSO_BYPASS_MODE) < 0) {
  656. LOG_DBG("failed to set FIFO mode");
  657. return -EIO;
  658. }
  659. if (lsm6dso_block_data_update_set(ctx, 1) < 0) {
  660. LOG_DBG("failed to set BDU mode");
  661. return -EIO;
  662. }
  663. return 0;
  664. }
  665. static int lsm6dso_init(const struct device *dev)
  666. {
  667. #ifdef CONFIG_LSM6DSO_TRIGGER
  668. const struct lsm6dso_config *cfg = dev->config;
  669. #endif
  670. struct lsm6dso_data *data = dev->data;
  671. LOG_INF("Initialize device %s", dev->name);
  672. data->dev = dev;
  673. #ifdef CONFIG_LSM6DSO_TRIGGER
  674. if (cfg->trig_enabled) {
  675. if (lsm6dso_init_interrupt(dev) < 0) {
  676. LOG_ERR("Failed to initialize interrupt.");
  677. return -EIO;
  678. }
  679. }
  680. #endif
  681. if (lsm6dso_init_chip(dev) < 0) {
  682. LOG_DBG("failed to initialize chip");
  683. return -EIO;
  684. }
  685. #ifdef CONFIG_LSM6DSO_SENSORHUB
  686. data->shub_inited = true;
  687. if (lsm6dso_shub_init(dev) < 0) {
  688. LOG_INF("shub: no external chips found");
  689. data->shub_inited = false;
  690. }
  691. #endif
  692. return 0;
  693. }
  694. #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
  695. #warning "LSM6DSO driver enabled without any devices"
  696. #endif
  697. /*
  698. * Device creation macro, shared by LSM6DSO_DEFINE_SPI() and
  699. * LSM6DSO_DEFINE_I2C().
  700. */
  701. #define LSM6DSO_DEVICE_INIT(inst) \
  702. DEVICE_DT_INST_DEFINE(inst, \
  703. lsm6dso_init, \
  704. NULL, \
  705. &lsm6dso_data_##inst, \
  706. &lsm6dso_config_##inst, \
  707. POST_KERNEL, \
  708. CONFIG_SENSOR_INIT_PRIORITY, \
  709. &lsm6dso_driver_api);
  710. /*
  711. * Instantiation macros used when a device is on a SPI bus.
  712. */
  713. #ifdef CONFIG_LSM6DSO_TRIGGER
  714. #define LSM6DSO_CFG_IRQ(inst) \
  715. .trig_enabled = true, \
  716. .gpio_drdy = GPIO_DT_SPEC_INST_GET(inst, irq_gpios), \
  717. .int_pin = DT_INST_PROP(inst, int_pin)
  718. #else
  719. #define LSM6DSO_CFG_IRQ(inst)
  720. #endif /* CONFIG_LSM6DSO_TRIGGER */
  721. #define LSM6DSO_SPI_OP (SPI_WORD_SET(8) | \
  722. SPI_OP_MODE_MASTER | \
  723. SPI_LINES_SINGLE | \
  724. SPI_MODE_CPOL | \
  725. SPI_MODE_CPHA) \
  726. #define LSM6DSO_CONFIG_SPI(inst) \
  727. { \
  728. .ctx = { \
  729. .read_reg = \
  730. (stmdev_read_ptr) stmemsc_spi_read, \
  731. .write_reg = \
  732. (stmdev_write_ptr) stmemsc_spi_write, \
  733. .handle = \
  734. (void *)&lsm6dso_config_##inst.stmemsc_cfg, \
  735. }, \
  736. .stmemsc_cfg.spi = { \
  737. .bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
  738. .spi_cfg = SPI_CONFIG_DT_INST(inst, \
  739. LSM6DSO_SPI_OP, \
  740. 0), \
  741. }, \
  742. COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
  743. (LSM6DSO_CFG_IRQ(inst)), ()) \
  744. }
  745. /*
  746. * Instantiation macros used when a device is on an I2C bus.
  747. */
  748. #define LSM6DSO_CONFIG_I2C(inst) \
  749. { \
  750. .ctx = { \
  751. .read_reg = \
  752. (stmdev_read_ptr) stmemsc_i2c_read, \
  753. .write_reg = \
  754. (stmdev_write_ptr) stmemsc_i2c_write, \
  755. .handle = \
  756. (void *)&lsm6dso_config_##inst.stmemsc_cfg, \
  757. }, \
  758. .stmemsc_cfg.i2c = { \
  759. .bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
  760. .i2c_slv_addr = DT_INST_REG_ADDR(inst), \
  761. }, \
  762. COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
  763. (LSM6DSO_CFG_IRQ(inst)), ()) \
  764. }
  765. /*
  766. * Main instantiation macro. Use of COND_CODE_1() selects the right
  767. * bus-specific macro at preprocessor time.
  768. */
  769. #define LSM6DSO_DEFINE(inst) \
  770. static struct lsm6dso_data lsm6dso_data_##inst; \
  771. static const struct lsm6dso_config lsm6dso_config_##inst = \
  772. COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
  773. (LSM6DSO_CONFIG_SPI(inst)), \
  774. (LSM6DSO_CONFIG_I2C(inst))); \
  775. LSM6DSO_DEVICE_INIT(inst)
  776. DT_INST_FOREACH_STATUS_OKAY(LSM6DSO_DEFINE)