zpa2326.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. /*
  2. * Murata ZPA2326 pressure and temperature sensor IIO driver
  3. *
  4. * Copyright (c) 2016 Parrot S.A.
  5. *
  6. * Author: Gregor Boirie <gregor.boirie@parrot.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. /**
  18. * DOC: ZPA2326 theory of operations
  19. *
  20. * This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO
  21. * modes.
  22. * A internal hardware trigger is also implemented to dispatch registered IIO
  23. * trigger consumers upon "sample ready" interrupts.
  24. *
  25. * ZPA2326 hardware supports 2 sampling mode: one shot and continuous.
  26. *
  27. * A complete one shot sampling cycle gets device out of low power mode,
  28. * performs pressure and temperature measurements, then automatically switches
  29. * back to low power mode. It is meant for on demand sampling with optimal power
  30. * saving at the cost of lower sampling rate and higher software overhead.
  31. * This is a natural candidate for IIO read_raw hook implementation
  32. * (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to
  33. * ensure explicit synchronization with external trigger events
  34. * (%INDIO_BUFFER_TRIGGERED).
  35. *
  36. * The continuous mode works according to a periodic hardware measurement
  37. * process continuously pushing samples into an internal hardware FIFO (for
  38. * pressure samples only). Measurement cycle completion may be signaled by a
  39. * "sample ready" interrupt.
  40. * Typical software sequence of operations :
  41. * - get device out of low power mode,
  42. * - setup hardware sampling period,
  43. * - at end of period, upon data ready interrupt: pop pressure samples out of
  44. * hardware FIFO and fetch temperature sample
  45. * - when no longer needed, stop sampling process by putting device into
  46. * low power mode.
  47. * This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree
  48. * declares a valid interrupt line. In this case, the internal hardware trigger
  49. * drives acquisition.
  50. *
  51. * Note that hardware sampling frequency is taken into account only when
  52. * internal hardware trigger is attached as the highest sampling rate seems to
  53. * be the most energy efficient.
  54. *
  55. * TODO:
  56. * preset pressure threshold crossing / IIO events ;
  57. * differential pressure sampling ;
  58. * hardware samples averaging.
  59. */
  60. #include <linux/module.h>
  61. #include <linux/kernel.h>
  62. #include <linux/delay.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/regulator/consumer.h>
  65. #include <linux/pm_runtime.h>
  66. #include <linux/regmap.h>
  67. #include <linux/iio/iio.h>
  68. #include <linux/iio/sysfs.h>
  69. #include <linux/iio/buffer.h>
  70. #include <linux/iio/trigger.h>
  71. #include <linux/iio/trigger_consumer.h>
  72. #include <linux/iio/triggered_buffer.h>
  73. #include "zpa2326.h"
  74. /* 200 ms should be enough for the longest conversion time in one-shot mode. */
  75. #define ZPA2326_CONVERSION_JIFFIES (HZ / 5)
  76. /* There should be a 1 ms delay (Tpup) after getting out of reset. */
  77. #define ZPA2326_TPUP_USEC_MIN (1000)
  78. #define ZPA2326_TPUP_USEC_MAX (2000)
  79. /**
  80. * struct zpa2326_frequency - Hardware sampling frequency descriptor
  81. * @hz : Frequency in Hertz.
  82. * @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG.
  83. */
  84. struct zpa2326_frequency {
  85. int hz;
  86. u16 odr;
  87. };
  88. /*
  89. * Keep these in strict ascending order: last array entry is expected to
  90. * correspond to the highest sampling frequency.
  91. */
  92. static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = {
  93. { .hz = 1, .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  94. { .hz = 5, .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  95. { .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  96. { .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  97. };
  98. /* Return the highest hardware sampling frequency available. */
  99. static const struct zpa2326_frequency *zpa2326_highest_frequency(void)
  100. {
  101. return &zpa2326_sampling_frequencies[
  102. ARRAY_SIZE(zpa2326_sampling_frequencies) - 1];
  103. }
  104. /**
  105. * struct zpa_private - Per-device internal private state
  106. * @timestamp: Buffered samples ready datum.
  107. * @regmap: Underlying I2C / SPI bus adapter used to abstract slave register
  108. * accesses.
  109. * @result: Allows sampling logic to get completion status of operations
  110. * that interrupt handlers perform asynchronously.
  111. * @data_ready: Interrupt handler uses this to wake user context up at sampling
  112. * operation completion.
  113. * @trigger: Optional hardware / interrupt driven trigger used to notify
  114. * external devices a new sample is ready.
  115. * @waken: Flag indicating whether or not device has just been powered on.
  116. * @irq: Optional interrupt line: negative or zero if not declared into
  117. * DT, in which case sampling logic keeps polling status register
  118. * to detect completion.
  119. * @frequency: Current hardware sampling frequency.
  120. * @vref: Power / voltage reference.
  121. * @vdd: Power supply.
  122. */
  123. struct zpa2326_private {
  124. s64 timestamp;
  125. struct regmap *regmap;
  126. int result;
  127. struct completion data_ready;
  128. struct iio_trigger *trigger;
  129. bool waken;
  130. int irq;
  131. const struct zpa2326_frequency *frequency;
  132. struct regulator *vref;
  133. struct regulator *vdd;
  134. };
  135. #define zpa2326_err(_idev, _format, _arg...) \
  136. dev_err(_idev->dev.parent, _format, ##_arg)
  137. #define zpa2326_warn(_idev, _format, _arg...) \
  138. dev_warn(_idev->dev.parent, _format, ##_arg)
  139. #ifdef DEBUG
  140. #define zpa2326_dbg(_idev, _format, _arg...) \
  141. dev_dbg(_idev->dev.parent, _format, ##_arg)
  142. #else
  143. #define zpa2326_dbg(_idev, _format, _arg...)
  144. #endif
  145. bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg)
  146. {
  147. switch (reg) {
  148. case ZPA2326_REF_P_XL_REG:
  149. case ZPA2326_REF_P_L_REG:
  150. case ZPA2326_REF_P_H_REG:
  151. case ZPA2326_RES_CONF_REG:
  152. case ZPA2326_CTRL_REG0_REG:
  153. case ZPA2326_CTRL_REG1_REG:
  154. case ZPA2326_CTRL_REG2_REG:
  155. case ZPA2326_CTRL_REG3_REG:
  156. case ZPA2326_THS_P_LOW_REG:
  157. case ZPA2326_THS_P_HIGH_REG:
  158. return true;
  159. default:
  160. return false;
  161. }
  162. }
  163. EXPORT_SYMBOL_GPL(zpa2326_isreg_writeable);
  164. bool zpa2326_isreg_readable(struct device *dev, unsigned int reg)
  165. {
  166. switch (reg) {
  167. case ZPA2326_REF_P_XL_REG:
  168. case ZPA2326_REF_P_L_REG:
  169. case ZPA2326_REF_P_H_REG:
  170. case ZPA2326_DEVICE_ID_REG:
  171. case ZPA2326_RES_CONF_REG:
  172. case ZPA2326_CTRL_REG0_REG:
  173. case ZPA2326_CTRL_REG1_REG:
  174. case ZPA2326_CTRL_REG2_REG:
  175. case ZPA2326_CTRL_REG3_REG:
  176. case ZPA2326_INT_SOURCE_REG:
  177. case ZPA2326_THS_P_LOW_REG:
  178. case ZPA2326_THS_P_HIGH_REG:
  179. case ZPA2326_STATUS_REG:
  180. case ZPA2326_PRESS_OUT_XL_REG:
  181. case ZPA2326_PRESS_OUT_L_REG:
  182. case ZPA2326_PRESS_OUT_H_REG:
  183. case ZPA2326_TEMP_OUT_L_REG:
  184. case ZPA2326_TEMP_OUT_H_REG:
  185. return true;
  186. default:
  187. return false;
  188. }
  189. }
  190. EXPORT_SYMBOL_GPL(zpa2326_isreg_readable);
  191. bool zpa2326_isreg_precious(struct device *dev, unsigned int reg)
  192. {
  193. switch (reg) {
  194. case ZPA2326_INT_SOURCE_REG:
  195. case ZPA2326_PRESS_OUT_H_REG:
  196. return true;
  197. default:
  198. return false;
  199. }
  200. }
  201. EXPORT_SYMBOL_GPL(zpa2326_isreg_precious);
  202. /**
  203. * zpa2326_enable_device() - Enable device, i.e. get out of low power mode.
  204. * @indio_dev: The IIO device associated with the hardware to enable.
  205. *
  206. * Required to access complete register space and to perform any sampling
  207. * or control operations.
  208. *
  209. * Return: Zero when successful, a negative error code otherwise.
  210. */
  211. static int zpa2326_enable_device(const struct iio_dev *indio_dev)
  212. {
  213. int err;
  214. err = regmap_write(((struct zpa2326_private *)
  215. iio_priv(indio_dev))->regmap,
  216. ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE);
  217. if (err) {
  218. zpa2326_err(indio_dev, "failed to enable device (%d)", err);
  219. return err;
  220. }
  221. zpa2326_dbg(indio_dev, "enabled");
  222. return 0;
  223. }
  224. /**
  225. * zpa2326_sleep() - Disable device, i.e. switch to low power mode.
  226. * @indio_dev: The IIO device associated with the hardware to disable.
  227. *
  228. * Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be
  229. * accessed once device is in the disabled state.
  230. *
  231. * Return: Zero when successful, a negative error code otherwise.
  232. */
  233. static int zpa2326_sleep(const struct iio_dev *indio_dev)
  234. {
  235. int err;
  236. err = regmap_write(((struct zpa2326_private *)
  237. iio_priv(indio_dev))->regmap,
  238. ZPA2326_CTRL_REG0_REG, 0);
  239. if (err) {
  240. zpa2326_err(indio_dev, "failed to sleep (%d)", err);
  241. return err;
  242. }
  243. zpa2326_dbg(indio_dev, "sleeping");
  244. return 0;
  245. }
  246. /**
  247. * zpa2326_reset_device() - Reset device to default hardware state.
  248. * @indio_dev: The IIO device associated with the hardware to reset.
  249. *
  250. * Disable sampling and empty hardware FIFO.
  251. * Device must be enabled before reset, i.e. not in low power mode.
  252. *
  253. * Return: Zero when successful, a negative error code otherwise.
  254. */
  255. static int zpa2326_reset_device(const struct iio_dev *indio_dev)
  256. {
  257. int err;
  258. err = regmap_write(((struct zpa2326_private *)
  259. iio_priv(indio_dev))->regmap,
  260. ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET);
  261. if (err) {
  262. zpa2326_err(indio_dev, "failed to reset device (%d)", err);
  263. return err;
  264. }
  265. usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX);
  266. zpa2326_dbg(indio_dev, "reset");
  267. return 0;
  268. }
  269. /**
  270. * zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot
  271. * mode.
  272. * @indio_dev: The IIO device associated with the sampling hardware.
  273. *
  274. * Device must have been previously enabled and configured for one shot mode.
  275. * Device will be switched back to low power mode at end of cycle.
  276. *
  277. * Return: Zero when successful, a negative error code otherwise.
  278. */
  279. static int zpa2326_start_oneshot(const struct iio_dev *indio_dev)
  280. {
  281. int err;
  282. err = regmap_write(((struct zpa2326_private *)
  283. iio_priv(indio_dev))->regmap,
  284. ZPA2326_CTRL_REG0_REG,
  285. ZPA2326_CTRL_REG0_ENABLE |
  286. ZPA2326_CTRL_REG0_ONE_SHOT);
  287. if (err) {
  288. zpa2326_err(indio_dev, "failed to start one shot cycle (%d)",
  289. err);
  290. return err;
  291. }
  292. zpa2326_dbg(indio_dev, "one shot cycle started");
  293. return 0;
  294. }
  295. /**
  296. * zpa2326_power_on() - Power on device to allow subsequent configuration.
  297. * @indio_dev: The IIO device associated with the sampling hardware.
  298. * @private: Internal private state related to @indio_dev.
  299. *
  300. * Sampling will be disabled, preventing strange things from happening in our
  301. * back. Hardware FIFO content will be cleared.
  302. * When successful, device will be left in the enabled state to allow further
  303. * configuration.
  304. *
  305. * Return: Zero when successful, a negative error code otherwise.
  306. */
  307. static int zpa2326_power_on(const struct iio_dev *indio_dev,
  308. const struct zpa2326_private *private)
  309. {
  310. int err;
  311. err = regulator_enable(private->vref);
  312. if (err)
  313. return err;
  314. err = regulator_enable(private->vdd);
  315. if (err)
  316. goto vref;
  317. zpa2326_dbg(indio_dev, "powered on");
  318. err = zpa2326_enable_device(indio_dev);
  319. if (err)
  320. goto vdd;
  321. err = zpa2326_reset_device(indio_dev);
  322. if (err)
  323. goto sleep;
  324. return 0;
  325. sleep:
  326. zpa2326_sleep(indio_dev);
  327. vdd:
  328. regulator_disable(private->vdd);
  329. vref:
  330. regulator_disable(private->vref);
  331. zpa2326_dbg(indio_dev, "powered off");
  332. return err;
  333. }
  334. /**
  335. * zpa2326_power_off() - Power off device, i.e. disable attached power
  336. * regulators.
  337. * @indio_dev: The IIO device associated with the sampling hardware.
  338. * @private: Internal private state related to @indio_dev.
  339. *
  340. * Return: Zero when successful, a negative error code otherwise.
  341. */
  342. static void zpa2326_power_off(const struct iio_dev *indio_dev,
  343. const struct zpa2326_private *private)
  344. {
  345. regulator_disable(private->vdd);
  346. regulator_disable(private->vref);
  347. zpa2326_dbg(indio_dev, "powered off");
  348. }
  349. /**
  350. * zpa2326_config_oneshot() - Setup device for one shot / on demand mode.
  351. * @indio_dev: The IIO device associated with the sampling hardware.
  352. * @irq: Optional interrupt line the hardware uses to notify new data
  353. * samples are ready. Negative or zero values indicate no interrupts
  354. * are available, meaning polling is required.
  355. *
  356. * Output Data Rate is configured for the highest possible rate so that
  357. * conversion time and power consumption are reduced to a minimum.
  358. * Note that hardware internal averaging machinery (not implemented in this
  359. * driver) is not applicable in this mode.
  360. *
  361. * Device must have been previously enabled before calling
  362. * zpa2326_config_oneshot().
  363. *
  364. * Return: Zero when successful, a negative error code otherwise.
  365. */
  366. static int zpa2326_config_oneshot(const struct iio_dev *indio_dev,
  367. int irq)
  368. {
  369. struct regmap *regs = ((struct zpa2326_private *)
  370. iio_priv(indio_dev))->regmap;
  371. const struct zpa2326_frequency *freq = zpa2326_highest_frequency();
  372. int err;
  373. /* Setup highest available Output Data Rate for one shot mode. */
  374. err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr);
  375. if (err)
  376. return err;
  377. if (irq > 0) {
  378. /* Request interrupt when new sample is available. */
  379. err = regmap_write(regs, ZPA2326_CTRL_REG1_REG,
  380. (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY);
  381. if (err) {
  382. dev_err(indio_dev->dev.parent,
  383. "failed to setup one shot mode (%d)", err);
  384. return err;
  385. }
  386. }
  387. zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz);
  388. return 0;
  389. }
  390. /**
  391. * zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO.
  392. * @indio_dev: The IIO device associated with the sampling hardware.
  393. * @min_count: Number of samples present within hardware FIFO.
  394. *
  395. * @min_count argument is a hint corresponding to the known minimum number of
  396. * samples currently living in the FIFO. This allows to reduce the number of bus
  397. * accesses by skipping status register read operation as long as we know for
  398. * sure there are still entries left.
  399. *
  400. * Return: Zero when successful, a negative error code otherwise.
  401. */
  402. static int zpa2326_clear_fifo(const struct iio_dev *indio_dev,
  403. unsigned int min_count)
  404. {
  405. struct regmap *regs = ((struct zpa2326_private *)
  406. iio_priv(indio_dev))->regmap;
  407. int err;
  408. unsigned int val;
  409. if (!min_count) {
  410. /*
  411. * No hint: read status register to determine whether FIFO is
  412. * empty or not.
  413. */
  414. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  415. if (err < 0)
  416. goto err;
  417. if (val & ZPA2326_STATUS_FIFO_E)
  418. /* Fifo is empty: nothing to trash. */
  419. return 0;
  420. }
  421. /* Clear FIFO. */
  422. do {
  423. /*
  424. * A single fetch from pressure MSB register is enough to pop
  425. * values out of FIFO.
  426. */
  427. err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val);
  428. if (err < 0)
  429. goto err;
  430. if (min_count) {
  431. /*
  432. * We know for sure there are at least min_count entries
  433. * left in FIFO. Skip status register read.
  434. */
  435. min_count--;
  436. continue;
  437. }
  438. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  439. if (err < 0)
  440. goto err;
  441. } while (!(val & ZPA2326_STATUS_FIFO_E));
  442. zpa2326_dbg(indio_dev, "FIFO cleared");
  443. return 0;
  444. err:
  445. zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err);
  446. return err;
  447. }
  448. /**
  449. * zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from
  450. * hardware FIFO.
  451. * @indio_dev: The IIO device associated with the sampling hardware.
  452. * @pressure: Sampled pressure output.
  453. *
  454. * Note that ZPA2326 hardware FIFO stores pressure samples only.
  455. *
  456. * Return: Zero when successful, a negative error code otherwise.
  457. */
  458. static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev,
  459. u32 *pressure)
  460. {
  461. struct regmap *regs = ((struct zpa2326_private *)
  462. iio_priv(indio_dev))->regmap;
  463. unsigned int val;
  464. int err;
  465. int cleared = -1;
  466. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  467. if (err < 0)
  468. return err;
  469. *pressure = 0;
  470. if (val & ZPA2326_STATUS_P_OR) {
  471. /*
  472. * Fifo overrun : first sample dequeued from FIFO is the
  473. * newest.
  474. */
  475. zpa2326_warn(indio_dev, "FIFO overflow");
  476. err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
  477. 3);
  478. if (err)
  479. return err;
  480. #define ZPA2326_FIFO_DEPTH (16U)
  481. /* Hardware FIFO may hold no more than 16 pressure samples. */
  482. return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1);
  483. }
  484. /*
  485. * Fifo has not overflown : retrieve newest sample. We need to pop
  486. * values out until FIFO is empty : last fetched pressure is the newest.
  487. * In nominal cases, we should find a single queued sample only.
  488. */
  489. do {
  490. err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
  491. 3);
  492. if (err)
  493. return err;
  494. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  495. if (err < 0)
  496. return err;
  497. cleared++;
  498. } while (!(val & ZPA2326_STATUS_FIFO_E));
  499. if (cleared)
  500. /*
  501. * Samples were pushed by hardware during previous rounds but we
  502. * didn't consume them fast enough: inform user.
  503. */
  504. zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared);
  505. return 0;
  506. }
  507. /**
  508. * zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer.
  509. * @indio_dev: The IIO device associated with the sampling hardware.
  510. * @private: Internal private state related to @indio_dev.
  511. *
  512. * Return: Zero when successful, a negative error code otherwise.
  513. */
  514. static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev,
  515. const struct zpa2326_private *private)
  516. {
  517. struct {
  518. u32 pressure;
  519. u16 temperature;
  520. u64 timestamp;
  521. } sample;
  522. int err;
  523. if (test_bit(0, indio_dev->active_scan_mask)) {
  524. /* Get current pressure from hardware FIFO. */
  525. err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure);
  526. if (err) {
  527. zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
  528. err);
  529. return err;
  530. }
  531. }
  532. if (test_bit(1, indio_dev->active_scan_mask)) {
  533. /* Get current temperature. */
  534. err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG,
  535. &sample.temperature, 2);
  536. if (err) {
  537. zpa2326_warn(indio_dev,
  538. "failed to fetch temperature (%d)", err);
  539. return err;
  540. }
  541. }
  542. /*
  543. * Now push samples using timestamp stored either :
  544. * - by hardware interrupt handler if interrupt is available: see
  545. * zpa2326_handle_irq(),
  546. * - or oneshot completion polling machinery : see
  547. * zpa2326_trigger_handler().
  548. */
  549. zpa2326_dbg(indio_dev, "filling raw samples buffer");
  550. iio_push_to_buffers_with_timestamp(indio_dev, &sample,
  551. private->timestamp);
  552. return 0;
  553. }
  554. #ifdef CONFIG_PM
  555. static int zpa2326_runtime_suspend(struct device *parent)
  556. {
  557. const struct iio_dev *indio_dev = dev_get_drvdata(parent);
  558. if (pm_runtime_autosuspend_expiration(parent))
  559. /* Userspace changed autosuspend delay. */
  560. return -EAGAIN;
  561. zpa2326_power_off(indio_dev, iio_priv(indio_dev));
  562. return 0;
  563. }
  564. static int zpa2326_runtime_resume(struct device *parent)
  565. {
  566. const struct iio_dev *indio_dev = dev_get_drvdata(parent);
  567. return zpa2326_power_on(indio_dev, iio_priv(indio_dev));
  568. }
  569. const struct dev_pm_ops zpa2326_pm_ops = {
  570. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  571. pm_runtime_force_resume)
  572. SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume,
  573. NULL)
  574. };
  575. EXPORT_SYMBOL_GPL(zpa2326_pm_ops);
  576. /**
  577. * zpa2326_resume() - Request the PM layer to power supply the device.
  578. * @indio_dev: The IIO device associated with the sampling hardware.
  579. *
  580. * Return:
  581. * < 0 - a negative error code meaning failure ;
  582. * 0 - success, device has just been powered up ;
  583. * 1 - success, device was already powered.
  584. */
  585. static int zpa2326_resume(const struct iio_dev *indio_dev)
  586. {
  587. int err;
  588. err = pm_runtime_get_sync(indio_dev->dev.parent);
  589. if (err < 0)
  590. return err;
  591. if (err > 0) {
  592. /*
  593. * Device was already power supplied: get it out of low power
  594. * mode and inform caller.
  595. */
  596. zpa2326_enable_device(indio_dev);
  597. return 1;
  598. }
  599. /* Inform caller device has just been brought back to life. */
  600. return 0;
  601. }
  602. /**
  603. * zpa2326_suspend() - Schedule a power down using autosuspend feature of PM
  604. * layer.
  605. * @indio_dev: The IIO device associated with the sampling hardware.
  606. *
  607. * Device is switched to low power mode at first to save power even when
  608. * attached regulator is a "dummy" one.
  609. */
  610. static void zpa2326_suspend(struct iio_dev *indio_dev)
  611. {
  612. struct device *parent = indio_dev->dev.parent;
  613. zpa2326_sleep(indio_dev);
  614. pm_runtime_mark_last_busy(parent);
  615. pm_runtime_put_autosuspend(parent);
  616. }
  617. static void zpa2326_init_runtime(struct device *parent)
  618. {
  619. pm_runtime_get_noresume(parent);
  620. pm_runtime_set_active(parent);
  621. pm_runtime_enable(parent);
  622. pm_runtime_set_autosuspend_delay(parent, 1000);
  623. pm_runtime_use_autosuspend(parent);
  624. pm_runtime_mark_last_busy(parent);
  625. pm_runtime_put_autosuspend(parent);
  626. }
  627. static void zpa2326_fini_runtime(struct device *parent)
  628. {
  629. pm_runtime_disable(parent);
  630. pm_runtime_set_suspended(parent);
  631. }
  632. #else /* !CONFIG_PM */
  633. static int zpa2326_resume(const struct iio_dev *indio_dev)
  634. {
  635. zpa2326_enable_device(indio_dev);
  636. return 0;
  637. }
  638. static void zpa2326_suspend(struct iio_dev *indio_dev)
  639. {
  640. zpa2326_sleep(indio_dev);
  641. }
  642. #define zpa2326_init_runtime(_parent)
  643. #define zpa2326_fini_runtime(_parent)
  644. #endif /* !CONFIG_PM */
  645. /**
  646. * zpa2326_handle_irq() - Process hardware interrupts.
  647. * @irq: Interrupt line the hardware uses to notify new data has arrived.
  648. * @data: The IIO device associated with the sampling hardware.
  649. *
  650. * Timestamp buffered samples as soon as possible then schedule threaded bottom
  651. * half.
  652. *
  653. * Return: Always successful.
  654. */
  655. static irqreturn_t zpa2326_handle_irq(int irq, void *data)
  656. {
  657. struct iio_dev *indio_dev = (struct iio_dev *)data;
  658. if (iio_buffer_enabled(indio_dev)) {
  659. /* Timestamping needed for buffered sampling only. */
  660. ((struct zpa2326_private *)
  661. iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev);
  662. }
  663. return IRQ_WAKE_THREAD;
  664. }
  665. /**
  666. * zpa2326_handle_threaded_irq() - Interrupt bottom-half handler.
  667. * @irq: Interrupt line the hardware uses to notify new data has arrived.
  668. * @data: The IIO device associated with the sampling hardware.
  669. *
  670. * Mainly ensures interrupt is caused by a real "new sample available"
  671. * condition. This relies upon the ability to perform blocking / sleeping bus
  672. * accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is
  673. * called from within a thread, i.e. not called from hard interrupt context.
  674. *
  675. * When device is using its own internal hardware trigger in continuous sampling
  676. * mode, data are available into hardware FIFO once interrupt has occurred. All
  677. * we have to do is to dispatch the trigger, which in turn will fetch data and
  678. * fill IIO buffer.
  679. *
  680. * When not using its own internal hardware trigger, the device has been
  681. * configured in one-shot mode either by an external trigger or the IIO read_raw
  682. * hook. This means one of the latter is currently waiting for sampling
  683. * completion, in which case we must simply wake it up.
  684. *
  685. * See zpa2326_trigger_handler().
  686. *
  687. * Return:
  688. * %IRQ_NONE - no consistent interrupt happened ;
  689. * %IRQ_HANDLED - there was new samples available.
  690. */
  691. static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data)
  692. {
  693. struct iio_dev *indio_dev = (struct iio_dev *)data;
  694. struct zpa2326_private *priv = iio_priv(indio_dev);
  695. unsigned int val;
  696. bool cont;
  697. irqreturn_t ret = IRQ_NONE;
  698. /*
  699. * Are we using our own internal trigger in triggered buffer mode, i.e.,
  700. * currently working in continuous sampling mode ?
  701. */
  702. cont = (iio_buffer_enabled(indio_dev) &&
  703. iio_trigger_using_own(indio_dev));
  704. /*
  705. * Device works according to a level interrupt scheme: reading interrupt
  706. * status de-asserts interrupt line.
  707. */
  708. priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
  709. if (priv->result < 0) {
  710. if (cont)
  711. return IRQ_NONE;
  712. goto complete;
  713. }
  714. /* Data ready is the only interrupt source we requested. */
  715. if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) {
  716. /*
  717. * Interrupt happened but no new sample available: likely caused
  718. * by spurious interrupts, in which case, returning IRQ_NONE
  719. * allows to benefit from the generic spurious interrupts
  720. * handling.
  721. */
  722. zpa2326_warn(indio_dev, "unexpected interrupt status %02x",
  723. val);
  724. if (cont)
  725. return IRQ_NONE;
  726. priv->result = -ENODATA;
  727. goto complete;
  728. }
  729. /* New sample available: dispatch internal trigger consumers. */
  730. iio_trigger_poll_chained(priv->trigger);
  731. if (cont)
  732. /*
  733. * Internal hardware trigger has been scheduled above : it will
  734. * fetch data on its own.
  735. */
  736. return IRQ_HANDLED;
  737. ret = IRQ_HANDLED;
  738. complete:
  739. /*
  740. * Wake up direct or externaly triggered buffer mode waiters: see
  741. * zpa2326_sample_oneshot() and zpa2326_trigger_handler().
  742. */
  743. complete(&priv->data_ready);
  744. return ret;
  745. }
  746. /**
  747. * zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt.
  748. * @indio_dev: The IIO device associated with the sampling hardware.
  749. * @private: Internal private state related to @indio_dev.
  750. *
  751. * Return: Zero when successful, a negative error code otherwise.
  752. */
  753. static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev,
  754. struct zpa2326_private *private)
  755. {
  756. int ret;
  757. unsigned int val;
  758. zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
  759. ret = wait_for_completion_interruptible_timeout(
  760. &private->data_ready, ZPA2326_CONVERSION_JIFFIES);
  761. if (ret > 0)
  762. /*
  763. * Interrupt handler completed before timeout: return operation
  764. * status.
  765. */
  766. return private->result;
  767. /* Clear all interrupts just to be sure. */
  768. regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
  769. if (!ret)
  770. /* Timed out. */
  771. ret = -ETIME;
  772. if (ret != -ERESTARTSYS)
  773. zpa2326_warn(indio_dev, "no one shot interrupt occurred (%d)",
  774. ret);
  775. return ret;
  776. }
  777. static int zpa2326_init_managed_irq(struct device *parent,
  778. struct iio_dev *indio_dev,
  779. struct zpa2326_private *private,
  780. int irq)
  781. {
  782. int err;
  783. private->irq = irq;
  784. if (irq <= 0) {
  785. /*
  786. * Platform declared no interrupt line: device will be polled
  787. * for data availability.
  788. */
  789. dev_info(parent, "no interrupt found, running in polling mode");
  790. return 0;
  791. }
  792. init_completion(&private->data_ready);
  793. /* Request handler to be scheduled into threaded interrupt context. */
  794. err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq,
  795. zpa2326_handle_threaded_irq,
  796. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  797. dev_name(parent), indio_dev);
  798. if (err) {
  799. dev_err(parent, "failed to request interrupt %d (%d)", irq,
  800. err);
  801. return err;
  802. }
  803. dev_info(parent, "using interrupt %d", irq);
  804. return 0;
  805. }
  806. /**
  807. * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready.
  808. * @indio_dev: The IIO device associated with the sampling hardware.
  809. *
  810. * Loop over registers content to detect end of sampling cycle. Used when DT
  811. * declared no valid interrupt lines.
  812. *
  813. * Return: Zero when successful, a negative error code otherwise.
  814. */
  815. static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
  816. {
  817. unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES;
  818. struct regmap *regs = ((struct zpa2326_private *)
  819. iio_priv(indio_dev))->regmap;
  820. unsigned int val;
  821. int err;
  822. zpa2326_dbg(indio_dev, "polling for one shot completion");
  823. /*
  824. * At least, 100 ms is needed for the device to complete its one-shot
  825. * cycle.
  826. */
  827. if (msleep_interruptible(100))
  828. return -ERESTARTSYS;
  829. /* Poll for conversion completion in hardware. */
  830. while (true) {
  831. err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
  832. if (err < 0)
  833. goto err;
  834. if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
  835. /* One-shot bit self clears at conversion end. */
  836. break;
  837. if (time_after(jiffies, tmout)) {
  838. /* Prevent from waiting forever : let's time out. */
  839. err = -ETIME;
  840. goto err;
  841. }
  842. usleep_range(10000, 20000);
  843. }
  844. /*
  845. * In oneshot mode, pressure sample availability guarantees that
  846. * temperature conversion has also completed : just check pressure
  847. * status bit to keep things simple.
  848. */
  849. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  850. if (err < 0)
  851. goto err;
  852. if (!(val & ZPA2326_STATUS_P_DA)) {
  853. /* No sample available. */
  854. err = -ENODATA;
  855. goto err;
  856. }
  857. return 0;
  858. err:
  859. zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
  860. return err;
  861. }
  862. /**
  863. * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU
  864. * endianness.
  865. * @indio_dev: The IIO device associated with the sampling hardware.
  866. * @type: Type of measurement / channel to fetch from.
  867. * @value: Sample output.
  868. *
  869. * Return: Zero when successful, a negative error code otherwise.
  870. */
  871. static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
  872. enum iio_chan_type type,
  873. int *value)
  874. {
  875. struct regmap *regs = ((struct zpa2326_private *)
  876. iio_priv(indio_dev))->regmap;
  877. int err;
  878. switch (type) {
  879. case IIO_PRESSURE:
  880. zpa2326_dbg(indio_dev, "fetching raw pressure sample");
  881. err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value,
  882. 3);
  883. if (err) {
  884. zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
  885. err);
  886. return err;
  887. }
  888. /* Pressure is a 24 bits wide little-endian unsigned int. */
  889. *value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) |
  890. ((u8 *)value)[0];
  891. return IIO_VAL_INT;
  892. case IIO_TEMP:
  893. zpa2326_dbg(indio_dev, "fetching raw temperature sample");
  894. err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2);
  895. if (err) {
  896. zpa2326_warn(indio_dev,
  897. "failed to fetch temperature (%d)", err);
  898. return err;
  899. }
  900. /* Temperature is a 16 bits wide little-endian signed int. */
  901. *value = (int)le16_to_cpup((__le16 *)value);
  902. return IIO_VAL_INT;
  903. default:
  904. return -EINVAL;
  905. }
  906. }
  907. /**
  908. * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle.
  909. * @indio_dev: The IIO device associated with the sampling hardware.
  910. * @type: Type of measurement / channel to fetch from.
  911. * @value: Sample output.
  912. *
  913. * Return: Zero when successful, a negative error code otherwise.
  914. */
  915. static int zpa2326_sample_oneshot(struct iio_dev *indio_dev,
  916. enum iio_chan_type type,
  917. int *value)
  918. {
  919. int ret;
  920. struct zpa2326_private *priv;
  921. ret = iio_device_claim_direct_mode(indio_dev);
  922. if (ret)
  923. return ret;
  924. ret = zpa2326_resume(indio_dev);
  925. if (ret < 0)
  926. goto release;
  927. priv = iio_priv(indio_dev);
  928. if (ret > 0) {
  929. /*
  930. * We were already power supplied. Just clear hardware FIFO to
  931. * get rid of samples acquired during previous rounds (if any).
  932. * Sampling operation always generates both temperature and
  933. * pressure samples. The latter are always enqueued into
  934. * hardware FIFO. This may lead to situations were pressure
  935. * samples still sit into FIFO when previous cycle(s) fetched
  936. * temperature data only.
  937. * Hence, we need to clear hardware FIFO content to prevent from
  938. * getting outdated values at the end of current cycle.
  939. */
  940. if (type == IIO_PRESSURE) {
  941. ret = zpa2326_clear_fifo(indio_dev, 0);
  942. if (ret)
  943. goto suspend;
  944. }
  945. } else {
  946. /*
  947. * We have just been power supplied, i.e. device is in default
  948. * "out of reset" state, meaning we need to reconfigure it
  949. * entirely.
  950. */
  951. ret = zpa2326_config_oneshot(indio_dev, priv->irq);
  952. if (ret)
  953. goto suspend;
  954. }
  955. /* Start a sampling cycle in oneshot mode. */
  956. ret = zpa2326_start_oneshot(indio_dev);
  957. if (ret)
  958. goto suspend;
  959. /* Wait for sampling cycle to complete. */
  960. if (priv->irq > 0)
  961. ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
  962. else
  963. ret = zpa2326_poll_oneshot_completion(indio_dev);
  964. if (ret)
  965. goto suspend;
  966. /* Retrieve raw sample value and convert it to CPU endianness. */
  967. ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
  968. suspend:
  969. zpa2326_suspend(indio_dev);
  970. release:
  971. iio_device_release_direct_mode(indio_dev);
  972. return ret;
  973. }
  974. /**
  975. * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one
  976. * shot mode.
  977. * @irq: The software interrupt assigned to @data
  978. * @data: The IIO poll function dispatched by external trigger our device is
  979. * attached to.
  980. *
  981. * Bottom-half handler called by the IIO trigger to which our device is
  982. * currently attached. Allows us to synchronize this device buffered sampling
  983. * either with external events (such as timer expiration, external device sample
  984. * ready, etc...) or with its own interrupt (internal hardware trigger).
  985. *
  986. * When using an external trigger, basically run the same sequence of operations
  987. * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO
  988. * is not cleared since already done at buffering enable time and samples
  989. * dequeueing always retrieves the most recent value.
  990. *
  991. * Otherwise, when internal hardware trigger has dispatched us, just fetch data
  992. * from hardware FIFO.
  993. *
  994. * Fetched data will pushed unprocessed to IIO buffer since samples conversion
  995. * is delegated to userspace in buffered mode (endianness, etc...).
  996. *
  997. * Return:
  998. * %IRQ_NONE - no consistent interrupt happened ;
  999. * %IRQ_HANDLED - there was new samples available.
  1000. */
  1001. static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
  1002. {
  1003. struct iio_dev *indio_dev = ((struct iio_poll_func *)
  1004. data)->indio_dev;
  1005. struct zpa2326_private *priv = iio_priv(indio_dev);
  1006. bool cont;
  1007. /*
  1008. * We have been dispatched, meaning we are in triggered buffer mode.
  1009. * Using our own internal trigger implies we are currently in continuous
  1010. * hardware sampling mode.
  1011. */
  1012. cont = iio_trigger_using_own(indio_dev);
  1013. if (!cont) {
  1014. /* On demand sampling : start a one shot cycle. */
  1015. if (zpa2326_start_oneshot(indio_dev))
  1016. goto out;
  1017. /* Wait for sampling cycle to complete. */
  1018. if (priv->irq <= 0) {
  1019. /* No interrupt available: poll for completion. */
  1020. if (zpa2326_poll_oneshot_completion(indio_dev))
  1021. goto out;
  1022. /* Only timestamp sample once it is ready. */
  1023. priv->timestamp = iio_get_time_ns(indio_dev);
  1024. } else {
  1025. /* Interrupt handlers will timestamp for us. */
  1026. if (zpa2326_wait_oneshot_completion(indio_dev, priv))
  1027. goto out;
  1028. }
  1029. }
  1030. /* Enqueue to IIO buffer / userspace. */
  1031. zpa2326_fill_sample_buffer(indio_dev, priv);
  1032. out:
  1033. if (!cont)
  1034. /* Don't switch to low power if sampling continuously. */
  1035. zpa2326_sleep(indio_dev);
  1036. /* Inform attached trigger we are done. */
  1037. iio_trigger_notify_done(indio_dev->trig);
  1038. return IRQ_HANDLED;
  1039. }
  1040. /**
  1041. * zpa2326_preenable_buffer() - Prepare device for configuring triggered
  1042. * sampling
  1043. * modes.
  1044. * @indio_dev: The IIO device associated with the sampling hardware.
  1045. *
  1046. * Basically power up device.
  1047. * Called with IIO device's lock held.
  1048. *
  1049. * Return: Zero when successful, a negative error code otherwise.
  1050. */
  1051. static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
  1052. {
  1053. int ret = zpa2326_resume(indio_dev);
  1054. if (ret < 0)
  1055. return ret;
  1056. /* Tell zpa2326_postenable_buffer() if we have just been powered on. */
  1057. ((struct zpa2326_private *)
  1058. iio_priv(indio_dev))->waken = iio_priv(indio_dev);
  1059. return 0;
  1060. }
  1061. /**
  1062. * zpa2326_postenable_buffer() - Configure device for triggered sampling.
  1063. * @indio_dev: The IIO device associated with the sampling hardware.
  1064. *
  1065. * Basically setup one-shot mode if plugging external trigger.
  1066. * Otherwise, let internal trigger configure continuous sampling :
  1067. * see zpa2326_set_trigger_state().
  1068. *
  1069. * If an error is returned, IIO layer will call our postdisable hook for us,
  1070. * i.e. no need to explicitly power device off here.
  1071. * Called with IIO device's lock held.
  1072. *
  1073. * Called with IIO device's lock held.
  1074. *
  1075. * Return: Zero when successful, a negative error code otherwise.
  1076. */
  1077. static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
  1078. {
  1079. const struct zpa2326_private *priv = iio_priv(indio_dev);
  1080. int err;
  1081. if (!priv->waken) {
  1082. /*
  1083. * We were already power supplied. Just clear hardware FIFO to
  1084. * get rid of samples acquired during previous rounds (if any).
  1085. */
  1086. err = zpa2326_clear_fifo(indio_dev, 0);
  1087. if (err)
  1088. goto err;
  1089. }
  1090. if (!iio_trigger_using_own(indio_dev) && priv->waken) {
  1091. /*
  1092. * We are using an external trigger and we have just been
  1093. * powered up: reconfigure one-shot mode.
  1094. */
  1095. err = zpa2326_config_oneshot(indio_dev, priv->irq);
  1096. if (err)
  1097. goto err;
  1098. }
  1099. /* Plug our own trigger event handler. */
  1100. err = iio_triggered_buffer_postenable(indio_dev);
  1101. if (err)
  1102. goto err;
  1103. return 0;
  1104. err:
  1105. zpa2326_err(indio_dev, "failed to enable buffering (%d)", err);
  1106. return err;
  1107. }
  1108. static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
  1109. {
  1110. zpa2326_suspend(indio_dev);
  1111. return 0;
  1112. }
  1113. static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
  1114. .preenable = zpa2326_preenable_buffer,
  1115. .postenable = zpa2326_postenable_buffer,
  1116. .predisable = iio_triggered_buffer_predisable,
  1117. .postdisable = zpa2326_postdisable_buffer
  1118. };
  1119. /**
  1120. * zpa2326_set_trigger_state() - Start / stop continuous sampling.
  1121. * @trig: The trigger being attached to IIO device associated with the sampling
  1122. * hardware.
  1123. * @state: Tell whether to start (true) or stop (false)
  1124. *
  1125. * Basically enable / disable hardware continuous sampling mode.
  1126. *
  1127. * Called with IIO device's lock held at postenable() or predisable() time.
  1128. *
  1129. * Return: Zero when successful, a negative error code otherwise.
  1130. */
  1131. static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
  1132. {
  1133. const struct iio_dev *indio_dev = dev_get_drvdata(
  1134. trig->dev.parent);
  1135. const struct zpa2326_private *priv = iio_priv(indio_dev);
  1136. int err;
  1137. if (!state) {
  1138. /*
  1139. * Switch trigger off : in case of failure, interrupt is left
  1140. * disabled in order to prevent handler from accessing released
  1141. * resources.
  1142. */
  1143. unsigned int val;
  1144. /*
  1145. * As device is working in continuous mode, handlers may be
  1146. * accessing resources we are currently freeing...
  1147. * Prevent this by disabling interrupt handlers and ensure
  1148. * the device will generate no more interrupts unless explicitly
  1149. * required to, i.e. by restoring back to default one shot mode.
  1150. */
  1151. disable_irq(priv->irq);
  1152. /*
  1153. * Disable continuous sampling mode to restore settings for
  1154. * one shot / direct sampling operations.
  1155. */
  1156. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
  1157. zpa2326_highest_frequency()->odr);
  1158. if (err)
  1159. return err;
  1160. /*
  1161. * Now that device won't generate interrupts on its own,
  1162. * acknowledge any currently active interrupts (may happen on
  1163. * rare occasions while stopping continuous mode).
  1164. */
  1165. err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
  1166. if (err < 0)
  1167. return err;
  1168. /*
  1169. * Re-enable interrupts only if we can guarantee the device will
  1170. * generate no more interrupts to prevent handlers from
  1171. * accessing released resources.
  1172. */
  1173. enable_irq(priv->irq);
  1174. zpa2326_dbg(indio_dev, "continuous mode stopped");
  1175. } else {
  1176. /*
  1177. * Switch trigger on : start continuous sampling at required
  1178. * frequency.
  1179. */
  1180. if (priv->waken) {
  1181. /* Enable interrupt if getting out of reset. */
  1182. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
  1183. (u8)
  1184. ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
  1185. if (err)
  1186. return err;
  1187. }
  1188. /* Enable continuous sampling at specified frequency. */
  1189. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
  1190. ZPA2326_CTRL_REG3_ENABLE_MEAS |
  1191. priv->frequency->odr);
  1192. if (err)
  1193. return err;
  1194. zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
  1195. priv->frequency->hz);
  1196. }
  1197. return 0;
  1198. }
  1199. static const struct iio_trigger_ops zpa2326_trigger_ops = {
  1200. .owner = THIS_MODULE,
  1201. .set_trigger_state = zpa2326_set_trigger_state,
  1202. };
  1203. /**
  1204. * zpa2326_init_trigger() - Create an interrupt driven / hardware trigger
  1205. * allowing to notify external devices a new sample is
  1206. * ready.
  1207. * @parent: Hardware sampling device @indio_dev is a child of.
  1208. * @indio_dev: The IIO device associated with the sampling hardware.
  1209. * @private: Internal private state related to @indio_dev.
  1210. * @irq: Optional interrupt line the hardware uses to notify new data
  1211. * samples are ready. Negative or zero values indicate no interrupts
  1212. * are available, meaning polling is required.
  1213. *
  1214. * Only relevant when DT declares a valid interrupt line.
  1215. *
  1216. * Return: Zero when successful, a negative error code otherwise.
  1217. */
  1218. static int zpa2326_init_managed_trigger(struct device *parent,
  1219. struct iio_dev *indio_dev,
  1220. struct zpa2326_private *private,
  1221. int irq)
  1222. {
  1223. struct iio_trigger *trigger;
  1224. int ret;
  1225. if (irq <= 0)
  1226. return 0;
  1227. trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
  1228. indio_dev->name, indio_dev->id);
  1229. if (!trigger)
  1230. return -ENOMEM;
  1231. /* Basic setup. */
  1232. trigger->dev.parent = parent;
  1233. trigger->ops = &zpa2326_trigger_ops;
  1234. private->trigger = trigger;
  1235. /* Register to triggers space. */
  1236. ret = devm_iio_trigger_register(parent, trigger);
  1237. if (ret)
  1238. dev_err(parent, "failed to register hardware trigger (%d)",
  1239. ret);
  1240. return ret;
  1241. }
  1242. static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
  1243. {
  1244. return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
  1245. }
  1246. static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
  1247. {
  1248. struct zpa2326_private *priv = iio_priv(indio_dev);
  1249. int freq;
  1250. int err;
  1251. /* Check if requested frequency is supported. */
  1252. for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
  1253. if (zpa2326_sampling_frequencies[freq].hz == hz)
  1254. break;
  1255. if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
  1256. return -EINVAL;
  1257. /* Don't allow changing frequency if buffered sampling is ongoing. */
  1258. err = iio_device_claim_direct_mode(indio_dev);
  1259. if (err)
  1260. return err;
  1261. priv->frequency = &zpa2326_sampling_frequencies[freq];
  1262. iio_device_release_direct_mode(indio_dev);
  1263. return 0;
  1264. }
  1265. /* Expose supported hardware sampling frequencies (Hz) through sysfs. */
  1266. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
  1267. static struct attribute *zpa2326_attributes[] = {
  1268. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  1269. NULL
  1270. };
  1271. static const struct attribute_group zpa2326_attribute_group = {
  1272. .attrs = zpa2326_attributes,
  1273. };
  1274. static int zpa2326_read_raw(struct iio_dev *indio_dev,
  1275. struct iio_chan_spec const *chan,
  1276. int *val,
  1277. int *val2,
  1278. long mask)
  1279. {
  1280. switch (mask) {
  1281. case IIO_CHAN_INFO_RAW:
  1282. return zpa2326_sample_oneshot(indio_dev, chan->type, val);
  1283. case IIO_CHAN_INFO_SCALE:
  1284. switch (chan->type) {
  1285. case IIO_PRESSURE:
  1286. /*
  1287. * Pressure resolution is 1/64 Pascal. Scale to kPascal
  1288. * as required by IIO ABI.
  1289. */
  1290. *val = 1;
  1291. *val2 = 64000;
  1292. return IIO_VAL_FRACTIONAL;
  1293. case IIO_TEMP:
  1294. /*
  1295. * Temperature follows the equation:
  1296. * Temp[degC] = Tempcode * 0.00649 - 176.83
  1297. * where:
  1298. * Tempcode is composed the raw sampled 16 bits.
  1299. *
  1300. * Hence, to produce a temperature in milli-degrees
  1301. * Celsius according to IIO ABI, we need to apply the
  1302. * following equation to raw samples:
  1303. * Temp[milli degC] = (Tempcode + Offset) * Scale
  1304. * where:
  1305. * Offset = -176.83 / 0.00649
  1306. * Scale = 0.00649 * 1000
  1307. */
  1308. *val = 6;
  1309. *val2 = 490000;
  1310. return IIO_VAL_INT_PLUS_MICRO;
  1311. default:
  1312. return -EINVAL;
  1313. }
  1314. case IIO_CHAN_INFO_OFFSET:
  1315. switch (chan->type) {
  1316. case IIO_TEMP:
  1317. *val = -17683000;
  1318. *val2 = 649;
  1319. return IIO_VAL_FRACTIONAL;
  1320. default:
  1321. return -EINVAL;
  1322. }
  1323. case IIO_CHAN_INFO_SAMP_FREQ:
  1324. *val = zpa2326_get_frequency(indio_dev);
  1325. return IIO_VAL_INT;
  1326. default:
  1327. return -EINVAL;
  1328. }
  1329. }
  1330. static int zpa2326_write_raw(struct iio_dev *indio_dev,
  1331. const struct iio_chan_spec *chan,
  1332. int val,
  1333. int val2,
  1334. long mask)
  1335. {
  1336. if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
  1337. return -EINVAL;
  1338. return zpa2326_set_frequency(indio_dev, val);
  1339. }
  1340. static const struct iio_chan_spec zpa2326_channels[] = {
  1341. [0] = {
  1342. .type = IIO_PRESSURE,
  1343. .scan_index = 0,
  1344. .scan_type = {
  1345. .sign = 'u',
  1346. .realbits = 24,
  1347. .storagebits = 32,
  1348. .endianness = IIO_LE,
  1349. },
  1350. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1351. BIT(IIO_CHAN_INFO_SCALE),
  1352. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  1353. },
  1354. [1] = {
  1355. .type = IIO_TEMP,
  1356. .scan_index = 1,
  1357. .scan_type = {
  1358. .sign = 's',
  1359. .realbits = 16,
  1360. .storagebits = 16,
  1361. .endianness = IIO_LE,
  1362. },
  1363. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1364. BIT(IIO_CHAN_INFO_SCALE) |
  1365. BIT(IIO_CHAN_INFO_OFFSET),
  1366. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  1367. },
  1368. [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
  1369. };
  1370. static const struct iio_info zpa2326_info = {
  1371. .driver_module = THIS_MODULE,
  1372. .attrs = &zpa2326_attribute_group,
  1373. .read_raw = zpa2326_read_raw,
  1374. .write_raw = zpa2326_write_raw,
  1375. };
  1376. static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
  1377. const char *name,
  1378. struct regmap *regmap)
  1379. {
  1380. struct iio_dev *indio_dev;
  1381. /* Allocate space to hold IIO device internal state. */
  1382. indio_dev = devm_iio_device_alloc(device,
  1383. sizeof(struct zpa2326_private));
  1384. if (!indio_dev)
  1385. return NULL;
  1386. /* Setup for userspace synchronous on demand sampling. */
  1387. indio_dev->modes = INDIO_DIRECT_MODE;
  1388. indio_dev->dev.parent = device;
  1389. indio_dev->channels = zpa2326_channels;
  1390. indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
  1391. indio_dev->name = name;
  1392. indio_dev->info = &zpa2326_info;
  1393. return indio_dev;
  1394. }
  1395. int zpa2326_probe(struct device *parent,
  1396. const char *name,
  1397. int irq,
  1398. unsigned int hwid,
  1399. struct regmap *regmap)
  1400. {
  1401. struct iio_dev *indio_dev;
  1402. struct zpa2326_private *priv;
  1403. int err;
  1404. unsigned int id;
  1405. indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
  1406. if (!indio_dev)
  1407. return -ENOMEM;
  1408. priv = iio_priv(indio_dev);
  1409. priv->vref = devm_regulator_get(parent, "vref");
  1410. if (IS_ERR(priv->vref))
  1411. return PTR_ERR(priv->vref);
  1412. priv->vdd = devm_regulator_get(parent, "vdd");
  1413. if (IS_ERR(priv->vdd))
  1414. return PTR_ERR(priv->vdd);
  1415. /* Set default hardware sampling frequency to highest rate supported. */
  1416. priv->frequency = zpa2326_highest_frequency();
  1417. /*
  1418. * Plug device's underlying bus abstraction : this MUST be set before
  1419. * registering interrupt handlers since an interrupt might happen if
  1420. * power up sequence is not properly applied.
  1421. */
  1422. priv->regmap = regmap;
  1423. err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
  1424. zpa2326_trigger_handler,
  1425. &zpa2326_buffer_setup_ops);
  1426. if (err)
  1427. return err;
  1428. err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
  1429. if (err)
  1430. return err;
  1431. err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
  1432. if (err)
  1433. return err;
  1434. /* Power up to check device ID and perform initial hardware setup. */
  1435. err = zpa2326_power_on(indio_dev, priv);
  1436. if (err)
  1437. return err;
  1438. /* Read id register to check we are talking to the right slave. */
  1439. err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
  1440. if (err)
  1441. goto sleep;
  1442. if (id != hwid) {
  1443. dev_err(parent, "found device with unexpected id %02x", id);
  1444. err = -ENODEV;
  1445. goto sleep;
  1446. }
  1447. err = zpa2326_config_oneshot(indio_dev, irq);
  1448. if (err)
  1449. goto sleep;
  1450. /* Setup done : go sleeping. Device will be awaken upon user request. */
  1451. err = zpa2326_sleep(indio_dev);
  1452. if (err)
  1453. goto poweroff;
  1454. dev_set_drvdata(parent, indio_dev);
  1455. zpa2326_init_runtime(parent);
  1456. err = iio_device_register(indio_dev);
  1457. if (err) {
  1458. zpa2326_fini_runtime(parent);
  1459. goto poweroff;
  1460. }
  1461. return 0;
  1462. sleep:
  1463. /* Put to sleep just in case power regulators are "dummy" ones. */
  1464. zpa2326_sleep(indio_dev);
  1465. poweroff:
  1466. zpa2326_power_off(indio_dev, priv);
  1467. return err;
  1468. }
  1469. EXPORT_SYMBOL_GPL(zpa2326_probe);
  1470. void zpa2326_remove(const struct device *parent)
  1471. {
  1472. struct iio_dev *indio_dev = dev_get_drvdata(parent);
  1473. iio_device_unregister(indio_dev);
  1474. zpa2326_fini_runtime(indio_dev->dev.parent);
  1475. zpa2326_sleep(indio_dev);
  1476. zpa2326_power_off(indio_dev, iio_priv(indio_dev));
  1477. }
  1478. EXPORT_SYMBOL_GPL(zpa2326_remove);
  1479. MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
  1480. MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
  1481. MODULE_LICENSE("GPL v2");