bmg160_core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. /*
  2. * BMG160 Gyro Sensor driver
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/acpi.h>
  19. #include <linux/pm.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/trigger.h>
  25. #include <linux/iio/events.h>
  26. #include <linux/iio/trigger_consumer.h>
  27. #include <linux/iio/triggered_buffer.h>
  28. #include <linux/regmap.h>
  29. #include "bmg160.h"
  30. #define BMG160_IRQ_NAME "bmg160_event"
  31. #define BMG160_REG_CHIP_ID 0x00
  32. #define BMG160_CHIP_ID_VAL 0x0F
  33. #define BMG160_REG_PMU_LPW 0x11
  34. #define BMG160_MODE_NORMAL 0x00
  35. #define BMG160_MODE_DEEP_SUSPEND 0x20
  36. #define BMG160_MODE_SUSPEND 0x80
  37. #define BMG160_REG_RANGE 0x0F
  38. #define BMG160_RANGE_2000DPS 0
  39. #define BMG160_RANGE_1000DPS 1
  40. #define BMG160_RANGE_500DPS 2
  41. #define BMG160_RANGE_250DPS 3
  42. #define BMG160_RANGE_125DPS 4
  43. #define BMG160_REG_PMU_BW 0x10
  44. #define BMG160_NO_FILTER 0
  45. #define BMG160_DEF_BW 100
  46. #define BMG160_REG_PMU_BW_RES BIT(7)
  47. #define BMG160_REG_INT_MAP_0 0x17
  48. #define BMG160_INT_MAP_0_BIT_ANY BIT(1)
  49. #define BMG160_REG_INT_MAP_1 0x18
  50. #define BMG160_INT_MAP_1_BIT_NEW_DATA BIT(0)
  51. #define BMG160_REG_INT_RST_LATCH 0x21
  52. #define BMG160_INT_MODE_LATCH_RESET 0x80
  53. #define BMG160_INT_MODE_LATCH_INT 0x0F
  54. #define BMG160_INT_MODE_NON_LATCH_INT 0x00
  55. #define BMG160_REG_INT_EN_0 0x15
  56. #define BMG160_DATA_ENABLE_INT BIT(7)
  57. #define BMG160_REG_INT_EN_1 0x16
  58. #define BMG160_INT1_BIT_OD BIT(1)
  59. #define BMG160_REG_XOUT_L 0x02
  60. #define BMG160_AXIS_TO_REG(axis) (BMG160_REG_XOUT_L + (axis * 2))
  61. #define BMG160_REG_SLOPE_THRES 0x1B
  62. #define BMG160_SLOPE_THRES_MASK 0x0F
  63. #define BMG160_REG_MOTION_INTR 0x1C
  64. #define BMG160_INT_MOTION_X BIT(0)
  65. #define BMG160_INT_MOTION_Y BIT(1)
  66. #define BMG160_INT_MOTION_Z BIT(2)
  67. #define BMG160_ANY_DUR_MASK 0x30
  68. #define BMG160_ANY_DUR_SHIFT 4
  69. #define BMG160_REG_INT_STATUS_2 0x0B
  70. #define BMG160_ANY_MOTION_MASK 0x07
  71. #define BMG160_ANY_MOTION_BIT_X BIT(0)
  72. #define BMG160_ANY_MOTION_BIT_Y BIT(1)
  73. #define BMG160_ANY_MOTION_BIT_Z BIT(2)
  74. #define BMG160_REG_TEMP 0x08
  75. #define BMG160_TEMP_CENTER_VAL 23
  76. #define BMG160_MAX_STARTUP_TIME_MS 80
  77. #define BMG160_AUTO_SUSPEND_DELAY_MS 2000
  78. struct bmg160_data {
  79. struct regmap *regmap;
  80. struct iio_trigger *dready_trig;
  81. struct iio_trigger *motion_trig;
  82. struct mutex mutex;
  83. s16 buffer[8];
  84. u32 dps_range;
  85. int ev_enable_state;
  86. int slope_thres;
  87. bool dready_trigger_on;
  88. bool motion_trigger_on;
  89. int irq;
  90. };
  91. enum bmg160_axis {
  92. AXIS_X,
  93. AXIS_Y,
  94. AXIS_Z,
  95. AXIS_MAX,
  96. };
  97. static const struct {
  98. int odr;
  99. int filter;
  100. int bw_bits;
  101. } bmg160_samp_freq_table[] = { {100, 32, 0x07},
  102. {200, 64, 0x06},
  103. {100, 12, 0x05},
  104. {200, 23, 0x04},
  105. {400, 47, 0x03},
  106. {1000, 116, 0x02},
  107. {2000, 230, 0x01} };
  108. static const struct {
  109. int scale;
  110. int dps_range;
  111. } bmg160_scale_table[] = { { 1065, BMG160_RANGE_2000DPS},
  112. { 532, BMG160_RANGE_1000DPS},
  113. { 266, BMG160_RANGE_500DPS},
  114. { 133, BMG160_RANGE_250DPS},
  115. { 66, BMG160_RANGE_125DPS} };
  116. static int bmg160_set_mode(struct bmg160_data *data, u8 mode)
  117. {
  118. struct device *dev = regmap_get_device(data->regmap);
  119. int ret;
  120. ret = regmap_write(data->regmap, BMG160_REG_PMU_LPW, mode);
  121. if (ret < 0) {
  122. dev_err(dev, "Error writing reg_pmu_lpw\n");
  123. return ret;
  124. }
  125. return 0;
  126. }
  127. static int bmg160_convert_freq_to_bit(int val)
  128. {
  129. int i;
  130. for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
  131. if (bmg160_samp_freq_table[i].odr == val)
  132. return bmg160_samp_freq_table[i].bw_bits;
  133. }
  134. return -EINVAL;
  135. }
  136. static int bmg160_set_bw(struct bmg160_data *data, int val)
  137. {
  138. struct device *dev = regmap_get_device(data->regmap);
  139. int ret;
  140. int bw_bits;
  141. bw_bits = bmg160_convert_freq_to_bit(val);
  142. if (bw_bits < 0)
  143. return bw_bits;
  144. ret = regmap_write(data->regmap, BMG160_REG_PMU_BW, bw_bits);
  145. if (ret < 0) {
  146. dev_err(dev, "Error writing reg_pmu_bw\n");
  147. return ret;
  148. }
  149. return 0;
  150. }
  151. static int bmg160_get_filter(struct bmg160_data *data, int *val)
  152. {
  153. struct device *dev = regmap_get_device(data->regmap);
  154. int ret;
  155. int i;
  156. unsigned int bw_bits;
  157. ret = regmap_read(data->regmap, BMG160_REG_PMU_BW, &bw_bits);
  158. if (ret < 0) {
  159. dev_err(dev, "Error reading reg_pmu_bw\n");
  160. return ret;
  161. }
  162. /* Ignore the readonly reserved bit. */
  163. bw_bits &= ~BMG160_REG_PMU_BW_RES;
  164. for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
  165. if (bmg160_samp_freq_table[i].bw_bits == bw_bits)
  166. break;
  167. }
  168. *val = bmg160_samp_freq_table[i].filter;
  169. return ret ? ret : IIO_VAL_INT;
  170. }
  171. static int bmg160_set_filter(struct bmg160_data *data, int val)
  172. {
  173. struct device *dev = regmap_get_device(data->regmap);
  174. int ret;
  175. int i;
  176. for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
  177. if (bmg160_samp_freq_table[i].filter == val)
  178. break;
  179. }
  180. ret = regmap_write(data->regmap, BMG160_REG_PMU_BW,
  181. bmg160_samp_freq_table[i].bw_bits);
  182. if (ret < 0) {
  183. dev_err(dev, "Error writing reg_pmu_bw\n");
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static int bmg160_chip_init(struct bmg160_data *data)
  189. {
  190. struct device *dev = regmap_get_device(data->regmap);
  191. int ret;
  192. unsigned int val;
  193. ret = regmap_read(data->regmap, BMG160_REG_CHIP_ID, &val);
  194. if (ret < 0) {
  195. dev_err(dev, "Error reading reg_chip_id\n");
  196. return ret;
  197. }
  198. dev_dbg(dev, "Chip Id %x\n", val);
  199. if (val != BMG160_CHIP_ID_VAL) {
  200. dev_err(dev, "invalid chip %x\n", val);
  201. return -ENODEV;
  202. }
  203. ret = bmg160_set_mode(data, BMG160_MODE_NORMAL);
  204. if (ret < 0)
  205. return ret;
  206. /* Wait upto 500 ms to be ready after changing mode */
  207. usleep_range(500, 1000);
  208. /* Set Bandwidth */
  209. ret = bmg160_set_bw(data, BMG160_DEF_BW);
  210. if (ret < 0)
  211. return ret;
  212. /* Set Default Range */
  213. ret = regmap_write(data->regmap, BMG160_REG_RANGE, BMG160_RANGE_500DPS);
  214. if (ret < 0) {
  215. dev_err(dev, "Error writing reg_range\n");
  216. return ret;
  217. }
  218. data->dps_range = BMG160_RANGE_500DPS;
  219. ret = regmap_read(data->regmap, BMG160_REG_SLOPE_THRES, &val);
  220. if (ret < 0) {
  221. dev_err(dev, "Error reading reg_slope_thres\n");
  222. return ret;
  223. }
  224. data->slope_thres = val;
  225. /* Set default interrupt mode */
  226. ret = regmap_update_bits(data->regmap, BMG160_REG_INT_EN_1,
  227. BMG160_INT1_BIT_OD, 0);
  228. if (ret < 0) {
  229. dev_err(dev, "Error updating bits in reg_int_en_1\n");
  230. return ret;
  231. }
  232. ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
  233. BMG160_INT_MODE_LATCH_INT |
  234. BMG160_INT_MODE_LATCH_RESET);
  235. if (ret < 0) {
  236. dev_err(dev,
  237. "Error writing reg_motion_intr\n");
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. static int bmg160_set_power_state(struct bmg160_data *data, bool on)
  243. {
  244. #ifdef CONFIG_PM
  245. struct device *dev = regmap_get_device(data->regmap);
  246. int ret;
  247. if (on)
  248. ret = pm_runtime_get_sync(dev);
  249. else {
  250. pm_runtime_mark_last_busy(dev);
  251. ret = pm_runtime_put_autosuspend(dev);
  252. }
  253. if (ret < 0) {
  254. dev_err(dev, "Failed: bmg160_set_power_state for %d\n", on);
  255. if (on)
  256. pm_runtime_put_noidle(dev);
  257. return ret;
  258. }
  259. #endif
  260. return 0;
  261. }
  262. static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
  263. bool status)
  264. {
  265. struct device *dev = regmap_get_device(data->regmap);
  266. int ret;
  267. /* Enable/Disable INT_MAP0 mapping */
  268. ret = regmap_update_bits(data->regmap, BMG160_REG_INT_MAP_0,
  269. BMG160_INT_MAP_0_BIT_ANY,
  270. (status ? BMG160_INT_MAP_0_BIT_ANY : 0));
  271. if (ret < 0) {
  272. dev_err(dev, "Error updating bits reg_int_map0\n");
  273. return ret;
  274. }
  275. /* Enable/Disable slope interrupts */
  276. if (status) {
  277. /* Update slope thres */
  278. ret = regmap_write(data->regmap, BMG160_REG_SLOPE_THRES,
  279. data->slope_thres);
  280. if (ret < 0) {
  281. dev_err(dev, "Error writing reg_slope_thres\n");
  282. return ret;
  283. }
  284. ret = regmap_write(data->regmap, BMG160_REG_MOTION_INTR,
  285. BMG160_INT_MOTION_X | BMG160_INT_MOTION_Y |
  286. BMG160_INT_MOTION_Z);
  287. if (ret < 0) {
  288. dev_err(dev, "Error writing reg_motion_intr\n");
  289. return ret;
  290. }
  291. /*
  292. * New data interrupt is always non-latched,
  293. * which will have higher priority, so no need
  294. * to set latched mode, we will be flooded anyway with INTR
  295. */
  296. if (!data->dready_trigger_on) {
  297. ret = regmap_write(data->regmap,
  298. BMG160_REG_INT_RST_LATCH,
  299. BMG160_INT_MODE_LATCH_INT |
  300. BMG160_INT_MODE_LATCH_RESET);
  301. if (ret < 0) {
  302. dev_err(dev, "Error writing reg_rst_latch\n");
  303. return ret;
  304. }
  305. }
  306. ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0,
  307. BMG160_DATA_ENABLE_INT);
  308. } else {
  309. ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0);
  310. }
  311. if (ret < 0) {
  312. dev_err(dev, "Error writing reg_int_en0\n");
  313. return ret;
  314. }
  315. return 0;
  316. }
  317. static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
  318. bool status)
  319. {
  320. struct device *dev = regmap_get_device(data->regmap);
  321. int ret;
  322. /* Enable/Disable INT_MAP1 mapping */
  323. ret = regmap_update_bits(data->regmap, BMG160_REG_INT_MAP_1,
  324. BMG160_INT_MAP_1_BIT_NEW_DATA,
  325. (status ? BMG160_INT_MAP_1_BIT_NEW_DATA : 0));
  326. if (ret < 0) {
  327. dev_err(dev, "Error updating bits in reg_int_map1\n");
  328. return ret;
  329. }
  330. if (status) {
  331. ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
  332. BMG160_INT_MODE_NON_LATCH_INT |
  333. BMG160_INT_MODE_LATCH_RESET);
  334. if (ret < 0) {
  335. dev_err(dev, "Error writing reg_rst_latch\n");
  336. return ret;
  337. }
  338. ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0,
  339. BMG160_DATA_ENABLE_INT);
  340. } else {
  341. /* Restore interrupt mode */
  342. ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
  343. BMG160_INT_MODE_LATCH_INT |
  344. BMG160_INT_MODE_LATCH_RESET);
  345. if (ret < 0) {
  346. dev_err(dev, "Error writing reg_rst_latch\n");
  347. return ret;
  348. }
  349. ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0);
  350. }
  351. if (ret < 0) {
  352. dev_err(dev, "Error writing reg_int_en0\n");
  353. return ret;
  354. }
  355. return 0;
  356. }
  357. static int bmg160_get_bw(struct bmg160_data *data, int *val)
  358. {
  359. struct device *dev = regmap_get_device(data->regmap);
  360. int i;
  361. unsigned int bw_bits;
  362. int ret;
  363. ret = regmap_read(data->regmap, BMG160_REG_PMU_BW, &bw_bits);
  364. if (ret < 0) {
  365. dev_err(dev, "Error reading reg_pmu_bw\n");
  366. return ret;
  367. }
  368. /* Ignore the readonly reserved bit. */
  369. bw_bits &= ~BMG160_REG_PMU_BW_RES;
  370. for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
  371. if (bmg160_samp_freq_table[i].bw_bits == bw_bits) {
  372. *val = bmg160_samp_freq_table[i].odr;
  373. return IIO_VAL_INT;
  374. }
  375. }
  376. return -EINVAL;
  377. }
  378. static int bmg160_set_scale(struct bmg160_data *data, int val)
  379. {
  380. struct device *dev = regmap_get_device(data->regmap);
  381. int ret, i;
  382. for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) {
  383. if (bmg160_scale_table[i].scale == val) {
  384. ret = regmap_write(data->regmap, BMG160_REG_RANGE,
  385. bmg160_scale_table[i].dps_range);
  386. if (ret < 0) {
  387. dev_err(dev, "Error writing reg_range\n");
  388. return ret;
  389. }
  390. data->dps_range = bmg160_scale_table[i].dps_range;
  391. return 0;
  392. }
  393. }
  394. return -EINVAL;
  395. }
  396. static int bmg160_get_temp(struct bmg160_data *data, int *val)
  397. {
  398. struct device *dev = regmap_get_device(data->regmap);
  399. int ret;
  400. unsigned int raw_val;
  401. mutex_lock(&data->mutex);
  402. ret = bmg160_set_power_state(data, true);
  403. if (ret < 0) {
  404. mutex_unlock(&data->mutex);
  405. return ret;
  406. }
  407. ret = regmap_read(data->regmap, BMG160_REG_TEMP, &raw_val);
  408. if (ret < 0) {
  409. dev_err(dev, "Error reading reg_temp\n");
  410. bmg160_set_power_state(data, false);
  411. mutex_unlock(&data->mutex);
  412. return ret;
  413. }
  414. *val = sign_extend32(raw_val, 7);
  415. ret = bmg160_set_power_state(data, false);
  416. mutex_unlock(&data->mutex);
  417. if (ret < 0)
  418. return ret;
  419. return IIO_VAL_INT;
  420. }
  421. static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val)
  422. {
  423. struct device *dev = regmap_get_device(data->regmap);
  424. int ret;
  425. __le16 raw_val;
  426. mutex_lock(&data->mutex);
  427. ret = bmg160_set_power_state(data, true);
  428. if (ret < 0) {
  429. mutex_unlock(&data->mutex);
  430. return ret;
  431. }
  432. ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val,
  433. sizeof(raw_val));
  434. if (ret < 0) {
  435. dev_err(dev, "Error reading axis %d\n", axis);
  436. bmg160_set_power_state(data, false);
  437. mutex_unlock(&data->mutex);
  438. return ret;
  439. }
  440. *val = sign_extend32(le16_to_cpu(raw_val), 15);
  441. ret = bmg160_set_power_state(data, false);
  442. mutex_unlock(&data->mutex);
  443. if (ret < 0)
  444. return ret;
  445. return IIO_VAL_INT;
  446. }
  447. static int bmg160_read_raw(struct iio_dev *indio_dev,
  448. struct iio_chan_spec const *chan,
  449. int *val, int *val2, long mask)
  450. {
  451. struct bmg160_data *data = iio_priv(indio_dev);
  452. int ret;
  453. switch (mask) {
  454. case IIO_CHAN_INFO_RAW:
  455. switch (chan->type) {
  456. case IIO_TEMP:
  457. return bmg160_get_temp(data, val);
  458. case IIO_ANGL_VEL:
  459. if (iio_buffer_enabled(indio_dev))
  460. return -EBUSY;
  461. else
  462. return bmg160_get_axis(data, chan->scan_index,
  463. val);
  464. default:
  465. return -EINVAL;
  466. }
  467. case IIO_CHAN_INFO_OFFSET:
  468. if (chan->type == IIO_TEMP) {
  469. *val = BMG160_TEMP_CENTER_VAL;
  470. return IIO_VAL_INT;
  471. } else
  472. return -EINVAL;
  473. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  474. return bmg160_get_filter(data, val);
  475. case IIO_CHAN_INFO_SCALE:
  476. *val = 0;
  477. switch (chan->type) {
  478. case IIO_TEMP:
  479. *val2 = 500000;
  480. return IIO_VAL_INT_PLUS_MICRO;
  481. case IIO_ANGL_VEL:
  482. {
  483. int i;
  484. for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) {
  485. if (bmg160_scale_table[i].dps_range ==
  486. data->dps_range) {
  487. *val2 = bmg160_scale_table[i].scale;
  488. return IIO_VAL_INT_PLUS_MICRO;
  489. }
  490. }
  491. return -EINVAL;
  492. }
  493. default:
  494. return -EINVAL;
  495. }
  496. case IIO_CHAN_INFO_SAMP_FREQ:
  497. *val2 = 0;
  498. mutex_lock(&data->mutex);
  499. ret = bmg160_get_bw(data, val);
  500. mutex_unlock(&data->mutex);
  501. return ret;
  502. default:
  503. return -EINVAL;
  504. }
  505. }
  506. static int bmg160_write_raw(struct iio_dev *indio_dev,
  507. struct iio_chan_spec const *chan,
  508. int val, int val2, long mask)
  509. {
  510. struct bmg160_data *data = iio_priv(indio_dev);
  511. int ret;
  512. switch (mask) {
  513. case IIO_CHAN_INFO_SAMP_FREQ:
  514. mutex_lock(&data->mutex);
  515. /*
  516. * Section 4.2 of spec
  517. * In suspend mode, the only supported operations are reading
  518. * registers as well as writing to the (0x14) softreset
  519. * register. Since we will be in suspend mode by default, change
  520. * mode to power on for other writes.
  521. */
  522. ret = bmg160_set_power_state(data, true);
  523. if (ret < 0) {
  524. mutex_unlock(&data->mutex);
  525. return ret;
  526. }
  527. ret = bmg160_set_bw(data, val);
  528. if (ret < 0) {
  529. bmg160_set_power_state(data, false);
  530. mutex_unlock(&data->mutex);
  531. return ret;
  532. }
  533. ret = bmg160_set_power_state(data, false);
  534. mutex_unlock(&data->mutex);
  535. return ret;
  536. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  537. if (val2)
  538. return -EINVAL;
  539. mutex_lock(&data->mutex);
  540. ret = bmg160_set_power_state(data, true);
  541. if (ret < 0) {
  542. bmg160_set_power_state(data, false);
  543. mutex_unlock(&data->mutex);
  544. return ret;
  545. }
  546. ret = bmg160_set_filter(data, val);
  547. if (ret < 0) {
  548. bmg160_set_power_state(data, false);
  549. mutex_unlock(&data->mutex);
  550. return ret;
  551. }
  552. ret = bmg160_set_power_state(data, false);
  553. mutex_unlock(&data->mutex);
  554. return ret;
  555. case IIO_CHAN_INFO_SCALE:
  556. if (val)
  557. return -EINVAL;
  558. mutex_lock(&data->mutex);
  559. /* Refer to comments above for the suspend mode ops */
  560. ret = bmg160_set_power_state(data, true);
  561. if (ret < 0) {
  562. mutex_unlock(&data->mutex);
  563. return ret;
  564. }
  565. ret = bmg160_set_scale(data, val2);
  566. if (ret < 0) {
  567. bmg160_set_power_state(data, false);
  568. mutex_unlock(&data->mutex);
  569. return ret;
  570. }
  571. ret = bmg160_set_power_state(data, false);
  572. mutex_unlock(&data->mutex);
  573. return ret;
  574. default:
  575. return -EINVAL;
  576. }
  577. return -EINVAL;
  578. }
  579. static int bmg160_read_event(struct iio_dev *indio_dev,
  580. const struct iio_chan_spec *chan,
  581. enum iio_event_type type,
  582. enum iio_event_direction dir,
  583. enum iio_event_info info,
  584. int *val, int *val2)
  585. {
  586. struct bmg160_data *data = iio_priv(indio_dev);
  587. *val2 = 0;
  588. switch (info) {
  589. case IIO_EV_INFO_VALUE:
  590. *val = data->slope_thres & BMG160_SLOPE_THRES_MASK;
  591. break;
  592. default:
  593. return -EINVAL;
  594. }
  595. return IIO_VAL_INT;
  596. }
  597. static int bmg160_write_event(struct iio_dev *indio_dev,
  598. const struct iio_chan_spec *chan,
  599. enum iio_event_type type,
  600. enum iio_event_direction dir,
  601. enum iio_event_info info,
  602. int val, int val2)
  603. {
  604. struct bmg160_data *data = iio_priv(indio_dev);
  605. switch (info) {
  606. case IIO_EV_INFO_VALUE:
  607. if (data->ev_enable_state)
  608. return -EBUSY;
  609. data->slope_thres &= ~BMG160_SLOPE_THRES_MASK;
  610. data->slope_thres |= (val & BMG160_SLOPE_THRES_MASK);
  611. break;
  612. default:
  613. return -EINVAL;
  614. }
  615. return 0;
  616. }
  617. static int bmg160_read_event_config(struct iio_dev *indio_dev,
  618. const struct iio_chan_spec *chan,
  619. enum iio_event_type type,
  620. enum iio_event_direction dir)
  621. {
  622. struct bmg160_data *data = iio_priv(indio_dev);
  623. return data->ev_enable_state;
  624. }
  625. static int bmg160_write_event_config(struct iio_dev *indio_dev,
  626. const struct iio_chan_spec *chan,
  627. enum iio_event_type type,
  628. enum iio_event_direction dir,
  629. int state)
  630. {
  631. struct bmg160_data *data = iio_priv(indio_dev);
  632. int ret;
  633. if (state && data->ev_enable_state)
  634. return 0;
  635. mutex_lock(&data->mutex);
  636. if (!state && data->motion_trigger_on) {
  637. data->ev_enable_state = 0;
  638. mutex_unlock(&data->mutex);
  639. return 0;
  640. }
  641. /*
  642. * We will expect the enable and disable to do operation in
  643. * in reverse order. This will happen here anyway as our
  644. * resume operation uses sync mode runtime pm calls, the
  645. * suspend operation will be delayed by autosuspend delay
  646. * So the disable operation will still happen in reverse of
  647. * enable operation. When runtime pm is disabled the mode
  648. * is always on so sequence doesn't matter
  649. */
  650. ret = bmg160_set_power_state(data, state);
  651. if (ret < 0) {
  652. mutex_unlock(&data->mutex);
  653. return ret;
  654. }
  655. ret = bmg160_setup_any_motion_interrupt(data, state);
  656. if (ret < 0) {
  657. bmg160_set_power_state(data, false);
  658. mutex_unlock(&data->mutex);
  659. return ret;
  660. }
  661. data->ev_enable_state = state;
  662. mutex_unlock(&data->mutex);
  663. return 0;
  664. }
  665. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000");
  666. static IIO_CONST_ATTR(in_anglvel_scale_available,
  667. "0.001065 0.000532 0.000266 0.000133 0.000066");
  668. static struct attribute *bmg160_attributes[] = {
  669. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  670. &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
  671. NULL,
  672. };
  673. static const struct attribute_group bmg160_attrs_group = {
  674. .attrs = bmg160_attributes,
  675. };
  676. static const struct iio_event_spec bmg160_event = {
  677. .type = IIO_EV_TYPE_ROC,
  678. .dir = IIO_EV_DIR_EITHER,
  679. .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
  680. BIT(IIO_EV_INFO_ENABLE)
  681. };
  682. #define BMG160_CHANNEL(_axis) { \
  683. .type = IIO_ANGL_VEL, \
  684. .modified = 1, \
  685. .channel2 = IIO_MOD_##_axis, \
  686. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  687. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  688. BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  689. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
  690. .scan_index = AXIS_##_axis, \
  691. .scan_type = { \
  692. .sign = 's', \
  693. .realbits = 16, \
  694. .storagebits = 16, \
  695. .endianness = IIO_LE, \
  696. }, \
  697. .event_spec = &bmg160_event, \
  698. .num_event_specs = 1 \
  699. }
  700. static const struct iio_chan_spec bmg160_channels[] = {
  701. {
  702. .type = IIO_TEMP,
  703. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  704. BIT(IIO_CHAN_INFO_SCALE) |
  705. BIT(IIO_CHAN_INFO_OFFSET),
  706. .scan_index = -1,
  707. },
  708. BMG160_CHANNEL(X),
  709. BMG160_CHANNEL(Y),
  710. BMG160_CHANNEL(Z),
  711. IIO_CHAN_SOFT_TIMESTAMP(3),
  712. };
  713. static const struct iio_info bmg160_info = {
  714. .attrs = &bmg160_attrs_group,
  715. .read_raw = bmg160_read_raw,
  716. .write_raw = bmg160_write_raw,
  717. .read_event_value = bmg160_read_event,
  718. .write_event_value = bmg160_write_event,
  719. .write_event_config = bmg160_write_event_config,
  720. .read_event_config = bmg160_read_event_config,
  721. .driver_module = THIS_MODULE,
  722. };
  723. static const unsigned long bmg160_accel_scan_masks[] = {
  724. BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
  725. 0};
  726. static irqreturn_t bmg160_trigger_handler(int irq, void *p)
  727. {
  728. struct iio_poll_func *pf = p;
  729. struct iio_dev *indio_dev = pf->indio_dev;
  730. struct bmg160_data *data = iio_priv(indio_dev);
  731. int ret;
  732. mutex_lock(&data->mutex);
  733. ret = regmap_bulk_read(data->regmap, BMG160_REG_XOUT_L,
  734. data->buffer, AXIS_MAX * 2);
  735. mutex_unlock(&data->mutex);
  736. if (ret < 0)
  737. goto err;
  738. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  739. pf->timestamp);
  740. err:
  741. iio_trigger_notify_done(indio_dev->trig);
  742. return IRQ_HANDLED;
  743. }
  744. static int bmg160_trig_try_reen(struct iio_trigger *trig)
  745. {
  746. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  747. struct bmg160_data *data = iio_priv(indio_dev);
  748. struct device *dev = regmap_get_device(data->regmap);
  749. int ret;
  750. /* new data interrupts don't need ack */
  751. if (data->dready_trigger_on)
  752. return 0;
  753. /* Set latched mode interrupt and clear any latched interrupt */
  754. ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
  755. BMG160_INT_MODE_LATCH_INT |
  756. BMG160_INT_MODE_LATCH_RESET);
  757. if (ret < 0) {
  758. dev_err(dev, "Error writing reg_rst_latch\n");
  759. return ret;
  760. }
  761. return 0;
  762. }
  763. static int bmg160_data_rdy_trigger_set_state(struct iio_trigger *trig,
  764. bool state)
  765. {
  766. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  767. struct bmg160_data *data = iio_priv(indio_dev);
  768. int ret;
  769. mutex_lock(&data->mutex);
  770. if (!state && data->ev_enable_state && data->motion_trigger_on) {
  771. data->motion_trigger_on = false;
  772. mutex_unlock(&data->mutex);
  773. return 0;
  774. }
  775. /*
  776. * Refer to comment in bmg160_write_event_config for
  777. * enable/disable operation order
  778. */
  779. ret = bmg160_set_power_state(data, state);
  780. if (ret < 0) {
  781. mutex_unlock(&data->mutex);
  782. return ret;
  783. }
  784. if (data->motion_trig == trig)
  785. ret = bmg160_setup_any_motion_interrupt(data, state);
  786. else
  787. ret = bmg160_setup_new_data_interrupt(data, state);
  788. if (ret < 0) {
  789. bmg160_set_power_state(data, false);
  790. mutex_unlock(&data->mutex);
  791. return ret;
  792. }
  793. if (data->motion_trig == trig)
  794. data->motion_trigger_on = state;
  795. else
  796. data->dready_trigger_on = state;
  797. mutex_unlock(&data->mutex);
  798. return 0;
  799. }
  800. static const struct iio_trigger_ops bmg160_trigger_ops = {
  801. .set_trigger_state = bmg160_data_rdy_trigger_set_state,
  802. .try_reenable = bmg160_trig_try_reen,
  803. .owner = THIS_MODULE,
  804. };
  805. static irqreturn_t bmg160_event_handler(int irq, void *private)
  806. {
  807. struct iio_dev *indio_dev = private;
  808. struct bmg160_data *data = iio_priv(indio_dev);
  809. struct device *dev = regmap_get_device(data->regmap);
  810. int ret;
  811. int dir;
  812. unsigned int val;
  813. ret = regmap_read(data->regmap, BMG160_REG_INT_STATUS_2, &val);
  814. if (ret < 0) {
  815. dev_err(dev, "Error reading reg_int_status2\n");
  816. goto ack_intr_status;
  817. }
  818. if (val & 0x08)
  819. dir = IIO_EV_DIR_RISING;
  820. else
  821. dir = IIO_EV_DIR_FALLING;
  822. if (val & BMG160_ANY_MOTION_BIT_X)
  823. iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
  824. 0,
  825. IIO_MOD_X,
  826. IIO_EV_TYPE_ROC,
  827. dir),
  828. iio_get_time_ns(indio_dev));
  829. if (val & BMG160_ANY_MOTION_BIT_Y)
  830. iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
  831. 0,
  832. IIO_MOD_Y,
  833. IIO_EV_TYPE_ROC,
  834. dir),
  835. iio_get_time_ns(indio_dev));
  836. if (val & BMG160_ANY_MOTION_BIT_Z)
  837. iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
  838. 0,
  839. IIO_MOD_Z,
  840. IIO_EV_TYPE_ROC,
  841. dir),
  842. iio_get_time_ns(indio_dev));
  843. ack_intr_status:
  844. if (!data->dready_trigger_on) {
  845. ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
  846. BMG160_INT_MODE_LATCH_INT |
  847. BMG160_INT_MODE_LATCH_RESET);
  848. if (ret < 0)
  849. dev_err(dev, "Error writing reg_rst_latch\n");
  850. }
  851. return IRQ_HANDLED;
  852. }
  853. static irqreturn_t bmg160_data_rdy_trig_poll(int irq, void *private)
  854. {
  855. struct iio_dev *indio_dev = private;
  856. struct bmg160_data *data = iio_priv(indio_dev);
  857. if (data->dready_trigger_on)
  858. iio_trigger_poll(data->dready_trig);
  859. else if (data->motion_trigger_on)
  860. iio_trigger_poll(data->motion_trig);
  861. if (data->ev_enable_state)
  862. return IRQ_WAKE_THREAD;
  863. else
  864. return IRQ_HANDLED;
  865. }
  866. static int bmg160_buffer_preenable(struct iio_dev *indio_dev)
  867. {
  868. struct bmg160_data *data = iio_priv(indio_dev);
  869. return bmg160_set_power_state(data, true);
  870. }
  871. static int bmg160_buffer_postdisable(struct iio_dev *indio_dev)
  872. {
  873. struct bmg160_data *data = iio_priv(indio_dev);
  874. return bmg160_set_power_state(data, false);
  875. }
  876. static const struct iio_buffer_setup_ops bmg160_buffer_setup_ops = {
  877. .preenable = bmg160_buffer_preenable,
  878. .postenable = iio_triggered_buffer_postenable,
  879. .predisable = iio_triggered_buffer_predisable,
  880. .postdisable = bmg160_buffer_postdisable,
  881. };
  882. static const char *bmg160_match_acpi_device(struct device *dev)
  883. {
  884. const struct acpi_device_id *id;
  885. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  886. if (!id)
  887. return NULL;
  888. return dev_name(dev);
  889. }
  890. int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
  891. const char *name)
  892. {
  893. struct bmg160_data *data;
  894. struct iio_dev *indio_dev;
  895. int ret;
  896. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  897. if (!indio_dev)
  898. return -ENOMEM;
  899. data = iio_priv(indio_dev);
  900. dev_set_drvdata(dev, indio_dev);
  901. data->irq = irq;
  902. data->regmap = regmap;
  903. ret = bmg160_chip_init(data);
  904. if (ret < 0)
  905. return ret;
  906. mutex_init(&data->mutex);
  907. if (ACPI_HANDLE(dev))
  908. name = bmg160_match_acpi_device(dev);
  909. indio_dev->dev.parent = dev;
  910. indio_dev->channels = bmg160_channels;
  911. indio_dev->num_channels = ARRAY_SIZE(bmg160_channels);
  912. indio_dev->name = name;
  913. indio_dev->available_scan_masks = bmg160_accel_scan_masks;
  914. indio_dev->modes = INDIO_DIRECT_MODE;
  915. indio_dev->info = &bmg160_info;
  916. if (data->irq > 0) {
  917. ret = devm_request_threaded_irq(dev,
  918. data->irq,
  919. bmg160_data_rdy_trig_poll,
  920. bmg160_event_handler,
  921. IRQF_TRIGGER_RISING,
  922. BMG160_IRQ_NAME,
  923. indio_dev);
  924. if (ret)
  925. return ret;
  926. data->dready_trig = devm_iio_trigger_alloc(dev,
  927. "%s-dev%d",
  928. indio_dev->name,
  929. indio_dev->id);
  930. if (!data->dready_trig)
  931. return -ENOMEM;
  932. data->motion_trig = devm_iio_trigger_alloc(dev,
  933. "%s-any-motion-dev%d",
  934. indio_dev->name,
  935. indio_dev->id);
  936. if (!data->motion_trig)
  937. return -ENOMEM;
  938. data->dready_trig->dev.parent = dev;
  939. data->dready_trig->ops = &bmg160_trigger_ops;
  940. iio_trigger_set_drvdata(data->dready_trig, indio_dev);
  941. ret = iio_trigger_register(data->dready_trig);
  942. if (ret)
  943. return ret;
  944. data->motion_trig->dev.parent = dev;
  945. data->motion_trig->ops = &bmg160_trigger_ops;
  946. iio_trigger_set_drvdata(data->motion_trig, indio_dev);
  947. ret = iio_trigger_register(data->motion_trig);
  948. if (ret) {
  949. data->motion_trig = NULL;
  950. goto err_trigger_unregister;
  951. }
  952. }
  953. ret = iio_triggered_buffer_setup(indio_dev,
  954. iio_pollfunc_store_time,
  955. bmg160_trigger_handler,
  956. &bmg160_buffer_setup_ops);
  957. if (ret < 0) {
  958. dev_err(dev,
  959. "iio triggered buffer setup failed\n");
  960. goto err_trigger_unregister;
  961. }
  962. ret = pm_runtime_set_active(dev);
  963. if (ret)
  964. goto err_buffer_cleanup;
  965. pm_runtime_enable(dev);
  966. pm_runtime_set_autosuspend_delay(dev,
  967. BMG160_AUTO_SUSPEND_DELAY_MS);
  968. pm_runtime_use_autosuspend(dev);
  969. ret = iio_device_register(indio_dev);
  970. if (ret < 0) {
  971. dev_err(dev, "unable to register iio device\n");
  972. goto err_buffer_cleanup;
  973. }
  974. return 0;
  975. err_buffer_cleanup:
  976. iio_triggered_buffer_cleanup(indio_dev);
  977. err_trigger_unregister:
  978. if (data->dready_trig)
  979. iio_trigger_unregister(data->dready_trig);
  980. if (data->motion_trig)
  981. iio_trigger_unregister(data->motion_trig);
  982. return ret;
  983. }
  984. EXPORT_SYMBOL_GPL(bmg160_core_probe);
  985. void bmg160_core_remove(struct device *dev)
  986. {
  987. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  988. struct bmg160_data *data = iio_priv(indio_dev);
  989. iio_device_unregister(indio_dev);
  990. pm_runtime_disable(dev);
  991. pm_runtime_set_suspended(dev);
  992. pm_runtime_put_noidle(dev);
  993. iio_triggered_buffer_cleanup(indio_dev);
  994. if (data->dready_trig) {
  995. iio_trigger_unregister(data->dready_trig);
  996. iio_trigger_unregister(data->motion_trig);
  997. }
  998. mutex_lock(&data->mutex);
  999. bmg160_set_mode(data, BMG160_MODE_DEEP_SUSPEND);
  1000. mutex_unlock(&data->mutex);
  1001. }
  1002. EXPORT_SYMBOL_GPL(bmg160_core_remove);
  1003. #ifdef CONFIG_PM_SLEEP
  1004. static int bmg160_suspend(struct device *dev)
  1005. {
  1006. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1007. struct bmg160_data *data = iio_priv(indio_dev);
  1008. mutex_lock(&data->mutex);
  1009. bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  1010. mutex_unlock(&data->mutex);
  1011. return 0;
  1012. }
  1013. static int bmg160_resume(struct device *dev)
  1014. {
  1015. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1016. struct bmg160_data *data = iio_priv(indio_dev);
  1017. mutex_lock(&data->mutex);
  1018. if (data->dready_trigger_on || data->motion_trigger_on ||
  1019. data->ev_enable_state)
  1020. bmg160_set_mode(data, BMG160_MODE_NORMAL);
  1021. mutex_unlock(&data->mutex);
  1022. return 0;
  1023. }
  1024. #endif
  1025. #ifdef CONFIG_PM
  1026. static int bmg160_runtime_suspend(struct device *dev)
  1027. {
  1028. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1029. struct bmg160_data *data = iio_priv(indio_dev);
  1030. int ret;
  1031. ret = bmg160_set_mode(data, BMG160_MODE_SUSPEND);
  1032. if (ret < 0) {
  1033. dev_err(dev, "set mode failed\n");
  1034. return -EAGAIN;
  1035. }
  1036. return 0;
  1037. }
  1038. static int bmg160_runtime_resume(struct device *dev)
  1039. {
  1040. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1041. struct bmg160_data *data = iio_priv(indio_dev);
  1042. int ret;
  1043. ret = bmg160_set_mode(data, BMG160_MODE_NORMAL);
  1044. if (ret < 0)
  1045. return ret;
  1046. msleep_interruptible(BMG160_MAX_STARTUP_TIME_MS);
  1047. return 0;
  1048. }
  1049. #endif
  1050. const struct dev_pm_ops bmg160_pm_ops = {
  1051. SET_SYSTEM_SLEEP_PM_OPS(bmg160_suspend, bmg160_resume)
  1052. SET_RUNTIME_PM_OPS(bmg160_runtime_suspend,
  1053. bmg160_runtime_resume, NULL)
  1054. };
  1055. EXPORT_SYMBOL_GPL(bmg160_pm_ops);
  1056. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  1057. MODULE_LICENSE("GPL v2");
  1058. MODULE_DESCRIPTION("BMG160 Gyro driver");