sca3000.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
  4. *
  5. * Copyright (c) 2009 Jonathan Cameron <jic23@kernel.org>
  6. *
  7. * See industrialio/accels/sca3000.h for comments.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/fs.h>
  11. #include <linux/device.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/events.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/kfifo_buf.h>
  23. #define SCA3000_WRITE_REG(a) (((a) << 2) | 0x02)
  24. #define SCA3000_READ_REG(a) ((a) << 2)
  25. #define SCA3000_REG_REVID_ADDR 0x00
  26. #define SCA3000_REG_REVID_MAJOR_MASK GENMASK(8, 4)
  27. #define SCA3000_REG_REVID_MINOR_MASK GENMASK(3, 0)
  28. #define SCA3000_REG_STATUS_ADDR 0x02
  29. #define SCA3000_LOCKED BIT(5)
  30. #define SCA3000_EEPROM_CS_ERROR BIT(1)
  31. #define SCA3000_SPI_FRAME_ERROR BIT(0)
  32. /* All reads done using register decrement so no need to directly access LSBs */
  33. #define SCA3000_REG_X_MSB_ADDR 0x05
  34. #define SCA3000_REG_Y_MSB_ADDR 0x07
  35. #define SCA3000_REG_Z_MSB_ADDR 0x09
  36. #define SCA3000_REG_RING_OUT_ADDR 0x0f
  37. /* Temp read untested - the e05 doesn't have the sensor */
  38. #define SCA3000_REG_TEMP_MSB_ADDR 0x13
  39. #define SCA3000_REG_MODE_ADDR 0x14
  40. #define SCA3000_MODE_PROT_MASK 0x28
  41. #define SCA3000_REG_MODE_RING_BUF_ENABLE BIT(7)
  42. #define SCA3000_REG_MODE_RING_BUF_8BIT BIT(6)
  43. /*
  44. * Free fall detection triggers an interrupt if the acceleration
  45. * is below a threshold for equivalent of 25cm drop
  46. */
  47. #define SCA3000_REG_MODE_FREE_FALL_DETECT BIT(4)
  48. #define SCA3000_REG_MODE_MEAS_MODE_NORMAL 0x00
  49. #define SCA3000_REG_MODE_MEAS_MODE_OP_1 0x01
  50. #define SCA3000_REG_MODE_MEAS_MODE_OP_2 0x02
  51. /*
  52. * In motion detection mode the accelerations are band pass filtered
  53. * (approx 1 - 25Hz) and then a programmable threshold used to trigger
  54. * and interrupt.
  55. */
  56. #define SCA3000_REG_MODE_MEAS_MODE_MOT_DET 0x03
  57. #define SCA3000_REG_MODE_MODE_MASK 0x03
  58. #define SCA3000_REG_BUF_COUNT_ADDR 0x15
  59. #define SCA3000_REG_INT_STATUS_ADDR 0x16
  60. #define SCA3000_REG_INT_STATUS_THREE_QUARTERS BIT(7)
  61. #define SCA3000_REG_INT_STATUS_HALF BIT(6)
  62. #define SCA3000_INT_STATUS_FREE_FALL BIT(3)
  63. #define SCA3000_INT_STATUS_Y_TRIGGER BIT(2)
  64. #define SCA3000_INT_STATUS_X_TRIGGER BIT(1)
  65. #define SCA3000_INT_STATUS_Z_TRIGGER BIT(0)
  66. /* Used to allow access to multiplexed registers */
  67. #define SCA3000_REG_CTRL_SEL_ADDR 0x18
  68. /* Only available for SCA3000-D03 and SCA3000-D01 */
  69. #define SCA3000_REG_CTRL_SEL_I2C_DISABLE 0x01
  70. #define SCA3000_REG_CTRL_SEL_MD_CTRL 0x02
  71. #define SCA3000_REG_CTRL_SEL_MD_Y_TH 0x03
  72. #define SCA3000_REG_CTRL_SEL_MD_X_TH 0x04
  73. #define SCA3000_REG_CTRL_SEL_MD_Z_TH 0x05
  74. /*
  75. * BE VERY CAREFUL WITH THIS, IF 3 BITS ARE NOT SET the device
  76. * will not function
  77. */
  78. #define SCA3000_REG_CTRL_SEL_OUT_CTRL 0x0B
  79. #define SCA3000_REG_OUT_CTRL_PROT_MASK 0xE0
  80. #define SCA3000_REG_OUT_CTRL_BUF_X_EN 0x10
  81. #define SCA3000_REG_OUT_CTRL_BUF_Y_EN 0x08
  82. #define SCA3000_REG_OUT_CTRL_BUF_Z_EN 0x04
  83. #define SCA3000_REG_OUT_CTRL_BUF_DIV_MASK 0x03
  84. #define SCA3000_REG_OUT_CTRL_BUF_DIV_4 0x02
  85. #define SCA3000_REG_OUT_CTRL_BUF_DIV_2 0x01
  86. /*
  87. * Control which motion detector interrupts are on.
  88. * For now only OR combinations are supported.
  89. */
  90. #define SCA3000_MD_CTRL_PROT_MASK 0xC0
  91. #define SCA3000_MD_CTRL_OR_Y BIT(0)
  92. #define SCA3000_MD_CTRL_OR_X BIT(1)
  93. #define SCA3000_MD_CTRL_OR_Z BIT(2)
  94. /* Currently unsupported */
  95. #define SCA3000_MD_CTRL_AND_Y BIT(3)
  96. #define SCA3000_MD_CTRL_AND_X BIT(4)
  97. #define SCA3000_MD_CTRL_AND_Z BIT(5)
  98. /*
  99. * Some control registers of complex access methods requiring this register to
  100. * be used to remove a lock.
  101. */
  102. #define SCA3000_REG_UNLOCK_ADDR 0x1e
  103. #define SCA3000_REG_INT_MASK_ADDR 0x21
  104. #define SCA3000_REG_INT_MASK_PROT_MASK 0x1C
  105. #define SCA3000_REG_INT_MASK_RING_THREE_QUARTER BIT(7)
  106. #define SCA3000_REG_INT_MASK_RING_HALF BIT(6)
  107. #define SCA3000_REG_INT_MASK_ALL_INTS 0x02
  108. #define SCA3000_REG_INT_MASK_ACTIVE_HIGH 0x01
  109. #define SCA3000_REG_INT_MASK_ACTIVE_LOW 0x00
  110. /* Values of multiplexed registers (write to ctrl_data after select) */
  111. #define SCA3000_REG_CTRL_DATA_ADDR 0x22
  112. /*
  113. * Measurement modes available on some sca3000 series chips. Code assumes others
  114. * may become available in the future.
  115. *
  116. * Bypass - Bypass the low-pass filter in the signal channel so as to increase
  117. * signal bandwidth.
  118. *
  119. * Narrow - Narrow low-pass filtering of the signal channel and half output
  120. * data rate by decimation.
  121. *
  122. * Wide - Widen low-pass filtering of signal channel to increase bandwidth
  123. */
  124. #define SCA3000_OP_MODE_BYPASS 0x01
  125. #define SCA3000_OP_MODE_NARROW 0x02
  126. #define SCA3000_OP_MODE_WIDE 0x04
  127. #define SCA3000_MAX_TX 6
  128. #define SCA3000_MAX_RX 2
  129. /**
  130. * struct sca3000_state - device instance state information
  131. * @us: the associated spi device
  132. * @info: chip variant information
  133. * @mo_det_use_count: reference counter for the motion detection unit
  134. * @lock: lock used to protect elements of sca3000_state
  135. * and the underlying device state.
  136. * @tx: dma-able transmit buffer
  137. * @rx: dma-able receive buffer
  138. **/
  139. struct sca3000_state {
  140. struct spi_device *us;
  141. const struct sca3000_chip_info *info;
  142. int mo_det_use_count;
  143. struct mutex lock;
  144. /* Can these share a cacheline ? */
  145. u8 rx[384] __aligned(IIO_DMA_MINALIGN);
  146. u8 tx[6] __aligned(IIO_DMA_MINALIGN);
  147. };
  148. /**
  149. * struct sca3000_chip_info - model dependent parameters
  150. * @scale: scale * 10^-6
  151. * @temp_output: some devices have temperature sensors.
  152. * @measurement_mode_freq: normal mode sampling frequency
  153. * @measurement_mode_3db_freq: 3db cutoff frequency of the low pass filter for
  154. * the normal measurement mode.
  155. * @option_mode_1: first optional mode. Not all models have one
  156. * @option_mode_1_freq: option mode 1 sampling frequency
  157. * @option_mode_1_3db_freq: 3db cutoff frequency of the low pass filter for
  158. * the first option mode.
  159. * @option_mode_2: second optional mode. Not all chips have one
  160. * @option_mode_2_freq: option mode 2 sampling frequency
  161. * @option_mode_2_3db_freq: 3db cutoff frequency of the low pass filter for
  162. * the second option mode.
  163. * @mot_det_mult_xz: Bit wise multipliers to calculate the threshold
  164. * for motion detection in the x and z axis.
  165. * @mot_det_mult_y: Bit wise multipliers to calculate the threshold
  166. * for motion detection in the y axis.
  167. *
  168. * This structure is used to hold information about the functionality of a given
  169. * sca3000 variant.
  170. **/
  171. struct sca3000_chip_info {
  172. unsigned int scale;
  173. bool temp_output;
  174. int measurement_mode_freq;
  175. int measurement_mode_3db_freq;
  176. int option_mode_1;
  177. int option_mode_1_freq;
  178. int option_mode_1_3db_freq;
  179. int option_mode_2;
  180. int option_mode_2_freq;
  181. int option_mode_2_3db_freq;
  182. int mot_det_mult_xz[6];
  183. int mot_det_mult_y[7];
  184. };
  185. enum sca3000_variant {
  186. d01,
  187. e02,
  188. e04,
  189. e05,
  190. };
  191. /*
  192. * Note where option modes are not defined, the chip simply does not
  193. * support any.
  194. * Other chips in the sca3000 series use i2c and are not included here.
  195. *
  196. * Some of these devices are only listed in the family data sheet and
  197. * do not actually appear to be available.
  198. */
  199. static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
  200. [d01] = {
  201. .scale = 7357,
  202. .temp_output = true,
  203. .measurement_mode_freq = 250,
  204. .measurement_mode_3db_freq = 45,
  205. .option_mode_1 = SCA3000_OP_MODE_BYPASS,
  206. .option_mode_1_freq = 250,
  207. .option_mode_1_3db_freq = 70,
  208. .mot_det_mult_xz = {50, 100, 200, 350, 650, 1300},
  209. .mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750},
  210. },
  211. [e02] = {
  212. .scale = 9810,
  213. .measurement_mode_freq = 125,
  214. .measurement_mode_3db_freq = 40,
  215. .option_mode_1 = SCA3000_OP_MODE_NARROW,
  216. .option_mode_1_freq = 63,
  217. .option_mode_1_3db_freq = 11,
  218. .mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050},
  219. .mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700},
  220. },
  221. [e04] = {
  222. .scale = 19620,
  223. .measurement_mode_freq = 100,
  224. .measurement_mode_3db_freq = 38,
  225. .option_mode_1 = SCA3000_OP_MODE_NARROW,
  226. .option_mode_1_freq = 50,
  227. .option_mode_1_3db_freq = 9,
  228. .option_mode_2 = SCA3000_OP_MODE_WIDE,
  229. .option_mode_2_freq = 400,
  230. .option_mode_2_3db_freq = 70,
  231. .mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100},
  232. .mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000},
  233. },
  234. [e05] = {
  235. .scale = 61313,
  236. .measurement_mode_freq = 200,
  237. .measurement_mode_3db_freq = 60,
  238. .option_mode_1 = SCA3000_OP_MODE_NARROW,
  239. .option_mode_1_freq = 50,
  240. .option_mode_1_3db_freq = 9,
  241. .option_mode_2 = SCA3000_OP_MODE_WIDE,
  242. .option_mode_2_freq = 400,
  243. .option_mode_2_3db_freq = 75,
  244. .mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900},
  245. .mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600},
  246. },
  247. };
  248. static int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
  249. {
  250. st->tx[0] = SCA3000_WRITE_REG(address);
  251. st->tx[1] = val;
  252. return spi_write(st->us, st->tx, 2);
  253. }
  254. static int sca3000_read_data_short(struct sca3000_state *st,
  255. u8 reg_address_high,
  256. int len)
  257. {
  258. struct spi_transfer xfer[2] = {
  259. {
  260. .len = 1,
  261. .tx_buf = st->tx,
  262. }, {
  263. .len = len,
  264. .rx_buf = st->rx,
  265. }
  266. };
  267. st->tx[0] = SCA3000_READ_REG(reg_address_high);
  268. return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
  269. }
  270. /**
  271. * sca3000_reg_lock_on() - test if the ctrl register lock is on
  272. * @st: Driver specific device instance data.
  273. *
  274. * Lock must be held.
  275. **/
  276. static int sca3000_reg_lock_on(struct sca3000_state *st)
  277. {
  278. int ret;
  279. ret = sca3000_read_data_short(st, SCA3000_REG_STATUS_ADDR, 1);
  280. if (ret < 0)
  281. return ret;
  282. return !(st->rx[0] & SCA3000_LOCKED);
  283. }
  284. /**
  285. * __sca3000_unlock_reg_lock() - unlock the control registers
  286. * @st: Driver specific device instance data.
  287. *
  288. * Note the device does not appear to support doing this in a single transfer.
  289. * This should only ever be used as part of ctrl reg read.
  290. * Lock must be held before calling this
  291. */
  292. static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
  293. {
  294. struct spi_transfer xfer[3] = {
  295. {
  296. .len = 2,
  297. .cs_change = 1,
  298. .tx_buf = st->tx,
  299. }, {
  300. .len = 2,
  301. .cs_change = 1,
  302. .tx_buf = st->tx + 2,
  303. }, {
  304. .len = 2,
  305. .tx_buf = st->tx + 4,
  306. },
  307. };
  308. st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR);
  309. st->tx[1] = 0x00;
  310. st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR);
  311. st->tx[3] = 0x50;
  312. st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR);
  313. st->tx[5] = 0xA0;
  314. return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
  315. }
  316. /**
  317. * sca3000_write_ctrl_reg() - write to a lock protect ctrl register
  318. * @st: Driver specific device instance data.
  319. * @sel: selects which registers we wish to write to
  320. * @val: the value to be written
  321. *
  322. * Certain control registers are protected against overwriting by the lock
  323. * register and use a shared write address. This function allows writing of
  324. * these registers.
  325. * Lock must be held.
  326. */
  327. static int sca3000_write_ctrl_reg(struct sca3000_state *st,
  328. u8 sel,
  329. uint8_t val)
  330. {
  331. int ret;
  332. ret = sca3000_reg_lock_on(st);
  333. if (ret < 0)
  334. return ret;
  335. if (ret) {
  336. ret = __sca3000_unlock_reg_lock(st);
  337. if (ret)
  338. return ret;
  339. }
  340. /* Set the control select register */
  341. ret = sca3000_write_reg(st, SCA3000_REG_CTRL_SEL_ADDR, sel);
  342. if (ret)
  343. return ret;
  344. /* Write the actual value into the register */
  345. return sca3000_write_reg(st, SCA3000_REG_CTRL_DATA_ADDR, val);
  346. }
  347. /**
  348. * sca3000_read_ctrl_reg() - read from lock protected control register.
  349. * @st: Driver specific device instance data.
  350. * @ctrl_reg: Which ctrl register do we want to read.
  351. *
  352. * Lock must be held.
  353. */
  354. static int sca3000_read_ctrl_reg(struct sca3000_state *st,
  355. u8 ctrl_reg)
  356. {
  357. int ret;
  358. ret = sca3000_reg_lock_on(st);
  359. if (ret < 0)
  360. return ret;
  361. if (ret) {
  362. ret = __sca3000_unlock_reg_lock(st);
  363. if (ret)
  364. return ret;
  365. }
  366. /* Set the control select register */
  367. ret = sca3000_write_reg(st, SCA3000_REG_CTRL_SEL_ADDR, ctrl_reg);
  368. if (ret)
  369. return ret;
  370. ret = sca3000_read_data_short(st, SCA3000_REG_CTRL_DATA_ADDR, 1);
  371. if (ret)
  372. return ret;
  373. return st->rx[0];
  374. }
  375. /**
  376. * sca3000_print_rev() - sysfs interface to read the chip revision number
  377. * @indio_dev: Device instance specific generic IIO data.
  378. * Driver specific device instance data can be obtained via
  379. * iio_priv(indio_dev)
  380. */
  381. static int sca3000_print_rev(struct iio_dev *indio_dev)
  382. {
  383. int ret;
  384. struct sca3000_state *st = iio_priv(indio_dev);
  385. mutex_lock(&st->lock);
  386. ret = sca3000_read_data_short(st, SCA3000_REG_REVID_ADDR, 1);
  387. if (ret < 0)
  388. goto error_ret;
  389. dev_info(&indio_dev->dev,
  390. "sca3000 revision major=%lu, minor=%lu\n",
  391. st->rx[0] & SCA3000_REG_REVID_MAJOR_MASK,
  392. st->rx[0] & SCA3000_REG_REVID_MINOR_MASK);
  393. error_ret:
  394. mutex_unlock(&st->lock);
  395. return ret;
  396. }
  397. static ssize_t
  398. sca3000_show_available_3db_freqs(struct device *dev,
  399. struct device_attribute *attr,
  400. char *buf)
  401. {
  402. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  403. struct sca3000_state *st = iio_priv(indio_dev);
  404. int len;
  405. len = sprintf(buf, "%d", st->info->measurement_mode_3db_freq);
  406. if (st->info->option_mode_1)
  407. len += sprintf(buf + len, " %d",
  408. st->info->option_mode_1_3db_freq);
  409. if (st->info->option_mode_2)
  410. len += sprintf(buf + len, " %d",
  411. st->info->option_mode_2_3db_freq);
  412. len += sprintf(buf + len, "\n");
  413. return len;
  414. }
  415. static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
  416. S_IRUGO, sca3000_show_available_3db_freqs,
  417. NULL, 0);
  418. static const struct iio_event_spec sca3000_event = {
  419. .type = IIO_EV_TYPE_MAG,
  420. .dir = IIO_EV_DIR_RISING,
  421. .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
  422. };
  423. /*
  424. * Note the hack in the number of bits to pretend we have 2 more than
  425. * we do in the fifo.
  426. */
  427. #define SCA3000_CHAN(index, mod) \
  428. { \
  429. .type = IIO_ACCEL, \
  430. .modified = 1, \
  431. .channel2 = mod, \
  432. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  433. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |\
  434. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),\
  435. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  436. .address = index, \
  437. .scan_index = index, \
  438. .scan_type = { \
  439. .sign = 's', \
  440. .realbits = 13, \
  441. .storagebits = 16, \
  442. .shift = 3, \
  443. .endianness = IIO_BE, \
  444. }, \
  445. .event_spec = &sca3000_event, \
  446. .num_event_specs = 1, \
  447. }
  448. static const struct iio_event_spec sca3000_freefall_event_spec = {
  449. .type = IIO_EV_TYPE_MAG,
  450. .dir = IIO_EV_DIR_FALLING,
  451. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  452. BIT(IIO_EV_INFO_PERIOD),
  453. };
  454. static const struct iio_chan_spec sca3000_channels[] = {
  455. SCA3000_CHAN(0, IIO_MOD_X),
  456. SCA3000_CHAN(1, IIO_MOD_Y),
  457. SCA3000_CHAN(2, IIO_MOD_Z),
  458. {
  459. .type = IIO_ACCEL,
  460. .modified = 1,
  461. .channel2 = IIO_MOD_X_AND_Y_AND_Z,
  462. .scan_index = -1, /* Fake channel */
  463. .event_spec = &sca3000_freefall_event_spec,
  464. .num_event_specs = 1,
  465. },
  466. };
  467. static const struct iio_chan_spec sca3000_channels_with_temp[] = {
  468. SCA3000_CHAN(0, IIO_MOD_X),
  469. SCA3000_CHAN(1, IIO_MOD_Y),
  470. SCA3000_CHAN(2, IIO_MOD_Z),
  471. {
  472. .type = IIO_TEMP,
  473. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  474. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
  475. BIT(IIO_CHAN_INFO_OFFSET),
  476. /* No buffer support */
  477. .scan_index = -1,
  478. .scan_type = {
  479. .sign = 'u',
  480. .realbits = 9,
  481. .storagebits = 16,
  482. .shift = 5,
  483. .endianness = IIO_BE,
  484. },
  485. },
  486. {
  487. .type = IIO_ACCEL,
  488. .modified = 1,
  489. .channel2 = IIO_MOD_X_AND_Y_AND_Z,
  490. .scan_index = -1, /* Fake channel */
  491. .event_spec = &sca3000_freefall_event_spec,
  492. .num_event_specs = 1,
  493. },
  494. };
  495. static u8 sca3000_addresses[3][3] = {
  496. [0] = {SCA3000_REG_X_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_X_TH,
  497. SCA3000_MD_CTRL_OR_X},
  498. [1] = {SCA3000_REG_Y_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_Y_TH,
  499. SCA3000_MD_CTRL_OR_Y},
  500. [2] = {SCA3000_REG_Z_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_Z_TH,
  501. SCA3000_MD_CTRL_OR_Z},
  502. };
  503. /**
  504. * __sca3000_get_base_freq() - obtain mode specific base frequency
  505. * @st: Private driver specific device instance specific state.
  506. * @info: chip type specific information.
  507. * @base_freq: Base frequency for the current measurement mode.
  508. *
  509. * lock must be held
  510. */
  511. static inline int __sca3000_get_base_freq(struct sca3000_state *st,
  512. const struct sca3000_chip_info *info,
  513. int *base_freq)
  514. {
  515. int ret;
  516. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  517. if (ret)
  518. return ret;
  519. switch (SCA3000_REG_MODE_MODE_MASK & st->rx[0]) {
  520. case SCA3000_REG_MODE_MEAS_MODE_NORMAL:
  521. *base_freq = info->measurement_mode_freq;
  522. break;
  523. case SCA3000_REG_MODE_MEAS_MODE_OP_1:
  524. *base_freq = info->option_mode_1_freq;
  525. break;
  526. case SCA3000_REG_MODE_MEAS_MODE_OP_2:
  527. *base_freq = info->option_mode_2_freq;
  528. break;
  529. default:
  530. ret = -EINVAL;
  531. }
  532. return ret;
  533. }
  534. /**
  535. * sca3000_read_raw_samp_freq() - read_raw handler for IIO_CHAN_INFO_SAMP_FREQ
  536. * @st: Private driver specific device instance specific state.
  537. * @val: The frequency read back.
  538. *
  539. * lock must be held
  540. **/
  541. static int sca3000_read_raw_samp_freq(struct sca3000_state *st, int *val)
  542. {
  543. int ret;
  544. ret = __sca3000_get_base_freq(st, st->info, val);
  545. if (ret)
  546. return ret;
  547. ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
  548. if (ret < 0)
  549. return ret;
  550. if (*val > 0) {
  551. ret &= SCA3000_REG_OUT_CTRL_BUF_DIV_MASK;
  552. switch (ret) {
  553. case SCA3000_REG_OUT_CTRL_BUF_DIV_2:
  554. *val /= 2;
  555. break;
  556. case SCA3000_REG_OUT_CTRL_BUF_DIV_4:
  557. *val /= 4;
  558. break;
  559. }
  560. }
  561. return 0;
  562. }
  563. /**
  564. * sca3000_write_raw_samp_freq() - write_raw handler for IIO_CHAN_INFO_SAMP_FREQ
  565. * @st: Private driver specific device instance specific state.
  566. * @val: The frequency desired.
  567. *
  568. * lock must be held
  569. */
  570. static int sca3000_write_raw_samp_freq(struct sca3000_state *st, int val)
  571. {
  572. int ret, base_freq, ctrlval;
  573. ret = __sca3000_get_base_freq(st, st->info, &base_freq);
  574. if (ret)
  575. return ret;
  576. ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
  577. if (ret < 0)
  578. return ret;
  579. ctrlval = ret & ~SCA3000_REG_OUT_CTRL_BUF_DIV_MASK;
  580. if (val == base_freq / 2)
  581. ctrlval |= SCA3000_REG_OUT_CTRL_BUF_DIV_2;
  582. if (val == base_freq / 4)
  583. ctrlval |= SCA3000_REG_OUT_CTRL_BUF_DIV_4;
  584. else if (val != base_freq)
  585. return -EINVAL;
  586. return sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
  587. ctrlval);
  588. }
  589. static int sca3000_read_3db_freq(struct sca3000_state *st, int *val)
  590. {
  591. int ret;
  592. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  593. if (ret)
  594. return ret;
  595. /* mask bottom 2 bits - only ones that are relevant */
  596. st->rx[0] &= SCA3000_REG_MODE_MODE_MASK;
  597. switch (st->rx[0]) {
  598. case SCA3000_REG_MODE_MEAS_MODE_NORMAL:
  599. *val = st->info->measurement_mode_3db_freq;
  600. return IIO_VAL_INT;
  601. case SCA3000_REG_MODE_MEAS_MODE_MOT_DET:
  602. return -EBUSY;
  603. case SCA3000_REG_MODE_MEAS_MODE_OP_1:
  604. *val = st->info->option_mode_1_3db_freq;
  605. return IIO_VAL_INT;
  606. case SCA3000_REG_MODE_MEAS_MODE_OP_2:
  607. *val = st->info->option_mode_2_3db_freq;
  608. return IIO_VAL_INT;
  609. default:
  610. return -EINVAL;
  611. }
  612. }
  613. static int sca3000_write_3db_freq(struct sca3000_state *st, int val)
  614. {
  615. int ret;
  616. int mode;
  617. if (val == st->info->measurement_mode_3db_freq)
  618. mode = SCA3000_REG_MODE_MEAS_MODE_NORMAL;
  619. else if (st->info->option_mode_1 &&
  620. (val == st->info->option_mode_1_3db_freq))
  621. mode = SCA3000_REG_MODE_MEAS_MODE_OP_1;
  622. else if (st->info->option_mode_2 &&
  623. (val == st->info->option_mode_2_3db_freq))
  624. mode = SCA3000_REG_MODE_MEAS_MODE_OP_2;
  625. else
  626. return -EINVAL;
  627. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  628. if (ret)
  629. return ret;
  630. st->rx[0] &= ~SCA3000_REG_MODE_MODE_MASK;
  631. st->rx[0] |= (mode & SCA3000_REG_MODE_MODE_MASK);
  632. return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, st->rx[0]);
  633. }
  634. static int sca3000_read_raw(struct iio_dev *indio_dev,
  635. struct iio_chan_spec const *chan,
  636. int *val,
  637. int *val2,
  638. long mask)
  639. {
  640. struct sca3000_state *st = iio_priv(indio_dev);
  641. int ret;
  642. u8 address;
  643. switch (mask) {
  644. case IIO_CHAN_INFO_RAW:
  645. mutex_lock(&st->lock);
  646. if (chan->type == IIO_ACCEL) {
  647. if (st->mo_det_use_count) {
  648. mutex_unlock(&st->lock);
  649. return -EBUSY;
  650. }
  651. address = sca3000_addresses[chan->address][0];
  652. ret = sca3000_read_data_short(st, address, 2);
  653. if (ret < 0) {
  654. mutex_unlock(&st->lock);
  655. return ret;
  656. }
  657. *val = sign_extend32(be16_to_cpup((__be16 *)st->rx) >>
  658. chan->scan_type.shift,
  659. chan->scan_type.realbits - 1);
  660. } else {
  661. /* get the temperature when available */
  662. ret = sca3000_read_data_short(st,
  663. SCA3000_REG_TEMP_MSB_ADDR,
  664. 2);
  665. if (ret < 0) {
  666. mutex_unlock(&st->lock);
  667. return ret;
  668. }
  669. *val = (be16_to_cpup((__be16 *)st->rx) >>
  670. chan->scan_type.shift) &
  671. GENMASK(chan->scan_type.realbits - 1, 0);
  672. }
  673. mutex_unlock(&st->lock);
  674. return IIO_VAL_INT;
  675. case IIO_CHAN_INFO_SCALE:
  676. *val = 0;
  677. if (chan->type == IIO_ACCEL)
  678. *val2 = st->info->scale;
  679. else /* temperature */
  680. *val2 = 555556;
  681. return IIO_VAL_INT_PLUS_MICRO;
  682. case IIO_CHAN_INFO_OFFSET:
  683. *val = -214;
  684. *val2 = 600000;
  685. return IIO_VAL_INT_PLUS_MICRO;
  686. case IIO_CHAN_INFO_SAMP_FREQ:
  687. mutex_lock(&st->lock);
  688. ret = sca3000_read_raw_samp_freq(st, val);
  689. mutex_unlock(&st->lock);
  690. return ret ? ret : IIO_VAL_INT;
  691. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  692. mutex_lock(&st->lock);
  693. ret = sca3000_read_3db_freq(st, val);
  694. mutex_unlock(&st->lock);
  695. return ret;
  696. default:
  697. return -EINVAL;
  698. }
  699. }
  700. static int sca3000_write_raw(struct iio_dev *indio_dev,
  701. struct iio_chan_spec const *chan,
  702. int val, int val2, long mask)
  703. {
  704. struct sca3000_state *st = iio_priv(indio_dev);
  705. int ret;
  706. switch (mask) {
  707. case IIO_CHAN_INFO_SAMP_FREQ:
  708. if (val2)
  709. return -EINVAL;
  710. mutex_lock(&st->lock);
  711. ret = sca3000_write_raw_samp_freq(st, val);
  712. mutex_unlock(&st->lock);
  713. return ret;
  714. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  715. if (val2)
  716. return -EINVAL;
  717. mutex_lock(&st->lock);
  718. ret = sca3000_write_3db_freq(st, val);
  719. mutex_unlock(&st->lock);
  720. return ret;
  721. default:
  722. return -EINVAL;
  723. }
  724. return ret;
  725. }
  726. /**
  727. * sca3000_read_av_freq() - sysfs function to get available frequencies
  728. * @dev: Device structure for this device.
  729. * @attr: Description of the attribute.
  730. * @buf: Incoming string
  731. *
  732. * The later modes are only relevant to the ring buffer - and depend on current
  733. * mode. Note that data sheet gives rather wide tolerances for these so integer
  734. * division will give good enough answer and not all chips have them specified
  735. * at all.
  736. **/
  737. static ssize_t sca3000_read_av_freq(struct device *dev,
  738. struct device_attribute *attr,
  739. char *buf)
  740. {
  741. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  742. struct sca3000_state *st = iio_priv(indio_dev);
  743. int len = 0, ret, val;
  744. mutex_lock(&st->lock);
  745. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  746. val = st->rx[0];
  747. mutex_unlock(&st->lock);
  748. if (ret)
  749. return ret;
  750. switch (val & SCA3000_REG_MODE_MODE_MASK) {
  751. case SCA3000_REG_MODE_MEAS_MODE_NORMAL:
  752. len += sprintf(buf + len, "%d %d %d\n",
  753. st->info->measurement_mode_freq,
  754. st->info->measurement_mode_freq / 2,
  755. st->info->measurement_mode_freq / 4);
  756. break;
  757. case SCA3000_REG_MODE_MEAS_MODE_OP_1:
  758. len += sprintf(buf + len, "%d %d %d\n",
  759. st->info->option_mode_1_freq,
  760. st->info->option_mode_1_freq / 2,
  761. st->info->option_mode_1_freq / 4);
  762. break;
  763. case SCA3000_REG_MODE_MEAS_MODE_OP_2:
  764. len += sprintf(buf + len, "%d %d %d\n",
  765. st->info->option_mode_2_freq,
  766. st->info->option_mode_2_freq / 2,
  767. st->info->option_mode_2_freq / 4);
  768. break;
  769. }
  770. return len;
  771. }
  772. /*
  773. * Should only really be registered if ring buffer support is compiled in.
  774. * Does no harm however and doing it right would add a fair bit of complexity
  775. */
  776. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
  777. /*
  778. * sca3000_read_event_value() - query of a threshold or period
  779. */
  780. static int sca3000_read_event_value(struct iio_dev *indio_dev,
  781. const struct iio_chan_spec *chan,
  782. enum iio_event_type type,
  783. enum iio_event_direction dir,
  784. enum iio_event_info info,
  785. int *val, int *val2)
  786. {
  787. struct sca3000_state *st = iio_priv(indio_dev);
  788. long ret;
  789. int i;
  790. switch (info) {
  791. case IIO_EV_INFO_VALUE:
  792. mutex_lock(&st->lock);
  793. ret = sca3000_read_ctrl_reg(st,
  794. sca3000_addresses[chan->address][1]);
  795. mutex_unlock(&st->lock);
  796. if (ret < 0)
  797. return ret;
  798. *val = 0;
  799. if (chan->channel2 == IIO_MOD_Y)
  800. for_each_set_bit(i, &ret,
  801. ARRAY_SIZE(st->info->mot_det_mult_y))
  802. *val += st->info->mot_det_mult_y[i];
  803. else
  804. for_each_set_bit(i, &ret,
  805. ARRAY_SIZE(st->info->mot_det_mult_xz))
  806. *val += st->info->mot_det_mult_xz[i];
  807. return IIO_VAL_INT;
  808. case IIO_EV_INFO_PERIOD:
  809. *val = 0;
  810. *val2 = 226000;
  811. return IIO_VAL_INT_PLUS_MICRO;
  812. default:
  813. return -EINVAL;
  814. }
  815. }
  816. /**
  817. * sca3000_write_event_value() - control of threshold and period
  818. * @indio_dev: Device instance specific IIO information.
  819. * @chan: Description of the channel for which the event is being
  820. * configured.
  821. * @type: The type of event being configured, here magnitude rising
  822. * as everything else is read only.
  823. * @dir: Direction of the event (here rising)
  824. * @info: What information about the event are we configuring.
  825. * Here the threshold only.
  826. * @val: Integer part of the value being written..
  827. * @val2: Non integer part of the value being written. Here always 0.
  828. */
  829. static int sca3000_write_event_value(struct iio_dev *indio_dev,
  830. const struct iio_chan_spec *chan,
  831. enum iio_event_type type,
  832. enum iio_event_direction dir,
  833. enum iio_event_info info,
  834. int val, int val2)
  835. {
  836. struct sca3000_state *st = iio_priv(indio_dev);
  837. int ret;
  838. int i;
  839. u8 nonlinear = 0;
  840. if (chan->channel2 == IIO_MOD_Y) {
  841. i = ARRAY_SIZE(st->info->mot_det_mult_y);
  842. while (i > 0)
  843. if (val >= st->info->mot_det_mult_y[--i]) {
  844. nonlinear |= (1 << i);
  845. val -= st->info->mot_det_mult_y[i];
  846. }
  847. } else {
  848. i = ARRAY_SIZE(st->info->mot_det_mult_xz);
  849. while (i > 0)
  850. if (val >= st->info->mot_det_mult_xz[--i]) {
  851. nonlinear |= (1 << i);
  852. val -= st->info->mot_det_mult_xz[i];
  853. }
  854. }
  855. mutex_lock(&st->lock);
  856. ret = sca3000_write_ctrl_reg(st,
  857. sca3000_addresses[chan->address][1],
  858. nonlinear);
  859. mutex_unlock(&st->lock);
  860. return ret;
  861. }
  862. static struct attribute *sca3000_attributes[] = {
  863. &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr,
  864. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  865. NULL,
  866. };
  867. static const struct attribute_group sca3000_attribute_group = {
  868. .attrs = sca3000_attributes,
  869. };
  870. static int sca3000_read_data(struct sca3000_state *st,
  871. u8 reg_address_high,
  872. u8 *rx,
  873. int len)
  874. {
  875. int ret;
  876. struct spi_transfer xfer[2] = {
  877. {
  878. .len = 1,
  879. .tx_buf = st->tx,
  880. }, {
  881. .len = len,
  882. .rx_buf = rx,
  883. }
  884. };
  885. st->tx[0] = SCA3000_READ_REG(reg_address_high);
  886. ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
  887. if (ret) {
  888. dev_err(&st->us->dev, "problem reading register\n");
  889. return ret;
  890. }
  891. return 0;
  892. }
  893. /**
  894. * sca3000_ring_int_process() - ring specific interrupt handling.
  895. * @val: Value of the interrupt status register.
  896. * @indio_dev: Device instance specific IIO device structure.
  897. */
  898. static void sca3000_ring_int_process(u8 val, struct iio_dev *indio_dev)
  899. {
  900. struct sca3000_state *st = iio_priv(indio_dev);
  901. int ret, i, num_available;
  902. mutex_lock(&st->lock);
  903. if (val & SCA3000_REG_INT_STATUS_HALF) {
  904. ret = sca3000_read_data_short(st, SCA3000_REG_BUF_COUNT_ADDR,
  905. 1);
  906. if (ret)
  907. goto error_ret;
  908. num_available = st->rx[0];
  909. /*
  910. * num_available is the total number of samples available
  911. * i.e. number of time points * number of channels.
  912. */
  913. ret = sca3000_read_data(st, SCA3000_REG_RING_OUT_ADDR, st->rx,
  914. num_available * 2);
  915. if (ret)
  916. goto error_ret;
  917. for (i = 0; i < num_available / 3; i++) {
  918. /*
  919. * Dirty hack to cover for 11 bit in fifo, 13 bit
  920. * direct reading.
  921. *
  922. * In theory the bottom two bits are undefined.
  923. * In reality they appear to always be 0.
  924. */
  925. iio_push_to_buffers(indio_dev, st->rx + i * 3 * 2);
  926. }
  927. }
  928. error_ret:
  929. mutex_unlock(&st->lock);
  930. }
  931. /**
  932. * sca3000_event_handler() - handling ring and non ring events
  933. * @irq: The irq being handled.
  934. * @private: struct iio_device pointer for the device.
  935. *
  936. * Ring related interrupt handler. Depending on event, push to
  937. * the ring buffer event chrdev or the event one.
  938. *
  939. * This function is complicated by the fact that the devices can signify ring
  940. * and non ring events via the same interrupt line and they can only
  941. * be distinguished via a read of the relevant status register.
  942. */
  943. static irqreturn_t sca3000_event_handler(int irq, void *private)
  944. {
  945. struct iio_dev *indio_dev = private;
  946. struct sca3000_state *st = iio_priv(indio_dev);
  947. int ret, val;
  948. s64 last_timestamp = iio_get_time_ns(indio_dev);
  949. /*
  950. * Could lead if badly timed to an extra read of status reg,
  951. * but ensures no interrupt is missed.
  952. */
  953. mutex_lock(&st->lock);
  954. ret = sca3000_read_data_short(st, SCA3000_REG_INT_STATUS_ADDR, 1);
  955. val = st->rx[0];
  956. mutex_unlock(&st->lock);
  957. if (ret)
  958. goto done;
  959. sca3000_ring_int_process(val, indio_dev);
  960. if (val & SCA3000_INT_STATUS_FREE_FALL)
  961. iio_push_event(indio_dev,
  962. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  963. 0,
  964. IIO_MOD_X_AND_Y_AND_Z,
  965. IIO_EV_TYPE_MAG,
  966. IIO_EV_DIR_FALLING),
  967. last_timestamp);
  968. if (val & SCA3000_INT_STATUS_Y_TRIGGER)
  969. iio_push_event(indio_dev,
  970. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  971. 0,
  972. IIO_MOD_Y,
  973. IIO_EV_TYPE_MAG,
  974. IIO_EV_DIR_RISING),
  975. last_timestamp);
  976. if (val & SCA3000_INT_STATUS_X_TRIGGER)
  977. iio_push_event(indio_dev,
  978. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  979. 0,
  980. IIO_MOD_X,
  981. IIO_EV_TYPE_MAG,
  982. IIO_EV_DIR_RISING),
  983. last_timestamp);
  984. if (val & SCA3000_INT_STATUS_Z_TRIGGER)
  985. iio_push_event(indio_dev,
  986. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  987. 0,
  988. IIO_MOD_Z,
  989. IIO_EV_TYPE_MAG,
  990. IIO_EV_DIR_RISING),
  991. last_timestamp);
  992. done:
  993. return IRQ_HANDLED;
  994. }
  995. /*
  996. * sca3000_read_event_config() what events are enabled
  997. */
  998. static int sca3000_read_event_config(struct iio_dev *indio_dev,
  999. const struct iio_chan_spec *chan,
  1000. enum iio_event_type type,
  1001. enum iio_event_direction dir)
  1002. {
  1003. struct sca3000_state *st = iio_priv(indio_dev);
  1004. int ret;
  1005. /* read current value of mode register */
  1006. mutex_lock(&st->lock);
  1007. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  1008. if (ret)
  1009. goto error_ret;
  1010. switch (chan->channel2) {
  1011. case IIO_MOD_X_AND_Y_AND_Z:
  1012. ret = !!(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT);
  1013. break;
  1014. case IIO_MOD_X:
  1015. case IIO_MOD_Y:
  1016. case IIO_MOD_Z:
  1017. /*
  1018. * Motion detection mode cannot run at the same time as
  1019. * acceleration data being read.
  1020. */
  1021. if ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK)
  1022. != SCA3000_REG_MODE_MEAS_MODE_MOT_DET) {
  1023. ret = 0;
  1024. } else {
  1025. ret = sca3000_read_ctrl_reg(st,
  1026. SCA3000_REG_CTRL_SEL_MD_CTRL);
  1027. if (ret < 0)
  1028. goto error_ret;
  1029. /* only supporting logical or's for now */
  1030. ret = !!(ret & sca3000_addresses[chan->address][2]);
  1031. }
  1032. break;
  1033. default:
  1034. ret = -EINVAL;
  1035. }
  1036. error_ret:
  1037. mutex_unlock(&st->lock);
  1038. return ret;
  1039. }
  1040. static int sca3000_freefall_set_state(struct iio_dev *indio_dev, bool state)
  1041. {
  1042. struct sca3000_state *st = iio_priv(indio_dev);
  1043. int ret;
  1044. /* read current value of mode register */
  1045. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  1046. if (ret)
  1047. return ret;
  1048. /* if off and should be on */
  1049. if (state && !(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT))
  1050. return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
  1051. st->rx[0] | SCA3000_REG_MODE_FREE_FALL_DETECT);
  1052. /* if on and should be off */
  1053. else if (!state && (st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT))
  1054. return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
  1055. st->rx[0] & ~SCA3000_REG_MODE_FREE_FALL_DETECT);
  1056. else
  1057. return 0;
  1058. }
  1059. static int sca3000_motion_detect_set_state(struct iio_dev *indio_dev, int axis,
  1060. bool state)
  1061. {
  1062. struct sca3000_state *st = iio_priv(indio_dev);
  1063. int ret, ctrlval;
  1064. /*
  1065. * First read the motion detector config to find out if
  1066. * this axis is on
  1067. */
  1068. ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
  1069. if (ret < 0)
  1070. return ret;
  1071. ctrlval = ret;
  1072. /* if off and should be on */
  1073. if (state && !(ctrlval & sca3000_addresses[axis][2])) {
  1074. ret = sca3000_write_ctrl_reg(st,
  1075. SCA3000_REG_CTRL_SEL_MD_CTRL,
  1076. ctrlval |
  1077. sca3000_addresses[axis][2]);
  1078. if (ret)
  1079. return ret;
  1080. st->mo_det_use_count++;
  1081. } else if (!state && (ctrlval & sca3000_addresses[axis][2])) {
  1082. ret = sca3000_write_ctrl_reg(st,
  1083. SCA3000_REG_CTRL_SEL_MD_CTRL,
  1084. ctrlval &
  1085. ~(sca3000_addresses[axis][2]));
  1086. if (ret)
  1087. return ret;
  1088. st->mo_det_use_count--;
  1089. }
  1090. /* read current value of mode register */
  1091. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  1092. if (ret)
  1093. return ret;
  1094. /* if off and should be on */
  1095. if ((st->mo_det_use_count) &&
  1096. ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK)
  1097. != SCA3000_REG_MODE_MEAS_MODE_MOT_DET))
  1098. return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
  1099. (st->rx[0] & ~SCA3000_REG_MODE_MODE_MASK)
  1100. | SCA3000_REG_MODE_MEAS_MODE_MOT_DET);
  1101. /* if on and should be off */
  1102. else if (!(st->mo_det_use_count) &&
  1103. ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK)
  1104. == SCA3000_REG_MODE_MEAS_MODE_MOT_DET))
  1105. return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
  1106. st->rx[0] & SCA3000_REG_MODE_MODE_MASK);
  1107. else
  1108. return 0;
  1109. }
  1110. /**
  1111. * sca3000_write_event_config() - simple on off control for motion detector
  1112. * @indio_dev: IIO device instance specific structure. Data specific to this
  1113. * particular driver may be accessed via iio_priv(indio_dev).
  1114. * @chan: Description of the channel whose event we are configuring.
  1115. * @type: The type of event.
  1116. * @dir: The direction of the event.
  1117. * @state: Desired state of event being configured.
  1118. *
  1119. * This is a per axis control, but enabling any will result in the
  1120. * motion detector unit being enabled.
  1121. * N.B. enabling motion detector stops normal data acquisition.
  1122. * There is a complexity in knowing which mode to return to when
  1123. * this mode is disabled. Currently normal mode is assumed.
  1124. **/
  1125. static int sca3000_write_event_config(struct iio_dev *indio_dev,
  1126. const struct iio_chan_spec *chan,
  1127. enum iio_event_type type,
  1128. enum iio_event_direction dir,
  1129. bool state)
  1130. {
  1131. struct sca3000_state *st = iio_priv(indio_dev);
  1132. int ret;
  1133. mutex_lock(&st->lock);
  1134. switch (chan->channel2) {
  1135. case IIO_MOD_X_AND_Y_AND_Z:
  1136. ret = sca3000_freefall_set_state(indio_dev, state);
  1137. break;
  1138. case IIO_MOD_X:
  1139. case IIO_MOD_Y:
  1140. case IIO_MOD_Z:
  1141. ret = sca3000_motion_detect_set_state(indio_dev,
  1142. chan->address,
  1143. state);
  1144. break;
  1145. default:
  1146. ret = -EINVAL;
  1147. break;
  1148. }
  1149. mutex_unlock(&st->lock);
  1150. return ret;
  1151. }
  1152. static inline
  1153. int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
  1154. {
  1155. struct sca3000_state *st = iio_priv(indio_dev);
  1156. int ret;
  1157. mutex_lock(&st->lock);
  1158. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  1159. if (ret)
  1160. goto error_ret;
  1161. if (state) {
  1162. dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n");
  1163. ret = sca3000_write_reg(st,
  1164. SCA3000_REG_MODE_ADDR,
  1165. (st->rx[0] | SCA3000_REG_MODE_RING_BUF_ENABLE));
  1166. } else
  1167. ret = sca3000_write_reg(st,
  1168. SCA3000_REG_MODE_ADDR,
  1169. (st->rx[0] & ~SCA3000_REG_MODE_RING_BUF_ENABLE));
  1170. error_ret:
  1171. mutex_unlock(&st->lock);
  1172. return ret;
  1173. }
  1174. /**
  1175. * sca3000_hw_ring_preenable() - hw ring buffer preenable function
  1176. * @indio_dev: structure representing the IIO device. Device instance
  1177. * specific state can be accessed via iio_priv(indio_dev).
  1178. *
  1179. * Very simple enable function as the chip will allows normal reads
  1180. * during ring buffer operation so as long as it is indeed running
  1181. * before we notify the core, the precise ordering does not matter.
  1182. */
  1183. static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
  1184. {
  1185. int ret;
  1186. struct sca3000_state *st = iio_priv(indio_dev);
  1187. mutex_lock(&st->lock);
  1188. /* Enable the 50% full interrupt */
  1189. ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
  1190. if (ret)
  1191. goto error_unlock;
  1192. ret = sca3000_write_reg(st,
  1193. SCA3000_REG_INT_MASK_ADDR,
  1194. st->rx[0] | SCA3000_REG_INT_MASK_RING_HALF);
  1195. if (ret)
  1196. goto error_unlock;
  1197. mutex_unlock(&st->lock);
  1198. return __sca3000_hw_ring_state_set(indio_dev, 1);
  1199. error_unlock:
  1200. mutex_unlock(&st->lock);
  1201. return ret;
  1202. }
  1203. static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
  1204. {
  1205. int ret;
  1206. struct sca3000_state *st = iio_priv(indio_dev);
  1207. ret = __sca3000_hw_ring_state_set(indio_dev, 0);
  1208. if (ret)
  1209. return ret;
  1210. /* Disable the 50% full interrupt */
  1211. mutex_lock(&st->lock);
  1212. ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
  1213. if (ret)
  1214. goto unlock;
  1215. ret = sca3000_write_reg(st,
  1216. SCA3000_REG_INT_MASK_ADDR,
  1217. st->rx[0] & ~SCA3000_REG_INT_MASK_RING_HALF);
  1218. unlock:
  1219. mutex_unlock(&st->lock);
  1220. return ret;
  1221. }
  1222. static const struct iio_buffer_setup_ops sca3000_ring_setup_ops = {
  1223. .preenable = &sca3000_hw_ring_preenable,
  1224. .postdisable = &sca3000_hw_ring_postdisable,
  1225. };
  1226. /**
  1227. * sca3000_clean_setup() - get the device into a predictable state
  1228. * @st: Device instance specific private data structure
  1229. *
  1230. * Devices use flash memory to store many of the register values
  1231. * and hence can come up in somewhat unpredictable states.
  1232. * Hence reset everything on driver load.
  1233. */
  1234. static int sca3000_clean_setup(struct sca3000_state *st)
  1235. {
  1236. int ret;
  1237. mutex_lock(&st->lock);
  1238. /* Ensure all interrupts have been acknowledged */
  1239. ret = sca3000_read_data_short(st, SCA3000_REG_INT_STATUS_ADDR, 1);
  1240. if (ret)
  1241. goto error_ret;
  1242. /* Turn off all motion detection channels */
  1243. ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
  1244. if (ret < 0)
  1245. goto error_ret;
  1246. ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
  1247. ret & SCA3000_MD_CTRL_PROT_MASK);
  1248. if (ret)
  1249. goto error_ret;
  1250. /* Disable ring buffer */
  1251. ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
  1252. if (ret < 0)
  1253. goto error_ret;
  1254. ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
  1255. (ret & SCA3000_REG_OUT_CTRL_PROT_MASK)
  1256. | SCA3000_REG_OUT_CTRL_BUF_X_EN
  1257. | SCA3000_REG_OUT_CTRL_BUF_Y_EN
  1258. | SCA3000_REG_OUT_CTRL_BUF_Z_EN
  1259. | SCA3000_REG_OUT_CTRL_BUF_DIV_4);
  1260. if (ret)
  1261. goto error_ret;
  1262. /* Enable interrupts, relevant to mode and set up as active low */
  1263. ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
  1264. if (ret)
  1265. goto error_ret;
  1266. ret = sca3000_write_reg(st,
  1267. SCA3000_REG_INT_MASK_ADDR,
  1268. (ret & SCA3000_REG_INT_MASK_PROT_MASK)
  1269. | SCA3000_REG_INT_MASK_ACTIVE_LOW);
  1270. if (ret)
  1271. goto error_ret;
  1272. /*
  1273. * Select normal measurement mode, free fall off, ring off
  1274. * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
  1275. * as that occurs in one of the example on the datasheet
  1276. */
  1277. ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
  1278. if (ret)
  1279. goto error_ret;
  1280. ret = sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
  1281. (st->rx[0] & SCA3000_MODE_PROT_MASK));
  1282. error_ret:
  1283. mutex_unlock(&st->lock);
  1284. return ret;
  1285. }
  1286. static const struct iio_info sca3000_info = {
  1287. .attrs = &sca3000_attribute_group,
  1288. .read_raw = &sca3000_read_raw,
  1289. .write_raw = &sca3000_write_raw,
  1290. .read_event_value = &sca3000_read_event_value,
  1291. .write_event_value = &sca3000_write_event_value,
  1292. .read_event_config = &sca3000_read_event_config,
  1293. .write_event_config = &sca3000_write_event_config,
  1294. };
  1295. static int sca3000_probe(struct spi_device *spi)
  1296. {
  1297. int ret;
  1298. struct sca3000_state *st;
  1299. struct iio_dev *indio_dev;
  1300. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  1301. if (!indio_dev)
  1302. return -ENOMEM;
  1303. st = iio_priv(indio_dev);
  1304. spi_set_drvdata(spi, indio_dev);
  1305. st->us = spi;
  1306. mutex_init(&st->lock);
  1307. st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
  1308. ->driver_data];
  1309. indio_dev->name = spi_get_device_id(spi)->name;
  1310. indio_dev->info = &sca3000_info;
  1311. if (st->info->temp_output) {
  1312. indio_dev->channels = sca3000_channels_with_temp;
  1313. indio_dev->num_channels =
  1314. ARRAY_SIZE(sca3000_channels_with_temp);
  1315. } else {
  1316. indio_dev->channels = sca3000_channels;
  1317. indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
  1318. }
  1319. indio_dev->modes = INDIO_DIRECT_MODE;
  1320. ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev,
  1321. &sca3000_ring_setup_ops);
  1322. if (ret)
  1323. return ret;
  1324. if (spi->irq) {
  1325. ret = request_threaded_irq(spi->irq,
  1326. NULL,
  1327. &sca3000_event_handler,
  1328. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1329. "sca3000",
  1330. indio_dev);
  1331. if (ret)
  1332. return ret;
  1333. }
  1334. ret = sca3000_clean_setup(st);
  1335. if (ret)
  1336. goto error_free_irq;
  1337. ret = sca3000_print_rev(indio_dev);
  1338. if (ret)
  1339. goto error_free_irq;
  1340. ret = iio_device_register(indio_dev);
  1341. if (ret)
  1342. goto error_free_irq;
  1343. return 0;
  1344. error_free_irq:
  1345. if (spi->irq)
  1346. free_irq(spi->irq, indio_dev);
  1347. return ret;
  1348. }
  1349. static int sca3000_stop_all_interrupts(struct sca3000_state *st)
  1350. {
  1351. int ret;
  1352. mutex_lock(&st->lock);
  1353. ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
  1354. if (ret)
  1355. goto error_ret;
  1356. ret = sca3000_write_reg(st, SCA3000_REG_INT_MASK_ADDR,
  1357. (st->rx[0] &
  1358. ~(SCA3000_REG_INT_MASK_RING_THREE_QUARTER |
  1359. SCA3000_REG_INT_MASK_RING_HALF |
  1360. SCA3000_REG_INT_MASK_ALL_INTS)));
  1361. error_ret:
  1362. mutex_unlock(&st->lock);
  1363. return ret;
  1364. }
  1365. static void sca3000_remove(struct spi_device *spi)
  1366. {
  1367. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  1368. struct sca3000_state *st = iio_priv(indio_dev);
  1369. iio_device_unregister(indio_dev);
  1370. /* Must ensure no interrupts can be generated after this! */
  1371. sca3000_stop_all_interrupts(st);
  1372. if (spi->irq)
  1373. free_irq(spi->irq, indio_dev);
  1374. }
  1375. static const struct spi_device_id sca3000_id[] = {
  1376. {"sca3000_d01", d01},
  1377. {"sca3000_e02", e02},
  1378. {"sca3000_e04", e04},
  1379. {"sca3000_e05", e05},
  1380. { }
  1381. };
  1382. MODULE_DEVICE_TABLE(spi, sca3000_id);
  1383. static struct spi_driver sca3000_driver = {
  1384. .driver = {
  1385. .name = "sca3000",
  1386. },
  1387. .probe = sca3000_probe,
  1388. .remove = sca3000_remove,
  1389. .id_table = sca3000_id,
  1390. };
  1391. module_spi_driver(sca3000_driver);
  1392. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1393. MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
  1394. MODULE_LICENSE("GPL v2");