st_magn_core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * STMicroelectronics magnetometers driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <[email protected]>
  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_IHL_IRQ_ADDR 0x63
  170. #define ST_MAGN_3_IHL_IRQ_MASK 0x04
  171. #define ST_MAGN_3_FS_AVL_15000_GAIN 1500
  172. #define ST_MAGN_3_MULTIREAD_BIT false
  173. #define ST_MAGN_3_OUT_X_L_ADDR 0x68
  174. #define ST_MAGN_3_OUT_Y_L_ADDR 0x6a
  175. #define ST_MAGN_3_OUT_Z_L_ADDR 0x6c
  176. static const struct iio_chan_spec st_magn_16bit_channels[] = {
  177. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  178. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  179. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_BE, 16, 16,
  180. ST_MAGN_DEFAULT_OUT_X_H_ADDR),
  181. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  182. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  183. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_BE, 16, 16,
  184. ST_MAGN_DEFAULT_OUT_Y_H_ADDR),
  185. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  186. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  187. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_BE, 16, 16,
  188. ST_MAGN_DEFAULT_OUT_Z_H_ADDR),
  189. IIO_CHAN_SOFT_TIMESTAMP(3)
  190. };
  191. static const struct iio_chan_spec st_magn_2_16bit_channels[] = {
  192. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  193. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  194. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
  195. ST_MAGN_2_OUT_X_L_ADDR),
  196. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  197. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  198. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
  199. ST_MAGN_2_OUT_Y_L_ADDR),
  200. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  201. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  202. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
  203. ST_MAGN_2_OUT_Z_L_ADDR),
  204. IIO_CHAN_SOFT_TIMESTAMP(3)
  205. };
  206. static const struct iio_chan_spec st_magn_3_16bit_channels[] = {
  207. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  208. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  209. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
  210. ST_MAGN_3_OUT_X_L_ADDR),
  211. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  212. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  213. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
  214. ST_MAGN_3_OUT_Y_L_ADDR),
  215. ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
  216. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  217. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
  218. ST_MAGN_3_OUT_Z_L_ADDR),
  219. IIO_CHAN_SOFT_TIMESTAMP(3)
  220. };
  221. static const struct st_sensor_settings st_magn_sensors_settings[] = {
  222. {
  223. .wai = 0, /* This sensor has no valid WhoAmI report 0 */
  224. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  225. .sensors_supported = {
  226. [0] = LSM303DLH_MAGN_DEV_NAME,
  227. },
  228. .ch = (struct iio_chan_spec *)st_magn_16bit_channels,
  229. .odr = {
  230. .addr = ST_MAGN_0_ODR_ADDR,
  231. .mask = ST_MAGN_0_ODR_MASK,
  232. .odr_avl = {
  233. { 1, ST_MAGN_0_ODR_AVL_1HZ_VAL, },
  234. { 2, ST_MAGN_0_ODR_AVL_2HZ_VAL, },
  235. { 3, ST_MAGN_0_ODR_AVL_3HZ_VAL, },
  236. { 8, ST_MAGN_0_ODR_AVL_8HZ_VAL, },
  237. { 15, ST_MAGN_0_ODR_AVL_15HZ_VAL, },
  238. { 30, ST_MAGN_0_ODR_AVL_30HZ_VAL, },
  239. { 75, ST_MAGN_0_ODR_AVL_75HZ_VAL, },
  240. },
  241. },
  242. .pw = {
  243. .addr = ST_MAGN_0_PW_ADDR,
  244. .mask = ST_MAGN_0_PW_MASK,
  245. .value_on = ST_MAGN_0_PW_ON,
  246. .value_off = ST_MAGN_0_PW_OFF,
  247. },
  248. .fs = {
  249. .addr = ST_MAGN_0_FS_ADDR,
  250. .mask = ST_MAGN_0_FS_MASK,
  251. .fs_avl = {
  252. [0] = {
  253. .num = ST_MAGN_FS_AVL_1300MG,
  254. .value = ST_MAGN_0_FS_AVL_1300_VAL,
  255. .gain = ST_MAGN_0_FS_AVL_1300_GAIN_XY,
  256. .gain2 = ST_MAGN_0_FS_AVL_1300_GAIN_Z,
  257. },
  258. [1] = {
  259. .num = ST_MAGN_FS_AVL_1900MG,
  260. .value = ST_MAGN_0_FS_AVL_1900_VAL,
  261. .gain = ST_MAGN_0_FS_AVL_1900_GAIN_XY,
  262. .gain2 = ST_MAGN_0_FS_AVL_1900_GAIN_Z,
  263. },
  264. [2] = {
  265. .num = ST_MAGN_FS_AVL_2500MG,
  266. .value = ST_MAGN_0_FS_AVL_2500_VAL,
  267. .gain = ST_MAGN_0_FS_AVL_2500_GAIN_XY,
  268. .gain2 = ST_MAGN_0_FS_AVL_2500_GAIN_Z,
  269. },
  270. [3] = {
  271. .num = ST_MAGN_FS_AVL_4000MG,
  272. .value = ST_MAGN_0_FS_AVL_4000_VAL,
  273. .gain = ST_MAGN_0_FS_AVL_4000_GAIN_XY,
  274. .gain2 = ST_MAGN_0_FS_AVL_4000_GAIN_Z,
  275. },
  276. [4] = {
  277. .num = ST_MAGN_FS_AVL_4700MG,
  278. .value = ST_MAGN_0_FS_AVL_4700_VAL,
  279. .gain = ST_MAGN_0_FS_AVL_4700_GAIN_XY,
  280. .gain2 = ST_MAGN_0_FS_AVL_4700_GAIN_Z,
  281. },
  282. [5] = {
  283. .num = ST_MAGN_FS_AVL_5600MG,
  284. .value = ST_MAGN_0_FS_AVL_5600_VAL,
  285. .gain = ST_MAGN_0_FS_AVL_5600_GAIN_XY,
  286. .gain2 = ST_MAGN_0_FS_AVL_5600_GAIN_Z,
  287. },
  288. [6] = {
  289. .num = ST_MAGN_FS_AVL_8100MG,
  290. .value = ST_MAGN_0_FS_AVL_8100_VAL,
  291. .gain = ST_MAGN_0_FS_AVL_8100_GAIN_XY,
  292. .gain2 = ST_MAGN_0_FS_AVL_8100_GAIN_Z,
  293. },
  294. },
  295. },
  296. .multi_read_bit = ST_MAGN_0_MULTIREAD_BIT,
  297. .bootime = 2,
  298. },
  299. {
  300. .wai = ST_MAGN_1_WAI_EXP,
  301. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  302. .sensors_supported = {
  303. [0] = LSM303DLHC_MAGN_DEV_NAME,
  304. [1] = LSM303DLM_MAGN_DEV_NAME,
  305. },
  306. .ch = (struct iio_chan_spec *)st_magn_16bit_channels,
  307. .odr = {
  308. .addr = ST_MAGN_1_ODR_ADDR,
  309. .mask = ST_MAGN_1_ODR_MASK,
  310. .odr_avl = {
  311. { 1, ST_MAGN_1_ODR_AVL_1HZ_VAL, },
  312. { 2, ST_MAGN_1_ODR_AVL_2HZ_VAL, },
  313. { 3, ST_MAGN_1_ODR_AVL_3HZ_VAL, },
  314. { 8, ST_MAGN_1_ODR_AVL_8HZ_VAL, },
  315. { 15, ST_MAGN_1_ODR_AVL_15HZ_VAL, },
  316. { 30, ST_MAGN_1_ODR_AVL_30HZ_VAL, },
  317. { 75, ST_MAGN_1_ODR_AVL_75HZ_VAL, },
  318. { 220, ST_MAGN_1_ODR_AVL_220HZ_VAL, },
  319. },
  320. },
  321. .pw = {
  322. .addr = ST_MAGN_1_PW_ADDR,
  323. .mask = ST_MAGN_1_PW_MASK,
  324. .value_on = ST_MAGN_1_PW_ON,
  325. .value_off = ST_MAGN_1_PW_OFF,
  326. },
  327. .fs = {
  328. .addr = ST_MAGN_1_FS_ADDR,
  329. .mask = ST_MAGN_1_FS_MASK,
  330. .fs_avl = {
  331. [0] = {
  332. .num = ST_MAGN_FS_AVL_1300MG,
  333. .value = ST_MAGN_1_FS_AVL_1300_VAL,
  334. .gain = ST_MAGN_1_FS_AVL_1300_GAIN_XY,
  335. .gain2 = ST_MAGN_1_FS_AVL_1300_GAIN_Z,
  336. },
  337. [1] = {
  338. .num = ST_MAGN_FS_AVL_1900MG,
  339. .value = ST_MAGN_1_FS_AVL_1900_VAL,
  340. .gain = ST_MAGN_1_FS_AVL_1900_GAIN_XY,
  341. .gain2 = ST_MAGN_1_FS_AVL_1900_GAIN_Z,
  342. },
  343. [2] = {
  344. .num = ST_MAGN_FS_AVL_2500MG,
  345. .value = ST_MAGN_1_FS_AVL_2500_VAL,
  346. .gain = ST_MAGN_1_FS_AVL_2500_GAIN_XY,
  347. .gain2 = ST_MAGN_1_FS_AVL_2500_GAIN_Z,
  348. },
  349. [3] = {
  350. .num = ST_MAGN_FS_AVL_4000MG,
  351. .value = ST_MAGN_1_FS_AVL_4000_VAL,
  352. .gain = ST_MAGN_1_FS_AVL_4000_GAIN_XY,
  353. .gain2 = ST_MAGN_1_FS_AVL_4000_GAIN_Z,
  354. },
  355. [4] = {
  356. .num = ST_MAGN_FS_AVL_4700MG,
  357. .value = ST_MAGN_1_FS_AVL_4700_VAL,
  358. .gain = ST_MAGN_1_FS_AVL_4700_GAIN_XY,
  359. .gain2 = ST_MAGN_1_FS_AVL_4700_GAIN_Z,
  360. },
  361. [5] = {
  362. .num = ST_MAGN_FS_AVL_5600MG,
  363. .value = ST_MAGN_1_FS_AVL_5600_VAL,
  364. .gain = ST_MAGN_1_FS_AVL_5600_GAIN_XY,
  365. .gain2 = ST_MAGN_1_FS_AVL_5600_GAIN_Z,
  366. },
  367. [6] = {
  368. .num = ST_MAGN_FS_AVL_8100MG,
  369. .value = ST_MAGN_1_FS_AVL_8100_VAL,
  370. .gain = ST_MAGN_1_FS_AVL_8100_GAIN_XY,
  371. .gain2 = ST_MAGN_1_FS_AVL_8100_GAIN_Z,
  372. },
  373. },
  374. },
  375. .multi_read_bit = ST_MAGN_1_MULTIREAD_BIT,
  376. .bootime = 2,
  377. },
  378. {
  379. .wai = ST_MAGN_2_WAI_EXP,
  380. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  381. .sensors_supported = {
  382. [0] = LIS3MDL_MAGN_DEV_NAME,
  383. },
  384. .ch = (struct iio_chan_spec *)st_magn_2_16bit_channels,
  385. .odr = {
  386. .addr = ST_MAGN_2_ODR_ADDR,
  387. .mask = ST_MAGN_2_ODR_MASK,
  388. .odr_avl = {
  389. { 1, ST_MAGN_2_ODR_AVL_1HZ_VAL, },
  390. { 2, ST_MAGN_2_ODR_AVL_2HZ_VAL, },
  391. { 3, ST_MAGN_2_ODR_AVL_3HZ_VAL, },
  392. { 5, ST_MAGN_2_ODR_AVL_5HZ_VAL, },
  393. { 10, ST_MAGN_2_ODR_AVL_10HZ_VAL, },
  394. { 20, ST_MAGN_2_ODR_AVL_20HZ_VAL, },
  395. { 40, ST_MAGN_2_ODR_AVL_40HZ_VAL, },
  396. { 80, ST_MAGN_2_ODR_AVL_80HZ_VAL, },
  397. },
  398. },
  399. .pw = {
  400. .addr = ST_MAGN_2_PW_ADDR,
  401. .mask = ST_MAGN_2_PW_MASK,
  402. .value_on = ST_MAGN_2_PW_ON,
  403. .value_off = ST_MAGN_2_PW_OFF,
  404. },
  405. .fs = {
  406. .addr = ST_MAGN_2_FS_ADDR,
  407. .mask = ST_MAGN_2_FS_MASK,
  408. .fs_avl = {
  409. [0] = {
  410. .num = ST_MAGN_FS_AVL_4000MG,
  411. .value = ST_MAGN_2_FS_AVL_4000_VAL,
  412. .gain = ST_MAGN_2_FS_AVL_4000_GAIN,
  413. },
  414. [1] = {
  415. .num = ST_MAGN_FS_AVL_8000MG,
  416. .value = ST_MAGN_2_FS_AVL_8000_VAL,
  417. .gain = ST_MAGN_2_FS_AVL_8000_GAIN,
  418. },
  419. [2] = {
  420. .num = ST_MAGN_FS_AVL_12000MG,
  421. .value = ST_MAGN_2_FS_AVL_12000_VAL,
  422. .gain = ST_MAGN_2_FS_AVL_12000_GAIN,
  423. },
  424. [3] = {
  425. .num = ST_MAGN_FS_AVL_16000MG,
  426. .value = ST_MAGN_2_FS_AVL_16000_VAL,
  427. .gain = ST_MAGN_2_FS_AVL_16000_GAIN,
  428. },
  429. },
  430. },
  431. .multi_read_bit = ST_MAGN_2_MULTIREAD_BIT,
  432. .bootime = 2,
  433. },
  434. {
  435. .wai = ST_MAGN_3_WAI_EXP,
  436. .wai_addr = ST_MAGN_3_WAI_ADDR,
  437. .sensors_supported = {
  438. [0] = LSM303AGR_MAGN_DEV_NAME,
  439. },
  440. .ch = (struct iio_chan_spec *)st_magn_3_16bit_channels,
  441. .odr = {
  442. .addr = ST_MAGN_3_ODR_ADDR,
  443. .mask = ST_MAGN_3_ODR_MASK,
  444. .odr_avl = {
  445. { 10, ST_MAGN_3_ODR_AVL_10HZ_VAL, },
  446. { 20, ST_MAGN_3_ODR_AVL_20HZ_VAL, },
  447. { 50, ST_MAGN_3_ODR_AVL_50HZ_VAL, },
  448. { 100, ST_MAGN_3_ODR_AVL_100HZ_VAL, },
  449. },
  450. },
  451. .pw = {
  452. .addr = ST_MAGN_3_PW_ADDR,
  453. .mask = ST_MAGN_3_PW_MASK,
  454. .value_on = ST_MAGN_3_PW_ON,
  455. .value_off = ST_MAGN_3_PW_OFF,
  456. },
  457. .fs = {
  458. .fs_avl = {
  459. [0] = {
  460. .num = ST_MAGN_FS_AVL_15000MG,
  461. .gain = ST_MAGN_3_FS_AVL_15000_GAIN,
  462. },
  463. },
  464. },
  465. .bdu = {
  466. .addr = ST_MAGN_3_BDU_ADDR,
  467. .mask = ST_MAGN_3_BDU_MASK,
  468. },
  469. .drdy_irq = {
  470. .addr = ST_MAGN_3_DRDY_IRQ_ADDR,
  471. .mask_int1 = ST_MAGN_3_DRDY_INT_MASK,
  472. .addr_ihl = ST_MAGN_3_IHL_IRQ_ADDR,
  473. .mask_ihl = ST_MAGN_3_IHL_IRQ_MASK,
  474. .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
  475. },
  476. .multi_read_bit = ST_MAGN_3_MULTIREAD_BIT,
  477. .bootime = 2,
  478. },
  479. };
  480. static int st_magn_read_raw(struct iio_dev *indio_dev,
  481. struct iio_chan_spec const *ch, int *val,
  482. int *val2, long mask)
  483. {
  484. int err;
  485. struct st_sensor_data *mdata = iio_priv(indio_dev);
  486. switch (mask) {
  487. case IIO_CHAN_INFO_RAW:
  488. err = st_sensors_read_info_raw(indio_dev, ch, val);
  489. if (err < 0)
  490. goto read_error;
  491. return IIO_VAL_INT;
  492. case IIO_CHAN_INFO_SCALE:
  493. *val = 0;
  494. if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
  495. (mdata->current_fullscale->gain2 != 0))
  496. *val2 = mdata->current_fullscale->gain2;
  497. else
  498. *val2 = mdata->current_fullscale->gain;
  499. return IIO_VAL_INT_PLUS_MICRO;
  500. case IIO_CHAN_INFO_SAMP_FREQ:
  501. *val = mdata->odr;
  502. return IIO_VAL_INT;
  503. default:
  504. return -EINVAL;
  505. }
  506. read_error:
  507. return err;
  508. }
  509. static int st_magn_write_raw(struct iio_dev *indio_dev,
  510. struct iio_chan_spec const *chan, int val, int val2, long mask)
  511. {
  512. int err;
  513. switch (mask) {
  514. case IIO_CHAN_INFO_SCALE:
  515. err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
  516. break;
  517. case IIO_CHAN_INFO_SAMP_FREQ:
  518. if (val2)
  519. return -EINVAL;
  520. mutex_lock(&indio_dev->mlock);
  521. err = st_sensors_set_odr(indio_dev, val);
  522. mutex_unlock(&indio_dev->mlock);
  523. return err;
  524. default:
  525. err = -EINVAL;
  526. }
  527. return err;
  528. }
  529. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  530. static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
  531. static struct attribute *st_magn_attributes[] = {
  532. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  533. &iio_dev_attr_in_magn_scale_available.dev_attr.attr,
  534. NULL,
  535. };
  536. static const struct attribute_group st_magn_attribute_group = {
  537. .attrs = st_magn_attributes,
  538. };
  539. static const struct iio_info magn_info = {
  540. .driver_module = THIS_MODULE,
  541. .attrs = &st_magn_attribute_group,
  542. .read_raw = &st_magn_read_raw,
  543. .write_raw = &st_magn_write_raw,
  544. .debugfs_reg_access = &st_sensors_debugfs_reg_access,
  545. };
  546. #ifdef CONFIG_IIO_TRIGGER
  547. static const struct iio_trigger_ops st_magn_trigger_ops = {
  548. .owner = THIS_MODULE,
  549. .set_trigger_state = ST_MAGN_TRIGGER_SET_STATE,
  550. .validate_device = st_sensors_validate_device,
  551. };
  552. #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
  553. #else
  554. #define ST_MAGN_TRIGGER_OPS NULL
  555. #endif
  556. int st_magn_common_probe(struct iio_dev *indio_dev)
  557. {
  558. struct st_sensor_data *mdata = iio_priv(indio_dev);
  559. int irq = mdata->get_irq_data_ready(indio_dev);
  560. int err;
  561. indio_dev->modes = INDIO_DIRECT_MODE;
  562. indio_dev->info = &magn_info;
  563. mutex_init(&mdata->tb.buf_lock);
  564. err = st_sensors_power_enable(indio_dev);
  565. if (err)
  566. return err;
  567. err = st_sensors_check_device_support(indio_dev,
  568. ARRAY_SIZE(st_magn_sensors_settings),
  569. st_magn_sensors_settings);
  570. if (err < 0)
  571. goto st_magn_power_off;
  572. mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
  573. mdata->multiread_bit = mdata->sensor_settings->multi_read_bit;
  574. indio_dev->channels = mdata->sensor_settings->ch;
  575. indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  576. mdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  577. &mdata->sensor_settings->fs.fs_avl[0];
  578. mdata->odr = mdata->sensor_settings->odr.odr_avl[0].hz;
  579. err = st_sensors_init_sensor(indio_dev, NULL);
  580. if (err < 0)
  581. goto st_magn_power_off;
  582. err = st_magn_allocate_ring(indio_dev);
  583. if (err < 0)
  584. goto st_magn_power_off;
  585. if (irq > 0) {
  586. err = st_sensors_allocate_trigger(indio_dev,
  587. ST_MAGN_TRIGGER_OPS);
  588. if (err < 0)
  589. goto st_magn_probe_trigger_error;
  590. }
  591. err = iio_device_register(indio_dev);
  592. if (err)
  593. goto st_magn_device_register_error;
  594. dev_info(&indio_dev->dev, "registered magnetometer %s\n",
  595. indio_dev->name);
  596. return 0;
  597. st_magn_device_register_error:
  598. if (irq > 0)
  599. st_sensors_deallocate_trigger(indio_dev);
  600. st_magn_probe_trigger_error:
  601. st_magn_deallocate_ring(indio_dev);
  602. st_magn_power_off:
  603. st_sensors_power_disable(indio_dev);
  604. return err;
  605. }
  606. EXPORT_SYMBOL(st_magn_common_probe);
  607. void st_magn_common_remove(struct iio_dev *indio_dev)
  608. {
  609. struct st_sensor_data *mdata = iio_priv(indio_dev);
  610. st_sensors_power_disable(indio_dev);
  611. iio_device_unregister(indio_dev);
  612. if (mdata->get_irq_data_ready(indio_dev) > 0)
  613. st_sensors_deallocate_trigger(indio_dev);
  614. st_magn_deallocate_ring(indio_dev);
  615. }
  616. EXPORT_SYMBOL(st_magn_common_remove);
  617. MODULE_AUTHOR("Denis Ciocca <[email protected]>");
  618. MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
  619. MODULE_LICENSE("GPL v2");