st_magn_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * STMicroelectronics magnetometers driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <denis.ciocca@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/errno.h>
  14. #include <linux/types.h>
  15. #include <linux/mutex.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/gpio.h>
  19. #include <linux/irq.h>
  20. #include <linux/delay.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/common/st_sensors.h>
  25. #include "st_magn.h"
  26. #define ST_MAGN_NUMBER_DATA_CHANNELS 3
  27. /* DEFAULT VALUE FOR SENSORS */
  28. #define ST_MAGN_DEFAULT_OUT_X_H_ADDR 0X03
  29. #define ST_MAGN_DEFAULT_OUT_Y_H_ADDR 0X07
  30. #define ST_MAGN_DEFAULT_OUT_Z_H_ADDR 0X05
  31. /* FULLSCALE */
  32. #define ST_MAGN_FS_AVL_1300MG 1300
  33. #define ST_MAGN_FS_AVL_1900MG 1900
  34. #define ST_MAGN_FS_AVL_2500MG 2500
  35. #define ST_MAGN_FS_AVL_4000MG 4000
  36. #define ST_MAGN_FS_AVL_4700MG 4700
  37. #define ST_MAGN_FS_AVL_5600MG 5600
  38. #define ST_MAGN_FS_AVL_8000MG 8000
  39. #define ST_MAGN_FS_AVL_8100MG 8100
  40. #define ST_MAGN_FS_AVL_12000MG 12000
  41. #define ST_MAGN_FS_AVL_16000MG 16000
  42. /* CUSTOM VALUES FOR SENSOR 0 */
  43. #define ST_MAGN_0_ODR_ADDR 0x00
  44. #define ST_MAGN_0_ODR_MASK 0x1c
  45. #define ST_MAGN_0_ODR_AVL_1HZ_VAL 0x00
  46. #define ST_MAGN_0_ODR_AVL_2HZ_VAL 0x01
  47. #define ST_MAGN_0_ODR_AVL_3HZ_VAL 0x02
  48. #define ST_MAGN_0_ODR_AVL_8HZ_VAL 0x03
  49. #define ST_MAGN_0_ODR_AVL_15HZ_VAL 0x04
  50. #define ST_MAGN_0_ODR_AVL_30HZ_VAL 0x05
  51. #define ST_MAGN_0_ODR_AVL_75HZ_VAL 0x06
  52. #define ST_MAGN_0_ODR_AVL_220HZ_VAL 0x07
  53. #define ST_MAGN_0_PW_ADDR 0x02
  54. #define ST_MAGN_0_PW_MASK 0x03
  55. #define ST_MAGN_0_PW_ON 0x00
  56. #define ST_MAGN_0_PW_OFF 0x03
  57. #define ST_MAGN_0_FS_ADDR 0x01
  58. #define ST_MAGN_0_FS_MASK 0xe0
  59. #define ST_MAGN_0_FS_AVL_1300_VAL 0x01
  60. #define ST_MAGN_0_FS_AVL_1900_VAL 0x02
  61. #define ST_MAGN_0_FS_AVL_2500_VAL 0x03
  62. #define ST_MAGN_0_FS_AVL_4000_VAL 0x04
  63. #define ST_MAGN_0_FS_AVL_4700_VAL 0x05
  64. #define ST_MAGN_0_FS_AVL_5600_VAL 0x06
  65. #define ST_MAGN_0_FS_AVL_8100_VAL 0x07
  66. #define ST_MAGN_0_FS_AVL_1300_GAIN_XY 1100
  67. #define ST_MAGN_0_FS_AVL_1900_GAIN_XY 855
  68. #define ST_MAGN_0_FS_AVL_2500_GAIN_XY 670
  69. #define ST_MAGN_0_FS_AVL_4000_GAIN_XY 450
  70. #define ST_MAGN_0_FS_AVL_4700_GAIN_XY 400
  71. #define ST_MAGN_0_FS_AVL_5600_GAIN_XY 330
  72. #define ST_MAGN_0_FS_AVL_8100_GAIN_XY 230
  73. #define ST_MAGN_0_FS_AVL_1300_GAIN_Z 980
  74. #define ST_MAGN_0_FS_AVL_1900_GAIN_Z 760
  75. #define ST_MAGN_0_FS_AVL_2500_GAIN_Z 600
  76. #define ST_MAGN_0_FS_AVL_4000_GAIN_Z 400
  77. #define ST_MAGN_0_FS_AVL_4700_GAIN_Z 355
  78. #define ST_MAGN_0_FS_AVL_5600_GAIN_Z 295
  79. #define ST_MAGN_0_FS_AVL_8100_GAIN_Z 205
  80. #define ST_MAGN_0_MULTIREAD_BIT false
  81. /* CUSTOM VALUES FOR SENSOR 1 */
  82. #define ST_MAGN_1_WAI_EXP 0x3c
  83. #define ST_MAGN_1_ODR_ADDR 0x00
  84. #define ST_MAGN_1_ODR_MASK 0x1c
  85. #define ST_MAGN_1_ODR_AVL_1HZ_VAL 0x00
  86. #define ST_MAGN_1_ODR_AVL_2HZ_VAL 0x01
  87. #define ST_MAGN_1_ODR_AVL_3HZ_VAL 0x02
  88. #define ST_MAGN_1_ODR_AVL_8HZ_VAL 0x03
  89. #define ST_MAGN_1_ODR_AVL_15HZ_VAL 0x04
  90. #define ST_MAGN_1_ODR_AVL_30HZ_VAL 0x05
  91. #define ST_MAGN_1_ODR_AVL_75HZ_VAL 0x06
  92. #define ST_MAGN_1_ODR_AVL_220HZ_VAL 0x07
  93. #define ST_MAGN_1_PW_ADDR 0x02
  94. #define ST_MAGN_1_PW_MASK 0x03
  95. #define ST_MAGN_1_PW_ON 0x00
  96. #define ST_MAGN_1_PW_OFF 0x03
  97. #define ST_MAGN_1_FS_ADDR 0x01
  98. #define ST_MAGN_1_FS_MASK 0xe0
  99. #define ST_MAGN_1_FS_AVL_1300_VAL 0x01
  100. #define ST_MAGN_1_FS_AVL_1900_VAL 0x02
  101. #define ST_MAGN_1_FS_AVL_2500_VAL 0x03
  102. #define ST_MAGN_1_FS_AVL_4000_VAL 0x04
  103. #define ST_MAGN_1_FS_AVL_4700_VAL 0x05
  104. #define ST_MAGN_1_FS_AVL_5600_VAL 0x06
  105. #define ST_MAGN_1_FS_AVL_8100_VAL 0x07
  106. #define ST_MAGN_1_FS_AVL_1300_GAIN_XY 909
  107. #define ST_MAGN_1_FS_AVL_1900_GAIN_XY 1169
  108. #define ST_MAGN_1_FS_AVL_2500_GAIN_XY 1492
  109. #define ST_MAGN_1_FS_AVL_4000_GAIN_XY 2222
  110. #define ST_MAGN_1_FS_AVL_4700_GAIN_XY 2500
  111. #define ST_MAGN_1_FS_AVL_5600_GAIN_XY 3030
  112. #define ST_MAGN_1_FS_AVL_8100_GAIN_XY 4347
  113. #define ST_MAGN_1_FS_AVL_1300_GAIN_Z 1020
  114. #define ST_MAGN_1_FS_AVL_1900_GAIN_Z 1315
  115. #define ST_MAGN_1_FS_AVL_2500_GAIN_Z 1666
  116. #define ST_MAGN_1_FS_AVL_4000_GAIN_Z 2500
  117. #define ST_MAGN_1_FS_AVL_4700_GAIN_Z 2816
  118. #define ST_MAGN_1_FS_AVL_5600_GAIN_Z 3389
  119. #define ST_MAGN_1_FS_AVL_8100_GAIN_Z 4878
  120. #define ST_MAGN_1_MULTIREAD_BIT false
  121. /* CUSTOM VALUES FOR SENSOR 2 */
  122. #define ST_MAGN_2_WAI_EXP 0x3d
  123. #define ST_MAGN_2_ODR_ADDR 0x20
  124. #define ST_MAGN_2_ODR_MASK 0x1c
  125. #define ST_MAGN_2_ODR_AVL_1HZ_VAL 0x00
  126. #define ST_MAGN_2_ODR_AVL_2HZ_VAL 0x01
  127. #define ST_MAGN_2_ODR_AVL_3HZ_VAL 0x02
  128. #define ST_MAGN_2_ODR_AVL_5HZ_VAL 0x03
  129. #define ST_MAGN_2_ODR_AVL_10HZ_VAL 0x04
  130. #define ST_MAGN_2_ODR_AVL_20HZ_VAL 0x05
  131. #define ST_MAGN_2_ODR_AVL_40HZ_VAL 0x06
  132. #define ST_MAGN_2_ODR_AVL_80HZ_VAL 0x07
  133. #define ST_MAGN_2_PW_ADDR 0x22
  134. #define ST_MAGN_2_PW_MASK 0x03
  135. #define ST_MAGN_2_PW_ON 0x00
  136. #define ST_MAGN_2_PW_OFF 0x03
  137. #define ST_MAGN_2_FS_ADDR 0x21
  138. #define ST_MAGN_2_FS_MASK 0x60
  139. #define ST_MAGN_2_FS_AVL_4000_VAL 0x00
  140. #define ST_MAGN_2_FS_AVL_8000_VAL 0x01
  141. #define ST_MAGN_2_FS_AVL_12000_VAL 0x02
  142. #define ST_MAGN_2_FS_AVL_16000_VAL 0x03
  143. #define ST_MAGN_2_FS_AVL_4000_GAIN 146
  144. #define ST_MAGN_2_FS_AVL_8000_GAIN 292
  145. #define ST_MAGN_2_FS_AVL_12000_GAIN 438
  146. #define ST_MAGN_2_FS_AVL_16000_GAIN 584
  147. #define ST_MAGN_2_MULTIREAD_BIT false
  148. #define ST_MAGN_2_OUT_X_L_ADDR 0x28
  149. #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
  150. #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
  151. static const struct iio_chan_spec st_magn_16bit_channels[] = {
  152. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  153. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  154. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_BE, 16, 16,
  155. ST_MAGN_DEFAULT_OUT_X_H_ADDR),
  156. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  157. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  158. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_BE, 16, 16,
  159. ST_MAGN_DEFAULT_OUT_Y_H_ADDR),
  160. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  161. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  162. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_BE, 16, 16,
  163. ST_MAGN_DEFAULT_OUT_Z_H_ADDR),
  164. IIO_CHAN_SOFT_TIMESTAMP(3)
  165. };
  166. static const struct iio_chan_spec st_magn_2_16bit_channels[] = {
  167. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  168. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  169. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
  170. ST_MAGN_2_OUT_X_L_ADDR),
  171. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  172. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  173. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
  174. ST_MAGN_2_OUT_Y_L_ADDR),
  175. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  176. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  177. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
  178. ST_MAGN_2_OUT_Z_L_ADDR),
  179. IIO_CHAN_SOFT_TIMESTAMP(3)
  180. };
  181. static const struct st_sensor_settings st_magn_sensors_settings[] = {
  182. {
  183. .wai = 0, /* This sensor has no valid WhoAmI report 0 */
  184. .sensors_supported = {
  185. [0] = LSM303DLH_MAGN_DEV_NAME,
  186. },
  187. .ch = (struct iio_chan_spec *)st_magn_16bit_channels,
  188. .odr = {
  189. .addr = ST_MAGN_0_ODR_ADDR,
  190. .mask = ST_MAGN_0_ODR_MASK,
  191. .odr_avl = {
  192. { 1, ST_MAGN_0_ODR_AVL_1HZ_VAL, },
  193. { 2, ST_MAGN_0_ODR_AVL_2HZ_VAL, },
  194. { 3, ST_MAGN_0_ODR_AVL_3HZ_VAL, },
  195. { 8, ST_MAGN_0_ODR_AVL_8HZ_VAL, },
  196. { 15, ST_MAGN_0_ODR_AVL_15HZ_VAL, },
  197. { 30, ST_MAGN_0_ODR_AVL_30HZ_VAL, },
  198. { 75, ST_MAGN_0_ODR_AVL_75HZ_VAL, },
  199. },
  200. },
  201. .pw = {
  202. .addr = ST_MAGN_0_PW_ADDR,
  203. .mask = ST_MAGN_0_PW_MASK,
  204. .value_on = ST_MAGN_0_PW_ON,
  205. .value_off = ST_MAGN_0_PW_OFF,
  206. },
  207. .fs = {
  208. .addr = ST_MAGN_0_FS_ADDR,
  209. .mask = ST_MAGN_0_FS_MASK,
  210. .fs_avl = {
  211. [0] = {
  212. .num = ST_MAGN_FS_AVL_1300MG,
  213. .value = ST_MAGN_0_FS_AVL_1300_VAL,
  214. .gain = ST_MAGN_0_FS_AVL_1300_GAIN_XY,
  215. .gain2 = ST_MAGN_0_FS_AVL_1300_GAIN_Z,
  216. },
  217. [1] = {
  218. .num = ST_MAGN_FS_AVL_1900MG,
  219. .value = ST_MAGN_0_FS_AVL_1900_VAL,
  220. .gain = ST_MAGN_0_FS_AVL_1900_GAIN_XY,
  221. .gain2 = ST_MAGN_0_FS_AVL_1900_GAIN_Z,
  222. },
  223. [2] = {
  224. .num = ST_MAGN_FS_AVL_2500MG,
  225. .value = ST_MAGN_0_FS_AVL_2500_VAL,
  226. .gain = ST_MAGN_0_FS_AVL_2500_GAIN_XY,
  227. .gain2 = ST_MAGN_0_FS_AVL_2500_GAIN_Z,
  228. },
  229. [3] = {
  230. .num = ST_MAGN_FS_AVL_4000MG,
  231. .value = ST_MAGN_0_FS_AVL_4000_VAL,
  232. .gain = ST_MAGN_0_FS_AVL_4000_GAIN_XY,
  233. .gain2 = ST_MAGN_0_FS_AVL_4000_GAIN_Z,
  234. },
  235. [4] = {
  236. .num = ST_MAGN_FS_AVL_4700MG,
  237. .value = ST_MAGN_0_FS_AVL_4700_VAL,
  238. .gain = ST_MAGN_0_FS_AVL_4700_GAIN_XY,
  239. .gain2 = ST_MAGN_0_FS_AVL_4700_GAIN_Z,
  240. },
  241. [5] = {
  242. .num = ST_MAGN_FS_AVL_5600MG,
  243. .value = ST_MAGN_0_FS_AVL_5600_VAL,
  244. .gain = ST_MAGN_0_FS_AVL_5600_GAIN_XY,
  245. .gain2 = ST_MAGN_0_FS_AVL_5600_GAIN_Z,
  246. },
  247. [6] = {
  248. .num = ST_MAGN_FS_AVL_8100MG,
  249. .value = ST_MAGN_0_FS_AVL_8100_VAL,
  250. .gain = ST_MAGN_0_FS_AVL_8100_GAIN_XY,
  251. .gain2 = ST_MAGN_0_FS_AVL_8100_GAIN_Z,
  252. },
  253. },
  254. },
  255. .multi_read_bit = ST_MAGN_0_MULTIREAD_BIT,
  256. .bootime = 2,
  257. },
  258. {
  259. .wai = ST_MAGN_1_WAI_EXP,
  260. .sensors_supported = {
  261. [0] = LSM303DLHC_MAGN_DEV_NAME,
  262. [1] = LSM303DLM_MAGN_DEV_NAME,
  263. },
  264. .ch = (struct iio_chan_spec *)st_magn_16bit_channels,
  265. .odr = {
  266. .addr = ST_MAGN_1_ODR_ADDR,
  267. .mask = ST_MAGN_1_ODR_MASK,
  268. .odr_avl = {
  269. { 1, ST_MAGN_1_ODR_AVL_1HZ_VAL, },
  270. { 2, ST_MAGN_1_ODR_AVL_2HZ_VAL, },
  271. { 3, ST_MAGN_1_ODR_AVL_3HZ_VAL, },
  272. { 8, ST_MAGN_1_ODR_AVL_8HZ_VAL, },
  273. { 15, ST_MAGN_1_ODR_AVL_15HZ_VAL, },
  274. { 30, ST_MAGN_1_ODR_AVL_30HZ_VAL, },
  275. { 75, ST_MAGN_1_ODR_AVL_75HZ_VAL, },
  276. { 220, ST_MAGN_1_ODR_AVL_220HZ_VAL, },
  277. },
  278. },
  279. .pw = {
  280. .addr = ST_MAGN_1_PW_ADDR,
  281. .mask = ST_MAGN_1_PW_MASK,
  282. .value_on = ST_MAGN_1_PW_ON,
  283. .value_off = ST_MAGN_1_PW_OFF,
  284. },
  285. .fs = {
  286. .addr = ST_MAGN_1_FS_ADDR,
  287. .mask = ST_MAGN_1_FS_MASK,
  288. .fs_avl = {
  289. [0] = {
  290. .num = ST_MAGN_FS_AVL_1300MG,
  291. .value = ST_MAGN_1_FS_AVL_1300_VAL,
  292. .gain = ST_MAGN_1_FS_AVL_1300_GAIN_XY,
  293. .gain2 = ST_MAGN_1_FS_AVL_1300_GAIN_Z,
  294. },
  295. [1] = {
  296. .num = ST_MAGN_FS_AVL_1900MG,
  297. .value = ST_MAGN_1_FS_AVL_1900_VAL,
  298. .gain = ST_MAGN_1_FS_AVL_1900_GAIN_XY,
  299. .gain2 = ST_MAGN_1_FS_AVL_1900_GAIN_Z,
  300. },
  301. [2] = {
  302. .num = ST_MAGN_FS_AVL_2500MG,
  303. .value = ST_MAGN_1_FS_AVL_2500_VAL,
  304. .gain = ST_MAGN_1_FS_AVL_2500_GAIN_XY,
  305. .gain2 = ST_MAGN_1_FS_AVL_2500_GAIN_Z,
  306. },
  307. [3] = {
  308. .num = ST_MAGN_FS_AVL_4000MG,
  309. .value = ST_MAGN_1_FS_AVL_4000_VAL,
  310. .gain = ST_MAGN_1_FS_AVL_4000_GAIN_XY,
  311. .gain2 = ST_MAGN_1_FS_AVL_4000_GAIN_Z,
  312. },
  313. [4] = {
  314. .num = ST_MAGN_FS_AVL_4700MG,
  315. .value = ST_MAGN_1_FS_AVL_4700_VAL,
  316. .gain = ST_MAGN_1_FS_AVL_4700_GAIN_XY,
  317. .gain2 = ST_MAGN_1_FS_AVL_4700_GAIN_Z,
  318. },
  319. [5] = {
  320. .num = ST_MAGN_FS_AVL_5600MG,
  321. .value = ST_MAGN_1_FS_AVL_5600_VAL,
  322. .gain = ST_MAGN_1_FS_AVL_5600_GAIN_XY,
  323. .gain2 = ST_MAGN_1_FS_AVL_5600_GAIN_Z,
  324. },
  325. [6] = {
  326. .num = ST_MAGN_FS_AVL_8100MG,
  327. .value = ST_MAGN_1_FS_AVL_8100_VAL,
  328. .gain = ST_MAGN_1_FS_AVL_8100_GAIN_XY,
  329. .gain2 = ST_MAGN_1_FS_AVL_8100_GAIN_Z,
  330. },
  331. },
  332. },
  333. .multi_read_bit = ST_MAGN_1_MULTIREAD_BIT,
  334. .bootime = 2,
  335. },
  336. {
  337. .wai = ST_MAGN_2_WAI_EXP,
  338. .sensors_supported = {
  339. [0] = LIS3MDL_MAGN_DEV_NAME,
  340. },
  341. .ch = (struct iio_chan_spec *)st_magn_2_16bit_channels,
  342. .odr = {
  343. .addr = ST_MAGN_2_ODR_ADDR,
  344. .mask = ST_MAGN_2_ODR_MASK,
  345. .odr_avl = {
  346. { 1, ST_MAGN_2_ODR_AVL_1HZ_VAL, },
  347. { 2, ST_MAGN_2_ODR_AVL_2HZ_VAL, },
  348. { 3, ST_MAGN_2_ODR_AVL_3HZ_VAL, },
  349. { 5, ST_MAGN_2_ODR_AVL_5HZ_VAL, },
  350. { 10, ST_MAGN_2_ODR_AVL_10HZ_VAL, },
  351. { 20, ST_MAGN_2_ODR_AVL_20HZ_VAL, },
  352. { 40, ST_MAGN_2_ODR_AVL_40HZ_VAL, },
  353. { 80, ST_MAGN_2_ODR_AVL_80HZ_VAL, },
  354. },
  355. },
  356. .pw = {
  357. .addr = ST_MAGN_2_PW_ADDR,
  358. .mask = ST_MAGN_2_PW_MASK,
  359. .value_on = ST_MAGN_2_PW_ON,
  360. .value_off = ST_MAGN_2_PW_OFF,
  361. },
  362. .fs = {
  363. .addr = ST_MAGN_2_FS_ADDR,
  364. .mask = ST_MAGN_2_FS_MASK,
  365. .fs_avl = {
  366. [0] = {
  367. .num = ST_MAGN_FS_AVL_4000MG,
  368. .value = ST_MAGN_2_FS_AVL_4000_VAL,
  369. .gain = ST_MAGN_2_FS_AVL_4000_GAIN,
  370. },
  371. [1] = {
  372. .num = ST_MAGN_FS_AVL_8000MG,
  373. .value = ST_MAGN_2_FS_AVL_8000_VAL,
  374. .gain = ST_MAGN_2_FS_AVL_8000_GAIN,
  375. },
  376. [2] = {
  377. .num = ST_MAGN_FS_AVL_12000MG,
  378. .value = ST_MAGN_2_FS_AVL_12000_VAL,
  379. .gain = ST_MAGN_2_FS_AVL_12000_GAIN,
  380. },
  381. [3] = {
  382. .num = ST_MAGN_FS_AVL_16000MG,
  383. .value = ST_MAGN_2_FS_AVL_16000_VAL,
  384. .gain = ST_MAGN_2_FS_AVL_16000_GAIN,
  385. },
  386. },
  387. },
  388. .multi_read_bit = ST_MAGN_2_MULTIREAD_BIT,
  389. .bootime = 2,
  390. },
  391. };
  392. static int st_magn_read_raw(struct iio_dev *indio_dev,
  393. struct iio_chan_spec const *ch, int *val,
  394. int *val2, long mask)
  395. {
  396. int err;
  397. struct st_sensor_data *mdata = iio_priv(indio_dev);
  398. switch (mask) {
  399. case IIO_CHAN_INFO_RAW:
  400. err = st_sensors_read_info_raw(indio_dev, ch, val);
  401. if (err < 0)
  402. goto read_error;
  403. return IIO_VAL_INT;
  404. case IIO_CHAN_INFO_SCALE:
  405. *val = 0;
  406. if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
  407. (mdata->current_fullscale->gain2 != 0))
  408. *val2 = mdata->current_fullscale->gain2;
  409. else
  410. *val2 = mdata->current_fullscale->gain;
  411. return IIO_VAL_INT_PLUS_MICRO;
  412. case IIO_CHAN_INFO_SAMP_FREQ:
  413. *val = mdata->odr;
  414. return IIO_VAL_INT;
  415. default:
  416. return -EINVAL;
  417. }
  418. read_error:
  419. return err;
  420. }
  421. static int st_magn_write_raw(struct iio_dev *indio_dev,
  422. struct iio_chan_spec const *chan, int val, int val2, long mask)
  423. {
  424. int err;
  425. switch (mask) {
  426. case IIO_CHAN_INFO_SCALE:
  427. err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
  428. break;
  429. case IIO_CHAN_INFO_SAMP_FREQ:
  430. if (val2)
  431. return -EINVAL;
  432. mutex_lock(&indio_dev->mlock);
  433. err = st_sensors_set_odr(indio_dev, val);
  434. mutex_unlock(&indio_dev->mlock);
  435. return err;
  436. default:
  437. err = -EINVAL;
  438. }
  439. return err;
  440. }
  441. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  442. static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
  443. static struct attribute *st_magn_attributes[] = {
  444. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  445. &iio_dev_attr_in_magn_scale_available.dev_attr.attr,
  446. NULL,
  447. };
  448. static const struct attribute_group st_magn_attribute_group = {
  449. .attrs = st_magn_attributes,
  450. };
  451. static const struct iio_info magn_info = {
  452. .driver_module = THIS_MODULE,
  453. .attrs = &st_magn_attribute_group,
  454. .read_raw = &st_magn_read_raw,
  455. .write_raw = &st_magn_write_raw,
  456. };
  457. int st_magn_common_probe(struct iio_dev *indio_dev)
  458. {
  459. struct st_sensor_data *mdata = iio_priv(indio_dev);
  460. int irq = mdata->get_irq_data_ready(indio_dev);
  461. int err;
  462. indio_dev->modes = INDIO_DIRECT_MODE;
  463. indio_dev->info = &magn_info;
  464. mutex_init(&mdata->tb.buf_lock);
  465. st_sensors_power_enable(indio_dev);
  466. err = st_sensors_check_device_support(indio_dev,
  467. ARRAY_SIZE(st_magn_sensors_settings),
  468. st_magn_sensors_settings);
  469. if (err < 0)
  470. return err;
  471. mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
  472. mdata->multiread_bit = mdata->sensor_settings->multi_read_bit;
  473. indio_dev->channels = mdata->sensor_settings->ch;
  474. indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  475. mdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  476. &mdata->sensor_settings->fs.fs_avl[0];
  477. mdata->odr = mdata->sensor_settings->odr.odr_avl[0].hz;
  478. err = st_sensors_init_sensor(indio_dev, NULL);
  479. if (err < 0)
  480. return err;
  481. err = st_magn_allocate_ring(indio_dev);
  482. if (err < 0)
  483. return err;
  484. if (irq > 0) {
  485. err = st_sensors_allocate_trigger(indio_dev, NULL);
  486. if (err < 0)
  487. goto st_magn_probe_trigger_error;
  488. }
  489. err = iio_device_register(indio_dev);
  490. if (err)
  491. goto st_magn_device_register_error;
  492. dev_info(&indio_dev->dev, "registered magnetometer %s\n",
  493. indio_dev->name);
  494. return 0;
  495. st_magn_device_register_error:
  496. if (irq > 0)
  497. st_sensors_deallocate_trigger(indio_dev);
  498. st_magn_probe_trigger_error:
  499. st_magn_deallocate_ring(indio_dev);
  500. return err;
  501. }
  502. EXPORT_SYMBOL(st_magn_common_probe);
  503. void st_magn_common_remove(struct iio_dev *indio_dev)
  504. {
  505. struct st_sensor_data *mdata = iio_priv(indio_dev);
  506. st_sensors_power_disable(indio_dev);
  507. iio_device_unregister(indio_dev);
  508. if (mdata->get_irq_data_ready(indio_dev) > 0)
  509. st_sensors_deallocate_trigger(indio_dev);
  510. st_magn_deallocate_ring(indio_dev);
  511. }
  512. EXPORT_SYMBOL(st_magn_common_remove);
  513. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  514. MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
  515. MODULE_LICENSE("GPL v2");