sca3000.c 42 KB

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