mpu3050-core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. if (mpu3050->irq_opendrain) {
  929. dev_info(&indio_dev->dev,
  930. "rising edge incompatible with open drain\n");
  931. mpu3050->irq_opendrain = false;
  932. }
  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. if (mpu3050->irq_opendrain) {
  944. dev_info(&indio_dev->dev,
  945. "active high incompatible with open drain\n");
  946. mpu3050->irq_opendrain = false;
  947. }
  948. /*
  949. * With level IRQs, we mask the IRQ until it is processed,
  950. * but with edge IRQs (pulses) we can queue several interrupts
  951. * in the top half.
  952. */
  953. irq_trig |= IRQF_ONESHOT;
  954. break;
  955. case IRQF_TRIGGER_LOW:
  956. mpu3050->irq_latch = true;
  957. mpu3050->irq_actl = true;
  958. irq_trig |= IRQF_ONESHOT;
  959. dev_info(&indio_dev->dev,
  960. "interrupts active low level\n");
  961. break;
  962. default:
  963. /* This is the most preferred mode, if possible */
  964. dev_err(&indio_dev->dev,
  965. "unsupported IRQ trigger specified (%lx), enforce "
  966. "rising edge\n", irq_trig);
  967. irq_trig = IRQF_TRIGGER_RISING;
  968. break;
  969. }
  970. /* An open drain line can be shared with several devices */
  971. if (mpu3050->irq_opendrain)
  972. irq_trig |= IRQF_SHARED;
  973. ret = request_threaded_irq(irq,
  974. mpu3050_irq_handler,
  975. mpu3050_irq_thread,
  976. irq_trig,
  977. mpu3050->trig->name,
  978. mpu3050->trig);
  979. if (ret) {
  980. dev_err(mpu3050->dev,
  981. "can't get IRQ %d, error %d\n", irq, ret);
  982. return ret;
  983. }
  984. mpu3050->irq = irq;
  985. mpu3050->trig->dev.parent = mpu3050->dev;
  986. mpu3050->trig->ops = &mpu3050_trigger_ops;
  987. iio_trigger_set_drvdata(mpu3050->trig, indio_dev);
  988. ret = iio_trigger_register(mpu3050->trig);
  989. if (ret)
  990. return ret;
  991. indio_dev->trig = iio_trigger_get(mpu3050->trig);
  992. return 0;
  993. }
  994. int mpu3050_common_probe(struct device *dev,
  995. struct regmap *map,
  996. int irq,
  997. const char *name)
  998. {
  999. struct iio_dev *indio_dev;
  1000. struct mpu3050 *mpu3050;
  1001. unsigned int val;
  1002. int ret;
  1003. indio_dev = devm_iio_device_alloc(dev, sizeof(*mpu3050));
  1004. if (!indio_dev)
  1005. return -ENOMEM;
  1006. mpu3050 = iio_priv(indio_dev);
  1007. mpu3050->dev = dev;
  1008. mpu3050->map = map;
  1009. mutex_init(&mpu3050->lock);
  1010. /* Default fullscale: 2000 degrees per second */
  1011. mpu3050->fullscale = FS_2000_DPS;
  1012. /* 1 kHz, divide by 100, default frequency = 10 Hz */
  1013. mpu3050->lpf = MPU3050_DLPF_CFG_188HZ;
  1014. mpu3050->divisor = 99;
  1015. /* Read the mounting matrix, if present */
  1016. ret = of_iio_read_mount_matrix(dev, "mount-matrix",
  1017. &mpu3050->orientation);
  1018. if (ret)
  1019. return ret;
  1020. /* Fetch and turn on regulators */
  1021. mpu3050->regs[0].supply = mpu3050_reg_vdd;
  1022. mpu3050->regs[1].supply = mpu3050_reg_vlogic;
  1023. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(mpu3050->regs),
  1024. mpu3050->regs);
  1025. if (ret) {
  1026. dev_err(dev, "Cannot get regulators\n");
  1027. return ret;
  1028. }
  1029. ret = mpu3050_power_up(mpu3050);
  1030. if (ret)
  1031. return ret;
  1032. ret = regmap_read(map, MPU3050_CHIP_ID_REG, &val);
  1033. if (ret) {
  1034. dev_err(dev, "could not read device ID\n");
  1035. ret = -ENODEV;
  1036. goto err_power_down;
  1037. }
  1038. if (val != MPU3050_CHIP_ID) {
  1039. dev_err(dev, "unsupported chip id %02x\n", (u8)val);
  1040. ret = -ENODEV;
  1041. goto err_power_down;
  1042. }
  1043. ret = regmap_read(map, MPU3050_PRODUCT_ID_REG, &val);
  1044. if (ret) {
  1045. dev_err(dev, "could not read device ID\n");
  1046. ret = -ENODEV;
  1047. goto err_power_down;
  1048. }
  1049. dev_info(dev, "found MPU-3050 part no: %d, version: %d\n",
  1050. ((val >> 4) & 0xf), (val & 0xf));
  1051. ret = mpu3050_hw_init(mpu3050);
  1052. if (ret)
  1053. goto err_power_down;
  1054. indio_dev->dev.parent = dev;
  1055. indio_dev->channels = mpu3050_channels;
  1056. indio_dev->num_channels = ARRAY_SIZE(mpu3050_channels);
  1057. indio_dev->info = &mpu3050_info;
  1058. indio_dev->available_scan_masks = mpu3050_scan_masks;
  1059. indio_dev->modes = INDIO_DIRECT_MODE;
  1060. indio_dev->name = name;
  1061. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  1062. mpu3050_trigger_handler,
  1063. &mpu3050_buffer_setup_ops);
  1064. if (ret) {
  1065. dev_err(dev, "triggered buffer setup failed\n");
  1066. goto err_power_down;
  1067. }
  1068. ret = iio_device_register(indio_dev);
  1069. if (ret) {
  1070. dev_err(dev, "device register failed\n");
  1071. goto err_cleanup_buffer;
  1072. }
  1073. dev_set_drvdata(dev, indio_dev);
  1074. /* Check if we have an assigned IRQ to use as trigger */
  1075. if (irq) {
  1076. ret = mpu3050_trigger_probe(indio_dev, irq);
  1077. if (ret)
  1078. dev_err(dev, "failed to register trigger\n");
  1079. }
  1080. /* Enable runtime PM */
  1081. pm_runtime_get_noresume(dev);
  1082. pm_runtime_set_active(dev);
  1083. pm_runtime_enable(dev);
  1084. /*
  1085. * Set autosuspend to two orders of magnitude larger than the
  1086. * start-up time. 100ms start-up time means 10000ms autosuspend,
  1087. * i.e. 10 seconds.
  1088. */
  1089. pm_runtime_set_autosuspend_delay(dev, 10000);
  1090. pm_runtime_use_autosuspend(dev);
  1091. pm_runtime_put(dev);
  1092. return 0;
  1093. err_cleanup_buffer:
  1094. iio_triggered_buffer_cleanup(indio_dev);
  1095. err_power_down:
  1096. mpu3050_power_down(mpu3050);
  1097. return ret;
  1098. }
  1099. EXPORT_SYMBOL(mpu3050_common_probe);
  1100. int mpu3050_common_remove(struct device *dev)
  1101. {
  1102. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1103. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  1104. pm_runtime_get_sync(dev);
  1105. pm_runtime_put_noidle(dev);
  1106. pm_runtime_disable(dev);
  1107. iio_triggered_buffer_cleanup(indio_dev);
  1108. if (mpu3050->irq)
  1109. free_irq(mpu3050->irq, mpu3050);
  1110. iio_device_unregister(indio_dev);
  1111. mpu3050_power_down(mpu3050);
  1112. return 0;
  1113. }
  1114. EXPORT_SYMBOL(mpu3050_common_remove);
  1115. #ifdef CONFIG_PM
  1116. static int mpu3050_runtime_suspend(struct device *dev)
  1117. {
  1118. return mpu3050_power_down(iio_priv(dev_get_drvdata(dev)));
  1119. }
  1120. static int mpu3050_runtime_resume(struct device *dev)
  1121. {
  1122. return mpu3050_power_up(iio_priv(dev_get_drvdata(dev)));
  1123. }
  1124. #endif /* CONFIG_PM */
  1125. const struct dev_pm_ops mpu3050_dev_pm_ops = {
  1126. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1127. pm_runtime_force_resume)
  1128. SET_RUNTIME_PM_OPS(mpu3050_runtime_suspend,
  1129. mpu3050_runtime_resume, NULL)
  1130. };
  1131. EXPORT_SYMBOL(mpu3050_dev_pm_ops);
  1132. MODULE_AUTHOR("Linus Walleij");
  1133. MODULE_DESCRIPTION("MPU3050 gyroscope driver");
  1134. MODULE_LICENSE("GPL");