msa311.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MEMSensing digital 3-Axis accelerometer
  4. *
  5. * MSA311 is a tri-axial, low-g accelerometer with I2C digital output for
  6. * sensitivity consumer applications. It has dynamic user-selectable full
  7. * scales range of +-2g/+-4g/+-8g/+-16g and allows acceleration measurements
  8. * with output data rates from 1Hz to 1000Hz.
  9. *
  10. * MSA311 is available in an ultra small (2mm x 2mm, height 0.95mm) LGA package
  11. * and is guaranteed to operate over -40C to +85C.
  12. *
  13. * This driver supports following MSA311 features:
  14. * - IIO interface
  15. * - Different power modes: NORMAL, SUSPEND
  16. * - ODR (Output Data Rate) selection
  17. * - Scale selection
  18. * - IIO triggered buffer
  19. * - NEW_DATA interrupt + trigger
  20. *
  21. * Below features to be done:
  22. * - Motion Events: ACTIVE, TAP, ORIENT, FREEFALL
  23. * - Low Power mode
  24. *
  25. * Copyright (c) 2022, SberDevices. All Rights Reserved.
  26. *
  27. * Author: Dmitry Rokosov <ddrokosov@sberdevices.ru>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/mod_devicetable.h>
  31. #include <linux/module.h>
  32. #include <linux/pm.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/regmap.h>
  35. #include <linux/string_choices.h>
  36. #include <linux/units.h>
  37. #include <linux/iio/buffer.h>
  38. #include <linux/iio/iio.h>
  39. #include <linux/iio/sysfs.h>
  40. #include <linux/iio/trigger.h>
  41. #include <linux/iio/trigger_consumer.h>
  42. #include <linux/iio/triggered_buffer.h>
  43. #define MSA311_SOFT_RESET_REG 0x00
  44. #define MSA311_PARTID_REG 0x01
  45. #define MSA311_ACC_X_REG 0x02
  46. #define MSA311_ACC_Y_REG 0x04
  47. #define MSA311_ACC_Z_REG 0x06
  48. #define MSA311_MOTION_INT_REG 0x09
  49. #define MSA311_DATA_INT_REG 0x0A
  50. #define MSA311_TAP_ACTIVE_STS_REG 0x0B
  51. #define MSA311_ORIENT_STS_REG 0x0C
  52. #define MSA311_RANGE_REG 0x0F
  53. #define MSA311_ODR_REG 0x10
  54. #define MSA311_PWR_MODE_REG 0x11
  55. #define MSA311_SWAP_POLARITY_REG 0x12
  56. #define MSA311_INT_SET_0_REG 0x16
  57. #define MSA311_INT_SET_1_REG 0x17
  58. #define MSA311_INT_MAP_0_REG 0x19
  59. #define MSA311_INT_MAP_1_REG 0x1A
  60. #define MSA311_INT_CONFIG_REG 0x20
  61. #define MSA311_INT_LATCH_REG 0x21
  62. #define MSA311_FREEFALL_DUR_REG 0x22
  63. #define MSA311_FREEFALL_TH_REG 0x23
  64. #define MSA311_FREEFALL_HY_REG 0x24
  65. #define MSA311_ACTIVE_DUR_REG 0x27
  66. #define MSA311_ACTIVE_TH_REG 0x28
  67. #define MSA311_TAP_DUR_REG 0x2A
  68. #define MSA311_TAP_TH_REG 0x2B
  69. #define MSA311_ORIENT_HY_REG 0x2C
  70. #define MSA311_Z_BLOCK_REG 0x2D
  71. #define MSA311_OFFSET_X_REG 0x38
  72. #define MSA311_OFFSET_Y_REG 0x39
  73. #define MSA311_OFFSET_Z_REG 0x3A
  74. enum msa311_fields {
  75. /* Soft_Reset */
  76. F_SOFT_RESET_I2C, F_SOFT_RESET_SPI,
  77. /* Motion_Interrupt */
  78. F_ORIENT_INT, F_S_TAP_INT, F_D_TAP_INT, F_ACTIVE_INT, F_FREEFALL_INT,
  79. /* Data_Interrupt */
  80. F_NEW_DATA_INT,
  81. /* Tap_Active_Status */
  82. F_TAP_SIGN, F_TAP_FIRST_X, F_TAP_FIRST_Y, F_TAP_FIRST_Z, F_ACTV_SIGN,
  83. F_ACTV_FIRST_X, F_ACTV_FIRST_Y, F_ACTV_FIRST_Z,
  84. /* Orientation_Status */
  85. F_ORIENT_Z, F_ORIENT_X_Y,
  86. /* Range */
  87. F_FS,
  88. /* ODR */
  89. F_X_AXIS_DIS, F_Y_AXIS_DIS, F_Z_AXIS_DIS, F_ODR,
  90. /* Power Mode/Bandwidth */
  91. F_PWR_MODE, F_LOW_POWER_BW,
  92. /* Swap_Polarity */
  93. F_X_POLARITY, F_Y_POLARITY, F_Z_POLARITY, F_X_Y_SWAP,
  94. /* Int_Set_0 */
  95. F_ORIENT_INT_EN, F_S_TAP_INT_EN, F_D_TAP_INT_EN, F_ACTIVE_INT_EN_Z,
  96. F_ACTIVE_INT_EN_Y, F_ACTIVE_INT_EN_X,
  97. /* Int_Set_1 */
  98. F_NEW_DATA_INT_EN, F_FREEFALL_INT_EN,
  99. /* Int_Map_0 */
  100. F_INT1_ORIENT, F_INT1_S_TAP, F_INT1_D_TAP, F_INT1_ACTIVE,
  101. F_INT1_FREEFALL,
  102. /* Int_Map_1 */
  103. F_INT1_NEW_DATA,
  104. /* Int_Config */
  105. F_INT1_OD, F_INT1_LVL,
  106. /* Int_Latch */
  107. F_RESET_INT, F_LATCH_INT,
  108. /* Freefall_Hy */
  109. F_FREEFALL_MODE, F_FREEFALL_HY,
  110. /* Active_Dur */
  111. F_ACTIVE_DUR,
  112. /* Tap_Dur */
  113. F_TAP_QUIET, F_TAP_SHOCK, F_TAP_DUR,
  114. /* Tap_Th */
  115. F_TAP_TH,
  116. /* Orient_Hy */
  117. F_ORIENT_HYST, F_ORIENT_BLOCKING, F_ORIENT_MODE,
  118. /* Z_Block */
  119. F_Z_BLOCKING,
  120. /* End of register map */
  121. F_MAX_FIELDS,
  122. };
  123. static const struct reg_field msa311_reg_fields[] = {
  124. /* Soft_Reset */
  125. [F_SOFT_RESET_I2C] = REG_FIELD(MSA311_SOFT_RESET_REG, 2, 2),
  126. [F_SOFT_RESET_SPI] = REG_FIELD(MSA311_SOFT_RESET_REG, 5, 5),
  127. /* Motion_Interrupt */
  128. [F_ORIENT_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 6, 6),
  129. [F_S_TAP_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 5, 5),
  130. [F_D_TAP_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 4, 4),
  131. [F_ACTIVE_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 2, 2),
  132. [F_FREEFALL_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 0, 0),
  133. /* Data_Interrupt */
  134. [F_NEW_DATA_INT] = REG_FIELD(MSA311_DATA_INT_REG, 0, 0),
  135. /* Tap_Active_Status */
  136. [F_TAP_SIGN] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 7, 7),
  137. [F_TAP_FIRST_X] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 6, 6),
  138. [F_TAP_FIRST_Y] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 5, 5),
  139. [F_TAP_FIRST_Z] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 4, 4),
  140. [F_ACTV_SIGN] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 3, 3),
  141. [F_ACTV_FIRST_X] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 2, 2),
  142. [F_ACTV_FIRST_Y] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 1, 1),
  143. [F_ACTV_FIRST_Z] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 0, 0),
  144. /* Orientation_Status */
  145. [F_ORIENT_Z] = REG_FIELD(MSA311_ORIENT_STS_REG, 6, 6),
  146. [F_ORIENT_X_Y] = REG_FIELD(MSA311_ORIENT_STS_REG, 4, 5),
  147. /* Range */
  148. [F_FS] = REG_FIELD(MSA311_RANGE_REG, 0, 1),
  149. /* ODR */
  150. [F_X_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 7, 7),
  151. [F_Y_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 6, 6),
  152. [F_Z_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 5, 5),
  153. [F_ODR] = REG_FIELD(MSA311_ODR_REG, 0, 3),
  154. /* Power Mode/Bandwidth */
  155. [F_PWR_MODE] = REG_FIELD(MSA311_PWR_MODE_REG, 6, 7),
  156. [F_LOW_POWER_BW] = REG_FIELD(MSA311_PWR_MODE_REG, 1, 4),
  157. /* Swap_Polarity */
  158. [F_X_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 3, 3),
  159. [F_Y_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 2, 2),
  160. [F_Z_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 1, 1),
  161. [F_X_Y_SWAP] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 0, 0),
  162. /* Int_Set_0 */
  163. [F_ORIENT_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 6, 6),
  164. [F_S_TAP_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 5, 5),
  165. [F_D_TAP_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 4, 4),
  166. [F_ACTIVE_INT_EN_Z] = REG_FIELD(MSA311_INT_SET_0_REG, 2, 2),
  167. [F_ACTIVE_INT_EN_Y] = REG_FIELD(MSA311_INT_SET_0_REG, 1, 1),
  168. [F_ACTIVE_INT_EN_X] = REG_FIELD(MSA311_INT_SET_0_REG, 0, 0),
  169. /* Int_Set_1 */
  170. [F_NEW_DATA_INT_EN] = REG_FIELD(MSA311_INT_SET_1_REG, 4, 4),
  171. [F_FREEFALL_INT_EN] = REG_FIELD(MSA311_INT_SET_1_REG, 3, 3),
  172. /* Int_Map_0 */
  173. [F_INT1_ORIENT] = REG_FIELD(MSA311_INT_MAP_0_REG, 6, 6),
  174. [F_INT1_S_TAP] = REG_FIELD(MSA311_INT_MAP_0_REG, 5, 5),
  175. [F_INT1_D_TAP] = REG_FIELD(MSA311_INT_MAP_0_REG, 4, 4),
  176. [F_INT1_ACTIVE] = REG_FIELD(MSA311_INT_MAP_0_REG, 2, 2),
  177. [F_INT1_FREEFALL] = REG_FIELD(MSA311_INT_MAP_0_REG, 0, 0),
  178. /* Int_Map_1 */
  179. [F_INT1_NEW_DATA] = REG_FIELD(MSA311_INT_MAP_1_REG, 0, 0),
  180. /* Int_Config */
  181. [F_INT1_OD] = REG_FIELD(MSA311_INT_CONFIG_REG, 1, 1),
  182. [F_INT1_LVL] = REG_FIELD(MSA311_INT_CONFIG_REG, 0, 0),
  183. /* Int_Latch */
  184. [F_RESET_INT] = REG_FIELD(MSA311_INT_LATCH_REG, 7, 7),
  185. [F_LATCH_INT] = REG_FIELD(MSA311_INT_LATCH_REG, 0, 3),
  186. /* Freefall_Hy */
  187. [F_FREEFALL_MODE] = REG_FIELD(MSA311_FREEFALL_HY_REG, 2, 2),
  188. [F_FREEFALL_HY] = REG_FIELD(MSA311_FREEFALL_HY_REG, 0, 1),
  189. /* Active_Dur */
  190. [F_ACTIVE_DUR] = REG_FIELD(MSA311_ACTIVE_DUR_REG, 0, 1),
  191. /* Tap_Dur */
  192. [F_TAP_QUIET] = REG_FIELD(MSA311_TAP_DUR_REG, 7, 7),
  193. [F_TAP_SHOCK] = REG_FIELD(MSA311_TAP_DUR_REG, 6, 6),
  194. [F_TAP_DUR] = REG_FIELD(MSA311_TAP_DUR_REG, 0, 2),
  195. /* Tap_Th */
  196. [F_TAP_TH] = REG_FIELD(MSA311_TAP_TH_REG, 0, 4),
  197. /* Orient_Hy */
  198. [F_ORIENT_HYST] = REG_FIELD(MSA311_ORIENT_HY_REG, 4, 6),
  199. [F_ORIENT_BLOCKING] = REG_FIELD(MSA311_ORIENT_HY_REG, 2, 3),
  200. [F_ORIENT_MODE] = REG_FIELD(MSA311_ORIENT_HY_REG, 0, 1),
  201. /* Z_Block */
  202. [F_Z_BLOCKING] = REG_FIELD(MSA311_Z_BLOCK_REG, 0, 3),
  203. };
  204. #define MSA311_WHO_AM_I 0x13
  205. /*
  206. * Possible Full Scale ranges
  207. *
  208. * Axis data is 12-bit signed value, so
  209. *
  210. * fs0 = (2 + 2) * 9.81 / (2^11) = 0.009580
  211. * fs1 = (4 + 4) * 9.81 / (2^11) = 0.019160
  212. * fs2 = (8 + 8) * 9.81 / (2^11) = 0.038320
  213. * fs3 = (16 + 16) * 9.81 / (2^11) = 0.076641
  214. */
  215. enum {
  216. MSA311_FS_2G,
  217. MSA311_FS_4G,
  218. MSA311_FS_8G,
  219. MSA311_FS_16G,
  220. };
  221. struct iio_decimal_fract {
  222. int integral;
  223. int microfract;
  224. };
  225. static const struct iio_decimal_fract msa311_fs_table[] = {
  226. {0, 9580}, {0, 19160}, {0, 38320}, {0, 76641},
  227. };
  228. /* Possible Output Data Rate values */
  229. enum {
  230. MSA311_ODR_1_HZ,
  231. MSA311_ODR_1_95_HZ,
  232. MSA311_ODR_3_9_HZ,
  233. MSA311_ODR_7_81_HZ,
  234. MSA311_ODR_15_63_HZ,
  235. MSA311_ODR_31_25_HZ,
  236. MSA311_ODR_62_5_HZ,
  237. MSA311_ODR_125_HZ,
  238. MSA311_ODR_250_HZ,
  239. MSA311_ODR_500_HZ,
  240. MSA311_ODR_1000_HZ,
  241. };
  242. static const struct iio_decimal_fract msa311_odr_table[] = {
  243. {1, 0}, {1, 950000}, {3, 900000}, {7, 810000}, {15, 630000},
  244. {31, 250000}, {62, 500000}, {125, 0}, {250, 0}, {500, 0}, {1000, 0},
  245. };
  246. /* All supported power modes */
  247. #define MSA311_PWR_MODE_NORMAL 0b00
  248. #define MSA311_PWR_MODE_LOW 0b01
  249. #define MSA311_PWR_MODE_UNKNOWN 0b10
  250. #define MSA311_PWR_MODE_SUSPEND 0b11
  251. static const char * const msa311_pwr_modes[] = {
  252. [MSA311_PWR_MODE_NORMAL] = "normal",
  253. [MSA311_PWR_MODE_LOW] = "low",
  254. [MSA311_PWR_MODE_UNKNOWN] = "unknown",
  255. [MSA311_PWR_MODE_SUSPEND] = "suspend",
  256. };
  257. /* Autosuspend delay */
  258. #define MSA311_PWR_SLEEP_DELAY_MS 2000
  259. /* Possible INT1 types and levels */
  260. enum {
  261. MSA311_INT1_OD_PUSH_PULL,
  262. MSA311_INT1_OD_OPEN_DRAIN,
  263. };
  264. enum {
  265. MSA311_INT1_LVL_LOW,
  266. MSA311_INT1_LVL_HIGH,
  267. };
  268. /* Latch INT modes */
  269. #define MSA311_LATCH_INT_NOT_LATCHED 0b0000
  270. #define MSA311_LATCH_INT_250MS 0b0001
  271. #define MSA311_LATCH_INT_500MS 0b0010
  272. #define MSA311_LATCH_INT_1S 0b0011
  273. #define MSA311_LATCH_INT_2S 0b0100
  274. #define MSA311_LATCH_INT_4S 0b0101
  275. #define MSA311_LATCH_INT_8S 0b0110
  276. #define MSA311_LATCH_INT_1MS 0b1010
  277. #define MSA311_LATCH_INT_2MS 0b1011
  278. #define MSA311_LATCH_INT_25MS 0b1100
  279. #define MSA311_LATCH_INT_50MS 0b1101
  280. #define MSA311_LATCH_INT_100MS 0b1110
  281. #define MSA311_LATCH_INT_LATCHED 0b0111
  282. static const struct regmap_range msa311_readonly_registers[] = {
  283. regmap_reg_range(MSA311_PARTID_REG, MSA311_ORIENT_STS_REG),
  284. };
  285. static const struct regmap_access_table msa311_writeable_table = {
  286. .no_ranges = msa311_readonly_registers,
  287. .n_no_ranges = ARRAY_SIZE(msa311_readonly_registers),
  288. };
  289. static const struct regmap_range msa311_writeonly_registers[] = {
  290. regmap_reg_range(MSA311_SOFT_RESET_REG, MSA311_SOFT_RESET_REG),
  291. };
  292. static const struct regmap_access_table msa311_readable_table = {
  293. .no_ranges = msa311_writeonly_registers,
  294. .n_no_ranges = ARRAY_SIZE(msa311_writeonly_registers),
  295. };
  296. static const struct regmap_range msa311_volatile_registers[] = {
  297. regmap_reg_range(MSA311_ACC_X_REG, MSA311_ORIENT_STS_REG),
  298. };
  299. static const struct regmap_access_table msa311_volatile_table = {
  300. .yes_ranges = msa311_volatile_registers,
  301. .n_yes_ranges = ARRAY_SIZE(msa311_volatile_registers),
  302. };
  303. static const struct regmap_config msa311_regmap_config = {
  304. .name = "msa311",
  305. .reg_bits = 8,
  306. .val_bits = 8,
  307. .max_register = MSA311_OFFSET_Z_REG,
  308. .wr_table = &msa311_writeable_table,
  309. .rd_table = &msa311_readable_table,
  310. .volatile_table = &msa311_volatile_table,
  311. .cache_type = REGCACHE_RBTREE,
  312. };
  313. #define MSA311_GENMASK(field) ({ \
  314. typeof(&(msa311_reg_fields)[0]) _field; \
  315. _field = &msa311_reg_fields[(field)]; \
  316. GENMASK(_field->msb, _field->lsb); \
  317. })
  318. /**
  319. * struct msa311_priv - MSA311 internal private state
  320. * @regs: Underlying I2C bus adapter used to abstract slave
  321. * register accesses
  322. * @fields: Abstract objects for each registers fields access
  323. * @dev: Device handler associated with appropriate bus client
  324. * @lock: Protects msa311 device state between setup and data access routines
  325. * (power transitions, samp_freq/scale tune, retrieving axes data, etc)
  326. * @chip_name: Chip name in the format "msa311-%02x" % partid
  327. * @new_data_trig: Optional NEW_DATA interrupt driven trigger used
  328. * to notify external consumers a new sample is ready
  329. */
  330. struct msa311_priv {
  331. struct regmap *regs;
  332. struct regmap_field *fields[F_MAX_FIELDS];
  333. struct device *dev;
  334. struct mutex lock;
  335. char *chip_name;
  336. struct iio_trigger *new_data_trig;
  337. };
  338. enum msa311_si {
  339. MSA311_SI_X,
  340. MSA311_SI_Y,
  341. MSA311_SI_Z,
  342. MSA311_SI_TIMESTAMP,
  343. };
  344. #define MSA311_ACCEL_CHANNEL(axis) { \
  345. .type = IIO_ACCEL, \
  346. .modified = 1, \
  347. .channel2 = IIO_MOD_##axis, \
  348. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  349. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  350. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  351. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
  352. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  353. .scan_index = MSA311_SI_##axis, \
  354. .scan_type = { \
  355. .sign = 's', \
  356. .realbits = 12, \
  357. .storagebits = 16, \
  358. .shift = 4, \
  359. .endianness = IIO_LE, \
  360. }, \
  361. .datasheet_name = "ACC_"#axis, \
  362. }
  363. static const struct iio_chan_spec msa311_channels[] = {
  364. MSA311_ACCEL_CHANNEL(X),
  365. MSA311_ACCEL_CHANNEL(Y),
  366. MSA311_ACCEL_CHANNEL(Z),
  367. IIO_CHAN_SOFT_TIMESTAMP(MSA311_SI_TIMESTAMP),
  368. };
  369. /**
  370. * msa311_get_odr() - Read Output Data Rate (ODR) value from MSA311 accel
  371. * @msa311: MSA311 internal private state
  372. * @odr: output ODR value
  373. *
  374. * This function should be called under msa311->lock.
  375. *
  376. * Return: 0 on success, -ERRNO in other failures
  377. */
  378. static int msa311_get_odr(struct msa311_priv *msa311, unsigned int *odr)
  379. {
  380. int err;
  381. err = regmap_field_read(msa311->fields[F_ODR], odr);
  382. if (err)
  383. return err;
  384. /*
  385. * Filter the same 1000Hz ODR register values based on datasheet info.
  386. * ODR can be equal to 1010-1111 for 1000Hz, but function returns 1010
  387. * all the time.
  388. */
  389. if (*odr > MSA311_ODR_1000_HZ)
  390. *odr = MSA311_ODR_1000_HZ;
  391. return 0;
  392. }
  393. /**
  394. * msa311_set_odr() - Setup Output Data Rate (ODR) value for MSA311 accel
  395. * @msa311: MSA311 internal private state
  396. * @odr: requested ODR value
  397. *
  398. * This function should be called under msa311->lock. Possible ODR values:
  399. * - 1Hz (not available in normal mode)
  400. * - 1.95Hz (not available in normal mode)
  401. * - 3.9Hz
  402. * - 7.81Hz
  403. * - 15.63Hz
  404. * - 31.25Hz
  405. * - 62.5Hz
  406. * - 125Hz
  407. * - 250Hz
  408. * - 500Hz
  409. * - 1000Hz
  410. *
  411. * Return: 0 on success, -EINVAL for bad ODR value in the certain power mode,
  412. * -ERRNO in other failures
  413. */
  414. static int msa311_set_odr(struct msa311_priv *msa311, unsigned int odr)
  415. {
  416. struct device *dev = msa311->dev;
  417. unsigned int pwr_mode;
  418. bool good_odr;
  419. int err;
  420. err = regmap_field_read(msa311->fields[F_PWR_MODE], &pwr_mode);
  421. if (err)
  422. return err;
  423. /* Filter bad ODR values */
  424. if (pwr_mode == MSA311_PWR_MODE_NORMAL)
  425. good_odr = (odr > MSA311_ODR_1_95_HZ);
  426. else
  427. good_odr = false;
  428. if (!good_odr) {
  429. dev_err(dev,
  430. "can't set odr %u.%06uHz, not available in %s mode\n",
  431. msa311_odr_table[odr].integral,
  432. msa311_odr_table[odr].microfract,
  433. msa311_pwr_modes[pwr_mode]);
  434. return -EINVAL;
  435. }
  436. return regmap_field_write(msa311->fields[F_ODR], odr);
  437. }
  438. /**
  439. * msa311_wait_for_next_data() - Wait next accel data available after resume
  440. * @msa311: MSA311 internal private state
  441. *
  442. * Return: 0 on success, -EINTR if msleep() was interrupted,
  443. * -ERRNO in other failures
  444. */
  445. static int msa311_wait_for_next_data(struct msa311_priv *msa311)
  446. {
  447. static const unsigned int unintr_thresh_ms = 20;
  448. struct device *dev = msa311->dev;
  449. unsigned long freq_uhz;
  450. unsigned long wait_ms;
  451. unsigned int odr;
  452. int err;
  453. err = msa311_get_odr(msa311, &odr);
  454. if (err) {
  455. dev_err(dev, "can't get actual frequency (%pe)\n",
  456. ERR_PTR(err));
  457. return err;
  458. }
  459. /*
  460. * After msa311 resuming is done, we need to wait for data
  461. * to be refreshed by accel logic.
  462. * A certain timeout is calculated based on the current ODR value.
  463. * If requested timeout isn't so long (let's assume 20ms),
  464. * we can wait for next data in uninterruptible sleep.
  465. */
  466. freq_uhz = msa311_odr_table[odr].integral * MICROHZ_PER_HZ +
  467. msa311_odr_table[odr].microfract;
  468. wait_ms = (MICROHZ_PER_HZ / freq_uhz) * MSEC_PER_SEC;
  469. if (wait_ms < unintr_thresh_ms)
  470. usleep_range(wait_ms * USEC_PER_MSEC,
  471. unintr_thresh_ms * USEC_PER_MSEC);
  472. else if (msleep_interruptible(wait_ms))
  473. return -EINTR;
  474. return 0;
  475. }
  476. /**
  477. * msa311_set_pwr_mode() - Install certain MSA311 power mode
  478. * @msa311: MSA311 internal private state
  479. * @mode: Power mode can be equal to NORMAL or SUSPEND
  480. *
  481. * This function should be called under msa311->lock.
  482. *
  483. * Return: 0 on success, -ERRNO on failure
  484. */
  485. static int msa311_set_pwr_mode(struct msa311_priv *msa311, unsigned int mode)
  486. {
  487. struct device *dev = msa311->dev;
  488. unsigned int prev_mode;
  489. int err;
  490. if (mode >= ARRAY_SIZE(msa311_pwr_modes))
  491. return -EINVAL;
  492. dev_dbg(dev, "transition to %s mode\n", msa311_pwr_modes[mode]);
  493. err = regmap_field_read(msa311->fields[F_PWR_MODE], &prev_mode);
  494. if (err)
  495. return err;
  496. err = regmap_field_write(msa311->fields[F_PWR_MODE], mode);
  497. if (err)
  498. return err;
  499. /* Wait actual data if we wake up */
  500. if (prev_mode == MSA311_PWR_MODE_SUSPEND &&
  501. mode == MSA311_PWR_MODE_NORMAL)
  502. return msa311_wait_for_next_data(msa311);
  503. return 0;
  504. }
  505. /**
  506. * msa311_get_axis() - Read MSA311 accel data for certain IIO channel axis spec
  507. * @msa311: MSA311 internal private state
  508. * @chan: IIO channel specification
  509. * @axis: Output accel axis data for requested IIO channel spec
  510. *
  511. * This function should be called under msa311->lock.
  512. *
  513. * Return: 0 on success, -EINVAL for unknown IIO channel specification,
  514. * -ERRNO in other failures
  515. */
  516. static int msa311_get_axis(struct msa311_priv *msa311,
  517. const struct iio_chan_spec * const chan,
  518. __le16 *axis)
  519. {
  520. struct device *dev = msa311->dev;
  521. unsigned int axis_reg;
  522. if (chan->scan_index < MSA311_SI_X || chan->scan_index > MSA311_SI_Z) {
  523. dev_err(dev, "invalid scan_index value [%d]\n",
  524. chan->scan_index);
  525. return -EINVAL;
  526. }
  527. /* Axes data layout has 2 byte gap for each axis starting from X axis */
  528. axis_reg = MSA311_ACC_X_REG + (chan->scan_index << 1);
  529. return regmap_bulk_read(msa311->regs, axis_reg, axis, sizeof(*axis));
  530. }
  531. static int msa311_read_raw_data(struct iio_dev *indio_dev,
  532. struct iio_chan_spec const *chan,
  533. int *val, int *val2)
  534. {
  535. struct msa311_priv *msa311 = iio_priv(indio_dev);
  536. struct device *dev = msa311->dev;
  537. __le16 axis;
  538. int err;
  539. err = pm_runtime_resume_and_get(dev);
  540. if (err)
  541. return err;
  542. err = iio_device_claim_direct_mode(indio_dev);
  543. if (err)
  544. return err;
  545. mutex_lock(&msa311->lock);
  546. err = msa311_get_axis(msa311, chan, &axis);
  547. mutex_unlock(&msa311->lock);
  548. iio_device_release_direct_mode(indio_dev);
  549. pm_runtime_mark_last_busy(dev);
  550. pm_runtime_put_autosuspend(dev);
  551. if (err) {
  552. dev_err(dev, "can't get axis %s (%pe)\n",
  553. chan->datasheet_name, ERR_PTR(err));
  554. return err;
  555. }
  556. /*
  557. * Axis data format is:
  558. * ACC_X = (ACC_X_MSB[7:0] << 4) | ACC_X_LSB[7:4]
  559. */
  560. *val = sign_extend32(le16_to_cpu(axis) >> chan->scan_type.shift,
  561. chan->scan_type.realbits - 1);
  562. return IIO_VAL_INT;
  563. }
  564. static int msa311_read_scale(struct iio_dev *indio_dev, int *val, int *val2)
  565. {
  566. struct msa311_priv *msa311 = iio_priv(indio_dev);
  567. struct device *dev = msa311->dev;
  568. unsigned int fs;
  569. int err;
  570. mutex_lock(&msa311->lock);
  571. err = regmap_field_read(msa311->fields[F_FS], &fs);
  572. mutex_unlock(&msa311->lock);
  573. if (err) {
  574. dev_err(dev, "can't get actual scale (%pe)\n", ERR_PTR(err));
  575. return err;
  576. }
  577. *val = msa311_fs_table[fs].integral;
  578. *val2 = msa311_fs_table[fs].microfract;
  579. return IIO_VAL_INT_PLUS_MICRO;
  580. }
  581. static int msa311_read_samp_freq(struct iio_dev *indio_dev,
  582. int *val, int *val2)
  583. {
  584. struct msa311_priv *msa311 = iio_priv(indio_dev);
  585. struct device *dev = msa311->dev;
  586. unsigned int odr;
  587. int err;
  588. mutex_lock(&msa311->lock);
  589. err = msa311_get_odr(msa311, &odr);
  590. mutex_unlock(&msa311->lock);
  591. if (err) {
  592. dev_err(dev, "can't get actual frequency (%pe)\n",
  593. ERR_PTR(err));
  594. return err;
  595. }
  596. *val = msa311_odr_table[odr].integral;
  597. *val2 = msa311_odr_table[odr].microfract;
  598. return IIO_VAL_INT_PLUS_MICRO;
  599. }
  600. static int msa311_read_raw(struct iio_dev *indio_dev,
  601. struct iio_chan_spec const *chan,
  602. int *val, int *val2, long mask)
  603. {
  604. switch (mask) {
  605. case IIO_CHAN_INFO_RAW:
  606. return msa311_read_raw_data(indio_dev, chan, val, val2);
  607. case IIO_CHAN_INFO_SCALE:
  608. return msa311_read_scale(indio_dev, val, val2);
  609. case IIO_CHAN_INFO_SAMP_FREQ:
  610. return msa311_read_samp_freq(indio_dev, val, val2);
  611. default:
  612. return -EINVAL;
  613. }
  614. }
  615. static int msa311_read_avail(struct iio_dev *indio_dev,
  616. struct iio_chan_spec const *chan,
  617. const int **vals, int *type,
  618. int *length, long mask)
  619. {
  620. switch (mask) {
  621. case IIO_CHAN_INFO_SAMP_FREQ:
  622. *vals = (int *)msa311_odr_table;
  623. *type = IIO_VAL_INT_PLUS_MICRO;
  624. /* ODR value has 2 ints (integer and fractional parts) */
  625. *length = ARRAY_SIZE(msa311_odr_table) * 2;
  626. return IIO_AVAIL_LIST;
  627. case IIO_CHAN_INFO_SCALE:
  628. *vals = (int *)msa311_fs_table;
  629. *type = IIO_VAL_INT_PLUS_MICRO;
  630. /* FS value has 2 ints (integer and fractional parts) */
  631. *length = ARRAY_SIZE(msa311_fs_table) * 2;
  632. return IIO_AVAIL_LIST;
  633. default:
  634. return -EINVAL;
  635. }
  636. }
  637. static int msa311_write_scale(struct iio_dev *indio_dev, int val, int val2)
  638. {
  639. struct msa311_priv *msa311 = iio_priv(indio_dev);
  640. struct device *dev = msa311->dev;
  641. unsigned int fs;
  642. int err;
  643. /* We do not have fs >= 1, so skip such values */
  644. if (val)
  645. return 0;
  646. err = pm_runtime_resume_and_get(dev);
  647. if (err)
  648. return err;
  649. err = -EINVAL;
  650. for (fs = 0; fs < ARRAY_SIZE(msa311_fs_table); fs++)
  651. /* Do not check msa311_fs_table[fs].integral, it's always 0 */
  652. if (val2 == msa311_fs_table[fs].microfract) {
  653. mutex_lock(&msa311->lock);
  654. err = regmap_field_write(msa311->fields[F_FS], fs);
  655. mutex_unlock(&msa311->lock);
  656. break;
  657. }
  658. pm_runtime_mark_last_busy(dev);
  659. pm_runtime_put_autosuspend(dev);
  660. if (err)
  661. dev_err(dev, "can't update scale (%pe)\n", ERR_PTR(err));
  662. return err;
  663. }
  664. static int msa311_write_samp_freq(struct iio_dev *indio_dev, int val, int val2)
  665. {
  666. struct msa311_priv *msa311 = iio_priv(indio_dev);
  667. struct device *dev = msa311->dev;
  668. unsigned int odr;
  669. int err;
  670. err = pm_runtime_resume_and_get(dev);
  671. if (err)
  672. return err;
  673. /*
  674. * Sampling frequency changing is prohibited when buffer mode is
  675. * enabled, because sometimes MSA311 chip returns outliers during
  676. * frequency values growing up in the read operation moment.
  677. */
  678. err = iio_device_claim_direct_mode(indio_dev);
  679. if (err)
  680. return err;
  681. err = -EINVAL;
  682. for (odr = 0; odr < ARRAY_SIZE(msa311_odr_table); odr++)
  683. if (val == msa311_odr_table[odr].integral &&
  684. val2 == msa311_odr_table[odr].microfract) {
  685. mutex_lock(&msa311->lock);
  686. err = msa311_set_odr(msa311, odr);
  687. mutex_unlock(&msa311->lock);
  688. break;
  689. }
  690. iio_device_release_direct_mode(indio_dev);
  691. pm_runtime_mark_last_busy(dev);
  692. pm_runtime_put_autosuspend(dev);
  693. if (err)
  694. dev_err(dev, "can't update frequency (%pe)\n", ERR_PTR(err));
  695. return err;
  696. }
  697. static int msa311_write_raw(struct iio_dev *indio_dev,
  698. struct iio_chan_spec const *chan,
  699. int val, int val2, long mask)
  700. {
  701. switch (mask) {
  702. case IIO_CHAN_INFO_SCALE:
  703. return msa311_write_scale(indio_dev, val, val2);
  704. case IIO_CHAN_INFO_SAMP_FREQ:
  705. return msa311_write_samp_freq(indio_dev, val, val2);
  706. default:
  707. return -EINVAL;
  708. }
  709. }
  710. static int msa311_debugfs_reg_access(struct iio_dev *indio_dev,
  711. unsigned int reg, unsigned int writeval,
  712. unsigned int *readval)
  713. {
  714. struct msa311_priv *msa311 = iio_priv(indio_dev);
  715. struct device *dev = msa311->dev;
  716. int err;
  717. if (reg > regmap_get_max_register(msa311->regs))
  718. return -EINVAL;
  719. err = pm_runtime_resume_and_get(dev);
  720. if (err)
  721. return err;
  722. mutex_lock(&msa311->lock);
  723. if (readval)
  724. err = regmap_read(msa311->regs, reg, readval);
  725. else
  726. err = regmap_write(msa311->regs, reg, writeval);
  727. mutex_unlock(&msa311->lock);
  728. pm_runtime_mark_last_busy(dev);
  729. pm_runtime_put_autosuspend(dev);
  730. if (err)
  731. dev_err(dev, "can't %s register %u from debugfs (%pe)\n",
  732. str_read_write(readval), reg, ERR_PTR(err));
  733. return err;
  734. }
  735. static int msa311_buffer_preenable(struct iio_dev *indio_dev)
  736. {
  737. struct msa311_priv *msa311 = iio_priv(indio_dev);
  738. struct device *dev = msa311->dev;
  739. return pm_runtime_resume_and_get(dev);
  740. }
  741. static int msa311_buffer_postdisable(struct iio_dev *indio_dev)
  742. {
  743. struct msa311_priv *msa311 = iio_priv(indio_dev);
  744. struct device *dev = msa311->dev;
  745. pm_runtime_mark_last_busy(dev);
  746. pm_runtime_put_autosuspend(dev);
  747. return 0;
  748. }
  749. static int msa311_set_new_data_trig_state(struct iio_trigger *trig, bool state)
  750. {
  751. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  752. struct msa311_priv *msa311 = iio_priv(indio_dev);
  753. struct device *dev = msa311->dev;
  754. int err;
  755. mutex_lock(&msa311->lock);
  756. err = regmap_field_write(msa311->fields[F_NEW_DATA_INT_EN], state);
  757. mutex_unlock(&msa311->lock);
  758. if (err)
  759. dev_err(dev,
  760. "can't %s buffer due to new_data_int failure (%pe)\n",
  761. str_enable_disable(state), ERR_PTR(err));
  762. return err;
  763. }
  764. static int msa311_validate_device(struct iio_trigger *trig,
  765. struct iio_dev *indio_dev)
  766. {
  767. return iio_trigger_get_drvdata(trig) == indio_dev ? 0 : -EINVAL;
  768. }
  769. static irqreturn_t msa311_buffer_thread(int irq, void *p)
  770. {
  771. struct iio_poll_func *pf = p;
  772. struct msa311_priv *msa311 = iio_priv(pf->indio_dev);
  773. struct iio_dev *indio_dev = pf->indio_dev;
  774. const struct iio_chan_spec *chan;
  775. struct device *dev = msa311->dev;
  776. int bit, err, i = 0;
  777. __le16 axis;
  778. struct {
  779. __le16 channels[MSA311_SI_Z + 1];
  780. s64 ts __aligned(8);
  781. } buf;
  782. memset(&buf, 0, sizeof(buf));
  783. mutex_lock(&msa311->lock);
  784. iio_for_each_active_channel(indio_dev, bit) {
  785. chan = &msa311_channels[bit];
  786. err = msa311_get_axis(msa311, chan, &axis);
  787. if (err) {
  788. mutex_unlock(&msa311->lock);
  789. dev_err(dev, "can't get axis %s (%pe)\n",
  790. chan->datasheet_name, ERR_PTR(err));
  791. goto notify_done;
  792. }
  793. buf.channels[i++] = axis;
  794. }
  795. mutex_unlock(&msa311->lock);
  796. iio_push_to_buffers_with_timestamp(indio_dev, &buf,
  797. iio_get_time_ns(indio_dev));
  798. notify_done:
  799. iio_trigger_notify_done(indio_dev->trig);
  800. return IRQ_HANDLED;
  801. }
  802. static irqreturn_t msa311_irq_thread(int irq, void *p)
  803. {
  804. struct msa311_priv *msa311 = iio_priv(p);
  805. unsigned int new_data_int_enabled;
  806. struct device *dev = msa311->dev;
  807. int err;
  808. mutex_lock(&msa311->lock);
  809. /*
  810. * We do not check NEW_DATA int status, because based on the
  811. * specification it's cleared automatically after a fixed time.
  812. * So just check that is enabled by driver logic.
  813. */
  814. err = regmap_field_read(msa311->fields[F_NEW_DATA_INT_EN],
  815. &new_data_int_enabled);
  816. mutex_unlock(&msa311->lock);
  817. if (err) {
  818. dev_err(dev, "can't read new_data interrupt state (%pe)\n",
  819. ERR_PTR(err));
  820. return IRQ_NONE;
  821. }
  822. if (new_data_int_enabled)
  823. iio_trigger_poll_nested(msa311->new_data_trig);
  824. return IRQ_HANDLED;
  825. }
  826. static const struct iio_info msa311_info = {
  827. .read_raw = msa311_read_raw,
  828. .read_avail = msa311_read_avail,
  829. .write_raw = msa311_write_raw,
  830. .debugfs_reg_access = msa311_debugfs_reg_access,
  831. };
  832. static const struct iio_buffer_setup_ops msa311_buffer_setup_ops = {
  833. .preenable = msa311_buffer_preenable,
  834. .postdisable = msa311_buffer_postdisable,
  835. };
  836. static const struct iio_trigger_ops msa311_new_data_trig_ops = {
  837. .set_trigger_state = msa311_set_new_data_trig_state,
  838. .validate_device = msa311_validate_device,
  839. };
  840. static int msa311_check_partid(struct msa311_priv *msa311)
  841. {
  842. struct device *dev = msa311->dev;
  843. unsigned int partid;
  844. int err;
  845. err = regmap_read(msa311->regs, MSA311_PARTID_REG, &partid);
  846. if (err)
  847. return dev_err_probe(dev, err, "failed to read partid\n");
  848. if (partid != MSA311_WHO_AM_I)
  849. dev_warn(dev, "invalid partid (%#x), expected (%#x)\n",
  850. partid, MSA311_WHO_AM_I);
  851. msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL,
  852. "msa311-%02x", partid);
  853. if (!msa311->chip_name)
  854. return dev_err_probe(dev, -ENOMEM, "can't alloc chip name\n");
  855. return 0;
  856. }
  857. static int msa311_soft_reset(struct msa311_priv *msa311)
  858. {
  859. struct device *dev = msa311->dev;
  860. int err;
  861. err = regmap_write(msa311->regs, MSA311_SOFT_RESET_REG,
  862. MSA311_GENMASK(F_SOFT_RESET_I2C) |
  863. MSA311_GENMASK(F_SOFT_RESET_SPI));
  864. if (err)
  865. return dev_err_probe(dev, err, "can't soft reset all logic\n");
  866. return 0;
  867. }
  868. static int msa311_chip_init(struct msa311_priv *msa311)
  869. {
  870. struct device *dev = msa311->dev;
  871. const char zero_bulk[2] = { };
  872. int err;
  873. err = regmap_write(msa311->regs, MSA311_RANGE_REG, MSA311_FS_16G);
  874. if (err)
  875. return dev_err_probe(dev, err, "failed to setup accel range\n");
  876. /* Disable all interrupts by default */
  877. err = regmap_bulk_write(msa311->regs, MSA311_INT_SET_0_REG,
  878. zero_bulk, sizeof(zero_bulk));
  879. if (err)
  880. return dev_err_probe(dev, err,
  881. "can't disable set0/set1 interrupts\n");
  882. /* Unmap all INT1 interrupts by default */
  883. err = regmap_bulk_write(msa311->regs, MSA311_INT_MAP_0_REG,
  884. zero_bulk, sizeof(zero_bulk));
  885. if (err)
  886. return dev_err_probe(dev, err,
  887. "failed to unmap map0/map1 interrupts\n");
  888. /* Disable all axes by default */
  889. err = regmap_clear_bits(msa311->regs, MSA311_ODR_REG,
  890. MSA311_GENMASK(F_X_AXIS_DIS) |
  891. MSA311_GENMASK(F_Y_AXIS_DIS) |
  892. MSA311_GENMASK(F_Z_AXIS_DIS));
  893. if (err)
  894. return dev_err_probe(dev, err, "can't enable all axes\n");
  895. err = msa311_set_odr(msa311, MSA311_ODR_125_HZ);
  896. if (err)
  897. return dev_err_probe(dev, err,
  898. "failed to set accel frequency\n");
  899. return 0;
  900. }
  901. static int msa311_setup_interrupts(struct msa311_priv *msa311)
  902. {
  903. struct device *dev = msa311->dev;
  904. struct i2c_client *i2c = to_i2c_client(dev);
  905. struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
  906. struct iio_trigger *trig;
  907. int err;
  908. /* Keep going without interrupts if no initialized I2C IRQ */
  909. if (i2c->irq <= 0)
  910. return 0;
  911. err = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
  912. msa311_irq_thread, IRQF_ONESHOT,
  913. msa311->chip_name, indio_dev);
  914. if (err)
  915. return dev_err_probe(dev, err, "failed to request IRQ\n");
  916. trig = devm_iio_trigger_alloc(dev, "%s-new-data", msa311->chip_name);
  917. if (!trig)
  918. return dev_err_probe(dev, -ENOMEM,
  919. "can't allocate newdata trigger\n");
  920. msa311->new_data_trig = trig;
  921. msa311->new_data_trig->ops = &msa311_new_data_trig_ops;
  922. iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev);
  923. err = devm_iio_trigger_register(dev, msa311->new_data_trig);
  924. if (err)
  925. return dev_err_probe(dev, err,
  926. "can't register newdata trigger\n");
  927. err = regmap_field_write(msa311->fields[F_INT1_OD],
  928. MSA311_INT1_OD_PUSH_PULL);
  929. if (err)
  930. return dev_err_probe(dev, err,
  931. "can't enable push-pull interrupt\n");
  932. err = regmap_field_write(msa311->fields[F_INT1_LVL],
  933. MSA311_INT1_LVL_HIGH);
  934. if (err)
  935. return dev_err_probe(dev, err,
  936. "can't set active interrupt level\n");
  937. err = regmap_field_write(msa311->fields[F_LATCH_INT],
  938. MSA311_LATCH_INT_LATCHED);
  939. if (err)
  940. return dev_err_probe(dev, err,
  941. "can't latch interrupt\n");
  942. err = regmap_field_write(msa311->fields[F_RESET_INT], 1);
  943. if (err)
  944. return dev_err_probe(dev, err,
  945. "can't reset interrupt\n");
  946. err = regmap_field_write(msa311->fields[F_INT1_NEW_DATA], 1);
  947. if (err)
  948. return dev_err_probe(dev, err,
  949. "can't map new data interrupt\n");
  950. return 0;
  951. }
  952. static int msa311_regmap_init(struct msa311_priv *msa311)
  953. {
  954. struct regmap_field **fields = msa311->fields;
  955. struct device *dev = msa311->dev;
  956. struct i2c_client *i2c = to_i2c_client(dev);
  957. struct regmap *regmap;
  958. int i;
  959. regmap = devm_regmap_init_i2c(i2c, &msa311_regmap_config);
  960. if (IS_ERR(regmap))
  961. return dev_err_probe(dev, PTR_ERR(regmap),
  962. "failed to register i2c regmap\n");
  963. msa311->regs = regmap;
  964. for (i = 0; i < F_MAX_FIELDS; i++) {
  965. fields[i] = devm_regmap_field_alloc(dev,
  966. msa311->regs,
  967. msa311_reg_fields[i]);
  968. if (IS_ERR(msa311->fields[i]))
  969. return dev_err_probe(dev, PTR_ERR(msa311->fields[i]),
  970. "can't alloc field[%d]\n", i);
  971. }
  972. return 0;
  973. }
  974. static void msa311_powerdown(void *msa311)
  975. {
  976. msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
  977. }
  978. static int msa311_probe(struct i2c_client *i2c)
  979. {
  980. struct device *dev = &i2c->dev;
  981. struct msa311_priv *msa311;
  982. struct iio_dev *indio_dev;
  983. int err;
  984. indio_dev = devm_iio_device_alloc(dev, sizeof(*msa311));
  985. if (!indio_dev)
  986. return dev_err_probe(dev, -ENOMEM,
  987. "IIO device allocation failed\n");
  988. msa311 = iio_priv(indio_dev);
  989. msa311->dev = dev;
  990. i2c_set_clientdata(i2c, indio_dev);
  991. err = msa311_regmap_init(msa311);
  992. if (err)
  993. return err;
  994. mutex_init(&msa311->lock);
  995. err = devm_regulator_get_enable(dev, "vdd");
  996. if (err)
  997. return dev_err_probe(dev, err, "can't get vdd supply\n");
  998. err = msa311_check_partid(msa311);
  999. if (err)
  1000. return err;
  1001. err = msa311_soft_reset(msa311);
  1002. if (err)
  1003. return err;
  1004. err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
  1005. if (err)
  1006. return dev_err_probe(dev, err, "failed to power on device\n");
  1007. /*
  1008. * Register powerdown deferred callback which suspends the chip
  1009. * after module unloaded.
  1010. *
  1011. * MSA311 should be in SUSPEND mode in the two cases:
  1012. * 1) When driver is loaded, but we do not have any data or
  1013. * configuration requests to it (we are solving it using
  1014. * autosuspend feature).
  1015. * 2) When driver is unloaded and device is not used (devm action is
  1016. * used in this case).
  1017. */
  1018. err = devm_add_action_or_reset(dev, msa311_powerdown, msa311);
  1019. if (err)
  1020. return dev_err_probe(dev, err, "can't add powerdown action\n");
  1021. err = pm_runtime_set_active(dev);
  1022. if (err)
  1023. return err;
  1024. err = devm_pm_runtime_enable(dev);
  1025. if (err)
  1026. return err;
  1027. pm_runtime_get_noresume(dev);
  1028. pm_runtime_set_autosuspend_delay(dev, MSA311_PWR_SLEEP_DELAY_MS);
  1029. pm_runtime_use_autosuspend(dev);
  1030. err = msa311_chip_init(msa311);
  1031. if (err)
  1032. return err;
  1033. indio_dev->modes = INDIO_DIRECT_MODE;
  1034. indio_dev->channels = msa311_channels;
  1035. indio_dev->num_channels = ARRAY_SIZE(msa311_channels);
  1036. indio_dev->name = msa311->chip_name;
  1037. indio_dev->info = &msa311_info;
  1038. err = devm_iio_triggered_buffer_setup(dev, indio_dev,
  1039. iio_pollfunc_store_time,
  1040. msa311_buffer_thread,
  1041. &msa311_buffer_setup_ops);
  1042. if (err)
  1043. return dev_err_probe(dev, err,
  1044. "can't setup IIO trigger buffer\n");
  1045. err = msa311_setup_interrupts(msa311);
  1046. if (err)
  1047. return err;
  1048. pm_runtime_mark_last_busy(dev);
  1049. pm_runtime_put_autosuspend(dev);
  1050. err = devm_iio_device_register(dev, indio_dev);
  1051. if (err)
  1052. return dev_err_probe(dev, err, "IIO device register failed\n");
  1053. return 0;
  1054. }
  1055. static int msa311_runtime_suspend(struct device *dev)
  1056. {
  1057. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1058. struct msa311_priv *msa311 = iio_priv(indio_dev);
  1059. int err;
  1060. mutex_lock(&msa311->lock);
  1061. err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
  1062. mutex_unlock(&msa311->lock);
  1063. if (err)
  1064. dev_err(dev, "failed to power off device (%pe)\n",
  1065. ERR_PTR(err));
  1066. return err;
  1067. }
  1068. static int msa311_runtime_resume(struct device *dev)
  1069. {
  1070. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1071. struct msa311_priv *msa311 = iio_priv(indio_dev);
  1072. int err;
  1073. mutex_lock(&msa311->lock);
  1074. err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
  1075. mutex_unlock(&msa311->lock);
  1076. if (err)
  1077. dev_err(dev, "failed to power on device (%pe)\n",
  1078. ERR_PTR(err));
  1079. return err;
  1080. }
  1081. static DEFINE_RUNTIME_DEV_PM_OPS(msa311_pm_ops, msa311_runtime_suspend,
  1082. msa311_runtime_resume, NULL);
  1083. static const struct i2c_device_id msa311_i2c_id[] = {
  1084. { .name = "msa311" },
  1085. { }
  1086. };
  1087. MODULE_DEVICE_TABLE(i2c, msa311_i2c_id);
  1088. static const struct of_device_id msa311_of_match[] = {
  1089. { .compatible = "memsensing,msa311" },
  1090. { }
  1091. };
  1092. MODULE_DEVICE_TABLE(of, msa311_of_match);
  1093. static struct i2c_driver msa311_driver = {
  1094. .driver = {
  1095. .name = "msa311",
  1096. .of_match_table = msa311_of_match,
  1097. .pm = pm_ptr(&msa311_pm_ops),
  1098. },
  1099. .probe = msa311_probe,
  1100. .id_table = msa311_i2c_id,
  1101. };
  1102. module_i2c_driver(msa311_driver);
  1103. MODULE_AUTHOR("Dmitry Rokosov <ddrokosov@sberdevices.ru>");
  1104. MODULE_DESCRIPTION("MEMSensing MSA311 3-axis accelerometer driver");
  1105. MODULE_LICENSE("GPL");