zpa2326.c 47 KB

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