st_magn_core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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_15000MG 15000
  42. #define ST_MAGN_FS_AVL_16000MG 16000
  43. /* CUSTOM VALUES FOR SENSOR 0 */
  44. #define ST_MAGN_0_ODR_ADDR 0x00
  45. #define ST_MAGN_0_ODR_MASK 0x1c
  46. #define ST_MAGN_0_ODR_AVL_1HZ_VAL 0x00
  47. #define ST_MAGN_0_ODR_AVL_2HZ_VAL 0x01
  48. #define ST_MAGN_0_ODR_AVL_3HZ_VAL 0x02
  49. #define ST_MAGN_0_ODR_AVL_8HZ_VAL 0x03
  50. #define ST_MAGN_0_ODR_AVL_15HZ_VAL 0x04
  51. #define ST_MAGN_0_ODR_AVL_30HZ_VAL 0x05
  52. #define ST_MAGN_0_ODR_AVL_75HZ_VAL 0x06
  53. #define ST_MAGN_0_ODR_AVL_220HZ_VAL 0x07
  54. #define ST_MAGN_0_PW_ADDR 0x02
  55. #define ST_MAGN_0_PW_MASK 0x03
  56. #define ST_MAGN_0_PW_ON 0x00
  57. #define ST_MAGN_0_PW_OFF 0x03
  58. #define ST_MAGN_0_FS_ADDR 0x01
  59. #define ST_MAGN_0_FS_MASK 0xe0
  60. #define ST_MAGN_0_FS_AVL_1300_VAL 0x01
  61. #define ST_MAGN_0_FS_AVL_1900_VAL 0x02
  62. #define ST_MAGN_0_FS_AVL_2500_VAL 0x03
  63. #define ST_MAGN_0_FS_AVL_4000_VAL 0x04
  64. #define ST_MAGN_0_FS_AVL_4700_VAL 0x05
  65. #define ST_MAGN_0_FS_AVL_5600_VAL 0x06
  66. #define ST_MAGN_0_FS_AVL_8100_VAL 0x07
  67. #define ST_MAGN_0_FS_AVL_1300_GAIN_XY 1100
  68. #define ST_MAGN_0_FS_AVL_1900_GAIN_XY 855
  69. #define ST_MAGN_0_FS_AVL_2500_GAIN_XY 670
  70. #define ST_MAGN_0_FS_AVL_4000_GAIN_XY 450
  71. #define ST_MAGN_0_FS_AVL_4700_GAIN_XY 400
  72. #define ST_MAGN_0_FS_AVL_5600_GAIN_XY 330
  73. #define ST_MAGN_0_FS_AVL_8100_GAIN_XY 230
  74. #define ST_MAGN_0_FS_AVL_1300_GAIN_Z 980
  75. #define ST_MAGN_0_FS_AVL_1900_GAIN_Z 760
  76. #define ST_MAGN_0_FS_AVL_2500_GAIN_Z 600
  77. #define ST_MAGN_0_FS_AVL_4000_GAIN_Z 400
  78. #define ST_MAGN_0_FS_AVL_4700_GAIN_Z 355
  79. #define ST_MAGN_0_FS_AVL_5600_GAIN_Z 295
  80. #define ST_MAGN_0_FS_AVL_8100_GAIN_Z 205
  81. #define ST_MAGN_0_MULTIREAD_BIT false
  82. /* CUSTOM VALUES FOR SENSOR 1 */
  83. #define ST_MAGN_1_WAI_EXP 0x3c
  84. #define ST_MAGN_1_ODR_ADDR 0x00
  85. #define ST_MAGN_1_ODR_MASK 0x1c
  86. #define ST_MAGN_1_ODR_AVL_1HZ_VAL 0x00
  87. #define ST_MAGN_1_ODR_AVL_2HZ_VAL 0x01
  88. #define ST_MAGN_1_ODR_AVL_3HZ_VAL 0x02
  89. #define ST_MAGN_1_ODR_AVL_8HZ_VAL 0x03
  90. #define ST_MAGN_1_ODR_AVL_15HZ_VAL 0x04
  91. #define ST_MAGN_1_ODR_AVL_30HZ_VAL 0x05
  92. #define ST_MAGN_1_ODR_AVL_75HZ_VAL 0x06
  93. #define ST_MAGN_1_ODR_AVL_220HZ_VAL 0x07
  94. #define ST_MAGN_1_PW_ADDR 0x02
  95. #define ST_MAGN_1_PW_MASK 0x03
  96. #define ST_MAGN_1_PW_ON 0x00
  97. #define ST_MAGN_1_PW_OFF 0x03
  98. #define ST_MAGN_1_FS_ADDR 0x01
  99. #define ST_MAGN_1_FS_MASK 0xe0
  100. #define ST_MAGN_1_FS_AVL_1300_VAL 0x01
  101. #define ST_MAGN_1_FS_AVL_1900_VAL 0x02
  102. #define ST_MAGN_1_FS_AVL_2500_VAL 0x03
  103. #define ST_MAGN_1_FS_AVL_4000_VAL 0x04
  104. #define ST_MAGN_1_FS_AVL_4700_VAL 0x05
  105. #define ST_MAGN_1_FS_AVL_5600_VAL 0x06
  106. #define ST_MAGN_1_FS_AVL_8100_VAL 0x07
  107. #define ST_MAGN_1_FS_AVL_1300_GAIN_XY 909
  108. #define ST_MAGN_1_FS_AVL_1900_GAIN_XY 1169
  109. #define ST_MAGN_1_FS_AVL_2500_GAIN_XY 1492
  110. #define ST_MAGN_1_FS_AVL_4000_GAIN_XY 2222
  111. #define ST_MAGN_1_FS_AVL_4700_GAIN_XY 2500
  112. #define ST_MAGN_1_FS_AVL_5600_GAIN_XY 3030
  113. #define ST_MAGN_1_FS_AVL_8100_GAIN_XY 4347
  114. #define ST_MAGN_1_FS_AVL_1300_GAIN_Z 1020
  115. #define ST_MAGN_1_FS_AVL_1900_GAIN_Z 1315
  116. #define ST_MAGN_1_FS_AVL_2500_GAIN_Z 1666
  117. #define ST_MAGN_1_FS_AVL_4000_GAIN_Z 2500
  118. #define ST_MAGN_1_FS_AVL_4700_GAIN_Z 2816
  119. #define ST_MAGN_1_FS_AVL_5600_GAIN_Z 3389
  120. #define ST_MAGN_1_FS_AVL_8100_GAIN_Z 4878
  121. #define ST_MAGN_1_MULTIREAD_BIT false
  122. /* CUSTOM VALUES FOR SENSOR 2 */
  123. #define ST_MAGN_2_WAI_EXP 0x3d
  124. #define ST_MAGN_2_ODR_ADDR 0x20
  125. #define ST_MAGN_2_ODR_MASK 0x1c
  126. #define ST_MAGN_2_ODR_AVL_1HZ_VAL 0x00
  127. #define ST_MAGN_2_ODR_AVL_2HZ_VAL 0x01
  128. #define ST_MAGN_2_ODR_AVL_3HZ_VAL 0x02
  129. #define ST_MAGN_2_ODR_AVL_5HZ_VAL 0x03
  130. #define ST_MAGN_2_ODR_AVL_10HZ_VAL 0x04
  131. #define ST_MAGN_2_ODR_AVL_20HZ_VAL 0x05
  132. #define ST_MAGN_2_ODR_AVL_40HZ_VAL 0x06
  133. #define ST_MAGN_2_ODR_AVL_80HZ_VAL 0x07
  134. #define ST_MAGN_2_PW_ADDR 0x22
  135. #define ST_MAGN_2_PW_MASK 0x03
  136. #define ST_MAGN_2_PW_ON 0x00
  137. #define ST_MAGN_2_PW_OFF 0x03
  138. #define ST_MAGN_2_FS_ADDR 0x21
  139. #define ST_MAGN_2_FS_MASK 0x60
  140. #define ST_MAGN_2_FS_AVL_4000_VAL 0x00
  141. #define ST_MAGN_2_FS_AVL_8000_VAL 0x01
  142. #define ST_MAGN_2_FS_AVL_12000_VAL 0x02
  143. #define ST_MAGN_2_FS_AVL_16000_VAL 0x03
  144. #define ST_MAGN_2_FS_AVL_4000_GAIN 146
  145. #define ST_MAGN_2_FS_AVL_8000_GAIN 292
  146. #define ST_MAGN_2_FS_AVL_12000_GAIN 438
  147. #define ST_MAGN_2_FS_AVL_16000_GAIN 584
  148. #define ST_MAGN_2_MULTIREAD_BIT false
  149. #define ST_MAGN_2_OUT_X_L_ADDR 0x28
  150. #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
  151. #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
  152. /* CUSTOM VALUES FOR SENSOR 3 */
  153. #define ST_MAGN_3_WAI_ADDR 0x4f
  154. #define ST_MAGN_3_WAI_EXP 0x40
  155. #define ST_MAGN_3_ODR_ADDR 0x60
  156. #define ST_MAGN_3_ODR_MASK 0x0c
  157. #define ST_MAGN_3_ODR_AVL_10HZ_VAL 0x00
  158. #define ST_MAGN_3_ODR_AVL_20HZ_VAL 0x01
  159. #define ST_MAGN_3_ODR_AVL_50HZ_VAL 0x02
  160. #define ST_MAGN_3_ODR_AVL_100HZ_VAL 0x03
  161. #define ST_MAGN_3_PW_ADDR 0x60
  162. #define ST_MAGN_3_PW_MASK 0x03
  163. #define ST_MAGN_3_PW_ON 0x00
  164. #define ST_MAGN_3_PW_OFF 0x03
  165. #define ST_MAGN_3_BDU_ADDR 0x62
  166. #define ST_MAGN_3_BDU_MASK 0x10
  167. #define ST_MAGN_3_DRDY_IRQ_ADDR 0x62
  168. #define ST_MAGN_3_DRDY_INT_MASK 0x01
  169. #define ST_MAGN_3_FS_AVL_15000_GAIN 1500
  170. #define ST_MAGN_3_MULTIREAD_BIT false
  171. #define ST_MAGN_3_OUT_X_L_ADDR 0x68
  172. #define ST_MAGN_3_OUT_Y_L_ADDR 0x6a
  173. #define ST_MAGN_3_OUT_Z_L_ADDR 0x6c
  174. static const struct iio_chan_spec st_magn_16bit_channels[] = {
  175. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  176. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  177. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_BE, 16, 16,
  178. ST_MAGN_DEFAULT_OUT_X_H_ADDR),
  179. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  180. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  181. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_BE, 16, 16,
  182. ST_MAGN_DEFAULT_OUT_Y_H_ADDR),
  183. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  184. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  185. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_BE, 16, 16,
  186. ST_MAGN_DEFAULT_OUT_Z_H_ADDR),
  187. IIO_CHAN_SOFT_TIMESTAMP(3)
  188. };
  189. static const struct iio_chan_spec st_magn_2_16bit_channels[] = {
  190. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  191. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  192. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
  193. ST_MAGN_2_OUT_X_L_ADDR),
  194. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  195. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  196. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
  197. ST_MAGN_2_OUT_Y_L_ADDR),
  198. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  199. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  200. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
  201. ST_MAGN_2_OUT_Z_L_ADDR),
  202. IIO_CHAN_SOFT_TIMESTAMP(3)
  203. };
  204. static const struct iio_chan_spec st_magn_3_16bit_channels[] = {
  205. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  206. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  207. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
  208. ST_MAGN_3_OUT_X_L_ADDR),
  209. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  210. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  211. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
  212. ST_MAGN_3_OUT_Y_L_ADDR),
  213. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  214. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  215. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
  216. ST_MAGN_3_OUT_Z_L_ADDR),
  217. IIO_CHAN_SOFT_TIMESTAMP(3)
  218. };
  219. static const struct st_sensor_settings st_magn_sensors_settings[] = {
  220. {
  221. .wai = 0, /* This sensor has no valid WhoAmI report 0 */
  222. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  223. .sensors_supported = {
  224. [0] = LSM303DLH_MAGN_DEV_NAME,
  225. },
  226. .ch = (struct iio_chan_spec *)st_magn_16bit_channels,
  227. .odr = {
  228. .addr = ST_MAGN_0_ODR_ADDR,
  229. .mask = ST_MAGN_0_ODR_MASK,
  230. .odr_avl = {
  231. { 1, ST_MAGN_0_ODR_AVL_1HZ_VAL, },
  232. { 2, ST_MAGN_0_ODR_AVL_2HZ_VAL, },
  233. { 3, ST_MAGN_0_ODR_AVL_3HZ_VAL, },
  234. { 8, ST_MAGN_0_ODR_AVL_8HZ_VAL, },
  235. { 15, ST_MAGN_0_ODR_AVL_15HZ_VAL, },
  236. { 30, ST_MAGN_0_ODR_AVL_30HZ_VAL, },
  237. { 75, ST_MAGN_0_ODR_AVL_75HZ_VAL, },
  238. },
  239. },
  240. .pw = {
  241. .addr = ST_MAGN_0_PW_ADDR,
  242. .mask = ST_MAGN_0_PW_MASK,
  243. .value_on = ST_MAGN_0_PW_ON,
  244. .value_off = ST_MAGN_0_PW_OFF,
  245. },
  246. .fs = {
  247. .addr = ST_MAGN_0_FS_ADDR,
  248. .mask = ST_MAGN_0_FS_MASK,
  249. .fs_avl = {
  250. [0] = {
  251. .num = ST_MAGN_FS_AVL_1300MG,
  252. .value = ST_MAGN_0_FS_AVL_1300_VAL,
  253. .gain = ST_MAGN_0_FS_AVL_1300_GAIN_XY,
  254. .gain2 = ST_MAGN_0_FS_AVL_1300_GAIN_Z,
  255. },
  256. [1] = {
  257. .num = ST_MAGN_FS_AVL_1900MG,
  258. .value = ST_MAGN_0_FS_AVL_1900_VAL,
  259. .gain = ST_MAGN_0_FS_AVL_1900_GAIN_XY,
  260. .gain2 = ST_MAGN_0_FS_AVL_1900_GAIN_Z,
  261. },
  262. [2] = {
  263. .num = ST_MAGN_FS_AVL_2500MG,
  264. .value = ST_MAGN_0_FS_AVL_2500_VAL,
  265. .gain = ST_MAGN_0_FS_AVL_2500_GAIN_XY,
  266. .gain2 = ST_MAGN_0_FS_AVL_2500_GAIN_Z,
  267. },
  268. [3] = {
  269. .num = ST_MAGN_FS_AVL_4000MG,
  270. .value = ST_MAGN_0_FS_AVL_4000_VAL,
  271. .gain = ST_MAGN_0_FS_AVL_4000_GAIN_XY,
  272. .gain2 = ST_MAGN_0_FS_AVL_4000_GAIN_Z,
  273. },
  274. [4] = {
  275. .num = ST_MAGN_FS_AVL_4700MG,
  276. .value = ST_MAGN_0_FS_AVL_4700_VAL,
  277. .gain = ST_MAGN_0_FS_AVL_4700_GAIN_XY,
  278. .gain2 = ST_MAGN_0_FS_AVL_4700_GAIN_Z,
  279. },
  280. [5] = {
  281. .num = ST_MAGN_FS_AVL_5600MG,
  282. .value = ST_MAGN_0_FS_AVL_5600_VAL,
  283. .gain = ST_MAGN_0_FS_AVL_5600_GAIN_XY,
  284. .gain2 = ST_MAGN_0_FS_AVL_5600_GAIN_Z,
  285. },
  286. [6] = {
  287. .num = ST_MAGN_FS_AVL_8100MG,
  288. .value = ST_MAGN_0_FS_AVL_8100_VAL,
  289. .gain = ST_MAGN_0_FS_AVL_8100_GAIN_XY,
  290. .gain2 = ST_MAGN_0_FS_AVL_8100_GAIN_Z,
  291. },
  292. },
  293. },
  294. .multi_read_bit = ST_MAGN_0_MULTIREAD_BIT,
  295. .bootime = 2,
  296. },
  297. {
  298. .wai = ST_MAGN_1_WAI_EXP,
  299. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  300. .sensors_supported = {
  301. [0] = LSM303DLHC_MAGN_DEV_NAME,
  302. [1] = LSM303DLM_MAGN_DEV_NAME,
  303. },
  304. .ch = (struct iio_chan_spec *)st_magn_16bit_channels,
  305. .odr = {
  306. .addr = ST_MAGN_1_ODR_ADDR,
  307. .mask = ST_MAGN_1_ODR_MASK,
  308. .odr_avl = {
  309. { 1, ST_MAGN_1_ODR_AVL_1HZ_VAL, },
  310. { 2, ST_MAGN_1_ODR_AVL_2HZ_VAL, },
  311. { 3, ST_MAGN_1_ODR_AVL_3HZ_VAL, },
  312. { 8, ST_MAGN_1_ODR_AVL_8HZ_VAL, },
  313. { 15, ST_MAGN_1_ODR_AVL_15HZ_VAL, },
  314. { 30, ST_MAGN_1_ODR_AVL_30HZ_VAL, },
  315. { 75, ST_MAGN_1_ODR_AVL_75HZ_VAL, },
  316. { 220, ST_MAGN_1_ODR_AVL_220HZ_VAL, },
  317. },
  318. },
  319. .pw = {
  320. .addr = ST_MAGN_1_PW_ADDR,
  321. .mask = ST_MAGN_1_PW_MASK,
  322. .value_on = ST_MAGN_1_PW_ON,
  323. .value_off = ST_MAGN_1_PW_OFF,
  324. },
  325. .fs = {
  326. .addr = ST_MAGN_1_FS_ADDR,
  327. .mask = ST_MAGN_1_FS_MASK,
  328. .fs_avl = {
  329. [0] = {
  330. .num = ST_MAGN_FS_AVL_1300MG,
  331. .value = ST_MAGN_1_FS_AVL_1300_VAL,
  332. .gain = ST_MAGN_1_FS_AVL_1300_GAIN_XY,
  333. .gain2 = ST_MAGN_1_FS_AVL_1300_GAIN_Z,
  334. },
  335. [1] = {
  336. .num = ST_MAGN_FS_AVL_1900MG,
  337. .value = ST_MAGN_1_FS_AVL_1900_VAL,
  338. .gain = ST_MAGN_1_FS_AVL_1900_GAIN_XY,
  339. .gain2 = ST_MAGN_1_FS_AVL_1900_GAIN_Z,
  340. },
  341. [2] = {
  342. .num = ST_MAGN_FS_AVL_2500MG,
  343. .value = ST_MAGN_1_FS_AVL_2500_VAL,
  344. .gain = ST_MAGN_1_FS_AVL_2500_GAIN_XY,
  345. .gain2 = ST_MAGN_1_FS_AVL_2500_GAIN_Z,
  346. },
  347. [3] = {
  348. .num = ST_MAGN_FS_AVL_4000MG,
  349. .value = ST_MAGN_1_FS_AVL_4000_VAL,
  350. .gain = ST_MAGN_1_FS_AVL_4000_GAIN_XY,
  351. .gain2 = ST_MAGN_1_FS_AVL_4000_GAIN_Z,
  352. },
  353. [4] = {
  354. .num = ST_MAGN_FS_AVL_4700MG,
  355. .value = ST_MAGN_1_FS_AVL_4700_VAL,
  356. .gain = ST_MAGN_1_FS_AVL_4700_GAIN_XY,
  357. .gain2 = ST_MAGN_1_FS_AVL_4700_GAIN_Z,
  358. },
  359. [5] = {
  360. .num = ST_MAGN_FS_AVL_5600MG,
  361. .value = ST_MAGN_1_FS_AVL_5600_VAL,
  362. .gain = ST_MAGN_1_FS_AVL_5600_GAIN_XY,
  363. .gain2 = ST_MAGN_1_FS_AVL_5600_GAIN_Z,
  364. },
  365. [6] = {
  366. .num = ST_MAGN_FS_AVL_8100MG,
  367. .value = ST_MAGN_1_FS_AVL_8100_VAL,
  368. .gain = ST_MAGN_1_FS_AVL_8100_GAIN_XY,
  369. .gain2 = ST_MAGN_1_FS_AVL_8100_GAIN_Z,
  370. },
  371. },
  372. },
  373. .multi_read_bit = ST_MAGN_1_MULTIREAD_BIT,
  374. .bootime = 2,
  375. },
  376. {
  377. .wai = ST_MAGN_2_WAI_EXP,
  378. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  379. .sensors_supported = {
  380. [0] = LIS3MDL_MAGN_DEV_NAME,
  381. },
  382. .ch = (struct iio_chan_spec *)st_magn_2_16bit_channels,
  383. .odr = {
  384. .addr = ST_MAGN_2_ODR_ADDR,
  385. .mask = ST_MAGN_2_ODR_MASK,
  386. .odr_avl = {
  387. { 1, ST_MAGN_2_ODR_AVL_1HZ_VAL, },
  388. { 2, ST_MAGN_2_ODR_AVL_2HZ_VAL, },
  389. { 3, ST_MAGN_2_ODR_AVL_3HZ_VAL, },
  390. { 5, ST_MAGN_2_ODR_AVL_5HZ_VAL, },
  391. { 10, ST_MAGN_2_ODR_AVL_10HZ_VAL, },
  392. { 20, ST_MAGN_2_ODR_AVL_20HZ_VAL, },
  393. { 40, ST_MAGN_2_ODR_AVL_40HZ_VAL, },
  394. { 80, ST_MAGN_2_ODR_AVL_80HZ_VAL, },
  395. },
  396. },
  397. .pw = {
  398. .addr = ST_MAGN_2_PW_ADDR,
  399. .mask = ST_MAGN_2_PW_MASK,
  400. .value_on = ST_MAGN_2_PW_ON,
  401. .value_off = ST_MAGN_2_PW_OFF,
  402. },
  403. .fs = {
  404. .addr = ST_MAGN_2_FS_ADDR,
  405. .mask = ST_MAGN_2_FS_MASK,
  406. .fs_avl = {
  407. [0] = {
  408. .num = ST_MAGN_FS_AVL_4000MG,
  409. .value = ST_MAGN_2_FS_AVL_4000_VAL,
  410. .gain = ST_MAGN_2_FS_AVL_4000_GAIN,
  411. },
  412. [1] = {
  413. .num = ST_MAGN_FS_AVL_8000MG,
  414. .value = ST_MAGN_2_FS_AVL_8000_VAL,
  415. .gain = ST_MAGN_2_FS_AVL_8000_GAIN,
  416. },
  417. [2] = {
  418. .num = ST_MAGN_FS_AVL_12000MG,
  419. .value = ST_MAGN_2_FS_AVL_12000_VAL,
  420. .gain = ST_MAGN_2_FS_AVL_12000_GAIN,
  421. },
  422. [3] = {
  423. .num = ST_MAGN_FS_AVL_16000MG,
  424. .value = ST_MAGN_2_FS_AVL_16000_VAL,
  425. .gain = ST_MAGN_2_FS_AVL_16000_GAIN,
  426. },
  427. },
  428. },
  429. .multi_read_bit = ST_MAGN_2_MULTIREAD_BIT,
  430. .bootime = 2,
  431. },
  432. {
  433. .wai = ST_MAGN_3_WAI_EXP,
  434. .wai_addr = ST_MAGN_3_WAI_ADDR,
  435. .sensors_supported = {
  436. [0] = LSM303AGR_MAGN_DEV_NAME,
  437. },
  438. .ch = (struct iio_chan_spec *)st_magn_3_16bit_channels,
  439. .odr = {
  440. .addr = ST_MAGN_3_ODR_ADDR,
  441. .mask = ST_MAGN_3_ODR_MASK,
  442. .odr_avl = {
  443. { 10, ST_MAGN_3_ODR_AVL_10HZ_VAL, },
  444. { 20, ST_MAGN_3_ODR_AVL_20HZ_VAL, },
  445. { 50, ST_MAGN_3_ODR_AVL_50HZ_VAL, },
  446. { 100, ST_MAGN_3_ODR_AVL_100HZ_VAL, },
  447. },
  448. },
  449. .pw = {
  450. .addr = ST_MAGN_3_PW_ADDR,
  451. .mask = ST_MAGN_3_PW_MASK,
  452. .value_on = ST_MAGN_3_PW_ON,
  453. .value_off = ST_MAGN_3_PW_OFF,
  454. },
  455. .fs = {
  456. .fs_avl = {
  457. [0] = {
  458. .num = ST_MAGN_FS_AVL_15000MG,
  459. .gain = ST_MAGN_3_FS_AVL_15000_GAIN,
  460. },
  461. },
  462. },
  463. .bdu = {
  464. .addr = ST_MAGN_3_BDU_ADDR,
  465. .mask = ST_MAGN_3_BDU_MASK,
  466. },
  467. .drdy_irq = {
  468. .addr = ST_MAGN_3_DRDY_IRQ_ADDR,
  469. .mask_int1 = ST_MAGN_3_DRDY_INT_MASK,
  470. },
  471. .multi_read_bit = ST_MAGN_3_MULTIREAD_BIT,
  472. .bootime = 2,
  473. },
  474. };
  475. static int st_magn_read_raw(struct iio_dev *indio_dev,
  476. struct iio_chan_spec const *ch, int *val,
  477. int *val2, long mask)
  478. {
  479. int err;
  480. struct st_sensor_data *mdata = iio_priv(indio_dev);
  481. switch (mask) {
  482. case IIO_CHAN_INFO_RAW:
  483. err = st_sensors_read_info_raw(indio_dev, ch, val);
  484. if (err < 0)
  485. goto read_error;
  486. return IIO_VAL_INT;
  487. case IIO_CHAN_INFO_SCALE:
  488. *val = 0;
  489. if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
  490. (mdata->current_fullscale->gain2 != 0))
  491. *val2 = mdata->current_fullscale->gain2;
  492. else
  493. *val2 = mdata->current_fullscale->gain;
  494. return IIO_VAL_INT_PLUS_MICRO;
  495. case IIO_CHAN_INFO_SAMP_FREQ:
  496. *val = mdata->odr;
  497. return IIO_VAL_INT;
  498. default:
  499. return -EINVAL;
  500. }
  501. read_error:
  502. return err;
  503. }
  504. static int st_magn_write_raw(struct iio_dev *indio_dev,
  505. struct iio_chan_spec const *chan, int val, int val2, long mask)
  506. {
  507. int err;
  508. switch (mask) {
  509. case IIO_CHAN_INFO_SCALE:
  510. err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
  511. break;
  512. case IIO_CHAN_INFO_SAMP_FREQ:
  513. if (val2)
  514. return -EINVAL;
  515. mutex_lock(&indio_dev->mlock);
  516. err = st_sensors_set_odr(indio_dev, val);
  517. mutex_unlock(&indio_dev->mlock);
  518. return err;
  519. default:
  520. err = -EINVAL;
  521. }
  522. return err;
  523. }
  524. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  525. static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
  526. static struct attribute *st_magn_attributes[] = {
  527. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  528. &iio_dev_attr_in_magn_scale_available.dev_attr.attr,
  529. NULL,
  530. };
  531. static const struct attribute_group st_magn_attribute_group = {
  532. .attrs = st_magn_attributes,
  533. };
  534. static const struct iio_info magn_info = {
  535. .driver_module = THIS_MODULE,
  536. .attrs = &st_magn_attribute_group,
  537. .read_raw = &st_magn_read_raw,
  538. .write_raw = &st_magn_write_raw,
  539. .debugfs_reg_access = &st_sensors_debugfs_reg_access,
  540. };
  541. #ifdef CONFIG_IIO_TRIGGER
  542. static const struct iio_trigger_ops st_magn_trigger_ops = {
  543. .owner = THIS_MODULE,
  544. .set_trigger_state = ST_MAGN_TRIGGER_SET_STATE,
  545. };
  546. #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
  547. #else
  548. #define ST_MAGN_TRIGGER_OPS NULL
  549. #endif
  550. int st_magn_common_probe(struct iio_dev *indio_dev)
  551. {
  552. struct st_sensor_data *mdata = iio_priv(indio_dev);
  553. int irq = mdata->get_irq_data_ready(indio_dev);
  554. int err;
  555. indio_dev->modes = INDIO_DIRECT_MODE;
  556. indio_dev->info = &magn_info;
  557. mutex_init(&mdata->tb.buf_lock);
  558. st_sensors_power_enable(indio_dev);
  559. err = st_sensors_check_device_support(indio_dev,
  560. ARRAY_SIZE(st_magn_sensors_settings),
  561. st_magn_sensors_settings);
  562. if (err < 0)
  563. return err;
  564. mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
  565. mdata->multiread_bit = mdata->sensor_settings->multi_read_bit;
  566. indio_dev->channels = mdata->sensor_settings->ch;
  567. indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  568. mdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  569. &mdata->sensor_settings->fs.fs_avl[0];
  570. mdata->odr = mdata->sensor_settings->odr.odr_avl[0].hz;
  571. err = st_sensors_init_sensor(indio_dev, NULL);
  572. if (err < 0)
  573. return err;
  574. err = st_magn_allocate_ring(indio_dev);
  575. if (err < 0)
  576. return err;
  577. if (irq > 0) {
  578. err = st_sensors_allocate_trigger(indio_dev,
  579. ST_MAGN_TRIGGER_OPS);
  580. if (err < 0)
  581. goto st_magn_probe_trigger_error;
  582. }
  583. err = iio_device_register(indio_dev);
  584. if (err)
  585. goto st_magn_device_register_error;
  586. dev_info(&indio_dev->dev, "registered magnetometer %s\n",
  587. indio_dev->name);
  588. return 0;
  589. st_magn_device_register_error:
  590. if (irq > 0)
  591. st_sensors_deallocate_trigger(indio_dev);
  592. st_magn_probe_trigger_error:
  593. st_magn_deallocate_ring(indio_dev);
  594. return err;
  595. }
  596. EXPORT_SYMBOL(st_magn_common_probe);
  597. void st_magn_common_remove(struct iio_dev *indio_dev)
  598. {
  599. struct st_sensor_data *mdata = iio_priv(indio_dev);
  600. st_sensors_power_disable(indio_dev);
  601. iio_device_unregister(indio_dev);
  602. if (mdata->get_irq_data_ready(indio_dev) > 0)
  603. st_sensors_deallocate_trigger(indio_dev);
  604. st_magn_deallocate_ring(indio_dev);
  605. }
  606. EXPORT_SYMBOL(st_magn_common_remove);
  607. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  608. MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
  609. MODULE_LICENSE("GPL v2");