mpu3050-core.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. /*
  2. * MPU3050 gyroscope driver
  3. *
  4. * Copyright (C) 2016 Linaro Ltd.
  5. * Author: Linus Walleij <linus.walleij@linaro.org>
  6. *
  7. * Based on the input subsystem driver, Copyright (C) 2011 Wistron Co.Ltd
  8. * Joseph Lai <joseph_lai@wistron.com> and trimmed down by
  9. * Alan Cox <alan@linux.intel.com> in turn based on bma023.c.
  10. * Device behaviour based on a misc driver posted by Nathan Royer in 2011.
  11. *
  12. * TODO: add support for setting up the low pass 3dB frequency.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/trigger.h>
  21. #include <linux/iio/trigger_consumer.h>
  22. #include <linux/iio/triggered_buffer.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/random.h>
  27. #include <linux/slab.h>
  28. #include "mpu3050.h"
  29. #define MPU3050_CHIP_ID 0x69
  30. /*
  31. * Register map: anything suffixed *_H is a big-endian high byte and always
  32. * followed by the corresponding low byte (*_L) even though these are not
  33. * explicitly included in the register definitions.
  34. */
  35. #define MPU3050_CHIP_ID_REG 0x00
  36. #define MPU3050_PRODUCT_ID_REG 0x01
  37. #define MPU3050_XG_OFFS_TC 0x05
  38. #define MPU3050_YG_OFFS_TC 0x08
  39. #define MPU3050_ZG_OFFS_TC 0x0B
  40. #define MPU3050_X_OFFS_USR_H 0x0C
  41. #define MPU3050_Y_OFFS_USR_H 0x0E
  42. #define MPU3050_Z_OFFS_USR_H 0x10
  43. #define MPU3050_FIFO_EN 0x12
  44. #define MPU3050_AUX_VDDIO 0x13
  45. #define MPU3050_SLV_ADDR 0x14
  46. #define MPU3050_SMPLRT_DIV 0x15
  47. #define MPU3050_DLPF_FS_SYNC 0x16
  48. #define MPU3050_INT_CFG 0x17
  49. #define MPU3050_AUX_ADDR 0x18
  50. #define MPU3050_INT_STATUS 0x1A
  51. #define MPU3050_TEMP_H 0x1B
  52. #define MPU3050_XOUT_H 0x1D
  53. #define MPU3050_YOUT_H 0x1F
  54. #define MPU3050_ZOUT_H 0x21
  55. #define MPU3050_DMP_CFG1 0x35
  56. #define MPU3050_DMP_CFG2 0x36
  57. #define MPU3050_BANK_SEL 0x37
  58. #define MPU3050_MEM_START_ADDR 0x38
  59. #define MPU3050_MEM_R_W 0x39
  60. #define MPU3050_FIFO_COUNT_H 0x3A
  61. #define MPU3050_FIFO_R 0x3C
  62. #define MPU3050_USR_CTRL 0x3D
  63. #define MPU3050_PWR_MGM 0x3E
  64. /* MPU memory bank read options */
  65. #define MPU3050_MEM_PRFTCH BIT(5)
  66. #define MPU3050_MEM_USER_BANK BIT(4)
  67. /* Bits 8-11 select memory bank */
  68. #define MPU3050_MEM_RAM_BANK_0 0
  69. #define MPU3050_MEM_RAM_BANK_1 1
  70. #define MPU3050_MEM_RAM_BANK_2 2
  71. #define MPU3050_MEM_RAM_BANK_3 3
  72. #define MPU3050_MEM_OTP_BANK_0 4
  73. #define MPU3050_AXIS_REGS(axis) (MPU3050_XOUT_H + (axis * 2))
  74. /* Register bits */
  75. /* FIFO Enable */
  76. #define MPU3050_FIFO_EN_FOOTER BIT(0)
  77. #define MPU3050_FIFO_EN_AUX_ZOUT BIT(1)
  78. #define MPU3050_FIFO_EN_AUX_YOUT BIT(2)
  79. #define MPU3050_FIFO_EN_AUX_XOUT BIT(3)
  80. #define MPU3050_FIFO_EN_GYRO_ZOUT BIT(4)
  81. #define MPU3050_FIFO_EN_GYRO_YOUT BIT(5)
  82. #define MPU3050_FIFO_EN_GYRO_XOUT BIT(6)
  83. #define MPU3050_FIFO_EN_TEMP_OUT BIT(7)
  84. /*
  85. * Digital Low Pass filter (DLPF)
  86. * Full Scale (FS)
  87. * and Synchronization
  88. */
  89. #define MPU3050_EXT_SYNC_NONE 0x00
  90. #define MPU3050_EXT_SYNC_TEMP 0x20
  91. #define MPU3050_EXT_SYNC_GYROX 0x40
  92. #define MPU3050_EXT_SYNC_GYROY 0x60
  93. #define MPU3050_EXT_SYNC_GYROZ 0x80
  94. #define MPU3050_EXT_SYNC_ACCELX 0xA0
  95. #define MPU3050_EXT_SYNC_ACCELY 0xC0
  96. #define MPU3050_EXT_SYNC_ACCELZ 0xE0
  97. #define MPU3050_EXT_SYNC_MASK 0xE0
  98. #define MPU3050_EXT_SYNC_SHIFT 5
  99. #define MPU3050_FS_250DPS 0x00
  100. #define MPU3050_FS_500DPS 0x08
  101. #define MPU3050_FS_1000DPS 0x10
  102. #define MPU3050_FS_2000DPS 0x18
  103. #define MPU3050_FS_MASK 0x18
  104. #define MPU3050_FS_SHIFT 3
  105. #define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
  106. #define MPU3050_DLPF_CFG_188HZ 0x01
  107. #define MPU3050_DLPF_CFG_98HZ 0x02
  108. #define MPU3050_DLPF_CFG_42HZ 0x03
  109. #define MPU3050_DLPF_CFG_20HZ 0x04
  110. #define MPU3050_DLPF_CFG_10HZ 0x05
  111. #define MPU3050_DLPF_CFG_5HZ 0x06
  112. #define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
  113. #define MPU3050_DLPF_CFG_MASK 0x07
  114. #define MPU3050_DLPF_CFG_SHIFT 0
  115. /* Interrupt config */
  116. #define MPU3050_INT_RAW_RDY_EN BIT(0)
  117. #define MPU3050_INT_DMP_DONE_EN BIT(1)
  118. #define MPU3050_INT_MPU_RDY_EN BIT(2)
  119. #define MPU3050_INT_ANYRD_2CLEAR BIT(4)
  120. #define MPU3050_INT_LATCH_EN BIT(5)
  121. #define MPU3050_INT_OPEN BIT(6)
  122. #define MPU3050_INT_ACTL BIT(7)
  123. /* Interrupt status */
  124. #define MPU3050_INT_STATUS_RAW_RDY BIT(0)
  125. #define MPU3050_INT_STATUS_DMP_DONE BIT(1)
  126. #define MPU3050_INT_STATUS_MPU_RDY BIT(2)
  127. #define MPU3050_INT_STATUS_FIFO_OVFLW BIT(7)
  128. /* USR_CTRL */
  129. #define MPU3050_USR_CTRL_FIFO_EN BIT(6)
  130. #define MPU3050_USR_CTRL_AUX_IF_EN BIT(5)
  131. #define MPU3050_USR_CTRL_AUX_IF_RST BIT(3)
  132. #define MPU3050_USR_CTRL_FIFO_RST BIT(1)
  133. #define MPU3050_USR_CTRL_GYRO_RST BIT(0)
  134. /* PWR_MGM */
  135. #define MPU3050_PWR_MGM_PLL_X 0x01
  136. #define MPU3050_PWR_MGM_PLL_Y 0x02
  137. #define MPU3050_PWR_MGM_PLL_Z 0x03
  138. #define MPU3050_PWR_MGM_CLKSEL_MASK 0x07
  139. #define MPU3050_PWR_MGM_STBY_ZG BIT(3)
  140. #define MPU3050_PWR_MGM_STBY_YG BIT(4)
  141. #define MPU3050_PWR_MGM_STBY_XG BIT(5)
  142. #define MPU3050_PWR_MGM_SLEEP BIT(6)
  143. #define MPU3050_PWR_MGM_RESET BIT(7)
  144. #define MPU3050_PWR_MGM_MASK 0xff
  145. /*
  146. * Fullscale precision is (for finest precision) +/- 250 deg/s, so the full
  147. * scale is actually 500 deg/s. All 16 bits are then used to cover this scale,
  148. * in two's complement.
  149. */
  150. static unsigned int mpu3050_fs_precision[] = {
  151. IIO_DEGREE_TO_RAD(250),
  152. IIO_DEGREE_TO_RAD(500),
  153. IIO_DEGREE_TO_RAD(1000),
  154. IIO_DEGREE_TO_RAD(2000)
  155. };
  156. /*
  157. * Regulator names
  158. */
  159. static const char mpu3050_reg_vdd[] = "vdd";
  160. static const char mpu3050_reg_vlogic[] = "vlogic";
  161. static unsigned int mpu3050_get_freq(struct mpu3050 *mpu3050)
  162. {
  163. unsigned int freq;
  164. if (mpu3050->lpf == MPU3050_DLPF_CFG_256HZ_NOLPF2)
  165. freq = 8000;
  166. else
  167. freq = 1000;
  168. freq /= (mpu3050->divisor + 1);
  169. return freq;
  170. }
  171. static int mpu3050_start_sampling(struct mpu3050 *mpu3050)
  172. {
  173. __be16 raw_val[3];
  174. int ret;
  175. int i;
  176. /* Reset */
  177. ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
  178. MPU3050_PWR_MGM_RESET, MPU3050_PWR_MGM_RESET);
  179. if (ret)
  180. return ret;
  181. /* Turn on the Z-axis PLL */
  182. ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
  183. MPU3050_PWR_MGM_CLKSEL_MASK,
  184. MPU3050_PWR_MGM_PLL_Z);
  185. if (ret)
  186. return ret;
  187. /* Write calibration offset registers */
  188. for (i = 0; i < 3; i++)
  189. raw_val[i] = cpu_to_be16(mpu3050->calibration[i]);
  190. ret = regmap_bulk_write(mpu3050->map, MPU3050_X_OFFS_USR_H, raw_val,
  191. sizeof(raw_val));
  192. if (ret)
  193. return ret;
  194. /* Set low pass filter (sample rate), sync and full scale */
  195. ret = regmap_write(mpu3050->map, MPU3050_DLPF_FS_SYNC,
  196. MPU3050_EXT_SYNC_NONE << MPU3050_EXT_SYNC_SHIFT |
  197. mpu3050->fullscale << MPU3050_FS_SHIFT |
  198. mpu3050->lpf << MPU3050_DLPF_CFG_SHIFT);
  199. if (ret)
  200. return ret;
  201. /* Set up sampling frequency */
  202. ret = regmap_write(mpu3050->map, MPU3050_SMPLRT_DIV, mpu3050->divisor);
  203. if (ret)
  204. return ret;
  205. /*
  206. * Max 50 ms start-up time after setting DLPF_FS_SYNC
  207. * according to the data sheet, then wait for the next sample
  208. * at this frequency T = 1000/f ms.
  209. */
  210. msleep(50 + 1000 / mpu3050_get_freq(mpu3050));
  211. return 0;
  212. }
  213. static int mpu3050_set_8khz_samplerate(struct mpu3050 *mpu3050)
  214. {
  215. int ret;
  216. u8 divisor;
  217. enum mpu3050_lpf lpf;
  218. lpf = mpu3050->lpf;
  219. divisor = mpu3050->divisor;
  220. mpu3050->lpf = LPF_256_HZ_NOLPF; /* 8 kHz base frequency */
  221. mpu3050->divisor = 0; /* Divide by 1 */
  222. ret = mpu3050_start_sampling(mpu3050);
  223. mpu3050->lpf = lpf;
  224. mpu3050->divisor = divisor;
  225. return ret;
  226. }
  227. static int mpu3050_read_raw(struct iio_dev *indio_dev,
  228. struct iio_chan_spec const *chan,
  229. int *val, int *val2,
  230. long mask)
  231. {
  232. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  233. int ret;
  234. __be16 raw_val;
  235. switch (mask) {
  236. case IIO_CHAN_INFO_OFFSET:
  237. switch (chan->type) {
  238. case IIO_TEMP:
  239. /* The temperature scaling is (x+23000)/280 Celsius */
  240. *val = 23000;
  241. return IIO_VAL_INT;
  242. default:
  243. return -EINVAL;
  244. }
  245. case IIO_CHAN_INFO_CALIBBIAS:
  246. switch (chan->type) {
  247. case IIO_ANGL_VEL:
  248. *val = mpu3050->calibration[chan->scan_index-1];
  249. return IIO_VAL_INT;
  250. default:
  251. return -EINVAL;
  252. }
  253. case IIO_CHAN_INFO_SAMP_FREQ:
  254. *val = mpu3050_get_freq(mpu3050);
  255. return IIO_VAL_INT;
  256. case IIO_CHAN_INFO_SCALE:
  257. switch (chan->type) {
  258. case IIO_TEMP:
  259. /* Millidegrees, see about temperature scaling above */
  260. *val = 1000;
  261. *val2 = 280;
  262. return IIO_VAL_FRACTIONAL;
  263. case IIO_ANGL_VEL:
  264. /*
  265. * Convert to the corresponding full scale in
  266. * radians. All 16 bits are used with sign to
  267. * span the available scale: to account for the one
  268. * missing value if we multiply by 1/S16_MAX, instead
  269. * multiply with 2/U16_MAX.
  270. */
  271. *val = mpu3050_fs_precision[mpu3050->fullscale] * 2;
  272. *val2 = U16_MAX;
  273. return IIO_VAL_FRACTIONAL;
  274. default:
  275. return -EINVAL;
  276. }
  277. case IIO_CHAN_INFO_RAW:
  278. /* Resume device */
  279. pm_runtime_get_sync(mpu3050->dev);
  280. mutex_lock(&mpu3050->lock);
  281. ret = mpu3050_set_8khz_samplerate(mpu3050);
  282. if (ret)
  283. goto out_read_raw_unlock;
  284. switch (chan->type) {
  285. case IIO_TEMP:
  286. ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H,
  287. &raw_val, sizeof(raw_val));
  288. if (ret) {
  289. dev_err(mpu3050->dev,
  290. "error reading temperature\n");
  291. goto out_read_raw_unlock;
  292. }
  293. *val = be16_to_cpu(raw_val);
  294. ret = IIO_VAL_INT;
  295. goto out_read_raw_unlock;
  296. case IIO_ANGL_VEL:
  297. ret = regmap_bulk_read(mpu3050->map,
  298. MPU3050_AXIS_REGS(chan->scan_index-1),
  299. &raw_val,
  300. sizeof(raw_val));
  301. if (ret) {
  302. dev_err(mpu3050->dev,
  303. "error reading axis data\n");
  304. goto out_read_raw_unlock;
  305. }
  306. *val = be16_to_cpu(raw_val);
  307. ret = IIO_VAL_INT;
  308. goto out_read_raw_unlock;
  309. default:
  310. ret = -EINVAL;
  311. goto out_read_raw_unlock;
  312. }
  313. default:
  314. break;
  315. }
  316. return -EINVAL;
  317. out_read_raw_unlock:
  318. mutex_unlock(&mpu3050->lock);
  319. pm_runtime_mark_last_busy(mpu3050->dev);
  320. pm_runtime_put_autosuspend(mpu3050->dev);
  321. return ret;
  322. }
  323. static int mpu3050_write_raw(struct iio_dev *indio_dev,
  324. const struct iio_chan_spec *chan,
  325. int val, int val2, long mask)
  326. {
  327. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  328. /*
  329. * Couldn't figure out a way to precalculate these at compile time.
  330. */
  331. unsigned int fs250 =
  332. DIV_ROUND_CLOSEST(mpu3050_fs_precision[0] * 1000000 * 2,
  333. U16_MAX);
  334. unsigned int fs500 =
  335. DIV_ROUND_CLOSEST(mpu3050_fs_precision[1] * 1000000 * 2,
  336. U16_MAX);
  337. unsigned int fs1000 =
  338. DIV_ROUND_CLOSEST(mpu3050_fs_precision[2] * 1000000 * 2,
  339. U16_MAX);
  340. unsigned int fs2000 =
  341. DIV_ROUND_CLOSEST(mpu3050_fs_precision[3] * 1000000 * 2,
  342. U16_MAX);
  343. switch (mask) {
  344. case IIO_CHAN_INFO_CALIBBIAS:
  345. if (chan->type != IIO_ANGL_VEL)
  346. return -EINVAL;
  347. mpu3050->calibration[chan->scan_index-1] = val;
  348. return 0;
  349. case IIO_CHAN_INFO_SAMP_FREQ:
  350. /*
  351. * The max samplerate is 8000 Hz, the minimum
  352. * 1000 / 256 ~= 4 Hz
  353. */
  354. if (val < 4 || val > 8000)
  355. return -EINVAL;
  356. /*
  357. * Above 1000 Hz we must turn off the digital low pass filter
  358. * so we get a base frequency of 8kHz to the divider
  359. */
  360. if (val > 1000) {
  361. mpu3050->lpf = LPF_256_HZ_NOLPF;
  362. mpu3050->divisor = DIV_ROUND_CLOSEST(8000, val) - 1;
  363. return 0;
  364. }
  365. mpu3050->lpf = LPF_188_HZ;
  366. mpu3050->divisor = DIV_ROUND_CLOSEST(1000, val) - 1;
  367. return 0;
  368. case IIO_CHAN_INFO_SCALE:
  369. if (chan->type != IIO_ANGL_VEL)
  370. return -EINVAL;
  371. /*
  372. * We support +/-250, +/-500, +/-1000 and +/2000 deg/s
  373. * which means we need to round to the closest radians
  374. * which will be roughly +/-4.3, +/-8.7, +/-17.5, +/-35
  375. * rad/s. The scale is then for the 16 bits used to cover
  376. * it 2/(2^16) of that.
  377. */
  378. /* Just too large, set the max range */
  379. if (val != 0) {
  380. mpu3050->fullscale = FS_2000_DPS;
  381. return 0;
  382. }
  383. /*
  384. * Now we're dealing with fractions below zero in millirad/s
  385. * do some integer interpolation and match with the closest
  386. * fullscale in the table.
  387. */
  388. if (val2 <= fs250 ||
  389. val2 < ((fs500 + fs250) / 2))
  390. mpu3050->fullscale = FS_250_DPS;
  391. else if (val2 <= fs500 ||
  392. val2 < ((fs1000 + fs500) / 2))
  393. mpu3050->fullscale = FS_500_DPS;
  394. else if (val2 <= fs1000 ||
  395. val2 < ((fs2000 + fs1000) / 2))
  396. mpu3050->fullscale = FS_1000_DPS;
  397. else
  398. /* Catch-all */
  399. mpu3050->fullscale = FS_2000_DPS;
  400. return 0;
  401. default:
  402. break;
  403. }
  404. return -EINVAL;
  405. }
  406. static irqreturn_t mpu3050_trigger_handler(int irq, void *p)
  407. {
  408. const struct iio_poll_func *pf = p;
  409. struct iio_dev *indio_dev = pf->indio_dev;
  410. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  411. int ret;
  412. /*
  413. * Temperature 1*16 bits
  414. * Three axes 3*16 bits
  415. * Timestamp 64 bits (4*16 bits)
  416. * Sum total 8*16 bits
  417. */
  418. __be16 hw_values[8];
  419. s64 timestamp;
  420. unsigned int datums_from_fifo = 0;
  421. /*
  422. * If we're using the hardware trigger, get the precise timestamp from
  423. * the top half of the threaded IRQ handler. Otherwise get the
  424. * timestamp here so it will be close in time to the actual values
  425. * read from the registers.
  426. */
  427. if (iio_trigger_using_own(indio_dev))
  428. timestamp = mpu3050->hw_timestamp;
  429. else
  430. timestamp = iio_get_time_ns(indio_dev);
  431. mutex_lock(&mpu3050->lock);
  432. /* Using the hardware IRQ trigger? Check the buffer then. */
  433. if (mpu3050->hw_irq_trigger) {
  434. __be16 raw_fifocnt;
  435. u16 fifocnt;
  436. /* X, Y, Z + temperature */
  437. unsigned int bytes_per_datum = 8;
  438. bool fifo_overflow = false;
  439. ret = regmap_bulk_read(mpu3050->map,
  440. MPU3050_FIFO_COUNT_H,
  441. &raw_fifocnt,
  442. sizeof(raw_fifocnt));
  443. if (ret)
  444. goto out_trigger_unlock;
  445. fifocnt = be16_to_cpu(raw_fifocnt);
  446. if (fifocnt == 512) {
  447. dev_info(mpu3050->dev,
  448. "FIFO overflow! Emptying and resetting FIFO\n");
  449. fifo_overflow = true;
  450. /* Reset and enable the FIFO */
  451. ret = regmap_update_bits(mpu3050->map,
  452. MPU3050_USR_CTRL,
  453. MPU3050_USR_CTRL_FIFO_EN |
  454. MPU3050_USR_CTRL_FIFO_RST,
  455. MPU3050_USR_CTRL_FIFO_EN |
  456. MPU3050_USR_CTRL_FIFO_RST);
  457. if (ret) {
  458. dev_info(mpu3050->dev, "error resetting FIFO\n");
  459. goto out_trigger_unlock;
  460. }
  461. mpu3050->pending_fifo_footer = false;
  462. }
  463. if (fifocnt)
  464. dev_dbg(mpu3050->dev,
  465. "%d bytes in the FIFO\n",
  466. fifocnt);
  467. while (!fifo_overflow && fifocnt > bytes_per_datum) {
  468. unsigned int toread;
  469. unsigned int offset;
  470. __be16 fifo_values[5];
  471. /*
  472. * If there is a FIFO footer in the pipe, first clear
  473. * that out. This follows the complex algorithm in the
  474. * datasheet that states that you may never leave the
  475. * FIFO empty after the first reading: you have to
  476. * always leave two footer bytes in it. The footer is
  477. * in practice just two zero bytes.
  478. */
  479. if (mpu3050->pending_fifo_footer) {
  480. toread = bytes_per_datum + 2;
  481. offset = 0;
  482. } else {
  483. toread = bytes_per_datum;
  484. offset = 1;
  485. /* Put in some dummy value */
  486. fifo_values[0] = 0xAAAA;
  487. }
  488. ret = regmap_bulk_read(mpu3050->map,
  489. MPU3050_FIFO_R,
  490. &fifo_values[offset],
  491. toread);
  492. dev_dbg(mpu3050->dev,
  493. "%04x %04x %04x %04x %04x\n",
  494. fifo_values[0],
  495. fifo_values[1],
  496. fifo_values[2],
  497. fifo_values[3],
  498. fifo_values[4]);
  499. /* Index past the footer (fifo_values[0]) and push */
  500. iio_push_to_buffers_with_timestamp(indio_dev,
  501. &fifo_values[1],
  502. timestamp);
  503. fifocnt -= toread;
  504. datums_from_fifo++;
  505. mpu3050->pending_fifo_footer = true;
  506. /*
  507. * If we're emptying the FIFO, just make sure to
  508. * check if something new appeared.
  509. */
  510. if (fifocnt < bytes_per_datum) {
  511. ret = regmap_bulk_read(mpu3050->map,
  512. MPU3050_FIFO_COUNT_H,
  513. &raw_fifocnt,
  514. sizeof(raw_fifocnt));
  515. if (ret)
  516. goto out_trigger_unlock;
  517. fifocnt = be16_to_cpu(raw_fifocnt);
  518. }
  519. if (fifocnt < bytes_per_datum)
  520. dev_dbg(mpu3050->dev,
  521. "%d bytes left in the FIFO\n",
  522. fifocnt);
  523. /*
  524. * At this point, the timestamp that triggered the
  525. * hardware interrupt is no longer valid for what
  526. * we are reading (the interrupt likely fired for
  527. * the value on the top of the FIFO), so set the
  528. * timestamp to zero and let userspace deal with it.
  529. */
  530. timestamp = 0;
  531. }
  532. }
  533. /*
  534. * If we picked some datums from the FIFO that's enough, else
  535. * fall through and just read from the current value registers.
  536. * This happens in two cases:
  537. *
  538. * - We are using some other trigger (external, like an HRTimer)
  539. * than the sensor's own sample generator. In this case the
  540. * sensor is just set to the max sampling frequency and we give
  541. * the trigger a copy of the latest value every time we get here.
  542. *
  543. * - The hardware trigger is active but unused and we actually use
  544. * another trigger which calls here with a frequency higher
  545. * than what the device provides data. We will then just read
  546. * duplicate values directly from the hardware registers.
  547. */
  548. if (datums_from_fifo) {
  549. dev_dbg(mpu3050->dev,
  550. "read %d datums from the FIFO\n",
  551. datums_from_fifo);
  552. goto out_trigger_unlock;
  553. }
  554. ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, &hw_values,
  555. sizeof(hw_values));
  556. if (ret) {
  557. dev_err(mpu3050->dev,
  558. "error reading axis data\n");
  559. goto out_trigger_unlock;
  560. }
  561. iio_push_to_buffers_with_timestamp(indio_dev, hw_values, timestamp);
  562. out_trigger_unlock:
  563. mutex_unlock(&mpu3050->lock);
  564. iio_trigger_notify_done(indio_dev->trig);
  565. return IRQ_HANDLED;
  566. }
  567. static int mpu3050_buffer_preenable(struct iio_dev *indio_dev)
  568. {
  569. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  570. pm_runtime_get_sync(mpu3050->dev);
  571. /* Unless we have OUR trigger active, run at full speed */
  572. if (!mpu3050->hw_irq_trigger)
  573. return mpu3050_set_8khz_samplerate(mpu3050);
  574. return 0;
  575. }
  576. static int mpu3050_buffer_postdisable(struct iio_dev *indio_dev)
  577. {
  578. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  579. pm_runtime_mark_last_busy(mpu3050->dev);
  580. pm_runtime_put_autosuspend(mpu3050->dev);
  581. return 0;
  582. }
  583. static const struct iio_buffer_setup_ops mpu3050_buffer_setup_ops = {
  584. .preenable = mpu3050_buffer_preenable,
  585. .postenable = iio_triggered_buffer_postenable,
  586. .predisable = iio_triggered_buffer_predisable,
  587. .postdisable = mpu3050_buffer_postdisable,
  588. };
  589. static const struct iio_mount_matrix *
  590. mpu3050_get_mount_matrix(const struct iio_dev *indio_dev,
  591. const struct iio_chan_spec *chan)
  592. {
  593. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  594. return &mpu3050->orientation;
  595. }
  596. static const struct iio_chan_spec_ext_info mpu3050_ext_info[] = {
  597. IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, mpu3050_get_mount_matrix),
  598. { },
  599. };
  600. #define MPU3050_AXIS_CHANNEL(axis, index) \
  601. { \
  602. .type = IIO_ANGL_VEL, \
  603. .modified = 1, \
  604. .channel2 = IIO_MOD_##axis, \
  605. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  606. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  607. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  608. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  609. .ext_info = mpu3050_ext_info, \
  610. .scan_index = index, \
  611. .scan_type = { \
  612. .sign = 's', \
  613. .realbits = 16, \
  614. .storagebits = 16, \
  615. .endianness = IIO_BE, \
  616. }, \
  617. }
  618. static const struct iio_chan_spec mpu3050_channels[] = {
  619. {
  620. .type = IIO_TEMP,
  621. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  622. BIT(IIO_CHAN_INFO_SCALE) |
  623. BIT(IIO_CHAN_INFO_OFFSET),
  624. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  625. .scan_index = 0,
  626. .scan_type = {
  627. .sign = 's',
  628. .realbits = 16,
  629. .storagebits = 16,
  630. .endianness = IIO_BE,
  631. },
  632. },
  633. MPU3050_AXIS_CHANNEL(X, 1),
  634. MPU3050_AXIS_CHANNEL(Y, 2),
  635. MPU3050_AXIS_CHANNEL(Z, 3),
  636. IIO_CHAN_SOFT_TIMESTAMP(4),
  637. };
  638. /* Four channels apart from timestamp, scan mask = 0x0f */
  639. static const unsigned long mpu3050_scan_masks[] = { 0xf, 0 };
  640. /*
  641. * These are just the hardcoded factors resulting from the more elaborate
  642. * calculations done with fractions in the scale raw get/set functions.
  643. */
  644. static IIO_CONST_ATTR(anglevel_scale_available,
  645. "0.000122070 "
  646. "0.000274658 "
  647. "0.000518798 "
  648. "0.001068115");
  649. static struct attribute *mpu3050_attributes[] = {
  650. &iio_const_attr_anglevel_scale_available.dev_attr.attr,
  651. NULL,
  652. };
  653. static const struct attribute_group mpu3050_attribute_group = {
  654. .attrs = mpu3050_attributes,
  655. };
  656. static const struct iio_info mpu3050_info = {
  657. .driver_module = THIS_MODULE,
  658. .read_raw = mpu3050_read_raw,
  659. .write_raw = mpu3050_write_raw,
  660. .attrs = &mpu3050_attribute_group,
  661. };
  662. /**
  663. * mpu3050_read_mem() - read MPU-3050 internal memory
  664. * @mpu3050: device to read from
  665. * @bank: target bank
  666. * @addr: target address
  667. * @len: number of bytes
  668. * @buf: the buffer to store the read bytes in
  669. */
  670. static int mpu3050_read_mem(struct mpu3050 *mpu3050,
  671. u8 bank,
  672. u8 addr,
  673. u8 len,
  674. u8 *buf)
  675. {
  676. int ret;
  677. ret = regmap_write(mpu3050->map,
  678. MPU3050_BANK_SEL,
  679. bank);
  680. if (ret)
  681. return ret;
  682. ret = regmap_write(mpu3050->map,
  683. MPU3050_MEM_START_ADDR,
  684. addr);
  685. if (ret)
  686. return ret;
  687. return regmap_bulk_read(mpu3050->map,
  688. MPU3050_MEM_R_W,
  689. buf,
  690. len);
  691. }
  692. static int mpu3050_hw_init(struct mpu3050 *mpu3050)
  693. {
  694. int ret;
  695. u8 otp[8];
  696. /* Reset */
  697. ret = regmap_update_bits(mpu3050->map,
  698. MPU3050_PWR_MGM,
  699. MPU3050_PWR_MGM_RESET,
  700. MPU3050_PWR_MGM_RESET);
  701. if (ret)
  702. return ret;
  703. /* Turn on the PLL */
  704. ret = regmap_update_bits(mpu3050->map,
  705. MPU3050_PWR_MGM,
  706. MPU3050_PWR_MGM_CLKSEL_MASK,
  707. MPU3050_PWR_MGM_PLL_Z);
  708. if (ret)
  709. return ret;
  710. /* Disable IRQs */
  711. ret = regmap_write(mpu3050->map,
  712. MPU3050_INT_CFG,
  713. 0);
  714. if (ret)
  715. return ret;
  716. /* Read out the 8 bytes of OTP (one-time-programmable) memory */
  717. ret = mpu3050_read_mem(mpu3050,
  718. (MPU3050_MEM_PRFTCH |
  719. MPU3050_MEM_USER_BANK |
  720. MPU3050_MEM_OTP_BANK_0),
  721. 0,
  722. sizeof(otp),
  723. otp);
  724. if (ret)
  725. return ret;
  726. /* This is device-unique data so it goes into the entropy pool */
  727. add_device_randomness(otp, sizeof(otp));
  728. dev_info(mpu3050->dev,
  729. "die ID: %04X, wafer ID: %02X, A lot ID: %04X, "
  730. "W lot ID: %03X, WP ID: %01X, rev ID: %02X\n",
  731. /* Die ID, bits 0-12 */
  732. (otp[1] << 8 | otp[0]) & 0x1fff,
  733. /* Wafer ID, bits 13-17 */
  734. ((otp[2] << 8 | otp[1]) & 0x03e0) >> 5,
  735. /* A lot ID, bits 18-33 */
  736. ((otp[4] << 16 | otp[3] << 8 | otp[2]) & 0x3fffc) >> 2,
  737. /* W lot ID, bits 34-45 */
  738. ((otp[5] << 8 | otp[4]) & 0x3ffc) >> 2,
  739. /* WP ID, bits 47-49 */
  740. ((otp[6] << 8 | otp[5]) & 0x0380) >> 7,
  741. /* rev ID, bits 50-55 */
  742. otp[6] >> 2);
  743. return 0;
  744. }
  745. static int mpu3050_power_up(struct mpu3050 *mpu3050)
  746. {
  747. int ret;
  748. ret = regulator_bulk_enable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs);
  749. if (ret) {
  750. dev_err(mpu3050->dev, "cannot enable regulators\n");
  751. return ret;
  752. }
  753. /*
  754. * 20-100 ms start-up time for register read/write according to
  755. * the datasheet, be on the safe side and wait 200 ms.
  756. */
  757. msleep(200);
  758. /* Take device out of sleep mode */
  759. ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
  760. MPU3050_PWR_MGM_SLEEP, 0);
  761. if (ret) {
  762. dev_err(mpu3050->dev, "error setting power mode\n");
  763. return ret;
  764. }
  765. msleep(10);
  766. return 0;
  767. }
  768. static int mpu3050_power_down(struct mpu3050 *mpu3050)
  769. {
  770. int ret;
  771. /*
  772. * Put MPU-3050 into sleep mode before cutting regulators.
  773. * This is important, because we may not be the sole user
  774. * of the regulator so the power may stay on after this, and
  775. * then we would be wasting power unless we go to sleep mode
  776. * first.
  777. */
  778. ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
  779. MPU3050_PWR_MGM_SLEEP, MPU3050_PWR_MGM_SLEEP);
  780. if (ret)
  781. dev_err(mpu3050->dev, "error putting to sleep\n");
  782. ret = regulator_bulk_disable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs);
  783. if (ret)
  784. dev_err(mpu3050->dev, "error disabling regulators\n");
  785. return 0;
  786. }
  787. static irqreturn_t mpu3050_irq_handler(int irq, void *p)
  788. {
  789. struct iio_trigger *trig = p;
  790. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  791. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  792. if (!mpu3050->hw_irq_trigger)
  793. return IRQ_NONE;
  794. /* Get the time stamp as close in time as possible */
  795. mpu3050->hw_timestamp = iio_get_time_ns(indio_dev);
  796. return IRQ_WAKE_THREAD;
  797. }
  798. static irqreturn_t mpu3050_irq_thread(int irq, void *p)
  799. {
  800. struct iio_trigger *trig = p;
  801. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  802. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  803. unsigned int val;
  804. int ret;
  805. /* ACK IRQ and check if it was from us */
  806. ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
  807. if (ret) {
  808. dev_err(mpu3050->dev, "error reading IRQ status\n");
  809. return IRQ_HANDLED;
  810. }
  811. if (!(val & MPU3050_INT_STATUS_RAW_RDY))
  812. return IRQ_NONE;
  813. iio_trigger_poll_chained(p);
  814. return IRQ_HANDLED;
  815. }
  816. /**
  817. * mpu3050_drdy_trigger_set_state() - set data ready interrupt state
  818. * @trig: trigger instance
  819. * @enable: true if trigger should be enabled, false to disable
  820. */
  821. static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
  822. bool enable)
  823. {
  824. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  825. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  826. unsigned int val;
  827. int ret;
  828. /* Disabling trigger: disable interrupt and return */
  829. if (!enable) {
  830. /* Disable all interrupts */
  831. ret = regmap_write(mpu3050->map,
  832. MPU3050_INT_CFG,
  833. 0);
  834. if (ret)
  835. dev_err(mpu3050->dev, "error disabling IRQ\n");
  836. /* Clear IRQ flag */
  837. ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
  838. if (ret)
  839. dev_err(mpu3050->dev, "error clearing IRQ status\n");
  840. /* Disable all things in the FIFO and reset it */
  841. ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
  842. if (ret)
  843. dev_err(mpu3050->dev, "error disabling FIFO\n");
  844. ret = regmap_write(mpu3050->map, MPU3050_USR_CTRL,
  845. MPU3050_USR_CTRL_FIFO_RST);
  846. if (ret)
  847. dev_err(mpu3050->dev, "error resetting FIFO\n");
  848. pm_runtime_mark_last_busy(mpu3050->dev);
  849. pm_runtime_put_autosuspend(mpu3050->dev);
  850. mpu3050->hw_irq_trigger = false;
  851. return 0;
  852. } else {
  853. /* Else we're enabling the trigger from this point */
  854. pm_runtime_get_sync(mpu3050->dev);
  855. mpu3050->hw_irq_trigger = true;
  856. /* Disable all things in the FIFO */
  857. ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
  858. if (ret)
  859. return ret;
  860. /* Reset and enable the FIFO */
  861. ret = regmap_update_bits(mpu3050->map, MPU3050_USR_CTRL,
  862. MPU3050_USR_CTRL_FIFO_EN |
  863. MPU3050_USR_CTRL_FIFO_RST,
  864. MPU3050_USR_CTRL_FIFO_EN |
  865. MPU3050_USR_CTRL_FIFO_RST);
  866. if (ret)
  867. return ret;
  868. mpu3050->pending_fifo_footer = false;
  869. /* Turn on the FIFO for temp+X+Y+Z */
  870. ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN,
  871. MPU3050_FIFO_EN_TEMP_OUT |
  872. MPU3050_FIFO_EN_GYRO_XOUT |
  873. MPU3050_FIFO_EN_GYRO_YOUT |
  874. MPU3050_FIFO_EN_GYRO_ZOUT |
  875. MPU3050_FIFO_EN_FOOTER);
  876. if (ret)
  877. return ret;
  878. /* Configure the sample engine */
  879. ret = mpu3050_start_sampling(mpu3050);
  880. if (ret)
  881. return ret;
  882. /* Clear IRQ flag */
  883. ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
  884. if (ret)
  885. dev_err(mpu3050->dev, "error clearing IRQ status\n");
  886. /* Give us interrupts whenever there is new data ready */
  887. val = MPU3050_INT_RAW_RDY_EN;
  888. if (mpu3050->irq_actl)
  889. val |= MPU3050_INT_ACTL;
  890. if (mpu3050->irq_latch)
  891. val |= MPU3050_INT_LATCH_EN;
  892. if (mpu3050->irq_opendrain)
  893. val |= MPU3050_INT_OPEN;
  894. ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
  895. if (ret)
  896. return ret;
  897. }
  898. return 0;
  899. }
  900. static const struct iio_trigger_ops mpu3050_trigger_ops = {
  901. .owner = THIS_MODULE,
  902. .set_trigger_state = mpu3050_drdy_trigger_set_state,
  903. };
  904. static int mpu3050_trigger_probe(struct iio_dev *indio_dev, int irq)
  905. {
  906. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  907. unsigned long irq_trig;
  908. int ret;
  909. mpu3050->trig = devm_iio_trigger_alloc(&indio_dev->dev,
  910. "%s-dev%d",
  911. indio_dev->name,
  912. indio_dev->id);
  913. if (!mpu3050->trig)
  914. return -ENOMEM;
  915. /* Check if IRQ is open drain */
  916. if (of_property_read_bool(mpu3050->dev->of_node, "drive-open-drain"))
  917. mpu3050->irq_opendrain = true;
  918. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  919. /*
  920. * Configure the interrupt generator hardware to supply whatever
  921. * the interrupt is configured for, edges low/high level low/high,
  922. * we can provide it all.
  923. */
  924. switch (irq_trig) {
  925. case IRQF_TRIGGER_RISING:
  926. dev_info(&indio_dev->dev,
  927. "pulse interrupts on the rising edge\n");
  928. break;
  929. case IRQF_TRIGGER_FALLING:
  930. mpu3050->irq_actl = true;
  931. dev_info(&indio_dev->dev,
  932. "pulse interrupts on the falling edge\n");
  933. break;
  934. case IRQF_TRIGGER_HIGH:
  935. mpu3050->irq_latch = true;
  936. dev_info(&indio_dev->dev,
  937. "interrupts active high level\n");
  938. /*
  939. * With level IRQs, we mask the IRQ until it is processed,
  940. * but with edge IRQs (pulses) we can queue several interrupts
  941. * in the top half.
  942. */
  943. irq_trig |= IRQF_ONESHOT;
  944. break;
  945. case IRQF_TRIGGER_LOW:
  946. mpu3050->irq_latch = true;
  947. mpu3050->irq_actl = true;
  948. irq_trig |= IRQF_ONESHOT;
  949. dev_info(&indio_dev->dev,
  950. "interrupts active low level\n");
  951. break;
  952. default:
  953. /* This is the most preferred mode, if possible */
  954. dev_err(&indio_dev->dev,
  955. "unsupported IRQ trigger specified (%lx), enforce "
  956. "rising edge\n", irq_trig);
  957. irq_trig = IRQF_TRIGGER_RISING;
  958. break;
  959. }
  960. /* An open drain line can be shared with several devices */
  961. if (mpu3050->irq_opendrain)
  962. irq_trig |= IRQF_SHARED;
  963. ret = request_threaded_irq(irq,
  964. mpu3050_irq_handler,
  965. mpu3050_irq_thread,
  966. irq_trig,
  967. mpu3050->trig->name,
  968. mpu3050->trig);
  969. if (ret) {
  970. dev_err(mpu3050->dev,
  971. "can't get IRQ %d, error %d\n", irq, ret);
  972. return ret;
  973. }
  974. mpu3050->irq = irq;
  975. mpu3050->trig->dev.parent = mpu3050->dev;
  976. mpu3050->trig->ops = &mpu3050_trigger_ops;
  977. iio_trigger_set_drvdata(mpu3050->trig, indio_dev);
  978. ret = iio_trigger_register(mpu3050->trig);
  979. if (ret)
  980. return ret;
  981. indio_dev->trig = iio_trigger_get(mpu3050->trig);
  982. return 0;
  983. }
  984. int mpu3050_common_probe(struct device *dev,
  985. struct regmap *map,
  986. int irq,
  987. const char *name)
  988. {
  989. struct iio_dev *indio_dev;
  990. struct mpu3050 *mpu3050;
  991. unsigned int val;
  992. int ret;
  993. indio_dev = devm_iio_device_alloc(dev, sizeof(*mpu3050));
  994. if (!indio_dev)
  995. return -ENOMEM;
  996. mpu3050 = iio_priv(indio_dev);
  997. mpu3050->dev = dev;
  998. mpu3050->map = map;
  999. mutex_init(&mpu3050->lock);
  1000. /* Default fullscale: 2000 degrees per second */
  1001. mpu3050->fullscale = FS_2000_DPS;
  1002. /* 1 kHz, divide by 100, default frequency = 10 Hz */
  1003. mpu3050->lpf = MPU3050_DLPF_CFG_188HZ;
  1004. mpu3050->divisor = 99;
  1005. /* Read the mounting matrix, if present */
  1006. ret = of_iio_read_mount_matrix(dev, "mount-matrix",
  1007. &mpu3050->orientation);
  1008. if (ret)
  1009. return ret;
  1010. /* Fetch and turn on regulators */
  1011. mpu3050->regs[0].supply = mpu3050_reg_vdd;
  1012. mpu3050->regs[1].supply = mpu3050_reg_vlogic;
  1013. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(mpu3050->regs),
  1014. mpu3050->regs);
  1015. if (ret) {
  1016. dev_err(dev, "Cannot get regulators\n");
  1017. return ret;
  1018. }
  1019. ret = mpu3050_power_up(mpu3050);
  1020. if (ret)
  1021. return ret;
  1022. ret = regmap_read(map, MPU3050_CHIP_ID_REG, &val);
  1023. if (ret) {
  1024. dev_err(dev, "could not read device ID\n");
  1025. ret = -ENODEV;
  1026. goto err_power_down;
  1027. }
  1028. if (val != MPU3050_CHIP_ID) {
  1029. dev_err(dev, "unsupported chip id %02x\n", (u8)val);
  1030. ret = -ENODEV;
  1031. goto err_power_down;
  1032. }
  1033. ret = regmap_read(map, MPU3050_PRODUCT_ID_REG, &val);
  1034. if (ret) {
  1035. dev_err(dev, "could not read device ID\n");
  1036. ret = -ENODEV;
  1037. goto err_power_down;
  1038. }
  1039. dev_info(dev, "found MPU-3050 part no: %d, version: %d\n",
  1040. ((val >> 4) & 0xf), (val & 0xf));
  1041. ret = mpu3050_hw_init(mpu3050);
  1042. if (ret)
  1043. goto err_power_down;
  1044. indio_dev->dev.parent = dev;
  1045. indio_dev->channels = mpu3050_channels;
  1046. indio_dev->num_channels = ARRAY_SIZE(mpu3050_channels);
  1047. indio_dev->info = &mpu3050_info;
  1048. indio_dev->available_scan_masks = mpu3050_scan_masks;
  1049. indio_dev->modes = INDIO_DIRECT_MODE;
  1050. indio_dev->name = name;
  1051. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  1052. mpu3050_trigger_handler,
  1053. &mpu3050_buffer_setup_ops);
  1054. if (ret) {
  1055. dev_err(dev, "triggered buffer setup failed\n");
  1056. goto err_power_down;
  1057. }
  1058. ret = iio_device_register(indio_dev);
  1059. if (ret) {
  1060. dev_err(dev, "device register failed\n");
  1061. goto err_cleanup_buffer;
  1062. }
  1063. dev_set_drvdata(dev, indio_dev);
  1064. /* Check if we have an assigned IRQ to use as trigger */
  1065. if (irq) {
  1066. ret = mpu3050_trigger_probe(indio_dev, irq);
  1067. if (ret)
  1068. dev_err(dev, "failed to register trigger\n");
  1069. }
  1070. /* Enable runtime PM */
  1071. pm_runtime_get_noresume(dev);
  1072. pm_runtime_set_active(dev);
  1073. pm_runtime_enable(dev);
  1074. /*
  1075. * Set autosuspend to two orders of magnitude larger than the
  1076. * start-up time. 100ms start-up time means 10000ms autosuspend,
  1077. * i.e. 10 seconds.
  1078. */
  1079. pm_runtime_set_autosuspend_delay(dev, 10000);
  1080. pm_runtime_use_autosuspend(dev);
  1081. pm_runtime_put(dev);
  1082. return 0;
  1083. err_cleanup_buffer:
  1084. iio_triggered_buffer_cleanup(indio_dev);
  1085. err_power_down:
  1086. mpu3050_power_down(mpu3050);
  1087. return ret;
  1088. }
  1089. EXPORT_SYMBOL(mpu3050_common_probe);
  1090. int mpu3050_common_remove(struct device *dev)
  1091. {
  1092. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1093. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  1094. pm_runtime_get_sync(dev);
  1095. pm_runtime_put_noidle(dev);
  1096. pm_runtime_disable(dev);
  1097. iio_triggered_buffer_cleanup(indio_dev);
  1098. if (mpu3050->irq)
  1099. free_irq(mpu3050->irq, mpu3050);
  1100. iio_device_unregister(indio_dev);
  1101. mpu3050_power_down(mpu3050);
  1102. return 0;
  1103. }
  1104. EXPORT_SYMBOL(mpu3050_common_remove);
  1105. #ifdef CONFIG_PM
  1106. static int mpu3050_runtime_suspend(struct device *dev)
  1107. {
  1108. return mpu3050_power_down(iio_priv(dev_get_drvdata(dev)));
  1109. }
  1110. static int mpu3050_runtime_resume(struct device *dev)
  1111. {
  1112. return mpu3050_power_up(iio_priv(dev_get_drvdata(dev)));
  1113. }
  1114. #endif /* CONFIG_PM */
  1115. const struct dev_pm_ops mpu3050_dev_pm_ops = {
  1116. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1117. pm_runtime_force_resume)
  1118. SET_RUNTIME_PM_OPS(mpu3050_runtime_suspend,
  1119. mpu3050_runtime_resume, NULL)
  1120. };
  1121. EXPORT_SYMBOL(mpu3050_dev_pm_ops);
  1122. MODULE_AUTHOR("Linus Walleij");
  1123. MODULE_DESCRIPTION("MPU3050 gyroscope driver");
  1124. MODULE_LICENSE("GPL");