mpu3050-core.c 33 KB

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