zpa2326.c 48 KB

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