zpa2326.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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. long timeout;
  759. zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
  760. timeout = wait_for_completion_interruptible_timeout(
  761. &private->data_ready, ZPA2326_CONVERSION_JIFFIES);
  762. if (timeout > 0)
  763. /*
  764. * Interrupt handler completed before timeout: return operation
  765. * status.
  766. */
  767. return private->result;
  768. /* Clear all interrupts just to be sure. */
  769. regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
  770. if (!timeout) {
  771. /* Timed out. */
  772. zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)",
  773. timeout);
  774. ret = -ETIME;
  775. } else if (timeout < 0) {
  776. zpa2326_warn(indio_dev,
  777. "wait for one shot interrupt cancelled");
  778. ret = -ERESTARTSYS;
  779. }
  780. return ret;
  781. }
  782. static int zpa2326_init_managed_irq(struct device *parent,
  783. struct iio_dev *indio_dev,
  784. struct zpa2326_private *private,
  785. int irq)
  786. {
  787. int err;
  788. private->irq = irq;
  789. if (irq <= 0) {
  790. /*
  791. * Platform declared no interrupt line: device will be polled
  792. * for data availability.
  793. */
  794. dev_info(parent, "no interrupt found, running in polling mode");
  795. return 0;
  796. }
  797. init_completion(&private->data_ready);
  798. /* Request handler to be scheduled into threaded interrupt context. */
  799. err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq,
  800. zpa2326_handle_threaded_irq,
  801. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  802. dev_name(parent), indio_dev);
  803. if (err) {
  804. dev_err(parent, "failed to request interrupt %d (%d)", irq,
  805. err);
  806. return err;
  807. }
  808. dev_info(parent, "using interrupt %d", irq);
  809. return 0;
  810. }
  811. /**
  812. * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready.
  813. * @indio_dev: The IIO device associated with the sampling hardware.
  814. *
  815. * Loop over registers content to detect end of sampling cycle. Used when DT
  816. * declared no valid interrupt lines.
  817. *
  818. * Return: Zero when successful, a negative error code otherwise.
  819. */
  820. static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
  821. {
  822. unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES;
  823. struct regmap *regs = ((struct zpa2326_private *)
  824. iio_priv(indio_dev))->regmap;
  825. unsigned int val;
  826. int err;
  827. zpa2326_dbg(indio_dev, "polling for one shot completion");
  828. /*
  829. * At least, 100 ms is needed for the device to complete its one-shot
  830. * cycle.
  831. */
  832. if (msleep_interruptible(100))
  833. return -ERESTARTSYS;
  834. /* Poll for conversion completion in hardware. */
  835. while (true) {
  836. err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
  837. if (err < 0)
  838. goto err;
  839. if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
  840. /* One-shot bit self clears at conversion end. */
  841. break;
  842. if (time_after(jiffies, tmout)) {
  843. /* Prevent from waiting forever : let's time out. */
  844. err = -ETIME;
  845. goto err;
  846. }
  847. usleep_range(10000, 20000);
  848. }
  849. /*
  850. * In oneshot mode, pressure sample availability guarantees that
  851. * temperature conversion has also completed : just check pressure
  852. * status bit to keep things simple.
  853. */
  854. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  855. if (err < 0)
  856. goto err;
  857. if (!(val & ZPA2326_STATUS_P_DA)) {
  858. /* No sample available. */
  859. err = -ENODATA;
  860. goto err;
  861. }
  862. return 0;
  863. err:
  864. zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
  865. return err;
  866. }
  867. /**
  868. * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU
  869. * endianness.
  870. * @indio_dev: The IIO device associated with the sampling hardware.
  871. * @type: Type of measurement / channel to fetch from.
  872. * @value: Sample output.
  873. *
  874. * Return: Zero when successful, a negative error code otherwise.
  875. */
  876. static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
  877. enum iio_chan_type type,
  878. int *value)
  879. {
  880. struct regmap *regs = ((struct zpa2326_private *)
  881. iio_priv(indio_dev))->regmap;
  882. int err;
  883. switch (type) {
  884. case IIO_PRESSURE:
  885. zpa2326_dbg(indio_dev, "fetching raw pressure sample");
  886. err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value,
  887. 3);
  888. if (err) {
  889. zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
  890. err);
  891. return err;
  892. }
  893. /* Pressure is a 24 bits wide little-endian unsigned int. */
  894. *value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) |
  895. ((u8 *)value)[0];
  896. return IIO_VAL_INT;
  897. case IIO_TEMP:
  898. zpa2326_dbg(indio_dev, "fetching raw temperature sample");
  899. err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2);
  900. if (err) {
  901. zpa2326_warn(indio_dev,
  902. "failed to fetch temperature (%d)", err);
  903. return err;
  904. }
  905. /* Temperature is a 16 bits wide little-endian signed int. */
  906. *value = (int)le16_to_cpup((__le16 *)value);
  907. return IIO_VAL_INT;
  908. default:
  909. return -EINVAL;
  910. }
  911. }
  912. /**
  913. * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle.
  914. * @indio_dev: The IIO device associated with the sampling hardware.
  915. * @type: Type of measurement / channel to fetch from.
  916. * @value: Sample output.
  917. *
  918. * Return: Zero when successful, a negative error code otherwise.
  919. */
  920. static int zpa2326_sample_oneshot(struct iio_dev *indio_dev,
  921. enum iio_chan_type type,
  922. int *value)
  923. {
  924. int ret;
  925. struct zpa2326_private *priv;
  926. ret = iio_device_claim_direct_mode(indio_dev);
  927. if (ret)
  928. return ret;
  929. ret = zpa2326_resume(indio_dev);
  930. if (ret < 0)
  931. goto release;
  932. priv = iio_priv(indio_dev);
  933. if (ret > 0) {
  934. /*
  935. * We were already power supplied. Just clear hardware FIFO to
  936. * get rid of samples acquired during previous rounds (if any).
  937. * Sampling operation always generates both temperature and
  938. * pressure samples. The latter are always enqueued into
  939. * hardware FIFO. This may lead to situations were pressure
  940. * samples still sit into FIFO when previous cycle(s) fetched
  941. * temperature data only.
  942. * Hence, we need to clear hardware FIFO content to prevent from
  943. * getting outdated values at the end of current cycle.
  944. */
  945. if (type == IIO_PRESSURE) {
  946. ret = zpa2326_clear_fifo(indio_dev, 0);
  947. if (ret)
  948. goto suspend;
  949. }
  950. } else {
  951. /*
  952. * We have just been power supplied, i.e. device is in default
  953. * "out of reset" state, meaning we need to reconfigure it
  954. * entirely.
  955. */
  956. ret = zpa2326_config_oneshot(indio_dev, priv->irq);
  957. if (ret)
  958. goto suspend;
  959. }
  960. /* Start a sampling cycle in oneshot mode. */
  961. ret = zpa2326_start_oneshot(indio_dev);
  962. if (ret)
  963. goto suspend;
  964. /* Wait for sampling cycle to complete. */
  965. if (priv->irq > 0)
  966. ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
  967. else
  968. ret = zpa2326_poll_oneshot_completion(indio_dev);
  969. if (ret)
  970. goto suspend;
  971. /* Retrieve raw sample value and convert it to CPU endianness. */
  972. ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
  973. suspend:
  974. zpa2326_suspend(indio_dev);
  975. release:
  976. iio_device_release_direct_mode(indio_dev);
  977. return ret;
  978. }
  979. /**
  980. * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one
  981. * shot mode.
  982. * @irq: The software interrupt assigned to @data
  983. * @data: The IIO poll function dispatched by external trigger our device is
  984. * attached to.
  985. *
  986. * Bottom-half handler called by the IIO trigger to which our device is
  987. * currently attached. Allows us to synchronize this device buffered sampling
  988. * either with external events (such as timer expiration, external device sample
  989. * ready, etc...) or with its own interrupt (internal hardware trigger).
  990. *
  991. * When using an external trigger, basically run the same sequence of operations
  992. * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO
  993. * is not cleared since already done at buffering enable time and samples
  994. * dequeueing always retrieves the most recent value.
  995. *
  996. * Otherwise, when internal hardware trigger has dispatched us, just fetch data
  997. * from hardware FIFO.
  998. *
  999. * Fetched data will pushed unprocessed to IIO buffer since samples conversion
  1000. * is delegated to userspace in buffered mode (endianness, etc...).
  1001. *
  1002. * Return:
  1003. * %IRQ_NONE - no consistent interrupt happened ;
  1004. * %IRQ_HANDLED - there was new samples available.
  1005. */
  1006. static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
  1007. {
  1008. struct iio_dev *indio_dev = ((struct iio_poll_func *)
  1009. data)->indio_dev;
  1010. struct zpa2326_private *priv = iio_priv(indio_dev);
  1011. bool cont;
  1012. /*
  1013. * We have been dispatched, meaning we are in triggered buffer mode.
  1014. * Using our own internal trigger implies we are currently in continuous
  1015. * hardware sampling mode.
  1016. */
  1017. cont = iio_trigger_using_own(indio_dev);
  1018. if (!cont) {
  1019. /* On demand sampling : start a one shot cycle. */
  1020. if (zpa2326_start_oneshot(indio_dev))
  1021. goto out;
  1022. /* Wait for sampling cycle to complete. */
  1023. if (priv->irq <= 0) {
  1024. /* No interrupt available: poll for completion. */
  1025. if (zpa2326_poll_oneshot_completion(indio_dev))
  1026. goto out;
  1027. /* Only timestamp sample once it is ready. */
  1028. priv->timestamp = iio_get_time_ns(indio_dev);
  1029. } else {
  1030. /* Interrupt handlers will timestamp for us. */
  1031. if (zpa2326_wait_oneshot_completion(indio_dev, priv))
  1032. goto out;
  1033. }
  1034. }
  1035. /* Enqueue to IIO buffer / userspace. */
  1036. zpa2326_fill_sample_buffer(indio_dev, priv);
  1037. out:
  1038. if (!cont)
  1039. /* Don't switch to low power if sampling continuously. */
  1040. zpa2326_sleep(indio_dev);
  1041. /* Inform attached trigger we are done. */
  1042. iio_trigger_notify_done(indio_dev->trig);
  1043. return IRQ_HANDLED;
  1044. }
  1045. /**
  1046. * zpa2326_preenable_buffer() - Prepare device for configuring triggered
  1047. * sampling
  1048. * modes.
  1049. * @indio_dev: The IIO device associated with the sampling hardware.
  1050. *
  1051. * Basically power up device.
  1052. * Called with IIO device's lock held.
  1053. *
  1054. * Return: Zero when successful, a negative error code otherwise.
  1055. */
  1056. static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
  1057. {
  1058. int ret = zpa2326_resume(indio_dev);
  1059. if (ret < 0)
  1060. return ret;
  1061. /* Tell zpa2326_postenable_buffer() if we have just been powered on. */
  1062. ((struct zpa2326_private *)
  1063. iio_priv(indio_dev))->waken = iio_priv(indio_dev);
  1064. return 0;
  1065. }
  1066. /**
  1067. * zpa2326_postenable_buffer() - Configure device for triggered sampling.
  1068. * @indio_dev: The IIO device associated with the sampling hardware.
  1069. *
  1070. * Basically setup one-shot mode if plugging external trigger.
  1071. * Otherwise, let internal trigger configure continuous sampling :
  1072. * see zpa2326_set_trigger_state().
  1073. *
  1074. * If an error is returned, IIO layer will call our postdisable hook for us,
  1075. * i.e. no need to explicitly power device off here.
  1076. * Called with IIO device's lock held.
  1077. *
  1078. * Called with IIO device's lock held.
  1079. *
  1080. * Return: Zero when successful, a negative error code otherwise.
  1081. */
  1082. static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
  1083. {
  1084. const struct zpa2326_private *priv = iio_priv(indio_dev);
  1085. int err;
  1086. if (!priv->waken) {
  1087. /*
  1088. * We were already power supplied. Just clear hardware FIFO to
  1089. * get rid of samples acquired during previous rounds (if any).
  1090. */
  1091. err = zpa2326_clear_fifo(indio_dev, 0);
  1092. if (err)
  1093. goto err;
  1094. }
  1095. if (!iio_trigger_using_own(indio_dev) && priv->waken) {
  1096. /*
  1097. * We are using an external trigger and we have just been
  1098. * powered up: reconfigure one-shot mode.
  1099. */
  1100. err = zpa2326_config_oneshot(indio_dev, priv->irq);
  1101. if (err)
  1102. goto err;
  1103. }
  1104. /* Plug our own trigger event handler. */
  1105. err = iio_triggered_buffer_postenable(indio_dev);
  1106. if (err)
  1107. goto err;
  1108. return 0;
  1109. err:
  1110. zpa2326_err(indio_dev, "failed to enable buffering (%d)", err);
  1111. return err;
  1112. }
  1113. static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
  1114. {
  1115. zpa2326_suspend(indio_dev);
  1116. return 0;
  1117. }
  1118. static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
  1119. .preenable = zpa2326_preenable_buffer,
  1120. .postenable = zpa2326_postenable_buffer,
  1121. .predisable = iio_triggered_buffer_predisable,
  1122. .postdisable = zpa2326_postdisable_buffer
  1123. };
  1124. /**
  1125. * zpa2326_set_trigger_state() - Start / stop continuous sampling.
  1126. * @trig: The trigger being attached to IIO device associated with the sampling
  1127. * hardware.
  1128. * @state: Tell whether to start (true) or stop (false)
  1129. *
  1130. * Basically enable / disable hardware continuous sampling mode.
  1131. *
  1132. * Called with IIO device's lock held at postenable() or predisable() time.
  1133. *
  1134. * Return: Zero when successful, a negative error code otherwise.
  1135. */
  1136. static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
  1137. {
  1138. const struct iio_dev *indio_dev = dev_get_drvdata(
  1139. trig->dev.parent);
  1140. const struct zpa2326_private *priv = iio_priv(indio_dev);
  1141. int err;
  1142. if (!state) {
  1143. /*
  1144. * Switch trigger off : in case of failure, interrupt is left
  1145. * disabled in order to prevent handler from accessing released
  1146. * resources.
  1147. */
  1148. unsigned int val;
  1149. /*
  1150. * As device is working in continuous mode, handlers may be
  1151. * accessing resources we are currently freeing...
  1152. * Prevent this by disabling interrupt handlers and ensure
  1153. * the device will generate no more interrupts unless explicitly
  1154. * required to, i.e. by restoring back to default one shot mode.
  1155. */
  1156. disable_irq(priv->irq);
  1157. /*
  1158. * Disable continuous sampling mode to restore settings for
  1159. * one shot / direct sampling operations.
  1160. */
  1161. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
  1162. zpa2326_highest_frequency()->odr);
  1163. if (err)
  1164. return err;
  1165. /*
  1166. * Now that device won't generate interrupts on its own,
  1167. * acknowledge any currently active interrupts (may happen on
  1168. * rare occasions while stopping continuous mode).
  1169. */
  1170. err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
  1171. if (err < 0)
  1172. return err;
  1173. /*
  1174. * Re-enable interrupts only if we can guarantee the device will
  1175. * generate no more interrupts to prevent handlers from
  1176. * accessing released resources.
  1177. */
  1178. enable_irq(priv->irq);
  1179. zpa2326_dbg(indio_dev, "continuous mode stopped");
  1180. } else {
  1181. /*
  1182. * Switch trigger on : start continuous sampling at required
  1183. * frequency.
  1184. */
  1185. if (priv->waken) {
  1186. /* Enable interrupt if getting out of reset. */
  1187. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
  1188. (u8)
  1189. ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
  1190. if (err)
  1191. return err;
  1192. }
  1193. /* Enable continuous sampling at specified frequency. */
  1194. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
  1195. ZPA2326_CTRL_REG3_ENABLE_MEAS |
  1196. priv->frequency->odr);
  1197. if (err)
  1198. return err;
  1199. zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
  1200. priv->frequency->hz);
  1201. }
  1202. return 0;
  1203. }
  1204. static const struct iio_trigger_ops zpa2326_trigger_ops = {
  1205. .owner = THIS_MODULE,
  1206. .set_trigger_state = zpa2326_set_trigger_state,
  1207. };
  1208. /**
  1209. * zpa2326_init_trigger() - Create an interrupt driven / hardware trigger
  1210. * allowing to notify external devices a new sample is
  1211. * ready.
  1212. * @parent: Hardware sampling device @indio_dev is a child of.
  1213. * @indio_dev: The IIO device associated with the sampling hardware.
  1214. * @private: Internal private state related to @indio_dev.
  1215. * @irq: Optional interrupt line the hardware uses to notify new data
  1216. * samples are ready. Negative or zero values indicate no interrupts
  1217. * are available, meaning polling is required.
  1218. *
  1219. * Only relevant when DT declares a valid interrupt line.
  1220. *
  1221. * Return: Zero when successful, a negative error code otherwise.
  1222. */
  1223. static int zpa2326_init_managed_trigger(struct device *parent,
  1224. struct iio_dev *indio_dev,
  1225. struct zpa2326_private *private,
  1226. int irq)
  1227. {
  1228. struct iio_trigger *trigger;
  1229. int ret;
  1230. if (irq <= 0)
  1231. return 0;
  1232. trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
  1233. indio_dev->name, indio_dev->id);
  1234. if (!trigger)
  1235. return -ENOMEM;
  1236. /* Basic setup. */
  1237. trigger->dev.parent = parent;
  1238. trigger->ops = &zpa2326_trigger_ops;
  1239. private->trigger = trigger;
  1240. /* Register to triggers space. */
  1241. ret = devm_iio_trigger_register(parent, trigger);
  1242. if (ret)
  1243. dev_err(parent, "failed to register hardware trigger (%d)",
  1244. ret);
  1245. return ret;
  1246. }
  1247. static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
  1248. {
  1249. return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
  1250. }
  1251. static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
  1252. {
  1253. struct zpa2326_private *priv = iio_priv(indio_dev);
  1254. int freq;
  1255. int err;
  1256. /* Check if requested frequency is supported. */
  1257. for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
  1258. if (zpa2326_sampling_frequencies[freq].hz == hz)
  1259. break;
  1260. if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
  1261. return -EINVAL;
  1262. /* Don't allow changing frequency if buffered sampling is ongoing. */
  1263. err = iio_device_claim_direct_mode(indio_dev);
  1264. if (err)
  1265. return err;
  1266. priv->frequency = &zpa2326_sampling_frequencies[freq];
  1267. iio_device_release_direct_mode(indio_dev);
  1268. return 0;
  1269. }
  1270. /* Expose supported hardware sampling frequencies (Hz) through sysfs. */
  1271. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
  1272. static struct attribute *zpa2326_attributes[] = {
  1273. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  1274. NULL
  1275. };
  1276. static const struct attribute_group zpa2326_attribute_group = {
  1277. .attrs = zpa2326_attributes,
  1278. };
  1279. static int zpa2326_read_raw(struct iio_dev *indio_dev,
  1280. struct iio_chan_spec const *chan,
  1281. int *val,
  1282. int *val2,
  1283. long mask)
  1284. {
  1285. switch (mask) {
  1286. case IIO_CHAN_INFO_RAW:
  1287. return zpa2326_sample_oneshot(indio_dev, chan->type, val);
  1288. case IIO_CHAN_INFO_SCALE:
  1289. switch (chan->type) {
  1290. case IIO_PRESSURE:
  1291. /*
  1292. * Pressure resolution is 1/64 Pascal. Scale to kPascal
  1293. * as required by IIO ABI.
  1294. */
  1295. *val = 1;
  1296. *val2 = 64000;
  1297. return IIO_VAL_FRACTIONAL;
  1298. case IIO_TEMP:
  1299. /*
  1300. * Temperature follows the equation:
  1301. * Temp[degC] = Tempcode * 0.00649 - 176.83
  1302. * where:
  1303. * Tempcode is composed the raw sampled 16 bits.
  1304. *
  1305. * Hence, to produce a temperature in milli-degrees
  1306. * Celsius according to IIO ABI, we need to apply the
  1307. * following equation to raw samples:
  1308. * Temp[milli degC] = (Tempcode + Offset) * Scale
  1309. * where:
  1310. * Offset = -176.83 / 0.00649
  1311. * Scale = 0.00649 * 1000
  1312. */
  1313. *val = 6;
  1314. *val2 = 490000;
  1315. return IIO_VAL_INT_PLUS_MICRO;
  1316. default:
  1317. return -EINVAL;
  1318. }
  1319. case IIO_CHAN_INFO_OFFSET:
  1320. switch (chan->type) {
  1321. case IIO_TEMP:
  1322. *val = -17683000;
  1323. *val2 = 649;
  1324. return IIO_VAL_FRACTIONAL;
  1325. default:
  1326. return -EINVAL;
  1327. }
  1328. case IIO_CHAN_INFO_SAMP_FREQ:
  1329. *val = zpa2326_get_frequency(indio_dev);
  1330. return IIO_VAL_INT;
  1331. default:
  1332. return -EINVAL;
  1333. }
  1334. }
  1335. static int zpa2326_write_raw(struct iio_dev *indio_dev,
  1336. const struct iio_chan_spec *chan,
  1337. int val,
  1338. int val2,
  1339. long mask)
  1340. {
  1341. if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
  1342. return -EINVAL;
  1343. return zpa2326_set_frequency(indio_dev, val);
  1344. }
  1345. static const struct iio_chan_spec zpa2326_channels[] = {
  1346. [0] = {
  1347. .type = IIO_PRESSURE,
  1348. .scan_index = 0,
  1349. .scan_type = {
  1350. .sign = 'u',
  1351. .realbits = 24,
  1352. .storagebits = 32,
  1353. .endianness = IIO_LE,
  1354. },
  1355. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1356. BIT(IIO_CHAN_INFO_SCALE),
  1357. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  1358. },
  1359. [1] = {
  1360. .type = IIO_TEMP,
  1361. .scan_index = 1,
  1362. .scan_type = {
  1363. .sign = 's',
  1364. .realbits = 16,
  1365. .storagebits = 16,
  1366. .endianness = IIO_LE,
  1367. },
  1368. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1369. BIT(IIO_CHAN_INFO_SCALE) |
  1370. BIT(IIO_CHAN_INFO_OFFSET),
  1371. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  1372. },
  1373. [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
  1374. };
  1375. static const struct iio_info zpa2326_info = {
  1376. .driver_module = THIS_MODULE,
  1377. .attrs = &zpa2326_attribute_group,
  1378. .read_raw = zpa2326_read_raw,
  1379. .write_raw = zpa2326_write_raw,
  1380. };
  1381. static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
  1382. const char *name,
  1383. struct regmap *regmap)
  1384. {
  1385. struct iio_dev *indio_dev;
  1386. /* Allocate space to hold IIO device internal state. */
  1387. indio_dev = devm_iio_device_alloc(device,
  1388. sizeof(struct zpa2326_private));
  1389. if (!indio_dev)
  1390. return NULL;
  1391. /* Setup for userspace synchronous on demand sampling. */
  1392. indio_dev->modes = INDIO_DIRECT_MODE;
  1393. indio_dev->dev.parent = device;
  1394. indio_dev->channels = zpa2326_channels;
  1395. indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
  1396. indio_dev->name = name;
  1397. indio_dev->info = &zpa2326_info;
  1398. return indio_dev;
  1399. }
  1400. int zpa2326_probe(struct device *parent,
  1401. const char *name,
  1402. int irq,
  1403. unsigned int hwid,
  1404. struct regmap *regmap)
  1405. {
  1406. struct iio_dev *indio_dev;
  1407. struct zpa2326_private *priv;
  1408. int err;
  1409. unsigned int id;
  1410. indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
  1411. if (!indio_dev)
  1412. return -ENOMEM;
  1413. priv = iio_priv(indio_dev);
  1414. priv->vref = devm_regulator_get(parent, "vref");
  1415. if (IS_ERR(priv->vref))
  1416. return PTR_ERR(priv->vref);
  1417. priv->vdd = devm_regulator_get(parent, "vdd");
  1418. if (IS_ERR(priv->vdd))
  1419. return PTR_ERR(priv->vdd);
  1420. /* Set default hardware sampling frequency to highest rate supported. */
  1421. priv->frequency = zpa2326_highest_frequency();
  1422. /*
  1423. * Plug device's underlying bus abstraction : this MUST be set before
  1424. * registering interrupt handlers since an interrupt might happen if
  1425. * power up sequence is not properly applied.
  1426. */
  1427. priv->regmap = regmap;
  1428. err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
  1429. zpa2326_trigger_handler,
  1430. &zpa2326_buffer_setup_ops);
  1431. if (err)
  1432. return err;
  1433. err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
  1434. if (err)
  1435. return err;
  1436. err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
  1437. if (err)
  1438. return err;
  1439. /* Power up to check device ID and perform initial hardware setup. */
  1440. err = zpa2326_power_on(indio_dev, priv);
  1441. if (err)
  1442. return err;
  1443. /* Read id register to check we are talking to the right slave. */
  1444. err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
  1445. if (err)
  1446. goto sleep;
  1447. if (id != hwid) {
  1448. dev_err(parent, "found device with unexpected id %02x", id);
  1449. err = -ENODEV;
  1450. goto sleep;
  1451. }
  1452. err = zpa2326_config_oneshot(indio_dev, irq);
  1453. if (err)
  1454. goto sleep;
  1455. /* Setup done : go sleeping. Device will be awaken upon user request. */
  1456. err = zpa2326_sleep(indio_dev);
  1457. if (err)
  1458. goto poweroff;
  1459. dev_set_drvdata(parent, indio_dev);
  1460. zpa2326_init_runtime(parent);
  1461. err = iio_device_register(indio_dev);
  1462. if (err) {
  1463. zpa2326_fini_runtime(parent);
  1464. goto poweroff;
  1465. }
  1466. return 0;
  1467. sleep:
  1468. /* Put to sleep just in case power regulators are "dummy" ones. */
  1469. zpa2326_sleep(indio_dev);
  1470. poweroff:
  1471. zpa2326_power_off(indio_dev, priv);
  1472. return err;
  1473. }
  1474. EXPORT_SYMBOL_GPL(zpa2326_probe);
  1475. void zpa2326_remove(const struct device *parent)
  1476. {
  1477. struct iio_dev *indio_dev = dev_get_drvdata(parent);
  1478. iio_device_unregister(indio_dev);
  1479. zpa2326_fini_runtime(indio_dev->dev.parent);
  1480. zpa2326_sleep(indio_dev);
  1481. zpa2326_power_off(indio_dev, iio_priv(indio_dev));
  1482. }
  1483. EXPORT_SYMBOL_GPL(zpa2326_remove);
  1484. MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
  1485. MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
  1486. MODULE_LICENSE("GPL v2");