msa311.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 <[email protected]>
  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_helpers.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. * @vdd: Optional external voltage regulator for the device power supply
  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. struct regulator *vdd;
  339. };
  340. enum msa311_si {
  341. MSA311_SI_X,
  342. MSA311_SI_Y,
  343. MSA311_SI_Z,
  344. MSA311_SI_TIMESTAMP,
  345. };
  346. #define MSA311_ACCEL_CHANNEL(axis) { \
  347. .type = IIO_ACCEL, \
  348. .modified = 1, \
  349. .channel2 = IIO_MOD_##axis, \
  350. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  351. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  352. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  353. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
  354. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  355. .scan_index = MSA311_SI_##axis, \
  356. .scan_type = { \
  357. .sign = 's', \
  358. .realbits = 12, \
  359. .storagebits = 16, \
  360. .shift = 4, \
  361. .endianness = IIO_LE, \
  362. }, \
  363. .datasheet_name = "ACC_"#axis, \
  364. }
  365. static const struct iio_chan_spec msa311_channels[] = {
  366. MSA311_ACCEL_CHANNEL(X),
  367. MSA311_ACCEL_CHANNEL(Y),
  368. MSA311_ACCEL_CHANNEL(Z),
  369. IIO_CHAN_SOFT_TIMESTAMP(MSA311_SI_TIMESTAMP),
  370. };
  371. /**
  372. * msa311_get_odr() - Read Output Data Rate (ODR) value from MSA311 accel
  373. * @msa311: MSA311 internal private state
  374. * @odr: output ODR value
  375. *
  376. * This function should be called under msa311->lock.
  377. *
  378. * Return: 0 on success, -ERRNO in other failures
  379. */
  380. static int msa311_get_odr(struct msa311_priv *msa311, unsigned int *odr)
  381. {
  382. int err;
  383. err = regmap_field_read(msa311->fields[F_ODR], odr);
  384. if (err)
  385. return err;
  386. /*
  387. * Filter the same 1000Hz ODR register values based on datasheet info.
  388. * ODR can be equal to 1010-1111 for 1000Hz, but function returns 1010
  389. * all the time.
  390. */
  391. if (*odr > MSA311_ODR_1000_HZ)
  392. *odr = MSA311_ODR_1000_HZ;
  393. return 0;
  394. }
  395. /**
  396. * msa311_set_odr() - Setup Output Data Rate (ODR) value for MSA311 accel
  397. * @msa311: MSA311 internal private state
  398. * @odr: requested ODR value
  399. *
  400. * This function should be called under msa311->lock. Possible ODR values:
  401. * - 1Hz (not available in normal mode)
  402. * - 1.95Hz (not available in normal mode)
  403. * - 3.9Hz
  404. * - 7.81Hz
  405. * - 15.63Hz
  406. * - 31.25Hz
  407. * - 62.5Hz
  408. * - 125Hz
  409. * - 250Hz
  410. * - 500Hz
  411. * - 1000Hz
  412. *
  413. * Return: 0 on success, -EINVAL for bad ODR value in the certain power mode,
  414. * -ERRNO in other failures
  415. */
  416. static int msa311_set_odr(struct msa311_priv *msa311, unsigned int odr)
  417. {
  418. struct device *dev = msa311->dev;
  419. unsigned int pwr_mode;
  420. bool good_odr;
  421. int err;
  422. err = regmap_field_read(msa311->fields[F_PWR_MODE], &pwr_mode);
  423. if (err)
  424. return err;
  425. /* Filter bad ODR values */
  426. if (pwr_mode == MSA311_PWR_MODE_NORMAL)
  427. good_odr = (odr > MSA311_ODR_1_95_HZ);
  428. else
  429. good_odr = false;
  430. if (!good_odr) {
  431. dev_err(dev,
  432. "can't set odr %u.%06uHz, not available in %s mode\n",
  433. msa311_odr_table[odr].integral,
  434. msa311_odr_table[odr].microfract,
  435. msa311_pwr_modes[pwr_mode]);
  436. return -EINVAL;
  437. }
  438. return regmap_field_write(msa311->fields[F_ODR], odr);
  439. }
  440. /**
  441. * msa311_wait_for_next_data() - Wait next accel data available after resume
  442. * @msa311: MSA311 internal private state
  443. *
  444. * Return: 0 on success, -EINTR if msleep() was interrupted,
  445. * -ERRNO in other failures
  446. */
  447. static int msa311_wait_for_next_data(struct msa311_priv *msa311)
  448. {
  449. static const unsigned int unintr_thresh_ms = 20;
  450. struct device *dev = msa311->dev;
  451. unsigned long freq_uhz;
  452. unsigned long wait_ms;
  453. unsigned int odr;
  454. int err;
  455. err = msa311_get_odr(msa311, &odr);
  456. if (err) {
  457. dev_err(dev, "can't get actual frequency (%pe)\n",
  458. ERR_PTR(err));
  459. return err;
  460. }
  461. /*
  462. * After msa311 resuming is done, we need to wait for data
  463. * to be refreshed by accel logic.
  464. * A certain timeout is calculated based on the current ODR value.
  465. * If requested timeout isn't so long (let's assume 20ms),
  466. * we can wait for next data in uninterruptible sleep.
  467. */
  468. freq_uhz = msa311_odr_table[odr].integral * MICROHZ_PER_HZ +
  469. msa311_odr_table[odr].microfract;
  470. wait_ms = (MICROHZ_PER_HZ / freq_uhz) * MSEC_PER_SEC;
  471. if (wait_ms < unintr_thresh_ms)
  472. usleep_range(wait_ms * USEC_PER_MSEC,
  473. unintr_thresh_ms * USEC_PER_MSEC);
  474. else if (msleep_interruptible(wait_ms))
  475. return -EINTR;
  476. return 0;
  477. }
  478. /**
  479. * msa311_set_pwr_mode() - Install certain MSA311 power mode
  480. * @msa311: MSA311 internal private state
  481. * @mode: Power mode can be equal to NORMAL or SUSPEND
  482. *
  483. * This function should be called under msa311->lock.
  484. *
  485. * Return: 0 on success, -ERRNO on failure
  486. */
  487. static int msa311_set_pwr_mode(struct msa311_priv *msa311, unsigned int mode)
  488. {
  489. struct device *dev = msa311->dev;
  490. unsigned int prev_mode;
  491. int err;
  492. if (mode >= ARRAY_SIZE(msa311_pwr_modes))
  493. return -EINVAL;
  494. dev_dbg(dev, "transition to %s mode\n", msa311_pwr_modes[mode]);
  495. err = regmap_field_read(msa311->fields[F_PWR_MODE], &prev_mode);
  496. if (err)
  497. return err;
  498. err = regmap_field_write(msa311->fields[F_PWR_MODE], mode);
  499. if (err)
  500. return err;
  501. /* Wait actual data if we wake up */
  502. if (prev_mode == MSA311_PWR_MODE_SUSPEND &&
  503. mode == MSA311_PWR_MODE_NORMAL)
  504. return msa311_wait_for_next_data(msa311);
  505. return 0;
  506. }
  507. /**
  508. * msa311_get_axis() - Read MSA311 accel data for certain IIO channel axis spec
  509. * @msa311: MSA311 internal private state
  510. * @chan: IIO channel specification
  511. * @axis: Output accel axis data for requested IIO channel spec
  512. *
  513. * This function should be called under msa311->lock.
  514. *
  515. * Return: 0 on success, -EINVAL for unknown IIO channel specification,
  516. * -ERRNO in other failures
  517. */
  518. static int msa311_get_axis(struct msa311_priv *msa311,
  519. const struct iio_chan_spec * const chan,
  520. __le16 *axis)
  521. {
  522. struct device *dev = msa311->dev;
  523. unsigned int axis_reg;
  524. if (chan->scan_index < MSA311_SI_X || chan->scan_index > MSA311_SI_Z) {
  525. dev_err(dev, "invalid scan_index value [%d]\n",
  526. chan->scan_index);
  527. return -EINVAL;
  528. }
  529. /* Axes data layout has 2 byte gap for each axis starting from X axis */
  530. axis_reg = MSA311_ACC_X_REG + (chan->scan_index << 1);
  531. return regmap_bulk_read(msa311->regs, axis_reg, axis, sizeof(*axis));
  532. }
  533. static int msa311_read_raw_data(struct iio_dev *indio_dev,
  534. struct iio_chan_spec const *chan,
  535. int *val, int *val2)
  536. {
  537. struct msa311_priv *msa311 = iio_priv(indio_dev);
  538. struct device *dev = msa311->dev;
  539. __le16 axis;
  540. int err;
  541. err = pm_runtime_resume_and_get(dev);
  542. if (err)
  543. return err;
  544. err = iio_device_claim_direct_mode(indio_dev);
  545. if (err)
  546. return err;
  547. mutex_lock(&msa311->lock);
  548. err = msa311_get_axis(msa311, chan, &axis);
  549. mutex_unlock(&msa311->lock);
  550. iio_device_release_direct_mode(indio_dev);
  551. pm_runtime_mark_last_busy(dev);
  552. pm_runtime_put_autosuspend(dev);
  553. if (err) {
  554. dev_err(dev, "can't get axis %s (%pe)\n",
  555. chan->datasheet_name, ERR_PTR(err));
  556. return err;
  557. }
  558. /*
  559. * Axis data format is:
  560. * ACC_X = (ACC_X_MSB[7:0] << 4) | ACC_X_LSB[7:4]
  561. */
  562. *val = sign_extend32(le16_to_cpu(axis) >> chan->scan_type.shift,
  563. chan->scan_type.realbits - 1);
  564. return IIO_VAL_INT;
  565. }
  566. static int msa311_read_scale(struct iio_dev *indio_dev, int *val, int *val2)
  567. {
  568. struct msa311_priv *msa311 = iio_priv(indio_dev);
  569. struct device *dev = msa311->dev;
  570. unsigned int fs;
  571. int err;
  572. mutex_lock(&msa311->lock);
  573. err = regmap_field_read(msa311->fields[F_FS], &fs);
  574. mutex_unlock(&msa311->lock);
  575. if (err) {
  576. dev_err(dev, "can't get actual scale (%pe)\n", ERR_PTR(err));
  577. return err;
  578. }
  579. *val = msa311_fs_table[fs].integral;
  580. *val2 = msa311_fs_table[fs].microfract;
  581. return IIO_VAL_INT_PLUS_MICRO;
  582. }
  583. static int msa311_read_samp_freq(struct iio_dev *indio_dev,
  584. int *val, int *val2)
  585. {
  586. struct msa311_priv *msa311 = iio_priv(indio_dev);
  587. struct device *dev = msa311->dev;
  588. unsigned int odr;
  589. int err;
  590. mutex_lock(&msa311->lock);
  591. err = msa311_get_odr(msa311, &odr);
  592. mutex_unlock(&msa311->lock);
  593. if (err) {
  594. dev_err(dev, "can't get actual frequency (%pe)\n",
  595. ERR_PTR(err));
  596. return err;
  597. }
  598. *val = msa311_odr_table[odr].integral;
  599. *val2 = msa311_odr_table[odr].microfract;
  600. return IIO_VAL_INT_PLUS_MICRO;
  601. }
  602. static int msa311_read_raw(struct iio_dev *indio_dev,
  603. struct iio_chan_spec const *chan,
  604. int *val, int *val2, long mask)
  605. {
  606. switch (mask) {
  607. case IIO_CHAN_INFO_RAW:
  608. return msa311_read_raw_data(indio_dev, chan, val, val2);
  609. case IIO_CHAN_INFO_SCALE:
  610. return msa311_read_scale(indio_dev, val, val2);
  611. case IIO_CHAN_INFO_SAMP_FREQ:
  612. return msa311_read_samp_freq(indio_dev, val, val2);
  613. default:
  614. return -EINVAL;
  615. }
  616. }
  617. static int msa311_read_avail(struct iio_dev *indio_dev,
  618. struct iio_chan_spec const *chan,
  619. const int **vals, int *type,
  620. int *length, long mask)
  621. {
  622. switch (mask) {
  623. case IIO_CHAN_INFO_SAMP_FREQ:
  624. *vals = (int *)msa311_odr_table;
  625. *type = IIO_VAL_INT_PLUS_MICRO;
  626. /* ODR value has 2 ints (integer and fractional parts) */
  627. *length = ARRAY_SIZE(msa311_odr_table) * 2;
  628. return IIO_AVAIL_LIST;
  629. case IIO_CHAN_INFO_SCALE:
  630. *vals = (int *)msa311_fs_table;
  631. *type = IIO_VAL_INT_PLUS_MICRO;
  632. /* FS value has 2 ints (integer and fractional parts) */
  633. *length = ARRAY_SIZE(msa311_fs_table) * 2;
  634. return IIO_AVAIL_LIST;
  635. default:
  636. return -EINVAL;
  637. }
  638. }
  639. static int msa311_write_scale(struct iio_dev *indio_dev, int val, int val2)
  640. {
  641. struct msa311_priv *msa311 = iio_priv(indio_dev);
  642. struct device *dev = msa311->dev;
  643. unsigned int fs;
  644. int err;
  645. /* We do not have fs >= 1, so skip such values */
  646. if (val)
  647. return 0;
  648. err = pm_runtime_resume_and_get(dev);
  649. if (err)
  650. return err;
  651. err = -EINVAL;
  652. for (fs = 0; fs < ARRAY_SIZE(msa311_fs_table); fs++)
  653. /* Do not check msa311_fs_table[fs].integral, it's always 0 */
  654. if (val2 == msa311_fs_table[fs].microfract) {
  655. mutex_lock(&msa311->lock);
  656. err = regmap_field_write(msa311->fields[F_FS], fs);
  657. mutex_unlock(&msa311->lock);
  658. break;
  659. }
  660. pm_runtime_mark_last_busy(dev);
  661. pm_runtime_put_autosuspend(dev);
  662. if (err)
  663. dev_err(dev, "can't update scale (%pe)\n", ERR_PTR(err));
  664. return err;
  665. }
  666. static int msa311_write_samp_freq(struct iio_dev *indio_dev, int val, int val2)
  667. {
  668. struct msa311_priv *msa311 = iio_priv(indio_dev);
  669. struct device *dev = msa311->dev;
  670. unsigned int odr;
  671. int err;
  672. err = pm_runtime_resume_and_get(dev);
  673. if (err)
  674. return err;
  675. /*
  676. * Sampling frequency changing is prohibited when buffer mode is
  677. * enabled, because sometimes MSA311 chip returns outliers during
  678. * frequency values growing up in the read operation moment.
  679. */
  680. err = iio_device_claim_direct_mode(indio_dev);
  681. if (err)
  682. return err;
  683. err = -EINVAL;
  684. for (odr = 0; odr < ARRAY_SIZE(msa311_odr_table); odr++)
  685. if (val == msa311_odr_table[odr].integral &&
  686. val2 == msa311_odr_table[odr].microfract) {
  687. mutex_lock(&msa311->lock);
  688. err = msa311_set_odr(msa311, odr);
  689. mutex_unlock(&msa311->lock);
  690. break;
  691. }
  692. iio_device_release_direct_mode(indio_dev);
  693. pm_runtime_mark_last_busy(dev);
  694. pm_runtime_put_autosuspend(dev);
  695. if (err)
  696. dev_err(dev, "can't update frequency (%pe)\n", ERR_PTR(err));
  697. return err;
  698. }
  699. static int msa311_write_raw(struct iio_dev *indio_dev,
  700. struct iio_chan_spec const *chan,
  701. int val, int val2, long mask)
  702. {
  703. switch (mask) {
  704. case IIO_CHAN_INFO_SCALE:
  705. return msa311_write_scale(indio_dev, val, val2);
  706. case IIO_CHAN_INFO_SAMP_FREQ:
  707. return msa311_write_samp_freq(indio_dev, val, val2);
  708. default:
  709. return -EINVAL;
  710. }
  711. }
  712. static int msa311_debugfs_reg_access(struct iio_dev *indio_dev,
  713. unsigned int reg, unsigned int writeval,
  714. unsigned int *readval)
  715. {
  716. struct msa311_priv *msa311 = iio_priv(indio_dev);
  717. struct device *dev = msa311->dev;
  718. int err;
  719. if (reg > regmap_get_max_register(msa311->regs))
  720. return -EINVAL;
  721. err = pm_runtime_resume_and_get(dev);
  722. if (err)
  723. return err;
  724. mutex_lock(&msa311->lock);
  725. if (readval)
  726. err = regmap_read(msa311->regs, reg, readval);
  727. else
  728. err = regmap_write(msa311->regs, reg, writeval);
  729. mutex_unlock(&msa311->lock);
  730. pm_runtime_mark_last_busy(dev);
  731. pm_runtime_put_autosuspend(dev);
  732. if (err)
  733. dev_err(dev, "can't %s register %u from debugfs (%pe)\n",
  734. str_read_write(readval), reg, ERR_PTR(err));
  735. return err;
  736. }
  737. static int msa311_buffer_preenable(struct iio_dev *indio_dev)
  738. {
  739. struct msa311_priv *msa311 = iio_priv(indio_dev);
  740. struct device *dev = msa311->dev;
  741. return pm_runtime_resume_and_get(dev);
  742. }
  743. static int msa311_buffer_postdisable(struct iio_dev *indio_dev)
  744. {
  745. struct msa311_priv *msa311 = iio_priv(indio_dev);
  746. struct device *dev = msa311->dev;
  747. pm_runtime_mark_last_busy(dev);
  748. pm_runtime_put_autosuspend(dev);
  749. return 0;
  750. }
  751. static int msa311_set_new_data_trig_state(struct iio_trigger *trig, bool state)
  752. {
  753. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  754. struct msa311_priv *msa311 = iio_priv(indio_dev);
  755. struct device *dev = msa311->dev;
  756. int err;
  757. mutex_lock(&msa311->lock);
  758. err = regmap_field_write(msa311->fields[F_NEW_DATA_INT_EN], state);
  759. mutex_unlock(&msa311->lock);
  760. if (err)
  761. dev_err(dev,
  762. "can't %s buffer due to new_data_int failure (%pe)\n",
  763. str_enable_disable(state), ERR_PTR(err));
  764. return err;
  765. }
  766. static int msa311_validate_device(struct iio_trigger *trig,
  767. struct iio_dev *indio_dev)
  768. {
  769. return iio_trigger_get_drvdata(trig) == indio_dev ? 0 : -EINVAL;
  770. }
  771. static irqreturn_t msa311_buffer_thread(int irq, void *p)
  772. {
  773. struct iio_poll_func *pf = p;
  774. struct msa311_priv *msa311 = iio_priv(pf->indio_dev);
  775. struct iio_dev *indio_dev = pf->indio_dev;
  776. const struct iio_chan_spec *chan;
  777. struct device *dev = msa311->dev;
  778. int bit, err, i = 0;
  779. __le16 axis;
  780. struct {
  781. __le16 channels[MSA311_SI_Z + 1];
  782. s64 ts __aligned(8);
  783. } buf;
  784. memset(&buf, 0, sizeof(buf));
  785. mutex_lock(&msa311->lock);
  786. for_each_set_bit(bit, indio_dev->active_scan_mask,
  787. indio_dev->masklength) {
  788. chan = &msa311_channels[bit];
  789. err = msa311_get_axis(msa311, chan, &axis);
  790. if (err) {
  791. mutex_unlock(&msa311->lock);
  792. dev_err(dev, "can't get axis %s (%pe)\n",
  793. chan->datasheet_name, ERR_PTR(err));
  794. goto notify_done;
  795. }
  796. buf.channels[i++] = axis;
  797. }
  798. mutex_unlock(&msa311->lock);
  799. iio_push_to_buffers_with_timestamp(indio_dev, &buf,
  800. iio_get_time_ns(indio_dev));
  801. notify_done:
  802. iio_trigger_notify_done(indio_dev->trig);
  803. return IRQ_HANDLED;
  804. }
  805. static irqreturn_t msa311_irq_thread(int irq, void *p)
  806. {
  807. struct msa311_priv *msa311 = iio_priv(p);
  808. unsigned int new_data_int_enabled;
  809. struct device *dev = msa311->dev;
  810. int err;
  811. mutex_lock(&msa311->lock);
  812. /*
  813. * We do not check NEW_DATA int status, because based on the
  814. * specification it's cleared automatically after a fixed time.
  815. * So just check that is enabled by driver logic.
  816. */
  817. err = regmap_field_read(msa311->fields[F_NEW_DATA_INT_EN],
  818. &new_data_int_enabled);
  819. mutex_unlock(&msa311->lock);
  820. if (err) {
  821. dev_err(dev, "can't read new_data interrupt state (%pe)\n",
  822. ERR_PTR(err));
  823. return IRQ_NONE;
  824. }
  825. if (new_data_int_enabled)
  826. iio_trigger_poll_chained(msa311->new_data_trig);
  827. return IRQ_HANDLED;
  828. }
  829. static const struct iio_info msa311_info = {
  830. .read_raw = msa311_read_raw,
  831. .read_avail = msa311_read_avail,
  832. .write_raw = msa311_write_raw,
  833. .debugfs_reg_access = msa311_debugfs_reg_access,
  834. };
  835. static const struct iio_buffer_setup_ops msa311_buffer_setup_ops = {
  836. .preenable = msa311_buffer_preenable,
  837. .postdisable = msa311_buffer_postdisable,
  838. };
  839. static const struct iio_trigger_ops msa311_new_data_trig_ops = {
  840. .set_trigger_state = msa311_set_new_data_trig_state,
  841. .validate_device = msa311_validate_device,
  842. };
  843. static int msa311_check_partid(struct msa311_priv *msa311)
  844. {
  845. struct device *dev = msa311->dev;
  846. unsigned int partid;
  847. int err;
  848. err = regmap_read(msa311->regs, MSA311_PARTID_REG, &partid);
  849. if (err)
  850. return dev_err_probe(dev, err, "failed to read partid\n");
  851. if (partid != MSA311_WHO_AM_I)
  852. dev_warn(dev, "invalid partid (%#x), expected (%#x)\n",
  853. partid, MSA311_WHO_AM_I);
  854. msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL,
  855. "msa311-%02x", partid);
  856. if (!msa311->chip_name)
  857. return dev_err_probe(dev, -ENOMEM, "can't alloc chip name\n");
  858. return 0;
  859. }
  860. static int msa311_soft_reset(struct msa311_priv *msa311)
  861. {
  862. struct device *dev = msa311->dev;
  863. int err;
  864. err = regmap_write(msa311->regs, MSA311_SOFT_RESET_REG,
  865. MSA311_GENMASK(F_SOFT_RESET_I2C) |
  866. MSA311_GENMASK(F_SOFT_RESET_SPI));
  867. if (err)
  868. return dev_err_probe(dev, err, "can't soft reset all logic\n");
  869. return 0;
  870. }
  871. static int msa311_chip_init(struct msa311_priv *msa311)
  872. {
  873. struct device *dev = msa311->dev;
  874. const char zero_bulk[2] = { };
  875. int err;
  876. err = regmap_write(msa311->regs, MSA311_RANGE_REG, MSA311_FS_16G);
  877. if (err)
  878. return dev_err_probe(dev, err, "failed to setup accel range\n");
  879. /* Disable all interrupts by default */
  880. err = regmap_bulk_write(msa311->regs, MSA311_INT_SET_0_REG,
  881. zero_bulk, sizeof(zero_bulk));
  882. if (err)
  883. return dev_err_probe(dev, err,
  884. "can't disable set0/set1 interrupts\n");
  885. /* Unmap all INT1 interrupts by default */
  886. err = regmap_bulk_write(msa311->regs, MSA311_INT_MAP_0_REG,
  887. zero_bulk, sizeof(zero_bulk));
  888. if (err)
  889. return dev_err_probe(dev, err,
  890. "failed to unmap map0/map1 interrupts\n");
  891. /* Disable all axes by default */
  892. err = regmap_update_bits(msa311->regs, MSA311_ODR_REG,
  893. MSA311_GENMASK(F_X_AXIS_DIS) |
  894. MSA311_GENMASK(F_Y_AXIS_DIS) |
  895. MSA311_GENMASK(F_Z_AXIS_DIS), 0);
  896. if (err)
  897. return dev_err_probe(dev, err, "can't enable all axes\n");
  898. err = msa311_set_odr(msa311, MSA311_ODR_125_HZ);
  899. if (err)
  900. return dev_err_probe(dev, err,
  901. "failed to set accel frequency\n");
  902. return 0;
  903. }
  904. static int msa311_setup_interrupts(struct msa311_priv *msa311)
  905. {
  906. struct device *dev = msa311->dev;
  907. struct i2c_client *i2c = to_i2c_client(dev);
  908. struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
  909. struct iio_trigger *trig;
  910. int err;
  911. /* Keep going without interrupts if no initialized I2C IRQ */
  912. if (i2c->irq <= 0)
  913. return 0;
  914. err = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
  915. msa311_irq_thread, IRQF_ONESHOT,
  916. msa311->chip_name, indio_dev);
  917. if (err)
  918. return dev_err_probe(dev, err, "failed to request IRQ\n");
  919. trig = devm_iio_trigger_alloc(dev, "%s-new-data", msa311->chip_name);
  920. if (!trig)
  921. return dev_err_probe(dev, -ENOMEM,
  922. "can't allocate newdata trigger\n");
  923. msa311->new_data_trig = trig;
  924. msa311->new_data_trig->ops = &msa311_new_data_trig_ops;
  925. iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev);
  926. err = devm_iio_trigger_register(dev, msa311->new_data_trig);
  927. if (err)
  928. return dev_err_probe(dev, err,
  929. "can't register newdata trigger\n");
  930. err = regmap_field_write(msa311->fields[F_INT1_OD],
  931. MSA311_INT1_OD_PUSH_PULL);
  932. if (err)
  933. return dev_err_probe(dev, err,
  934. "can't enable push-pull interrupt\n");
  935. err = regmap_field_write(msa311->fields[F_INT1_LVL],
  936. MSA311_INT1_LVL_HIGH);
  937. if (err)
  938. return dev_err_probe(dev, err,
  939. "can't set active interrupt level\n");
  940. err = regmap_field_write(msa311->fields[F_LATCH_INT],
  941. MSA311_LATCH_INT_LATCHED);
  942. if (err)
  943. return dev_err_probe(dev, err,
  944. "can't latch interrupt\n");
  945. err = regmap_field_write(msa311->fields[F_RESET_INT], 1);
  946. if (err)
  947. return dev_err_probe(dev, err,
  948. "can't reset interrupt\n");
  949. err = regmap_field_write(msa311->fields[F_INT1_NEW_DATA], 1);
  950. if (err)
  951. return dev_err_probe(dev, err,
  952. "can't map new data interrupt\n");
  953. return 0;
  954. }
  955. static int msa311_regmap_init(struct msa311_priv *msa311)
  956. {
  957. struct regmap_field **fields = msa311->fields;
  958. struct device *dev = msa311->dev;
  959. struct i2c_client *i2c = to_i2c_client(dev);
  960. struct regmap *regmap;
  961. int i;
  962. regmap = devm_regmap_init_i2c(i2c, &msa311_regmap_config);
  963. if (IS_ERR(regmap))
  964. return dev_err_probe(dev, PTR_ERR(regmap),
  965. "failed to register i2c regmap\n");
  966. msa311->regs = regmap;
  967. for (i = 0; i < F_MAX_FIELDS; i++) {
  968. fields[i] = devm_regmap_field_alloc(dev,
  969. msa311->regs,
  970. msa311_reg_fields[i]);
  971. if (IS_ERR(msa311->fields[i]))
  972. return dev_err_probe(dev, PTR_ERR(msa311->fields[i]),
  973. "can't alloc field[%d]\n", i);
  974. }
  975. return 0;
  976. }
  977. static void msa311_powerdown(void *msa311)
  978. {
  979. msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
  980. }
  981. static void msa311_vdd_disable(void *vdd)
  982. {
  983. regulator_disable(vdd);
  984. }
  985. static int msa311_probe(struct i2c_client *i2c)
  986. {
  987. struct device *dev = &i2c->dev;
  988. struct msa311_priv *msa311;
  989. struct iio_dev *indio_dev;
  990. int err;
  991. indio_dev = devm_iio_device_alloc(dev, sizeof(*msa311));
  992. if (!indio_dev)
  993. return dev_err_probe(dev, -ENOMEM,
  994. "IIO device allocation failed\n");
  995. msa311 = iio_priv(indio_dev);
  996. msa311->dev = dev;
  997. i2c_set_clientdata(i2c, indio_dev);
  998. err = msa311_regmap_init(msa311);
  999. if (err)
  1000. return err;
  1001. mutex_init(&msa311->lock);
  1002. msa311->vdd = devm_regulator_get(dev, "vdd");
  1003. if (IS_ERR(msa311->vdd))
  1004. return dev_err_probe(dev, PTR_ERR(msa311->vdd),
  1005. "can't get vdd supply\n");
  1006. err = regulator_enable(msa311->vdd);
  1007. if (err)
  1008. return dev_err_probe(dev, err, "can't enable vdd supply\n");
  1009. err = devm_add_action_or_reset(dev, msa311_vdd_disable, msa311->vdd);
  1010. if (err)
  1011. return dev_err_probe(dev, err,
  1012. "can't add vdd disable action\n");
  1013. err = msa311_check_partid(msa311);
  1014. if (err)
  1015. return err;
  1016. err = msa311_soft_reset(msa311);
  1017. if (err)
  1018. return err;
  1019. err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
  1020. if (err)
  1021. return dev_err_probe(dev, err, "failed to power on device\n");
  1022. /*
  1023. * Register powerdown deferred callback which suspends the chip
  1024. * after module unloaded.
  1025. *
  1026. * MSA311 should be in SUSPEND mode in the two cases:
  1027. * 1) When driver is loaded, but we do not have any data or
  1028. * configuration requests to it (we are solving it using
  1029. * autosuspend feature).
  1030. * 2) When driver is unloaded and device is not used (devm action is
  1031. * used in this case).
  1032. */
  1033. err = devm_add_action_or_reset(dev, msa311_powerdown, msa311);
  1034. if (err)
  1035. return dev_err_probe(dev, err, "can't add powerdown action\n");
  1036. err = pm_runtime_set_active(dev);
  1037. if (err)
  1038. return err;
  1039. err = devm_pm_runtime_enable(dev);
  1040. if (err)
  1041. return err;
  1042. pm_runtime_get_noresume(dev);
  1043. pm_runtime_set_autosuspend_delay(dev, MSA311_PWR_SLEEP_DELAY_MS);
  1044. pm_runtime_use_autosuspend(dev);
  1045. err = msa311_chip_init(msa311);
  1046. if (err)
  1047. return err;
  1048. indio_dev->modes = INDIO_DIRECT_MODE;
  1049. indio_dev->channels = msa311_channels;
  1050. indio_dev->num_channels = ARRAY_SIZE(msa311_channels);
  1051. indio_dev->name = msa311->chip_name;
  1052. indio_dev->info = &msa311_info;
  1053. err = devm_iio_triggered_buffer_setup(dev, indio_dev,
  1054. iio_pollfunc_store_time,
  1055. msa311_buffer_thread,
  1056. &msa311_buffer_setup_ops);
  1057. if (err)
  1058. return dev_err_probe(dev, err,
  1059. "can't setup IIO trigger buffer\n");
  1060. err = msa311_setup_interrupts(msa311);
  1061. if (err)
  1062. return err;
  1063. pm_runtime_mark_last_busy(dev);
  1064. pm_runtime_put_autosuspend(dev);
  1065. err = devm_iio_device_register(dev, indio_dev);
  1066. if (err)
  1067. return dev_err_probe(dev, err, "IIO device register failed\n");
  1068. return 0;
  1069. }
  1070. static int msa311_runtime_suspend(struct device *dev)
  1071. {
  1072. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1073. struct msa311_priv *msa311 = iio_priv(indio_dev);
  1074. int err;
  1075. mutex_lock(&msa311->lock);
  1076. err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
  1077. mutex_unlock(&msa311->lock);
  1078. if (err)
  1079. dev_err(dev, "failed to power off device (%pe)\n",
  1080. ERR_PTR(err));
  1081. return err;
  1082. }
  1083. static int msa311_runtime_resume(struct device *dev)
  1084. {
  1085. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1086. struct msa311_priv *msa311 = iio_priv(indio_dev);
  1087. int err;
  1088. mutex_lock(&msa311->lock);
  1089. err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
  1090. mutex_unlock(&msa311->lock);
  1091. if (err)
  1092. dev_err(dev, "failed to power on device (%pe)\n",
  1093. ERR_PTR(err));
  1094. return err;
  1095. }
  1096. static DEFINE_RUNTIME_DEV_PM_OPS(msa311_pm_ops, msa311_runtime_suspend,
  1097. msa311_runtime_resume, NULL);
  1098. static const struct i2c_device_id msa311_i2c_id[] = {
  1099. { .name = "msa311" },
  1100. { }
  1101. };
  1102. MODULE_DEVICE_TABLE(i2c, msa311_i2c_id);
  1103. static const struct of_device_id msa311_of_match[] = {
  1104. { .compatible = "memsensing,msa311" },
  1105. { }
  1106. };
  1107. MODULE_DEVICE_TABLE(of, msa311_of_match);
  1108. static struct i2c_driver msa311_driver = {
  1109. .driver = {
  1110. .name = "msa311",
  1111. .of_match_table = msa311_of_match,
  1112. .pm = pm_ptr(&msa311_pm_ops),
  1113. },
  1114. .probe_new = msa311_probe,
  1115. .id_table = msa311_i2c_id,
  1116. };
  1117. module_i2c_driver(msa311_driver);
  1118. MODULE_AUTHOR("Dmitry Rokosov <[email protected]>");
  1119. MODULE_DESCRIPTION("MEMSensing MSA311 3-axis accelerometer driver");
  1120. MODULE_LICENSE("GPL");