mpu3050-core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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. .read_raw = mpu3050_read_raw,
  658. .write_raw = mpu3050_write_raw,
  659. .attrs = &mpu3050_attribute_group,
  660. };
  661. /**
  662. * mpu3050_read_mem() - read MPU-3050 internal memory
  663. * @mpu3050: device to read from
  664. * @bank: target bank
  665. * @addr: target address
  666. * @len: number of bytes
  667. * @buf: the buffer to store the read bytes in
  668. */
  669. static int mpu3050_read_mem(struct mpu3050 *mpu3050,
  670. u8 bank,
  671. u8 addr,
  672. u8 len,
  673. u8 *buf)
  674. {
  675. int ret;
  676. ret = regmap_write(mpu3050->map,
  677. MPU3050_BANK_SEL,
  678. bank);
  679. if (ret)
  680. return ret;
  681. ret = regmap_write(mpu3050->map,
  682. MPU3050_MEM_START_ADDR,
  683. addr);
  684. if (ret)
  685. return ret;
  686. return regmap_bulk_read(mpu3050->map,
  687. MPU3050_MEM_R_W,
  688. buf,
  689. len);
  690. }
  691. static int mpu3050_hw_init(struct mpu3050 *mpu3050)
  692. {
  693. int ret;
  694. u8 otp[8];
  695. /* Reset */
  696. ret = regmap_update_bits(mpu3050->map,
  697. MPU3050_PWR_MGM,
  698. MPU3050_PWR_MGM_RESET,
  699. MPU3050_PWR_MGM_RESET);
  700. if (ret)
  701. return ret;
  702. /* Turn on the PLL */
  703. ret = regmap_update_bits(mpu3050->map,
  704. MPU3050_PWR_MGM,
  705. MPU3050_PWR_MGM_CLKSEL_MASK,
  706. MPU3050_PWR_MGM_PLL_Z);
  707. if (ret)
  708. return ret;
  709. /* Disable IRQs */
  710. ret = regmap_write(mpu3050->map,
  711. MPU3050_INT_CFG,
  712. 0);
  713. if (ret)
  714. return ret;
  715. /* Read out the 8 bytes of OTP (one-time-programmable) memory */
  716. ret = mpu3050_read_mem(mpu3050,
  717. (MPU3050_MEM_PRFTCH |
  718. MPU3050_MEM_USER_BANK |
  719. MPU3050_MEM_OTP_BANK_0),
  720. 0,
  721. sizeof(otp),
  722. otp);
  723. if (ret)
  724. return ret;
  725. /* This is device-unique data so it goes into the entropy pool */
  726. add_device_randomness(otp, sizeof(otp));
  727. dev_info(mpu3050->dev,
  728. "die ID: %04X, wafer ID: %02X, A lot ID: %04X, "
  729. "W lot ID: %03X, WP ID: %01X, rev ID: %02X\n",
  730. /* Die ID, bits 0-12 */
  731. (otp[1] << 8 | otp[0]) & 0x1fff,
  732. /* Wafer ID, bits 13-17 */
  733. ((otp[2] << 8 | otp[1]) & 0x03e0) >> 5,
  734. /* A lot ID, bits 18-33 */
  735. ((otp[4] << 16 | otp[3] << 8 | otp[2]) & 0x3fffc) >> 2,
  736. /* W lot ID, bits 34-45 */
  737. ((otp[5] << 8 | otp[4]) & 0x3ffc) >> 2,
  738. /* WP ID, bits 47-49 */
  739. ((otp[6] << 8 | otp[5]) & 0x0380) >> 7,
  740. /* rev ID, bits 50-55 */
  741. otp[6] >> 2);
  742. return 0;
  743. }
  744. static int mpu3050_power_up(struct mpu3050 *mpu3050)
  745. {
  746. int ret;
  747. ret = regulator_bulk_enable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs);
  748. if (ret) {
  749. dev_err(mpu3050->dev, "cannot enable regulators\n");
  750. return ret;
  751. }
  752. /*
  753. * 20-100 ms start-up time for register read/write according to
  754. * the datasheet, be on the safe side and wait 200 ms.
  755. */
  756. msleep(200);
  757. /* Take device out of sleep mode */
  758. ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
  759. MPU3050_PWR_MGM_SLEEP, 0);
  760. if (ret) {
  761. dev_err(mpu3050->dev, "error setting power mode\n");
  762. return ret;
  763. }
  764. msleep(10);
  765. return 0;
  766. }
  767. static int mpu3050_power_down(struct mpu3050 *mpu3050)
  768. {
  769. int ret;
  770. /*
  771. * Put MPU-3050 into sleep mode before cutting regulators.
  772. * This is important, because we may not be the sole user
  773. * of the regulator so the power may stay on after this, and
  774. * then we would be wasting power unless we go to sleep mode
  775. * first.
  776. */
  777. ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
  778. MPU3050_PWR_MGM_SLEEP, MPU3050_PWR_MGM_SLEEP);
  779. if (ret)
  780. dev_err(mpu3050->dev, "error putting to sleep\n");
  781. ret = regulator_bulk_disable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs);
  782. if (ret)
  783. dev_err(mpu3050->dev, "error disabling regulators\n");
  784. return 0;
  785. }
  786. static irqreturn_t mpu3050_irq_handler(int irq, void *p)
  787. {
  788. struct iio_trigger *trig = p;
  789. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  790. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  791. if (!mpu3050->hw_irq_trigger)
  792. return IRQ_NONE;
  793. /* Get the time stamp as close in time as possible */
  794. mpu3050->hw_timestamp = iio_get_time_ns(indio_dev);
  795. return IRQ_WAKE_THREAD;
  796. }
  797. static irqreturn_t mpu3050_irq_thread(int irq, void *p)
  798. {
  799. struct iio_trigger *trig = p;
  800. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  801. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  802. unsigned int val;
  803. int ret;
  804. /* ACK IRQ and check if it was from us */
  805. ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
  806. if (ret) {
  807. dev_err(mpu3050->dev, "error reading IRQ status\n");
  808. return IRQ_HANDLED;
  809. }
  810. if (!(val & MPU3050_INT_STATUS_RAW_RDY))
  811. return IRQ_NONE;
  812. iio_trigger_poll_chained(p);
  813. return IRQ_HANDLED;
  814. }
  815. /**
  816. * mpu3050_drdy_trigger_set_state() - set data ready interrupt state
  817. * @trig: trigger instance
  818. * @enable: true if trigger should be enabled, false to disable
  819. */
  820. static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
  821. bool enable)
  822. {
  823. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  824. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  825. unsigned int val;
  826. int ret;
  827. /* Disabling trigger: disable interrupt and return */
  828. if (!enable) {
  829. /* Disable all interrupts */
  830. ret = regmap_write(mpu3050->map,
  831. MPU3050_INT_CFG,
  832. 0);
  833. if (ret)
  834. dev_err(mpu3050->dev, "error disabling IRQ\n");
  835. /* Clear IRQ flag */
  836. ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
  837. if (ret)
  838. dev_err(mpu3050->dev, "error clearing IRQ status\n");
  839. /* Disable all things in the FIFO and reset it */
  840. ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
  841. if (ret)
  842. dev_err(mpu3050->dev, "error disabling FIFO\n");
  843. ret = regmap_write(mpu3050->map, MPU3050_USR_CTRL,
  844. MPU3050_USR_CTRL_FIFO_RST);
  845. if (ret)
  846. dev_err(mpu3050->dev, "error resetting FIFO\n");
  847. pm_runtime_mark_last_busy(mpu3050->dev);
  848. pm_runtime_put_autosuspend(mpu3050->dev);
  849. mpu3050->hw_irq_trigger = false;
  850. return 0;
  851. } else {
  852. /* Else we're enabling the trigger from this point */
  853. pm_runtime_get_sync(mpu3050->dev);
  854. mpu3050->hw_irq_trigger = true;
  855. /* Disable all things in the FIFO */
  856. ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
  857. if (ret)
  858. return ret;
  859. /* Reset and enable the FIFO */
  860. ret = regmap_update_bits(mpu3050->map, MPU3050_USR_CTRL,
  861. MPU3050_USR_CTRL_FIFO_EN |
  862. MPU3050_USR_CTRL_FIFO_RST,
  863. MPU3050_USR_CTRL_FIFO_EN |
  864. MPU3050_USR_CTRL_FIFO_RST);
  865. if (ret)
  866. return ret;
  867. mpu3050->pending_fifo_footer = false;
  868. /* Turn on the FIFO for temp+X+Y+Z */
  869. ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN,
  870. MPU3050_FIFO_EN_TEMP_OUT |
  871. MPU3050_FIFO_EN_GYRO_XOUT |
  872. MPU3050_FIFO_EN_GYRO_YOUT |
  873. MPU3050_FIFO_EN_GYRO_ZOUT |
  874. MPU3050_FIFO_EN_FOOTER);
  875. if (ret)
  876. return ret;
  877. /* Configure the sample engine */
  878. ret = mpu3050_start_sampling(mpu3050);
  879. if (ret)
  880. return ret;
  881. /* Clear IRQ flag */
  882. ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
  883. if (ret)
  884. dev_err(mpu3050->dev, "error clearing IRQ status\n");
  885. /* Give us interrupts whenever there is new data ready */
  886. val = MPU3050_INT_RAW_RDY_EN;
  887. if (mpu3050->irq_actl)
  888. val |= MPU3050_INT_ACTL;
  889. if (mpu3050->irq_latch)
  890. val |= MPU3050_INT_LATCH_EN;
  891. if (mpu3050->irq_opendrain)
  892. val |= MPU3050_INT_OPEN;
  893. ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
  894. if (ret)
  895. return ret;
  896. }
  897. return 0;
  898. }
  899. static const struct iio_trigger_ops mpu3050_trigger_ops = {
  900. .set_trigger_state = mpu3050_drdy_trigger_set_state,
  901. };
  902. static int mpu3050_trigger_probe(struct iio_dev *indio_dev, int irq)
  903. {
  904. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  905. unsigned long irq_trig;
  906. int ret;
  907. mpu3050->trig = devm_iio_trigger_alloc(&indio_dev->dev,
  908. "%s-dev%d",
  909. indio_dev->name,
  910. indio_dev->id);
  911. if (!mpu3050->trig)
  912. return -ENOMEM;
  913. /* Check if IRQ is open drain */
  914. if (of_property_read_bool(mpu3050->dev->of_node, "drive-open-drain"))
  915. mpu3050->irq_opendrain = true;
  916. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  917. /*
  918. * Configure the interrupt generator hardware to supply whatever
  919. * the interrupt is configured for, edges low/high level low/high,
  920. * we can provide it all.
  921. */
  922. switch (irq_trig) {
  923. case IRQF_TRIGGER_RISING:
  924. dev_info(&indio_dev->dev,
  925. "pulse interrupts on the rising edge\n");
  926. break;
  927. case IRQF_TRIGGER_FALLING:
  928. mpu3050->irq_actl = true;
  929. dev_info(&indio_dev->dev,
  930. "pulse interrupts on the falling edge\n");
  931. break;
  932. case IRQF_TRIGGER_HIGH:
  933. mpu3050->irq_latch = true;
  934. dev_info(&indio_dev->dev,
  935. "interrupts active high level\n");
  936. /*
  937. * With level IRQs, we mask the IRQ until it is processed,
  938. * but with edge IRQs (pulses) we can queue several interrupts
  939. * in the top half.
  940. */
  941. irq_trig |= IRQF_ONESHOT;
  942. break;
  943. case IRQF_TRIGGER_LOW:
  944. mpu3050->irq_latch = true;
  945. mpu3050->irq_actl = true;
  946. irq_trig |= IRQF_ONESHOT;
  947. dev_info(&indio_dev->dev,
  948. "interrupts active low level\n");
  949. break;
  950. default:
  951. /* This is the most preferred mode, if possible */
  952. dev_err(&indio_dev->dev,
  953. "unsupported IRQ trigger specified (%lx), enforce "
  954. "rising edge\n", irq_trig);
  955. irq_trig = IRQF_TRIGGER_RISING;
  956. break;
  957. }
  958. /* An open drain line can be shared with several devices */
  959. if (mpu3050->irq_opendrain)
  960. irq_trig |= IRQF_SHARED;
  961. ret = request_threaded_irq(irq,
  962. mpu3050_irq_handler,
  963. mpu3050_irq_thread,
  964. irq_trig,
  965. mpu3050->trig->name,
  966. mpu3050->trig);
  967. if (ret) {
  968. dev_err(mpu3050->dev,
  969. "can't get IRQ %d, error %d\n", irq, ret);
  970. return ret;
  971. }
  972. mpu3050->irq = irq;
  973. mpu3050->trig->dev.parent = mpu3050->dev;
  974. mpu3050->trig->ops = &mpu3050_trigger_ops;
  975. iio_trigger_set_drvdata(mpu3050->trig, indio_dev);
  976. ret = iio_trigger_register(mpu3050->trig);
  977. if (ret)
  978. return ret;
  979. indio_dev->trig = iio_trigger_get(mpu3050->trig);
  980. return 0;
  981. }
  982. int mpu3050_common_probe(struct device *dev,
  983. struct regmap *map,
  984. int irq,
  985. const char *name)
  986. {
  987. struct iio_dev *indio_dev;
  988. struct mpu3050 *mpu3050;
  989. unsigned int val;
  990. int ret;
  991. indio_dev = devm_iio_device_alloc(dev, sizeof(*mpu3050));
  992. if (!indio_dev)
  993. return -ENOMEM;
  994. mpu3050 = iio_priv(indio_dev);
  995. mpu3050->dev = dev;
  996. mpu3050->map = map;
  997. mutex_init(&mpu3050->lock);
  998. /* Default fullscale: 2000 degrees per second */
  999. mpu3050->fullscale = FS_2000_DPS;
  1000. /* 1 kHz, divide by 100, default frequency = 10 Hz */
  1001. mpu3050->lpf = MPU3050_DLPF_CFG_188HZ;
  1002. mpu3050->divisor = 99;
  1003. /* Read the mounting matrix, if present */
  1004. ret = of_iio_read_mount_matrix(dev, "mount-matrix",
  1005. &mpu3050->orientation);
  1006. if (ret)
  1007. return ret;
  1008. /* Fetch and turn on regulators */
  1009. mpu3050->regs[0].supply = mpu3050_reg_vdd;
  1010. mpu3050->regs[1].supply = mpu3050_reg_vlogic;
  1011. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(mpu3050->regs),
  1012. mpu3050->regs);
  1013. if (ret) {
  1014. dev_err(dev, "Cannot get regulators\n");
  1015. return ret;
  1016. }
  1017. ret = mpu3050_power_up(mpu3050);
  1018. if (ret)
  1019. return ret;
  1020. ret = regmap_read(map, MPU3050_CHIP_ID_REG, &val);
  1021. if (ret) {
  1022. dev_err(dev, "could not read device ID\n");
  1023. ret = -ENODEV;
  1024. goto err_power_down;
  1025. }
  1026. if (val != MPU3050_CHIP_ID) {
  1027. dev_err(dev, "unsupported chip id %02x\n", (u8)val);
  1028. ret = -ENODEV;
  1029. goto err_power_down;
  1030. }
  1031. ret = regmap_read(map, MPU3050_PRODUCT_ID_REG, &val);
  1032. if (ret) {
  1033. dev_err(dev, "could not read device ID\n");
  1034. ret = -ENODEV;
  1035. goto err_power_down;
  1036. }
  1037. dev_info(dev, "found MPU-3050 part no: %d, version: %d\n",
  1038. ((val >> 4) & 0xf), (val & 0xf));
  1039. ret = mpu3050_hw_init(mpu3050);
  1040. if (ret)
  1041. goto err_power_down;
  1042. indio_dev->dev.parent = dev;
  1043. indio_dev->channels = mpu3050_channels;
  1044. indio_dev->num_channels = ARRAY_SIZE(mpu3050_channels);
  1045. indio_dev->info = &mpu3050_info;
  1046. indio_dev->available_scan_masks = mpu3050_scan_masks;
  1047. indio_dev->modes = INDIO_DIRECT_MODE;
  1048. indio_dev->name = name;
  1049. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  1050. mpu3050_trigger_handler,
  1051. &mpu3050_buffer_setup_ops);
  1052. if (ret) {
  1053. dev_err(dev, "triggered buffer setup failed\n");
  1054. goto err_power_down;
  1055. }
  1056. ret = iio_device_register(indio_dev);
  1057. if (ret) {
  1058. dev_err(dev, "device register failed\n");
  1059. goto err_cleanup_buffer;
  1060. }
  1061. dev_set_drvdata(dev, indio_dev);
  1062. /* Check if we have an assigned IRQ to use as trigger */
  1063. if (irq) {
  1064. ret = mpu3050_trigger_probe(indio_dev, irq);
  1065. if (ret)
  1066. dev_err(dev, "failed to register trigger\n");
  1067. }
  1068. /* Enable runtime PM */
  1069. pm_runtime_get_noresume(dev);
  1070. pm_runtime_set_active(dev);
  1071. pm_runtime_enable(dev);
  1072. /*
  1073. * Set autosuspend to two orders of magnitude larger than the
  1074. * start-up time. 100ms start-up time means 10000ms autosuspend,
  1075. * i.e. 10 seconds.
  1076. */
  1077. pm_runtime_set_autosuspend_delay(dev, 10000);
  1078. pm_runtime_use_autosuspend(dev);
  1079. pm_runtime_put(dev);
  1080. return 0;
  1081. err_cleanup_buffer:
  1082. iio_triggered_buffer_cleanup(indio_dev);
  1083. err_power_down:
  1084. mpu3050_power_down(mpu3050);
  1085. return ret;
  1086. }
  1087. EXPORT_SYMBOL(mpu3050_common_probe);
  1088. int mpu3050_common_remove(struct device *dev)
  1089. {
  1090. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1091. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  1092. pm_runtime_get_sync(dev);
  1093. pm_runtime_put_noidle(dev);
  1094. pm_runtime_disable(dev);
  1095. iio_triggered_buffer_cleanup(indio_dev);
  1096. if (mpu3050->irq)
  1097. free_irq(mpu3050->irq, mpu3050);
  1098. iio_device_unregister(indio_dev);
  1099. mpu3050_power_down(mpu3050);
  1100. return 0;
  1101. }
  1102. EXPORT_SYMBOL(mpu3050_common_remove);
  1103. #ifdef CONFIG_PM
  1104. static int mpu3050_runtime_suspend(struct device *dev)
  1105. {
  1106. return mpu3050_power_down(iio_priv(dev_get_drvdata(dev)));
  1107. }
  1108. static int mpu3050_runtime_resume(struct device *dev)
  1109. {
  1110. return mpu3050_power_up(iio_priv(dev_get_drvdata(dev)));
  1111. }
  1112. #endif /* CONFIG_PM */
  1113. const struct dev_pm_ops mpu3050_dev_pm_ops = {
  1114. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1115. pm_runtime_force_resume)
  1116. SET_RUNTIME_PM_OPS(mpu3050_runtime_suspend,
  1117. mpu3050_runtime_resume, NULL)
  1118. };
  1119. EXPORT_SYMBOL(mpu3050_dev_pm_ops);
  1120. MODULE_AUTHOR("Linus Walleij");
  1121. MODULE_DESCRIPTION("MPU3050 gyroscope driver");
  1122. MODULE_LICENSE("GPL");