msa311.c 35 KB

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